Merge branch 'master' of git://git.denx.de/u-boot-fdt
[glsdk/glsdk-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  */
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 */
38 #ifndef CONFIG_CMD_FDT_MAX_DUMP
39 #define CONFIG_CMD_FDT_MAX_DUMP 64
40 #endif
42 /*
43  * Global data (for the gd->bd)
44  */
45 DECLARE_GLOBAL_DATA_PTR;
47 static int fdt_valid(void);
48 static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
49 static int fdt_print(const char *pathp, char *prop, int depth);
50 static int is_printable_string(const void *data, int len);
52 /*
53  * The working_fdt points to our working flattened device tree.
54  */
55 struct fdt_header *working_fdt;
57 void set_working_fdt_addr(void *addr)
58 {
59         char buf[17];
61         working_fdt = addr;
63         sprintf(buf, "%lx", (unsigned long)addr);
64         setenv("fdtaddr", buf);
65 }
67 /*
68  * Get a value from the fdt and format it to be set in the environment
69  */
70 static int fdt_value_setenv(const void *nodep, int len, const char *var)
71 {
72         if (is_printable_string(nodep, len))
73                 setenv(var, (void *)nodep);
74         else if (len == 4) {
75                 char buf[11];
77                 sprintf(buf, "0x%08X", *(uint32_t *)nodep);
78                 setenv(var, buf);
79         } else if (len%4 == 0 && len <= 20) {
80                 /* Needed to print things like sha1 hashes. */
81                 char buf[41];
82                 int i;
84                 for (i = 0; i < len; i += sizeof(unsigned int))
85                         sprintf(buf + (i * 2), "%08x",
86                                 *(unsigned int *)(nodep + i));
87                 setenv(var, buf);
88         } else {
89                 printf("error: unprintable value\n");
90                 return 1;
91         }
92         return 0;
93 }
95 /*
96  * Flattened Device Tree command, see the help for parameter definitions.
97  */
98 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
99 {
100         if (argc < 2)
101                 return CMD_RET_USAGE;
103         /*
104          * Set the address of the fdt
105          */
106         if (argv[1][0] == 'a') {
107                 unsigned long addr;
108                 /*
109                  * Set the address [and length] of the fdt.
110                  */
111                 if (argc == 2) {
112                         if (!fdt_valid()) {
113                                 return 1;
114                         }
115                         printf("The address of the fdt is %p\n", working_fdt);
116                         return 0;
117                 }
119                 addr = simple_strtoul(argv[2], NULL, 16);
120                 set_working_fdt_addr((void *)addr);
122                 if (!fdt_valid()) {
123                         return 1;
124                 }
126                 if (argc >= 4) {
127                         int  len;
128                         int  err;
129                         /*
130                          * Optional new length
131                          */
132                         len = simple_strtoul(argv[3], NULL, 16);
133                         if (len < fdt_totalsize(working_fdt)) {
134                                 printf ("New length %d < existing length %d, "
135                                         "ignoring.\n",
136                                         len, fdt_totalsize(working_fdt));
137                         } else {
138                                 /*
139                                  * Open in place with a new length.
140                                  */
141                                 err = fdt_open_into(working_fdt, working_fdt, len);
142                                 if (err != 0) {
143                                         printf ("libfdt fdt_open_into(): %s\n",
144                                                 fdt_strerror(err));
145                                 }
146                         }
147                 }
149                 return CMD_RET_SUCCESS;
150         }
152         if (!working_fdt) {
153                 puts(
154                         "No FDT memory address configured. Please configure\n"
155                         "the FDT address via \"fdt addr <address>\" command.\n"
156                         "Aborting!\n");
157                 return CMD_RET_FAILURE;
158         }
160         /*
161          * Move the working_fdt
162          */
163         if (strncmp(argv[1], "mo", 2) == 0) {
164                 struct fdt_header *newaddr;
165                 int  len;
166                 int  err;
168                 if (argc < 4)
169                         return CMD_RET_USAGE;
171                 /*
172                  * Set the address and length of the fdt.
173                  */
174                 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
175                 if (!fdt_valid()) {
176                         return 1;
177                 }
179                 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
181                 /*
182                  * If the user specifies a length, use that.  Otherwise use the
183                  * current length.
184                  */
185                 if (argc <= 4) {
186                         len = fdt_totalsize(working_fdt);
187                 } else {
188                         len = simple_strtoul(argv[4], NULL, 16);
189                         if (len < fdt_totalsize(working_fdt)) {
190                                 printf ("New length 0x%X < existing length "
191                                         "0x%X, aborting.\n",
192                                         len, fdt_totalsize(working_fdt));
193                                 return 1;
194                         }
195                 }
197                 /*
198                  * Copy to the new location.
199                  */
200                 err = fdt_open_into(working_fdt, newaddr, len);
201                 if (err != 0) {
202                         printf ("libfdt fdt_open_into(): %s\n",
203                                 fdt_strerror(err));
204                         return 1;
205                 }
206                 working_fdt = newaddr;
208         /*
209          * Make a new node
210          */
211         } else if (strncmp(argv[1], "mk", 2) == 0) {
212                 char *pathp;            /* path */
213                 char *nodep;            /* new node to add */
214                 int  nodeoffset;        /* node offset from libfdt */
215                 int  err;
217                 /*
218                  * Parameters: Node path, new node to be appended to the path.
219                  */
220                 if (argc < 4)
221                         return CMD_RET_USAGE;
223                 pathp = argv[2];
224                 nodep = argv[3];
226                 nodeoffset = fdt_path_offset (working_fdt, pathp);
227                 if (nodeoffset < 0) {
228                         /*
229                          * Not found or something else bad happened.
230                          */
231                         printf ("libfdt fdt_path_offset() returned %s\n",
232                                 fdt_strerror(nodeoffset));
233                         return 1;
234                 }
235                 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
236                 if (err < 0) {
237                         printf ("libfdt fdt_add_subnode(): %s\n",
238                                 fdt_strerror(err));
239                         return 1;
240                 }
242         /*
243          * Set the value of a property in the working_fdt.
244          */
245         } else if (argv[1][0] == 's') {
246                 char *pathp;            /* path */
247                 char *prop;             /* property */
248                 int  nodeoffset;        /* node offset from libfdt */
249                 static char data[SCRATCHPAD];   /* storage for the property */
250                 int  len;               /* new length of the property */
251                 int  ret;               /* return value */
253                 /*
254                  * Parameters: Node path, property, optional value.
255                  */
256                 if (argc < 4)
257                         return CMD_RET_USAGE;
259                 pathp  = argv[2];
260                 prop   = argv[3];
261                 if (argc == 4) {
262                         len = 0;
263                 } else {
264                         ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
265                         if (ret != 0)
266                                 return ret;
267                 }
269                 nodeoffset = fdt_path_offset (working_fdt, pathp);
270                 if (nodeoffset < 0) {
271                         /*
272                          * Not found or something else bad happened.
273                          */
274                         printf ("libfdt fdt_path_offset() returned %s\n",
275                                 fdt_strerror(nodeoffset));
276                         return 1;
277                 }
279                 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
280                 if (ret < 0) {
281                         printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
282                         return 1;
283                 }
285         /********************************************************************
286          * Get the value of a property in the working_fdt.
287          ********************************************************************/
288         } else if (argv[1][0] == 'g') {
289                 char *subcmd;           /* sub-command */
290                 char *pathp;            /* path */
291                 char *prop;             /* property */
292                 char *var;              /* variable to store result */
293                 int  nodeoffset;        /* node offset from libfdt */
294                 const void *nodep;      /* property node pointer */
295                 int  len = 0;           /* new length of the property */
297                 /*
298                  * Parameters: Node path, property, optional value.
299                  */
300                 if (argc < 5)
301                         return CMD_RET_USAGE;
303                 subcmd = argv[2];
305                 if (argc < 6 && subcmd[0] != 's')
306                         return CMD_RET_USAGE;
308                 var    = argv[3];
309                 pathp  = argv[4];
310                 prop   = argv[5];
312                 nodeoffset = fdt_path_offset(working_fdt, pathp);
313                 if (nodeoffset < 0) {
314                         /*
315                          * Not found or something else bad happened.
316                          */
317                         printf("libfdt fdt_path_offset() returned %s\n",
318                                 fdt_strerror(nodeoffset));
319                         return 1;
320                 }
322                 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
323                         int reqIndex = -1;
324                         int startDepth = fdt_node_depth(
325                                 working_fdt, nodeoffset);
326                         int curDepth = startDepth;
327                         int curIndex = -1;
328                         int nextNodeOffset = fdt_next_node(
329                                 working_fdt, nodeoffset, &curDepth);
331                         if (subcmd[0] == 'n')
332                                 reqIndex = simple_strtoul(argv[5], NULL, 16);
334                         while (curDepth > startDepth) {
335                                 if (curDepth == startDepth + 1)
336                                         curIndex++;
337                                 if (subcmd[0] == 'n' && curIndex == reqIndex) {
338                                         const char *nodeName = fdt_get_name(
339                                             working_fdt, nextNodeOffset, NULL);
341                                         setenv(var, (char *)nodeName);
342                                         return 0;
343                                 }
344                                 nextNodeOffset = fdt_next_node(
345                                         working_fdt, nextNodeOffset, &curDepth);
346                                 if (nextNodeOffset < 0)
347                                         break;
348                         }
349                         if (subcmd[0] == 's') {
350                                 /* get the num nodes at this level */
351                                 char buf[11];
353                                 sprintf(buf, "%d", curIndex + 1);
354                                 setenv(var, buf);
355                         } else {
356                                 /* node index not found */
357                                 printf("libfdt node not found\n");
358                                 return 1;
359                         }
360                 } else {
361                         nodep = fdt_getprop(
362                                 working_fdt, nodeoffset, prop, &len);
363                         if (len == 0) {
364                                 /* no property value */
365                                 setenv(var, "");
366                                 return 0;
367                         } else if (len > 0) {
368                                 if (subcmd[0] == 'v') {
369                                         int ret;
371                                         ret = fdt_value_setenv(nodep, len, var);
372                                         if (ret != 0)
373                                                 return ret;
374                                 } else if (subcmd[0] == 'a') {
375                                         /* Get address */
376                                         char buf[11];
378                                         sprintf(buf, "0x%08X", (uint32_t)nodep);
379                                         setenv(var, buf);
380                                 } else if (subcmd[0] == 's') {
381                                         /* Get size */
382                                         char buf[11];
384                                         sprintf(buf, "0x%08X", len);
385                                         setenv(var, buf);
386                                 } else
387                                         return CMD_RET_USAGE;
388                                 return 0;
389                         } else {
390                                 printf("libfdt fdt_getprop(): %s\n",
391                                         fdt_strerror(len));
392                                 return 1;
393                         }
394                 }
396         /*
397          * Print (recursive) / List (single level)
398          */
399         } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
400                 int depth = MAX_LEVEL;  /* how deep to print */
401                 char *pathp;            /* path */
402                 char *prop;             /* property */
403                 int  ret;               /* return value */
404                 static char root[2] = "/";
406                 /*
407                  * list is an alias for print, but limited to 1 level
408                  */
409                 if (argv[1][0] == 'l') {
410                         depth = 1;
411                 }
413                 /*
414                  * Get the starting path.  The root node is an oddball,
415                  * the offset is zero and has no name.
416                  */
417                 if (argc == 2)
418                         pathp = root;
419                 else
420                         pathp = argv[2];
421                 if (argc > 3)
422                         prop = argv[3];
423                 else
424                         prop = NULL;
426                 ret = fdt_print(pathp, prop, depth);
427                 if (ret != 0)
428                         return ret;
430         /*
431          * Remove a property/node
432          */
433         } else if (strncmp(argv[1], "rm", 2) == 0) {
434                 int  nodeoffset;        /* node offset from libfdt */
435                 int  err;
437                 /*
438                  * Get the path.  The root node is an oddball, the offset
439                  * is zero and has no name.
440                  */
441                 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
442                 if (nodeoffset < 0) {
443                         /*
444                          * Not found or something else bad happened.
445                          */
446                         printf ("libfdt fdt_path_offset() returned %s\n",
447                                 fdt_strerror(nodeoffset));
448                         return 1;
449                 }
450                 /*
451                  * Do the delete.  A fourth parameter means delete a property,
452                  * otherwise delete the node.
453                  */
454                 if (argc > 3) {
455                         err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
456                         if (err < 0) {
457                                 printf("libfdt fdt_delprop():  %s\n",
458                                         fdt_strerror(err));
459                                 return err;
460                         }
461                 } else {
462                         err = fdt_del_node(working_fdt, nodeoffset);
463                         if (err < 0) {
464                                 printf("libfdt fdt_del_node():  %s\n",
465                                         fdt_strerror(err));
466                                 return err;
467                         }
468                 }
470         /*
471          * Display header info
472          */
473         } else if (argv[1][0] == 'h') {
474                 u32 version = fdt_version(working_fdt);
475                 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
476                 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
477                        fdt_totalsize(working_fdt));
478                 printf("off_dt_struct:\t\t0x%x\n",
479                        fdt_off_dt_struct(working_fdt));
480                 printf("off_dt_strings:\t\t0x%x\n",
481                        fdt_off_dt_strings(working_fdt));
482                 printf("off_mem_rsvmap:\t\t0x%x\n",
483                        fdt_off_mem_rsvmap(working_fdt));
484                 printf("version:\t\t%d\n", version);
485                 printf("last_comp_version:\t%d\n",
486                        fdt_last_comp_version(working_fdt));
487                 if (version >= 2)
488                         printf("boot_cpuid_phys:\t0x%x\n",
489                                 fdt_boot_cpuid_phys(working_fdt));
490                 if (version >= 3)
491                         printf("size_dt_strings:\t0x%x\n",
492                                 fdt_size_dt_strings(working_fdt));
493                 if (version >= 17)
494                         printf("size_dt_struct:\t\t0x%x\n",
495                                 fdt_size_dt_struct(working_fdt));
496                 printf("number mem_rsv:\t\t0x%x\n",
497                        fdt_num_mem_rsv(working_fdt));
498                 printf("\n");
500         /*
501          * Set boot cpu id
502          */
503         } else if (strncmp(argv[1], "boo", 3) == 0) {
504                 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
505                 fdt_set_boot_cpuid_phys(working_fdt, tmp);
507         /*
508          * memory command
509          */
510         } else if (strncmp(argv[1], "me", 2) == 0) {
511                 uint64_t addr, size;
512                 int err;
513                 addr = simple_strtoull(argv[2], NULL, 16);
514                 size = simple_strtoull(argv[3], NULL, 16);
515                 err = fdt_fixup_memory(working_fdt, addr, size);
516                 if (err < 0)
517                         return err;
519         /*
520          * mem reserve commands
521          */
522         } else if (strncmp(argv[1], "rs", 2) == 0) {
523                 if (argv[2][0] == 'p') {
524                         uint64_t addr, size;
525                         int total = fdt_num_mem_rsv(working_fdt);
526                         int j, err;
527                         printf("index\t\t   start\t\t    size\n");
528                         printf("-------------------------------"
529                                 "-----------------\n");
530                         for (j = 0; j < total; j++) {
531                                 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
532                                 if (err < 0) {
533                                         printf("libfdt fdt_get_mem_rsv():  %s\n",
534                                                         fdt_strerror(err));
535                                         return err;
536                                 }
537                                 printf("    %x\t%08x%08x\t%08x%08x\n", j,
538                                         (u32)(addr >> 32),
539                                         (u32)(addr & 0xffffffff),
540                                         (u32)(size >> 32),
541                                         (u32)(size & 0xffffffff));
542                         }
543                 } else if (argv[2][0] == 'a') {
544                         uint64_t addr, size;
545                         int err;
546                         addr = simple_strtoull(argv[3], NULL, 16);
547                         size = simple_strtoull(argv[4], NULL, 16);
548                         err = fdt_add_mem_rsv(working_fdt, addr, size);
550                         if (err < 0) {
551                                 printf("libfdt fdt_add_mem_rsv():  %s\n",
552                                         fdt_strerror(err));
553                                 return err;
554                         }
555                 } else if (argv[2][0] == 'd') {
556                         unsigned long idx = simple_strtoul(argv[3], NULL, 16);
557                         int err = fdt_del_mem_rsv(working_fdt, idx);
559                         if (err < 0) {
560                                 printf("libfdt fdt_del_mem_rsv():  %s\n",
561                                         fdt_strerror(err));
562                                 return err;
563                         }
564                 } else {
565                         /* Unrecognized command */
566                         return CMD_RET_USAGE;
567                 }
568         }
569 #ifdef CONFIG_OF_BOARD_SETUP
570         /* Call the board-specific fixup routine */
571         else if (strncmp(argv[1], "boa", 3) == 0)
572                 ft_board_setup(working_fdt, gd->bd);
573 #endif
574         /* Create a chosen node */
575         else if (argv[1][0] == 'c') {
576                 unsigned long initrd_start = 0, initrd_end = 0;
578                 if ((argc != 2) && (argc != 4))
579                         return CMD_RET_USAGE;
581                 if (argc == 4) {
582                         initrd_start = simple_strtoul(argv[2], NULL, 16);
583                         initrd_end = simple_strtoul(argv[3], NULL, 16);
584                 }
586                 fdt_chosen(working_fdt, 1);
587                 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
588         }
589         /* resize the fdt */
590         else if (strncmp(argv[1], "re", 2) == 0) {
591                 fdt_resize(working_fdt);
592         }
593         else {
594                 /* Unrecognized command */
595                 return CMD_RET_USAGE;
596         }
598         return 0;
601 /****************************************************************************/
603 static int fdt_valid(void)
605         int  err;
607         if (working_fdt == NULL) {
608                 printf ("The address of the fdt is invalid (NULL).\n");
609                 return 0;
610         }
612         err = fdt_check_header(working_fdt);
613         if (err == 0)
614                 return 1;       /* valid */
616         if (err < 0) {
617                 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
618                 /*
619                  * Be more informative on bad version.
620                  */
621                 if (err == -FDT_ERR_BADVERSION) {
622                         if (fdt_version(working_fdt) <
623                             FDT_FIRST_SUPPORTED_VERSION) {
624                                 printf (" - too old, fdt %d < %d",
625                                         fdt_version(working_fdt),
626                                         FDT_FIRST_SUPPORTED_VERSION);
627                                 working_fdt = NULL;
628                         }
629                         if (fdt_last_comp_version(working_fdt) >
630                             FDT_LAST_SUPPORTED_VERSION) {
631                                 printf (" - too new, fdt %d > %d",
632                                         fdt_version(working_fdt),
633                                         FDT_LAST_SUPPORTED_VERSION);
634                                 working_fdt = NULL;
635                         }
636                         return 0;
637                 }
638                 printf("\n");
639                 return 0;
640         }
641         return 1;
644 /****************************************************************************/
646 /*
647  * Parse the user's input, partially heuristic.  Valid formats:
648  * <0x00112233 4 05>    - an array of cells.  Numbers follow standard
649  *                      C conventions.
650  * [00 11 22 .. nn] - byte stream
651  * "string"     - If the the value doesn't start with "<" or "[", it is
652  *                      treated as a string.  Note that the quotes are
653  *                      stripped by the parser before we get the string.
654  * newval: An array of strings containing the new property as specified
655  *      on the command line
656  * count: The number of strings in the array
657  * data: A bytestream to be placed in the property
658  * len: The length of the resulting bytestream
659  */
660 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
662         char *cp;               /* temporary char pointer */
663         char *newp;             /* temporary newval char pointer */
664         unsigned long tmp;      /* holds converted values */
665         int stridx = 0;
667         *len = 0;
668         newp = newval[0];
670         /* An array of cells */
671         if (*newp == '<') {
672                 newp++;
673                 while ((*newp != '>') && (stridx < count)) {
674                         /*
675                          * Keep searching until we find that last ">"
676                          * That way users don't have to escape the spaces
677                          */
678                         if (*newp == '\0') {
679                                 newp = newval[++stridx];
680                                 continue;
681                         }
683                         cp = newp;
684                         tmp = simple_strtoul(cp, &newp, 0);
685                         *(uint32_t *)data = __cpu_to_be32(tmp);
686                         data  += 4;
687                         *len += 4;
689                         /* If the ptr didn't advance, something went wrong */
690                         if ((newp - cp) <= 0) {
691                                 printf("Sorry, I could not convert \"%s\"\n",
692                                         cp);
693                                 return 1;
694                         }
696                         while (*newp == ' ')
697                                 newp++;
698                 }
700                 if (*newp != '>') {
701                         printf("Unexpected character '%c'\n", *newp);
702                         return 1;
703                 }
704         } else if (*newp == '[') {
705                 /*
706                  * Byte stream.  Convert the values.
707                  */
708                 newp++;
709                 while ((stridx < count) && (*newp != ']')) {
710                         while (*newp == ' ')
711                                 newp++;
712                         if (*newp == '\0') {
713                                 newp = newval[++stridx];
714                                 continue;
715                         }
716                         if (!isxdigit(*newp))
717                                 break;
718                         tmp = simple_strtoul(newp, &newp, 16);
719                         *data++ = tmp & 0xFF;
720                         *len    = *len + 1;
721                 }
722                 if (*newp != ']') {
723                         printf("Unexpected character '%c'\n", *newp);
724                         return 1;
725                 }
726         } else {
727                 /*
728                  * Assume it is one or more strings.  Copy it into our
729                  * data area for convenience (including the
730                  * terminating '\0's).
731                  */
732                 while (stridx < count) {
733                         size_t length = strlen(newp) + 1;
734                         strcpy(data, newp);
735                         data += length;
736                         *len += length;
737                         newp = newval[++stridx];
738                 }
739         }
740         return 0;
743 /****************************************************************************/
745 /*
746  * Heuristic to guess if this is a string or concatenated strings.
747  */
749 static int is_printable_string(const void *data, int len)
751         const char *s = data;
753         /* zero length is not */
754         if (len == 0)
755                 return 0;
757         /* must terminate with zero or '\n' */
758         if (s[len - 1] != '\0' && s[len - 1] != '\n')
759                 return 0;
761         /* printable or a null byte (concatenated strings) */
762         while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
763                 /*
764                  * If we see a null, there are three possibilities:
765                  * 1) If len == 1, it is the end of the string, printable
766                  * 2) Next character also a null, not printable.
767                  * 3) Next character not a null, continue to check.
768                  */
769                 if (s[0] == '\0') {
770                         if (len == 1)
771                                 return 1;
772                         if (s[1] == '\0')
773                                 return 0;
774                 }
775                 s++;
776                 len--;
777         }
779         /* Not the null termination, or not done yet: not printable */
780         if (*s != '\0' || (len != 0))
781                 return 0;
783         return 1;
787 /*
788  * Print the property in the best format, a heuristic guess.  Print as
789  * a string, concatenated strings, a byte, word, double word, or (if all
790  * else fails) it is printed as a stream of bytes.
791  */
792 static void print_data(const void *data, int len)
794         int j;
796         /* no data, don't print */
797         if (len == 0)
798                 return;
800         /*
801          * It is a string, but it may have multiple strings (embedded '\0's).
802          */
803         if (is_printable_string(data, len)) {
804                 puts("\"");
805                 j = 0;
806                 while (j < len) {
807                         if (j > 0)
808                                 puts("\", \"");
809                         puts(data);
810                         j    += strlen(data) + 1;
811                         data += strlen(data) + 1;
812                 }
813                 puts("\"");
814                 return;
815         }
817         if ((len %4) == 0) {
818                 if (len > CONFIG_CMD_FDT_MAX_DUMP)
819                         printf("* 0x%08x [0x%08x]", (unsigned int)data, len);
820                 else {
821                         const u32 *p;
823                         printf("<");
824                         for (j = 0, p = data; j < len/4; j++)
825                                 printf("0x%08x%s", fdt32_to_cpu(p[j]),
826                                         j < (len/4 - 1) ? " " : "");
827                         printf(">");
828                 }
829         } else { /* anything else... hexdump */
830                 if (len > CONFIG_CMD_FDT_MAX_DUMP)
831                         printf("* 0x%08x [0x%08x]", (unsigned int)data, len);
832                 else {
833                         const u8 *s;
835                         printf("[");
836                         for (j = 0, s = data; j < len; j++)
837                                 printf("%02x%s", s[j], j < len - 1 ? " " : "");
838                         printf("]");
839                 }
840         }
843 /****************************************************************************/
845 /*
846  * Recursively print (a portion of) the working_fdt.  The depth parameter
847  * determines how deeply nested the fdt is printed.
848  */
849 static int fdt_print(const char *pathp, char *prop, int depth)
851         static char tabs[MAX_LEVEL+1] =
852                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
853                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
854         const void *nodep;      /* property node pointer */
855         int  nodeoffset;        /* node offset from libfdt */
856         int  nextoffset;        /* next node offset from libfdt */
857         uint32_t tag;           /* tag */
858         int  len;               /* length of the property */
859         int  level = 0;         /* keep track of nesting level */
860         const struct fdt_property *fdt_prop;
862         nodeoffset = fdt_path_offset (working_fdt, pathp);
863         if (nodeoffset < 0) {
864                 /*
865                  * Not found or something else bad happened.
866                  */
867                 printf ("libfdt fdt_path_offset() returned %s\n",
868                         fdt_strerror(nodeoffset));
869                 return 1;
870         }
871         /*
872          * The user passed in a property as well as node path.
873          * Print only the given property and then return.
874          */
875         if (prop) {
876                 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
877                 if (len == 0) {
878                         /* no property value */
879                         printf("%s %s\n", pathp, prop);
880                         return 0;
881                 } else if (len > 0) {
882                         printf("%s = ", prop);
883                         print_data (nodep, len);
884                         printf("\n");
885                         return 0;
886                 } else {
887                         printf ("libfdt fdt_getprop(): %s\n",
888                                 fdt_strerror(len));
889                         return 1;
890                 }
891         }
893         /*
894          * The user passed in a node path and no property,
895          * print the node and all subnodes.
896          */
897         while(level >= 0) {
898                 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
899                 switch(tag) {
900                 case FDT_BEGIN_NODE:
901                         pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
902                         if (level <= depth) {
903                                 if (pathp == NULL)
904                                         pathp = "/* NULL pointer error */";
905                                 if (*pathp == '\0')
906                                         pathp = "/";    /* root is nameless */
907                                 printf("%s%s {\n",
908                                         &tabs[MAX_LEVEL - level], pathp);
909                         }
910                         level++;
911                         if (level >= MAX_LEVEL) {
912                                 printf("Nested too deep, aborting.\n");
913                                 return 1;
914                         }
915                         break;
916                 case FDT_END_NODE:
917                         level--;
918                         if (level <= depth)
919                                 printf("%s};\n", &tabs[MAX_LEVEL - level]);
920                         if (level == 0) {
921                                 level = -1;             /* exit the loop */
922                         }
923                         break;
924                 case FDT_PROP:
925                         fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
926                                         sizeof(*fdt_prop));
927                         pathp    = fdt_string(working_fdt,
928                                         fdt32_to_cpu(fdt_prop->nameoff));
929                         len      = fdt32_to_cpu(fdt_prop->len);
930                         nodep    = fdt_prop->data;
931                         if (len < 0) {
932                                 printf ("libfdt fdt_getprop(): %s\n",
933                                         fdt_strerror(len));
934                                 return 1;
935                         } else if (len == 0) {
936                                 /* the property has no value */
937                                 if (level <= depth)
938                                         printf("%s%s;\n",
939                                                 &tabs[MAX_LEVEL - level],
940                                                 pathp);
941                         } else {
942                                 if (level <= depth) {
943                                         printf("%s%s = ",
944                                                 &tabs[MAX_LEVEL - level],
945                                                 pathp);
946                                         print_data (nodep, len);
947                                         printf(";\n");
948                                 }
949                         }
950                         break;
951                 case FDT_NOP:
952                         printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
953                         break;
954                 case FDT_END:
955                         return 1;
956                 default:
957                         if (level <= depth)
958                                 printf("Unknown tag 0x%08X\n", tag);
959                         return 1;
960                 }
961                 nodeoffset = nextoffset;
962         }
963         return 0;
966 /********************************************************************/
968 U_BOOT_CMD(
969         fdt,    255,    0,      do_fdt,
970         "flattened device tree utility commands",
971             "addr   <addr> [<length>]        - Set the fdt location to <addr>\n"
972 #ifdef CONFIG_OF_BOARD_SETUP
973         "fdt boardsetup                      - Do board-specific set up\n"
974 #endif
975         "fdt move   <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
976         "fdt resize                          - Resize fdt to size + padding to 4k addr\n"
977         "fdt print  <path> [<prop>]          - Recursive print starting at <path>\n"
978         "fdt list   <path> [<prop>]          - Print one level starting at <path>\n"
979         "fdt get value <var> <path> <prop>   - Get <property> and store in <var>\n"
980         "fdt get name <var> <path> <index>   - Get name of node <index> and store in <var>\n"
981         "fdt get addr <var> <path> <prop>    - Get start address of <property> and store in <var>\n"
982         "fdt get size <var> <path> [<prop>]  - Get size of [<property>] or num nodes and store in <var>\n"
983         "fdt set    <path> <prop> [<val>]    - Set <property> [to <val>]\n"
984         "fdt mknode <path> <node>            - Create a new node after <path>\n"
985         "fdt rm     <path> [<prop>]          - Delete the node or <property>\n"
986         "fdt header                          - Display header info\n"
987         "fdt bootcpu <id>                    - Set boot cpuid\n"
988         "fdt memory <addr> <size>            - Add/Update memory node\n"
989         "fdt rsvmem print                    - Show current mem reserves\n"
990         "fdt rsvmem add <addr> <size>        - Add a mem reserve\n"
991         "fdt rsvmem delete <index>           - Delete a mem reserves\n"
992         "fdt chosen [<start> <end>]          - Add/update the /chosen branch in the tree\n"
993         "                                        <start>/<end> - initrd start/end addr\n"
994         "NOTE: Dereference aliases by omiting the leading '/', "
995                 "e.g. fdt print ethernet0."
996 );