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>
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;
48 * Function prototypes/declarations.
50 #ifdef CONFIG_OF_BOARD_SETUP
51 void ft_board_setup(void *blob, bd_t *bd);
54 static int fdt_valid(void);
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);
60 * Flattened Device Tree command, see the help for parameter definitions.
62 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
65 printf ("Usage:\n%s\n", cmdtp->usage);
69 /********************************************************************
70 * Set the address of the fdt
71 ********************************************************************/
72 if (argv[1][0] == 'a') {
74 * Set the address [and length] of the fdt.
76 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
88 len = simple_strtoul(argv[3], NULL, 16);
89 if (len < fdt_totalsize(fdt)) {
90 printf ("New length %d < existing length %d, "
92 len, fdt_totalsize(fdt));
95 * Open in place with a new length.
97 err = fdt_open_into(fdt, fdt, len);
99 printf ("libfdt fdt_open_into(): %s\n",
105 /********************************************************************
107 ********************************************************************/
108 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
109 struct fdt_header *newaddr;
114 printf ("Usage:\n%s\n", cmdtp->usage);
119 * Set the address and length of the fdt.
121 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
126 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
129 * If the user specifies a length, use that. Otherwise use the
133 len = fdt_totalsize(fdt);
135 len = simple_strtoul(argv[4], NULL, 16);
136 if (len < fdt_totalsize(fdt)) {
137 printf ("New length 0x%X < existing length "
139 len, fdt_totalsize(fdt));
145 * Copy to the new location.
147 err = fdt_open_into(fdt, newaddr, len);
149 printf ("libfdt fdt_open_into(): %s\n",
155 /********************************************************************
157 ********************************************************************/
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 */
165 * Parameters: Node path, new node to be appended to the path.
168 printf ("Usage:\n%s\n", cmdtp->usage);
175 nodeoffset = fdt_find_node_by_path (fdt, pathp);
176 if (nodeoffset < 0) {
178 * Not found or something else bad happened.
180 printf ("libfdt fdt_find_node_by_path() returned %s\n",
181 fdt_strerror(nodeoffset));
184 err = fdt_add_subnode(fdt, nodeoffset, nodep);
186 printf ("libfdt fdt_add_subnode(): %s\n",
191 /********************************************************************
192 * Set the value of a property in the fdt.
193 ********************************************************************/
194 } else if (argv[1][0] == 's') {
195 char *pathp; /* path */
196 char *prop; /* property */
197 char *newval; /* value from the user (as a string) */
198 int nodeoffset; /* node offset from libfdt */
199 static char data[SCRATCHPAD]; /* storage for the property */
200 int len; /* new length of the property */
201 int ret; /* return value */
204 * Parameters: Node path, property, value.
207 printf ("Usage:\n%s\n", cmdtp->usage);
215 nodeoffset = fdt_find_node_by_path (fdt, pathp);
216 if (nodeoffset < 0) {
218 * Not found or something else bad happened.
220 printf ("libfdt fdt_find_node_by_path() returned %s\n",
221 fdt_strerror(nodeoffset));
224 ret = fdt_parse_prop(pathp, prop, newval, data, &len);
228 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
230 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
234 /********************************************************************
235 * Print (recursive) / List (single level)
236 ********************************************************************/
237 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
238 int depth = MAX_LEVEL; /* how deep to print */
239 char *pathp; /* path */
240 char *prop; /* property */
241 int ret; /* return value */
244 * list is an alias for print, but limited to 1 level
246 if (argv[1][0] == 'l') {
251 * Get the starting path. The root node is an oddball,
252 * the offset is zero and has no name.
260 ret = fdt_print(pathp, prop, depth);
264 /********************************************************************
265 * Remove a property/node
266 ********************************************************************/
267 } else if (argv[1][0] == 'r') {
268 int nodeoffset; /* node offset from libfdt */
272 * Get the path. The root node is an oddball, the offset
273 * is zero and has no name.
275 nodeoffset = fdt_find_node_by_path (fdt, argv[2]);
276 if (nodeoffset < 0) {
278 * Not found or something else bad happened.
280 printf ("libfdt fdt_find_node_by_path() returned %s\n",
281 fdt_strerror(nodeoffset));
285 * Do the delete. A fourth parameter means delete a property,
286 * otherwise delete the node.
289 err = fdt_delprop(fdt, nodeoffset, argv[3]);
291 printf("libfdt fdt_delprop(): %s\n",
296 err = fdt_del_node(fdt, nodeoffset);
298 printf("libfdt fdt_del_node(): %s\n",
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);
311 /********************************************************************
312 * Create a chosen node
313 ********************************************************************/
314 } else if (argv[1][0] == 'c') {
315 fdt_chosen(fdt, 0, 0, 1);
317 /********************************************************************
318 * Create a u-boot-env node
319 ********************************************************************/
320 } else if (argv[1][0] == 'e') {
323 /********************************************************************
325 ********************************************************************/
326 } else if (argv[1][0] == 'b') {
329 /********************************************************************
330 * Unrecognized command
331 ********************************************************************/
333 printf ("Usage:\n%s\n", cmdtp->usage);
340 /****************************************************************************/
342 static int fdt_valid(void)
347 printf ("The address of the fdt is invalid (NULL).\n");
351 err = fdt_check_header(fdt);
353 return 1; /* valid */
356 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
358 * Be more informative on bad version.
360 if (err == -FDT_ERR_BADVERSION) {
361 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
362 printf (" - too old, fdt $d < %d",
364 FDT_FIRST_SUPPORTED_VERSION);
367 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
368 printf (" - too new, fdt $d > %d",
370 FDT_LAST_SUPPORTED_VERSION);
381 /****************************************************************************/
384 * Parse the user's input, partially heuristic. Valid formats:
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.
395 static int fdt_parse_prop(char *pathp, char *prop, char *newval,
396 char *data, int *len)
398 char *cp; /* temporary char pointer */
399 unsigned long tmp; /* holds converted values */
401 if (*newval == '<') {
403 * Bigger values than bytes.
407 while ((*newval != '>') && (*newval != '\0')) {
409 tmp = simple_strtoul(cp, &newval, 16);
410 if ((newval - cp) <= 2) {
414 } else if ((newval - cp) <= 4) {
415 *(uint16_t *)data = __cpu_to_be16(tmp);
418 } else if ((newval - cp) <= 8) {
419 *(uint32_t *)data = __cpu_to_be32(tmp);
423 printf("Sorry, I could not convert \"%s\"\n",
427 while (*newval == ' ')
430 if (*newval != '>') {
431 printf("Unexpected character '%c'\n", *newval);
434 } else if (*newval == '[') {
436 * Byte stream. Convert the values.
440 while ((*newval != ']') && (*newval != '\0')) {
441 tmp = simple_strtoul(newval, &newval, 16);
442 *data++ = tmp & 0xFF;
444 while (*newval == ' ')
447 if (*newval != ']') {
448 printf("Unexpected character '%c'\n", *newval);
453 * Assume it is a string. Copy it into our data area for
454 * convenience (including the terminating '\0').
456 *len = strlen(newval) + 1;
457 strcpy(data, newval);
462 /****************************************************************************/
465 * Heuristic to guess if this is a string or concatenated strings.
468 static int is_printable_string(const void *data, int len)
470 const char *s = data;
472 /* zero length is not */
476 /* must terminate with zero */
477 if (s[len - 1] != '\0')
480 /* printable or a null byte (concatenated strings) */
481 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
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.
498 /* Not the null termination, or not done yet: not printable */
499 if (*s != '\0' || (len != 0))
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.
511 static void print_data(const void *data, int len)
516 /* no data, don't print */
521 * It is a string, but it may have multiple strings (embedded '\0's).
523 if (is_printable_string(data, len)) {
530 j += strlen(data) + 1;
531 data += strlen(data) + 1;
539 printf("<%02x>", (*(u8 *) data) & 0xff);
541 case 2: /* half-word */
542 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
545 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
547 case 8: /* double-word */
549 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
551 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
553 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
556 default: /* anything else... hexdump */
558 for (j = 0, s = data; j < len; j++)
559 printf("%02x%s", s[j], j < len - 1 ? " " : "");
566 /****************************************************************************/
569 * Recursively print (a portion of) the fdt. The depth parameter
570 * determines how deeply nested the fdt is printed.
572 static int fdt_print(char *pathp, char *prop, int depth)
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 */
585 nodeoffset = fdt_find_node_by_path (fdt, pathp);
586 if (nodeoffset < 0) {
588 * Not found or something else bad happened.
590 printf ("libfdt fdt_find_node_by_path() returned %s\n",
591 fdt_strerror(nodeoffset));
595 * The user passed in a property as well as node path.
596 * Print only the given property and then return.
599 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
601 /* no property value */
602 printf("%s %s\n", pathp, prop);
604 } else if (len > 0) {
606 print_data (nodep, len);
610 printf ("libfdt fdt_getprop(): %s\n",
617 * The user passed in a node path and no property,
618 * print the node and all subnodes.
620 offstack[0] = nodeoffset;
623 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
628 &tabs[MAX_LEVEL - level], pathp);
630 offstack[level] = nodeoffset;
631 if (level >= MAX_LEVEL) {
632 printf("Aaaiii <splat> nested too deep. "
640 printf("%s};\n", &tabs[MAX_LEVEL - level]);
642 level = -1; /* exit the loop */
646 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
648 printf ("libfdt fdt_getprop(): %s\n",
651 } else if (len == 0) {
652 /* the property has no value */
655 &tabs[MAX_LEVEL - level],
660 &tabs[MAX_LEVEL - level],
662 print_data (nodep, len);
673 printf("Unknown tag 0x%08X\n", tag);
676 nodeoffset = nextoffset;
681 /********************************************************************/
685 "fdt - flattened device tree utility commands\n",
686 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
687 #ifdef CONFIG_OF_BOARD_SETUP
688 "fdt boardsetup - Do board-specific set up\n"
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"
696 "fdt chosen - Add/update the /chosen branch in the tree\n"
697 #ifdef CONFIG_OF_HAS_UBOOT_ENV
698 "fdt env - Add/replace the /u-boot-env branch in the tree\n"
700 #ifdef CONFIG_OF_HAS_BD_T
701 "fdt bd_t - Add/replace the /bd_t branch in the tree\n"
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"
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"
711 #endif /* CONFIG_OF_LIBFDT */