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