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)
59 setenv_addr("fdtaddr", addr);
63 * Get a value from the fdt and format it to be set in the environment
65 static int fdt_value_setenv(const void *nodep, int len, const char *var)
67 if (is_printable_string(nodep, len))
68 setenv(var, (void *)nodep);
72 sprintf(buf, "0x%08X", *(uint32_t *)nodep);
74 } else if (len%4 == 0 && len <= 20) {
75 /* Needed to print things like sha1 hashes. */
79 for (i = 0; i < len; i += sizeof(unsigned int))
80 sprintf(buf + (i * 2), "%08x",
81 *(unsigned int *)(nodep + i));
84 printf("error: unprintable value\n");
91 * Flattened Device Tree command, see the help for parameter definitions.
93 static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
99 * Set the address of the fdt
101 if (argv[1][0] == 'a') {
104 * Set the address [and length] of the fdt.
110 printf("The address of the fdt is %p\n", working_fdt);
114 addr = simple_strtoul(argv[2], NULL, 16);
115 set_working_fdt_addr((void *)addr);
125 * Optional new length
127 len = simple_strtoul(argv[3], NULL, 16);
128 if (len < fdt_totalsize(working_fdt)) {
129 printf ("New length %d < existing length %d, "
131 len, fdt_totalsize(working_fdt));
134 * Open in place with a new length.
136 err = fdt_open_into(working_fdt, working_fdt, len);
138 printf ("libfdt fdt_open_into(): %s\n",
144 return CMD_RET_SUCCESS;
149 "No FDT memory address configured. Please configure\n"
150 "the FDT address via \"fdt addr <address>\" command.\n"
152 return CMD_RET_FAILURE;
156 * Move the working_fdt
158 if (strncmp(argv[1], "mo", 2) == 0) {
159 struct fdt_header *newaddr;
164 return CMD_RET_USAGE;
167 * Set the address and length of the fdt.
169 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
174 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
177 * If the user specifies a length, use that. Otherwise use the
181 len = fdt_totalsize(working_fdt);
183 len = simple_strtoul(argv[4], NULL, 16);
184 if (len < fdt_totalsize(working_fdt)) {
185 printf ("New length 0x%X < existing length "
187 len, fdt_totalsize(working_fdt));
193 * Copy to the new location.
195 err = fdt_open_into(working_fdt, newaddr, len);
197 printf ("libfdt fdt_open_into(): %s\n",
201 working_fdt = newaddr;
206 } else if (strncmp(argv[1], "mk", 2) == 0) {
207 char *pathp; /* path */
208 char *nodep; /* new node to add */
209 int nodeoffset; /* node offset from libfdt */
213 * Parameters: Node path, new node to be appended to the path.
216 return CMD_RET_USAGE;
221 nodeoffset = fdt_path_offset (working_fdt, pathp);
222 if (nodeoffset < 0) {
224 * Not found or something else bad happened.
226 printf ("libfdt fdt_path_offset() returned %s\n",
227 fdt_strerror(nodeoffset));
230 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
232 printf ("libfdt fdt_add_subnode(): %s\n",
238 * Set the value of a property in the working_fdt.
240 } else if (argv[1][0] == 's') {
241 char *pathp; /* path */
242 char *prop; /* property */
243 int nodeoffset; /* node offset from libfdt */
244 static char data[SCRATCHPAD]; /* storage for the property */
245 int len; /* new length of the property */
246 int ret; /* return value */
249 * Parameters: Node path, property, optional value.
252 return CMD_RET_USAGE;
259 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
264 nodeoffset = fdt_path_offset (working_fdt, pathp);
265 if (nodeoffset < 0) {
267 * Not found or something else bad happened.
269 printf ("libfdt fdt_path_offset() returned %s\n",
270 fdt_strerror(nodeoffset));
274 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
276 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
280 /********************************************************************
281 * Get the value of a property in the working_fdt.
282 ********************************************************************/
283 } else if (argv[1][0] == 'g') {
284 char *subcmd; /* sub-command */
285 char *pathp; /* path */
286 char *prop; /* property */
287 char *var; /* variable to store result */
288 int nodeoffset; /* node offset from libfdt */
289 const void *nodep; /* property node pointer */
290 int len = 0; /* new length of the property */
293 * Parameters: Node path, property, optional value.
296 return CMD_RET_USAGE;
300 if (argc < 6 && subcmd[0] != 's')
301 return CMD_RET_USAGE;
307 nodeoffset = fdt_path_offset(working_fdt, pathp);
308 if (nodeoffset < 0) {
310 * Not found or something else bad happened.
312 printf("libfdt fdt_path_offset() returned %s\n",
313 fdt_strerror(nodeoffset));
317 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
319 int startDepth = fdt_node_depth(
320 working_fdt, nodeoffset);
321 int curDepth = startDepth;
323 int nextNodeOffset = fdt_next_node(
324 working_fdt, nodeoffset, &curDepth);
326 if (subcmd[0] == 'n')
327 reqIndex = simple_strtoul(argv[5], NULL, 16);
329 while (curDepth > startDepth) {
330 if (curDepth == startDepth + 1)
332 if (subcmd[0] == 'n' && curIndex == reqIndex) {
333 const char *nodeName = fdt_get_name(
334 working_fdt, nextNodeOffset, NULL);
336 setenv(var, (char *)nodeName);
339 nextNodeOffset = fdt_next_node(
340 working_fdt, nextNodeOffset, &curDepth);
341 if (nextNodeOffset < 0)
344 if (subcmd[0] == 's') {
345 /* get the num nodes at this level */
346 setenv_ulong(var, curIndex + 1);
348 /* node index not found */
349 printf("libfdt node not found\n");
354 working_fdt, nodeoffset, prop, &len);
356 /* no property value */
359 } else if (len > 0) {
360 if (subcmd[0] == 'v') {
363 ret = fdt_value_setenv(nodep, len, var);
366 } else if (subcmd[0] == 'a') {
370 sprintf(buf, "0x%p", nodep);
372 } else if (subcmd[0] == 's') {
376 sprintf(buf, "0x%08X", len);
379 return CMD_RET_USAGE;
382 printf("libfdt fdt_getprop(): %s\n",
389 * Print (recursive) / List (single level)
391 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
392 int depth = MAX_LEVEL; /* how deep to print */
393 char *pathp; /* path */
394 char *prop; /* property */
395 int ret; /* return value */
396 static char root[2] = "/";
399 * list is an alias for print, but limited to 1 level
401 if (argv[1][0] == 'l') {
406 * Get the starting path. The root node is an oddball,
407 * the offset is zero and has no name.
418 ret = fdt_print(pathp, prop, depth);
423 * Remove a property/node
425 } else if (strncmp(argv[1], "rm", 2) == 0) {
426 int nodeoffset; /* node offset from libfdt */
430 * Get the path. The root node is an oddball, the offset
431 * is zero and has no name.
433 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
434 if (nodeoffset < 0) {
436 * Not found or something else bad happened.
438 printf ("libfdt fdt_path_offset() returned %s\n",
439 fdt_strerror(nodeoffset));
443 * Do the delete. A fourth parameter means delete a property,
444 * otherwise delete the node.
447 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
449 printf("libfdt fdt_delprop(): %s\n",
454 err = fdt_del_node(working_fdt, nodeoffset);
456 printf("libfdt fdt_del_node(): %s\n",
463 * Display header info
465 } else if (argv[1][0] == 'h') {
466 u32 version = fdt_version(working_fdt);
467 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
468 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
469 fdt_totalsize(working_fdt));
470 printf("off_dt_struct:\t\t0x%x\n",
471 fdt_off_dt_struct(working_fdt));
472 printf("off_dt_strings:\t\t0x%x\n",
473 fdt_off_dt_strings(working_fdt));
474 printf("off_mem_rsvmap:\t\t0x%x\n",
475 fdt_off_mem_rsvmap(working_fdt));
476 printf("version:\t\t%d\n", version);
477 printf("last_comp_version:\t%d\n",
478 fdt_last_comp_version(working_fdt));
480 printf("boot_cpuid_phys:\t0x%x\n",
481 fdt_boot_cpuid_phys(working_fdt));
483 printf("size_dt_strings:\t0x%x\n",
484 fdt_size_dt_strings(working_fdt));
486 printf("size_dt_struct:\t\t0x%x\n",
487 fdt_size_dt_struct(working_fdt));
488 printf("number mem_rsv:\t\t0x%x\n",
489 fdt_num_mem_rsv(working_fdt));
495 } else if (strncmp(argv[1], "boo", 3) == 0) {
496 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
497 fdt_set_boot_cpuid_phys(working_fdt, tmp);
502 } else if (strncmp(argv[1], "me", 2) == 0) {
505 addr = simple_strtoull(argv[2], NULL, 16);
506 size = simple_strtoull(argv[3], NULL, 16);
507 err = fdt_fixup_memory(working_fdt, addr, size);
512 * mem reserve commands
514 } else if (strncmp(argv[1], "rs", 2) == 0) {
515 if (argv[2][0] == 'p') {
517 int total = fdt_num_mem_rsv(working_fdt);
519 printf("index\t\t start\t\t size\n");
520 printf("-------------------------------"
521 "-----------------\n");
522 for (j = 0; j < total; j++) {
523 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
525 printf("libfdt fdt_get_mem_rsv(): %s\n",
529 printf(" %x\t%08x%08x\t%08x%08x\n", j,
531 (u32)(addr & 0xffffffff),
533 (u32)(size & 0xffffffff));
535 } else if (argv[2][0] == 'a') {
538 addr = simple_strtoull(argv[3], NULL, 16);
539 size = simple_strtoull(argv[4], NULL, 16);
540 err = fdt_add_mem_rsv(working_fdt, addr, size);
543 printf("libfdt fdt_add_mem_rsv(): %s\n",
547 } else if (argv[2][0] == 'd') {
548 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
549 int err = fdt_del_mem_rsv(working_fdt, idx);
552 printf("libfdt fdt_del_mem_rsv(): %s\n",
557 /* Unrecognized command */
558 return CMD_RET_USAGE;
561 #ifdef CONFIG_OF_BOARD_SETUP
562 /* Call the board-specific fixup routine */
563 else if (strncmp(argv[1], "boa", 3) == 0)
564 ft_board_setup(working_fdt, gd->bd);
566 /* Create a chosen node */
567 else if (argv[1][0] == 'c') {
568 unsigned long initrd_start = 0, initrd_end = 0;
570 if ((argc != 2) && (argc != 4))
571 return CMD_RET_USAGE;
574 initrd_start = simple_strtoul(argv[2], NULL, 16);
575 initrd_end = simple_strtoul(argv[3], NULL, 16);
578 fdt_chosen(working_fdt, 1);
579 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
582 else if (strncmp(argv[1], "re", 2) == 0) {
583 fdt_resize(working_fdt);
586 /* Unrecognized command */
587 return CMD_RET_USAGE;
593 /****************************************************************************/
595 static int fdt_valid(void)
599 if (working_fdt == NULL) {
600 printf ("The address of the fdt is invalid (NULL).\n");
604 err = fdt_check_header(working_fdt);
606 return 1; /* valid */
609 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
611 * Be more informative on bad version.
613 if (err == -FDT_ERR_BADVERSION) {
614 if (fdt_version(working_fdt) <
615 FDT_FIRST_SUPPORTED_VERSION) {
616 printf (" - too old, fdt %d < %d",
617 fdt_version(working_fdt),
618 FDT_FIRST_SUPPORTED_VERSION);
621 if (fdt_last_comp_version(working_fdt) >
622 FDT_LAST_SUPPORTED_VERSION) {
623 printf (" - too new, fdt %d > %d",
624 fdt_version(working_fdt),
625 FDT_LAST_SUPPORTED_VERSION);
636 /****************************************************************************/
639 * Parse the user's input, partially heuristic. Valid formats:
640 * <0x00112233 4 05> - an array of cells. Numbers follow standard
642 * [00 11 22 .. nn] - byte stream
643 * "string" - If the the value doesn't start with "<" or "[", it is
644 * treated as a string. Note that the quotes are
645 * stripped by the parser before we get the string.
646 * newval: An array of strings containing the new property as specified
647 * on the command line
648 * count: The number of strings in the array
649 * data: A bytestream to be placed in the property
650 * len: The length of the resulting bytestream
652 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
654 char *cp; /* temporary char pointer */
655 char *newp; /* temporary newval char pointer */
656 unsigned long tmp; /* holds converted values */
662 /* An array of cells */
665 while ((*newp != '>') && (stridx < count)) {
667 * Keep searching until we find that last ">"
668 * That way users don't have to escape the spaces
671 newp = newval[++stridx];
676 tmp = simple_strtoul(cp, &newp, 0);
677 *(__be32 *)data = __cpu_to_be32(tmp);
681 /* If the ptr didn't advance, something went wrong */
682 if ((newp - cp) <= 0) {
683 printf("Sorry, I could not convert \"%s\"\n",
693 printf("Unexpected character '%c'\n", *newp);
696 } else if (*newp == '[') {
698 * Byte stream. Convert the values.
701 while ((stridx < count) && (*newp != ']')) {
705 newp = newval[++stridx];
708 if (!isxdigit(*newp))
710 tmp = simple_strtoul(newp, &newp, 16);
711 *data++ = tmp & 0xFF;
715 printf("Unexpected character '%c'\n", *newp);
720 * Assume it is one or more strings. Copy it into our
721 * data area for convenience (including the
722 * terminating '\0's).
724 while (stridx < count) {
725 size_t length = strlen(newp) + 1;
729 newp = newval[++stridx];
735 /****************************************************************************/
738 * Heuristic to guess if this is a string or concatenated strings.
741 static int is_printable_string(const void *data, int len)
743 const char *s = data;
745 /* zero length is not */
749 /* must terminate with zero or '\n' */
750 if (s[len - 1] != '\0' && s[len - 1] != '\n')
753 /* printable or a null byte (concatenated strings) */
754 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
756 * If we see a null, there are three possibilities:
757 * 1) If len == 1, it is the end of the string, printable
758 * 2) Next character also a null, not printable.
759 * 3) Next character not a null, continue to check.
771 /* Not the null termination, or not done yet: not printable */
772 if (*s != '\0' || (len != 0))
780 * Print the property in the best format, a heuristic guess. Print as
781 * a string, concatenated strings, a byte, word, double word, or (if all
782 * else fails) it is printed as a stream of bytes.
784 static void print_data(const void *data, int len)
788 /* no data, don't print */
793 * It is a string, but it may have multiple strings (embedded '\0's).
795 if (is_printable_string(data, len)) {
802 j += strlen(data) + 1;
803 data += strlen(data) + 1;
810 if (len > CONFIG_CMD_FDT_MAX_DUMP)
811 printf("* 0x%p [0x%08x]", data, len);
816 for (j = 0, p = data; j < len/4; j++)
817 printf("0x%08x%s", fdt32_to_cpu(p[j]),
818 j < (len/4 - 1) ? " " : "");
821 } else { /* anything else... hexdump */
822 if (len > CONFIG_CMD_FDT_MAX_DUMP)
823 printf("* 0x%p [0x%08x]", data, len);
828 for (j = 0, s = data; j < len; j++)
829 printf("%02x%s", s[j], j < len - 1 ? " " : "");
835 /****************************************************************************/
838 * Recursively print (a portion of) the working_fdt. The depth parameter
839 * determines how deeply nested the fdt is printed.
841 static int fdt_print(const char *pathp, char *prop, int depth)
843 static char tabs[MAX_LEVEL+1] =
844 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
845 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
846 const void *nodep; /* property node pointer */
847 int nodeoffset; /* node offset from libfdt */
848 int nextoffset; /* next node offset from libfdt */
849 uint32_t tag; /* tag */
850 int len; /* length of the property */
851 int level = 0; /* keep track of nesting level */
852 const struct fdt_property *fdt_prop;
854 nodeoffset = fdt_path_offset (working_fdt, pathp);
855 if (nodeoffset < 0) {
857 * Not found or something else bad happened.
859 printf ("libfdt fdt_path_offset() returned %s\n",
860 fdt_strerror(nodeoffset));
864 * The user passed in a property as well as node path.
865 * Print only the given property and then return.
868 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
870 /* no property value */
871 printf("%s %s\n", pathp, prop);
873 } else if (len > 0) {
874 printf("%s = ", prop);
875 print_data (nodep, len);
879 printf ("libfdt fdt_getprop(): %s\n",
886 * The user passed in a node path and no property,
887 * print the node and all subnodes.
890 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
893 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
894 if (level <= depth) {
896 pathp = "/* NULL pointer error */";
898 pathp = "/"; /* root is nameless */
900 &tabs[MAX_LEVEL - level], pathp);
903 if (level >= MAX_LEVEL) {
904 printf("Nested too deep, aborting.\n");
911 printf("%s};\n", &tabs[MAX_LEVEL - level]);
913 level = -1; /* exit the loop */
917 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
919 pathp = fdt_string(working_fdt,
920 fdt32_to_cpu(fdt_prop->nameoff));
921 len = fdt32_to_cpu(fdt_prop->len);
922 nodep = fdt_prop->data;
924 printf ("libfdt fdt_getprop(): %s\n",
927 } else if (len == 0) {
928 /* the property has no value */
931 &tabs[MAX_LEVEL - level],
934 if (level <= depth) {
936 &tabs[MAX_LEVEL - level],
938 print_data (nodep, len);
944 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
950 printf("Unknown tag 0x%08X\n", tag);
953 nodeoffset = nextoffset;
958 /********************************************************************/
959 #ifdef CONFIG_SYS_LONGHELP
960 static char fdt_help_text[] =
961 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
962 #ifdef CONFIG_OF_BOARD_SETUP
963 "fdt boardsetup - Do board-specific set up\n"
965 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
966 "fdt resize - Resize fdt to size + padding to 4k addr\n"
967 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
968 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
969 "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
970 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
971 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
972 "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
973 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
974 "fdt mknode <path> <node> - Create a new node after <path>\n"
975 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
976 "fdt header - Display header info\n"
977 "fdt bootcpu <id> - Set boot cpuid\n"
978 "fdt memory <addr> <size> - Add/Update memory node\n"
979 "fdt rsvmem print - Show current mem reserves\n"
980 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
981 "fdt rsvmem delete <index> - Delete a mem reserves\n"
982 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
983 " <start>/<end> - initrd start/end addr\n"
984 "NOTE: Dereference aliases by omiting the leading '/', "
985 "e.g. fdt print ethernet0.";
990 "flattened device tree utility commands", fdt_help_text