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