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 *pathp, char *prop, char *newval,
46 char *data, int *len);
47 static int fdt_print(const char *pathp, char *prop, int depth);
50 * Flattened Device Tree command, see the help for parameter definitions.
52 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
55 printf ("Usage:\n%s\n", cmdtp->usage);
59 /********************************************************************
60 * Set the address of the fdt
61 ********************************************************************/
62 if (argv[1][0] == 'a') {
64 * Set the address [and length] of the fdt.
66 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
78 len = simple_strtoul(argv[3], NULL, 16);
79 if (len < fdt_totalsize(fdt)) {
80 printf ("New length %d < existing length %d, "
82 len, fdt_totalsize(fdt));
85 * Open in place with a new length.
87 err = fdt_open_into(fdt, fdt, len);
89 printf ("libfdt fdt_open_into(): %s\n",
95 /********************************************************************
97 ********************************************************************/
98 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
99 struct fdt_header *newaddr;
104 printf ("Usage:\n%s\n", cmdtp->usage);
109 * Set the address and length of the fdt.
111 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
116 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
119 * If the user specifies a length, use that. Otherwise use the
123 len = fdt_totalsize(fdt);
125 len = simple_strtoul(argv[4], NULL, 16);
126 if (len < fdt_totalsize(fdt)) {
127 printf ("New length 0x%X < existing length "
129 len, fdt_totalsize(fdt));
135 * Copy to the new location.
137 err = fdt_open_into(fdt, newaddr, len);
139 printf ("libfdt fdt_open_into(): %s\n",
145 /********************************************************************
147 ********************************************************************/
148 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
149 char *pathp; /* path */
150 char *nodep; /* new node to add */
151 int nodeoffset; /* node offset from libfdt */
155 * Parameters: Node path, new node to be appended to the path.
158 printf ("Usage:\n%s\n", cmdtp->usage);
165 nodeoffset = fdt_path_offset (fdt, pathp);
166 if (nodeoffset < 0) {
168 * Not found or something else bad happened.
170 printf ("libfdt fdt_path_offset() returned %s\n",
171 fdt_strerror(nodeoffset));
174 err = fdt_add_subnode(fdt, nodeoffset, nodep);
176 printf ("libfdt fdt_add_subnode(): %s\n",
181 /********************************************************************
182 * Set the value of a property in the fdt.
183 ********************************************************************/
184 } else if (argv[1][0] == 's') {
185 char *pathp; /* path */
186 char *prop; /* property */
187 int nodeoffset; /* node offset from libfdt */
188 static char data[SCRATCHPAD]; /* storage for the property */
189 int len; /* new length of the property */
190 int ret; /* return value */
193 * Parameters: Node path, property, optional value.
196 printf ("Usage:\n%s\n", cmdtp->usage);
205 ret = fdt_parse_prop(pathp, prop, argv[4], data, &len);
210 nodeoffset = fdt_path_offset (fdt, pathp);
211 if (nodeoffset < 0) {
213 * Not found or something else bad happened.
215 printf ("libfdt fdt_path_offset() returned %s\n",
216 fdt_strerror(nodeoffset));
220 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
222 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
226 /********************************************************************
227 * Print (recursive) / List (single level)
228 ********************************************************************/
229 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
230 int depth = MAX_LEVEL; /* how deep to print */
231 char *pathp; /* path */
232 char *prop; /* property */
233 int ret; /* return value */
234 static char root[2] = "/";
237 * list is an alias for print, but limited to 1 level
239 if (argv[1][0] == 'l') {
244 * Get the starting path. The root node is an oddball,
245 * the offset is zero and has no name.
256 ret = fdt_print(pathp, prop, depth);
260 /********************************************************************
261 * Remove a property/node
262 ********************************************************************/
263 } else if ((argv[1][0] == 'r') && (argv[1][1] == 'm')) {
264 int nodeoffset; /* node offset from libfdt */
268 * Get the path. The root node is an oddball, the offset
269 * is zero and has no name.
271 nodeoffset = fdt_path_offset (fdt, argv[2]);
272 if (nodeoffset < 0) {
274 * Not found or something else bad happened.
276 printf ("libfdt fdt_path_offset() returned %s\n",
277 fdt_strerror(nodeoffset));
281 * Do the delete. A fourth parameter means delete a property,
282 * otherwise delete the node.
285 err = fdt_delprop(fdt, nodeoffset, argv[3]);
287 printf("libfdt fdt_delprop(): %s\n",
292 err = fdt_del_node(fdt, nodeoffset);
294 printf("libfdt fdt_del_node(): %s\n",
300 /********************************************************************
301 * Display header info
302 ********************************************************************/
303 } else if (argv[1][0] == 'h') {
304 u32 version = fdt_version(fdt);
305 printf("magic:\t\t\t0x%x\n", fdt_magic(fdt));
306 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(fdt), fdt_totalsize(fdt));
307 printf("off_dt_struct:\t\t0x%x\n", fdt_off_dt_struct(fdt));
308 printf("off_dt_strings:\t\t0x%x\n", fdt_off_dt_strings(fdt));
309 printf("off_mem_rsvmap:\t\t0x%x\n", fdt_off_mem_rsvmap(fdt));
310 printf("version:\t\t%d\n", version);
311 printf("last_comp_version:\t%d\n", fdt_last_comp_version(fdt));
313 printf("boot_cpuid_phys:\t0x%x\n",
314 fdt_boot_cpuid_phys(fdt));
316 printf("size_dt_strings:\t0x%x\n",
317 fdt_size_dt_strings(fdt));
319 printf("size_dt_struct:\t\t0x%x\n",
320 fdt_size_dt_struct(fdt));
321 printf("number mem_rsv:\t\t0x%x\n", fdt_num_mem_rsv(fdt));
324 /********************************************************************
326 ********************************************************************/
327 } else if ((argv[1][0] == 'b') && (argv[1][1] == 'o') &&
328 (argv[1][2] == 'o')) {
329 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
330 fdt_set_boot_cpuid_phys(fdt, tmp);
332 /********************************************************************
334 ********************************************************************/
335 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'e')) {
338 #ifdef CFG_64BIT_STRTOUL
339 addr = simple_strtoull(argv[2], NULL, 16);
340 size = simple_strtoull(argv[3], NULL, 16);
342 addr = simple_strtoul(argv[2], NULL, 16);
343 size = simple_strtoul(argv[3], NULL, 16);
345 err = fdt_fixup_memory(fdt, addr, size);
349 /********************************************************************
350 * mem reserve commands
351 ********************************************************************/
352 } else if ((argv[1][0] == 'r') && (argv[1][1] == 's')) {
353 if (argv[2][0] == 'p') {
355 int total = fdt_num_mem_rsv(fdt);
357 printf("index\t\t start\t\t size\n");
358 printf("-------------------------------"
359 "-----------------\n");
360 for (j = 0; j < total; j++) {
361 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
363 printf("libfdt fdt_get_mem_rsv(): %s\n",
367 printf(" %x\t%08x%08x\t%08x%08x\n", j,
369 (u32)(addr & 0xffffffff),
371 (u32)(size & 0xffffffff));
373 } else if (argv[2][0] == 'a') {
376 #ifdef CFG_64BIT_STRTOUL
377 addr = simple_strtoull(argv[3], NULL, 16);
378 size = simple_strtoull(argv[4], NULL, 16);
380 addr = simple_strtoul(argv[3], NULL, 16);
381 size = simple_strtoul(argv[4], NULL, 16);
383 err = fdt_add_mem_rsv(fdt, addr, size);
386 printf("libfdt fdt_add_mem_rsv(): %s\n",
390 } else if (argv[2][0] == 'd') {
391 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
392 int err = fdt_del_mem_rsv(fdt, idx);
395 printf("libfdt fdt_del_mem_rsv(): %s\n",
400 /* Unrecognized command */
401 printf ("Usage:\n%s\n", cmdtp->usage);
405 #ifdef CONFIG_OF_BOARD_SETUP
406 /* Call the board-specific fixup routine */
407 else if (argv[1][0] == 'b')
408 ft_board_setup(fdt, gd->bd);
410 /* Create a chosen node */
411 else if (argv[1][0] == 'c')
412 fdt_chosen(fdt, 0, 0, 1);
414 #ifdef CONFIG_OF_HAS_UBOOT_ENV
415 /* Create a u-boot-env node */
416 else if (argv[1][0] == 'e')
419 #ifdef CONFIG_OF_HAS_BD_T
420 /* Create a bd_t node */
421 else if (argv[1][0] == 'b')
425 /* Unrecognized command */
426 printf ("Usage:\n%s\n", cmdtp->usage);
433 /****************************************************************************/
435 static int fdt_valid(void)
440 printf ("The address of the fdt is invalid (NULL).\n");
444 err = fdt_check_header(fdt);
446 return 1; /* valid */
449 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
451 * Be more informative on bad version.
453 if (err == -FDT_ERR_BADVERSION) {
454 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
455 printf (" - too old, fdt $d < %d",
457 FDT_FIRST_SUPPORTED_VERSION);
460 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
461 printf (" - too new, fdt $d > %d",
463 FDT_LAST_SUPPORTED_VERSION);
474 /****************************************************************************/
477 * Parse the user's input, partially heuristic. Valid formats:
479 * <0011> - hex half word (16 bits)
480 * <00112233> - hex word (32 bits)
481 * - hex double words (64 bits) are not supported, must use
482 * a byte stream instead.
483 * [00 11 22 .. nn] - byte stream
484 * "string" - If the the value doesn't start with "<" or "[", it is
485 * treated as a string. Note that the quotes are
486 * stripped by the parser before we get the string.
488 static int fdt_parse_prop(char *pathp, char *prop, char *newval,
489 char *data, int *len)
491 char *cp; /* temporary char pointer */
492 unsigned long tmp; /* holds converted values */
494 if (*newval == '<') {
496 * Bigger values than bytes.
500 while ((*newval != '>') && (*newval != '\0')) {
502 tmp = simple_strtoul(cp, &newval, 16);
503 if ((newval - cp) <= 2) {
507 } else if ((newval - cp) <= 4) {
508 *(uint16_t *)data = __cpu_to_be16(tmp);
511 } else if ((newval - cp) <= 8) {
512 *(uint32_t *)data = __cpu_to_be32(tmp);
516 printf("Sorry, I could not convert \"%s\"\n",
520 while (*newval == ' ')
523 if (*newval != '>') {
524 printf("Unexpected character '%c'\n", *newval);
527 } else if (*newval == '[') {
529 * Byte stream. Convert the values.
533 while ((*newval != ']') && (*newval != '\0')) {
534 tmp = simple_strtoul(newval, &newval, 16);
535 *data++ = tmp & 0xFF;
537 while (*newval == ' ')
540 if (*newval != ']') {
541 printf("Unexpected character '%c'\n", *newval);
546 * Assume it is a string. Copy it into our data area for
547 * convenience (including the terminating '\0').
549 *len = strlen(newval) + 1;
550 strcpy(data, newval);
555 /****************************************************************************/
558 * Heuristic to guess if this is a string or concatenated strings.
561 static int is_printable_string(const void *data, int len)
563 const char *s = data;
565 /* zero length is not */
569 /* must terminate with zero */
570 if (s[len - 1] != '\0')
573 /* printable or a null byte (concatenated strings) */
574 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
576 * If we see a null, there are three possibilities:
577 * 1) If len == 1, it is the end of the string, printable
578 * 2) Next character also a null, not printable.
579 * 3) Next character not a null, continue to check.
591 /* Not the null termination, or not done yet: not printable */
592 if (*s != '\0' || (len != 0))
600 * Print the property in the best format, a heuristic guess. Print as
601 * a string, concatenated strings, a byte, word, double word, or (if all
602 * else fails) it is printed as a stream of bytes.
604 static void print_data(const void *data, int len)
609 /* no data, don't print */
614 * It is a string, but it may have multiple strings (embedded '\0's).
616 if (is_printable_string(data, len)) {
623 j += strlen(data) + 1;
624 data += strlen(data) + 1;
632 printf("<0x%02x>", (*(u8 *) data) & 0xff);
634 case 2: /* half-word */
635 printf("<0x%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
638 printf("<0x%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
640 case 8: /* double-word */
642 printf("<0x%016llx>", be64_to_cpu(*(uint64_t *) data));
644 printf("<0x%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
646 printf("0x%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
649 default: /* anything else... hexdump */
651 for (j = 0, s = data; j < len; j++)
652 printf("%02x%s", s[j], j < len - 1 ? " " : "");
659 /****************************************************************************/
662 * Recursively print (a portion of) the fdt. The depth parameter
663 * determines how deeply nested the fdt is printed.
665 static int fdt_print(const char *pathp, char *prop, int depth)
667 static char tabs[MAX_LEVEL+1] =
668 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
669 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
670 const void *nodep; /* property node pointer */
671 int nodeoffset; /* node offset from libfdt */
672 int nextoffset; /* next node offset from libfdt */
673 uint32_t tag; /* tag */
674 int len; /* length of the property */
675 int level = 0; /* keep track of nesting level */
676 const struct fdt_property *fdt_prop;
678 nodeoffset = fdt_path_offset (fdt, pathp);
679 if (nodeoffset < 0) {
681 * Not found or something else bad happened.
683 printf ("libfdt fdt_path_offset() returned %s\n",
684 fdt_strerror(nodeoffset));
688 * The user passed in a property as well as node path.
689 * Print only the given property and then return.
692 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
694 /* no property value */
695 printf("%s %s\n", pathp, prop);
697 } else if (len > 0) {
698 printf("%s = ", prop);
699 print_data (nodep, len);
703 printf ("libfdt fdt_getprop(): %s\n",
710 * The user passed in a node path and no property,
711 * print the node and all subnodes.
714 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
717 pathp = fdt_get_name(fdt, nodeoffset, NULL);
718 if (level <= depth) {
720 pathp = "/* NULL pointer error */";
722 pathp = "/"; /* root is nameless */
724 &tabs[MAX_LEVEL - level], pathp);
727 if (level >= MAX_LEVEL) {
728 printf("Nested too deep, aborting.\n");
735 printf("%s};\n", &tabs[MAX_LEVEL - level]);
737 level = -1; /* exit the loop */
741 fdt_prop = fdt_offset_ptr(fdt, nodeoffset,
743 pathp = fdt_string(fdt,
744 fdt32_to_cpu(fdt_prop->nameoff));
745 len = fdt32_to_cpu(fdt_prop->len);
746 nodep = fdt_prop->data;
748 printf ("libfdt fdt_getprop(): %s\n",
751 } else if (len == 0) {
752 /* the property has no value */
755 &tabs[MAX_LEVEL - level],
758 if (level <= depth) {
760 &tabs[MAX_LEVEL - level],
762 print_data (nodep, len);
768 printf("/* NOP */\n", &tabs[MAX_LEVEL - level]);
774 printf("Unknown tag 0x%08X\n", tag);
777 nodeoffset = nextoffset;
782 /********************************************************************/
786 "fdt - flattened device tree utility commands\n",
787 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
788 #ifdef CONFIG_OF_BOARD_SETUP
789 "fdt boardsetup - Do board-specific set up\n"
791 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
792 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
793 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
794 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
795 "fdt mknode <path> <node> - Create a new node after <path>\n"
796 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
797 "fdt header - Display header info\n"
798 "fdt bootcpu <id> - Set boot cpuid\n"
799 "fdt memory <addr> <size> - Add/Update memory node\n"
800 "fdt rsvmem print - Show current mem reserves\n"
801 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
802 "fdt rsvmem delete <index> - Delete a mem reserves\n"
803 "fdt chosen - Add/update the /chosen branch in the tree\n"
804 #ifdef CONFIG_OF_HAS_UBOOT_ENV
805 "fdt env - Add/replace the /u-boot-env branch in the tree\n"
807 #ifdef CONFIG_OF_HAS_BD_T
808 "fdt bd_t - Add/replace the /bd_t branch in the tree\n"
810 "NOTE: If the path or property you are setting/printing has a '#' character\n"
811 " or spaces, you MUST escape it with a \\ character or quote it with \".\n"