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>
31 #include <asm/global_data.h>
34 #include <fdt_support.h>
36 #define MAX_LEVEL 32 /* how deeply nested we will go */
37 #define SCRATCHPAD 1024 /* bytes of scratchpad memory */
40 * Global data (for the gd->bd)
42 DECLARE_GLOBAL_DATA_PTR;
44 static int fdt_valid(void);
45 static int fdt_parse_prop(char **newval, int count, char *data, int *len);
46 static int fdt_print(const char *pathp, char *prop, int depth);
49 * The working_fdt points to our working flattened device tree.
51 struct fdt_header *working_fdt;
54 * Flattened Device Tree command, see the help for parameter definitions.
56 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
59 printf ("Usage:\n%s\n", cmdtp->usage);
63 /********************************************************************
64 * Set the address of the fdt
65 ********************************************************************/
66 if (argv[1][0] == 'a') {
68 * Set the address [and length] of the fdt.
70 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
82 len = simple_strtoul(argv[3], NULL, 16);
83 if (len < fdt_totalsize(working_fdt)) {
84 printf ("New length %d < existing length %d, "
86 len, fdt_totalsize(working_fdt));
89 * Open in place with a new length.
91 err = fdt_open_into(working_fdt, working_fdt, len);
93 printf ("libfdt fdt_open_into(): %s\n",
99 /********************************************************************
100 * Move the working_fdt
101 ********************************************************************/
102 } else if (strncmp(argv[1], "mo", 2) == 0) {
103 struct fdt_header *newaddr;
108 printf ("Usage:\n%s\n", cmdtp->usage);
113 * Set the address and length of the fdt.
115 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
120 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
123 * If the user specifies a length, use that. Otherwise use the
127 len = fdt_totalsize(working_fdt);
129 len = simple_strtoul(argv[4], NULL, 16);
130 if (len < fdt_totalsize(working_fdt)) {
131 printf ("New length 0x%X < existing length "
133 len, fdt_totalsize(working_fdt));
139 * Copy to the new location.
141 err = fdt_open_into(working_fdt, newaddr, len);
143 printf ("libfdt fdt_open_into(): %s\n",
147 working_fdt = newaddr;
149 /********************************************************************
151 ********************************************************************/
152 } else if (strncmp(argv[1], "mk", 2) == 0) {
153 char *pathp; /* path */
154 char *nodep; /* new node to add */
155 int nodeoffset; /* node offset from libfdt */
159 * Parameters: Node path, new node to be appended to the path.
162 printf ("Usage:\n%s\n", cmdtp->usage);
169 nodeoffset = fdt_path_offset (working_fdt, pathp);
170 if (nodeoffset < 0) {
172 * Not found or something else bad happened.
174 printf ("libfdt fdt_path_offset() returned %s\n",
175 fdt_strerror(nodeoffset));
178 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
180 printf ("libfdt fdt_add_subnode(): %s\n",
185 /********************************************************************
186 * Set the value of a property in the working_fdt.
187 ********************************************************************/
188 } else if (argv[1][0] == 's') {
189 char *pathp; /* path */
190 char *prop; /* property */
191 int nodeoffset; /* node offset from libfdt */
192 static char data[SCRATCHPAD]; /* storage for the property */
193 int len; /* new length of the property */
194 int ret; /* return value */
197 * Parameters: Node path, property, optional value.
200 printf ("Usage:\n%s\n", cmdtp->usage);
209 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
214 nodeoffset = fdt_path_offset (working_fdt, pathp);
215 if (nodeoffset < 0) {
217 * Not found or something else bad happened.
219 printf ("libfdt fdt_path_offset() returned %s\n",
220 fdt_strerror(nodeoffset));
224 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
226 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
230 /********************************************************************
231 * Print (recursive) / List (single level)
232 ********************************************************************/
233 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
234 int depth = MAX_LEVEL; /* how deep to print */
235 char *pathp; /* path */
236 char *prop; /* property */
237 int ret; /* return value */
238 static char root[2] = "/";
241 * list is an alias for print, but limited to 1 level
243 if (argv[1][0] == 'l') {
248 * Get the starting path. The root node is an oddball,
249 * 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 (strncmp(argv[1], "rm", 2) == 0) {
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_path_offset (working_fdt, argv[2]);
276 if (nodeoffset < 0) {
278 * Not found or something else bad happened.
280 printf ("libfdt fdt_path_offset() 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(working_fdt, nodeoffset, argv[3]);
291 printf("libfdt fdt_delprop(): %s\n",
296 err = fdt_del_node(working_fdt, nodeoffset);
298 printf("libfdt fdt_del_node(): %s\n",
304 /********************************************************************
305 * Display header info
306 ********************************************************************/
307 } else if (argv[1][0] == 'h') {
308 u32 version = fdt_version(working_fdt);
309 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
310 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
311 fdt_totalsize(working_fdt));
312 printf("off_dt_struct:\t\t0x%x\n",
313 fdt_off_dt_struct(working_fdt));
314 printf("off_dt_strings:\t\t0x%x\n",
315 fdt_off_dt_strings(working_fdt));
316 printf("off_mem_rsvmap:\t\t0x%x\n",
317 fdt_off_mem_rsvmap(working_fdt));
318 printf("version:\t\t%d\n", version);
319 printf("last_comp_version:\t%d\n",
320 fdt_last_comp_version(working_fdt));
322 printf("boot_cpuid_phys:\t0x%x\n",
323 fdt_boot_cpuid_phys(working_fdt));
325 printf("size_dt_strings:\t0x%x\n",
326 fdt_size_dt_strings(working_fdt));
328 printf("size_dt_struct:\t\t0x%x\n",
329 fdt_size_dt_struct(working_fdt));
330 printf("number mem_rsv:\t\t0x%x\n",
331 fdt_num_mem_rsv(working_fdt));
334 /********************************************************************
336 ********************************************************************/
337 } else if (strncmp(argv[1], "boo", 3) == 0) {
338 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
339 fdt_set_boot_cpuid_phys(working_fdt, tmp);
341 /********************************************************************
343 ********************************************************************/
344 } else if (strncmp(argv[1], "me", 2) == 0) {
347 #ifdef CFG_64BIT_STRTOUL
348 addr = simple_strtoull(argv[2], NULL, 16);
349 size = simple_strtoull(argv[3], NULL, 16);
351 addr = simple_strtoul(argv[2], NULL, 16);
352 size = simple_strtoul(argv[3], NULL, 16);
354 err = fdt_fixup_memory(working_fdt, addr, size);
358 /********************************************************************
359 * mem reserve commands
360 ********************************************************************/
361 } else if (strncmp(argv[1], "rs", 2) == 0) {
362 if (argv[2][0] == 'p') {
364 int total = fdt_num_mem_rsv(working_fdt);
366 printf("index\t\t start\t\t size\n");
367 printf("-------------------------------"
368 "-----------------\n");
369 for (j = 0; j < total; j++) {
370 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
372 printf("libfdt fdt_get_mem_rsv(): %s\n",
376 printf(" %x\t%08x%08x\t%08x%08x\n", j,
378 (u32)(addr & 0xffffffff),
380 (u32)(size & 0xffffffff));
382 } else if (argv[2][0] == 'a') {
385 #ifdef CFG_64BIT_STRTOUL
386 addr = simple_strtoull(argv[3], NULL, 16);
387 size = simple_strtoull(argv[4], NULL, 16);
389 addr = simple_strtoul(argv[3], NULL, 16);
390 size = simple_strtoul(argv[4], NULL, 16);
392 err = fdt_add_mem_rsv(working_fdt, addr, size);
395 printf("libfdt fdt_add_mem_rsv(): %s\n",
399 } else if (argv[2][0] == 'd') {
400 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
401 int err = fdt_del_mem_rsv(working_fdt, idx);
404 printf("libfdt fdt_del_mem_rsv(): %s\n",
409 /* Unrecognized command */
410 printf ("Usage:\n%s\n", cmdtp->usage);
414 #ifdef CONFIG_OF_BOARD_SETUP
415 /* Call the board-specific fixup routine */
416 else if (strncmp(argv[1], "boa", 3) == 0)
417 ft_board_setup(working_fdt, gd->bd);
419 /* Create a chosen node */
420 else if (argv[1][0] == 'c')
421 fdt_chosen(working_fdt, 0, 0, 1);
423 /* Unrecognized command */
424 printf ("Usage:\n%s\n", cmdtp->usage);
431 /****************************************************************************/
433 static int fdt_valid(void)
437 if (working_fdt == NULL) {
438 printf ("The address of the fdt is invalid (NULL).\n");
442 err = fdt_check_header(working_fdt);
444 return 1; /* valid */
447 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
449 * Be more informative on bad version.
451 if (err == -FDT_ERR_BADVERSION) {
452 if (fdt_version(working_fdt) <
453 FDT_FIRST_SUPPORTED_VERSION) {
454 printf (" - too old, fdt $d < %d",
455 fdt_version(working_fdt),
456 FDT_FIRST_SUPPORTED_VERSION);
459 if (fdt_last_comp_version(working_fdt) >
460 FDT_LAST_SUPPORTED_VERSION) {
461 printf (" - too new, fdt $d > %d",
462 fdt_version(working_fdt),
463 FDT_LAST_SUPPORTED_VERSION);
474 /****************************************************************************/
477 * Parse the user's input, partially heuristic. Valid formats:
478 * <0x00112233 4 05> - an array of cells. Numbers follow standard
480 * [00 11 22 .. nn] - byte stream
481 * "string" - If the the value doesn't start with "<" or "[", it is
482 * treated as a string. Note that the quotes are
483 * stripped by the parser before we get the string.
484 * newval: An array of strings containing the new property as specified
485 * on the command line
486 * count: The number of strings in the array
487 * data: A bytestream to be placed in the property
488 * len: The length of the resulting bytestream
490 static int fdt_parse_prop(char **newval, int count, char *data, int *len)
492 char *cp; /* temporary char pointer */
493 char *newp; /* temporary newval char pointer */
494 unsigned long tmp; /* holds converted values */
500 /* An array of cells */
503 while ((*newp != '>') && (stridx < count)) {
505 * Keep searching until we find that last ">"
506 * That way users don't have to escape the spaces
509 newp = newval[++stridx];
514 tmp = simple_strtoul(cp, &newp, 0);
515 *(uint32_t *)data = __cpu_to_be32(tmp);
519 /* If the ptr didn't advance, something went wrong */
520 if ((newp - cp) <= 0) {
521 printf("Sorry, I could not convert \"%s\"\n",
531 printf("Unexpected character '%c'\n", *newp);
534 } else if (*newp == '[') {
536 * Byte stream. Convert the values.
539 while ((*newp != ']') && (stridx < count)) {
540 tmp = simple_strtoul(newp, &newp, 16);
541 *data++ = tmp & 0xFF;
546 newp = newval[++stridx];
549 printf("Unexpected character '%c'\n", *newval);
554 * Assume it is a string. Copy it into our data area for
555 * convenience (including the terminating '\0').
557 while (stridx < count) {
558 *len = strlen(newp) + 1;
560 newp = newval[++stridx];
566 /****************************************************************************/
569 * Heuristic to guess if this is a string or concatenated strings.
572 static int is_printable_string(const void *data, int len)
574 const char *s = data;
576 /* zero length is not */
580 /* must terminate with zero */
581 if (s[len - 1] != '\0')
584 /* printable or a null byte (concatenated strings) */
585 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
587 * If we see a null, there are three possibilities:
588 * 1) If len == 1, it is the end of the string, printable
589 * 2) Next character also a null, not printable.
590 * 3) Next character not a null, continue to check.
602 /* Not the null termination, or not done yet: not printable */
603 if (*s != '\0' || (len != 0))
611 * Print the property in the best format, a heuristic guess. Print as
612 * a string, concatenated strings, a byte, word, double word, or (if all
613 * else fails) it is printed as a stream of bytes.
615 static void print_data(const void *data, int len)
619 /* no data, don't print */
624 * It is a string, but it may have multiple strings (embedded '\0's).
626 if (is_printable_string(data, len)) {
633 j += strlen(data) + 1;
634 data += strlen(data) + 1;
644 for (j = 0, p = data; j < len/4; j ++)
645 printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
647 } else { /* anything else... hexdump */
651 for (j = 0, s = data; j < len; j++)
652 printf("%02x%s", s[j], j < len - 1 ? " " : "");
657 /****************************************************************************/
660 * Recursively print (a portion of) the working_fdt. The depth parameter
661 * determines how deeply nested the fdt is printed.
663 static int fdt_print(const char *pathp, char *prop, int depth)
665 static char tabs[MAX_LEVEL+1] =
666 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
667 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
668 const void *nodep; /* property node pointer */
669 int nodeoffset; /* node offset from libfdt */
670 int nextoffset; /* next node offset from libfdt */
671 uint32_t tag; /* tag */
672 int len; /* length of the property */
673 int level = 0; /* keep track of nesting level */
674 const struct fdt_property *fdt_prop;
676 nodeoffset = fdt_path_offset (working_fdt, pathp);
677 if (nodeoffset < 0) {
679 * Not found or something else bad happened.
681 printf ("libfdt fdt_path_offset() returned %s\n",
682 fdt_strerror(nodeoffset));
686 * The user passed in a property as well as node path.
687 * Print only the given property and then return.
690 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
692 /* no property value */
693 printf("%s %s\n", pathp, prop);
695 } else if (len > 0) {
696 printf("%s = ", prop);
697 print_data (nodep, len);
701 printf ("libfdt fdt_getprop(): %s\n",
708 * The user passed in a node path and no property,
709 * print the node and all subnodes.
712 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
715 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
716 if (level <= depth) {
718 pathp = "/* NULL pointer error */";
720 pathp = "/"; /* root is nameless */
722 &tabs[MAX_LEVEL - level], pathp);
725 if (level >= MAX_LEVEL) {
726 printf("Nested too deep, aborting.\n");
733 printf("%s};\n", &tabs[MAX_LEVEL - level]);
735 level = -1; /* exit the loop */
739 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
741 pathp = fdt_string(working_fdt,
742 fdt32_to_cpu(fdt_prop->nameoff));
743 len = fdt32_to_cpu(fdt_prop->len);
744 nodep = fdt_prop->data;
746 printf ("libfdt fdt_getprop(): %s\n",
749 } else if (len == 0) {
750 /* the property has no value */
753 &tabs[MAX_LEVEL - level],
756 if (level <= depth) {
758 &tabs[MAX_LEVEL - level],
760 print_data (nodep, len);
766 printf("/* NOP */\n", &tabs[MAX_LEVEL - level]);
772 printf("Unknown tag 0x%08X\n", tag);
775 nodeoffset = nextoffset;
780 /********************************************************************/
784 "fdt - flattened device tree utility commands\n",
785 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
786 #ifdef CONFIG_OF_BOARD_SETUP
787 "fdt boardsetup - Do board-specific set up\n"
789 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
790 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
791 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
792 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
793 "fdt mknode <path> <node> - Create a new node after <path>\n"
794 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
795 "fdt header - Display header info\n"
796 "fdt bootcpu <id> - Set boot cpuid\n"
797 "fdt memory <addr> <size> - Add/Update memory node\n"
798 "fdt rsvmem print - Show current mem reserves\n"
799 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
800 "fdt rsvmem delete <index> - Delete a mem reserves\n"
801 "fdt chosen - Add/update the /chosen branch in the tree\n"
802 "NOTE: If the path or property you are setting/printing has a '#' character\n"
803 " or spaces, you MUST escape it with a \\ character or quote it with \".\n"