]>
Commit | Line | Data |
---|---|---|
781e09ee GVB |
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> | |
781e09ee GVB |
31 | #include <asm/global_data.h> |
32 | #include <fdt.h> | |
33 | #include <libfdt.h> | |
64dbbd40 | 34 | #include <fdt_support.h> |
781e09ee GVB |
35 | |
36 | #define MAX_LEVEL 32 /* how deeply nested we will go */ | |
fd61e55d | 37 | #define SCRATCHPAD 1024 /* bytes of scratchpad memory */ |
f0a29d43 JH |
38 | #ifndef CONFIG_CMD_FDT_MAX_DUMP |
39 | #define CONFIG_CMD_FDT_MAX_DUMP 64 | |
40 | #endif | |
781e09ee GVB |
41 | |
42 | /* | |
43 | * Global data (for the gd->bd) | |
44 | */ | |
45 | DECLARE_GLOBAL_DATA_PTR; | |
46 | ||
781e09ee | 47 | static int fdt_valid(void); |
54841ab5 | 48 | static int fdt_parse_prop(char *const*newval, int count, char *data, int *len); |
dbaf07ce | 49 | static int fdt_print(const char *pathp, char *prop, int depth); |
bc80295b | 50 | static int is_printable_string(const void *data, int len); |
781e09ee | 51 | |
ae9e97fa GVB |
52 | /* |
53 | * The working_fdt points to our working flattened device tree. | |
54 | */ | |
55 | struct fdt_header *working_fdt; | |
56 | ||
54f9c866 KG |
57 | void set_working_fdt_addr(void *addr) |
58 | { | |
59 | char buf[17]; | |
60 | ||
61 | working_fdt = addr; | |
62 | ||
63 | sprintf(buf, "%lx", (unsigned long)addr); | |
64 | setenv("fdtaddr", buf); | |
65 | } | |
66 | ||
bc80295b JH |
67 | /* |
68 | * Get a value from the fdt and format it to be set in the environment | |
69 | */ | |
70 | static int fdt_value_setenv(const void *nodep, int len, const char *var) | |
71 | { | |
72 | if (is_printable_string(nodep, len)) | |
73 | setenv(var, (void *)nodep); | |
74 | else if (len == 4) { | |
75 | char buf[11]; | |
76 | ||
77 | sprintf(buf, "0x%08X", *(uint32_t *)nodep); | |
78 | setenv(var, buf); | |
79 | } else if (len%4 == 0 && len <= 20) { | |
80 | /* Needed to print things like sha1 hashes. */ | |
81 | char buf[41]; | |
82 | int i; | |
83 | ||
84 | for (i = 0; i < len; i += sizeof(unsigned int)) | |
85 | sprintf(buf + (i * 2), "%08x", | |
86 | *(unsigned int *)(nodep + i)); | |
87 | setenv(var, buf); | |
88 | } else { | |
89 | printf("error: unprintable value\n"); | |
90 | return 1; | |
91 | } | |
92 | return 0; | |
93 | } | |
94 | ||
781e09ee GVB |
95 | /* |
96 | * Flattened Device Tree command, see the help for parameter definitions. | |
97 | */ | |
54841ab5 | 98 | int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[]) |
781e09ee | 99 | { |
47e26b1b | 100 | if (argc < 2) |
4c12eeb8 | 101 | return CMD_RET_USAGE; |
781e09ee | 102 | |
47e26b1b | 103 | /* |
781e09ee | 104 | * Set the address of the fdt |
47e26b1b | 105 | */ |
25114033 | 106 | if (argv[1][0] == 'a') { |
54f9c866 | 107 | unsigned long addr; |
781e09ee GVB |
108 | /* |
109 | * Set the address [and length] of the fdt. | |
110 | */ | |
7dbc38ad KG |
111 | if (argc == 2) { |
112 | if (!fdt_valid()) { | |
113 | return 1; | |
114 | } | |
115 | printf("The address of the fdt is %p\n", working_fdt); | |
116 | return 0; | |
117 | } | |
118 | ||
54f9c866 KG |
119 | addr = simple_strtoul(argv[2], NULL, 16); |
120 | set_working_fdt_addr((void *)addr); | |
781e09ee GVB |
121 | |
122 | if (!fdt_valid()) { | |
123 | return 1; | |
124 | } | |
125 | ||
126 | if (argc >= 4) { | |
127 | int len; | |
128 | int err; | |
129 | /* | |
130 | * Optional new length | |
131 | */ | |
91623528 | 132 | len = simple_strtoul(argv[3], NULL, 16); |
e489b9c0 | 133 | if (len < fdt_totalsize(working_fdt)) { |
addd8ce8 GVB |
134 | printf ("New length %d < existing length %d, " |
135 | "ignoring.\n", | |
e489b9c0 | 136 | len, fdt_totalsize(working_fdt)); |
781e09ee GVB |
137 | } else { |
138 | /* | |
139 | * Open in place with a new length. | |
140 | */ | |
e489b9c0 | 141 | err = fdt_open_into(working_fdt, working_fdt, len); |
781e09ee | 142 | if (err != 0) { |
addd8ce8 GVB |
143 | printf ("libfdt fdt_open_into(): %s\n", |
144 | fdt_strerror(err)); | |
781e09ee GVB |
145 | } |
146 | } | |
147 | } | |
148 | ||
e02c9458 MV |
149 | return CMD_RET_SUCCESS; |
150 | } | |
151 | ||
152 | if (!working_fdt) { | |
153 | puts( | |
154 | "No FDT memory address configured. Please configure\n" | |
155 | "the FDT address via \"fdt addr <address>\" command.\n" | |
156 | "Aborting!\n"); | |
157 | return CMD_RET_FAILURE; | |
158 | } | |
159 | ||
47e26b1b | 160 | /* |
e489b9c0 | 161 | * Move the working_fdt |
47e26b1b | 162 | */ |
e02c9458 | 163 | if (strncmp(argv[1], "mo", 2) == 0) { |
781e09ee GVB |
164 | struct fdt_header *newaddr; |
165 | int len; | |
166 | int err; | |
167 | ||
47e26b1b | 168 | if (argc < 4) |
4c12eeb8 | 169 | return CMD_RET_USAGE; |
781e09ee GVB |
170 | |
171 | /* | |
172 | * Set the address and length of the fdt. | |
173 | */ | |
e489b9c0 | 174 | working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16); |
781e09ee GVB |
175 | if (!fdt_valid()) { |
176 | return 1; | |
177 | } | |
178 | ||
addd8ce8 | 179 | newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16); |
6be07cc1 GVB |
180 | |
181 | /* | |
182 | * If the user specifies a length, use that. Otherwise use the | |
183 | * current length. | |
184 | */ | |
185 | if (argc <= 4) { | |
e489b9c0 | 186 | len = fdt_totalsize(working_fdt); |
6be07cc1 GVB |
187 | } else { |
188 | len = simple_strtoul(argv[4], NULL, 16); | |
e489b9c0 | 189 | if (len < fdt_totalsize(working_fdt)) { |
addd8ce8 GVB |
190 | printf ("New length 0x%X < existing length " |
191 | "0x%X, aborting.\n", | |
e489b9c0 | 192 | len, fdt_totalsize(working_fdt)); |
6be07cc1 GVB |
193 | return 1; |
194 | } | |
781e09ee GVB |
195 | } |
196 | ||
197 | /* | |
198 | * Copy to the new location. | |
199 | */ | |
e489b9c0 | 200 | err = fdt_open_into(working_fdt, newaddr, len); |
781e09ee | 201 | if (err != 0) { |
addd8ce8 GVB |
202 | printf ("libfdt fdt_open_into(): %s\n", |
203 | fdt_strerror(err)); | |
781e09ee GVB |
204 | return 1; |
205 | } | |
e489b9c0 | 206 | working_fdt = newaddr; |
781e09ee | 207 | |
47e26b1b | 208 | /* |
25114033 | 209 | * Make a new node |
47e26b1b | 210 | */ |
2fb698bf | 211 | } else if (strncmp(argv[1], "mk", 2) == 0) { |
25114033 GVB |
212 | char *pathp; /* path */ |
213 | char *nodep; /* new node to add */ | |
214 | int nodeoffset; /* node offset from libfdt */ | |
215 | int err; | |
216 | ||
217 | /* | |
218 | * Parameters: Node path, new node to be appended to the path. | |
219 | */ | |
47e26b1b | 220 | if (argc < 4) |
4c12eeb8 | 221 | return CMD_RET_USAGE; |
25114033 GVB |
222 | |
223 | pathp = argv[2]; | |
224 | nodep = argv[3]; | |
225 | ||
e489b9c0 | 226 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
25114033 GVB |
227 | if (nodeoffset < 0) { |
228 | /* | |
229 | * Not found or something else bad happened. | |
230 | */ | |
8d04f02f | 231 | printf ("libfdt fdt_path_offset() returned %s\n", |
06e19a07 | 232 | fdt_strerror(nodeoffset)); |
25114033 GVB |
233 | return 1; |
234 | } | |
e489b9c0 | 235 | err = fdt_add_subnode(working_fdt, nodeoffset, nodep); |
25114033 | 236 | if (err < 0) { |
addd8ce8 GVB |
237 | printf ("libfdt fdt_add_subnode(): %s\n", |
238 | fdt_strerror(err)); | |
25114033 GVB |
239 | return 1; |
240 | } | |
241 | ||
47e26b1b | 242 | /* |
e489b9c0 | 243 | * Set the value of a property in the working_fdt. |
47e26b1b | 244 | */ |
25114033 | 245 | } else if (argv[1][0] == 's') { |
781e09ee | 246 | char *pathp; /* path */ |
addd8ce8 | 247 | char *prop; /* property */ |
781e09ee | 248 | int nodeoffset; /* node offset from libfdt */ |
addd8ce8 GVB |
249 | static char data[SCRATCHPAD]; /* storage for the property */ |
250 | int len; /* new length of the property */ | |
251 | int ret; /* return value */ | |
781e09ee GVB |
252 | |
253 | /* | |
ea6d8be1 | 254 | * Parameters: Node path, property, optional value. |
781e09ee | 255 | */ |
47e26b1b | 256 | if (argc < 4) |
4c12eeb8 | 257 | return CMD_RET_USAGE; |
781e09ee GVB |
258 | |
259 | pathp = argv[2]; | |
260 | prop = argv[3]; | |
ea6d8be1 GVB |
261 | if (argc == 4) { |
262 | len = 0; | |
263 | } else { | |
4abd844d | 264 | ret = fdt_parse_prop(&argv[4], argc - 4, data, &len); |
ea6d8be1 GVB |
265 | if (ret != 0) |
266 | return ret; | |
267 | } | |
781e09ee | 268 | |
e489b9c0 | 269 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
25114033 | 270 | if (nodeoffset < 0) { |
781e09ee | 271 | /* |
25114033 | 272 | * Not found or something else bad happened. |
781e09ee | 273 | */ |
8d04f02f | 274 | printf ("libfdt fdt_path_offset() returned %s\n", |
06e19a07 | 275 | fdt_strerror(nodeoffset)); |
781e09ee | 276 | return 1; |
25114033 | 277 | } |
25114033 | 278 | |
e489b9c0 | 279 | ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len); |
25114033 GVB |
280 | if (ret < 0) { |
281 | printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret)); | |
282 | return 1; | |
781e09ee GVB |
283 | } |
284 | ||
bc80295b JH |
285 | /******************************************************************** |
286 | * Get the value of a property in the working_fdt. | |
287 | ********************************************************************/ | |
288 | } else if (argv[1][0] == 'g') { | |
289 | char *subcmd; /* sub-command */ | |
290 | char *pathp; /* path */ | |
291 | char *prop; /* property */ | |
292 | char *var; /* variable to store result */ | |
293 | int nodeoffset; /* node offset from libfdt */ | |
294 | const void *nodep; /* property node pointer */ | |
295 | int len = 0; /* new length of the property */ | |
296 | ||
297 | /* | |
298 | * Parameters: Node path, property, optional value. | |
299 | */ | |
300 | if (argc < 5) | |
301 | return CMD_RET_USAGE; | |
302 | ||
303 | subcmd = argv[2]; | |
304 | ||
305 | if (argc < 6 && subcmd[0] != 's') | |
306 | return CMD_RET_USAGE; | |
307 | ||
308 | var = argv[3]; | |
309 | pathp = argv[4]; | |
310 | prop = argv[5]; | |
311 | ||
312 | nodeoffset = fdt_path_offset(working_fdt, pathp); | |
313 | if (nodeoffset < 0) { | |
314 | /* | |
315 | * Not found or something else bad happened. | |
316 | */ | |
317 | printf("libfdt fdt_path_offset() returned %s\n", | |
318 | fdt_strerror(nodeoffset)); | |
319 | return 1; | |
320 | } | |
321 | ||
322 | if (subcmd[0] == 'n' || (subcmd[0] == 's' && argc == 5)) { | |
323 | int reqIndex = -1; | |
324 | int startDepth = fdt_node_depth( | |
325 | working_fdt, nodeoffset); | |
326 | int curDepth = startDepth; | |
327 | int curIndex = -1; | |
328 | int nextNodeOffset = fdt_next_node( | |
329 | working_fdt, nodeoffset, &curDepth); | |
330 | ||
331 | if (subcmd[0] == 'n') | |
332 | reqIndex = simple_strtoul(argv[5], NULL, 16); | |
333 | ||
334 | while (curDepth > startDepth) { | |
335 | if (curDepth == startDepth + 1) | |
336 | curIndex++; | |
337 | if (subcmd[0] == 'n' && curIndex == reqIndex) { | |
338 | const char *nodeName = fdt_get_name( | |
339 | working_fdt, nextNodeOffset, NULL); | |
340 | ||
341 | setenv(var, (char *)nodeName); | |
342 | return 0; | |
343 | } | |
344 | nextNodeOffset = fdt_next_node( | |
345 | working_fdt, nextNodeOffset, &curDepth); | |
346 | if (nextNodeOffset < 0) | |
347 | break; | |
348 | } | |
349 | if (subcmd[0] == 's') { | |
350 | /* get the num nodes at this level */ | |
351 | char buf[11]; | |
352 | ||
353 | sprintf(buf, "%d", curIndex + 1); | |
354 | setenv(var, buf); | |
355 | } else { | |
356 | /* node index not found */ | |
357 | printf("libfdt node not found\n"); | |
358 | return 1; | |
359 | } | |
360 | } else { | |
361 | nodep = fdt_getprop( | |
362 | working_fdt, nodeoffset, prop, &len); | |
363 | if (len == 0) { | |
364 | /* no property value */ | |
365 | setenv(var, ""); | |
366 | return 0; | |
367 | } else if (len > 0) { | |
368 | if (subcmd[0] == 'v') { | |
369 | int ret; | |
370 | ||
371 | ret = fdt_value_setenv(nodep, len, var); | |
372 | if (ret != 0) | |
373 | return ret; | |
374 | } else if (subcmd[0] == 'a') { | |
375 | /* Get address */ | |
376 | char buf[11]; | |
377 | ||
378 | sprintf(buf, "0x%08X", (uint32_t)nodep); | |
379 | setenv(var, buf); | |
380 | } else if (subcmd[0] == 's') { | |
381 | /* Get size */ | |
382 | char buf[11]; | |
383 | ||
384 | sprintf(buf, "0x%08X", len); | |
385 | setenv(var, buf); | |
386 | } else | |
387 | return CMD_RET_USAGE; | |
388 | return 0; | |
389 | } else { | |
390 | printf("libfdt fdt_getprop(): %s\n", | |
391 | fdt_strerror(len)); | |
392 | return 1; | |
393 | } | |
394 | } | |
395 | ||
47e26b1b | 396 | /* |
781e09ee | 397 | * Print (recursive) / List (single level) |
47e26b1b | 398 | */ |
25114033 | 399 | } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) { |
781e09ee GVB |
400 | int depth = MAX_LEVEL; /* how deep to print */ |
401 | char *pathp; /* path */ | |
addd8ce8 GVB |
402 | char *prop; /* property */ |
403 | int ret; /* return value */ | |
f738b4a7 | 404 | static char root[2] = "/"; |
781e09ee GVB |
405 | |
406 | /* | |
407 | * list is an alias for print, but limited to 1 level | |
408 | */ | |
25114033 | 409 | if (argv[1][0] == 'l') { |
781e09ee GVB |
410 | depth = 1; |
411 | } | |
412 | ||
413 | /* | |
414 | * Get the starting path. The root node is an oddball, | |
415 | * the offset is zero and has no name. | |
416 | */ | |
f738b4a7 KG |
417 | if (argc == 2) |
418 | pathp = root; | |
419 | else | |
420 | pathp = argv[2]; | |
781e09ee GVB |
421 | if (argc > 3) |
422 | prop = argv[3]; | |
423 | else | |
424 | prop = NULL; | |
425 | ||
addd8ce8 GVB |
426 | ret = fdt_print(pathp, prop, depth); |
427 | if (ret != 0) | |
428 | return ret; | |
781e09ee | 429 | |
47e26b1b | 430 | /* |
781e09ee | 431 | * Remove a property/node |
47e26b1b | 432 | */ |
2fb698bf | 433 | } else if (strncmp(argv[1], "rm", 2) == 0) { |
781e09ee GVB |
434 | int nodeoffset; /* node offset from libfdt */ |
435 | int err; | |
436 | ||
437 | /* | |
438 | * Get the path. The root node is an oddball, the offset | |
439 | * is zero and has no name. | |
440 | */ | |
e489b9c0 | 441 | nodeoffset = fdt_path_offset (working_fdt, argv[2]); |
25114033 GVB |
442 | if (nodeoffset < 0) { |
443 | /* | |
444 | * Not found or something else bad happened. | |
445 | */ | |
8d04f02f | 446 | printf ("libfdt fdt_path_offset() returned %s\n", |
06e19a07 | 447 | fdt_strerror(nodeoffset)); |
25114033 | 448 | return 1; |
781e09ee GVB |
449 | } |
450 | /* | |
451 | * Do the delete. A fourth parameter means delete a property, | |
452 | * otherwise delete the node. | |
453 | */ | |
454 | if (argc > 3) { | |
e489b9c0 | 455 | err = fdt_delprop(working_fdt, nodeoffset, argv[3]); |
781e09ee | 456 | if (err < 0) { |
addd8ce8 GVB |
457 | printf("libfdt fdt_delprop(): %s\n", |
458 | fdt_strerror(err)); | |
781e09ee GVB |
459 | return err; |
460 | } | |
461 | } else { | |
e489b9c0 | 462 | err = fdt_del_node(working_fdt, nodeoffset); |
781e09ee | 463 | if (err < 0) { |
addd8ce8 GVB |
464 | printf("libfdt fdt_del_node(): %s\n", |
465 | fdt_strerror(err)); | |
781e09ee GVB |
466 | return err; |
467 | } | |
468 | } | |
804887e6 | 469 | |
47e26b1b | 470 | /* |
804887e6 | 471 | * Display header info |
47e26b1b | 472 | */ |
804887e6 | 473 | } else if (argv[1][0] == 'h') { |
e489b9c0 KP |
474 | u32 version = fdt_version(working_fdt); |
475 | printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt)); | |
476 | printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt), | |
477 | fdt_totalsize(working_fdt)); | |
478 | printf("off_dt_struct:\t\t0x%x\n", | |
479 | fdt_off_dt_struct(working_fdt)); | |
480 | printf("off_dt_strings:\t\t0x%x\n", | |
481 | fdt_off_dt_strings(working_fdt)); | |
482 | printf("off_mem_rsvmap:\t\t0x%x\n", | |
483 | fdt_off_mem_rsvmap(working_fdt)); | |
804887e6 | 484 | printf("version:\t\t%d\n", version); |
e489b9c0 KP |
485 | printf("last_comp_version:\t%d\n", |
486 | fdt_last_comp_version(working_fdt)); | |
804887e6 KG |
487 | if (version >= 2) |
488 | printf("boot_cpuid_phys:\t0x%x\n", | |
e489b9c0 | 489 | fdt_boot_cpuid_phys(working_fdt)); |
804887e6 KG |
490 | if (version >= 3) |
491 | printf("size_dt_strings:\t0x%x\n", | |
e489b9c0 | 492 | fdt_size_dt_strings(working_fdt)); |
804887e6 KG |
493 | if (version >= 17) |
494 | printf("size_dt_struct:\t\t0x%x\n", | |
e489b9c0 KP |
495 | fdt_size_dt_struct(working_fdt)); |
496 | printf("number mem_rsv:\t\t0x%x\n", | |
497 | fdt_num_mem_rsv(working_fdt)); | |
804887e6 KG |
498 | printf("\n"); |
499 | ||
47e26b1b | 500 | /* |
804887e6 | 501 | * Set boot cpu id |
47e26b1b | 502 | */ |
2fb698bf | 503 | } else if (strncmp(argv[1], "boo", 3) == 0) { |
804887e6 | 504 | unsigned long tmp = simple_strtoul(argv[2], NULL, 16); |
e489b9c0 | 505 | fdt_set_boot_cpuid_phys(working_fdt, tmp); |
804887e6 | 506 | |
47e26b1b | 507 | /* |
804887e6 | 508 | * memory command |
47e26b1b | 509 | */ |
2fb698bf | 510 | } else if (strncmp(argv[1], "me", 2) == 0) { |
804887e6 KG |
511 | uint64_t addr, size; |
512 | int err; | |
4b142feb HS |
513 | addr = simple_strtoull(argv[2], NULL, 16); |
514 | size = simple_strtoull(argv[3], NULL, 16); | |
e489b9c0 | 515 | err = fdt_fixup_memory(working_fdt, addr, size); |
804887e6 KG |
516 | if (err < 0) |
517 | return err; | |
518 | ||
47e26b1b | 519 | /* |
804887e6 | 520 | * mem reserve commands |
47e26b1b | 521 | */ |
2fb698bf | 522 | } else if (strncmp(argv[1], "rs", 2) == 0) { |
804887e6 KG |
523 | if (argv[2][0] == 'p') { |
524 | uint64_t addr, size; | |
e489b9c0 | 525 | int total = fdt_num_mem_rsv(working_fdt); |
804887e6 KG |
526 | int j, err; |
527 | printf("index\t\t start\t\t size\n"); | |
528 | printf("-------------------------------" | |
529 | "-----------------\n"); | |
530 | for (j = 0; j < total; j++) { | |
e489b9c0 | 531 | err = fdt_get_mem_rsv(working_fdt, j, &addr, &size); |
804887e6 KG |
532 | if (err < 0) { |
533 | printf("libfdt fdt_get_mem_rsv(): %s\n", | |
534 | fdt_strerror(err)); | |
535 | return err; | |
536 | } | |
537 | printf(" %x\t%08x%08x\t%08x%08x\n", j, | |
538 | (u32)(addr >> 32), | |
539 | (u32)(addr & 0xffffffff), | |
540 | (u32)(size >> 32), | |
541 | (u32)(size & 0xffffffff)); | |
542 | } | |
543 | } else if (argv[2][0] == 'a') { | |
544 | uint64_t addr, size; | |
545 | int err; | |
804887e6 KG |
546 | addr = simple_strtoull(argv[3], NULL, 16); |
547 | size = simple_strtoull(argv[4], NULL, 16); | |
e489b9c0 | 548 | err = fdt_add_mem_rsv(working_fdt, addr, size); |
804887e6 KG |
549 | |
550 | if (err < 0) { | |
551 | printf("libfdt fdt_add_mem_rsv(): %s\n", | |
552 | fdt_strerror(err)); | |
553 | return err; | |
554 | } | |
555 | } else if (argv[2][0] == 'd') { | |
556 | unsigned long idx = simple_strtoul(argv[3], NULL, 16); | |
e489b9c0 | 557 | int err = fdt_del_mem_rsv(working_fdt, idx); |
804887e6 KG |
558 | |
559 | if (err < 0) { | |
560 | printf("libfdt fdt_del_mem_rsv(): %s\n", | |
561 | fdt_strerror(err)); | |
562 | return err; | |
563 | } | |
564 | } else { | |
565 | /* Unrecognized command */ | |
4c12eeb8 | 566 | return CMD_RET_USAGE; |
804887e6 | 567 | } |
99dffca3 | 568 | } |
fd61e55d | 569 | #ifdef CONFIG_OF_BOARD_SETUP |
99dffca3 | 570 | /* Call the board-specific fixup routine */ |
2fb698bf | 571 | else if (strncmp(argv[1], "boa", 3) == 0) |
e489b9c0 | 572 | ft_board_setup(working_fdt, gd->bd); |
fd61e55d | 573 | #endif |
99dffca3 | 574 | /* Create a chosen node */ |
f953d99f KG |
575 | else if (argv[1][0] == 'c') { |
576 | unsigned long initrd_start = 0, initrd_end = 0; | |
577 | ||
47e26b1b | 578 | if ((argc != 2) && (argc != 4)) |
4c12eeb8 | 579 | return CMD_RET_USAGE; |
f953d99f KG |
580 | |
581 | if (argc == 4) { | |
582 | initrd_start = simple_strtoul(argv[2], NULL, 16); | |
583 | initrd_end = simple_strtoul(argv[3], NULL, 16); | |
584 | } | |
585 | ||
56844a22 HS |
586 | fdt_chosen(working_fdt, 1); |
587 | fdt_initrd(working_fdt, initrd_start, initrd_end, 1); | |
40afac22 KG |
588 | } |
589 | /* resize the fdt */ | |
590 | else if (strncmp(argv[1], "re", 2) == 0) { | |
591 | fdt_resize(working_fdt); | |
592 | } | |
593 | else { | |
99dffca3 | 594 | /* Unrecognized command */ |
4c12eeb8 | 595 | return CMD_RET_USAGE; |
781e09ee GVB |
596 | } |
597 | ||
598 | return 0; | |
599 | } | |
600 | ||
addd8ce8 | 601 | /****************************************************************************/ |
781e09ee GVB |
602 | |
603 | static int fdt_valid(void) | |
604 | { | |
64dbbd40 GVB |
605 | int err; |
606 | ||
e489b9c0 | 607 | if (working_fdt == NULL) { |
64dbbd40 | 608 | printf ("The address of the fdt is invalid (NULL).\n"); |
781e09ee GVB |
609 | return 0; |
610 | } | |
64dbbd40 | 611 | |
e489b9c0 | 612 | err = fdt_check_header(working_fdt); |
64dbbd40 GVB |
613 | if (err == 0) |
614 | return 1; /* valid */ | |
615 | ||
616 | if (err < 0) { | |
25114033 | 617 | printf("libfdt fdt_check_header(): %s", fdt_strerror(err)); |
64dbbd40 GVB |
618 | /* |
619 | * Be more informative on bad version. | |
620 | */ | |
621 | if (err == -FDT_ERR_BADVERSION) { | |
e489b9c0 KP |
622 | if (fdt_version(working_fdt) < |
623 | FDT_FIRST_SUPPORTED_VERSION) { | |
dc4b0b38 | 624 | printf (" - too old, fdt %d < %d", |
e489b9c0 | 625 | fdt_version(working_fdt), |
addd8ce8 | 626 | FDT_FIRST_SUPPORTED_VERSION); |
e489b9c0 | 627 | working_fdt = NULL; |
64dbbd40 | 628 | } |
e489b9c0 KP |
629 | if (fdt_last_comp_version(working_fdt) > |
630 | FDT_LAST_SUPPORTED_VERSION) { | |
dc4b0b38 | 631 | printf (" - too new, fdt %d > %d", |
e489b9c0 | 632 | fdt_version(working_fdt), |
addd8ce8 | 633 | FDT_LAST_SUPPORTED_VERSION); |
e489b9c0 | 634 | working_fdt = NULL; |
64dbbd40 GVB |
635 | } |
636 | return 0; | |
637 | } | |
638 | printf("\n"); | |
781e09ee GVB |
639 | return 0; |
640 | } | |
641 | return 1; | |
642 | } | |
643 | ||
addd8ce8 | 644 | /****************************************************************************/ |
781e09ee GVB |
645 | |
646 | /* | |
addd8ce8 | 647 | * Parse the user's input, partially heuristic. Valid formats: |
4abd844d | 648 | * <0x00112233 4 05> - an array of cells. Numbers follow standard |
53677ef1 | 649 | * C conventions. |
addd8ce8 GVB |
650 | * [00 11 22 .. nn] - byte stream |
651 | * "string" - If the the value doesn't start with "<" or "[", it is | |
652 | * treated as a string. Note that the quotes are | |
653 | * stripped by the parser before we get the string. | |
4abd844d | 654 | * newval: An array of strings containing the new property as specified |
53677ef1 | 655 | * on the command line |
4abd844d AF |
656 | * count: The number of strings in the array |
657 | * data: A bytestream to be placed in the property | |
658 | * len: The length of the resulting bytestream | |
addd8ce8 | 659 | */ |
54841ab5 | 660 | static int fdt_parse_prop(char * const *newval, int count, char *data, int *len) |
addd8ce8 GVB |
661 | { |
662 | char *cp; /* temporary char pointer */ | |
4abd844d | 663 | char *newp; /* temporary newval char pointer */ |
addd8ce8 | 664 | unsigned long tmp; /* holds converted values */ |
4abd844d | 665 | int stridx = 0; |
addd8ce8 | 666 | |
4abd844d AF |
667 | *len = 0; |
668 | newp = newval[0]; | |
669 | ||
670 | /* An array of cells */ | |
671 | if (*newp == '<') { | |
672 | newp++; | |
673 | while ((*newp != '>') && (stridx < count)) { | |
674 | /* | |
675 | * Keep searching until we find that last ">" | |
676 | * That way users don't have to escape the spaces | |
677 | */ | |
678 | if (*newp == '\0') { | |
679 | newp = newval[++stridx]; | |
680 | continue; | |
681 | } | |
682 | ||
683 | cp = newp; | |
684 | tmp = simple_strtoul(cp, &newp, 0); | |
685 | *(uint32_t *)data = __cpu_to_be32(tmp); | |
686 | data += 4; | |
687 | *len += 4; | |
688 | ||
689 | /* If the ptr didn't advance, something went wrong */ | |
690 | if ((newp - cp) <= 0) { | |
addd8ce8 GVB |
691 | printf("Sorry, I could not convert \"%s\"\n", |
692 | cp); | |
693 | return 1; | |
694 | } | |
4abd844d AF |
695 | |
696 | while (*newp == ' ') | |
697 | newp++; | |
addd8ce8 | 698 | } |
4abd844d AF |
699 | |
700 | if (*newp != '>') { | |
701 | printf("Unexpected character '%c'\n", *newp); | |
addd8ce8 GVB |
702 | return 1; |
703 | } | |
4abd844d | 704 | } else if (*newp == '[') { |
addd8ce8 GVB |
705 | /* |
706 | * Byte stream. Convert the values. | |
707 | */ | |
4abd844d | 708 | newp++; |
6e748ea0 | 709 | while ((stridx < count) && (*newp != ']')) { |
4abd844d AF |
710 | while (*newp == ' ') |
711 | newp++; | |
6e748ea0 | 712 | if (*newp == '\0') { |
4abd844d | 713 | newp = newval[++stridx]; |
6e748ea0 KM |
714 | continue; |
715 | } | |
716 | if (!isxdigit(*newp)) | |
717 | break; | |
718 | tmp = simple_strtoul(newp, &newp, 16); | |
719 | *data++ = tmp & 0xFF; | |
720 | *len = *len + 1; | |
addd8ce8 | 721 | } |
4abd844d | 722 | if (*newp != ']') { |
dc4b0b38 | 723 | printf("Unexpected character '%c'\n", *newp); |
addd8ce8 GVB |
724 | return 1; |
725 | } | |
726 | } else { | |
727 | /* | |
6e748ea0 KM |
728 | * Assume it is one or more strings. Copy it into our |
729 | * data area for convenience (including the | |
730 | * terminating '\0's). | |
addd8ce8 | 731 | */ |
4abd844d | 732 | while (stridx < count) { |
6e748ea0 | 733 | size_t length = strlen(newp) + 1; |
4abd844d | 734 | strcpy(data, newp); |
6e748ea0 KM |
735 | data += length; |
736 | *len += length; | |
4abd844d AF |
737 | newp = newval[++stridx]; |
738 | } | |
addd8ce8 GVB |
739 | } |
740 | return 0; | |
741 | } | |
742 | ||
743 | /****************************************************************************/ | |
744 | ||
745 | /* | |
746 | * Heuristic to guess if this is a string or concatenated strings. | |
781e09ee GVB |
747 | */ |
748 | ||
749 | static int is_printable_string(const void *data, int len) | |
750 | { | |
751 | const char *s = data; | |
752 | ||
753 | /* zero length is not */ | |
754 | if (len == 0) | |
755 | return 0; | |
756 | ||
757 | /* must terminate with zero */ | |
758 | if (s[len - 1] != '\0') | |
759 | return 0; | |
760 | ||
761 | /* printable or a null byte (concatenated strings) */ | |
762 | while (((*s == '\0') || isprint(*s)) && (len > 0)) { | |
763 | /* | |
764 | * If we see a null, there are three possibilities: | |
765 | * 1) If len == 1, it is the end of the string, printable | |
766 | * 2) Next character also a null, not printable. | |
767 | * 3) Next character not a null, continue to check. | |
768 | */ | |
769 | if (s[0] == '\0') { | |
770 | if (len == 1) | |
771 | return 1; | |
772 | if (s[1] == '\0') | |
773 | return 0; | |
774 | } | |
775 | s++; | |
776 | len--; | |
777 | } | |
778 | ||
779 | /* Not the null termination, or not done yet: not printable */ | |
780 | if (*s != '\0' || (len != 0)) | |
781 | return 0; | |
782 | ||
783 | return 1; | |
784 | } | |
785 | ||
addd8ce8 GVB |
786 | |
787 | /* | |
788 | * Print the property in the best format, a heuristic guess. Print as | |
789 | * a string, concatenated strings, a byte, word, double word, or (if all | |
790 | * else fails) it is printed as a stream of bytes. | |
791 | */ | |
781e09ee GVB |
792 | static void print_data(const void *data, int len) |
793 | { | |
794 | int j; | |
781e09ee GVB |
795 | |
796 | /* no data, don't print */ | |
797 | if (len == 0) | |
798 | return; | |
799 | ||
800 | /* | |
801 | * It is a string, but it may have multiple strings (embedded '\0's). | |
802 | */ | |
803 | if (is_printable_string(data, len)) { | |
804 | puts("\""); | |
805 | j = 0; | |
806 | while (j < len) { | |
807 | if (j > 0) | |
808 | puts("\", \""); | |
809 | puts(data); | |
810 | j += strlen(data) + 1; | |
811 | data += strlen(data) + 1; | |
812 | } | |
813 | puts("\""); | |
814 | return; | |
815 | } | |
816 | ||
4abd844d | 817 | if ((len %4) == 0) { |
f0a29d43 JH |
818 | if (len > CONFIG_CMD_FDT_MAX_DUMP) |
819 | printf("* 0x%08x [0x%08x]", (unsigned int)data, len); | |
820 | else { | |
821 | const u32 *p; | |
822 | ||
823 | printf("<"); | |
824 | for (j = 0, p = data; j < len/4; j++) | |
825 | printf("0x%08x%s", fdt32_to_cpu(p[j]), | |
826 | j < (len/4 - 1) ? " " : ""); | |
827 | printf(">"); | |
828 | } | |
4abd844d | 829 | } else { /* anything else... hexdump */ |
f0a29d43 JH |
830 | if (len > CONFIG_CMD_FDT_MAX_DUMP) |
831 | printf("* 0x%08x [0x%08x]", (unsigned int)data, len); | |
832 | else { | |
833 | const u8 *s; | |
834 | ||
835 | printf("["); | |
836 | for (j = 0, s = data; j < len; j++) | |
837 | printf("%02x%s", s[j], j < len - 1 ? " " : ""); | |
838 | printf("]"); | |
839 | } | |
781e09ee GVB |
840 | } |
841 | } | |
842 | ||
addd8ce8 GVB |
843 | /****************************************************************************/ |
844 | ||
845 | /* | |
e489b9c0 | 846 | * Recursively print (a portion of) the working_fdt. The depth parameter |
addd8ce8 GVB |
847 | * determines how deeply nested the fdt is printed. |
848 | */ | |
dbaf07ce | 849 | static int fdt_print(const char *pathp, char *prop, int depth) |
addd8ce8 | 850 | { |
addd8ce8 GVB |
851 | static char tabs[MAX_LEVEL+1] = |
852 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" | |
853 | "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"; | |
dbaf07ce | 854 | const void *nodep; /* property node pointer */ |
addd8ce8 GVB |
855 | int nodeoffset; /* node offset from libfdt */ |
856 | int nextoffset; /* next node offset from libfdt */ | |
857 | uint32_t tag; /* tag */ | |
858 | int len; /* length of the property */ | |
859 | int level = 0; /* keep track of nesting level */ | |
91623528 | 860 | const struct fdt_property *fdt_prop; |
addd8ce8 | 861 | |
e489b9c0 | 862 | nodeoffset = fdt_path_offset (working_fdt, pathp); |
addd8ce8 GVB |
863 | if (nodeoffset < 0) { |
864 | /* | |
865 | * Not found or something else bad happened. | |
866 | */ | |
8d04f02f | 867 | printf ("libfdt fdt_path_offset() returned %s\n", |
06e19a07 | 868 | fdt_strerror(nodeoffset)); |
addd8ce8 GVB |
869 | return 1; |
870 | } | |
871 | /* | |
872 | * The user passed in a property as well as node path. | |
873 | * Print only the given property and then return. | |
874 | */ | |
875 | if (prop) { | |
e489b9c0 | 876 | nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len); |
addd8ce8 GVB |
877 | if (len == 0) { |
878 | /* no property value */ | |
879 | printf("%s %s\n", pathp, prop); | |
880 | return 0; | |
881 | } else if (len > 0) { | |
28f384b1 | 882 | printf("%s = ", prop); |
addd8ce8 GVB |
883 | print_data (nodep, len); |
884 | printf("\n"); | |
885 | return 0; | |
886 | } else { | |
887 | printf ("libfdt fdt_getprop(): %s\n", | |
888 | fdt_strerror(len)); | |
889 | return 1; | |
890 | } | |
891 | } | |
892 | ||
893 | /* | |
894 | * The user passed in a node path and no property, | |
895 | * print the node and all subnodes. | |
896 | */ | |
addd8ce8 | 897 | while(level >= 0) { |
e489b9c0 | 898 | tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset); |
addd8ce8 GVB |
899 | switch(tag) { |
900 | case FDT_BEGIN_NODE: | |
e489b9c0 | 901 | pathp = fdt_get_name(working_fdt, nodeoffset, NULL); |
91623528 GVB |
902 | if (level <= depth) { |
903 | if (pathp == NULL) | |
904 | pathp = "/* NULL pointer error */"; | |
905 | if (*pathp == '\0') | |
906 | pathp = "/"; /* root is nameless */ | |
addd8ce8 GVB |
907 | printf("%s%s {\n", |
908 | &tabs[MAX_LEVEL - level], pathp); | |
91623528 | 909 | } |
addd8ce8 | 910 | level++; |
addd8ce8 | 911 | if (level >= MAX_LEVEL) { |
91623528 | 912 | printf("Nested too deep, aborting.\n"); |
addd8ce8 GVB |
913 | return 1; |
914 | } | |
915 | break; | |
916 | case FDT_END_NODE: | |
917 | level--; | |
91623528 | 918 | if (level <= depth) |
addd8ce8 GVB |
919 | printf("%s};\n", &tabs[MAX_LEVEL - level]); |
920 | if (level == 0) { | |
921 | level = -1; /* exit the loop */ | |
922 | } | |
923 | break; | |
924 | case FDT_PROP: | |
e489b9c0 | 925 | fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset, |
91623528 | 926 | sizeof(*fdt_prop)); |
e489b9c0 | 927 | pathp = fdt_string(working_fdt, |
91623528 GVB |
928 | fdt32_to_cpu(fdt_prop->nameoff)); |
929 | len = fdt32_to_cpu(fdt_prop->len); | |
930 | nodep = fdt_prop->data; | |
addd8ce8 GVB |
931 | if (len < 0) { |
932 | printf ("libfdt fdt_getprop(): %s\n", | |
933 | fdt_strerror(len)); | |
934 | return 1; | |
935 | } else if (len == 0) { | |
936 | /* the property has no value */ | |
91623528 | 937 | if (level <= depth) |
addd8ce8 GVB |
938 | printf("%s%s;\n", |
939 | &tabs[MAX_LEVEL - level], | |
940 | pathp); | |
941 | } else { | |
91623528 | 942 | if (level <= depth) { |
28f384b1 | 943 | printf("%s%s = ", |
addd8ce8 GVB |
944 | &tabs[MAX_LEVEL - level], |
945 | pathp); | |
946 | print_data (nodep, len); | |
947 | printf(";\n"); | |
948 | } | |
949 | } | |
950 | break; | |
951 | case FDT_NOP: | |
dc4b0b38 | 952 | printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]); |
addd8ce8 GVB |
953 | break; |
954 | case FDT_END: | |
955 | return 1; | |
956 | default: | |
91623528 | 957 | if (level <= depth) |
addd8ce8 GVB |
958 | printf("Unknown tag 0x%08X\n", tag); |
959 | return 1; | |
960 | } | |
961 | nodeoffset = nextoffset; | |
962 | } | |
963 | return 0; | |
964 | } | |
965 | ||
781e09ee GVB |
966 | /********************************************************************/ |
967 | ||
781e09ee | 968 | U_BOOT_CMD( |
4abd844d | 969 | fdt, 255, 0, do_fdt, |
2fb2604d | 970 | "flattened device tree utility commands", |
781e09ee | 971 | "addr <addr> [<length>] - Set the fdt location to <addr>\n" |
fd61e55d GVB |
972 | #ifdef CONFIG_OF_BOARD_SETUP |
973 | "fdt boardsetup - Do board-specific set up\n" | |
974 | #endif | |
238cb7a4 | 975 | "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n" |
40afac22 | 976 | "fdt resize - Resize fdt to size + padding to 4k addr\n" |
781e09ee GVB |
977 | "fdt print <path> [<prop>] - Recursive print starting at <path>\n" |
978 | "fdt list <path> [<prop>] - Print one level starting at <path>\n" | |
bc80295b JH |
979 | "fdt get value <var> <path> <prop> - Get <property> and store in <var>\n" |
980 | "fdt get name <var> <path> <index> - Get name of node <index> and store in <var>\n" | |
981 | "fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>\n" | |
982 | "fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>\n" | |
781e09ee GVB |
983 | "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n" |
984 | "fdt mknode <path> <node> - Create a new node after <path>\n" | |
985 | "fdt rm <path> [<prop>] - Delete the node or <property>\n" | |
804887e6 KG |
986 | "fdt header - Display header info\n" |
987 | "fdt bootcpu <id> - Set boot cpuid\n" | |
988 | "fdt memory <addr> <size> - Add/Update memory node\n" | |
989 | "fdt rsvmem print - Show current mem reserves\n" | |
990 | "fdt rsvmem add <addr> <size> - Add a mem reserve\n" | |
991 | "fdt rsvmem delete <index> - Delete a mem reserves\n" | |
f953d99f KG |
992 | "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n" |
993 | " <start>/<end> - initrd start/end addr\n" | |
109c30fb | 994 | "NOTE: Dereference aliases by omiting the leading '/', " |
a89c33db | 995 | "e.g. fdt print ethernet0." |
781e09ee | 996 | ); |