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