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;
50 static char data[SCRATCHPAD];
54 * Function prototypes/declarations.
56 static int fdt_valid(void);
57 static void print_data(const void *data, int len);
61 * Flattened Device Tree command, see the help for parameter definitions.
63 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
68 printf ("Usage:\n%s\n", cmdtp->usage);
73 * Figure out which subcommand was given
76 /********************************************************************
77 * Set the address of the fdt
78 ********************************************************************/
81 * Set the address [and length] of the fdt.
83 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
95 len = simple_strtoul(argv[3], NULL, 16);
96 if (len < fdt_totalsize(fdt)) {
97 printf ("New length %d < existing length %d, ignoring.\n",
98 len, fdt_totalsize(fdt));
101 * Open in place with a new length.
103 err = fdt_open_into(fdt, fdt, len);
105 printf ("libfdt: %s\n", fdt_strerror(err));
110 /********************************************************************
112 ********************************************************************/
113 } else if (op == 'm') {
114 struct fdt_header *newaddr;
119 printf ("Usage:\n%s\n", cmdtp->usage);
124 * Set the address and length of the fdt.
126 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
131 newaddr = (struct fdt_header *)simple_strtoul(argv[3], NULL, 16);
132 len = simple_strtoul(argv[4], NULL, 16);
133 if (len < fdt_totalsize(fdt)) {
134 printf ("New length %d < existing length %d, aborting.\n",
135 len, fdt_totalsize(fdt));
140 * Copy to the new location.
142 err = fdt_open_into(fdt, newaddr, len);
144 printf ("libfdt: %s\n", fdt_strerror(err));
149 /********************************************************************
150 * Set the value of a node in the fdt.
151 ********************************************************************/
152 } else if (op == 's') {
153 char *pathp; /* path */
154 char *prop; /* property */
155 struct fdt_property *nodep; /* node struct pointer */
156 char *newval; /* value from the user (as a string) */
157 char *vp; /* temporary value pointer */
158 char *cp; /* temporary char pointer */
159 int nodeoffset; /* node offset from libfdt */
160 int len; /* new length of the property */
161 int oldlen; /* original length of the property */
162 unsigned long tmp; /* holds converted values */
163 int ret; /* return value */
166 * Parameters: Node path, property, value.
169 printf ("Usage:\n%s\n", cmdtp->usage);
177 if (strcmp(pathp, "/") == 0) {
180 nodeoffset = fdt_path_offset (fdt, pathp);
181 if (nodeoffset < 0) {
183 * Not found or something else bad happened.
185 printf ("libfdt: %s\n", fdt_strerror(nodeoffset));
189 nodep = fdt_getprop (fdt, nodeoffset, prop, &oldlen);
191 printf ("libfdt %s\n", fdt_strerror(oldlen));
193 } else if (oldlen == 0) {
195 * The specified property has no value
197 printf("%s has no value, cannot set one (yet).\n", prop);
201 * Convert the new property
204 if (*newval == '<') {
206 * Bigger values than bytes.
210 while ((*newval != '>') && (*newval != '\0')) {
212 tmp = simple_strtoul(cp, &newval, 16);
213 if ((newval - cp) <= 2) {
217 } else if ((newval - cp) <= 4) {
218 *(uint16_t *)vp = __cpu_to_be16(tmp);
221 } else if ((newval - cp) <= 8) {
222 *(uint32_t *)vp = __cpu_to_be32(tmp);
226 printf("Sorry, I could not convert \"%s\"\n", cp);
229 while (*newval == ' ')
232 if (*newval != '>') {
233 printf("Unexpected character '%c'\n", *newval);
236 } else if (*newval == '[') {
238 * Byte stream. Convert the values.
242 while ((*newval != ']') && (*newval != '\0')) {
243 tmp = simple_strtoul(newval, &newval, 16);
246 while (*newval == ' ')
249 if (*newval != ']') {
250 printf("Unexpected character '%c'\n", *newval);
255 * Assume it is a string. Copy it into our data area for
256 * convenience (including the terminating '\0').
258 len = strlen(newval) + 1;
259 strcpy(data, newval);
262 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
264 printf ("libfdt %s\n", fdt_strerror(ret));
269 /********************************************************************
270 * Print (recursive) / List (single level)
271 ********************************************************************/
272 } else if ((op == 'p') || (op == 'l')) {
274 * Recursively print (a portion of) the fdt.
276 static int offstack[MAX_LEVEL];
277 static char tabs[MAX_LEVEL+1] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
278 int depth = MAX_LEVEL; /* how deep to print */
279 char *pathp; /* path */
280 char *prop; /* property */
281 void *nodep; /* property node pointer */
282 int nodeoffset; /* node offset from libfdt */
283 int nextoffset; /* next node offset from libfdt */
284 uint32_t tag; /* tag */
285 int len; /* length of the property */
286 int level = 0; /* keep track of nesting level */
289 * list is an alias for print, but limited to 1 level
296 * Get the starting path. The root node is an oddball,
297 * the offset is zero and has no name.
305 if (strcmp(pathp, "/") == 0) {
309 nodeoffset = fdt_path_offset (fdt, pathp);
310 if (nodeoffset < 0) {
312 * Not found or something else bad happened.
314 printf ("libfdt %s\n", fdt_strerror(nodeoffset));
319 * The user passed in a property as well as node path. Print only
320 * the given property and then return.
323 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
325 printf("%s %s\n", pathp, prop); /* no property value */
327 } else if (len > 0) {
329 print_data (nodep, len);
333 printf ("libfdt %s\n", fdt_strerror(len));
339 * The user passed in a node path and no property, print the node
342 offstack[0] = nodeoffset;
345 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
349 printf("%s%s {\n", &tabs[MAX_LEVEL - level], pathp);
351 offstack[level] = nodeoffset;
352 if (level >= MAX_LEVEL) {
353 printf("Aaaiii <splat> nested too deep.\n");
360 printf("%s};\n", &tabs[MAX_LEVEL - level]);
362 level = -1; /* exit the loop */
366 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
368 printf ("libfdt %s\n", fdt_strerror(len));
370 } else if (len == 0) {
371 /* the property has no value */
373 printf("%s%s;\n", &tabs[MAX_LEVEL - level], pathp);
376 printf("%s%s=", &tabs[MAX_LEVEL - level], pathp);
377 print_data (nodep, len);
388 printf("Unknown tag 0x%08X\n", tag);
391 nodeoffset = nextoffset;
394 /********************************************************************
395 * Remove a property/node
396 ********************************************************************/
397 } else if (op == 'r') {
398 int nodeoffset; /* node offset from libfdt */
402 * Get the path. The root node is an oddball, the offset
403 * is zero and has no name.
405 if (strcmp(argv[2], "/") == 0) {
408 nodeoffset = fdt_path_offset (fdt, argv[2]);
409 if (nodeoffset < 0) {
411 * Not found or something else bad happened.
413 printf ("libfdt %s\n", fdt_strerror(nodeoffset));
418 * Do the delete. A fourth parameter means delete a property,
419 * otherwise delete the node.
422 err = fdt_delprop(fdt, nodeoffset, argv[3]);
424 printf("fdt_delprop libfdt: %s\n", fdt_strerror(err));
428 err = fdt_del_node(fdt, nodeoffset);
430 printf("fdt_del_node libfdt: %s\n", fdt_strerror(err));
435 /********************************************************************
436 * Create a chosen node
437 ********************************************************************/
438 } else if (op == 'c') {
439 fdt_chosen(fdt, 0, 0, 1);
441 /********************************************************************
442 * Create a u-boot-env node
443 ********************************************************************/
444 } else if (op == 'e') {
447 /********************************************************************
449 ********************************************************************/
450 } else if (op == 'b') {
453 /********************************************************************
454 * Unrecognized command
455 ********************************************************************/
457 printf ("Usage:\n%s\n", cmdtp->usage);
464 /********************************************************************/
466 static int fdt_valid(void)
471 printf ("The address of the fdt is invalid (NULL).\n");
475 err = fdt_check_header(fdt);
477 return 1; /* valid */
480 printf("libfdt: %s", fdt_strerror(err));
482 * Be more informative on bad version.
484 if (err == -FDT_ERR_BADVERSION) {
485 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
486 printf (" - too old, fdt $d < %d",
487 fdt_version(fdt), FDT_FIRST_SUPPORTED_VERSION);
490 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
491 printf (" - too new, fdt $d > %d",
492 fdt_version(fdt), FDT_LAST_SUPPORTED_VERSION);
503 /********************************************************************/
506 * OF flat tree handling
507 * Written by: Pantelis Antoniou <pantelis.antoniou@gmail.com>
508 * Updated by: Matthew McClintock <msm@freescale.com>
509 * Converted to libfdt by: Gerald Van Baren <vanbaren@cideas.com>
512 static int is_printable_string(const void *data, int len)
514 const char *s = data;
516 /* zero length is not */
520 /* must terminate with zero */
521 if (s[len - 1] != '\0')
524 /* printable or a null byte (concatenated strings) */
525 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
527 * If we see a null, there are three possibilities:
528 * 1) If len == 1, it is the end of the string, printable
529 * 2) Next character also a null, not printable.
530 * 3) Next character not a null, continue to check.
542 /* Not the null termination, or not done yet: not printable */
543 if (*s != '\0' || (len != 0))
549 static void print_data(const void *data, int len)
554 /* no data, don't print */
559 * It is a string, but it may have multiple strings (embedded '\0's).
561 if (is_printable_string(data, len)) {
568 j += strlen(data) + 1;
569 data += strlen(data) + 1;
577 printf("<%02x>", (*(u8 *) data) & 0xff);
579 case 2: /* half-word */
580 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
583 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
585 case 8: /* double-word */
587 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
589 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
591 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
594 default: /* anything else... hexdump */
596 for (j = 0, s = data; j < len; j++)
597 printf("%02x%s", s[j], j < len - 1 ? " " : "");
604 /********************************************************************/
608 "fdt - flattened device tree utility commands\n",
609 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
610 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
611 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
612 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
613 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
614 "fdt mknode <path> <node> - Create a new node after <path>\n"
615 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
616 "fdt chosen - Add/update the \"/chosen\" branch in the tree\n"
617 #ifdef CONFIG_OF_HAS_UBOOT_ENV
618 "fdt env - Add/replace the \"/u-boot-env\" branch in the tree\n"
620 #ifdef CONFIG_OF_HAS_BD_T
621 "fdt bd_t - Add/replace the \"/bd_t\" branch in the tree\n"
624 " * Set a larger length with the fdt addr command to add to the blob.\n"
625 " * If the property you are setting/printing has a '#' character,\n"
626 " you MUST escape it with a \\ character or quote it with \" or\n"
627 " it will be ignored as a comment.\n"
628 " * If the value has spaces in it, you MUST escape the spaces with\n"
629 " \\ characters or quote it with \"\"\n"
630 "Examples: fdt print / # print the whole tree\n"
631 " fdt print /cpus \"#address-cells\"\n"
632 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
635 #endif /* CONFIG_OF_LIBFDT */