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