blob: 85921287f852243557c878b9dc7334df23eaba25 [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);
Andy Fleming4abd8442008-03-31 20:45:56 -050045static int fdt_parse_prop(char **newval, int count, char *data, int *len);
Kumar Galadbaf07c2007-11-21 14:07:46 -060046static int fdt_print(const char *pathp, char *prop, int depth);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040047
Gerald Van Baren781e09e2007-03-31 12:22:10 -040048/*
49 * Flattened Device Tree command, see the help for parameter definitions.
50 */
51int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
52{
Gerald Van Baren781e09e2007-03-31 12:22:10 -040053 if (argc < 2) {
54 printf ("Usage:\n%s\n", cmdtp->usage);
55 return 1;
56 }
57
Gerald Van Baren781e09e2007-03-31 12:22:10 -040058 /********************************************************************
59 * Set the address of the fdt
60 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -040061 if (argv[1][0] == 'a') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -040062 /*
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 */
Gerald Van Baren91623522007-11-22 17:23:23 -050077 len = simple_strtoul(argv[3], NULL, 16);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040078 if (len < fdt_totalsize(fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -040079 printf ("New length %d < existing length %d, "
80 "ignoring.\n",
Gerald Van Baren781e09e2007-03-31 12:22:10 -040081 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) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -040088 printf ("libfdt fdt_open_into(): %s\n",
89 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -040090 }
91 }
92 }
93
94 /********************************************************************
95 * Move the fdt
96 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -040097 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'o')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -040098 struct fdt_header *newaddr;
99 int len;
100 int err;
101
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400102 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400103 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
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400115 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400116
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)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400126 printf ("New length 0x%X < existing length "
127 "0x%X, aborting.\n",
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400128 len, fdt_totalsize(fdt));
129 return 1;
130 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400131 }
132
133 /*
134 * Copy to the new location.
135 */
136 err = fdt_open_into(fdt, newaddr, len);
137 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400138 printf ("libfdt fdt_open_into(): %s\n",
139 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400140 return 1;
141 }
142 fdt = newaddr;
143
144 /********************************************************************
Gerald Van Baren25114032007-05-12 09:47:25 -0400145 * Make a new node
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400146 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -0400147 } else if ((argv[1][0] == 'm') && (argv[1][1] == 'k')) {
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
Kumar Gala8d04f022007-10-24 11:04:22 -0500164 nodeoffset = fdt_path_offset (fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400165 if (nodeoffset < 0) {
166 /*
167 * Not found or something else bad happened.
168 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500169 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400170 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400171 return 1;
172 }
173 err = fdt_add_subnode(fdt, nodeoffset, nodep);
174 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400175 printf ("libfdt fdt_add_subnode(): %s\n",
176 fdt_strerror(err));
Gerald Van Baren25114032007-05-12 09:47:25 -0400177 return 1;
178 }
179
180 /********************************************************************
181 * Set the value of a property in the fdt.
182 ********************************************************************/
183 } else if (argv[1][0] == 's') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400184 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400185 char *prop; /* property */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400186 int nodeoffset; /* node offset from libfdt */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400187 static char data[SCRATCHPAD]; /* storage for the property */
188 int len; /* new length of the property */
189 int ret; /* return value */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400190
191 /*
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500192 * Parameters: Node path, property, optional value.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400193 */
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500194 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400195 printf ("Usage:\n%s\n", cmdtp->usage);
196 return 1;
197 }
198
199 pathp = argv[2];
200 prop = argv[3];
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500201 if (argc == 4) {
202 len = 0;
203 } else {
Andy Fleming4abd8442008-03-31 20:45:56 -0500204 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500205 if (ret != 0)
206 return ret;
207 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400208
Kumar Gala8d04f022007-10-24 11:04:22 -0500209 nodeoffset = fdt_path_offset (fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400210 if (nodeoffset < 0) {
211 /*
212 * Not found or something else bad happened.
213 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500214 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400215 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400216 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400217 }
Gerald Van Baren25114032007-05-12 09:47:25 -0400218
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;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400223 }
224
225 /********************************************************************
226 * Print (recursive) / List (single level)
227 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -0400228 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400229 int depth = MAX_LEVEL; /* how deep to print */
230 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400231 char *prop; /* property */
232 int ret; /* return value */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500233 static char root[2] = "/";
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400234
235 /*
236 * list is an alias for print, but limited to 1 level
237 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400238 if (argv[1][0] == 'l') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400239 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 */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500246 if (argc == 2)
247 pathp = root;
248 else
249 pathp = argv[2];
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400250 if (argc > 3)
251 prop = argv[3];
252 else
253 prop = NULL;
254
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400255 ret = fdt_print(pathp, prop, depth);
256 if (ret != 0)
257 return ret;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400258
259 /********************************************************************
260 * Remove a property/node
261 ********************************************************************/
Kumar Gala804887e2008-02-15 03:34:36 -0600262 } else if ((argv[1][0] == 'r') && (argv[1][1] == 'm')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400263 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 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500270 nodeoffset = fdt_path_offset (fdt, argv[2]);
Gerald Van Baren25114032007-05-12 09:47:25 -0400271 if (nodeoffset < 0) {
272 /*
273 * Not found or something else bad happened.
274 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500275 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400276 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400277 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400278 }
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) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400286 printf("libfdt fdt_delprop(): %s\n",
287 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400288 return err;
289 }
290 } else {
291 err = fdt_del_node(fdt, nodeoffset);
292 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400293 printf("libfdt fdt_del_node(): %s\n",
294 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400295 return err;
296 }
297 }
Kumar Gala804887e2008-02-15 03:34:36 -0600298
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 ********************************************************************/
Kumar Galafe30a352008-02-20 14:32:36 -0600326 } else if ((argv[1][0] == 'b') && (argv[1][1] == 'o') &&
327 (argv[1][2] == 'o')) {
Kumar Gala804887e2008-02-15 03:34:36 -0600328 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 */
Gerald Van Baren47abe8a2008-06-07 12:25:05 -0400406 else if ((argv[1][0] == 'b') && (argv[1][1] == 'o') &&
407 (argv[1][2] == 'a'))
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400408 ft_board_setup(fdt, gd->bd);
409#endif
Kim Phillips99dffca2007-07-17 13:57:04 -0500410 /* Create a chosen node */
411 else if (argv[1][0] == 'c')
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400412 fdt_chosen(fdt, 0, 0, 1);
Kim Phillips99dffca2007-07-17 13:57:04 -0500413 else {
414 /* Unrecognized command */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400415 printf ("Usage:\n%s\n", cmdtp->usage);
416 return 1;
417 }
418
419 return 0;
420}
421
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400422/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400423
424static int fdt_valid(void)
425{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400426 int err;
427
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400428 if (fdt == NULL) {
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400429 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400430 return 0;
431 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400432
433 err = fdt_check_header(fdt);
434 if (err == 0)
435 return 1; /* valid */
436
437 if (err < 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400438 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400439 /*
440 * Be more informative on bad version.
441 */
442 if (err == -FDT_ERR_BADVERSION) {
443 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) {
444 printf (" - too old, fdt $d < %d",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400445 fdt_version(fdt),
446 FDT_FIRST_SUPPORTED_VERSION);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400447 fdt = NULL;
448 }
449 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION) {
450 printf (" - too new, fdt $d > %d",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400451 fdt_version(fdt),
452 FDT_LAST_SUPPORTED_VERSION);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400453 fdt = NULL;
454 }
455 return 0;
456 }
457 printf("\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400458 return 0;
459 }
460 return 1;
461}
462
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400463/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400464
465/*
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400466 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming4abd8442008-03-31 20:45:56 -0500467 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200468 * C conventions.
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400469 * [00 11 22 .. nn] - byte stream
470 * "string" - If the the value doesn't start with "<" or "[", it is
471 * treated as a string. Note that the quotes are
472 * stripped by the parser before we get the string.
Andy Fleming4abd8442008-03-31 20:45:56 -0500473 * newval: An array of strings containing the new property as specified
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200474 * on the command line
Andy Fleming4abd8442008-03-31 20:45:56 -0500475 * count: The number of strings in the array
476 * data: A bytestream to be placed in the property
477 * len: The length of the resulting bytestream
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400478 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500479static int fdt_parse_prop(char **newval, int count, char *data, int *len)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400480{
481 char *cp; /* temporary char pointer */
Andy Fleming4abd8442008-03-31 20:45:56 -0500482 char *newp; /* temporary newval char pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400483 unsigned long tmp; /* holds converted values */
Andy Fleming4abd8442008-03-31 20:45:56 -0500484 int stridx = 0;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400485
Andy Fleming4abd8442008-03-31 20:45:56 -0500486 *len = 0;
487 newp = newval[0];
488
489 /* An array of cells */
490 if (*newp == '<') {
491 newp++;
492 while ((*newp != '>') && (stridx < count)) {
493 /*
494 * Keep searching until we find that last ">"
495 * That way users don't have to escape the spaces
496 */
497 if (*newp == '\0') {
498 newp = newval[++stridx];
499 continue;
500 }
501
502 cp = newp;
503 tmp = simple_strtoul(cp, &newp, 0);
504 *(uint32_t *)data = __cpu_to_be32(tmp);
505 data += 4;
506 *len += 4;
507
508 /* If the ptr didn't advance, something went wrong */
509 if ((newp - cp) <= 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400510 printf("Sorry, I could not convert \"%s\"\n",
511 cp);
512 return 1;
513 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500514
515 while (*newp == ' ')
516 newp++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400517 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500518
519 if (*newp != '>') {
520 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400521 return 1;
522 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500523 } else if (*newp == '[') {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400524 /*
525 * Byte stream. Convert the values.
526 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500527 newp++;
528 while ((*newp != ']') && (stridx < count)) {
529 tmp = simple_strtoul(newp, &newp, 16);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400530 *data++ = tmp & 0xFF;
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400531 *len = *len + 1;
Andy Fleming4abd8442008-03-31 20:45:56 -0500532 while (*newp == ' ')
533 newp++;
534 if (*newp != '\0')
535 newp = newval[++stridx];
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400536 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500537 if (*newp != ']') {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400538 printf("Unexpected character '%c'\n", *newval);
539 return 1;
540 }
541 } else {
542 /*
543 * Assume it is a string. Copy it into our data area for
544 * convenience (including the terminating '\0').
545 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500546 while (stridx < count) {
547 *len = strlen(newp) + 1;
548 strcpy(data, newp);
549 newp = newval[++stridx];
550 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400551 }
552 return 0;
553}
554
555/****************************************************************************/
556
557/*
558 * Heuristic to guess if this is a string or concatenated strings.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400559 */
560
561static int is_printable_string(const void *data, int len)
562{
563 const char *s = data;
564
565 /* zero length is not */
566 if (len == 0)
567 return 0;
568
569 /* must terminate with zero */
570 if (s[len - 1] != '\0')
571 return 0;
572
573 /* printable or a null byte (concatenated strings) */
574 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
575 /*
576 * If we see a null, there are three possibilities:
577 * 1) If len == 1, it is the end of the string, printable
578 * 2) Next character also a null, not printable.
579 * 3) Next character not a null, continue to check.
580 */
581 if (s[0] == '\0') {
582 if (len == 1)
583 return 1;
584 if (s[1] == '\0')
585 return 0;
586 }
587 s++;
588 len--;
589 }
590
591 /* Not the null termination, or not done yet: not printable */
592 if (*s != '\0' || (len != 0))
593 return 0;
594
595 return 1;
596}
597
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400598
599/*
600 * Print the property in the best format, a heuristic guess. Print as
601 * a string, concatenated strings, a byte, word, double word, or (if all
602 * else fails) it is printed as a stream of bytes.
603 */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400604static void print_data(const void *data, int len)
605{
606 int j;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400607
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
Andy Fleming4abd8442008-03-31 20:45:56 -0500629 if ((len %4) == 0) {
630 const u32 *p;
631
632 printf("<");
633 for (j = 0, p = data; j < len/4; j ++)
634 printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
635 printf(">");
636 } else { /* anything else... hexdump */
637 const u8 *s;
638
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400639 printf("[");
640 for (j = 0, s = data; j < len; j++)
641 printf("%02x%s", s[j], j < len - 1 ? " " : "");
642 printf("]");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400643 }
644}
645
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400646/****************************************************************************/
647
648/*
649 * Recursively print (a portion of) the fdt. The depth parameter
650 * determines how deeply nested the fdt is printed.
651 */
Kumar Galadbaf07c2007-11-21 14:07:46 -0600652static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400653{
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400654 static char tabs[MAX_LEVEL+1] =
655 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
656 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Galadbaf07c2007-11-21 14:07:46 -0600657 const void *nodep; /* property node pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400658 int nodeoffset; /* node offset from libfdt */
659 int nextoffset; /* next node offset from libfdt */
660 uint32_t tag; /* tag */
661 int len; /* length of the property */
662 int level = 0; /* keep track of nesting level */
Gerald Van Baren91623522007-11-22 17:23:23 -0500663 const struct fdt_property *fdt_prop;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400664
Kumar Gala8d04f022007-10-24 11:04:22 -0500665 nodeoffset = fdt_path_offset (fdt, pathp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400666 if (nodeoffset < 0) {
667 /*
668 * Not found or something else bad happened.
669 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500670 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400671 fdt_strerror(nodeoffset));
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400672 return 1;
673 }
674 /*
675 * The user passed in a property as well as node path.
676 * Print only the given property and then return.
677 */
678 if (prop) {
679 nodep = fdt_getprop (fdt, nodeoffset, prop, &len);
680 if (len == 0) {
681 /* no property value */
682 printf("%s %s\n", pathp, prop);
683 return 0;
684 } else if (len > 0) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500685 printf("%s = ", prop);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400686 print_data (nodep, len);
687 printf("\n");
688 return 0;
689 } else {
690 printf ("libfdt fdt_getprop(): %s\n",
691 fdt_strerror(len));
692 return 1;
693 }
694 }
695
696 /*
697 * The user passed in a node path and no property,
698 * print the node and all subnodes.
699 */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400700 while(level >= 0) {
Kumar Gala8d04f022007-10-24 11:04:22 -0500701 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400702 switch(tag) {
703 case FDT_BEGIN_NODE:
Gerald Van Baren91623522007-11-22 17:23:23 -0500704 pathp = fdt_get_name(fdt, nodeoffset, NULL);
705 if (level <= depth) {
706 if (pathp == NULL)
707 pathp = "/* NULL pointer error */";
708 if (*pathp == '\0')
709 pathp = "/"; /* root is nameless */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400710 printf("%s%s {\n",
711 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Baren91623522007-11-22 17:23:23 -0500712 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400713 level++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400714 if (level >= MAX_LEVEL) {
Gerald Van Baren91623522007-11-22 17:23:23 -0500715 printf("Nested too deep, aborting.\n");
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400716 return 1;
717 }
718 break;
719 case FDT_END_NODE:
720 level--;
Gerald Van Baren91623522007-11-22 17:23:23 -0500721 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400722 printf("%s};\n", &tabs[MAX_LEVEL - level]);
723 if (level == 0) {
724 level = -1; /* exit the loop */
725 }
726 break;
727 case FDT_PROP:
Gerald Van Baren91623522007-11-22 17:23:23 -0500728 fdt_prop = fdt_offset_ptr(fdt, nodeoffset,
729 sizeof(*fdt_prop));
730 pathp = fdt_string(fdt,
731 fdt32_to_cpu(fdt_prop->nameoff));
732 len = fdt32_to_cpu(fdt_prop->len);
733 nodep = fdt_prop->data;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400734 if (len < 0) {
735 printf ("libfdt fdt_getprop(): %s\n",
736 fdt_strerror(len));
737 return 1;
738 } else if (len == 0) {
739 /* the property has no value */
Gerald Van Baren91623522007-11-22 17:23:23 -0500740 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400741 printf("%s%s;\n",
742 &tabs[MAX_LEVEL - level],
743 pathp);
744 } else {
Gerald Van Baren91623522007-11-22 17:23:23 -0500745 if (level <= depth) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500746 printf("%s%s = ",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400747 &tabs[MAX_LEVEL - level],
748 pathp);
749 print_data (nodep, len);
750 printf(";\n");
751 }
752 }
753 break;
754 case FDT_NOP:
Gerald Van Baren91623522007-11-22 17:23:23 -0500755 printf("/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400756 break;
757 case FDT_END:
758 return 1;
759 default:
Gerald Van Baren91623522007-11-22 17:23:23 -0500760 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400761 printf("Unknown tag 0x%08X\n", tag);
762 return 1;
763 }
764 nodeoffset = nextoffset;
765 }
766 return 0;
767}
768
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400769/********************************************************************/
770
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400771U_BOOT_CMD(
Andy Fleming4abd8442008-03-31 20:45:56 -0500772 fdt, 255, 0, do_fdt,
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400773 "fdt - flattened device tree utility commands\n",
774 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400775#ifdef CONFIG_OF_BOARD_SETUP
776 "fdt boardsetup - Do board-specific set up\n"
777#endif
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500778 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400779 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
780 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
781 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
782 "fdt mknode <path> <node> - Create a new node after <path>\n"
783 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Kumar Gala804887e2008-02-15 03:34:36 -0600784 "fdt header - Display header info\n"
785 "fdt bootcpu <id> - Set boot cpuid\n"
786 "fdt memory <addr> <size> - Add/Update memory node\n"
787 "fdt rsvmem print - Show current mem reserves\n"
788 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
789 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400790 "fdt chosen - Add/update the /chosen branch in the tree\n"
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500791 "NOTE: If the path or property you are setting/printing has a '#' character\n"
792 " or spaces, you MUST escape it with a \\ character or quote it with \".\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400793);