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 */
40 * Global data (for the gd->bd)
42 DECLARE_GLOBAL_DATA_PTR
;
44 static int fdt_valid(void);
45 static int fdt_parse_prop(char **newval
, int count
, char *data
, int *len
);
46 static int fdt_print(const char *pathp
, char *prop
, int depth
);
49 * Flattened Device Tree command, see the help for parameter definitions.
51 int do_fdt (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
54 printf ("Usage:\n%s\n", cmdtp
->usage
);
58 /********************************************************************
59 * Set the address of the fdt
60 ********************************************************************/
61 if (argv
[1][0] == 'a') {
63 * Set the address [and length] of the fdt.
65 working_fdt
= (struct fdt_header
*)simple_strtoul(argv
[2], NULL
, 16);
77 len
= simple_strtoul(argv
[3], NULL
, 16);
78 if (len
< fdt_totalsize(working_fdt
)) {
79 printf ("New length %d < existing length %d, "
81 len
, fdt_totalsize(working_fdt
));
84 * Open in place with a new length.
86 err
= fdt_open_into(working_fdt
, working_fdt
, len
);
88 printf ("libfdt fdt_open_into(): %s\n",
94 /********************************************************************
95 * Move the working_fdt
96 ********************************************************************/
97 } else if (strncmp(argv
[1], "mo", 2) == 0) {
98 struct fdt_header
*newaddr
;
103 printf ("Usage:\n%s\n", cmdtp
->usage
);
108 * Set the address and length of the fdt.
110 working_fdt
= (struct fdt_header
*)simple_strtoul(argv
[2], NULL
, 16);
115 newaddr
= (struct fdt_header
*)simple_strtoul(argv
[3],NULL
,16);
118 * If the user specifies a length, use that. Otherwise use the
122 len
= fdt_totalsize(working_fdt
);
124 len
= simple_strtoul(argv
[4], NULL
, 16);
125 if (len
< fdt_totalsize(working_fdt
)) {
126 printf ("New length 0x%X < existing length "
128 len
, fdt_totalsize(working_fdt
));
134 * Copy to the new location.
136 err
= fdt_open_into(working_fdt
, newaddr
, len
);
138 printf ("libfdt fdt_open_into(): %s\n",
142 working_fdt
= newaddr
;
144 /********************************************************************
146 ********************************************************************/
147 } else if (strncmp(argv
[1], "mk", 2) == 0) {
148 char *pathp
; /* path */
149 char *nodep
; /* new node to add */
150 int nodeoffset
; /* node offset from libfdt */
154 * Parameters: Node path, new node to be appended to the path.
157 printf ("Usage:\n%s\n", cmdtp
->usage
);
164 nodeoffset
= fdt_path_offset (working_fdt
, pathp
);
165 if (nodeoffset
< 0) {
167 * Not found or something else bad happened.
169 printf ("libfdt fdt_path_offset() returned %s\n",
170 fdt_strerror(nodeoffset
));
173 err
= fdt_add_subnode(working_fdt
, nodeoffset
, nodep
);
175 printf ("libfdt fdt_add_subnode(): %s\n",
180 /********************************************************************
181 * Set the value of a property in the working_fdt.
182 ********************************************************************/
183 } else if (argv
[1][0] == 's') {
184 char *pathp
; /* path */
185 char *prop
; /* property */
186 int nodeoffset
; /* node offset from libfdt */
187 static char data
[SCRATCHPAD
]; /* storage for the property */
188 int len
; /* new length of the property */
189 int ret
; /* return value */
192 * Parameters: Node path, property, optional value.
195 printf ("Usage:\n%s\n", cmdtp
->usage
);
204 ret
= fdt_parse_prop(&argv
[4], argc
- 4, data
, &len
);
209 nodeoffset
= fdt_path_offset (working_fdt
, pathp
);
210 if (nodeoffset
< 0) {
212 * Not found or something else bad happened.
214 printf ("libfdt fdt_path_offset() returned %s\n",
215 fdt_strerror(nodeoffset
));
219 ret
= fdt_setprop(working_fdt
, nodeoffset
, prop
, data
, len
);
221 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret
));
225 /********************************************************************
226 * Print (recursive) / List (single level)
227 ********************************************************************/
228 } else if ((argv
[1][0] == 'p') || (argv
[1][0] == 'l')) {
229 int depth
= MAX_LEVEL
; /* how deep to print */
230 char *pathp
; /* path */
231 char *prop
; /* property */
232 int ret
; /* return value */
233 static char root
[2] = "/";
236 * list is an alias for print, but limited to 1 level
238 if (argv
[1][0] == 'l') {
243 * Get the starting path. The root node is an oddball,
244 * the offset is zero and has no name.
255 ret
= fdt_print(pathp
, prop
, depth
);
259 /********************************************************************
260 * Remove a property/node
261 ********************************************************************/
262 } else if (strncmp(argv
[1], "rm", 2) == 0) {
263 int nodeoffset
; /* node offset from libfdt */
267 * Get the path. The root node is an oddball, the offset
268 * is zero and has no name.
270 nodeoffset
= fdt_path_offset (working_fdt
, argv
[2]);
271 if (nodeoffset
< 0) {
273 * Not found or something else bad happened.
275 printf ("libfdt fdt_path_offset() returned %s\n",
276 fdt_strerror(nodeoffset
));
280 * Do the delete. A fourth parameter means delete a property,
281 * otherwise delete the node.
284 err
= fdt_delprop(working_fdt
, nodeoffset
, argv
[3]);
286 printf("libfdt fdt_delprop(): %s\n",
291 err
= fdt_del_node(working_fdt
, nodeoffset
);
293 printf("libfdt fdt_del_node(): %s\n",
299 /********************************************************************
300 * Display header info
301 ********************************************************************/
302 } else if (argv
[1][0] == 'h') {
303 u32 version
= fdt_version(working_fdt
);
304 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt
));
305 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt
),
306 fdt_totalsize(working_fdt
));
307 printf("off_dt_struct:\t\t0x%x\n",
308 fdt_off_dt_struct(working_fdt
));
309 printf("off_dt_strings:\t\t0x%x\n",
310 fdt_off_dt_strings(working_fdt
));
311 printf("off_mem_rsvmap:\t\t0x%x\n",
312 fdt_off_mem_rsvmap(working_fdt
));
313 printf("version:\t\t%d\n", version
);
314 printf("last_comp_version:\t%d\n",
315 fdt_last_comp_version(working_fdt
));
317 printf("boot_cpuid_phys:\t0x%x\n",
318 fdt_boot_cpuid_phys(working_fdt
));
320 printf("size_dt_strings:\t0x%x\n",
321 fdt_size_dt_strings(working_fdt
));
323 printf("size_dt_struct:\t\t0x%x\n",
324 fdt_size_dt_struct(working_fdt
));
325 printf("number mem_rsv:\t\t0x%x\n",
326 fdt_num_mem_rsv(working_fdt
));
329 /********************************************************************
331 ********************************************************************/
332 } else if (strncmp(argv
[1], "boo", 3) == 0) {
333 unsigned long tmp
= simple_strtoul(argv
[2], NULL
, 16);
334 fdt_set_boot_cpuid_phys(working_fdt
, tmp
);
336 /********************************************************************
338 ********************************************************************/
339 } else if (strncmp(argv
[1], "me", 2) == 0) {
342 #ifdef CFG_64BIT_STRTOUL
343 addr
= simple_strtoull(argv
[2], NULL
, 16);
344 size
= simple_strtoull(argv
[3], NULL
, 16);
346 addr
= simple_strtoul(argv
[2], NULL
, 16);
347 size
= simple_strtoul(argv
[3], NULL
, 16);
349 err
= fdt_fixup_memory(working_fdt
, addr
, size
);
353 /********************************************************************
354 * mem reserve commands
355 ********************************************************************/
356 } else if (strncmp(argv
[1], "rs", 2) == 0) {
357 if (argv
[2][0] == 'p') {
359 int total
= fdt_num_mem_rsv(working_fdt
);
361 printf("index\t\t start\t\t size\n");
362 printf("-------------------------------"
363 "-----------------\n");
364 for (j
= 0; j
< total
; j
++) {
365 err
= fdt_get_mem_rsv(working_fdt
, j
, &addr
, &size
);
367 printf("libfdt fdt_get_mem_rsv(): %s\n",
371 printf(" %x\t%08x%08x\t%08x%08x\n", j
,
373 (u32
)(addr
& 0xffffffff),
375 (u32
)(size
& 0xffffffff));
377 } else if (argv
[2][0] == 'a') {
380 #ifdef CFG_64BIT_STRTOUL
381 addr
= simple_strtoull(argv
[3], NULL
, 16);
382 size
= simple_strtoull(argv
[4], NULL
, 16);
384 addr
= simple_strtoul(argv
[3], NULL
, 16);
385 size
= simple_strtoul(argv
[4], NULL
, 16);
387 err
= fdt_add_mem_rsv(working_fdt
, addr
, size
);
390 printf("libfdt fdt_add_mem_rsv(): %s\n",
394 } else if (argv
[2][0] == 'd') {
395 unsigned long idx
= simple_strtoul(argv
[3], NULL
, 16);
396 int err
= fdt_del_mem_rsv(working_fdt
, idx
);
399 printf("libfdt fdt_del_mem_rsv(): %s\n",
404 /* Unrecognized command */
405 printf ("Usage:\n%s\n", cmdtp
->usage
);
409 #ifdef CONFIG_OF_BOARD_SETUP
410 /* Call the board-specific fixup routine */
411 else if (strncmp(argv
[1], "boa", 3) == 0)
412 ft_board_setup(working_fdt
, gd
->bd
);
414 /* Create a chosen node */
415 else if (argv
[1][0] == 'c')
416 fdt_chosen(working_fdt
, 0, 0, 1);
418 /* Unrecognized command */
419 printf ("Usage:\n%s\n", cmdtp
->usage
);
426 /****************************************************************************/
428 static int fdt_valid(void)
432 if (working_fdt
== NULL
) {
433 printf ("The address of the fdt is invalid (NULL).\n");
437 err
= fdt_check_header(working_fdt
);
439 return 1; /* valid */
442 printf("libfdt fdt_check_header(): %s", fdt_strerror(err
));
444 * Be more informative on bad version.
446 if (err
== -FDT_ERR_BADVERSION
) {
447 if (fdt_version(working_fdt
) <
448 FDT_FIRST_SUPPORTED_VERSION
) {
449 printf (" - too old, fdt $d < %d",
450 fdt_version(working_fdt
),
451 FDT_FIRST_SUPPORTED_VERSION
);
454 if (fdt_last_comp_version(working_fdt
) >
455 FDT_LAST_SUPPORTED_VERSION
) {
456 printf (" - too new, fdt $d > %d",
457 fdt_version(working_fdt
),
458 FDT_LAST_SUPPORTED_VERSION
);
469 /****************************************************************************/
472 * Parse the user's input, partially heuristic. Valid formats:
473 * <0x00112233 4 05> - an array of cells. Numbers follow standard
475 * [00 11 22 .. nn] - byte stream
476 * "string" - If the the value doesn't start with "<" or "[", it is
477 * treated as a string. Note that the quotes are
478 * stripped by the parser before we get the string.
479 * newval: An array of strings containing the new property as specified
480 * on the command line
481 * count: The number of strings in the array
482 * data: A bytestream to be placed in the property
483 * len: The length of the resulting bytestream
485 static int fdt_parse_prop(char **newval
, int count
, char *data
, int *len
)
487 char *cp
; /* temporary char pointer */
488 char *newp
; /* temporary newval char pointer */
489 unsigned long tmp
; /* holds converted values */
495 /* An array of cells */
498 while ((*newp
!= '>') && (stridx
< count
)) {
500 * Keep searching until we find that last ">"
501 * That way users don't have to escape the spaces
504 newp
= newval
[++stridx
];
509 tmp
= simple_strtoul(cp
, &newp
, 0);
510 *(uint32_t *)data
= __cpu_to_be32(tmp
);
514 /* If the ptr didn't advance, something went wrong */
515 if ((newp
- cp
) <= 0) {
516 printf("Sorry, I could not convert \"%s\"\n",
526 printf("Unexpected character '%c'\n", *newp
);
529 } else if (*newp
== '[') {
531 * Byte stream. Convert the values.
534 while ((*newp
!= ']') && (stridx
< count
)) {
535 tmp
= simple_strtoul(newp
, &newp
, 16);
536 *data
++ = tmp
& 0xFF;
541 newp
= newval
[++stridx
];
544 printf("Unexpected character '%c'\n", *newval
);
549 * Assume it is a string. Copy it into our data area for
550 * convenience (including the terminating '\0').
552 while (stridx
< count
) {
553 *len
= strlen(newp
) + 1;
555 newp
= newval
[++stridx
];
561 /****************************************************************************/
564 * Heuristic to guess if this is a string or concatenated strings.
567 static int is_printable_string(const void *data
, int len
)
569 const char *s
= data
;
571 /* zero length is not */
575 /* must terminate with zero */
576 if (s
[len
- 1] != '\0')
579 /* printable or a null byte (concatenated strings) */
580 while (((*s
== '\0') || isprint(*s
)) && (len
> 0)) {
582 * If we see a null, there are three possibilities:
583 * 1) If len == 1, it is the end of the string, printable
584 * 2) Next character also a null, not printable.
585 * 3) Next character not a null, continue to check.
597 /* Not the null termination, or not done yet: not printable */
598 if (*s
!= '\0' || (len
!= 0))
606 * Print the property in the best format, a heuristic guess. Print as
607 * a string, concatenated strings, a byte, word, double word, or (if all
608 * else fails) it is printed as a stream of bytes.
610 static void print_data(const void *data
, int len
)
614 /* no data, don't print */
619 * It is a string, but it may have multiple strings (embedded '\0's).
621 if (is_printable_string(data
, len
)) {
628 j
+= strlen(data
) + 1;
629 data
+= strlen(data
) + 1;
639 for (j
= 0, p
= data
; j
< len
/4; j
++)
640 printf("0x%x%s", p
[j
], j
< (len
/4 - 1) ? " " : "");
642 } else { /* anything else... hexdump */
646 for (j
= 0, s
= data
; j
< len
; j
++)
647 printf("%02x%s", s
[j
], j
< len
- 1 ? " " : "");
652 /****************************************************************************/
655 * Recursively print (a portion of) the working_fdt. The depth parameter
656 * determines how deeply nested the fdt is printed.
658 static int fdt_print(const char *pathp
, char *prop
, int depth
)
660 static char tabs
[MAX_LEVEL
+1] =
661 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
662 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
663 const void *nodep
; /* property node pointer */
664 int nodeoffset
; /* node offset from libfdt */
665 int nextoffset
; /* next node offset from libfdt */
666 uint32_t tag
; /* tag */
667 int len
; /* length of the property */
668 int level
= 0; /* keep track of nesting level */
669 const struct fdt_property
*fdt_prop
;
671 nodeoffset
= fdt_path_offset (working_fdt
, pathp
);
672 if (nodeoffset
< 0) {
674 * Not found or something else bad happened.
676 printf ("libfdt fdt_path_offset() returned %s\n",
677 fdt_strerror(nodeoffset
));
681 * The user passed in a property as well as node path.
682 * Print only the given property and then return.
685 nodep
= fdt_getprop (working_fdt
, nodeoffset
, prop
, &len
);
687 /* no property value */
688 printf("%s %s\n", pathp
, prop
);
690 } else if (len
> 0) {
691 printf("%s = ", prop
);
692 print_data (nodep
, len
);
696 printf ("libfdt fdt_getprop(): %s\n",
703 * The user passed in a node path and no property,
704 * print the node and all subnodes.
707 tag
= fdt_next_tag(working_fdt
, nodeoffset
, &nextoffset
);
710 pathp
= fdt_get_name(working_fdt
, nodeoffset
, NULL
);
711 if (level
<= depth
) {
713 pathp
= "/* NULL pointer error */";
715 pathp
= "/"; /* root is nameless */
717 &tabs
[MAX_LEVEL
- level
], pathp
);
720 if (level
>= MAX_LEVEL
) {
721 printf("Nested too deep, aborting.\n");
728 printf("%s};\n", &tabs
[MAX_LEVEL
- level
]);
730 level
= -1; /* exit the loop */
734 fdt_prop
= fdt_offset_ptr(working_fdt
, nodeoffset
,
736 pathp
= fdt_string(working_fdt
,
737 fdt32_to_cpu(fdt_prop
->nameoff
));
738 len
= fdt32_to_cpu(fdt_prop
->len
);
739 nodep
= fdt_prop
->data
;
741 printf ("libfdt fdt_getprop(): %s\n",
744 } else if (len
== 0) {
745 /* the property has no value */
748 &tabs
[MAX_LEVEL
- level
],
751 if (level
<= depth
) {
753 &tabs
[MAX_LEVEL
- level
],
755 print_data (nodep
, len
);
761 printf("/* NOP */\n", &tabs
[MAX_LEVEL
- level
]);
767 printf("Unknown tag 0x%08X\n", tag
);
770 nodeoffset
= nextoffset
;
775 /********************************************************************/
779 "fdt - flattened device tree utility commands\n",
780 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
781 #ifdef CONFIG_OF_BOARD_SETUP
782 "fdt boardsetup - Do board-specific set up\n"
784 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
785 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
786 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
787 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
788 "fdt mknode <path> <node> - Create a new node after <path>\n"
789 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
790 "fdt header - Display header info\n"
791 "fdt bootcpu <id> - Set boot cpuid\n"
792 "fdt memory <addr> <size> - Add/Update memory node\n"
793 "fdt rsvmem print - Show current mem reserves\n"
794 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
795 "fdt rsvmem delete <index> - Delete a mem reserves\n"
796 "fdt chosen - Add/update the /chosen branch in the tree\n"
797 "NOTE: If the path or property you are setting/printing has a '#' character\n"
798 " or spaces, you MUST escape it with a \\ character or quote it with \".\n"