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