]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/cmd_fdt.c
Merge branch 'arm/master' into arm/next
[karo-tx-uboot.git] / common / cmd_fdt.c
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>
7  *
8  * See file CREDITS for list of people who contributed to this
9  * project.
10  *
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.
15  *
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.
20  *
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
25  */
26
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>
35
36 #define MAX_LEVEL       32              /* how deeply nested we will go */
37 #define SCRATCHPAD      1024            /* bytes of scratchpad memory */
38
39 /*
40  * Global data (for the gd->bd)
41  */
42 DECLARE_GLOBAL_DATA_PTR;
43
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);
47
48 /*
49  * The working_fdt points to our working flattened device tree.
50  */
51 struct fdt_header *working_fdt;
52
53 void set_working_fdt_addr(void *addr)
54 {
55         char buf[17];
56
57         working_fdt = addr;
58
59         sprintf(buf, "%lx", (unsigned long)addr);
60         setenv("fdtaddr", buf);
61 }
62
63 /*
64  * Flattened Device Tree command, see the help for parameter definitions.
65  */
66 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
67 {
68         if (argc < 2) {
69                 cmd_usage(cmdtp);
70                 return 1;
71         }
72
73         /********************************************************************
74          * Set the address of the fdt
75          ********************************************************************/
76         if (argv[1][0] == 'a') {
77                 unsigned long addr;
78                 /*
79                  * Set the address [and length] of the fdt.
80                  */
81                 if (argc == 2) {
82                         if (!fdt_valid()) {
83                                 return 1;
84                         }
85                         printf("The address of the fdt is %p\n", working_fdt);
86                         return 0;
87                 }
88
89                 addr = simple_strtoul(argv[2], NULL, 16);
90                 set_working_fdt_addr((void *)addr);
91
92                 if (!fdt_valid()) {
93                         return 1;
94                 }
95
96                 if (argc >= 4) {
97                         int  len;
98                         int  err;
99                         /*
100                          * Optional new length
101                          */
102                         len = simple_strtoul(argv[3], NULL, 16);
103                         if (len < fdt_totalsize(working_fdt)) {
104                                 printf ("New length %d < existing length %d, "
105                                         "ignoring.\n",
106                                         len, fdt_totalsize(working_fdt));
107                         } else {
108                                 /*
109                                  * Open in place with a new length.
110                                  */
111                                 err = fdt_open_into(working_fdt, working_fdt, len);
112                                 if (err != 0) {
113                                         printf ("libfdt fdt_open_into(): %s\n",
114                                                 fdt_strerror(err));
115                                 }
116                         }
117                 }
118
119         /********************************************************************
120          * Move the working_fdt
121          ********************************************************************/
122         } else if (strncmp(argv[1], "mo", 2) == 0) {
123                 struct fdt_header *newaddr;
124                 int  len;
125                 int  err;
126
127                 if (argc < 4) {
128                         cmd_usage(cmdtp);
129                         return 1;
130                 }
131
132                 /*
133                  * Set the address and length of the fdt.
134                  */
135                 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
136                 if (!fdt_valid()) {
137                         return 1;
138                 }
139
140                 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
141
142                 /*
143                  * If the user specifies a length, use that.  Otherwise use the
144                  * current length.
145                  */
146                 if (argc <= 4) {
147                         len = fdt_totalsize(working_fdt);
148                 } else {
149                         len = simple_strtoul(argv[4], NULL, 16);
150                         if (len < fdt_totalsize(working_fdt)) {
151                                 printf ("New length 0x%X < existing length "
152                                         "0x%X, aborting.\n",
153                                         len, fdt_totalsize(working_fdt));
154                                 return 1;
155                         }
156                 }
157
158                 /*
159                  * Copy to the new location.
160                  */
161                 err = fdt_open_into(working_fdt, newaddr, len);
162                 if (err != 0) {
163                         printf ("libfdt fdt_open_into(): %s\n",
164                                 fdt_strerror(err));
165                         return 1;
166                 }
167                 working_fdt = newaddr;
168
169         /********************************************************************
170          * Make a new node
171          ********************************************************************/
172         } else if (strncmp(argv[1], "mk", 2) == 0) {
173                 char *pathp;            /* path */
174                 char *nodep;            /* new node to add */
175                 int  nodeoffset;        /* node offset from libfdt */
176                 int  err;
177
178                 /*
179                  * Parameters: Node path, new node to be appended to the path.
180                  */
181                 if (argc < 4) {
182                         cmd_usage(cmdtp);
183                         return 1;
184                 }
185
186                 pathp = argv[2];
187                 nodep = argv[3];
188
189                 nodeoffset = fdt_path_offset (working_fdt, pathp);
190                 if (nodeoffset < 0) {
191                         /*
192                          * Not found or something else bad happened.
193                          */
194                         printf ("libfdt fdt_path_offset() returned %s\n",
195                                 fdt_strerror(nodeoffset));
196                         return 1;
197                 }
198                 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
199                 if (err < 0) {
200                         printf ("libfdt fdt_add_subnode(): %s\n",
201                                 fdt_strerror(err));
202                         return 1;
203                 }
204
205         /********************************************************************
206          * Set the value of a property in the working_fdt.
207          ********************************************************************/
208         } else if (argv[1][0] == 's') {
209                 char *pathp;            /* path */
210                 char *prop;             /* property */
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 */
215
216                 /*
217                  * Parameters: Node path, property, optional value.
218                  */
219                 if (argc < 4) {
220                         cmd_usage(cmdtp);
221                         return 1;
222                 }
223
224                 pathp  = argv[2];
225                 prop   = argv[3];
226                 if (argc == 4) {
227                         len = 0;
228                 } else {
229                         ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
230                         if (ret != 0)
231                                 return ret;
232                 }
233
234                 nodeoffset = fdt_path_offset (working_fdt, pathp);
235                 if (nodeoffset < 0) {
236                         /*
237                          * Not found or something else bad happened.
238                          */
239                         printf ("libfdt fdt_path_offset() returned %s\n",
240                                 fdt_strerror(nodeoffset));
241                         return 1;
242                 }
243
244                 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
245                 if (ret < 0) {
246                         printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
247                         return 1;
248                 }
249
250         /********************************************************************
251          * Print (recursive) / List (single level)
252          ********************************************************************/
253         } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
254                 int depth = MAX_LEVEL;  /* how deep to print */
255                 char *pathp;            /* path */
256                 char *prop;             /* property */
257                 int  ret;               /* return value */
258                 static char root[2] = "/";
259
260                 /*
261                  * list is an alias for print, but limited to 1 level
262                  */
263                 if (argv[1][0] == 'l') {
264                         depth = 1;
265                 }
266
267                 /*
268                  * Get the starting path.  The root node is an oddball,
269                  * the offset is zero and has no name.
270                  */
271                 if (argc == 2)
272                         pathp = root;
273                 else
274                         pathp = argv[2];
275                 if (argc > 3)
276                         prop = argv[3];
277                 else
278                         prop = NULL;
279
280                 ret = fdt_print(pathp, prop, depth);
281                 if (ret != 0)
282                         return ret;
283
284         /********************************************************************
285          * Remove a property/node
286          ********************************************************************/
287         } else if (strncmp(argv[1], "rm", 2) == 0) {
288                 int  nodeoffset;        /* node offset from libfdt */
289                 int  err;
290
291                 /*
292                  * Get the path.  The root node is an oddball, the offset
293                  * is zero and has no name.
294                  */
295                 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
296                 if (nodeoffset < 0) {
297                         /*
298                          * Not found or something else bad happened.
299                          */
300                         printf ("libfdt fdt_path_offset() returned %s\n",
301                                 fdt_strerror(nodeoffset));
302                         return 1;
303                 }
304                 /*
305                  * Do the delete.  A fourth parameter means delete a property,
306                  * otherwise delete the node.
307                  */
308                 if (argc > 3) {
309                         err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
310                         if (err < 0) {
311                                 printf("libfdt fdt_delprop():  %s\n",
312                                         fdt_strerror(err));
313                                 return err;
314                         }
315                 } else {
316                         err = fdt_del_node(working_fdt, nodeoffset);
317                         if (err < 0) {
318                                 printf("libfdt fdt_del_node():  %s\n",
319                                         fdt_strerror(err));
320                                 return err;
321                         }
322                 }
323
324         /********************************************************************
325          * Display header info
326          ********************************************************************/
327         } else if (argv[1][0] == 'h') {
328                 u32 version = fdt_version(working_fdt);
329                 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
330                 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
331                        fdt_totalsize(working_fdt));
332                 printf("off_dt_struct:\t\t0x%x\n",
333                        fdt_off_dt_struct(working_fdt));
334                 printf("off_dt_strings:\t\t0x%x\n",
335                        fdt_off_dt_strings(working_fdt));
336                 printf("off_mem_rsvmap:\t\t0x%x\n",
337                        fdt_off_mem_rsvmap(working_fdt));
338                 printf("version:\t\t%d\n", version);
339                 printf("last_comp_version:\t%d\n",
340                        fdt_last_comp_version(working_fdt));
341                 if (version >= 2)
342                         printf("boot_cpuid_phys:\t0x%x\n",
343                                 fdt_boot_cpuid_phys(working_fdt));
344                 if (version >= 3)
345                         printf("size_dt_strings:\t0x%x\n",
346                                 fdt_size_dt_strings(working_fdt));
347                 if (version >= 17)
348                         printf("size_dt_struct:\t\t0x%x\n",
349                                 fdt_size_dt_struct(working_fdt));
350                 printf("number mem_rsv:\t\t0x%x\n",
351                        fdt_num_mem_rsv(working_fdt));
352                 printf("\n");
353
354         /********************************************************************
355          * Set boot cpu id
356          ********************************************************************/
357         } else if (strncmp(argv[1], "boo", 3) == 0) {
358                 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
359                 fdt_set_boot_cpuid_phys(working_fdt, tmp);
360
361         /********************************************************************
362          * memory command
363          ********************************************************************/
364         } else if (strncmp(argv[1], "me", 2) == 0) {
365                 uint64_t addr, size;
366                 int err;
367 #ifdef CONFIG_SYS_64BIT_STRTOUL
368                         addr = simple_strtoull(argv[2], NULL, 16);
369                         size = simple_strtoull(argv[3], NULL, 16);
370 #else
371                         addr = simple_strtoul(argv[2], NULL, 16);
372                         size = simple_strtoul(argv[3], NULL, 16);
373 #endif
374                 err = fdt_fixup_memory(working_fdt, addr, size);
375                 if (err < 0)
376                         return err;
377
378         /********************************************************************
379          * mem reserve commands
380          ********************************************************************/
381         } else if (strncmp(argv[1], "rs", 2) == 0) {
382                 if (argv[2][0] == 'p') {
383                         uint64_t addr, size;
384                         int total = fdt_num_mem_rsv(working_fdt);
385                         int j, err;
386                         printf("index\t\t   start\t\t    size\n");
387                         printf("-------------------------------"
388                                 "-----------------\n");
389                         for (j = 0; j < total; j++) {
390                                 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
391                                 if (err < 0) {
392                                         printf("libfdt fdt_get_mem_rsv():  %s\n",
393                                                         fdt_strerror(err));
394                                         return err;
395                                 }
396                                 printf("    %x\t%08x%08x\t%08x%08x\n", j,
397                                         (u32)(addr >> 32),
398                                         (u32)(addr & 0xffffffff),
399                                         (u32)(size >> 32),
400                                         (u32)(size & 0xffffffff));
401                         }
402                 } else if (argv[2][0] == 'a') {
403                         uint64_t addr, size;
404                         int err;
405 #ifdef CONFIG_SYS_64BIT_STRTOUL
406                         addr = simple_strtoull(argv[3], NULL, 16);
407                         size = simple_strtoull(argv[4], NULL, 16);
408 #else
409                         addr = simple_strtoul(argv[3], NULL, 16);
410                         size = simple_strtoul(argv[4], NULL, 16);
411 #endif
412                         err = fdt_add_mem_rsv(working_fdt, addr, size);
413
414                         if (err < 0) {
415                                 printf("libfdt fdt_add_mem_rsv():  %s\n",
416                                         fdt_strerror(err));
417                                 return err;
418                         }
419                 } else if (argv[2][0] == 'd') {
420                         unsigned long idx = simple_strtoul(argv[3], NULL, 16);
421                         int err = fdt_del_mem_rsv(working_fdt, idx);
422
423                         if (err < 0) {
424                                 printf("libfdt fdt_del_mem_rsv():  %s\n",
425                                         fdt_strerror(err));
426                                 return err;
427                         }
428                 } else {
429                         /* Unrecognized command */
430                         cmd_usage(cmdtp);
431                         return 1;
432                 }
433         }
434 #ifdef CONFIG_OF_BOARD_SETUP
435         /* Call the board-specific fixup routine */
436         else if (strncmp(argv[1], "boa", 3) == 0)
437                 ft_board_setup(working_fdt, gd->bd);
438 #endif
439         /* Create a chosen node */
440         else if (argv[1][0] == 'c') {
441                 unsigned long initrd_start = 0, initrd_end = 0;
442
443                 if ((argc != 2) && (argc != 4)) {
444                         cmd_usage(cmdtp);
445                         return 1;
446                 }
447
448                 if (argc == 4) {
449                         initrd_start = simple_strtoul(argv[2], NULL, 16);
450                         initrd_end = simple_strtoul(argv[3], NULL, 16);
451                 }
452
453                 fdt_chosen(working_fdt, 1);
454                 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
455         }
456         /* resize the fdt */
457         else if (strncmp(argv[1], "re", 2) == 0) {
458                 fdt_resize(working_fdt);
459         }
460         else {
461                 /* Unrecognized command */
462                 cmd_usage(cmdtp);
463                 return 1;
464         }
465
466         return 0;
467 }
468
469 /****************************************************************************/
470
471 static int fdt_valid(void)
472 {
473         int  err;
474
475         if (working_fdt == NULL) {
476                 printf ("The address of the fdt is invalid (NULL).\n");
477                 return 0;
478         }
479
480         err = fdt_check_header(working_fdt);
481         if (err == 0)
482                 return 1;       /* valid */
483
484         if (err < 0) {
485                 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
486                 /*
487                  * Be more informative on bad version.
488                  */
489                 if (err == -FDT_ERR_BADVERSION) {
490                         if (fdt_version(working_fdt) <
491                             FDT_FIRST_SUPPORTED_VERSION) {
492                                 printf (" - too old, fdt %d < %d",
493                                         fdt_version(working_fdt),
494                                         FDT_FIRST_SUPPORTED_VERSION);
495                                 working_fdt = NULL;
496                         }
497                         if (fdt_last_comp_version(working_fdt) >
498                             FDT_LAST_SUPPORTED_VERSION) {
499                                 printf (" - too new, fdt %d > %d",
500                                         fdt_version(working_fdt),
501                                         FDT_LAST_SUPPORTED_VERSION);
502                                 working_fdt = NULL;
503                         }
504                         return 0;
505                 }
506                 printf("\n");
507                 return 0;
508         }
509         return 1;
510 }
511
512 /****************************************************************************/
513
514 /*
515  * Parse the user's input, partially heuristic.  Valid formats:
516  * <0x00112233 4 05>    - an array of cells.  Numbers follow standard
517  *                      C conventions.
518  * [00 11 22 .. nn] - byte stream
519  * "string"     - If the the value doesn't start with "<" or "[", it is
520  *                      treated as a string.  Note that the quotes are
521  *                      stripped by the parser before we get the string.
522  * newval: An array of strings containing the new property as specified
523  *      on the command line
524  * count: The number of strings in the array
525  * data: A bytestream to be placed in the property
526  * len: The length of the resulting bytestream
527  */
528 static int fdt_parse_prop(char **newval, int count, char *data, int *len)
529 {
530         char *cp;               /* temporary char pointer */
531         char *newp;             /* temporary newval char pointer */
532         unsigned long tmp;      /* holds converted values */
533         int stridx = 0;
534
535         *len = 0;
536         newp = newval[0];
537
538         /* An array of cells */
539         if (*newp == '<') {
540                 newp++;
541                 while ((*newp != '>') && (stridx < count)) {
542                         /*
543                          * Keep searching until we find that last ">"
544                          * That way users don't have to escape the spaces
545                          */
546                         if (*newp == '\0') {
547                                 newp = newval[++stridx];
548                                 continue;
549                         }
550
551                         cp = newp;
552                         tmp = simple_strtoul(cp, &newp, 0);
553                         *(uint32_t *)data = __cpu_to_be32(tmp);
554                         data  += 4;
555                         *len += 4;
556
557                         /* If the ptr didn't advance, something went wrong */
558                         if ((newp - cp) <= 0) {
559                                 printf("Sorry, I could not convert \"%s\"\n",
560                                         cp);
561                                 return 1;
562                         }
563
564                         while (*newp == ' ')
565                                 newp++;
566                 }
567
568                 if (*newp != '>') {
569                         printf("Unexpected character '%c'\n", *newp);
570                         return 1;
571                 }
572         } else if (*newp == '[') {
573                 /*
574                  * Byte stream.  Convert the values.
575                  */
576                 newp++;
577                 while ((stridx < count) && (*newp != ']')) {
578                         while (*newp == ' ')
579                                 newp++;
580                         if (*newp == '\0') {
581                                 newp = newval[++stridx];
582                                 continue;
583                         }
584                         if (!isxdigit(*newp))
585                                 break;
586                         tmp = simple_strtoul(newp, &newp, 16);
587                         *data++ = tmp & 0xFF;
588                         *len    = *len + 1;
589                 }
590                 if (*newp != ']') {
591                         printf("Unexpected character '%c'\n", *newp);
592                         return 1;
593                 }
594         } else {
595                 /*
596                  * Assume it is one or more strings.  Copy it into our
597                  * data area for convenience (including the
598                  * terminating '\0's).
599                  */
600                 while (stridx < count) {
601                         size_t length = strlen(newp) + 1;
602                         strcpy(data, newp);
603                         data += length;
604                         *len += length;
605                         newp = newval[++stridx];
606                 }
607         }
608         return 0;
609 }
610
611 /****************************************************************************/
612
613 /*
614  * Heuristic to guess if this is a string or concatenated strings.
615  */
616
617 static int is_printable_string(const void *data, int len)
618 {
619         const char *s = data;
620
621         /* zero length is not */
622         if (len == 0)
623                 return 0;
624
625         /* must terminate with zero */
626         if (s[len - 1] != '\0')
627                 return 0;
628
629         /* printable or a null byte (concatenated strings) */
630         while (((*s == '\0') || isprint(*s)) && (len > 0)) {
631                 /*
632                  * If we see a null, there are three possibilities:
633                  * 1) If len == 1, it is the end of the string, printable
634                  * 2) Next character also a null, not printable.
635                  * 3) Next character not a null, continue to check.
636                  */
637                 if (s[0] == '\0') {
638                         if (len == 1)
639                                 return 1;
640                         if (s[1] == '\0')
641                                 return 0;
642                 }
643                 s++;
644                 len--;
645         }
646
647         /* Not the null termination, or not done yet: not printable */
648         if (*s != '\0' || (len != 0))
649                 return 0;
650
651         return 1;
652 }
653
654
655 /*
656  * Print the property in the best format, a heuristic guess.  Print as
657  * a string, concatenated strings, a byte, word, double word, or (if all
658  * else fails) it is printed as a stream of bytes.
659  */
660 static void print_data(const void *data, int len)
661 {
662         int j;
663
664         /* no data, don't print */
665         if (len == 0)
666                 return;
667
668         /*
669          * It is a string, but it may have multiple strings (embedded '\0's).
670          */
671         if (is_printable_string(data, len)) {
672                 puts("\"");
673                 j = 0;
674                 while (j < len) {
675                         if (j > 0)
676                                 puts("\", \"");
677                         puts(data);
678                         j    += strlen(data) + 1;
679                         data += strlen(data) + 1;
680                 }
681                 puts("\"");
682                 return;
683         }
684
685         if ((len %4) == 0) {
686                 const u32 *p;
687
688                 printf("<");
689                 for (j = 0, p = data; j < len/4; j ++)
690                         printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
691                 printf(">");
692         } else { /* anything else... hexdump */
693                 const u8 *s;
694
695                 printf("[");
696                 for (j = 0, s = data; j < len; j++)
697                         printf("%02x%s", s[j], j < len - 1 ? " " : "");
698                 printf("]");
699         }
700 }
701
702 /****************************************************************************/
703
704 /*
705  * Recursively print (a portion of) the working_fdt.  The depth parameter
706  * determines how deeply nested the fdt is printed.
707  */
708 static int fdt_print(const char *pathp, char *prop, int depth)
709 {
710         static char tabs[MAX_LEVEL+1] =
711                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
712                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
713         const void *nodep;      /* property node pointer */
714         int  nodeoffset;        /* node offset from libfdt */
715         int  nextoffset;        /* next node offset from libfdt */
716         uint32_t tag;           /* tag */
717         int  len;               /* length of the property */
718         int  level = 0;         /* keep track of nesting level */
719         const struct fdt_property *fdt_prop;
720
721         nodeoffset = fdt_path_offset (working_fdt, pathp);
722         if (nodeoffset < 0) {
723                 /*
724                  * Not found or something else bad happened.
725                  */
726                 printf ("libfdt fdt_path_offset() returned %s\n",
727                         fdt_strerror(nodeoffset));
728                 return 1;
729         }
730         /*
731          * The user passed in a property as well as node path.
732          * Print only the given property and then return.
733          */
734         if (prop) {
735                 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
736                 if (len == 0) {
737                         /* no property value */
738                         printf("%s %s\n", pathp, prop);
739                         return 0;
740                 } else if (len > 0) {
741                         printf("%s = ", prop);
742                         print_data (nodep, len);
743                         printf("\n");
744                         return 0;
745                 } else {
746                         printf ("libfdt fdt_getprop(): %s\n",
747                                 fdt_strerror(len));
748                         return 1;
749                 }
750         }
751
752         /*
753          * The user passed in a node path and no property,
754          * print the node and all subnodes.
755          */
756         while(level >= 0) {
757                 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
758                 switch(tag) {
759                 case FDT_BEGIN_NODE:
760                         pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
761                         if (level <= depth) {
762                                 if (pathp == NULL)
763                                         pathp = "/* NULL pointer error */";
764                                 if (*pathp == '\0')
765                                         pathp = "/";    /* root is nameless */
766                                 printf("%s%s {\n",
767                                         &tabs[MAX_LEVEL - level], pathp);
768                         }
769                         level++;
770                         if (level >= MAX_LEVEL) {
771                                 printf("Nested too deep, aborting.\n");
772                                 return 1;
773                         }
774                         break;
775                 case FDT_END_NODE:
776                         level--;
777                         if (level <= depth)
778                                 printf("%s};\n", &tabs[MAX_LEVEL - level]);
779                         if (level == 0) {
780                                 level = -1;             /* exit the loop */
781                         }
782                         break;
783                 case FDT_PROP:
784                         fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
785                                         sizeof(*fdt_prop));
786                         pathp    = fdt_string(working_fdt,
787                                         fdt32_to_cpu(fdt_prop->nameoff));
788                         len      = fdt32_to_cpu(fdt_prop->len);
789                         nodep    = fdt_prop->data;
790                         if (len < 0) {
791                                 printf ("libfdt fdt_getprop(): %s\n",
792                                         fdt_strerror(len));
793                                 return 1;
794                         } else if (len == 0) {
795                                 /* the property has no value */
796                                 if (level <= depth)
797                                         printf("%s%s;\n",
798                                                 &tabs[MAX_LEVEL - level],
799                                                 pathp);
800                         } else {
801                                 if (level <= depth) {
802                                         printf("%s%s = ",
803                                                 &tabs[MAX_LEVEL - level],
804                                                 pathp);
805                                         print_data (nodep, len);
806                                         printf(";\n");
807                                 }
808                         }
809                         break;
810                 case FDT_NOP:
811                         printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
812                         break;
813                 case FDT_END:
814                         return 1;
815                 default:
816                         if (level <= depth)
817                                 printf("Unknown tag 0x%08X\n", tag);
818                         return 1;
819                 }
820                 nodeoffset = nextoffset;
821         }
822         return 0;
823 }
824
825 /********************************************************************/
826
827 U_BOOT_CMD(
828         fdt,    255,    0,      do_fdt,
829         "flattened device tree utility commands",
830             "addr   <addr> [<length>]        - Set the fdt location to <addr>\n"
831 #ifdef CONFIG_OF_BOARD_SETUP
832         "fdt boardsetup                      - Do board-specific set up\n"
833 #endif
834         "fdt move   <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
835         "fdt resize                          - Resize fdt to size + padding to 4k addr\n"
836         "fdt print  <path> [<prop>]          - Recursive print starting at <path>\n"
837         "fdt list   <path> [<prop>]          - Print one level starting at <path>\n"
838         "fdt set    <path> <prop> [<val>]    - Set <property> [to <val>]\n"
839         "fdt mknode <path> <node>            - Create a new node after <path>\n"
840         "fdt rm     <path> [<prop>]          - Delete the node or <property>\n"
841         "fdt header                          - Display header info\n"
842         "fdt bootcpu <id>                    - Set boot cpuid\n"
843         "fdt memory <addr> <size>            - Add/Update memory node\n"
844         "fdt rsvmem print                    - Show current mem reserves\n"
845         "fdt rsvmem add <addr> <size>        - Add a mem reserve\n"
846         "fdt rsvmem delete <index>           - Delete a mem reserves\n"
847         "fdt chosen [<start> <end>]          - Add/update the /chosen branch in the tree\n"
848         "                                        <start>/<end> - initrd start/end addr\n"
849         "NOTE: Dereference aliases by omiting the leading '/', "
850                 "e.g. fdt print ethernet0."
851 );