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>
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 */
38 #ifndef CONFIG_CMD_FDT_MAX_DUMP
39 #define CONFIG_CMD_FDT_MAX_DUMP 64
43 * Global data (for the gd->bd)
45 DECLARE_GLOBAL_DATA_PTR;
47 static int fdt_valid(void);
48 static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
49 static int fdt_print(const char *pathp, char *prop, int depth);
52 * The working_fdt points to our working flattened device tree.
54 struct fdt_header *working_fdt;
56 void set_working_fdt_addr(void *addr)
62 sprintf(buf, "%lx", (unsigned long)addr);
63 setenv("fdtaddr", buf);
67 * Flattened Device Tree command, see the help for parameter definitions.
69 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
75 * Set the address of the fdt
77 if (argv[1][0] == 'a') {
80 * Set the address [and length] of the fdt.
86 printf("The address of the fdt is %p\n", working_fdt);
90 addr = simple_strtoul(argv[2], NULL, 16);
91 set_working_fdt_addr((void *)addr);
101 * Optional new length
103 len = simple_strtoul(argv[3], NULL, 16);
104 if (len < fdt_totalsize(working_fdt)) {
105 printf ("New length %d < existing length %d, "
107 len, fdt_totalsize(working_fdt));
110 * Open in place with a new length.
112 err = fdt_open_into(working_fdt, working_fdt, len);
114 printf ("libfdt fdt_open_into(): %s\n",
120 return CMD_RET_SUCCESS;
125 "No FDT memory address configured. Please configure\n"
126 "the FDT address via \"fdt addr <address>\" command.\n"
128 return CMD_RET_FAILURE;
132 * Move the working_fdt
134 if (strncmp(argv[1], "mo", 2) == 0) {
135 struct fdt_header *newaddr;
140 return CMD_RET_USAGE;
143 * Set the address and length of the fdt.
145 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
150 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
153 * If the user specifies a length, use that. Otherwise use the
157 len = fdt_totalsize(working_fdt);
159 len = simple_strtoul(argv[4], NULL, 16);
160 if (len < fdt_totalsize(working_fdt)) {
161 printf ("New length 0x%X < existing length "
163 len, fdt_totalsize(working_fdt));
169 * Copy to the new location.
171 err = fdt_open_into(working_fdt, newaddr, len);
173 printf ("libfdt fdt_open_into(): %s\n",
177 working_fdt = newaddr;
182 } else if (strncmp(argv[1], "mk", 2) == 0) {
183 char *pathp; /* path */
184 char *nodep; /* new node to add */
185 int nodeoffset; /* node offset from libfdt */
189 * Parameters: Node path, new node to be appended to the path.
192 return CMD_RET_USAGE;
197 nodeoffset = fdt_path_offset (working_fdt, pathp);
198 if (nodeoffset < 0) {
200 * Not found or something else bad happened.
202 printf ("libfdt fdt_path_offset() returned %s\n",
203 fdt_strerror(nodeoffset));
206 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
208 printf ("libfdt fdt_add_subnode(): %s\n",
214 * Set the value of a property in the working_fdt.
216 } else if (argv[1][0] == 's') {
217 char *pathp; /* path */
218 char *prop; /* property */
219 int nodeoffset; /* node offset from libfdt */
220 static char data[SCRATCHPAD]; /* storage for the property */
221 int len; /* new length of the property */
222 int ret; /* return value */
225 * Parameters: Node path, property, optional value.
228 return CMD_RET_USAGE;
235 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
240 nodeoffset = fdt_path_offset (working_fdt, pathp);
241 if (nodeoffset < 0) {
243 * Not found or something else bad happened.
245 printf ("libfdt fdt_path_offset() returned %s\n",
246 fdt_strerror(nodeoffset));
250 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
252 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
257 * Print (recursive) / List (single level)
259 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
260 int depth = MAX_LEVEL; /* how deep to print */
261 char *pathp; /* path */
262 char *prop; /* property */
263 int ret; /* return value */
264 static char root[2] = "/";
267 * list is an alias for print, but limited to 1 level
269 if (argv[1][0] == 'l') {
274 * Get the starting path. The root node is an oddball,
275 * the offset is zero and has no name.
286 ret = fdt_print(pathp, prop, depth);
291 * Remove a property/node
293 } else if (strncmp(argv[1], "rm", 2) == 0) {
294 int nodeoffset; /* node offset from libfdt */
298 * Get the path. The root node is an oddball, the offset
299 * is zero and has no name.
301 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
302 if (nodeoffset < 0) {
304 * Not found or something else bad happened.
306 printf ("libfdt fdt_path_offset() returned %s\n",
307 fdt_strerror(nodeoffset));
311 * Do the delete. A fourth parameter means delete a property,
312 * otherwise delete the node.
315 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
317 printf("libfdt fdt_delprop(): %s\n",
322 err = fdt_del_node(working_fdt, nodeoffset);
324 printf("libfdt fdt_del_node(): %s\n",
331 * Display header info
333 } else if (argv[1][0] == 'h') {
334 u32 version = fdt_version(working_fdt);
335 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
336 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
337 fdt_totalsize(working_fdt));
338 printf("off_dt_struct:\t\t0x%x\n",
339 fdt_off_dt_struct(working_fdt));
340 printf("off_dt_strings:\t\t0x%x\n",
341 fdt_off_dt_strings(working_fdt));
342 printf("off_mem_rsvmap:\t\t0x%x\n",
343 fdt_off_mem_rsvmap(working_fdt));
344 printf("version:\t\t%d\n", version);
345 printf("last_comp_version:\t%d\n",
346 fdt_last_comp_version(working_fdt));
348 printf("boot_cpuid_phys:\t0x%x\n",
349 fdt_boot_cpuid_phys(working_fdt));
351 printf("size_dt_strings:\t0x%x\n",
352 fdt_size_dt_strings(working_fdt));
354 printf("size_dt_struct:\t\t0x%x\n",
355 fdt_size_dt_struct(working_fdt));
356 printf("number mem_rsv:\t\t0x%x\n",
357 fdt_num_mem_rsv(working_fdt));
363 } else if (strncmp(argv[1], "boo", 3) == 0) {
364 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
365 fdt_set_boot_cpuid_phys(working_fdt, tmp);
370 } else if (strncmp(argv[1], "me", 2) == 0) {
373 addr = simple_strtoull(argv[2], NULL, 16);
374 size = simple_strtoull(argv[3], NULL, 16);
375 err = fdt_fixup_memory(working_fdt, addr, size);
380 * mem reserve commands
382 } else if (strncmp(argv[1], "rs", 2) == 0) {
383 if (argv[2][0] == 'p') {
385 int total = fdt_num_mem_rsv(working_fdt);
387 printf("index\t\t start\t\t size\n");
388 printf("-------------------------------"
389 "-----------------\n");
390 for (j = 0; j < total; j++) {
391 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
393 printf("libfdt fdt_get_mem_rsv(): %s\n",
397 printf(" %x\t%08x%08x\t%08x%08x\n", j,
399 (u32)(addr & 0xffffffff),
401 (u32)(size & 0xffffffff));
403 } else if (argv[2][0] == 'a') {
406 addr = simple_strtoull(argv[3], NULL, 16);
407 size = simple_strtoull(argv[4], NULL, 16);
408 err = fdt_add_mem_rsv(working_fdt, addr, size);
411 printf("libfdt fdt_add_mem_rsv(): %s\n",
415 } else if (argv[2][0] == 'd') {
416 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
417 int err = fdt_del_mem_rsv(working_fdt, idx);
420 printf("libfdt fdt_del_mem_rsv(): %s\n",
425 /* Unrecognized command */
426 return CMD_RET_USAGE;
429 #ifdef CONFIG_OF_BOARD_SETUP
430 /* Call the board-specific fixup routine */
431 else if (strncmp(argv[1], "boa", 3) == 0)
432 ft_board_setup(working_fdt, gd->bd);
434 /* Create a chosen node */
435 else if (argv[1][0] == 'c') {
436 unsigned long initrd_start = 0, initrd_end = 0;
438 if ((argc != 2) && (argc != 4))
439 return CMD_RET_USAGE;
442 initrd_start = simple_strtoul(argv[2], NULL, 16);
443 initrd_end = simple_strtoul(argv[3], NULL, 16);
446 fdt_chosen(working_fdt, 1);
447 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
450 else if (strncmp(argv[1], "re", 2) == 0) {
451 fdt_resize(working_fdt);
454 /* Unrecognized command */
455 return CMD_RET_USAGE;
461 /****************************************************************************/
463 static int fdt_valid(void)
467 if (working_fdt == NULL) {
468 printf ("The address of the fdt is invalid (NULL).\n");
472 err = fdt_check_header(working_fdt);
474 return 1; /* valid */
477 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
479 * Be more informative on bad version.
481 if (err == -FDT_ERR_BADVERSION) {
482 if (fdt_version(working_fdt) <
483 FDT_FIRST_SUPPORTED_VERSION) {
484 printf (" - too old, fdt %d < %d",
485 fdt_version(working_fdt),
486 FDT_FIRST_SUPPORTED_VERSION);
489 if (fdt_last_comp_version(working_fdt) >
490 FDT_LAST_SUPPORTED_VERSION) {
491 printf (" - too new, fdt %d > %d",
492 fdt_version(working_fdt),
493 FDT_LAST_SUPPORTED_VERSION);
504 /****************************************************************************/
507 * Parse the user's input, partially heuristic. Valid formats:
508 * <0x00112233 4 05> - an array of cells. Numbers follow standard
510 * [00 11 22 .. nn] - byte stream
511 * "string" - If the the value doesn't start with "<" or "[", it is
512 * treated as a string. Note that the quotes are
513 * stripped by the parser before we get the string.
514 * newval: An array of strings containing the new property as specified
515 * on the command line
516 * count: The number of strings in the array
517 * data: A bytestream to be placed in the property
518 * len: The length of the resulting bytestream
520 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
522 char *cp; /* temporary char pointer */
523 char *newp; /* temporary newval char pointer */
524 unsigned long tmp; /* holds converted values */
530 /* An array of cells */
533 while ((*newp != '>') && (stridx < count)) {
535 * Keep searching until we find that last ">"
536 * That way users don't have to escape the spaces
539 newp = newval[++stridx];
544 tmp = simple_strtoul(cp, &newp, 0);
545 *(uint32_t *)data = __cpu_to_be32(tmp);
549 /* If the ptr didn't advance, something went wrong */
550 if ((newp - cp) <= 0) {
551 printf("Sorry, I could not convert \"%s\"\n",
561 printf("Unexpected character '%c'\n", *newp);
564 } else if (*newp == '[') {
566 * Byte stream. Convert the values.
569 while ((stridx < count) && (*newp != ']')) {
573 newp = newval[++stridx];
576 if (!isxdigit(*newp))
578 tmp = simple_strtoul(newp, &newp, 16);
579 *data++ = tmp & 0xFF;
583 printf("Unexpected character '%c'\n", *newp);
588 * Assume it is one or more strings. Copy it into our
589 * data area for convenience (including the
590 * terminating '\0's).
592 while (stridx < count) {
593 size_t length = strlen(newp) + 1;
597 newp = newval[++stridx];
603 /****************************************************************************/
606 * Heuristic to guess if this is a string or concatenated strings.
609 static int is_printable_string(const void *data, int len)
611 const char *s = data;
613 /* zero length is not */
617 /* must terminate with zero */
618 if (s[len - 1] != '\0')
621 /* printable or a null byte (concatenated strings) */
622 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
624 * If we see a null, there are three possibilities:
625 * 1) If len == 1, it is the end of the string, printable
626 * 2) Next character also a null, not printable.
627 * 3) Next character not a null, continue to check.
639 /* Not the null termination, or not done yet: not printable */
640 if (*s != '\0' || (len != 0))
648 * Print the property in the best format, a heuristic guess. Print as
649 * a string, concatenated strings, a byte, word, double word, or (if all
650 * else fails) it is printed as a stream of bytes.
652 static void print_data(const void *data, int len)
656 /* no data, don't print */
661 * It is a string, but it may have multiple strings (embedded '\0's).
663 if (is_printable_string(data, len)) {
670 j += strlen(data) + 1;
671 data += strlen(data) + 1;
678 if (len > CONFIG_CMD_FDT_MAX_DUMP)
679 printf("* 0x%08x [0x%08x]", (unsigned int)data, len);
684 for (j = 0, p = data; j < len/4; j++)
685 printf("0x%08x%s", fdt32_to_cpu(p[j]),
686 j < (len/4 - 1) ? " " : "");
689 } else { /* anything else... hexdump */
690 if (len > CONFIG_CMD_FDT_MAX_DUMP)
691 printf("* 0x%08x [0x%08x]", (unsigned int)data, len);
696 for (j = 0, s = data; j < len; j++)
697 printf("%02x%s", s[j], j < len - 1 ? " " : "");
703 /****************************************************************************/
706 * Recursively print (a portion of) the working_fdt. The depth parameter
707 * determines how deeply nested the fdt is printed.
709 static int fdt_print(const char *pathp, char *prop, int depth)
711 static char tabs[MAX_LEVEL+1] =
712 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
713 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
714 const void *nodep; /* property node pointer */
715 int nodeoffset; /* node offset from libfdt */
716 int nextoffset; /* next node offset from libfdt */
717 uint32_t tag; /* tag */
718 int len; /* length of the property */
719 int level = 0; /* keep track of nesting level */
720 const struct fdt_property *fdt_prop;
722 nodeoffset = fdt_path_offset (working_fdt, pathp);
723 if (nodeoffset < 0) {
725 * Not found or something else bad happened.
727 printf ("libfdt fdt_path_offset() returned %s\n",
728 fdt_strerror(nodeoffset));
732 * The user passed in a property as well as node path.
733 * Print only the given property and then return.
736 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
738 /* no property value */
739 printf("%s %s\n", pathp, prop);
741 } else if (len > 0) {
742 printf("%s = ", prop);
743 print_data (nodep, len);
747 printf ("libfdt fdt_getprop(): %s\n",
754 * The user passed in a node path and no property,
755 * print the node and all subnodes.
758 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
761 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
762 if (level <= depth) {
764 pathp = "/* NULL pointer error */";
766 pathp = "/"; /* root is nameless */
768 &tabs[MAX_LEVEL - level], pathp);
771 if (level >= MAX_LEVEL) {
772 printf("Nested too deep, aborting.\n");
779 printf("%s};\n", &tabs[MAX_LEVEL - level]);
781 level = -1; /* exit the loop */
785 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
787 pathp = fdt_string(working_fdt,
788 fdt32_to_cpu(fdt_prop->nameoff));
789 len = fdt32_to_cpu(fdt_prop->len);
790 nodep = fdt_prop->data;
792 printf ("libfdt fdt_getprop(): %s\n",
795 } else if (len == 0) {
796 /* the property has no value */
799 &tabs[MAX_LEVEL - level],
802 if (level <= depth) {
804 &tabs[MAX_LEVEL - level],
806 print_data (nodep, len);
812 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
818 printf("Unknown tag 0x%08X\n", tag);
821 nodeoffset = nextoffset;
826 /********************************************************************/
830 "flattened device tree utility commands",
831 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
832 #ifdef CONFIG_OF_BOARD_SETUP
833 "fdt boardsetup - Do board-specific set up\n"
835 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
836 "fdt resize - Resize fdt to size + padding to 4k addr\n"
837 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
838 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
839 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
840 "fdt mknode <path> <node> - Create a new node after <path>\n"
841 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
842 "fdt header - Display header info\n"
843 "fdt bootcpu <id> - Set boot cpuid\n"
844 "fdt memory <addr> <size> - Add/Update memory node\n"
845 "fdt rsvmem print - Show current mem reserves\n"
846 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
847 "fdt rsvmem delete <index> - Delete a mem reserves\n"
848 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
849 " <start>/<end> - initrd start/end addr\n"
850 "NOTE: Dereference aliases by omiting the leading '/', "
851 "e.g. fdt print ethernet0."