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