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>
33 #include <fdt_support.h>
35 #define MAX_LEVEL 32 /* how deeply nested we will go */
36 #define SCRATCHPAD 1024 /* bytes of scratchpad memory */
37 #ifndef CONFIG_CMD_FDT_MAX_DUMP
38 #define CONFIG_CMD_FDT_MAX_DUMP 64
42 * Global data (for the gd->bd)
44 DECLARE_GLOBAL_DATA_PTR;
46 static int fdt_valid(void);
47 static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
48 static int fdt_print(const char *pathp, char *prop, int depth);
49 static int is_printable_string(const void *data, int len);
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 * Get a value from the fdt and format it to be set in the environment
69 static int fdt_value_setenv(const void *nodep, int len, const char *var)
71 if (is_printable_string(nodep, len))
72 setenv(var, (void *)nodep);
76 sprintf(buf, "0x%08X", *(uint32_t *)nodep);
78 } else if (len%4 == 0 && len <= 20) {
79 /* Needed to print things like sha1 hashes. */
83 for (i = 0; i < len; i += sizeof(unsigned int))
84 sprintf(buf + (i * 2), "%08x",
85 *(unsigned int *)(nodep + i));
88 printf("error: unprintable value\n");
95 * Flattened Device Tree command, see the help for parameter definitions.
97 static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
100 return CMD_RET_USAGE;
103 * Set the address of the fdt
105 if (argv[1][0] == 'a') {
108 * Set the address [and length] of the fdt.
114 printf("The address of the fdt is %p\n", working_fdt);
118 addr = simple_strtoul(argv[2], NULL, 16);
119 set_working_fdt_addr((void *)addr);
129 * Optional new length
131 len = simple_strtoul(argv[3], NULL, 16);
132 if (len < fdt_totalsize(working_fdt)) {
133 printf ("New length %d < existing length %d, "
135 len, fdt_totalsize(working_fdt));
138 * Open in place with a new length.
140 err = fdt_open_into(working_fdt, working_fdt, len);
142 printf ("libfdt fdt_open_into(): %s\n",
148 return CMD_RET_SUCCESS;
153 "No FDT memory address configured. Please configure\n"
154 "the FDT address via \"fdt addr <address>\" command.\n"
156 return CMD_RET_FAILURE;
160 * Move the working_fdt
162 if (strncmp(argv[1], "mo", 2) == 0) {
163 struct fdt_header *newaddr;
168 return CMD_RET_USAGE;
171 * Set the address and length of the fdt.
173 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
178 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
181 * If the user specifies a length, use that. Otherwise use the
185 len = fdt_totalsize(working_fdt);
187 len = simple_strtoul(argv[4], NULL, 16);
188 if (len < fdt_totalsize(working_fdt)) {
189 printf ("New length 0x%X < existing length "
191 len, fdt_totalsize(working_fdt));
197 * Copy to the new location.
199 err = fdt_open_into(working_fdt, newaddr, len);
201 printf ("libfdt fdt_open_into(): %s\n",
205 working_fdt = newaddr;
210 } else if (strncmp(argv[1], "mk", 2) == 0) {
211 char *pathp; /* path */
212 char *nodep; /* new node to add */
213 int nodeoffset; /* node offset from libfdt */
217 * Parameters: Node path, new node to be appended to the path.
220 return CMD_RET_USAGE;
225 nodeoffset = fdt_path_offset (working_fdt, pathp);
226 if (nodeoffset < 0) {
228 * Not found or something else bad happened.
230 printf ("libfdt fdt_path_offset() returned %s\n",
231 fdt_strerror(nodeoffset));
234 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
236 printf ("libfdt fdt_add_subnode(): %s\n",
242 * Set the value of a property in the working_fdt.
244 } else if (argv[1][0] == 's') {
245 char *pathp; /* path */
246 char *prop; /* property */
247 int nodeoffset; /* node offset from libfdt */
248 static char data[SCRATCHPAD]; /* storage for the property */
249 int len; /* new length of the property */
250 int ret; /* return value */
253 * Parameters: Node path, property, optional value.
256 return CMD_RET_USAGE;
263 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
268 nodeoffset = fdt_path_offset (working_fdt, pathp);
269 if (nodeoffset < 0) {
271 * Not found or something else bad happened.
273 printf ("libfdt fdt_path_offset() returned %s\n",
274 fdt_strerror(nodeoffset));
278 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
280 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
284 /********************************************************************
285 * Get the value of a property in the working_fdt.
286 ********************************************************************/
287 } else if (argv[1][0] == 'g') {
288 char *subcmd; /* sub-command */
289 char *pathp; /* path */
290 char *prop; /* property */
291 char *var; /* variable to store result */
292 int nodeoffset; /* node offset from libfdt */
293 const void *nodep; /* property node pointer */
294 int len = 0; /* new length of the property */
297 * Parameters: Node path, property, optional value.
300 return CMD_RET_USAGE;
304 if (argc < 6 && subcmd[0] != 's')
305 return CMD_RET_USAGE;
311 nodeoffset = fdt_path_offset(working_fdt, pathp);
312 if (nodeoffset < 0) {
314 * Not found or something else bad happened.
316 printf("libfdt fdt_path_offset() returned %s\n",
317 fdt_strerror(nodeoffset));
321 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
323 int startDepth = fdt_node_depth(
324 working_fdt, nodeoffset);
325 int curDepth = startDepth;
327 int nextNodeOffset = fdt_next_node(
328 working_fdt, nodeoffset, &curDepth);
330 if (subcmd[0] == 'n')
331 reqIndex = simple_strtoul(argv[5], NULL, 16);
333 while (curDepth > startDepth) {
334 if (curDepth == startDepth + 1)
336 if (subcmd[0] == 'n' && curIndex == reqIndex) {
337 const char *nodeName = fdt_get_name(
338 working_fdt, nextNodeOffset, NULL);
340 setenv(var, (char *)nodeName);
343 nextNodeOffset = fdt_next_node(
344 working_fdt, nextNodeOffset, &curDepth);
345 if (nextNodeOffset < 0)
348 if (subcmd[0] == 's') {
349 /* get the num nodes at this level */
352 sprintf(buf, "%d", curIndex + 1);
355 /* node index not found */
356 printf("libfdt node not found\n");
361 working_fdt, nodeoffset, prop, &len);
363 /* no property value */
366 } else if (len > 0) {
367 if (subcmd[0] == 'v') {
370 ret = fdt_value_setenv(nodep, len, var);
373 } else if (subcmd[0] == 'a') {
377 sprintf(buf, "0x%p", nodep);
379 } else if (subcmd[0] == 's') {
383 sprintf(buf, "0x%08X", len);
386 return CMD_RET_USAGE;
389 printf("libfdt fdt_getprop(): %s\n",
396 * Print (recursive) / List (single level)
398 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
399 int depth = MAX_LEVEL; /* how deep to print */
400 char *pathp; /* path */
401 char *prop; /* property */
402 int ret; /* return value */
403 static char root[2] = "/";
406 * list is an alias for print, but limited to 1 level
408 if (argv[1][0] == 'l') {
413 * Get the starting path. The root node is an oddball,
414 * the offset is zero and has no name.
425 ret = fdt_print(pathp, prop, depth);
430 * Remove a property/node
432 } else if (strncmp(argv[1], "rm", 2) == 0) {
433 int nodeoffset; /* node offset from libfdt */
437 * Get the path. The root node is an oddball, the offset
438 * is zero and has no name.
440 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
441 if (nodeoffset < 0) {
443 * Not found or something else bad happened.
445 printf ("libfdt fdt_path_offset() returned %s\n",
446 fdt_strerror(nodeoffset));
450 * Do the delete. A fourth parameter means delete a property,
451 * otherwise delete the node.
454 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
456 printf("libfdt fdt_delprop(): %s\n",
461 err = fdt_del_node(working_fdt, nodeoffset);
463 printf("libfdt fdt_del_node(): %s\n",
470 * Display header info
472 } else if (argv[1][0] == 'h') {
473 u32 version = fdt_version(working_fdt);
474 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
475 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
476 fdt_totalsize(working_fdt));
477 printf("off_dt_struct:\t\t0x%x\n",
478 fdt_off_dt_struct(working_fdt));
479 printf("off_dt_strings:\t\t0x%x\n",
480 fdt_off_dt_strings(working_fdt));
481 printf("off_mem_rsvmap:\t\t0x%x\n",
482 fdt_off_mem_rsvmap(working_fdt));
483 printf("version:\t\t%d\n", version);
484 printf("last_comp_version:\t%d\n",
485 fdt_last_comp_version(working_fdt));
487 printf("boot_cpuid_phys:\t0x%x\n",
488 fdt_boot_cpuid_phys(working_fdt));
490 printf("size_dt_strings:\t0x%x\n",
491 fdt_size_dt_strings(working_fdt));
493 printf("size_dt_struct:\t\t0x%x\n",
494 fdt_size_dt_struct(working_fdt));
495 printf("number mem_rsv:\t\t0x%x\n",
496 fdt_num_mem_rsv(working_fdt));
502 } else if (strncmp(argv[1], "boo", 3) == 0) {
503 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
504 fdt_set_boot_cpuid_phys(working_fdt, tmp);
509 } else if (strncmp(argv[1], "me", 2) == 0) {
512 addr = simple_strtoull(argv[2], NULL, 16);
513 size = simple_strtoull(argv[3], NULL, 16);
514 err = fdt_fixup_memory(working_fdt, addr, size);
519 * mem reserve commands
521 } else if (strncmp(argv[1], "rs", 2) == 0) {
522 if (argv[2][0] == 'p') {
524 int total = fdt_num_mem_rsv(working_fdt);
526 printf("index\t\t start\t\t size\n");
527 printf("-------------------------------"
528 "-----------------\n");
529 for (j = 0; j < total; j++) {
530 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
532 printf("libfdt fdt_get_mem_rsv(): %s\n",
536 printf(" %x\t%08x%08x\t%08x%08x\n", j,
538 (u32)(addr & 0xffffffff),
540 (u32)(size & 0xffffffff));
542 } else if (argv[2][0] == 'a') {
545 addr = simple_strtoull(argv[3], NULL, 16);
546 size = simple_strtoull(argv[4], NULL, 16);
547 err = fdt_add_mem_rsv(working_fdt, addr, size);
550 printf("libfdt fdt_add_mem_rsv(): %s\n",
554 } else if (argv[2][0] == 'd') {
555 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
556 int err = fdt_del_mem_rsv(working_fdt, idx);
559 printf("libfdt fdt_del_mem_rsv(): %s\n",
564 /* Unrecognized command */
565 return CMD_RET_USAGE;
568 #ifdef CONFIG_OF_BOARD_SETUP
569 /* Call the board-specific fixup routine */
570 else if (strncmp(argv[1], "boa", 3) == 0)
571 ft_board_setup(working_fdt, gd->bd);
573 /* Create a chosen node */
574 else if (argv[1][0] == 'c') {
575 unsigned long initrd_start = 0, initrd_end = 0;
577 if ((argc != 2) && (argc != 4))
578 return CMD_RET_USAGE;
581 initrd_start = simple_strtoul(argv[2], NULL, 16);
582 initrd_end = simple_strtoul(argv[3], NULL, 16);
585 fdt_chosen(working_fdt, 1);
586 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
589 else if (strncmp(argv[1], "re", 2) == 0) {
590 fdt_resize(working_fdt);
593 /* Unrecognized command */
594 return CMD_RET_USAGE;
600 /****************************************************************************/
602 static int fdt_valid(void)
606 if (working_fdt == NULL) {
607 printf ("The address of the fdt is invalid (NULL).\n");
611 err = fdt_check_header(working_fdt);
613 return 1; /* valid */
616 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
618 * Be more informative on bad version.
620 if (err == -FDT_ERR_BADVERSION) {
621 if (fdt_version(working_fdt) <
622 FDT_FIRST_SUPPORTED_VERSION) {
623 printf (" - too old, fdt %d < %d",
624 fdt_version(working_fdt),
625 FDT_FIRST_SUPPORTED_VERSION);
628 if (fdt_last_comp_version(working_fdt) >
629 FDT_LAST_SUPPORTED_VERSION) {
630 printf (" - too new, fdt %d > %d",
631 fdt_version(working_fdt),
632 FDT_LAST_SUPPORTED_VERSION);
643 /****************************************************************************/
646 * Parse the user's input, partially heuristic. Valid formats:
647 * <0x00112233 4 05> - an array of cells. Numbers follow standard
649 * [00 11 22 .. nn] - byte stream
650 * "string" - If the the value doesn't start with "<" or "[", it is
651 * treated as a string. Note that the quotes are
652 * stripped by the parser before we get the string.
653 * newval: An array of strings containing the new property as specified
654 * on the command line
655 * count: The number of strings in the array
656 * data: A bytestream to be placed in the property
657 * len: The length of the resulting bytestream
659 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
661 char *cp; /* temporary char pointer */
662 char *newp; /* temporary newval char pointer */
663 unsigned long tmp; /* holds converted values */
669 /* An array of cells */
672 while ((*newp != '>') && (stridx < count)) {
674 * Keep searching until we find that last ">"
675 * That way users don't have to escape the spaces
678 newp = newval[++stridx];
683 tmp = simple_strtoul(cp, &newp, 0);
684 *(__be32 *)data = __cpu_to_be32(tmp);
688 /* If the ptr didn't advance, something went wrong */
689 if ((newp - cp) <= 0) {
690 printf("Sorry, I could not convert \"%s\"\n",
700 printf("Unexpected character '%c'\n", *newp);
703 } else if (*newp == '[') {
705 * Byte stream. Convert the values.
708 while ((stridx < count) && (*newp != ']')) {
712 newp = newval[++stridx];
715 if (!isxdigit(*newp))
717 tmp = simple_strtoul(newp, &newp, 16);
718 *data++ = tmp & 0xFF;
722 printf("Unexpected character '%c'\n", *newp);
727 * Assume it is one or more strings. Copy it into our
728 * data area for convenience (including the
729 * terminating '\0's).
731 while (stridx < count) {
732 size_t length = strlen(newp) + 1;
736 newp = newval[++stridx];
742 /****************************************************************************/
745 * Heuristic to guess if this is a string or concatenated strings.
748 static int is_printable_string(const void *data, int len)
750 const char *s = data;
752 /* zero length is not */
756 /* must terminate with zero or '\n' */
757 if (s[len - 1] != '\0' && s[len - 1] != '\n')
760 /* printable or a null byte (concatenated strings) */
761 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
763 * If we see a null, there are three possibilities:
764 * 1) If len == 1, it is the end of the string, printable
765 * 2) Next character also a null, not printable.
766 * 3) Next character not a null, continue to check.
778 /* Not the null termination, or not done yet: not printable */
779 if (*s != '\0' || (len != 0))
787 * Print the property in the best format, a heuristic guess. Print as
788 * a string, concatenated strings, a byte, word, double word, or (if all
789 * else fails) it is printed as a stream of bytes.
791 static void print_data(const void *data, int len)
795 /* no data, don't print */
800 * It is a string, but it may have multiple strings (embedded '\0's).
802 if (is_printable_string(data, len)) {
809 j += strlen(data) + 1;
810 data += strlen(data) + 1;
817 if (len > CONFIG_CMD_FDT_MAX_DUMP)
818 printf("* 0x%p [0x%08x]", data, len);
823 for (j = 0, p = data; j < len/4; j++)
824 printf("0x%08x%s", fdt32_to_cpu(p[j]),
825 j < (len/4 - 1) ? " " : "");
828 } else { /* anything else... hexdump */
829 if (len > CONFIG_CMD_FDT_MAX_DUMP)
830 printf("* 0x%p [0x%08x]", data, len);
835 for (j = 0, s = data; j < len; j++)
836 printf("%02x%s", s[j], j < len - 1 ? " " : "");
842 /****************************************************************************/
845 * Recursively print (a portion of) the working_fdt. The depth parameter
846 * determines how deeply nested the fdt is printed.
848 static int fdt_print(const char *pathp, char *prop, int depth)
850 static char tabs[MAX_LEVEL+1] =
851 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
852 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
853 const void *nodep; /* property node pointer */
854 int nodeoffset; /* node offset from libfdt */
855 int nextoffset; /* next node offset from libfdt */
856 uint32_t tag; /* tag */
857 int len; /* length of the property */
858 int level = 0; /* keep track of nesting level */
859 const struct fdt_property *fdt_prop;
861 nodeoffset = fdt_path_offset (working_fdt, pathp);
862 if (nodeoffset < 0) {
864 * Not found or something else bad happened.
866 printf ("libfdt fdt_path_offset() returned %s\n",
867 fdt_strerror(nodeoffset));
871 * The user passed in a property as well as node path.
872 * Print only the given property and then return.
875 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
877 /* no property value */
878 printf("%s %s\n", pathp, prop);
880 } else if (len > 0) {
881 printf("%s = ", prop);
882 print_data (nodep, len);
886 printf ("libfdt fdt_getprop(): %s\n",
893 * The user passed in a node path and no property,
894 * print the node and all subnodes.
897 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
900 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
901 if (level <= depth) {
903 pathp = "/* NULL pointer error */";
905 pathp = "/"; /* root is nameless */
907 &tabs[MAX_LEVEL - level], pathp);
910 if (level >= MAX_LEVEL) {
911 printf("Nested too deep, aborting.\n");
918 printf("%s};\n", &tabs[MAX_LEVEL - level]);
920 level = -1; /* exit the loop */
924 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
926 pathp = fdt_string(working_fdt,
927 fdt32_to_cpu(fdt_prop->nameoff));
928 len = fdt32_to_cpu(fdt_prop->len);
929 nodep = fdt_prop->data;
931 printf ("libfdt fdt_getprop(): %s\n",
934 } else if (len == 0) {
935 /* the property has no value */
938 &tabs[MAX_LEVEL - level],
941 if (level <= depth) {
943 &tabs[MAX_LEVEL - level],
945 print_data (nodep, len);
951 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
957 printf("Unknown tag 0x%08X\n", tag);
960 nodeoffset = nextoffset;
965 /********************************************************************/
966 #ifdef CONFIG_SYS_LONGHELP
967 static char fdt_help_text[] =
968 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
969 #ifdef CONFIG_OF_BOARD_SETUP
970 "fdt boardsetup - Do board-specific set up\n"
972 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
973 "fdt resize - Resize fdt to size + padding to 4k addr\n"
974 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
975 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
976 "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
977 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
978 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
979 "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
980 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
981 "fdt mknode <path> <node> - Create a new node after <path>\n"
982 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
983 "fdt header - Display header info\n"
984 "fdt bootcpu <id> - Set boot cpuid\n"
985 "fdt memory <addr> <size> - Add/Update memory node\n"
986 "fdt rsvmem print - Show current mem reserves\n"
987 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
988 "fdt rsvmem delete <index> - Delete a mem reserves\n"
989 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
990 " <start>/<end> - initrd start/end addr\n"
991 "NOTE: Dereference aliases by omiting the leading '/', "
992 "e.g. fdt print ethernet0.";
997 "flattened device tree utility commands", fdt_help_text