blob: 8bc900aa795757a59829edc3b94305038d421cae [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/*
Gerald Van Barenae9e97f2008-06-10 22:15:58 -040049 * The working_fdt points to our working flattened device tree.
50 */
51struct fdt_header *working_fdt;
52
Kumar Gala54f9c862008-08-15 08:24:39 -050053void set_working_fdt_addr(void *addr)
54{
55 char buf[17];
56
57 working_fdt = addr;
58
59 sprintf(buf, "%lx", (unsigned long)addr);
60 setenv("fdtaddr", buf);
61}
62
Gerald Van Barenae9e97f2008-06-10 22:15:58 -040063/*
Gerald Van Baren781e09e2007-03-31 12:22:10 -040064 * Flattened Device Tree command, see the help for parameter definitions.
65 */
66int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
67{
Gerald Van Baren781e09e2007-03-31 12:22:10 -040068 if (argc < 2) {
69 printf ("Usage:\n%s\n", cmdtp->usage);
70 return 1;
71 }
72
Gerald Van Baren781e09e2007-03-31 12:22:10 -040073 /********************************************************************
74 * Set the address of the fdt
75 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -040076 if (argv[1][0] == 'a') {
Kumar Gala54f9c862008-08-15 08:24:39 -050077 unsigned long addr;
Gerald Van Baren781e09e2007-03-31 12:22:10 -040078 /*
79 * Set the address [and length] of the fdt.
80 */
Kumar Gala7dbc38a2008-08-15 08:24:35 -050081 if (argc == 2) {
82 if (!fdt_valid()) {
83 return 1;
84 }
85 printf("The address of the fdt is %p\n", working_fdt);
86 return 0;
87 }
88
Kumar Gala54f9c862008-08-15 08:24:39 -050089 addr = simple_strtoul(argv[2], NULL, 16);
90 set_working_fdt_addr((void *)addr);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040091
92 if (!fdt_valid()) {
93 return 1;
94 }
95
96 if (argc >= 4) {
97 int len;
98 int err;
99 /*
100 * Optional new length
101 */
Gerald Van Baren91623522007-11-22 17:23:23 -0500102 len = simple_strtoul(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500103 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400104 printf ("New length %d < existing length %d, "
105 "ignoring.\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500106 len, fdt_totalsize(working_fdt));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400107 } else {
108 /*
109 * Open in place with a new length.
110 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500111 err = fdt_open_into(working_fdt, working_fdt, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400112 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400113 printf ("libfdt fdt_open_into(): %s\n",
114 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400115 }
116 }
117 }
118
119 /********************************************************************
Kim Phillipse489b9c2008-06-10 11:06:17 -0500120 * Move the working_fdt
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400121 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400122 } else if (strncmp(argv[1], "mo", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400123 struct fdt_header *newaddr;
124 int len;
125 int err;
126
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400127 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400128 printf ("Usage:\n%s\n", cmdtp->usage);
129 return 1;
130 }
131
132 /*
133 * Set the address and length of the fdt.
134 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500135 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400136 if (!fdt_valid()) {
137 return 1;
138 }
139
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400140 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400141
142 /*
143 * If the user specifies a length, use that. Otherwise use the
144 * current length.
145 */
146 if (argc <= 4) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500147 len = fdt_totalsize(working_fdt);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400148 } else {
149 len = simple_strtoul(argv[4], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500150 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400151 printf ("New length 0x%X < existing length "
152 "0x%X, aborting.\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500153 len, fdt_totalsize(working_fdt));
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400154 return 1;
155 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400156 }
157
158 /*
159 * Copy to the new location.
160 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500161 err = fdt_open_into(working_fdt, newaddr, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400162 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400163 printf ("libfdt fdt_open_into(): %s\n",
164 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400165 return 1;
166 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500167 working_fdt = newaddr;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400168
169 /********************************************************************
Gerald Van Baren25114032007-05-12 09:47:25 -0400170 * Make a new node
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400171 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400172 } else if (strncmp(argv[1], "mk", 2) == 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400173 char *pathp; /* path */
174 char *nodep; /* new node to add */
175 int nodeoffset; /* node offset from libfdt */
176 int err;
177
178 /*
179 * Parameters: Node path, new node to be appended to the path.
180 */
181 if (argc < 4) {
182 printf ("Usage:\n%s\n", cmdtp->usage);
183 return 1;
184 }
185
186 pathp = argv[2];
187 nodep = argv[3];
188
Kim Phillipse489b9c2008-06-10 11:06:17 -0500189 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400190 if (nodeoffset < 0) {
191 /*
192 * Not found or something else bad happened.
193 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500194 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400195 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400196 return 1;
197 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500198 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
Gerald Van Baren25114032007-05-12 09:47:25 -0400199 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400200 printf ("libfdt fdt_add_subnode(): %s\n",
201 fdt_strerror(err));
Gerald Van Baren25114032007-05-12 09:47:25 -0400202 return 1;
203 }
204
205 /********************************************************************
Kim Phillipse489b9c2008-06-10 11:06:17 -0500206 * Set the value of a property in the working_fdt.
Gerald Van Baren25114032007-05-12 09:47:25 -0400207 ********************************************************************/
208 } else if (argv[1][0] == 's') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400209 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400210 char *prop; /* property */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400211 int nodeoffset; /* node offset from libfdt */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400212 static char data[SCRATCHPAD]; /* storage for the property */
213 int len; /* new length of the property */
214 int ret; /* return value */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400215
216 /*
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500217 * Parameters: Node path, property, optional value.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400218 */
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500219 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400220 printf ("Usage:\n%s\n", cmdtp->usage);
221 return 1;
222 }
223
224 pathp = argv[2];
225 prop = argv[3];
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500226 if (argc == 4) {
227 len = 0;
228 } else {
Andy Fleming4abd8442008-03-31 20:45:56 -0500229 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500230 if (ret != 0)
231 return ret;
232 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400233
Kim Phillipse489b9c2008-06-10 11:06:17 -0500234 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400235 if (nodeoffset < 0) {
236 /*
237 * Not found or something else bad happened.
238 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500239 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400240 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400241 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400242 }
Gerald Van Baren25114032007-05-12 09:47:25 -0400243
Kim Phillipse489b9c2008-06-10 11:06:17 -0500244 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
Gerald Van Baren25114032007-05-12 09:47:25 -0400245 if (ret < 0) {
246 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
247 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400248 }
249
250 /********************************************************************
251 * Print (recursive) / List (single level)
252 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -0400253 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400254 int depth = MAX_LEVEL; /* how deep to print */
255 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400256 char *prop; /* property */
257 int ret; /* return value */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500258 static char root[2] = "/";
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400259
260 /*
261 * list is an alias for print, but limited to 1 level
262 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400263 if (argv[1][0] == 'l') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400264 depth = 1;
265 }
266
267 /*
268 * Get the starting path. The root node is an oddball,
269 * the offset is zero and has no name.
270 */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500271 if (argc == 2)
272 pathp = root;
273 else
274 pathp = argv[2];
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400275 if (argc > 3)
276 prop = argv[3];
277 else
278 prop = NULL;
279
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400280 ret = fdt_print(pathp, prop, depth);
281 if (ret != 0)
282 return ret;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400283
284 /********************************************************************
285 * Remove a property/node
286 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400287 } else if (strncmp(argv[1], "rm", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400288 int nodeoffset; /* node offset from libfdt */
289 int err;
290
291 /*
292 * Get the path. The root node is an oddball, the offset
293 * is zero and has no name.
294 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500295 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
Gerald Van Baren25114032007-05-12 09:47:25 -0400296 if (nodeoffset < 0) {
297 /*
298 * Not found or something else bad happened.
299 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500300 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400301 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400302 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400303 }
304 /*
305 * Do the delete. A fourth parameter means delete a property,
306 * otherwise delete the node.
307 */
308 if (argc > 3) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500309 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400310 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400311 printf("libfdt fdt_delprop(): %s\n",
312 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400313 return err;
314 }
315 } else {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500316 err = fdt_del_node(working_fdt, nodeoffset);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400317 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400318 printf("libfdt fdt_del_node(): %s\n",
319 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400320 return err;
321 }
322 }
Kumar Gala804887e2008-02-15 03:34:36 -0600323
324 /********************************************************************
325 * Display header info
326 ********************************************************************/
327 } else if (argv[1][0] == 'h') {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500328 u32 version = fdt_version(working_fdt);
329 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
330 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
331 fdt_totalsize(working_fdt));
332 printf("off_dt_struct:\t\t0x%x\n",
333 fdt_off_dt_struct(working_fdt));
334 printf("off_dt_strings:\t\t0x%x\n",
335 fdt_off_dt_strings(working_fdt));
336 printf("off_mem_rsvmap:\t\t0x%x\n",
337 fdt_off_mem_rsvmap(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600338 printf("version:\t\t%d\n", version);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500339 printf("last_comp_version:\t%d\n",
340 fdt_last_comp_version(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600341 if (version >= 2)
342 printf("boot_cpuid_phys:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500343 fdt_boot_cpuid_phys(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600344 if (version >= 3)
345 printf("size_dt_strings:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500346 fdt_size_dt_strings(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600347 if (version >= 17)
348 printf("size_dt_struct:\t\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500349 fdt_size_dt_struct(working_fdt));
350 printf("number mem_rsv:\t\t0x%x\n",
351 fdt_num_mem_rsv(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600352 printf("\n");
353
354 /********************************************************************
355 * Set boot cpu id
356 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400357 } else if (strncmp(argv[1], "boo", 3) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600358 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500359 fdt_set_boot_cpuid_phys(working_fdt, tmp);
Kumar Gala804887e2008-02-15 03:34:36 -0600360
361 /********************************************************************
362 * memory command
363 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400364 } else if (strncmp(argv[1], "me", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600365 uint64_t addr, size;
366 int err;
367#ifdef CFG_64BIT_STRTOUL
368 addr = simple_strtoull(argv[2], NULL, 16);
369 size = simple_strtoull(argv[3], NULL, 16);
370#else
371 addr = simple_strtoul(argv[2], NULL, 16);
372 size = simple_strtoul(argv[3], NULL, 16);
373#endif
Kim Phillipse489b9c2008-06-10 11:06:17 -0500374 err = fdt_fixup_memory(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600375 if (err < 0)
376 return err;
377
378 /********************************************************************
379 * mem reserve commands
380 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400381 } else if (strncmp(argv[1], "rs", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600382 if (argv[2][0] == 'p') {
383 uint64_t addr, size;
Kim Phillipse489b9c2008-06-10 11:06:17 -0500384 int total = fdt_num_mem_rsv(working_fdt);
Kumar Gala804887e2008-02-15 03:34:36 -0600385 int j, err;
386 printf("index\t\t start\t\t size\n");
387 printf("-------------------------------"
388 "-----------------\n");
389 for (j = 0; j < total; j++) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500390 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
Kumar Gala804887e2008-02-15 03:34:36 -0600391 if (err < 0) {
392 printf("libfdt fdt_get_mem_rsv(): %s\n",
393 fdt_strerror(err));
394 return err;
395 }
396 printf(" %x\t%08x%08x\t%08x%08x\n", j,
397 (u32)(addr >> 32),
398 (u32)(addr & 0xffffffff),
399 (u32)(size >> 32),
400 (u32)(size & 0xffffffff));
401 }
402 } else if (argv[2][0] == 'a') {
403 uint64_t addr, size;
404 int err;
405#ifdef CFG_64BIT_STRTOUL
406 addr = simple_strtoull(argv[3], NULL, 16);
407 size = simple_strtoull(argv[4], NULL, 16);
408#else
409 addr = simple_strtoul(argv[3], NULL, 16);
410 size = simple_strtoul(argv[4], NULL, 16);
411#endif
Kim Phillipse489b9c2008-06-10 11:06:17 -0500412 err = fdt_add_mem_rsv(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600413
414 if (err < 0) {
415 printf("libfdt fdt_add_mem_rsv(): %s\n",
416 fdt_strerror(err));
417 return err;
418 }
419 } else if (argv[2][0] == 'd') {
420 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500421 int err = fdt_del_mem_rsv(working_fdt, idx);
Kumar Gala804887e2008-02-15 03:34:36 -0600422
423 if (err < 0) {
424 printf("libfdt fdt_del_mem_rsv(): %s\n",
425 fdt_strerror(err));
426 return err;
427 }
428 } else {
429 /* Unrecognized command */
430 printf ("Usage:\n%s\n", cmdtp->usage);
431 return 1;
432 }
Kim Phillips99dffca2007-07-17 13:57:04 -0500433 }
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400434#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillips99dffca2007-07-17 13:57:04 -0500435 /* Call the board-specific fixup routine */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400436 else if (strncmp(argv[1], "boa", 3) == 0)
Kim Phillipse489b9c2008-06-10 11:06:17 -0500437 ft_board_setup(working_fdt, gd->bd);
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400438#endif
Kim Phillips99dffca2007-07-17 13:57:04 -0500439 /* Create a chosen node */
Kumar Galaf953d992008-08-15 08:24:34 -0500440 else if (argv[1][0] == 'c') {
441 unsigned long initrd_start = 0, initrd_end = 0;
442
443 if ((argc != 2) && (argc != 4)) {
444 printf ("Usage:\n%s\n", cmdtp->usage);
445 return 1;
446 }
447
448 if (argc == 4) {
449 initrd_start = simple_strtoul(argv[2], NULL, 16);
450 initrd_end = simple_strtoul(argv[3], NULL, 16);
451 }
452
453 fdt_chosen(working_fdt, initrd_start, initrd_end, 1);
454 } else {
Kim Phillips99dffca2007-07-17 13:57:04 -0500455 /* Unrecognized command */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400456 printf ("Usage:\n%s\n", cmdtp->usage);
457 return 1;
458 }
459
460 return 0;
461}
462
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400463/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400464
465static int fdt_valid(void)
466{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400467 int err;
468
Kim Phillipse489b9c2008-06-10 11:06:17 -0500469 if (working_fdt == NULL) {
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400470 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400471 return 0;
472 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400473
Kim Phillipse489b9c2008-06-10 11:06:17 -0500474 err = fdt_check_header(working_fdt);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400475 if (err == 0)
476 return 1; /* valid */
477
478 if (err < 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400479 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400480 /*
481 * Be more informative on bad version.
482 */
483 if (err == -FDT_ERR_BADVERSION) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500484 if (fdt_version(working_fdt) <
485 FDT_FIRST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700486 printf (" - too old, fdt %d < %d",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500487 fdt_version(working_fdt),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400488 FDT_FIRST_SUPPORTED_VERSION);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500489 working_fdt = NULL;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400490 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500491 if (fdt_last_comp_version(working_fdt) >
492 FDT_LAST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700493 printf (" - too new, fdt %d > %d",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500494 fdt_version(working_fdt),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400495 FDT_LAST_SUPPORTED_VERSION);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500496 working_fdt = NULL;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400497 }
498 return 0;
499 }
500 printf("\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400501 return 0;
502 }
503 return 1;
504}
505
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400506/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400507
508/*
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400509 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming4abd8442008-03-31 20:45:56 -0500510 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200511 * C conventions.
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400512 * [00 11 22 .. nn] - byte stream
513 * "string" - If the the value doesn't start with "<" or "[", it is
514 * treated as a string. Note that the quotes are
515 * stripped by the parser before we get the string.
Andy Fleming4abd8442008-03-31 20:45:56 -0500516 * newval: An array of strings containing the new property as specified
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200517 * on the command line
Andy Fleming4abd8442008-03-31 20:45:56 -0500518 * count: The number of strings in the array
519 * data: A bytestream to be placed in the property
520 * len: The length of the resulting bytestream
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400521 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500522static int fdt_parse_prop(char **newval, int count, char *data, int *len)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400523{
524 char *cp; /* temporary char pointer */
Andy Fleming4abd8442008-03-31 20:45:56 -0500525 char *newp; /* temporary newval char pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400526 unsigned long tmp; /* holds converted values */
Andy Fleming4abd8442008-03-31 20:45:56 -0500527 int stridx = 0;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400528
Andy Fleming4abd8442008-03-31 20:45:56 -0500529 *len = 0;
530 newp = newval[0];
531
532 /* An array of cells */
533 if (*newp == '<') {
534 newp++;
535 while ((*newp != '>') && (stridx < count)) {
536 /*
537 * Keep searching until we find that last ">"
538 * That way users don't have to escape the spaces
539 */
540 if (*newp == '\0') {
541 newp = newval[++stridx];
542 continue;
543 }
544
545 cp = newp;
546 tmp = simple_strtoul(cp, &newp, 0);
547 *(uint32_t *)data = __cpu_to_be32(tmp);
548 data += 4;
549 *len += 4;
550
551 /* If the ptr didn't advance, something went wrong */
552 if ((newp - cp) <= 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400553 printf("Sorry, I could not convert \"%s\"\n",
554 cp);
555 return 1;
556 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500557
558 while (*newp == ' ')
559 newp++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400560 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500561
562 if (*newp != '>') {
563 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400564 return 1;
565 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500566 } else if (*newp == '[') {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400567 /*
568 * Byte stream. Convert the values.
569 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500570 newp++;
571 while ((*newp != ']') && (stridx < count)) {
572 tmp = simple_strtoul(newp, &newp, 16);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400573 *data++ = tmp & 0xFF;
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400574 *len = *len + 1;
Andy Fleming4abd8442008-03-31 20:45:56 -0500575 while (*newp == ' ')
576 newp++;
577 if (*newp != '\0')
578 newp = newval[++stridx];
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400579 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500580 if (*newp != ']') {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700581 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400582 return 1;
583 }
584 } else {
585 /*
586 * Assume it is a string. Copy it into our data area for
587 * convenience (including the terminating '\0').
588 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500589 while (stridx < count) {
590 *len = strlen(newp) + 1;
591 strcpy(data, newp);
592 newp = newval[++stridx];
593 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400594 }
595 return 0;
596}
597
598/****************************************************************************/
599
600/*
601 * Heuristic to guess if this is a string or concatenated strings.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400602 */
603
604static int is_printable_string(const void *data, int len)
605{
606 const char *s = data;
607
608 /* zero length is not */
609 if (len == 0)
610 return 0;
611
612 /* must terminate with zero */
613 if (s[len - 1] != '\0')
614 return 0;
615
616 /* printable or a null byte (concatenated strings) */
617 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
618 /*
619 * If we see a null, there are three possibilities:
620 * 1) If len == 1, it is the end of the string, printable
621 * 2) Next character also a null, not printable.
622 * 3) Next character not a null, continue to check.
623 */
624 if (s[0] == '\0') {
625 if (len == 1)
626 return 1;
627 if (s[1] == '\0')
628 return 0;
629 }
630 s++;
631 len--;
632 }
633
634 /* Not the null termination, or not done yet: not printable */
635 if (*s != '\0' || (len != 0))
636 return 0;
637
638 return 1;
639}
640
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400641
642/*
643 * Print the property in the best format, a heuristic guess. Print as
644 * a string, concatenated strings, a byte, word, double word, or (if all
645 * else fails) it is printed as a stream of bytes.
646 */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400647static void print_data(const void *data, int len)
648{
649 int j;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400650
651 /* no data, don't print */
652 if (len == 0)
653 return;
654
655 /*
656 * It is a string, but it may have multiple strings (embedded '\0's).
657 */
658 if (is_printable_string(data, len)) {
659 puts("\"");
660 j = 0;
661 while (j < len) {
662 if (j > 0)
663 puts("\", \"");
664 puts(data);
665 j += strlen(data) + 1;
666 data += strlen(data) + 1;
667 }
668 puts("\"");
669 return;
670 }
671
Andy Fleming4abd8442008-03-31 20:45:56 -0500672 if ((len %4) == 0) {
673 const u32 *p;
674
675 printf("<");
676 for (j = 0, p = data; j < len/4; j ++)
677 printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
678 printf(">");
679 } else { /* anything else... hexdump */
680 const u8 *s;
681
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400682 printf("[");
683 for (j = 0, s = data; j < len; j++)
684 printf("%02x%s", s[j], j < len - 1 ? " " : "");
685 printf("]");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400686 }
687}
688
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400689/****************************************************************************/
690
691/*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500692 * Recursively print (a portion of) the working_fdt. The depth parameter
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400693 * determines how deeply nested the fdt is printed.
694 */
Kumar Galadbaf07c2007-11-21 14:07:46 -0600695static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400696{
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400697 static char tabs[MAX_LEVEL+1] =
698 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
699 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Galadbaf07c2007-11-21 14:07:46 -0600700 const void *nodep; /* property node pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400701 int nodeoffset; /* node offset from libfdt */
702 int nextoffset; /* next node offset from libfdt */
703 uint32_t tag; /* tag */
704 int len; /* length of the property */
705 int level = 0; /* keep track of nesting level */
Gerald Van Baren91623522007-11-22 17:23:23 -0500706 const struct fdt_property *fdt_prop;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400707
Kim Phillipse489b9c2008-06-10 11:06:17 -0500708 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400709 if (nodeoffset < 0) {
710 /*
711 * Not found or something else bad happened.
712 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500713 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400714 fdt_strerror(nodeoffset));
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400715 return 1;
716 }
717 /*
718 * The user passed in a property as well as node path.
719 * Print only the given property and then return.
720 */
721 if (prop) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500722 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400723 if (len == 0) {
724 /* no property value */
725 printf("%s %s\n", pathp, prop);
726 return 0;
727 } else if (len > 0) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500728 printf("%s = ", prop);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400729 print_data (nodep, len);
730 printf("\n");
731 return 0;
732 } else {
733 printf ("libfdt fdt_getprop(): %s\n",
734 fdt_strerror(len));
735 return 1;
736 }
737 }
738
739 /*
740 * The user passed in a node path and no property,
741 * print the node and all subnodes.
742 */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400743 while(level >= 0) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500744 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400745 switch(tag) {
746 case FDT_BEGIN_NODE:
Kim Phillipse489b9c2008-06-10 11:06:17 -0500747 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
Gerald Van Baren91623522007-11-22 17:23:23 -0500748 if (level <= depth) {
749 if (pathp == NULL)
750 pathp = "/* NULL pointer error */";
751 if (*pathp == '\0')
752 pathp = "/"; /* root is nameless */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400753 printf("%s%s {\n",
754 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Baren91623522007-11-22 17:23:23 -0500755 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400756 level++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400757 if (level >= MAX_LEVEL) {
Gerald Van Baren91623522007-11-22 17:23:23 -0500758 printf("Nested too deep, aborting.\n");
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400759 return 1;
760 }
761 break;
762 case FDT_END_NODE:
763 level--;
Gerald Van Baren91623522007-11-22 17:23:23 -0500764 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400765 printf("%s};\n", &tabs[MAX_LEVEL - level]);
766 if (level == 0) {
767 level = -1; /* exit the loop */
768 }
769 break;
770 case FDT_PROP:
Kim Phillipse489b9c2008-06-10 11:06:17 -0500771 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
Gerald Van Baren91623522007-11-22 17:23:23 -0500772 sizeof(*fdt_prop));
Kim Phillipse489b9c2008-06-10 11:06:17 -0500773 pathp = fdt_string(working_fdt,
Gerald Van Baren91623522007-11-22 17:23:23 -0500774 fdt32_to_cpu(fdt_prop->nameoff));
775 len = fdt32_to_cpu(fdt_prop->len);
776 nodep = fdt_prop->data;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400777 if (len < 0) {
778 printf ("libfdt fdt_getprop(): %s\n",
779 fdt_strerror(len));
780 return 1;
781 } else if (len == 0) {
782 /* the property has no value */
Gerald Van Baren91623522007-11-22 17:23:23 -0500783 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400784 printf("%s%s;\n",
785 &tabs[MAX_LEVEL - level],
786 pathp);
787 } else {
Gerald Van Baren91623522007-11-22 17:23:23 -0500788 if (level <= depth) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500789 printf("%s%s = ",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400790 &tabs[MAX_LEVEL - level],
791 pathp);
792 print_data (nodep, len);
793 printf(";\n");
794 }
795 }
796 break;
797 case FDT_NOP:
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700798 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400799 break;
800 case FDT_END:
801 return 1;
802 default:
Gerald Van Baren91623522007-11-22 17:23:23 -0500803 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400804 printf("Unknown tag 0x%08X\n", tag);
805 return 1;
806 }
807 nodeoffset = nextoffset;
808 }
809 return 0;
810}
811
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400812/********************************************************************/
813
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400814U_BOOT_CMD(
Andy Fleming4abd8442008-03-31 20:45:56 -0500815 fdt, 255, 0, do_fdt,
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400816 "fdt - flattened device tree utility commands\n",
817 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400818#ifdef CONFIG_OF_BOARD_SETUP
819 "fdt boardsetup - Do board-specific set up\n"
820#endif
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500821 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400822 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
823 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
824 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
825 "fdt mknode <path> <node> - Create a new node after <path>\n"
826 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Kumar Gala804887e2008-02-15 03:34:36 -0600827 "fdt header - Display header info\n"
828 "fdt bootcpu <id> - Set boot cpuid\n"
829 "fdt memory <addr> <size> - Add/Update memory node\n"
830 "fdt rsvmem print - Show current mem reserves\n"
831 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
832 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Kumar Galaf953d992008-08-15 08:24:34 -0500833 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
834 " <start>/<end> - initrd start/end addr\n"
Gerald Van Baren109c30f2008-08-22 14:37:05 -0400835 "NOTE: Dereference aliases by omiting the leading '/', "
836 "e.g. fdt print ethernet0.\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400837);