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