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