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);
61 * Flattened Device Tree command, see the help for parameter definitions.
63 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
68 printf ("Usage:\n%s\n", cmdtp->usage);
73 * Figure out which subcommand was given
76 /********************************************************************
77 * Set the address of the fdt
78 ********************************************************************/
81 * Set the address [and length] of the fdt.
83 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
95 len = simple_strtoul(argv[3], NULL, 16);
96 if (len < fdt_totalsize(fdt)) {
97 printf ("New length %d < existing length %d, ignoring.\n",
98 len, fdt_totalsize(fdt));
101 * Open in place with a new length.
103 err = fdt_open_into(fdt, fdt, len);
105 printf ("libfdt: %s\n", fdt_strerror(err));
110 /********************************************************************
112 ********************************************************************/
113 } else if (op == 'm') {
114 struct fdt_header *newaddr;
119 printf ("Usage:\n%s\n", cmdtp->usage);
124 * Set the address and length of the fdt.
126 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
131 newaddr = (struct fdt_header *)simple_strtoul(argv[3], NULL, 16);
134 * If the user specifies a length, use that. Otherwise use the
138 len = fdt_totalsize(fdt);
140 len = simple_strtoul(argv[4], NULL, 16);
141 if (len < fdt_totalsize(fdt)) {
142 printf ("New length 0x%X < existing length 0x%X, aborting.\n",
143 len, fdt_totalsize(fdt));
149 * Copy to the new location.
151 err = fdt_open_into(fdt, newaddr, len);
153 printf ("libfdt: %s\n", fdt_strerror(err));
158 /********************************************************************
159 * Set the value of a node in the fdt.
160 ********************************************************************/
161 } else if (op == 's') {
162 char *pathp; /* path */
163 char *prop; /* property */
164 struct fdt_property *nodep; /* node struct pointer */
165 char *newval; /* value from the user (as a string) */
166 char *vp; /* temporary value pointer */
167 char *cp; /* temporary char pointer */
168 int nodeoffset; /* node offset from libfdt */
169 int len; /* new length of the property */
170 int oldlen; /* original length of the property */
171 unsigned long tmp; /* holds converted values */
172 int ret; /* return value */
175 * Parameters: Node path, property, value.
178 printf ("Usage:\n%s\n", cmdtp->usage);
186 if (strcmp(pathp, "/") == 0) {
189 nodeoffset = fdt_path_offset (fdt, pathp);
190 if (nodeoffset < 0) {
192 * Not found or something else bad happened.
194 printf ("libfdt: %s\n", fdt_strerror(nodeoffset));
198 nodep = fdt_getprop (fdt, nodeoffset, prop, &oldlen);
200 printf ("libfdt %s\n", fdt_strerror(oldlen));
202 } else if (oldlen == 0) {
204 * The specified property has no value
206 printf("%s has no value, cannot set one (yet).\n", prop);
210 * Convert the new property
213 if (*newval == '<') {
215 * Bigger values than bytes.
219 while ((*newval != '>') && (*newval != '\0')) {
221 tmp = simple_strtoul(cp, &newval, 16);
222 if ((newval - cp) <= 2) {
226 } else if ((newval - cp) <= 4) {
227 *(uint16_t *)vp = __cpu_to_be16(tmp);
230 } else if ((newval - cp) <= 8) {
231 *(uint32_t *)vp = __cpu_to_be32(tmp);
235 printf("Sorry, I could not convert \"%s\"\n", cp);
238 while (*newval == ' ')
241 if (*newval != '>') {
242 printf("Unexpected character '%c'\n", *newval);
245 } else if (*newval == '[') {
247 * Byte stream. Convert the values.
251 while ((*newval != ']') && (*newval != '\0')) {
252 tmp = simple_strtoul(newval, &newval, 16);
255 while (*newval == ' ')
258 if (*newval != ']') {
259 printf("Unexpected character '%c'\n", *newval);
264 * Assume it is a string. Copy it into our data area for
265 * convenience (including the terminating '\0').
267 len = strlen(newval) + 1;
268 strcpy(data, newval);
271 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
273 printf ("libfdt %s\n", fdt_strerror(ret));
278 /********************************************************************
279 * Print (recursive) / List (single level)
280 ********************************************************************/
281 } else if ((op == 'p') || (op == 'l')) {
283 * Recursively print (a portion of) the fdt.
285 static int offstack[MAX_LEVEL];
286 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";
287 int depth = MAX_LEVEL; /* how deep to print */
288 char *pathp; /* path */
289 char *prop; /* property */
290 void *nodep; /* property node pointer */
291 int nodeoffset; /* node offset from libfdt */
292 int nextoffset; /* next node offset from libfdt */
293 uint32_t tag; /* tag */
294 int len; /* length of the property */
295 int level = 0; /* keep track of nesting level */
298 * list is an alias for print, but limited to 1 level
305 * Get the starting path. The root node is an oddball,
306 * the offset is zero and has no name.
314 if (strcmp(pathp, "/") == 0) {
318 nodeoffset = fdt_path_offset (fdt, pathp);
319 if (nodeoffset < 0) {
321 * Not found or something else bad happened.
323 printf ("libfdt %s\n", fdt_strerror(nodeoffset));
328 * The user passed in a property as well as node path. Print only
329 * the given property and then return.
332 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
334 printf("%s %s\n", pathp, prop); /* no property value */
336 } else if (len > 0) {
338 print_data (nodep, len);
342 printf ("libfdt %s\n", fdt_strerror(len));
348 * The user passed in a node path and no property, print the node
351 offstack[0] = nodeoffset;
354 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
358 printf("%s%s {\n", &tabs[MAX_LEVEL - level], pathp);
360 offstack[level] = nodeoffset;
361 if (level >= MAX_LEVEL) {
362 printf("Aaaiii <splat> nested too deep.\n");
369 printf("%s};\n", &tabs[MAX_LEVEL - level]);
371 level = -1; /* exit the loop */
375 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
377 printf ("libfdt %s\n", fdt_strerror(len));
379 } else if (len == 0) {
380 /* the property has no value */
382 printf("%s%s;\n", &tabs[MAX_LEVEL - level], pathp);
385 printf("%s%s=", &tabs[MAX_LEVEL - level], pathp);
386 print_data (nodep, len);
397 printf("Unknown tag 0x%08X\n", tag);
400 nodeoffset = nextoffset;
403 /********************************************************************
404 * Remove a property/node
405 ********************************************************************/
406 } else if (op == 'r') {
407 int nodeoffset; /* node offset from libfdt */
411 * Get the path. The root node is an oddball, the offset
412 * is zero and has no name.
414 if (strcmp(argv[2], "/") == 0) {
417 nodeoffset = fdt_path_offset (fdt, argv[2]);
418 if (nodeoffset < 0) {
420 * Not found or something else bad happened.
422 printf ("libfdt %s\n", fdt_strerror(nodeoffset));
427 * Do the delete. A fourth parameter means delete a property,
428 * otherwise delete the node.
431 err = fdt_delprop(fdt, nodeoffset, argv[3]);
433 printf("fdt_delprop libfdt: %s\n", fdt_strerror(err));
437 err = fdt_del_node(fdt, nodeoffset);
439 printf("fdt_del_node libfdt: %s\n", fdt_strerror(err));
444 /********************************************************************
445 * Create a chosen node
446 ********************************************************************/
447 } else if (op == 'c') {
448 fdt_chosen(fdt, 0, 0, 1);
450 /********************************************************************
451 * Create a u-boot-env node
452 ********************************************************************/
453 } else if (op == 'e') {
456 /********************************************************************
458 ********************************************************************/
459 } else if (op == 'b') {
462 /********************************************************************
463 * Unrecognized command
464 ********************************************************************/
466 printf ("Usage:\n%s\n", cmdtp->usage);
473 /********************************************************************/
475 static int fdt_valid(void)
480 printf ("The address of the fdt is invalid (NULL).\n");
484 err = fdt_check_header(fdt);
486 return 1; /* valid */
489 printf("libfdt: %s", fdt_strerror(err));
491 * Be more informative on bad version.
493 if (err == -FDT_ERR_BADVERSION) {
494 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
495 printf (" - too old, fdt $d < %d",
496 fdt_version(fdt), FDT_FIRST_SUPPORTED_VERSION);
499 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
500 printf (" - too new, fdt $d > %d",
501 fdt_version(fdt), FDT_LAST_SUPPORTED_VERSION);
512 /********************************************************************/
515 * OF flat tree handling
521 static int is_printable_string(const void *data, int len)
523 const char *s = data;
525 /* zero length is not */
529 /* must terminate with zero */
530 if (s[len - 1] != '\0')
533 /* printable or a null byte (concatenated strings) */
534 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
536 * If we see a null, there are three possibilities:
537 * 1) If len == 1, it is the end of the string, printable
538 * 2) Next character also a null, not printable.
539 * 3) Next character not a null, continue to check.
551 /* Not the null termination, or not done yet: not printable */
552 if (*s != '\0' || (len != 0))
558 static void print_data(const void *data, int len)
563 /* no data, don't print */
568 * It is a string, but it may have multiple strings (embedded '\0's).
570 if (is_printable_string(data, len)) {
577 j += strlen(data) + 1;
578 data += strlen(data) + 1;
586 printf("<%02x>", (*(u8 *) data) & 0xff);
588 case 2: /* half-word */
589 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
592 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
594 case 8: /* double-word */
596 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
598 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
600 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
603 default: /* anything else... hexdump */
605 for (j = 0, s = data; j < len; j++)
606 printf("%02x%s", s[j], j < len - 1 ? " " : "");
613 /********************************************************************/
617 "fdt - flattened device tree utility commands\n",
618 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
619 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
620 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
621 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
622 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
623 "fdt mknode <path> <node> - Create a new node after <path>\n"
624 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
625 "fdt chosen - Add/update the \"/chosen\" branch in the tree\n"
626 #ifdef CONFIG_OF_HAS_UBOOT_ENV
627 "fdt env - Add/replace the \"/u-boot-env\" branch in the tree\n"
629 #ifdef CONFIG_OF_HAS_BD_T
630 "fdt bd_t - Add/replace the \"/bd_t\" branch in the tree\n"
633 " * Set a larger length with the fdt addr command to add to the blob.\n"
634 " * If the property you are setting/printing has a '#' character,\n"
635 " you MUST escape it with a \\ character or quote it with \" or\n"
636 " it will be ignored as a comment.\n"
637 " * If the value has spaces in it, you MUST escape the spaces with\n"
638 " \\ characters or quote it with \"\"\n"
639 "Examples: fdt print / # print the whole tree\n"
640 " fdt print /cpus \"#address-cells\"\n"
641 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
644 #endif /* CONFIG_OF_LIBFDT */