]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_fdt.c
FDT command improvements.
[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>
31
32#ifdef CONFIG_OF_LIBFDT
64dbbd40 33
781e09ee
GVB
34#include <asm/global_data.h>
35#include <fdt.h>
36#include <libfdt.h>
64dbbd40 37#include <fdt_support.h>
781e09ee
GVB
38
39#define MAX_LEVEL 32 /* how deeply nested we will go */
40#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
41
42/*
43 * Global data (for the gd->bd)
44 */
45DECLARE_GLOBAL_DATA_PTR;
46
47/*
48 * Scratchpad memory.
49 */
50static char data[SCRATCHPAD];
51
52
53/*
54 * Function prototypes/declarations.
55 */
56static int fdt_valid(void);
57static void print_data(const void *data, int len);
781e09ee 58
25114033
GVB
59static int findnodeoffset(const char *pathp)
60{
61 int nodeoffset;
62
63 if (strcmp(pathp, "/") == 0) {
64 nodeoffset = 0;
65 } else {
66 nodeoffset = fdt_path_offset (fdt, pathp);
67 if (nodeoffset < 0) {
68 /*
69 * Not found or something else bad happened.
70 */
71 printf ("findnodeoffset() libfdt: %s\n", fdt_strerror(nodeoffset));
72 }
73 }
74 return nodeoffset;
75}
781e09ee
GVB
76
77/*
78 * Flattened Device Tree command, see the help for parameter definitions.
79 */
80int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
81{
781e09ee
GVB
82 if (argc < 2) {
83 printf ("Usage:\n%s\n", cmdtp->usage);
84 return 1;
85 }
86
781e09ee
GVB
87 /********************************************************************
88 * Set the address of the fdt
89 ********************************************************************/
25114033 90 if (argv[1][0] == 'a') {
781e09ee
GVB
91 /*
92 * Set the address [and length] of the fdt.
93 */
94 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
95
96 if (!fdt_valid()) {
97 return 1;
98 }
99
100 if (argc >= 4) {
101 int len;
102 int err;
103 /*
104 * Optional new length
105 */
106 len = simple_strtoul(argv[3], NULL, 16);
107 if (len < fdt_totalsize(fdt)) {
108 printf ("New length %d < existing length %d, ignoring.\n",
109 len, fdt_totalsize(fdt));
110 } else {
111 /*
112 * Open in place with a new length.
113 */
114 err = fdt_open_into(fdt, fdt, len);
115 if (err != 0) {
25114033 116 printf ("libfdt fdt_open_into(): %s\n", fdt_strerror(err));
781e09ee
GVB
117 }
118 }
119 }
120
121 /********************************************************************
122 * Move the fdt
123 ********************************************************************/
25114033 124 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
781e09ee
GVB
125 struct fdt_header *newaddr;
126 int len;
127 int err;
128
6be07cc1 129 if (argc < 4) {
781e09ee
GVB
130 printf ("Usage:\n%s\n", cmdtp->usage);
131 return 1;
132 }
133
134 /*
135 * Set the address and length of the fdt.
136 */
137 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
138 if (!fdt_valid()) {
139 return 1;
140 }
141
142 newaddr = (struct fdt_header *)simple_strtoul(argv[3], NULL, 16);
6be07cc1
GVB
143
144 /*
145 * If the user specifies a length, use that. Otherwise use the
146 * current length.
147 */
148 if (argc <= 4) {
149 len = fdt_totalsize(fdt);
150 } else {
151 len = simple_strtoul(argv[4], NULL, 16);
152 if (len < fdt_totalsize(fdt)) {
153 printf ("New length 0x%X < existing length 0x%X, aborting.\n",
154 len, fdt_totalsize(fdt));
155 return 1;
156 }
781e09ee
GVB
157 }
158
159 /*
160 * Copy to the new location.
161 */
162 err = fdt_open_into(fdt, newaddr, len);
163 if (err != 0) {
25114033 164 printf ("libfdt fdt_open_into(): %s\n", fdt_strerror(err));
781e09ee
GVB
165 return 1;
166 }
167 fdt = newaddr;
168
169 /********************************************************************
25114033
GVB
170 * Make a new node
171 ********************************************************************/
172 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
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
189 nodeoffset = findnodeoffset(pathp);
190 if (nodeoffset < 0) {
191 /*
192 * Not found or something else bad happened.
193 */
194 return 1;
195 }
196 err = fdt_add_subnode(fdt, nodeoffset, nodep);
197 if (err < 0) {
198 printf ("libfdt fdt_add_subnode(): %s\n", fdt_strerror(err));
199 return 1;
200 }
201
202 /********************************************************************
203 * Set the value of a property in the fdt.
781e09ee 204 ********************************************************************/
25114033 205 } else if (argv[1][0] == 's') {
781e09ee
GVB
206 char *pathp; /* path */
207 char *prop; /* property */
208 struct fdt_property *nodep; /* node struct pointer */
209 char *newval; /* value from the user (as a string) */
210 char *vp; /* temporary value pointer */
211 char *cp; /* temporary char pointer */
212 int nodeoffset; /* node offset from libfdt */
213 int len; /* new length of the property */
214 int oldlen; /* original length of the property */
215 unsigned long tmp; /* holds converted values */
216 int ret; /* return value */
217
218 /*
219 * Parameters: Node path, property, value.
220 */
221 if (argc < 5) {
222 printf ("Usage:\n%s\n", cmdtp->usage);
223 return 1;
224 }
225
226 pathp = argv[2];
227 prop = argv[3];
228 newval = argv[4];
229
25114033
GVB
230 nodeoffset = findnodeoffset(pathp);
231 if (nodeoffset < 0) {
781e09ee 232 /*
25114033 233 * Not found or something else bad happened.
781e09ee 234 */
781e09ee 235 return 1;
25114033
GVB
236 }
237 /*
238 * Convert the new property
239 */
240 vp = data;
241 if (*newval == '<') {
781e09ee 242 /*
25114033 243 * Bigger values than bytes.
781e09ee 244 */
25114033
GVB
245 len = 0;
246 newval++;
247 while ((*newval != '>') && (*newval != '\0')) {
248 cp = newval;
249 tmp = simple_strtoul(cp, &newval, 16);
250 if ((newval - cp) <= 2) {
251 *vp = tmp & 0xFF;
252 vp += 1;
253 len += 1;
254 } else if ((newval - cp) <= 4) {
255 *(uint16_t *)vp = __cpu_to_be16(tmp);
256 vp += 2;
257 len += 2;
258 } else if ((newval - cp) <= 8) {
259 *(uint32_t *)vp = __cpu_to_be32(tmp);
260 vp += 4;
261 len += 4;
262 } else {
263 printf("Sorry, I could not convert \"%s\"\n", cp);
781e09ee
GVB
264 return 1;
265 }
25114033
GVB
266 while (*newval == ' ')
267 newval++;
781e09ee 268 }
25114033
GVB
269 if (*newval != '>') {
270 printf("Unexpected character '%c'\n", *newval);
271 return 1;
272 }
273 } else if (*newval == '[') {
274 /*
275 * Byte stream. Convert the values.
276 */
277 len = 0;
278 newval++;
279 while ((*newval != ']') && (*newval != '\0')) {
280 tmp = simple_strtoul(newval, &newval, 16);
281 *vp++ = tmp & 0xFF;
282 len++;
283 while (*newval == ' ')
284 newval++;
285 }
286 if (*newval != ']') {
287 printf("Unexpected character '%c'\n", *newval);
781e09ee
GVB
288 return 1;
289 }
25114033
GVB
290 } else {
291 /*
292 * Assume it is a string. Copy it into our data area for
293 * convenience (including the terminating '\0').
294 */
295 len = strlen(newval) + 1;
296 strcpy(data, newval);
297 }
298
299 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
300 if (ret < 0) {
301 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
302 return 1;
781e09ee
GVB
303 }
304
305 /********************************************************************
306 * Print (recursive) / List (single level)
307 ********************************************************************/
25114033 308 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
781e09ee
GVB
309 /*
310 * Recursively print (a portion of) the fdt.
311 */
312 static int offstack[MAX_LEVEL];
313 static char tabs[MAX_LEVEL+1] = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
314 int depth = MAX_LEVEL; /* how deep to print */
315 char *pathp; /* path */
316 char *prop; /* property */
317 void *nodep; /* property node pointer */
318 int nodeoffset; /* node offset from libfdt */
319 int nextoffset; /* next node offset from libfdt */
320 uint32_t tag; /* tag */
321 int len; /* length of the property */
322 int level = 0; /* keep track of nesting level */
323
324 /*
325 * list is an alias for print, but limited to 1 level
326 */
25114033 327 if (argv[1][0] == 'l') {
781e09ee
GVB
328 depth = 1;
329 }
330
331 /*
332 * Get the starting path. The root node is an oddball,
333 * the offset is zero and has no name.
334 */
335 pathp = argv[2];
336 if (argc > 3)
337 prop = argv[3];
338 else
339 prop = NULL;
340
25114033
GVB
341 nodeoffset = findnodeoffset(pathp);
342 if (nodeoffset < 0) {
343 /*
344 * Not found or something else bad happened.
345 */
346 return 1;
781e09ee
GVB
347 }
348 /*
349 * The user passed in a property as well as node path. Print only
350 * the given property and then return.
351 */
352 if (prop) {
353 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
354 if (len == 0) {
355 printf("%s %s\n", pathp, prop); /* no property value */
356 return 0;
357 } else if (len > 0) {
358 printf("%s=", prop);
359 print_data (nodep, len);
360 printf("\n");
361 return 0;
362 } else {
25114033 363 printf ("libfdt fdt_getprop(): %s\n", fdt_strerror(len));
781e09ee
GVB
364 return 1;
365 }
366 }
367
368 /*
369 * The user passed in a node path and no property, print the node
370 * and all subnodes.
371 */
372 offstack[0] = nodeoffset;
373
374 while(level >= 0) {
375 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, &pathp);
376 switch(tag) {
377 case FDT_BEGIN_NODE:
378 if(level <= depth)
379 printf("%s%s {\n", &tabs[MAX_LEVEL - level], pathp);
380 level++;
381 offstack[level] = nodeoffset;
382 if (level >= MAX_LEVEL) {
25114033 383 printf("Aaaiii <splat> nested too deep. Aborting.\n");
781e09ee
GVB
384 return 1;
385 }
386 break;
387 case FDT_END_NODE:
388 level--;
389 if(level <= depth)
390 printf("%s};\n", &tabs[MAX_LEVEL - level]);
391 if (level == 0) {
392 level = -1; /* exit the loop */
393 }
394 break;
395 case FDT_PROP:
396 nodep = fdt_getprop (fdt, offstack[level], pathp, &len);
397 if (len < 0) {
25114033 398 printf ("libfdt fdt_getprop(): %s\n", fdt_strerror(len));
781e09ee
GVB
399 return 1;
400 } else if (len == 0) {
401 /* the property has no value */
402 if(level <= depth)
403 printf("%s%s;\n", &tabs[MAX_LEVEL - level], pathp);
404 } else {
405 if(level <= depth) {
406 printf("%s%s=", &tabs[MAX_LEVEL - level], pathp);
407 print_data (nodep, len);
408 printf(";\n");
409 }
410 }
411 break;
412 case FDT_NOP:
413 break;
414 case FDT_END:
415 return 1;
416 default:
417 if(level <= depth)
418 printf("Unknown tag 0x%08X\n", tag);
419 return 1;
420 }
421 nodeoffset = nextoffset;
422 }
423
424 /********************************************************************
425 * Remove a property/node
426 ********************************************************************/
25114033 427 } else if (argv[1][0] == 'r') {
781e09ee
GVB
428 int nodeoffset; /* node offset from libfdt */
429 int err;
430
431 /*
432 * Get the path. The root node is an oddball, the offset
433 * is zero and has no name.
434 */
25114033
GVB
435 nodeoffset = findnodeoffset(argv[2]);
436 if (nodeoffset < 0) {
437 /*
438 * Not found or something else bad happened.
439 */
440 return 1;
781e09ee
GVB
441 }
442 /*
443 * Do the delete. A fourth parameter means delete a property,
444 * otherwise delete the node.
445 */
446 if (argc > 3) {
447 err = fdt_delprop(fdt, nodeoffset, argv[3]);
448 if (err < 0) {
25114033 449 printf("libfdt fdt_delprop(): %s\n", fdt_strerror(err));
781e09ee
GVB
450 return err;
451 }
452 } else {
453 err = fdt_del_node(fdt, nodeoffset);
454 if (err < 0) {
25114033 455 printf("libfdt fdt_del_node(): %s\n", fdt_strerror(err));
781e09ee
GVB
456 return err;
457 }
458 }
459
460 /********************************************************************
461 * Create a chosen node
462 ********************************************************************/
25114033 463 } else if (argv[1][0] == 'c') {
64dbbd40 464 fdt_chosen(fdt, 0, 0, 1);
781e09ee
GVB
465
466 /********************************************************************
467 * Create a u-boot-env node
468 ********************************************************************/
25114033 469 } else if (argv[1][0] == 'e') {
781e09ee
GVB
470 fdt_env(fdt);
471
472 /********************************************************************
473 * Create a bd_t node
474 ********************************************************************/
25114033 475 } else if (argv[1][0] == 'b') {
781e09ee
GVB
476 fdt_bd_t(fdt);
477
478 /********************************************************************
479 * Unrecognized command
480 ********************************************************************/
481 } else {
482 printf ("Usage:\n%s\n", cmdtp->usage);
483 return 1;
484 }
485
486 return 0;
487}
488
489/********************************************************************/
490
491static int fdt_valid(void)
492{
64dbbd40
GVB
493 int err;
494
781e09ee 495 if (fdt == NULL) {
64dbbd40 496 printf ("The address of the fdt is invalid (NULL).\n");
781e09ee
GVB
497 return 0;
498 }
64dbbd40
GVB
499
500 err = fdt_check_header(fdt);
501 if (err == 0)
502 return 1; /* valid */
503
504 if (err < 0) {
25114033 505 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
64dbbd40
GVB
506 /*
507 * Be more informative on bad version.
508 */
509 if (err == -FDT_ERR_BADVERSION) {
510 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
511 printf (" - too old, fdt $d < %d",
512 fdt_version(fdt), FDT_FIRST_SUPPORTED_VERSION);
513 fdt = NULL;
514 }
515 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
516 printf (" - too new, fdt $d > %d",
517 fdt_version(fdt), FDT_LAST_SUPPORTED_VERSION);
518 fdt = NULL;
519 }
520 return 0;
521 }
522 printf("\n");
781e09ee
GVB
523 return 0;
524 }
525 return 1;
526}
527
528/********************************************************************/
529
530/*
531 * OF flat tree handling
532 * Written by: Pantelis Antoniou <pantelis.antoniou@gmail.com>
533 * Updated by: Matthew McClintock <msm@freescale.com>
534 * Converted to libfdt by: Gerald Van Baren <vanbaren@cideas.com>
535 */
536
537static int is_printable_string(const void *data, int len)
538{
539 const char *s = data;
540
541 /* zero length is not */
542 if (len == 0)
543 return 0;
544
545 /* must terminate with zero */
546 if (s[len - 1] != '\0')
547 return 0;
548
549 /* printable or a null byte (concatenated strings) */
550 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
551 /*
552 * If we see a null, there are three possibilities:
553 * 1) If len == 1, it is the end of the string, printable
554 * 2) Next character also a null, not printable.
555 * 3) Next character not a null, continue to check.
556 */
557 if (s[0] == '\0') {
558 if (len == 1)
559 return 1;
560 if (s[1] == '\0')
561 return 0;
562 }
563 s++;
564 len--;
565 }
566
567 /* Not the null termination, or not done yet: not printable */
568 if (*s != '\0' || (len != 0))
569 return 0;
570
571 return 1;
572}
573
574static void print_data(const void *data, int len)
575{
576 int j;
577 const u8 *s;
578
579 /* no data, don't print */
580 if (len == 0)
581 return;
582
583 /*
584 * It is a string, but it may have multiple strings (embedded '\0's).
585 */
586 if (is_printable_string(data, len)) {
587 puts("\"");
588 j = 0;
589 while (j < len) {
590 if (j > 0)
591 puts("\", \"");
592 puts(data);
593 j += strlen(data) + 1;
594 data += strlen(data) + 1;
595 }
596 puts("\"");
597 return;
598 }
599
600 switch (len) {
601 case 1: /* byte */
602 printf("<%02x>", (*(u8 *) data) & 0xff);
603 break;
604 case 2: /* half-word */
605 printf("<%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
606 break;
607 case 4: /* word */
608 printf("<%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
609 break;
610 case 8: /* double-word */
611#if __WORDSIZE == 64
612 printf("<%016llx>", be64_to_cpu(*(uint64_t *) data));
613#else
614 printf("<%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
615 data += 4;
616 printf("%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
617#endif
618 break;
619 default: /* anything else... hexdump */
620 printf("[");
621 for (j = 0, s = data; j < len; j++)
622 printf("%02x%s", s[j], j < len - 1 ? " " : "");
623 printf("]");
624
625 break;
626 }
627}
628
629/********************************************************************/
630
781e09ee
GVB
631U_BOOT_CMD(
632 fdt, 5, 0, do_fdt,
633 "fdt - flattened device tree utility commands\n",
634 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
635 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr>\n"
636 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
637 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
638 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
639 "fdt mknode <path> <node> - Create a new node after <path>\n"
640 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
641 "fdt chosen - Add/update the \"/chosen\" branch in the tree\n"
642#ifdef CONFIG_OF_HAS_UBOOT_ENV
643 "fdt env - Add/replace the \"/u-boot-env\" branch in the tree\n"
644#endif
645#ifdef CONFIG_OF_HAS_BD_T
646 "fdt bd_t - Add/replace the \"/bd_t\" branch in the tree\n"
647#endif
648 "Hints:\n"
781e09ee
GVB
649 " * If the property you are setting/printing has a '#' character,\n"
650 " you MUST escape it with a \\ character or quote it with \" or\n"
651 " it will be ignored as a comment.\n"
652 " * If the value has spaces in it, you MUST escape the spaces with\n"
653 " \\ characters or quote it with \"\"\n"
654 "Examples: fdt print / # print the whole tree\n"
655 " fdt print /cpus \"#address-cells\"\n"
656 " fdt set /cpus \"#address-cells\" \"[00 00 00 01]\"\n"
657);
658
64dbbd40 659#endif /* CONFIG_OF_LIBFDT */