[BUILD] conditionally compile common/cmd_*.c in common/Makefile
[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 *pathp, char *prop, char *newval,
46         char *data, int *len);
47 static int fdt_print(char *pathp, char *prop, int depth);
48
49 /*
50  * Flattened Device Tree command, see the help for parameter definitions.
51  */
52 int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
53 {
54         if (argc < 2) {
55                 printf ("Usage:\n%s\n", cmdtp->usage);
56                 return 1;
57         }
58
59         /********************************************************************
60          * Set the address of the fdt
61          ********************************************************************/
62         if (argv[1][0] == 'a') {
63                 /*
64                  * Set the address [and length] of the fdt.
65                  */
66                 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
67
68                 if (!fdt_valid()) {
69                         return 1;
70                 }
71
72                 if (argc >= 4) {
73                         int  len;
74                         int  err;
75                         /*
76                          * Optional new length
77                          */
78                         len =  simple_strtoul(argv[3], NULL, 16);
79                         if (len < fdt_totalsize(fdt)) {
80                                 printf ("New length %d < existing length %d, "
81                                         "ignoring.\n",
82                                         len, fdt_totalsize(fdt));
83                         } else {
84                                 /*
85                                  * Open in place with a new length.
86                                  */
87                                 err = fdt_open_into(fdt, fdt, len);
88                                 if (err != 0) {
89                                         printf ("libfdt fdt_open_into(): %s\n",
90                                                 fdt_strerror(err));
91                                 }
92                         }
93                 }
94
95         /********************************************************************
96          * Move the fdt
97          ********************************************************************/
98         } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
99                 struct fdt_header *newaddr;
100                 int  len;
101                 int  err;
102
103                 if (argc < 4) {
104                         printf ("Usage:\n%s\n", cmdtp->usage);
105                         return 1;
106                 }
107
108                 /*
109                  * Set the address and length of the fdt.
110                  */
111                 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
112                 if (!fdt_valid()) {
113                         return 1;
114                 }
115
116                 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
117
118                 /*
119                  * If the user specifies a length, use that.  Otherwise use the
120                  * current length.
121                  */
122                 if (argc <= 4) {
123                         len = fdt_totalsize(fdt);
124                 } else {
125                         len = simple_strtoul(argv[4], NULL, 16);
126                         if (len < fdt_totalsize(fdt)) {
127                                 printf ("New length 0x%X < existing length "
128                                         "0x%X, aborting.\n",
129                                         len, fdt_totalsize(fdt));
130                                 return 1;
131                         }
132                 }
133
134                 /*
135                  * Copy to the new location.
136                  */
137                 err = fdt_open_into(fdt, newaddr, len);
138                 if (err != 0) {
139                         printf ("libfdt fdt_open_into(): %s\n",
140                                 fdt_strerror(err));
141                         return 1;
142                 }
143                 fdt = newaddr;
144
145         /********************************************************************
146          * Make a new node
147          ********************************************************************/
148         } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
149                 char *pathp;            /* path */
150                 char *nodep;            /* new node to add */
151                 int  nodeoffset;        /* node offset from libfdt */
152                 int  err;
153
154                 /*
155                  * Parameters: Node path, new node to be appended to the path.
156                  */
157                 if (argc < 4) {
158                         printf ("Usage:\n%s\n", cmdtp->usage);
159                         return 1;
160                 }
161
162                 pathp = argv[2];
163                 nodep = argv[3];
164
165                 nodeoffset = fdt_find_node_by_path (fdt, pathp);
166                 if (nodeoffset < 0) {
167                         /*
168                          * Not found or something else bad happened.
169                          */
170                         printf ("libfdt fdt_find_node_by_path() returned %s\n",
171                                 fdt_strerror(nodeoffset));
172                         return 1;
173                 }
174                 err = fdt_add_subnode(fdt, nodeoffset, nodep);
175                 if (err < 0) {
176                         printf ("libfdt fdt_add_subnode(): %s\n",
177                                 fdt_strerror(err));
178                         return 1;
179                 }
180
181         /********************************************************************
182          * Set the value of a property in the fdt.
183          ********************************************************************/
184         } else if (argv[1][0] == 's') {
185                 char *pathp;            /* path */
186                 char *prop;             /* property */
187                 char *newval;           /* value from the user (as a string) */
188                 int  nodeoffset;        /* node offset from libfdt */
189                 static char data[SCRATCHPAD];   /* storage for the property */
190                 int  len;               /* new length of the property */
191                 int  ret;               /* return value */
192
193                 /*
194                  * Parameters: Node path, property, value.
195                  */
196                 if (argc < 5) {
197                         printf ("Usage:\n%s\n", cmdtp->usage);
198                         return 1;
199                 }
200
201                 pathp  = argv[2];
202                 prop   = argv[3];
203                 newval = argv[4];
204
205                 nodeoffset = fdt_find_node_by_path (fdt, pathp);
206                 if (nodeoffset < 0) {
207                         /*
208                          * Not found or something else bad happened.
209                          */
210                         printf ("libfdt fdt_find_node_by_path() returned %s\n",
211                                 fdt_strerror(nodeoffset));
212                         return 1;
213                 }
214                 ret = fdt_parse_prop(pathp, prop, newval, data, &len);
215                 if (ret != 0)
216                         return ret;
217
218                 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
219                 if (ret < 0) {
220                         printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
221                         return 1;
222                 }
223
224         /********************************************************************
225          * Print (recursive) / List (single level)
226          ********************************************************************/
227         } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
228                 int depth = MAX_LEVEL;  /* how deep to print */
229                 char *pathp;            /* path */
230                 char *prop;             /* property */
231                 int  ret;               /* return value */
232
233                 /*
234                  * list is an alias for print, but limited to 1 level
235                  */
236                 if (argv[1][0] == 'l') {
237                         depth = 1;
238                 }
239
240                 /*
241                  * Get the starting path.  The root node is an oddball,
242                  * the offset is zero and has no name.
243                  */
244                 pathp = argv[2];
245                 if (argc > 3)
246                         prop = argv[3];
247                 else
248                         prop = NULL;
249
250                 ret = fdt_print(pathp, prop, depth);
251                 if (ret != 0)
252                         return ret;
253
254         /********************************************************************
255          * Remove a property/node
256          ********************************************************************/
257         } else if (argv[1][0] == 'r') {
258                 int  nodeoffset;        /* node offset from libfdt */
259                 int  err;
260
261                 /*
262                  * Get the path.  The root node is an oddball, the offset
263                  * is zero and has no name.
264                  */
265                 nodeoffset = fdt_find_node_by_path (fdt, argv[2]);
266                 if (nodeoffset < 0) {
267                         /*
268                          * Not found or something else bad happened.
269                          */
270                         printf ("libfdt fdt_find_node_by_path() returned %s\n",
271                                 fdt_strerror(nodeoffset));
272                         return 1;
273                 }
274                 /*
275                  * Do the delete.  A fourth parameter means delete a property,
276                  * otherwise delete the node.
277                  */
278                 if (argc > 3) {
279                         err = fdt_delprop(fdt, nodeoffset, argv[3]);
280                         if (err < 0) {
281                                 printf("libfdt fdt_delprop():  %s\n",
282                                         fdt_strerror(err));
283                                 return err;
284                         }
285                 } else {
286                         err = fdt_del_node(fdt, nodeoffset);
287                         if (err < 0) {
288                                 printf("libfdt fdt_del_node():  %s\n",
289                                         fdt_strerror(err));
290                                 return err;
291                         }
292                 }
293         }
294 #ifdef CONFIG_OF_BOARD_SETUP
295         /* Call the board-specific fixup routine */
296         else if (argv[1][0] == 'b')
297                 ft_board_setup(fdt, gd->bd);
298 #endif
299         /* Create a chosen node */
300         else if (argv[1][0] == 'c')
301                 fdt_chosen(fdt, 0, 0, 1);
302
303 #ifdef CONFIG_OF_HAS_UBOOT_ENV
304         /* Create a u-boot-env node */
305         else if (argv[1][0] == 'e')
306                 fdt_env(fdt);
307 #endif
308 #ifdef CONFIG_OF_HAS_BD_T
309         /* Create a bd_t node */
310         else if (argv[1][0] == 'b')
311                 fdt_bd_t(fdt);
312 #endif
313         else {
314                 /* Unrecognized command */
315                 printf ("Usage:\n%s\n", cmdtp->usage);
316                 return 1;
317         }
318
319         return 0;
320 }
321
322 /****************************************************************************/
323
324 static int fdt_valid(void)
325 {
326         int  err;
327
328         if (fdt == NULL) {
329                 printf ("The address of the fdt is invalid (NULL).\n");
330                 return 0;
331         }
332
333         err = fdt_check_header(fdt);
334         if (err == 0)
335                 return 1;       /* valid */
336
337         if (err < 0) {
338                 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
339                 /*
340                  * Be more informative on bad version.
341                  */
342                 if (err == -FDT_ERR_BADVERSION) {
343                         if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
344                                 printf (" - too old, fdt $d < %d",
345                                         fdt_version(fdt),
346                                         FDT_FIRST_SUPPORTED_VERSION);
347                                 fdt = NULL;
348                         }
349                         if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
350                                 printf (" - too new, fdt $d > %d",
351                                         fdt_version(fdt),
352                                         FDT_LAST_SUPPORTED_VERSION);
353                                 fdt = NULL;
354                         }
355                         return 0;
356                 }
357                 printf("\n");
358                 return 0;
359         }
360         return 1;
361 }
362
363 /****************************************************************************/
364
365 /*
366  * Parse the user's input, partially heuristic.  Valid formats:
367  * <00>         - hex byte
368  * <0011>       - hex half word (16 bits)
369  * <00112233>   - hex word (32 bits)
370  *              - hex double words (64 bits) are not supported, must use
371  *                      a byte stream instead.
372  * [00 11 22 .. nn] - byte stream
373  * "string"     - If the the value doesn't start with "<" or "[", it is
374  *                      treated as a string.  Note that the quotes are
375  *                      stripped by the parser before we get the string.
376  */
377 static int fdt_parse_prop(char *pathp, char *prop, char *newval,
378         char *data, int *len)
379 {
380         char *cp;               /* temporary char pointer */
381         unsigned long tmp;      /* holds converted values */
382
383         if (*newval == '<') {
384                 /*
385                  * Bigger values than bytes.
386                  */
387                 *len = 0;
388                 newval++;
389                 while ((*newval != '>') && (*newval != '\0')) {
390                         cp = newval;
391                         tmp = simple_strtoul(cp, &newval, 16);
392                         if ((newval - cp) <= 2) {
393                                 *data = tmp & 0xFF;
394                                 data  += 1;
395                                 *len += 1;
396                         } else if ((newval - cp) <= 4) {
397                                 *(uint16_t *)data = __cpu_to_be16(tmp);
398                                 data  += 2;
399                                 *len += 2;
400                         } else if ((newval - cp) <= 8) {
401                                 *(uint32_t *)data = __cpu_to_be32(tmp);
402                                 data  += 4;
403                                 *len += 4;
404                         } else {
405                                 printf("Sorry, I could not convert \"%s\"\n",
406                                         cp);
407                                 return 1;
408                         }
409                         while (*newval == ' ')
410                                 newval++;
411                 }
412                 if (*newval != '>') {
413                         printf("Unexpected character '%c'\n", *newval);
414                         return 1;
415                 }
416         } else if (*newval == '[') {
417                 /*
418                  * Byte stream.  Convert the values.
419                  */
420                 *len = 0;
421                 newval++;
422                 while ((*newval != ']') && (*newval != '\0')) {
423                         tmp = simple_strtoul(newval, &newval, 16);
424                         *data++ = tmp & 0xFF;
425                         *len    = *len + 1;
426                         while (*newval == ' ')
427                                 newval++;
428                 }
429                 if (*newval != ']') {
430                         printf("Unexpected character '%c'\n", *newval);
431                         return 1;
432                 }
433         } else {
434                 /*
435                  * Assume it is a string.  Copy it into our data area for
436                  * convenience (including the terminating '\0').
437                  */
438                 *len = strlen(newval) + 1;
439                 strcpy(data, newval);
440         }
441         return 0;
442 }
443
444 /****************************************************************************/
445
446 /*
447  * Heuristic to guess if this is a string or concatenated strings.
448  */
449
450 static int is_printable_string(const void *data, int len)
451 {
452         const char *s = data;
453
454         /* zero length is not */
455         if (len == 0)
456                 return 0;
457
458         /* must terminate with zero */
459         if (s[len - 1] != '\0')
460                 return 0;
461
462         /* printable or a null byte (concatenated strings) */
463         while (((*s == '\0') || isprint(*s)) && (len > 0)) {
464                 /*
465                  * If we see a null, there are three possibilities:
466                  * 1) If len == 1, it is the end of the string, printable
467                  * 2) Next character also a null, not printable.
468                  * 3) Next character not a null, continue to check.
469                  */
470                 if (s[0] == '\0') {
471                         if (len == 1)
472                                 return 1;
473                         if (s[1] == '\0')
474                                 return 0;
475                 }
476                 s++;
477                 len--;
478         }
479
480         /* Not the null termination, or not done yet: not printable */
481         if (*s != '\0' || (len != 0))
482                 return 0;
483
484         return 1;
485 }
486
487
488 /*
489  * Print the property in the best format, a heuristic guess.  Print as
490  * a string, concatenated strings, a byte, word, double word, or (if all
491  * else fails) it is printed as a stream of bytes.
492  */
493 static void print_data(const void *data, int len)
494 {
495         int j;
496         const u8 *s;
497
498         /* no data, don't print */
499         if (len == 0)
500                 return;
501
502         /*
503          * It is a string, but it may have multiple strings (embedded '\0's).
504          */
505         if (is_printable_string(data, len)) {
506                 puts("\"");
507                 j = 0;
508                 while (j < len) {
509                         if (j > 0)
510                                 puts("\", \"");
511                         puts(data);
512                         j    += strlen(data) + 1;
513                         data += strlen(data) + 1;
514                 }
515                 puts("\"");
516                 return;
517         }
518
519         switch (len) {
520         case 1:  /* byte */
521                 printf("<%02x>", (*(u8 *) data) & 0xff);
522                 break;
523         case 2:  /* half-word */
524                 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
525                 break;
526         case 4:  /* word */
527                 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
528                 break;
529         case 8:  /* double-word */
530 #if __WORDSIZE == 64
531                 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
532 #else
533                 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
534                 data += 4;
535                 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
536 #endif
537                 break;
538         default:                /* anything else... hexdump */
539                 printf("[");
540                 for (j = 0, s = data; j < len; j++)
541                         printf("%02x%s", s[j], j < len - 1 ? " " : "");
542                 printf("]");
543
544                 break;
545         }
546 }
547
548 /****************************************************************************/
549
550 /*
551  * Recursively print (a portion of) the fdt.  The depth parameter
552  * determines how deeply nested the fdt is printed.
553  */
554 static int fdt_print(char *pathp, char *prop, int depth)
555 {
556         static int offstack[MAX_LEVEL];
557         static char tabs[MAX_LEVEL+1] =
558                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
559                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
560         void *nodep;            /* property node pointer */
561         int  nodeoffset;        /* node offset from libfdt */
562         int  nextoffset;        /* next node offset from libfdt */
563         uint32_t tag;           /* tag */
564         int  len;               /* length of the property */
565         int  level = 0;         /* keep track of nesting level */
566
567         nodeoffset = fdt_find_node_by_path (fdt, pathp);
568         if (nodeoffset < 0) {
569                 /*
570                  * Not found or something else bad happened.
571                  */
572                 printf ("libfdt fdt_find_node_by_path() returned %s\n",
573                         fdt_strerror(nodeoffset));
574                 return 1;
575         }
576         /*
577          * The user passed in a property as well as node path.
578          * Print only the given property and then return.
579          */
580         if (prop) {
581                 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
582                 if (len == 0) {
583                         /* no property value */
584                         printf("%s %s\n", pathp, prop);
585                         return 0;
586                 } else if (len > 0) {
587                         printf("%s=", prop);
588                         print_data (nodep, len);
589                         printf("\n");
590                         return 0;
591                 } else {
592                         printf ("libfdt fdt_getprop(): %s\n",
593                                 fdt_strerror(len));
594                         return 1;
595                 }
596         }
597
598         /*
599          * The user passed in a node path and no property,
600          * print the node and all subnodes.
601          */
602         offstack[0] = nodeoffset;
603
604         while(level >= 0) {
605                 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
606                 switch(tag) {
607                 case FDT_BEGIN_NODE:
608                         if(level <= depth)
609                                 printf("%s%s {\n",
610                                         &tabs[MAX_LEVEL - level], pathp);
611                         level++;
612                         offstack[level] = nodeoffset;
613                         if (level >= MAX_LEVEL) {
614                                 printf("Aaaiii <splat> nested too deep. "
615                                         "Aborting.\n");
616                                 return 1;
617                         }
618                         break;
619                 case FDT_END_NODE:
620                         level--;
621                         if(level <= depth)
622                                 printf("%s};\n", &tabs[MAX_LEVEL - level]);
623                         if (level == 0) {
624                                 level = -1;             /* exit the loop */
625                         }
626                         break;
627                 case FDT_PROP:
628                         nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
629                         if (len < 0) {
630                                 printf ("libfdt fdt_getprop(): %s\n",
631                                         fdt_strerror(len));
632                                 return 1;
633                         } else if (len == 0) {
634                                 /* the property has no value */
635                                 if(level <= depth)
636                                         printf("%s%s;\n",
637                                                 &tabs[MAX_LEVEL - level],
638                                                 pathp);
639                         } else {
640                                 if(level <= depth) {
641                                         printf("%s%s=",
642                                                 &tabs[MAX_LEVEL - level],
643                                                 pathp);
644                                         print_data (nodep, len);
645                                         printf(";\n");
646                                 }
647                         }
648                         break;
649                 case FDT_NOP:
650                         break;
651                 case FDT_END:
652                         return 1;
653                 default:
654                         if(level <= depth)
655                                 printf("Unknown tag 0x%08X\n", tag);
656                         return 1;
657                 }
658                 nodeoffset = nextoffset;
659         }
660         return 0;
661 }
662
663 /********************************************************************/
664
665 U_BOOT_CMD(
666         fdt,    5,      0,      do_fdt,
667         "fdt     - flattened device tree utility commands\n",
668             "addr   <addr> [<length>]        - Set the fdt location to <addr>\n"
669 #ifdef CONFIG_OF_BOARD_SETUP
670         "fdt boardsetup                      - Do board-specific set up\n"
671 #endif
672         "fdt move   <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
673         "fdt print  <path> [<prop>]          - Recursive print starting at <path>\n"
674         "fdt list   <path> [<prop>]          - Print one level starting at <path>\n"
675         "fdt set    <path> <prop> [<val>]    - Set <property> [to <val>]\n"
676         "fdt mknode <path> <node>            - Create a new node after <path>\n"
677         "fdt rm     <path> [<prop>]          - Delete the node or <property>\n"
678         "fdt chosen - Add/update the /chosen branch in the tree\n"
679 #ifdef CONFIG_OF_HAS_UBOOT_ENV
680         "fdt env    - Add/replace the /u-boot-env branch in the tree\n"
681 #endif
682 #ifdef CONFIG_OF_HAS_BD_T
683         "fdt bd_t   - Add/replace the /bd_t branch in the tree\n"
684 #endif
685         "Hints:\n"
686         " If the property you are setting/printing has a '#' character or spaces,\n"
687         "     you MUST escape it with a \\ character or quote it with \".\n"
688         "Examples: fdt print /               # print the whole tree\n"
689         "          fdt print /cpus \"#address-cells\"\n"
690         "          fdt set   /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
691 );