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(struct fdt_header **blobp);
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.
107 if (!fdt_valid(&working_fdt))
109 printf("The address of the fdt is %p\n", working_fdt);
113 addr = simple_strtoul(argv[2], NULL, 16);
114 set_working_fdt_addr((void *)addr);
116 if (!fdt_valid(&working_fdt))
123 * Optional new length
125 len = simple_strtoul(argv[3], NULL, 16);
126 if (len < fdt_totalsize(working_fdt)) {
127 printf ("New length %d < existing length %d, "
129 len, fdt_totalsize(working_fdt));
132 * Open in place with a new length.
134 err = fdt_open_into(working_fdt, working_fdt, len);
136 printf ("libfdt fdt_open_into(): %s\n",
142 return CMD_RET_SUCCESS;
147 "No FDT memory address configured. Please configure\n"
148 "the FDT address via \"fdt addr <address>\" command.\n"
150 return CMD_RET_FAILURE;
154 * Move the working_fdt
156 if (strncmp(argv[1], "mo", 2) == 0) {
157 struct fdt_header *newaddr;
162 return CMD_RET_USAGE;
165 * Set the address and length of the fdt.
167 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
168 if (!fdt_valid(&working_fdt))
171 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
174 * If the user specifies a length, use that. Otherwise use the
178 len = fdt_totalsize(working_fdt);
180 len = simple_strtoul(argv[4], NULL, 16);
181 if (len < fdt_totalsize(working_fdt)) {
182 printf ("New length 0x%X < existing length "
184 len, fdt_totalsize(working_fdt));
190 * Copy to the new location.
192 err = fdt_open_into(working_fdt, newaddr, len);
194 printf ("libfdt fdt_open_into(): %s\n",
198 working_fdt = newaddr;
203 } else if (strncmp(argv[1], "mk", 2) == 0) {
204 char *pathp; /* path */
205 char *nodep; /* new node to add */
206 int nodeoffset; /* node offset from libfdt */
210 * Parameters: Node path, new node to be appended to the path.
213 return CMD_RET_USAGE;
218 nodeoffset = fdt_path_offset (working_fdt, pathp);
219 if (nodeoffset < 0) {
221 * Not found or something else bad happened.
223 printf ("libfdt fdt_path_offset() returned %s\n",
224 fdt_strerror(nodeoffset));
227 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
229 printf ("libfdt fdt_add_subnode(): %s\n",
235 * Set the value of a property in the working_fdt.
237 } else if (argv[1][0] == 's') {
238 char *pathp; /* path */
239 char *prop; /* property */
240 int nodeoffset; /* node offset from libfdt */
241 static char data[SCRATCHPAD]; /* storage for the property */
242 int len; /* new length of the property */
243 int ret; /* return value */
246 * Parameters: Node path, property, optional value.
249 return CMD_RET_USAGE;
256 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
261 nodeoffset = fdt_path_offset (working_fdt, pathp);
262 if (nodeoffset < 0) {
264 * Not found or something else bad happened.
266 printf ("libfdt fdt_path_offset() returned %s\n",
267 fdt_strerror(nodeoffset));
271 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
273 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
277 /********************************************************************
278 * Get the value of a property in the working_fdt.
279 ********************************************************************/
280 } else if (argv[1][0] == 'g') {
281 char *subcmd; /* sub-command */
282 char *pathp; /* path */
283 char *prop; /* property */
284 char *var; /* variable to store result */
285 int nodeoffset; /* node offset from libfdt */
286 const void *nodep; /* property node pointer */
287 int len = 0; /* new length of the property */
290 * Parameters: Node path, property, optional value.
293 return CMD_RET_USAGE;
297 if (argc < 6 && subcmd[0] != 's')
298 return CMD_RET_USAGE;
304 nodeoffset = fdt_path_offset(working_fdt, pathp);
305 if (nodeoffset < 0) {
307 * Not found or something else bad happened.
309 printf("libfdt fdt_path_offset() returned %s\n",
310 fdt_strerror(nodeoffset));
314 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
316 int startDepth = fdt_node_depth(
317 working_fdt, nodeoffset);
318 int curDepth = startDepth;
320 int nextNodeOffset = fdt_next_node(
321 working_fdt, nodeoffset, &curDepth);
323 if (subcmd[0] == 'n')
324 reqIndex = simple_strtoul(argv[5], NULL, 16);
326 while (curDepth > startDepth) {
327 if (curDepth == startDepth + 1)
329 if (subcmd[0] == 'n' && curIndex == reqIndex) {
330 const char *nodeName = fdt_get_name(
331 working_fdt, nextNodeOffset, NULL);
333 setenv(var, (char *)nodeName);
336 nextNodeOffset = fdt_next_node(
337 working_fdt, nextNodeOffset, &curDepth);
338 if (nextNodeOffset < 0)
341 if (subcmd[0] == 's') {
342 /* get the num nodes at this level */
343 setenv_ulong(var, curIndex + 1);
345 /* node index not found */
346 printf("libfdt node not found\n");
351 working_fdt, nodeoffset, prop, &len);
353 /* no property value */
356 } else if (len > 0) {
357 if (subcmd[0] == 'v') {
360 ret = fdt_value_setenv(nodep, len, var);
363 } else if (subcmd[0] == 'a') {
367 sprintf(buf, "0x%p", nodep);
369 } else if (subcmd[0] == 's') {
373 sprintf(buf, "0x%08X", len);
376 return CMD_RET_USAGE;
379 printf("libfdt fdt_getprop(): %s\n",
386 * Print (recursive) / List (single level)
388 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
389 int depth = MAX_LEVEL; /* how deep to print */
390 char *pathp; /* path */
391 char *prop; /* property */
392 int ret; /* return value */
393 static char root[2] = "/";
396 * list is an alias for print, but limited to 1 level
398 if (argv[1][0] == 'l') {
403 * Get the starting path. The root node is an oddball,
404 * the offset is zero and has no name.
415 ret = fdt_print(pathp, prop, depth);
420 * Remove a property/node
422 } else if (strncmp(argv[1], "rm", 2) == 0) {
423 int nodeoffset; /* node offset from libfdt */
427 * Get the path. The root node is an oddball, the offset
428 * is zero and has no name.
430 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
431 if (nodeoffset < 0) {
433 * Not found or something else bad happened.
435 printf ("libfdt fdt_path_offset() returned %s\n",
436 fdt_strerror(nodeoffset));
440 * Do the delete. A fourth parameter means delete a property,
441 * otherwise delete the node.
444 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
446 printf("libfdt fdt_delprop(): %s\n",
451 err = fdt_del_node(working_fdt, nodeoffset);
453 printf("libfdt fdt_del_node(): %s\n",
460 * Display header info
462 } else if (argv[1][0] == 'h') {
463 u32 version = fdt_version(working_fdt);
464 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
465 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
466 fdt_totalsize(working_fdt));
467 printf("off_dt_struct:\t\t0x%x\n",
468 fdt_off_dt_struct(working_fdt));
469 printf("off_dt_strings:\t\t0x%x\n",
470 fdt_off_dt_strings(working_fdt));
471 printf("off_mem_rsvmap:\t\t0x%x\n",
472 fdt_off_mem_rsvmap(working_fdt));
473 printf("version:\t\t%d\n", version);
474 printf("last_comp_version:\t%d\n",
475 fdt_last_comp_version(working_fdt));
477 printf("boot_cpuid_phys:\t0x%x\n",
478 fdt_boot_cpuid_phys(working_fdt));
480 printf("size_dt_strings:\t0x%x\n",
481 fdt_size_dt_strings(working_fdt));
483 printf("size_dt_struct:\t\t0x%x\n",
484 fdt_size_dt_struct(working_fdt));
485 printf("number mem_rsv:\t\t0x%x\n",
486 fdt_num_mem_rsv(working_fdt));
492 } else if (strncmp(argv[1], "boo", 3) == 0) {
493 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
494 fdt_set_boot_cpuid_phys(working_fdt, tmp);
499 } else if (strncmp(argv[1], "me", 2) == 0) {
502 addr = simple_strtoull(argv[2], NULL, 16);
503 size = simple_strtoull(argv[3], NULL, 16);
504 err = fdt_fixup_memory(working_fdt, addr, size);
509 * mem reserve commands
511 } else if (strncmp(argv[1], "rs", 2) == 0) {
512 if (argv[2][0] == 'p') {
514 int total = fdt_num_mem_rsv(working_fdt);
516 printf("index\t\t start\t\t size\n");
517 printf("-------------------------------"
518 "-----------------\n");
519 for (j = 0; j < total; j++) {
520 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
522 printf("libfdt fdt_get_mem_rsv(): %s\n",
526 printf(" %x\t%08x%08x\t%08x%08x\n", j,
528 (u32)(addr & 0xffffffff),
530 (u32)(size & 0xffffffff));
532 } else if (argv[2][0] == 'a') {
535 addr = simple_strtoull(argv[3], NULL, 16);
536 size = simple_strtoull(argv[4], NULL, 16);
537 err = fdt_add_mem_rsv(working_fdt, addr, size);
540 printf("libfdt fdt_add_mem_rsv(): %s\n",
544 } else if (argv[2][0] == 'd') {
545 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
546 int err = fdt_del_mem_rsv(working_fdt, idx);
549 printf("libfdt fdt_del_mem_rsv(): %s\n",
554 /* Unrecognized command */
555 return CMD_RET_USAGE;
558 #ifdef CONFIG_OF_BOARD_SETUP
559 /* Call the board-specific fixup routine */
560 else if (strncmp(argv[1], "boa", 3) == 0)
561 ft_board_setup(working_fdt, gd->bd);
563 /* Create a chosen node */
564 else if (argv[1][0] == 'c') {
565 unsigned long initrd_start = 0, initrd_end = 0;
567 if ((argc != 2) && (argc != 4))
568 return CMD_RET_USAGE;
571 initrd_start = simple_strtoul(argv[2], NULL, 16);
572 initrd_end = simple_strtoul(argv[3], NULL, 16);
575 fdt_chosen(working_fdt, 1);
576 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
579 else if (strncmp(argv[1], "re", 2) == 0) {
580 fdt_resize(working_fdt);
583 /* Unrecognized command */
584 return CMD_RET_USAGE;
590 /****************************************************************************/
593 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
595 * @blobp: Pointer to FDT pointer
596 * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL)
598 static int fdt_valid(struct fdt_header **blobp)
600 const void *blob = *blobp;
604 printf ("The address of the fdt is invalid (NULL).\n");
608 err = fdt_check_header(blob);
610 return 1; /* valid */
613 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
615 * Be more informative on bad version.
617 if (err == -FDT_ERR_BADVERSION) {
618 if (fdt_version(blob) <
619 FDT_FIRST_SUPPORTED_VERSION) {
620 printf (" - too old, fdt %d < %d",
622 FDT_FIRST_SUPPORTED_VERSION);
624 if (fdt_last_comp_version(blob) >
625 FDT_LAST_SUPPORTED_VERSION) {
626 printf (" - too new, fdt %d > %d",
628 FDT_LAST_SUPPORTED_VERSION);
638 /****************************************************************************/
641 * Parse the user's input, partially heuristic. Valid formats:
642 * <0x00112233 4 05> - an array of cells. Numbers follow standard
644 * [00 11 22 .. nn] - byte stream
645 * "string" - If the the value doesn't start with "<" or "[", it is
646 * treated as a string. Note that the quotes are
647 * stripped by the parser before we get the string.
648 * newval: An array of strings containing the new property as specified
649 * on the command line
650 * count: The number of strings in the array
651 * data: A bytestream to be placed in the property
652 * len: The length of the resulting bytestream
654 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
656 char *cp; /* temporary char pointer */
657 char *newp; /* temporary newval char pointer */
658 unsigned long tmp; /* holds converted values */
664 /* An array of cells */
667 while ((*newp != '>') && (stridx < count)) {
669 * Keep searching until we find that last ">"
670 * That way users don't have to escape the spaces
673 newp = newval[++stridx];
678 tmp = simple_strtoul(cp, &newp, 0);
679 *(__be32 *)data = __cpu_to_be32(tmp);
683 /* If the ptr didn't advance, something went wrong */
684 if ((newp - cp) <= 0) {
685 printf("Sorry, I could not convert \"%s\"\n",
695 printf("Unexpected character '%c'\n", *newp);
698 } else if (*newp == '[') {
700 * Byte stream. Convert the values.
703 while ((stridx < count) && (*newp != ']')) {
707 newp = newval[++stridx];
710 if (!isxdigit(*newp))
712 tmp = simple_strtoul(newp, &newp, 16);
713 *data++ = tmp & 0xFF;
717 printf("Unexpected character '%c'\n", *newp);
722 * Assume it is one or more strings. Copy it into our
723 * data area for convenience (including the
724 * terminating '\0's).
726 while (stridx < count) {
727 size_t length = strlen(newp) + 1;
731 newp = newval[++stridx];
737 /****************************************************************************/
740 * Heuristic to guess if this is a string or concatenated strings.
743 static int is_printable_string(const void *data, int len)
745 const char *s = data;
747 /* zero length is not */
751 /* must terminate with zero or '\n' */
752 if (s[len - 1] != '\0' && s[len - 1] != '\n')
755 /* printable or a null byte (concatenated strings) */
756 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
758 * If we see a null, there are three possibilities:
759 * 1) If len == 1, it is the end of the string, printable
760 * 2) Next character also a null, not printable.
761 * 3) Next character not a null, continue to check.
773 /* Not the null termination, or not done yet: not printable */
774 if (*s != '\0' || (len != 0))
782 * Print the property in the best format, a heuristic guess. Print as
783 * a string, concatenated strings, a byte, word, double word, or (if all
784 * else fails) it is printed as a stream of bytes.
786 static void print_data(const void *data, int len)
790 /* no data, don't print */
795 * It is a string, but it may have multiple strings (embedded '\0's).
797 if (is_printable_string(data, len)) {
804 j += strlen(data) + 1;
805 data += strlen(data) + 1;
812 if (len > CONFIG_CMD_FDT_MAX_DUMP)
813 printf("* 0x%p [0x%08x]", data, len);
818 for (j = 0, p = data; j < len/4; j++)
819 printf("0x%08x%s", fdt32_to_cpu(p[j]),
820 j < (len/4 - 1) ? " " : "");
823 } else { /* anything else... hexdump */
824 if (len > CONFIG_CMD_FDT_MAX_DUMP)
825 printf("* 0x%p [0x%08x]", data, len);
830 for (j = 0, s = data; j < len; j++)
831 printf("%02x%s", s[j], j < len - 1 ? " " : "");
837 /****************************************************************************/
840 * Recursively print (a portion of) the working_fdt. The depth parameter
841 * determines how deeply nested the fdt is printed.
843 static int fdt_print(const char *pathp, char *prop, int depth)
845 static char tabs[MAX_LEVEL+1] =
846 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
847 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
848 const void *nodep; /* property node pointer */
849 int nodeoffset; /* node offset from libfdt */
850 int nextoffset; /* next node offset from libfdt */
851 uint32_t tag; /* tag */
852 int len; /* length of the property */
853 int level = 0; /* keep track of nesting level */
854 const struct fdt_property *fdt_prop;
856 nodeoffset = fdt_path_offset (working_fdt, pathp);
857 if (nodeoffset < 0) {
859 * Not found or something else bad happened.
861 printf ("libfdt fdt_path_offset() returned %s\n",
862 fdt_strerror(nodeoffset));
866 * The user passed in a property as well as node path.
867 * Print only the given property and then return.
870 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
872 /* no property value */
873 printf("%s %s\n", pathp, prop);
875 } else if (len > 0) {
876 printf("%s = ", prop);
877 print_data (nodep, len);
881 printf ("libfdt fdt_getprop(): %s\n",
888 * The user passed in a node path and no property,
889 * print the node and all subnodes.
892 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
895 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
896 if (level <= depth) {
898 pathp = "/* NULL pointer error */";
900 pathp = "/"; /* root is nameless */
902 &tabs[MAX_LEVEL - level], pathp);
905 if (level >= MAX_LEVEL) {
906 printf("Nested too deep, aborting.\n");
913 printf("%s};\n", &tabs[MAX_LEVEL - level]);
915 level = -1; /* exit the loop */
919 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
921 pathp = fdt_string(working_fdt,
922 fdt32_to_cpu(fdt_prop->nameoff));
923 len = fdt32_to_cpu(fdt_prop->len);
924 nodep = fdt_prop->data;
926 printf ("libfdt fdt_getprop(): %s\n",
929 } else if (len == 0) {
930 /* the property has no value */
933 &tabs[MAX_LEVEL - level],
936 if (level <= depth) {
938 &tabs[MAX_LEVEL - level],
940 print_data (nodep, len);
946 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
952 printf("Unknown tag 0x%08X\n", tag);
955 nodeoffset = nextoffset;
960 /********************************************************************/
961 #ifdef CONFIG_SYS_LONGHELP
962 static char fdt_help_text[] =
963 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
964 #ifdef CONFIG_OF_BOARD_SETUP
965 "fdt boardsetup - Do board-specific set up\n"
967 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
968 "fdt resize - Resize fdt to size + padding to 4k addr\n"
969 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
970 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
971 "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
972 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
973 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
974 "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
975 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
976 "fdt mknode <path> <node> - Create a new node after <path>\n"
977 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
978 "fdt header - Display header info\n"
979 "fdt bootcpu <id> - Set boot cpuid\n"
980 "fdt memory <addr> <size> - Add/Update memory node\n"
981 "fdt rsvmem print - Show current mem reserves\n"
982 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
983 "fdt rsvmem delete <index> - Delete a mem reserves\n"
984 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
985 " <start>/<end> - initrd start/end addr\n"
986 "NOTE: Dereference aliases by omiting the leading '/', "
987 "e.g. fdt print ethernet0.";
992 "flattened device tree utility commands", fdt_help_text