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 */ |
Simon Glass | 5d927b4 | 2017-05-17 03:25:19 -0600 | [diff] [blame] | 24 | #define CMD_FDT_MAX_DUMP 64 |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | * Global data (for the gd->bd) |
| 28 | */ |
| 29 | DECLARE_GLOBAL_DATA_PTR; |
| 30 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 31 | static int fdt_valid(struct fdt_header **blobp); |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 32 | 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] | 33 | static int fdt_print(const char *pathp, char *prop, int depth); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 34 | static int is_printable_string(const void *data, int len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 35 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 36 | /* |
Gerald Van Baren | ae9e97f | 2008-06-10 22:15:58 -0400 | [diff] [blame] | 37 | * The working_fdt points to our working flattened device tree. |
| 38 | */ |
| 39 | struct fdt_header *working_fdt; |
| 40 | |
Joe Hershberger | 90fbee3 | 2015-02-04 21:56:53 -0600 | [diff] [blame] | 41 | void set_working_fdt_addr(ulong addr) |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 42 | { |
Simon Glass | a92fd65 | 2013-04-20 08:42:45 +0000 | [diff] [blame] | 43 | void *buf; |
| 44 | |
Joe Hershberger | 90fbee3 | 2015-02-04 21:56:53 -0600 | [diff] [blame] | 45 | buf = map_sysmem(addr, 0); |
Simon Glass | a92fd65 | 2013-04-20 08:42:45 +0000 | [diff] [blame] | 46 | working_fdt = buf; |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 47 | env_set_hex("fdtaddr", addr); |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 48 | } |
| 49 | |
Gerald Van Baren | ae9e97f | 2008-06-10 22:15:58 -0400 | [diff] [blame] | 50 | /* |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 51 | * Get a value from the fdt and format it to be set in the environment |
| 52 | */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 53 | static int fdt_value_env_set(const void *nodep, int len, const char *var) |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 54 | { |
| 55 | if (is_printable_string(nodep, len)) |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 56 | env_set(var, (void *)nodep); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 57 | else if (len == 4) { |
| 58 | char buf[11]; |
| 59 | |
Andreas Färber | b05bf6c | 2017-01-09 16:08:02 +0100 | [diff] [blame] | 60 | sprintf(buf, "0x%08X", fdt32_to_cpu(*(fdt32_t *)nodep)); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 61 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 62 | } else if (len%4 == 0 && len <= 20) { |
| 63 | /* Needed to print things like sha1 hashes. */ |
| 64 | char buf[41]; |
| 65 | int i; |
| 66 | |
| 67 | for (i = 0; i < len; i += sizeof(unsigned int)) |
| 68 | sprintf(buf + (i * 2), "%08x", |
| 69 | *(unsigned int *)(nodep + i)); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 70 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 71 | } else { |
| 72 | printf("error: unprintable value\n"); |
| 73 | return 1; |
| 74 | } |
| 75 | return 0; |
| 76 | } |
| 77 | |
Heiko Schocher | 8244127 | 2018-11-15 06:06:06 +0100 | [diff] [blame] | 78 | static const char * const fdt_member_table[] = { |
| 79 | "magic", |
| 80 | "totalsize", |
| 81 | "off_dt_struct", |
| 82 | "off_dt_strings", |
| 83 | "off_mem_rsvmap", |
| 84 | "version", |
| 85 | "last_comp_version", |
| 86 | "boot_cpuid_phys", |
| 87 | "size_dt_strings", |
| 88 | "size_dt_struct", |
| 89 | }; |
| 90 | |
| 91 | static int fdt_get_header_value(int argc, char * const argv[]) |
| 92 | { |
| 93 | fdt32_t *fdtp = (fdt32_t *)working_fdt; |
| 94 | ulong val; |
| 95 | int i; |
| 96 | |
| 97 | if (argv[2][0] != 'g') |
| 98 | return CMD_RET_FAILURE; |
| 99 | |
| 100 | for (i = 0; i < ARRAY_SIZE(fdt_member_table); i++) { |
| 101 | if (strcmp(fdt_member_table[i], argv[4])) |
| 102 | continue; |
| 103 | |
| 104 | val = fdt32_to_cpu(fdtp[i]); |
| 105 | env_set_hex(argv[3], val); |
| 106 | return CMD_RET_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | return CMD_RET_FAILURE; |
| 110 | } |
| 111 | |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 112 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 113 | * Flattened Device Tree command, see the help for parameter definitions. |
| 114 | */ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 115 | static int do_fdt(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 116 | { |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 117 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 118 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 119 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 120 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 121 | * Set the address of the fdt |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 122 | */ |
Maxime Ripard | f0ed68e | 2016-07-05 10:26:34 +0200 | [diff] [blame] | 123 | if (strncmp(argv[1], "ad", 2) == 0) { |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 124 | unsigned long addr; |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 125 | int control = 0; |
| 126 | struct fdt_header *blob; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 127 | /* |
| 128 | * Set the address [and length] of the fdt. |
| 129 | */ |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 130 | argc -= 2; |
| 131 | argv += 2; |
| 132 | /* Temporary #ifdef - some archs don't have fdt_blob yet */ |
| 133 | #ifdef CONFIG_OF_CONTROL |
| 134 | if (argc && !strcmp(*argv, "-c")) { |
| 135 | control = 1; |
| 136 | argc--; |
| 137 | argv++; |
| 138 | } |
| 139 | #endif |
| 140 | if (argc == 0) { |
| 141 | if (control) |
| 142 | blob = (struct fdt_header *)gd->fdt_blob; |
| 143 | else |
| 144 | blob = working_fdt; |
| 145 | if (!blob || !fdt_valid(&blob)) |
Kumar Gala | 7dbc38a | 2008-08-15 08:24:35 -0500 | [diff] [blame] | 146 | return 1; |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 147 | printf("The address of the fdt is %#08lx\n", |
Joe Hershberger | c71a016 | 2015-02-04 21:56:54 -0600 | [diff] [blame] | 148 | control ? (ulong)map_to_sysmem(blob) : |
Simon Glass | bfebc8c | 2017-08-03 12:22:13 -0600 | [diff] [blame] | 149 | env_get_hex("fdtaddr", 0)); |
Kumar Gala | 7dbc38a | 2008-08-15 08:24:35 -0500 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 153 | addr = simple_strtoul(argv[0], NULL, 16); |
Simon Glass | a92fd65 | 2013-04-20 08:42:45 +0000 | [diff] [blame] | 154 | blob = map_sysmem(addr, 0); |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 155 | if (!fdt_valid(&blob)) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 156 | return 1; |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 157 | if (control) |
| 158 | gd->fdt_blob = blob; |
| 159 | else |
Joe Hershberger | 90fbee3 | 2015-02-04 21:56:53 -0600 | [diff] [blame] | 160 | set_working_fdt_addr(addr); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 161 | |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 162 | if (argc >= 2) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 163 | int len; |
| 164 | int err; |
| 165 | /* |
| 166 | * Optional new length |
| 167 | */ |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 168 | len = simple_strtoul(argv[1], NULL, 16); |
| 169 | if (len < fdt_totalsize(blob)) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 170 | printf ("New length %d < existing length %d, " |
| 171 | "ignoring.\n", |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 172 | len, fdt_totalsize(blob)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 173 | } else { |
| 174 | /* |
| 175 | * Open in place with a new length. |
| 176 | */ |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 177 | err = fdt_open_into(blob, blob, len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 178 | if (err != 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 179 | printf ("libfdt fdt_open_into(): %s\n", |
| 180 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
Marek Vasut | e02c945 | 2012-09-05 08:34:44 +0200 | [diff] [blame] | 185 | return CMD_RET_SUCCESS; |
| 186 | } |
| 187 | |
| 188 | if (!working_fdt) { |
| 189 | puts( |
| 190 | "No FDT memory address configured. Please configure\n" |
| 191 | "the FDT address via \"fdt addr <address>\" command.\n" |
| 192 | "Aborting!\n"); |
| 193 | return CMD_RET_FAILURE; |
| 194 | } |
| 195 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 196 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 197 | * Move the working_fdt |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 198 | */ |
Marek Vasut | e02c945 | 2012-09-05 08:34:44 +0200 | [diff] [blame] | 199 | if (strncmp(argv[1], "mo", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 200 | struct fdt_header *newaddr; |
| 201 | int len; |
| 202 | int err; |
| 203 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 204 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 205 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * Set the address and length of the fdt. |
| 209 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 210 | working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16); |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 211 | if (!fdt_valid(&working_fdt)) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 212 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 213 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 214 | newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 215 | |
| 216 | /* |
| 217 | * If the user specifies a length, use that. Otherwise use the |
| 218 | * current length. |
| 219 | */ |
| 220 | if (argc <= 4) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 221 | len = fdt_totalsize(working_fdt); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 222 | } else { |
| 223 | len = simple_strtoul(argv[4], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 224 | if (len < fdt_totalsize(working_fdt)) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 225 | printf ("New length 0x%X < existing length " |
| 226 | "0x%X, aborting.\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 227 | len, fdt_totalsize(working_fdt)); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 228 | return 1; |
| 229 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
| 233 | * Copy to the new location. |
| 234 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 235 | err = fdt_open_into(working_fdt, newaddr, len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 236 | if (err != 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 237 | printf ("libfdt fdt_open_into(): %s\n", |
| 238 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 239 | return 1; |
| 240 | } |
Hiroyuki Yokoyama | b1a7e79 | 2018-10-18 20:43:54 +0200 | [diff] [blame] | 241 | set_working_fdt_addr((ulong)newaddr); |
Fabien Parent | f7f191e | 2016-11-24 15:02:18 +0100 | [diff] [blame] | 242 | #ifdef CONFIG_OF_SYSTEM_SETUP |
| 243 | /* Call the board-specific fixup routine */ |
| 244 | } else if (strncmp(argv[1], "sys", 3) == 0) { |
| 245 | int err = ft_system_setup(working_fdt, gd->bd); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 246 | |
Fabien Parent | f7f191e | 2016-11-24 15:02:18 +0100 | [diff] [blame] | 247 | if (err) { |
| 248 | printf("Failed to add system information to FDT: %s\n", |
| 249 | fdt_strerror(err)); |
| 250 | return CMD_RET_FAILURE; |
| 251 | } |
| 252 | #endif |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 253 | /* |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 254 | * Make a new node |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 255 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 256 | } else if (strncmp(argv[1], "mk", 2) == 0) { |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 257 | char *pathp; /* path */ |
| 258 | char *nodep; /* new node to add */ |
| 259 | int nodeoffset; /* node offset from libfdt */ |
| 260 | int err; |
| 261 | |
| 262 | /* |
| 263 | * Parameters: Node path, new node to be appended to the path. |
| 264 | */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 265 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 266 | return CMD_RET_USAGE; |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 267 | |
| 268 | pathp = argv[2]; |
| 269 | nodep = argv[3]; |
| 270 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 271 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 272 | if (nodeoffset < 0) { |
| 273 | /* |
| 274 | * Not found or something else bad happened. |
| 275 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 276 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 277 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 278 | return 1; |
| 279 | } |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 280 | err = fdt_add_subnode(working_fdt, nodeoffset, nodep); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 281 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 282 | printf ("libfdt fdt_add_subnode(): %s\n", |
| 283 | fdt_strerror(err)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 284 | return 1; |
| 285 | } |
| 286 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 287 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 288 | * Set the value of a property in the working_fdt. |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 289 | */ |
Tom Warren | 0688b75 | 2020-03-26 15:20:44 -0700 | [diff] [blame] | 290 | } else if (strncmp(argv[1], "se", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 291 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 292 | char *prop; /* property */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 293 | int nodeoffset; /* node offset from libfdt */ |
Bernhard Messerklinger | 6dfd65f | 2017-09-28 11:29:52 +0200 | [diff] [blame] | 294 | static char data[SCRATCHPAD] __aligned(4);/* property storage */ |
Hannes Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 295 | const void *ptmp; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 296 | int len; /* new length of the property */ |
| 297 | int ret; /* return value */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 298 | |
| 299 | /* |
Gerald Van Baren | ea6d8be | 2008-01-05 14:52:04 -0500 | [diff] [blame] | 300 | * Parameters: Node path, property, optional value. |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 301 | */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 302 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 303 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 304 | |
| 305 | pathp = argv[2]; |
| 306 | prop = argv[3]; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 307 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 308 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 309 | if (nodeoffset < 0) { |
| 310 | /* |
| 311 | * Not found or something else bad happened. |
| 312 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 313 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 314 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 315 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 316 | } |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 317 | |
Hannes Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 318 | if (argc == 4) { |
| 319 | len = 0; |
| 320 | } else { |
| 321 | ptmp = fdt_getprop(working_fdt, nodeoffset, prop, &len); |
| 322 | if (len > SCRATCHPAD) { |
| 323 | printf("prop (%d) doesn't fit in scratchpad!\n", |
| 324 | len); |
| 325 | return 1; |
| 326 | } |
Hannes Schmelzer | cee8c35 | 2017-08-18 14:41:14 +0200 | [diff] [blame] | 327 | if (ptmp != NULL) |
| 328 | memcpy(data, ptmp, len); |
| 329 | |
Hannes Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 330 | ret = fdt_parse_prop(&argv[4], argc - 4, data, &len); |
| 331 | if (ret != 0) |
| 332 | return ret; |
| 333 | } |
| 334 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 335 | ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 336 | if (ret < 0) { |
| 337 | printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret)); |
| 338 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 339 | } |
| 340 | |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 341 | /******************************************************************** |
| 342 | * Get the value of a property in the working_fdt. |
| 343 | ********************************************************************/ |
| 344 | } else if (argv[1][0] == 'g') { |
| 345 | char *subcmd; /* sub-command */ |
| 346 | char *pathp; /* path */ |
| 347 | char *prop; /* property */ |
| 348 | char *var; /* variable to store result */ |
| 349 | int nodeoffset; /* node offset from libfdt */ |
| 350 | const void *nodep; /* property node pointer */ |
| 351 | int len = 0; /* new length of the property */ |
| 352 | |
| 353 | /* |
| 354 | * Parameters: Node path, property, optional value. |
| 355 | */ |
| 356 | if (argc < 5) |
| 357 | return CMD_RET_USAGE; |
| 358 | |
| 359 | subcmd = argv[2]; |
| 360 | |
| 361 | if (argc < 6 && subcmd[0] != 's') |
| 362 | return CMD_RET_USAGE; |
| 363 | |
| 364 | var = argv[3]; |
| 365 | pathp = argv[4]; |
| 366 | prop = argv[5]; |
| 367 | |
| 368 | nodeoffset = fdt_path_offset(working_fdt, pathp); |
| 369 | if (nodeoffset < 0) { |
| 370 | /* |
| 371 | * Not found or something else bad happened. |
| 372 | */ |
| 373 | printf("libfdt fdt_path_offset() returned %s\n", |
| 374 | fdt_strerror(nodeoffset)); |
| 375 | return 1; |
| 376 | } |
| 377 | |
| 378 | if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) { |
| 379 | int reqIndex = -1; |
| 380 | int startDepth = fdt_node_depth( |
| 381 | working_fdt, nodeoffset); |
| 382 | int curDepth = startDepth; |
| 383 | int curIndex = -1; |
| 384 | int nextNodeOffset = fdt_next_node( |
| 385 | working_fdt, nodeoffset, &curDepth); |
| 386 | |
| 387 | if (subcmd[0] == 'n') |
| 388 | reqIndex = simple_strtoul(argv[5], NULL, 16); |
| 389 | |
| 390 | while (curDepth > startDepth) { |
| 391 | if (curDepth == startDepth + 1) |
| 392 | curIndex++; |
| 393 | if (subcmd[0] == 'n' && curIndex == reqIndex) { |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 394 | const char *node_name; |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 395 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 396 | node_name = fdt_get_name(working_fdt, |
| 397 | nextNodeOffset, |
| 398 | NULL); |
| 399 | env_set(var, node_name); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | nextNodeOffset = fdt_next_node( |
| 403 | working_fdt, nextNodeOffset, &curDepth); |
| 404 | if (nextNodeOffset < 0) |
| 405 | break; |
| 406 | } |
| 407 | if (subcmd[0] == 's') { |
| 408 | /* get the num nodes at this level */ |
Simon Glass | 018f530 | 2017-08-03 12:22:10 -0600 | [diff] [blame] | 409 | env_set_ulong(var, curIndex + 1); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 410 | } else { |
| 411 | /* node index not found */ |
| 412 | printf("libfdt node not found\n"); |
| 413 | return 1; |
| 414 | } |
| 415 | } else { |
| 416 | nodep = fdt_getprop( |
| 417 | working_fdt, nodeoffset, prop, &len); |
| 418 | if (len == 0) { |
| 419 | /* no property value */ |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 420 | env_set(var, ""); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 421 | return 0; |
Simon Glass | 72c98ed | 2017-06-07 10:28:41 -0600 | [diff] [blame] | 422 | } else if (nodep && len > 0) { |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 423 | if (subcmd[0] == 'v') { |
| 424 | int ret; |
| 425 | |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 426 | ret = fdt_value_env_set(nodep, len, |
| 427 | var); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 428 | if (ret != 0) |
| 429 | return ret; |
| 430 | } else if (subcmd[0] == 'a') { |
| 431 | /* Get address */ |
| 432 | char buf[11]; |
| 433 | |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 434 | sprintf(buf, "0x%p", nodep); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 435 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 436 | } else if (subcmd[0] == 's') { |
| 437 | /* Get size */ |
| 438 | char buf[11]; |
| 439 | |
| 440 | sprintf(buf, "0x%08X", len); |
Simon Glass | 382bee5 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 441 | env_set(var, buf); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 442 | } else |
| 443 | return CMD_RET_USAGE; |
| 444 | return 0; |
| 445 | } else { |
| 446 | printf("libfdt fdt_getprop(): %s\n", |
| 447 | fdt_strerror(len)); |
| 448 | return 1; |
| 449 | } |
| 450 | } |
| 451 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 452 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 453 | * Print (recursive) / List (single level) |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 454 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 455 | } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 456 | int depth = MAX_LEVEL; /* how deep to print */ |
| 457 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 458 | char *prop; /* property */ |
| 459 | int ret; /* return value */ |
Kumar Gala | f738b4a | 2007-10-25 16:15:07 -0500 | [diff] [blame] | 460 | static char root[2] = "/"; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * list is an alias for print, but limited to 1 level |
| 464 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 465 | if (argv[1][0] == 'l') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 466 | depth = 1; |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Get the starting path. The root node is an oddball, |
| 471 | * the offset is zero and has no name. |
| 472 | */ |
Kumar Gala | f738b4a | 2007-10-25 16:15:07 -0500 | [diff] [blame] | 473 | if (argc == 2) |
| 474 | pathp = root; |
| 475 | else |
| 476 | pathp = argv[2]; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 477 | if (argc > 3) |
| 478 | prop = argv[3]; |
| 479 | else |
| 480 | prop = NULL; |
| 481 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 482 | ret = fdt_print(pathp, prop, depth); |
| 483 | if (ret != 0) |
| 484 | return ret; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 485 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 486 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 487 | * Remove a property/node |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 488 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 489 | } else if (strncmp(argv[1], "rm", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 490 | int nodeoffset; /* node offset from libfdt */ |
| 491 | int err; |
| 492 | |
| 493 | /* |
| 494 | * Get the path. The root node is an oddball, the offset |
| 495 | * is zero and has no name. |
| 496 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 497 | nodeoffset = fdt_path_offset (working_fdt, argv[2]); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 498 | if (nodeoffset < 0) { |
| 499 | /* |
| 500 | * Not found or something else bad happened. |
| 501 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 502 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 503 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 504 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 505 | } |
| 506 | /* |
| 507 | * Do the delete. A fourth parameter means delete a property, |
| 508 | * otherwise delete the node. |
| 509 | */ |
| 510 | if (argc > 3) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 511 | err = fdt_delprop(working_fdt, nodeoffset, argv[3]); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 512 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 513 | printf("libfdt fdt_delprop(): %s\n", |
| 514 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 515 | return err; |
| 516 | } |
| 517 | } else { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 518 | err = fdt_del_node(working_fdt, nodeoffset); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 519 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 520 | printf("libfdt fdt_del_node(): %s\n", |
| 521 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 522 | return err; |
| 523 | } |
| 524 | } |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 525 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 526 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 527 | * Display header info |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 528 | */ |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 529 | } else if (argv[1][0] == 'h') { |
Heiko Schocher | 8244127 | 2018-11-15 06:06:06 +0100 | [diff] [blame] | 530 | if (argc == 5) |
| 531 | return fdt_get_header_value(argc, argv); |
| 532 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 533 | u32 version = fdt_version(working_fdt); |
| 534 | printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt)); |
| 535 | printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt), |
| 536 | fdt_totalsize(working_fdt)); |
| 537 | printf("off_dt_struct:\t\t0x%x\n", |
| 538 | fdt_off_dt_struct(working_fdt)); |
| 539 | printf("off_dt_strings:\t\t0x%x\n", |
| 540 | fdt_off_dt_strings(working_fdt)); |
| 541 | printf("off_mem_rsvmap:\t\t0x%x\n", |
| 542 | fdt_off_mem_rsvmap(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 543 | printf("version:\t\t%d\n", version); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 544 | printf("last_comp_version:\t%d\n", |
| 545 | fdt_last_comp_version(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 546 | if (version >= 2) |
| 547 | printf("boot_cpuid_phys:\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 548 | fdt_boot_cpuid_phys(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 549 | if (version >= 3) |
| 550 | printf("size_dt_strings:\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 551 | fdt_size_dt_strings(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 552 | if (version >= 17) |
| 553 | printf("size_dt_struct:\t\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 554 | fdt_size_dt_struct(working_fdt)); |
| 555 | printf("number mem_rsv:\t\t0x%x\n", |
| 556 | fdt_num_mem_rsv(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 557 | printf("\n"); |
| 558 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 559 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 560 | * Set boot cpu id |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 561 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 562 | } else if (strncmp(argv[1], "boo", 3) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 563 | unsigned long tmp = simple_strtoul(argv[2], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 564 | fdt_set_boot_cpuid_phys(working_fdt, tmp); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 565 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 566 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 567 | * memory command |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 568 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 569 | } else if (strncmp(argv[1], "me", 2) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 570 | uint64_t addr, size; |
| 571 | int err; |
Heiko Schocher | 4b142fe | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 572 | addr = simple_strtoull(argv[2], NULL, 16); |
| 573 | size = simple_strtoull(argv[3], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 574 | err = fdt_fixup_memory(working_fdt, addr, size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 575 | if (err < 0) |
| 576 | return err; |
| 577 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 578 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 579 | * mem reserve commands |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 580 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 581 | } else if (strncmp(argv[1], "rs", 2) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 582 | if (argv[2][0] == 'p') { |
| 583 | uint64_t addr, size; |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 584 | int total = fdt_num_mem_rsv(working_fdt); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 585 | int j, err; |
| 586 | printf("index\t\t start\t\t size\n"); |
| 587 | printf("-------------------------------" |
| 588 | "-----------------\n"); |
| 589 | for (j = 0; j < total; j++) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 590 | err = fdt_get_mem_rsv(working_fdt, j, &addr, &size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 591 | if (err < 0) { |
| 592 | printf("libfdt fdt_get_mem_rsv(): %s\n", |
| 593 | fdt_strerror(err)); |
| 594 | return err; |
| 595 | } |
| 596 | printf(" %x\t%08x%08x\t%08x%08x\n", j, |
| 597 | (u32)(addr >> 32), |
| 598 | (u32)(addr & 0xffffffff), |
| 599 | (u32)(size >> 32), |
| 600 | (u32)(size & 0xffffffff)); |
| 601 | } |
| 602 | } else if (argv[2][0] == 'a') { |
| 603 | uint64_t addr, size; |
| 604 | int err; |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 605 | addr = simple_strtoull(argv[3], NULL, 16); |
| 606 | size = simple_strtoull(argv[4], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 607 | err = fdt_add_mem_rsv(working_fdt, addr, size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 608 | |
| 609 | if (err < 0) { |
| 610 | printf("libfdt fdt_add_mem_rsv(): %s\n", |
| 611 | fdt_strerror(err)); |
| 612 | return err; |
| 613 | } |
| 614 | } else if (argv[2][0] == 'd') { |
| 615 | unsigned long idx = simple_strtoul(argv[3], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 616 | int err = fdt_del_mem_rsv(working_fdt, idx); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 617 | |
| 618 | if (err < 0) { |
| 619 | printf("libfdt fdt_del_mem_rsv(): %s\n", |
| 620 | fdt_strerror(err)); |
| 621 | return err; |
| 622 | } |
| 623 | } else { |
| 624 | /* Unrecognized command */ |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 625 | return CMD_RET_USAGE; |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 626 | } |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 627 | } |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 628 | #ifdef CONFIG_OF_BOARD_SETUP |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 629 | /* Call the board-specific fixup routine */ |
Simon Glass | 4ba98dc | 2014-10-23 18:58:48 -0600 | [diff] [blame] | 630 | else if (strncmp(argv[1], "boa", 3) == 0) { |
| 631 | int err = ft_board_setup(working_fdt, gd->bd); |
| 632 | |
| 633 | if (err) { |
| 634 | printf("Failed to update board information in FDT: %s\n", |
| 635 | fdt_strerror(err)); |
| 636 | return CMD_RET_FAILURE; |
| 637 | } |
Nicholas Faustini | 2c76d31 | 2018-10-03 12:58:48 +0200 | [diff] [blame] | 638 | #ifdef CONFIG_SOC_KEYSTONE |
| 639 | ft_board_setup_ex(working_fdt, gd->bd); |
| 640 | #endif |
Simon Glass | 4ba98dc | 2014-10-23 18:58:48 -0600 | [diff] [blame] | 641 | } |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 642 | #endif |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 643 | /* Create a chosen node */ |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 644 | else if (strncmp(argv[1], "cho", 3) == 0) { |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 645 | unsigned long initrd_start = 0, initrd_end = 0; |
| 646 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 647 | if ((argc != 2) && (argc != 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 648 | return CMD_RET_USAGE; |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 649 | |
| 650 | if (argc == 4) { |
| 651 | initrd_start = simple_strtoul(argv[2], NULL, 16); |
| 652 | initrd_end = simple_strtoul(argv[3], NULL, 16); |
| 653 | } |
| 654 | |
Masahiro Yamada | bc6ed0f | 2014-04-18 17:41:00 +0900 | [diff] [blame] | 655 | fdt_chosen(working_fdt); |
Masahiro Yamada | dbe963a | 2014-04-18 17:40:59 +0900 | [diff] [blame] | 656 | fdt_initrd(working_fdt, initrd_start, initrd_end); |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 657 | |
| 658 | #if defined(CONFIG_FIT_SIGNATURE) |
| 659 | } else if (strncmp(argv[1], "che", 3) == 0) { |
| 660 | int cfg_noffset; |
| 661 | int ret; |
| 662 | unsigned long addr; |
| 663 | struct fdt_header *blob; |
| 664 | |
| 665 | if (!working_fdt) |
| 666 | return CMD_RET_FAILURE; |
| 667 | |
| 668 | if (argc > 2) { |
| 669 | addr = simple_strtoul(argv[2], NULL, 16); |
| 670 | blob = map_sysmem(addr, 0); |
| 671 | } else { |
| 672 | blob = (struct fdt_header *)gd->fdt_blob; |
| 673 | } |
| 674 | if (!fdt_valid(&blob)) |
| 675 | return 1; |
| 676 | |
| 677 | gd->fdt_blob = blob; |
| 678 | cfg_noffset = fit_conf_get_node(working_fdt, NULL); |
| 679 | if (!cfg_noffset) { |
| 680 | printf("Could not find configuration node: %s\n", |
| 681 | fdt_strerror(cfg_noffset)); |
| 682 | return CMD_RET_FAILURE; |
| 683 | } |
| 684 | |
| 685 | ret = fit_config_verify(working_fdt, cfg_noffset); |
Simon Glass | 12df2ab | 2014-06-12 07:24:45 -0600 | [diff] [blame] | 686 | if (ret == 0) |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 687 | return CMD_RET_SUCCESS; |
| 688 | else |
| 689 | return CMD_RET_FAILURE; |
| 690 | #endif |
| 691 | |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 692 | } |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 693 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 694 | /* apply an overlay */ |
| 695 | else if (strncmp(argv[1], "ap", 2) == 0) { |
| 696 | unsigned long addr; |
| 697 | struct fdt_header *blob; |
Stefan Agner | 082b141 | 2016-12-20 15:58:45 +0100 | [diff] [blame] | 698 | int ret; |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 699 | |
| 700 | if (argc != 3) |
| 701 | return CMD_RET_USAGE; |
| 702 | |
| 703 | if (!working_fdt) |
| 704 | return CMD_RET_FAILURE; |
| 705 | |
| 706 | addr = simple_strtoul(argv[2], NULL, 16); |
| 707 | blob = map_sysmem(addr, 0); |
| 708 | if (!fdt_valid(&blob)) |
| 709 | return CMD_RET_FAILURE; |
| 710 | |
Pantelis Antoniou | 81ecc5d | 2017-09-04 23:12:12 +0300 | [diff] [blame] | 711 | /* apply method prints messages on error */ |
| 712 | ret = fdt_overlay_apply_verbose(working_fdt, blob); |
| 713 | if (ret) |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 714 | return CMD_RET_FAILURE; |
| 715 | } |
| 716 | #endif |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 717 | /* resize the fdt */ |
| 718 | else if (strncmp(argv[1], "re", 2) == 0) { |
Hannes Schmelzer | ef47683 | 2016-09-20 18:10:43 +0200 | [diff] [blame] | 719 | uint extrasize; |
| 720 | if (argc > 2) |
| 721 | extrasize = simple_strtoul(argv[2], NULL, 16); |
| 722 | else |
| 723 | extrasize = 0; |
| 724 | fdt_shrink_to_minimum(working_fdt, extrasize); |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 725 | } |
| 726 | else { |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 727 | /* Unrecognized command */ |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 728 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | return 0; |
| 732 | } |
| 733 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 734 | /****************************************************************************/ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 735 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 736 | /** |
| 737 | * fdt_valid() - Check if an FDT is valid. If not, change it to NULL |
| 738 | * |
| 739 | * @blobp: Pointer to FDT pointer |
| 740 | * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL) |
| 741 | */ |
| 742 | static int fdt_valid(struct fdt_header **blobp) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 743 | { |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 744 | const void *blob = *blobp; |
| 745 | int err; |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 746 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 747 | if (blob == NULL) { |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 748 | printf ("The address of the fdt is invalid (NULL).\n"); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 749 | return 0; |
| 750 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 751 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 752 | err = fdt_check_header(blob); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 753 | if (err == 0) |
| 754 | return 1; /* valid */ |
| 755 | |
| 756 | if (err < 0) { |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 757 | printf("libfdt fdt_check_header(): %s", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 758 | /* |
| 759 | * Be more informative on bad version. |
| 760 | */ |
| 761 | if (err == -FDT_ERR_BADVERSION) { |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 762 | if (fdt_version(blob) < |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 763 | FDT_FIRST_SUPPORTED_VERSION) { |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 764 | printf (" - too old, fdt %d < %d", |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 765 | fdt_version(blob), |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 766 | FDT_FIRST_SUPPORTED_VERSION); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 767 | } |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 768 | if (fdt_last_comp_version(blob) > |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 769 | FDT_LAST_SUPPORTED_VERSION) { |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 770 | printf (" - too new, fdt %d > %d", |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 771 | fdt_version(blob), |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 772 | FDT_LAST_SUPPORTED_VERSION); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 773 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 774 | } |
| 775 | printf("\n"); |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame] | 776 | *blobp = NULL; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | return 1; |
| 780 | } |
| 781 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 782 | /****************************************************************************/ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 783 | |
| 784 | /* |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 785 | * Parse the user's input, partially heuristic. Valid formats: |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 786 | * <0x00112233 4 05> - an array of cells. Numbers follow standard |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 787 | * C conventions. |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 788 | * [00 11 22 .. nn] - byte stream |
| 789 | * "string" - If the the value doesn't start with "<" or "[", it is |
| 790 | * treated as a string. Note that the quotes are |
| 791 | * stripped by the parser before we get the string. |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 792 | * newval: An array of strings containing the new property as specified |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 793 | * on the command line |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 794 | * count: The number of strings in the array |
| 795 | * data: A bytestream to be placed in the property |
| 796 | * len: The length of the resulting bytestream |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 797 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 798 | 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] | 799 | { |
| 800 | char *cp; /* temporary char pointer */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 801 | char *newp; /* temporary newval char pointer */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 802 | unsigned long tmp; /* holds converted values */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 803 | int stridx = 0; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 804 | |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 805 | *len = 0; |
| 806 | newp = newval[0]; |
| 807 | |
| 808 | /* An array of cells */ |
| 809 | if (*newp == '<') { |
| 810 | newp++; |
| 811 | while ((*newp != '>') && (stridx < count)) { |
| 812 | /* |
| 813 | * Keep searching until we find that last ">" |
| 814 | * That way users don't have to escape the spaces |
| 815 | */ |
| 816 | if (*newp == '\0') { |
| 817 | newp = newval[++stridx]; |
| 818 | continue; |
| 819 | } |
| 820 | |
| 821 | cp = newp; |
| 822 | tmp = simple_strtoul(cp, &newp, 0); |
Hannes Schmelzer | 9620d87 | 2017-05-30 15:05:44 +0200 | [diff] [blame] | 823 | if (*cp != '?') |
| 824 | *(fdt32_t *)data = cpu_to_fdt32(tmp); |
| 825 | else |
| 826 | newp++; |
| 827 | |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 828 | data += 4; |
| 829 | *len += 4; |
| 830 | |
| 831 | /* If the ptr didn't advance, something went wrong */ |
| 832 | if ((newp - cp) <= 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 833 | printf("Sorry, I could not convert \"%s\"\n", |
| 834 | cp); |
| 835 | return 1; |
| 836 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 837 | |
| 838 | while (*newp == ' ') |
| 839 | newp++; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 840 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 841 | |
| 842 | if (*newp != '>') { |
| 843 | printf("Unexpected character '%c'\n", *newp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 844 | return 1; |
| 845 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 846 | } else if (*newp == '[') { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 847 | /* |
| 848 | * Byte stream. Convert the values. |
| 849 | */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 850 | newp++; |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 851 | while ((stridx < count) && (*newp != ']')) { |
| 852 | while (*newp == ' ') |
| 853 | newp++; |
| 854 | if (*newp == '\0') { |
| 855 | newp = newval[++stridx]; |
| 856 | continue; |
| 857 | } |
| 858 | if (!isxdigit(*newp)) |
| 859 | break; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 860 | tmp = simple_strtoul(newp, &newp, 16); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 861 | *data++ = tmp & 0xFF; |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 862 | *len = *len + 1; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 863 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 864 | if (*newp != ']') { |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 865 | printf("Unexpected character '%c'\n", *newp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 866 | return 1; |
| 867 | } |
| 868 | } else { |
| 869 | /* |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 870 | * Assume it is one or more strings. Copy it into our |
| 871 | * data area for convenience (including the |
| 872 | * terminating '\0's). |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 873 | */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 874 | while (stridx < count) { |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 875 | size_t length = strlen(newp) + 1; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 876 | strcpy(data, newp); |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 877 | data += length; |
| 878 | *len += length; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 879 | newp = newval[++stridx]; |
| 880 | } |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 881 | } |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /****************************************************************************/ |
| 886 | |
| 887 | /* |
| 888 | * Heuristic to guess if this is a string or concatenated strings. |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 889 | */ |
| 890 | |
| 891 | static int is_printable_string(const void *data, int len) |
| 892 | { |
| 893 | const char *s = data; |
| 894 | |
| 895 | /* zero length is not */ |
| 896 | if (len == 0) |
| 897 | return 0; |
| 898 | |
Joe Hershberger | 8805bee | 2012-08-17 10:34:38 +0000 | [diff] [blame] | 899 | /* must terminate with zero or '\n' */ |
| 900 | if (s[len - 1] != '\0' && s[len - 1] != '\n') |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 901 | return 0; |
| 902 | |
| 903 | /* printable or a null byte (concatenated strings) */ |
Joe Hershberger | 8805bee | 2012-08-17 10:34:38 +0000 | [diff] [blame] | 904 | while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 905 | /* |
| 906 | * If we see a null, there are three possibilities: |
| 907 | * 1) If len == 1, it is the end of the string, printable |
| 908 | * 2) Next character also a null, not printable. |
| 909 | * 3) Next character not a null, continue to check. |
| 910 | */ |
| 911 | if (s[0] == '\0') { |
| 912 | if (len == 1) |
| 913 | return 1; |
| 914 | if (s[1] == '\0') |
| 915 | return 0; |
| 916 | } |
| 917 | s++; |
| 918 | len--; |
| 919 | } |
| 920 | |
| 921 | /* Not the null termination, or not done yet: not printable */ |
| 922 | if (*s != '\0' || (len != 0)) |
| 923 | return 0; |
| 924 | |
| 925 | return 1; |
| 926 | } |
| 927 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 928 | |
| 929 | /* |
| 930 | * Print the property in the best format, a heuristic guess. Print as |
| 931 | * a string, concatenated strings, a byte, word, double word, or (if all |
| 932 | * else fails) it is printed as a stream of bytes. |
| 933 | */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 934 | static void print_data(const void *data, int len) |
| 935 | { |
| 936 | int j; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 937 | |
| 938 | /* no data, don't print */ |
| 939 | if (len == 0) |
| 940 | return; |
| 941 | |
| 942 | /* |
| 943 | * It is a string, but it may have multiple strings (embedded '\0's). |
| 944 | */ |
| 945 | if (is_printable_string(data, len)) { |
| 946 | puts("\""); |
| 947 | j = 0; |
| 948 | while (j < len) { |
| 949 | if (j > 0) |
| 950 | puts("\", \""); |
| 951 | puts(data); |
| 952 | j += strlen(data) + 1; |
| 953 | data += strlen(data) + 1; |
| 954 | } |
| 955 | puts("\""); |
| 956 | return; |
| 957 | } |
| 958 | |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 959 | if ((len %4) == 0) { |
Simon Glass | 5d927b4 | 2017-05-17 03:25:19 -0600 | [diff] [blame] | 960 | if (len > CMD_FDT_MAX_DUMP) |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 961 | printf("* 0x%p [0x%08x]", data, len); |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 962 | else { |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 963 | const __be32 *p; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 964 | |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 965 | printf("<"); |
| 966 | for (j = 0, p = data; j < len/4; j++) |
| 967 | printf("0x%08x%s", fdt32_to_cpu(p[j]), |
| 968 | j < (len/4 - 1) ? " " : ""); |
| 969 | printf(">"); |
| 970 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 971 | } else { /* anything else... hexdump */ |
Simon Glass | 5d927b4 | 2017-05-17 03:25:19 -0600 | [diff] [blame] | 972 | if (len > CMD_FDT_MAX_DUMP) |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 973 | printf("* 0x%p [0x%08x]", data, len); |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 974 | else { |
| 975 | const u8 *s; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 976 | |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 977 | printf("["); |
| 978 | for (j = 0, s = data; j < len; j++) |
| 979 | printf("%02x%s", s[j], j < len - 1 ? " " : ""); |
| 980 | printf("]"); |
| 981 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 985 | /****************************************************************************/ |
| 986 | |
| 987 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 988 | * Recursively print (a portion of) the working_fdt. The depth parameter |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 989 | * determines how deeply nested the fdt is printed. |
| 990 | */ |
Kumar Gala | dbaf07c | 2007-11-21 14:07:46 -0600 | [diff] [blame] | 991 | static int fdt_print(const char *pathp, char *prop, int depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 992 | { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 993 | static char tabs[MAX_LEVEL+1] = |
| 994 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" |
| 995 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; |
Kumar Gala | dbaf07c | 2007-11-21 14:07:46 -0600 | [diff] [blame] | 996 | const void *nodep; /* property node pointer */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 997 | int nodeoffset; /* node offset from libfdt */ |
| 998 | int nextoffset; /* next node offset from libfdt */ |
| 999 | uint32_t tag; /* tag */ |
| 1000 | int len; /* length of the property */ |
| 1001 | int level = 0; /* keep track of nesting level */ |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1002 | const struct fdt_property *fdt_prop; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1003 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1004 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1005 | if (nodeoffset < 0) { |
| 1006 | /* |
| 1007 | * Not found or something else bad happened. |
| 1008 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 1009 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 1010 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1011 | return 1; |
| 1012 | } |
| 1013 | /* |
| 1014 | * The user passed in a property as well as node path. |
| 1015 | * Print only the given property and then return. |
| 1016 | */ |
| 1017 | if (prop) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1018 | nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1019 | if (len == 0) { |
| 1020 | /* no property value */ |
| 1021 | printf("%s %s\n", pathp, prop); |
| 1022 | return 0; |
Simon Glass | 9f95267 | 2017-06-07 10:28:42 -0600 | [diff] [blame] | 1023 | } else if (nodep && len > 0) { |
Gerald Van Baren | 28f384b | 2007-11-23 19:43:20 -0500 | [diff] [blame] | 1024 | printf("%s = ", prop); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1025 | print_data (nodep, len); |
| 1026 | printf("\n"); |
| 1027 | return 0; |
| 1028 | } else { |
| 1029 | printf ("libfdt fdt_getprop(): %s\n", |
| 1030 | fdt_strerror(len)); |
| 1031 | return 1; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * The user passed in a node path and no property, |
| 1037 | * print the node and all subnodes. |
| 1038 | */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1039 | while(level >= 0) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1040 | tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1041 | switch(tag) { |
| 1042 | case FDT_BEGIN_NODE: |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1043 | pathp = fdt_get_name(working_fdt, nodeoffset, NULL); |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1044 | if (level <= depth) { |
| 1045 | if (pathp == NULL) |
| 1046 | pathp = "/* NULL pointer error */"; |
| 1047 | if (*pathp == '\0') |
| 1048 | pathp = "/"; /* root is nameless */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1049 | printf("%s%s {\n", |
| 1050 | &tabs[MAX_LEVEL - level], pathp); |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1051 | } |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1052 | level++; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1053 | if (level >= MAX_LEVEL) { |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1054 | printf("Nested too deep, aborting.\n"); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1055 | return 1; |
| 1056 | } |
| 1057 | break; |
| 1058 | case FDT_END_NODE: |
| 1059 | level--; |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1060 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1061 | printf("%s};\n", &tabs[MAX_LEVEL - level]); |
| 1062 | if (level == 0) { |
| 1063 | level = -1; /* exit the loop */ |
| 1064 | } |
| 1065 | break; |
| 1066 | case FDT_PROP: |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1067 | fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset, |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1068 | sizeof(*fdt_prop)); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 1069 | pathp = fdt_string(working_fdt, |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1070 | fdt32_to_cpu(fdt_prop->nameoff)); |
| 1071 | len = fdt32_to_cpu(fdt_prop->len); |
| 1072 | nodep = fdt_prop->data; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1073 | if (len < 0) { |
| 1074 | printf ("libfdt fdt_getprop(): %s\n", |
| 1075 | fdt_strerror(len)); |
| 1076 | return 1; |
| 1077 | } else if (len == 0) { |
| 1078 | /* the property has no value */ |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1079 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1080 | printf("%s%s;\n", |
| 1081 | &tabs[MAX_LEVEL - level], |
| 1082 | pathp); |
| 1083 | } else { |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1084 | if (level <= depth) { |
Gerald Van Baren | 28f384b | 2007-11-23 19:43:20 -0500 | [diff] [blame] | 1085 | printf("%s%s = ", |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1086 | &tabs[MAX_LEVEL - level], |
| 1087 | pathp); |
| 1088 | print_data (nodep, len); |
| 1089 | printf(";\n"); |
| 1090 | } |
| 1091 | } |
| 1092 | break; |
| 1093 | case FDT_NOP: |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 1094 | printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1095 | break; |
| 1096 | case FDT_END: |
| 1097 | return 1; |
| 1098 | default: |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 1099 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 1100 | printf("Unknown tag 0x%08X\n", tag); |
| 1101 | return 1; |
| 1102 | } |
| 1103 | nodeoffset = nextoffset; |
| 1104 | } |
| 1105 | return 0; |
| 1106 | } |
| 1107 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1108 | /********************************************************************/ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1109 | #ifdef CONFIG_SYS_LONGHELP |
| 1110 | static char fdt_help_text[] = |
Simon Glass | 4b57865 | 2013-04-20 08:42:44 +0000 | [diff] [blame] | 1111 | "addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>\n" |
Maxime Ripard | e6628ad | 2016-07-05 10:26:45 +0200 | [diff] [blame] | 1112 | #ifdef CONFIG_OF_LIBFDT_OVERLAY |
| 1113 | "fdt apply <addr> - Apply overlay to the DT\n" |
| 1114 | #endif |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 1115 | #ifdef CONFIG_OF_BOARD_SETUP |
| 1116 | "fdt boardsetup - Do board-specific set up\n" |
| 1117 | #endif |
Simon Glass | c654b51 | 2014-10-23 18:58:54 -0600 | [diff] [blame] | 1118 | #ifdef CONFIG_OF_SYSTEM_SETUP |
| 1119 | "fdt systemsetup - Do system-specific set up\n" |
| 1120 | #endif |
Gerald Van Baren | 238cb7a | 2008-01-05 15:33:29 -0500 | [diff] [blame] | 1121 | "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] | 1122 | "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] | 1123 | "fdt print <path> [<prop>] - Recursive print starting at <path>\n" |
| 1124 | "fdt list <path> [<prop>] - Print one level starting at <path>\n" |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 1125 | "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n" |
| 1126 | "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n" |
| 1127 | "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n" |
| 1128 | "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n" |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1129 | "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n" |
| 1130 | "fdt mknode <path> <node> - Create a new node after <path>\n" |
| 1131 | "fdt rm <path> [<prop>] - Delete the node or <property>\n" |
Heiko Schocher | 8244127 | 2018-11-15 06:06:06 +0100 | [diff] [blame] | 1132 | "fdt header [get <var> <member>] - Display header info\n" |
| 1133 | " get - get header member <member> and store it in <var>\n" |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 1134 | "fdt bootcpu <id> - Set boot cpuid\n" |
| 1135 | "fdt memory <addr> <size> - Add/Update memory node\n" |
| 1136 | "fdt rsvmem print - Show current mem reserves\n" |
| 1137 | "fdt rsvmem add <addr> <size> - Add a mem reserve\n" |
| 1138 | "fdt rsvmem delete <index> - Delete a mem reserves\n" |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 1139 | "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n" |
| 1140 | " <start>/<end> - initrd start/end addr\n" |
Heiko Schocher | 097dd3e | 2014-03-03 12:19:24 +0100 | [diff] [blame] | 1141 | #if defined(CONFIG_FIT_SIGNATURE) |
| 1142 | "fdt checksign [<addr>] - check FIT signature\n" |
| 1143 | " <start> - addr of key blob\n" |
| 1144 | " default gd->fdt_blob\n" |
| 1145 | #endif |
Robert P. J. Day | 1cc0a9f | 2016-05-04 04:47:31 -0400 | [diff] [blame] | 1146 | "NOTE: Dereference aliases by omitting the leading '/', " |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 1147 | "e.g. fdt print ethernet0."; |
| 1148 | #endif |
| 1149 | |
| 1150 | U_BOOT_CMD( |
| 1151 | fdt, 255, 0, do_fdt, |
| 1152 | "flattened device tree utility commands", fdt_help_text |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1153 | ); |