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