blob: 571b8f14d56f3bc97dcd49eb8465cfdc9988d786 [file] [log] [blame]
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001/*
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>
31
32#ifdef CONFIG_OF_LIBFDT
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040033
Gerald Van Barenade8ef42007-03-31 12:22:10 -040034#include <asm/global_data.h>
35#include <fdt.h>
36#include <libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040037#include <fdt_support.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040038
39#define MAX_LEVEL 32 /* how deeply nested we will go */
Gerald Van Baren5606a362007-06-25 23:25:28 -040040#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
Gerald Van Barenade8ef42007-03-31 12:22:10 -040041
42/*
43 * Global data (for the gd->bd)
44 */
45DECLARE_GLOBAL_DATA_PTR;
46
Gerald Van Barenade8ef42007-03-31 12:22:10 -040047static int fdt_valid(void);
Gerald Van Barene596a162007-05-16 22:39:59 -040048static int fdt_parse_prop(char *pathp, char *prop, char *newval,
49 char *data, int *len);
50static int fdt_print(char *pathp, char *prop, int depth);
Gerald Van Barenade8ef42007-03-31 12:22:10 -040051
Gerald Van Barenade8ef42007-03-31 12:22:10 -040052/*
53 * Flattened Device Tree command, see the help for parameter definitions.
54 */
55int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
56{
Gerald Van Barenade8ef42007-03-31 12:22:10 -040057 if (argc < 2) {
58 printf ("Usage:\n%s\n", cmdtp->usage);
59 return 1;
60 }
61
Gerald Van Barenade8ef42007-03-31 12:22:10 -040062 /********************************************************************
63 * Set the address of the fdt
64 ********************************************************************/
Gerald Van Baren0ea55d22007-05-12 09:47:25 -040065 if (argv[1][0] == 'a') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -040066 /*
67 * Set the address [and length] of the fdt.
68 */
69 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
70
71 if (!fdt_valid()) {
72 return 1;
73 }
74
75 if (argc >= 4) {
76 int len;
77 int err;
78 /*
79 * Optional new length
80 */
81 len = simple_strtoul(argv[3], NULL, 16);
82 if (len < fdt_totalsize(fdt)) {
Gerald Van Barene596a162007-05-16 22:39:59 -040083 printf ("New length %d < existing length %d, "
84 "ignoring.\n",
Gerald Van Barenade8ef42007-03-31 12:22:10 -040085 len, fdt_totalsize(fdt));
86 } else {
87 /*
88 * Open in place with a new length.
89 */
90 err = fdt_open_into(fdt, fdt, len);
91 if (err != 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -040092 printf ("libfdt fdt_open_into(): %s\n",
93 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -040094 }
95 }
96 }
97
98 /********************************************************************
99 * Move the fdt
100 ********************************************************************/
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400101 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400102 struct fdt_header *newaddr;
103 int len;
104 int err;
105
Gerald Van Baren4d223922007-04-25 22:47:15 -0400106 if (argc < 4) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400107 printf ("Usage:\n%s\n", cmdtp->usage);
108 return 1;
109 }
110
111 /*
112 * Set the address and length of the fdt.
113 */
114 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
115 if (!fdt_valid()) {
116 return 1;
117 }
118
Gerald Van Barene596a162007-05-16 22:39:59 -0400119 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren4d223922007-04-25 22:47:15 -0400120
121 /*
122 * If the user specifies a length, use that. Otherwise use the
123 * current length.
124 */
125 if (argc <= 4) {
126 len = fdt_totalsize(fdt);
127 } else {
128 len = simple_strtoul(argv[4], NULL, 16);
129 if (len < fdt_totalsize(fdt)) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400130 printf ("New length 0x%X < existing length "
131 "0x%X, aborting.\n",
Gerald Van Baren4d223922007-04-25 22:47:15 -0400132 len, fdt_totalsize(fdt));
133 return 1;
134 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400135 }
136
137 /*
138 * Copy to the new location.
139 */
140 err = fdt_open_into(fdt, newaddr, len);
141 if (err != 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400142 printf ("libfdt fdt_open_into(): %s\n",
143 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400144 return 1;
145 }
146 fdt = newaddr;
147
148 /********************************************************************
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400149 * Make a new node
150 ********************************************************************/
151 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
152 char *pathp; /* path */
153 char *nodep; /* new node to add */
154 int nodeoffset; /* node offset from libfdt */
155 int err;
156
157 /*
158 * Parameters: Node path, new node to be appended to the path.
159 */
160 if (argc < 4) {
161 printf ("Usage:\n%s\n", cmdtp->usage);
162 return 1;
163 }
164
165 pathp = argv[2];
166 nodep = argv[3];
167
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400168 nodeoffset = fdt_find_node_by_path (fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400169 if (nodeoffset < 0) {
170 /*
171 * Not found or something else bad happened.
172 */
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400173 printf ("libfdt fdt_find_node_by_path() returned %s\n",
174 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400175 return 1;
176 }
177 err = fdt_add_subnode(fdt, nodeoffset, nodep);
178 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400179 printf ("libfdt fdt_add_subnode(): %s\n",
180 fdt_strerror(err));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400181 return 1;
182 }
183
184 /********************************************************************
185 * Set the value of a property in the fdt.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400186 ********************************************************************/
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400187 } else if (argv[1][0] == 's') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400188 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400189 char *prop; /* property */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400190 char *newval; /* value from the user (as a string) */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400191 int nodeoffset; /* node offset from libfdt */
Gerald Van Barene596a162007-05-16 22:39:59 -0400192 static char data[SCRATCHPAD]; /* storage for the property */
193 int len; /* new length of the property */
194 int ret; /* return value */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400195
196 /*
197 * Parameters: Node path, property, value.
198 */
199 if (argc < 5) {
200 printf ("Usage:\n%s\n", cmdtp->usage);
201 return 1;
202 }
203
204 pathp = argv[2];
205 prop = argv[3];
206 newval = argv[4];
207
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400208 nodeoffset = fdt_find_node_by_path (fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400209 if (nodeoffset < 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400210 /*
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400211 * Not found or something else bad happened.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400212 */
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400213 printf ("libfdt fdt_find_node_by_path() returned %s\n",
214 fdt_strerror(nodeoffset));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400215 return 1;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400216 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400217 ret = fdt_parse_prop(pathp, prop, newval, data, &len);
218 if (ret != 0)
219 return ret;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400220
221 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
222 if (ret < 0) {
223 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
224 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400225 }
226
227 /********************************************************************
228 * Print (recursive) / List (single level)
229 ********************************************************************/
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400230 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400231 int depth = MAX_LEVEL; /* how deep to print */
232 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400233 char *prop; /* property */
234 int ret; /* return value */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400235
236 /*
237 * list is an alias for print, but limited to 1 level
238 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400239 if (argv[1][0] == 'l') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400240 depth = 1;
241 }
242
243 /*
244 * Get the starting path. The root node is an oddball,
245 * the offset is zero and has no name.
246 */
247 pathp = argv[2];
248 if (argc > 3)
249 prop = argv[3];
250 else
251 prop = NULL;
252
Gerald Van Barene596a162007-05-16 22:39:59 -0400253 ret = fdt_print(pathp, prop, depth);
254 if (ret != 0)
255 return ret;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400256
257 /********************************************************************
258 * Remove a property/node
259 ********************************************************************/
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400260 } else if (argv[1][0] == 'r') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400261 int nodeoffset; /* node offset from libfdt */
262 int err;
263
264 /*
265 * Get the path. The root node is an oddball, the offset
266 * is zero and has no name.
267 */
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400268 nodeoffset = fdt_find_node_by_path (fdt, argv[2]);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400269 if (nodeoffset < 0) {
270 /*
271 * Not found or something else bad happened.
272 */
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400273 printf ("libfdt fdt_find_node_by_path() returned %s\n",
274 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400275 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400276 }
277 /*
278 * Do the delete. A fourth parameter means delete a property,
279 * otherwise delete the node.
280 */
281 if (argc > 3) {
282 err = fdt_delprop(fdt, nodeoffset, argv[3]);
283 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400284 printf("libfdt fdt_delprop(): %s\n",
285 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400286 return err;
287 }
288 } else {
289 err = fdt_del_node(fdt, nodeoffset);
290 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400291 printf("libfdt fdt_del_node(): %s\n",
292 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400293 return err;
294 }
295 }
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500296 }
Gerald Van Baren5606a362007-06-25 23:25:28 -0400297#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500298 /* Call the board-specific fixup routine */
299 else if (argv[1][0] == 'b')
Gerald Van Baren5606a362007-06-25 23:25:28 -0400300 ft_board_setup(fdt, gd->bd);
301#endif
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500302 /* Create a chosen node */
303 else if (argv[1][0] == 'c')
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400304 fdt_chosen(fdt, 0, 0, 1);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400305
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500306#ifdef CONFIG_OF_HAS_UBOOT_ENV
307 /* Create a u-boot-env node */
308 else if (argv[1][0] == 'e')
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400309 fdt_env(fdt);
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500310#endif
311#ifdef CONFIG_OF_HAS_BD_T
312 /* Create a bd_t node */
313 else if (argv[1][0] == 'b')
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400314 fdt_bd_t(fdt);
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500315#endif
316 else {
317 /* Unrecognized command */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400318 printf ("Usage:\n%s\n", cmdtp->usage);
319 return 1;
320 }
321
322 return 0;
323}
324
Gerald Van Barene596a162007-05-16 22:39:59 -0400325/****************************************************************************/
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400326
327static int fdt_valid(void)
328{
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400329 int err;
330
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400331 if (fdt == NULL) {
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400332 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400333 return 0;
334 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400335
336 err = fdt_check_header(fdt);
337 if (err == 0)
338 return 1; /* valid */
339
340 if (err < 0) {
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400341 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400342 /*
343 * Be more informative on bad version.
344 */
345 if (err == -FDT_ERR_BADVERSION) {
346 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
347 printf (" - too old, fdt $d < %d",
Gerald Van Barene596a162007-05-16 22:39:59 -0400348 fdt_version(fdt),
349 FDT_FIRST_SUPPORTED_VERSION);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400350 fdt = NULL;
351 }
352 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
353 printf (" - too new, fdt $d > %d",
Gerald Van Barene596a162007-05-16 22:39:59 -0400354 fdt_version(fdt),
355 FDT_LAST_SUPPORTED_VERSION);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400356 fdt = NULL;
357 }
358 return 0;
359 }
360 printf("\n");
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400361 return 0;
362 }
363 return 1;
364}
365
Gerald Van Barene596a162007-05-16 22:39:59 -0400366/****************************************************************************/
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400367
368/*
Gerald Van Barene596a162007-05-16 22:39:59 -0400369 * Parse the user's input, partially heuristic. Valid formats:
370 * <00> - hex byte
371 * <0011> - hex half word (16 bits)
372 * <00112233> - hex word (32 bits)
373 * - hex double words (64 bits) are not supported, must use
374 * a byte stream instead.
375 * [00 11 22 .. nn] - byte stream
376 * "string" - If the the value doesn't start with "<" or "[", it is
377 * treated as a string. Note that the quotes are
378 * stripped by the parser before we get the string.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400379 */
Gerald Van Barene596a162007-05-16 22:39:59 -0400380static int fdt_parse_prop(char *pathp, char *prop, char *newval,
381 char *data, int *len)
382{
383 char *cp; /* temporary char pointer */
384 unsigned long tmp; /* holds converted values */
385
386 if (*newval == '<') {
387 /*
388 * Bigger values than bytes.
389 */
390 *len = 0;
391 newval++;
392 while ((*newval != '>') && (*newval != '\0')) {
393 cp = newval;
394 tmp = simple_strtoul(cp, &newval, 16);
395 if ((newval - cp) <= 2) {
396 *data = tmp & 0xFF;
397 data += 1;
398 *len += 1;
399 } else if ((newval - cp) <= 4) {
400 *(uint16_t *)data = __cpu_to_be16(tmp);
401 data += 2;
402 *len += 2;
403 } else if ((newval - cp) <= 8) {
404 *(uint32_t *)data = __cpu_to_be32(tmp);
405 data += 4;
406 *len += 4;
407 } else {
408 printf("Sorry, I could not convert \"%s\"\n",
409 cp);
410 return 1;
411 }
412 while (*newval == ' ')
413 newval++;
414 }
415 if (*newval != '>') {
416 printf("Unexpected character '%c'\n", *newval);
417 return 1;
418 }
419 } else if (*newval == '[') {
420 /*
421 * Byte stream. Convert the values.
422 */
423 *len = 0;
424 newval++;
425 while ((*newval != ']') && (*newval != '\0')) {
426 tmp = simple_strtoul(newval, &newval, 16);
427 *data++ = tmp & 0xFF;
Gerald Van Baren5606a362007-06-25 23:25:28 -0400428 *len = *len + 1;
Gerald Van Barene596a162007-05-16 22:39:59 -0400429 while (*newval == ' ')
430 newval++;
431 }
432 if (*newval != ']') {
433 printf("Unexpected character '%c'\n", *newval);
434 return 1;
435 }
436 } else {
437 /*
438 * Assume it is a string. Copy it into our data area for
439 * convenience (including the terminating '\0').
440 */
441 *len = strlen(newval) + 1;
442 strcpy(data, newval);
443 }
444 return 0;
445}
446
447/****************************************************************************/
448
449/*
450 * Heuristic to guess if this is a string or concatenated strings.
451 */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400452
453static int is_printable_string(const void *data, int len)
454{
455 const char *s = data;
456
457 /* zero length is not */
458 if (len == 0)
459 return 0;
460
461 /* must terminate with zero */
462 if (s[len - 1] != '\0')
463 return 0;
464
465 /* printable or a null byte (concatenated strings) */
466 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
467 /*
468 * If we see a null, there are three possibilities:
469 * 1) If len == 1, it is the end of the string, printable
470 * 2) Next character also a null, not printable.
471 * 3) Next character not a null, continue to check.
472 */
473 if (s[0] == '\0') {
474 if (len == 1)
475 return 1;
476 if (s[1] == '\0')
477 return 0;
478 }
479 s++;
480 len--;
481 }
482
483 /* Not the null termination, or not done yet: not printable */
484 if (*s != '\0' || (len != 0))
485 return 0;
486
487 return 1;
488}
489
Gerald Van Barene596a162007-05-16 22:39:59 -0400490
491/*
492 * Print the property in the best format, a heuristic guess. Print as
493 * a string, concatenated strings, a byte, word, double word, or (if all
494 * else fails) it is printed as a stream of bytes.
495 */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400496static void print_data(const void *data, int len)
497{
498 int j;
499 const u8 *s;
500
501 /* no data, don't print */
502 if (len == 0)
503 return;
504
505 /*
506 * It is a string, but it may have multiple strings (embedded '\0's).
507 */
508 if (is_printable_string(data, len)) {
509 puts("\"");
510 j = 0;
511 while (j < len) {
512 if (j > 0)
513 puts("\", \"");
514 puts(data);
515 j += strlen(data) + 1;
516 data += strlen(data) + 1;
517 }
518 puts("\"");
519 return;
520 }
521
522 switch (len) {
523 case 1: /* byte */
524 printf("<%02x>", (*(u8 *) data) & 0xff);
525 break;
526 case 2: /* half-word */
527 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
528 break;
529 case 4: /* word */
530 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
531 break;
532 case 8: /* double-word */
533#if __WORDSIZE == 64
534 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
535#else
536 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
537 data += 4;
538 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
539#endif
540 break;
541 default: /* anything else... hexdump */
542 printf("[");
543 for (j = 0, s = data; j < len; j++)
544 printf("%02x%s", s[j], j < len - 1 ? " " : "");
545 printf("]");
546
547 break;
548 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400549}
550
551/****************************************************************************/
552
553/*
554 * Recursively print (a portion of) the fdt. The depth parameter
555 * determines how deeply nested the fdt is printed.
556 */
557static int fdt_print(char *pathp, char *prop, int depth)
558{
559 static int offstack[MAX_LEVEL];
560 static char tabs[MAX_LEVEL+1] =
561 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
562 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
563 void *nodep; /* property node pointer */
564 int nodeoffset; /* node offset from libfdt */
565 int nextoffset; /* next node offset from libfdt */
566 uint32_t tag; /* tag */
567 int len; /* length of the property */
568 int level = 0; /* keep track of nesting level */
569
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400570 nodeoffset = fdt_find_node_by_path (fdt, pathp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400571 if (nodeoffset < 0) {
572 /*
573 * Not found or something else bad happened.
574 */
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400575 printf ("libfdt fdt_find_node_by_path() returned %s\n",
576 fdt_strerror(nodeoffset));
Gerald Van Barene596a162007-05-16 22:39:59 -0400577 return 1;
578 }
579 /*
580 * The user passed in a property as well as node path.
581 * Print only the given property and then return.
582 */
583 if (prop) {
584 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
585 if (len == 0) {
586 /* no property value */
587 printf("%s %s\n", pathp, prop);
588 return 0;
589 } else if (len > 0) {
590 printf("%s=", prop);
591 print_data (nodep, len);
592 printf("\n");
593 return 0;
594 } else {
595 printf ("libfdt fdt_getprop(): %s\n",
596 fdt_strerror(len));
597 return 1;
598 }
599 }
600
601 /*
602 * The user passed in a node path and no property,
603 * print the node and all subnodes.
604 */
605 offstack[0] = nodeoffset;
606
607 while(level >= 0) {
608 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
609 switch(tag) {
610 case FDT_BEGIN_NODE:
611 if(level <= depth)
612 printf("%s%s {\n",
613 &tabs[MAX_LEVEL - level], pathp);
614 level++;
615 offstack[level] = nodeoffset;
616 if (level >= MAX_LEVEL) {
617 printf("Aaaiii <splat> nested too deep. "
618 "Aborting.\n");
619 return 1;
620 }
621 break;
622 case FDT_END_NODE:
623 level--;
624 if(level <= depth)
625 printf("%s};\n", &tabs[MAX_LEVEL - level]);
626 if (level == 0) {
627 level = -1; /* exit the loop */
628 }
629 break;
630 case FDT_PROP:
631 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
632 if (len < 0) {
633 printf ("libfdt fdt_getprop(): %s\n",
634 fdt_strerror(len));
635 return 1;
636 } else if (len == 0) {
637 /* the property has no value */
638 if(level <= depth)
639 printf("%s%s;\n",
640 &tabs[MAX_LEVEL - level],
641 pathp);
642 } else {
643 if(level <= depth) {
644 printf("%s%s=",
645 &tabs[MAX_LEVEL - level],
646 pathp);
647 print_data (nodep, len);
648 printf(";\n");
649 }
650 }
651 break;
652 case FDT_NOP:
653 break;
654 case FDT_END:
655 return 1;
656 default:
657 if(level <= depth)
658 printf("Unknown tag 0x%08X\n", tag);
659 return 1;
660 }
661 nodeoffset = nextoffset;
662 }
663 return 0;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400664}
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400665
666/********************************************************************/
667
668U_BOOT_CMD(
669 fdt, 5, 0, do_fdt,
670 "fdt - flattened device tree utility commands\n",
671 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
Gerald Van Baren5606a362007-06-25 23:25:28 -0400672#ifdef CONFIG_OF_BOARD_SETUP
673 "fdt boardsetup - Do board-specific set up\n"
674#endif
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400675 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
676 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
677 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
678 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
679 "fdt mknode <path> <node> - Create a new node after <path>\n"
680 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Gerald Van Baren5606a362007-06-25 23:25:28 -0400681 "fdt chosen - Add/update the /chosen branch in the tree\n"
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400682#ifdef CONFIG_OF_HAS_UBOOT_ENV
Gerald Van Baren5606a362007-06-25 23:25:28 -0400683 "fdt env - Add/replace the /u-boot-env branch in the tree\n"
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400684#endif
685#ifdef CONFIG_OF_HAS_BD_T
Gerald Van Baren5606a362007-06-25 23:25:28 -0400686 "fdt bd_t - Add/replace the /bd_t branch in the tree\n"
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400687#endif
688 "Hints:\n"
Gerald Van Baren5606a362007-06-25 23:25:28 -0400689 " If the property you are setting/printing has a '#' character or spaces,\n"
690 " you MUST escape it with a \\ character or quote it with \".\n"
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400691 "Examples: fdt print / # print the whole tree\n"
692 " fdt print /cpus \"#address-cells\"\n"
693 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
694);
695
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400696#endif /* CONFIG_OF_LIBFDT */