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[])
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",
117 return CMD_RET_SUCCESS;
122 "No FDT memory address configured. Please configure\n"
123 "the FDT address via \"fdt addr <address>\" command.\n"
125 return CMD_RET_FAILURE;
129 * Move the working_fdt
131 if (strncmp(argv[1], "mo", 2) == 0) {
132 struct fdt_header *newaddr;
137 return CMD_RET_USAGE;
140 * Set the address and length of the fdt.
142 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
147 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
150 * If the user specifies a length, use that. Otherwise use the
154 len = fdt_totalsize(working_fdt);
156 len = simple_strtoul(argv[4], NULL, 16);
157 if (len < fdt_totalsize(working_fdt)) {
158 printf ("New length 0x%X < existing length "
160 len, fdt_totalsize(working_fdt));
166 * Copy to the new location.
168 err = fdt_open_into(working_fdt, newaddr, len);
170 printf ("libfdt fdt_open_into(): %s\n",
174 working_fdt = newaddr;
179 } else if (strncmp(argv[1], "mk", 2) == 0) {
180 char *pathp; /* path */
181 char *nodep; /* new node to add */
182 int nodeoffset; /* node offset from libfdt */
186 * Parameters: Node path, new node to be appended to the path.
189 return CMD_RET_USAGE;
194 nodeoffset = fdt_path_offset (working_fdt, pathp);
195 if (nodeoffset < 0) {
197 * Not found or something else bad happened.
199 printf ("libfdt fdt_path_offset() returned %s\n",
200 fdt_strerror(nodeoffset));
203 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
205 printf ("libfdt fdt_add_subnode(): %s\n",
211 * Set the value of a property in the working_fdt.
213 } else if (argv[1][0] == 's') {
214 char *pathp; /* path */
215 char *prop; /* property */
216 int nodeoffset; /* node offset from libfdt */
217 static char data[SCRATCHPAD]; /* storage for the property */
218 int len; /* new length of the property */
219 int ret; /* return value */
222 * Parameters: Node path, property, optional value.
225 return CMD_RET_USAGE;
232 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
237 nodeoffset = fdt_path_offset (working_fdt, pathp);
238 if (nodeoffset < 0) {
240 * Not found or something else bad happened.
242 printf ("libfdt fdt_path_offset() returned %s\n",
243 fdt_strerror(nodeoffset));
247 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
249 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
254 * Print (recursive) / List (single level)
256 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
257 int depth = MAX_LEVEL; /* how deep to print */
258 char *pathp; /* path */
259 char *prop; /* property */
260 int ret; /* return value */
261 static char root[2] = "/";
264 * list is an alias for print, but limited to 1 level
266 if (argv[1][0] == 'l') {
271 * Get the starting path. The root node is an oddball,
272 * the offset is zero and has no name.
283 ret = fdt_print(pathp, prop, depth);
288 * Remove a property/node
290 } else if (strncmp(argv[1], "rm", 2) == 0) {
291 int nodeoffset; /* node offset from libfdt */
295 * Get the path. The root node is an oddball, the offset
296 * is zero and has no name.
298 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
299 if (nodeoffset < 0) {
301 * Not found or something else bad happened.
303 printf ("libfdt fdt_path_offset() returned %s\n",
304 fdt_strerror(nodeoffset));
308 * Do the delete. A fourth parameter means delete a property,
309 * otherwise delete the node.
312 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
314 printf("libfdt fdt_delprop(): %s\n",
319 err = fdt_del_node(working_fdt, nodeoffset);
321 printf("libfdt fdt_del_node(): %s\n",
328 * Display header info
330 } else if (argv[1][0] == 'h') {
331 u32 version = fdt_version(working_fdt);
332 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
333 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
334 fdt_totalsize(working_fdt));
335 printf("off_dt_struct:\t\t0x%x\n",
336 fdt_off_dt_struct(working_fdt));
337 printf("off_dt_strings:\t\t0x%x\n",
338 fdt_off_dt_strings(working_fdt));
339 printf("off_mem_rsvmap:\t\t0x%x\n",
340 fdt_off_mem_rsvmap(working_fdt));
341 printf("version:\t\t%d\n", version);
342 printf("last_comp_version:\t%d\n",
343 fdt_last_comp_version(working_fdt));
345 printf("boot_cpuid_phys:\t0x%x\n",
346 fdt_boot_cpuid_phys(working_fdt));
348 printf("size_dt_strings:\t0x%x\n",
349 fdt_size_dt_strings(working_fdt));
351 printf("size_dt_struct:\t\t0x%x\n",
352 fdt_size_dt_struct(working_fdt));
353 printf("number mem_rsv:\t\t0x%x\n",
354 fdt_num_mem_rsv(working_fdt));
360 } else if (strncmp(argv[1], "boo", 3) == 0) {
361 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
362 fdt_set_boot_cpuid_phys(working_fdt, tmp);
367 } else if (strncmp(argv[1], "me", 2) == 0) {
370 addr = simple_strtoull(argv[2], NULL, 16);
371 size = simple_strtoull(argv[3], NULL, 16);
372 err = fdt_fixup_memory(working_fdt, addr, size);
377 * mem reserve commands
379 } else if (strncmp(argv[1], "rs", 2) == 0) {
380 if (argv[2][0] == 'p') {
382 int total = fdt_num_mem_rsv(working_fdt);
384 printf("index\t\t start\t\t size\n");
385 printf("-------------------------------"
386 "-----------------\n");
387 for (j = 0; j < total; j++) {
388 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
390 printf("libfdt fdt_get_mem_rsv(): %s\n",
394 printf(" %x\t%08x%08x\t%08x%08x\n", j,
396 (u32)(addr & 0xffffffff),
398 (u32)(size & 0xffffffff));
400 } else if (argv[2][0] == 'a') {
403 addr = simple_strtoull(argv[3], NULL, 16);
404 size = simple_strtoull(argv[4], NULL, 16);
405 err = fdt_add_mem_rsv(working_fdt, addr, size);
408 printf("libfdt fdt_add_mem_rsv(): %s\n",
412 } else if (argv[2][0] == 'd') {
413 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
414 int err = fdt_del_mem_rsv(working_fdt, idx);
417 printf("libfdt fdt_del_mem_rsv(): %s\n",
422 /* Unrecognized command */
423 return CMD_RET_USAGE;
426 #ifdef CONFIG_OF_BOARD_SETUP
427 /* Call the board-specific fixup routine */
428 else if (strncmp(argv[1], "boa", 3) == 0)
429 ft_board_setup(working_fdt, gd->bd);
431 /* Create a chosen node */
432 else if (argv[1][0] == 'c') {
433 unsigned long initrd_start = 0, initrd_end = 0;
435 if ((argc != 2) && (argc != 4))
436 return CMD_RET_USAGE;
439 initrd_start = simple_strtoul(argv[2], NULL, 16);
440 initrd_end = simple_strtoul(argv[3], NULL, 16);
443 fdt_chosen(working_fdt, 1);
444 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
447 else if (strncmp(argv[1], "re", 2) == 0) {
448 fdt_resize(working_fdt);
451 /* Unrecognized command */
452 return CMD_RET_USAGE;
458 /****************************************************************************/
460 static int fdt_valid(void)
464 if (working_fdt == NULL) {
465 printf ("The address of the fdt is invalid (NULL).\n");
469 err = fdt_check_header(working_fdt);
471 return 1; /* valid */
474 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
476 * Be more informative on bad version.
478 if (err == -FDT_ERR_BADVERSION) {
479 if (fdt_version(working_fdt) <
480 FDT_FIRST_SUPPORTED_VERSION) {
481 printf (" - too old, fdt %d < %d",
482 fdt_version(working_fdt),
483 FDT_FIRST_SUPPORTED_VERSION);
486 if (fdt_last_comp_version(working_fdt) >
487 FDT_LAST_SUPPORTED_VERSION) {
488 printf (" - too new, fdt %d > %d",
489 fdt_version(working_fdt),
490 FDT_LAST_SUPPORTED_VERSION);
501 /****************************************************************************/
504 * Parse the user's input, partially heuristic. Valid formats:
505 * <0x00112233 4 05> - an array of cells. Numbers follow standard
507 * [00 11 22 .. nn] - byte stream
508 * "string" - If the the value doesn't start with "<" or "[", it is
509 * treated as a string. Note that the quotes are
510 * stripped by the parser before we get the string.
511 * newval: An array of strings containing the new property as specified
512 * on the command line
513 * count: The number of strings in the array
514 * data: A bytestream to be placed in the property
515 * len: The length of the resulting bytestream
517 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
519 char *cp; /* temporary char pointer */
520 char *newp; /* temporary newval char pointer */
521 unsigned long tmp; /* holds converted values */
527 /* An array of cells */
530 while ((*newp != '>') && (stridx < count)) {
532 * Keep searching until we find that last ">"
533 * That way users don't have to escape the spaces
536 newp = newval[++stridx];
541 tmp = simple_strtoul(cp, &newp, 0);
542 *(uint32_t *)data = __cpu_to_be32(tmp);
546 /* If the ptr didn't advance, something went wrong */
547 if ((newp - cp) <= 0) {
548 printf("Sorry, I could not convert \"%s\"\n",
558 printf("Unexpected character '%c'\n", *newp);
561 } else if (*newp == '[') {
563 * Byte stream. Convert the values.
566 while ((stridx < count) && (*newp != ']')) {
570 newp = newval[++stridx];
573 if (!isxdigit(*newp))
575 tmp = simple_strtoul(newp, &newp, 16);
576 *data++ = tmp & 0xFF;
580 printf("Unexpected character '%c'\n", *newp);
585 * Assume it is one or more strings. Copy it into our
586 * data area for convenience (including the
587 * terminating '\0's).
589 while (stridx < count) {
590 size_t length = strlen(newp) + 1;
594 newp = newval[++stridx];
600 /****************************************************************************/
603 * Heuristic to guess if this is a string or concatenated strings.
606 static int is_printable_string(const void *data, int len)
608 const char *s = data;
610 /* zero length is not */
614 /* must terminate with zero */
615 if (s[len - 1] != '\0')
618 /* printable or a null byte (concatenated strings) */
619 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
621 * If we see a null, there are three possibilities:
622 * 1) If len == 1, it is the end of the string, printable
623 * 2) Next character also a null, not printable.
624 * 3) Next character not a null, continue to check.
636 /* Not the null termination, or not done yet: not printable */
637 if (*s != '\0' || (len != 0))
645 * Print the property in the best format, a heuristic guess. Print as
646 * a string, concatenated strings, a byte, word, double word, or (if all
647 * else fails) it is printed as a stream of bytes.
649 static void print_data(const void *data, int len)
653 /* no data, don't print */
658 * It is a string, but it may have multiple strings (embedded '\0's).
660 if (is_printable_string(data, len)) {
667 j += strlen(data) + 1;
668 data += strlen(data) + 1;
678 for (j = 0, p = data; j < len/4; j ++)
679 printf("0x%x%s", fdt32_to_cpu(p[j]), j < (len/4 - 1) ? " " : "");
681 } else { /* anything else... hexdump */
685 for (j = 0, s = data; j < len; j++)
686 printf("%02x%s", s[j], j < len - 1 ? " " : "");
691 /****************************************************************************/
694 * Recursively print (a portion of) the working_fdt. The depth parameter
695 * determines how deeply nested the fdt is printed.
697 static int fdt_print(const char *pathp, char *prop, int depth)
699 static char tabs[MAX_LEVEL+1] =
700 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
701 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
702 const void *nodep; /* property node pointer */
703 int nodeoffset; /* node offset from libfdt */
704 int nextoffset; /* next node offset from libfdt */
705 uint32_t tag; /* tag */
706 int len; /* length of the property */
707 int level = 0; /* keep track of nesting level */
708 const struct fdt_property *fdt_prop;
710 nodeoffset = fdt_path_offset (working_fdt, pathp);
711 if (nodeoffset < 0) {
713 * Not found or something else bad happened.
715 printf ("libfdt fdt_path_offset() returned %s\n",
716 fdt_strerror(nodeoffset));
720 * The user passed in a property as well as node path.
721 * Print only the given property and then return.
724 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
726 /* no property value */
727 printf("%s %s\n", pathp, prop);
729 } else if (len > 0) {
730 printf("%s = ", prop);
731 print_data (nodep, len);
735 printf ("libfdt fdt_getprop(): %s\n",
742 * The user passed in a node path and no property,
743 * print the node and all subnodes.
746 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
749 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
750 if (level <= depth) {
752 pathp = "/* NULL pointer error */";
754 pathp = "/"; /* root is nameless */
756 &tabs[MAX_LEVEL - level], pathp);
759 if (level >= MAX_LEVEL) {
760 printf("Nested too deep, aborting.\n");
767 printf("%s};\n", &tabs[MAX_LEVEL - level]);
769 level = -1; /* exit the loop */
773 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
775 pathp = fdt_string(working_fdt,
776 fdt32_to_cpu(fdt_prop->nameoff));
777 len = fdt32_to_cpu(fdt_prop->len);
778 nodep = fdt_prop->data;
780 printf ("libfdt fdt_getprop(): %s\n",
783 } else if (len == 0) {
784 /* the property has no value */
787 &tabs[MAX_LEVEL - level],
790 if (level <= depth) {
792 &tabs[MAX_LEVEL - level],
794 print_data (nodep, len);
800 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
806 printf("Unknown tag 0x%08X\n", tag);
809 nodeoffset = nextoffset;
814 /********************************************************************/
818 "flattened device tree utility commands",
819 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
820 #ifdef CONFIG_OF_BOARD_SETUP
821 "fdt boardsetup - Do board-specific set up\n"
823 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
824 "fdt resize - Resize fdt to size + padding to 4k addr\n"
825 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
826 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
827 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
828 "fdt mknode <path> <node> - Create a new node after <path>\n"
829 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
830 "fdt header - Display header info\n"
831 "fdt bootcpu <id> - Set boot cpuid\n"
832 "fdt memory <addr> <size> - Add/Update memory node\n"
833 "fdt rsvmem print - Show current mem reserves\n"
834 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
835 "fdt rsvmem delete <index> - Delete a mem reserves\n"
836 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
837 " <start>/<end> - initrd start/end addr\n"
838 "NOTE: Dereference aliases by omiting the leading '/', "
839 "e.g. fdt print ethernet0."