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