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