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 char *newval; /* value from the user (as a string) */
188 int nodeoffset; /* node offset from libfdt */
189 static char data[SCRATCHPAD]; /* storage for the property */
190 int len; /* new length of the property */
191 int ret; /* return value */
194 * Parameters: Node path, property, value.
197 printf ("Usage:\n%s\n", cmdtp->usage);
205 nodeoffset = fdt_path_offset (fdt, pathp);
206 if (nodeoffset < 0) {
208 * Not found or something else bad happened.
210 printf ("libfdt fdt_path_offset() returned %s\n",
211 fdt_strerror(nodeoffset));
214 ret = fdt_parse_prop(pathp, prop, newval, data, &len);
218 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
220 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
224 /********************************************************************
225 * Print (recursive) / List (single level)
226 ********************************************************************/
227 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
228 int depth = MAX_LEVEL; /* how deep to print */
229 char *pathp; /* path */
230 char *prop; /* property */
231 int ret; /* return value */
232 static char root[2] = "/";
235 * list is an alias for print, but limited to 1 level
237 if (argv[1][0] == 'l') {
242 * Get the starting path. The root node is an oddball,
243 * the offset is zero and has no name.
254 ret = fdt_print(pathp, prop, depth);
258 /********************************************************************
259 * Remove a property/node
260 ********************************************************************/
261 } else if (argv[1][0] == 'r') {
262 int nodeoffset; /* node offset from libfdt */
266 * Get the path. The root node is an oddball, the offset
267 * is zero and has no name.
269 nodeoffset = fdt_path_offset (fdt, argv[2]);
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 * Do the delete. A fourth parameter means delete a property,
280 * otherwise delete the node.
283 err = fdt_delprop(fdt, nodeoffset, argv[3]);
285 printf("libfdt fdt_delprop(): %s\n",
290 err = fdt_del_node(fdt, nodeoffset);
292 printf("libfdt fdt_del_node(): %s\n",
298 #ifdef CONFIG_OF_BOARD_SETUP
299 /* Call the board-specific fixup routine */
300 else if (argv[1][0] == 'b')
301 ft_board_setup(fdt, gd->bd);
303 /* Create a chosen node */
304 else if (argv[1][0] == 'c')
305 fdt_chosen(fdt, 0, 0, 1);
307 #ifdef CONFIG_OF_HAS_UBOOT_ENV
308 /* Create a u-boot-env node */
309 else if (argv[1][0] == 'e')
312 #ifdef CONFIG_OF_HAS_BD_T
313 /* Create a bd_t node */
314 else if (argv[1][0] == 'b')
318 /* Unrecognized command */
319 printf ("Usage:\n%s\n", cmdtp->usage);
326 /****************************************************************************/
328 static int fdt_valid(void)
333 printf ("The address of the fdt is invalid (NULL).\n");
337 err = fdt_check_header(fdt);
339 return 1; /* valid */
342 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
344 * Be more informative on bad version.
346 if (err == -FDT_ERR_BADVERSION) {
347 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
348 printf (" - too old, fdt $d < %d",
350 FDT_FIRST_SUPPORTED_VERSION);
353 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
354 printf (" - too new, fdt $d > %d",
356 FDT_LAST_SUPPORTED_VERSION);
367 /****************************************************************************/
370 * Parse the user's input, partially heuristic. Valid formats:
372 * <0011> - hex half word (16 bits)
373 * <00112233> - hex word (32 bits)
374 * - hex double words (64 bits) are not supported, must use
375 * a byte stream instead.
376 * [00 11 22 .. nn] - byte stream
377 * "string" - If the the value doesn't start with "<" or "[", it is
378 * treated as a string. Note that the quotes are
379 * stripped by the parser before we get the string.
381 static int fdt_parse_prop(char *pathp, char *prop, char *newval,
382 char *data, int *len)
384 char *cp; /* temporary char pointer */
385 unsigned long tmp; /* holds converted values */
387 if (*newval == '<') {
389 * Bigger values than bytes.
393 while ((*newval != '>') && (*newval != '\0')) {
395 tmp = simple_strtoul(cp, &newval, 16);
396 if ((newval - cp) <= 2) {
400 } else if ((newval - cp) <= 4) {
401 *(uint16_t *)data = __cpu_to_be16(tmp);
404 } else if ((newval - cp) <= 8) {
405 *(uint32_t *)data = __cpu_to_be32(tmp);
409 printf("Sorry, I could not convert \"%s\"\n",
413 while (*newval == ' ')
416 if (*newval != '>') {
417 printf("Unexpected character '%c'\n", *newval);
420 } else if (*newval == '[') {
422 * Byte stream. Convert the values.
426 while ((*newval != ']') && (*newval != '\0')) {
427 tmp = simple_strtoul(newval, &newval, 16);
428 *data++ = tmp & 0xFF;
430 while (*newval == ' ')
433 if (*newval != ']') {
434 printf("Unexpected character '%c'\n", *newval);
439 * Assume it is a string. Copy it into our data area for
440 * convenience (including the terminating '\0').
442 *len = strlen(newval) + 1;
443 strcpy(data, newval);
448 /****************************************************************************/
451 * Heuristic to guess if this is a string or concatenated strings.
454 static int is_printable_string(const void *data, int len)
456 const char *s = data;
458 /* zero length is not */
462 /* must terminate with zero */
463 if (s[len - 1] != '\0')
466 /* printable or a null byte (concatenated strings) */
467 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
469 * If we see a null, there are three possibilities:
470 * 1) If len == 1, it is the end of the string, printable
471 * 2) Next character also a null, not printable.
472 * 3) Next character not a null, continue to check.
484 /* Not the null termination, or not done yet: not printable */
485 if (*s != '\0' || (len != 0))
493 * Print the property in the best format, a heuristic guess. Print as
494 * a string, concatenated strings, a byte, word, double word, or (if all
495 * else fails) it is printed as a stream of bytes.
497 static void print_data(const void *data, int len)
502 /* no data, don't print */
507 * It is a string, but it may have multiple strings (embedded '\0's).
509 if (is_printable_string(data, len)) {
516 j += strlen(data) + 1;
517 data += strlen(data) + 1;
525 printf("<%02x>", (*(u8 *) data) & 0xff);
527 case 2: /* half-word */
528 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
531 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
533 case 8: /* double-word */
535 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
537 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
539 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
542 default: /* anything else... hexdump */
544 for (j = 0, s = data; j < len; j++)
545 printf("%02x%s", s[j], j < len - 1 ? " " : "");
552 /****************************************************************************/
555 * Recursively print (a portion of) the fdt. The depth parameter
556 * determines how deeply nested the fdt is printed.
558 static int fdt_print(const char *pathp, char *prop, int depth)
560 static int offstack[MAX_LEVEL];
561 static char tabs[MAX_LEVEL+1] =
562 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
563 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
564 const void *nodep; /* property node pointer */
565 int nodeoffset; /* node offset from libfdt */
566 int nextoffset; /* next node offset from libfdt */
567 uint32_t tag; /* tag */
568 int len; /* length of the property */
569 int level = 0; /* keep track of nesting level */
570 const struct fdt_property *prop1;
572 nodeoffset = fdt_path_offset (fdt, pathp);
573 if (nodeoffset < 0) {
575 * Not found or something else bad happened.
577 printf ("libfdt fdt_path_offset() returned %s\n",
578 fdt_strerror(nodeoffset));
582 * The user passed in a property as well as node path.
583 * Print only the given property and then return.
586 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
588 /* no property value */
589 printf("%s %s\n", pathp, prop);
591 } else if (len > 0) {
593 print_data (nodep, len);
597 printf ("libfdt fdt_getprop(): %s\n",
604 * The user passed in a node path and no property,
605 * print the node and all subnodes.
607 offstack[0] = nodeoffset;
610 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
613 pathp = fdt_offset_ptr(fdt, nodeoffset, 1);
616 &tabs[MAX_LEVEL - level], pathp);
618 offstack[level] = nodeoffset;
619 if (level >= MAX_LEVEL) {
620 printf("Aaaiii <splat> nested too deep. "
628 printf("%s};\n", &tabs[MAX_LEVEL - level]);
630 level = -1; /* exit the loop */
634 prop1 = fdt_offset_ptr(fdt, nodeoffset, sizeof(*prop1));
635 pathp = fdt_string(fdt, fdt32_to_cpu(prop1->nameoff));
636 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
638 printf ("libfdt fdt_getprop(): %s\n",
641 } else if (len == 0) {
642 /* the property has no value */
645 &tabs[MAX_LEVEL - level],
650 &tabs[MAX_LEVEL - level],
652 print_data (nodep, len);
663 printf("Unknown tag 0x%08X\n", tag);
666 nodeoffset = nextoffset;
671 /********************************************************************/
675 "fdt - flattened device tree utility commands\n",
676 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
677 #ifdef CONFIG_OF_BOARD_SETUP
678 "fdt boardsetup - Do board-specific set up\n"
680 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
681 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
682 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
683 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
684 "fdt mknode <path> <node> - Create a new node after <path>\n"
685 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
686 "fdt chosen - Add/update the /chosen branch in the tree\n"
687 #ifdef CONFIG_OF_HAS_UBOOT_ENV
688 "fdt env - Add/replace the /u-boot-env branch in the tree\n"
690 #ifdef CONFIG_OF_HAS_BD_T
691 "fdt bd_t - Add/replace the /bd_t branch in the tree\n"
694 " If the property you are setting/printing has a '#' character or spaces,\n"
695 " you MUST escape it with a \\ character or quote it with \".\n"
696 "Examples: fdt print / # print the whole tree\n"
697 " fdt print /cpus \"#address-cells\"\n"
698 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"