cmd: fdt: Use env_set_hex() for "get addr" and "get size"
[platform/kernel/u-boot.git] / cmd / fdt.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2007
4  * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5  * Based on code written by:
6  *   Pantelis Antoniou <pantelis.antoniou@gmail.com> and
7  *   Matthew McClintock <msm@freescale.com>
8  */
9
10 #include <common.h>
11 #include <command.h>
12 #include <env.h>
13 #include <image.h>
14 #include <linux/ctype.h>
15 #include <linux/types.h>
16 #include <asm/global_data.h>
17 #include <linux/libfdt.h>
18 #include <fdt_support.h>
19 #include <mapmem.h>
20 #include <asm/io.h>
21
22 #define MAX_LEVEL       32              /* how deeply nested we will go */
23 #define SCRATCHPAD      1024            /* bytes of scratchpad memory */
24
25 /*
26  * Global data (for the gd->bd)
27  */
28 DECLARE_GLOBAL_DATA_PTR;
29
30 static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
31 static int fdt_print(const char *pathp, char *prop, int depth);
32 static int is_printable_string(const void *data, int len);
33
34 /*
35  * The working_fdt points to our working flattened device tree.
36  */
37 struct fdt_header *working_fdt;
38
39 void set_working_fdt_addr(ulong addr)
40 {
41         void *buf;
42
43         printf("Working FDT set to %lx\n", addr);
44         buf = map_sysmem(addr, 0);
45         working_fdt = buf;
46         env_set_hex("fdtaddr", addr);
47 }
48
49 /*
50  * Get a value from the fdt and format it to be set in the environment
51  */
52 static int fdt_value_env_set(const void *nodep, int len,
53                              const char *var, int index)
54 {
55         if (is_printable_string(nodep, len)) {
56                 const char *nodec = (const char *)nodep;
57                 int i;
58
59                 /*
60                  * Iterate over all members in stringlist and find the one at
61                  * offset $index. If no such index exists, indicate failure.
62                  */
63                 for (i = 0; i < len; ) {
64                         if (index-- > 0) {
65                                 i += strlen(nodec) + 1;
66                                 nodec += strlen(nodec) + 1;
67                                 continue;
68                         }
69
70                         env_set(var, nodec);
71                         return 0;
72                 }
73
74                 return 1;
75         } else if (len == 4) {
76                 char buf[11];
77
78                 sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep));
79                 env_set(var, buf);
80         } else if (len % 4 == 0 && index >= 0) {
81                 /* Needed to print integer arrays. */
82                 const unsigned int *nodec = (const unsigned int *)nodep;
83                 char buf[11];
84
85                 if (index * 4 >= len)
86                         return 1;
87
88                 sprintf(buf, "0x%08X", fdt32_to_cpu(*(nodec + index)));
89                 env_set(var, buf);
90         } else if (len % 4 == 0 && len <= 20) {
91                 /* Needed to print things like sha1 hashes. */
92                 char buf[41];
93                 int i;
94
95                 for (i = 0; i < len; i += sizeof(unsigned int))
96                         sprintf(buf + (i * 2), "%08x",
97                                 *(unsigned int *)(nodep + i));
98                 env_set(var, buf);
99         } else {
100                 printf("error: unprintable value\n");
101                 return 1;
102         }
103         return 0;
104 }
105
106 static const char * const fdt_member_table[] = {
107         "magic",
108         "totalsize",
109         "off_dt_struct",
110         "off_dt_strings",
111         "off_mem_rsvmap",
112         "version",
113         "last_comp_version",
114         "boot_cpuid_phys",
115         "size_dt_strings",
116         "size_dt_struct",
117 };
118
119 static int fdt_get_header_value(int argc, char *const argv[])
120 {
121         fdt32_t *fdtp = (fdt32_t *)working_fdt;
122         ulong val;
123         int i;
124
125         if (argv[2][0] != 'g')
126                 return CMD_RET_FAILURE;
127
128         for (i = 0; i < ARRAY_SIZE(fdt_member_table); i++) {
129                 if (strcmp(fdt_member_table[i], argv[4]))
130                         continue;
131
132                 val = fdt32_to_cpu(fdtp[i]);
133                 env_set_hex(argv[3], val);
134                 return CMD_RET_SUCCESS;
135         }
136
137         return CMD_RET_FAILURE;
138 }
139
140 /*
141  * Flattened Device Tree command, see the help for parameter definitions.
142  */
143 static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
144 {
145         if (argc < 2)
146                 return CMD_RET_USAGE;
147
148         /* fdt addr: Set the address of the fdt */
149         if (strncmp(argv[1], "ad", 2) == 0) {
150                 unsigned long addr;
151                 int control = 0;
152                 int quiet = 0;
153                 struct fdt_header *blob;
154
155                 /* Set the address [and length] of the fdt */
156                 argc -= 2;
157                 argv += 2;
158                 while (argc > 0 && **argv == '-') {
159                         char *arg = *argv;
160
161                         while (*++arg) {
162                                 switch (*arg) {
163                                 case 'c':
164                                         control = 1;
165                                         break;
166                                 case 'q':
167                                         quiet = 1;
168                                         break;
169                                 default:
170                                         return CMD_RET_USAGE;
171                                 }
172                         }
173                         argc--;
174                         argv++;
175                 }
176                 if (argc == 0) {
177                         if (control)
178                                 blob = (struct fdt_header *)gd->fdt_blob;
179                         else
180                                 blob = working_fdt;
181                         if (!blob || !fdt_valid(&blob))
182                                 return 1;
183                         printf("%s fdt: %08lx\n",
184                                control ? "Control" : "Working",
185                                control ? (ulong)map_to_sysmem(blob) :
186                                env_get_hex("fdtaddr", 0));
187                         return 0;
188                 }
189
190                 addr = hextoul(argv[0], NULL);
191                 blob = map_sysmem(addr, 0);
192                 if ((quiet && fdt_check_header(blob)) ||
193                     (!quiet && !fdt_valid(&blob)))
194                         return 1;
195                 if (control)
196                         gd->fdt_blob = blob;
197                 else
198                         set_working_fdt_addr(addr);
199
200                 if (argc >= 2) {
201                         int  len;
202                         int  err;
203
204                         /* Optional new length */
205                         len = hextoul(argv[1], NULL);
206                         if (len < fdt_totalsize(blob)) {
207                                 if (!quiet)
208                                         printf("New length %d < existing length %d, ignoring\n",
209                                                len, fdt_totalsize(blob));
210                         } else {
211                                 /* Open in place with a new length */
212                                 err = fdt_open_into(blob, blob, len);
213                                 if (!quiet && err != 0) {
214                                         printf("libfdt fdt_open_into(): %s\n",
215                                                fdt_strerror(err));
216                                 }
217                         }
218                 }
219
220                 return CMD_RET_SUCCESS;
221
222         /*
223          * Move the working_fdt
224          */
225         } else if (strncmp(argv[1], "mo", 2) == 0) {
226                 struct fdt_header *newaddr;
227                 int  len;
228                 int  err;
229
230                 if (argc < 4)
231                         return CMD_RET_USAGE;
232
233                 /*
234                  * Set the address and length of the fdt.
235                  */
236                 working_fdt = map_sysmem(hextoul(argv[2], NULL), 0);
237                 if (!fdt_valid(&working_fdt))
238                         return 1;
239
240                 newaddr = map_sysmem(hextoul(argv[3], NULL), 0);
241
242                 /*
243                  * If the user specifies a length, use that.  Otherwise use the
244                  * current length.
245                  */
246                 if (argc <= 4) {
247                         len = fdt_totalsize(working_fdt);
248                 } else {
249                         len = hextoul(argv[4], NULL);
250                         if (len < fdt_totalsize(working_fdt)) {
251                                 printf ("New length 0x%X < existing length "
252                                         "0x%X, aborting.\n",
253                                         len, fdt_totalsize(working_fdt));
254                                 return 1;
255                         }
256                 }
257
258                 /*
259                  * Copy to the new location.
260                  */
261                 err = fdt_open_into(working_fdt, newaddr, len);
262                 if (err != 0) {
263                         printf ("libfdt fdt_open_into(): %s\n",
264                                 fdt_strerror(err));
265                         return 1;
266                 }
267                 set_working_fdt_addr(map_to_sysmem(newaddr));
268
269                 return CMD_RET_SUCCESS;
270         }
271
272         if (!working_fdt) {
273                 puts("No FDT memory address configured. Please configure\n"
274                      "the FDT address via \"fdt addr <address>\" command.\n"
275                      "Aborting!\n");
276                 return CMD_RET_FAILURE;
277         }
278
279 #ifdef CONFIG_OF_SYSTEM_SETUP
280         /* Call the board-specific fixup routine */
281         if (strncmp(argv[1], "sys", 3) == 0) {
282                 int err = ft_system_setup(working_fdt, gd->bd);
283
284                 if (err) {
285                         printf("Failed to add system information to FDT: %s\n",
286                                fdt_strerror(err));
287                         return CMD_RET_FAILURE;
288                 }
289
290                 return CMD_RET_SUCCESS;
291         }
292 #endif
293         /*
294          * Make a new node
295          */
296         if (strncmp(argv[1], "mk", 2) == 0) {
297                 char *pathp;            /* path */
298                 char *nodep;            /* new node to add */
299                 int  nodeoffset;        /* node offset from libfdt */
300                 int  err;
301
302                 /*
303                  * Parameters: Node path, new node to be appended to the path.
304                  */
305                 if (argc < 4)
306                         return CMD_RET_USAGE;
307
308                 pathp = argv[2];
309                 nodep = argv[3];
310
311                 nodeoffset = fdt_path_offset (working_fdt, pathp);
312                 if (nodeoffset < 0) {
313                         /*
314                          * Not found or something else bad happened.
315                          */
316                         printf ("libfdt fdt_path_offset() returned %s\n",
317                                 fdt_strerror(nodeoffset));
318                         return 1;
319                 }
320                 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
321                 if (err < 0) {
322                         printf ("libfdt fdt_add_subnode(): %s\n",
323                                 fdt_strerror(err));
324                         return 1;
325                 }
326
327         /*
328          * Set the value of a property in the working_fdt.
329          */
330         } else if (strncmp(argv[1], "se", 2) == 0) {
331                 char *pathp;            /* path */
332                 char *prop;             /* property */
333                 int  nodeoffset;        /* node offset from libfdt */
334                 static char data[SCRATCHPAD] __aligned(4);/* property storage */
335                 const void *ptmp;
336                 int  len;               /* new length of the property */
337                 int  ret;               /* return value */
338
339                 /*
340                  * Parameters: Node path, property, optional value.
341                  */
342                 if (argc < 4)
343                         return CMD_RET_USAGE;
344
345                 pathp  = argv[2];
346                 prop   = argv[3];
347
348                 nodeoffset = fdt_path_offset (working_fdt, pathp);
349                 if (nodeoffset < 0) {
350                         /*
351                          * Not found or something else bad happened.
352                          */
353                         printf ("libfdt fdt_path_offset() returned %s\n",
354                                 fdt_strerror(nodeoffset));
355                         return 1;
356                 }
357
358                 if (argc == 4) {
359                         len = 0;
360                 } else {
361                         ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len);
362                         if (len > SCRATCHPAD) {
363                                 printf("prop (%d) doesn't fit in scratchpad!\n",
364                                        len);
365                                 return 1;
366                         }
367                         if (ptmp != NULL)
368                                 memcpy(data, ptmp, len);
369
370                         ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
371                         if (ret != 0)
372                                 return ret;
373                 }
374
375                 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
376                 if (ret < 0) {
377                         printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
378                         return 1;
379                 }
380
381         /********************************************************************
382          * Get the value of a property in the working_fdt.
383          ********************************************************************/
384         } else if (argv[1][0] == 'g') {
385                 char *subcmd;           /* sub-command */
386                 char *pathp;            /* path */
387                 char *prop;             /* property */
388                 char *var;              /* variable to store result */
389                 int  nodeoffset;        /* node offset from libfdt */
390                 const void *nodep;      /* property node pointer */
391                 int  len = 0;           /* new length of the property */
392
393                 /*
394                  * Parameters: Node path, property, optional value.
395                  */
396                 if (argc < 5)
397                         return CMD_RET_USAGE;
398
399                 subcmd = argv[2];
400
401                 if (argc < 6 && subcmd[0] != 's')
402                         return CMD_RET_USAGE;
403
404                 var    = argv[3];
405                 pathp  = argv[4];
406                 prop   = argv[5];
407
408                 nodeoffset = fdt_path_offset(working_fdt, pathp);
409                 if (nodeoffset < 0) {
410                         /*
411                          * Not found or something else bad happened.
412                          */
413                         printf("libfdt fdt_path_offset() returned %s\n",
414                                 fdt_strerror(nodeoffset));
415                         return 1;
416                 }
417
418                 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
419                         int req_index = -1;
420                         int startDepth = fdt_node_depth(
421                                 working_fdt, nodeoffset);
422                         int curDepth = startDepth;
423                         int cur_index = -1;
424                         int nextNodeOffset = fdt_next_node(
425                                 working_fdt, nodeoffset, &curDepth);
426
427                         if (subcmd[0] == 'n')
428                                 req_index = hextoul(argv[5], NULL);
429
430                         while (curDepth > startDepth) {
431                                 if (curDepth == startDepth + 1)
432                                         cur_index++;
433                                 if (subcmd[0] == 'n' &&
434                                     cur_index == req_index) {
435                                         const char *node_name;
436
437                                         node_name = fdt_get_name(working_fdt,
438                                                                  nextNodeOffset,
439                                                                  NULL);
440                                         env_set(var, node_name);
441                                         return 0;
442                                 }
443                                 nextNodeOffset = fdt_next_node(
444                                         working_fdt, nextNodeOffset, &curDepth);
445                                 if (nextNodeOffset < 0)
446                                         break;
447                         }
448                         if (subcmd[0] == 's') {
449                                 /* get the num nodes at this level */
450                                 env_set_ulong(var, cur_index + 1);
451                         } else {
452                                 /* node index not found */
453                                 printf("libfdt node not found\n");
454                                 return 1;
455                         }
456                 } else {
457                         nodep = fdt_getprop(
458                                 working_fdt, nodeoffset, prop, &len);
459                         if (nodep && len >= 0) {
460                                 if (subcmd[0] == 'v') {
461                                         int index = -1;
462                                         int ret;
463
464                                         if (len == 0) {
465                                                 /* no property value */
466                                                 env_set(var, "");
467                                                 return 0;
468                                         }
469
470                                         if (argc == 7)
471                                                 index = simple_strtoul(argv[6], NULL, 10);
472
473                                         ret = fdt_value_env_set(nodep, len,
474                                                                 var, index);
475                                         if (ret != 0)
476                                                 return ret;
477                                 } else if (subcmd[0] == 'a') {
478                                         env_set_hex(var, (ulong)map_to_sysmem(nodep));
479                                 } else if (subcmd[0] == 's') {
480                                         env_set_hex(var, len);
481                                 } else
482                                         return CMD_RET_USAGE;
483                                 return 0;
484                         } else {
485                                 printf("libfdt fdt_getprop(): %s\n",
486                                         fdt_strerror(len));
487                                 return 1;
488                         }
489                 }
490
491         /*
492          * Print (recursive) / List (single level)
493          */
494         } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
495                 int depth = MAX_LEVEL;  /* how deep to print */
496                 char *pathp;            /* path */
497                 char *prop;             /* property */
498                 int  ret;               /* return value */
499                 static char root[2] = "/";
500
501                 /*
502                  * list is an alias for print, but limited to 1 level
503                  */
504                 if (argv[1][0] == 'l') {
505                         depth = 1;
506                 }
507
508                 /*
509                  * Get the starting path.  The root node is an oddball,
510                  * the offset is zero and has no name.
511                  */
512                 if (argc == 2)
513                         pathp = root;
514                 else
515                         pathp = argv[2];
516                 if (argc > 3)
517                         prop = argv[3];
518                 else
519                         prop = NULL;
520
521                 ret = fdt_print(pathp, prop, depth);
522                 if (ret != 0)
523                         return ret;
524
525         /*
526          * Remove a property/node
527          */
528         } else if (strncmp(argv[1], "rm", 2) == 0) {
529                 int  nodeoffset;        /* node offset from libfdt */
530                 int  err;
531
532                 /*
533                  * Get the path.  The root node is an oddball, the offset
534                  * is zero and has no name.
535                  */
536                 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
537                 if (nodeoffset < 0) {
538                         /*
539                          * Not found or something else bad happened.
540                          */
541                         printf ("libfdt fdt_path_offset() returned %s\n",
542                                 fdt_strerror(nodeoffset));
543                         return 1;
544                 }
545                 /*
546                  * Do the delete.  A fourth parameter means delete a property,
547                  * otherwise delete the node.
548                  */
549                 if (argc > 3) {
550                         err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
551                         if (err < 0) {
552                                 printf("libfdt fdt_delprop(): %s\n",
553                                         fdt_strerror(err));
554                                 return CMD_RET_FAILURE;
555                         }
556                 } else {
557                         err = fdt_del_node(working_fdt, nodeoffset);
558                         if (err < 0) {
559                                 printf("libfdt fdt_del_node(): %s\n",
560                                         fdt_strerror(err));
561                                 return CMD_RET_FAILURE;
562                         }
563                 }
564
565         /*
566          * Display header info
567          */
568         } else if (argv[1][0] == 'h') {
569                 if (argc == 5)
570                         return fdt_get_header_value(argc, argv);
571
572                 u32 version = fdt_version(working_fdt);
573                 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
574                 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
575                        fdt_totalsize(working_fdt));
576                 printf("off_dt_struct:\t\t0x%x\n",
577                        fdt_off_dt_struct(working_fdt));
578                 printf("off_dt_strings:\t\t0x%x\n",
579                        fdt_off_dt_strings(working_fdt));
580                 printf("off_mem_rsvmap:\t\t0x%x\n",
581                        fdt_off_mem_rsvmap(working_fdt));
582                 printf("version:\t\t%d\n", version);
583                 printf("last_comp_version:\t%d\n",
584                        fdt_last_comp_version(working_fdt));
585                 if (version >= 2)
586                         printf("boot_cpuid_phys:\t0x%x\n",
587                                 fdt_boot_cpuid_phys(working_fdt));
588                 if (version >= 3)
589                         printf("size_dt_strings:\t0x%x\n",
590                                 fdt_size_dt_strings(working_fdt));
591                 if (version >= 17)
592                         printf("size_dt_struct:\t\t0x%x\n",
593                                 fdt_size_dt_struct(working_fdt));
594                 printf("number mem_rsv:\t\t0x%x\n",
595                        fdt_num_mem_rsv(working_fdt));
596                 printf("\n");
597
598         /*
599          * Set boot cpu id
600          */
601         } else if (strncmp(argv[1], "boo", 3) == 0) {
602                 unsigned long tmp;
603
604                 if (argc != 3)
605                         return CMD_RET_USAGE;
606
607                 tmp = hextoul(argv[2], NULL);
608                 fdt_set_boot_cpuid_phys(working_fdt, tmp);
609
610         /*
611          * memory command
612          */
613         } else if (strncmp(argv[1], "me", 2) == 0) {
614                 uint64_t addr, size;
615                 int err;
616
617                 if (argc != 4)
618                         return CMD_RET_USAGE;
619
620                 addr = simple_strtoull(argv[2], NULL, 16);
621                 size = simple_strtoull(argv[3], NULL, 16);
622                 err = fdt_fixup_memory(working_fdt, addr, size);
623                 if (err < 0)
624                         return err;
625
626         /*
627          * mem reserve commands
628          */
629         } else if (strncmp(argv[1], "rs", 2) == 0) {
630                 if (argv[2][0] == 'p') {
631                         uint64_t addr, size;
632                         int total = fdt_num_mem_rsv(working_fdt);
633                         int j, err;
634                         printf("index\t\t   start\t\t    size\n");
635                         printf("-------------------------------"
636                                 "-----------------\n");
637                         for (j = 0; j < total; j++) {
638                                 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
639                                 if (err < 0) {
640                                         printf("libfdt fdt_get_mem_rsv():  %s\n",
641                                                         fdt_strerror(err));
642                                         return err;
643                                 }
644                                 printf("    %x\t%08x%08x\t%08x%08x\n", j,
645                                         (u32)(addr >> 32),
646                                         (u32)(addr & 0xffffffff),
647                                         (u32)(size >> 32),
648                                         (u32)(size & 0xffffffff));
649                         }
650                 } else if (argv[2][0] == 'a') {
651                         uint64_t addr, size;
652                         int err;
653                         addr = simple_strtoull(argv[3], NULL, 16);
654                         size = simple_strtoull(argv[4], NULL, 16);
655                         err = fdt_add_mem_rsv(working_fdt, addr, size);
656
657                         if (err < 0) {
658                                 printf("libfdt fdt_add_mem_rsv(): %s\n",
659                                         fdt_strerror(err));
660                                 return CMD_RET_FAILURE;
661                         }
662                 } else if (argv[2][0] == 'd') {
663                         unsigned long idx = hextoul(argv[3], NULL);
664                         int err = fdt_del_mem_rsv(working_fdt, idx);
665
666                         if (err < 0) {
667                                 printf("libfdt fdt_del_mem_rsv(): %s\n",
668                                         fdt_strerror(err));
669                                 return CMD_RET_FAILURE;
670                         }
671                 } else {
672                         /* Unrecognized command */
673                         return CMD_RET_USAGE;
674                 }
675         }
676 #ifdef CONFIG_OF_BOARD_SETUP
677         /* Call the board-specific fixup routine */
678         else if (strncmp(argv[1], "boa", 3) == 0) {
679                 int err = ft_board_setup(working_fdt, gd->bd);
680
681                 if (err) {
682                         printf("Failed to update board information in FDT: %s\n",
683                                fdt_strerror(err));
684                         return CMD_RET_FAILURE;
685                 }
686 #ifdef CONFIG_ARCH_KEYSTONE
687                 ft_board_setup_ex(working_fdt, gd->bd);
688 #endif
689         }
690 #endif
691         /* Create a chosen node */
692         else if (strncmp(argv[1], "cho", 3) == 0) {
693                 unsigned long initrd_start = 0, initrd_end = 0;
694
695                 if ((argc != 2) && (argc != 4))
696                         return CMD_RET_USAGE;
697
698                 if (argc == 4) {
699                         initrd_start = hextoul(argv[2], NULL);
700                         initrd_end = initrd_start + hextoul(argv[3], NULL) - 1;
701                 }
702
703                 fdt_chosen(working_fdt);
704                 fdt_initrd(working_fdt, initrd_start, initrd_end);
705
706 #if defined(CONFIG_FIT_SIGNATURE)
707         } else if (strncmp(argv[1], "che", 3) == 0) {
708                 int cfg_noffset;
709                 int ret;
710                 unsigned long addr;
711                 struct fdt_header *blob;
712
713                 if (!working_fdt)
714                         return CMD_RET_FAILURE;
715
716                 if (argc > 2) {
717                         addr = hextoul(argv[2], NULL);
718                         blob = map_sysmem(addr, 0);
719                 } else {
720                         blob = (struct fdt_header *)gd->fdt_blob;
721                 }
722                 if (!fdt_valid(&blob))
723                         return 1;
724
725                 gd->fdt_blob = blob;
726                 cfg_noffset = fit_conf_get_node(working_fdt, NULL);
727                 if (!cfg_noffset) {
728                         printf("Could not find configuration node: %s\n",
729                                fdt_strerror(cfg_noffset));
730                         return CMD_RET_FAILURE;
731                 }
732
733                 ret = fit_config_verify(working_fdt, cfg_noffset);
734                 if (ret == 0)
735                         return CMD_RET_SUCCESS;
736                 else
737                         return CMD_RET_FAILURE;
738 #endif
739
740         }
741 #ifdef CONFIG_OF_LIBFDT_OVERLAY
742         /* apply an overlay */
743         else if (strncmp(argv[1], "ap", 2) == 0) {
744                 unsigned long addr;
745                 struct fdt_header *blob;
746                 int ret;
747
748                 if (argc != 3)
749                         return CMD_RET_USAGE;
750
751                 if (!working_fdt)
752                         return CMD_RET_FAILURE;
753
754                 addr = hextoul(argv[2], NULL);
755                 blob = map_sysmem(addr, 0);
756                 if (!fdt_valid(&blob))
757                         return CMD_RET_FAILURE;
758
759                 /* apply method prints messages on error */
760                 ret = fdt_overlay_apply_verbose(working_fdt, blob);
761                 if (ret)
762                         return CMD_RET_FAILURE;
763         }
764 #endif
765         /* resize the fdt */
766         else if (strncmp(argv[1], "re", 2) == 0) {
767                 uint extrasize;
768                 if (argc > 2)
769                         extrasize = hextoul(argv[2], NULL);
770                 else
771                         extrasize = 0;
772                 fdt_shrink_to_minimum(working_fdt, extrasize);
773         }
774         else {
775                 /* Unrecognized command */
776                 return CMD_RET_USAGE;
777         }
778
779         return 0;
780 }
781
782 /****************************************************************************/
783
784 /*
785  * Parse the user's input, partially heuristic.  Valid formats:
786  * <0x00112233 4 05>    - an array of cells.  Numbers follow standard
787  *                      C conventions.
788  * [00 11 22 .. nn] - byte stream
789  * "string"     - If the the value doesn't start with "<" or "[", it is
790  *                      treated as a string.  Note that the quotes are
791  *                      stripped by the parser before we get the string.
792  * newval: An array of strings containing the new property as specified
793  *      on the command line
794  * count: The number of strings in the array
795  * data: A bytestream to be placed in the property
796  * len: The length of the resulting bytestream
797  */
798 static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
799 {
800         char *cp;               /* temporary char pointer */
801         char *newp;             /* temporary newval char pointer */
802         unsigned long tmp;      /* holds converted values */
803         int stridx = 0;
804
805         *len = 0;
806         newp = newval[0];
807
808         /* An array of cells */
809         if (*newp == '<') {
810                 newp++;
811                 while ((*newp != '>') && (stridx < count)) {
812                         /*
813                          * Keep searching until we find that last ">"
814                          * That way users don't have to escape the spaces
815                          */
816                         if (*newp == '\0') {
817                                 newp = newval[++stridx];
818                                 continue;
819                         }
820
821                         cp = newp;
822                         tmp = simple_strtoul(cp, &newp, 0);
823                         if (*cp != '?')
824                                 *(fdt32_t *)data = cpu_to_fdt32(tmp);
825                         else
826                                 newp++;
827
828                         data  += 4;
829                         *len += 4;
830
831                         /* If the ptr didn't advance, something went wrong */
832                         if ((newp - cp) <= 0) {
833                                 printf("Sorry, I could not convert \"%s\"\n",
834                                         cp);
835                                 return 1;
836                         }
837
838                         while (*newp == ' ')
839                                 newp++;
840                 }
841
842                 if (*newp != '>') {
843                         printf("Unexpected character '%c'\n", *newp);
844                         return 1;
845                 }
846         } else if (*newp == '[') {
847                 /*
848                  * Byte stream.  Convert the values.
849                  */
850                 newp++;
851                 while ((stridx < count) && (*newp != ']')) {
852                         while (*newp == ' ')
853                                 newp++;
854                         if (*newp == '\0') {
855                                 newp = newval[++stridx];
856                                 continue;
857                         }
858                         if (!isxdigit(*newp))
859                                 break;
860                         tmp = hextoul(newp, &newp);
861                         *data++ = tmp & 0xFF;
862                         *len    = *len + 1;
863                 }
864                 if (*newp != ']') {
865                         printf("Unexpected character '%c'\n", *newp);
866                         return 1;
867                 }
868         } else {
869                 /*
870                  * Assume it is one or more strings.  Copy it into our
871                  * data area for convenience (including the
872                  * terminating '\0's).
873                  */
874                 while (stridx < count) {
875                         size_t length = strlen(newp) + 1;
876                         strcpy(data, newp);
877                         data += length;
878                         *len += length;
879                         newp = newval[++stridx];
880                 }
881         }
882         return 0;
883 }
884
885 /****************************************************************************/
886
887 /*
888  * Heuristic to guess if this is a string or concatenated strings.
889  */
890
891 static int is_printable_string(const void *data, int len)
892 {
893         const char *s = data;
894         const char *ss, *se;
895
896         /* zero length is not */
897         if (len == 0)
898                 return 0;
899
900         /* must terminate with zero */
901         if (s[len - 1] != '\0')
902                 return 0;
903
904         se = s + len;
905
906         while (s < se) {
907                 ss = s;
908                 while (s < se && *s && isprint((unsigned char)*s))
909                         s++;
910
911                 /* not zero, or not done yet */
912                 if (*s != '\0' || s == ss)
913                         return 0;
914
915                 s++;
916         }
917
918         return 1;
919 }
920
921 /*
922  * Print the property in the best format, a heuristic guess.  Print as
923  * a string, concatenated strings, a byte, word, double word, or (if all
924  * else fails) it is printed as a stream of bytes.
925  */
926 static void print_data(const void *data, int len)
927 {
928         int j;
929         const char *env_max_dump;
930         ulong max_dump = ULONG_MAX;
931
932         /* no data, don't print */
933         if (len == 0)
934                 return;
935
936         env_max_dump = env_get("fdt_max_dump");
937         if (env_max_dump)
938                 max_dump = hextoul(env_max_dump, NULL);
939
940         /*
941          * It is a string, but it may have multiple strings (embedded '\0's).
942          */
943         if (is_printable_string(data, len)) {
944                 puts("\"");
945                 j = 0;
946                 while (j < len) {
947                         if (j > 0)
948                                 puts("\", \"");
949                         puts(data);
950                         j    += strlen(data) + 1;
951                         data += strlen(data) + 1;
952                 }
953                 puts("\"");
954                 return;
955         }
956
957         if ((len %4) == 0) {
958                 if (len > max_dump)
959                         printf("* 0x%p [0x%08x]", data, len);
960                 else {
961                         const __be32 *p;
962
963                         printf("<");
964                         for (j = 0, p = data; j < len/4; j++)
965                                 printf("0x%08x%s", fdt32_to_cpu(p[j]),
966                                         j < (len/4 - 1) ? " " : "");
967                         printf(">");
968                 }
969         } else { /* anything else... hexdump */
970                 if (len > max_dump)
971                         printf("* 0x%p [0x%08x]", data, len);
972                 else {
973                         const u8 *s;
974
975                         printf("[");
976                         for (j = 0, s = data; j < len; j++)
977                                 printf("%02x%s", s[j], j < len - 1 ? " " : "");
978                         printf("]");
979                 }
980         }
981 }
982
983 /****************************************************************************/
984
985 /*
986  * Recursively print (a portion of) the working_fdt.  The depth parameter
987  * determines how deeply nested the fdt is printed.
988  */
989 static int fdt_print(const char *pathp, char *prop, int depth)
990 {
991         static char tabs[MAX_LEVEL+1] =
992                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
993                 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
994         const void *nodep;      /* property node pointer */
995         int  nodeoffset;        /* node offset from libfdt */
996         int  nextoffset;        /* next node offset from libfdt */
997         uint32_t tag;           /* tag */
998         int  len;               /* length of the property */
999         int  level = 0;         /* keep track of nesting level */
1000         const struct fdt_property *fdt_prop;
1001
1002         nodeoffset = fdt_path_offset (working_fdt, pathp);
1003         if (nodeoffset < 0) {
1004                 /*
1005                  * Not found or something else bad happened.
1006                  */
1007                 printf ("libfdt fdt_path_offset() returned %s\n",
1008                         fdt_strerror(nodeoffset));
1009                 return 1;
1010         }
1011         /*
1012          * The user passed in a property as well as node path.
1013          * Print only the given property and then return.
1014          */
1015         if (prop) {
1016                 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
1017                 if (len == 0) {
1018                         /* no property value */
1019                         printf("%s %s\n", pathp, prop);
1020                         return 0;
1021                 } else if (nodep && len > 0) {
1022                         printf("%s = ", prop);
1023                         print_data (nodep, len);
1024                         printf("\n");
1025                         return 0;
1026                 } else {
1027                         printf ("libfdt fdt_getprop(): %s\n",
1028                                 fdt_strerror(len));
1029                         return 1;
1030                 }
1031         }
1032
1033         /*
1034          * The user passed in a node path and no property,
1035          * print the node and all subnodes.
1036          */
1037         while(level >= 0) {
1038                 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
1039                 switch(tag) {
1040                 case FDT_BEGIN_NODE:
1041                         pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
1042                         if (level <= depth) {
1043                                 if (pathp == NULL)
1044                                         pathp = "/* NULL pointer error */";
1045                                 if (*pathp == '\0')
1046                                         pathp = "/";    /* root is nameless */
1047                                 printf("%s%s {\n",
1048                                         &tabs[MAX_LEVEL - level], pathp);
1049                         }
1050                         level++;
1051                         if (level >= MAX_LEVEL) {
1052                                 printf("Nested too deep, aborting.\n");
1053                                 return 1;
1054                         }
1055                         break;
1056                 case FDT_END_NODE:
1057                         level--;
1058                         if (level <= depth)
1059                                 printf("%s};\n", &tabs[MAX_LEVEL - level]);
1060                         if (level == 0) {
1061                                 level = -1;             /* exit the loop */
1062                         }
1063                         break;
1064                 case FDT_PROP:
1065                         fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
1066                                         sizeof(*fdt_prop));
1067                         pathp    = fdt_string(working_fdt,
1068                                         fdt32_to_cpu(fdt_prop->nameoff));
1069                         len      = fdt32_to_cpu(fdt_prop->len);
1070                         nodep    = fdt_prop->data;
1071                         if (len < 0) {
1072                                 printf ("libfdt fdt_getprop(): %s\n",
1073                                         fdt_strerror(len));
1074                                 return 1;
1075                         } else if (len == 0) {
1076                                 /* the property has no value */
1077                                 if (level <= depth)
1078                                         printf("%s%s;\n",
1079                                                 &tabs[MAX_LEVEL - level],
1080                                                 pathp);
1081                         } else {
1082                                 if (level <= depth) {
1083                                         printf("%s%s = ",
1084                                                 &tabs[MAX_LEVEL - level],
1085                                                 pathp);
1086                                         print_data (nodep, len);
1087                                         printf(";\n");
1088                                 }
1089                         }
1090                         break;
1091                 case FDT_NOP:
1092                         printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
1093                         break;
1094                 case FDT_END:
1095                         return 1;
1096                 default:
1097                         if (level <= depth)
1098                                 printf("Unknown tag 0x%08X\n", tag);
1099                         return 1;
1100                 }
1101                 nodeoffset = nextoffset;
1102         }
1103         return 0;
1104 }
1105
1106 /********************************************************************/
1107 #ifdef CONFIG_SYS_LONGHELP
1108 static char fdt_help_text[] =
1109         "addr [-c] [-q] <addr> [<size>]  - Set the [control] fdt location to <addr>\n"
1110 #ifdef CONFIG_OF_LIBFDT_OVERLAY
1111         "fdt apply <addr>                    - Apply overlay to the DT\n"
1112 #endif
1113 #ifdef CONFIG_OF_BOARD_SETUP
1114         "fdt boardsetup                      - Do board-specific set up\n"
1115 #endif
1116 #ifdef CONFIG_OF_SYSTEM_SETUP
1117         "fdt systemsetup                     - Do system-specific set up\n"
1118 #endif
1119         "fdt move   <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
1120         "fdt resize [<extrasize>]            - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n"
1121         "fdt print  <path> [<prop>]          - Recursive print starting at <path>\n"
1122         "fdt list   <path> [<prop>]          - Print one level starting at <path>\n"
1123         "fdt get value <var> <path> <prop> [<index>] - Get <property> and store in <var>\n"
1124         "                                      In case of stringlist property, use optional <index>\n"
1125         "                                      to select string within the stringlist. Default is 0.\n"
1126         "fdt get name <var> <path> <index>   - Get name of node <index> and store in <var>\n"
1127         "fdt get addr <var> <path> <prop>    - Get start address of <property> and store in <var>\n"
1128         "fdt get size <var> <path> [<prop>]  - Get size of [<property>] or num nodes and store in <var>\n"
1129         "fdt set    <path> <prop> [<val>]    - Set <property> [to <val>]\n"
1130         "fdt mknode <path> <node>            - Create a new node after <path>\n"
1131         "fdt rm     <path> [<prop>]          - Delete the node or <property>\n"
1132         "fdt header [get <var> <member>]     - Display header info\n"
1133         "                                      get - get header member <member> and store it in <var>\n"
1134         "fdt bootcpu <id>                    - Set boot cpuid\n"
1135         "fdt memory <addr> <size>            - Add/Update memory node\n"
1136         "fdt rsvmem print                    - Show current mem reserves\n"
1137         "fdt rsvmem add <addr> <size>        - Add a mem reserve\n"
1138         "fdt rsvmem delete <index>           - Delete a mem reserves\n"
1139         "fdt chosen [<start> <size>]         - Add/update the /chosen branch in the tree\n"
1140         "                                        <start>/<size> - initrd start addr/size\n"
1141 #if defined(CONFIG_FIT_SIGNATURE)
1142         "fdt checksign [<addr>]              - check FIT signature\n"
1143         "                                      <addr> - address of key blob\n"
1144         "                                               default gd->fdt_blob\n"
1145 #endif
1146         "NOTE: Dereference aliases by omitting the leading '/', "
1147                 "e.g. fdt print ethernet0.";
1148 #endif
1149
1150 U_BOOT_CMD(
1151         fdt,    255,    0,      do_fdt,
1152         "flattened device tree utility commands", fdt_help_text
1153 );