Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2007 |
| 3 | * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com |
| 4 | * Based on code written by: |
| 5 | * Pantelis Antoniou <pantelis.antoniou@gmail.com> and |
| 6 | * Matthew McClintock <msm@freescale.com> |
| 7 | * |
| 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <command.h> |
| 29 | #include <linux/ctype.h> |
| 30 | #include <linux/types.h> |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 31 | #include <asm/global_data.h> |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 32 | #include <libfdt.h> |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 33 | #include <fdt_support.h> |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 34 | |
| 35 | #define MAX_LEVEL 32 /* how deeply nested we will go */ |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 36 | #define SCRATCHPAD 1024 /* bytes of scratchpad memory */ |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 37 | #ifndef CONFIG_CMD_FDT_MAX_DUMP |
| 38 | #define CONFIG_CMD_FDT_MAX_DUMP 64 |
| 39 | #endif |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * Global data (for the gd->bd) |
| 43 | */ |
| 44 | DECLARE_GLOBAL_DATA_PTR; |
| 45 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 46 | static int fdt_valid(struct fdt_header **blobp); |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 47 | 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] | 48 | static int fdt_print(const char *pathp, char *prop, int depth); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 49 | static int is_printable_string(const void *data, int len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 50 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 51 | /* |
Gerald Van Baren | ae9e97f | 2008-06-10 22:15:58 -0400 | [diff] [blame] | 52 | * The working_fdt points to our working flattened device tree. |
| 53 | */ |
| 54 | struct fdt_header *working_fdt; |
| 55 | |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 56 | void set_working_fdt_addr(void *addr) |
| 57 | { |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 58 | working_fdt = addr; |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 59 | setenv_addr("fdtaddr", addr); |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Gerald Van Baren | ae9e97f | 2008-06-10 22:15:58 -0400 | [diff] [blame] | 62 | /* |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 63 | * Get a value from the fdt and format it to be set in the environment |
| 64 | */ |
| 65 | static int fdt_value_setenv(const void *nodep, int len, const char *var) |
| 66 | { |
| 67 | if (is_printable_string(nodep, len)) |
| 68 | setenv(var, (void *)nodep); |
| 69 | else if (len == 4) { |
| 70 | char buf[11]; |
| 71 | |
| 72 | sprintf(buf, "0x%08X", *(uint32_t *)nodep); |
| 73 | setenv(var, buf); |
| 74 | } else if (len%4 == 0 && len <= 20) { |
| 75 | /* Needed to print things like sha1 hashes. */ |
| 76 | char buf[41]; |
| 77 | int i; |
| 78 | |
| 79 | for (i = 0; i < len; i += sizeof(unsigned int)) |
| 80 | sprintf(buf + (i * 2), "%08x", |
| 81 | *(unsigned int *)(nodep + i)); |
| 82 | setenv(var, buf); |
| 83 | } else { |
| 84 | printf("error: unprintable value\n"); |
| 85 | return 1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 91 | * Flattened Device Tree command, see the help for parameter definitions. |
| 92 | */ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 93 | 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] | 94 | { |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 95 | if (argc < 2) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 96 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 97 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 98 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 99 | * Set the address of the fdt |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 100 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 101 | if (argv[1][0] == 'a') { |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 102 | unsigned long addr; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 103 | /* |
| 104 | * Set the address [and length] of the fdt. |
| 105 | */ |
Kumar Gala | 7dbc38a | 2008-08-15 08:24:35 -0500 | [diff] [blame] | 106 | if (argc == 2) { |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 107 | if (!fdt_valid(&working_fdt)) |
Kumar Gala | 7dbc38a | 2008-08-15 08:24:35 -0500 | [diff] [blame] | 108 | return 1; |
Kumar Gala | 7dbc38a | 2008-08-15 08:24:35 -0500 | [diff] [blame] | 109 | printf("The address of the fdt is %p\n", working_fdt); |
| 110 | return 0; |
| 111 | } |
| 112 | |
Kumar Gala | 54f9c86 | 2008-08-15 08:24:39 -0500 | [diff] [blame] | 113 | addr = simple_strtoul(argv[2], NULL, 16); |
| 114 | set_working_fdt_addr((void *)addr); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 115 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 116 | if (!fdt_valid(&working_fdt)) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 117 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 118 | |
| 119 | if (argc >= 4) { |
| 120 | int len; |
| 121 | int err; |
| 122 | /* |
| 123 | * Optional new length |
| 124 | */ |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 125 | len = simple_strtoul(argv[3], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 126 | if (len < fdt_totalsize(working_fdt)) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 127 | printf ("New length %d < existing length %d, " |
| 128 | "ignoring.\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 129 | len, fdt_totalsize(working_fdt)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 130 | } else { |
| 131 | /* |
| 132 | * Open in place with a new length. |
| 133 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 134 | err = fdt_open_into(working_fdt, working_fdt, len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 135 | if (err != 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 136 | printf ("libfdt fdt_open_into(): %s\n", |
| 137 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Marek Vasut | e02c945 | 2012-09-05 08:34:44 +0200 | [diff] [blame] | 142 | return CMD_RET_SUCCESS; |
| 143 | } |
| 144 | |
| 145 | if (!working_fdt) { |
| 146 | puts( |
| 147 | "No FDT memory address configured. Please configure\n" |
| 148 | "the FDT address via \"fdt addr <address>\" command.\n" |
| 149 | "Aborting!\n"); |
| 150 | return CMD_RET_FAILURE; |
| 151 | } |
| 152 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 153 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 154 | * Move the working_fdt |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 155 | */ |
Marek Vasut | e02c945 | 2012-09-05 08:34:44 +0200 | [diff] [blame] | 156 | if (strncmp(argv[1], "mo", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 157 | struct fdt_header *newaddr; |
| 158 | int len; |
| 159 | int err; |
| 160 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 161 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 162 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 163 | |
| 164 | /* |
| 165 | * Set the address and length of the fdt. |
| 166 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 167 | working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16); |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 168 | if (!fdt_valid(&working_fdt)) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 169 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 170 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 171 | newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * If the user specifies a length, use that. Otherwise use the |
| 175 | * current length. |
| 176 | */ |
| 177 | if (argc <= 4) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 178 | len = fdt_totalsize(working_fdt); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 179 | } else { |
| 180 | len = simple_strtoul(argv[4], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 181 | if (len < fdt_totalsize(working_fdt)) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 182 | printf ("New length 0x%X < existing length " |
| 183 | "0x%X, aborting.\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 184 | len, fdt_totalsize(working_fdt)); |
Gerald Van Baren | 6be07cc | 2007-04-25 22:47:15 -0400 | [diff] [blame] | 185 | return 1; |
| 186 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Copy to the new location. |
| 191 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 192 | err = fdt_open_into(working_fdt, newaddr, len); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 193 | if (err != 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 194 | printf ("libfdt fdt_open_into(): %s\n", |
| 195 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 196 | return 1; |
| 197 | } |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 198 | working_fdt = newaddr; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 199 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 200 | /* |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 201 | * Make a new node |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 202 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 203 | } else if (strncmp(argv[1], "mk", 2) == 0) { |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 204 | char *pathp; /* path */ |
| 205 | char *nodep; /* new node to add */ |
| 206 | int nodeoffset; /* node offset from libfdt */ |
| 207 | int err; |
| 208 | |
| 209 | /* |
| 210 | * Parameters: Node path, new node to be appended to the path. |
| 211 | */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 212 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 213 | return CMD_RET_USAGE; |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 214 | |
| 215 | pathp = argv[2]; |
| 216 | nodep = argv[3]; |
| 217 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 218 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 219 | if (nodeoffset < 0) { |
| 220 | /* |
| 221 | * Not found or something else bad happened. |
| 222 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 223 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 224 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 225 | return 1; |
| 226 | } |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 227 | err = fdt_add_subnode(working_fdt, nodeoffset, nodep); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 228 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 229 | printf ("libfdt fdt_add_subnode(): %s\n", |
| 230 | fdt_strerror(err)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 231 | return 1; |
| 232 | } |
| 233 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 234 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 235 | * Set the value of a property in the working_fdt. |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 236 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 237 | } else if (argv[1][0] == 's') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 238 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 239 | char *prop; /* property */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 240 | int nodeoffset; /* node offset from libfdt */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 241 | static char data[SCRATCHPAD]; /* storage for the property */ |
| 242 | int len; /* new length of the property */ |
| 243 | int ret; /* return value */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 244 | |
| 245 | /* |
Gerald Van Baren | ea6d8be | 2008-01-05 14:52:04 -0500 | [diff] [blame] | 246 | * Parameters: Node path, property, optional value. |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 247 | */ |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 248 | if (argc < 4) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 249 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 250 | |
| 251 | pathp = argv[2]; |
| 252 | prop = argv[3]; |
Gerald Van Baren | ea6d8be | 2008-01-05 14:52:04 -0500 | [diff] [blame] | 253 | if (argc == 4) { |
| 254 | len = 0; |
| 255 | } else { |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 256 | ret = fdt_parse_prop(&argv[4], argc - 4, data, &len); |
Gerald Van Baren | ea6d8be | 2008-01-05 14:52:04 -0500 | [diff] [blame] | 257 | if (ret != 0) |
| 258 | return ret; |
| 259 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 260 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 261 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 262 | if (nodeoffset < 0) { |
| 263 | /* |
| 264 | * Not found or something else bad happened. |
| 265 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 266 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 267 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 268 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 269 | } |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 270 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 271 | ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 272 | if (ret < 0) { |
| 273 | printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret)); |
| 274 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 275 | } |
| 276 | |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 277 | /******************************************************************** |
| 278 | * Get the value of a property in the working_fdt. |
| 279 | ********************************************************************/ |
| 280 | } else if (argv[1][0] == 'g') { |
| 281 | char *subcmd; /* sub-command */ |
| 282 | char *pathp; /* path */ |
| 283 | char *prop; /* property */ |
| 284 | char *var; /* variable to store result */ |
| 285 | int nodeoffset; /* node offset from libfdt */ |
| 286 | const void *nodep; /* property node pointer */ |
| 287 | int len = 0; /* new length of the property */ |
| 288 | |
| 289 | /* |
| 290 | * Parameters: Node path, property, optional value. |
| 291 | */ |
| 292 | if (argc < 5) |
| 293 | return CMD_RET_USAGE; |
| 294 | |
| 295 | subcmd = argv[2]; |
| 296 | |
| 297 | if (argc < 6 && subcmd[0] != 's') |
| 298 | return CMD_RET_USAGE; |
| 299 | |
| 300 | var = argv[3]; |
| 301 | pathp = argv[4]; |
| 302 | prop = argv[5]; |
| 303 | |
| 304 | nodeoffset = fdt_path_offset(working_fdt, pathp); |
| 305 | if (nodeoffset < 0) { |
| 306 | /* |
| 307 | * Not found or something else bad happened. |
| 308 | */ |
| 309 | printf("libfdt fdt_path_offset() returned %s\n", |
| 310 | fdt_strerror(nodeoffset)); |
| 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) { |
| 315 | int reqIndex = -1; |
| 316 | int startDepth = fdt_node_depth( |
| 317 | working_fdt, nodeoffset); |
| 318 | int curDepth = startDepth; |
| 319 | int curIndex = -1; |
| 320 | int nextNodeOffset = fdt_next_node( |
| 321 | working_fdt, nodeoffset, &curDepth); |
| 322 | |
| 323 | if (subcmd[0] == 'n') |
| 324 | reqIndex = simple_strtoul(argv[5], NULL, 16); |
| 325 | |
| 326 | while (curDepth > startDepth) { |
| 327 | if (curDepth == startDepth + 1) |
| 328 | curIndex++; |
| 329 | if (subcmd[0] == 'n' && curIndex == reqIndex) { |
| 330 | const char *nodeName = fdt_get_name( |
| 331 | working_fdt, nextNodeOffset, NULL); |
| 332 | |
| 333 | setenv(var, (char *)nodeName); |
| 334 | return 0; |
| 335 | } |
| 336 | nextNodeOffset = fdt_next_node( |
| 337 | working_fdt, nextNodeOffset, &curDepth); |
| 338 | if (nextNodeOffset < 0) |
| 339 | break; |
| 340 | } |
| 341 | if (subcmd[0] == 's') { |
| 342 | /* get the num nodes at this level */ |
Simon Glass | bfc5996 | 2013-02-24 17:33:21 +0000 | [diff] [blame] | 343 | setenv_ulong(var, curIndex + 1); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 344 | } else { |
| 345 | /* node index not found */ |
| 346 | printf("libfdt node not found\n"); |
| 347 | return 1; |
| 348 | } |
| 349 | } else { |
| 350 | nodep = fdt_getprop( |
| 351 | working_fdt, nodeoffset, prop, &len); |
| 352 | if (len == 0) { |
| 353 | /* no property value */ |
| 354 | setenv(var, ""); |
| 355 | return 0; |
| 356 | } else if (len > 0) { |
| 357 | if (subcmd[0] == 'v') { |
| 358 | int ret; |
| 359 | |
| 360 | ret = fdt_value_setenv(nodep, len, var); |
| 361 | if (ret != 0) |
| 362 | return ret; |
| 363 | } else if (subcmd[0] == 'a') { |
| 364 | /* Get address */ |
| 365 | char buf[11]; |
| 366 | |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 367 | sprintf(buf, "0x%p", nodep); |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 368 | setenv(var, buf); |
| 369 | } else if (subcmd[0] == 's') { |
| 370 | /* Get size */ |
| 371 | char buf[11]; |
| 372 | |
| 373 | sprintf(buf, "0x%08X", len); |
| 374 | setenv(var, buf); |
| 375 | } else |
| 376 | return CMD_RET_USAGE; |
| 377 | return 0; |
| 378 | } else { |
| 379 | printf("libfdt fdt_getprop(): %s\n", |
| 380 | fdt_strerror(len)); |
| 381 | return 1; |
| 382 | } |
| 383 | } |
| 384 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 385 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 386 | * Print (recursive) / List (single level) |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 387 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 388 | } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 389 | int depth = MAX_LEVEL; /* how deep to print */ |
| 390 | char *pathp; /* path */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 391 | char *prop; /* property */ |
| 392 | int ret; /* return value */ |
Kumar Gala | f738b4a | 2007-10-25 16:15:07 -0500 | [diff] [blame] | 393 | static char root[2] = "/"; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 394 | |
| 395 | /* |
| 396 | * list is an alias for print, but limited to 1 level |
| 397 | */ |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 398 | if (argv[1][0] == 'l') { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 399 | depth = 1; |
| 400 | } |
| 401 | |
| 402 | /* |
| 403 | * Get the starting path. The root node is an oddball, |
| 404 | * the offset is zero and has no name. |
| 405 | */ |
Kumar Gala | f738b4a | 2007-10-25 16:15:07 -0500 | [diff] [blame] | 406 | if (argc == 2) |
| 407 | pathp = root; |
| 408 | else |
| 409 | pathp = argv[2]; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 410 | if (argc > 3) |
| 411 | prop = argv[3]; |
| 412 | else |
| 413 | prop = NULL; |
| 414 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 415 | ret = fdt_print(pathp, prop, depth); |
| 416 | if (ret != 0) |
| 417 | return ret; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 418 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 419 | /* |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 420 | * Remove a property/node |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 421 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 422 | } else if (strncmp(argv[1], "rm", 2) == 0) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 423 | int nodeoffset; /* node offset from libfdt */ |
| 424 | int err; |
| 425 | |
| 426 | /* |
| 427 | * Get the path. The root node is an oddball, the offset |
| 428 | * is zero and has no name. |
| 429 | */ |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 430 | nodeoffset = fdt_path_offset (working_fdt, argv[2]); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 431 | if (nodeoffset < 0) { |
| 432 | /* |
| 433 | * Not found or something else bad happened. |
| 434 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 435 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 436 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 437 | return 1; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 438 | } |
| 439 | /* |
| 440 | * Do the delete. A fourth parameter means delete a property, |
| 441 | * otherwise delete the node. |
| 442 | */ |
| 443 | if (argc > 3) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 444 | err = fdt_delprop(working_fdt, nodeoffset, argv[3]); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 445 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 446 | printf("libfdt fdt_delprop(): %s\n", |
| 447 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 448 | return err; |
| 449 | } |
| 450 | } else { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 451 | err = fdt_del_node(working_fdt, nodeoffset); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 452 | if (err < 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 453 | printf("libfdt fdt_del_node(): %s\n", |
| 454 | fdt_strerror(err)); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 455 | return err; |
| 456 | } |
| 457 | } |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 458 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 459 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 460 | * Display header info |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 461 | */ |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 462 | } else if (argv[1][0] == 'h') { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 463 | u32 version = fdt_version(working_fdt); |
| 464 | printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt)); |
| 465 | printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt), |
| 466 | fdt_totalsize(working_fdt)); |
| 467 | printf("off_dt_struct:\t\t0x%x\n", |
| 468 | fdt_off_dt_struct(working_fdt)); |
| 469 | printf("off_dt_strings:\t\t0x%x\n", |
| 470 | fdt_off_dt_strings(working_fdt)); |
| 471 | printf("off_mem_rsvmap:\t\t0x%x\n", |
| 472 | fdt_off_mem_rsvmap(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 473 | printf("version:\t\t%d\n", version); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 474 | printf("last_comp_version:\t%d\n", |
| 475 | fdt_last_comp_version(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 476 | if (version >= 2) |
| 477 | printf("boot_cpuid_phys:\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 478 | fdt_boot_cpuid_phys(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 479 | if (version >= 3) |
| 480 | printf("size_dt_strings:\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 481 | fdt_size_dt_strings(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 482 | if (version >= 17) |
| 483 | printf("size_dt_struct:\t\t0x%x\n", |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 484 | fdt_size_dt_struct(working_fdt)); |
| 485 | printf("number mem_rsv:\t\t0x%x\n", |
| 486 | fdt_num_mem_rsv(working_fdt)); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 487 | printf("\n"); |
| 488 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 489 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 490 | * Set boot cpu id |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 491 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 492 | } else if (strncmp(argv[1], "boo", 3) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 493 | unsigned long tmp = simple_strtoul(argv[2], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 494 | fdt_set_boot_cpuid_phys(working_fdt, tmp); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 495 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 496 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 497 | * memory command |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 498 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 499 | } else if (strncmp(argv[1], "me", 2) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 500 | uint64_t addr, size; |
| 501 | int err; |
Heiko Schocher | 4b142fe | 2009-12-03 11:21:21 +0100 | [diff] [blame] | 502 | addr = simple_strtoull(argv[2], NULL, 16); |
| 503 | size = simple_strtoull(argv[3], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 504 | err = fdt_fixup_memory(working_fdt, addr, size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 505 | if (err < 0) |
| 506 | return err; |
| 507 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 508 | /* |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 509 | * mem reserve commands |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 510 | */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 511 | } else if (strncmp(argv[1], "rs", 2) == 0) { |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 512 | if (argv[2][0] == 'p') { |
| 513 | uint64_t addr, size; |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 514 | int total = fdt_num_mem_rsv(working_fdt); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 515 | int j, err; |
| 516 | printf("index\t\t start\t\t size\n"); |
| 517 | printf("-------------------------------" |
| 518 | "-----------------\n"); |
| 519 | for (j = 0; j < total; j++) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 520 | err = fdt_get_mem_rsv(working_fdt, j, &addr, &size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 521 | if (err < 0) { |
| 522 | printf("libfdt fdt_get_mem_rsv(): %s\n", |
| 523 | fdt_strerror(err)); |
| 524 | return err; |
| 525 | } |
| 526 | printf(" %x\t%08x%08x\t%08x%08x\n", j, |
| 527 | (u32)(addr >> 32), |
| 528 | (u32)(addr & 0xffffffff), |
| 529 | (u32)(size >> 32), |
| 530 | (u32)(size & 0xffffffff)); |
| 531 | } |
| 532 | } else if (argv[2][0] == 'a') { |
| 533 | uint64_t addr, size; |
| 534 | int err; |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 535 | addr = simple_strtoull(argv[3], NULL, 16); |
| 536 | size = simple_strtoull(argv[4], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 537 | err = fdt_add_mem_rsv(working_fdt, addr, size); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 538 | |
| 539 | if (err < 0) { |
| 540 | printf("libfdt fdt_add_mem_rsv(): %s\n", |
| 541 | fdt_strerror(err)); |
| 542 | return err; |
| 543 | } |
| 544 | } else if (argv[2][0] == 'd') { |
| 545 | unsigned long idx = simple_strtoul(argv[3], NULL, 16); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 546 | int err = fdt_del_mem_rsv(working_fdt, idx); |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 547 | |
| 548 | if (err < 0) { |
| 549 | printf("libfdt fdt_del_mem_rsv(): %s\n", |
| 550 | fdt_strerror(err)); |
| 551 | return err; |
| 552 | } |
| 553 | } else { |
| 554 | /* Unrecognized command */ |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 555 | return CMD_RET_USAGE; |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 556 | } |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 557 | } |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 558 | #ifdef CONFIG_OF_BOARD_SETUP |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 559 | /* Call the board-specific fixup routine */ |
Gerald Van Baren | 2fb698b | 2008-06-09 21:02:17 -0400 | [diff] [blame] | 560 | else if (strncmp(argv[1], "boa", 3) == 0) |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 561 | ft_board_setup(working_fdt, gd->bd); |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 562 | #endif |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 563 | /* Create a chosen node */ |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 564 | else if (argv[1][0] == 'c') { |
| 565 | unsigned long initrd_start = 0, initrd_end = 0; |
| 566 | |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 567 | if ((argc != 2) && (argc != 4)) |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 568 | return CMD_RET_USAGE; |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 569 | |
| 570 | if (argc == 4) { |
| 571 | initrd_start = simple_strtoul(argv[2], NULL, 16); |
| 572 | initrd_end = simple_strtoul(argv[3], NULL, 16); |
| 573 | } |
| 574 | |
Heiko Schocher | 56844a2 | 2008-09-11 08:11:23 +0200 | [diff] [blame] | 575 | fdt_chosen(working_fdt, 1); |
| 576 | fdt_initrd(working_fdt, initrd_start, initrd_end, 1); |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 577 | } |
| 578 | /* resize the fdt */ |
| 579 | else if (strncmp(argv[1], "re", 2) == 0) { |
| 580 | fdt_resize(working_fdt); |
| 581 | } |
| 582 | else { |
Kim Phillips | 99dffca | 2007-07-17 13:57:04 -0500 | [diff] [blame] | 583 | /* Unrecognized command */ |
Simon Glass | 4c12eeb | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 584 | return CMD_RET_USAGE; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | return 0; |
| 588 | } |
| 589 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 590 | /****************************************************************************/ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 591 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 592 | /** |
| 593 | * fdt_valid() - Check if an FDT is valid. If not, change it to NULL |
| 594 | * |
| 595 | * @blobp: Pointer to FDT pointer |
| 596 | * @return 1 if OK, 0 if bad (in which case *blobp is set to NULL) |
| 597 | */ |
| 598 | static int fdt_valid(struct fdt_header **blobp) |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 599 | { |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 600 | const void *blob = *blobp; |
| 601 | int err; |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 602 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 603 | if (blob == NULL) { |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 604 | printf ("The address of the fdt is invalid (NULL).\n"); |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 605 | return 0; |
| 606 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 607 | |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 608 | err = fdt_check_header(blob); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 609 | if (err == 0) |
| 610 | return 1; /* valid */ |
| 611 | |
| 612 | if (err < 0) { |
Gerald Van Baren | 2511403 | 2007-05-12 09:47:25 -0400 | [diff] [blame] | 613 | printf("libfdt fdt_check_header(): %s", fdt_strerror(err)); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 614 | /* |
| 615 | * Be more informative on bad version. |
| 616 | */ |
| 617 | if (err == -FDT_ERR_BADVERSION) { |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 618 | if (fdt_version(blob) < |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 619 | FDT_FIRST_SUPPORTED_VERSION) { |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 620 | printf (" - too old, fdt %d < %d", |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 621 | fdt_version(blob), |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 622 | FDT_FIRST_SUPPORTED_VERSION); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 623 | } |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 624 | if (fdt_last_comp_version(blob) > |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 625 | FDT_LAST_SUPPORTED_VERSION) { |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 626 | printf (" - too new, fdt %d > %d", |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 627 | fdt_version(blob), |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 628 | FDT_LAST_SUPPORTED_VERSION); |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 629 | } |
Gerald Van Baren | 64dbbd4 | 2007-04-06 14:19:43 -0400 | [diff] [blame] | 630 | } |
| 631 | printf("\n"); |
Simon Glass | d14da91 | 2013-04-20 08:42:42 +0000 | [diff] [blame^] | 632 | *blobp = NULL; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 633 | return 0; |
| 634 | } |
| 635 | return 1; |
| 636 | } |
| 637 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 638 | /****************************************************************************/ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 639 | |
| 640 | /* |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 641 | * Parse the user's input, partially heuristic. Valid formats: |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 642 | * <0x00112233 4 05> - an array of cells. Numbers follow standard |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 643 | * C conventions. |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 644 | * [00 11 22 .. nn] - byte stream |
| 645 | * "string" - If the the value doesn't start with "<" or "[", it is |
| 646 | * treated as a string. Note that the quotes are |
| 647 | * stripped by the parser before we get the string. |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 648 | * newval: An array of strings containing the new property as specified |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 649 | * on the command line |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 650 | * count: The number of strings in the array |
| 651 | * data: A bytestream to be placed in the property |
| 652 | * len: The length of the resulting bytestream |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 653 | */ |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 654 | 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] | 655 | { |
| 656 | char *cp; /* temporary char pointer */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 657 | char *newp; /* temporary newval char pointer */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 658 | unsigned long tmp; /* holds converted values */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 659 | int stridx = 0; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 660 | |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 661 | *len = 0; |
| 662 | newp = newval[0]; |
| 663 | |
| 664 | /* An array of cells */ |
| 665 | if (*newp == '<') { |
| 666 | newp++; |
| 667 | while ((*newp != '>') && (stridx < count)) { |
| 668 | /* |
| 669 | * Keep searching until we find that last ">" |
| 670 | * That way users don't have to escape the spaces |
| 671 | */ |
| 672 | if (*newp == '\0') { |
| 673 | newp = newval[++stridx]; |
| 674 | continue; |
| 675 | } |
| 676 | |
| 677 | cp = newp; |
| 678 | tmp = simple_strtoul(cp, &newp, 0); |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 679 | *(__be32 *)data = __cpu_to_be32(tmp); |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 680 | data += 4; |
| 681 | *len += 4; |
| 682 | |
| 683 | /* If the ptr didn't advance, something went wrong */ |
| 684 | if ((newp - cp) <= 0) { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 685 | printf("Sorry, I could not convert \"%s\"\n", |
| 686 | cp); |
| 687 | return 1; |
| 688 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 689 | |
| 690 | while (*newp == ' ') |
| 691 | newp++; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 692 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 693 | |
| 694 | if (*newp != '>') { |
| 695 | printf("Unexpected character '%c'\n", *newp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 696 | return 1; |
| 697 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 698 | } else if (*newp == '[') { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 699 | /* |
| 700 | * Byte stream. Convert the values. |
| 701 | */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 702 | newp++; |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 703 | while ((stridx < count) && (*newp != ']')) { |
| 704 | while (*newp == ' ') |
| 705 | newp++; |
| 706 | if (*newp == '\0') { |
| 707 | newp = newval[++stridx]; |
| 708 | continue; |
| 709 | } |
| 710 | if (!isxdigit(*newp)) |
| 711 | break; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 712 | tmp = simple_strtoul(newp, &newp, 16); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 713 | *data++ = tmp & 0xFF; |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 714 | *len = *len + 1; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 715 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 716 | if (*newp != ']') { |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 717 | printf("Unexpected character '%c'\n", *newp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 718 | return 1; |
| 719 | } |
| 720 | } else { |
| 721 | /* |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 722 | * Assume it is one or more strings. Copy it into our |
| 723 | * data area for convenience (including the |
| 724 | * terminating '\0's). |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 725 | */ |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 726 | while (stridx < count) { |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 727 | size_t length = strlen(newp) + 1; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 728 | strcpy(data, newp); |
Ken MacLeod | 6e748ea | 2009-09-11 15:16:18 -0500 | [diff] [blame] | 729 | data += length; |
| 730 | *len += length; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 731 | newp = newval[++stridx]; |
| 732 | } |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 733 | } |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | /****************************************************************************/ |
| 738 | |
| 739 | /* |
| 740 | * Heuristic to guess if this is a string or concatenated strings. |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 741 | */ |
| 742 | |
| 743 | static int is_printable_string(const void *data, int len) |
| 744 | { |
| 745 | const char *s = data; |
| 746 | |
| 747 | /* zero length is not */ |
| 748 | if (len == 0) |
| 749 | return 0; |
| 750 | |
Joe Hershberger | 8805bee | 2012-08-17 10:34:38 +0000 | [diff] [blame] | 751 | /* must terminate with zero or '\n' */ |
| 752 | if (s[len - 1] != '\0' && s[len - 1] != '\n') |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 753 | return 0; |
| 754 | |
| 755 | /* printable or a null byte (concatenated strings) */ |
Joe Hershberger | 8805bee | 2012-08-17 10:34:38 +0000 | [diff] [blame] | 756 | while (((*s == '\0') || isprint(*s) || isspace(*s)) && (len > 0)) { |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 757 | /* |
| 758 | * If we see a null, there are three possibilities: |
| 759 | * 1) If len == 1, it is the end of the string, printable |
| 760 | * 2) Next character also a null, not printable. |
| 761 | * 3) Next character not a null, continue to check. |
| 762 | */ |
| 763 | if (s[0] == '\0') { |
| 764 | if (len == 1) |
| 765 | return 1; |
| 766 | if (s[1] == '\0') |
| 767 | return 0; |
| 768 | } |
| 769 | s++; |
| 770 | len--; |
| 771 | } |
| 772 | |
| 773 | /* Not the null termination, or not done yet: not printable */ |
| 774 | if (*s != '\0' || (len != 0)) |
| 775 | return 0; |
| 776 | |
| 777 | return 1; |
| 778 | } |
| 779 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 780 | |
| 781 | /* |
| 782 | * Print the property in the best format, a heuristic guess. Print as |
| 783 | * a string, concatenated strings, a byte, word, double word, or (if all |
| 784 | * else fails) it is printed as a stream of bytes. |
| 785 | */ |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 786 | static void print_data(const void *data, int len) |
| 787 | { |
| 788 | int j; |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 789 | |
| 790 | /* no data, don't print */ |
| 791 | if (len == 0) |
| 792 | return; |
| 793 | |
| 794 | /* |
| 795 | * It is a string, but it may have multiple strings (embedded '\0's). |
| 796 | */ |
| 797 | if (is_printable_string(data, len)) { |
| 798 | puts("\""); |
| 799 | j = 0; |
| 800 | while (j < len) { |
| 801 | if (j > 0) |
| 802 | puts("\", \""); |
| 803 | puts(data); |
| 804 | j += strlen(data) + 1; |
| 805 | data += strlen(data) + 1; |
| 806 | } |
| 807 | puts("\""); |
| 808 | return; |
| 809 | } |
| 810 | |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 811 | if ((len %4) == 0) { |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 812 | if (len > CONFIG_CMD_FDT_MAX_DUMP) |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 813 | printf("* 0x%p [0x%08x]", data, len); |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 814 | else { |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 815 | const __be32 *p; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 816 | |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 817 | printf("<"); |
| 818 | for (j = 0, p = data; j < len/4; j++) |
| 819 | printf("0x%08x%s", fdt32_to_cpu(p[j]), |
| 820 | j < (len/4 - 1) ? " " : ""); |
| 821 | printf(">"); |
| 822 | } |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 823 | } else { /* anything else... hexdump */ |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 824 | if (len > CONFIG_CMD_FDT_MAX_DUMP) |
Tom Rini | 085b9c3 | 2012-10-29 14:53:18 +0000 | [diff] [blame] | 825 | printf("* 0x%p [0x%08x]", data, len); |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 826 | else { |
| 827 | const u8 *s; |
Andy Fleming | 4abd844 | 2008-03-31 20:45:56 -0500 | [diff] [blame] | 828 | |
Joe Hershberger | f0a29d4 | 2012-08-17 10:34:36 +0000 | [diff] [blame] | 829 | printf("["); |
| 830 | for (j = 0, s = data; j < len; j++) |
| 831 | printf("%02x%s", s[j], j < len - 1 ? " " : ""); |
| 832 | printf("]"); |
| 833 | } |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 837 | /****************************************************************************/ |
| 838 | |
| 839 | /* |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 840 | * Recursively print (a portion of) the working_fdt. The depth parameter |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 841 | * determines how deeply nested the fdt is printed. |
| 842 | */ |
Kumar Gala | dbaf07c | 2007-11-21 14:07:46 -0600 | [diff] [blame] | 843 | static int fdt_print(const char *pathp, char *prop, int depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 844 | { |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 845 | static char tabs[MAX_LEVEL+1] = |
| 846 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" |
| 847 | "\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] | 848 | const void *nodep; /* property node pointer */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 849 | int nodeoffset; /* node offset from libfdt */ |
| 850 | int nextoffset; /* next node offset from libfdt */ |
| 851 | uint32_t tag; /* tag */ |
| 852 | int len; /* length of the property */ |
| 853 | int level = 0; /* keep track of nesting level */ |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 854 | const struct fdt_property *fdt_prop; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 855 | |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 856 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 857 | if (nodeoffset < 0) { |
| 858 | /* |
| 859 | * Not found or something else bad happened. |
| 860 | */ |
Kumar Gala | 8d04f02 | 2007-10-24 11:04:22 -0500 | [diff] [blame] | 861 | printf ("libfdt fdt_path_offset() returned %s\n", |
Gerald Van Baren | 06e19a0 | 2007-05-21 23:27:16 -0400 | [diff] [blame] | 862 | fdt_strerror(nodeoffset)); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 863 | return 1; |
| 864 | } |
| 865 | /* |
| 866 | * The user passed in a property as well as node path. |
| 867 | * Print only the given property and then return. |
| 868 | */ |
| 869 | if (prop) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 870 | nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 871 | if (len == 0) { |
| 872 | /* no property value */ |
| 873 | printf("%s %s\n", pathp, prop); |
| 874 | return 0; |
| 875 | } else if (len > 0) { |
Gerald Van Baren | 28f384b | 2007-11-23 19:43:20 -0500 | [diff] [blame] | 876 | printf("%s = ", prop); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 877 | print_data (nodep, len); |
| 878 | printf("\n"); |
| 879 | return 0; |
| 880 | } else { |
| 881 | printf ("libfdt fdt_getprop(): %s\n", |
| 882 | fdt_strerror(len)); |
| 883 | return 1; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /* |
| 888 | * The user passed in a node path and no property, |
| 889 | * print the node and all subnodes. |
| 890 | */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 891 | while(level >= 0) { |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 892 | tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 893 | switch(tag) { |
| 894 | case FDT_BEGIN_NODE: |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 895 | pathp = fdt_get_name(working_fdt, nodeoffset, NULL); |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 896 | if (level <= depth) { |
| 897 | if (pathp == NULL) |
| 898 | pathp = "/* NULL pointer error */"; |
| 899 | if (*pathp == '\0') |
| 900 | pathp = "/"; /* root is nameless */ |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 901 | printf("%s%s {\n", |
| 902 | &tabs[MAX_LEVEL - level], pathp); |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 903 | } |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 904 | level++; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 905 | if (level >= MAX_LEVEL) { |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 906 | printf("Nested too deep, aborting.\n"); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 907 | return 1; |
| 908 | } |
| 909 | break; |
| 910 | case FDT_END_NODE: |
| 911 | level--; |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 912 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 913 | printf("%s};\n", &tabs[MAX_LEVEL - level]); |
| 914 | if (level == 0) { |
| 915 | level = -1; /* exit the loop */ |
| 916 | } |
| 917 | break; |
| 918 | case FDT_PROP: |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 919 | fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset, |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 920 | sizeof(*fdt_prop)); |
Kim Phillips | e489b9c | 2008-06-10 11:06:17 -0500 | [diff] [blame] | 921 | pathp = fdt_string(working_fdt, |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 922 | fdt32_to_cpu(fdt_prop->nameoff)); |
| 923 | len = fdt32_to_cpu(fdt_prop->len); |
| 924 | nodep = fdt_prop->data; |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 925 | if (len < 0) { |
| 926 | printf ("libfdt fdt_getprop(): %s\n", |
| 927 | fdt_strerror(len)); |
| 928 | return 1; |
| 929 | } else if (len == 0) { |
| 930 | /* the property has no value */ |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 931 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 932 | printf("%s%s;\n", |
| 933 | &tabs[MAX_LEVEL - level], |
| 934 | pathp); |
| 935 | } else { |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 936 | if (level <= depth) { |
Gerald Van Baren | 28f384b | 2007-11-23 19:43:20 -0500 | [diff] [blame] | 937 | printf("%s%s = ", |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 938 | &tabs[MAX_LEVEL - level], |
| 939 | pathp); |
| 940 | print_data (nodep, len); |
| 941 | printf(";\n"); |
| 942 | } |
| 943 | } |
| 944 | break; |
| 945 | case FDT_NOP: |
Andrew Klossner | dc4b0b3 | 2008-07-07 06:41:14 -0700 | [diff] [blame] | 946 | printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]); |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 947 | break; |
| 948 | case FDT_END: |
| 949 | return 1; |
| 950 | default: |
Gerald Van Baren | 9162352 | 2007-11-22 17:23:23 -0500 | [diff] [blame] | 951 | if (level <= depth) |
Gerald Van Baren | addd8ce | 2007-05-16 22:39:59 -0400 | [diff] [blame] | 952 | printf("Unknown tag 0x%08X\n", tag); |
| 953 | return 1; |
| 954 | } |
| 955 | nodeoffset = nextoffset; |
| 956 | } |
| 957 | return 0; |
| 958 | } |
| 959 | |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 960 | /********************************************************************/ |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 961 | #ifdef CONFIG_SYS_LONGHELP |
| 962 | static char fdt_help_text[] = |
| 963 | "addr <addr> [<length>] - Set the fdt location to <addr>\n" |
Gerald Van Baren | fd61e55 | 2007-06-25 23:25:28 -0400 | [diff] [blame] | 964 | #ifdef CONFIG_OF_BOARD_SETUP |
| 965 | "fdt boardsetup - Do board-specific set up\n" |
| 966 | #endif |
Gerald Van Baren | 238cb7a | 2008-01-05 15:33:29 -0500 | [diff] [blame] | 967 | "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n" |
Kumar Gala | 40afac2 | 2008-08-15 08:24:44 -0500 | [diff] [blame] | 968 | "fdt resize - Resize fdt to size + padding to 4k addr\n" |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 969 | "fdt print <path> [<prop>] - Recursive print starting at <path>\n" |
| 970 | "fdt list <path> [<prop>] - Print one level starting at <path>\n" |
Joe Hershberger | bc80295 | 2012-08-17 10:34:37 +0000 | [diff] [blame] | 971 | "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n" |
| 972 | "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n" |
| 973 | "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n" |
| 974 | "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] | 975 | "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n" |
| 976 | "fdt mknode <path> <node> - Create a new node after <path>\n" |
| 977 | "fdt rm <path> [<prop>] - Delete the node or <property>\n" |
Kumar Gala | 804887e | 2008-02-15 03:34:36 -0600 | [diff] [blame] | 978 | "fdt header - Display header info\n" |
| 979 | "fdt bootcpu <id> - Set boot cpuid\n" |
| 980 | "fdt memory <addr> <size> - Add/Update memory node\n" |
| 981 | "fdt rsvmem print - Show current mem reserves\n" |
| 982 | "fdt rsvmem add <addr> <size> - Add a mem reserve\n" |
| 983 | "fdt rsvmem delete <index> - Delete a mem reserves\n" |
Kumar Gala | f953d99 | 2008-08-15 08:24:34 -0500 | [diff] [blame] | 984 | "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n" |
| 985 | " <start>/<end> - initrd start/end addr\n" |
Gerald Van Baren | 109c30f | 2008-08-22 14:37:05 -0400 | [diff] [blame] | 986 | "NOTE: Dereference aliases by omiting the leading '/', " |
Kim Phillips | 088f1b1 | 2012-10-29 13:34:31 +0000 | [diff] [blame] | 987 | "e.g. fdt print ethernet0."; |
| 988 | #endif |
| 989 | |
| 990 | U_BOOT_CMD( |
| 991 | fdt, 255, 0, do_fdt, |
| 992 | "flattened device tree utility commands", fdt_help_text |
Gerald Van Baren | 781e09e | 2007-03-31 12:22:10 -0400 | [diff] [blame] | 993 | ); |