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 *const*newval, int count, char *data, int *len);
46 static int fdt_print(const char *pathp, char *prop, int depth);
49 * The working_fdt points to our working flattened device tree.
51 struct fdt_header *working_fdt;
53 void set_working_fdt_addr(void *addr)
59 sprintf(buf, "%lx", (unsigned long)addr);
60 setenv("fdtaddr", buf);
64 * Flattened Device Tree command, see the help for parameter definitions.
66 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
69 return cmd_usage(cmdtp);
72 * Set the address of the fdt
74 if (argv[1][0] == 'a') {
77 * Set the address [and length] of the fdt.
83 printf("The address of the fdt is %p\n", working_fdt);
87 addr = simple_strtoul(argv[2], NULL, 16);
88 set_working_fdt_addr((void *)addr);
100 len = simple_strtoul(argv[3], NULL, 16);
101 if (len < fdt_totalsize(working_fdt)) {
102 printf ("New length %d < existing length %d, "
104 len, fdt_totalsize(working_fdt));
107 * Open in place with a new length.
109 err = fdt_open_into(working_fdt, working_fdt, len);
111 printf ("libfdt fdt_open_into(): %s\n",
118 * Move the working_fdt
120 } else if (strncmp(argv[1], "mo", 2) == 0) {
121 struct fdt_header *newaddr;
126 return cmd_usage(cmdtp);
129 * Set the address and length of the fdt.
131 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
136 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
139 * If the user specifies a length, use that. Otherwise use the
143 len = fdt_totalsize(working_fdt);
145 len = simple_strtoul(argv[4], NULL, 16);
146 if (len < fdt_totalsize(working_fdt)) {
147 printf ("New length 0x%X < existing length "
149 len, fdt_totalsize(working_fdt));
155 * Copy to the new location.
157 err = fdt_open_into(working_fdt, newaddr, len);
159 printf ("libfdt fdt_open_into(): %s\n",
163 working_fdt = newaddr;
168 } else if (strncmp(argv[1], "mk", 2) == 0) {
169 char *pathp; /* path */
170 char *nodep; /* new node to add */
171 int nodeoffset; /* node offset from libfdt */
175 * Parameters: Node path, new node to be appended to the path.
178 return cmd_usage(cmdtp);
183 nodeoffset = fdt_path_offset (working_fdt, pathp);
184 if (nodeoffset < 0) {
186 * Not found or something else bad happened.
188 printf ("libfdt fdt_path_offset() returned %s\n",
189 fdt_strerror(nodeoffset));
192 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
194 printf ("libfdt fdt_add_subnode(): %s\n",
200 * Set the value of a property in the working_fdt.
202 } else if (argv[1][0] == 's') {
203 char *pathp; /* path */
204 char *prop; /* property */
205 int nodeoffset; /* node offset from libfdt */
206 static char data[SCRATCHPAD]; /* storage for the property */
207 int len; /* new length of the property */
208 int ret; /* return value */
211 * Parameters: Node path, property, optional value.
214 return cmd_usage(cmdtp);
221 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
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));
236 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
238 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
243 * Print (recursive) / List (single level)
245 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
246 int depth = MAX_LEVEL; /* how deep to print */
247 char *pathp; /* path */
248 char *prop; /* property */
249 int ret; /* return value */
250 static char root[2] = "/";
253 * list is an alias for print, but limited to 1 level
255 if (argv[1][0] == 'l') {
260 * Get the starting path. The root node is an oddball,
261 * the offset is zero and has no name.
272 ret = fdt_print(pathp, prop, depth);
277 * Remove a property/node
279 } else if (strncmp(argv[1], "rm", 2) == 0) {
280 int nodeoffset; /* node offset from libfdt */
284 * Get the path. The root node is an oddball, the offset
285 * is zero and has no name.
287 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
288 if (nodeoffset < 0) {
290 * Not found or something else bad happened.
292 printf ("libfdt fdt_path_offset() returned %s\n",
293 fdt_strerror(nodeoffset));
297 * Do the delete. A fourth parameter means delete a property,
298 * otherwise delete the node.
301 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
303 printf("libfdt fdt_delprop(): %s\n",
308 err = fdt_del_node(working_fdt, nodeoffset);
310 printf("libfdt fdt_del_node(): %s\n",
317 * Display header info
319 } else if (argv[1][0] == 'h') {
320 u32 version = fdt_version(working_fdt);
321 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
322 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
323 fdt_totalsize(working_fdt));
324 printf("off_dt_struct:\t\t0x%x\n",
325 fdt_off_dt_struct(working_fdt));
326 printf("off_dt_strings:\t\t0x%x\n",
327 fdt_off_dt_strings(working_fdt));
328 printf("off_mem_rsvmap:\t\t0x%x\n",
329 fdt_off_mem_rsvmap(working_fdt));
330 printf("version:\t\t%d\n", version);
331 printf("last_comp_version:\t%d\n",
332 fdt_last_comp_version(working_fdt));
334 printf("boot_cpuid_phys:\t0x%x\n",
335 fdt_boot_cpuid_phys(working_fdt));
337 printf("size_dt_strings:\t0x%x\n",
338 fdt_size_dt_strings(working_fdt));
340 printf("size_dt_struct:\t\t0x%x\n",
341 fdt_size_dt_struct(working_fdt));
342 printf("number mem_rsv:\t\t0x%x\n",
343 fdt_num_mem_rsv(working_fdt));
349 } else if (strncmp(argv[1], "boo", 3) == 0) {
350 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
351 fdt_set_boot_cpuid_phys(working_fdt, tmp);
356 } else if (strncmp(argv[1], "me", 2) == 0) {
359 addr = simple_strtoull(argv[2], NULL, 16);
360 size = simple_strtoull(argv[3], NULL, 16);
361 err = fdt_fixup_memory(working_fdt, addr, size);
366 * mem reserve commands
368 } else if (strncmp(argv[1], "rs", 2) == 0) {
369 if (argv[2][0] == 'p') {
371 int total = fdt_num_mem_rsv(working_fdt);
373 printf("index\t\t start\t\t size\n");
374 printf("-------------------------------"
375 "-----------------\n");
376 for (j = 0; j < total; j++) {
377 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
379 printf("libfdt fdt_get_mem_rsv(): %s\n",
383 printf(" %x\t%08x%08x\t%08x%08x\n", j,
385 (u32)(addr & 0xffffffff),
387 (u32)(size & 0xffffffff));
389 } else if (argv[2][0] == 'a') {
392 addr = simple_strtoull(argv[3], NULL, 16);
393 size = simple_strtoull(argv[4], NULL, 16);
394 err = fdt_add_mem_rsv(working_fdt, addr, size);
397 printf("libfdt fdt_add_mem_rsv(): %s\n",
401 } else if (argv[2][0] == 'd') {
402 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
403 int err = fdt_del_mem_rsv(working_fdt, idx);
406 printf("libfdt fdt_del_mem_rsv(): %s\n",
411 /* Unrecognized command */
412 return cmd_usage(cmdtp);
415 #ifdef CONFIG_OF_BOARD_SETUP
416 /* Call the board-specific fixup routine */
417 else if (strncmp(argv[1], "boa", 3) == 0)
418 ft_board_setup(working_fdt, gd->bd);
420 /* Create a chosen node */
421 else if (argv[1][0] == 'c') {
422 unsigned long initrd_start = 0, initrd_end = 0;
424 if ((argc != 2) && (argc != 4))
425 return cmd_usage(cmdtp);
428 initrd_start = simple_strtoul(argv[2], NULL, 16);
429 initrd_end = simple_strtoul(argv[3], NULL, 16);
432 fdt_chosen(working_fdt, 1);
433 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
436 else if (strncmp(argv[1], "re", 2) == 0) {
437 fdt_resize(working_fdt);
440 /* Unrecognized command */
441 return cmd_usage(cmdtp);
447 /****************************************************************************/
449 static int fdt_valid(void)
453 if (working_fdt == NULL) {
454 printf ("The address of the fdt is invalid (NULL).\n");
458 err = fdt_check_header(working_fdt);
460 return 1; /* valid */
463 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
465 * Be more informative on bad version.
467 if (err == -FDT_ERR_BADVERSION) {
468 if (fdt_version(working_fdt) <
469 FDT_FIRST_SUPPORTED_VERSION) {
470 printf (" - too old, fdt %d < %d",
471 fdt_version(working_fdt),
472 FDT_FIRST_SUPPORTED_VERSION);
475 if (fdt_last_comp_version(working_fdt) >
476 FDT_LAST_SUPPORTED_VERSION) {
477 printf (" - too new, fdt %d > %d",
478 fdt_version(working_fdt),
479 FDT_LAST_SUPPORTED_VERSION);
490 /****************************************************************************/
493 * Parse the user's input, partially heuristic. Valid formats:
494 * <0x00112233 4 05> - an array of cells. Numbers follow standard
496 * [00 11 22 .. nn] - byte stream
497 * "string" - If the the value doesn't start with "<" or "[", it is
498 * treated as a string. Note that the quotes are
499 * stripped by the parser before we get the string.
500 * newval: An array of strings containing the new property as specified
501 * on the command line
502 * count: The number of strings in the array
503 * data: A bytestream to be placed in the property
504 * len: The length of the resulting bytestream
506 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
508 char *cp; /* temporary char pointer */
509 char *newp; /* temporary newval char pointer */
510 unsigned long tmp; /* holds converted values */
516 /* An array of cells */
519 while ((*newp != '>') && (stridx < count)) {
521 * Keep searching until we find that last ">"
522 * That way users don't have to escape the spaces
525 newp = newval[++stridx];
530 tmp = simple_strtoul(cp, &newp, 0);
531 *(uint32_t *)data = __cpu_to_be32(tmp);
535 /* If the ptr didn't advance, something went wrong */
536 if ((newp - cp) <= 0) {
537 printf("Sorry, I could not convert \"%s\"\n",
547 printf("Unexpected character '%c'\n", *newp);
550 } else if (*newp == '[') {
552 * Byte stream. Convert the values.
555 while ((stridx < count) && (*newp != ']')) {
559 newp = newval[++stridx];
562 if (!isxdigit(*newp))
564 tmp = simple_strtoul(newp, &newp, 16);
565 *data++ = tmp & 0xFF;
569 printf("Unexpected character '%c'\n", *newp);
574 * Assume it is one or more strings. Copy it into our
575 * data area for convenience (including the
576 * terminating '\0's).
578 while (stridx < count) {
579 size_t length = strlen(newp) + 1;
583 newp = newval[++stridx];
589 /****************************************************************************/
592 * Heuristic to guess if this is a string or concatenated strings.
595 static int is_printable_string(const void *data, int len)
597 const char *s = data;
599 /* zero length is not */
603 /* must terminate with zero */
604 if (s[len - 1] != '\0')
607 /* printable or a null byte (concatenated strings) */
608 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
610 * If we see a null, there are three possibilities:
611 * 1) If len == 1, it is the end of the string, printable
612 * 2) Next character also a null, not printable.
613 * 3) Next character not a null, continue to check.
625 /* Not the null termination, or not done yet: not printable */
626 if (*s != '\0' || (len != 0))
634 * Print the property in the best format, a heuristic guess. Print as
635 * a string, concatenated strings, a byte, word, double word, or (if all
636 * else fails) it is printed as a stream of bytes.
638 static void print_data(const void *data, int len)
642 /* no data, don't print */
647 * It is a string, but it may have multiple strings (embedded '\0's).
649 if (is_printable_string(data, len)) {
656 j += strlen(data) + 1;
657 data += strlen(data) + 1;
667 for (j = 0, p = data; j < len/4; j ++)
668 printf("0x%x%s", fdt32_to_cpu(p[j]), j < (len/4 - 1) ? " " : "");
670 } else { /* anything else... hexdump */
674 for (j = 0, s = data; j < len; j++)
675 printf("%02x%s", s[j], j < len - 1 ? " " : "");
680 /****************************************************************************/
683 * Recursively print (a portion of) the working_fdt. The depth parameter
684 * determines how deeply nested the fdt is printed.
686 static int fdt_print(const char *pathp, char *prop, int depth)
688 static char tabs[MAX_LEVEL+1] =
689 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
690 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
691 const void *nodep; /* property node pointer */
692 int nodeoffset; /* node offset from libfdt */
693 int nextoffset; /* next node offset from libfdt */
694 uint32_t tag; /* tag */
695 int len; /* length of the property */
696 int level = 0; /* keep track of nesting level */
697 const struct fdt_property *fdt_prop;
699 nodeoffset = fdt_path_offset (working_fdt, pathp);
700 if (nodeoffset < 0) {
702 * Not found or something else bad happened.
704 printf ("libfdt fdt_path_offset() returned %s\n",
705 fdt_strerror(nodeoffset));
709 * The user passed in a property as well as node path.
710 * Print only the given property and then return.
713 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
715 /* no property value */
716 printf("%s %s\n", pathp, prop);
718 } else if (len > 0) {
719 printf("%s = ", prop);
720 print_data (nodep, len);
724 printf ("libfdt fdt_getprop(): %s\n",
731 * The user passed in a node path and no property,
732 * print the node and all subnodes.
735 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
738 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
739 if (level <= depth) {
741 pathp = "/* NULL pointer error */";
743 pathp = "/"; /* root is nameless */
745 &tabs[MAX_LEVEL - level], pathp);
748 if (level >= MAX_LEVEL) {
749 printf("Nested too deep, aborting.\n");
756 printf("%s};\n", &tabs[MAX_LEVEL - level]);
758 level = -1; /* exit the loop */
762 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
764 pathp = fdt_string(working_fdt,
765 fdt32_to_cpu(fdt_prop->nameoff));
766 len = fdt32_to_cpu(fdt_prop->len);
767 nodep = fdt_prop->data;
769 printf ("libfdt fdt_getprop(): %s\n",
772 } else if (len == 0) {
773 /* the property has no value */
776 &tabs[MAX_LEVEL - level],
779 if (level <= depth) {
781 &tabs[MAX_LEVEL - level],
783 print_data (nodep, len);
789 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
795 printf("Unknown tag 0x%08X\n", tag);
798 nodeoffset = nextoffset;
803 /********************************************************************/
807 "flattened device tree utility commands",
808 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
809 #ifdef CONFIG_OF_BOARD_SETUP
810 "fdt boardsetup - Do board-specific set up\n"
812 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
813 "fdt resize - Resize fdt to size + padding to 4k addr\n"
814 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
815 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
816 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
817 "fdt mknode <path> <node> - Create a new node after <path>\n"
818 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
819 "fdt header - Display header info\n"
820 "fdt bootcpu <id> - Set boot cpuid\n"
821 "fdt memory <addr> <size> - Add/Update memory node\n"
822 "fdt rsvmem print - Show current mem reserves\n"
823 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
824 "fdt rsvmem delete <index> - Delete a mem reserves\n"
825 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
826 " <start>/<end> - initrd start/end addr\n"
827 "NOTE: Dereference aliases by omiting the leading '/', "
828 "e.g. fdt print ethernet0."