]> Git Repo - J-u-boot.git/blame - cmd/fdt.c
cmd: fdt: Fix fdt rsvmem behavior on non-existent index and error message space
[J-u-boot.git] / cmd / fdt.c
CommitLineData
83d290c5 1// SPDX-License-Identifier: GPL-2.0+
781e09ee
GVB
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]>
781e09ee
GVB
8 */
9
10#include <common.h>
11#include <command.h>
c7694dd4 12#include <env.h>
4d72caa5 13#include <image.h>
781e09ee
GVB
14#include <linux/ctype.h>
15#include <linux/types.h>
781e09ee 16#include <asm/global_data.h>
b08c8c48 17#include <linux/libfdt.h>
64dbbd40 18#include <fdt_support.h>
0eb25b61 19#include <mapmem.h>
a92fd657 20#include <asm/io.h>
781e09ee
GVB
21
22#define MAX_LEVEL 32 /* how deeply nested we will go */
fd61e55d 23#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
781e09ee
GVB
24
25/*
26 * Global data (for the gd->bd)
27 */
28DECLARE_GLOBAL_DATA_PTR;
29
54841ab5 30static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
dbaf07ce 31static int fdt_print(const char *pathp, char *prop, int depth);
bc80295b 32static int is_printable_string(const void *data, int len);
781e09ee 33
ae9e97fa
GVB
34/*
35 * The working_fdt points to our working flattened device tree.
36 */
37struct fdt_header *working_fdt;
38
90fbee3e 39void set_working_fdt_addr(ulong addr)
54f9c866 40{
a92fd657
SG
41 void *buf;
42
baf41410 43 printf("Working FDT set to %lx\n", addr);
90fbee3e 44 buf = map_sysmem(addr, 0);
a92fd657 45 working_fdt = buf;
018f5303 46 env_set_hex("fdtaddr", addr);
54f9c866
KG
47}
48
bc80295b
JH
49/*
50 * Get a value from the fdt and format it to be set in the environment
51 */
13982ced
MV
52static int fdt_value_env_set(const void *nodep, int len,
53 const char *var, int index)
bc80295b 54{
13982ced
MV
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 */
7dfcf2a5
MV
63 for (i = 0; i < len; ) {
64 if (index-- > 0) {
65 i += strlen(nodec) + 1;
66 nodec += strlen(nodec) + 1;
13982ced 67 continue;
7dfcf2a5 68 }
13982ced 69
7dfcf2a5 70 env_set(var, nodec);
13982ced
MV
71 return 0;
72 }
73
74 return 1;
75 } else if (len == 4) {
bc80295b
JH
76 char buf[11];
77
b05bf6c7 78 sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep));
382bee57 79 env_set(var, buf);
bc80295b
JH
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));
382bee57 88 env_set(var, buf);
bc80295b
JH
89 } else {
90 printf("error: unprintable value\n");
91 return 1;
92 }
93 return 0;
94}
95
8244127d
HS
96static 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
09140113 109static int fdt_get_header_value(int argc, char *const argv[])
8244127d
HS
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
781e09ee
GVB
130/*
131 * Flattened Device Tree command, see the help for parameter definitions.
132 */
09140113 133static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
781e09ee 134{
47e26b1b 135 if (argc < 2)
4c12eeb8 136 return CMD_RET_USAGE;
781e09ee 137
0c929631 138 /* fdt addr: Set the address of the fdt */
f0ed68e2 139 if (strncmp(argv[1], "ad", 2) == 0) {
54f9c866 140 unsigned long addr;
4b578655 141 int control = 0;
e9496ec3 142 int quiet = 0;
4b578655 143 struct fdt_header *blob;
0c929631
SG
144
145 /* Set the address [and length] of the fdt */
4b578655
SG
146 argc -= 2;
147 argv += 2;
e9496ec3
PH
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 }
4b578655
SG
163 argc--;
164 argv++;
165 }
4b578655
SG
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))
7dbc38ad 172 return 1;
b29a0dbd
SG
173 printf("%s fdt: %08lx\n",
174 control ? "Control" : "Working",
c71a0164 175 control ? (ulong)map_to_sysmem(blob) :
b29a0dbd 176 env_get_hex("fdtaddr", 0));
7dbc38ad
KG
177 return 0;
178 }
179
7e5f460e 180 addr = hextoul(argv[0], NULL);
a92fd657 181 blob = map_sysmem(addr, 0);
e9496ec3
PH
182 if ((quiet && fdt_check_header(blob)) ||
183 (!quiet && !fdt_valid(&blob)))
781e09ee 184 return 1;
4b578655
SG
185 if (control)
186 gd->fdt_blob = blob;
187 else
90fbee3e 188 set_working_fdt_addr(addr);
781e09ee 189
4b578655 190 if (argc >= 2) {
781e09ee
GVB
191 int len;
192 int err;
0c929631
SG
193
194 /* Optional new length */
7e5f460e 195 len = hextoul(argv[1], NULL);
4b578655 196 if (len < fdt_totalsize(blob)) {
e9496ec3
PH
197 if (!quiet)
198 printf("New length %d < existing length %d, ignoring\n",
199 len, fdt_totalsize(blob));
781e09ee 200 } else {
0c929631 201 /* Open in place with a new length */
4b578655 202 err = fdt_open_into(blob, blob, len);
e9496ec3 203 if (!quiet && err != 0) {
0c929631
SG
204 printf("libfdt fdt_open_into(): %s\n",
205 fdt_strerror(err));
781e09ee
GVB
206 }
207 }
208 }
209
e02c9458 210 return CMD_RET_SUCCESS;
e02c9458 211
47e26b1b 212 /*
e489b9c0 213 * Move the working_fdt
47e26b1b 214 */
4ee85df9 215 } else if (strncmp(argv[1], "mo", 2) == 0) {
781e09ee
GVB
216 struct fdt_header *newaddr;
217 int len;
218 int err;
219
47e26b1b 220 if (argc < 4)
4c12eeb8 221 return CMD_RET_USAGE;
781e09ee
GVB
222
223 /*
224 * Set the address and length of the fdt.
225 */
64597346 226 working_fdt = map_sysmem(hextoul(argv[2], NULL), 0);
d14da913 227 if (!fdt_valid(&working_fdt))
781e09ee 228 return 1;
781e09ee 229
64597346 230 newaddr = map_sysmem(hextoul(argv[3], NULL), 0);
6be07cc1
GVB
231
232 /*
233 * If the user specifies a length, use that. Otherwise use the
234 * current length.
235 */
236 if (argc <= 4) {
e489b9c0 237 len = fdt_totalsize(working_fdt);
6be07cc1 238 } else {
7e5f460e 239 len = hextoul(argv[4], NULL);
e489b9c0 240 if (len < fdt_totalsize(working_fdt)) {
addd8ce8
GVB
241 printf ("New length 0x%X < existing length "
242 "0x%X, aborting.\n",
e489b9c0 243 len, fdt_totalsize(working_fdt));
6be07cc1
GVB
244 return 1;
245 }
781e09ee
GVB
246 }
247
248 /*
249 * Copy to the new location.
250 */
e489b9c0 251 err = fdt_open_into(working_fdt, newaddr, len);
781e09ee 252 if (err != 0) {
addd8ce8
GVB
253 printf ("libfdt fdt_open_into(): %s\n",
254 fdt_strerror(err));
781e09ee
GVB
255 return 1;
256 }
64597346 257 set_working_fdt_addr(map_to_sysmem(newaddr));
4ee85df9
AP
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
f7f191ee
FP
269#ifdef CONFIG_OF_SYSTEM_SETUP
270 /* Call the board-specific fixup routine */
4ee85df9 271 if (strncmp(argv[1], "sys", 3) == 0) {
f7f191ee 272 int err = ft_system_setup(working_fdt, gd->bd);
781e09ee 273
f7f191ee
FP
274 if (err) {
275 printf("Failed to add system information to FDT: %s\n",
276 fdt_strerror(err));
277 return CMD_RET_FAILURE;
278 }
4ee85df9
AP
279
280 return CMD_RET_SUCCESS;
281 }
f7f191ee 282#endif
47e26b1b 283 /*
25114033 284 * Make a new node
47e26b1b 285 */
4ee85df9 286 if (strncmp(argv[1], "mk", 2) == 0) {
25114033
GVB
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 */
47e26b1b 295 if (argc < 4)
4c12eeb8 296 return CMD_RET_USAGE;
25114033
GVB
297
298 pathp = argv[2];
299 nodep = argv[3];
300
e489b9c0 301 nodeoffset = fdt_path_offset (working_fdt, pathp);
25114033
GVB
302 if (nodeoffset < 0) {
303 /*
304 * Not found or something else bad happened.
305 */
8d04f02f 306 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 307 fdt_strerror(nodeoffset));
25114033
GVB
308 return 1;
309 }
e489b9c0 310 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
25114033 311 if (err < 0) {
addd8ce8
GVB
312 printf ("libfdt fdt_add_subnode(): %s\n",
313 fdt_strerror(err));
25114033
GVB
314 return 1;
315 }
316
47e26b1b 317 /*
e489b9c0 318 * Set the value of a property in the working_fdt.
47e26b1b 319 */
0688b758 320 } else if (strncmp(argv[1], "se", 2) == 0) {
781e09ee 321 char *pathp; /* path */
addd8ce8 322 char *prop; /* property */
781e09ee 323 int nodeoffset; /* node offset from libfdt */
6dfd65f8 324 static char data[SCRATCHPAD] __aligned(4);/* property storage */
9620d872 325 const void *ptmp;
addd8ce8
GVB
326 int len; /* new length of the property */
327 int ret; /* return value */
781e09ee
GVB
328
329 /*
ea6d8be1 330 * Parameters: Node path, property, optional value.
781e09ee 331 */
47e26b1b 332 if (argc < 4)
4c12eeb8 333 return CMD_RET_USAGE;
781e09ee
GVB
334
335 pathp = argv[2];
336 prop = argv[3];
781e09ee 337
e489b9c0 338 nodeoffset = fdt_path_offset (working_fdt, pathp);
25114033 339 if (nodeoffset < 0) {
781e09ee 340 /*
25114033 341 * Not found or something else bad happened.
781e09ee 342 */
8d04f02f 343 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 344 fdt_strerror(nodeoffset));
781e09ee 345 return 1;
25114033 346 }
25114033 347
9620d872
HS
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 }
cee8c35d
HS
357 if (ptmp != NULL)
358 memcpy(data, ptmp, len);
359
9620d872
HS
360 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
361 if (ret != 0)
362 return ret;
363 }
364
e489b9c0 365 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
25114033
GVB
366 if (ret < 0) {
367 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
368 return 1;
781e09ee
GVB
369 }
370
bc80295b
JH
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)) {
7e5f460e 409 int req_index = -1;
bc80295b
JH
410 int startDepth = fdt_node_depth(
411 working_fdt, nodeoffset);
412 int curDepth = startDepth;
7e5f460e 413 int cur_index = -1;
bc80295b
JH
414 int nextNodeOffset = fdt_next_node(
415 working_fdt, nodeoffset, &curDepth);
416
417 if (subcmd[0] == 'n')
7e5f460e 418 req_index = hextoul(argv[5], NULL);
bc80295b
JH
419
420 while (curDepth > startDepth) {
421 if (curDepth == startDepth + 1)
7e5f460e
SG
422 cur_index++;
423 if (subcmd[0] == 'n' &&
424 cur_index == req_index) {
382bee57 425 const char *node_name;
bc80295b 426
382bee57
SG
427 node_name = fdt_get_name(working_fdt,
428 nextNodeOffset,
429 NULL);
430 env_set(var, node_name);
bc80295b
JH
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 */
7e5f460e 440 env_set_ulong(var, cur_index + 1);
bc80295b
JH
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);
45d20f55 449 if (nodep && len >= 0) {
bc80295b 450 if (subcmd[0] == 'v') {
13982ced 451 int index = 0;
bc80295b
JH
452 int ret;
453
45d20f55
MV
454 if (len == 0) {
455 /* no property value */
456 env_set(var, "");
457 return 0;
458 }
459
13982ced
MV
460 if (argc == 7)
461 index = simple_strtoul(argv[6], NULL, 10);
462
382bee57 463 ret = fdt_value_env_set(nodep, len,
13982ced 464 var, index);
bc80295b
JH
465 if (ret != 0)
466 return ret;
467 } else if (subcmd[0] == 'a') {
468 /* Get address */
469 char buf[11];
470
085b9c3a 471 sprintf(buf, "0x%p", nodep);
382bee57 472 env_set(var, buf);
bc80295b
JH
473 } else if (subcmd[0] == 's') {
474 /* Get size */
475 char buf[11];
476
477 sprintf(buf, "0x%08X", len);
382bee57 478 env_set(var, buf);
bc80295b
JH
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
47e26b1b 489 /*
781e09ee 490 * Print (recursive) / List (single level)
47e26b1b 491 */
25114033 492 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
781e09ee
GVB
493 int depth = MAX_LEVEL; /* how deep to print */
494 char *pathp; /* path */
addd8ce8
GVB
495 char *prop; /* property */
496 int ret; /* return value */
f738b4a7 497 static char root[2] = "/";
781e09ee
GVB
498
499 /*
500 * list is an alias for print, but limited to 1 level
501 */
25114033 502 if (argv[1][0] == 'l') {
781e09ee
GVB
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 */
f738b4a7
KG
510 if (argc == 2)
511 pathp = root;
512 else
513 pathp = argv[2];
781e09ee
GVB
514 if (argc > 3)
515 prop = argv[3];
516 else
517 prop = NULL;
518
addd8ce8
GVB
519 ret = fdt_print(pathp, prop, depth);
520 if (ret != 0)
521 return ret;
781e09ee 522
47e26b1b 523 /*
781e09ee 524 * Remove a property/node
47e26b1b 525 */
2fb698bf 526 } else if (strncmp(argv[1], "rm", 2) == 0) {
781e09ee
GVB
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 */
e489b9c0 534 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
25114033
GVB
535 if (nodeoffset < 0) {
536 /*
537 * Not found or something else bad happened.
538 */
8d04f02f 539 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 540 fdt_strerror(nodeoffset));
25114033 541 return 1;
781e09ee
GVB
542 }
543 /*
544 * Do the delete. A fourth parameter means delete a property,
545 * otherwise delete the node.
546 */
547 if (argc > 3) {
e489b9c0 548 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
781e09ee 549 if (err < 0) {
9597637f 550 printf("libfdt fdt_delprop(): %s\n",
addd8ce8 551 fdt_strerror(err));
9597637f 552 return CMD_RET_FAILURE;
781e09ee
GVB
553 }
554 } else {
e489b9c0 555 err = fdt_del_node(working_fdt, nodeoffset);
781e09ee 556 if (err < 0) {
9597637f 557 printf("libfdt fdt_del_node(): %s\n",
addd8ce8 558 fdt_strerror(err));
9597637f 559 return CMD_RET_FAILURE;
781e09ee
GVB
560 }
561 }
804887e6 562
47e26b1b 563 /*
804887e6 564 * Display header info
47e26b1b 565 */
804887e6 566 } else if (argv[1][0] == 'h') {
8244127d
HS
567 if (argc == 5)
568 return fdt_get_header_value(argc, argv);
569
e489b9c0
KP
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));
804887e6 580 printf("version:\t\t%d\n", version);
e489b9c0
KP
581 printf("last_comp_version:\t%d\n",
582 fdt_last_comp_version(working_fdt));
804887e6
KG
583 if (version >= 2)
584 printf("boot_cpuid_phys:\t0x%x\n",
e489b9c0 585 fdt_boot_cpuid_phys(working_fdt));
804887e6
KG
586 if (version >= 3)
587 printf("size_dt_strings:\t0x%x\n",
e489b9c0 588 fdt_size_dt_strings(working_fdt));
804887e6
KG
589 if (version >= 17)
590 printf("size_dt_struct:\t\t0x%x\n",
e489b9c0
KP
591 fdt_size_dt_struct(working_fdt));
592 printf("number mem_rsv:\t\t0x%x\n",
593 fdt_num_mem_rsv(working_fdt));
804887e6
KG
594 printf("\n");
595
47e26b1b 596 /*
804887e6 597 * Set boot cpu id
47e26b1b 598 */
2fb698bf 599 } else if (strncmp(argv[1], "boo", 3) == 0) {
7e5f460e 600 unsigned long tmp = hextoul(argv[2], NULL);
e489b9c0 601 fdt_set_boot_cpuid_phys(working_fdt, tmp);
804887e6 602
47e26b1b 603 /*
804887e6 604 * memory command
47e26b1b 605 */
2fb698bf 606 } else if (strncmp(argv[1], "me", 2) == 0) {
804887e6
KG
607 uint64_t addr, size;
608 int err;
4b142feb
HS
609 addr = simple_strtoull(argv[2], NULL, 16);
610 size = simple_strtoull(argv[3], NULL, 16);
e489b9c0 611 err = fdt_fixup_memory(working_fdt, addr, size);
804887e6
KG
612 if (err < 0)
613 return err;
614
47e26b1b 615 /*
804887e6 616 * mem reserve commands
47e26b1b 617 */
2fb698bf 618 } else if (strncmp(argv[1], "rs", 2) == 0) {
804887e6
KG
619 if (argv[2][0] == 'p') {
620 uint64_t addr, size;
e489b9c0 621 int total = fdt_num_mem_rsv(working_fdt);
804887e6
KG
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++) {
e489b9c0 627 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
804887e6
KG
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;
804887e6
KG
642 addr = simple_strtoull(argv[3], NULL, 16);
643 size = simple_strtoull(argv[4], NULL, 16);
e489b9c0 644 err = fdt_add_mem_rsv(working_fdt, addr, size);
804887e6
KG
645
646 if (err < 0) {
778c7ab5 647 printf("libfdt fdt_add_mem_rsv(): %s\n",
804887e6 648 fdt_strerror(err));
778c7ab5 649 return CMD_RET_FAILURE;
804887e6
KG
650 }
651 } else if (argv[2][0] == 'd') {
7e5f460e 652 unsigned long idx = hextoul(argv[3], NULL);
e489b9c0 653 int err = fdt_del_mem_rsv(working_fdt, idx);
804887e6
KG
654
655 if (err < 0) {
778c7ab5 656 printf("libfdt fdt_del_mem_rsv(): %s\n",
804887e6 657 fdt_strerror(err));
778c7ab5 658 return CMD_RET_FAILURE;
804887e6
KG
659 }
660 } else {
661 /* Unrecognized command */
4c12eeb8 662 return CMD_RET_USAGE;
804887e6 663 }
99dffca3 664 }
fd61e55d 665#ifdef CONFIG_OF_BOARD_SETUP
99dffca3 666 /* Call the board-specific fixup routine */
4ba98dc2
SG
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 }
f899cc14 675#ifdef CONFIG_ARCH_KEYSTONE
2c76d311
NF
676 ft_board_setup_ex(working_fdt, gd->bd);
677#endif
4ba98dc2 678 }
fd61e55d 679#endif
99dffca3 680 /* Create a chosen node */
097dd3e0 681 else if (strncmp(argv[1], "cho", 3) == 0) {
f953d99f
KG
682 unsigned long initrd_start = 0, initrd_end = 0;
683
47e26b1b 684 if ((argc != 2) && (argc != 4))
4c12eeb8 685 return CMD_RET_USAGE;
f953d99f
KG
686
687 if (argc == 4) {
7e5f460e 688 initrd_start = hextoul(argv[2], NULL);
dbf6f7c9 689 initrd_end = initrd_start + hextoul(argv[3], NULL) - 1;
f953d99f
KG
690 }
691
bc6ed0f9 692 fdt_chosen(working_fdt);
dbe963ae 693 fdt_initrd(working_fdt, initrd_start, initrd_end);
097dd3e0
HS
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) {
7e5f460e 706 addr = hextoul(argv[2], NULL);
097dd3e0
HS
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);
12df2abe 723 if (ret == 0)
097dd3e0
HS
724 return CMD_RET_SUCCESS;
725 else
726 return CMD_RET_FAILURE;
727#endif
728
40afac22 729 }
e6628ad7
MR
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;
082b1414 735 int ret;
e6628ad7
MR
736
737 if (argc != 3)
738 return CMD_RET_USAGE;
739
740 if (!working_fdt)
741 return CMD_RET_FAILURE;
742
7e5f460e 743 addr = hextoul(argv[2], NULL);
e6628ad7
MR
744 blob = map_sysmem(addr, 0);
745 if (!fdt_valid(&blob))
746 return CMD_RET_FAILURE;
747
81ecc5d9
PA
748 /* apply method prints messages on error */
749 ret = fdt_overlay_apply_verbose(working_fdt, blob);
750 if (ret)
e6628ad7
MR
751 return CMD_RET_FAILURE;
752 }
753#endif
40afac22
KG
754 /* resize the fdt */
755 else if (strncmp(argv[1], "re", 2) == 0) {
ef476836
HS
756 uint extrasize;
757 if (argc > 2)
7e5f460e 758 extrasize = hextoul(argv[2], NULL);
ef476836
HS
759 else
760 extrasize = 0;
761 fdt_shrink_to_minimum(working_fdt, extrasize);
40afac22
KG
762 }
763 else {
99dffca3 764 /* Unrecognized command */
4c12eeb8 765 return CMD_RET_USAGE;
781e09ee
GVB
766 }
767
768 return 0;
769}
770
addd8ce8 771/****************************************************************************/
781e09ee 772
781e09ee 773/*
addd8ce8 774 * Parse the user's input, partially heuristic. Valid formats:
4abd844d 775 * <0x00112233 4 05> - an array of cells. Numbers follow standard
53677ef1 776 * C conventions.
addd8ce8
GVB
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.
4abd844d 781 * newval: An array of strings containing the new property as specified
53677ef1 782 * on the command line
4abd844d
AF
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
addd8ce8 786 */
54841ab5 787static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
addd8ce8
GVB
788{
789 char *cp; /* temporary char pointer */
4abd844d 790 char *newp; /* temporary newval char pointer */
addd8ce8 791 unsigned long tmp; /* holds converted values */
4abd844d 792 int stridx = 0;
addd8ce8 793
4abd844d
AF
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);
9620d872
HS
812 if (*cp != '?')
813 *(fdt32_t *)data = cpu_to_fdt32(tmp);
814 else
815 newp++;
816
4abd844d
AF
817 data += 4;
818 *len += 4;
819
820 /* If the ptr didn't advance, something went wrong */
821 if ((newp - cp) <= 0) {
addd8ce8
GVB
822 printf("Sorry, I could not convert \"%s\"\n",
823 cp);
824 return 1;
825 }
4abd844d
AF
826
827 while (*newp == ' ')
828 newp++;
addd8ce8 829 }
4abd844d
AF
830
831 if (*newp != '>') {
832 printf("Unexpected character '%c'\n", *newp);
addd8ce8
GVB
833 return 1;
834 }
4abd844d 835 } else if (*newp == '[') {
addd8ce8
GVB
836 /*
837 * Byte stream. Convert the values.
838 */
4abd844d 839 newp++;
6e748ea0 840 while ((stridx < count) && (*newp != ']')) {
4abd844d
AF
841 while (*newp == ' ')
842 newp++;
6e748ea0 843 if (*newp == '\0') {
4abd844d 844 newp = newval[++stridx];
6e748ea0
KM
845 continue;
846 }
847 if (!isxdigit(*newp))
848 break;
7e5f460e 849 tmp = hextoul(newp, &newp);
6e748ea0
KM
850 *data++ = tmp & 0xFF;
851 *len = *len + 1;
addd8ce8 852 }
4abd844d 853 if (*newp != ']') {
dc4b0b38 854 printf("Unexpected character '%c'\n", *newp);
addd8ce8
GVB
855 return 1;
856 }
857 } else {
858 /*
6e748ea0
KM
859 * Assume it is one or more strings. Copy it into our
860 * data area for convenience (including the
861 * terminating '\0's).
addd8ce8 862 */
4abd844d 863 while (stridx < count) {
6e748ea0 864 size_t length = strlen(newp) + 1;
4abd844d 865 strcpy(data, newp);
6e748ea0
KM
866 data += length;
867 *len += length;
4abd844d
AF
868 newp = newval[++stridx];
869 }
addd8ce8
GVB
870 }
871 return 0;
872}
873
874/****************************************************************************/
875
876/*
877 * Heuristic to guess if this is a string or concatenated strings.
781e09ee
GVB
878 */
879
880static int is_printable_string(const void *data, int len)
881{
882 const char *s = data;
56915fa4 883 const char *ss, *se;
781e09ee
GVB
884
885 /* zero length is not */
886 if (len == 0)
887 return 0;
888
56915fa4
MV
889 /* must terminate with zero */
890 if (s[len - 1] != '\0')
781e09ee
GVB
891 return 0;
892
56915fa4
MV
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
781e09ee 904 s++;
781e09ee
GVB
905 }
906
781e09ee
GVB
907 return 1;
908}
909
addd8ce8
GVB
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 */
781e09ee
GVB
915static void print_data(const void *data, int len)
916{
917 int j;
43913f01
HS
918 const char *env_max_dump;
919 ulong max_dump = ULONG_MAX;
781e09ee
GVB
920
921 /* no data, don't print */
922 if (len == 0)
923 return;
924
43913f01
HS
925 env_max_dump = env_get("fdt_max_dump");
926 if (env_max_dump)
7e5f460e 927 max_dump = hextoul(env_max_dump, NULL);
43913f01 928
781e09ee
GVB
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
4abd844d 946 if ((len %4) == 0) {
43913f01 947 if (len > max_dump)
085b9c3a 948 printf("* 0x%p [0x%08x]", data, len);
f0a29d43 949 else {
088f1b19 950 const __be32 *p;
f0a29d43
JH
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 }
4abd844d 958 } else { /* anything else... hexdump */
43913f01 959 if (len > max_dump)
085b9c3a 960 printf("* 0x%p [0x%08x]", data, len);
f0a29d43
JH
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 }
781e09ee
GVB
969 }
970}
971
addd8ce8
GVB
972/****************************************************************************/
973
974/*
e489b9c0 975 * Recursively print (a portion of) the working_fdt. The depth parameter
addd8ce8
GVB
976 * determines how deeply nested the fdt is printed.
977 */
dbaf07ce 978static int fdt_print(const char *pathp, char *prop, int depth)
addd8ce8 979{
addd8ce8
GVB
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";
dbaf07ce 983 const void *nodep; /* property node pointer */
addd8ce8
GVB
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 */
91623528 989 const struct fdt_property *fdt_prop;
addd8ce8 990
e489b9c0 991 nodeoffset = fdt_path_offset (working_fdt, pathp);
addd8ce8
GVB
992 if (nodeoffset < 0) {
993 /*
994 * Not found or something else bad happened.
995 */
8d04f02f 996 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 997 fdt_strerror(nodeoffset));
addd8ce8
GVB
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) {
e489b9c0 1005 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
addd8ce8
GVB
1006 if (len == 0) {
1007 /* no property value */
1008 printf("%s %s\n", pathp, prop);
1009 return 0;
9f952672 1010 } else if (nodep && len > 0) {
28f384b1 1011 printf("%s = ", prop);
addd8ce8
GVB
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 */
addd8ce8 1026 while(level >= 0) {
e489b9c0 1027 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
addd8ce8
GVB
1028 switch(tag) {
1029 case FDT_BEGIN_NODE:
e489b9c0 1030 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
91623528
GVB
1031 if (level <= depth) {
1032 if (pathp == NULL)
1033 pathp = "/* NULL pointer error */";
1034 if (*pathp == '\0')
1035 pathp = "/"; /* root is nameless */
addd8ce8
GVB
1036 printf("%s%s {\n",
1037 &tabs[MAX_LEVEL - level], pathp);
91623528 1038 }
addd8ce8 1039 level++;
addd8ce8 1040 if (level >= MAX_LEVEL) {
91623528 1041 printf("Nested too deep, aborting.\n");
addd8ce8
GVB
1042 return 1;
1043 }
1044 break;
1045 case FDT_END_NODE:
1046 level--;
91623528 1047 if (level <= depth)
addd8ce8
GVB
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:
e489b9c0 1054 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
91623528 1055 sizeof(*fdt_prop));
e489b9c0 1056 pathp = fdt_string(working_fdt,
91623528
GVB
1057 fdt32_to_cpu(fdt_prop->nameoff));
1058 len = fdt32_to_cpu(fdt_prop->len);
1059 nodep = fdt_prop->data;
addd8ce8
GVB
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 */
91623528 1066 if (level <= depth)
addd8ce8
GVB
1067 printf("%s%s;\n",
1068 &tabs[MAX_LEVEL - level],
1069 pathp);
1070 } else {
91623528 1071 if (level <= depth) {
28f384b1 1072 printf("%s%s = ",
addd8ce8
GVB
1073 &tabs[MAX_LEVEL - level],
1074 pathp);
1075 print_data (nodep, len);
1076 printf(";\n");
1077 }
1078 }
1079 break;
1080 case FDT_NOP:
dc4b0b38 1081 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
addd8ce8
GVB
1082 break;
1083 case FDT_END:
1084 return 1;
1085 default:
91623528 1086 if (level <= depth)
addd8ce8
GVB
1087 printf("Unknown tag 0x%08X\n", tag);
1088 return 1;
1089 }
1090 nodeoffset = nextoffset;
1091 }
1092 return 0;
1093}
1094
781e09ee 1095/********************************************************************/
088f1b19
KP
1096#ifdef CONFIG_SYS_LONGHELP
1097static char fdt_help_text[] =
e4269636 1098 "addr [-c] [-q] <addr> [<size>] - Set the [control] fdt location to <addr>\n"
e6628ad7
MR
1099#ifdef CONFIG_OF_LIBFDT_OVERLAY
1100 "fdt apply <addr> - Apply overlay to the DT\n"
1101#endif
fd61e55d
GVB
1102#ifdef CONFIG_OF_BOARD_SETUP
1103 "fdt boardsetup - Do board-specific set up\n"
c654b517
SG
1104#endif
1105#ifdef CONFIG_OF_SYSTEM_SETUP
1106 "fdt systemsetup - Do system-specific set up\n"
fd61e55d 1107#endif
238cb7a4 1108 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
ef476836 1109 "fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n"
781e09ee
GVB
1110 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
1111 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
13982ced
MV
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"
bc80295b
JH
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"
781e09ee
GVB
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"
8244127d
HS
1121 "fdt header [get <var> <member>] - Display header info\n"
1122 " get - get header member <member> and store it in <var>\n"
804887e6
KG
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"
dbf6f7c9
SA
1128 "fdt chosen [<start> <size>] - Add/update the /chosen branch in the tree\n"
1129 " <start>/<size> - initrd start addr/size\n"
097dd3e0
HS
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
1cc0a9f4 1135 "NOTE: Dereference aliases by omitting the leading '/', "
088f1b19
KP
1136 "e.g. fdt print ethernet0.";
1137#endif
1138
1139U_BOOT_CMD(
1140 fdt, 255, 0, do_fdt,
1141 "flattened device tree utility commands", fdt_help_text
781e09ee 1142);
This page took 0.73123 seconds and 4 git commands to generate.