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