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>
32 #ifdef CONFIG_OF_LIBFDT
34 #include <asm/global_data.h>
37 #include <fdt_support.h>
39 #define MAX_LEVEL 32 /* how deeply nested we will go */
40 #define SCRATCHPAD 1024 /* bytes of scratchpad memory */
43 * Global data (for the gd->bd)
45 DECLARE_GLOBAL_DATA_PTR;
48 * Function prototypes/declarations.
50 static int fdt_valid(void);
51 static int fdt_parse_prop(char *pathp, char *prop, char *newval,
52 char *data, int *len);
53 static int fdt_print(char *pathp, char *prop, int depth);
56 * Flattened Device Tree command, see the help for parameter definitions.
58 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
61 printf ("Usage:\n%s\n", cmdtp->usage);
65 /********************************************************************
66 * Set the address of the fdt
67 ********************************************************************/
68 if (argv[1][0] == 'a') {
70 * Set the address [and length] of the fdt.
72 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
84 len = simple_strtoul(argv[3], NULL, 16);
85 if (len < fdt_totalsize(fdt)) {
86 printf ("New length %d < existing length %d, "
88 len, fdt_totalsize(fdt));
91 * Open in place with a new length.
93 err = fdt_open_into(fdt, fdt, len);
95 printf ("libfdt fdt_open_into(): %s\n",
101 /********************************************************************
103 ********************************************************************/
104 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
105 struct fdt_header *newaddr;
110 printf ("Usage:\n%s\n", cmdtp->usage);
115 * Set the address and length of the fdt.
117 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
122 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
125 * If the user specifies a length, use that. Otherwise use the
129 len = fdt_totalsize(fdt);
131 len = simple_strtoul(argv[4], NULL, 16);
132 if (len < fdt_totalsize(fdt)) {
133 printf ("New length 0x%X < existing length "
135 len, fdt_totalsize(fdt));
141 * Copy to the new location.
143 err = fdt_open_into(fdt, newaddr, len);
145 printf ("libfdt fdt_open_into(): %s\n",
151 /********************************************************************
153 ********************************************************************/
154 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
155 char *pathp; /* path */
156 char *nodep; /* new node to add */
157 int nodeoffset; /* node offset from libfdt */
161 * Parameters: Node path, new node to be appended to the path.
164 printf ("Usage:\n%s\n", cmdtp->usage);
171 nodeoffset = fdt_find_node_by_path (fdt, pathp);
172 if (nodeoffset < 0) {
174 * Not found or something else bad happened.
176 printf ("libfdt fdt_find_node_by_path() returned %s\n",
177 fdt_strerror(nodeoffset));
180 err = fdt_add_subnode(fdt, nodeoffset, nodep);
182 printf ("libfdt fdt_add_subnode(): %s\n",
187 /********************************************************************
188 * Set the value of a property in the fdt.
189 ********************************************************************/
190 } else if (argv[1][0] == 's') {
191 char *pathp; /* path */
192 char *prop; /* property */
193 char *newval; /* value from the user (as a string) */
194 int nodeoffset; /* node offset from libfdt */
195 static char data[SCRATCHPAD]; /* storage for the property */
196 int len; /* new length of the property */
197 int ret; /* return value */
200 * Parameters: Node path, property, value.
203 printf ("Usage:\n%s\n", cmdtp->usage);
211 nodeoffset = fdt_find_node_by_path (fdt, pathp);
212 if (nodeoffset < 0) {
214 * Not found or something else bad happened.
216 printf ("libfdt fdt_find_node_by_path() returned %s\n",
217 fdt_strerror(nodeoffset));
220 ret = fdt_parse_prop(pathp, prop, newval, data, &len);
224 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
226 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
230 /********************************************************************
231 * Print (recursive) / List (single level)
232 ********************************************************************/
233 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
234 int depth = MAX_LEVEL; /* how deep to print */
235 char *pathp; /* path */
236 char *prop; /* property */
237 int ret; /* return value */
240 * list is an alias for print, but limited to 1 level
242 if (argv[1][0] == 'l') {
247 * Get the starting path. The root node is an oddball,
248 * 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') {
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_find_node_by_path (fdt, argv[2]);
272 if (nodeoffset < 0) {
274 * Not found or something else bad happened.
276 printf ("libfdt fdt_find_node_by_path() 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 * Create a chosen node
302 ********************************************************************/
303 } else if (argv[1][0] == 'c') {
304 fdt_chosen(fdt, 0, 0, 1);
306 /********************************************************************
307 * Create a u-boot-env node
308 ********************************************************************/
309 } else if (argv[1][0] == 'e') {
312 /********************************************************************
314 ********************************************************************/
315 } else if (argv[1][0] == 'b') {
318 /********************************************************************
319 * Unrecognized command
320 ********************************************************************/
322 printf ("Usage:\n%s\n", cmdtp->usage);
329 /****************************************************************************/
331 static int fdt_valid(void)
336 printf ("The address of the fdt is invalid (NULL).\n");
340 err = fdt_check_header(fdt);
342 return 1; /* valid */
345 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
347 * Be more informative on bad version.
349 if (err == -FDT_ERR_BADVERSION) {
350 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
351 printf (" - too old, fdt $d < %d",
353 FDT_FIRST_SUPPORTED_VERSION);
356 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
357 printf (" - too new, fdt $d > %d",
359 FDT_LAST_SUPPORTED_VERSION);
370 /****************************************************************************/
373 * Parse the user's input, partially heuristic. Valid formats:
375 * <0011> - hex half word (16 bits)
376 * <00112233> - hex word (32 bits)
377 * - hex double words (64 bits) are not supported, must use
378 * a byte stream instead.
379 * [00 11 22 .. nn] - byte stream
380 * "string" - If the the value doesn't start with "<" or "[", it is
381 * treated as a string. Note that the quotes are
382 * stripped by the parser before we get the string.
384 static int fdt_parse_prop(char *pathp, char *prop, char *newval,
385 char *data, int *len)
387 char *cp; /* temporary char pointer */
388 unsigned long tmp; /* holds converted values */
390 if (*newval == '<') {
392 * Bigger values than bytes.
396 while ((*newval != '>') && (*newval != '\0')) {
398 tmp = simple_strtoul(cp, &newval, 16);
399 if ((newval - cp) <= 2) {
403 } else if ((newval - cp) <= 4) {
404 *(uint16_t *)data = __cpu_to_be16(tmp);
407 } else if ((newval - cp) <= 8) {
408 *(uint32_t *)data = __cpu_to_be32(tmp);
412 printf("Sorry, I could not convert \"%s\"\n",
416 while (*newval == ' ')
419 if (*newval != '>') {
420 printf("Unexpected character '%c'\n", *newval);
423 } else if (*newval == '[') {
425 * Byte stream. Convert the values.
429 while ((*newval != ']') && (*newval != '\0')) {
430 tmp = simple_strtoul(newval, &newval, 16);
431 *data++ = tmp & 0xFF;
433 while (*newval == ' ')
436 if (*newval != ']') {
437 printf("Unexpected character '%c'\n", *newval);
442 * Assume it is a string. Copy it into our data area for
443 * convenience (including the terminating '\0').
445 *len = strlen(newval) + 1;
446 strcpy(data, newval);
451 /****************************************************************************/
454 * Heuristic to guess if this is a string or concatenated strings.
457 static int is_printable_string(const void *data, int len)
459 const char *s = data;
461 /* zero length is not */
465 /* must terminate with zero */
466 if (s[len - 1] != '\0')
469 /* printable or a null byte (concatenated strings) */
470 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
472 * If we see a null, there are three possibilities:
473 * 1) If len == 1, it is the end of the string, printable
474 * 2) Next character also a null, not printable.
475 * 3) Next character not a null, continue to check.
487 /* Not the null termination, or not done yet: not printable */
488 if (*s != '\0' || (len != 0))
496 * Print the property in the best format, a heuristic guess. Print as
497 * a string, concatenated strings, a byte, word, double word, or (if all
498 * else fails) it is printed as a stream of bytes.
500 static void print_data(const void *data, int len)
505 /* no data, don't print */
510 * It is a string, but it may have multiple strings (embedded '\0's).
512 if (is_printable_string(data, len)) {
519 j += strlen(data) + 1;
520 data += strlen(data) + 1;
528 printf("<%02x>", (*(u8 *) data) & 0xff);
530 case 2: /* half-word */
531 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
534 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
536 case 8: /* double-word */
538 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
540 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
542 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
545 default: /* anything else... hexdump */
547 for (j = 0, s = data; j < len; j++)
548 printf("%02x%s", s[j], j < len - 1 ? " " : "");
555 /****************************************************************************/
558 * Recursively print (a portion of) the fdt. The depth parameter
559 * determines how deeply nested the fdt is printed.
561 static int fdt_print(char *pathp, char *prop, int depth)
563 static int offstack[MAX_LEVEL];
564 static char tabs[MAX_LEVEL+1] =
565 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
566 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
567 void *nodep; /* property node pointer */
568 int nodeoffset; /* node offset from libfdt */
569 int nextoffset; /* next node offset from libfdt */
570 uint32_t tag; /* tag */
571 int len; /* length of the property */
572 int level = 0; /* keep track of nesting level */
574 nodeoffset = fdt_find_node_by_path (fdt, pathp);
575 if (nodeoffset < 0) {
577 * Not found or something else bad happened.
579 printf ("libfdt fdt_find_node_by_path() returned %s\n",
580 fdt_strerror(nodeoffset));
584 * The user passed in a property as well as node path.
585 * Print only the given property and then return.
588 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
590 /* no property value */
591 printf("%s %s\n", pathp, prop);
593 } else if (len > 0) {
595 print_data (nodep, len);
599 printf ("libfdt fdt_getprop(): %s\n",
606 * The user passed in a node path and no property,
607 * print the node and all subnodes.
609 offstack[0] = nodeoffset;
612 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
617 &tabs[MAX_LEVEL - level], pathp);
619 offstack[level] = nodeoffset;
620 if (level >= MAX_LEVEL) {
621 printf("Aaaiii <splat> nested too deep. "
629 printf("%s};\n", &tabs[MAX_LEVEL - level]);
631 level = -1; /* exit the loop */
635 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
637 printf ("libfdt fdt_getprop(): %s\n",
640 } else if (len == 0) {
641 /* the property has no value */
644 &tabs[MAX_LEVEL - level],
649 &tabs[MAX_LEVEL - level],
651 print_data (nodep, len);
662 printf("Unknown tag 0x%08X\n", tag);
665 nodeoffset = nextoffset;
670 /********************************************************************/
674 "fdt - flattened device tree utility commands\n",
675 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
676 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
677 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
678 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
679 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
680 "fdt mknode <path> <node> - Create a new node after <path>\n"
681 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
682 "fdt chosen - Add/update the \"/chosen\" branch in the tree\n"
683 #ifdef CONFIG_OF_HAS_UBOOT_ENV
684 "fdt env - Add/replace the \"/u-boot-env\" branch in the tree\n"
686 #ifdef CONFIG_OF_HAS_BD_T
687 "fdt bd_t - Add/replace the \"/bd_t\" branch in the tree\n"
690 " * If the property you are setting/printing has a '#' character,\n"
691 " you MUST escape it with a \\ character or quote it with \" or\n"
692 " it will be ignored as a comment.\n"
693 " * If the value has spaces in it, you MUST escape the spaces with\n"
694 " \\ characters or quote it with \"\"\n"
695 "Examples: fdt print / # print the whole tree\n"
696 " fdt print /cpus \"#address-cells\"\n"
697 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
700 #endif /* CONFIG_OF_LIBFDT */