]> Git Repo - J-u-boot.git/blame - cmd/fdt.c
cmd: fdt: move: Use map_sysmem to convert pointers
[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
MV
210 return CMD_RET_SUCCESS;
211 }
212
213 if (!working_fdt) {
0c929631
SG
214 puts("No FDT memory address configured. Please configure\n"
215 "the FDT address via \"fdt addr <address>\" command.\n"
216 "Aborting!\n");
e02c9458
MV
217 return CMD_RET_FAILURE;
218 }
219
47e26b1b 220 /*
e489b9c0 221 * Move the working_fdt
47e26b1b 222 */
e02c9458 223 if (strncmp(argv[1], "mo", 2) == 0) {
781e09ee
GVB
224 struct fdt_header *newaddr;
225 int len;
226 int err;
227
47e26b1b 228 if (argc < 4)
4c12eeb8 229 return CMD_RET_USAGE;
781e09ee
GVB
230
231 /*
232 * Set the address and length of the fdt.
233 */
64597346 234 working_fdt = map_sysmem(hextoul(argv[2], NULL), 0);
d14da913 235 if (!fdt_valid(&working_fdt))
781e09ee 236 return 1;
781e09ee 237
64597346 238 newaddr = map_sysmem(hextoul(argv[3], NULL), 0);
6be07cc1
GVB
239
240 /*
241 * If the user specifies a length, use that. Otherwise use the
242 * current length.
243 */
244 if (argc <= 4) {
e489b9c0 245 len = fdt_totalsize(working_fdt);
6be07cc1 246 } else {
7e5f460e 247 len = hextoul(argv[4], NULL);
e489b9c0 248 if (len < fdt_totalsize(working_fdt)) {
addd8ce8
GVB
249 printf ("New length 0x%X < existing length "
250 "0x%X, aborting.\n",
e489b9c0 251 len, fdt_totalsize(working_fdt));
6be07cc1
GVB
252 return 1;
253 }
781e09ee
GVB
254 }
255
256 /*
257 * Copy to the new location.
258 */
e489b9c0 259 err = fdt_open_into(working_fdt, newaddr, len);
781e09ee 260 if (err != 0) {
addd8ce8
GVB
261 printf ("libfdt fdt_open_into(): %s\n",
262 fdt_strerror(err));
781e09ee
GVB
263 return 1;
264 }
64597346 265 set_working_fdt_addr(map_to_sysmem(newaddr));
f7f191ee
FP
266#ifdef CONFIG_OF_SYSTEM_SETUP
267 /* Call the board-specific fixup routine */
268 } else if (strncmp(argv[1], "sys", 3) == 0) {
269 int err = ft_system_setup(working_fdt, gd->bd);
781e09ee 270
f7f191ee
FP
271 if (err) {
272 printf("Failed to add system information to FDT: %s\n",
273 fdt_strerror(err));
274 return CMD_RET_FAILURE;
275 }
276#endif
47e26b1b 277 /*
25114033 278 * Make a new node
47e26b1b 279 */
2fb698bf 280 } else if (strncmp(argv[1], "mk", 2) == 0) {
25114033
GVB
281 char *pathp; /* path */
282 char *nodep; /* new node to add */
283 int nodeoffset; /* node offset from libfdt */
284 int err;
285
286 /*
287 * Parameters: Node path, new node to be appended to the path.
288 */
47e26b1b 289 if (argc < 4)
4c12eeb8 290 return CMD_RET_USAGE;
25114033
GVB
291
292 pathp = argv[2];
293 nodep = argv[3];
294
e489b9c0 295 nodeoffset = fdt_path_offset (working_fdt, pathp);
25114033
GVB
296 if (nodeoffset < 0) {
297 /*
298 * Not found or something else bad happened.
299 */
8d04f02f 300 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 301 fdt_strerror(nodeoffset));
25114033
GVB
302 return 1;
303 }
e489b9c0 304 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
25114033 305 if (err < 0) {
addd8ce8
GVB
306 printf ("libfdt fdt_add_subnode(): %s\n",
307 fdt_strerror(err));
25114033
GVB
308 return 1;
309 }
310
47e26b1b 311 /*
e489b9c0 312 * Set the value of a property in the working_fdt.
47e26b1b 313 */
0688b758 314 } else if (strncmp(argv[1], "se", 2) == 0) {
781e09ee 315 char *pathp; /* path */
addd8ce8 316 char *prop; /* property */
781e09ee 317 int nodeoffset; /* node offset from libfdt */
6dfd65f8 318 static char data[SCRATCHPAD] __aligned(4);/* property storage */
9620d872 319 const void *ptmp;
addd8ce8
GVB
320 int len; /* new length of the property */
321 int ret; /* return value */
781e09ee
GVB
322
323 /*
ea6d8be1 324 * Parameters: Node path, property, optional value.
781e09ee 325 */
47e26b1b 326 if (argc < 4)
4c12eeb8 327 return CMD_RET_USAGE;
781e09ee
GVB
328
329 pathp = argv[2];
330 prop = argv[3];
781e09ee 331
e489b9c0 332 nodeoffset = fdt_path_offset (working_fdt, pathp);
25114033 333 if (nodeoffset < 0) {
781e09ee 334 /*
25114033 335 * Not found or something else bad happened.
781e09ee 336 */
8d04f02f 337 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 338 fdt_strerror(nodeoffset));
781e09ee 339 return 1;
25114033 340 }
25114033 341
9620d872
HS
342 if (argc == 4) {
343 len = 0;
344 } else {
345 ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len);
346 if (len > SCRATCHPAD) {
347 printf("prop (%d) doesn't fit in scratchpad!\n",
348 len);
349 return 1;
350 }
cee8c35d
HS
351 if (ptmp != NULL)
352 memcpy(data, ptmp, len);
353
9620d872
HS
354 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
355 if (ret != 0)
356 return ret;
357 }
358
e489b9c0 359 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
25114033
GVB
360 if (ret < 0) {
361 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
362 return 1;
781e09ee
GVB
363 }
364
bc80295b
JH
365 /********************************************************************
366 * Get the value of a property in the working_fdt.
367 ********************************************************************/
368 } else if (argv[1][0] == 'g') {
369 char *subcmd; /* sub-command */
370 char *pathp; /* path */
371 char *prop; /* property */
372 char *var; /* variable to store result */
373 int nodeoffset; /* node offset from libfdt */
374 const void *nodep; /* property node pointer */
375 int len = 0; /* new length of the property */
376
377 /*
378 * Parameters: Node path, property, optional value.
379 */
380 if (argc < 5)
381 return CMD_RET_USAGE;
382
383 subcmd = argv[2];
384
385 if (argc < 6 && subcmd[0] != 's')
386 return CMD_RET_USAGE;
387
388 var = argv[3];
389 pathp = argv[4];
390 prop = argv[5];
391
392 nodeoffset = fdt_path_offset(working_fdt, pathp);
393 if (nodeoffset < 0) {
394 /*
395 * Not found or something else bad happened.
396 */
397 printf("libfdt fdt_path_offset() returned %s\n",
398 fdt_strerror(nodeoffset));
399 return 1;
400 }
401
402 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
7e5f460e 403 int req_index = -1;
bc80295b
JH
404 int startDepth = fdt_node_depth(
405 working_fdt, nodeoffset);
406 int curDepth = startDepth;
7e5f460e 407 int cur_index = -1;
bc80295b
JH
408 int nextNodeOffset = fdt_next_node(
409 working_fdt, nodeoffset, &curDepth);
410
411 if (subcmd[0] == 'n')
7e5f460e 412 req_index = hextoul(argv[5], NULL);
bc80295b
JH
413
414 while (curDepth > startDepth) {
415 if (curDepth == startDepth + 1)
7e5f460e
SG
416 cur_index++;
417 if (subcmd[0] == 'n' &&
418 cur_index == req_index) {
382bee57 419 const char *node_name;
bc80295b 420
382bee57
SG
421 node_name = fdt_get_name(working_fdt,
422 nextNodeOffset,
423 NULL);
424 env_set(var, node_name);
bc80295b
JH
425 return 0;
426 }
427 nextNodeOffset = fdt_next_node(
428 working_fdt, nextNodeOffset, &curDepth);
429 if (nextNodeOffset < 0)
430 break;
431 }
432 if (subcmd[0] == 's') {
433 /* get the num nodes at this level */
7e5f460e 434 env_set_ulong(var, cur_index + 1);
bc80295b
JH
435 } else {
436 /* node index not found */
437 printf("libfdt node not found\n");
438 return 1;
439 }
440 } else {
441 nodep = fdt_getprop(
442 working_fdt, nodeoffset, prop, &len);
443 if (len == 0) {
444 /* no property value */
382bee57 445 env_set(var, "");
bc80295b 446 return 0;
72c98ed1 447 } else if (nodep && len > 0) {
bc80295b 448 if (subcmd[0] == 'v') {
13982ced 449 int index = 0;
bc80295b
JH
450 int ret;
451
13982ced
MV
452 if (argc == 7)
453 index = simple_strtoul(argv[6], NULL, 10);
454
382bee57 455 ret = fdt_value_env_set(nodep, len,
13982ced 456 var, index);
bc80295b
JH
457 if (ret != 0)
458 return ret;
459 } else if (subcmd[0] == 'a') {
460 /* Get address */
461 char buf[11];
462
085b9c3a 463 sprintf(buf, "0x%p", nodep);
382bee57 464 env_set(var, buf);
bc80295b
JH
465 } else if (subcmd[0] == 's') {
466 /* Get size */
467 char buf[11];
468
469 sprintf(buf, "0x%08X", len);
382bee57 470 env_set(var, buf);
bc80295b
JH
471 } else
472 return CMD_RET_USAGE;
473 return 0;
474 } else {
475 printf("libfdt fdt_getprop(): %s\n",
476 fdt_strerror(len));
477 return 1;
478 }
479 }
480
47e26b1b 481 /*
781e09ee 482 * Print (recursive) / List (single level)
47e26b1b 483 */
25114033 484 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
781e09ee
GVB
485 int depth = MAX_LEVEL; /* how deep to print */
486 char *pathp; /* path */
addd8ce8
GVB
487 char *prop; /* property */
488 int ret; /* return value */
f738b4a7 489 static char root[2] = "/";
781e09ee
GVB
490
491 /*
492 * list is an alias for print, but limited to 1 level
493 */
25114033 494 if (argv[1][0] == 'l') {
781e09ee
GVB
495 depth = 1;
496 }
497
498 /*
499 * Get the starting path. The root node is an oddball,
500 * the offset is zero and has no name.
501 */
f738b4a7
KG
502 if (argc == 2)
503 pathp = root;
504 else
505 pathp = argv[2];
781e09ee
GVB
506 if (argc > 3)
507 prop = argv[3];
508 else
509 prop = NULL;
510
addd8ce8
GVB
511 ret = fdt_print(pathp, prop, depth);
512 if (ret != 0)
513 return ret;
781e09ee 514
47e26b1b 515 /*
781e09ee 516 * Remove a property/node
47e26b1b 517 */
2fb698bf 518 } else if (strncmp(argv[1], "rm", 2) == 0) {
781e09ee
GVB
519 int nodeoffset; /* node offset from libfdt */
520 int err;
521
522 /*
523 * Get the path. The root node is an oddball, the offset
524 * is zero and has no name.
525 */
e489b9c0 526 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
25114033
GVB
527 if (nodeoffset < 0) {
528 /*
529 * Not found or something else bad happened.
530 */
8d04f02f 531 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 532 fdt_strerror(nodeoffset));
25114033 533 return 1;
781e09ee
GVB
534 }
535 /*
536 * Do the delete. A fourth parameter means delete a property,
537 * otherwise delete the node.
538 */
539 if (argc > 3) {
e489b9c0 540 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
781e09ee 541 if (err < 0) {
addd8ce8
GVB
542 printf("libfdt fdt_delprop(): %s\n",
543 fdt_strerror(err));
781e09ee
GVB
544 return err;
545 }
546 } else {
e489b9c0 547 err = fdt_del_node(working_fdt, nodeoffset);
781e09ee 548 if (err < 0) {
addd8ce8
GVB
549 printf("libfdt fdt_del_node(): %s\n",
550 fdt_strerror(err));
781e09ee
GVB
551 return err;
552 }
553 }
804887e6 554
47e26b1b 555 /*
804887e6 556 * Display header info
47e26b1b 557 */
804887e6 558 } else if (argv[1][0] == 'h') {
8244127d
HS
559 if (argc == 5)
560 return fdt_get_header_value(argc, argv);
561
e489b9c0
KP
562 u32 version = fdt_version(working_fdt);
563 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
564 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
565 fdt_totalsize(working_fdt));
566 printf("off_dt_struct:\t\t0x%x\n",
567 fdt_off_dt_struct(working_fdt));
568 printf("off_dt_strings:\t\t0x%x\n",
569 fdt_off_dt_strings(working_fdt));
570 printf("off_mem_rsvmap:\t\t0x%x\n",
571 fdt_off_mem_rsvmap(working_fdt));
804887e6 572 printf("version:\t\t%d\n", version);
e489b9c0
KP
573 printf("last_comp_version:\t%d\n",
574 fdt_last_comp_version(working_fdt));
804887e6
KG
575 if (version >= 2)
576 printf("boot_cpuid_phys:\t0x%x\n",
e489b9c0 577 fdt_boot_cpuid_phys(working_fdt));
804887e6
KG
578 if (version >= 3)
579 printf("size_dt_strings:\t0x%x\n",
e489b9c0 580 fdt_size_dt_strings(working_fdt));
804887e6
KG
581 if (version >= 17)
582 printf("size_dt_struct:\t\t0x%x\n",
e489b9c0
KP
583 fdt_size_dt_struct(working_fdt));
584 printf("number mem_rsv:\t\t0x%x\n",
585 fdt_num_mem_rsv(working_fdt));
804887e6
KG
586 printf("\n");
587
47e26b1b 588 /*
804887e6 589 * Set boot cpu id
47e26b1b 590 */
2fb698bf 591 } else if (strncmp(argv[1], "boo", 3) == 0) {
7e5f460e 592 unsigned long tmp = hextoul(argv[2], NULL);
e489b9c0 593 fdt_set_boot_cpuid_phys(working_fdt, tmp);
804887e6 594
47e26b1b 595 /*
804887e6 596 * memory command
47e26b1b 597 */
2fb698bf 598 } else if (strncmp(argv[1], "me", 2) == 0) {
804887e6
KG
599 uint64_t addr, size;
600 int err;
4b142feb
HS
601 addr = simple_strtoull(argv[2], NULL, 16);
602 size = simple_strtoull(argv[3], NULL, 16);
e489b9c0 603 err = fdt_fixup_memory(working_fdt, addr, size);
804887e6
KG
604 if (err < 0)
605 return err;
606
47e26b1b 607 /*
804887e6 608 * mem reserve commands
47e26b1b 609 */
2fb698bf 610 } else if (strncmp(argv[1], "rs", 2) == 0) {
804887e6
KG
611 if (argv[2][0] == 'p') {
612 uint64_t addr, size;
e489b9c0 613 int total = fdt_num_mem_rsv(working_fdt);
804887e6
KG
614 int j, err;
615 printf("index\t\t start\t\t size\n");
616 printf("-------------------------------"
617 "-----------------\n");
618 for (j = 0; j < total; j++) {
e489b9c0 619 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
804887e6
KG
620 if (err < 0) {
621 printf("libfdt fdt_get_mem_rsv(): %s\n",
622 fdt_strerror(err));
623 return err;
624 }
625 printf(" %x\t%08x%08x\t%08x%08x\n", j,
626 (u32)(addr >> 32),
627 (u32)(addr & 0xffffffff),
628 (u32)(size >> 32),
629 (u32)(size & 0xffffffff));
630 }
631 } else if (argv[2][0] == 'a') {
632 uint64_t addr, size;
633 int err;
804887e6
KG
634 addr = simple_strtoull(argv[3], NULL, 16);
635 size = simple_strtoull(argv[4], NULL, 16);
e489b9c0 636 err = fdt_add_mem_rsv(working_fdt, addr, size);
804887e6
KG
637
638 if (err < 0) {
639 printf("libfdt fdt_add_mem_rsv(): %s\n",
640 fdt_strerror(err));
641 return err;
642 }
643 } else if (argv[2][0] == 'd') {
7e5f460e 644 unsigned long idx = hextoul(argv[3], NULL);
e489b9c0 645 int err = fdt_del_mem_rsv(working_fdt, idx);
804887e6
KG
646
647 if (err < 0) {
648 printf("libfdt fdt_del_mem_rsv(): %s\n",
649 fdt_strerror(err));
650 return err;
651 }
652 } else {
653 /* Unrecognized command */
4c12eeb8 654 return CMD_RET_USAGE;
804887e6 655 }
99dffca3 656 }
fd61e55d 657#ifdef CONFIG_OF_BOARD_SETUP
99dffca3 658 /* Call the board-specific fixup routine */
4ba98dc2
SG
659 else if (strncmp(argv[1], "boa", 3) == 0) {
660 int err = ft_board_setup(working_fdt, gd->bd);
661
662 if (err) {
663 printf("Failed to update board information in FDT: %s\n",
664 fdt_strerror(err));
665 return CMD_RET_FAILURE;
666 }
f899cc14 667#ifdef CONFIG_ARCH_KEYSTONE
2c76d311
NF
668 ft_board_setup_ex(working_fdt, gd->bd);
669#endif
4ba98dc2 670 }
fd61e55d 671#endif
99dffca3 672 /* Create a chosen node */
097dd3e0 673 else if (strncmp(argv[1], "cho", 3) == 0) {
f953d99f
KG
674 unsigned long initrd_start = 0, initrd_end = 0;
675
47e26b1b 676 if ((argc != 2) && (argc != 4))
4c12eeb8 677 return CMD_RET_USAGE;
f953d99f
KG
678
679 if (argc == 4) {
7e5f460e 680 initrd_start = hextoul(argv[2], NULL);
dbf6f7c9 681 initrd_end = initrd_start + hextoul(argv[3], NULL) - 1;
f953d99f
KG
682 }
683
bc6ed0f9 684 fdt_chosen(working_fdt);
dbe963ae 685 fdt_initrd(working_fdt, initrd_start, initrd_end);
097dd3e0
HS
686
687#if defined(CONFIG_FIT_SIGNATURE)
688 } else if (strncmp(argv[1], "che", 3) == 0) {
689 int cfg_noffset;
690 int ret;
691 unsigned long addr;
692 struct fdt_header *blob;
693
694 if (!working_fdt)
695 return CMD_RET_FAILURE;
696
697 if (argc > 2) {
7e5f460e 698 addr = hextoul(argv[2], NULL);
097dd3e0
HS
699 blob = map_sysmem(addr, 0);
700 } else {
701 blob = (struct fdt_header *)gd->fdt_blob;
702 }
703 if (!fdt_valid(&blob))
704 return 1;
705
706 gd->fdt_blob = blob;
707 cfg_noffset = fit_conf_get_node(working_fdt, NULL);
708 if (!cfg_noffset) {
709 printf("Could not find configuration node: %s\n",
710 fdt_strerror(cfg_noffset));
711 return CMD_RET_FAILURE;
712 }
713
714 ret = fit_config_verify(working_fdt, cfg_noffset);
12df2abe 715 if (ret == 0)
097dd3e0
HS
716 return CMD_RET_SUCCESS;
717 else
718 return CMD_RET_FAILURE;
719#endif
720
40afac22 721 }
e6628ad7
MR
722#ifdef CONFIG_OF_LIBFDT_OVERLAY
723 /* apply an overlay */
724 else if (strncmp(argv[1], "ap", 2) == 0) {
725 unsigned long addr;
726 struct fdt_header *blob;
082b1414 727 int ret;
e6628ad7
MR
728
729 if (argc != 3)
730 return CMD_RET_USAGE;
731
732 if (!working_fdt)
733 return CMD_RET_FAILURE;
734
7e5f460e 735 addr = hextoul(argv[2], NULL);
e6628ad7
MR
736 blob = map_sysmem(addr, 0);
737 if (!fdt_valid(&blob))
738 return CMD_RET_FAILURE;
739
81ecc5d9
PA
740 /* apply method prints messages on error */
741 ret = fdt_overlay_apply_verbose(working_fdt, blob);
742 if (ret)
e6628ad7
MR
743 return CMD_RET_FAILURE;
744 }
745#endif
40afac22
KG
746 /* resize the fdt */
747 else if (strncmp(argv[1], "re", 2) == 0) {
ef476836
HS
748 uint extrasize;
749 if (argc > 2)
7e5f460e 750 extrasize = hextoul(argv[2], NULL);
ef476836
HS
751 else
752 extrasize = 0;
753 fdt_shrink_to_minimum(working_fdt, extrasize);
40afac22
KG
754 }
755 else {
99dffca3 756 /* Unrecognized command */
4c12eeb8 757 return CMD_RET_USAGE;
781e09ee
GVB
758 }
759
760 return 0;
761}
762
addd8ce8 763/****************************************************************************/
781e09ee 764
781e09ee 765/*
addd8ce8 766 * Parse the user's input, partially heuristic. Valid formats:
4abd844d 767 * <0x00112233 4 05> - an array of cells. Numbers follow standard
53677ef1 768 * C conventions.
addd8ce8
GVB
769 * [00 11 22 .. nn] - byte stream
770 * "string" - If the the value doesn't start with "<" or "[", it is
771 * treated as a string. Note that the quotes are
772 * stripped by the parser before we get the string.
4abd844d 773 * newval: An array of strings containing the new property as specified
53677ef1 774 * on the command line
4abd844d
AF
775 * count: The number of strings in the array
776 * data: A bytestream to be placed in the property
777 * len: The length of the resulting bytestream
addd8ce8 778 */
54841ab5 779static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
addd8ce8
GVB
780{
781 char *cp; /* temporary char pointer */
4abd844d 782 char *newp; /* temporary newval char pointer */
addd8ce8 783 unsigned long tmp; /* holds converted values */
4abd844d 784 int stridx = 0;
addd8ce8 785
4abd844d
AF
786 *len = 0;
787 newp = newval[0];
788
789 /* An array of cells */
790 if (*newp == '<') {
791 newp++;
792 while ((*newp != '>') && (stridx < count)) {
793 /*
794 * Keep searching until we find that last ">"
795 * That way users don't have to escape the spaces
796 */
797 if (*newp == '\0') {
798 newp = newval[++stridx];
799 continue;
800 }
801
802 cp = newp;
803 tmp = simple_strtoul(cp, &newp, 0);
9620d872
HS
804 if (*cp != '?')
805 *(fdt32_t *)data = cpu_to_fdt32(tmp);
806 else
807 newp++;
808
4abd844d
AF
809 data += 4;
810 *len += 4;
811
812 /* If the ptr didn't advance, something went wrong */
813 if ((newp - cp) <= 0) {
addd8ce8
GVB
814 printf("Sorry, I could not convert \"%s\"\n",
815 cp);
816 return 1;
817 }
4abd844d
AF
818
819 while (*newp == ' ')
820 newp++;
addd8ce8 821 }
4abd844d
AF
822
823 if (*newp != '>') {
824 printf("Unexpected character '%c'\n", *newp);
addd8ce8
GVB
825 return 1;
826 }
4abd844d 827 } else if (*newp == '[') {
addd8ce8
GVB
828 /*
829 * Byte stream. Convert the values.
830 */
4abd844d 831 newp++;
6e748ea0 832 while ((stridx < count) && (*newp != ']')) {
4abd844d
AF
833 while (*newp == ' ')
834 newp++;
6e748ea0 835 if (*newp == '\0') {
4abd844d 836 newp = newval[++stridx];
6e748ea0
KM
837 continue;
838 }
839 if (!isxdigit(*newp))
840 break;
7e5f460e 841 tmp = hextoul(newp, &newp);
6e748ea0
KM
842 *data++ = tmp & 0xFF;
843 *len = *len + 1;
addd8ce8 844 }
4abd844d 845 if (*newp != ']') {
dc4b0b38 846 printf("Unexpected character '%c'\n", *newp);
addd8ce8
GVB
847 return 1;
848 }
849 } else {
850 /*
6e748ea0
KM
851 * Assume it is one or more strings. Copy it into our
852 * data area for convenience (including the
853 * terminating '\0's).
addd8ce8 854 */
4abd844d 855 while (stridx < count) {
6e748ea0 856 size_t length = strlen(newp) + 1;
4abd844d 857 strcpy(data, newp);
6e748ea0
KM
858 data += length;
859 *len += length;
4abd844d
AF
860 newp = newval[++stridx];
861 }
addd8ce8
GVB
862 }
863 return 0;
864}
865
866/****************************************************************************/
867
868/*
869 * Heuristic to guess if this is a string or concatenated strings.
781e09ee
GVB
870 */
871
872static int is_printable_string(const void *data, int len)
873{
874 const char *s = data;
875
876 /* zero length is not */
877 if (len == 0)
878 return 0;
879
8805beec
JH
880 /* must terminate with zero or '\n' */
881 if (s[len - 1] != '\0' && s[len - 1] != '\n')
781e09ee
GVB
882 return 0;
883
884 /* printable or a null byte (concatenated strings) */
8805beec 885 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
781e09ee
GVB
886 /*
887 * If we see a null, there are three possibilities:
888 * 1) If len == 1, it is the end of the string, printable
889 * 2) Next character also a null, not printable.
890 * 3) Next character not a null, continue to check.
891 */
892 if (s[0] == '\0') {
893 if (len == 1)
894 return 1;
895 if (s[1] == '\0')
896 return 0;
897 }
898 s++;
899 len--;
900 }
901
902 /* Not the null termination, or not done yet: not printable */
903 if (*s != '\0' || (len != 0))
904 return 0;
905
906 return 1;
907}
908
addd8ce8
GVB
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 */
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.824497 seconds and 4 git commands to generate.