blob: 9e2de34737f358fab0b9619b20b1e34ec0094e5c [file] [log] [blame]
Gerald Van Baren781e09e2007-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>
Gerald Van Baren781e09e2007-03-31 12:22:10 -040031#include <asm/global_data.h>
32#include <fdt.h>
33#include <libfdt.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040034#include <fdt_support.h>
Gerald Van Baren781e09e2007-03-31 12:22:10 -040035
36#define MAX_LEVEL 32 /* how deeply nested we will go */
Gerald Van Barenfd61e552007-06-25 23:25:28 -040037#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
Joe Hershbergerf0a29d42012-08-17 10:34:36 +000038#ifndef CONFIG_CMD_FDT_MAX_DUMP
39#define CONFIG_CMD_FDT_MAX_DUMP 64
40#endif
Gerald Van Baren781e09e2007-03-31 12:22:10 -040041
42/*
43 * Global data (for the gd->bd)
44 */
45DECLARE_GLOBAL_DATA_PTR;
46
Gerald Van Baren781e09e2007-03-31 12:22:10 -040047static int fdt_valid(void);
Wolfgang Denk54841ab2010-06-28 22:00:46 +020048static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
Kumar Galadbaf07c2007-11-21 14:07:46 -060049static int fdt_print(const char *pathp, char *prop, int depth);
Joe Hershbergerbc802952012-08-17 10:34:37 +000050static int is_printable_string(const void *data, int len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040051
Gerald Van Baren781e09e2007-03-31 12:22:10 -040052/*
Gerald Van Barenae9e97f2008-06-10 22:15:58 -040053 * The working_fdt points to our working flattened device tree.
54 */
55struct fdt_header *working_fdt;
56
Kumar Gala54f9c862008-08-15 08:24:39 -050057void set_working_fdt_addr(void *addr)
58{
59 char buf[17];
60
61 working_fdt = addr;
62
63 sprintf(buf, "%lx", (unsigned long)addr);
64 setenv("fdtaddr", buf);
65}
66
Gerald Van Barenae9e97f2008-06-10 22:15:58 -040067/*
Joe Hershbergerbc802952012-08-17 10:34:37 +000068 * Get a value from the fdt and format it to be set in the environment
69 */
70static int fdt_value_setenv(const void *nodep, int len, const char *var)
71{
72 if (is_printable_string(nodep, len))
73 setenv(var, (void *)nodep);
74 else if (len == 4) {
75 char buf[11];
76
77 sprintf(buf, "0x%08X", *(uint32_t *)nodep);
78 setenv(var, buf);
79 } else if (len%4 == 0 && len <= 20) {
80 /* Needed to print things like sha1 hashes. */
81 char buf[41];
82 int i;
83
84 for (i = 0; i < len; i += sizeof(unsigned int))
85 sprintf(buf + (i * 2), "%08x",
86 *(unsigned int *)(nodep + i));
87 setenv(var, buf);
88 } else {
89 printf("error: unprintable value\n");
90 return 1;
91 }
92 return 0;
93}
94
95/*
Gerald Van Baren781e09e2007-03-31 12:22:10 -040096 * Flattened Device Tree command, see the help for parameter definitions.
97 */
Kim Phillips088f1b12012-10-29 13:34:31 +000098static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Gerald Van Baren781e09e2007-03-31 12:22:10 -040099{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200100 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000101 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400102
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200103 /*
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400104 * Set the address of the fdt
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200105 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400106 if (argv[1][0] == 'a') {
Kumar Gala54f9c862008-08-15 08:24:39 -0500107 unsigned long addr;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400108 /*
109 * Set the address [and length] of the fdt.
110 */
Kumar Gala7dbc38a2008-08-15 08:24:35 -0500111 if (argc == 2) {
112 if (!fdt_valid()) {
113 return 1;
114 }
115 printf("The address of the fdt is %p\n", working_fdt);
116 return 0;
117 }
118
Kumar Gala54f9c862008-08-15 08:24:39 -0500119 addr = simple_strtoul(argv[2], NULL, 16);
120 set_working_fdt_addr((void *)addr);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400121
122 if (!fdt_valid()) {
123 return 1;
124 }
125
126 if (argc >= 4) {
127 int len;
128 int err;
129 /*
130 * Optional new length
131 */
Gerald Van Baren91623522007-11-22 17:23:23 -0500132 len = simple_strtoul(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500133 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400134 printf ("New length %d < existing length %d, "
135 "ignoring.\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500136 len, fdt_totalsize(working_fdt));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400137 } else {
138 /*
139 * Open in place with a new length.
140 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500141 err = fdt_open_into(working_fdt, working_fdt, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400142 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400143 printf ("libfdt fdt_open_into(): %s\n",
144 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400145 }
146 }
147 }
148
Marek Vasute02c9452012-09-05 08:34:44 +0200149 return CMD_RET_SUCCESS;
150 }
151
152 if (!working_fdt) {
153 puts(
154 "No FDT memory address configured. Please configure\n"
155 "the FDT address via \"fdt addr <address>\" command.\n"
156 "Aborting!\n");
157 return CMD_RET_FAILURE;
158 }
159
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200160 /*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500161 * Move the working_fdt
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200162 */
Marek Vasute02c9452012-09-05 08:34:44 +0200163 if (strncmp(argv[1], "mo", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400164 struct fdt_header *newaddr;
165 int len;
166 int err;
167
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200168 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000169 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400170
171 /*
172 * Set the address and length of the fdt.
173 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500174 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400175 if (!fdt_valid()) {
176 return 1;
177 }
178
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400179 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400180
181 /*
182 * If the user specifies a length, use that. Otherwise use the
183 * current length.
184 */
185 if (argc <= 4) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500186 len = fdt_totalsize(working_fdt);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400187 } else {
188 len = simple_strtoul(argv[4], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500189 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400190 printf ("New length 0x%X < existing length "
191 "0x%X, aborting.\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500192 len, fdt_totalsize(working_fdt));
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400193 return 1;
194 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400195 }
196
197 /*
198 * Copy to the new location.
199 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500200 err = fdt_open_into(working_fdt, newaddr, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400201 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400202 printf ("libfdt fdt_open_into(): %s\n",
203 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400204 return 1;
205 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500206 working_fdt = newaddr;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400207
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200208 /*
Gerald Van Baren25114032007-05-12 09:47:25 -0400209 * Make a new node
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200210 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400211 } else if (strncmp(argv[1], "mk", 2) == 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400212 char *pathp; /* path */
213 char *nodep; /* new node to add */
214 int nodeoffset; /* node offset from libfdt */
215 int err;
216
217 /*
218 * Parameters: Node path, new node to be appended to the path.
219 */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200220 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000221 return CMD_RET_USAGE;
Gerald Van Baren25114032007-05-12 09:47:25 -0400222
223 pathp = argv[2];
224 nodep = argv[3];
225
Kim Phillipse489b9c2008-06-10 11:06:17 -0500226 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400227 if (nodeoffset < 0) {
228 /*
229 * Not found or something else bad happened.
230 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500231 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400232 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400233 return 1;
234 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500235 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
Gerald Van Baren25114032007-05-12 09:47:25 -0400236 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400237 printf ("libfdt fdt_add_subnode(): %s\n",
238 fdt_strerror(err));
Gerald Van Baren25114032007-05-12 09:47:25 -0400239 return 1;
240 }
241
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200242 /*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500243 * Set the value of a property in the working_fdt.
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200244 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400245 } else if (argv[1][0] == 's') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400246 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400247 char *prop; /* property */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400248 int nodeoffset; /* node offset from libfdt */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400249 static char data[SCRATCHPAD]; /* storage for the property */
250 int len; /* new length of the property */
251 int ret; /* return value */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400252
253 /*
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500254 * Parameters: Node path, property, optional value.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400255 */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200256 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000257 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400258
259 pathp = argv[2];
260 prop = argv[3];
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500261 if (argc == 4) {
262 len = 0;
263 } else {
Andy Fleming4abd8442008-03-31 20:45:56 -0500264 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500265 if (ret != 0)
266 return ret;
267 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400268
Kim Phillipse489b9c2008-06-10 11:06:17 -0500269 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400270 if (nodeoffset < 0) {
271 /*
272 * Not found or something else bad happened.
273 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500274 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400275 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400276 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400277 }
Gerald Van Baren25114032007-05-12 09:47:25 -0400278
Kim Phillipse489b9c2008-06-10 11:06:17 -0500279 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
Gerald Van Baren25114032007-05-12 09:47:25 -0400280 if (ret < 0) {
281 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
282 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400283 }
284
Joe Hershbergerbc802952012-08-17 10:34:37 +0000285 /********************************************************************
286 * Get the value of a property in the working_fdt.
287 ********************************************************************/
288 } else if (argv[1][0] == 'g') {
289 char *subcmd; /* sub-command */
290 char *pathp; /* path */
291 char *prop; /* property */
292 char *var; /* variable to store result */
293 int nodeoffset; /* node offset from libfdt */
294 const void *nodep; /* property node pointer */
295 int len = 0; /* new length of the property */
296
297 /*
298 * Parameters: Node path, property, optional value.
299 */
300 if (argc < 5)
301 return CMD_RET_USAGE;
302
303 subcmd = argv[2];
304
305 if (argc < 6 && subcmd[0] != 's')
306 return CMD_RET_USAGE;
307
308 var = argv[3];
309 pathp = argv[4];
310 prop = argv[5];
311
312 nodeoffset = fdt_path_offset(working_fdt, pathp);
313 if (nodeoffset < 0) {
314 /*
315 * Not found or something else bad happened.
316 */
317 printf("libfdt fdt_path_offset() returned %s\n",
318 fdt_strerror(nodeoffset));
319 return 1;
320 }
321
322 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
323 int reqIndex = -1;
324 int startDepth = fdt_node_depth(
325 working_fdt, nodeoffset);
326 int curDepth = startDepth;
327 int curIndex = -1;
328 int nextNodeOffset = fdt_next_node(
329 working_fdt, nodeoffset, &curDepth);
330
331 if (subcmd[0] == 'n')
332 reqIndex = simple_strtoul(argv[5], NULL, 16);
333
334 while (curDepth > startDepth) {
335 if (curDepth == startDepth + 1)
336 curIndex++;
337 if (subcmd[0] == 'n' && curIndex == reqIndex) {
338 const char *nodeName = fdt_get_name(
339 working_fdt, nextNodeOffset, NULL);
340
341 setenv(var, (char *)nodeName);
342 return 0;
343 }
344 nextNodeOffset = fdt_next_node(
345 working_fdt, nextNodeOffset, &curDepth);
346 if (nextNodeOffset < 0)
347 break;
348 }
349 if (subcmd[0] == 's') {
350 /* get the num nodes at this level */
351 char buf[11];
352
353 sprintf(buf, "%d", curIndex + 1);
354 setenv(var, buf);
355 } else {
356 /* node index not found */
357 printf("libfdt node not found\n");
358 return 1;
359 }
360 } else {
361 nodep = fdt_getprop(
362 working_fdt, nodeoffset, prop, &len);
363 if (len == 0) {
364 /* no property value */
365 setenv(var, "");
366 return 0;
367 } else if (len > 0) {
368 if (subcmd[0] == 'v') {
369 int ret;
370
371 ret = fdt_value_setenv(nodep, len, var);
372 if (ret != 0)
373 return ret;
374 } else if (subcmd[0] == 'a') {
375 /* Get address */
376 char buf[11];
377
Tom Rini085b9c32012-10-29 14:53:18 +0000378 sprintf(buf, "0x%p", nodep);
Joe Hershbergerbc802952012-08-17 10:34:37 +0000379 setenv(var, buf);
380 } else if (subcmd[0] == 's') {
381 /* Get size */
382 char buf[11];
383
384 sprintf(buf, "0x%08X", len);
385 setenv(var, buf);
386 } else
387 return CMD_RET_USAGE;
388 return 0;
389 } else {
390 printf("libfdt fdt_getprop(): %s\n",
391 fdt_strerror(len));
392 return 1;
393 }
394 }
395
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200396 /*
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400397 * Print (recursive) / List (single level)
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200398 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400399 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400400 int depth = MAX_LEVEL; /* how deep to print */
401 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400402 char *prop; /* property */
403 int ret; /* return value */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500404 static char root[2] = "/";
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400405
406 /*
407 * list is an alias for print, but limited to 1 level
408 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400409 if (argv[1][0] == 'l') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400410 depth = 1;
411 }
412
413 /*
414 * Get the starting path. The root node is an oddball,
415 * the offset is zero and has no name.
416 */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500417 if (argc == 2)
418 pathp = root;
419 else
420 pathp = argv[2];
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400421 if (argc > 3)
422 prop = argv[3];
423 else
424 prop = NULL;
425
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400426 ret = fdt_print(pathp, prop, depth);
427 if (ret != 0)
428 return ret;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400429
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200430 /*
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400431 * Remove a property/node
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200432 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400433 } else if (strncmp(argv[1], "rm", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400434 int nodeoffset; /* node offset from libfdt */
435 int err;
436
437 /*
438 * Get the path. The root node is an oddball, the offset
439 * is zero and has no name.
440 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500441 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
Gerald Van Baren25114032007-05-12 09:47:25 -0400442 if (nodeoffset < 0) {
443 /*
444 * Not found or something else bad happened.
445 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500446 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400447 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400448 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400449 }
450 /*
451 * Do the delete. A fourth parameter means delete a property,
452 * otherwise delete the node.
453 */
454 if (argc > 3) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500455 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400456 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400457 printf("libfdt fdt_delprop(): %s\n",
458 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400459 return err;
460 }
461 } else {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500462 err = fdt_del_node(working_fdt, nodeoffset);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400463 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400464 printf("libfdt fdt_del_node(): %s\n",
465 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400466 return err;
467 }
468 }
Kumar Gala804887e2008-02-15 03:34:36 -0600469
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200470 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600471 * Display header info
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200472 */
Kumar Gala804887e2008-02-15 03:34:36 -0600473 } else if (argv[1][0] == 'h') {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500474 u32 version = fdt_version(working_fdt);
475 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
476 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
477 fdt_totalsize(working_fdt));
478 printf("off_dt_struct:\t\t0x%x\n",
479 fdt_off_dt_struct(working_fdt));
480 printf("off_dt_strings:\t\t0x%x\n",
481 fdt_off_dt_strings(working_fdt));
482 printf("off_mem_rsvmap:\t\t0x%x\n",
483 fdt_off_mem_rsvmap(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600484 printf("version:\t\t%d\n", version);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500485 printf("last_comp_version:\t%d\n",
486 fdt_last_comp_version(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600487 if (version >= 2)
488 printf("boot_cpuid_phys:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500489 fdt_boot_cpuid_phys(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600490 if (version >= 3)
491 printf("size_dt_strings:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500492 fdt_size_dt_strings(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600493 if (version >= 17)
494 printf("size_dt_struct:\t\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500495 fdt_size_dt_struct(working_fdt));
496 printf("number mem_rsv:\t\t0x%x\n",
497 fdt_num_mem_rsv(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600498 printf("\n");
499
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200500 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600501 * Set boot cpu id
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200502 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400503 } else if (strncmp(argv[1], "boo", 3) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600504 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500505 fdt_set_boot_cpuid_phys(working_fdt, tmp);
Kumar Gala804887e2008-02-15 03:34:36 -0600506
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200507 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600508 * memory command
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200509 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400510 } else if (strncmp(argv[1], "me", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600511 uint64_t addr, size;
512 int err;
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100513 addr = simple_strtoull(argv[2], NULL, 16);
514 size = simple_strtoull(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500515 err = fdt_fixup_memory(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600516 if (err < 0)
517 return err;
518
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200519 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600520 * mem reserve commands
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200521 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400522 } else if (strncmp(argv[1], "rs", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600523 if (argv[2][0] == 'p') {
524 uint64_t addr, size;
Kim Phillipse489b9c2008-06-10 11:06:17 -0500525 int total = fdt_num_mem_rsv(working_fdt);
Kumar Gala804887e2008-02-15 03:34:36 -0600526 int j, err;
527 printf("index\t\t start\t\t size\n");
528 printf("-------------------------------"
529 "-----------------\n");
530 for (j = 0; j < total; j++) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500531 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
Kumar Gala804887e2008-02-15 03:34:36 -0600532 if (err < 0) {
533 printf("libfdt fdt_get_mem_rsv(): %s\n",
534 fdt_strerror(err));
535 return err;
536 }
537 printf(" %x\t%08x%08x\t%08x%08x\n", j,
538 (u32)(addr >> 32),
539 (u32)(addr & 0xffffffff),
540 (u32)(size >> 32),
541 (u32)(size & 0xffffffff));
542 }
543 } else if (argv[2][0] == 'a') {
544 uint64_t addr, size;
545 int err;
Kumar Gala804887e2008-02-15 03:34:36 -0600546 addr = simple_strtoull(argv[3], NULL, 16);
547 size = simple_strtoull(argv[4], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500548 err = fdt_add_mem_rsv(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600549
550 if (err < 0) {
551 printf("libfdt fdt_add_mem_rsv(): %s\n",
552 fdt_strerror(err));
553 return err;
554 }
555 } else if (argv[2][0] == 'd') {
556 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500557 int err = fdt_del_mem_rsv(working_fdt, idx);
Kumar Gala804887e2008-02-15 03:34:36 -0600558
559 if (err < 0) {
560 printf("libfdt fdt_del_mem_rsv(): %s\n",
561 fdt_strerror(err));
562 return err;
563 }
564 } else {
565 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000566 return CMD_RET_USAGE;
Kumar Gala804887e2008-02-15 03:34:36 -0600567 }
Kim Phillips99dffca2007-07-17 13:57:04 -0500568 }
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400569#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillips99dffca2007-07-17 13:57:04 -0500570 /* Call the board-specific fixup routine */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400571 else if (strncmp(argv[1], "boa", 3) == 0)
Kim Phillipse489b9c2008-06-10 11:06:17 -0500572 ft_board_setup(working_fdt, gd->bd);
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400573#endif
Kim Phillips99dffca2007-07-17 13:57:04 -0500574 /* Create a chosen node */
Kumar Galaf953d992008-08-15 08:24:34 -0500575 else if (argv[1][0] == 'c') {
576 unsigned long initrd_start = 0, initrd_end = 0;
577
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200578 if ((argc != 2) && (argc != 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000579 return CMD_RET_USAGE;
Kumar Galaf953d992008-08-15 08:24:34 -0500580
581 if (argc == 4) {
582 initrd_start = simple_strtoul(argv[2], NULL, 16);
583 initrd_end = simple_strtoul(argv[3], NULL, 16);
584 }
585
Heiko Schocher56844a22008-09-11 08:11:23 +0200586 fdt_chosen(working_fdt, 1);
587 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
Kumar Gala40afac22008-08-15 08:24:44 -0500588 }
589 /* resize the fdt */
590 else if (strncmp(argv[1], "re", 2) == 0) {
591 fdt_resize(working_fdt);
592 }
593 else {
Kim Phillips99dffca2007-07-17 13:57:04 -0500594 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000595 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400596 }
597
598 return 0;
599}
600
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400601/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400602
603static int fdt_valid(void)
604{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400605 int err;
606
Kim Phillipse489b9c2008-06-10 11:06:17 -0500607 if (working_fdt == NULL) {
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400608 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400609 return 0;
610 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400611
Kim Phillipse489b9c2008-06-10 11:06:17 -0500612 err = fdt_check_header(working_fdt);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400613 if (err == 0)
614 return 1; /* valid */
615
616 if (err < 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400617 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400618 /*
619 * Be more informative on bad version.
620 */
621 if (err == -FDT_ERR_BADVERSION) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500622 if (fdt_version(working_fdt) <
623 FDT_FIRST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700624 printf (" - too old, fdt %d < %d",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500625 fdt_version(working_fdt),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400626 FDT_FIRST_SUPPORTED_VERSION);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500627 working_fdt = NULL;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400628 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500629 if (fdt_last_comp_version(working_fdt) >
630 FDT_LAST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700631 printf (" - too new, fdt %d > %d",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500632 fdt_version(working_fdt),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400633 FDT_LAST_SUPPORTED_VERSION);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500634 working_fdt = NULL;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400635 }
636 return 0;
637 }
638 printf("\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400639 return 0;
640 }
641 return 1;
642}
643
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400644/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400645
646/*
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400647 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming4abd8442008-03-31 20:45:56 -0500648 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200649 * C conventions.
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400650 * [00 11 22 .. nn] - byte stream
651 * "string" - If the the value doesn't start with "<" or "[", it is
652 * treated as a string. Note that the quotes are
653 * stripped by the parser before we get the string.
Andy Fleming4abd8442008-03-31 20:45:56 -0500654 * newval: An array of strings containing the new property as specified
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200655 * on the command line
Andy Fleming4abd8442008-03-31 20:45:56 -0500656 * count: The number of strings in the array
657 * data: A bytestream to be placed in the property
658 * len: The length of the resulting bytestream
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400659 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200660static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400661{
662 char *cp; /* temporary char pointer */
Andy Fleming4abd8442008-03-31 20:45:56 -0500663 char *newp; /* temporary newval char pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400664 unsigned long tmp; /* holds converted values */
Andy Fleming4abd8442008-03-31 20:45:56 -0500665 int stridx = 0;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400666
Andy Fleming4abd8442008-03-31 20:45:56 -0500667 *len = 0;
668 newp = newval[0];
669
670 /* An array of cells */
671 if (*newp == '<') {
672 newp++;
673 while ((*newp != '>') && (stridx < count)) {
674 /*
675 * Keep searching until we find that last ">"
676 * That way users don't have to escape the spaces
677 */
678 if (*newp == '\0') {
679 newp = newval[++stridx];
680 continue;
681 }
682
683 cp = newp;
684 tmp = simple_strtoul(cp, &newp, 0);
Kim Phillips088f1b12012-10-29 13:34:31 +0000685 *(__be32 *)data = __cpu_to_be32(tmp);
Andy Fleming4abd8442008-03-31 20:45:56 -0500686 data += 4;
687 *len += 4;
688
689 /* If the ptr didn't advance, something went wrong */
690 if ((newp - cp) <= 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400691 printf("Sorry, I could not convert \"%s\"\n",
692 cp);
693 return 1;
694 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500695
696 while (*newp == ' ')
697 newp++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400698 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500699
700 if (*newp != '>') {
701 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400702 return 1;
703 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500704 } else if (*newp == '[') {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400705 /*
706 * Byte stream. Convert the values.
707 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500708 newp++;
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500709 while ((stridx < count) && (*newp != ']')) {
710 while (*newp == ' ')
711 newp++;
712 if (*newp == '\0') {
713 newp = newval[++stridx];
714 continue;
715 }
716 if (!isxdigit(*newp))
717 break;
Andy Fleming4abd8442008-03-31 20:45:56 -0500718 tmp = simple_strtoul(newp, &newp, 16);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400719 *data++ = tmp & 0xFF;
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400720 *len = *len + 1;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400721 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500722 if (*newp != ']') {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700723 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400724 return 1;
725 }
726 } else {
727 /*
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500728 * Assume it is one or more strings. Copy it into our
729 * data area for convenience (including the
730 * terminating '\0's).
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400731 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500732 while (stridx < count) {
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500733 size_t length = strlen(newp) + 1;
Andy Fleming4abd8442008-03-31 20:45:56 -0500734 strcpy(data, newp);
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500735 data += length;
736 *len += length;
Andy Fleming4abd8442008-03-31 20:45:56 -0500737 newp = newval[++stridx];
738 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400739 }
740 return 0;
741}
742
743/****************************************************************************/
744
745/*
746 * Heuristic to guess if this is a string or concatenated strings.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400747 */
748
749static int is_printable_string(const void *data, int len)
750{
751 const char *s = data;
752
753 /* zero length is not */
754 if (len == 0)
755 return 0;
756
Joe Hershberger8805bee2012-08-17 10:34:38 +0000757 /* must terminate with zero or '\n' */
758 if (s[len - 1] != '\0' && s[len - 1] != '\n')
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400759 return 0;
760
761 /* printable or a null byte (concatenated strings) */
Joe Hershberger8805bee2012-08-17 10:34:38 +0000762 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400763 /*
764 * If we see a null, there are three possibilities:
765 * 1) If len == 1, it is the end of the string, printable
766 * 2) Next character also a null, not printable.
767 * 3) Next character not a null, continue to check.
768 */
769 if (s[0] == '\0') {
770 if (len == 1)
771 return 1;
772 if (s[1] == '\0')
773 return 0;
774 }
775 s++;
776 len--;
777 }
778
779 /* Not the null termination, or not done yet: not printable */
780 if (*s != '\0' || (len != 0))
781 return 0;
782
783 return 1;
784}
785
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400786
787/*
788 * Print the property in the best format, a heuristic guess. Print as
789 * a string, concatenated strings, a byte, word, double word, or (if all
790 * else fails) it is printed as a stream of bytes.
791 */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400792static void print_data(const void *data, int len)
793{
794 int j;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400795
796 /* no data, don't print */
797 if (len == 0)
798 return;
799
800 /*
801 * It is a string, but it may have multiple strings (embedded '\0's).
802 */
803 if (is_printable_string(data, len)) {
804 puts("\"");
805 j = 0;
806 while (j < len) {
807 if (j > 0)
808 puts("\", \"");
809 puts(data);
810 j += strlen(data) + 1;
811 data += strlen(data) + 1;
812 }
813 puts("\"");
814 return;
815 }
816
Andy Fleming4abd8442008-03-31 20:45:56 -0500817 if ((len %4) == 0) {
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000818 if (len > CONFIG_CMD_FDT_MAX_DUMP)
Tom Rini085b9c32012-10-29 14:53:18 +0000819 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000820 else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000821 const __be32 *p;
Andy Fleming4abd8442008-03-31 20:45:56 -0500822
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000823 printf("<");
824 for (j = 0, p = data; j < len/4; j++)
825 printf("0x%08x%s", fdt32_to_cpu(p[j]),
826 j < (len/4 - 1) ? " " : "");
827 printf(">");
828 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500829 } else { /* anything else... hexdump */
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000830 if (len > CONFIG_CMD_FDT_MAX_DUMP)
Tom Rini085b9c32012-10-29 14:53:18 +0000831 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000832 else {
833 const u8 *s;
Andy Fleming4abd8442008-03-31 20:45:56 -0500834
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000835 printf("[");
836 for (j = 0, s = data; j < len; j++)
837 printf("%02x%s", s[j], j < len - 1 ? " " : "");
838 printf("]");
839 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400840 }
841}
842
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400843/****************************************************************************/
844
845/*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500846 * Recursively print (a portion of) the working_fdt. The depth parameter
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400847 * determines how deeply nested the fdt is printed.
848 */
Kumar Galadbaf07c2007-11-21 14:07:46 -0600849static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400850{
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400851 static char tabs[MAX_LEVEL+1] =
852 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
853 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Galadbaf07c2007-11-21 14:07:46 -0600854 const void *nodep; /* property node pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400855 int nodeoffset; /* node offset from libfdt */
856 int nextoffset; /* next node offset from libfdt */
857 uint32_t tag; /* tag */
858 int len; /* length of the property */
859 int level = 0; /* keep track of nesting level */
Gerald Van Baren91623522007-11-22 17:23:23 -0500860 const struct fdt_property *fdt_prop;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400861
Kim Phillipse489b9c2008-06-10 11:06:17 -0500862 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400863 if (nodeoffset < 0) {
864 /*
865 * Not found or something else bad happened.
866 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500867 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400868 fdt_strerror(nodeoffset));
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400869 return 1;
870 }
871 /*
872 * The user passed in a property as well as node path.
873 * Print only the given property and then return.
874 */
875 if (prop) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500876 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400877 if (len == 0) {
878 /* no property value */
879 printf("%s %s\n", pathp, prop);
880 return 0;
881 } else if (len > 0) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500882 printf("%s = ", prop);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400883 print_data (nodep, len);
884 printf("\n");
885 return 0;
886 } else {
887 printf ("libfdt fdt_getprop(): %s\n",
888 fdt_strerror(len));
889 return 1;
890 }
891 }
892
893 /*
894 * The user passed in a node path and no property,
895 * print the node and all subnodes.
896 */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400897 while(level >= 0) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500898 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400899 switch(tag) {
900 case FDT_BEGIN_NODE:
Kim Phillipse489b9c2008-06-10 11:06:17 -0500901 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
Gerald Van Baren91623522007-11-22 17:23:23 -0500902 if (level <= depth) {
903 if (pathp == NULL)
904 pathp = "/* NULL pointer error */";
905 if (*pathp == '\0')
906 pathp = "/"; /* root is nameless */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400907 printf("%s%s {\n",
908 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Baren91623522007-11-22 17:23:23 -0500909 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400910 level++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400911 if (level >= MAX_LEVEL) {
Gerald Van Baren91623522007-11-22 17:23:23 -0500912 printf("Nested too deep, aborting.\n");
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400913 return 1;
914 }
915 break;
916 case FDT_END_NODE:
917 level--;
Gerald Van Baren91623522007-11-22 17:23:23 -0500918 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400919 printf("%s};\n", &tabs[MAX_LEVEL - level]);
920 if (level == 0) {
921 level = -1; /* exit the loop */
922 }
923 break;
924 case FDT_PROP:
Kim Phillipse489b9c2008-06-10 11:06:17 -0500925 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
Gerald Van Baren91623522007-11-22 17:23:23 -0500926 sizeof(*fdt_prop));
Kim Phillipse489b9c2008-06-10 11:06:17 -0500927 pathp = fdt_string(working_fdt,
Gerald Van Baren91623522007-11-22 17:23:23 -0500928 fdt32_to_cpu(fdt_prop->nameoff));
929 len = fdt32_to_cpu(fdt_prop->len);
930 nodep = fdt_prop->data;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400931 if (len < 0) {
932 printf ("libfdt fdt_getprop(): %s\n",
933 fdt_strerror(len));
934 return 1;
935 } else if (len == 0) {
936 /* the property has no value */
Gerald Van Baren91623522007-11-22 17:23:23 -0500937 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400938 printf("%s%s;\n",
939 &tabs[MAX_LEVEL - level],
940 pathp);
941 } else {
Gerald Van Baren91623522007-11-22 17:23:23 -0500942 if (level <= depth) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500943 printf("%s%s = ",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400944 &tabs[MAX_LEVEL - level],
945 pathp);
946 print_data (nodep, len);
947 printf(";\n");
948 }
949 }
950 break;
951 case FDT_NOP:
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700952 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400953 break;
954 case FDT_END:
955 return 1;
956 default:
Gerald Van Baren91623522007-11-22 17:23:23 -0500957 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400958 printf("Unknown tag 0x%08X\n", tag);
959 return 1;
960 }
961 nodeoffset = nextoffset;
962 }
963 return 0;
964}
965
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400966/********************************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +0000967#ifdef CONFIG_SYS_LONGHELP
968static char fdt_help_text[] =
969 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400970#ifdef CONFIG_OF_BOARD_SETUP
971 "fdt boardsetup - Do board-specific set up\n"
972#endif
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500973 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Kumar Gala40afac22008-08-15 08:24:44 -0500974 "fdt resize - Resize fdt to size + padding to 4k addr\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400975 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
976 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
Joe Hershbergerbc802952012-08-17 10:34:37 +0000977 "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
978 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
979 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
980 "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400981 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
982 "fdt mknode <path> <node> - Create a new node after <path>\n"
983 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Kumar Gala804887e2008-02-15 03:34:36 -0600984 "fdt header - Display header info\n"
985 "fdt bootcpu <id> - Set boot cpuid\n"
986 "fdt memory <addr> <size> - Add/Update memory node\n"
987 "fdt rsvmem print - Show current mem reserves\n"
988 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
989 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Kumar Galaf953d992008-08-15 08:24:34 -0500990 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
991 " <start>/<end> - initrd start/end addr\n"
Gerald Van Baren109c30f2008-08-22 14:37:05 -0400992 "NOTE: Dereference aliases by omiting the leading '/', "
Kim Phillips088f1b12012-10-29 13:34:31 +0000993 "e.g. fdt print ethernet0.";
994#endif
995
996U_BOOT_CMD(
997 fdt, 255, 0, do_fdt,
998 "flattened device tree utility commands", fdt_help_text
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400999);