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
);
50 static int is_printable_string(const void *data
, int len
);
53 * The working_fdt points to our working flattened device tree.
55 struct fdt_header
*working_fdt
;
57 void set_working_fdt_addr(void *addr
)
63 sprintf(buf
, "%lx", (unsigned long)addr
);
64 setenv("fdtaddr", buf
);
68 * Get a value from the fdt and format it to be set in the environment
70 static int fdt_value_setenv(const void *nodep
, int len
, const char *var
)
72 if (is_printable_string(nodep
, len
))
73 setenv(var
, (void *)nodep
);
77 sprintf(buf
, "0x%08X", *(uint32_t *)nodep
);
79 } else if (len
%4 == 0 && len
<= 20) {
80 /* Needed to print things like sha1 hashes. */
84 for (i
= 0; i
< len
; i
+= sizeof(unsigned int))
85 sprintf(buf
+ (i
* 2), "%08x",
86 *(unsigned int *)(nodep
+ i
));
89 printf("error: unprintable value\n");
96 * Flattened Device Tree command, see the help for parameter definitions.
98 int do_fdt (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
101 return CMD_RET_USAGE
;
104 * Set the address of the fdt
106 if (argv
[1][0] == 'a') {
109 * Set the address [and length] of the fdt.
115 printf("The address of the fdt is %p\n", working_fdt
);
119 addr
= simple_strtoul(argv
[2], NULL
, 16);
120 set_working_fdt_addr((void *)addr
);
130 * Optional new length
132 len
= simple_strtoul(argv
[3], NULL
, 16);
133 if (len
< fdt_totalsize(working_fdt
)) {
134 printf ("New length %d < existing length %d, "
136 len
, fdt_totalsize(working_fdt
));
139 * Open in place with a new length.
141 err
= fdt_open_into(working_fdt
, working_fdt
, len
);
143 printf ("libfdt fdt_open_into(): %s\n",
149 return CMD_RET_SUCCESS
;
154 "No FDT memory address configured. Please configure\n"
155 "the FDT address via \"fdt addr <address>\" command.\n"
157 return CMD_RET_FAILURE
;
161 * Move the working_fdt
163 if (strncmp(argv
[1], "mo", 2) == 0) {
164 struct fdt_header
*newaddr
;
169 return CMD_RET_USAGE
;
172 * Set the address and length of the fdt.
174 working_fdt
= (struct fdt_header
*)simple_strtoul(argv
[2], NULL
, 16);
179 newaddr
= (struct fdt_header
*)simple_strtoul(argv
[3],NULL
,16);
182 * If the user specifies a length, use that. Otherwise use the
186 len
= fdt_totalsize(working_fdt
);
188 len
= simple_strtoul(argv
[4], NULL
, 16);
189 if (len
< fdt_totalsize(working_fdt
)) {
190 printf ("New length 0x%X < existing length "
192 len
, fdt_totalsize(working_fdt
));
198 * Copy to the new location.
200 err
= fdt_open_into(working_fdt
, newaddr
, len
);
202 printf ("libfdt fdt_open_into(): %s\n",
206 working_fdt
= newaddr
;
211 } else if (strncmp(argv
[1], "mk", 2) == 0) {
212 char *pathp
; /* path */
213 char *nodep
; /* new node to add */
214 int nodeoffset
; /* node offset from libfdt */
218 * Parameters: Node path, new node to be appended to the path.
221 return CMD_RET_USAGE
;
226 nodeoffset
= fdt_path_offset (working_fdt
, pathp
);
227 if (nodeoffset
< 0) {
229 * Not found or something else bad happened.
231 printf ("libfdt fdt_path_offset() returned %s\n",
232 fdt_strerror(nodeoffset
));
235 err
= fdt_add_subnode(working_fdt
, nodeoffset
, nodep
);
237 printf ("libfdt fdt_add_subnode(): %s\n",
243 * Set the value of a property in the working_fdt.
245 } else if (argv
[1][0] == 's') {
246 char *pathp
; /* path */
247 char *prop
; /* property */
248 int nodeoffset
; /* node offset from libfdt */
249 static char data
[SCRATCHPAD
]; /* storage for the property */
250 int len
; /* new length of the property */
251 int ret
; /* return value */
254 * Parameters: Node path, property, optional value.
257 return CMD_RET_USAGE
;
264 ret
= fdt_parse_prop(&argv
[4], argc
- 4, data
, &len
);
269 nodeoffset
= fdt_path_offset (working_fdt
, pathp
);
270 if (nodeoffset
< 0) {
272 * Not found or something else bad happened.
274 printf ("libfdt fdt_path_offset() returned %s\n",
275 fdt_strerror(nodeoffset
));
279 ret
= fdt_setprop(working_fdt
, nodeoffset
, prop
, data
, len
);
281 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret
));
285 /********************************************************************
286 * Get the value of a property in the working_fdt.
287 ********************************************************************/
288 } else if (argv
[1][0] == 'g') {
289 char *subcmd
; /* sub-command */
290 char *pathp
; /* path */
291 char *prop
; /* property */
292 char *var
; /* variable to store result */
293 int nodeoffset
; /* node offset from libfdt */
294 const void *nodep
; /* property node pointer */
295 int len
= 0; /* new length of the property */
298 * Parameters: Node path, property, optional value.
301 return CMD_RET_USAGE
;
305 if (argc
< 6 && subcmd
[0] != 's')
306 return CMD_RET_USAGE
;
312 nodeoffset
= fdt_path_offset(working_fdt
, pathp
);
313 if (nodeoffset
< 0) {
315 * Not found or something else bad happened.
317 printf("libfdt fdt_path_offset() returned %s\n",
318 fdt_strerror(nodeoffset
));
322 if (subcmd
[0] == 'n' || (subcmd
[0] == 's' && argc
== 5)) {
324 int startDepth
= fdt_node_depth(
325 working_fdt
, nodeoffset
);
326 int curDepth
= startDepth
;
328 int nextNodeOffset
= fdt_next_node(
329 working_fdt
, nodeoffset
, &curDepth
);
331 if (subcmd
[0] == 'n')
332 reqIndex
= simple_strtoul(argv
[5], NULL
, 16);
334 while (curDepth
> startDepth
) {
335 if (curDepth
== startDepth
+ 1)
337 if (subcmd
[0] == 'n' && curIndex
== reqIndex
) {
338 const char *nodeName
= fdt_get_name(
339 working_fdt
, nextNodeOffset
, NULL
);
341 setenv(var
, (char *)nodeName
);
344 nextNodeOffset
= fdt_next_node(
345 working_fdt
, nextNodeOffset
, &curDepth
);
346 if (nextNodeOffset
< 0)
349 if (subcmd
[0] == 's') {
350 /* get the num nodes at this level */
353 sprintf(buf
, "%d", curIndex
+ 1);
356 /* node index not found */
357 printf("libfdt node not found\n");
362 working_fdt
, nodeoffset
, prop
, &len
);
364 /* no property value */
367 } else if (len
> 0) {
368 if (subcmd
[0] == 'v') {
371 ret
= fdt_value_setenv(nodep
, len
, var
);
374 } else if (subcmd
[0] == 'a') {
378 sprintf(buf
, "0x%08X", (uint32_t)nodep
);
380 } else if (subcmd
[0] == 's') {
384 sprintf(buf
, "0x%08X", len
);
387 return CMD_RET_USAGE
;
390 printf("libfdt fdt_getprop(): %s\n",
397 * Print (recursive) / List (single level)
399 } else if ((argv
[1][0] == 'p') || (argv
[1][0] == 'l')) {
400 int depth
= MAX_LEVEL
; /* how deep to print */
401 char *pathp
; /* path */
402 char *prop
; /* property */
403 int ret
; /* return value */
404 static char root
[2] = "/";
407 * list is an alias for print, but limited to 1 level
409 if (argv
[1][0] == 'l') {
414 * Get the starting path. The root node is an oddball,
415 * the offset is zero and has no name.
426 ret
= fdt_print(pathp
, prop
, depth
);
431 * Remove a property/node
433 } else if (strncmp(argv
[1], "rm", 2) == 0) {
434 int nodeoffset
; /* node offset from libfdt */
438 * Get the path. The root node is an oddball, the offset
439 * is zero and has no name.
441 nodeoffset
= fdt_path_offset (working_fdt
, argv
[2]);
442 if (nodeoffset
< 0) {
444 * Not found or something else bad happened.
446 printf ("libfdt fdt_path_offset() returned %s\n",
447 fdt_strerror(nodeoffset
));
451 * Do the delete. A fourth parameter means delete a property,
452 * otherwise delete the node.
455 err
= fdt_delprop(working_fdt
, nodeoffset
, argv
[3]);
457 printf("libfdt fdt_delprop(): %s\n",
462 err
= fdt_del_node(working_fdt
, nodeoffset
);
464 printf("libfdt fdt_del_node(): %s\n",
471 * Display header info
473 } else if (argv
[1][0] == 'h') {
474 u32 version
= fdt_version(working_fdt
);
475 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt
));
476 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt
),
477 fdt_totalsize(working_fdt
));
478 printf("off_dt_struct:\t\t0x%x\n",
479 fdt_off_dt_struct(working_fdt
));
480 printf("off_dt_strings:\t\t0x%x\n",
481 fdt_off_dt_strings(working_fdt
));
482 printf("off_mem_rsvmap:\t\t0x%x\n",
483 fdt_off_mem_rsvmap(working_fdt
));
484 printf("version:\t\t%d\n", version
);
485 printf("last_comp_version:\t%d\n",
486 fdt_last_comp_version(working_fdt
));
488 printf("boot_cpuid_phys:\t0x%x\n",
489 fdt_boot_cpuid_phys(working_fdt
));
491 printf("size_dt_strings:\t0x%x\n",
492 fdt_size_dt_strings(working_fdt
));
494 printf("size_dt_struct:\t\t0x%x\n",
495 fdt_size_dt_struct(working_fdt
));
496 printf("number mem_rsv:\t\t0x%x\n",
497 fdt_num_mem_rsv(working_fdt
));
503 } else if (strncmp(argv
[1], "boo", 3) == 0) {
504 unsigned long tmp
= simple_strtoul(argv
[2], NULL
, 16);
505 fdt_set_boot_cpuid_phys(working_fdt
, tmp
);
510 } else if (strncmp(argv
[1], "me", 2) == 0) {
513 addr
= simple_strtoull(argv
[2], NULL
, 16);
514 size
= simple_strtoull(argv
[3], NULL
, 16);
515 err
= fdt_fixup_memory(working_fdt
, addr
, size
);
520 * mem reserve commands
522 } else if (strncmp(argv
[1], "rs", 2) == 0) {
523 if (argv
[2][0] == 'p') {
525 int total
= fdt_num_mem_rsv(working_fdt
);
527 printf("index\t\t start\t\t size\n");
528 printf("-------------------------------"
529 "-----------------\n");
530 for (j
= 0; j
< total
; j
++) {
531 err
= fdt_get_mem_rsv(working_fdt
, j
, &addr
, &size
);
533 printf("libfdt fdt_get_mem_rsv(): %s\n",
537 printf(" %x\t%08x%08x\t%08x%08x\n", j
,
539 (u32
)(addr
& 0xffffffff),
541 (u32
)(size
& 0xffffffff));
543 } else if (argv
[2][0] == 'a') {
546 addr
= simple_strtoull(argv
[3], NULL
, 16);
547 size
= simple_strtoull(argv
[4], NULL
, 16);
548 err
= fdt_add_mem_rsv(working_fdt
, addr
, size
);
551 printf("libfdt fdt_add_mem_rsv(): %s\n",
555 } else if (argv
[2][0] == 'd') {
556 unsigned long idx
= simple_strtoul(argv
[3], NULL
, 16);
557 int err
= fdt_del_mem_rsv(working_fdt
, idx
);
560 printf("libfdt fdt_del_mem_rsv(): %s\n",
565 /* Unrecognized command */
566 return CMD_RET_USAGE
;
569 #ifdef CONFIG_OF_BOARD_SETUP
570 /* Call the board-specific fixup routine */
571 else if (strncmp(argv
[1], "boa", 3) == 0)
572 ft_board_setup(working_fdt
, gd
->bd
);
574 /* Create a chosen node */
575 else if (argv
[1][0] == 'c') {
576 unsigned long initrd_start
= 0, initrd_end
= 0;
578 if ((argc
!= 2) && (argc
!= 4))
579 return CMD_RET_USAGE
;
582 initrd_start
= simple_strtoul(argv
[2], NULL
, 16);
583 initrd_end
= simple_strtoul(argv
[3], NULL
, 16);
586 fdt_chosen(working_fdt
, 1);
587 fdt_initrd(working_fdt
, initrd_start
, initrd_end
, 1);
590 else if (strncmp(argv
[1], "re", 2) == 0) {
591 fdt_resize(working_fdt
);
594 /* Unrecognized command */
595 return CMD_RET_USAGE
;
601 /****************************************************************************/
603 static int fdt_valid(void)
607 if (working_fdt
== NULL
) {
608 printf ("The address of the fdt is invalid (NULL).\n");
612 err
= fdt_check_header(working_fdt
);
614 return 1; /* valid */
617 printf("libfdt fdt_check_header(): %s", fdt_strerror(err
));
619 * Be more informative on bad version.
621 if (err
== -FDT_ERR_BADVERSION
) {
622 if (fdt_version(working_fdt
) <
623 FDT_FIRST_SUPPORTED_VERSION
) {
624 printf (" - too old, fdt %d < %d",
625 fdt_version(working_fdt
),
626 FDT_FIRST_SUPPORTED_VERSION
);
629 if (fdt_last_comp_version(working_fdt
) >
630 FDT_LAST_SUPPORTED_VERSION
) {
631 printf (" - too new, fdt %d > %d",
632 fdt_version(working_fdt
),
633 FDT_LAST_SUPPORTED_VERSION
);
644 /****************************************************************************/
647 * Parse the user's input, partially heuristic. Valid formats:
648 * <0x00112233 4 05> - an array of cells. Numbers follow standard
650 * [00 11 22 .. nn] - byte stream
651 * "string" - If the the value doesn't start with "<" or "[", it is
652 * treated as a string. Note that the quotes are
653 * stripped by the parser before we get the string.
654 * newval: An array of strings containing the new property as specified
655 * on the command line
656 * count: The number of strings in the array
657 * data: A bytestream to be placed in the property
658 * len: The length of the resulting bytestream
660 static int fdt_parse_prop(char * const *newval
, int count
, char *data
, int *len
)
662 char *cp
; /* temporary char pointer */
663 char *newp
; /* temporary newval char pointer */
664 unsigned long tmp
; /* holds converted values */
670 /* An array of cells */
673 while ((*newp
!= '>') && (stridx
< count
)) {
675 * Keep searching until we find that last ">"
676 * That way users don't have to escape the spaces
679 newp
= newval
[++stridx
];
684 tmp
= simple_strtoul(cp
, &newp
, 0);
685 *(uint32_t *)data
= __cpu_to_be32(tmp
);
689 /* If the ptr didn't advance, something went wrong */
690 if ((newp
- cp
) <= 0) {
691 printf("Sorry, I could not convert \"%s\"\n",
701 printf("Unexpected character '%c'\n", *newp
);
704 } else if (*newp
== '[') {
706 * Byte stream. Convert the values.
709 while ((stridx
< count
) && (*newp
!= ']')) {
713 newp
= newval
[++stridx
];
716 if (!isxdigit(*newp
))
718 tmp
= simple_strtoul(newp
, &newp
, 16);
719 *data
++ = tmp
& 0xFF;
723 printf("Unexpected character '%c'\n", *newp
);
728 * Assume it is one or more strings. Copy it into our
729 * data area for convenience (including the
730 * terminating '\0's).
732 while (stridx
< count
) {
733 size_t length
= strlen(newp
) + 1;
737 newp
= newval
[++stridx
];
743 /****************************************************************************/
746 * Heuristic to guess if this is a string or concatenated strings.
749 static int is_printable_string(const void *data
, int len
)
751 const char *s
= data
;
753 /* zero length is not */
757 /* must terminate with zero or '\n' */
758 if (s
[len
- 1] != '\0' && s
[len
- 1] != '\n')
761 /* printable or a null byte (concatenated strings) */
762 while (((*s
== '\0') || isprint(*s
) || isspace(*s
)) && (len
> 0)) {
764 * If we see a null, there are three possibilities:
765 * 1) If len == 1, it is the end of the string, printable
766 * 2) Next character also a null, not printable.
767 * 3) Next character not a null, continue to check.
779 /* Not the null termination, or not done yet: not printable */
780 if (*s
!= '\0' || (len
!= 0))
788 * Print the property in the best format, a heuristic guess. Print as
789 * a string, concatenated strings, a byte, word, double word, or (if all
790 * else fails) it is printed as a stream of bytes.
792 static void print_data(const void *data
, int len
)
796 /* no data, don't print */
801 * It is a string, but it may have multiple strings (embedded '\0's).
803 if (is_printable_string(data
, len
)) {
810 j
+= strlen(data
) + 1;
811 data
+= strlen(data
) + 1;
818 if (len
> CONFIG_CMD_FDT_MAX_DUMP
)
819 printf("* 0x%08x [0x%08x]", (unsigned int)data
, len
);
824 for (j
= 0, p
= data
; j
< len
/4; j
++)
825 printf("0x%08x%s", fdt32_to_cpu(p
[j
]),
826 j
< (len
/4 - 1) ? " " : "");
829 } else { /* anything else... hexdump */
830 if (len
> CONFIG_CMD_FDT_MAX_DUMP
)
831 printf("* 0x%08x [0x%08x]", (unsigned int)data
, len
);
836 for (j
= 0, s
= data
; j
< len
; j
++)
837 printf("%02x%s", s
[j
], j
< len
- 1 ? " " : "");
843 /****************************************************************************/
846 * Recursively print (a portion of) the working_fdt. The depth parameter
847 * determines how deeply nested the fdt is printed.
849 static int fdt_print(const char *pathp
, char *prop
, int depth
)
851 static char tabs
[MAX_LEVEL
+1] =
852 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
853 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
854 const void *nodep
; /* property node pointer */
855 int nodeoffset
; /* node offset from libfdt */
856 int nextoffset
; /* next node offset from libfdt */
857 uint32_t tag
; /* tag */
858 int len
; /* length of the property */
859 int level
= 0; /* keep track of nesting level */
860 const struct fdt_property
*fdt_prop
;
862 nodeoffset
= fdt_path_offset (working_fdt
, pathp
);
863 if (nodeoffset
< 0) {
865 * Not found or something else bad happened.
867 printf ("libfdt fdt_path_offset() returned %s\n",
868 fdt_strerror(nodeoffset
));
872 * The user passed in a property as well as node path.
873 * Print only the given property and then return.
876 nodep
= fdt_getprop (working_fdt
, nodeoffset
, prop
, &len
);
878 /* no property value */
879 printf("%s %s\n", pathp
, prop
);
881 } else if (len
> 0) {
882 printf("%s = ", prop
);
883 print_data (nodep
, len
);
887 printf ("libfdt fdt_getprop(): %s\n",
894 * The user passed in a node path and no property,
895 * print the node and all subnodes.
898 tag
= fdt_next_tag(working_fdt
, nodeoffset
, &nextoffset
);
901 pathp
= fdt_get_name(working_fdt
, nodeoffset
, NULL
);
902 if (level
<= depth
) {
904 pathp
= "/* NULL pointer error */";
906 pathp
= "/"; /* root is nameless */
908 &tabs
[MAX_LEVEL
- level
], pathp
);
911 if (level
>= MAX_LEVEL
) {
912 printf("Nested too deep, aborting.\n");
919 printf("%s};\n", &tabs
[MAX_LEVEL
- level
]);
921 level
= -1; /* exit the loop */
925 fdt_prop
= fdt_offset_ptr(working_fdt
, nodeoffset
,
927 pathp
= fdt_string(working_fdt
,
928 fdt32_to_cpu(fdt_prop
->nameoff
));
929 len
= fdt32_to_cpu(fdt_prop
->len
);
930 nodep
= fdt_prop
->data
;
932 printf ("libfdt fdt_getprop(): %s\n",
935 } else if (len
== 0) {
936 /* the property has no value */
939 &tabs
[MAX_LEVEL
- level
],
942 if (level
<= depth
) {
944 &tabs
[MAX_LEVEL
- level
],
946 print_data (nodep
, len
);
952 printf("%s/* NOP */\n", &tabs
[MAX_LEVEL
- level
]);
958 printf("Unknown tag 0x%08X\n", tag
);
961 nodeoffset
= nextoffset
;
966 /********************************************************************/
970 "flattened device tree utility commands",
971 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
972 #ifdef CONFIG_OF_BOARD_SETUP
973 "fdt boardsetup - Do board-specific set up\n"
975 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
976 "fdt resize - Resize fdt to size + padding to 4k addr\n"
977 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
978 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
979 "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
980 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
981 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
982 "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
983 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
984 "fdt mknode <path> <node> - Create a new node after <path>\n"
985 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
986 "fdt header - Display header info\n"
987 "fdt bootcpu <id> - Set boot cpuid\n"
988 "fdt memory <addr> <size> - Add/Update memory node\n"
989 "fdt rsvmem print - Show current mem reserves\n"
990 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
991 "fdt rsvmem delete <index> - Delete a mem reserves\n"
992 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
993 " <start>/<end> - initrd start/end addr\n"
994 "NOTE: Dereference aliases by omiting the leading '/', "
995 "e.g. fdt print ethernet0."