blob: 436fec9928a20dc8e7260da43a3aef9e039b263a [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
53/*
Gerald Van Baren781e09e2007-03-31 12:22:10 -040054 * Flattened Device Tree command, see the help for parameter definitions.
55 */
56int do_fdt (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
57{
Gerald Van Baren781e09e2007-03-31 12:22:10 -040058 if (argc < 2) {
59 printf ("Usage:\n%s\n", cmdtp->usage);
60 return 1;
61 }
62
Gerald Van Baren781e09e2007-03-31 12:22:10 -040063 /********************************************************************
64 * Set the address of the fdt
65 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -040066 if (argv[1][0] == 'a') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -040067 /*
68 * Set the address [and length] of the fdt.
69 */
Kim Phillipse489b9c2008-06-10 11:06:17 -050070 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040071
72 if (!fdt_valid()) {
73 return 1;
74 }
75
76 if (argc >= 4) {
77 int len;
78 int err;
79 /*
80 * Optional new length
81 */
Gerald Van Baren91623522007-11-22 17:23:23 -050082 len = simple_strtoul(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -050083 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -040084 printf ("New length %d < existing length %d, "
85 "ignoring.\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -050086 len, fdt_totalsize(working_fdt));
Gerald Van Baren781e09e2007-03-31 12:22:10 -040087 } else {
88 /*
89 * Open in place with a new length.
90 */
Kim Phillipse489b9c2008-06-10 11:06:17 -050091 err = fdt_open_into(working_fdt, working_fdt, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -040092 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -040093 printf ("libfdt fdt_open_into(): %s\n",
94 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -040095 }
96 }
97 }
98
99 /********************************************************************
Kim Phillipse489b9c2008-06-10 11:06:17 -0500100 * Move the working_fdt
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400101 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400102 } else if (strncmp(argv[1], "mo", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400103 struct fdt_header *newaddr;
104 int len;
105 int err;
106
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400107 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400108 printf ("Usage:\n%s\n", cmdtp->usage);
109 return 1;
110 }
111
112 /*
113 * Set the address and length of the fdt.
114 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500115 working_fdt = (struct fdt_header *)simple_strtoul(argv[2], NULL, 16);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400116 if (!fdt_valid()) {
117 return 1;
118 }
119
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400120 newaddr = (struct fdt_header *)simple_strtoul(argv[3],NULL,16);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400121
122 /*
123 * If the user specifies a length, use that. Otherwise use the
124 * current length.
125 */
126 if (argc <= 4) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500127 len = fdt_totalsize(working_fdt);
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400128 } else {
129 len = simple_strtoul(argv[4], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500130 if (len < fdt_totalsize(working_fdt)) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400131 printf ("New length 0x%X < existing length "
132 "0x%X, aborting.\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500133 len, fdt_totalsize(working_fdt));
Gerald Van Baren6be07cc2007-04-25 22:47:15 -0400134 return 1;
135 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400136 }
137
138 /*
139 * Copy to the new location.
140 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500141 err = fdt_open_into(working_fdt, newaddr, len);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400142 if (err != 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400143 printf ("libfdt fdt_open_into(): %s\n",
144 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400145 return 1;
146 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500147 working_fdt = newaddr;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400148
149 /********************************************************************
Gerald Van Baren25114032007-05-12 09:47:25 -0400150 * Make a new node
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400151 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400152 } else if (strncmp(argv[1], "mk", 2) == 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400153 char *pathp; /* path */
154 char *nodep; /* new node to add */
155 int nodeoffset; /* node offset from libfdt */
156 int err;
157
158 /*
159 * Parameters: Node path, new node to be appended to the path.
160 */
161 if (argc < 4) {
162 printf ("Usage:\n%s\n", cmdtp->usage);
163 return 1;
164 }
165
166 pathp = argv[2];
167 nodep = argv[3];
168
Kim Phillipse489b9c2008-06-10 11:06:17 -0500169 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400170 if (nodeoffset < 0) {
171 /*
172 * Not found or something else bad happened.
173 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500174 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400175 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400176 return 1;
177 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500178 err = fdt_add_subnode(working_fdt, nodeoffset, nodep);
Gerald Van Baren25114032007-05-12 09:47:25 -0400179 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400180 printf ("libfdt fdt_add_subnode(): %s\n",
181 fdt_strerror(err));
Gerald Van Baren25114032007-05-12 09:47:25 -0400182 return 1;
183 }
184
185 /********************************************************************
Kim Phillipse489b9c2008-06-10 11:06:17 -0500186 * Set the value of a property in the working_fdt.
Gerald Van Baren25114032007-05-12 09:47:25 -0400187 ********************************************************************/
188 } else if (argv[1][0] == 's') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400189 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400190 char *prop; /* property */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400191 int nodeoffset; /* node offset from libfdt */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400192 static char data[SCRATCHPAD]; /* storage for the property */
193 int len; /* new length of the property */
194 int ret; /* return value */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400195
196 /*
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500197 * Parameters: Node path, property, optional value.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400198 */
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500199 if (argc < 4) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400200 printf ("Usage:\n%s\n", cmdtp->usage);
201 return 1;
202 }
203
204 pathp = argv[2];
205 prop = argv[3];
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500206 if (argc == 4) {
207 len = 0;
208 } else {
Andy Fleming4abd8442008-03-31 20:45:56 -0500209 ret = fdt_parse_prop(&argv[4], argc - 4, data, &len);
Gerald Van Barenea6d8be2008-01-05 14:52:04 -0500210 if (ret != 0)
211 return ret;
212 }
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400213
Kim Phillipse489b9c2008-06-10 11:06:17 -0500214 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Baren25114032007-05-12 09:47:25 -0400215 if (nodeoffset < 0) {
216 /*
217 * Not found or something else bad happened.
218 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500219 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400220 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400221 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400222 }
Gerald Van Baren25114032007-05-12 09:47:25 -0400223
Kim Phillipse489b9c2008-06-10 11:06:17 -0500224 ret = fdt_setprop(working_fdt, nodeoffset, prop, data, len);
Gerald Van Baren25114032007-05-12 09:47:25 -0400225 if (ret < 0) {
226 printf ("libfdt fdt_setprop(): %s\n", fdt_strerror(ret));
227 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400228 }
229
230 /********************************************************************
231 * Print (recursive) / List (single level)
232 ********************************************************************/
Gerald Van Baren25114032007-05-12 09:47:25 -0400233 } else if ((argv[1][0] == 'p') || (argv[1][0] == 'l')) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400234 int depth = MAX_LEVEL; /* how deep to print */
235 char *pathp; /* path */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400236 char *prop; /* property */
237 int ret; /* return value */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500238 static char root[2] = "/";
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400239
240 /*
241 * list is an alias for print, but limited to 1 level
242 */
Gerald Van Baren25114032007-05-12 09:47:25 -0400243 if (argv[1][0] == 'l') {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400244 depth = 1;
245 }
246
247 /*
248 * Get the starting path. The root node is an oddball,
249 * the offset is zero and has no name.
250 */
Kumar Galaf738b4a2007-10-25 16:15:07 -0500251 if (argc == 2)
252 pathp = root;
253 else
254 pathp = argv[2];
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400255 if (argc > 3)
256 prop = argv[3];
257 else
258 prop = NULL;
259
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400260 ret = fdt_print(pathp, prop, depth);
261 if (ret != 0)
262 return ret;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400263
264 /********************************************************************
265 * Remove a property/node
266 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400267 } else if (strncmp(argv[1], "rm", 2) == 0) {
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400268 int nodeoffset; /* node offset from libfdt */
269 int err;
270
271 /*
272 * Get the path. The root node is an oddball, the offset
273 * is zero and has no name.
274 */
Kim Phillipse489b9c2008-06-10 11:06:17 -0500275 nodeoffset = fdt_path_offset (working_fdt, argv[2]);
Gerald Van Baren25114032007-05-12 09:47:25 -0400276 if (nodeoffset < 0) {
277 /*
278 * Not found or something else bad happened.
279 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500280 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400281 fdt_strerror(nodeoffset));
Gerald Van Baren25114032007-05-12 09:47:25 -0400282 return 1;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400283 }
284 /*
285 * Do the delete. A fourth parameter means delete a property,
286 * otherwise delete the node.
287 */
288 if (argc > 3) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500289 err = fdt_delprop(working_fdt, nodeoffset, argv[3]);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400290 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400291 printf("libfdt fdt_delprop(): %s\n",
292 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400293 return err;
294 }
295 } else {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500296 err = fdt_del_node(working_fdt, nodeoffset);
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400297 if (err < 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400298 printf("libfdt fdt_del_node(): %s\n",
299 fdt_strerror(err));
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400300 return err;
301 }
302 }
Kumar Gala804887e2008-02-15 03:34:36 -0600303
304 /********************************************************************
305 * Display header info
306 ********************************************************************/
307 } else if (argv[1][0] == 'h') {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500308 u32 version = fdt_version(working_fdt);
309 printf("magic:\t\t\t0x%x\n", fdt_magic(working_fdt));
310 printf("totalsize:\t\t0x%x (%d)\n", fdt_totalsize(working_fdt),
311 fdt_totalsize(working_fdt));
312 printf("off_dt_struct:\t\t0x%x\n",
313 fdt_off_dt_struct(working_fdt));
314 printf("off_dt_strings:\t\t0x%x\n",
315 fdt_off_dt_strings(working_fdt));
316 printf("off_mem_rsvmap:\t\t0x%x\n",
317 fdt_off_mem_rsvmap(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600318 printf("version:\t\t%d\n", version);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500319 printf("last_comp_version:\t%d\n",
320 fdt_last_comp_version(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600321 if (version >= 2)
322 printf("boot_cpuid_phys:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500323 fdt_boot_cpuid_phys(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600324 if (version >= 3)
325 printf("size_dt_strings:\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500326 fdt_size_dt_strings(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600327 if (version >= 17)
328 printf("size_dt_struct:\t\t0x%x\n",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500329 fdt_size_dt_struct(working_fdt));
330 printf("number mem_rsv:\t\t0x%x\n",
331 fdt_num_mem_rsv(working_fdt));
Kumar Gala804887e2008-02-15 03:34:36 -0600332 printf("\n");
333
334 /********************************************************************
335 * Set boot cpu id
336 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400337 } else if (strncmp(argv[1], "boo", 3) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600338 unsigned long tmp = simple_strtoul(argv[2], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500339 fdt_set_boot_cpuid_phys(working_fdt, tmp);
Kumar Gala804887e2008-02-15 03:34:36 -0600340
341 /********************************************************************
342 * memory command
343 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400344 } else if (strncmp(argv[1], "me", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600345 uint64_t addr, size;
346 int err;
347#ifdef CFG_64BIT_STRTOUL
348 addr = simple_strtoull(argv[2], NULL, 16);
349 size = simple_strtoull(argv[3], NULL, 16);
350#else
351 addr = simple_strtoul(argv[2], NULL, 16);
352 size = simple_strtoul(argv[3], NULL, 16);
353#endif
Kim Phillipse489b9c2008-06-10 11:06:17 -0500354 err = fdt_fixup_memory(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600355 if (err < 0)
356 return err;
357
358 /********************************************************************
359 * mem reserve commands
360 ********************************************************************/
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400361 } else if (strncmp(argv[1], "rs", 2) == 0) {
Kumar Gala804887e2008-02-15 03:34:36 -0600362 if (argv[2][0] == 'p') {
363 uint64_t addr, size;
Kim Phillipse489b9c2008-06-10 11:06:17 -0500364 int total = fdt_num_mem_rsv(working_fdt);
Kumar Gala804887e2008-02-15 03:34:36 -0600365 int j, err;
366 printf("index\t\t start\t\t size\n");
367 printf("-------------------------------"
368 "-----------------\n");
369 for (j = 0; j < total; j++) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500370 err = fdt_get_mem_rsv(working_fdt, j, &addr, &size);
Kumar Gala804887e2008-02-15 03:34:36 -0600371 if (err < 0) {
372 printf("libfdt fdt_get_mem_rsv(): %s\n",
373 fdt_strerror(err));
374 return err;
375 }
376 printf(" %x\t%08x%08x\t%08x%08x\n", j,
377 (u32)(addr >> 32),
378 (u32)(addr & 0xffffffff),
379 (u32)(size >> 32),
380 (u32)(size & 0xffffffff));
381 }
382 } else if (argv[2][0] == 'a') {
383 uint64_t addr, size;
384 int err;
385#ifdef CFG_64BIT_STRTOUL
386 addr = simple_strtoull(argv[3], NULL, 16);
387 size = simple_strtoull(argv[4], NULL, 16);
388#else
389 addr = simple_strtoul(argv[3], NULL, 16);
390 size = simple_strtoul(argv[4], NULL, 16);
391#endif
Kim Phillipse489b9c2008-06-10 11:06:17 -0500392 err = fdt_add_mem_rsv(working_fdt, addr, size);
Kumar Gala804887e2008-02-15 03:34:36 -0600393
394 if (err < 0) {
395 printf("libfdt fdt_add_mem_rsv(): %s\n",
396 fdt_strerror(err));
397 return err;
398 }
399 } else if (argv[2][0] == 'd') {
400 unsigned long idx = simple_strtoul(argv[3], NULL, 16);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500401 int err = fdt_del_mem_rsv(working_fdt, idx);
Kumar Gala804887e2008-02-15 03:34:36 -0600402
403 if (err < 0) {
404 printf("libfdt fdt_del_mem_rsv(): %s\n",
405 fdt_strerror(err));
406 return err;
407 }
408 } else {
409 /* Unrecognized command */
410 printf ("Usage:\n%s\n", cmdtp->usage);
411 return 1;
412 }
Kim Phillips99dffca2007-07-17 13:57:04 -0500413 }
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400414#ifdef CONFIG_OF_BOARD_SETUP
Kim Phillips99dffca2007-07-17 13:57:04 -0500415 /* Call the board-specific fixup routine */
Gerald Van Baren2fb698b2008-06-09 21:02:17 -0400416 else if (strncmp(argv[1], "boa", 3) == 0)
Kim Phillipse489b9c2008-06-10 11:06:17 -0500417 ft_board_setup(working_fdt, gd->bd);
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400418#endif
Kim Phillips99dffca2007-07-17 13:57:04 -0500419 /* Create a chosen node */
Kumar Galaf953d992008-08-15 08:24:34 -0500420 else if (argv[1][0] == 'c') {
421 unsigned long initrd_start = 0, initrd_end = 0;
422
423 if ((argc != 2) && (argc != 4)) {
424 printf ("Usage:\n%s\n", cmdtp->usage);
425 return 1;
426 }
427
428 if (argc == 4) {
429 initrd_start = simple_strtoul(argv[2], NULL, 16);
430 initrd_end = simple_strtoul(argv[3], NULL, 16);
431 }
432
433 fdt_chosen(working_fdt, initrd_start, initrd_end, 1);
434 } else {
Kim Phillips99dffca2007-07-17 13:57:04 -0500435 /* Unrecognized command */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400436 printf ("Usage:\n%s\n", cmdtp->usage);
437 return 1;
438 }
439
440 return 0;
441}
442
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400443/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400444
445static int fdt_valid(void)
446{
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400447 int err;
448
Kim Phillipse489b9c2008-06-10 11:06:17 -0500449 if (working_fdt == NULL) {
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400450 printf ("The address of the fdt is invalid (NULL).\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400451 return 0;
452 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400453
Kim Phillipse489b9c2008-06-10 11:06:17 -0500454 err = fdt_check_header(working_fdt);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400455 if (err == 0)
456 return 1; /* valid */
457
458 if (err < 0) {
Gerald Van Baren25114032007-05-12 09:47:25 -0400459 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400460 /*
461 * Be more informative on bad version.
462 */
463 if (err == -FDT_ERR_BADVERSION) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500464 if (fdt_version(working_fdt) <
465 FDT_FIRST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700466 printf (" - too old, fdt %d < %d",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500467 fdt_version(working_fdt),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400468 FDT_FIRST_SUPPORTED_VERSION);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500469 working_fdt = NULL;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400470 }
Kim Phillipse489b9c2008-06-10 11:06:17 -0500471 if (fdt_last_comp_version(working_fdt) >
472 FDT_LAST_SUPPORTED_VERSION) {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700473 printf (" - too new, fdt %d > %d",
Kim Phillipse489b9c2008-06-10 11:06:17 -0500474 fdt_version(working_fdt),
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400475 FDT_LAST_SUPPORTED_VERSION);
Kim Phillipse489b9c2008-06-10 11:06:17 -0500476 working_fdt = NULL;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400477 }
478 return 0;
479 }
480 printf("\n");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400481 return 0;
482 }
483 return 1;
484}
485
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400486/****************************************************************************/
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400487
488/*
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400489 * Parse the user's input, partially heuristic. Valid formats:
Andy Fleming4abd8442008-03-31 20:45:56 -0500490 * <0x00112233 4 05> - an array of cells. Numbers follow standard
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200491 * C conventions.
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400492 * [00 11 22 .. nn] - byte stream
493 * "string" - If the the value doesn't start with "<" or "[", it is
494 * treated as a string. Note that the quotes are
495 * stripped by the parser before we get the string.
Andy Fleming4abd8442008-03-31 20:45:56 -0500496 * newval: An array of strings containing the new property as specified
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200497 * on the command line
Andy Fleming4abd8442008-03-31 20:45:56 -0500498 * count: The number of strings in the array
499 * data: A bytestream to be placed in the property
500 * len: The length of the resulting bytestream
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400501 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500502static int fdt_parse_prop(char **newval, int count, char *data, int *len)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400503{
504 char *cp; /* temporary char pointer */
Andy Fleming4abd8442008-03-31 20:45:56 -0500505 char *newp; /* temporary newval char pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400506 unsigned long tmp; /* holds converted values */
Andy Fleming4abd8442008-03-31 20:45:56 -0500507 int stridx = 0;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400508
Andy Fleming4abd8442008-03-31 20:45:56 -0500509 *len = 0;
510 newp = newval[0];
511
512 /* An array of cells */
513 if (*newp == '<') {
514 newp++;
515 while ((*newp != '>') && (stridx < count)) {
516 /*
517 * Keep searching until we find that last ">"
518 * That way users don't have to escape the spaces
519 */
520 if (*newp == '\0') {
521 newp = newval[++stridx];
522 continue;
523 }
524
525 cp = newp;
526 tmp = simple_strtoul(cp, &newp, 0);
527 *(uint32_t *)data = __cpu_to_be32(tmp);
528 data += 4;
529 *len += 4;
530
531 /* If the ptr didn't advance, something went wrong */
532 if ((newp - cp) <= 0) {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400533 printf("Sorry, I could not convert \"%s\"\n",
534 cp);
535 return 1;
536 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500537
538 while (*newp == ' ')
539 newp++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400540 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500541
542 if (*newp != '>') {
543 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400544 return 1;
545 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500546 } else if (*newp == '[') {
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400547 /*
548 * Byte stream. Convert the values.
549 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500550 newp++;
551 while ((*newp != ']') && (stridx < count)) {
552 tmp = simple_strtoul(newp, &newp, 16);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400553 *data++ = tmp & 0xFF;
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400554 *len = *len + 1;
Andy Fleming4abd8442008-03-31 20:45:56 -0500555 while (*newp == ' ')
556 newp++;
557 if (*newp != '\0')
558 newp = newval[++stridx];
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400559 }
Andy Fleming4abd8442008-03-31 20:45:56 -0500560 if (*newp != ']') {
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700561 printf("Unexpected character '%c'\n", *newp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400562 return 1;
563 }
564 } else {
565 /*
566 * Assume it is a string. Copy it into our data area for
567 * convenience (including the terminating '\0').
568 */
Andy Fleming4abd8442008-03-31 20:45:56 -0500569 while (stridx < count) {
570 *len = strlen(newp) + 1;
571 strcpy(data, newp);
572 newp = newval[++stridx];
573 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400574 }
575 return 0;
576}
577
578/****************************************************************************/
579
580/*
581 * Heuristic to guess if this is a string or concatenated strings.
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400582 */
583
584static int is_printable_string(const void *data, int len)
585{
586 const char *s = data;
587
588 /* zero length is not */
589 if (len == 0)
590 return 0;
591
592 /* must terminate with zero */
593 if (s[len - 1] != '\0')
594 return 0;
595
596 /* printable or a null byte (concatenated strings) */
597 while (((*s == '\0') || isprint(*s)) && (len > 0)) {
598 /*
599 * If we see a null, there are three possibilities:
600 * 1) If len == 1, it is the end of the string, printable
601 * 2) Next character also a null, not printable.
602 * 3) Next character not a null, continue to check.
603 */
604 if (s[0] == '\0') {
605 if (len == 1)
606 return 1;
607 if (s[1] == '\0')
608 return 0;
609 }
610 s++;
611 len--;
612 }
613
614 /* Not the null termination, or not done yet: not printable */
615 if (*s != '\0' || (len != 0))
616 return 0;
617
618 return 1;
619}
620
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400621
622/*
623 * Print the property in the best format, a heuristic guess. Print as
624 * a string, concatenated strings, a byte, word, double word, or (if all
625 * else fails) it is printed as a stream of bytes.
626 */
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400627static void print_data(const void *data, int len)
628{
629 int j;
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400630
631 /* no data, don't print */
632 if (len == 0)
633 return;
634
635 /*
636 * It is a string, but it may have multiple strings (embedded '\0's).
637 */
638 if (is_printable_string(data, len)) {
639 puts("\"");
640 j = 0;
641 while (j < len) {
642 if (j > 0)
643 puts("\", \"");
644 puts(data);
645 j += strlen(data) + 1;
646 data += strlen(data) + 1;
647 }
648 puts("\"");
649 return;
650 }
651
Andy Fleming4abd8442008-03-31 20:45:56 -0500652 if ((len %4) == 0) {
653 const u32 *p;
654
655 printf("<");
656 for (j = 0, p = data; j < len/4; j ++)
657 printf("0x%x%s", p[j], j < (len/4 - 1) ? " " : "");
658 printf(">");
659 } else { /* anything else... hexdump */
660 const u8 *s;
661
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400662 printf("[");
663 for (j = 0, s = data; j < len; j++)
664 printf("%02x%s", s[j], j < len - 1 ? " " : "");
665 printf("]");
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400666 }
667}
668
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400669/****************************************************************************/
670
671/*
Kim Phillipse489b9c2008-06-10 11:06:17 -0500672 * Recursively print (a portion of) the working_fdt. The depth parameter
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400673 * determines how deeply nested the fdt is printed.
674 */
Kumar Galadbaf07c2007-11-21 14:07:46 -0600675static int fdt_print(const char *pathp, char *prop, int depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400676{
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400677 static char tabs[MAX_LEVEL+1] =
678 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
679 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
Kumar Galadbaf07c2007-11-21 14:07:46 -0600680 const void *nodep; /* property node pointer */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400681 int nodeoffset; /* node offset from libfdt */
682 int nextoffset; /* next node offset from libfdt */
683 uint32_t tag; /* tag */
684 int len; /* length of the property */
685 int level = 0; /* keep track of nesting level */
Gerald Van Baren91623522007-11-22 17:23:23 -0500686 const struct fdt_property *fdt_prop;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400687
Kim Phillipse489b9c2008-06-10 11:06:17 -0500688 nodeoffset = fdt_path_offset (working_fdt, pathp);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400689 if (nodeoffset < 0) {
690 /*
691 * Not found or something else bad happened.
692 */
Kumar Gala8d04f022007-10-24 11:04:22 -0500693 printf ("libfdt fdt_path_offset() returned %s\n",
Gerald Van Baren06e19a02007-05-21 23:27:16 -0400694 fdt_strerror(nodeoffset));
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400695 return 1;
696 }
697 /*
698 * The user passed in a property as well as node path.
699 * Print only the given property and then return.
700 */
701 if (prop) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500702 nodep = fdt_getprop (working_fdt, nodeoffset, prop, &len);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400703 if (len == 0) {
704 /* no property value */
705 printf("%s %s\n", pathp, prop);
706 return 0;
707 } else if (len > 0) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500708 printf("%s = ", prop);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400709 print_data (nodep, len);
710 printf("\n");
711 return 0;
712 } else {
713 printf ("libfdt fdt_getprop(): %s\n",
714 fdt_strerror(len));
715 return 1;
716 }
717 }
718
719 /*
720 * The user passed in a node path and no property,
721 * print the node and all subnodes.
722 */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400723 while(level >= 0) {
Kim Phillipse489b9c2008-06-10 11:06:17 -0500724 tag = fdt_next_tag(working_fdt, nodeoffset, &nextoffset);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400725 switch(tag) {
726 case FDT_BEGIN_NODE:
Kim Phillipse489b9c2008-06-10 11:06:17 -0500727 pathp = fdt_get_name(working_fdt, nodeoffset, NULL);
Gerald Van Baren91623522007-11-22 17:23:23 -0500728 if (level <= depth) {
729 if (pathp == NULL)
730 pathp = "/* NULL pointer error */";
731 if (*pathp == '\0')
732 pathp = "/"; /* root is nameless */
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400733 printf("%s%s {\n",
734 &tabs[MAX_LEVEL - level], pathp);
Gerald Van Baren91623522007-11-22 17:23:23 -0500735 }
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400736 level++;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400737 if (level >= MAX_LEVEL) {
Gerald Van Baren91623522007-11-22 17:23:23 -0500738 printf("Nested too deep, aborting.\n");
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400739 return 1;
740 }
741 break;
742 case FDT_END_NODE:
743 level--;
Gerald Van Baren91623522007-11-22 17:23:23 -0500744 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400745 printf("%s};\n", &tabs[MAX_LEVEL - level]);
746 if (level == 0) {
747 level = -1; /* exit the loop */
748 }
749 break;
750 case FDT_PROP:
Kim Phillipse489b9c2008-06-10 11:06:17 -0500751 fdt_prop = fdt_offset_ptr(working_fdt, nodeoffset,
Gerald Van Baren91623522007-11-22 17:23:23 -0500752 sizeof(*fdt_prop));
Kim Phillipse489b9c2008-06-10 11:06:17 -0500753 pathp = fdt_string(working_fdt,
Gerald Van Baren91623522007-11-22 17:23:23 -0500754 fdt32_to_cpu(fdt_prop->nameoff));
755 len = fdt32_to_cpu(fdt_prop->len);
756 nodep = fdt_prop->data;
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400757 if (len < 0) {
758 printf ("libfdt fdt_getprop(): %s\n",
759 fdt_strerror(len));
760 return 1;
761 } else if (len == 0) {
762 /* the property has no value */
Gerald Van Baren91623522007-11-22 17:23:23 -0500763 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400764 printf("%s%s;\n",
765 &tabs[MAX_LEVEL - level],
766 pathp);
767 } else {
Gerald Van Baren91623522007-11-22 17:23:23 -0500768 if (level <= depth) {
Gerald Van Baren28f384b2007-11-23 19:43:20 -0500769 printf("%s%s = ",
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400770 &tabs[MAX_LEVEL - level],
771 pathp);
772 print_data (nodep, len);
773 printf(";\n");
774 }
775 }
776 break;
777 case FDT_NOP:
Andrew Klossnerdc4b0b32008-07-07 06:41:14 -0700778 printf("%s/* NOP */\n", &tabs[MAX_LEVEL - level]);
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400779 break;
780 case FDT_END:
781 return 1;
782 default:
Gerald Van Baren91623522007-11-22 17:23:23 -0500783 if (level <= depth)
Gerald Van Barenaddd8ce2007-05-16 22:39:59 -0400784 printf("Unknown tag 0x%08X\n", tag);
785 return 1;
786 }
787 nodeoffset = nextoffset;
788 }
789 return 0;
790}
791
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400792/********************************************************************/
793
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400794U_BOOT_CMD(
Andy Fleming4abd8442008-03-31 20:45:56 -0500795 fdt, 255, 0, do_fdt,
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400796 "fdt - flattened device tree utility commands\n",
797 "addr <addr> [<length>] - Set the fdt location to <addr>\n"
Gerald Van Barenfd61e552007-06-25 23:25:28 -0400798#ifdef CONFIG_OF_BOARD_SETUP
799 "fdt boardsetup - Do board-specific set up\n"
800#endif
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500801 "fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400802 "fdt print <path> [<prop>] - Recursive print starting at <path>\n"
803 "fdt list <path> [<prop>] - Print one level starting at <path>\n"
804 "fdt set <path> <prop> [<val>] - Set <property> [to <val>]\n"
805 "fdt mknode <path> <node> - Create a new node after <path>\n"
806 "fdt rm <path> [<prop>] - Delete the node or <property>\n"
Kumar Gala804887e2008-02-15 03:34:36 -0600807 "fdt header - Display header info\n"
808 "fdt bootcpu <id> - Set boot cpuid\n"
809 "fdt memory <addr> <size> - Add/Update memory node\n"
810 "fdt rsvmem print - Show current mem reserves\n"
811 "fdt rsvmem add <addr> <size> - Add a mem reserve\n"
812 "fdt rsvmem delete <index> - Delete a mem reserves\n"
Kumar Galaf953d992008-08-15 08:24:34 -0500813 "fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree\n"
814 " <start>/<end> - initrd start/end addr\n"
Gerald Van Baren238cb7a2008-01-05 15:33:29 -0500815 "NOTE: If the path or property you are setting/printing has a '#' character\n"
816 " or spaces, you MUST escape it with a \\ character or quote it with \".\n"
Gerald Van Baren781e09e2007-03-31 12:22:10 -0400817);