blob: c31560bd65e15411eab50c60e83c3d9b658445f4 [file] [log] [blame]
Gerald Van Baren781e09e2007-03-31 12:22:10 -04001/*
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 Baren781e09e2007-03-31 12:22:10 -040031#include <asm/global_data.h>
32#include <fdt.h>
33#include <libfdt.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040034#include <fdt_support.h>
Gerald Van Baren781e09e2007-03-31 12:22:10 -040035
36#define MAX_LEVEL 32 /* how deeply nested we will go */
Gerald Van Barenfd61e552007-06-25 23:25:28 -040037#define SCRATCHPAD 1024 /* bytes of scratchpad memory */
Gerald Van Baren781e09e2007-03-31 12:22:10 -040038
39/*
40 * Global data (for the gd->bd)
41 */
42DECLARE_GLOBAL_DATA_PTR;
43
Gerald Van Baren781e09e2007-03-31 12:22:10 -040044static int fdt_valid(void);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -040045static int fdt_parse_prop(char *pathp, char *prop, char *newval,
46 char *data, int *len);
Kumar Galadbaf07c2007-11-21 14:07:46 -060047static int fdt_print(const char *pathp, char *prop, int depth);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040048
Gerald Van Baren781e09e2007-03-31 12:22:10 -040049/*
50 * Flattened Device Tree command, see the help for parameter definitions.
51 */
52int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
53{
Gerald Van Baren781e09e2007-03-31 12:22:10 -040054 if (argc < 2) {
55 printf ("Usage:\n%s\n", cmdtp->usage);
56 return 1;
57 }
58
Gerald Van Baren781e09e2007-03-31 12:22:10 -040059 /********************************************************************
60 * Set the address of the fdt
61 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -040062 if (argv[1][0] == 'a') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -040063 /*
64 * Set the address [and length] of the fdt.
65 */
66 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
67
68 if (!fdt_valid()) {
69 return 1;
70 }
71
72 if (argc >= 4) {
73 int len;
74 int err;
75 /*
76 * Optional new length
77 */
Gerald Van Baren91623522007-11-22 17:23:23 -050078 len = simple_strtoul(argv[3], NULL, 16);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040079 if (len < fdt_totalsize(fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -040080 printf ("New length %d < existing length %d, "
81 "ignoring.\n",
Gerald Van Baren781e09e2007-03-31 12:22:10 -040082 len, fdt_totalsize(fdt));
83 } else {
84 /*
85 * Open in place with a new length.
86 */
87 err = fdt_open_into(fdt, fdt, len);
88 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -040089 printf ("libfdt fdt_open_into(): %s\n",
90 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -040091 }
92 }
93 }
94
95 /********************************************************************
96 * Move the fdt
97 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -040098 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -040099 struct fdt_header *newaddr;
100 int len;
101 int err;
102
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400103 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400104 printf ("Usage:\n%s\n", cmdtp->usage);
105 return 1;
106 }
107
108 /*
109 * Set the address and length of the fdt.
110 */
111 fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
112 if (!fdt_valid()) {
113 return 1;
114 }
115
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400116 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400117
118 /*
119 * If the user specifies a length, use that. Otherwise use the
120 * current length.
121 */
122 if (argc <= 4) {
123 len = fdt_totalsize(fdt);
124 } else {
125 len = simple_strtoul(argv[4], NULL, 16);
126 if (len < fdt_totalsize(fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400127 printf ("New length 0x%X < existing length "
128 "0x%X, aborting.\n",
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400129 len, fdt_totalsize(fdt));
130 return 1;
131 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400132 }
133
134 /*
135 * Copy to the new location.
136 */
137 err = fdt_open_into(fdt, newaddr, len);
138 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400139 printf ("libfdt fdt_open_into(): %s\n",
140 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400141 return 1;
142 }
143 fdt = newaddr;
144
145 /********************************************************************
Gerald Van Baren25114032007-05-12 09:47:25 -0400146 * Make a new node
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400147 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -0400148 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
149 char *pathp; /* path */
150 char *nodep; /* new node to add */
151 int nodeoffset; /* node offset from libfdt */
152 int err;
153
154 /*
155 * Parameters: Node path, new node to be appended to the path.
156 */
157 if (argc < 4) {
158 printf ("Usage:\n%s\n", cmdtp->usage);
159 return 1;
160 }
161
162 pathp = argv[2];
163 nodep = argv[3];
164
Kumar Gala8d04f022007-10-24 11:04:22 -0500165 nodeoffset = fdt_path_offset (fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400166 if (nodeoffset < 0) {
167 /*
168 * Not found or something else bad happened.
169 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500170 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400171 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400172 return 1;
173 }
174 err = fdt_add_subnode(fdt, nodeoffset, nodep);
175 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400176 printf ("libfdt fdt_add_subnode(): %s\n",
177 fdt_strerror(err));
Gerald Van Baren25114032007-05-12 09:47:25 -0400178 return 1;
179 }
180
181 /********************************************************************
182 * Set the value of a property in the fdt.
183 ********************************************************************/
184 } else if (argv[1][0] == 's') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400185 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400186 char *prop; /* property */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400187 int nodeoffset; /* node offset from libfdt */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400188 static char data[SCRATCHPAD]; /* storage for the property */
189 int len; /* new length of the property */
190 int ret; /* return value */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400191
192 /*
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500193 * Parameters: Node path, property, optional value.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400194 */
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500195 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400196 printf ("Usage:\n%s\n", cmdtp->usage);
197 return 1;
198 }
199
200 pathp = argv[2];
201 prop = argv[3];
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500202 if (argc == 4) {
203 len = 0;
204 } else {
205 ret = fdt_parse_prop(pathp, prop, argv[4], data, &len);
206 if (ret != 0)
207 return ret;
208 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400209
Kumar Gala8d04f022007-10-24 11:04:22 -0500210 nodeoffset = fdt_path_offset (fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400211 if (nodeoffset < 0) {
212 /*
213 * Not found or something else bad happened.
214 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500215 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400216 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400217 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400218 }
Gerald Van Baren25114032007-05-12 09:47:25 -0400219
220 ret = fdt_setprop(fdt, nodeoffset, prop, data, len);
221 if (ret < 0) {
222 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
223 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400224 }
225
226 /********************************************************************
227 * Print (recursive) / List (single level)
228 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -0400229 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400230 int depth = MAX_LEVEL; /* how deep to print */
231 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400232 char *prop; /* property */
233 int ret; /* return value */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500234 static char root[2] = "/";
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400235
236 /*
237 * list is an alias for print, but limited to 1 level
238 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400239 if (argv[1][0] == 'l') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400240 depth = 1;
241 }
242
243 /*
244 * Get the starting path. The root node is an oddball,
245 * the offset is zero and has no name.
246 */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500247 if (argc == 2)
248 pathp = root;
249 else
250 pathp = argv[2];
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400251 if (argc > 3)
252 prop = argv[3];
253 else
254 prop = NULL;
255
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400256 ret = fdt_print(pathp, prop, depth);
257 if (ret != 0)
258 return ret;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400259
260 /********************************************************************
261 * Remove a property/node
262 ********************************************************************/
Kumar Gala804887e2008-02-15 03:34:36 -0600263 } else if ((argv[1][0] == 'r') && (argv[1][1] == 'm')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400264 int nodeoffset; /* node offset from libfdt */
265 int err;
266
267 /*
268 * Get the path. The root node is an oddball, the offset
269 * is zero and has no name.
270 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500271 nodeoffset = fdt_path_offset (fdt, argv[2]);
Gerald Van Baren25114032007-05-12 09:47:25 -0400272 if (nodeoffset < 0) {
273 /*
274 * Not found or something else bad happened.
275 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500276 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400277 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400278 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400279 }
280 /*
281 * Do the delete. A fourth parameter means delete a property,
282 * otherwise delete the node.
283 */
284 if (argc > 3) {
285 err = fdt_delprop(fdt, nodeoffset, argv[3]);
286 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400287 printf("libfdt fdt_delprop(): %s\n",
288 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400289 return err;
290 }
291 } else {
292 err = fdt_del_node(fdt, nodeoffset);
293 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400294 printf("libfdt fdt_del_node(): %s\n",
295 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400296 return err;
297 }
298 }
Kumar Gala804887e2008-02-15 03:34:36 -0600299
300 /********************************************************************
301 * Display header info
302 ********************************************************************/
303 } else if (argv[1][0] == 'h') {
304 u32 version = fdt_version(fdt);
305 printf("magic:\t\t\t0x%x\n", fdt_magic(fdt));
306 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(fdt), fdt_totalsize(fdt));
307 printf("off_dt_struct:\t\t0x%x\n", fdt_off_dt_struct(fdt));
308 printf("off_dt_strings:\t\t0x%x\n", fdt_off_dt_strings(fdt));
309 printf("off_mem_rsvmap:\t\t0x%x\n", fdt_off_mem_rsvmap(fdt));
310 printf("version:\t\t%d\n", version);
311 printf("last_comp_version:\t%d\n", fdt_last_comp_version(fdt));
312 if (version >= 2)
313 printf("boot_cpuid_phys:\t0x%x\n",
314 fdt_boot_cpuid_phys(fdt));
315 if (version >= 3)
316 printf("size_dt_strings:\t0x%x\n",
317 fdt_size_dt_strings(fdt));
318 if (version >= 17)
319 printf("size_dt_struct:\t\t0x%x\n",
320 fdt_size_dt_struct(fdt));
321 printf("number mem_rsv:\t\t0x%x\n", fdt_num_mem_rsv(fdt));
322 printf("\n");
323
324 /********************************************************************
325 * Set boot cpu id
326 ********************************************************************/
327 } else if ((argv[1][0] == 'b') && (argv[1][1] == 'o')) {
328 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
329 fdt_set_boot_cpuid_phys(fdt, tmp);
330
331 /********************************************************************
332 * memory command
333 ********************************************************************/
334 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'e')) {
335 uint64_t addr, size;
336 int err;
337#ifdef CFG_64BIT_STRTOUL
338 addr = simple_strtoull(argv[2], NULL, 16);
339 size = simple_strtoull(argv[3], NULL, 16);
340#else
341 addr = simple_strtoul(argv[2], NULL, 16);
342 size = simple_strtoul(argv[3], NULL, 16);
343#endif
344 err = fdt_fixup_memory(fdt, addr, size);
345 if (err < 0)
346 return err;
347
348 /********************************************************************
349 * mem reserve commands
350 ********************************************************************/
351 } else if ((argv[1][0] == 'r') && (argv[1][1] == 's')) {
352 if (argv[2][0] == 'p') {
353 uint64_t addr, size;
354 int total = fdt_num_mem_rsv(fdt);
355 int j, err;
356 printf("index\t\t start\t\t size\n");
357 printf("-------------------------------"
358 "-----------------\n");
359 for (j = 0; j < total; j++) {
360 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
361 if (err < 0) {
362 printf("libfdt fdt_get_mem_rsv(): %s\n",
363 fdt_strerror(err));
364 return err;
365 }
366 printf(" %x\t%08x%08x\t%08x%08x\n", j,
367 (u32)(addr >> 32),
368 (u32)(addr & 0xffffffff),
369 (u32)(size >> 32),
370 (u32)(size & 0xffffffff));
371 }
372 } else if (argv[2][0] == 'a') {
373 uint64_t addr, size;
374 int err;
375#ifdef CFG_64BIT_STRTOUL
376 addr = simple_strtoull(argv[3], NULL, 16);
377 size = simple_strtoull(argv[4], NULL, 16);
378#else
379 addr = simple_strtoul(argv[3], NULL, 16);
380 size = simple_strtoul(argv[4], NULL, 16);
381#endif
382 err = fdt_add_mem_rsv(fdt, addr, size);
383
384 if (err < 0) {
385 printf("libfdt fdt_add_mem_rsv(): %s\n",
386 fdt_strerror(err));
387 return err;
388 }
389 } else if (argv[2][0] == 'd') {
390 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
391 int err = fdt_del_mem_rsv(fdt, idx);
392
393 if (err < 0) {
394 printf("libfdt fdt_del_mem_rsv(): %s\n",
395 fdt_strerror(err));
396 return err;
397 }
398 } else {
399 /* Unrecognized command */
400 printf ("Usage:\n%s\n", cmdtp->usage);
401 return 1;
402 }
Kim Phillips99dffca2007-07-17 13:57:04 -0500403 }
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400404#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillips99dffca2007-07-17 13:57:04 -0500405 /* Call the board-specific fixup routine */
406 else if (argv[1][0] == 'b')
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400407 ft_board_setup(fdt, gd->bd);
408#endif
Kim Phillips99dffca2007-07-17 13:57:04 -0500409 /* Create a chosen node */
410 else if (argv[1][0] == 'c')
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400411 fdt_chosen(fdt, 0, 0, 1);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400412
Kim Phillips99dffca2007-07-17 13:57:04 -0500413#ifdef CONFIG_OF_HAS_UBOOT_ENV
414 /* Create a u-boot-env node */
415 else if (argv[1][0] == 'e')
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400416 fdt_env(fdt);
Kim Phillips99dffca2007-07-17 13:57:04 -0500417#endif
418#ifdef CONFIG_OF_HAS_BD_T
419 /* Create a bd_t node */
420 else if (argv[1][0] == 'b')
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400421 fdt_bd_t(fdt);
Kim Phillips99dffca2007-07-17 13:57:04 -0500422#endif
423 else {
424 /* Unrecognized command */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400425 printf ("Usage:\n%s\n", cmdtp->usage);
426 return 1;
427 }
428
429 return 0;
430}
431
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400432/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400433
434static int fdt_valid(void)
435{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400436 int err;
437
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400438 if (fdt == NULL) {
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400439 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400440 return 0;
441 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400442
443 err = fdt_check_header(fdt);
444 if (err == 0)
445 return 1; /* valid */
446
447 if (err < 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400448 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400449 /*
450 * Be more informative on bad version.
451 */
452 if (err == -FDT_ERR_BADVERSION) {
453 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
454 printf (" - too old, fdt $d < %d",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400455 fdt_version(fdt),
456 FDT_FIRST_SUPPORTED_VERSION);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400457 fdt = NULL;
458 }
459 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
460 printf (" - too new, fdt $d > %d",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400461 fdt_version(fdt),
462 FDT_LAST_SUPPORTED_VERSION);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400463 fdt = NULL;
464 }
465 return 0;
466 }
467 printf("\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400468 return 0;
469 }
470 return 1;
471}
472
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400473/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400474
475/*
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400476 * Parse the user's input, partially heuristic. Valid formats:
477 * <00> - hex byte
478 * <0011> - hex half word (16 bits)
479 * <00112233> - hex word (32 bits)
480 * - hex double words (64 bits) are not supported, must use
481 * a byte stream instead.
482 * [00 11 22 .. nn] - byte stream
483 * "string" - If the the value doesn't start with "<" or "[", it is
484 * treated as a string. Note that the quotes are
485 * stripped by the parser before we get the string.
486 */
487static int fdt_parse_prop(char *pathp, char *prop, char *newval,
488 char *data, int *len)
489{
490 char *cp; /* temporary char pointer */
491 unsigned long tmp; /* holds converted values */
492
493 if (*newval == '<') {
494 /*
495 * Bigger values than bytes.
496 */
497 *len = 0;
498 newval++;
499 while ((*newval != '>') && (*newval != '\0')) {
500 cp = newval;
501 tmp = simple_strtoul(cp, &newval, 16);
502 if ((newval - cp) <= 2) {
503 *data = tmp & 0xFF;
504 data += 1;
505 *len += 1;
506 } else if ((newval - cp) <= 4) {
507 *(uint16_t *)data = __cpu_to_be16(tmp);
508 data += 2;
509 *len += 2;
510 } else if ((newval - cp) <= 8) {
511 *(uint32_t *)data = __cpu_to_be32(tmp);
512 data += 4;
513 *len += 4;
514 } else {
515 printf("Sorry, I could not convert \"%s\"\n",
516 cp);
517 return 1;
518 }
519 while (*newval == ' ')
520 newval++;
521 }
522 if (*newval != '>') {
523 printf("Unexpected character '%c'\n", *newval);
524 return 1;
525 }
526 } else if (*newval == '[') {
527 /*
528 * Byte stream. Convert the values.
529 */
530 *len = 0;
531 newval++;
532 while ((*newval != ']') && (*newval != '\0')) {
533 tmp = simple_strtoul(newval, &newval, 16);
534 *data++ = tmp & 0xFF;
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400535 *len = *len + 1;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400536 while (*newval == ' ')
537 newval++;
538 }
539 if (*newval != ']') {
540 printf("Unexpected character '%c'\n", *newval);
541 return 1;
542 }
543 } else {
544 /*
545 * Assume it is a string. Copy it into our data area for
546 * convenience (including the terminating '\0').
547 */
548 *len = strlen(newval) + 1;
549 strcpy(data, newval);
550 }
551 return 0;
552}
553
554/****************************************************************************/
555
556/*
557 * Heuristic to guess if this is a string or concatenated strings.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400558 */
559
560static int is_printable_string(const void *data, int len)
561{
562 const char *s = data;
563
564 /* zero length is not */
565 if (len == 0)
566 return 0;
567
568 /* must terminate with zero */
569 if (s[len - 1] != '\0')
570 return 0;
571
572 /* printable or a null byte (concatenated strings) */
573 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
574 /*
575 * If we see a null, there are three possibilities:
576 * 1) If len == 1, it is the end of the string, printable
577 * 2) Next character also a null, not printable.
578 * 3) Next character not a null, continue to check.
579 */
580 if (s[0] == '\0') {
581 if (len == 1)
582 return 1;
583 if (s[1] == '\0')
584 return 0;
585 }
586 s++;
587 len--;
588 }
589
590 /* Not the null termination, or not done yet: not printable */
591 if (*s != '\0' || (len != 0))
592 return 0;
593
594 return 1;
595}
596
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400597
598/*
599 * Print the property in the best format, a heuristic guess. Print as
600 * a string, concatenated strings, a byte, word, double word, or (if all
601 * else fails) it is printed as a stream of bytes.
602 */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400603static void print_data(const void *data, int len)
604{
605 int j;
606 const u8 *s;
607
608 /* no data, don't print */
609 if (len == 0)
610 return;
611
612 /*
613 * It is a string, but it may have multiple strings (embedded '\0's).
614 */
615 if (is_printable_string(data, len)) {
616 puts("\"");
617 j = 0;
618 while (j < len) {
619 if (j > 0)
620 puts("\", \"");
621 puts(data);
622 j += strlen(data) + 1;
623 data += strlen(data) + 1;
624 }
625 puts("\"");
626 return;
627 }
628
629 switch (len) {
630 case 1: /* byte */
Gerald Van Baren91623522007-11-22 17:23:23 -0500631 printf("<0x%02x>", (*(u8 *) data) & 0xff);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400632 break;
633 case 2: /* half-word */
Gerald Van Baren91623522007-11-22 17:23:23 -0500634 printf("<0x%04x>", be16_to_cpu(*(u16 *) data) & 0xffff);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400635 break;
636 case 4: /* word */
Gerald Van Baren91623522007-11-22 17:23:23 -0500637 printf("<0x%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400638 break;
639 case 8: /* double-word */
640#if __WORDSIZE == 64
Gerald Van Baren91623522007-11-22 17:23:23 -0500641 printf("<0x%016llx>", be64_to_cpu(*(uint64_t *) data));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400642#else
Gerald Van Baren91623522007-11-22 17:23:23 -0500643 printf("<0x%08x ", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400644 data += 4;
Gerald Van Baren91623522007-11-22 17:23:23 -0500645 printf("0x%08x>", be32_to_cpu(*(u32 *) data) & 0xffffffffU);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400646#endif
647 break;
648 default: /* anything else... hexdump */
649 printf("[");
650 for (j = 0, s = data; j < len; j++)
651 printf("%02x%s", s[j], j < len - 1 ? " " : "");
652 printf("]");
653
654 break;
655 }
656}
657
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400658/****************************************************************************/
659
660/*
661 * Recursively print (a portion of) the fdt. The depth parameter
662 * determines how deeply nested the fdt is printed.
663 */
Kumar Galadbaf07c2007-11-21 14:07:46 -0600664static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400665{
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400666 static char tabs[MAX_LEVEL+1] =
667 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
668 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Galadbaf07c2007-11-21 14:07:46 -0600669 const void *nodep; /* property node pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400670 int nodeoffset; /* node offset from libfdt */
671 int nextoffset; /* next node offset from libfdt */
672 uint32_t tag; /* tag */
673 int len; /* length of the property */
674 int level = 0; /* keep track of nesting level */
Gerald Van Baren91623522007-11-22 17:23:23 -0500675 const struct fdt_property *fdt_prop;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400676
Kumar Gala8d04f022007-10-24 11:04:22 -0500677 nodeoffset = fdt_path_offset (fdt, pathp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400678 if (nodeoffset < 0) {
679 /*
680 * Not found or something else bad happened.
681 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500682 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400683 fdt_strerror(nodeoffset));
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400684 return 1;
685 }
686 /*
687 * The user passed in a property as well as node path.
688 * Print only the given property and then return.
689 */
690 if (prop) {
691 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
692 if (len == 0) {
693 /* no property value */
694 printf("%s %s\n", pathp, prop);
695 return 0;
696 } else if (len > 0) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500697 printf("%s = ", prop);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400698 print_data (nodep, len);
699 printf("\n");
700 return 0;
701 } else {
702 printf ("libfdt fdt_getprop(): %s\n",
703 fdt_strerror(len));
704 return 1;
705 }
706 }
707
708 /*
709 * The user passed in a node path and no property,
710 * print the node and all subnodes.
711 */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400712 while(level >= 0) {
Kumar Gala8d04f022007-10-24 11:04:22 -0500713 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400714 switch(tag) {
715 case FDT_BEGIN_NODE:
Gerald Van Baren91623522007-11-22 17:23:23 -0500716 pathp = fdt_get_name(fdt, nodeoffset, NULL);
717 if (level <= depth) {
718 if (pathp == NULL)
719 pathp = "/* NULL pointer error */";
720 if (*pathp == '\0')
721 pathp = "/"; /* root is nameless */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400722 printf("%s%s {\n",
723 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Baren91623522007-11-22 17:23:23 -0500724 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400725 level++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400726 if (level >= MAX_LEVEL) {
Gerald Van Baren91623522007-11-22 17:23:23 -0500727 printf("Nested too deep, aborting.\n");
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400728 return 1;
729 }
730 break;
731 case FDT_END_NODE:
732 level--;
Gerald Van Baren91623522007-11-22 17:23:23 -0500733 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400734 printf("%s};\n", &tabs[MAX_LEVEL - level]);
735 if (level == 0) {
736 level = -1; /* exit the loop */
737 }
738 break;
739 case FDT_PROP:
Gerald Van Baren91623522007-11-22 17:23:23 -0500740 fdt_prop = fdt_offset_ptr(fdt, nodeoffset,
741 sizeof(*fdt_prop));
742 pathp = fdt_string(fdt,
743 fdt32_to_cpu(fdt_prop->nameoff));
744 len = fdt32_to_cpu(fdt_prop->len);
745 nodep = fdt_prop->data;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400746 if (len < 0) {
747 printf ("libfdt fdt_getprop(): %s\n",
748 fdt_strerror(len));
749 return 1;
750 } else if (len == 0) {
751 /* the property has no value */
Gerald Van Baren91623522007-11-22 17:23:23 -0500752 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400753 printf("%s%s;\n",
754 &tabs[MAX_LEVEL - level],
755 pathp);
756 } else {
Gerald Van Baren91623522007-11-22 17:23:23 -0500757 if (level <= depth) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500758 printf("%s%s = ",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400759 &tabs[MAX_LEVEL - level],
760 pathp);
761 print_data (nodep, len);
762 printf(";\n");
763 }
764 }
765 break;
766 case FDT_NOP:
Gerald Van Baren91623522007-11-22 17:23:23 -0500767 printf("/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400768 break;
769 case FDT_END:
770 return 1;
771 default:
Gerald Van Baren91623522007-11-22 17:23:23 -0500772 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400773 printf("Unknown tag 0x%08X\n", tag);
774 return 1;
775 }
776 nodeoffset = nextoffset;
777 }
778 return 0;
779}
780
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400781/********************************************************************/
782
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400783U_BOOT_CMD(
784 fdt, 5, 0, do_fdt,
785 "fdt - flattened device tree utility commands\n",
786 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400787#ifdef CONFIG_OF_BOARD_SETUP
788 "fdt boardsetup - Do board-specific set up\n"
789#endif
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500790 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400791 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
792 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
793 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
794 "fdt mknode <path> <node> - Create a new node after <path>\n"
795 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Kumar Gala804887e2008-02-15 03:34:36 -0600796 "fdt header - Display header info\n"
797 "fdt bootcpu <id> - Set boot cpuid\n"
798 "fdt memory <addr> <size> - Add/Update memory node\n"
799 "fdt rsvmem print - Show current mem reserves\n"
800 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
801 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400802 "fdt chosen - Add/update the /chosen branch in the tree\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400803#ifdef CONFIG_OF_HAS_UBOOT_ENV
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400804 "fdt env - Add/replace the /u-boot-env branch in the tree\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400805#endif
806#ifdef CONFIG_OF_HAS_BD_T
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400807 "fdt bd_t - Add/replace the /bd_t branch in the tree\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400808#endif
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500809 "NOTE: If the path or property you are setting/printing has a '#' character\n"
810 " or spaces, you MUST escape it with a \\ character or quote it with \".\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400811);