blob: f66ceca2c71276c5c199bb1ea04129cfdfd4f795 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerald Van Baren781e09e2007-03-31 12:22:10 -04002/*
3 * (C) Copyright 2007
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 * Based on code written by:
6 * Pantelis Antoniou <pantelis.antoniou@gmail.com> and
7 * Matthew McClintock <msm@freescale.com>
Gerald Van Baren781e09e2007-03-31 12:22:10 -04008 */
9
10#include <common.h>
11#include <command.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060012#include <env.h>
Simon Glass4d72caa2020-05-10 11:40:01 -060013#include <image.h>
Gerald Van Baren781e09e2007-03-31 12:22:10 -040014#include <linux/ctype.h>
15#include <linux/types.h>
Gerald Van Baren781e09e2007-03-31 12:22:10 -040016#include <asm/global_data.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040018#include <fdt_support.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050019#include <mapmem.h>
Simon Glassa92fd652013-04-20 08:42:45 +000020#include <asm/io.h>
Gerald Van Baren781e09e2007-03-31 12:22:10 -040021
22#define MAX_LEVEL 32 /* how deeply nested we will go */
Gerald Van Barenfd61e552007-06-25 23:25:28 -040023#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
Simon Glass5d927b42017-05-17 03:25:19 -060024#define CMD_FDT_MAX_DUMP 64
Gerald Van Baren781e09e2007-03-31 12:22:10 -040025
26/*
27 * Global data (for the gd->bd)
28 */
29DECLARE_GLOBAL_DATA_PTR;
30
Simon Glassd14da912013-04-20 08:42:42 +000031static int fdt_valid(struct fdt_header **blobp);
Wolfgang Denk54841ab2010-06-28 22:00:46 +020032static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
Kumar Galadbaf07c2007-11-21 14:07:46 -060033static int fdt_print(const char *pathp, char *prop, int depth);
Joe Hershbergerbc802952012-08-17 10:34:37 +000034static int is_printable_string(const void *data, int len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040035
Gerald Van Baren781e09e2007-03-31 12:22:10 -040036/*
Gerald Van Barenae9e97f2008-06-10 22:15:58 -040037 * The working_fdt points to our working flattened device tree.
38 */
39struct fdt_header *working_fdt;
40
Joe Hershberger90fbee32015-02-04 21:56:53 -060041void set_working_fdt_addr(ulong addr)
Kumar Gala54f9c862008-08-15 08:24:39 -050042{
Simon Glassa92fd652013-04-20 08:42:45 +000043 void *buf;
44
Joe Hershberger90fbee32015-02-04 21:56:53 -060045 buf = map_sysmem(addr, 0);
Simon Glassa92fd652013-04-20 08:42:45 +000046 working_fdt = buf;
Simon Glass018f5302017-08-03 12:22:10 -060047 env_set_hex("fdtaddr", addr);
Kumar Gala54f9c862008-08-15 08:24:39 -050048}
49
Gerald Van Barenae9e97f2008-06-10 22:15:58 -040050/*
Joe Hershbergerbc802952012-08-17 10:34:37 +000051 * Get a value from the fdt and format it to be set in the environment
52 */
Simon Glass382bee52017-08-03 12:22:09 -060053static int fdt_value_env_set(const void *nodep, int len, const char *var)
Joe Hershbergerbc802952012-08-17 10:34:37 +000054{
55 if (is_printable_string(nodep, len))
Simon Glass382bee52017-08-03 12:22:09 -060056 env_set(var, (void *)nodep);
Joe Hershbergerbc802952012-08-17 10:34:37 +000057 else if (len == 4) {
58 char buf[11];
59
Andreas Färberb05bf6c2017-01-09 16:08:02 +010060 sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep));
Simon Glass382bee52017-08-03 12:22:09 -060061 env_set(var, buf);
Joe Hershbergerbc802952012-08-17 10:34:37 +000062 } else if (len%4 == 0 && len <= 20) {
63 /* Needed to print things like sha1 hashes. */
64 char buf[41];
65 int i;
66
67 for (i = 0; i < len; i += sizeof(unsigned int))
68 sprintf(buf + (i * 2), "%08x",
69 *(unsigned int *)(nodep + i));
Simon Glass382bee52017-08-03 12:22:09 -060070 env_set(var, buf);
Joe Hershbergerbc802952012-08-17 10:34:37 +000071 } else {
72 printf("error: unprintable value\n");
73 return 1;
74 }
75 return 0;
76}
77
Heiko Schocher82441272018-11-15 06:06:06 +010078static const char * const fdt_member_table[] = {
79 "magic",
80 "totalsize",
81 "off_dt_struct",
82 "off_dt_strings",
83 "off_mem_rsvmap",
84 "version",
85 "last_comp_version",
86 "boot_cpuid_phys",
87 "size_dt_strings",
88 "size_dt_struct",
89};
90
91static int fdt_get_header_value(int argc, char * const argv[])
92{
93 fdt32_t *fdtp = (fdt32_t *)working_fdt;
94 ulong val;
95 int i;
96
97 if (argv[2][0] != 'g')
98 return CMD_RET_FAILURE;
99
100 for (i = 0; i < ARRAY_SIZE(fdt_member_table); i++) {
101 if (strcmp(fdt_member_table[i], argv[4]))
102 continue;
103
104 val = fdt32_to_cpu(fdtp[i]);
105 env_set_hex(argv[3], val);
106 return CMD_RET_SUCCESS;
107 }
108
109 return CMD_RET_FAILURE;
110}
111
Joe Hershbergerbc802952012-08-17 10:34:37 +0000112/*
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400113 * Flattened Device Tree command, see the help for parameter definitions.
114 */
Kim Phillips088f1b12012-10-29 13:34:31 +0000115static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400116{
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200117 if (argc < 2)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000118 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400119
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200120 /*
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400121 * Set the address of the fdt
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200122 */
Maxime Ripardf0ed68e2016-07-05 10:26:34 +0200123 if (strncmp(argv[1], "ad", 2) == 0) {
Kumar Gala54f9c862008-08-15 08:24:39 -0500124 unsigned long addr;
Simon Glass4b578652013-04-20 08:42:44 +0000125 int control = 0;
126 struct fdt_header *blob;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400127 /*
128 * Set the address [and length] of the fdt.
129 */
Simon Glass4b578652013-04-20 08:42:44 +0000130 argc -= 2;
131 argv += 2;
132/* Temporary #ifdef - some archs don't have fdt_blob yet */
133#ifdef CONFIG_OF_CONTROL
134 if (argc && !strcmp(*argv, "-c")) {
135 control = 1;
136 argc--;
137 argv++;
138 }
139#endif
140 if (argc == 0) {
141 if (control)
142 blob = (struct fdt_header *)gd->fdt_blob;
143 else
144 blob = working_fdt;
145 if (!blob || !fdt_valid(&blob))
Kumar Gala7dbc38a2008-08-15 08:24:35 -0500146 return 1;
Simon Glass4b578652013-04-20 08:42:44 +0000147 printf("The address of the fdt is %#08lx\n",
Joe Hershbergerc71a0162015-02-04 21:56:54 -0600148 control ? (ulong)map_to_sysmem(blob) :
Simon Glassbfebc8c2017-08-03 12:22:13 -0600149 env_get_hex("fdtaddr", 0));
Kumar Gala7dbc38a2008-08-15 08:24:35 -0500150 return 0;
151 }
152
Simon Glass4b578652013-04-20 08:42:44 +0000153 addr = simple_strtoul(argv[0], NULL, 16);
Simon Glassa92fd652013-04-20 08:42:45 +0000154 blob = map_sysmem(addr, 0);
Simon Glass4b578652013-04-20 08:42:44 +0000155 if (!fdt_valid(&blob))
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400156 return 1;
Simon Glass4b578652013-04-20 08:42:44 +0000157 if (control)
158 gd->fdt_blob = blob;
159 else
Joe Hershberger90fbee32015-02-04 21:56:53 -0600160 set_working_fdt_addr(addr);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400161
Simon Glass4b578652013-04-20 08:42:44 +0000162 if (argc >= 2) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400163 int len;
164 int err;
165 /*
166 * Optional new length
167 */
Simon Glass4b578652013-04-20 08:42:44 +0000168 len = simple_strtoul(argv[1], NULL, 16);
169 if (len < fdt_totalsize(blob)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400170 printf ("New length %d < existing length %d, "
171 "ignoring.\n",
Simon Glass4b578652013-04-20 08:42:44 +0000172 len, fdt_totalsize(blob));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400173 } else {
174 /*
175 * Open in place with a new length.
176 */
Simon Glass4b578652013-04-20 08:42:44 +0000177 err = fdt_open_into(blob, blob, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400178 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400179 printf ("libfdt fdt_open_into(): %s\n",
180 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400181 }
182 }
183 }
184
Marek Vasute02c9452012-09-05 08:34:44 +0200185 return CMD_RET_SUCCESS;
186 }
187
188 if (!working_fdt) {
189 puts(
190 "No FDT memory address configured. Please configure\n"
191 "the FDT address via \"fdt addr <address>\" command.\n"
192 "Aborting!\n");
193 return CMD_RET_FAILURE;
194 }
195
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200196 /*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500197 * Move the working_fdt
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200198 */
Marek Vasute02c9452012-09-05 08:34:44 +0200199 if (strncmp(argv[1], "mo", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400200 struct fdt_header *newaddr;
201 int len;
202 int err;
203
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200204 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000205 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400206
207 /*
208 * Set the address and length of the fdt.
209 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500210 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
Simon Glassd14da912013-04-20 08:42:42 +0000211 if (!fdt_valid(&working_fdt))
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400212 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400213
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400214 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400215
216 /*
217 * If the user specifies a length, use that. Otherwise use the
218 * current length.
219 */
220 if (argc <= 4) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500221 len = fdt_totalsize(working_fdt);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400222 } else {
223 len = simple_strtoul(argv[4], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500224 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400225 printf ("New length 0x%X < existing length "
226 "0x%X, aborting.\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500227 len, fdt_totalsize(working_fdt));
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400228 return 1;
229 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400230 }
231
232 /*
233 * Copy to the new location.
234 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500235 err = fdt_open_into(working_fdt, newaddr, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400236 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400237 printf ("libfdt fdt_open_into(): %s\n",
238 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400239 return 1;
240 }
Hiroyuki Yokoyamab1a7e792018-10-18 20:43:54 +0200241 set_working_fdt_addr((ulong)newaddr);
Fabien Parentf7f191e2016-11-24 15:02:18 +0100242#ifdef CONFIG_OF_SYSTEM_SETUP
243 /* Call the board-specific fixup routine */
244 } else if (strncmp(argv[1], "sys", 3) == 0) {
245 int err = ft_system_setup(working_fdt, gd->bd);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400246
Fabien Parentf7f191e2016-11-24 15:02:18 +0100247 if (err) {
248 printf("Failed to add system information to FDT: %s\n",
249 fdt_strerror(err));
250 return CMD_RET_FAILURE;
251 }
252#endif
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200253 /*
Gerald Van Baren25114032007-05-12 09:47:25 -0400254 * Make a new node
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200255 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400256 } else if (strncmp(argv[1], "mk", 2) == 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400257 char *pathp; /* path */
258 char *nodep; /* new node to add */
259 int nodeoffset; /* node offset from libfdt */
260 int err;
261
262 /*
263 * Parameters: Node path, new node to be appended to the path.
264 */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200265 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000266 return CMD_RET_USAGE;
Gerald Van Baren25114032007-05-12 09:47:25 -0400267
268 pathp = argv[2];
269 nodep = argv[3];
270
Kim Phillipse489b9c2008-06-10 11:06:17 -0500271 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400272 if (nodeoffset < 0) {
273 /*
274 * Not found or something else bad happened.
275 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500276 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400277 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400278 return 1;
279 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500280 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
Gerald Van Baren25114032007-05-12 09:47:25 -0400281 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400282 printf ("libfdt fdt_add_subnode(): %s\n",
283 fdt_strerror(err));
Gerald Van Baren25114032007-05-12 09:47:25 -0400284 return 1;
285 }
286
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200287 /*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500288 * Set the value of a property in the working_fdt.
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200289 */
Tom Warren0688b752020-03-26 15:20:44 -0700290 } else if (strncmp(argv[1], "se", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400291 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400292 char *prop; /* property */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400293 int nodeoffset; /* node offset from libfdt */
Bernhard Messerklinger6dfd65f2017-09-28 11:29:52 +0200294 static char data[SCRATCHPAD] __aligned(4);/* property storage */
Hannes Schmelzer9620d872017-05-30 15:05:44 +0200295 const void *ptmp;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400296 int len; /* new length of the property */
297 int ret; /* return value */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400298
299 /*
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500300 * Parameters: Node path, property, optional value.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400301 */
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200302 if (argc < 4)
Simon Glass4c12eeb2011-12-10 08:44:01 +0000303 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400304
305 pathp = argv[2];
306 prop = argv[3];
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400307
Kim Phillipse489b9c2008-06-10 11:06:17 -0500308 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400309 if (nodeoffset < 0) {
310 /*
311 * Not found or something else bad happened.
312 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500313 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400314 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400315 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400316 }
Gerald Van Baren25114032007-05-12 09:47:25 -0400317
Hannes Schmelzer9620d872017-05-30 15:05:44 +0200318 if (argc == 4) {
319 len = 0;
320 } else {
321 ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len);
322 if (len > SCRATCHPAD) {
323 printf("prop (%d) doesn't fit in scratchpad!\n",
324 len);
325 return 1;
326 }
Hannes Schmelzercee8c352017-08-18 14:41:14 +0200327 if (ptmp != NULL)
328 memcpy(data, ptmp, len);
329
Hannes Schmelzer9620d872017-05-30 15:05:44 +0200330 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
331 if (ret != 0)
332 return ret;
333 }
334
Kim Phillipse489b9c2008-06-10 11:06:17 -0500335 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
Gerald Van Baren25114032007-05-12 09:47:25 -0400336 if (ret < 0) {
337 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
338 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400339 }
340
Joe Hershbergerbc802952012-08-17 10:34:37 +0000341 /********************************************************************
342 * Get the value of a property in the working_fdt.
343 ********************************************************************/
344 } else if (argv[1][0] == 'g') {
345 char *subcmd; /* sub-command */
346 char *pathp; /* path */
347 char *prop; /* property */
348 char *var; /* variable to store result */
349 int nodeoffset; /* node offset from libfdt */
350 const void *nodep; /* property node pointer */
351 int len = 0; /* new length of the property */
352
353 /*
354 * Parameters: Node path, property, optional value.
355 */
356 if (argc < 5)
357 return CMD_RET_USAGE;
358
359 subcmd = argv[2];
360
361 if (argc < 6 && subcmd[0] != 's')
362 return CMD_RET_USAGE;
363
364 var = argv[3];
365 pathp = argv[4];
366 prop = argv[5];
367
368 nodeoffset = fdt_path_offset(working_fdt, pathp);
369 if (nodeoffset < 0) {
370 /*
371 * Not found or something else bad happened.
372 */
373 printf("libfdt fdt_path_offset() returned %s\n",
374 fdt_strerror(nodeoffset));
375 return 1;
376 }
377
378 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
379 int reqIndex = -1;
380 int startDepth = fdt_node_depth(
381 working_fdt, nodeoffset);
382 int curDepth = startDepth;
383 int curIndex = -1;
384 int nextNodeOffset = fdt_next_node(
385 working_fdt, nodeoffset, &curDepth);
386
387 if (subcmd[0] == 'n')
388 reqIndex = simple_strtoul(argv[5], NULL, 16);
389
390 while (curDepth > startDepth) {
391 if (curDepth == startDepth + 1)
392 curIndex++;
393 if (subcmd[0] == 'n' && curIndex == reqIndex) {
Simon Glass382bee52017-08-03 12:22:09 -0600394 const char *node_name;
Joe Hershbergerbc802952012-08-17 10:34:37 +0000395
Simon Glass382bee52017-08-03 12:22:09 -0600396 node_name = fdt_get_name(working_fdt,
397 nextNodeOffset,
398 NULL);
399 env_set(var, node_name);
Joe Hershbergerbc802952012-08-17 10:34:37 +0000400 return 0;
401 }
402 nextNodeOffset = fdt_next_node(
403 working_fdt, nextNodeOffset, &curDepth);
404 if (nextNodeOffset < 0)
405 break;
406 }
407 if (subcmd[0] == 's') {
408 /* get the num nodes at this level */
Simon Glass018f5302017-08-03 12:22:10 -0600409 env_set_ulong(var, curIndex + 1);
Joe Hershbergerbc802952012-08-17 10:34:37 +0000410 } else {
411 /* node index not found */
412 printf("libfdt node not found\n");
413 return 1;
414 }
415 } else {
416 nodep = fdt_getprop(
417 working_fdt, nodeoffset, prop, &len);
418 if (len == 0) {
419 /* no property value */
Simon Glass382bee52017-08-03 12:22:09 -0600420 env_set(var, "");
Joe Hershbergerbc802952012-08-17 10:34:37 +0000421 return 0;
Simon Glass72c98ed2017-06-07 10:28:41 -0600422 } else if (nodep && len > 0) {
Joe Hershbergerbc802952012-08-17 10:34:37 +0000423 if (subcmd[0] == 'v') {
424 int ret;
425
Simon Glass382bee52017-08-03 12:22:09 -0600426 ret = fdt_value_env_set(nodep, len,
427 var);
Joe Hershbergerbc802952012-08-17 10:34:37 +0000428 if (ret != 0)
429 return ret;
430 } else if (subcmd[0] == 'a') {
431 /* Get address */
432 char buf[11];
433
Tom Rini085b9c32012-10-29 14:53:18 +0000434 sprintf(buf, "0x%p", nodep);
Simon Glass382bee52017-08-03 12:22:09 -0600435 env_set(var, buf);
Joe Hershbergerbc802952012-08-17 10:34:37 +0000436 } else if (subcmd[0] == 's') {
437 /* Get size */
438 char buf[11];
439
440 sprintf(buf, "0x%08X", len);
Simon Glass382bee52017-08-03 12:22:09 -0600441 env_set(var, buf);
Joe Hershbergerbc802952012-08-17 10:34:37 +0000442 } else
443 return CMD_RET_USAGE;
444 return 0;
445 } else {
446 printf("libfdt fdt_getprop(): %s\n",
447 fdt_strerror(len));
448 return 1;
449 }
450 }
451
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200452 /*
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400453 * Print (recursive) / List (single level)
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200454 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400455 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400456 int depth = MAX_LEVEL; /* how deep to print */
457 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400458 char *prop; /* property */
459 int ret; /* return value */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500460 static char root[2] = "/";
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400461
462 /*
463 * list is an alias for print, but limited to 1 level
464 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400465 if (argv[1][0] == 'l') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400466 depth = 1;
467 }
468
469 /*
470 * Get the starting path. The root node is an oddball,
471 * the offset is zero and has no name.
472 */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500473 if (argc == 2)
474 pathp = root;
475 else
476 pathp = argv[2];
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400477 if (argc > 3)
478 prop = argv[3];
479 else
480 prop = NULL;
481
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400482 ret = fdt_print(pathp, prop, depth);
483 if (ret != 0)
484 return ret;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400485
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200486 /*
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400487 * Remove a property/node
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200488 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400489 } else if (strncmp(argv[1], "rm", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400490 int nodeoffset; /* node offset from libfdt */
491 int err;
492
493 /*
494 * Get the path. The root node is an oddball, the offset
495 * is zero and has no name.
496 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500497 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
Gerald Van Baren25114032007-05-12 09:47:25 -0400498 if (nodeoffset < 0) {
499 /*
500 * Not found or something else bad happened.
501 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500502 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400503 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400504 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400505 }
506 /*
507 * Do the delete. A fourth parameter means delete a property,
508 * otherwise delete the node.
509 */
510 if (argc > 3) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500511 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400512 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400513 printf("libfdt fdt_delprop(): %s\n",
514 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400515 return err;
516 }
517 } else {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500518 err = fdt_del_node(working_fdt, nodeoffset);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400519 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400520 printf("libfdt fdt_del_node(): %s\n",
521 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400522 return err;
523 }
524 }
Kumar Gala804887e2008-02-15 03:34:36 -0600525
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200526 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600527 * Display header info
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200528 */
Kumar Gala804887e2008-02-15 03:34:36 -0600529 } else if (argv[1][0] == 'h') {
Heiko Schocher82441272018-11-15 06:06:06 +0100530 if (argc == 5)
531 return fdt_get_header_value(argc, argv);
532
Kim Phillipse489b9c2008-06-10 11:06:17 -0500533 u32 version = fdt_version(working_fdt);
534 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
535 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
536 fdt_totalsize(working_fdt));
537 printf("off_dt_struct:\t\t0x%x\n",
538 fdt_off_dt_struct(working_fdt));
539 printf("off_dt_strings:\t\t0x%x\n",
540 fdt_off_dt_strings(working_fdt));
541 printf("off_mem_rsvmap:\t\t0x%x\n",
542 fdt_off_mem_rsvmap(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600543 printf("version:\t\t%d\n", version);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500544 printf("last_comp_version:\t%d\n",
545 fdt_last_comp_version(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600546 if (version >= 2)
547 printf("boot_cpuid_phys:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500548 fdt_boot_cpuid_phys(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600549 if (version >= 3)
550 printf("size_dt_strings:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500551 fdt_size_dt_strings(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600552 if (version >= 17)
553 printf("size_dt_struct:\t\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500554 fdt_size_dt_struct(working_fdt));
555 printf("number mem_rsv:\t\t0x%x\n",
556 fdt_num_mem_rsv(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600557 printf("\n");
558
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200559 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600560 * Set boot cpu id
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200561 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400562 } else if (strncmp(argv[1], "boo", 3) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600563 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500564 fdt_set_boot_cpuid_phys(working_fdt, tmp);
Kumar Gala804887e2008-02-15 03:34:36 -0600565
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200566 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600567 * memory command
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200568 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400569 } else if (strncmp(argv[1], "me", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600570 uint64_t addr, size;
571 int err;
Heiko Schocher4b142fe2009-12-03 11:21:21 +0100572 addr = simple_strtoull(argv[2], NULL, 16);
573 size = simple_strtoull(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500574 err = fdt_fixup_memory(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600575 if (err < 0)
576 return err;
577
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200578 /*
Kumar Gala804887e2008-02-15 03:34:36 -0600579 * mem reserve commands
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200580 */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400581 } else if (strncmp(argv[1], "rs", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600582 if (argv[2][0] == 'p') {
583 uint64_t addr, size;
Kim Phillipse489b9c2008-06-10 11:06:17 -0500584 int total = fdt_num_mem_rsv(working_fdt);
Kumar Gala804887e2008-02-15 03:34:36 -0600585 int j, err;
586 printf("index\t\t start\t\t size\n");
587 printf("-------------------------------"
588 "-----------------\n");
589 for (j = 0; j < total; j++) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500590 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
Kumar Gala804887e2008-02-15 03:34:36 -0600591 if (err < 0) {
592 printf("libfdt fdt_get_mem_rsv(): %s\n",
593 fdt_strerror(err));
594 return err;
595 }
596 printf(" %x\t%08x%08x\t%08x%08x\n", j,
597 (u32)(addr >> 32),
598 (u32)(addr & 0xffffffff),
599 (u32)(size >> 32),
600 (u32)(size & 0xffffffff));
601 }
602 } else if (argv[2][0] == 'a') {
603 uint64_t addr, size;
604 int err;
Kumar Gala804887e2008-02-15 03:34:36 -0600605 addr = simple_strtoull(argv[3], NULL, 16);
606 size = simple_strtoull(argv[4], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500607 err = fdt_add_mem_rsv(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600608
609 if (err < 0) {
610 printf("libfdt fdt_add_mem_rsv(): %s\n",
611 fdt_strerror(err));
612 return err;
613 }
614 } else if (argv[2][0] == 'd') {
615 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500616 int err = fdt_del_mem_rsv(working_fdt, idx);
Kumar Gala804887e2008-02-15 03:34:36 -0600617
618 if (err < 0) {
619 printf("libfdt fdt_del_mem_rsv(): %s\n",
620 fdt_strerror(err));
621 return err;
622 }
623 } else {
624 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000625 return CMD_RET_USAGE;
Kumar Gala804887e2008-02-15 03:34:36 -0600626 }
Kim Phillips99dffca2007-07-17 13:57:04 -0500627 }
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400628#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillips99dffca2007-07-17 13:57:04 -0500629 /* Call the board-specific fixup routine */
Simon Glass4ba98dc2014-10-23 18:58:48 -0600630 else if (strncmp(argv[1], "boa", 3) == 0) {
631 int err = ft_board_setup(working_fdt, gd->bd);
632
633 if (err) {
634 printf("Failed to update board information in FDT: %s\n",
635 fdt_strerror(err));
636 return CMD_RET_FAILURE;
637 }
Nicholas Faustini2c76d312018-10-03 12:58:48 +0200638#ifdef CONFIG_SOC_KEYSTONE
639 ft_board_setup_ex(working_fdt, gd->bd);
640#endif
Simon Glass4ba98dc2014-10-23 18:58:48 -0600641 }
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400642#endif
Kim Phillips99dffca2007-07-17 13:57:04 -0500643 /* Create a chosen node */
Heiko Schocher097dd3e2014-03-03 12:19:24 +0100644 else if (strncmp(argv[1], "cho", 3) == 0) {
Kumar Galaf953d992008-08-15 08:24:34 -0500645 unsigned long initrd_start = 0, initrd_end = 0;
646
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200647 if ((argc != 2) && (argc != 4))
Simon Glass4c12eeb2011-12-10 08:44:01 +0000648 return CMD_RET_USAGE;
Kumar Galaf953d992008-08-15 08:24:34 -0500649
650 if (argc == 4) {
651 initrd_start = simple_strtoul(argv[2], NULL, 16);
652 initrd_end = simple_strtoul(argv[3], NULL, 16);
653 }
654
Masahiro Yamadabc6ed0f2014-04-18 17:41:00 +0900655 fdt_chosen(working_fdt);
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900656 fdt_initrd(working_fdt, initrd_start, initrd_end);
Heiko Schocher097dd3e2014-03-03 12:19:24 +0100657
658#if defined(CONFIG_FIT_SIGNATURE)
659 } else if (strncmp(argv[1], "che", 3) == 0) {
660 int cfg_noffset;
661 int ret;
662 unsigned long addr;
663 struct fdt_header *blob;
664
665 if (!working_fdt)
666 return CMD_RET_FAILURE;
667
668 if (argc > 2) {
669 addr = simple_strtoul(argv[2], NULL, 16);
670 blob = map_sysmem(addr, 0);
671 } else {
672 blob = (struct fdt_header *)gd->fdt_blob;
673 }
674 if (!fdt_valid(&blob))
675 return 1;
676
677 gd->fdt_blob = blob;
678 cfg_noffset = fit_conf_get_node(working_fdt, NULL);
679 if (!cfg_noffset) {
680 printf("Could not find configuration node: %s\n",
681 fdt_strerror(cfg_noffset));
682 return CMD_RET_FAILURE;
683 }
684
685 ret = fit_config_verify(working_fdt, cfg_noffset);
Simon Glass12df2ab2014-06-12 07:24:45 -0600686 if (ret == 0)
Heiko Schocher097dd3e2014-03-03 12:19:24 +0100687 return CMD_RET_SUCCESS;
688 else
689 return CMD_RET_FAILURE;
690#endif
691
Kumar Gala40afac22008-08-15 08:24:44 -0500692 }
Maxime Riparde6628ad2016-07-05 10:26:45 +0200693#ifdef CONFIG_OF_LIBFDT_OVERLAY
694 /* apply an overlay */
695 else if (strncmp(argv[1], "ap", 2) == 0) {
696 unsigned long addr;
697 struct fdt_header *blob;
Stefan Agner082b1412016-12-20 15:58:45 +0100698 int ret;
Maxime Riparde6628ad2016-07-05 10:26:45 +0200699
700 if (argc != 3)
701 return CMD_RET_USAGE;
702
703 if (!working_fdt)
704 return CMD_RET_FAILURE;
705
706 addr = simple_strtoul(argv[2], NULL, 16);
707 blob = map_sysmem(addr, 0);
708 if (!fdt_valid(&blob))
709 return CMD_RET_FAILURE;
710
Pantelis Antoniou81ecc5d2017-09-04 23:12:12 +0300711 /* apply method prints messages on error */
712 ret = fdt_overlay_apply_verbose(working_fdt, blob);
713 if (ret)
Maxime Riparde6628ad2016-07-05 10:26:45 +0200714 return CMD_RET_FAILURE;
715 }
716#endif
Kumar Gala40afac22008-08-15 08:24:44 -0500717 /* resize the fdt */
718 else if (strncmp(argv[1], "re", 2) == 0) {
Hannes Schmelzeref476832016-09-20 18:10:43 +0200719 uint extrasize;
720 if (argc > 2)
721 extrasize = simple_strtoul(argv[2], NULL, 16);
722 else
723 extrasize = 0;
724 fdt_shrink_to_minimum(working_fdt, extrasize);
Kumar Gala40afac22008-08-15 08:24:44 -0500725 }
726 else {
Kim Phillips99dffca2007-07-17 13:57:04 -0500727 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000728 return CMD_RET_USAGE;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400729 }
730
731 return 0;
732}
733
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400734/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400735
Simon Glassd14da912013-04-20 08:42:42 +0000736/**
737 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
738 *
739 * @blobp: Pointer to FDT pointer
740 * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL)
741 */
742static int fdt_valid(struct fdt_header **blobp)
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400743{
Simon Glassd14da912013-04-20 08:42:42 +0000744 const void *blob = *blobp;
745 int err;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400746
Simon Glassd14da912013-04-20 08:42:42 +0000747 if (blob == NULL) {
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400748 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400749 return 0;
750 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400751
Simon Glassd14da912013-04-20 08:42:42 +0000752 err = fdt_check_header(blob);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400753 if (err == 0)
754 return 1; /* valid */
755
756 if (err < 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400757 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400758 /*
759 * Be more informative on bad version.
760 */
761 if (err == -FDT_ERR_BADVERSION) {
Simon Glassd14da912013-04-20 08:42:42 +0000762 if (fdt_version(blob) <
Kim Phillipse489b9c2008-06-10 11:06:17 -0500763 FDT_FIRST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700764 printf (" - too old, fdt %d < %d",
Simon Glassd14da912013-04-20 08:42:42 +0000765 fdt_version(blob),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400766 FDT_FIRST_SUPPORTED_VERSION);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400767 }
Simon Glassd14da912013-04-20 08:42:42 +0000768 if (fdt_last_comp_version(blob) >
Kim Phillipse489b9c2008-06-10 11:06:17 -0500769 FDT_LAST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700770 printf (" - too new, fdt %d > %d",
Simon Glassd14da912013-04-20 08:42:42 +0000771 fdt_version(blob),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400772 FDT_LAST_SUPPORTED_VERSION);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400773 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400774 }
775 printf("\n");
Simon Glassd14da912013-04-20 08:42:42 +0000776 *blobp = NULL;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400777 return 0;
778 }
779 return 1;
780}
781
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400782/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400783
784/*
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400785 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming4abd8442008-03-31 20:45:56 -0500786 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200787 * C conventions.
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400788 * [00 11 22 .. nn] - byte stream
789 * "string" - If the the value doesn't start with "<" or "[", it is
790 * treated as a string. Note that the quotes are
791 * stripped by the parser before we get the string.
Andy Fleming4abd8442008-03-31 20:45:56 -0500792 * newval: An array of strings containing the new property as specified
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200793 * on the command line
Andy Fleming4abd8442008-03-31 20:45:56 -0500794 * count: The number of strings in the array
795 * data: A bytestream to be placed in the property
796 * len: The length of the resulting bytestream
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400797 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200798static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400799{
800 char *cp; /* temporary char pointer */
Andy Fleming4abd8442008-03-31 20:45:56 -0500801 char *newp; /* temporary newval char pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400802 unsigned long tmp; /* holds converted values */
Andy Fleming4abd8442008-03-31 20:45:56 -0500803 int stridx = 0;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400804
Andy Fleming4abd8442008-03-31 20:45:56 -0500805 *len = 0;
806 newp = newval[0];
807
808 /* An array of cells */
809 if (*newp == '<') {
810 newp++;
811 while ((*newp != '>') && (stridx < count)) {
812 /*
813 * Keep searching until we find that last ">"
814 * That way users don't have to escape the spaces
815 */
816 if (*newp == '\0') {
817 newp = newval[++stridx];
818 continue;
819 }
820
821 cp = newp;
822 tmp = simple_strtoul(cp, &newp, 0);
Hannes Schmelzer9620d872017-05-30 15:05:44 +0200823 if (*cp != '?')
824 *(fdt32_t *)data = cpu_to_fdt32(tmp);
825 else
826 newp++;
827
Andy Fleming4abd8442008-03-31 20:45:56 -0500828 data += 4;
829 *len += 4;
830
831 /* If the ptr didn't advance, something went wrong */
832 if ((newp - cp) <= 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400833 printf("Sorry, I could not convert \"%s\"\n",
834 cp);
835 return 1;
836 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500837
838 while (*newp == ' ')
839 newp++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400840 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500841
842 if (*newp != '>') {
843 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400844 return 1;
845 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500846 } else if (*newp == '[') {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400847 /*
848 * Byte stream. Convert the values.
849 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500850 newp++;
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500851 while ((stridx < count) && (*newp != ']')) {
852 while (*newp == ' ')
853 newp++;
854 if (*newp == '\0') {
855 newp = newval[++stridx];
856 continue;
857 }
858 if (!isxdigit(*newp))
859 break;
Andy Fleming4abd8442008-03-31 20:45:56 -0500860 tmp = simple_strtoul(newp, &newp, 16);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400861 *data++ = tmp & 0xFF;
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400862 *len = *len + 1;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400863 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500864 if (*newp != ']') {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700865 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400866 return 1;
867 }
868 } else {
869 /*
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500870 * Assume it is one or more strings. Copy it into our
871 * data area for convenience (including the
872 * terminating '\0's).
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400873 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500874 while (stridx < count) {
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500875 size_t length = strlen(newp) + 1;
Andy Fleming4abd8442008-03-31 20:45:56 -0500876 strcpy(data, newp);
Ken MacLeod6e748ea2009-09-11 15:16:18 -0500877 data += length;
878 *len += length;
Andy Fleming4abd8442008-03-31 20:45:56 -0500879 newp = newval[++stridx];
880 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400881 }
882 return 0;
883}
884
885/****************************************************************************/
886
887/*
888 * Heuristic to guess if this is a string or concatenated strings.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400889 */
890
891static int is_printable_string(const void *data, int len)
892{
893 const char *s = data;
894
895 /* zero length is not */
896 if (len == 0)
897 return 0;
898
Joe Hershberger8805bee2012-08-17 10:34:38 +0000899 /* must terminate with zero or '\n' */
900 if (s[len - 1] != '\0' && s[len - 1] != '\n')
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400901 return 0;
902
903 /* printable or a null byte (concatenated strings) */
Joe Hershberger8805bee2012-08-17 10:34:38 +0000904 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400905 /*
906 * If we see a null, there are three possibilities:
907 * 1) If len == 1, it is the end of the string, printable
908 * 2) Next character also a null, not printable.
909 * 3) Next character not a null, continue to check.
910 */
911 if (s[0] == '\0') {
912 if (len == 1)
913 return 1;
914 if (s[1] == '\0')
915 return 0;
916 }
917 s++;
918 len--;
919 }
920
921 /* Not the null termination, or not done yet: not printable */
922 if (*s != '\0' || (len != 0))
923 return 0;
924
925 return 1;
926}
927
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400928
929/*
930 * Print the property in the best format, a heuristic guess. Print as
931 * a string, concatenated strings, a byte, word, double word, or (if all
932 * else fails) it is printed as a stream of bytes.
933 */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400934static void print_data(const void *data, int len)
935{
936 int j;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400937
938 /* no data, don't print */
939 if (len == 0)
940 return;
941
942 /*
943 * It is a string, but it may have multiple strings (embedded '\0's).
944 */
945 if (is_printable_string(data, len)) {
946 puts("\"");
947 j = 0;
948 while (j < len) {
949 if (j > 0)
950 puts("\", \"");
951 puts(data);
952 j += strlen(data) + 1;
953 data += strlen(data) + 1;
954 }
955 puts("\"");
956 return;
957 }
958
Andy Fleming4abd8442008-03-31 20:45:56 -0500959 if ((len %4) == 0) {
Simon Glass5d927b42017-05-17 03:25:19 -0600960 if (len > CMD_FDT_MAX_DUMP)
Tom Rini085b9c32012-10-29 14:53:18 +0000961 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000962 else {
Kim Phillips088f1b12012-10-29 13:34:31 +0000963 const __be32 *p;
Andy Fleming4abd8442008-03-31 20:45:56 -0500964
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000965 printf("<");
966 for (j = 0, p = data; j < len/4; j++)
967 printf("0x%08x%s", fdt32_to_cpu(p[j]),
968 j < (len/4 - 1) ? " " : "");
969 printf(">");
970 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500971 } else { /* anything else... hexdump */
Simon Glass5d927b42017-05-17 03:25:19 -0600972 if (len > CMD_FDT_MAX_DUMP)
Tom Rini085b9c32012-10-29 14:53:18 +0000973 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000974 else {
975 const u8 *s;
Andy Fleming4abd8442008-03-31 20:45:56 -0500976
Joe Hershbergerf0a29d42012-08-17 10:34:36 +0000977 printf("[");
978 for (j = 0, s = data; j < len; j++)
979 printf("%02x%s", s[j], j < len - 1 ? " " : "");
980 printf("]");
981 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400982 }
983}
984
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400985/****************************************************************************/
986
987/*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500988 * Recursively print (a portion of) the working_fdt. The depth parameter
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400989 * determines how deeply nested the fdt is printed.
990 */
Kumar Galadbaf07c2007-11-21 14:07:46 -0600991static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400992{
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400993 static char tabs[MAX_LEVEL+1] =
994 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
995 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Galadbaf07c2007-11-21 14:07:46 -0600996 const void *nodep; /* property node pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400997 int nodeoffset; /* node offset from libfdt */
998 int nextoffset; /* next node offset from libfdt */
999 uint32_t tag; /* tag */
1000 int len; /* length of the property */
1001 int level = 0; /* keep track of nesting level */
Gerald Van Baren91623522007-11-22 17:23:23 -05001002 const struct fdt_property *fdt_prop;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001003
Kim Phillipse489b9c2008-06-10 11:06:17 -05001004 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001005 if (nodeoffset < 0) {
1006 /*
1007 * Not found or something else bad happened.
1008 */
Kumar Gala8d04f022007-10-24 11:04:22 -05001009 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -04001010 fdt_strerror(nodeoffset));
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001011 return 1;
1012 }
1013 /*
1014 * The user passed in a property as well as node path.
1015 * Print only the given property and then return.
1016 */
1017 if (prop) {
Kim Phillipse489b9c2008-06-10 11:06:17 -05001018 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001019 if (len == 0) {
1020 /* no property value */
1021 printf("%s %s\n", pathp, prop);
1022 return 0;
Simon Glass9f952672017-06-07 10:28:42 -06001023 } else if (nodep && len > 0) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -05001024 printf("%s = ", prop);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001025 print_data (nodep, len);
1026 printf("\n");
1027 return 0;
1028 } else {
1029 printf ("libfdt fdt_getprop(): %s\n",
1030 fdt_strerror(len));
1031 return 1;
1032 }
1033 }
1034
1035 /*
1036 * The user passed in a node path and no property,
1037 * print the node and all subnodes.
1038 */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001039 while(level >= 0) {
Kim Phillipse489b9c2008-06-10 11:06:17 -05001040 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001041 switch(tag) {
1042 case FDT_BEGIN_NODE:
Kim Phillipse489b9c2008-06-10 11:06:17 -05001043 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
Gerald Van Baren91623522007-11-22 17:23:23 -05001044 if (level <= depth) {
1045 if (pathp == NULL)
1046 pathp = "/* NULL pointer error */";
1047 if (*pathp == '\0')
1048 pathp = "/"; /* root is nameless */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001049 printf("%s%s {\n",
1050 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Baren91623522007-11-22 17:23:23 -05001051 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001052 level++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001053 if (level >= MAX_LEVEL) {
Gerald Van Baren91623522007-11-22 17:23:23 -05001054 printf("Nested too deep, aborting.\n");
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001055 return 1;
1056 }
1057 break;
1058 case FDT_END_NODE:
1059 level--;
Gerald Van Baren91623522007-11-22 17:23:23 -05001060 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001061 printf("%s};\n", &tabs[MAX_LEVEL - level]);
1062 if (level == 0) {
1063 level = -1; /* exit the loop */
1064 }
1065 break;
1066 case FDT_PROP:
Kim Phillipse489b9c2008-06-10 11:06:17 -05001067 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
Gerald Van Baren91623522007-11-22 17:23:23 -05001068 sizeof(*fdt_prop));
Kim Phillipse489b9c2008-06-10 11:06:17 -05001069 pathp = fdt_string(working_fdt,
Gerald Van Baren91623522007-11-22 17:23:23 -05001070 fdt32_to_cpu(fdt_prop->nameoff));
1071 len = fdt32_to_cpu(fdt_prop->len);
1072 nodep = fdt_prop->data;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001073 if (len < 0) {
1074 printf ("libfdt fdt_getprop(): %s\n",
1075 fdt_strerror(len));
1076 return 1;
1077 } else if (len == 0) {
1078 /* the property has no value */
Gerald Van Baren91623522007-11-22 17:23:23 -05001079 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001080 printf("%s%s;\n",
1081 &tabs[MAX_LEVEL - level],
1082 pathp);
1083 } else {
Gerald Van Baren91623522007-11-22 17:23:23 -05001084 if (level <= depth) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -05001085 printf("%s%s = ",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001086 &tabs[MAX_LEVEL - level],
1087 pathp);
1088 print_data (nodep, len);
1089 printf(";\n");
1090 }
1091 }
1092 break;
1093 case FDT_NOP:
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -07001094 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001095 break;
1096 case FDT_END:
1097 return 1;
1098 default:
Gerald Van Baren91623522007-11-22 17:23:23 -05001099 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -04001100 printf("Unknown tag 0x%08X\n", tag);
1101 return 1;
1102 }
1103 nodeoffset = nextoffset;
1104 }
1105 return 0;
1106}
1107
Gerald Van Baren781e09e2007-03-31 12:22:10 -04001108/********************************************************************/
Kim Phillips088f1b12012-10-29 13:34:31 +00001109#ifdef CONFIG_SYS_LONGHELP
1110static char fdt_help_text[] =
Simon Glass4b578652013-04-20 08:42:44 +00001111 "addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>\n"
Maxime Riparde6628ad2016-07-05 10:26:45 +02001112#ifdef CONFIG_OF_LIBFDT_OVERLAY
1113 "fdt apply <addr> - Apply overlay to the DT\n"
1114#endif
Gerald Van Barenfd61e552007-06-25 23:25:28 -04001115#ifdef CONFIG_OF_BOARD_SETUP
1116 "fdt boardsetup - Do board-specific set up\n"
1117#endif
Simon Glassc654b512014-10-23 18:58:54 -06001118#ifdef CONFIG_OF_SYSTEM_SETUP
1119 "fdt systemsetup - Do system-specific set up\n"
1120#endif
Gerald Van Baren238cb7a2008-01-05 15:33:29 -05001121 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Hannes Schmelzeref476832016-09-20 18:10:43 +02001122 "fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -04001123 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
1124 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
Joe Hershbergerbc802952012-08-17 10:34:37 +00001125 "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n"
1126 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
1127 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
1128 "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 -04001129 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
1130 "fdt mknode <path> <node> - Create a new node after <path>\n"
1131 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Heiko Schocher82441272018-11-15 06:06:06 +01001132 "fdt header [get <var> <member>] - Display header info\n"
1133 " get - get header member <member> and store it in <var>\n"
Kumar Gala804887e2008-02-15 03:34:36 -06001134 "fdt bootcpu <id> - Set boot cpuid\n"
1135 "fdt memory <addr> <size> - Add/Update memory node\n"
1136 "fdt rsvmem print - Show current mem reserves\n"
1137 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
1138 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Kumar Galaf953d992008-08-15 08:24:34 -05001139 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
1140 " <start>/<end> - initrd start/end addr\n"
Heiko Schocher097dd3e2014-03-03 12:19:24 +01001141#if defined(CONFIG_FIT_SIGNATURE)
1142 "fdt checksign [<addr>] - check FIT signature\n"
1143 " <start> - addr of key blob\n"
1144 " default gd->fdt_blob\n"
1145#endif
Robert P. J. Day1cc0a9f2016-05-04 04:47:31 -04001146 "NOTE: Dereference aliases by omitting the leading '/', "
Kim Phillips088f1b12012-10-29 13:34:31 +00001147 "e.g. fdt print ethernet0.";
1148#endif
1149
1150U_BOOT_CMD(
1151 fdt, 255, 0, do_fdt,
1152 "flattened device tree utility commands", fdt_help_text
Gerald Van Baren781e09e2007-03-31 12:22:10 -04001153);