blob: 6eec947fcb3ecbf572120b0b9fce7c0de2d94587 [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>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040031#include <asm/global_data.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040032#include <libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040033#include <fdt_support.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040034
35#define MAX_LEVEL 32 /* how deeply nested we will go */
Gerald Van Baren5606a362007-06-25 23:25:28 -040036#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
Joe Hershbergerf72c6922012-08-17 10:34:36 +000037#ifndef CONFIG_CMD_FDT_MAX_DUMP
38#define CONFIG_CMD_FDT_MAX_DUMP 64
39#endif
Gerald Van Barenade8ef42007-03-31 12:22:10 -040040
41/*
42 * Global data (for the gd->bd)
43 */
44DECLARE_GLOBAL_DATA_PTR;
45
Gerald Van Barenade8ef42007-03-31 12:22:10 -040046static int fdt_valid(void);
Wolfgang Denk6262d0212010-06-28 22:00:46 +020047static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
Kumar Gala1e386af2007-11-21 14:07:46 -060048static int fdt_print(const char *pathp, char *prop, int depth);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000049static int is_printable_string(const void *data, int len);
Gerald Van Barenade8ef42007-03-31 12:22:10 -040050
Gerald Van Barenade8ef42007-03-31 12:22:10 -040051/*
Gerald Van Barene81ce612008-06-10 22:15:58 -040052 * The working_fdt points to our working flattened device tree.
53 */
54struct fdt_header *working_fdt;
55
Kumar Gala9f4e7e42008-08-15 08:24:39 -050056void set_working_fdt_addr(void *addr)
57{
58 char buf[17];
59
60 working_fdt = addr;
61
62 sprintf(buf, "%lx", (unsigned long)addr);
63 setenv("fdtaddr", buf);
64}
65
Gerald Van Barene81ce612008-06-10 22:15:58 -040066/*
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000067 * Get a value from the fdt and format it to be set in the environment
68 */
69static int fdt_value_setenv(const void *nodep, int len, const char *var)
70{
71 if (is_printable_string(nodep, len))
72 setenv(var, (void *)nodep);
73 else if (len == 4) {
74 char buf[11];
75
76 sprintf(buf, "0x%08X", *(uint32_t *)nodep);
77 setenv(var, buf);
78 } else if (len%4 == 0 && len <= 20) {
79 /* Needed to print things like sha1 hashes. */
80 char buf[41];
81 int i;
82
83 for (i = 0; i < len; i += sizeof(unsigned int))
84 sprintf(buf + (i * 2), "%08x",
85 *(unsigned int *)(nodep + i));
86 setenv(var, buf);
87 } else {
88 printf("error: unprintable value\n");
89 return 1;
90 }
91 return 0;
92}
93
94/*
Gerald Van Barenade8ef42007-03-31 12:22:10 -040095 * Flattened Device Tree command, see the help for parameter definitions.
96 */
Kim Phillipsdc00a682012-10-29 13:34:31 +000097static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Gerald Van Barenade8ef42007-03-31 12:22:10 -040098{
Wolfgang Denk3b683112010-07-17 01:06:04 +020099 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000100 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400101
Wolfgang Denk3b683112010-07-17 01:06:04 +0200102 /*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400103 * Set the address of the fdt
Wolfgang Denk3b683112010-07-17 01:06:04 +0200104 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400105 if (argv[1][0] == 'a') {
Kumar Gala9f4e7e42008-08-15 08:24:39 -0500106 unsigned long addr;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400107 /*
108 * Set the address [and length] of the fdt.
109 */
Kumar Gala708a18b2008-08-15 08:24:35 -0500110 if (argc == 2) {
111 if (!fdt_valid()) {
112 return 1;
113 }
114 printf("The address of the fdt is %p\n", working_fdt);
115 return 0;
116 }
117
Kumar Gala9f4e7e42008-08-15 08:24:39 -0500118 addr = simple_strtoul(argv[2], NULL, 16);
119 set_working_fdt_addr((void *)addr);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400120
121 if (!fdt_valid()) {
122 return 1;
123 }
124
125 if (argc >= 4) {
126 int len;
127 int err;
128 /*
129 * Optional new length
130 */
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500131 len = simple_strtoul(argv[3], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500132 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400133 printf ("New length %d < existing length %d, "
134 "ignoring.\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500135 len, fdt_totalsize(working_fdt));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400136 } else {
137 /*
138 * Open in place with a new length.
139 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500140 err = fdt_open_into(working_fdt, working_fdt, len);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400141 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 }
145 }
146 }
147
Marek Vasutaeccfee2012-09-05 08:34:44 +0200148 return CMD_RET_SUCCESS;
149 }
150
151 if (!working_fdt) {
152 puts(
153 "No FDT memory address configured. Please configure\n"
154 "the FDT address via \"fdt addr <address>\" command.\n"
155 "Aborting!\n");
156 return CMD_RET_FAILURE;
157 }
158
Wolfgang Denk3b683112010-07-17 01:06:04 +0200159 /*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500160 * Move the working_fdt
Wolfgang Denk3b683112010-07-17 01:06:04 +0200161 */
Marek Vasutaeccfee2012-09-05 08:34:44 +0200162 if (strncmp(argv[1], "mo", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400163 struct fdt_header *newaddr;
164 int len;
165 int err;
166
Wolfgang Denk3b683112010-07-17 01:06:04 +0200167 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000168 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400169
170 /*
171 * Set the address and length of the fdt.
172 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500173 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400174 if (!fdt_valid()) {
175 return 1;
176 }
177
Gerald Van Barene596a162007-05-16 22:39:59 -0400178 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren4d223922007-04-25 22:47:15 -0400179
180 /*
181 * If the user specifies a length, use that. Otherwise use the
182 * current length.
183 */
184 if (argc <= 4) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500185 len = fdt_totalsize(working_fdt);
Gerald Van Baren4d223922007-04-25 22:47:15 -0400186 } else {
187 len = simple_strtoul(argv[4], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500188 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400189 printf ("New length 0x%X < existing length "
190 "0x%X, aborting.\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500191 len, fdt_totalsize(working_fdt));
Gerald Van Baren4d223922007-04-25 22:47:15 -0400192 return 1;
193 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400194 }
195
196 /*
197 * Copy to the new location.
198 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500199 err = fdt_open_into(working_fdt, newaddr, len);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400200 if (err != 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400201 printf ("libfdt fdt_open_into(): %s\n",
202 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400203 return 1;
204 }
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500205 working_fdt = newaddr;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400206
Wolfgang Denk3b683112010-07-17 01:06:04 +0200207 /*
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400208 * Make a new node
Wolfgang Denk3b683112010-07-17 01:06:04 +0200209 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400210 } else if (strncmp(argv[1], "mk", 2) == 0) {
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400211 char *pathp; /* path */
212 char *nodep; /* new node to add */
213 int nodeoffset; /* node offset from libfdt */
214 int err;
215
216 /*
217 * Parameters: Node path, new node to be appended to the path.
218 */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200219 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000220 return CMD_RET_USAGE;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400221
222 pathp = argv[2];
223 nodep = argv[3];
224
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500225 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400226 if (nodeoffset < 0) {
227 /*
228 * Not found or something else bad happened.
229 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500230 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400231 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400232 return 1;
233 }
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500234 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400235 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400236 printf ("libfdt fdt_add_subnode(): %s\n",
237 fdt_strerror(err));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400238 return 1;
239 }
240
Wolfgang Denk3b683112010-07-17 01:06:04 +0200241 /*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500242 * Set the value of a property in the working_fdt.
Wolfgang Denk3b683112010-07-17 01:06:04 +0200243 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400244 } else if (argv[1][0] == 's') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400245 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400246 char *prop; /* property */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400247 int nodeoffset; /* node offset from libfdt */
Gerald Van Barene596a162007-05-16 22:39:59 -0400248 static char data[SCRATCHPAD]; /* storage for the property */
249 int len; /* new length of the property */
250 int ret; /* return value */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400251
252 /*
Gerald Van Barend91383a2008-01-05 14:52:04 -0500253 * Parameters: Node path, property, optional value.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400254 */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200255 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000256 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400257
258 pathp = argv[2];
259 prop = argv[3];
Gerald Van Barend91383a2008-01-05 14:52:04 -0500260 if (argc == 4) {
261 len = 0;
262 } else {
Andy Fleming46891472008-03-31 20:45:56 -0500263 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
Gerald Van Barend91383a2008-01-05 14:52:04 -0500264 if (ret != 0)
265 return ret;
266 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400267
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500268 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400269 if (nodeoffset < 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400270 /*
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400271 * Not found or something else bad happened.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400272 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500273 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400274 fdt_strerror(nodeoffset));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400275 return 1;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400276 }
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400277
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500278 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400279 if (ret < 0) {
280 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
281 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400282 }
283
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000284 /********************************************************************
285 * Get the value of a property in the working_fdt.
286 ********************************************************************/
287 } else if (argv[1][0] == 'g') {
288 char *subcmd; /* sub-command */
289 char *pathp; /* path */
290 char *prop; /* property */
291 char *var; /* variable to store result */
292 int nodeoffset; /* node offset from libfdt */
293 const void *nodep; /* property node pointer */
294 int len = 0; /* new length of the property */
295
296 /*
297 * Parameters: Node path, property, optional value.
298 */
299 if (argc < 5)
300 return CMD_RET_USAGE;
301
302 subcmd = argv[2];
303
304 if (argc < 6 && subcmd[0] != 's')
305 return CMD_RET_USAGE;
306
307 var = argv[3];
308 pathp = argv[4];
309 prop = argv[5];
310
311 nodeoffset = fdt_path_offset(working_fdt, pathp);
312 if (nodeoffset < 0) {
313 /*
314 * Not found or something else bad happened.
315 */
316 printf("libfdt fdt_path_offset() returned %s\n",
317 fdt_strerror(nodeoffset));
318 return 1;
319 }
320
321 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
322 int reqIndex = -1;
323 int startDepth = fdt_node_depth(
324 working_fdt, nodeoffset);
325 int curDepth = startDepth;
326 int curIndex = -1;
327 int nextNodeOffset = fdt_next_node(
328 working_fdt, nodeoffset, &curDepth);
329
330 if (subcmd[0] == 'n')
331 reqIndex = simple_strtoul(argv[5], NULL, 16);
332
333 while (curDepth > startDepth) {
334 if (curDepth == startDepth + 1)
335 curIndex++;
336 if (subcmd[0] == 'n' && curIndex == reqIndex) {
337 const char *nodeName = fdt_get_name(
338 working_fdt, nextNodeOffset, NULL);
339
340 setenv(var, (char *)nodeName);
341 return 0;
342 }
343 nextNodeOffset = fdt_next_node(
344 working_fdt, nextNodeOffset, &curDepth);
345 if (nextNodeOffset < 0)
346 break;
347 }
348 if (subcmd[0] == 's') {
349 /* get the num nodes at this level */
350 char buf[11];
351
352 sprintf(buf, "%d", curIndex + 1);
353 setenv(var, buf);
354 } else {
355 /* node index not found */
356 printf("libfdt node not found\n");
357 return 1;
358 }
359 } else {
360 nodep = fdt_getprop(
361 working_fdt, nodeoffset, prop, &len);
362 if (len == 0) {
363 /* no property value */
364 setenv(var, "");
365 return 0;
366 } else if (len > 0) {
367 if (subcmd[0] == 'v') {
368 int ret;
369
370 ret = fdt_value_setenv(nodep, len, var);
371 if (ret != 0)
372 return ret;
373 } else if (subcmd[0] == 'a') {
374 /* Get address */
375 char buf[11];
376
Tom Rini1db8b532012-10-29 14:53:18 +0000377 sprintf(buf, "0x%p", nodep);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000378 setenv(var, buf);
379 } else if (subcmd[0] == 's') {
380 /* Get size */
381 char buf[11];
382
383 sprintf(buf, "0x%08X", len);
384 setenv(var, buf);
385 } else
386 return CMD_RET_USAGE;
387 return 0;
388 } else {
389 printf("libfdt fdt_getprop(): %s\n",
390 fdt_strerror(len));
391 return 1;
392 }
393 }
394
Wolfgang Denk3b683112010-07-17 01:06:04 +0200395 /*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400396 * Print (recursive) / List (single level)
Wolfgang Denk3b683112010-07-17 01:06:04 +0200397 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400398 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400399 int depth = MAX_LEVEL; /* how deep to print */
400 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400401 char *prop; /* property */
402 int ret; /* return value */
Kumar Gala51ab7f92007-10-25 16:15:07 -0500403 static char root[2] = "/";
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400404
405 /*
406 * list is an alias for print, but limited to 1 level
407 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400408 if (argv[1][0] == 'l') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400409 depth = 1;
410 }
411
412 /*
413 * Get the starting path. The root node is an oddball,
414 * the offset is zero and has no name.
415 */
Kumar Gala51ab7f92007-10-25 16:15:07 -0500416 if (argc == 2)
417 pathp = root;
418 else
419 pathp = argv[2];
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400420 if (argc > 3)
421 prop = argv[3];
422 else
423 prop = NULL;
424
Gerald Van Barene596a162007-05-16 22:39:59 -0400425 ret = fdt_print(pathp, prop, depth);
426 if (ret != 0)
427 return ret;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400428
Wolfgang Denk3b683112010-07-17 01:06:04 +0200429 /*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400430 * Remove a property/node
Wolfgang Denk3b683112010-07-17 01:06:04 +0200431 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400432 } else if (strncmp(argv[1], "rm", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400433 int nodeoffset; /* node offset from libfdt */
434 int err;
435
436 /*
437 * Get the path. The root node is an oddball, the offset
438 * is zero and has no name.
439 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500440 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400441 if (nodeoffset < 0) {
442 /*
443 * Not found or something else bad happened.
444 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500445 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400446 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400447 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400448 }
449 /*
450 * Do the delete. A fourth parameter means delete a property,
451 * otherwise delete the node.
452 */
453 if (argc > 3) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500454 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400455 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400456 printf("libfdt fdt_delprop(): %s\n",
457 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400458 return err;
459 }
460 } else {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500461 err = fdt_del_node(working_fdt, nodeoffset);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400462 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400463 printf("libfdt fdt_del_node(): %s\n",
464 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400465 return err;
466 }
467 }
Kumar Galab79da7b2008-02-15 03:34:36 -0600468
Wolfgang Denk3b683112010-07-17 01:06:04 +0200469 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600470 * Display header info
Wolfgang Denk3b683112010-07-17 01:06:04 +0200471 */
Kumar Galab79da7b2008-02-15 03:34:36 -0600472 } else if (argv[1][0] == 'h') {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500473 u32 version = fdt_version(working_fdt);
474 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
475 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
476 fdt_totalsize(working_fdt));
477 printf("off_dt_struct:\t\t0x%x\n",
478 fdt_off_dt_struct(working_fdt));
479 printf("off_dt_strings:\t\t0x%x\n",
480 fdt_off_dt_strings(working_fdt));
481 printf("off_mem_rsvmap:\t\t0x%x\n",
482 fdt_off_mem_rsvmap(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600483 printf("version:\t\t%d\n", version);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500484 printf("last_comp_version:\t%d\n",
485 fdt_last_comp_version(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600486 if (version >= 2)
487 printf("boot_cpuid_phys:\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500488 fdt_boot_cpuid_phys(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600489 if (version >= 3)
490 printf("size_dt_strings:\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500491 fdt_size_dt_strings(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600492 if (version >= 17)
493 printf("size_dt_struct:\t\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500494 fdt_size_dt_struct(working_fdt));
495 printf("number mem_rsv:\t\t0x%x\n",
496 fdt_num_mem_rsv(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600497 printf("\n");
498
Wolfgang Denk3b683112010-07-17 01:06:04 +0200499 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600500 * Set boot cpu id
Wolfgang Denk3b683112010-07-17 01:06:04 +0200501 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400502 } else if (strncmp(argv[1], "boo", 3) == 0) {
Kumar Galab79da7b2008-02-15 03:34:36 -0600503 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500504 fdt_set_boot_cpuid_phys(working_fdt, tmp);
Kumar Galab79da7b2008-02-15 03:34:36 -0600505
Wolfgang Denk3b683112010-07-17 01:06:04 +0200506 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600507 * memory command
Wolfgang Denk3b683112010-07-17 01:06:04 +0200508 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400509 } else if (strncmp(argv[1], "me", 2) == 0) {
Kumar Galab79da7b2008-02-15 03:34:36 -0600510 uint64_t addr, size;
511 int err;
Heiko Schocher0f602e12009-12-03 11:21:21 +0100512 addr = simple_strtoull(argv[2], NULL, 16);
513 size = simple_strtoull(argv[3], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500514 err = fdt_fixup_memory(working_fdt, addr, size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600515 if (err < 0)
516 return err;
517
Wolfgang Denk3b683112010-07-17 01:06:04 +0200518 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600519 * mem reserve commands
Wolfgang Denk3b683112010-07-17 01:06:04 +0200520 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400521 } else if (strncmp(argv[1], "rs", 2) == 0) {
Kumar Galab79da7b2008-02-15 03:34:36 -0600522 if (argv[2][0] == 'p') {
523 uint64_t addr, size;
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500524 int total = fdt_num_mem_rsv(working_fdt);
Kumar Galab79da7b2008-02-15 03:34:36 -0600525 int j, err;
526 printf("index\t\t start\t\t size\n");
527 printf("-------------------------------"
528 "-----------------\n");
529 for (j = 0; j < total; j++) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500530 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600531 if (err < 0) {
532 printf("libfdt fdt_get_mem_rsv(): %s\n",
533 fdt_strerror(err));
534 return err;
535 }
536 printf(" %x\t%08x%08x\t%08x%08x\n", j,
537 (u32)(addr >> 32),
538 (u32)(addr & 0xffffffff),
539 (u32)(size >> 32),
540 (u32)(size & 0xffffffff));
541 }
542 } else if (argv[2][0] == 'a') {
543 uint64_t addr, size;
544 int err;
Kumar Galab79da7b2008-02-15 03:34:36 -0600545 addr = simple_strtoull(argv[3], NULL, 16);
546 size = simple_strtoull(argv[4], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500547 err = fdt_add_mem_rsv(working_fdt, addr, size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600548
549 if (err < 0) {
550 printf("libfdt fdt_add_mem_rsv(): %s\n",
551 fdt_strerror(err));
552 return err;
553 }
554 } else if (argv[2][0] == 'd') {
555 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500556 int err = fdt_del_mem_rsv(working_fdt, idx);
Kumar Galab79da7b2008-02-15 03:34:36 -0600557
558 if (err < 0) {
559 printf("libfdt fdt_del_mem_rsv(): %s\n",
560 fdt_strerror(err));
561 return err;
562 }
563 } else {
564 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +0000565 return CMD_RET_USAGE;
Kumar Galab79da7b2008-02-15 03:34:36 -0600566 }
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500567 }
Gerald Van Baren5606a362007-06-25 23:25:28 -0400568#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500569 /* Call the board-specific fixup routine */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400570 else if (strncmp(argv[1], "boa", 3) == 0)
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500571 ft_board_setup(working_fdt, gd->bd);
Gerald Van Baren5606a362007-06-25 23:25:28 -0400572#endif
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500573 /* Create a chosen node */
Kumar Galafb5dcd52008-08-15 08:24:34 -0500574 else if (argv[1][0] == 'c') {
575 unsigned long initrd_start = 0, initrd_end = 0;
576
Wolfgang Denk3b683112010-07-17 01:06:04 +0200577 if ((argc != 2) && (argc != 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000578 return CMD_RET_USAGE;
Kumar Galafb5dcd52008-08-15 08:24:34 -0500579
580 if (argc == 4) {
581 initrd_start = simple_strtoul(argv[2], NULL, 16);
582 initrd_end = simple_strtoul(argv[3], NULL, 16);
583 }
584
Heiko Schocherd29abae2008-09-11 08:11:23 +0200585 fdt_chosen(working_fdt, 1);
586 fdt_initrd(working_fdt, initrd_start, initrd_end, 1);
Kumar Gala58911df2008-08-15 08:24:44 -0500587 }
588 /* resize the fdt */
589 else if (strncmp(argv[1], "re", 2) == 0) {
590 fdt_resize(working_fdt);
591 }
592 else {
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500593 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +0000594 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400595 }
596
597 return 0;
598}
599
Gerald Van Barene596a162007-05-16 22:39:59 -0400600/****************************************************************************/
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400601
602static int fdt_valid(void)
603{
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400604 int err;
605
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500606 if (working_fdt == NULL) {
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400607 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400608 return 0;
609 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400610
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500611 err = fdt_check_header(working_fdt);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400612 if (err == 0)
613 return 1; /* valid */
614
615 if (err < 0) {
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400616 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400617 /*
618 * Be more informative on bad version.
619 */
620 if (err == -FDT_ERR_BADVERSION) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500621 if (fdt_version(working_fdt) <
622 FDT_FIRST_SUPPORTED_VERSION) {
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700623 printf (" - too old, fdt %d < %d",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500624 fdt_version(working_fdt),
Gerald Van Barene596a162007-05-16 22:39:59 -0400625 FDT_FIRST_SUPPORTED_VERSION);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500626 working_fdt = NULL;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400627 }
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500628 if (fdt_last_comp_version(working_fdt) >
629 FDT_LAST_SUPPORTED_VERSION) {
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700630 printf (" - too new, fdt %d > %d",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500631 fdt_version(working_fdt),
Gerald Van Barene596a162007-05-16 22:39:59 -0400632 FDT_LAST_SUPPORTED_VERSION);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500633 working_fdt = NULL;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400634 }
635 return 0;
636 }
637 printf("\n");
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400638 return 0;
639 }
640 return 1;
641}
642
Gerald Van Barene596a162007-05-16 22:39:59 -0400643/****************************************************************************/
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400644
645/*
Gerald Van Barene596a162007-05-16 22:39:59 -0400646 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming46891472008-03-31 20:45:56 -0500647 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denka1be4762008-05-20 16:00:29 +0200648 * C conventions.
Gerald Van Barene596a162007-05-16 22:39:59 -0400649 * [00 11 22 .. nn] - byte stream
650 * "string" - If the the value doesn't start with "<" or "[", it is
651 * treated as a string. Note that the quotes are
652 * stripped by the parser before we get the string.
Andy Fleming46891472008-03-31 20:45:56 -0500653 * newval: An array of strings containing the new property as specified
Wolfgang Denka1be4762008-05-20 16:00:29 +0200654 * on the command line
Andy Fleming46891472008-03-31 20:45:56 -0500655 * count: The number of strings in the array
656 * data: A bytestream to be placed in the property
657 * len: The length of the resulting bytestream
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400658 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200659static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
Gerald Van Barene596a162007-05-16 22:39:59 -0400660{
661 char *cp; /* temporary char pointer */
Andy Fleming46891472008-03-31 20:45:56 -0500662 char *newp; /* temporary newval char pointer */
Gerald Van Barene596a162007-05-16 22:39:59 -0400663 unsigned long tmp; /* holds converted values */
Andy Fleming46891472008-03-31 20:45:56 -0500664 int stridx = 0;
Gerald Van Barene596a162007-05-16 22:39:59 -0400665
Andy Fleming46891472008-03-31 20:45:56 -0500666 *len = 0;
667 newp = newval[0];
668
669 /* An array of cells */
670 if (*newp == '<') {
671 newp++;
672 while ((*newp != '>') && (stridx < count)) {
673 /*
674 * Keep searching until we find that last ">"
675 * That way users don't have to escape the spaces
676 */
677 if (*newp == '\0') {
678 newp = newval[++stridx];
679 continue;
680 }
681
682 cp = newp;
683 tmp = simple_strtoul(cp, &newp, 0);
Kim Phillipsdc00a682012-10-29 13:34:31 +0000684 *(__be32 *)data = __cpu_to_be32(tmp);
Andy Fleming46891472008-03-31 20:45:56 -0500685 data += 4;
686 *len += 4;
687
688 /* If the ptr didn't advance, something went wrong */
689 if ((newp - cp) <= 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400690 printf("Sorry, I could not convert \"%s\"\n",
691 cp);
692 return 1;
693 }
Andy Fleming46891472008-03-31 20:45:56 -0500694
695 while (*newp == ' ')
696 newp++;
Gerald Van Barene596a162007-05-16 22:39:59 -0400697 }
Andy Fleming46891472008-03-31 20:45:56 -0500698
699 if (*newp != '>') {
700 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400701 return 1;
702 }
Andy Fleming46891472008-03-31 20:45:56 -0500703 } else if (*newp == '[') {
Gerald Van Barene596a162007-05-16 22:39:59 -0400704 /*
705 * Byte stream. Convert the values.
706 */
Andy Fleming46891472008-03-31 20:45:56 -0500707 newp++;
Ken MacLeodd028afa2009-09-11 15:16:18 -0500708 while ((stridx < count) && (*newp != ']')) {
Andy Fleming46891472008-03-31 20:45:56 -0500709 while (*newp == ' ')
710 newp++;
Ken MacLeodd028afa2009-09-11 15:16:18 -0500711 if (*newp == '\0') {
Andy Fleming46891472008-03-31 20:45:56 -0500712 newp = newval[++stridx];
Ken MacLeodd028afa2009-09-11 15:16:18 -0500713 continue;
714 }
715 if (!isxdigit(*newp))
716 break;
717 tmp = simple_strtoul(newp, &newp, 16);
718 *data++ = tmp & 0xFF;
719 *len = *len + 1;
Gerald Van Barene596a162007-05-16 22:39:59 -0400720 }
Andy Fleming46891472008-03-31 20:45:56 -0500721 if (*newp != ']') {
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700722 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400723 return 1;
724 }
725 } else {
726 /*
Ken MacLeodd028afa2009-09-11 15:16:18 -0500727 * Assume it is one or more strings. Copy it into our
728 * data area for convenience (including the
729 * terminating '\0's).
Gerald Van Barene596a162007-05-16 22:39:59 -0400730 */
Andy Fleming46891472008-03-31 20:45:56 -0500731 while (stridx < count) {
Ken MacLeodd028afa2009-09-11 15:16:18 -0500732 size_t length = strlen(newp) + 1;
Andy Fleming46891472008-03-31 20:45:56 -0500733 strcpy(data, newp);
Ken MacLeodd028afa2009-09-11 15:16:18 -0500734 data += length;
735 *len += length;
Andy Fleming46891472008-03-31 20:45:56 -0500736 newp = newval[++stridx];
737 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400738 }
739 return 0;
740}
741
742/****************************************************************************/
743
744/*
745 * Heuristic to guess if this is a string or concatenated strings.
746 */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400747
748static int is_printable_string(const void *data, int len)
749{
750 const char *s = data;
751
752 /* zero length is not */
753 if (len == 0)
754 return 0;
755
Joe Hershbergerea2243b2012-08-17 10:34:38 +0000756 /* must terminate with zero or '\n' */
757 if (s[len - 1] != '\0' && s[len - 1] != '\n')
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400758 return 0;
759
760 /* printable or a null byte (concatenated strings) */
Joe Hershbergerea2243b2012-08-17 10:34:38 +0000761 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400762 /*
763 * If we see a null, there are three possibilities:
764 * 1) If len == 1, it is the end of the string, printable
765 * 2) Next character also a null, not printable.
766 * 3) Next character not a null, continue to check.
767 */
768 if (s[0] == '\0') {
769 if (len == 1)
770 return 1;
771 if (s[1] == '\0')
772 return 0;
773 }
774 s++;
775 len--;
776 }
777
778 /* Not the null termination, or not done yet: not printable */
779 if (*s != '\0' || (len != 0))
780 return 0;
781
782 return 1;
783}
784
Gerald Van Barene596a162007-05-16 22:39:59 -0400785
786/*
787 * Print the property in the best format, a heuristic guess. Print as
788 * a string, concatenated strings, a byte, word, double word, or (if all
789 * else fails) it is printed as a stream of bytes.
790 */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400791static void print_data(const void *data, int len)
792{
793 int j;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400794
795 /* no data, don't print */
796 if (len == 0)
797 return;
798
799 /*
800 * It is a string, but it may have multiple strings (embedded '\0's).
801 */
802 if (is_printable_string(data, len)) {
803 puts("\"");
804 j = 0;
805 while (j < len) {
806 if (j > 0)
807 puts("\", \"");
808 puts(data);
809 j += strlen(data) + 1;
810 data += strlen(data) + 1;
811 }
812 puts("\"");
813 return;
814 }
815
Andy Fleming46891472008-03-31 20:45:56 -0500816 if ((len %4) == 0) {
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000817 if (len > CONFIG_CMD_FDT_MAX_DUMP)
Tom Rini1db8b532012-10-29 14:53:18 +0000818 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000819 else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000820 const __be32 *p;
Andy Fleming46891472008-03-31 20:45:56 -0500821
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000822 printf("<");
823 for (j = 0, p = data; j < len/4; j++)
824 printf("0x%08x%s", fdt32_to_cpu(p[j]),
825 j < (len/4 - 1) ? " " : "");
826 printf(">");
827 }
Andy Fleming46891472008-03-31 20:45:56 -0500828 } else { /* anything else... hexdump */
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000829 if (len > CONFIG_CMD_FDT_MAX_DUMP)
Tom Rini1db8b532012-10-29 14:53:18 +0000830 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000831 else {
832 const u8 *s;
Andy Fleming46891472008-03-31 20:45:56 -0500833
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000834 printf("[");
835 for (j = 0, s = data; j < len; j++)
836 printf("%02x%s", s[j], j < len - 1 ? " " : "");
837 printf("]");
838 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400839 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400840}
841
842/****************************************************************************/
843
844/*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500845 * Recursively print (a portion of) the working_fdt. The depth parameter
Gerald Van Barene596a162007-05-16 22:39:59 -0400846 * determines how deeply nested the fdt is printed.
847 */
Kumar Gala1e386af2007-11-21 14:07:46 -0600848static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barene596a162007-05-16 22:39:59 -0400849{
Gerald Van Barene596a162007-05-16 22:39:59 -0400850 static char tabs[MAX_LEVEL+1] =
851 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
852 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Gala1e386af2007-11-21 14:07:46 -0600853 const void *nodep; /* property node pointer */
Gerald Van Barene596a162007-05-16 22:39:59 -0400854 int nodeoffset; /* node offset from libfdt */
855 int nextoffset; /* next node offset from libfdt */
856 uint32_t tag; /* tag */
857 int len; /* length of the property */
858 int level = 0; /* keep track of nesting level */
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500859 const struct fdt_property *fdt_prop;
Gerald Van Barene596a162007-05-16 22:39:59 -0400860
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500861 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400862 if (nodeoffset < 0) {
863 /*
864 * Not found or something else bad happened.
865 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500866 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400867 fdt_strerror(nodeoffset));
Gerald Van Barene596a162007-05-16 22:39:59 -0400868 return 1;
869 }
870 /*
871 * The user passed in a property as well as node path.
872 * Print only the given property and then return.
873 */
874 if (prop) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500875 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
Gerald Van Barene596a162007-05-16 22:39:59 -0400876 if (len == 0) {
877 /* no property value */
878 printf("%s %s\n", pathp, prop);
879 return 0;
880 } else if (len > 0) {
Gerald Van Barenfc1a1092007-11-23 19:43:20 -0500881 printf("%s = ", prop);
Gerald Van Barene596a162007-05-16 22:39:59 -0400882 print_data (nodep, len);
883 printf("\n");
884 return 0;
885 } else {
886 printf ("libfdt fdt_getprop(): %s\n",
887 fdt_strerror(len));
888 return 1;
889 }
890 }
891
892 /*
893 * The user passed in a node path and no property,
894 * print the node and all subnodes.
895 */
Gerald Van Barene596a162007-05-16 22:39:59 -0400896 while(level >= 0) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500897 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
Gerald Van Barene596a162007-05-16 22:39:59 -0400898 switch(tag) {
899 case FDT_BEGIN_NODE:
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500900 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500901 if (level <= depth) {
902 if (pathp == NULL)
903 pathp = "/* NULL pointer error */";
904 if (*pathp == '\0')
905 pathp = "/"; /* root is nameless */
Gerald Van Barene596a162007-05-16 22:39:59 -0400906 printf("%s%s {\n",
907 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500908 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400909 level++;
Gerald Van Barene596a162007-05-16 22:39:59 -0400910 if (level >= MAX_LEVEL) {
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500911 printf("Nested too deep, aborting.\n");
Gerald Van Barene596a162007-05-16 22:39:59 -0400912 return 1;
913 }
914 break;
915 case FDT_END_NODE:
916 level--;
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500917 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -0400918 printf("%s};\n", &tabs[MAX_LEVEL - level]);
919 if (level == 0) {
920 level = -1; /* exit the loop */
921 }
922 break;
923 case FDT_PROP:
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500924 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500925 sizeof(*fdt_prop));
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500926 pathp = fdt_string(working_fdt,
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500927 fdt32_to_cpu(fdt_prop->nameoff));
928 len = fdt32_to_cpu(fdt_prop->len);
929 nodep = fdt_prop->data;
Gerald Van Barene596a162007-05-16 22:39:59 -0400930 if (len < 0) {
931 printf ("libfdt fdt_getprop(): %s\n",
932 fdt_strerror(len));
933 return 1;
934 } else if (len == 0) {
935 /* the property has no value */
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500936 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -0400937 printf("%s%s;\n",
938 &tabs[MAX_LEVEL - level],
939 pathp);
940 } else {
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500941 if (level <= depth) {
Gerald Van Barenfc1a1092007-11-23 19:43:20 -0500942 printf("%s%s = ",
Gerald Van Barene596a162007-05-16 22:39:59 -0400943 &tabs[MAX_LEVEL - level],
944 pathp);
945 print_data (nodep, len);
946 printf(";\n");
947 }
948 }
949 break;
950 case FDT_NOP:
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700951 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barene596a162007-05-16 22:39:59 -0400952 break;
953 case FDT_END:
954 return 1;
955 default:
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500956 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -0400957 printf("Unknown tag 0x%08X\n", tag);
958 return 1;
959 }
960 nodeoffset = nextoffset;
961 }
962 return 0;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400963}
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400964
965/********************************************************************/
Kim Phillipsdc00a682012-10-29 13:34:31 +0000966#ifdef CONFIG_SYS_LONGHELP
967static char fdt_help_text[] =
968 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
Gerald Van Baren5606a362007-06-25 23:25:28 -0400969#ifdef CONFIG_OF_BOARD_SETUP
970 "fdt boardsetup - Do board-specific set up\n"
971#endif
Gerald Van Baren7655c092008-01-05 15:33:29 -0500972 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Kumar Gala58911df2008-08-15 08:24:44 -0500973 "fdt resize - Resize fdt to size + padding to 4k addr\n"
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400974 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
975 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000976 "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
977 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
978 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
979 "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n"
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400980 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
981 "fdt mknode <path> <node> - Create a new node after <path>\n"
982 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Kumar Galab79da7b2008-02-15 03:34:36 -0600983 "fdt header - Display header info\n"
984 "fdt bootcpu <id> - Set boot cpuid\n"
985 "fdt memory <addr> <size> - Add/Update memory node\n"
986 "fdt rsvmem print - Show current mem reserves\n"
987 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
988 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Kumar Galafb5dcd52008-08-15 08:24:34 -0500989 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
990 " <start>/<end> - initrd start/end addr\n"
Gerald Van Baren0989fcf2008-08-22 14:37:05 -0400991 "NOTE: Dereference aliases by omiting the leading '/', "
Kim Phillipsdc00a682012-10-29 13:34:31 +0000992 "e.g. fdt print ethernet0.";
993#endif
994
995U_BOOT_CMD(
996 fdt, 255, 0, do_fdt,
997 "flattened device tree utility commands", fdt_help_text
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400998);