Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com |
| 4 | * Based on code written by: |
| 5 | * Pantelis Antoniou <pantelis.antoniou@gmail.com> and |
| 6 | * Matthew McClintock <msm@freescale.com> |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <linux/ctype.h> |
| 30 | #include <linux/types.h> |
| 31 | |
| 32 | #ifdef CONFIG_OF_LIBFDT |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 33 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 34 | #include <asm/global_data.h> |
| 35 | #include <fdt.h> |
| 36 | #include <libfdt.h> |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 37 | #include <fdt_support.h> |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 38 | |
| 39 | #define MAX_LEVEL 32 /* how deeply nested we will go */ |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 40 | #define SCRATCHPAD 1024 /* bytes of scratchpad memory */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 41 | |
| 42 | /* |
| 43 | * Global data (for the gd->bd) |
| 44 | */ |
| 45 | DECLARE_GLOBAL_DATA_PTR; |
| 46 | |
| 47 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 48 | * Function prototypes/declarations. |
| 49 | */ |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 50 | #ifdef CONFIG_OF_BOARD_SETUP |
| 51 | void ft_board_setup(void *blob, bd_t *bd); |
| 52 | #endif |
| 53 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 54 | static int fdt_valid(void); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 55 | static int fdt_parse_prop(char *pathp, char *prop, char *newval, |
| 56 | char *data, int *len); |
| 57 | static int fdt_print(char *pathp, char *prop, int depth); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 58 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 59 | /* |
| 60 | * Flattened Device Tree command, see the help for parameter definitions. |
| 61 | */ |
| 62 | int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]) |
| 63 | { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 64 | if (argc < 2) { |
| 65 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 66 | return 1; |
| 67 | } |
| 68 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 69 | /******************************************************************** |
| 70 | * Set the address of the fdt |
| 71 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 72 | if (argv[1][0] == 'a') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 73 | /* |
| 74 | * Set the address [and length] of the fdt. |
| 75 | */ |
| 76 | fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16); |
| 77 | |
| 78 | if (!fdt_valid()) { |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | if (argc >= 4) { |
| 83 | int len; |
| 84 | int err; |
| 85 | /* |
| 86 | * Optional new length |
| 87 | */ |
| 88 | len = simple_strtoul(argv[3], NULL, 16); |
| 89 | if (len < fdt_totalsize(fdt)) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 90 | printf ("New length %d < existing length %d, " |
| 91 | "ignoring.\n", |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 92 | len, fdt_totalsize(fdt)); |
| 93 | } else { |
| 94 | /* |
| 95 | * Open in place with a new length. |
| 96 | */ |
| 97 | err = fdt_open_into(fdt, fdt, len); |
| 98 | if (err != 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 99 | printf ("libfdt fdt_open_into(): %s\n", |
| 100 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /******************************************************************** |
| 106 | * Move the fdt |
| 107 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 108 | } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 109 | struct fdt_header *newaddr; |
| 110 | int len; |
| 111 | int err; |
| 112 | |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 113 | if (argc < 4) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 114 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * Set the address and length of the fdt. |
| 120 | */ |
| 121 | fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16); |
| 122 | if (!fdt_valid()) { |
| 123 | return 1; |
| 124 | } |
| 125 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 126 | newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 127 | |
| 128 | /* |
| 129 | * If the user specifies a length, use that. Otherwise use the |
| 130 | * current length. |
| 131 | */ |
| 132 | if (argc <= 4) { |
| 133 | len = fdt_totalsize(fdt); |
| 134 | } else { |
| 135 | len = simple_strtoul(argv[4], NULL, 16); |
| 136 | if (len < fdt_totalsize(fdt)) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 137 | printf ("New length 0x%X < existing length " |
| 138 | "0x%X, aborting.\n", |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 139 | len, fdt_totalsize(fdt)); |
| 140 | return 1; |
| 141 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Copy to the new location. |
| 146 | */ |
| 147 | err = fdt_open_into(fdt, newaddr, len); |
| 148 | if (err != 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 149 | printf ("libfdt fdt_open_into(): %s\n", |
| 150 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | fdt = newaddr; |
| 154 | |
| 155 | /******************************************************************** |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 156 | * Make a new node |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 157 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 158 | } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) { |
| 159 | char *pathp; /* path */ |
| 160 | char *nodep; /* new node to add */ |
| 161 | int nodeoffset; /* node offset from libfdt */ |
| 162 | int err; |
| 163 | |
| 164 | /* |
| 165 | * Parameters: Node path, new node to be appended to the path. |
| 166 | */ |
| 167 | if (argc < 4) { |
| 168 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 169 | return 1; |
| 170 | } |
| 171 | |
| 172 | pathp = argv[2]; |
| 173 | nodep = argv[3]; |
| 174 | |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 175 | nodeoffset = fdt_find_node_by_path (fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 176 | if (nodeoffset < 0) { |
| 177 | /* |
| 178 | * Not found or something else bad happened. |
| 179 | */ |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 180 | printf ("libfdt fdt_find_node_by_path() returned %s\n", |
| 181 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 182 | return 1; |
| 183 | } |
| 184 | err = fdt_add_subnode(fdt, nodeoffset, nodep); |
| 185 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 186 | printf ("libfdt fdt_add_subnode(): %s\n", |
| 187 | fdt_strerror(err)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | /******************************************************************** |
| 192 | * Set the value of a property in the fdt. |
| 193 | ********************************************************************/ |
| 194 | } else if (argv[1][0] == 's') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 195 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 196 | char *prop; /* property */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 197 | char *newval; /* value from the user (as a string) */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 198 | int nodeoffset; /* node offset from libfdt */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 199 | static char data[SCRATCHPAD]; /* storage for the property */ |
| 200 | int len; /* new length of the property */ |
| 201 | int ret; /* return value */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 202 | |
| 203 | /* |
| 204 | * Parameters: Node path, property, value. |
| 205 | */ |
| 206 | if (argc < 5) { |
| 207 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 208 | return 1; |
| 209 | } |
| 210 | |
| 211 | pathp = argv[2]; |
| 212 | prop = argv[3]; |
| 213 | newval = argv[4]; |
| 214 | |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 215 | nodeoffset = fdt_find_node_by_path (fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 216 | if (nodeoffset < 0) { |
| 217 | /* |
| 218 | * Not found or something else bad happened. |
| 219 | */ |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 220 | printf ("libfdt fdt_find_node_by_path() returned %s\n", |
| 221 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 222 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 223 | } |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 224 | ret = fdt_parse_prop(pathp, prop, newval, data, &len); |
| 225 | if (ret != 0) |
| 226 | return ret; |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 227 | |
| 228 | ret = fdt_setprop(fdt, nodeoffset, prop, data, len); |
| 229 | if (ret < 0) { |
| 230 | printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret)); |
| 231 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /******************************************************************** |
| 235 | * Print (recursive) / List (single level) |
| 236 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 237 | } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 238 | int depth = MAX_LEVEL; /* how deep to print */ |
| 239 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 240 | char *prop; /* property */ |
| 241 | int ret; /* return value */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 242 | |
| 243 | /* |
| 244 | * list is an alias for print, but limited to 1 level |
| 245 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 246 | if (argv[1][0] == 'l') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 247 | depth = 1; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Get the starting path. The root node is an oddball, |
| 252 | * the offset is zero and has no name. |
| 253 | */ |
| 254 | pathp = argv[2]; |
| 255 | if (argc > 3) |
| 256 | prop = argv[3]; |
| 257 | else |
| 258 | prop = NULL; |
| 259 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 260 | ret = fdt_print(pathp, prop, depth); |
| 261 | if (ret != 0) |
| 262 | return ret; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 263 | |
| 264 | /******************************************************************** |
| 265 | * Remove a property/node |
| 266 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 267 | } else if (argv[1][0] == 'r') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 268 | int nodeoffset; /* node offset from libfdt */ |
| 269 | int err; |
| 270 | |
| 271 | /* |
| 272 | * Get the path. The root node is an oddball, the offset |
| 273 | * is zero and has no name. |
| 274 | */ |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 275 | nodeoffset = fdt_find_node_by_path (fdt, argv[2]); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 276 | if (nodeoffset < 0) { |
| 277 | /* |
| 278 | * Not found or something else bad happened. |
| 279 | */ |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 280 | printf ("libfdt fdt_find_node_by_path() returned %s\n", |
| 281 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 282 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 283 | } |
| 284 | /* |
| 285 | * Do the delete. A fourth parameter means delete a property, |
| 286 | * otherwise delete the node. |
| 287 | */ |
| 288 | if (argc > 3) { |
| 289 | err = fdt_delprop(fdt, nodeoffset, argv[3]); |
| 290 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 291 | printf("libfdt fdt_delprop(): %s\n", |
| 292 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 293 | return err; |
| 294 | } |
| 295 | } else { |
| 296 | err = fdt_del_node(fdt, nodeoffset); |
| 297 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 298 | printf("libfdt fdt_del_node(): %s\n", |
| 299 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 300 | return err; |
| 301 | } |
| 302 | } |
| 303 | |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 304 | #ifdef CONFIG_OF_BOARD_SETUP |
| 305 | /******************************************************************** |
| 306 | * Call the board-specific fixup routine |
| 307 | ********************************************************************/ |
| 308 | } else if (argv[1][0] == 'b') { |
| 309 | ft_board_setup(fdt, gd->bd); |
| 310 | #endif |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 311 | /******************************************************************** |
| 312 | * Create a chosen node |
| 313 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 314 | } else if (argv[1][0] == 'c') { |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 315 | fdt_chosen(fdt, 0, 0, 1); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 316 | |
| 317 | /******************************************************************** |
| 318 | * Create a u-boot-env node |
| 319 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 320 | } else if (argv[1][0] == 'e') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 321 | fdt_env(fdt); |
| 322 | |
| 323 | /******************************************************************** |
| 324 | * Create a bd_t node |
| 325 | ********************************************************************/ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 326 | } else if (argv[1][0] == 'b') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 327 | fdt_bd_t(fdt); |
| 328 | |
| 329 | /******************************************************************** |
| 330 | * Unrecognized command |
| 331 | ********************************************************************/ |
| 332 | } else { |
| 333 | printf ("Usage:\n%s\n", cmdtp->usage); |
| 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 340 | /****************************************************************************/ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 341 | |
| 342 | static int fdt_valid(void) |
| 343 | { |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 344 | int err; |
| 345 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 346 | if (fdt == NULL) { |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 347 | printf ("The address of the fdt is invalid (NULL).\n"); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 348 | return 0; |
| 349 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 350 | |
| 351 | err = fdt_check_header(fdt); |
| 352 | if (err == 0) |
| 353 | return 1; /* valid */ |
| 354 | |
| 355 | if (err < 0) { |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 356 | printf("libfdt fdt_check_header(): %s", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 357 | /* |
| 358 | * Be more informative on bad version. |
| 359 | */ |
| 360 | if (err == -FDT_ERR_BADVERSION) { |
| 361 | if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) { |
| 362 | printf (" - too old, fdt $d < %d", |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 363 | fdt_version(fdt), |
| 364 | FDT_FIRST_SUPPORTED_VERSION); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 365 | fdt = NULL; |
| 366 | } |
| 367 | if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) { |
| 368 | printf (" - too new, fdt $d > %d", |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 369 | fdt_version(fdt), |
| 370 | FDT_LAST_SUPPORTED_VERSION); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 371 | fdt = NULL; |
| 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | printf("\n"); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | return 1; |
| 379 | } |
| 380 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 381 | /****************************************************************************/ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 382 | |
| 383 | /* |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 384 | * Parse the user's input, partially heuristic. Valid formats: |
| 385 | * <00> - hex byte |
| 386 | * <0011> - hex half word (16 bits) |
| 387 | * <00112233> - hex word (32 bits) |
| 388 | * - hex double words (64 bits) are not supported, must use |
| 389 | * a byte stream instead. |
| 390 | * [00 11 22 .. nn] - byte stream |
| 391 | * "string" - If the the value doesn't start with "<" or "[", it is |
| 392 | * treated as a string. Note that the quotes are |
| 393 | * stripped by the parser before we get the string. |
| 394 | */ |
| 395 | static int fdt_parse_prop(char *pathp, char *prop, char *newval, |
| 396 | char *data, int *len) |
| 397 | { |
| 398 | char *cp; /* temporary char pointer */ |
| 399 | unsigned long tmp; /* holds converted values */ |
| 400 | |
| 401 | if (*newval == '<') { |
| 402 | /* |
| 403 | * Bigger values than bytes. |
| 404 | */ |
| 405 | *len = 0; |
| 406 | newval++; |
| 407 | while ((*newval != '>') && (*newval != '\0')) { |
| 408 | cp = newval; |
| 409 | tmp = simple_strtoul(cp, &newval, 16); |
| 410 | if ((newval - cp) <= 2) { |
| 411 | *data = tmp & 0xFF; |
| 412 | data += 1; |
| 413 | *len += 1; |
| 414 | } else if ((newval - cp) <= 4) { |
| 415 | *(uint16_t *)data = __cpu_to_be16(tmp); |
| 416 | data += 2; |
| 417 | *len += 2; |
| 418 | } else if ((newval - cp) <= 8) { |
| 419 | *(uint32_t *)data = __cpu_to_be32(tmp); |
| 420 | data += 4; |
| 421 | *len += 4; |
| 422 | } else { |
| 423 | printf("Sorry, I could not convert \"%s\"\n", |
| 424 | cp); |
| 425 | return 1; |
| 426 | } |
| 427 | while (*newval == ' ') |
| 428 | newval++; |
| 429 | } |
| 430 | if (*newval != '>') { |
| 431 | printf("Unexpected character '%c'\n", *newval); |
| 432 | return 1; |
| 433 | } |
| 434 | } else if (*newval == '[') { |
| 435 | /* |
| 436 | * Byte stream. Convert the values. |
| 437 | */ |
| 438 | *len = 0; |
| 439 | newval++; |
| 440 | while ((*newval != ']') && (*newval != '\0')) { |
| 441 | tmp = simple_strtoul(newval, &newval, 16); |
| 442 | *data++ = tmp & 0xFF; |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 443 | *len = *len + 1; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 444 | while (*newval == ' ') |
| 445 | newval++; |
| 446 | } |
| 447 | if (*newval != ']') { |
| 448 | printf("Unexpected character '%c'\n", *newval); |
| 449 | return 1; |
| 450 | } |
| 451 | } else { |
| 452 | /* |
| 453 | * Assume it is a string. Copy it into our data area for |
| 454 | * convenience (including the terminating '\0'). |
| 455 | */ |
| 456 | *len = strlen(newval) + 1; |
| 457 | strcpy(data, newval); |
| 458 | } |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | /****************************************************************************/ |
| 463 | |
| 464 | /* |
| 465 | * Heuristic to guess if this is a string or concatenated strings. |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 466 | */ |
| 467 | |
| 468 | static int is_printable_string(const void *data, int len) |
| 469 | { |
| 470 | const char *s = data; |
| 471 | |
| 472 | /* zero length is not */ |
| 473 | if (len == 0) |
| 474 | return 0; |
| 475 | |
| 476 | /* must terminate with zero */ |
| 477 | if (s[len - 1] != '\0') |
| 478 | return 0; |
| 479 | |
| 480 | /* printable or a null byte (concatenated strings) */ |
| 481 | while (((*s == '\0') || isprint(*s)) && (len > 0)) { |
| 482 | /* |
| 483 | * If we see a null, there are three possibilities: |
| 484 | * 1) If len == 1, it is the end of the string, printable |
| 485 | * 2) Next character also a null, not printable. |
| 486 | * 3) Next character not a null, continue to check. |
| 487 | */ |
| 488 | if (s[0] == '\0') { |
| 489 | if (len == 1) |
| 490 | return 1; |
| 491 | if (s[1] == '\0') |
| 492 | return 0; |
| 493 | } |
| 494 | s++; |
| 495 | len--; |
| 496 | } |
| 497 | |
| 498 | /* Not the null termination, or not done yet: not printable */ |
| 499 | if (*s != '\0' || (len != 0)) |
| 500 | return 0; |
| 501 | |
| 502 | return 1; |
| 503 | } |
| 504 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 505 | |
| 506 | /* |
| 507 | * Print the property in the best format, a heuristic guess. Print as |
| 508 | * a string, concatenated strings, a byte, word, double word, or (if all |
| 509 | * else fails) it is printed as a stream of bytes. |
| 510 | */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 511 | static void print_data(const void *data, int len) |
| 512 | { |
| 513 | int j; |
| 514 | const u8 *s; |
| 515 | |
| 516 | /* no data, don't print */ |
| 517 | if (len == 0) |
| 518 | return; |
| 519 | |
| 520 | /* |
| 521 | * It is a string, but it may have multiple strings (embedded '\0's). |
| 522 | */ |
| 523 | if (is_printable_string(data, len)) { |
| 524 | puts("\""); |
| 525 | j = 0; |
| 526 | while (j < len) { |
| 527 | if (j > 0) |
| 528 | puts("\", \""); |
| 529 | puts(data); |
| 530 | j += strlen(data) + 1; |
| 531 | data += strlen(data) + 1; |
| 532 | } |
| 533 | puts("\""); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | switch (len) { |
| 538 | case 1: /* byte */ |
| 539 | printf("<%02x>", (*(u8 *) data) & 0xff); |
| 540 | break; |
| 541 | case 2: /* half-word */ |
| 542 | printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff); |
| 543 | break; |
| 544 | case 4: /* word */ |
| 545 | printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU); |
| 546 | break; |
| 547 | case 8: /* double-word */ |
| 548 | #if __WORDSIZE == 64 |
| 549 | printf("<%016llx>", be64_to_cpu(*(uint64_t *) data)); |
| 550 | #else |
| 551 | printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU); |
| 552 | data += 4; |
| 553 | printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU); |
| 554 | #endif |
| 555 | break; |
| 556 | default: /* anything else... hexdump */ |
| 557 | printf("["); |
| 558 | for (j = 0, s = data; j < len; j++) |
| 559 | printf("%02x%s", s[j], j < len - 1 ? " " : ""); |
| 560 | printf("]"); |
| 561 | |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 566 | /****************************************************************************/ |
| 567 | |
| 568 | /* |
| 569 | * Recursively print (a portion of) the fdt. The depth parameter |
| 570 | * determines how deeply nested the fdt is printed. |
| 571 | */ |
| 572 | static int fdt_print(char *pathp, char *prop, int depth) |
| 573 | { |
| 574 | static int offstack[MAX_LEVEL]; |
| 575 | static char tabs[MAX_LEVEL+1] = |
| 576 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" |
| 577 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; |
| 578 | void *nodep; /* property node pointer */ |
| 579 | int nodeoffset; /* node offset from libfdt */ |
| 580 | int nextoffset; /* next node offset from libfdt */ |
| 581 | uint32_t tag; /* tag */ |
| 582 | int len; /* length of the property */ |
| 583 | int level = 0; /* keep track of nesting level */ |
| 584 | |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 585 | nodeoffset = fdt_find_node_by_path (fdt, pathp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 586 | if (nodeoffset < 0) { |
| 587 | /* |
| 588 | * Not found or something else bad happened. |
| 589 | */ |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 590 | printf ("libfdt fdt_find_node_by_path() returned %s\n", |
| 591 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 592 | return 1; |
| 593 | } |
| 594 | /* |
| 595 | * The user passed in a property as well as node path. |
| 596 | * Print only the given property and then return. |
| 597 | */ |
| 598 | if (prop) { |
| 599 | nodep = fdt_getprop (fdt, nodeoffset, prop, &len); |
| 600 | if (len == 0) { |
| 601 | /* no property value */ |
| 602 | printf("%s %s\n", pathp, prop); |
| 603 | return 0; |
| 604 | } else if (len > 0) { |
| 605 | printf("%s=", prop); |
| 606 | print_data (nodep, len); |
| 607 | printf("\n"); |
| 608 | return 0; |
| 609 | } else { |
| 610 | printf ("libfdt fdt_getprop(): %s\n", |
| 611 | fdt_strerror(len)); |
| 612 | return 1; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | /* |
| 617 | * The user passed in a node path and no property, |
| 618 | * print the node and all subnodes. |
| 619 | */ |
| 620 | offstack[0] = nodeoffset; |
| 621 | |
| 622 | while(level >= 0) { |
| 623 | tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp); |
| 624 | switch(tag) { |
| 625 | case FDT_BEGIN_NODE: |
| 626 | if(level <= depth) |
| 627 | printf("%s%s {\n", |
| 628 | &tabs[MAX_LEVEL - level], pathp); |
| 629 | level++; |
| 630 | offstack[level] = nodeoffset; |
| 631 | if (level >= MAX_LEVEL) { |
| 632 | printf("Aaaiii <splat> nested too deep. " |
| 633 | "Aborting.\n"); |
| 634 | return 1; |
| 635 | } |
| 636 | break; |
| 637 | case FDT_END_NODE: |
| 638 | level--; |
| 639 | if(level <= depth) |
| 640 | printf("%s};\n", &tabs[MAX_LEVEL - level]); |
| 641 | if (level == 0) { |
| 642 | level = -1; /* exit the loop */ |
| 643 | } |
| 644 | break; |
| 645 | case FDT_PROP: |
| 646 | nodep = fdt_getprop (fdt, offstack[level], pathp, &len); |
| 647 | if (len < 0) { |
| 648 | printf ("libfdt fdt_getprop(): %s\n", |
| 649 | fdt_strerror(len)); |
| 650 | return 1; |
| 651 | } else if (len == 0) { |
| 652 | /* the property has no value */ |
| 653 | if(level <= depth) |
| 654 | printf("%s%s;\n", |
| 655 | &tabs[MAX_LEVEL - level], |
| 656 | pathp); |
| 657 | } else { |
| 658 | if(level <= depth) { |
| 659 | printf("%s%s=", |
| 660 | &tabs[MAX_LEVEL - level], |
| 661 | pathp); |
| 662 | print_data (nodep, len); |
| 663 | printf(";\n"); |
| 664 | } |
| 665 | } |
| 666 | break; |
| 667 | case FDT_NOP: |
| 668 | break; |
| 669 | case FDT_END: |
| 670 | return 1; |
| 671 | default: |
| 672 | if(level <= depth) |
| 673 | printf("Unknown tag 0x%08X\n", tag); |
| 674 | return 1; |
| 675 | } |
| 676 | nodeoffset = nextoffset; |
| 677 | } |
| 678 | return 0; |
| 679 | } |
| 680 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 681 | /********************************************************************/ |
| 682 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 683 | U_BOOT_CMD( |
| 684 | fdt, 5, 0, do_fdt, |
| 685 | "fdt - flattened device tree utility commands\n", |
| 686 | "addr <addr> [<length>] - Set the fdt location to <addr>\n" |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 687 | #ifdef CONFIG_OF_BOARD_SETUP |
| 688 | "fdt boardsetup - Do board-specific set up\n" |
| 689 | #endif |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 690 | "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n" |
| 691 | "fdt print <path> [<prop>] - Recursive print starting at <path>\n" |
| 692 | "fdt list <path> [<prop>] - Print one level starting at <path>\n" |
| 693 | "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n" |
| 694 | "fdt mknode <path> <node> - Create a new node after <path>\n" |
| 695 | "fdt rm <path> [<prop>] - Delete the node or <property>\n" |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 696 | "fdt chosen - Add/update the /chosen branch in the tree\n" |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 697 | #ifdef CONFIG_OF_HAS_UBOOT_ENV |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 698 | "fdt env - Add/replace the /u-boot-env branch in the tree\n" |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 699 | #endif |
| 700 | #ifdef CONFIG_OF_HAS_BD_T |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 701 | "fdt bd_t - Add/replace the /bd_t branch in the tree\n" |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 702 | #endif |
| 703 | "Hints:\n" |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame^] | 704 | " If the property you are setting/printing has a '#' character or spaces,\n" |
| 705 | " you MUST escape it with a \\ character or quote it with \".\n" |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 706 | "Examples: fdt print / # print the whole tree\n" |
| 707 | " fdt print /cpus \"#address-cells\"\n" |
| 708 | " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n" |
| 709 | ); |
| 710 | |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 711 | #endif /* CONFIG_OF_LIBFDT */ |