Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 2 | /* |
| 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 Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <command.h> |
Simon Glass | c7694dd | 2019-08-01 09:46:46 -0600 | [diff] [blame] | 12 | #include <env.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 13 | #include <image.h> |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 14 | #include <linux/ctype.h> |
| 15 | #include <linux/types.h> |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 17 | #include <linux/libfdt.h> |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 18 | #include <fdt_support.h> |
Joe Hershberger | 0eb25b6 | 2015-03-22 17:08:59 -0500 | [diff] [blame] | 19 | #include <mapmem.h> |
Simon Glass | a92fd65 | 2013-04-20 08:42:45 +0000 | [diff] [blame] | 20 | #include <asm/io.h> |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 21 | |
| 22 | #define MAX_LEVEL 32 /* how deeply nested we will go */ |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 23 | #define SCRATCHPAD 1024 /* bytes of scratchpad memory */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * Global data (for the gd->bd) |
| 27 | */ |
| 28 | DECLARE_GLOBAL_DATA_PTR; |
| 29 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 30 | static int fdt_parse_prop(char *const*newval, int count, char *data, int *len); |
Kumar Gala | dbaf07c | 2007-11-21 14:07:46 -0600 | [diff] [blame] | 31 | static int fdt_print(const char *pathp, char *prop, int depth); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 32 | static int is_printable_string(const void *data, int len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 33 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 34 | /* |
Gerald Van Baren | ae9e97f | 2008-06-10 22:15:58 -0400 | [diff] [blame] | 35 | * The working_fdt points to our working flattened device tree. |
| 36 | */ |
| 37 | struct fdt_header *working_fdt; |
| 38 | |
Joe Hershberger | 90fbee3 | 2015-02-04 21:56:53 -0600 | [diff] [blame] | 39 | void set_working_fdt_addr(ulong addr) |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 40 | { |
Simon Glass | a92fd65 | 2013-04-20 08:42:45 +0000 | [diff] [blame] | 41 | void *buf; |
| 42 | |
Simon Glass | baf4141 | 2022-10-11 09:47:12 -0600 | [diff] [blame] | 43 | printf("Working FDT set to %lx\n", addr); |
Joe Hershberger | 90fbee3 | 2015-02-04 21:56:53 -0600 | [diff] [blame] | 44 | buf = map_sysmem(addr, 0); |
Simon Glass | a92fd65 | 2013-04-20 08:42:45 +0000 | [diff] [blame] | 45 | working_fdt = buf; |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 46 | env_set_hex("fdtaddr", addr); |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Gerald Van Baren | ae9e97f | 2008-06-10 22:15:58 -0400 | [diff] [blame] | 49 | /* |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 50 | * Get a value from the fdt and format it to be set in the environment |
| 51 | */ |
Marek Vasut | 13982ce | 2022-07-08 23:50:43 +0200 | [diff] [blame] | 52 | static int fdt_value_env_set(const void *nodep, int len, |
| 53 | const char *var, int index) |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 54 | { |
Marek Vasut | 13982ce | 2022-07-08 23:50:43 +0200 | [diff] [blame] | 55 | 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 Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 73 | char buf[11]; |
| 74 | |
Andreas Färber | b05bf6c | 2017-01-09 16:08:02 +0100 | [diff] [blame] | 75 | sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep)); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 76 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 77 | } 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 Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 85 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 86 | } else { |
| 87 | printf("error: unprintable value\n"); |
| 88 | return 1; |
| 89 | } |
| 90 | return 0; |
| 91 | } |
| 92 | |
Heiko Schocher | 8244127 | 2018-11-15 06:06:06 +0100 | [diff] [blame] | 93 | static 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 Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 106 | static int fdt_get_header_value(int argc, char *const argv[]) |
Heiko Schocher | 8244127 | 2018-11-15 06:06:06 +0100 | [diff] [blame] | 107 | { |
| 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 Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 127 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 128 | * Flattened Device Tree command, see the help for parameter definitions. |
| 129 | */ |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 130 | static int do_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 131 | { |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 132 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 133 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 134 | |
Simon Glass | 0c92963 | 2021-07-21 14:55:25 -0600 | [diff] [blame] | 135 | /* fdt addr: Set the address of the fdt */ |
Maxime Ripard | f0ed68e | 2016-07-05 10:26:34 +0200 | [diff] [blame] | 136 | if (strncmp(argv[1], "ad", 2) == 0) { |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 137 | unsigned long addr; |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 138 | int control = 0; |
Peter Hoyes | e9496ec | 2022-03-31 11:53:22 +0100 | [diff] [blame] | 139 | int quiet = 0; |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 140 | struct fdt_header *blob; |
Simon Glass | 0c92963 | 2021-07-21 14:55:25 -0600 | [diff] [blame] | 141 | |
| 142 | /* Set the address [and length] of the fdt */ |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 143 | argc -= 2; |
| 144 | argv += 2; |
Peter Hoyes | e9496ec | 2022-03-31 11:53:22 +0100 | [diff] [blame] | 145 | 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 Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 160 | argc--; |
| 161 | argv++; |
| 162 | } |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 163 | 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 Gala | 7dbc38a | 2008-08-15 08:24:35 -0500 | [diff] [blame] | 169 | return 1; |
Simon Glass | b29a0db | 2021-07-21 14:55:26 -0600 | [diff] [blame] | 170 | printf("%s fdt: %08lx\n", |
| 171 | control ? "Control" : "Working", |
Joe Hershberger | c71a016 | 2015-02-04 21:56:54 -0600 | [diff] [blame] | 172 | control ? (ulong)map_to_sysmem(blob) : |
Simon Glass | b29a0db | 2021-07-21 14:55:26 -0600 | [diff] [blame] | 173 | env_get_hex("fdtaddr", 0)); |
Kumar Gala | 7dbc38a | 2008-08-15 08:24:35 -0500 | [diff] [blame] | 174 | return 0; |
| 175 | } |
| 176 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 177 | addr = hextoul(argv[0], NULL); |
Simon Glass | a92fd65 | 2013-04-20 08:42:45 +0000 | [diff] [blame] | 178 | blob = map_sysmem(addr, 0); |
Peter Hoyes | e9496ec | 2022-03-31 11:53:22 +0100 | [diff] [blame] | 179 | if ((quiet && fdt_check_header(blob)) || |
| 180 | (!quiet && !fdt_valid(&blob))) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 181 | return 1; |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 182 | if (control) |
| 183 | gd->fdt_blob = blob; |
| 184 | else |
Joe Hershberger | 90fbee3 | 2015-02-04 21:56:53 -0600 | [diff] [blame] | 185 | set_working_fdt_addr(addr); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 186 | |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 187 | if (argc >= 2) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 188 | int len; |
| 189 | int err; |
Simon Glass | 0c92963 | 2021-07-21 14:55:25 -0600 | [diff] [blame] | 190 | |
| 191 | /* Optional new length */ |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 192 | len = hextoul(argv[1], NULL); |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 193 | if (len < fdt_totalsize(blob)) { |
Peter Hoyes | e9496ec | 2022-03-31 11:53:22 +0100 | [diff] [blame] | 194 | if (!quiet) |
| 195 | printf("New length %d < existing length %d, ignoring\n", |
| 196 | len, fdt_totalsize(blob)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 197 | } else { |
Simon Glass | 0c92963 | 2021-07-21 14:55:25 -0600 | [diff] [blame] | 198 | /* Open in place with a new length */ |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 199 | err = fdt_open_into(blob, blob, len); |
Peter Hoyes | e9496ec | 2022-03-31 11:53:22 +0100 | [diff] [blame] | 200 | if (!quiet && err != 0) { |
Simon Glass | 0c92963 | 2021-07-21 14:55:25 -0600 | [diff] [blame] | 201 | printf("libfdt fdt_open_into(): %s\n", |
| 202 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
Marek Vasut | e02c945 | 2012-09-05 08:34:44 +0200 | [diff] [blame] | 207 | return CMD_RET_SUCCESS; |
| 208 | } |
| 209 | |
| 210 | if (!working_fdt) { |
Simon Glass | 0c92963 | 2021-07-21 14:55:25 -0600 | [diff] [blame] | 211 | puts("No FDT memory address configured. Please configure\n" |
| 212 | "the FDT address via \"fdt addr <address>\" command.\n" |
| 213 | "Aborting!\n"); |
Marek Vasut | e02c945 | 2012-09-05 08:34:44 +0200 | [diff] [blame] | 214 | return CMD_RET_FAILURE; |
| 215 | } |
| 216 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 217 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 218 | * Move the working_fdt |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 219 | */ |
Marek Vasut | e02c945 | 2012-09-05 08:34:44 +0200 | [diff] [blame] | 220 | if (strncmp(argv[1], "mo", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 221 | struct fdt_header *newaddr; |
| 222 | int len; |
| 223 | int err; |
| 224 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 225 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 226 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 227 | |
| 228 | /* |
| 229 | * Set the address and length of the fdt. |
| 230 | */ |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 231 | working_fdt = (struct fdt_header *)hextoul(argv[2], NULL); |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 232 | if (!fdt_valid(&working_fdt)) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 233 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 234 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 235 | newaddr = (struct fdt_header *)hextoul(argv[3], NULL); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 236 | |
| 237 | /* |
| 238 | * If the user specifies a length, use that. Otherwise use the |
| 239 | * current length. |
| 240 | */ |
| 241 | if (argc <= 4) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 242 | len = fdt_totalsize(working_fdt); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 243 | } else { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 244 | len = hextoul(argv[4], NULL); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 245 | if (len < fdt_totalsize(working_fdt)) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 246 | printf ("New length 0x%X < existing length " |
| 247 | "0x%X, aborting.\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 248 | len, fdt_totalsize(working_fdt)); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 249 | return 1; |
| 250 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Copy to the new location. |
| 255 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 256 | err = fdt_open_into(working_fdt, newaddr, len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 257 | if (err != 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 258 | printf ("libfdt fdt_open_into(): %s\n", |
| 259 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 260 | return 1; |
| 261 | } |
Hiroyuki Yokoyama | b1a7e79 | 2018-10-18 20:43:54 +0200 | [diff] [blame] | 262 | set_working_fdt_addr((ulong)newaddr); |
Fabien Parent | f7f191e | 2016-11-24 15:02:18 +0100 | [diff] [blame] | 263 | #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 Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 267 | |
Fabien Parent | f7f191e | 2016-11-24 15:02:18 +0100 | [diff] [blame] | 268 | 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 Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 274 | /* |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 275 | * Make a new node |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 276 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 277 | } else if (strncmp(argv[1], "mk", 2) == 0) { |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 278 | 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 Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 286 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 287 | return CMD_RET_USAGE; |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 288 | |
| 289 | pathp = argv[2]; |
| 290 | nodep = argv[3]; |
| 291 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 292 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 293 | if (nodeoffset < 0) { |
| 294 | /* |
| 295 | * Not found or something else bad happened. |
| 296 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 297 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 298 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 299 | return 1; |
| 300 | } |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 301 | err = fdt_add_subnode(working_fdt, nodeoffset, nodep); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 302 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 303 | printf ("libfdt fdt_add_subnode(): %s\n", |
| 304 | fdt_strerror(err)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 305 | return 1; |
| 306 | } |
| 307 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 308 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 309 | * Set the value of a property in the working_fdt. |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 310 | */ |
Tom Warren | 0688b75 | 2020-03-26 15:20:44 -0700 | [diff] [blame] | 311 | } else if (strncmp(argv[1], "se", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 312 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 313 | char *prop; /* property */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 314 | int nodeoffset; /* node offset from libfdt */ |
Bernhard Messerklinger | 6dfd65f | 2017-09-28 11:29:52 +0200 | [diff] [blame] | 315 | static char data[SCRATCHPAD] __aligned(4);/* property storage */ |
Hannes Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 316 | const void *ptmp; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 317 | int len; /* new length of the property */ |
| 318 | int ret; /* return value */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 319 | |
| 320 | /* |
Gerald Van Baren | ea6d8be | 2008-01-05 14:52:04 -0500 | [diff] [blame] | 321 | * Parameters: Node path, property, optional value. |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 322 | */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 323 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 324 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 325 | |
| 326 | pathp = argv[2]; |
| 327 | prop = argv[3]; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 328 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 329 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 330 | if (nodeoffset < 0) { |
| 331 | /* |
| 332 | * Not found or something else bad happened. |
| 333 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 334 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 335 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 336 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 337 | } |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 338 | |
Hannes Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 339 | 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 Schmelzer | cee8c35 | 2017-08-18 14:41:14 +0200 | [diff] [blame] | 348 | if (ptmp != NULL) |
| 349 | memcpy(data, ptmp, len); |
| 350 | |
Hannes Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 351 | ret = fdt_parse_prop(&argv[4], argc - 4, data, &len); |
| 352 | if (ret != 0) |
| 353 | return ret; |
| 354 | } |
| 355 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 356 | ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 357 | if (ret < 0) { |
| 358 | printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret)); |
| 359 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 360 | } |
| 361 | |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 362 | /******************************************************************** |
| 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 Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 400 | int req_index = -1; |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 401 | int startDepth = fdt_node_depth( |
| 402 | working_fdt, nodeoffset); |
| 403 | int curDepth = startDepth; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 404 | int cur_index = -1; |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 405 | int nextNodeOffset = fdt_next_node( |
| 406 | working_fdt, nodeoffset, &curDepth); |
| 407 | |
| 408 | if (subcmd[0] == 'n') |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 409 | req_index = hextoul(argv[5], NULL); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 410 | |
| 411 | while (curDepth > startDepth) { |
| 412 | if (curDepth == startDepth + 1) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 413 | cur_index++; |
| 414 | if (subcmd[0] == 'n' && |
| 415 | cur_index == req_index) { |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 416 | const char *node_name; |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 417 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 418 | node_name = fdt_get_name(working_fdt, |
| 419 | nextNodeOffset, |
| 420 | NULL); |
| 421 | env_set(var, node_name); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 422 | 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 Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 431 | env_set_ulong(var, cur_index + 1); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 432 | } 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 Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 442 | env_set(var, ""); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 443 | return 0; |
Simon Glass | 72c98ed | 2017-06-07 10:28:41 -0600 | [diff] [blame] | 444 | } else if (nodep && len > 0) { |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 445 | if (subcmd[0] == 'v') { |
Marek Vasut | 13982ce | 2022-07-08 23:50:43 +0200 | [diff] [blame] | 446 | int index = 0; |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 447 | int ret; |
| 448 | |
Marek Vasut | 13982ce | 2022-07-08 23:50:43 +0200 | [diff] [blame] | 449 | if (argc == 7) |
| 450 | index = simple_strtoul(argv[6], NULL, 10); |
| 451 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 452 | ret = fdt_value_env_set(nodep, len, |
Marek Vasut | 13982ce | 2022-07-08 23:50:43 +0200 | [diff] [blame] | 453 | var, index); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 454 | if (ret != 0) |
| 455 | return ret; |
| 456 | } else if (subcmd[0] == 'a') { |
| 457 | /* Get address */ |
| 458 | char buf[11]; |
| 459 | |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 460 | sprintf(buf, "0x%p", nodep); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 461 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 462 | } else if (subcmd[0] == 's') { |
| 463 | /* Get size */ |
| 464 | char buf[11]; |
| 465 | |
| 466 | sprintf(buf, "0x%08X", len); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 467 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 468 | } 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 Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 478 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 479 | * Print (recursive) / List (single level) |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 480 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 481 | } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 482 | int depth = MAX_LEVEL; /* how deep to print */ |
| 483 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 484 | char *prop; /* property */ |
| 485 | int ret; /* return value */ |
Kumar Gala | f738b4a | 2007-10-25 16:15:07 -0500 | [diff] [blame] | 486 | static char root[2] = "/"; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 487 | |
| 488 | /* |
| 489 | * list is an alias for print, but limited to 1 level |
| 490 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 491 | if (argv[1][0] == 'l') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 492 | 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 Gala | f738b4a | 2007-10-25 16:15:07 -0500 | [diff] [blame] | 499 | if (argc == 2) |
| 500 | pathp = root; |
| 501 | else |
| 502 | pathp = argv[2]; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 503 | if (argc > 3) |
| 504 | prop = argv[3]; |
| 505 | else |
| 506 | prop = NULL; |
| 507 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 508 | ret = fdt_print(pathp, prop, depth); |
| 509 | if (ret != 0) |
| 510 | return ret; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 511 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 512 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 513 | * Remove a property/node |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 514 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 515 | } else if (strncmp(argv[1], "rm", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 516 | 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 Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 523 | nodeoffset = fdt_path_offset (working_fdt, argv[2]); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 524 | if (nodeoffset < 0) { |
| 525 | /* |
| 526 | * Not found or something else bad happened. |
| 527 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 528 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 529 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 530 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 531 | } |
| 532 | /* |
| 533 | * Do the delete. A fourth parameter means delete a property, |
| 534 | * otherwise delete the node. |
| 535 | */ |
| 536 | if (argc > 3) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 537 | err = fdt_delprop(working_fdt, nodeoffset, argv[3]); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 538 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 539 | printf("libfdt fdt_delprop(): %s\n", |
| 540 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 541 | return err; |
| 542 | } |
| 543 | } else { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 544 | err = fdt_del_node(working_fdt, nodeoffset); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 545 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 546 | printf("libfdt fdt_del_node(): %s\n", |
| 547 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 548 | return err; |
| 549 | } |
| 550 | } |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 551 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 552 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 553 | * Display header info |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 554 | */ |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 555 | } else if (argv[1][0] == 'h') { |
Heiko Schocher | 8244127 | 2018-11-15 06:06:06 +0100 | [diff] [blame] | 556 | if (argc == 5) |
| 557 | return fdt_get_header_value(argc, argv); |
| 558 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 559 | 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 Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 569 | printf("version:\t\t%d\n", version); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 570 | printf("last_comp_version:\t%d\n", |
| 571 | fdt_last_comp_version(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 572 | if (version >= 2) |
| 573 | printf("boot_cpuid_phys:\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 574 | fdt_boot_cpuid_phys(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 575 | if (version >= 3) |
| 576 | printf("size_dt_strings:\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 577 | fdt_size_dt_strings(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 578 | if (version >= 17) |
| 579 | printf("size_dt_struct:\t\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 580 | fdt_size_dt_struct(working_fdt)); |
| 581 | printf("number mem_rsv:\t\t0x%x\n", |
| 582 | fdt_num_mem_rsv(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 583 | printf("\n"); |
| 584 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 585 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 586 | * Set boot cpu id |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 587 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 588 | } else if (strncmp(argv[1], "boo", 3) == 0) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 589 | unsigned long tmp = hextoul(argv[2], NULL); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 590 | fdt_set_boot_cpuid_phys(working_fdt, tmp); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 591 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 592 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 593 | * memory command |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 594 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 595 | } else if (strncmp(argv[1], "me", 2) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 596 | uint64_t addr, size; |
| 597 | int err; |
Heiko Schocher | 4b142fe | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 598 | addr = simple_strtoull(argv[2], NULL, 16); |
| 599 | size = simple_strtoull(argv[3], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 600 | err = fdt_fixup_memory(working_fdt, addr, size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 601 | if (err < 0) |
| 602 | return err; |
| 603 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 604 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 605 | * mem reserve commands |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 606 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 607 | } else if (strncmp(argv[1], "rs", 2) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 608 | if (argv[2][0] == 'p') { |
| 609 | uint64_t addr, size; |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 610 | int total = fdt_num_mem_rsv(working_fdt); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 611 | 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 Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 616 | err = fdt_get_mem_rsv(working_fdt, j, &addr, &size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 617 | 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 Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 631 | addr = simple_strtoull(argv[3], NULL, 16); |
| 632 | size = simple_strtoull(argv[4], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 633 | err = fdt_add_mem_rsv(working_fdt, addr, size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 634 | |
| 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 Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 641 | unsigned long idx = hextoul(argv[3], NULL); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 642 | int err = fdt_del_mem_rsv(working_fdt, idx); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 643 | |
| 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 Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 651 | return CMD_RET_USAGE; |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 652 | } |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 653 | } |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 654 | #ifdef CONFIG_OF_BOARD_SETUP |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 655 | /* Call the board-specific fixup routine */ |
Simon Glass | 4ba98dc | 2014-10-23 18:58:48 -0600 | [diff] [blame] | 656 | 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 Rini | f899cc1 | 2021-09-12 20:32:32 -0400 | [diff] [blame] | 664 | #ifdef CONFIG_ARCH_KEYSTONE |
Nicholas Faustini | 2c76d31 | 2018-10-03 12:58:48 +0200 | [diff] [blame] | 665 | ft_board_setup_ex(working_fdt, gd->bd); |
| 666 | #endif |
Simon Glass | 4ba98dc | 2014-10-23 18:58:48 -0600 | [diff] [blame] | 667 | } |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 668 | #endif |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 669 | /* Create a chosen node */ |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 670 | else if (strncmp(argv[1], "cho", 3) == 0) { |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 671 | unsigned long initrd_start = 0, initrd_end = 0; |
| 672 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 673 | if ((argc != 2) && (argc != 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 674 | return CMD_RET_USAGE; |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 675 | |
| 676 | if (argc == 4) { |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 677 | initrd_start = hextoul(argv[2], NULL); |
Sean Anderson | dbf6f7c | 2022-03-22 16:59:21 -0400 | [diff] [blame] | 678 | initrd_end = initrd_start + hextoul(argv[3], NULL) - 1; |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 679 | } |
| 680 | |
Masahiro Yamada | bc6ed0f | 2014-04-18 17:41:00 +0900 | [diff] [blame] | 681 | fdt_chosen(working_fdt); |
Masahiro Yamada | dbe963a | 2014-04-18 17:40:59 +0900 | [diff] [blame] | 682 | fdt_initrd(working_fdt, initrd_start, initrd_end); |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 683 | |
| 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 Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 695 | addr = hextoul(argv[2], NULL); |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 696 | 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 Glass | 12df2ab | 2014-06-12 07:24:45 -0600 | [diff] [blame] | 712 | if (ret == 0) |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 713 | return CMD_RET_SUCCESS; |
| 714 | else |
| 715 | return CMD_RET_FAILURE; |
| 716 | #endif |
| 717 | |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 718 | } |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 719 | #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 Agner | 082b141 | 2016-12-20 15:58:45 +0100 | [diff] [blame] | 724 | int ret; |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 725 | |
| 726 | if (argc != 3) |
| 727 | return CMD_RET_USAGE; |
| 728 | |
| 729 | if (!working_fdt) |
| 730 | return CMD_RET_FAILURE; |
| 731 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 732 | addr = hextoul(argv[2], NULL); |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 733 | blob = map_sysmem(addr, 0); |
| 734 | if (!fdt_valid(&blob)) |
| 735 | return CMD_RET_FAILURE; |
| 736 | |
Pantelis Antoniou | 81ecc5d | 2017-09-04 23:12:12 +0300 | [diff] [blame] | 737 | /* apply method prints messages on error */ |
| 738 | ret = fdt_overlay_apply_verbose(working_fdt, blob); |
| 739 | if (ret) |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 740 | return CMD_RET_FAILURE; |
| 741 | } |
| 742 | #endif |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 743 | /* resize the fdt */ |
| 744 | else if (strncmp(argv[1], "re", 2) == 0) { |
Hannes Schmelzer | ef47683 | 2016-09-20 18:10:43 +0200 | [diff] [blame] | 745 | uint extrasize; |
| 746 | if (argc > 2) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 747 | extrasize = hextoul(argv[2], NULL); |
Hannes Schmelzer | ef47683 | 2016-09-20 18:10:43 +0200 | [diff] [blame] | 748 | else |
| 749 | extrasize = 0; |
| 750 | fdt_shrink_to_minimum(working_fdt, extrasize); |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 751 | } |
| 752 | else { |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 753 | /* Unrecognized command */ |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 754 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | return 0; |
| 758 | } |
| 759 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 760 | /****************************************************************************/ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 761 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 762 | /* |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 763 | * Parse the user's input, partially heuristic. Valid formats: |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 764 | * <0x00112233 4 05> - an array of cells. Numbers follow standard |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 765 | * C conventions. |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 766 | * [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 Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 770 | * newval: An array of strings containing the new property as specified |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 771 | * on the command line |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 772 | * 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 Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 775 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 776 | static int fdt_parse_prop(char * const *newval, int count, char *data, int *len) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 777 | { |
| 778 | char *cp; /* temporary char pointer */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 779 | char *newp; /* temporary newval char pointer */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 780 | unsigned long tmp; /* holds converted values */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 781 | int stridx = 0; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 782 | |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 783 | *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 Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 801 | if (*cp != '?') |
| 802 | *(fdt32_t *)data = cpu_to_fdt32(tmp); |
| 803 | else |
| 804 | newp++; |
| 805 | |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 806 | data += 4; |
| 807 | *len += 4; |
| 808 | |
| 809 | /* If the ptr didn't advance, something went wrong */ |
| 810 | if ((newp - cp) <= 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 811 | printf("Sorry, I could not convert \"%s\"\n", |
| 812 | cp); |
| 813 | return 1; |
| 814 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 815 | |
| 816 | while (*newp == ' ') |
| 817 | newp++; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 818 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 819 | |
| 820 | if (*newp != '>') { |
| 821 | printf("Unexpected character '%c'\n", *newp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 822 | return 1; |
| 823 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 824 | } else if (*newp == '[') { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 825 | /* |
| 826 | * Byte stream. Convert the values. |
| 827 | */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 828 | newp++; |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 829 | while ((stridx < count) && (*newp != ']')) { |
| 830 | while (*newp == ' ') |
| 831 | newp++; |
| 832 | if (*newp == '\0') { |
| 833 | newp = newval[++stridx]; |
| 834 | continue; |
| 835 | } |
| 836 | if (!isxdigit(*newp)) |
| 837 | break; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 838 | tmp = hextoul(newp, &newp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 839 | *data++ = tmp & 0xFF; |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 840 | *len = *len + 1; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 841 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 842 | if (*newp != ']') { |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 843 | printf("Unexpected character '%c'\n", *newp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 844 | return 1; |
| 845 | } |
| 846 | } else { |
| 847 | /* |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 848 | * Assume it is one or more strings. Copy it into our |
| 849 | * data area for convenience (including the |
| 850 | * terminating '\0's). |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 851 | */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 852 | while (stridx < count) { |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 853 | size_t length = strlen(newp) + 1; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 854 | strcpy(data, newp); |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 855 | data += length; |
| 856 | *len += length; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 857 | newp = newval[++stridx]; |
| 858 | } |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 859 | } |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | /****************************************************************************/ |
| 864 | |
| 865 | /* |
| 866 | * Heuristic to guess if this is a string or concatenated strings. |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 867 | */ |
| 868 | |
| 869 | static 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 Hershberger | 8805bee | 2012-08-17 10:34:38 +0000 | [diff] [blame] | 877 | /* must terminate with zero or '\n' */ |
| 878 | if (s[len - 1] != '\0' && s[len - 1] != '\n') |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 879 | return 0; |
| 880 | |
| 881 | /* printable or a null byte (concatenated strings) */ |
Joe Hershberger | 8805bee | 2012-08-17 10:34:38 +0000 | [diff] [blame] | 882 | while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 883 | /* |
| 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 Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 906 | |
| 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 Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 912 | static void print_data(const void *data, int len) |
| 913 | { |
| 914 | int j; |
Heinrich Schuchardt | 43913f0 | 2020-06-19 19:45:55 +0200 | [diff] [blame] | 915 | const char *env_max_dump; |
| 916 | ulong max_dump = ULONG_MAX; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 917 | |
| 918 | /* no data, don't print */ |
| 919 | if (len == 0) |
| 920 | return; |
| 921 | |
Heinrich Schuchardt | 43913f0 | 2020-06-19 19:45:55 +0200 | [diff] [blame] | 922 | env_max_dump = env_get("fdt_max_dump"); |
| 923 | if (env_max_dump) |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 924 | max_dump = hextoul(env_max_dump, NULL); |
Heinrich Schuchardt | 43913f0 | 2020-06-19 19:45:55 +0200 | [diff] [blame] | 925 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 926 | /* |
| 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 Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 943 | if ((len %4) == 0) { |
Heinrich Schuchardt | 43913f0 | 2020-06-19 19:45:55 +0200 | [diff] [blame] | 944 | if (len > max_dump) |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 945 | printf("* 0x%p [0x%08x]", data, len); |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 946 | else { |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 947 | const __be32 *p; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 948 | |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 949 | 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 Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 955 | } else { /* anything else... hexdump */ |
Heinrich Schuchardt | 43913f0 | 2020-06-19 19:45:55 +0200 | [diff] [blame] | 956 | if (len > max_dump) |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 957 | printf("* 0x%p [0x%08x]", data, len); |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 958 | else { |
| 959 | const u8 *s; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 960 | |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 961 | printf("["); |
| 962 | for (j = 0, s = data; j < len; j++) |
| 963 | printf("%02x%s", s[j], j < len - 1 ? " " : ""); |
| 964 | printf("]"); |
| 965 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 969 | /****************************************************************************/ |
| 970 | |
| 971 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 972 | * Recursively print (a portion of) the working_fdt. The depth parameter |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 973 | * determines how deeply nested the fdt is printed. |
| 974 | */ |
Kumar Gala | dbaf07c | 2007-11-21 14:07:46 -0600 | [diff] [blame] | 975 | static int fdt_print(const char *pathp, char *prop, int depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 976 | { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 977 | 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 Gala | dbaf07c | 2007-11-21 14:07:46 -0600 | [diff] [blame] | 980 | const void *nodep; /* property node pointer */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 981 | 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 Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 986 | const struct fdt_property *fdt_prop; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 987 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 988 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 989 | if (nodeoffset < 0) { |
| 990 | /* |
| 991 | * Not found or something else bad happened. |
| 992 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 993 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 994 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 995 | 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 Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1002 | nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1003 | if (len == 0) { |
| 1004 | /* no property value */ |
| 1005 | printf("%s %s\n", pathp, prop); |
| 1006 | return 0; |
Simon Glass | 9f95267 | 2017-06-07 10:28:42 -0600 | [diff] [blame] | 1007 | } else if (nodep && len > 0) { |
Gerald Van Baren | 28f384b | 2007-11-23 19:43:20 -0500 | [diff] [blame] | 1008 | printf("%s = ", prop); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1009 | 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 Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1023 | while(level >= 0) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1024 | tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1025 | switch(tag) { |
| 1026 | case FDT_BEGIN_NODE: |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1027 | pathp = fdt_get_name(working_fdt, nodeoffset, NULL); |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1028 | if (level <= depth) { |
| 1029 | if (pathp == NULL) |
| 1030 | pathp = "/* NULL pointer error */"; |
| 1031 | if (*pathp == '\0') |
| 1032 | pathp = "/"; /* root is nameless */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1033 | printf("%s%s {\n", |
| 1034 | &tabs[MAX_LEVEL - level], pathp); |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1035 | } |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1036 | level++; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1037 | if (level >= MAX_LEVEL) { |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1038 | printf("Nested too deep, aborting.\n"); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1039 | return 1; |
| 1040 | } |
| 1041 | break; |
| 1042 | case FDT_END_NODE: |
| 1043 | level--; |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1044 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1045 | 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 Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1051 | fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset, |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1052 | sizeof(*fdt_prop)); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1053 | pathp = fdt_string(working_fdt, |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1054 | fdt32_to_cpu(fdt_prop->nameoff)); |
| 1055 | len = fdt32_to_cpu(fdt_prop->len); |
| 1056 | nodep = fdt_prop->data; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1057 | 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 Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1063 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1064 | printf("%s%s;\n", |
| 1065 | &tabs[MAX_LEVEL - level], |
| 1066 | pathp); |
| 1067 | } else { |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1068 | if (level <= depth) { |
Gerald Van Baren | 28f384b | 2007-11-23 19:43:20 -0500 | [diff] [blame] | 1069 | printf("%s%s = ", |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1070 | &tabs[MAX_LEVEL - level], |
| 1071 | pathp); |
| 1072 | print_data (nodep, len); |
| 1073 | printf(";\n"); |
| 1074 | } |
| 1075 | } |
| 1076 | break; |
| 1077 | case FDT_NOP: |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 1078 | printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1079 | break; |
| 1080 | case FDT_END: |
| 1081 | return 1; |
| 1082 | default: |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1083 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1084 | printf("Unknown tag 0x%08X\n", tag); |
| 1085 | return 1; |
| 1086 | } |
| 1087 | nodeoffset = nextoffset; |
| 1088 | } |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1092 | /********************************************************************/ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1093 | #ifdef CONFIG_SYS_LONGHELP |
| 1094 | static char fdt_help_text[] = |
Heinrich Schuchardt | e426963 | 2022-04-25 18:35:05 +0200 | [diff] [blame] | 1095 | "addr [-c] [-q] <addr> [<size>] - Set the [control] fdt location to <addr>\n" |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 1096 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 1097 | "fdt apply <addr> - Apply overlay to the DT\n" |
| 1098 | #endif |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 1099 | #ifdef CONFIG_OF_BOARD_SETUP |
| 1100 | "fdt boardsetup - Do board-specific set up\n" |
| 1101 | #endif |
Simon Glass | c654b51 | 2014-10-23 18:58:54 -0600 | [diff] [blame] | 1102 | #ifdef CONFIG_OF_SYSTEM_SETUP |
| 1103 | "fdt systemsetup - Do system-specific set up\n" |
| 1104 | #endif |
Gerald Van Baren | 238cb7a | 2008-01-05 15:33:29 -0500 | [diff] [blame] | 1105 | "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n" |
Hannes Schmelzer | ef47683 | 2016-09-20 18:10:43 +0200 | [diff] [blame] | 1106 | "fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed\n" |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1107 | "fdt print <path> [<prop>] - Recursive print starting at <path>\n" |
| 1108 | "fdt list <path> [<prop>] - Print one level starting at <path>\n" |
Marek Vasut | 13982ce | 2022-07-08 23:50:43 +0200 | [diff] [blame] | 1109 | "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 Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 1112 | "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 Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1115 | "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 Schocher | 8244127 | 2018-11-15 06:06:06 +0100 | [diff] [blame] | 1118 | "fdt header [get <var> <member>] - Display header info\n" |
| 1119 | " get - get header member <member> and store it in <var>\n" |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 1120 | "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 Anderson | dbf6f7c | 2022-03-22 16:59:21 -0400 | [diff] [blame] | 1125 | "fdt chosen [<start> <size>] - Add/update the /chosen branch in the tree\n" |
| 1126 | " <start>/<size> - initrd start addr/size\n" |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 1127 | #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. Day | 1cc0a9f | 2016-05-04 04:47:31 -0400 | [diff] [blame] | 1132 | "NOTE: Dereference aliases by omitting the leading '/', " |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1133 | "e.g. fdt print ethernet0."; |
| 1134 | #endif |
| 1135 | |
| 1136 | U_BOOT_CMD( |
| 1137 | fdt, 255, 0, do_fdt, |
| 1138 | "flattened device tree utility commands", fdt_help_text |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1139 | ); |