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);
55 static int findnodeoffset(const char *pathp)
59 if (strcmp(pathp, "/") == 0) {
62 nodeoffset = fdt_path_offset (fdt, pathp);
65 * Not found or something else bad happened.
67 printf ("findnodeoffset() libfdt: %s\n",
68 fdt_strerror(nodeoffset));
75 * Flattened Device Tree command, see the help for parameter definitions.
77 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
80 printf ("Usage:\n%s\n", cmdtp->usage);
84 /********************************************************************
85 * Set the address of the fdt
86 ********************************************************************/
87 if (argv[1][0] == 'a') {
89 * Set the address [and length] of the fdt.
91 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
101 * Optional new length
103 len = simple_strtoul(argv[3], NULL, 16);
104 if (len < fdt_totalsize(fdt)) {
105 printf ("New length %d < existing length %d, "
107 len, fdt_totalsize(fdt));
110 * Open in place with a new length.
112 err = fdt_open_into(fdt, fdt, len);
114 printf ("libfdt fdt_open_into(): %s\n",
120 /********************************************************************
122 ********************************************************************/
123 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
124 struct fdt_header *newaddr;
129 printf ("Usage:\n%s\n", cmdtp->usage);
134 * Set the address and length of the fdt.
136 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
141 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
144 * If the user specifies a length, use that. Otherwise use the
148 len = fdt_totalsize(fdt);
150 len = simple_strtoul(argv[4], NULL, 16);
151 if (len < fdt_totalsize(fdt)) {
152 printf ("New length 0x%X < existing length "
154 len, fdt_totalsize(fdt));
160 * Copy to the new location.
162 err = fdt_open_into(fdt, newaddr, len);
164 printf ("libfdt fdt_open_into(): %s\n",
170 /********************************************************************
172 ********************************************************************/
173 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
174 char *pathp; /* path */
175 char *nodep; /* new node to add */
176 int nodeoffset; /* node offset from libfdt */
180 * Parameters: Node path, new node to be appended to the path.
183 printf ("Usage:\n%s\n", cmdtp->usage);
190 nodeoffset = findnodeoffset(pathp);
191 if (nodeoffset < 0) {
193 * Not found or something else bad happened.
197 err = fdt_add_subnode(fdt, nodeoffset, nodep);
199 printf ("libfdt fdt_add_subnode(): %s\n",
204 /********************************************************************
205 * Set the value of a property in the fdt.
206 ********************************************************************/
207 } else if (argv[1][0] == 's') {
208 char *pathp; /* path */
209 char *prop; /* property */
210 char *newval; /* value from the user (as a string) */
211 int nodeoffset; /* node offset from libfdt */
212 static char data[SCRATCHPAD]; /* storage for the property */
213 int len; /* new length of the property */
214 int ret; /* return value */
217 * Parameters: Node path, property, value.
220 printf ("Usage:\n%s\n", cmdtp->usage);
228 nodeoffset = findnodeoffset(pathp);
229 if (nodeoffset < 0) {
231 * Not found or something else bad happened.
235 ret = fdt_parse_prop(pathp, prop, newval, data, &len);
239 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
241 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
245 /********************************************************************
246 * Print (recursive) / List (single level)
247 ********************************************************************/
248 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
249 int depth = MAX_LEVEL; /* how deep to print */
250 char *pathp; /* path */
251 char *prop; /* property */
252 int ret; /* return value */
255 * list is an alias for print, but limited to 1 level
257 if (argv[1][0] == 'l') {
262 * Get the starting path. The root node is an oddball,
263 * the offset is zero and has no name.
271 ret = fdt_print(pathp, prop, depth);
275 /********************************************************************
276 * Remove a property/node
277 ********************************************************************/
278 } else if (argv[1][0] == 'r') {
279 int nodeoffset; /* node offset from libfdt */
283 * Get the path. The root node is an oddball, the offset
284 * is zero and has no name.
286 nodeoffset = findnodeoffset(argv[2]);
287 if (nodeoffset < 0) {
289 * Not found or something else bad happened.
294 * Do the delete. A fourth parameter means delete a property,
295 * otherwise delete the node.
298 err = fdt_delprop(fdt, nodeoffset, argv[3]);
300 printf("libfdt fdt_delprop(): %s\n",
305 err = fdt_del_node(fdt, nodeoffset);
307 printf("libfdt fdt_del_node(): %s\n",
313 /********************************************************************
314 * Create a chosen node
315 ********************************************************************/
316 } else if (argv[1][0] == 'c') {
317 fdt_chosen(fdt, 0, 0, 1);
319 /********************************************************************
320 * Create a u-boot-env node
321 ********************************************************************/
322 } else if (argv[1][0] == 'e') {
325 /********************************************************************
327 ********************************************************************/
328 } else if (argv[1][0] == 'b') {
331 /********************************************************************
332 * Unrecognized command
333 ********************************************************************/
335 printf ("Usage:\n%s\n", cmdtp->usage);
342 /****************************************************************************/
344 static int fdt_valid(void)
349 printf ("The address of the fdt is invalid (NULL).\n");
353 err = fdt_check_header(fdt);
355 return 1; /* valid */
358 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
360 * Be more informative on bad version.
362 if (err == -FDT_ERR_BADVERSION) {
363 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
364 printf (" - too old, fdt $d < %d",
366 FDT_FIRST_SUPPORTED_VERSION);
369 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
370 printf (" - too new, fdt $d > %d",
372 FDT_LAST_SUPPORTED_VERSION);
383 /****************************************************************************/
386 * Parse the user's input, partially heuristic. Valid formats:
388 * <0011> - hex half word (16 bits)
389 * <00112233> - hex word (32 bits)
390 * - hex double words (64 bits) are not supported, must use
391 * a byte stream instead.
392 * [00 11 22 .. nn] - byte stream
393 * "string" - If the the value doesn't start with "<" or "[", it is
394 * treated as a string. Note that the quotes are
395 * stripped by the parser before we get the string.
397 static int fdt_parse_prop(char *pathp, char *prop, char *newval,
398 char *data, int *len)
400 char *cp; /* temporary char pointer */
401 unsigned long tmp; /* holds converted values */
403 if (*newval == '<') {
405 * Bigger values than bytes.
409 while ((*newval != '>') && (*newval != '\0')) {
411 tmp = simple_strtoul(cp, &newval, 16);
412 if ((newval - cp) <= 2) {
416 } else if ((newval - cp) <= 4) {
417 *(uint16_t *)data = __cpu_to_be16(tmp);
420 } else if ((newval - cp) <= 8) {
421 *(uint32_t *)data = __cpu_to_be32(tmp);
425 printf("Sorry, I could not convert \"%s\"\n",
429 while (*newval == ' ')
432 if (*newval != '>') {
433 printf("Unexpected character '%c'\n", *newval);
436 } else if (*newval == '[') {
438 * Byte stream. Convert the values.
442 while ((*newval != ']') && (*newval != '\0')) {
443 tmp = simple_strtoul(newval, &newval, 16);
444 *data++ = tmp & 0xFF;
446 while (*newval == ' ')
449 if (*newval != ']') {
450 printf("Unexpected character '%c'\n", *newval);
455 * Assume it is a string. Copy it into our data area for
456 * convenience (including the terminating '\0').
458 *len = strlen(newval) + 1;
459 strcpy(data, newval);
464 /****************************************************************************/
467 * Heuristic to guess if this is a string or concatenated strings.
470 static int is_printable_string(const void *data, int len)
472 const char *s = data;
474 /* zero length is not */
478 /* must terminate with zero */
479 if (s[len - 1] != '\0')
482 /* printable or a null byte (concatenated strings) */
483 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
485 * If we see a null, there are three possibilities:
486 * 1) If len == 1, it is the end of the string, printable
487 * 2) Next character also a null, not printable.
488 * 3) Next character not a null, continue to check.
500 /* Not the null termination, or not done yet: not printable */
501 if (*s != '\0' || (len != 0))
509 * Print the property in the best format, a heuristic guess. Print as
510 * a string, concatenated strings, a byte, word, double word, or (if all
511 * else fails) it is printed as a stream of bytes.
513 static void print_data(const void *data, int len)
518 /* no data, don't print */
523 * It is a string, but it may have multiple strings (embedded '\0's).
525 if (is_printable_string(data, len)) {
532 j += strlen(data) + 1;
533 data += strlen(data) + 1;
541 printf("<%02x>", (*(u8 *) data) & 0xff);
543 case 2: /* half-word */
544 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
547 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
549 case 8: /* double-word */
551 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
553 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
555 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
558 default: /* anything else... hexdump */
560 for (j = 0, s = data; j < len; j++)
561 printf("%02x%s", s[j], j < len - 1 ? " " : "");
568 /****************************************************************************/
571 * Recursively print (a portion of) the fdt. The depth parameter
572 * determines how deeply nested the fdt is printed.
574 static int fdt_print(char *pathp, char *prop, int depth)
576 static int offstack[MAX_LEVEL];
577 static char tabs[MAX_LEVEL+1] =
578 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
579 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
580 void *nodep; /* property node pointer */
581 int nodeoffset; /* node offset from libfdt */
582 int nextoffset; /* next node offset from libfdt */
583 uint32_t tag; /* tag */
584 int len; /* length of the property */
585 int level = 0; /* keep track of nesting level */
587 nodeoffset = findnodeoffset(pathp);
588 if (nodeoffset < 0) {
590 * Not found or something else bad happened.
595 * The user passed in a property as well as node path.
596 * Print only the given property and then return.
599 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
601 /* no property value */
602 printf("%s %s\n", pathp, prop);
604 } else if (len > 0) {
606 print_data (nodep, len);
610 printf ("libfdt fdt_getprop(): %s\n",
617 * The user passed in a node path and no property,
618 * print the node and all subnodes.
620 offstack[0] = nodeoffset;
623 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
628 &tabs[MAX_LEVEL - level], pathp);
630 offstack[level] = nodeoffset;
631 if (level >= MAX_LEVEL) {
632 printf("Aaaiii <splat> nested too deep. "
640 printf("%s};\n", &tabs[MAX_LEVEL - level]);
642 level = -1; /* exit the loop */
646 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
648 printf ("libfdt fdt_getprop(): %s\n",
651 } else if (len == 0) {
652 /* the property has no value */
655 &tabs[MAX_LEVEL - level],
660 &tabs[MAX_LEVEL - level],
662 print_data (nodep, len);
673 printf("Unknown tag 0x%08X\n", tag);
676 nodeoffset = nextoffset;
681 /********************************************************************/
685 "fdt - flattened device tree utility commands\n",
686 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
687 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
688 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
689 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
690 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
691 "fdt mknode <path> <node> - Create a new node after <path>\n"
692 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
693 "fdt chosen - Add/update the \"/chosen\" branch in the tree\n"
694 #ifdef CONFIG_OF_HAS_UBOOT_ENV
695 "fdt env - Add/replace the \"/u-boot-env\" branch in the tree\n"
697 #ifdef CONFIG_OF_HAS_BD_T
698 "fdt bd_t - Add/replace the \"/bd_t\" branch in the tree\n"
701 " * If the property you are setting/printing has a '#' character,\n"
702 " you MUST escape it with a \\ character or quote it with \" or\n"
703 " it will be ignored as a comment.\n"
704 " * If the value has spaces in it, you MUST escape the spaces with\n"
705 " \\ characters or quote it with \"\"\n"
706 "Examples: fdt print / # print the whole tree\n"
707 " fdt print /cpus \"#address-cells\"\n"
708 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
711 #endif /* CONFIG_OF_LIBFDT */