Fixed tools/env utilities
[u-boot-openmoko/mini2440.git] / common / cmd_fdt.c
blob7436a95153c378004acb68e9e1d759e0dd91d254
1 /*
2 * (C) Copyright 2007
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
9 * project.
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,
24 * MA 02111-1307 USA
27 #include <common.h>
28 #include <command.h>
29 #include <linux/ctype.h>
30 #include <linux/types.h>
31 #include <asm/global_data.h>
32 #include <fdt.h>
33 #include <libfdt.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 **newval, int count, char *data, int *len);
46 static int fdt_print(const char *pathp, char *prop, int depth);
49 * Flattened Device Tree command, see the help for parameter definitions.
51 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
53 if (argc < 2) {
54 printf ("Usage:\n%s\n", cmdtp->usage);
55 return 1;
58 /********************************************************************
59 * Set the address of the fdt
60 ********************************************************************/
61 if (argv[1][0] == 'a') {
63 * Set the address [and length] of the fdt.
65 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
67 if (!fdt_valid()) {
68 return 1;
71 if (argc >= 4) {
72 int len;
73 int err;
75 * Optional new length
77 len = simple_strtoul(argv[3], NULL, 16);
78 if (len < fdt_totalsize(fdt)) {
79 printf ("New length %d < existing length %d, "
80 "ignoring.\n",
81 len, fdt_totalsize(fdt));
82 } else {
84 * Open in place with a new length.
86 err = fdt_open_into(fdt, fdt, len);
87 if (err != 0) {
88 printf ("libfdt fdt_open_into(): %s\n",
89 fdt_strerror(err));
94 /********************************************************************
95 * Move the fdt
96 ********************************************************************/
97 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
98 struct fdt_header *newaddr;
99 int len;
100 int err;
102 if (argc < 4) {
103 printf ("Usage:\n%s\n", cmdtp->usage);
104 return 1;
108 * Set the address and length of the fdt.
110 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
111 if (!fdt_valid()) {
112 return 1;
115 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
118 * If the user specifies a length, use that. Otherwise use the
119 * current length.
121 if (argc <= 4) {
122 len = fdt_totalsize(fdt);
123 } else {
124 len = simple_strtoul(argv[4], NULL, 16);
125 if (len < fdt_totalsize(fdt)) {
126 printf ("New length 0x%X < existing length "
127 "0x%X, aborting.\n",
128 len, fdt_totalsize(fdt));
129 return 1;
134 * Copy to the new location.
136 err = fdt_open_into(fdt, newaddr, len);
137 if (err != 0) {
138 printf ("libfdt fdt_open_into(): %s\n",
139 fdt_strerror(err));
140 return 1;
142 fdt = newaddr;
144 /********************************************************************
145 * Make a new node
146 ********************************************************************/
147 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
148 char *pathp; /* path */
149 char *nodep; /* new node to add */
150 int nodeoffset; /* node offset from libfdt */
151 int err;
154 * Parameters: Node path, new node to be appended to the path.
156 if (argc < 4) {
157 printf ("Usage:\n%s\n", cmdtp->usage);
158 return 1;
161 pathp = argv[2];
162 nodep = argv[3];
164 nodeoffset = fdt_path_offset (fdt, pathp);
165 if (nodeoffset < 0) {
167 * Not found or something else bad happened.
169 printf ("libfdt fdt_path_offset() returned %s\n",
170 fdt_strerror(nodeoffset));
171 return 1;
173 err = fdt_add_subnode(fdt, nodeoffset, nodep);
174 if (err < 0) {
175 printf ("libfdt fdt_add_subnode(): %s\n",
176 fdt_strerror(err));
177 return 1;
180 /********************************************************************
181 * Set the value of a property in the fdt.
182 ********************************************************************/
183 } else if (argv[1][0] == 's') {
184 char *pathp; /* path */
185 char *prop; /* property */
186 int nodeoffset; /* node offset from libfdt */
187 static char data[SCRATCHPAD]; /* storage for the property */
188 int len; /* new length of the property */
189 int ret; /* return value */
192 * Parameters: Node path, property, optional value.
194 if (argc < 4) {
195 printf ("Usage:\n%s\n", cmdtp->usage);
196 return 1;
199 pathp = argv[2];
200 prop = argv[3];
201 if (argc == 4) {
202 len = 0;
203 } else {
204 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
205 if (ret != 0)
206 return ret;
209 nodeoffset = fdt_path_offset (fdt, pathp);
210 if (nodeoffset < 0) {
212 * Not found or something else bad happened.
214 printf ("libfdt fdt_path_offset() returned %s\n",
215 fdt_strerror(nodeoffset));
216 return 1;
219 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
220 if (ret < 0) {
221 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
222 return 1;
225 /********************************************************************
226 * Print (recursive) / List (single level)
227 ********************************************************************/
228 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
229 int depth = MAX_LEVEL; /* how deep to print */
230 char *pathp; /* path */
231 char *prop; /* property */
232 int ret; /* return value */
233 static char root[2] = "/";
236 * list is an alias for print, but limited to 1 level
238 if (argv[1][0] == 'l') {
239 depth = 1;
243 * Get the starting path. The root node is an oddball,
244 * the offset is zero and has no name.
246 if (argc == 2)
247 pathp = root;
248 else
249 pathp = argv[2];
250 if (argc > 3)
251 prop = argv[3];
252 else
253 prop = NULL;
255 ret = fdt_print(pathp, prop, depth);
256 if (ret != 0)
257 return ret;
259 /********************************************************************
260 * Remove a property/node
261 ********************************************************************/
262 } else if ((argv[1][0] == 'r') && (argv[1][1] == 'm')) {
263 int nodeoffset; /* node offset from libfdt */
264 int err;
267 * Get the path. The root node is an oddball, the offset
268 * is zero and has no name.
270 nodeoffset = fdt_path_offset (fdt, argv[2]);
271 if (nodeoffset < 0) {
273 * Not found or something else bad happened.
275 printf ("libfdt fdt_path_offset() returned %s\n",
276 fdt_strerror(nodeoffset));
277 return 1;
280 * Do the delete. A fourth parameter means delete a property,
281 * otherwise delete the node.
283 if (argc > 3) {
284 err = fdt_delprop(fdt, nodeoffset, argv[3]);
285 if (err < 0) {
286 printf("libfdt fdt_delprop(): %s\n",
287 fdt_strerror(err));
288 return err;
290 } else {
291 err = fdt_del_node(fdt, nodeoffset);
292 if (err < 0) {
293 printf("libfdt fdt_del_node(): %s\n",
294 fdt_strerror(err));
295 return err;
299 /********************************************************************
300 * Display header info
301 ********************************************************************/
302 } else if (argv[1][0] == 'h') {
303 u32 version = fdt_version(fdt);
304 printf("magic:\t\t\t0x%x\n", fdt_magic(fdt));
305 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(fdt), fdt_totalsize(fdt));
306 printf("off_dt_struct:\t\t0x%x\n", fdt_off_dt_struct(fdt));
307 printf("off_dt_strings:\t\t0x%x\n", fdt_off_dt_strings(fdt));
308 printf("off_mem_rsvmap:\t\t0x%x\n", fdt_off_mem_rsvmap(fdt));
309 printf("version:\t\t%d\n", version);
310 printf("last_comp_version:\t%d\n", fdt_last_comp_version(fdt));
311 if (version >= 2)
312 printf("boot_cpuid_phys:\t0x%x\n",
313 fdt_boot_cpuid_phys(fdt));
314 if (version >= 3)
315 printf("size_dt_strings:\t0x%x\n",
316 fdt_size_dt_strings(fdt));
317 if (version >= 17)
318 printf("size_dt_struct:\t\t0x%x\n",
319 fdt_size_dt_struct(fdt));
320 printf("number mem_rsv:\t\t0x%x\n", fdt_num_mem_rsv(fdt));
321 printf("\n");
323 /********************************************************************
324 * Set boot cpu id
325 ********************************************************************/
326 } else if ((argv[1][0] == 'b') && (argv[1][1] == 'o') &&
327 (argv[1][2] == 'o')) {
328 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
329 fdt_set_boot_cpuid_phys(fdt, tmp);
331 /********************************************************************
332 * memory command
333 ********************************************************************/
334 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'e')) {
335 uint64_t addr, size;
336 int err;
337 #ifdef CFG_64BIT_STRTOUL
338 addr = simple_strtoull(argv[2], NULL, 16);
339 size = simple_strtoull(argv[3], NULL, 16);
340 #else
341 addr = simple_strtoul(argv[2], NULL, 16);
342 size = simple_strtoul(argv[3], NULL, 16);
343 #endif
344 err = fdt_fixup_memory(fdt, addr, size);
345 if (err < 0)
346 return err;
348 /********************************************************************
349 * mem reserve commands
350 ********************************************************************/
351 } else if ((argv[1][0] == 'r') && (argv[1][1] == 's')) {
352 if (argv[2][0] == 'p') {
353 uint64_t addr, size;
354 int total = fdt_num_mem_rsv(fdt);
355 int j, err;
356 printf("index\t\t start\t\t size\n");
357 printf("-------------------------------"
358 "-----------------\n");
359 for (j = 0; j < total; j++) {
360 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
361 if (err < 0) {
362 printf("libfdt fdt_get_mem_rsv(): %s\n",
363 fdt_strerror(err));
364 return err;
366 printf(" %x\t%08x%08x\t%08x%08x\n", j,
367 (u32)(addr >> 32),
368 (u32)(addr & 0xffffffff),
369 (u32)(size >> 32),
370 (u32)(size & 0xffffffff));
372 } else if (argv[2][0] == 'a') {
373 uint64_t addr, size;
374 int err;
375 #ifdef CFG_64BIT_STRTOUL
376 addr = simple_strtoull(argv[3], NULL, 16);
377 size = simple_strtoull(argv[4], NULL, 16);
378 #else
379 addr = simple_strtoul(argv[3], NULL, 16);
380 size = simple_strtoul(argv[4], NULL, 16);
381 #endif
382 err = fdt_add_mem_rsv(fdt, addr, size);
384 if (err < 0) {
385 printf("libfdt fdt_add_mem_rsv(): %s\n",
386 fdt_strerror(err));
387 return err;
389 } else if (argv[2][0] == 'd') {
390 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
391 int err = fdt_del_mem_rsv(fdt, idx);
393 if (err < 0) {
394 printf("libfdt fdt_del_mem_rsv(): %s\n",
395 fdt_strerror(err));
396 return err;
398 } else {
399 /* Unrecognized command */
400 printf ("Usage:\n%s\n", cmdtp->usage);
401 return 1;
404 #ifdef CONFIG_OF_BOARD_SETUP
405 /* Call the board-specific fixup routine */
406 else if (argv[1][0] == 'b')
407 ft_board_setup(fdt, gd->bd);
408 #endif
409 /* Create a chosen node */
410 else if (argv[1][0] == 'c')
411 fdt_chosen(fdt, 0, 0, 1);
412 else {
413 /* Unrecognized command */
414 printf ("Usage:\n%s\n", cmdtp->usage);
415 return 1;
418 return 0;
421 /****************************************************************************/
423 static int fdt_valid(void)
425 int err;
427 if (fdt == NULL) {
428 printf ("The address of the fdt is invalid (NULL).\n");
429 return 0;
432 err = fdt_check_header(fdt);
433 if (err == 0)
434 return 1; /* valid */
436 if (err < 0) {
437 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
439 * Be more informative on bad version.
441 if (err == -FDT_ERR_BADVERSION) {
442 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
443 printf (" - too old, fdt $d < %d",
444 fdt_version(fdt),
445 FDT_FIRST_SUPPORTED_VERSION);
446 fdt = NULL;
448 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
449 printf (" - too new, fdt $d > %d",
450 fdt_version(fdt),
451 FDT_LAST_SUPPORTED_VERSION);
452 fdt = NULL;
454 return 0;
456 printf("\n");
457 return 0;
459 return 1;
462 /****************************************************************************/
465 * Parse the user's input, partially heuristic. Valid formats:
466 * <0x00112233 4 05> - an array of cells. Numbers follow standard
467 * C conventions.
468 * [00 11 22 .. nn] - byte stream
469 * "string" - If the the value doesn't start with "<" or "[", it is
470 * treated as a string. Note that the quotes are
471 * stripped by the parser before we get the string.
472 * newval: An array of strings containing the new property as specified
473 * on the command line
474 * count: The number of strings in the array
475 * data: A bytestream to be placed in the property
476 * len: The length of the resulting bytestream
478 static int fdt_parse_prop(char **newval, int count, char *data, int *len)
480 char *cp; /* temporary char pointer */
481 char *newp; /* temporary newval char pointer */
482 unsigned long tmp; /* holds converted values */
483 int stridx = 0;
485 *len = 0;
486 newp = newval[0];
488 /* An array of cells */
489 if (*newp == '<') {
490 newp++;
491 while ((*newp != '>') && (stridx < count)) {
493 * Keep searching until we find that last ">"
494 * That way users don't have to escape the spaces
496 if (*newp == '\0') {
497 newp = newval[++stridx];
498 continue;
501 cp = newp;
502 tmp = simple_strtoul(cp, &newp, 0);
503 *(uint32_t *)data = __cpu_to_be32(tmp);
504 data += 4;
505 *len += 4;
507 /* If the ptr didn't advance, something went wrong */
508 if ((newp - cp) <= 0) {
509 printf("Sorry, I could not convert \"%s\"\n",
510 cp);
511 return 1;
514 while (*newp == ' ')
515 newp++;
518 if (*newp != '>') {
519 printf("Unexpected character '%c'\n", *newp);
520 return 1;
522 } else if (*newp == '[') {
524 * Byte stream. Convert the values.
526 newp++;
527 while ((*newp != ']') && (stridx < count)) {
528 tmp = simple_strtoul(newp, &newp, 16);
529 *data++ = tmp & 0xFF;
530 *len = *len + 1;
531 while (*newp == ' ')
532 newp++;
533 if (*newp != '\0')
534 newp = newval[++stridx];
536 if (*newp != ']') {
537 printf("Unexpected character '%c'\n", *newval);
538 return 1;
540 } else {
542 * Assume it is a string. Copy it into our data area for
543 * convenience (including the terminating '\0').
545 while (stridx < count) {
546 *len = strlen(newp) + 1;
547 strcpy(data, newp);
548 newp = newval[++stridx];
551 return 0;
554 /****************************************************************************/
557 * Heuristic to guess if this is a string or concatenated strings.
560 static int is_printable_string(const void *data, int len)
562 const char *s = data;
564 /* zero length is not */
565 if (len == 0)
566 return 0;
568 /* must terminate with zero */
569 if (s[len - 1] != '\0')
570 return 0;
572 /* printable or a null byte (concatenated strings) */
573 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
575 * If we see a null, there are three possibilities:
576 * 1) If len == 1, it is the end of the string, printable
577 * 2) Next character also a null, not printable.
578 * 3) Next character not a null, continue to check.
580 if (s[0] == '\0') {
581 if (len == 1)
582 return 1;
583 if (s[1] == '\0')
584 return 0;
586 s++;
587 len--;
590 /* Not the null termination, or not done yet: not printable */
591 if (*s != '\0' || (len != 0))
592 return 0;
594 return 1;
599 * Print the property in the best format, a heuristic guess. Print as
600 * a string, concatenated strings, a byte, word, double word, or (if all
601 * else fails) it is printed as a stream of bytes.
603 static void print_data(const void *data, int len)
605 int j;
607 /* no data, don't print */
608 if (len == 0)
609 return;
612 * It is a string, but it may have multiple strings (embedded '\0's).
614 if (is_printable_string(data, len)) {
615 puts("\"");
616 j = 0;
617 while (j < len) {
618 if (j > 0)
619 puts("\", \"");
620 puts(data);
621 j += strlen(data) + 1;
622 data += strlen(data) + 1;
624 puts("\"");
625 return;
628 if ((len %4) == 0) {
629 const u32 *p;
631 printf("<");
632 for (j = 0, p = data; j < len/4; j ++)
633 printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
634 printf(">");
635 } else { /* anything else... hexdump */
636 const u8 *s;
638 printf("[");
639 for (j = 0, s = data; j < len; j++)
640 printf("%02x%s", s[j], j < len - 1 ? " " : "");
641 printf("]");
645 /****************************************************************************/
648 * Recursively print (a portion of) the fdt. The depth parameter
649 * determines how deeply nested the fdt is printed.
651 static int fdt_print(const char *pathp, char *prop, int depth)
653 static char tabs[MAX_LEVEL+1] =
654 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
655 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
656 const void *nodep; /* property node pointer */
657 int nodeoffset; /* node offset from libfdt */
658 int nextoffset; /* next node offset from libfdt */
659 uint32_t tag; /* tag */
660 int len; /* length of the property */
661 int level = 0; /* keep track of nesting level */
662 const struct fdt_property *fdt_prop;
664 nodeoffset = fdt_path_offset (fdt, pathp);
665 if (nodeoffset < 0) {
667 * Not found or something else bad happened.
669 printf ("libfdt fdt_path_offset() returned %s\n",
670 fdt_strerror(nodeoffset));
671 return 1;
674 * The user passed in a property as well as node path.
675 * Print only the given property and then return.
677 if (prop) {
678 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
679 if (len == 0) {
680 /* no property value */
681 printf("%s %s\n", pathp, prop);
682 return 0;
683 } else if (len > 0) {
684 printf("%s = ", prop);
685 print_data (nodep, len);
686 printf("\n");
687 return 0;
688 } else {
689 printf ("libfdt fdt_getprop(): %s\n",
690 fdt_strerror(len));
691 return 1;
696 * The user passed in a node path and no property,
697 * print the node and all subnodes.
699 while(level >= 0) {
700 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
701 switch(tag) {
702 case FDT_BEGIN_NODE:
703 pathp = fdt_get_name(fdt, nodeoffset, NULL);
704 if (level <= depth) {
705 if (pathp == NULL)
706 pathp = "/* NULL pointer error */";
707 if (*pathp == '\0')
708 pathp = "/"; /* root is nameless */
709 printf("%s%s {\n",
710 &tabs[MAX_LEVEL - level], pathp);
712 level++;
713 if (level >= MAX_LEVEL) {
714 printf("Nested too deep, aborting.\n");
715 return 1;
717 break;
718 case FDT_END_NODE:
719 level--;
720 if (level <= depth)
721 printf("%s};\n", &tabs[MAX_LEVEL - level]);
722 if (level == 0) {
723 level = -1; /* exit the loop */
725 break;
726 case FDT_PROP:
727 fdt_prop = fdt_offset_ptr(fdt, nodeoffset,
728 sizeof(*fdt_prop));
729 pathp = fdt_string(fdt,
730 fdt32_to_cpu(fdt_prop->nameoff));
731 len = fdt32_to_cpu(fdt_prop->len);
732 nodep = fdt_prop->data;
733 if (len < 0) {
734 printf ("libfdt fdt_getprop(): %s\n",
735 fdt_strerror(len));
736 return 1;
737 } else if (len == 0) {
738 /* the property has no value */
739 if (level <= depth)
740 printf("%s%s;\n",
741 &tabs[MAX_LEVEL - level],
742 pathp);
743 } else {
744 if (level <= depth) {
745 printf("%s%s = ",
746 &tabs[MAX_LEVEL - level],
747 pathp);
748 print_data (nodep, len);
749 printf(";\n");
752 break;
753 case FDT_NOP:
754 printf("/* NOP */\n", &tabs[MAX_LEVEL - level]);
755 break;
756 case FDT_END:
757 return 1;
758 default:
759 if (level <= depth)
760 printf("Unknown tag 0x%08X\n", tag);
761 return 1;
763 nodeoffset = nextoffset;
765 return 0;
768 /********************************************************************/
770 U_BOOT_CMD(
771 fdt, 255, 0, do_fdt,
772 "fdt - flattened device tree utility commands\n",
773 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
774 #ifdef CONFIG_OF_BOARD_SETUP
775 "fdt boardsetup - Do board-specific set up\n"
776 #endif
777 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
778 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
779 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
780 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
781 "fdt mknode <path> <node> - Create a new node after <path>\n"
782 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
783 "fdt header - Display header info\n"
784 "fdt bootcpu <id> - Set boot cpuid\n"
785 "fdt memory <addr> <size> - Add/Update memory node\n"
786 "fdt rsvmem print - Show current mem reserves\n"
787 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
788 "fdt rsvmem delete <index> - Delete a mem reserves\n"
789 "fdt chosen - Add/update the /chosen branch in the tree\n"
790 "NOTE: If the path or property you are setting/printing has a '#' character\n"
791 " or spaces, you MUST escape it with a \\ character or quote it with \".\n"