blob: 4b2dcfec863d5f4602815f7eca51f414b8ae06b1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerald Van Barenade8ef42007-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 Barenade8ef42007-03-31 12:22:10 -04008 */
9
10#include <common.h>
11#include <command.h>
Simon Glass313112a2019-08-01 09:46:46 -060012#include <env.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060013#include <image.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040014#include <linux/ctype.h>
15#include <linux/types.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040016#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090017#include <linux/libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040018#include <fdt_support.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050019#include <mapmem.h>
Simon Glassb7fc7102013-04-20 08:42:45 +000020#include <asm/io.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040021
22#define MAX_LEVEL 32 /* how deeply nested we will go */
Gerald Van Baren5606a362007-06-25 23:25:28 -040023#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
Gerald Van Barenade8ef42007-03-31 12:22:10 -040024
25/*
26 * Global data (for the gd->bd)
27 */
28DECLARE_GLOBAL_DATA_PTR;
29
Wolfgang Denk6262d0212010-06-28 22:00:46 +020030static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
Kumar Gala1e386af2007-11-21 14:07:46 -060031static int fdt_print(const char *pathp, char *prop, int depth);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000032static int is_printable_string(const void *data, int len);
Gerald Van Barenade8ef42007-03-31 12:22:10 -040033
Gerald Van Barenade8ef42007-03-31 12:22:10 -040034/*
Gerald Van Barene81ce612008-06-10 22:15:58 -040035 * The working_fdt points to our working flattened device tree.
36 */
37struct fdt_header *working_fdt;
38
Joe Hershberger3d4ea4c2015-02-04 21:56:53 -060039void set_working_fdt_addr(ulong addr)
Kumar Gala9f4e7e42008-08-15 08:24:39 -050040{
Simon Glassb7fc7102013-04-20 08:42:45 +000041 void *buf;
42
Simon Glass84328cf2022-10-11 09:47:12 -060043 printf("Working FDT set to %lx\n", addr);
Joe Hershberger3d4ea4c2015-02-04 21:56:53 -060044 buf = map_sysmem(addr, 0);
Simon Glassb7fc7102013-04-20 08:42:45 +000045 working_fdt = buf;
Simon Glass4d949a22017-08-03 12:22:10 -060046 env_set_hex("fdtaddr", addr);
Kumar Gala9f4e7e42008-08-15 08:24:39 -050047}
48
Gerald Van Barene81ce612008-06-10 22:15:58 -040049/*
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000050 * Get a value from the fdt and format it to be set in the environment
51 */
Marek Vasut0282f9f2022-07-08 23:50:43 +020052static int fdt_value_env_set(const void *nodep, int len,
53 const char *var, int index)
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000054{
Marek Vasut0282f9f2022-07-08 23:50:43 +020055 if (is_printable_string(nodep, len)) {
56 const char *nodec = (const char *)nodep;
57 int i;
58
59 /*
60 * Iterate over all members in stringlist and find the one at
61 * offset $index. If no such index exists, indicate failure.
62 */
63 for (i = 0; i < len; i += strlen(nodec) + 1) {
64 if (index-- > 0)
65 continue;
66
67 env_set(var, nodec + i);
68 return 0;
69 }
70
71 return 1;
72 } else if (len == 4) {
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000073 char buf[11];
74
Andreas Färber1a09cd22017-01-09 16:08:02 +010075 sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep));
Simon Glass6a38e412017-08-03 12:22:09 -060076 env_set(var, buf);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000077 } else if (len%4 == 0 && len <= 20) {
78 /* Needed to print things like sha1 hashes. */
79 char buf[41];
80 int i;
81
82 for (i = 0; i < len; i += sizeof(unsigned int))
83 sprintf(buf + (i * 2), "%08x",
84 *(unsigned int *)(nodep + i));
Simon Glass6a38e412017-08-03 12:22:09 -060085 env_set(var, buf);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000086 } else {
87 printf("error: unprintable value\n");
88 return 1;
89 }
90 return 0;
91}
92
Heiko Schocherfefc7ee2018-11-15 06:06:06 +010093static const char * const fdt_member_table[] = {
94 "magic",
95 "totalsize",
96 "off_dt_struct",
97 "off_dt_strings",
98 "off_mem_rsvmap",
99 "version",
100 "last_comp_version",
101 "boot_cpuid_phys",
102 "size_dt_strings",
103 "size_dt_struct",
104};
105
Simon Glassed38aef2020-05-10 11:40:03 -0600106static int fdt_get_header_value(int argc, char *const argv[])
Heiko Schocherfefc7ee2018-11-15 06:06:06 +0100107{
108 fdt32_t *fdtp = (fdt32_t *)working_fdt;
109 ulong val;
110 int i;
111
112 if (argv[2][0] != 'g')
113 return CMD_RET_FAILURE;
114
115 for (i = 0; i < ARRAY_SIZE(fdt_member_table); i++) {
116 if (strcmp(fdt_member_table[i], argv[4]))
117 continue;
118
119 val = fdt32_to_cpu(fdtp[i]);
120 env_set_hex(argv[3], val);
121 return CMD_RET_SUCCESS;
122 }
123
124 return CMD_RET_FAILURE;
125}
126
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000127/*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400128 * Flattened Device Tree command, see the help for parameter definitions.
129 */
Simon Glassed38aef2020-05-10 11:40:03 -0600130static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400131{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200132 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000133 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400134
Simon Glasse4647a12021-07-21 14:55:25 -0600135 /* fdt addr: Set the address of the fdt */
Maxime Ripard09caa012016-07-05 10:26:34 +0200136 if (strncmp(argv[1], "ad", 2) == 0) {
Kumar Gala9f4e7e42008-08-15 08:24:39 -0500137 unsigned long addr;
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000138 int control = 0;
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100139 int quiet = 0;
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000140 struct fdt_header *blob;
Simon Glasse4647a12021-07-21 14:55:25 -0600141
142 /* Set the address [and length] of the fdt */
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000143 argc -= 2;
144 argv += 2;
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100145 while (argc > 0 && **argv == '-') {
146 char *arg = *argv;
147
148 while (*++arg) {
149 switch (*arg) {
150 case 'c':
151 control = 1;
152 break;
153 case 'q':
154 quiet = 1;
155 break;
156 default:
157 return CMD_RET_USAGE;
158 }
159 }
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000160 argc--;
161 argv++;
162 }
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000163 if (argc == 0) {
164 if (control)
165 blob = (struct fdt_header *)gd->fdt_blob;
166 else
167 blob = working_fdt;
168 if (!blob || !fdt_valid(&blob))
Kumar Gala708a18b2008-08-15 08:24:35 -0500169 return 1;
Simon Glasseedc9222021-07-21 14:55:26 -0600170 printf("%s fdt: %08lx\n",
171 control ? "Control" : "Working",
Joe Hershberger6256dcc2015-02-04 21:56:54 -0600172 control ? (ulong)map_to_sysmem(blob) :
Simon Glasseedc9222021-07-21 14:55:26 -0600173 env_get_hex("fdtaddr", 0));
Kumar Gala708a18b2008-08-15 08:24:35 -0500174 return 0;
175 }
176
Simon Glass3ff49ec2021-07-24 09:03:29 -0600177 addr = hextoul(argv[0], NULL);
Simon Glassb7fc7102013-04-20 08:42:45 +0000178 blob = map_sysmem(addr, 0);
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100179 if ((quiet && fdt_check_header(blob)) ||
180 (!quiet && !fdt_valid(&blob)))
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400181 return 1;
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000182 if (control)
183 gd->fdt_blob = blob;
184 else
Joe Hershberger3d4ea4c2015-02-04 21:56:53 -0600185 set_working_fdt_addr(addr);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400186
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000187 if (argc >= 2) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400188 int len;
189 int err;
Simon Glasse4647a12021-07-21 14:55:25 -0600190
191 /* Optional new length */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600192 len = hextoul(argv[1], NULL);
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000193 if (len < fdt_totalsize(blob)) {
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100194 if (!quiet)
195 printf("New length %d < existing length %d, ignoring\n",
196 len, fdt_totalsize(blob));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400197 } else {
Simon Glasse4647a12021-07-21 14:55:25 -0600198 /* Open in place with a new length */
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000199 err = fdt_open_into(blob, blob, len);
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100200 if (!quiet && err != 0) {
Simon Glasse4647a12021-07-21 14:55:25 -0600201 printf("libfdt fdt_open_into(): %s\n",
202 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400203 }
204 }
205 }
206
Marek Vasutaeccfee2012-09-05 08:34:44 +0200207 return CMD_RET_SUCCESS;
208 }
209
210 if (!working_fdt) {
Simon Glasse4647a12021-07-21 14:55:25 -0600211 puts("No FDT memory address configured. Please configure\n"
212 "the FDT address via \"fdt addr <address>\" command.\n"
213 "Aborting!\n");
Marek Vasutaeccfee2012-09-05 08:34:44 +0200214 return CMD_RET_FAILURE;
215 }
216
Wolfgang Denk3b683112010-07-17 01:06:04 +0200217 /*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500218 * Move the working_fdt
Wolfgang Denk3b683112010-07-17 01:06:04 +0200219 */
Marek Vasutaeccfee2012-09-05 08:34:44 +0200220 if (strncmp(argv[1], "mo", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400221 struct fdt_header *newaddr;
222 int len;
223 int err;
224
Wolfgang Denk3b683112010-07-17 01:06:04 +0200225 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000226 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400227
228 /*
229 * Set the address and length of the fdt.
230 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600231 working_fdt = (struct fdt_header *)hextoul(argv[2], NULL);
Simon Glass546b9a32013-04-20 08:42:42 +0000232 if (!fdt_valid(&working_fdt))
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400233 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400234
Simon Glass3ff49ec2021-07-24 09:03:29 -0600235 newaddr = (struct fdt_header *)hextoul(argv[3], NULL);
Gerald Van Baren4d223922007-04-25 22:47:15 -0400236
237 /*
238 * If the user specifies a length, use that. Otherwise use the
239 * current length.
240 */
241 if (argc <= 4) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500242 len = fdt_totalsize(working_fdt);
Gerald Van Baren4d223922007-04-25 22:47:15 -0400243 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600244 len = hextoul(argv[4], NULL);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500245 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400246 printf ("New length 0x%X < existing length "
247 "0x%X, aborting.\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500248 len, fdt_totalsize(working_fdt));
Gerald Van Baren4d223922007-04-25 22:47:15 -0400249 return 1;
250 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400251 }
252
253 /*
254 * Copy to the new location.
255 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500256 err = fdt_open_into(working_fdt, newaddr, len);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400257 if (err != 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400258 printf ("libfdt fdt_open_into(): %s\n",
259 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400260 return 1;
261 }
Hiroyuki Yokoyama5f107b52018-10-18 20:43:54 +0200262 set_working_fdt_addr((ulong)newaddr);
Fabien Parenta0559ac2016-11-24 15:02:18 +0100263#ifdef CONFIG_OF_SYSTEM_SETUP
264 /* Call the board-specific fixup routine */
265 } else if (strncmp(argv[1], "sys", 3) == 0) {
266 int err = ft_system_setup(working_fdt, gd->bd);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400267
Fabien Parenta0559ac2016-11-24 15:02:18 +0100268 if (err) {
269 printf("Failed to add system information to FDT: %s\n",
270 fdt_strerror(err));
271 return CMD_RET_FAILURE;
272 }
273#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200274 /*
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400275 * Make a new node
Wolfgang Denk3b683112010-07-17 01:06:04 +0200276 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400277 } else if (strncmp(argv[1], "mk", 2) == 0) {
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400278 char *pathp; /* path */
279 char *nodep; /* new node to add */
280 int nodeoffset; /* node offset from libfdt */
281 int err;
282
283 /*
284 * Parameters: Node path, new node to be appended to the path.
285 */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200286 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000287 return CMD_RET_USAGE;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400288
289 pathp = argv[2];
290 nodep = argv[3];
291
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500292 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400293 if (nodeoffset < 0) {
294 /*
295 * Not found or something else bad happened.
296 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500297 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400298 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400299 return 1;
300 }
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500301 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400302 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400303 printf ("libfdt fdt_add_subnode(): %s\n",
304 fdt_strerror(err));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400305 return 1;
306 }
307
Wolfgang Denk3b683112010-07-17 01:06:04 +0200308 /*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500309 * Set the value of a property in the working_fdt.
Wolfgang Denk3b683112010-07-17 01:06:04 +0200310 */
Tom Warrend2c6dc82020-03-26 15:20:44 -0700311 } else if (strncmp(argv[1], "se", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400312 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400313 char *prop; /* property */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400314 int nodeoffset; /* node offset from libfdt */
Bernhard Messerklinger9b2664f2017-09-28 11:29:52 +0200315 static char data[SCRATCHPAD] __aligned(4);/* property storage */
Hannes Schmelzer98788612017-05-30 15:05:44 +0200316 const void *ptmp;
Gerald Van Barene596a162007-05-16 22:39:59 -0400317 int len; /* new length of the property */
318 int ret; /* return value */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400319
320 /*
Gerald Van Barend91383a2008-01-05 14:52:04 -0500321 * Parameters: Node path, property, optional value.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400322 */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200323 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000324 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400325
326 pathp = argv[2];
327 prop = argv[3];
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400328
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500329 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400330 if (nodeoffset < 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400331 /*
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400332 * Not found or something else bad happened.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400333 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500334 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400335 fdt_strerror(nodeoffset));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400336 return 1;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400337 }
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400338
Hannes Schmelzer98788612017-05-30 15:05:44 +0200339 if (argc == 4) {
340 len = 0;
341 } else {
342 ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len);
343 if (len > SCRATCHPAD) {
344 printf("prop (%d) doesn't fit in scratchpad!\n",
345 len);
346 return 1;
347 }
Hannes Schmelzera99c0522017-08-18 14:41:14 +0200348 if (ptmp != NULL)
349 memcpy(data, ptmp, len);
350
Hannes Schmelzer98788612017-05-30 15:05:44 +0200351 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
352 if (ret != 0)
353 return ret;
354 }
355
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500356 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400357 if (ret < 0) {
358 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
359 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400360 }
361
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000362 /********************************************************************
363 * Get the value of a property in the working_fdt.
364 ********************************************************************/
365 } else if (argv[1][0] == 'g') {
366 char *subcmd; /* sub-command */
367 char *pathp; /* path */
368 char *prop; /* property */
369 char *var; /* variable to store result */
370 int nodeoffset; /* node offset from libfdt */
371 const void *nodep; /* property node pointer */
372 int len = 0; /* new length of the property */
373
374 /*
375 * Parameters: Node path, property, optional value.
376 */
377 if (argc < 5)
378 return CMD_RET_USAGE;
379
380 subcmd = argv[2];
381
382 if (argc < 6 && subcmd[0] != 's')
383 return CMD_RET_USAGE;
384
385 var = argv[3];
386 pathp = argv[4];
387 prop = argv[5];
388
389 nodeoffset = fdt_path_offset(working_fdt, pathp);
390 if (nodeoffset < 0) {
391 /*
392 * Not found or something else bad happened.
393 */
394 printf("libfdt fdt_path_offset() returned %s\n",
395 fdt_strerror(nodeoffset));
396 return 1;
397 }
398
399 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600400 int req_index = -1;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000401 int startDepth = fdt_node_depth(
402 working_fdt, nodeoffset);
403 int curDepth = startDepth;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600404 int cur_index = -1;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000405 int nextNodeOffset = fdt_next_node(
406 working_fdt, nodeoffset, &curDepth);
407
408 if (subcmd[0] == 'n')
Simon Glass3ff49ec2021-07-24 09:03:29 -0600409 req_index = hextoul(argv[5], NULL);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000410
411 while (curDepth > startDepth) {
412 if (curDepth == startDepth + 1)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600413 cur_index++;
414 if (subcmd[0] == 'n' &&
415 cur_index == req_index) {
Simon Glass6a38e412017-08-03 12:22:09 -0600416 const char *node_name;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000417
Simon Glass6a38e412017-08-03 12:22:09 -0600418 node_name = fdt_get_name(working_fdt,
419 nextNodeOffset,
420 NULL);
421 env_set(var, node_name);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000422 return 0;
423 }
424 nextNodeOffset = fdt_next_node(
425 working_fdt, nextNodeOffset, &curDepth);
426 if (nextNodeOffset < 0)
427 break;
428 }
429 if (subcmd[0] == 's') {
430 /* get the num nodes at this level */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600431 env_set_ulong(var, cur_index + 1);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000432 } else {
433 /* node index not found */
434 printf("libfdt node not found\n");
435 return 1;
436 }
437 } else {
438 nodep = fdt_getprop(
439 working_fdt, nodeoffset, prop, &len);
440 if (len == 0) {
441 /* no property value */
Simon Glass6a38e412017-08-03 12:22:09 -0600442 env_set(var, "");
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000443 return 0;
Simon Glass03746912017-06-07 10:28:41 -0600444 } else if (nodep && len > 0) {
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000445 if (subcmd[0] == 'v') {
Marek Vasut0282f9f2022-07-08 23:50:43 +0200446 int index = 0;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000447 int ret;
448
Marek Vasut0282f9f2022-07-08 23:50:43 +0200449 if (argc == 7)
450 index = simple_strtoul(argv[6], NULL, 10);
451
Simon Glass6a38e412017-08-03 12:22:09 -0600452 ret = fdt_value_env_set(nodep, len,
Marek Vasut0282f9f2022-07-08 23:50:43 +0200453 var, index);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000454 if (ret != 0)
455 return ret;
456 } else if (subcmd[0] == 'a') {
457 /* Get address */
458 char buf[11];
459
Tom Rini1db8b532012-10-29 14:53:18 +0000460 sprintf(buf, "0x%p", nodep);
Simon Glass6a38e412017-08-03 12:22:09 -0600461 env_set(var, buf);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000462 } else if (subcmd[0] == 's') {
463 /* Get size */
464 char buf[11];
465
466 sprintf(buf, "0x%08X", len);
Simon Glass6a38e412017-08-03 12:22:09 -0600467 env_set(var, buf);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000468 } else
469 return CMD_RET_USAGE;
470 return 0;
471 } else {
472 printf("libfdt fdt_getprop(): %s\n",
473 fdt_strerror(len));
474 return 1;
475 }
476 }
477
Wolfgang Denk3b683112010-07-17 01:06:04 +0200478 /*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400479 * Print (recursive) / List (single level)
Wolfgang Denk3b683112010-07-17 01:06:04 +0200480 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400481 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400482 int depth = MAX_LEVEL; /* how deep to print */
483 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400484 char *prop; /* property */
485 int ret; /* return value */
Kumar Gala51ab7f92007-10-25 16:15:07 -0500486 static char root[2] = "/";
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400487
488 /*
489 * list is an alias for print, but limited to 1 level
490 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400491 if (argv[1][0] == 'l') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400492 depth = 1;
493 }
494
495 /*
496 * Get the starting path. The root node is an oddball,
497 * the offset is zero and has no name.
498 */
Kumar Gala51ab7f92007-10-25 16:15:07 -0500499 if (argc == 2)
500 pathp = root;
501 else
502 pathp = argv[2];
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400503 if (argc > 3)
504 prop = argv[3];
505 else
506 prop = NULL;
507
Gerald Van Barene596a162007-05-16 22:39:59 -0400508 ret = fdt_print(pathp, prop, depth);
509 if (ret != 0)
510 return ret;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400511
Wolfgang Denk3b683112010-07-17 01:06:04 +0200512 /*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400513 * Remove a property/node
Wolfgang Denk3b683112010-07-17 01:06:04 +0200514 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400515 } else if (strncmp(argv[1], "rm", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400516 int nodeoffset; /* node offset from libfdt */
517 int err;
518
519 /*
520 * Get the path. The root node is an oddball, the offset
521 * is zero and has no name.
522 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500523 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400524 if (nodeoffset < 0) {
525 /*
526 * Not found or something else bad happened.
527 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500528 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400529 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400530 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400531 }
532 /*
533 * Do the delete. A fourth parameter means delete a property,
534 * otherwise delete the node.
535 */
536 if (argc > 3) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500537 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400538 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400539 printf("libfdt fdt_delprop(): %s\n",
540 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400541 return err;
542 }
543 } else {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500544 err = fdt_del_node(working_fdt, nodeoffset);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400545 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400546 printf("libfdt fdt_del_node(): %s\n",
547 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400548 return err;
549 }
550 }
Kumar Galab79da7b2008-02-15 03:34:36 -0600551
Wolfgang Denk3b683112010-07-17 01:06:04 +0200552 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600553 * Display header info
Wolfgang Denk3b683112010-07-17 01:06:04 +0200554 */
Kumar Galab79da7b2008-02-15 03:34:36 -0600555 } else if (argv[1][0] == 'h') {
Heiko Schocherfefc7ee2018-11-15 06:06:06 +0100556 if (argc == 5)
557 return fdt_get_header_value(argc, argv);
558
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500559 u32 version = fdt_version(working_fdt);
560 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
561 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
562 fdt_totalsize(working_fdt));
563 printf("off_dt_struct:\t\t0x%x\n",
564 fdt_off_dt_struct(working_fdt));
565 printf("off_dt_strings:\t\t0x%x\n",
566 fdt_off_dt_strings(working_fdt));
567 printf("off_mem_rsvmap:\t\t0x%x\n",
568 fdt_off_mem_rsvmap(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600569 printf("version:\t\t%d\n", version);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500570 printf("last_comp_version:\t%d\n",
571 fdt_last_comp_version(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600572 if (version >= 2)
573 printf("boot_cpuid_phys:\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500574 fdt_boot_cpuid_phys(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600575 if (version >= 3)
576 printf("size_dt_strings:\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500577 fdt_size_dt_strings(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600578 if (version >= 17)
579 printf("size_dt_struct:\t\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500580 fdt_size_dt_struct(working_fdt));
581 printf("number mem_rsv:\t\t0x%x\n",
582 fdt_num_mem_rsv(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600583 printf("\n");
584
Wolfgang Denk3b683112010-07-17 01:06:04 +0200585 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600586 * Set boot cpu id
Wolfgang Denk3b683112010-07-17 01:06:04 +0200587 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400588 } else if (strncmp(argv[1], "boo", 3) == 0) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600589 unsigned long tmp = hextoul(argv[2], NULL);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500590 fdt_set_boot_cpuid_phys(working_fdt, tmp);
Kumar Galab79da7b2008-02-15 03:34:36 -0600591
Wolfgang Denk3b683112010-07-17 01:06:04 +0200592 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600593 * memory command
Wolfgang Denk3b683112010-07-17 01:06:04 +0200594 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400595 } else if (strncmp(argv[1], "me", 2) == 0) {
Kumar Galab79da7b2008-02-15 03:34:36 -0600596 uint64_t addr, size;
597 int err;
Heiko Schocher0f602e12009-12-03 11:21:21 +0100598 addr = simple_strtoull(argv[2], NULL, 16);
599 size = simple_strtoull(argv[3], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500600 err = fdt_fixup_memory(working_fdt, addr, size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600601 if (err < 0)
602 return err;
603
Wolfgang Denk3b683112010-07-17 01:06:04 +0200604 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600605 * mem reserve commands
Wolfgang Denk3b683112010-07-17 01:06:04 +0200606 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400607 } else if (strncmp(argv[1], "rs", 2) == 0) {
Kumar Galab79da7b2008-02-15 03:34:36 -0600608 if (argv[2][0] == 'p') {
609 uint64_t addr, size;
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500610 int total = fdt_num_mem_rsv(working_fdt);
Kumar Galab79da7b2008-02-15 03:34:36 -0600611 int j, err;
612 printf("index\t\t start\t\t size\n");
613 printf("-------------------------------"
614 "-----------------\n");
615 for (j = 0; j < total; j++) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500616 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600617 if (err < 0) {
618 printf("libfdt fdt_get_mem_rsv(): %s\n",
619 fdt_strerror(err));
620 return err;
621 }
622 printf(" %x\t%08x%08x\t%08x%08x\n", j,
623 (u32)(addr >> 32),
624 (u32)(addr & 0xffffffff),
625 (u32)(size >> 32),
626 (u32)(size & 0xffffffff));
627 }
628 } else if (argv[2][0] == 'a') {
629 uint64_t addr, size;
630 int err;
Kumar Galab79da7b2008-02-15 03:34:36 -0600631 addr = simple_strtoull(argv[3], NULL, 16);
632 size = simple_strtoull(argv[4], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500633 err = fdt_add_mem_rsv(working_fdt, addr, size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600634
635 if (err < 0) {
636 printf("libfdt fdt_add_mem_rsv(): %s\n",
637 fdt_strerror(err));
638 return err;
639 }
640 } else if (argv[2][0] == 'd') {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600641 unsigned long idx = hextoul(argv[3], NULL);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500642 int err = fdt_del_mem_rsv(working_fdt, idx);
Kumar Galab79da7b2008-02-15 03:34:36 -0600643
644 if (err < 0) {
645 printf("libfdt fdt_del_mem_rsv(): %s\n",
646 fdt_strerror(err));
647 return err;
648 }
649 } else {
650 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +0000651 return CMD_RET_USAGE;
Kumar Galab79da7b2008-02-15 03:34:36 -0600652 }
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500653 }
Gerald Van Baren5606a362007-06-25 23:25:28 -0400654#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500655 /* Call the board-specific fixup routine */
Simon Glass406ba622014-10-23 18:58:48 -0600656 else if (strncmp(argv[1], "boa", 3) == 0) {
657 int err = ft_board_setup(working_fdt, gd->bd);
658
659 if (err) {
660 printf("Failed to update board information in FDT: %s\n",
661 fdt_strerror(err));
662 return CMD_RET_FAILURE;
663 }
Tom Rini84c0f692021-09-12 20:32:32 -0400664#ifdef CONFIG_ARCH_KEYSTONE
Nicholas Faustinie5fb7862018-10-03 12:58:48 +0200665 ft_board_setup_ex(working_fdt, gd->bd);
666#endif
Simon Glass406ba622014-10-23 18:58:48 -0600667 }
Gerald Van Baren5606a362007-06-25 23:25:28 -0400668#endif
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500669 /* Create a chosen node */
Heiko Schocherfce16b92014-03-03 12:19:24 +0100670 else if (strncmp(argv[1], "cho", 3) == 0) {
Kumar Galafb5dcd52008-08-15 08:24:34 -0500671 unsigned long initrd_start = 0, initrd_end = 0;
672
Wolfgang Denk3b683112010-07-17 01:06:04 +0200673 if ((argc != 2) && (argc != 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000674 return CMD_RET_USAGE;
Kumar Galafb5dcd52008-08-15 08:24:34 -0500675
676 if (argc == 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600677 initrd_start = hextoul(argv[2], NULL);
Sean Anderson86b6f472022-03-22 16:59:21 -0400678 initrd_end = initrd_start + hextoul(argv[3], NULL) - 1;
Kumar Galafb5dcd52008-08-15 08:24:34 -0500679 }
680
Masahiro Yamada451c2042014-04-18 17:41:00 +0900681 fdt_chosen(working_fdt);
Masahiro Yamada5b114892014-04-18 17:40:59 +0900682 fdt_initrd(working_fdt, initrd_start, initrd_end);
Heiko Schocherfce16b92014-03-03 12:19:24 +0100683
684#if defined(CONFIG_FIT_SIGNATURE)
685 } else if (strncmp(argv[1], "che", 3) == 0) {
686 int cfg_noffset;
687 int ret;
688 unsigned long addr;
689 struct fdt_header *blob;
690
691 if (!working_fdt)
692 return CMD_RET_FAILURE;
693
694 if (argc > 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600695 addr = hextoul(argv[2], NULL);
Heiko Schocherfce16b92014-03-03 12:19:24 +0100696 blob = map_sysmem(addr, 0);
697 } else {
698 blob = (struct fdt_header *)gd->fdt_blob;
699 }
700 if (!fdt_valid(&blob))
701 return 1;
702
703 gd->fdt_blob = blob;
704 cfg_noffset = fit_conf_get_node(working_fdt, NULL);
705 if (!cfg_noffset) {
706 printf("Could not find configuration node: %s\n",
707 fdt_strerror(cfg_noffset));
708 return CMD_RET_FAILURE;
709 }
710
711 ret = fit_config_verify(working_fdt, cfg_noffset);
Simon Glass5da42d92014-06-12 07:24:45 -0600712 if (ret == 0)
Heiko Schocherfce16b92014-03-03 12:19:24 +0100713 return CMD_RET_SUCCESS;
714 else
715 return CMD_RET_FAILURE;
716#endif
717
Kumar Gala58911df2008-08-15 08:24:44 -0500718 }
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200719#ifdef CONFIG_OF_LIBFDT_OVERLAY
720 /* apply an overlay */
721 else if (strncmp(argv[1], "ap", 2) == 0) {
722 unsigned long addr;
723 struct fdt_header *blob;
Stefan Agnerfed14d02016-12-20 15:58:45 +0100724 int ret;
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200725
726 if (argc != 3)
727 return CMD_RET_USAGE;
728
729 if (!working_fdt)
730 return CMD_RET_FAILURE;
731
Simon Glass3ff49ec2021-07-24 09:03:29 -0600732 addr = hextoul(argv[2], NULL);
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200733 blob = map_sysmem(addr, 0);
734 if (!fdt_valid(&blob))
735 return CMD_RET_FAILURE;
736
Pantelis Antonioud9456a42017-09-04 23:12:12 +0300737 /* apply method prints messages on error */
738 ret = fdt_overlay_apply_verbose(working_fdt, blob);
739 if (ret)
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200740 return CMD_RET_FAILURE;
741 }
742#endif
Kumar Gala58911df2008-08-15 08:24:44 -0500743 /* resize the fdt */
744 else if (strncmp(argv[1], "re", 2) == 0) {
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200745 uint extrasize;
746 if (argc > 2)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600747 extrasize = hextoul(argv[2], NULL);
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200748 else
749 extrasize = 0;
750 fdt_shrink_to_minimum(working_fdt, extrasize);
Kumar Gala58911df2008-08-15 08:24:44 -0500751 }
752 else {
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500753 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +0000754 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400755 }
756
757 return 0;
758}
759
Gerald Van Barene596a162007-05-16 22:39:59 -0400760/****************************************************************************/
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400761
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400762/*
Gerald Van Barene596a162007-05-16 22:39:59 -0400763 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming46891472008-03-31 20:45:56 -0500764 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denka1be4762008-05-20 16:00:29 +0200765 * C conventions.
Gerald Van Barene596a162007-05-16 22:39:59 -0400766 * [00 11 22 .. nn] - byte stream
767 * "string" - If the the value doesn't start with "<" or "[", it is
768 * treated as a string. Note that the quotes are
769 * stripped by the parser before we get the string.
Andy Fleming46891472008-03-31 20:45:56 -0500770 * newval: An array of strings containing the new property as specified
Wolfgang Denka1be4762008-05-20 16:00:29 +0200771 * on the command line
Andy Fleming46891472008-03-31 20:45:56 -0500772 * count: The number of strings in the array
773 * data: A bytestream to be placed in the property
774 * len: The length of the resulting bytestream
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400775 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200776static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
Gerald Van Barene596a162007-05-16 22:39:59 -0400777{
778 char *cp; /* temporary char pointer */
Andy Fleming46891472008-03-31 20:45:56 -0500779 char *newp; /* temporary newval char pointer */
Gerald Van Barene596a162007-05-16 22:39:59 -0400780 unsigned long tmp; /* holds converted values */
Andy Fleming46891472008-03-31 20:45:56 -0500781 int stridx = 0;
Gerald Van Barene596a162007-05-16 22:39:59 -0400782
Andy Fleming46891472008-03-31 20:45:56 -0500783 *len = 0;
784 newp = newval[0];
785
786 /* An array of cells */
787 if (*newp == '<') {
788 newp++;
789 while ((*newp != '>') && (stridx < count)) {
790 /*
791 * Keep searching until we find that last ">"
792 * That way users don't have to escape the spaces
793 */
794 if (*newp == '\0') {
795 newp = newval[++stridx];
796 continue;
797 }
798
799 cp = newp;
800 tmp = simple_strtoul(cp, &newp, 0);
Hannes Schmelzer98788612017-05-30 15:05:44 +0200801 if (*cp != '?')
802 *(fdt32_t *)data = cpu_to_fdt32(tmp);
803 else
804 newp++;
805
Andy Fleming46891472008-03-31 20:45:56 -0500806 data += 4;
807 *len += 4;
808
809 /* If the ptr didn't advance, something went wrong */
810 if ((newp - cp) <= 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400811 printf("Sorry, I could not convert \"%s\"\n",
812 cp);
813 return 1;
814 }
Andy Fleming46891472008-03-31 20:45:56 -0500815
816 while (*newp == ' ')
817 newp++;
Gerald Van Barene596a162007-05-16 22:39:59 -0400818 }
Andy Fleming46891472008-03-31 20:45:56 -0500819
820 if (*newp != '>') {
821 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400822 return 1;
823 }
Andy Fleming46891472008-03-31 20:45:56 -0500824 } else if (*newp == '[') {
Gerald Van Barene596a162007-05-16 22:39:59 -0400825 /*
826 * Byte stream. Convert the values.
827 */
Andy Fleming46891472008-03-31 20:45:56 -0500828 newp++;
Ken MacLeodd028afa2009-09-11 15:16:18 -0500829 while ((stridx < count) && (*newp != ']')) {
Andy Fleming46891472008-03-31 20:45:56 -0500830 while (*newp == ' ')
831 newp++;
Ken MacLeodd028afa2009-09-11 15:16:18 -0500832 if (*newp == '\0') {
Andy Fleming46891472008-03-31 20:45:56 -0500833 newp = newval[++stridx];
Ken MacLeodd028afa2009-09-11 15:16:18 -0500834 continue;
835 }
836 if (!isxdigit(*newp))
837 break;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600838 tmp = hextoul(newp, &newp);
Ken MacLeodd028afa2009-09-11 15:16:18 -0500839 *data++ = tmp & 0xFF;
840 *len = *len + 1;
Gerald Van Barene596a162007-05-16 22:39:59 -0400841 }
Andy Fleming46891472008-03-31 20:45:56 -0500842 if (*newp != ']') {
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700843 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400844 return 1;
845 }
846 } else {
847 /*
Ken MacLeodd028afa2009-09-11 15:16:18 -0500848 * Assume it is one or more strings. Copy it into our
849 * data area for convenience (including the
850 * terminating '\0's).
Gerald Van Barene596a162007-05-16 22:39:59 -0400851 */
Andy Fleming46891472008-03-31 20:45:56 -0500852 while (stridx < count) {
Ken MacLeodd028afa2009-09-11 15:16:18 -0500853 size_t length = strlen(newp) + 1;
Andy Fleming46891472008-03-31 20:45:56 -0500854 strcpy(data, newp);
Ken MacLeodd028afa2009-09-11 15:16:18 -0500855 data += length;
856 *len += length;
Andy Fleming46891472008-03-31 20:45:56 -0500857 newp = newval[++stridx];
858 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400859 }
860 return 0;
861}
862
863/****************************************************************************/
864
865/*
866 * Heuristic to guess if this is a string or concatenated strings.
867 */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400868
869static int is_printable_string(const void *data, int len)
870{
871 const char *s = data;
872
873 /* zero length is not */
874 if (len == 0)
875 return 0;
876
Joe Hershbergerea2243b2012-08-17 10:34:38 +0000877 /* must terminate with zero or '\n' */
878 if (s[len - 1] != '\0' && s[len - 1] != '\n')
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400879 return 0;
880
881 /* printable or a null byte (concatenated strings) */
Joe Hershbergerea2243b2012-08-17 10:34:38 +0000882 while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400883 /*
884 * If we see a null, there are three possibilities:
885 * 1) If len == 1, it is the end of the string, printable
886 * 2) Next character also a null, not printable.
887 * 3) Next character not a null, continue to check.
888 */
889 if (s[0] == '\0') {
890 if (len == 1)
891 return 1;
892 if (s[1] == '\0')
893 return 0;
894 }
895 s++;
896 len--;
897 }
898
899 /* Not the null termination, or not done yet: not printable */
900 if (*s != '\0' || (len != 0))
901 return 0;
902
903 return 1;
904}
905
Gerald Van Barene596a162007-05-16 22:39:59 -0400906
907/*
908 * Print the property in the best format, a heuristic guess. Print as
909 * a string, concatenated strings, a byte, word, double word, or (if all
910 * else fails) it is printed as a stream of bytes.
911 */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400912static void print_data(const void *data, int len)
913{
914 int j;
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200915 const char *env_max_dump;
916 ulong max_dump = ULONG_MAX;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400917
918 /* no data, don't print */
919 if (len == 0)
920 return;
921
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200922 env_max_dump = env_get("fdt_max_dump");
923 if (env_max_dump)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600924 max_dump = hextoul(env_max_dump, NULL);
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200925
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400926 /*
927 * It is a string, but it may have multiple strings (embedded '\0's).
928 */
929 if (is_printable_string(data, len)) {
930 puts("\"");
931 j = 0;
932 while (j < len) {
933 if (j > 0)
934 puts("\", \"");
935 puts(data);
936 j += strlen(data) + 1;
937 data += strlen(data) + 1;
938 }
939 puts("\"");
940 return;
941 }
942
Andy Fleming46891472008-03-31 20:45:56 -0500943 if ((len %4) == 0) {
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200944 if (len > max_dump)
Tom Rini1db8b532012-10-29 14:53:18 +0000945 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000946 else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000947 const __be32 *p;
Andy Fleming46891472008-03-31 20:45:56 -0500948
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000949 printf("<");
950 for (j = 0, p = data; j < len/4; j++)
951 printf("0x%08x%s", fdt32_to_cpu(p[j]),
952 j < (len/4 - 1) ? " " : "");
953 printf(">");
954 }
Andy Fleming46891472008-03-31 20:45:56 -0500955 } else { /* anything else... hexdump */
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200956 if (len > max_dump)
Tom Rini1db8b532012-10-29 14:53:18 +0000957 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000958 else {
959 const u8 *s;
Andy Fleming46891472008-03-31 20:45:56 -0500960
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000961 printf("[");
962 for (j = 0, s = data; j < len; j++)
963 printf("%02x%s", s[j], j < len - 1 ? " " : "");
964 printf("]");
965 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400966 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400967}
968
969/****************************************************************************/
970
971/*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500972 * Recursively print (a portion of) the working_fdt. The depth parameter
Gerald Van Barene596a162007-05-16 22:39:59 -0400973 * determines how deeply nested the fdt is printed.
974 */
Kumar Gala1e386af2007-11-21 14:07:46 -0600975static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barene596a162007-05-16 22:39:59 -0400976{
Gerald Van Barene596a162007-05-16 22:39:59 -0400977 static char tabs[MAX_LEVEL+1] =
978 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
979 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Gala1e386af2007-11-21 14:07:46 -0600980 const void *nodep; /* property node pointer */
Gerald Van Barene596a162007-05-16 22:39:59 -0400981 int nodeoffset; /* node offset from libfdt */
982 int nextoffset; /* next node offset from libfdt */
983 uint32_t tag; /* tag */
984 int len; /* length of the property */
985 int level = 0; /* keep track of nesting level */
Gerald Van Bareneb999ee2007-11-22 17:23:23 -0500986 const struct fdt_property *fdt_prop;
Gerald Van Barene596a162007-05-16 22:39:59 -0400987
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500988 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400989 if (nodeoffset < 0) {
990 /*
991 * Not found or something else bad happened.
992 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500993 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400994 fdt_strerror(nodeoffset));
Gerald Van Barene596a162007-05-16 22:39:59 -0400995 return 1;
996 }
997 /*
998 * The user passed in a property as well as node path.
999 * Print only the given property and then return.
1000 */
1001 if (prop) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001002 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
Gerald Van Barene596a162007-05-16 22:39:59 -04001003 if (len == 0) {
1004 /* no property value */
1005 printf("%s %s\n", pathp, prop);
1006 return 0;
Simon Glass4bc73ef2017-06-07 10:28:42 -06001007 } else if (nodep && len > 0) {
Gerald Van Barenfc1a1092007-11-23 19:43:20 -05001008 printf("%s = ", prop);
Gerald Van Barene596a162007-05-16 22:39:59 -04001009 print_data (nodep, len);
1010 printf("\n");
1011 return 0;
1012 } else {
1013 printf ("libfdt fdt_getprop(): %s\n",
1014 fdt_strerror(len));
1015 return 1;
1016 }
1017 }
1018
1019 /*
1020 * The user passed in a node path and no property,
1021 * print the node and all subnodes.
1022 */
Gerald Van Barene596a162007-05-16 22:39:59 -04001023 while(level >= 0) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001024 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
Gerald Van Barene596a162007-05-16 22:39:59 -04001025 switch(tag) {
1026 case FDT_BEGIN_NODE:
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001027 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001028 if (level <= depth) {
1029 if (pathp == NULL)
1030 pathp = "/* NULL pointer error */";
1031 if (*pathp == '\0')
1032 pathp = "/"; /* root is nameless */
Gerald Van Barene596a162007-05-16 22:39:59 -04001033 printf("%s%s {\n",
1034 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001035 }
Gerald Van Barene596a162007-05-16 22:39:59 -04001036 level++;
Gerald Van Barene596a162007-05-16 22:39:59 -04001037 if (level >= MAX_LEVEL) {
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001038 printf("Nested too deep, aborting.\n");
Gerald Van Barene596a162007-05-16 22:39:59 -04001039 return 1;
1040 }
1041 break;
1042 case FDT_END_NODE:
1043 level--;
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001044 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -04001045 printf("%s};\n", &tabs[MAX_LEVEL - level]);
1046 if (level == 0) {
1047 level = -1; /* exit the loop */
1048 }
1049 break;
1050 case FDT_PROP:
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001051 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001052 sizeof(*fdt_prop));
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001053 pathp = fdt_string(working_fdt,
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001054 fdt32_to_cpu(fdt_prop->nameoff));
1055 len = fdt32_to_cpu(fdt_prop->len);
1056 nodep = fdt_prop->data;
Gerald Van Barene596a162007-05-16 22:39:59 -04001057 if (len < 0) {
1058 printf ("libfdt fdt_getprop(): %s\n",
1059 fdt_strerror(len));
1060 return 1;
1061 } else if (len == 0) {
1062 /* the property has no value */
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001063 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -04001064 printf("%s%s;\n",
1065 &tabs[MAX_LEVEL - level],
1066 pathp);
1067 } else {
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001068 if (level <= depth) {
Gerald Van Barenfc1a1092007-11-23 19:43:20 -05001069 printf("%s%s = ",
Gerald Van Barene596a162007-05-16 22:39:59 -04001070 &tabs[MAX_LEVEL - level],
1071 pathp);
1072 print_data (nodep, len);
1073 printf(";\n");
1074 }
1075 }
1076 break;
1077 case FDT_NOP:
Andrew Klossnere4ad4542008-07-07 06:41:14 -07001078 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barene596a162007-05-16 22:39:59 -04001079 break;
1080 case FDT_END:
1081 return 1;
1082 default:
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001083 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -04001084 printf("Unknown tag 0x%08X\n", tag);
1085 return 1;
1086 }
1087 nodeoffset = nextoffset;
1088 }
1089 return 0;
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001090}
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001091
1092/********************************************************************/
Kim Phillipsdc00a682012-10-29 13:34:31 +00001093#ifdef CONFIG_SYS_LONGHELP
1094static char fdt_help_text[] =
Heinrich Schuchardt9696dbb2022-04-25 18:35:05 +02001095 "addr [-c] [-q] <addr> [<size>] - Set the [control] fdt location to <addr>\n"
Maxime Ripard5a680aa2016-07-05 10:26:45 +02001096#ifdef CONFIG_OF_LIBFDT_OVERLAY
1097 "fdt apply <addr> - Apply overlay to the DT\n"
1098#endif
Gerald Van Baren5606a362007-06-25 23:25:28 -04001099#ifdef CONFIG_OF_BOARD_SETUP
1100 "fdt boardsetup - Do board-specific set up\n"
1101#endif
Simon Glass6c0be912014-10-23 18:58:54 -06001102#ifdef CONFIG_OF_SYSTEM_SETUP
1103 "fdt systemsetup - Do system-specific set up\n"
1104#endif
Gerald Van Baren7655c092008-01-05 15:33:29 -05001105 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +02001106 "fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n"
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001107 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
1108 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
Marek Vasut0282f9f2022-07-08 23:50:43 +02001109 "fdt get value <var> <path> <prop> [<index>] - Get <property> and store in <var>\n"
1110 " In case of stringlist property, use optional <index>\n"
1111 " to select string within the stringlist. Default is 0.\n"
Joe Hershbergerfc5abf02012-08-17 10:34:37 +00001112 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
1113 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
1114 "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 -04001115 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
1116 "fdt mknode <path> <node> - Create a new node after <path>\n"
1117 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Heiko Schocherfefc7ee2018-11-15 06:06:06 +01001118 "fdt header [get <var> <member>] - Display header info\n"
1119 " get - get header member <member> and store it in <var>\n"
Kumar Galab79da7b2008-02-15 03:34:36 -06001120 "fdt bootcpu <id> - Set boot cpuid\n"
1121 "fdt memory <addr> <size> - Add/Update memory node\n"
1122 "fdt rsvmem print - Show current mem reserves\n"
1123 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
1124 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Sean Anderson86b6f472022-03-22 16:59:21 -04001125 "fdt chosen [<start> <size>] - Add/update the /chosen branch in the tree\n"
1126 " <start>/<size> - initrd start addr/size\n"
Heiko Schocherfce16b92014-03-03 12:19:24 +01001127#if defined(CONFIG_FIT_SIGNATURE)
1128 "fdt checksign [<addr>] - check FIT signature\n"
1129 " <start> - addr of key blob\n"
1130 " default gd->fdt_blob\n"
1131#endif
Robert P. J. Day8c60f922016-05-04 04:47:31 -04001132 "NOTE: Dereference aliases by omitting the leading '/', "
Kim Phillipsdc00a682012-10-29 13:34:31 +00001133 "e.g. fdt print ethernet0.";
1134#endif
1135
1136U_BOOT_CMD(
1137 fdt, 255, 0, do_fdt,
1138 "flattened device tree utility commands", fdt_help_text
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001139);