]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_fdt.c
fdt: Added resize command
[people/ms/u-boot.git] / common / cmd_fdt.c
CommitLineData
781e09ee
GVB
1/*
2 * (C) Copyright 2007
3 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
4 * Based on code written by:
5 * Pantelis Antoniou <pantelis.antoniou@gmail.com> and
6 * Matthew McClintock <msm@freescale.com>
7 *
8 * See file CREDITS for list of people who contributed to this
9 * project.
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 * MA 02111-1307 USA
25 */
26
27#include <common.h>
28#include <command.h>
29#include <linux/ctype.h>
30#include <linux/types.h>
781e09ee
GVB
31#include <asm/global_data.h>
32#include <fdt.h>
33#include <libfdt.h>
64dbbd40 34#include <fdt_support.h>
781e09ee
GVB
35
36#define MAX_LEVEL 32 /* how deeply nested we will go */
fd61e55d 37#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
781e09ee
GVB
38
39/*
40 * Global data (for the gd->bd)
41 */
42DECLARE_GLOBAL_DATA_PTR;
43
781e09ee 44static int fdt_valid(void);
4abd844d 45static int fdt_parse_prop(char **newval, int count, char *data, int *len);
dbaf07ce 46static int fdt_print(const char *pathp, char *prop, int depth);
781e09ee 47
ae9e97fa
GVB
48/*
49 * The working_fdt points to our working flattened device tree.
50 */
51struct fdt_header *working_fdt;
52
54f9c866
KG
53void set_working_fdt_addr(void *addr)
54{
55 char buf[17];
56
57 working_fdt = addr;
58
59 sprintf(buf, "%lx", (unsigned long)addr);
60 setenv("fdtaddr", buf);
61}
62
781e09ee
GVB
63/*
64 * Flattened Device Tree command, see the help for parameter definitions.
65 */
66int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
67{
781e09ee
GVB
68 if (argc < 2) {
69 printf ("Usage:\n%s\n", cmdtp->usage);
70 return 1;
71 }
72
781e09ee
GVB
73 /********************************************************************
74 * Set the address of the fdt
75 ********************************************************************/
25114033 76 if (argv[1][0] == 'a') {
54f9c866 77 unsigned long addr;
781e09ee
GVB
78 /*
79 * Set the address [and length] of the fdt.
80 */
7dbc38ad
KG
81 if (argc == 2) {
82 if (!fdt_valid()) {
83 return 1;
84 }
85 printf("The address of the fdt is %p\n", working_fdt);
86 return 0;
87 }
88
54f9c866
KG
89 addr = simple_strtoul(argv[2], NULL, 16);
90 set_working_fdt_addr((void *)addr);
781e09ee
GVB
91
92 if (!fdt_valid()) {
93 return 1;
94 }
95
96 if (argc >= 4) {
97 int len;
98 int err;
99 /*
100 * Optional new length
101 */
91623528 102 len = simple_strtoul(argv[3], NULL, 16);
e489b9c0 103 if (len < fdt_totalsize(working_fdt)) {
addd8ce8
GVB
104 printf ("New length %d < existing length %d, "
105 "ignoring.\n",
e489b9c0 106 len, fdt_totalsize(working_fdt));
781e09ee
GVB
107 } else {
108 /*
109 * Open in place with a new length.
110 */
e489b9c0 111 err = fdt_open_into(working_fdt, working_fdt, len);
781e09ee 112 if (err != 0) {
addd8ce8
GVB
113 printf ("libfdt fdt_open_into(): %s\n",
114 fdt_strerror(err));
781e09ee
GVB
115 }
116 }
117 }
118
119 /********************************************************************
e489b9c0 120 * Move the working_fdt
781e09ee 121 ********************************************************************/
2fb698bf 122 } else if (strncmp(argv[1], "mo", 2) == 0) {
781e09ee
GVB
123 struct fdt_header *newaddr;
124 int len;
125 int err;
126
6be07cc1 127 if (argc < 4) {
781e09ee
GVB
128 printf ("Usage:\n%s\n", cmdtp->usage);
129 return 1;
130 }
131
132 /*
133 * Set the address and length of the fdt.
134 */
e489b9c0 135 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
781e09ee
GVB
136 if (!fdt_valid()) {
137 return 1;
138 }
139
addd8ce8 140 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
6be07cc1
GVB
141
142 /*
143 * If the user specifies a length, use that. Otherwise use the
144 * current length.
145 */
146 if (argc <= 4) {
e489b9c0 147 len = fdt_totalsize(working_fdt);
6be07cc1
GVB
148 } else {
149 len = simple_strtoul(argv[4], NULL, 16);
e489b9c0 150 if (len < fdt_totalsize(working_fdt)) {
addd8ce8
GVB
151 printf ("New length 0x%X < existing length "
152 "0x%X, aborting.\n",
e489b9c0 153 len, fdt_totalsize(working_fdt));
6be07cc1
GVB
154 return 1;
155 }
781e09ee
GVB
156 }
157
158 /*
159 * Copy to the new location.
160 */
e489b9c0 161 err = fdt_open_into(working_fdt, newaddr, len);
781e09ee 162 if (err != 0) {
addd8ce8
GVB
163 printf ("libfdt fdt_open_into(): %s\n",
164 fdt_strerror(err));
781e09ee
GVB
165 return 1;
166 }
e489b9c0 167 working_fdt = newaddr;
781e09ee
GVB
168
169 /********************************************************************
25114033
GVB
170 * Make a new node
171 ********************************************************************/
2fb698bf 172 } else if (strncmp(argv[1], "mk", 2) == 0) {
25114033
GVB
173 char *pathp; /* path */
174 char *nodep; /* new node to add */
175 int nodeoffset; /* node offset from libfdt */
176 int err;
177
178 /*
179 * Parameters: Node path, new node to be appended to the path.
180 */
181 if (argc < 4) {
182 printf ("Usage:\n%s\n", cmdtp->usage);
183 return 1;
184 }
185
186 pathp = argv[2];
187 nodep = argv[3];
188
e489b9c0 189 nodeoffset = fdt_path_offset (working_fdt, pathp);
25114033
GVB
190 if (nodeoffset < 0) {
191 /*
192 * Not found or something else bad happened.
193 */
8d04f02f 194 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 195 fdt_strerror(nodeoffset));
25114033
GVB
196 return 1;
197 }
e489b9c0 198 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
25114033 199 if (err < 0) {
addd8ce8
GVB
200 printf ("libfdt fdt_add_subnode(): %s\n",
201 fdt_strerror(err));
25114033
GVB
202 return 1;
203 }
204
205 /********************************************************************
e489b9c0 206 * Set the value of a property in the working_fdt.
781e09ee 207 ********************************************************************/
25114033 208 } else if (argv[1][0] == 's') {
781e09ee 209 char *pathp; /* path */
addd8ce8 210 char *prop; /* property */
781e09ee 211 int nodeoffset; /* node offset from libfdt */
addd8ce8
GVB
212 static char data[SCRATCHPAD]; /* storage for the property */
213 int len; /* new length of the property */
214 int ret; /* return value */
781e09ee
GVB
215
216 /*
ea6d8be1 217 * Parameters: Node path, property, optional value.
781e09ee 218 */
ea6d8be1 219 if (argc < 4) {
781e09ee
GVB
220 printf ("Usage:\n%s\n", cmdtp->usage);
221 return 1;
222 }
223
224 pathp = argv[2];
225 prop = argv[3];
ea6d8be1
GVB
226 if (argc == 4) {
227 len = 0;
228 } else {
4abd844d 229 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
ea6d8be1
GVB
230 if (ret != 0)
231 return ret;
232 }
781e09ee 233
e489b9c0 234 nodeoffset = fdt_path_offset (working_fdt, pathp);
25114033 235 if (nodeoffset < 0) {
781e09ee 236 /*
25114033 237 * Not found or something else bad happened.
781e09ee 238 */
8d04f02f 239 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 240 fdt_strerror(nodeoffset));
781e09ee 241 return 1;
25114033 242 }
25114033 243
e489b9c0 244 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
25114033
GVB
245 if (ret < 0) {
246 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
247 return 1;
781e09ee
GVB
248 }
249
250 /********************************************************************
251 * Print (recursive) / List (single level)
252 ********************************************************************/
25114033 253 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
781e09ee
GVB
254 int depth = MAX_LEVEL; /* how deep to print */
255 char *pathp; /* path */
addd8ce8
GVB
256 char *prop; /* property */
257 int ret; /* return value */
f738b4a7 258 static char root[2] = "/";
781e09ee
GVB
259
260 /*
261 * list is an alias for print, but limited to 1 level
262 */
25114033 263 if (argv[1][0] == 'l') {
781e09ee
GVB
264 depth = 1;
265 }
266
267 /*
268 * Get the starting path. The root node is an oddball,
269 * the offset is zero and has no name.
270 */
f738b4a7
KG
271 if (argc == 2)
272 pathp = root;
273 else
274 pathp = argv[2];
781e09ee
GVB
275 if (argc > 3)
276 prop = argv[3];
277 else
278 prop = NULL;
279
addd8ce8
GVB
280 ret = fdt_print(pathp, prop, depth);
281 if (ret != 0)
282 return ret;
781e09ee
GVB
283
284 /********************************************************************
285 * Remove a property/node
286 ********************************************************************/
2fb698bf 287 } else if (strncmp(argv[1], "rm", 2) == 0) {
781e09ee
GVB
288 int nodeoffset; /* node offset from libfdt */
289 int err;
290
291 /*
292 * Get the path. The root node is an oddball, the offset
293 * is zero and has no name.
294 */
e489b9c0 295 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
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 302 return 1;
781e09ee
GVB
303 }
304 /*
305 * Do the delete. A fourth parameter means delete a property,
306 * otherwise delete the node.
307 */
308 if (argc > 3) {
e489b9c0 309 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
781e09ee 310 if (err < 0) {
addd8ce8
GVB
311 printf("libfdt fdt_delprop(): %s\n",
312 fdt_strerror(err));
781e09ee
GVB
313 return err;
314 }
315 } else {
e489b9c0 316 err = fdt_del_node(working_fdt, nodeoffset);
781e09ee 317 if (err < 0) {
addd8ce8
GVB
318 printf("libfdt fdt_del_node(): %s\n",
319 fdt_strerror(err));
781e09ee
GVB
320 return err;
321 }
322 }
804887e6
KG
323
324 /********************************************************************
325 * Display header info
326 ********************************************************************/
327 } else if (argv[1][0] == 'h') {
e489b9c0
KP
328 u32 version = fdt_version(working_fdt);
329 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
330 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
331 fdt_totalsize(working_fdt));
332 printf("off_dt_struct:\t\t0x%x\n",
333 fdt_off_dt_struct(working_fdt));
334 printf("off_dt_strings:\t\t0x%x\n",
335 fdt_off_dt_strings(working_fdt));
336 printf("off_mem_rsvmap:\t\t0x%x\n",
337 fdt_off_mem_rsvmap(working_fdt));
804887e6 338 printf("version:\t\t%d\n", version);
e489b9c0
KP
339 printf("last_comp_version:\t%d\n",
340 fdt_last_comp_version(working_fdt));
804887e6
KG
341 if (version >= 2)
342 printf("boot_cpuid_phys:\t0x%x\n",
e489b9c0 343 fdt_boot_cpuid_phys(working_fdt));
804887e6
KG
344 if (version >= 3)
345 printf("size_dt_strings:\t0x%x\n",
e489b9c0 346 fdt_size_dt_strings(working_fdt));
804887e6
KG
347 if (version >= 17)
348 printf("size_dt_struct:\t\t0x%x\n",
e489b9c0
KP
349 fdt_size_dt_struct(working_fdt));
350 printf("number mem_rsv:\t\t0x%x\n",
351 fdt_num_mem_rsv(working_fdt));
804887e6
KG
352 printf("\n");
353
354 /********************************************************************
355 * Set boot cpu id
356 ********************************************************************/
2fb698bf 357 } else if (strncmp(argv[1], "boo", 3) == 0) {
804887e6 358 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
e489b9c0 359 fdt_set_boot_cpuid_phys(working_fdt, tmp);
804887e6
KG
360
361 /********************************************************************
362 * memory command
363 ********************************************************************/
2fb698bf 364 } else if (strncmp(argv[1], "me", 2) == 0) {
804887e6
KG
365 uint64_t addr, size;
366 int err;
367#ifdef CFG_64BIT_STRTOUL
368 addr = simple_strtoull(argv[2], NULL, 16);
369 size = simple_strtoull(argv[3], NULL, 16);
370#else
371 addr = simple_strtoul(argv[2], NULL, 16);
372 size = simple_strtoul(argv[3], NULL, 16);
373#endif
e489b9c0 374 err = fdt_fixup_memory(working_fdt, addr, size);
804887e6
KG
375 if (err < 0)
376 return err;
377
378 /********************************************************************
379 * mem reserve commands
380 ********************************************************************/
2fb698bf 381 } else if (strncmp(argv[1], "rs", 2) == 0) {
804887e6
KG
382 if (argv[2][0] == 'p') {
383 uint64_t addr, size;
e489b9c0 384 int total = fdt_num_mem_rsv(working_fdt);
804887e6
KG
385 int j, err;
386 printf("index\t\t start\t\t size\n");
387 printf("-------------------------------"
388 "-----------------\n");
389 for (j = 0; j < total; j++) {
e489b9c0 390 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
804887e6
KG
391 if (err < 0) {
392 printf("libfdt fdt_get_mem_rsv(): %s\n",
393 fdt_strerror(err));
394 return err;
395 }
396 printf(" %x\t%08x%08x\t%08x%08x\n", j,
397 (u32)(addr >> 32),
398 (u32)(addr & 0xffffffff),
399 (u32)(size >> 32),
400 (u32)(size & 0xffffffff));
401 }
402 } else if (argv[2][0] == 'a') {
403 uint64_t addr, size;
404 int err;
405#ifdef CFG_64BIT_STRTOUL
406 addr = simple_strtoull(argv[3], NULL, 16);
407 size = simple_strtoull(argv[4], NULL, 16);
408#else
409 addr = simple_strtoul(argv[3], NULL, 16);
410 size = simple_strtoul(argv[4], NULL, 16);
411#endif
e489b9c0 412 err = fdt_add_mem_rsv(working_fdt, addr, size);
804887e6
KG
413
414 if (err < 0) {
415 printf("libfdt fdt_add_mem_rsv(): %s\n",
416 fdt_strerror(err));
417 return err;
418 }
419 } else if (argv[2][0] == 'd') {
420 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
e489b9c0 421 int err = fdt_del_mem_rsv(working_fdt, idx);
804887e6
KG
422
423 if (err < 0) {
424 printf("libfdt fdt_del_mem_rsv(): %s\n",
425 fdt_strerror(err));
426 return err;
427 }
428 } else {
429 /* Unrecognized command */
430 printf ("Usage:\n%s\n", cmdtp->usage);
431 return 1;
432 }
99dffca3 433 }
fd61e55d 434#ifdef CONFIG_OF_BOARD_SETUP
99dffca3 435 /* Call the board-specific fixup routine */
2fb698bf 436 else if (strncmp(argv[1], "boa", 3) == 0)
e489b9c0 437 ft_board_setup(working_fdt, gd->bd);
fd61e55d 438#endif
99dffca3 439 /* Create a chosen node */
f953d99f
KG
440 else if (argv[1][0] == 'c') {
441 unsigned long initrd_start = 0, initrd_end = 0;
442
443 if ((argc != 2) && (argc != 4)) {
444 printf ("Usage:\n%s\n", cmdtp->usage);
445 return 1;
446 }
447
448 if (argc == 4) {
449 initrd_start = simple_strtoul(argv[2], NULL, 16);
450 initrd_end = simple_strtoul(argv[3], NULL, 16);
451 }
452
453 fdt_chosen(working_fdt, initrd_start, initrd_end, 1);
40afac22
KG
454 }
455 /* resize the fdt */
456 else if (strncmp(argv[1], "re", 2) == 0) {
457 fdt_resize(working_fdt);
458 }
459 else {
99dffca3 460 /* Unrecognized command */
781e09ee
GVB
461 printf ("Usage:\n%s\n", cmdtp->usage);
462 return 1;
463 }
464
465 return 0;
466}
467
addd8ce8 468/****************************************************************************/
781e09ee
GVB
469
470static int fdt_valid(void)
471{
64dbbd40
GVB
472 int err;
473
e489b9c0 474 if (working_fdt == NULL) {
64dbbd40 475 printf ("The address of the fdt is invalid (NULL).\n");
781e09ee
GVB
476 return 0;
477 }
64dbbd40 478
e489b9c0 479 err = fdt_check_header(working_fdt);
64dbbd40
GVB
480 if (err == 0)
481 return 1; /* valid */
482
483 if (err < 0) {
25114033 484 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
64dbbd40
GVB
485 /*
486 * Be more informative on bad version.
487 */
488 if (err == -FDT_ERR_BADVERSION) {
e489b9c0
KP
489 if (fdt_version(working_fdt) <
490 FDT_FIRST_SUPPORTED_VERSION) {
dc4b0b38 491 printf (" - too old, fdt %d < %d",
e489b9c0 492 fdt_version(working_fdt),
addd8ce8 493 FDT_FIRST_SUPPORTED_VERSION);
e489b9c0 494 working_fdt = NULL;
64dbbd40 495 }
e489b9c0
KP
496 if (fdt_last_comp_version(working_fdt) >
497 FDT_LAST_SUPPORTED_VERSION) {
dc4b0b38 498 printf (" - too new, fdt %d > %d",
e489b9c0 499 fdt_version(working_fdt),
addd8ce8 500 FDT_LAST_SUPPORTED_VERSION);
e489b9c0 501 working_fdt = NULL;
64dbbd40
GVB
502 }
503 return 0;
504 }
505 printf("\n");
781e09ee
GVB
506 return 0;
507 }
508 return 1;
509}
510
addd8ce8 511/****************************************************************************/
781e09ee
GVB
512
513/*
addd8ce8 514 * Parse the user's input, partially heuristic. Valid formats:
4abd844d 515 * <0x00112233 4 05> - an array of cells. Numbers follow standard
53677ef1 516 * C conventions.
addd8ce8
GVB
517 * [00 11 22 .. nn] - byte stream
518 * "string" - If the the value doesn't start with "<" or "[", it is
519 * treated as a string. Note that the quotes are
520 * stripped by the parser before we get the string.
4abd844d 521 * newval: An array of strings containing the new property as specified
53677ef1 522 * on the command line
4abd844d
AF
523 * count: The number of strings in the array
524 * data: A bytestream to be placed in the property
525 * len: The length of the resulting bytestream
addd8ce8 526 */
4abd844d 527static int fdt_parse_prop(char **newval, int count, char *data, int *len)
addd8ce8
GVB
528{
529 char *cp; /* temporary char pointer */
4abd844d 530 char *newp; /* temporary newval char pointer */
addd8ce8 531 unsigned long tmp; /* holds converted values */
4abd844d 532 int stridx = 0;
addd8ce8 533
4abd844d
AF
534 *len = 0;
535 newp = newval[0];
536
537 /* An array of cells */
538 if (*newp == '<') {
539 newp++;
540 while ((*newp != '>') && (stridx < count)) {
541 /*
542 * Keep searching until we find that last ">"
543 * That way users don't have to escape the spaces
544 */
545 if (*newp == '\0') {
546 newp = newval[++stridx];
547 continue;
548 }
549
550 cp = newp;
551 tmp = simple_strtoul(cp, &newp, 0);
552 *(uint32_t *)data = __cpu_to_be32(tmp);
553 data += 4;
554 *len += 4;
555
556 /* If the ptr didn't advance, something went wrong */
557 if ((newp - cp) <= 0) {
addd8ce8
GVB
558 printf("Sorry, I could not convert \"%s\"\n",
559 cp);
560 return 1;
561 }
4abd844d
AF
562
563 while (*newp == ' ')
564 newp++;
addd8ce8 565 }
4abd844d
AF
566
567 if (*newp != '>') {
568 printf("Unexpected character '%c'\n", *newp);
addd8ce8
GVB
569 return 1;
570 }
4abd844d 571 } else if (*newp == '[') {
addd8ce8
GVB
572 /*
573 * Byte stream. Convert the values.
574 */
4abd844d
AF
575 newp++;
576 while ((*newp != ']') && (stridx < count)) {
577 tmp = simple_strtoul(newp, &newp, 16);
addd8ce8 578 *data++ = tmp & 0xFF;
fd61e55d 579 *len = *len + 1;
4abd844d
AF
580 while (*newp == ' ')
581 newp++;
582 if (*newp != '\0')
583 newp = newval[++stridx];
addd8ce8 584 }
4abd844d 585 if (*newp != ']') {
dc4b0b38 586 printf("Unexpected character '%c'\n", *newp);
addd8ce8
GVB
587 return 1;
588 }
589 } else {
590 /*
591 * Assume it is a string. Copy it into our data area for
592 * convenience (including the terminating '\0').
593 */
4abd844d
AF
594 while (stridx < count) {
595 *len = strlen(newp) + 1;
596 strcpy(data, newp);
597 newp = newval[++stridx];
598 }
addd8ce8
GVB
599 }
600 return 0;
601}
602
603/****************************************************************************/
604
605/*
606 * Heuristic to guess if this is a string or concatenated strings.
781e09ee
GVB
607 */
608
609static int is_printable_string(const void *data, int len)
610{
611 const char *s = data;
612
613 /* zero length is not */
614 if (len == 0)
615 return 0;
616
617 /* must terminate with zero */
618 if (s[len - 1] != '\0')
619 return 0;
620
621 /* printable or a null byte (concatenated strings) */
622 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
623 /*
624 * If we see a null, there are three possibilities:
625 * 1) If len == 1, it is the end of the string, printable
626 * 2) Next character also a null, not printable.
627 * 3) Next character not a null, continue to check.
628 */
629 if (s[0] == '\0') {
630 if (len == 1)
631 return 1;
632 if (s[1] == '\0')
633 return 0;
634 }
635 s++;
636 len--;
637 }
638
639 /* Not the null termination, or not done yet: not printable */
640 if (*s != '\0' || (len != 0))
641 return 0;
642
643 return 1;
644}
645
addd8ce8
GVB
646
647/*
648 * Print the property in the best format, a heuristic guess. Print as
649 * a string, concatenated strings, a byte, word, double word, or (if all
650 * else fails) it is printed as a stream of bytes.
651 */
781e09ee
GVB
652static void print_data(const void *data, int len)
653{
654 int j;
781e09ee
GVB
655
656 /* no data, don't print */
657 if (len == 0)
658 return;
659
660 /*
661 * It is a string, but it may have multiple strings (embedded '\0's).
662 */
663 if (is_printable_string(data, len)) {
664 puts("\"");
665 j = 0;
666 while (j < len) {
667 if (j > 0)
668 puts("\", \"");
669 puts(data);
670 j += strlen(data) + 1;
671 data += strlen(data) + 1;
672 }
673 puts("\"");
674 return;
675 }
676
4abd844d
AF
677 if ((len %4) == 0) {
678 const u32 *p;
679
680 printf("<");
681 for (j = 0, p = data; j < len/4; j ++)
682 printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
683 printf(">");
684 } else { /* anything else... hexdump */
685 const u8 *s;
686
781e09ee
GVB
687 printf("[");
688 for (j = 0, s = data; j < len; j++)
689 printf("%02x%s", s[j], j < len - 1 ? " " : "");
690 printf("]");
781e09ee
GVB
691 }
692}
693
addd8ce8
GVB
694/****************************************************************************/
695
696/*
e489b9c0 697 * Recursively print (a portion of) the working_fdt. The depth parameter
addd8ce8
GVB
698 * determines how deeply nested the fdt is printed.
699 */
dbaf07ce 700static int fdt_print(const char *pathp, char *prop, int depth)
addd8ce8 701{
addd8ce8
GVB
702 static char tabs[MAX_LEVEL+1] =
703 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
704 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
dbaf07ce 705 const void *nodep; /* property node pointer */
addd8ce8
GVB
706 int nodeoffset; /* node offset from libfdt */
707 int nextoffset; /* next node offset from libfdt */
708 uint32_t tag; /* tag */
709 int len; /* length of the property */
710 int level = 0; /* keep track of nesting level */
91623528 711 const struct fdt_property *fdt_prop;
addd8ce8 712
e489b9c0 713 nodeoffset = fdt_path_offset (working_fdt, pathp);
addd8ce8
GVB
714 if (nodeoffset < 0) {
715 /*
716 * Not found or something else bad happened.
717 */
8d04f02f 718 printf ("libfdt fdt_path_offset() returned %s\n",
06e19a07 719 fdt_strerror(nodeoffset));
addd8ce8
GVB
720 return 1;
721 }
722 /*
723 * The user passed in a property as well as node path.
724 * Print only the given property and then return.
725 */
726 if (prop) {
e489b9c0 727 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
addd8ce8
GVB
728 if (len == 0) {
729 /* no property value */
730 printf("%s %s\n", pathp, prop);
731 return 0;
732 } else if (len > 0) {
28f384b1 733 printf("%s = ", prop);
addd8ce8
GVB
734 print_data (nodep, len);
735 printf("\n");
736 return 0;
737 } else {
738 printf ("libfdt fdt_getprop(): %s\n",
739 fdt_strerror(len));
740 return 1;
741 }
742 }
743
744 /*
745 * The user passed in a node path and no property,
746 * print the node and all subnodes.
747 */
addd8ce8 748 while(level >= 0) {
e489b9c0 749 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
addd8ce8
GVB
750 switch(tag) {
751 case FDT_BEGIN_NODE:
e489b9c0 752 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
91623528
GVB
753 if (level <= depth) {
754 if (pathp == NULL)
755 pathp = "/* NULL pointer error */";
756 if (*pathp == '\0')
757 pathp = "/"; /* root is nameless */
addd8ce8
GVB
758 printf("%s%s {\n",
759 &tabs[MAX_LEVEL - level], pathp);
91623528 760 }
addd8ce8 761 level++;
addd8ce8 762 if (level >= MAX_LEVEL) {
91623528 763 printf("Nested too deep, aborting.\n");
addd8ce8
GVB
764 return 1;
765 }
766 break;
767 case FDT_END_NODE:
768 level--;
91623528 769 if (level <= depth)
addd8ce8
GVB
770 printf("%s};\n", &tabs[MAX_LEVEL - level]);
771 if (level == 0) {
772 level = -1; /* exit the loop */
773 }
774 break;
775 case FDT_PROP:
e489b9c0 776 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
91623528 777 sizeof(*fdt_prop));
e489b9c0 778 pathp = fdt_string(working_fdt,
91623528
GVB
779 fdt32_to_cpu(fdt_prop->nameoff));
780 len = fdt32_to_cpu(fdt_prop->len);
781 nodep = fdt_prop->data;
addd8ce8
GVB
782 if (len < 0) {
783 printf ("libfdt fdt_getprop(): %s\n",
784 fdt_strerror(len));
785 return 1;
786 } else if (len == 0) {
787 /* the property has no value */
91623528 788 if (level <= depth)
addd8ce8
GVB
789 printf("%s%s;\n",
790 &tabs[MAX_LEVEL - level],
791 pathp);
792 } else {
91623528 793 if (level <= depth) {
28f384b1 794 printf("%s%s = ",
addd8ce8
GVB
795 &tabs[MAX_LEVEL - level],
796 pathp);
797 print_data (nodep, len);
798 printf(";\n");
799 }
800 }
801 break;
802 case FDT_NOP:
dc4b0b38 803 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
addd8ce8
GVB
804 break;
805 case FDT_END:
806 return 1;
807 default:
91623528 808 if (level <= depth)
addd8ce8
GVB
809 printf("Unknown tag 0x%08X\n", tag);
810 return 1;
811 }
812 nodeoffset = nextoffset;
813 }
814 return 0;
815}
816
781e09ee
GVB
817/********************************************************************/
818
781e09ee 819U_BOOT_CMD(
4abd844d 820 fdt, 255, 0, do_fdt,
781e09ee
GVB
821 "fdt - flattened device tree utility commands\n",
822 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
fd61e55d
GVB
823#ifdef CONFIG_OF_BOARD_SETUP
824 "fdt boardsetup - Do board-specific set up\n"
825#endif
238cb7a4 826 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
40afac22 827 "fdt resize - Resize fdt to size + padding to 4k addr\n"
781e09ee
GVB
828 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
829 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
830 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
831 "fdt mknode <path> <node> - Create a new node after <path>\n"
832 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
804887e6
KG
833 "fdt header - Display header info\n"
834 "fdt bootcpu <id> - Set boot cpuid\n"
835 "fdt memory <addr> <size> - Add/Update memory node\n"
836 "fdt rsvmem print - Show current mem reserves\n"
837 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
838 "fdt rsvmem delete <index> - Delete a mem reserves\n"
f953d99f
KG
839 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
840 " <start>/<end> - initrd start/end addr\n"
109c30fb
GVB
841 "NOTE: Dereference aliases by omiting the leading '/', "
842 "e.g. fdt print ethernet0.\n"
781e09ee 843);