blob: d16b141ce32d2173b08e64a8084b7b2b0f5a5192 [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
Gerald Van Barenade8ef42007-03-31 12:22:10 -040010#include <command.h>
Simon Glass313112a2019-08-01 09:46:46 -060011#include <env.h>
Simon Glass2dc9c342020-05-10 11:40:01 -060012#include <image.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040013#include <linux/ctype.h>
14#include <linux/types.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040015#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090016#include <linux/libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040017#include <fdt_support.h>
Joe Hershberger65b905b2015-03-22 17:08:59 -050018#include <mapmem.h>
Simon Glassb7fc7102013-04-20 08:42:45 +000019#include <asm/io.h>
Gerald Van Barenade8ef42007-03-31 12:22:10 -040020
21#define MAX_LEVEL 32 /* how deeply nested we will go */
Gerald Van Baren5606a362007-06-25 23:25:28 -040022#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
Gerald Van Barenade8ef42007-03-31 12:22:10 -040023
24/*
25 * Global data (for the gd->bd)
26 */
27DECLARE_GLOBAL_DATA_PTR;
28
Wolfgang Denk6262d0212010-06-28 22:00:46 +020029static int fdt_parse_prop(char *const*newval, int count, char *data, int *len);
Kumar Gala1e386af2007-11-21 14:07:46 -060030static int fdt_print(const char *pathp, char *prop, int depth);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000031static int is_printable_string(const void *data, int len);
Gerald Van Barenade8ef42007-03-31 12:22:10 -040032
Gerald Van Barenade8ef42007-03-31 12:22:10 -040033/*
Gerald Van Barene81ce612008-06-10 22:15:58 -040034 * The working_fdt points to our working flattened device tree.
35 */
36struct fdt_header *working_fdt;
37
Peter Hoyescc91ed72023-03-21 13:01:16 +000038static void set_working_fdt_addr_quiet(ulong addr)
Kumar Gala9f4e7e42008-08-15 08:24:39 -050039{
Simon Glassb7fc7102013-04-20 08:42:45 +000040 void *buf;
41
Joe Hershberger3d4ea4c2015-02-04 21:56:53 -060042 buf = map_sysmem(addr, 0);
Simon Glassb7fc7102013-04-20 08:42:45 +000043 working_fdt = buf;
Simon Glass4d949a22017-08-03 12:22:10 -060044 env_set_hex("fdtaddr", addr);
Kumar Gala9f4e7e42008-08-15 08:24:39 -050045}
46
Peter Hoyescc91ed72023-03-21 13:01:16 +000047void set_working_fdt_addr(ulong addr)
48{
49 printf("Working FDT set to %lx\n", addr);
50 set_working_fdt_addr_quiet(addr);
51}
52
Gerald Van Barene81ce612008-06-10 22:15:58 -040053/*
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000054 * Get a value from the fdt and format it to be set in the environment
55 */
Marek Vasut0282f9f2022-07-08 23:50:43 +020056static int fdt_value_env_set(const void *nodep, int len,
57 const char *var, int index)
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000058{
Marek Vasut0282f9f2022-07-08 23:50:43 +020059 if (is_printable_string(nodep, len)) {
60 const char *nodec = (const char *)nodep;
61 int i;
62
63 /*
64 * Iterate over all members in stringlist and find the one at
65 * offset $index. If no such index exists, indicate failure.
66 */
Marek Vasut0b425492022-11-14 22:49:59 +010067 for (i = 0; i < len; ) {
68 if (index-- > 0) {
69 i += strlen(nodec) + 1;
70 nodec += strlen(nodec) + 1;
Marek Vasut0282f9f2022-07-08 23:50:43 +020071 continue;
Marek Vasut0b425492022-11-14 22:49:59 +010072 }
Marek Vasut0282f9f2022-07-08 23:50:43 +020073
Marek Vasut0b425492022-11-14 22:49:59 +010074 env_set(var, nodec);
Marek Vasut0282f9f2022-07-08 23:50:43 +020075 return 0;
76 }
77
78 return 1;
79 } else if (len == 4) {
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000080 char buf[11];
81
Andreas Färber1a09cd22017-01-09 16:08:02 +010082 sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep));
Simon Glass6a38e412017-08-03 12:22:09 -060083 env_set(var, buf);
Marek Vasuteeaae332023-03-02 04:08:23 +010084 } else if (len % 4 == 0 && index >= 0) {
85 /* Needed to print integer arrays. */
86 const unsigned int *nodec = (const unsigned int *)nodep;
87 char buf[11];
88
89 if (index * 4 >= len)
90 return 1;
91
92 sprintf(buf, "0x%08X", fdt32_to_cpu(*(nodec + index)));
93 env_set(var, buf);
94 } else if (len % 4 == 0 && len <= 20) {
Joe Hershbergerfc5abf02012-08-17 10:34:37 +000095 /* Needed to print things like sha1 hashes. */
96 char buf[41];
97 int i;
98
99 for (i = 0; i < len; i += sizeof(unsigned int))
100 sprintf(buf + (i * 2), "%08x",
101 *(unsigned int *)(nodep + i));
Simon Glass6a38e412017-08-03 12:22:09 -0600102 env_set(var, buf);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000103 } else {
104 printf("error: unprintable value\n");
105 return 1;
106 }
107 return 0;
108}
109
Heiko Schocherfefc7ee2018-11-15 06:06:06 +0100110static const char * const fdt_member_table[] = {
111 "magic",
112 "totalsize",
113 "off_dt_struct",
114 "off_dt_strings",
115 "off_mem_rsvmap",
116 "version",
117 "last_comp_version",
118 "boot_cpuid_phys",
119 "size_dt_strings",
120 "size_dt_struct",
121};
122
Simon Glassed38aef2020-05-10 11:40:03 -0600123static int fdt_get_header_value(int argc, char *const argv[])
Heiko Schocherfefc7ee2018-11-15 06:06:06 +0100124{
125 fdt32_t *fdtp = (fdt32_t *)working_fdt;
126 ulong val;
127 int i;
128
129 if (argv[2][0] != 'g')
130 return CMD_RET_FAILURE;
131
132 for (i = 0; i < ARRAY_SIZE(fdt_member_table); i++) {
133 if (strcmp(fdt_member_table[i], argv[4]))
134 continue;
135
136 val = fdt32_to_cpu(fdtp[i]);
137 env_set_hex(argv[3], val);
138 return CMD_RET_SUCCESS;
139 }
140
141 return CMD_RET_FAILURE;
142}
143
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000144/*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400145 * Flattened Device Tree command, see the help for parameter definitions.
146 */
Simon Glassed38aef2020-05-10 11:40:03 -0600147static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400148{
Wolfgang Denk3b683112010-07-17 01:06:04 +0200149 if (argc < 2)
Simon Glassa06dfc72011-12-10 08:44:01 +0000150 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400151
Simon Glasse4647a12021-07-21 14:55:25 -0600152 /* fdt addr: Set the address of the fdt */
Maxime Ripard09caa012016-07-05 10:26:34 +0200153 if (strncmp(argv[1], "ad", 2) == 0) {
Kumar Gala9f4e7e42008-08-15 08:24:39 -0500154 unsigned long addr;
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000155 int control = 0;
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100156 int quiet = 0;
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000157 struct fdt_header *blob;
Simon Glasse4647a12021-07-21 14:55:25 -0600158
159 /* Set the address [and length] of the fdt */
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000160 argc -= 2;
161 argv += 2;
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100162 while (argc > 0 && **argv == '-') {
163 char *arg = *argv;
164
165 while (*++arg) {
166 switch (*arg) {
167 case 'c':
168 control = 1;
169 break;
170 case 'q':
171 quiet = 1;
172 break;
173 default:
174 return CMD_RET_USAGE;
175 }
176 }
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000177 argc--;
178 argv++;
179 }
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000180 if (argc == 0) {
181 if (control)
182 blob = (struct fdt_header *)gd->fdt_blob;
183 else
184 blob = working_fdt;
185 if (!blob || !fdt_valid(&blob))
Kumar Gala708a18b2008-08-15 08:24:35 -0500186 return 1;
Simon Glasseedc9222021-07-21 14:55:26 -0600187 printf("%s fdt: %08lx\n",
188 control ? "Control" : "Working",
Joe Hershberger6256dcc2015-02-04 21:56:54 -0600189 control ? (ulong)map_to_sysmem(blob) :
Simon Glasseedc9222021-07-21 14:55:26 -0600190 env_get_hex("fdtaddr", 0));
Kumar Gala708a18b2008-08-15 08:24:35 -0500191 return 0;
192 }
193
Simon Glass3ff49ec2021-07-24 09:03:29 -0600194 addr = hextoul(argv[0], NULL);
Simon Glassb7fc7102013-04-20 08:42:45 +0000195 blob = map_sysmem(addr, 0);
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100196 if ((quiet && fdt_check_header(blob)) ||
197 (!quiet && !fdt_valid(&blob)))
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400198 return 1;
Peter Hoyescc91ed72023-03-21 13:01:16 +0000199 if (control) {
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000200 gd->fdt_blob = blob;
Peter Hoyescc91ed72023-03-21 13:01:16 +0000201 } else {
202 if (quiet)
203 set_working_fdt_addr_quiet(addr);
204 else
205 set_working_fdt_addr(addr);
206 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400207
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000208 if (argc >= 2) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400209 int len;
210 int err;
Simon Glasse4647a12021-07-21 14:55:25 -0600211
212 /* Optional new length */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600213 len = hextoul(argv[1], NULL);
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000214 if (len < fdt_totalsize(blob)) {
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100215 if (!quiet)
216 printf("New length %d < existing length %d, ignoring\n",
217 len, fdt_totalsize(blob));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400218 } else {
Simon Glasse4647a12021-07-21 14:55:25 -0600219 /* Open in place with a new length */
Simon Glass0bfc4cc2013-04-20 08:42:44 +0000220 err = fdt_open_into(blob, blob, len);
Peter Hoyesfbbaa592022-03-31 11:53:22 +0100221 if (!quiet && err != 0) {
Simon Glasse4647a12021-07-21 14:55:25 -0600222 printf("libfdt fdt_open_into(): %s\n",
223 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400224 }
225 }
226 }
227
Marek Vasutaeccfee2012-09-05 08:34:44 +0200228 return CMD_RET_SUCCESS;
Marek Vasutaeccfee2012-09-05 08:34:44 +0200229
Wolfgang Denk3b683112010-07-17 01:06:04 +0200230 /*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500231 * Move the working_fdt
Wolfgang Denk3b683112010-07-17 01:06:04 +0200232 */
Andre Przywarae9049942023-02-10 11:02:12 +0000233 } else if (strncmp(argv[1], "mo", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400234 struct fdt_header *newaddr;
235 int len;
236 int err;
237
Wolfgang Denk3b683112010-07-17 01:06:04 +0200238 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000239 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400240
241 /*
242 * Set the address and length of the fdt.
243 */
Andre Przywaraa906f872023-02-10 11:02:11 +0000244 working_fdt = map_sysmem(hextoul(argv[2], NULL), 0);
Simon Glass546b9a32013-04-20 08:42:42 +0000245 if (!fdt_valid(&working_fdt))
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400246 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400247
Andre Przywaraa906f872023-02-10 11:02:11 +0000248 newaddr = map_sysmem(hextoul(argv[3], NULL), 0);
Gerald Van Baren4d223922007-04-25 22:47:15 -0400249
250 /*
251 * If the user specifies a length, use that. Otherwise use the
252 * current length.
253 */
254 if (argc <= 4) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500255 len = fdt_totalsize(working_fdt);
Gerald Van Baren4d223922007-04-25 22:47:15 -0400256 } else {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600257 len = hextoul(argv[4], NULL);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500258 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400259 printf ("New length 0x%X < existing length "
260 "0x%X, aborting.\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500261 len, fdt_totalsize(working_fdt));
Gerald Van Baren4d223922007-04-25 22:47:15 -0400262 return 1;
263 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400264 }
265
266 /*
267 * Copy to the new location.
268 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500269 err = fdt_open_into(working_fdt, newaddr, len);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400270 if (err != 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400271 printf ("libfdt fdt_open_into(): %s\n",
272 fdt_strerror(err));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400273 return 1;
274 }
Andre Przywaraa906f872023-02-10 11:02:11 +0000275 set_working_fdt_addr(map_to_sysmem(newaddr));
Andre Przywarae9049942023-02-10 11:02:12 +0000276
277 return CMD_RET_SUCCESS;
278 }
279
280 if (!working_fdt) {
281 puts("No FDT memory address configured. Please configure\n"
282 "the FDT address via \"fdt addr <address>\" command.\n"
283 "Aborting!\n");
284 return CMD_RET_FAILURE;
285 }
286
Fabien Parenta0559ac2016-11-24 15:02:18 +0100287#ifdef CONFIG_OF_SYSTEM_SETUP
288 /* Call the board-specific fixup routine */
Andre Przywarae9049942023-02-10 11:02:12 +0000289 if (strncmp(argv[1], "sys", 3) == 0) {
Fabien Parenta0559ac2016-11-24 15:02:18 +0100290 int err = ft_system_setup(working_fdt, gd->bd);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400291
Fabien Parenta0559ac2016-11-24 15:02:18 +0100292 if (err) {
293 printf("Failed to add system information to FDT: %s\n",
294 fdt_strerror(err));
295 return CMD_RET_FAILURE;
296 }
Andre Przywarae9049942023-02-10 11:02:12 +0000297
298 return CMD_RET_SUCCESS;
299 }
Fabien Parenta0559ac2016-11-24 15:02:18 +0100300#endif
Wolfgang Denk3b683112010-07-17 01:06:04 +0200301 /*
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400302 * Make a new node
Wolfgang Denk3b683112010-07-17 01:06:04 +0200303 */
Andre Przywarae9049942023-02-10 11:02:12 +0000304 if (strncmp(argv[1], "mk", 2) == 0) {
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400305 char *pathp; /* path */
306 char *nodep; /* new node to add */
307 int nodeoffset; /* node offset from libfdt */
308 int err;
309
310 /*
311 * Parameters: Node path, new node to be appended to the path.
312 */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200313 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000314 return CMD_RET_USAGE;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400315
316 pathp = argv[2];
317 nodep = argv[3];
318
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500319 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400320 if (nodeoffset < 0) {
321 /*
322 * Not found or something else bad happened.
323 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500324 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400325 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400326 return 1;
327 }
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500328 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400329 if (err < 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400330 printf ("libfdt fdt_add_subnode(): %s\n",
331 fdt_strerror(err));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400332 return 1;
333 }
334
Wolfgang Denk3b683112010-07-17 01:06:04 +0200335 /*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500336 * Set the value of a property in the working_fdt.
Wolfgang Denk3b683112010-07-17 01:06:04 +0200337 */
Tom Warrend2c6dc82020-03-26 15:20:44 -0700338 } else if (strncmp(argv[1], "se", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400339 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400340 char *prop; /* property */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400341 int nodeoffset; /* node offset from libfdt */
Bernhard Messerklinger9b2664f2017-09-28 11:29:52 +0200342 static char data[SCRATCHPAD] __aligned(4);/* property storage */
Hannes Schmelzer98788612017-05-30 15:05:44 +0200343 const void *ptmp;
Gerald Van Barene596a162007-05-16 22:39:59 -0400344 int len; /* new length of the property */
345 int ret; /* return value */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400346
347 /*
Gerald Van Barend91383a2008-01-05 14:52:04 -0500348 * Parameters: Node path, property, optional value.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400349 */
Wolfgang Denk3b683112010-07-17 01:06:04 +0200350 if (argc < 4)
Simon Glassa06dfc72011-12-10 08:44:01 +0000351 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400352
353 pathp = argv[2];
354 prop = argv[3];
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400355
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500356 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400357 if (nodeoffset < 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400358 /*
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400359 * Not found or something else bad happened.
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400360 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500361 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400362 fdt_strerror(nodeoffset));
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400363 return 1;
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400364 }
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400365
Hannes Schmelzer98788612017-05-30 15:05:44 +0200366 if (argc == 4) {
367 len = 0;
368 } else {
369 ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len);
370 if (len > SCRATCHPAD) {
371 printf("prop (%d) doesn't fit in scratchpad!\n",
372 len);
373 return 1;
374 }
Hannes Schmelzera99c0522017-08-18 14:41:14 +0200375 if (ptmp != NULL)
376 memcpy(data, ptmp, len);
377
Hannes Schmelzer98788612017-05-30 15:05:44 +0200378 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
379 if (ret != 0)
380 return ret;
381 }
382
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500383 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400384 if (ret < 0) {
385 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
386 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400387 }
388
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000389 /********************************************************************
390 * Get the value of a property in the working_fdt.
391 ********************************************************************/
392 } else if (argv[1][0] == 'g') {
393 char *subcmd; /* sub-command */
394 char *pathp; /* path */
395 char *prop; /* property */
396 char *var; /* variable to store result */
397 int nodeoffset; /* node offset from libfdt */
398 const void *nodep; /* property node pointer */
399 int len = 0; /* new length of the property */
400
401 /*
402 * Parameters: Node path, property, optional value.
403 */
404 if (argc < 5)
405 return CMD_RET_USAGE;
406
407 subcmd = argv[2];
408
409 if (argc < 6 && subcmd[0] != 's')
410 return CMD_RET_USAGE;
411
412 var = argv[3];
413 pathp = argv[4];
414 prop = argv[5];
415
416 nodeoffset = fdt_path_offset(working_fdt, pathp);
417 if (nodeoffset < 0) {
418 /*
419 * Not found or something else bad happened.
420 */
421 printf("libfdt fdt_path_offset() returned %s\n",
422 fdt_strerror(nodeoffset));
423 return 1;
424 }
425
426 if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600427 int req_index = -1;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000428 int startDepth = fdt_node_depth(
429 working_fdt, nodeoffset);
430 int curDepth = startDepth;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600431 int cur_index = -1;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000432 int nextNodeOffset = fdt_next_node(
433 working_fdt, nodeoffset, &curDepth);
434
435 if (subcmd[0] == 'n')
Simon Glass3ff49ec2021-07-24 09:03:29 -0600436 req_index = hextoul(argv[5], NULL);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000437
438 while (curDepth > startDepth) {
439 if (curDepth == startDepth + 1)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600440 cur_index++;
441 if (subcmd[0] == 'n' &&
442 cur_index == req_index) {
Simon Glass6a38e412017-08-03 12:22:09 -0600443 const char *node_name;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000444
Simon Glass6a38e412017-08-03 12:22:09 -0600445 node_name = fdt_get_name(working_fdt,
446 nextNodeOffset,
447 NULL);
448 env_set(var, node_name);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000449 return 0;
450 }
451 nextNodeOffset = fdt_next_node(
452 working_fdt, nextNodeOffset, &curDepth);
453 if (nextNodeOffset < 0)
454 break;
455 }
456 if (subcmd[0] == 's') {
457 /* get the num nodes at this level */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600458 env_set_ulong(var, cur_index + 1);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000459 } else {
460 /* node index not found */
461 printf("libfdt node not found\n");
462 return 1;
463 }
464 } else {
465 nodep = fdt_getprop(
466 working_fdt, nodeoffset, prop, &len);
Marek Vasut5c385f82023-03-02 04:08:15 +0100467 if (nodep && len >= 0) {
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000468 if (subcmd[0] == 'v') {
Marek Vasuteeaae332023-03-02 04:08:23 +0100469 int index = -1;
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000470 int ret;
471
Marek Vasut5c385f82023-03-02 04:08:15 +0100472 if (len == 0) {
473 /* no property value */
474 env_set(var, "");
475 return 0;
476 }
477
Marek Vasut0282f9f2022-07-08 23:50:43 +0200478 if (argc == 7)
479 index = simple_strtoul(argv[6], NULL, 10);
480
Simon Glass6a38e412017-08-03 12:22:09 -0600481 ret = fdt_value_env_set(nodep, len,
Marek Vasut0282f9f2022-07-08 23:50:43 +0200482 var, index);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000483 if (ret != 0)
484 return ret;
485 } else if (subcmd[0] == 'a') {
Marek Vasutf7039672023-03-11 17:29:21 +0100486 env_set_hex(var, (ulong)map_to_sysmem(nodep));
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000487 } else if (subcmd[0] == 's') {
Marek Vasutf7039672023-03-11 17:29:21 +0100488 env_set_hex(var, len);
Joe Hershbergerfc5abf02012-08-17 10:34:37 +0000489 } else
490 return CMD_RET_USAGE;
491 return 0;
492 } else {
493 printf("libfdt fdt_getprop(): %s\n",
494 fdt_strerror(len));
495 return 1;
496 }
497 }
498
Wolfgang Denk3b683112010-07-17 01:06:04 +0200499 /*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400500 * Print (recursive) / List (single level)
Wolfgang Denk3b683112010-07-17 01:06:04 +0200501 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400502 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400503 int depth = MAX_LEVEL; /* how deep to print */
504 char *pathp; /* path */
Gerald Van Barene596a162007-05-16 22:39:59 -0400505 char *prop; /* property */
506 int ret; /* return value */
Kumar Gala51ab7f92007-10-25 16:15:07 -0500507 static char root[2] = "/";
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400508
509 /*
510 * list is an alias for print, but limited to 1 level
511 */
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400512 if (argv[1][0] == 'l') {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400513 depth = 1;
514 }
515
516 /*
517 * Get the starting path. The root node is an oddball,
518 * the offset is zero and has no name.
519 */
Kumar Gala51ab7f92007-10-25 16:15:07 -0500520 if (argc == 2)
521 pathp = root;
522 else
523 pathp = argv[2];
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400524 if (argc > 3)
525 prop = argv[3];
526 else
527 prop = NULL;
528
Gerald Van Barene596a162007-05-16 22:39:59 -0400529 ret = fdt_print(pathp, prop, depth);
530 if (ret != 0)
531 return ret;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400532
Wolfgang Denk3b683112010-07-17 01:06:04 +0200533 /*
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400534 * Remove a property/node
Wolfgang Denk3b683112010-07-17 01:06:04 +0200535 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400536 } else if (strncmp(argv[1], "rm", 2) == 0) {
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400537 int nodeoffset; /* node offset from libfdt */
538 int err;
539
540 /*
541 * Get the path. The root node is an oddball, the offset
542 * is zero and has no name.
543 */
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500544 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400545 if (nodeoffset < 0) {
546 /*
547 * Not found or something else bad happened.
548 */
Kumar Galac8ab7052007-10-24 11:04:22 -0500549 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -0400550 fdt_strerror(nodeoffset));
Gerald Van Baren0ea55d22007-05-12 09:47:25 -0400551 return 1;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400552 }
553 /*
554 * Do the delete. A fourth parameter means delete a property,
555 * otherwise delete the node.
556 */
557 if (argc > 3) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500558 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400559 if (err < 0) {
Marek Vasuta7a58872023-03-02 04:08:16 +0100560 printf("libfdt fdt_delprop(): %s\n",
Gerald Van Barene596a162007-05-16 22:39:59 -0400561 fdt_strerror(err));
Marek Vasuta7a58872023-03-02 04:08:16 +0100562 return CMD_RET_FAILURE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400563 }
564 } else {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500565 err = fdt_del_node(working_fdt, nodeoffset);
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400566 if (err < 0) {
Marek Vasuta7a58872023-03-02 04:08:16 +0100567 printf("libfdt fdt_del_node(): %s\n",
Gerald Van Barene596a162007-05-16 22:39:59 -0400568 fdt_strerror(err));
Marek Vasuta7a58872023-03-02 04:08:16 +0100569 return CMD_RET_FAILURE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400570 }
571 }
Kumar Galab79da7b2008-02-15 03:34:36 -0600572
Wolfgang Denk3b683112010-07-17 01:06:04 +0200573 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600574 * Display header info
Wolfgang Denk3b683112010-07-17 01:06:04 +0200575 */
Kumar Galab79da7b2008-02-15 03:34:36 -0600576 } else if (argv[1][0] == 'h') {
Heiko Schocherfefc7ee2018-11-15 06:06:06 +0100577 if (argc == 5)
578 return fdt_get_header_value(argc, argv);
579
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500580 u32 version = fdt_version(working_fdt);
581 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
582 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
583 fdt_totalsize(working_fdt));
584 printf("off_dt_struct:\t\t0x%x\n",
585 fdt_off_dt_struct(working_fdt));
586 printf("off_dt_strings:\t\t0x%x\n",
587 fdt_off_dt_strings(working_fdt));
588 printf("off_mem_rsvmap:\t\t0x%x\n",
589 fdt_off_mem_rsvmap(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600590 printf("version:\t\t%d\n", version);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500591 printf("last_comp_version:\t%d\n",
592 fdt_last_comp_version(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600593 if (version >= 2)
594 printf("boot_cpuid_phys:\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500595 fdt_boot_cpuid_phys(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600596 if (version >= 3)
597 printf("size_dt_strings:\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500598 fdt_size_dt_strings(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600599 if (version >= 17)
600 printf("size_dt_struct:\t\t0x%x\n",
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500601 fdt_size_dt_struct(working_fdt));
602 printf("number mem_rsv:\t\t0x%x\n",
603 fdt_num_mem_rsv(working_fdt));
Kumar Galab79da7b2008-02-15 03:34:36 -0600604 printf("\n");
605
Wolfgang Denk3b683112010-07-17 01:06:04 +0200606 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600607 * Set boot cpu id
Wolfgang Denk3b683112010-07-17 01:06:04 +0200608 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400609 } else if (strncmp(argv[1], "boo", 3) == 0) {
Marek Vasut48d7a772023-03-02 04:08:18 +0100610 unsigned long tmp;
611
612 if (argc != 3)
613 return CMD_RET_USAGE;
614
615 tmp = hextoul(argv[2], NULL);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500616 fdt_set_boot_cpuid_phys(working_fdt, tmp);
Kumar Galab79da7b2008-02-15 03:34:36 -0600617
Wolfgang Denk3b683112010-07-17 01:06:04 +0200618 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600619 * memory command
Wolfgang Denk3b683112010-07-17 01:06:04 +0200620 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400621 } else if (strncmp(argv[1], "me", 2) == 0) {
Kumar Galab79da7b2008-02-15 03:34:36 -0600622 uint64_t addr, size;
623 int err;
Marek Vasutc9dfd022023-03-02 04:08:19 +0100624
625 if (argc != 4)
626 return CMD_RET_USAGE;
627
Heiko Schocher0f602e12009-12-03 11:21:21 +0100628 addr = simple_strtoull(argv[2], NULL, 16);
629 size = simple_strtoull(argv[3], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500630 err = fdt_fixup_memory(working_fdt, addr, size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600631 if (err < 0)
632 return err;
633
Wolfgang Denk3b683112010-07-17 01:06:04 +0200634 /*
Kumar Galab79da7b2008-02-15 03:34:36 -0600635 * mem reserve commands
Wolfgang Denk3b683112010-07-17 01:06:04 +0200636 */
Gerald Van Baren3b9d6292008-06-09 21:02:17 -0400637 } else if (strncmp(argv[1], "rs", 2) == 0) {
Kumar Galab79da7b2008-02-15 03:34:36 -0600638 if (argv[2][0] == 'p') {
639 uint64_t addr, size;
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500640 int total = fdt_num_mem_rsv(working_fdt);
Kumar Galab79da7b2008-02-15 03:34:36 -0600641 int j, err;
642 printf("index\t\t start\t\t size\n");
643 printf("-------------------------------"
644 "-----------------\n");
645 for (j = 0; j < total; j++) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500646 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600647 if (err < 0) {
648 printf("libfdt fdt_get_mem_rsv(): %s\n",
649 fdt_strerror(err));
650 return err;
651 }
652 printf(" %x\t%08x%08x\t%08x%08x\n", j,
653 (u32)(addr >> 32),
654 (u32)(addr & 0xffffffff),
655 (u32)(size >> 32),
656 (u32)(size & 0xffffffff));
657 }
658 } else if (argv[2][0] == 'a') {
659 uint64_t addr, size;
660 int err;
Kumar Galab79da7b2008-02-15 03:34:36 -0600661 addr = simple_strtoull(argv[3], NULL, 16);
662 size = simple_strtoull(argv[4], NULL, 16);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500663 err = fdt_add_mem_rsv(working_fdt, addr, size);
Kumar Galab79da7b2008-02-15 03:34:36 -0600664
665 if (err < 0) {
Marek Vasut794c8e62023-03-02 04:08:17 +0100666 printf("libfdt fdt_add_mem_rsv(): %s\n",
Kumar Galab79da7b2008-02-15 03:34:36 -0600667 fdt_strerror(err));
Marek Vasut794c8e62023-03-02 04:08:17 +0100668 return CMD_RET_FAILURE;
Kumar Galab79da7b2008-02-15 03:34:36 -0600669 }
670 } else if (argv[2][0] == 'd') {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600671 unsigned long idx = hextoul(argv[3], NULL);
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500672 int err = fdt_del_mem_rsv(working_fdt, idx);
Kumar Galab79da7b2008-02-15 03:34:36 -0600673
674 if (err < 0) {
Marek Vasut794c8e62023-03-02 04:08:17 +0100675 printf("libfdt fdt_del_mem_rsv(): %s\n",
Kumar Galab79da7b2008-02-15 03:34:36 -0600676 fdt_strerror(err));
Marek Vasut794c8e62023-03-02 04:08:17 +0100677 return CMD_RET_FAILURE;
Kumar Galab79da7b2008-02-15 03:34:36 -0600678 }
679 } else {
680 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +0000681 return CMD_RET_USAGE;
Kumar Galab79da7b2008-02-15 03:34:36 -0600682 }
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500683 }
Gerald Van Baren5606a362007-06-25 23:25:28 -0400684#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500685 /* Call the board-specific fixup routine */
Simon Glass406ba622014-10-23 18:58:48 -0600686 else if (strncmp(argv[1], "boa", 3) == 0) {
687 int err = ft_board_setup(working_fdt, gd->bd);
688
689 if (err) {
690 printf("Failed to update board information in FDT: %s\n",
691 fdt_strerror(err));
692 return CMD_RET_FAILURE;
693 }
Tom Rini84c0f692021-09-12 20:32:32 -0400694#ifdef CONFIG_ARCH_KEYSTONE
Nicholas Faustinie5fb7862018-10-03 12:58:48 +0200695 ft_board_setup_ex(working_fdt, gd->bd);
696#endif
Simon Glass406ba622014-10-23 18:58:48 -0600697 }
Gerald Van Baren5606a362007-06-25 23:25:28 -0400698#endif
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500699 /* Create a chosen node */
Heiko Schocherfce16b92014-03-03 12:19:24 +0100700 else if (strncmp(argv[1], "cho", 3) == 0) {
Kumar Galafb5dcd52008-08-15 08:24:34 -0500701 unsigned long initrd_start = 0, initrd_end = 0;
702
Wolfgang Denk3b683112010-07-17 01:06:04 +0200703 if ((argc != 2) && (argc != 4))
Simon Glassa06dfc72011-12-10 08:44:01 +0000704 return CMD_RET_USAGE;
Kumar Galafb5dcd52008-08-15 08:24:34 -0500705
706 if (argc == 4) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600707 initrd_start = hextoul(argv[2], NULL);
Sean Anderson86b6f472022-03-22 16:59:21 -0400708 initrd_end = initrd_start + hextoul(argv[3], NULL) - 1;
Kumar Galafb5dcd52008-08-15 08:24:34 -0500709 }
710
Masahiro Yamada451c2042014-04-18 17:41:00 +0900711 fdt_chosen(working_fdt);
Masahiro Yamada5b114892014-04-18 17:40:59 +0900712 fdt_initrd(working_fdt, initrd_start, initrd_end);
Heiko Schocherfce16b92014-03-03 12:19:24 +0100713
714#if defined(CONFIG_FIT_SIGNATURE)
715 } else if (strncmp(argv[1], "che", 3) == 0) {
716 int cfg_noffset;
717 int ret;
718 unsigned long addr;
719 struct fdt_header *blob;
720
721 if (!working_fdt)
722 return CMD_RET_FAILURE;
723
724 if (argc > 2) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600725 addr = hextoul(argv[2], NULL);
Heiko Schocherfce16b92014-03-03 12:19:24 +0100726 blob = map_sysmem(addr, 0);
727 } else {
728 blob = (struct fdt_header *)gd->fdt_blob;
729 }
730 if (!fdt_valid(&blob))
731 return 1;
732
733 gd->fdt_blob = blob;
734 cfg_noffset = fit_conf_get_node(working_fdt, NULL);
Bin Meng79980132023-05-01 11:35:25 +0800735 if (cfg_noffset < 0) {
Heiko Schocherfce16b92014-03-03 12:19:24 +0100736 printf("Could not find configuration node: %s\n",
737 fdt_strerror(cfg_noffset));
738 return CMD_RET_FAILURE;
739 }
740
741 ret = fit_config_verify(working_fdt, cfg_noffset);
Simon Glass5da42d92014-06-12 07:24:45 -0600742 if (ret == 0)
Heiko Schocherfce16b92014-03-03 12:19:24 +0100743 return CMD_RET_SUCCESS;
744 else
745 return CMD_RET_FAILURE;
746#endif
747
Kumar Gala58911df2008-08-15 08:24:44 -0500748 }
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200749#ifdef CONFIG_OF_LIBFDT_OVERLAY
750 /* apply an overlay */
751 else if (strncmp(argv[1], "ap", 2) == 0) {
752 unsigned long addr;
753 struct fdt_header *blob;
Stefan Agnerfed14d02016-12-20 15:58:45 +0100754 int ret;
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200755
756 if (argc != 3)
757 return CMD_RET_USAGE;
758
759 if (!working_fdt)
760 return CMD_RET_FAILURE;
761
Simon Glass3ff49ec2021-07-24 09:03:29 -0600762 addr = hextoul(argv[2], NULL);
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200763 blob = map_sysmem(addr, 0);
764 if (!fdt_valid(&blob))
765 return CMD_RET_FAILURE;
766
Pantelis Antonioud9456a42017-09-04 23:12:12 +0300767 /* apply method prints messages on error */
768 ret = fdt_overlay_apply_verbose(working_fdt, blob);
769 if (ret)
Maxime Ripard5a680aa2016-07-05 10:26:45 +0200770 return CMD_RET_FAILURE;
771 }
772#endif
Kumar Gala58911df2008-08-15 08:24:44 -0500773 /* resize the fdt */
774 else if (strncmp(argv[1], "re", 2) == 0) {
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200775 uint extrasize;
776 if (argc > 2)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600777 extrasize = hextoul(argv[2], NULL);
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200778 else
779 extrasize = 0;
780 fdt_shrink_to_minimum(working_fdt, extrasize);
Kumar Gala58911df2008-08-15 08:24:44 -0500781 }
782 else {
Kim Phillipsbd9d1152007-07-17 13:57:04 -0500783 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +0000784 return CMD_RET_USAGE;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400785 }
786
787 return 0;
788}
789
Gerald Van Barene596a162007-05-16 22:39:59 -0400790/****************************************************************************/
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400791
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400792/*
Gerald Van Barene596a162007-05-16 22:39:59 -0400793 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming46891472008-03-31 20:45:56 -0500794 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denka1be4762008-05-20 16:00:29 +0200795 * C conventions.
Gerald Van Barene596a162007-05-16 22:39:59 -0400796 * [00 11 22 .. nn] - byte stream
797 * "string" - If the the value doesn't start with "<" or "[", it is
798 * treated as a string. Note that the quotes are
799 * stripped by the parser before we get the string.
Andy Fleming46891472008-03-31 20:45:56 -0500800 * newval: An array of strings containing the new property as specified
Wolfgang Denka1be4762008-05-20 16:00:29 +0200801 * on the command line
Andy Fleming46891472008-03-31 20:45:56 -0500802 * count: The number of strings in the array
803 * data: A bytestream to be placed in the property
804 * len: The length of the resulting bytestream
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400805 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200806static int fdt_parse_prop(char * const *newval, int count, char *data, int *len)
Gerald Van Barene596a162007-05-16 22:39:59 -0400807{
808 char *cp; /* temporary char pointer */
Andy Fleming46891472008-03-31 20:45:56 -0500809 char *newp; /* temporary newval char pointer */
Gerald Van Barene596a162007-05-16 22:39:59 -0400810 unsigned long tmp; /* holds converted values */
Andy Fleming46891472008-03-31 20:45:56 -0500811 int stridx = 0;
Gerald Van Barene596a162007-05-16 22:39:59 -0400812
Andy Fleming46891472008-03-31 20:45:56 -0500813 *len = 0;
814 newp = newval[0];
815
816 /* An array of cells */
817 if (*newp == '<') {
818 newp++;
819 while ((*newp != '>') && (stridx < count)) {
820 /*
821 * Keep searching until we find that last ">"
822 * That way users don't have to escape the spaces
823 */
824 if (*newp == '\0') {
825 newp = newval[++stridx];
826 continue;
827 }
828
829 cp = newp;
830 tmp = simple_strtoul(cp, &newp, 0);
Hannes Schmelzer98788612017-05-30 15:05:44 +0200831 if (*cp != '?')
832 *(fdt32_t *)data = cpu_to_fdt32(tmp);
833 else
834 newp++;
835
Andy Fleming46891472008-03-31 20:45:56 -0500836 data += 4;
837 *len += 4;
838
839 /* If the ptr didn't advance, something went wrong */
840 if ((newp - cp) <= 0) {
Gerald Van Barene596a162007-05-16 22:39:59 -0400841 printf("Sorry, I could not convert \"%s\"\n",
842 cp);
843 return 1;
844 }
Andy Fleming46891472008-03-31 20:45:56 -0500845
846 while (*newp == ' ')
847 newp++;
Gerald Van Barene596a162007-05-16 22:39:59 -0400848 }
Andy Fleming46891472008-03-31 20:45:56 -0500849
850 if (*newp != '>') {
851 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400852 return 1;
853 }
Andy Fleming46891472008-03-31 20:45:56 -0500854 } else if (*newp == '[') {
Gerald Van Barene596a162007-05-16 22:39:59 -0400855 /*
856 * Byte stream. Convert the values.
857 */
Andy Fleming46891472008-03-31 20:45:56 -0500858 newp++;
Ken MacLeodd028afa2009-09-11 15:16:18 -0500859 while ((stridx < count) && (*newp != ']')) {
Andy Fleming46891472008-03-31 20:45:56 -0500860 while (*newp == ' ')
861 newp++;
Ken MacLeodd028afa2009-09-11 15:16:18 -0500862 if (*newp == '\0') {
Andy Fleming46891472008-03-31 20:45:56 -0500863 newp = newval[++stridx];
Ken MacLeodd028afa2009-09-11 15:16:18 -0500864 continue;
865 }
866 if (!isxdigit(*newp))
867 break;
Simon Glass3ff49ec2021-07-24 09:03:29 -0600868 tmp = hextoul(newp, &newp);
Ken MacLeodd028afa2009-09-11 15:16:18 -0500869 *data++ = tmp & 0xFF;
870 *len = *len + 1;
Gerald Van Barene596a162007-05-16 22:39:59 -0400871 }
Andy Fleming46891472008-03-31 20:45:56 -0500872 if (*newp != ']') {
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700873 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barene596a162007-05-16 22:39:59 -0400874 return 1;
875 }
876 } else {
877 /*
Ken MacLeodd028afa2009-09-11 15:16:18 -0500878 * Assume it is one or more strings. Copy it into our
879 * data area for convenience (including the
880 * terminating '\0's).
Gerald Van Barene596a162007-05-16 22:39:59 -0400881 */
Andy Fleming46891472008-03-31 20:45:56 -0500882 while (stridx < count) {
Ken MacLeodd028afa2009-09-11 15:16:18 -0500883 size_t length = strlen(newp) + 1;
Andy Fleming46891472008-03-31 20:45:56 -0500884 strcpy(data, newp);
Ken MacLeodd028afa2009-09-11 15:16:18 -0500885 data += length;
886 *len += length;
Andy Fleming46891472008-03-31 20:45:56 -0500887 newp = newval[++stridx];
888 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400889 }
890 return 0;
891}
892
893/****************************************************************************/
894
895/*
896 * Heuristic to guess if this is a string or concatenated strings.
897 */
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400898
899static int is_printable_string(const void *data, int len)
900{
901 const char *s = data;
Marek Vasutfdc794a2023-03-02 04:08:14 +0100902 const char *ss, *se;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400903
904 /* zero length is not */
905 if (len == 0)
906 return 0;
907
Marek Vasutfdc794a2023-03-02 04:08:14 +0100908 /* must terminate with zero */
909 if (s[len - 1] != '\0')
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400910 return 0;
911
Marek Vasutfdc794a2023-03-02 04:08:14 +0100912 se = s + len;
913
914 while (s < se) {
915 ss = s;
916 while (s < se && *s && isprint((unsigned char)*s))
917 s++;
918
919 /* not zero, or not done yet */
920 if (*s != '\0' || s == ss)
921 return 0;
922
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400923 s++;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400924 }
925
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400926 return 1;
927}
928
Gerald Van Barene596a162007-05-16 22:39:59 -0400929/*
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 Barenade8ef42007-03-31 12:22:10 -0400934static void print_data(const void *data, int len)
935{
936 int j;
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200937 const char *env_max_dump;
938 ulong max_dump = ULONG_MAX;
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400939
940 /* no data, don't print */
941 if (len == 0)
942 return;
943
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200944 env_max_dump = env_get("fdt_max_dump");
945 if (env_max_dump)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600946 max_dump = hextoul(env_max_dump, NULL);
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200947
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400948 /*
949 * It is a string, but it may have multiple strings (embedded '\0's).
950 */
951 if (is_printable_string(data, len)) {
952 puts("\"");
953 j = 0;
954 while (j < len) {
955 if (j > 0)
956 puts("\", \"");
957 puts(data);
958 j += strlen(data) + 1;
959 data += strlen(data) + 1;
960 }
961 puts("\"");
962 return;
963 }
964
Andy Fleming46891472008-03-31 20:45:56 -0500965 if ((len %4) == 0) {
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200966 if (len > max_dump)
Tom Rini1db8b532012-10-29 14:53:18 +0000967 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000968 else {
Kim Phillipsdc00a682012-10-29 13:34:31 +0000969 const __be32 *p;
Andy Fleming46891472008-03-31 20:45:56 -0500970
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000971 printf("<");
972 for (j = 0, p = data; j < len/4; j++)
973 printf("0x%08x%s", fdt32_to_cpu(p[j]),
974 j < (len/4 - 1) ? " " : "");
975 printf(">");
976 }
Andy Fleming46891472008-03-31 20:45:56 -0500977 } else { /* anything else... hexdump */
Heinrich Schuchardt1a5f58d2020-06-19 19:45:55 +0200978 if (len > max_dump)
Tom Rini1db8b532012-10-29 14:53:18 +0000979 printf("* 0x%p [0x%08x]", data, len);
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000980 else {
981 const u8 *s;
Andy Fleming46891472008-03-31 20:45:56 -0500982
Joe Hershbergerf72c6922012-08-17 10:34:36 +0000983 printf("[");
984 for (j = 0, s = data; j < len; j++)
985 printf("%02x%s", s[j], j < len - 1 ? " " : "");
986 printf("]");
987 }
Gerald Van Barenade8ef42007-03-31 12:22:10 -0400988 }
Gerald Van Barene596a162007-05-16 22:39:59 -0400989}
990
991/****************************************************************************/
992
993/*
Kim Phillipsf3a42e22008-06-10 11:06:17 -0500994 * Recursively print (a portion of) the working_fdt. The depth parameter
Gerald Van Barene596a162007-05-16 22:39:59 -0400995 * determines how deeply nested the fdt is printed.
996 */
Kumar Gala1e386af2007-11-21 14:07:46 -0600997static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barene596a162007-05-16 22:39:59 -0400998{
Gerald Van Barene596a162007-05-16 22:39:59 -0400999 static char tabs[MAX_LEVEL+1] =
1000 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
1001 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Gala1e386af2007-11-21 14:07:46 -06001002 const void *nodep; /* property node pointer */
Gerald Van Barene596a162007-05-16 22:39:59 -04001003 int nodeoffset; /* node offset from libfdt */
1004 int nextoffset; /* next node offset from libfdt */
1005 uint32_t tag; /* tag */
1006 int len; /* length of the property */
1007 int level = 0; /* keep track of nesting level */
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001008 const struct fdt_property *fdt_prop;
Gerald Van Barene596a162007-05-16 22:39:59 -04001009
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001010 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Barene596a162007-05-16 22:39:59 -04001011 if (nodeoffset < 0) {
1012 /*
1013 * Not found or something else bad happened.
1014 */
Kumar Galac8ab7052007-10-24 11:04:22 -05001015 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren1178d6a2007-05-21 23:27:16 -04001016 fdt_strerror(nodeoffset));
Gerald Van Barene596a162007-05-16 22:39:59 -04001017 return 1;
1018 }
1019 /*
1020 * The user passed in a property as well as node path.
1021 * Print only the given property and then return.
1022 */
1023 if (prop) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001024 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
Gerald Van Barene596a162007-05-16 22:39:59 -04001025 if (len == 0) {
1026 /* no property value */
1027 printf("%s %s\n", pathp, prop);
1028 return 0;
Simon Glass4bc73ef2017-06-07 10:28:42 -06001029 } else if (nodep && len > 0) {
Gerald Van Barenfc1a1092007-11-23 19:43:20 -05001030 printf("%s = ", prop);
Gerald Van Barene596a162007-05-16 22:39:59 -04001031 print_data (nodep, len);
1032 printf("\n");
1033 return 0;
1034 } else {
1035 printf ("libfdt fdt_getprop(): %s\n",
1036 fdt_strerror(len));
1037 return 1;
1038 }
1039 }
1040
1041 /*
1042 * The user passed in a node path and no property,
1043 * print the node and all subnodes.
1044 */
Gerald Van Barene596a162007-05-16 22:39:59 -04001045 while(level >= 0) {
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001046 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
Gerald Van Barene596a162007-05-16 22:39:59 -04001047 switch(tag) {
1048 case FDT_BEGIN_NODE:
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001049 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001050 if (level <= depth) {
1051 if (pathp == NULL)
1052 pathp = "/* NULL pointer error */";
1053 if (*pathp == '\0')
1054 pathp = "/"; /* root is nameless */
Gerald Van Barene596a162007-05-16 22:39:59 -04001055 printf("%s%s {\n",
1056 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001057 }
Gerald Van Barene596a162007-05-16 22:39:59 -04001058 level++;
Gerald Van Barene596a162007-05-16 22:39:59 -04001059 if (level >= MAX_LEVEL) {
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001060 printf("Nested too deep, aborting.\n");
Gerald Van Barene596a162007-05-16 22:39:59 -04001061 return 1;
1062 }
1063 break;
1064 case FDT_END_NODE:
1065 level--;
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001066 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -04001067 printf("%s};\n", &tabs[MAX_LEVEL - level]);
1068 if (level == 0) {
1069 level = -1; /* exit the loop */
1070 }
1071 break;
1072 case FDT_PROP:
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001073 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001074 sizeof(*fdt_prop));
Kim Phillipsf3a42e22008-06-10 11:06:17 -05001075 pathp = fdt_string(working_fdt,
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001076 fdt32_to_cpu(fdt_prop->nameoff));
1077 len = fdt32_to_cpu(fdt_prop->len);
1078 nodep = fdt_prop->data;
Gerald Van Barene596a162007-05-16 22:39:59 -04001079 if (len < 0) {
1080 printf ("libfdt fdt_getprop(): %s\n",
1081 fdt_strerror(len));
1082 return 1;
1083 } else if (len == 0) {
1084 /* the property has no value */
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001085 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -04001086 printf("%s%s;\n",
1087 &tabs[MAX_LEVEL - level],
1088 pathp);
1089 } else {
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001090 if (level <= depth) {
Gerald Van Barenfc1a1092007-11-23 19:43:20 -05001091 printf("%s%s = ",
Gerald Van Barene596a162007-05-16 22:39:59 -04001092 &tabs[MAX_LEVEL - level],
1093 pathp);
1094 print_data (nodep, len);
1095 printf(";\n");
1096 }
1097 }
1098 break;
1099 case FDT_NOP:
Andrew Klossnere4ad4542008-07-07 06:41:14 -07001100 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barene596a162007-05-16 22:39:59 -04001101 break;
1102 case FDT_END:
1103 return 1;
1104 default:
Gerald Van Bareneb999ee2007-11-22 17:23:23 -05001105 if (level <= depth)
Gerald Van Barene596a162007-05-16 22:39:59 -04001106 printf("Unknown tag 0x%08X\n", tag);
1107 return 1;
1108 }
1109 nodeoffset = nextoffset;
1110 }
1111 return 0;
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001112}
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001113
1114/********************************************************************/
Tom Rini03f146c2023-10-07 15:13:08 -04001115U_BOOT_LONGHELP(fdt,
Heinrich Schuchardt9696dbb2022-04-25 18:35:05 +02001116 "addr [-c] [-q] <addr> [<size>] - Set the [control] fdt location to <addr>\n"
Maxime Ripard5a680aa2016-07-05 10:26:45 +02001117#ifdef CONFIG_OF_LIBFDT_OVERLAY
1118 "fdt apply <addr> - Apply overlay to the DT\n"
1119#endif
Gerald Van Baren5606a362007-06-25 23:25:28 -04001120#ifdef CONFIG_OF_BOARD_SETUP
1121 "fdt boardsetup - Do board-specific set up\n"
1122#endif
Simon Glass6c0be912014-10-23 18:58:54 -06001123#ifdef CONFIG_OF_SYSTEM_SETUP
1124 "fdt systemsetup - Do system-specific set up\n"
1125#endif
Gerald Van Baren7655c092008-01-05 15:33:29 -05001126 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +02001127 "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 -04001128 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
1129 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
Marek Vasut0282f9f2022-07-08 23:50:43 +02001130 "fdt get value <var> <path> <prop> [<index>] - Get <property> and store in <var>\n"
1131 " In case of stringlist property, use optional <index>\n"
1132 " to select string within the stringlist. Default is 0.\n"
Joe Hershbergerfc5abf02012-08-17 10:34:37 +00001133 "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n"
1134 "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n"
1135 "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 -04001136 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
1137 "fdt mknode <path> <node> - Create a new node after <path>\n"
1138 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Heiko Schocherfefc7ee2018-11-15 06:06:06 +01001139 "fdt header [get <var> <member>] - Display header info\n"
1140 " get - get header member <member> and store it in <var>\n"
Kumar Galab79da7b2008-02-15 03:34:36 -06001141 "fdt bootcpu <id> - Set boot cpuid\n"
1142 "fdt memory <addr> <size> - Add/Update memory node\n"
1143 "fdt rsvmem print - Show current mem reserves\n"
1144 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
1145 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Sean Anderson86b6f472022-03-22 16:59:21 -04001146 "fdt chosen [<start> <size>] - Add/update the /chosen branch in the tree\n"
1147 " <start>/<size> - initrd start addr/size\n"
Heiko Schocherfce16b92014-03-03 12:19:24 +01001148#if defined(CONFIG_FIT_SIGNATURE)
1149 "fdt checksign [<addr>] - check FIT signature\n"
Marek Vasute71c9ee2023-03-02 04:08:20 +01001150 " <addr> - address of key blob\n"
1151 " default gd->fdt_blob\n"
Heiko Schocherfce16b92014-03-03 12:19:24 +01001152#endif
Robert P. J. Day8c60f922016-05-04 04:47:31 -04001153 "NOTE: Dereference aliases by omitting the leading '/', "
Tom Rini03f146c2023-10-07 15:13:08 -04001154 "e.g. fdt print ethernet0.");
Kim Phillipsdc00a682012-10-29 13:34:31 +00001155
1156U_BOOT_CMD(
1157 fdt, 255, 0, do_fdt,
1158 "flattened device tree utility commands", fdt_help_text
Gerald Van Barenade8ef42007-03-31 12:22:10 -04001159);