4 * Based on code written by:
8 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <linux/ctype.h>
30 #include <linux/types.h>
32 #ifdef CONFIG_OF_LIBFDT
34 #include <asm/global_data.h>
37 #include <fdt_support.h>
39 #define MAX_LEVEL 32 /* how deeply nested we will go */
40 #define SCRATCHPAD 1024 /* bytes of scratchpad memory */
43 * Global data (for the gd->bd)
45 DECLARE_GLOBAL_DATA_PTR;
50 static char data[SCRATCHPAD];
54 * Function prototypes/declarations.
56 static int fdt_valid(void);
57 static void print_data(const void *data, int len);
59 static int findnodeoffset(const char *pathp)
63 if (strcmp(pathp, "/") == 0) {
66 nodeoffset = fdt_path_offset (fdt, pathp);
69 * Not found or something else bad happened.
71 printf ("findnodeoffset() libfdt: %s\n", fdt_strerror(nodeoffset));
78 * Flattened Device Tree command, see the help for parameter definitions.
80 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
83 printf ("Usage:\n%s\n", cmdtp->usage);
87 /********************************************************************
88 * Set the address of the fdt
89 ********************************************************************/
90 if (argv[1][0] == 'a') {
92 * Set the address [and length] of the fdt.
94 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
104 * Optional new length
106 len = simple_strtoul(argv[3], NULL, 16);
107 if (len < fdt_totalsize(fdt)) {
108 printf ("New length %d < existing length %d, ignoring.\n",
109 len, fdt_totalsize(fdt));
112 * Open in place with a new length.
114 err = fdt_open_into(fdt, fdt, len);
116 printf ("libfdt fdt_open_into(): %s\n", fdt_strerror(err));
121 /********************************************************************
123 ********************************************************************/
124 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
125 struct fdt_header *newaddr;
130 printf ("Usage:\n%s\n", cmdtp->usage);
135 * Set the address and length of the fdt.
137 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
142 newaddr = (struct fdt_header *)simple_strtoul(argv[3], NULL, 16);
145 * If the user specifies a length, use that. Otherwise use the
149 len = fdt_totalsize(fdt);
151 len = simple_strtoul(argv[4], NULL, 16);
152 if (len < fdt_totalsize(fdt)) {
153 printf ("New length 0x%X < existing length 0x%X, aborting.\n",
154 len, fdt_totalsize(fdt));
160 * Copy to the new location.
162 err = fdt_open_into(fdt, newaddr, len);
164 printf ("libfdt fdt_open_into(): %s\n", fdt_strerror(err));
169 /********************************************************************
171 ********************************************************************/
172 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
173 char *pathp; /* path */
174 char *nodep; /* new node to add */
175 int nodeoffset; /* node offset from libfdt */
179 * Parameters: Node path, new node to be appended to the path.
182 printf ("Usage:\n%s\n", cmdtp->usage);
189 nodeoffset = findnodeoffset(pathp);
190 if (nodeoffset < 0) {
192 * Not found or something else bad happened.
196 err = fdt_add_subnode(fdt, nodeoffset, nodep);
198 printf ("libfdt fdt_add_subnode(): %s\n", fdt_strerror(err));
202 /********************************************************************
203 * Set the value of a property in the fdt.
204 ********************************************************************/
205 } else if (argv[1][0] == 's') {
206 char *pathp; /* path */
207 char *prop; /* property */
208 struct fdt_property *nodep; /* node struct pointer */
209 char *newval; /* value from the user (as a string) */
210 char *vp; /* temporary value pointer */
211 char *cp; /* temporary char pointer */
212 int nodeoffset; /* node offset from libfdt */
213 int len; /* new length of the property */
214 int oldlen; /* original length of the property */
215 unsigned long tmp; /* holds converted values */
216 int ret; /* return value */
219 * Parameters: Node path, property, value.
222 printf ("Usage:\n%s\n", cmdtp->usage);
230 nodeoffset = findnodeoffset(pathp);
231 if (nodeoffset < 0) {
233 * Not found or something else bad happened.
238 * Convert the new property
241 if (*newval == '<') {
243 * Bigger values than bytes.
247 while ((*newval != '>') && (*newval != '\0')) {
249 tmp = simple_strtoul(cp, &newval, 16);
250 if ((newval - cp) <= 2) {
254 } else if ((newval - cp) <= 4) {
255 *(uint16_t *)vp = __cpu_to_be16(tmp);
258 } else if ((newval - cp) <= 8) {
259 *(uint32_t *)vp = __cpu_to_be32(tmp);
263 printf("Sorry, I could not convert \"%s\"\n", cp);
266 while (*newval == ' ')
269 if (*newval != '>') {
270 printf("Unexpected character '%c'\n", *newval);
273 } else if (*newval == '[') {
275 * Byte stream. Convert the values.
279 while ((*newval != ']') && (*newval != '\0')) {
280 tmp = simple_strtoul(newval, &newval, 16);
283 while (*newval == ' ')
286 if (*newval != ']') {
287 printf("Unexpected character '%c'\n", *newval);
292 * Assume it is a string. Copy it into our data area for
293 * convenience (including the terminating '\0').
295 len = strlen(newval) + 1;
296 strcpy(data, newval);
299 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
301 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
305 /********************************************************************
306 * Print (recursive) / List (single level)
307 ********************************************************************/
308 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
310 * Recursively print (a portion of) the fdt.
312 static int offstack[MAX_LEVEL];
313 static char tabs[MAX_LEVEL+1] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
314 int depth = MAX_LEVEL; /* how deep to print */
315 char *pathp; /* path */
316 char *prop; /* property */
317 void *nodep; /* property node pointer */
318 int nodeoffset; /* node offset from libfdt */
319 int nextoffset; /* next node offset from libfdt */
320 uint32_t tag; /* tag */
321 int len; /* length of the property */
322 int level = 0; /* keep track of nesting level */
325 * list is an alias for print, but limited to 1 level
327 if (argv[1][0] == 'l') {
332 * Get the starting path. The root node is an oddball,
333 * the offset is zero and has no name.
341 nodeoffset = findnodeoffset(pathp);
342 if (nodeoffset < 0) {
344 * Not found or something else bad happened.
349 * The user passed in a property as well as node path. Print only
350 * the given property and then return.
353 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
355 printf("%s %s\n", pathp, prop); /* no property value */
357 } else if (len > 0) {
359 print_data (nodep, len);
363 printf ("libfdt fdt_getprop(): %s\n", fdt_strerror(len));
369 * The user passed in a node path and no property, print the node
372 offstack[0] = nodeoffset;
375 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
379 printf("%s%s {\n", &tabs[MAX_LEVEL - level], pathp);
381 offstack[level] = nodeoffset;
382 if (level >= MAX_LEVEL) {
383 printf("Aaaiii <splat> nested too deep. Aborting.\n");
390 printf("%s};\n", &tabs[MAX_LEVEL - level]);
392 level = -1; /* exit the loop */
396 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
398 printf ("libfdt fdt_getprop(): %s\n", fdt_strerror(len));
400 } else if (len == 0) {
401 /* the property has no value */
403 printf("%s%s;\n", &tabs[MAX_LEVEL - level], pathp);
406 printf("%s%s=", &tabs[MAX_LEVEL - level], pathp);
407 print_data (nodep, len);
418 printf("Unknown tag 0x%08X\n", tag);
421 nodeoffset = nextoffset;
424 /********************************************************************
425 * Remove a property/node
426 ********************************************************************/
427 } else if (argv[1][0] == 'r') {
428 int nodeoffset; /* node offset from libfdt */
432 * Get the path. The root node is an oddball, the offset
433 * is zero and has no name.
435 nodeoffset = findnodeoffset(argv[2]);
436 if (nodeoffset < 0) {
438 * Not found or something else bad happened.
443 * Do the delete. A fourth parameter means delete a property,
444 * otherwise delete the node.
447 err = fdt_delprop(fdt, nodeoffset, argv[3]);
449 printf("libfdt fdt_delprop(): %s\n", fdt_strerror(err));
453 err = fdt_del_node(fdt, nodeoffset);
455 printf("libfdt fdt_del_node(): %s\n", fdt_strerror(err));
460 /********************************************************************
461 * Create a chosen node
462 ********************************************************************/
463 } else if (argv[1][0] == 'c') {
464 fdt_chosen(fdt, 0, 0, 1);
466 /********************************************************************
467 * Create a u-boot-env node
468 ********************************************************************/
469 } else if (argv[1][0] == 'e') {
472 /********************************************************************
474 ********************************************************************/
475 } else if (argv[1][0] == 'b') {
478 /********************************************************************
479 * Unrecognized command
480 ********************************************************************/
482 printf ("Usage:\n%s\n", cmdtp->usage);
489 /********************************************************************/
491 static int fdt_valid(void)
496 printf ("The address of the fdt is invalid (NULL).\n");
500 err = fdt_check_header(fdt);
502 return 1; /* valid */
505 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
507 * Be more informative on bad version.
509 if (err == -FDT_ERR_BADVERSION) {
510 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
511 printf (" - too old, fdt $d < %d",
512 fdt_version(fdt), FDT_FIRST_SUPPORTED_VERSION);
515 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
516 printf (" - too new, fdt $d > %d",
517 fdt_version(fdt), FDT_LAST_SUPPORTED_VERSION);
528 /********************************************************************/
531 * OF flat tree handling
537 static int is_printable_string(const void *data, int len)
539 const char *s = data;
541 /* zero length is not */
545 /* must terminate with zero */
546 if (s[len - 1] != '\0')
549 /* printable or a null byte (concatenated strings) */
550 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
552 * If we see a null, there are three possibilities:
553 * 1) If len == 1, it is the end of the string, printable
554 * 2) Next character also a null, not printable.
555 * 3) Next character not a null, continue to check.
567 /* Not the null termination, or not done yet: not printable */
568 if (*s != '\0' || (len != 0))
574 static void print_data(const void *data, int len)
579 /* no data, don't print */
584 * It is a string, but it may have multiple strings (embedded '\0's).
586 if (is_printable_string(data, len)) {
593 j += strlen(data) + 1;
594 data += strlen(data) + 1;
602 printf("<%02x>", (*(u8 *) data) & 0xff);
604 case 2: /* half-word */
605 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
608 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
610 case 8: /* double-word */
612 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
614 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
616 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
619 default: /* anything else... hexdump */
621 for (j = 0, s = data; j < len; j++)
622 printf("%02x%s", s[j], j < len - 1 ? " " : "");
629 /********************************************************************/
633 "fdt - flattened device tree utility commands\n",
634 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
635 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
636 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
637 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
638 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
639 "fdt mknode <path> <node> - Create a new node after <path>\n"
640 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
641 "fdt chosen - Add/update the \"/chosen\" branch in the tree\n"
642 #ifdef CONFIG_OF_HAS_UBOOT_ENV
643 "fdt env - Add/replace the \"/u-boot-env\" branch in the tree\n"
645 #ifdef CONFIG_OF_HAS_BD_T
646 "fdt bd_t - Add/replace the \"/bd_t\" branch in the tree\n"
649 " * If the property you are setting/printing has a '#' character,\n"
650 " you MUST escape it with a \\ character or quote it with \" or\n"
651 " it will be ignored as a comment.\n"
652 " * If the value has spaces in it, you MUST escape the spaces with\n"
653 " \\ characters or quote it with \"\"\n"
654 "Examples: fdt print / # print the whole tree\n"
655 " fdt print /cpus \"#address-cells\"\n"
656 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
659 #endif /* CONFIG_OF_LIBFDT */