]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/boot/fdt/fdt_loader_cmd.c
MFC 273927:
[FreeBSD/stable/10.git] / sys / boot / fdt / fdt_loader_cmd.c
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Semihalf under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <stand.h>
34 #include <fdt.h>
35 #include <libfdt.h>
36 #include <sys/param.h>
37 #include <sys/linker.h>
38 #include <machine/elf.h>
39
40 #include "bootstrap.h"
41 #include "fdt_platform.h"
42
43 #ifdef DEBUG
44 #define debugf(fmt, args...) do { printf("%s(): ", __func__);   \
45     printf(fmt,##args); } while (0)
46 #else
47 #define debugf(fmt, args...)
48 #endif
49
50 #define FDT_CWD_LEN     256
51 #define FDT_MAX_DEPTH   6
52
53 #define FDT_PROP_SEP    " = "
54
55 #define COPYOUT(s,d,l)  archsw.arch_copyout(s, d, l)
56 #define COPYIN(s,d,l)   archsw.arch_copyin(s, d, l)
57
58 #define FDT_STATIC_DTB_SYMBOL   "fdt_static_dtb"
59
60 #define CMD_REQUIRES_BLOB       0x01
61
62 /* Location of FDT yet to be loaded. */
63 /* This may be in read-only memory, so can't be manipulated directly. */
64 static struct fdt_header *fdt_to_load = NULL;
65 /* Location of FDT on heap. */
66 /* This is the copy we actually manipulate. */
67 static struct fdt_header *fdtp = NULL;
68 /* Size of FDT blob */
69 static size_t fdtp_size = 0;
70 /* Location of FDT in kernel or module. */
71 /* This won't be set if FDT is loaded from disk or memory. */
72 /* If it is set, we'll update it when fdt_copy() gets called. */
73 static vm_offset_t fdtp_va = 0;
74
75 static int fdt_load_dtb(vm_offset_t va);
76
77 static int fdt_cmd_nyi(int argc, char *argv[]);
78
79 static int fdt_cmd_addr(int argc, char *argv[]);
80 static int fdt_cmd_mkprop(int argc, char *argv[]);
81 static int fdt_cmd_cd(int argc, char *argv[]);
82 static int fdt_cmd_hdr(int argc, char *argv[]);
83 static int fdt_cmd_ls(int argc, char *argv[]);
84 static int fdt_cmd_prop(int argc, char *argv[]);
85 static int fdt_cmd_pwd(int argc, char *argv[]);
86 static int fdt_cmd_rm(int argc, char *argv[]);
87 static int fdt_cmd_mknode(int argc, char *argv[]);
88 static int fdt_cmd_mres(int argc, char *argv[]);
89
90 typedef int cmdf_t(int, char *[]);
91
92 struct cmdtab {
93         const char      *name;
94         cmdf_t          *handler;
95         int             flags;
96 };
97
98 static const struct cmdtab commands[] = {
99         { "addr", &fdt_cmd_addr,        0 },
100         { "alias", &fdt_cmd_nyi,        0 },
101         { "cd", &fdt_cmd_cd,            CMD_REQUIRES_BLOB },
102         { "header", &fdt_cmd_hdr,       CMD_REQUIRES_BLOB },
103         { "ls", &fdt_cmd_ls,            CMD_REQUIRES_BLOB },
104         { "mknode", &fdt_cmd_mknode,    CMD_REQUIRES_BLOB },
105         { "mkprop", &fdt_cmd_mkprop,    CMD_REQUIRES_BLOB },
106         { "mres", &fdt_cmd_mres,        CMD_REQUIRES_BLOB },
107         { "prop", &fdt_cmd_prop,        CMD_REQUIRES_BLOB },
108         { "pwd", &fdt_cmd_pwd,          CMD_REQUIRES_BLOB },
109         { "rm", &fdt_cmd_rm,            CMD_REQUIRES_BLOB },
110         { NULL, NULL }
111 };
112
113 static char cwd[FDT_CWD_LEN] = "/";
114
115 static vm_offset_t
116 fdt_find_static_dtb()
117 {
118         Elf_Ehdr *ehdr;
119         Elf_Shdr *shdr;
120         Elf_Sym sym;
121         vm_offset_t strtab, symtab, fdt_start;
122         uint64_t offs;
123         struct preloaded_file *kfp;
124         struct file_metadata *md;
125         char *strp;
126         int i, sym_count;
127
128         debugf("fdt_find_static_dtb()\n");
129
130         sym_count = symtab = strtab = 0;
131         strp = NULL;
132
133         offs = __elfN(relocation_offset);
134
135         kfp = file_findfile(NULL, NULL);
136         if (kfp == NULL)
137                 return (0);
138
139         /* Locate the dynamic symbols and strtab. */
140         md = file_findmetadata(kfp, MODINFOMD_ELFHDR);
141         if (md == NULL)
142                 return (0);
143         ehdr = (Elf_Ehdr *)md->md_data;
144
145         md = file_findmetadata(kfp, MODINFOMD_SHDR);
146         if (md == NULL)
147                 return (0);
148         shdr = (Elf_Shdr *)md->md_data;
149
150         for (i = 0; i < ehdr->e_shnum; ++i) {
151                 if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) {
152                         symtab = shdr[i].sh_addr + offs;
153                         sym_count = shdr[i].sh_size / sizeof(Elf_Sym);
154                 } else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) {
155                         strtab = shdr[i].sh_addr + offs;
156                 }
157         }
158
159         /*
160          * The most efficent way to find a symbol would be to calculate a
161          * hash, find proper bucket and chain, and thus find a symbol.
162          * However, that would involve code duplication (e.g. for hash
163          * function). So we're using simpler and a bit slower way: we're
164          * iterating through symbols, searching for the one which name is
165          * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit,
166          * we are eliminating symbols type of which is not STT_NOTYPE, or(and)
167          * those which binding attribute is not STB_GLOBAL.
168          */
169         fdt_start = 0;
170         while (sym_count > 0 && fdt_start == 0) {
171                 COPYOUT(symtab, &sym, sizeof(sym));
172                 symtab += sizeof(sym);
173                 --sym_count;
174                 if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
175                     ELF_ST_TYPE(sym.st_info) != STT_NOTYPE)
176                         continue;
177                 strp = strdupout(strtab + sym.st_name);
178                 if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0)
179                         fdt_start = (vm_offset_t)sym.st_value + offs;
180                 free(strp);
181         }
182         return (fdt_start);
183 }
184
185 static int
186 fdt_load_dtb(vm_offset_t va)
187 {
188         struct fdt_header header;
189         int err;
190
191         debugf("fdt_load_dtb(0x%08jx)\n", (uintmax_t)va);
192
193         COPYOUT(va, &header, sizeof(header));
194         err = fdt_check_header(&header);
195         if (err < 0) {
196                 if (err == -FDT_ERR_BADVERSION)
197                         sprintf(command_errbuf,
198                             "incompatible blob version: %d, should be: %d",
199                             fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
200
201                 else
202                         sprintf(command_errbuf, "error validating blob: %s",
203                             fdt_strerror(err));
204                 return (1);
205         }
206
207         /*
208          * Release previous blob
209          */
210         if (fdtp)
211                 free(fdtp);
212
213         fdtp_size = fdt_totalsize(&header);
214         fdtp = malloc(fdtp_size);
215
216         if (fdtp == NULL) {
217                 command_errmsg = "can't allocate memory for device tree copy";
218                 return (1);
219         }
220
221         fdtp_va = va;
222         COPYOUT(va, fdtp, fdtp_size);
223         debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
224
225         return (0);
226 }
227
228 int
229 fdt_load_dtb_addr(struct fdt_header *header)
230 {
231         int err;
232
233         debugf("fdt_load_dtb_addr(0x%p)\n", header);
234
235         fdtp_size = fdt_totalsize(header);
236         err = fdt_check_header(header);
237         if (err < 0) {
238                 sprintf(command_errbuf, "error validating blob: %s",
239                     fdt_strerror(err));
240                 return (err);
241         }
242         free(fdtp);
243         if ((fdtp = malloc(fdtp_size)) == NULL) {
244                 command_errmsg = "can't allocate memory for device tree copy";
245                 return (1);
246         }
247
248         fdtp_va = 0; // Don't write this back into module or kernel.
249         bcopy(header, fdtp, fdtp_size);
250         return (0);
251 }
252
253 int
254 fdt_load_dtb_file(const char * filename)
255 {
256         struct preloaded_file *bfp, *oldbfp;
257         int err;
258
259         debugf("fdt_load_dtb_file(%s)\n", filename);
260
261         oldbfp = file_findfile(NULL, "dtb");
262
263         /* Attempt to load and validate a new dtb from a file. */
264         if ((bfp = file_loadraw(filename, "dtb")) == NULL) {
265                 sprintf(command_errbuf, "failed to load file '%s'", filename);
266                 return (1);
267         }
268         if ((err = fdt_load_dtb(bfp->f_addr)) != 0) {
269                 file_discard(bfp);
270                 return (err);
271         }
272
273         /* A new dtb was validated, discard any previous file. */
274         if (oldbfp)
275                 file_discard(oldbfp);
276         return (0);
277 }
278
279 int
280 fdt_setup_fdtp()
281 {
282         struct preloaded_file *bfp;
283         vm_offset_t va;
284         
285         debugf("fdt_setup_fdtp()\n");
286
287         /* If we already loaded a file, use it. */
288         if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
289                 if (fdt_load_dtb(bfp->f_addr) == 0) {
290                         printf("Using DTB from loaded file '%s'.\n", 
291                             bfp->f_name);
292                         return (0);
293                 }
294         }
295
296         /* If we were given the address of a valid blob in memory, use it. */
297         if (fdt_to_load != NULL) {
298                 if (fdt_load_dtb_addr(fdt_to_load) == 0) {
299                         printf("Using DTB from memory address 0x%08X.\n",
300                             (unsigned int)fdt_to_load);
301                         return (0);
302                 }
303         }
304
305         if (fdt_platform_load_dtb() == 0)
306                 return (0);
307
308         /* If there is a dtb compiled into the kernel, use it. */
309         if ((va = fdt_find_static_dtb()) != 0) {
310                 if (fdt_load_dtb(va) == 0) {
311                         printf("Using DTB compiled into kernel.\n");
312                         return (0);
313                 }
314         }
315         
316         command_errmsg = "No device tree blob found!\n";
317         return (1);
318 }
319
320 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
321     (cellbuf), (lim), (cellsize), 0);
322
323 /* Force using base 16 */
324 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
325     (cellbuf), (lim), (cellsize), 16);
326
327 static int
328 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize,
329     uint8_t base)
330 {
331         const char *buf = str;
332         const char *end = str + strlen(str) - 2;
333         uint32_t *u32buf = NULL;
334         uint8_t *u8buf = NULL;
335         int cnt = 0;
336
337         if (cellsize == sizeof(uint32_t))
338                 u32buf = (uint32_t *)cellbuf;
339         else
340                 u8buf = (uint8_t *)cellbuf;
341
342         if (lim == 0)
343                 return (0);
344
345         while (buf < end) {
346
347                 /* Skip white whitespace(s)/separators */
348                 while (!isxdigit(*buf) && buf < end)
349                         buf++;
350
351                 if (u32buf != NULL)
352                         u32buf[cnt] =
353                             cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
354
355                 else
356                         u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
357
358                 if (cnt + 1 <= lim - 1)
359                         cnt++;
360                 else
361                         break;
362                 buf++;
363                 /* Find another number */
364                 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
365                         buf++;
366         }
367         return (cnt);
368 }
369
370 void
371 fdt_fixup_ethernet(const char *str, char *ethstr, int len)
372 {
373         uint8_t tmp_addr[6];
374
375         /* Convert macaddr string into a vector of uints */
376         fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
377         /* Set actual property to a value from vect */
378         fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
379             "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
380 }
381
382 void
383 fdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
384 {
385         int lo, o = 0, o2, maxo = 0, depth;
386         const uint32_t zero = 0;
387
388         /* We want to modify every subnode of /cpus */
389         o = fdt_path_offset(fdtp, "/cpus");
390         if (o < 0)
391                 return;
392
393         /* maxo should contain offset of node next to /cpus */
394         depth = 0;
395         maxo = o;
396         while (depth != -1)
397                 maxo = fdt_next_node(fdtp, maxo, &depth);
398
399         /* Find CPU frequency properties */
400         o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
401             &zero, sizeof(uint32_t));
402
403         o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
404             sizeof(uint32_t));
405
406         lo = MIN(o, o2);
407
408         while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
409
410                 o = fdt_node_offset_by_prop_value(fdtp, lo,
411                     "clock-frequency", &zero, sizeof(uint32_t));
412
413                 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
414                     &zero, sizeof(uint32_t));
415
416                 /* We're only interested in /cpus subnode(s) */
417                 if (lo > maxo)
418                         break;
419
420                 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
421                     (uint32_t)cpufreq);
422
423                 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
424                     (uint32_t)busfreq);
425
426                 lo = MIN(o, o2);
427         }
428 }
429
430 static int
431 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
432 {
433         int cells_in_tuple, i, tuples, tuple_size;
434         uint32_t cur_start, cur_size;
435
436         cells_in_tuple = (addr_cells + size_cells);
437         tuple_size = cells_in_tuple * sizeof(uint32_t);
438         tuples = len / tuple_size;
439         if (tuples == 0)
440                 return (EINVAL);
441
442         for (i = 0; i < tuples; i++) {
443                 if (addr_cells == 2)
444                         cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
445                 else
446                         cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
447
448                 if (size_cells == 2)
449                         cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
450                 else
451                         cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
452
453                 if (cur_size == 0)
454                         return (EINVAL);
455
456                 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
457                     i, cur_start, cur_size);
458         }
459         return (0);
460 }
461
462 void
463 fdt_fixup_memory(struct fdt_mem_region *region, size_t num)
464 {
465         struct fdt_mem_region *curmr;
466         uint32_t addr_cells, size_cells;
467         uint32_t *addr_cellsp, *reg,  *size_cellsp;
468         int err, i, len, memory, root;
469         size_t realmrno;
470         uint8_t *buf, *sb;
471         uint64_t rstart, rsize;
472         int reserved;
473
474         root = fdt_path_offset(fdtp, "/");
475         if (root < 0) {
476                 sprintf(command_errbuf, "Could not find root node !");
477                 return;
478         }
479
480         memory = fdt_path_offset(fdtp, "/memory");
481         if (memory <= 0) {
482                 /* Create proper '/memory' node. */
483                 memory = fdt_add_subnode(fdtp, root, "memory");
484                 if (memory <= 0) {
485                         sprintf(command_errbuf, "Could not fixup '/memory' "
486                             "node, error code : %d!\n", memory);
487                         return;
488                 }
489
490                 err = fdt_setprop(fdtp, memory, "device_type", "memory",
491                     sizeof("memory"));
492
493                 if (err < 0)
494                         return;
495         }
496
497         addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
498             NULL);
499         size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
500
501         if (addr_cellsp == NULL || size_cellsp == NULL) {
502                 sprintf(command_errbuf, "Could not fixup '/memory' node : "
503                     "%s %s property not found in root node!\n",
504                     (!addr_cellsp) ? "#address-cells" : "",
505                     (!size_cellsp) ? "#size-cells" : "");
506                 return;
507         }
508
509         addr_cells = fdt32_to_cpu(*addr_cellsp);
510         size_cells = fdt32_to_cpu(*size_cellsp);
511
512         /*
513          * Convert memreserve data to memreserve property
514          * Check if property already exists
515          */
516         reserved = fdt_num_mem_rsv(fdtp);
517         if (reserved &&
518             (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
519                 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
520                 sb = buf = (uint8_t *)malloc(len);
521                 if (!buf)
522                         return;
523
524                 bzero(buf, len);
525
526                 for (i = 0; i < reserved; i++) {
527                         if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
528                                 break;
529                         if (rsize) {
530                                 /* Ensure endianess, and put cells into a buffer */
531                                 if (addr_cells == 2)
532                                         *(uint64_t *)buf =
533                                             cpu_to_fdt64(rstart);
534                                 else
535                                         *(uint32_t *)buf =
536                                             cpu_to_fdt32(rstart);
537
538                                 buf += sizeof(uint32_t) * addr_cells;
539                                 if (size_cells == 2)
540                                         *(uint64_t *)buf =
541                                             cpu_to_fdt64(rsize);
542                                 else
543                                         *(uint32_t *)buf =
544                                             cpu_to_fdt32(rsize);
545
546                                 buf += sizeof(uint32_t) * size_cells;
547                         }
548                 }
549
550                 /* Set property */
551                 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
552                         printf("Could not fixup 'memreserve' property.\n");
553
554                 free(sb);
555         } 
556
557         /* Count valid memory regions entries in sysinfo. */
558         realmrno = num;
559         for (i = 0; i < num; i++)
560                 if (region[i].start == 0 && region[i].size == 0)
561                         realmrno--;
562
563         if (realmrno == 0) {
564                 sprintf(command_errbuf, "Could not fixup '/memory' node : "
565                     "sysinfo doesn't contain valid memory regions info!\n");
566                 return;
567         }
568
569         if ((reg = (uint32_t *)fdt_getprop(fdtp, memory, "reg",
570             &len)) != NULL) {
571
572                 if (fdt_reg_valid(reg, len, addr_cells, size_cells) == 0)
573                         /*
574                          * Do not apply fixup if existing 'reg' property
575                          * seems to be valid.
576                          */
577                         return;
578         }
579
580         len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
581         sb = buf = (uint8_t *)malloc(len);
582         if (!buf)
583                 return;
584
585         bzero(buf, len);
586
587         for (i = 0; i < num; i++) {
588                 curmr = &region[i];
589                 if (curmr->size != 0) {
590                         /* Ensure endianess, and put cells into a buffer */
591                         if (addr_cells == 2)
592                                 *(uint64_t *)buf =
593                                     cpu_to_fdt64(curmr->start);
594                         else
595                                 *(uint32_t *)buf =
596                                     cpu_to_fdt32(curmr->start);
597
598                         buf += sizeof(uint32_t) * addr_cells;
599                         if (size_cells == 2)
600                                 *(uint64_t *)buf =
601                                     cpu_to_fdt64(curmr->size);
602                         else
603                                 *(uint32_t *)buf =
604                                     cpu_to_fdt32(curmr->size);
605
606                         buf += sizeof(uint32_t) * size_cells;
607                 }
608         }
609
610         /* Set property */
611         if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
612                 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
613
614         free(sb);
615 }
616
617 void
618 fdt_fixup_stdout(const char *str)
619 {
620         char *ptr;
621         int serialno;
622         int len, no, sero;
623         const struct fdt_property *prop;
624         char *tmp[10];
625
626         ptr = (char *)str + strlen(str) - 1;
627         while (ptr > str && isdigit(*(str - 1)))
628                 str--;
629
630         if (ptr == str)
631                 return;
632
633         serialno = (int)strtol(ptr, NULL, 0);
634         no = fdt_path_offset(fdtp, "/chosen");
635         if (no < 0)
636                 return;
637
638         prop = fdt_get_property(fdtp, no, "stdout", &len);
639
640         /* If /chosen/stdout does not extist, create it */
641         if (prop == NULL || (prop != NULL && len == 0)) {
642
643                 bzero(tmp, 10 * sizeof(char));
644                 strcpy((char *)&tmp, "serial");
645                 if (strlen(ptr) > 3)
646                         /* Serial number too long */
647                         return;
648
649                 strncpy((char *)tmp + 6, ptr, 3);
650                 sero = fdt_path_offset(fdtp, (const char *)tmp);
651                 if (sero < 0)
652                         /*
653                          * If serial device we're trying to assign
654                          * stdout to doesn't exist in DT -- return.
655                          */
656                         return;
657
658                 fdt_setprop(fdtp, no, "stdout", &tmp,
659                     strlen((char *)&tmp) + 1);
660                 fdt_setprop(fdtp, no, "stdin", &tmp,
661                     strlen((char *)&tmp) + 1);
662         }
663 }
664
665 /*
666  * Locate the blob, fix it up and return its location.
667  */
668 static int
669 fdt_fixup(void)
670 {
671         int chosen, len;
672
673         len = 0;
674
675         debugf("fdt_fixup()\n");
676
677         if (fdtp == NULL && fdt_setup_fdtp() != 0)
678                 return (0);
679
680         /* Create /chosen node (if not exists) */
681         if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
682             -FDT_ERR_NOTFOUND)
683                 chosen = fdt_add_subnode(fdtp, 0, "chosen");
684
685         /* Value assigned to fixup-applied does not matter. */
686         if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
687                 return (1);
688
689         fdt_platform_fixups();
690
691         fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
692         return (1);
693 }
694
695 /*
696  * Copy DTB blob to specified location and return size
697  */
698 int
699 fdt_copy(vm_offset_t va)
700 {
701         int err;
702         debugf("fdt_copy va 0x%08x\n", va);
703         if (fdtp == NULL) {
704                 err = fdt_setup_fdtp();
705                 if (err) {
706                         printf("No valid device tree blob found!\n");
707                         return (0);
708                 }
709         }
710
711         if (fdt_fixup() == 0)
712                 return (0);
713
714         if (fdtp_va != 0) {
715                 /* Overwrite the FDT with the fixed version. */
716                 /* XXX Is this really appropriate? */
717                 COPYIN(fdtp, fdtp_va, fdtp_size);
718         }
719         COPYIN(fdtp, va, fdtp_size);
720         return (fdtp_size);
721 }
722
723
724
725 int
726 command_fdt_internal(int argc, char *argv[])
727 {
728         cmdf_t *cmdh;
729         int flags;
730         char *cmd;
731         int i, err;
732
733         if (argc < 2) {
734                 command_errmsg = "usage is 'fdt <command> [<args>]";
735                 return (CMD_ERROR);
736         }
737
738         /*
739          * Validate fdt <command>.
740          */
741         cmd = strdup(argv[1]);
742         i = 0;
743         cmdh = NULL;
744         while (!(commands[i].name == NULL)) {
745                 if (strcmp(cmd, commands[i].name) == 0) {
746                         /* found it */
747                         cmdh = commands[i].handler;
748                         flags = commands[i].flags;
749                         break;
750                 }
751                 i++;
752         }
753         if (cmdh == NULL) {
754                 command_errmsg = "unknown command";
755                 return (CMD_ERROR);
756         }
757
758         if (flags & CMD_REQUIRES_BLOB) {
759                 /*
760                  * Check if uboot env vars were parsed already. If not, do it now.
761                  */
762                 if (fdt_fixup() == 0)
763                         return (CMD_ERROR);
764         }
765
766         /*
767          * Call command handler.
768          */
769         err = (*cmdh)(argc, argv);
770
771         return (err);
772 }
773
774 static int
775 fdt_cmd_addr(int argc, char *argv[])
776 {
777         struct preloaded_file *fp;
778         struct fdt_header *hdr;
779         const char *addr;
780         char *cp;
781
782         fdt_to_load = NULL;
783
784         if (argc > 2)
785                 addr = argv[2];
786         else {
787                 sprintf(command_errbuf, "no address specified");
788                 return (CMD_ERROR);
789         }
790
791         hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
792         if (cp == addr) {
793                 sprintf(command_errbuf, "Invalid address: %s", addr);
794                 return (CMD_ERROR);
795         }
796
797         while ((fp = file_findfile(NULL, "dtb")) != NULL) {
798                 file_discard(fp);
799         }
800
801         fdt_to_load = hdr;
802         return (CMD_OK);
803 }
804
805 static int
806 fdt_cmd_cd(int argc, char *argv[])
807 {
808         char *path;
809         char tmp[FDT_CWD_LEN];
810         int len, o;
811
812         path = (argc > 2) ? argv[2] : "/";
813
814         if (path[0] == '/') {
815                 len = strlen(path);
816                 if (len >= FDT_CWD_LEN)
817                         goto fail;
818         } else {
819                 /* Handle path specification relative to cwd */
820                 len = strlen(cwd) + strlen(path) + 1;
821                 if (len >= FDT_CWD_LEN)
822                         goto fail;
823
824                 strcpy(tmp, cwd);
825                 strcat(tmp, "/");
826                 strcat(tmp, path);
827                 path = tmp;
828         }
829
830         o = fdt_path_offset(fdtp, path);
831         if (o < 0) {
832                 sprintf(command_errbuf, "could not find node: '%s'", path);
833                 return (CMD_ERROR);
834         }
835
836         strcpy(cwd, path);
837         return (CMD_OK);
838
839 fail:
840         sprintf(command_errbuf, "path too long: %d, max allowed: %d",
841             len, FDT_CWD_LEN - 1);
842         return (CMD_ERROR);
843 }
844
845 static int
846 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
847 {
848         char line[80];
849         int ver;
850
851         if (fdtp == NULL) {
852                 command_errmsg = "no device tree blob pointer?!";
853                 return (CMD_ERROR);
854         }
855
856         ver = fdt_version(fdtp);
857         pager_open();
858         sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
859         pager_output(line);
860         sprintf(line, " magic                   = 0x%08x\n", fdt_magic(fdtp));
861         pager_output(line);
862         sprintf(line, " size                    = %d\n", fdt_totalsize(fdtp));
863         pager_output(line);
864         sprintf(line, " off_dt_struct           = 0x%08x\n",
865             fdt_off_dt_struct(fdtp));
866         pager_output(line);
867         sprintf(line, " off_dt_strings          = 0x%08x\n",
868             fdt_off_dt_strings(fdtp));
869         pager_output(line);
870         sprintf(line, " off_mem_rsvmap          = 0x%08x\n",
871             fdt_off_mem_rsvmap(fdtp));
872         pager_output(line);
873         sprintf(line, " version                 = %d\n", ver); 
874         pager_output(line);
875         sprintf(line, " last compatible version = %d\n",
876             fdt_last_comp_version(fdtp));
877         pager_output(line);
878         if (ver >= 2) {
879                 sprintf(line, " boot_cpuid              = %d\n",
880                     fdt_boot_cpuid_phys(fdtp));
881                 pager_output(line);
882         }
883         if (ver >= 3) {
884                 sprintf(line, " size_dt_strings         = %d\n",
885                     fdt_size_dt_strings(fdtp));
886                 pager_output(line);
887         }
888         if (ver >= 17) {
889                 sprintf(line, " size_dt_struct          = %d\n",
890                     fdt_size_dt_struct(fdtp));
891                 pager_output(line);
892         }
893         pager_close();
894
895         return (CMD_OK);
896 }
897
898 static int
899 fdt_cmd_ls(int argc, char *argv[])
900 {
901         const char *prevname[FDT_MAX_DEPTH] = { NULL };
902         const char *name;
903         char *path;
904         int i, o, depth, len;
905
906         path = (argc > 2) ? argv[2] : NULL;
907         if (path == NULL)
908                 path = cwd;
909
910         o = fdt_path_offset(fdtp, path);
911         if (o < 0) {
912                 sprintf(command_errbuf, "could not find node: '%s'", path);
913                 return (CMD_ERROR);
914         }
915
916         for (depth = 0;
917             (o >= 0) && (depth >= 0);
918             o = fdt_next_node(fdtp, o, &depth)) {
919
920                 name = fdt_get_name(fdtp, o, &len);
921
922                 if (depth > FDT_MAX_DEPTH) {
923                         printf("max depth exceeded: %d\n", depth);
924                         continue;
925                 }
926
927                 prevname[depth] = name;
928
929                 /* Skip root (i = 1) when printing devices */
930                 for (i = 1; i <= depth; i++) {
931                         if (prevname[i] == NULL)
932                                 break;
933
934                         if (strcmp(cwd, "/") == 0)
935                                 printf("/");
936                         printf("%s", prevname[i]);
937                 }
938                 printf("\n");
939         }
940
941         return (CMD_OK);
942 }
943
944 static __inline int
945 isprint(int c)
946 {
947
948         return (c >= ' ' && c <= 0x7e);
949 }
950
951 static int
952 fdt_isprint(const void *data, int len, int *count)
953 {
954         const char *d;
955         char ch;
956         int yesno, i;
957
958         if (len == 0)
959                 return (0);
960
961         d = (const char *)data;
962         if (d[len - 1] != '\0')
963                 return (0);
964
965         *count = 0;
966         yesno = 1;
967         for (i = 0; i < len; i++) {
968                 ch = *(d + i);
969                 if (isprint(ch) || (ch == '\0' && i > 0)) {
970                         /* Count strings */
971                         if (ch == '\0')
972                                 (*count)++;
973                         continue;
974                 }
975
976                 yesno = 0;
977                 break;
978         }
979
980         return (yesno);
981 }
982
983 static int
984 fdt_data_str(const void *data, int len, int count, char **buf)
985 {
986         char *b, *tmp;
987         const char *d;
988         int buf_len, i, l;
989
990         /*
991          * Calculate the length for the string and allocate memory.
992          *
993          * Note that 'len' already includes at least one terminator.
994          */
995         buf_len = len;
996         if (count > 1) {
997                 /*
998                  * Each token had already a terminator buried in 'len', but we
999                  * only need one eventually, don't count space for these.
1000                  */
1001                 buf_len -= count - 1;
1002
1003                 /* Each consecutive token requires a ", " separator. */
1004                 buf_len += count * 2;
1005         }
1006
1007         /* Add some space for surrounding double quotes. */
1008         buf_len += count * 2;
1009
1010         /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1011         b = (char *)malloc(buf_len);
1012         tmp = (char *)malloc(buf_len);
1013         if (b == NULL)
1014                 goto error;
1015
1016         if (tmp == NULL) {
1017                 free(b);
1018                 goto error;
1019         }
1020
1021         b[0] = '\0';
1022
1023         /*
1024          * Now that we have space, format the string.
1025          */
1026         i = 0;
1027         do {
1028                 d = (const char *)data + i;
1029                 l = strlen(d) + 1;
1030
1031                 sprintf(tmp, "\"%s\"%s", d,
1032                     (i + l) < len ?  ", " : "");
1033                 strcat(b, tmp);
1034
1035                 i += l;
1036
1037         } while (i < len);
1038         *buf = b;
1039
1040         free(tmp);
1041
1042         return (0);
1043 error:
1044         return (1);
1045 }
1046
1047 static int
1048 fdt_data_cell(const void *data, int len, char **buf)
1049 {
1050         char *b, *tmp;
1051         const uint32_t *c;
1052         int count, i, l;
1053
1054         /* Number of cells */
1055         count = len / 4;
1056
1057         /*
1058          * Calculate the length for the string and allocate memory.
1059          */
1060
1061         /* Each byte translates to 2 output characters */
1062         l = len * 2;
1063         if (count > 1) {
1064                 /* Each consecutive cell requires a " " separator. */
1065                 l += (count - 1) * 1;
1066         }
1067         /* Each cell will have a "0x" prefix */
1068         l += count * 2;
1069         /* Space for surrounding <> and terminator */
1070         l += 3;
1071
1072         b = (char *)malloc(l);
1073         tmp = (char *)malloc(l);
1074         if (b == NULL)
1075                 goto error;
1076
1077         if (tmp == NULL) {
1078                 free(b);
1079                 goto error;
1080         }
1081
1082         b[0] = '\0';
1083         strcat(b, "<");
1084
1085         for (i = 0; i < len; i += 4) {
1086                 c = (const uint32_t *)((const uint8_t *)data + i);
1087                 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1088                     i < (len - 4) ? " " : "");
1089                 strcat(b, tmp);
1090         }
1091         strcat(b, ">");
1092         *buf = b;
1093
1094         free(tmp);
1095
1096         return (0);
1097 error:
1098         return (1);
1099 }
1100
1101 static int
1102 fdt_data_bytes(const void *data, int len, char **buf)
1103 {
1104         char *b, *tmp;
1105         const char *d;
1106         int i, l;
1107
1108         /*
1109          * Calculate the length for the string and allocate memory.
1110          */
1111
1112         /* Each byte translates to 2 output characters */
1113         l = len * 2;
1114         if (len > 1)
1115                 /* Each consecutive byte requires a " " separator. */
1116                 l += (len - 1) * 1;
1117         /* Each byte will have a "0x" prefix */
1118         l += len * 2;
1119         /* Space for surrounding [] and terminator. */
1120         l += 3;
1121
1122         b = (char *)malloc(l);
1123         tmp = (char *)malloc(l);
1124         if (b == NULL)
1125                 goto error;
1126
1127         if (tmp == NULL) {
1128                 free(b);
1129                 goto error;
1130         }
1131
1132         b[0] = '\0';
1133         strcat(b, "[");
1134
1135         for (i = 0, d = data; i < len; i++) {
1136                 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1137                 strcat(b, tmp);
1138         }
1139         strcat(b, "]");
1140         *buf = b;
1141
1142         free(tmp);
1143
1144         return (0);
1145 error:
1146         return (1);
1147 }
1148
1149 static int
1150 fdt_data_fmt(const void *data, int len, char **buf)
1151 {
1152         int count;
1153
1154         if (len == 0) {
1155                 *buf = NULL;
1156                 return (1);
1157         }
1158
1159         if (fdt_isprint(data, len, &count))
1160                 return (fdt_data_str(data, len, count, buf));
1161
1162         else if ((len % 4) == 0)
1163                 return (fdt_data_cell(data, len, buf));
1164
1165         else
1166                 return (fdt_data_bytes(data, len, buf));
1167 }
1168
1169 static int
1170 fdt_prop(int offset)
1171 {
1172         char *line, *buf;
1173         const struct fdt_property *prop;
1174         const char *name;
1175         const void *data;
1176         int len, rv;
1177
1178         line = NULL;
1179         prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1180         if (prop == NULL)
1181                 return (1);
1182
1183         name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1184         len = fdt32_to_cpu(prop->len);
1185
1186         rv = 0;
1187         buf = NULL;
1188         if (len == 0) {
1189                 /* Property without value */
1190                 line = (char *)malloc(strlen(name) + 2);
1191                 if (line == NULL) {
1192                         rv = 2;
1193                         goto out2;
1194                 }
1195                 sprintf(line, "%s\n", name);
1196                 goto out1;
1197         }
1198
1199         /*
1200          * Process property with value
1201          */
1202         data = prop->data;
1203
1204         if (fdt_data_fmt(data, len, &buf) != 0) {
1205                 rv = 3;
1206                 goto out2;
1207         }
1208
1209         line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1210             strlen(buf) + 2);
1211         if (line == NULL) {
1212                 sprintf(command_errbuf, "could not allocate space for string");
1213                 rv = 4;
1214                 goto out2;
1215         }
1216
1217         sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1218
1219 out1:
1220         pager_open();
1221         pager_output(line);
1222         pager_close();
1223
1224 out2:
1225         if (buf)
1226                 free(buf);
1227
1228         if (line)
1229                 free(line);
1230
1231         return (rv);
1232 }
1233
1234 static int
1235 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1236 {
1237         uint32_t cells[100];
1238         const char *buf;
1239         int len, rv;
1240         const struct fdt_property *p;
1241
1242         p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1243
1244         if (p != NULL) {
1245                 if (mode == 1) {
1246                          /* Adding inexistant value in mode 1 is forbidden */
1247                         sprintf(command_errbuf, "property already exists!");
1248                         return (CMD_ERROR);
1249                 }
1250         } else if (mode == 0) {
1251                 sprintf(command_errbuf, "property does not exist!");
1252                 return (CMD_ERROR);
1253         }
1254         len = strlen(value);
1255         rv = 0;
1256         buf = value;
1257
1258         switch (*buf) {
1259         case '&':
1260                 /* phandles */
1261                 break;
1262         case '<':
1263                 /* Data cells */
1264                 len = fdt_strtovect(buf, (void *)&cells, 100,
1265                     sizeof(uint32_t));
1266
1267                 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1268                     len * sizeof(uint32_t));
1269                 break;
1270         case '[':
1271                 /* Data bytes */
1272                 len = fdt_strtovect(buf, (void *)&cells, 100,
1273                     sizeof(uint8_t));
1274
1275                 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1276                     len * sizeof(uint8_t));
1277                 break;
1278         case '"':
1279         default:
1280                 /* Default -- string */
1281                 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1282                 break;
1283         }
1284
1285         if (rv != 0) {
1286                 if (rv == -FDT_ERR_NOSPACE)
1287                         sprintf(command_errbuf,
1288                             "Device tree blob is too small!\n");
1289                 else
1290                         sprintf(command_errbuf,
1291                             "Could not add/modify property!\n");
1292         }
1293         return (rv);
1294 }
1295
1296 /* Merge strings from argv into a single string */
1297 static int
1298 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1299 {
1300         char *buf;
1301         int i, idx, sz;
1302
1303         *buffer = NULL;
1304         sz = 0;
1305
1306         for (i = start; i < argc; i++)
1307                 sz += strlen(argv[i]);
1308
1309         /* Additional bytes for whitespaces between args */
1310         sz += argc - start;
1311
1312         buf = (char *)malloc(sizeof(char) * sz);
1313         bzero(buf, sizeof(char) * sz);
1314
1315         if (buf == NULL) {
1316                 sprintf(command_errbuf, "could not allocate space "
1317                     "for string");
1318                 return (1);
1319         }
1320
1321         idx = 0;
1322         for (i = start, idx = 0; i < argc; i++) {
1323                 strcpy(buf + idx, argv[i]);
1324                 idx += strlen(argv[i]);
1325                 buf[idx] = ' ';
1326                 idx++;
1327         }
1328         buf[sz - 1] = '\0';
1329         *buffer = buf;
1330         return (0);
1331 }
1332
1333 /* Extract offset and name of node/property from a given path */
1334 static int
1335 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1336 {
1337         int o;
1338         char *path = *pathp, *name = NULL, *subpath = NULL;
1339
1340         subpath = strrchr(path, '/');
1341         if (subpath == NULL) {
1342                 o = fdt_path_offset(fdtp, cwd);
1343                 name = path;
1344                 path = (char *)&cwd;
1345         } else {
1346                 *subpath = '\0';
1347                 if (strlen(path) == 0)
1348                         path = cwd;
1349
1350                 name = subpath + 1;
1351                 o = fdt_path_offset(fdtp, path);
1352         }
1353
1354         if (strlen(name) == 0) {
1355                 sprintf(command_errbuf, "name not specified");
1356                 return (1);
1357         }
1358         if (o < 0) {
1359                 sprintf(command_errbuf, "could not find node: '%s'", path);
1360                 return (1);
1361         }
1362         *namep = name;
1363         *nodeoff = o;
1364         *pathp = path;
1365         return (0);
1366 }
1367
1368 static int
1369 fdt_cmd_prop(int argc, char *argv[])
1370 {
1371         char *path, *propname, *value;
1372         int o, next, depth, rv;
1373         uint32_t tag;
1374
1375         path = (argc > 2) ? argv[2] : NULL;
1376
1377         value = NULL;
1378
1379         if (argc > 3) {
1380                 /* Merge property value strings into one */
1381                 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1382                         return (CMD_ERROR);
1383         } else
1384                 value = NULL;
1385
1386         if (path == NULL)
1387                 path = cwd;
1388
1389         rv = CMD_OK;
1390
1391         if (value) {
1392                 /* If value is specified -- try to modify prop. */
1393                 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1394                         return (CMD_ERROR);
1395
1396                 rv = fdt_modprop(o, propname, value, 0);
1397                 if (rv)
1398                         return (CMD_ERROR);
1399                 return (CMD_OK);
1400
1401         }
1402         /* User wants to display properties */
1403         o = fdt_path_offset(fdtp, path);
1404
1405         if (o < 0) {
1406                 sprintf(command_errbuf, "could not find node: '%s'", path);
1407                 rv = CMD_ERROR;
1408                 goto out;
1409         }
1410
1411         depth = 0;
1412         while (depth >= 0) {
1413                 tag = fdt_next_tag(fdtp, o, &next);
1414                 switch (tag) {
1415                 case FDT_NOP:
1416                         break;
1417                 case FDT_PROP:
1418                         if (depth > 1)
1419                                 /* Don't process properties of nested nodes */
1420                                 break;
1421
1422                         if (fdt_prop(o) != 0) {
1423                                 sprintf(command_errbuf, "could not process "
1424                                     "property");
1425                                 rv = CMD_ERROR;
1426                                 goto out;
1427                         }
1428                         break;
1429                 case FDT_BEGIN_NODE:
1430                         depth++;
1431                         if (depth > FDT_MAX_DEPTH) {
1432                                 printf("warning: nesting too deep: %d\n",
1433                                     depth);
1434                                 goto out;
1435                         }
1436                         break;
1437                 case FDT_END_NODE:
1438                         depth--;
1439                         if (depth == 0)
1440                                 /*
1441                                  * This is the end of our starting node, force
1442                                  * the loop finish.
1443                                  */
1444                                 depth--;
1445                         break;
1446                 }
1447                 o = next;
1448         }
1449 out:
1450         return (rv);
1451 }
1452
1453 static int
1454 fdt_cmd_mkprop(int argc, char *argv[])
1455 {
1456         int o;
1457         char *path, *propname, *value;
1458
1459         path = (argc > 2) ? argv[2] : NULL;
1460
1461         value = NULL;
1462
1463         if (argc > 3) {
1464                 /* Merge property value strings into one */
1465                 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1466                         return (CMD_ERROR);
1467         } else
1468                 value = NULL;
1469
1470         if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1471                 return (CMD_ERROR);
1472
1473         if (fdt_modprop(o, propname, value, 1))
1474                 return (CMD_ERROR);
1475
1476         return (CMD_OK);
1477 }
1478
1479 static int
1480 fdt_cmd_rm(int argc, char *argv[])
1481 {
1482         int o, rv;
1483         char *path = NULL, *propname;
1484
1485         if (argc > 2)
1486                 path = argv[2];
1487         else {
1488                 sprintf(command_errbuf, "no node/property name specified");
1489                 return (CMD_ERROR);
1490         }
1491
1492         o = fdt_path_offset(fdtp, path);
1493         if (o < 0) {
1494                 /* If node not found -- try to find & delete property */
1495                 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1496                         return (CMD_ERROR);
1497
1498                 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1499                         sprintf(command_errbuf, "could not delete"
1500                             "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1501                             "(property/node does not exist)" : "");
1502                         return (CMD_ERROR);
1503
1504                 } else
1505                         return (CMD_OK);
1506         }
1507         /* If node exists -- remove node */
1508         rv = fdt_del_node(fdtp, o);
1509         if (rv) {
1510                 sprintf(command_errbuf, "could not delete node");
1511                 return (CMD_ERROR);
1512         }
1513         return (CMD_OK);
1514 }
1515
1516 static int
1517 fdt_cmd_mknode(int argc, char *argv[])
1518 {
1519         int o, rv;
1520         char *path = NULL, *nodename = NULL;
1521
1522         if (argc > 2)
1523                 path = argv[2];
1524         else {
1525                 sprintf(command_errbuf, "no node name specified");
1526                 return (CMD_ERROR);
1527         }
1528
1529         if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1530                 return (CMD_ERROR);
1531
1532         rv = fdt_add_subnode(fdtp, o, nodename);
1533
1534         if (rv < 0) {
1535                 if (rv == -FDT_ERR_NOSPACE)
1536                         sprintf(command_errbuf,
1537                             "Device tree blob is too small!\n");
1538                 else
1539                         sprintf(command_errbuf,
1540                             "Could not add node!\n");
1541                 return (CMD_ERROR);
1542         }
1543         return (CMD_OK);
1544 }
1545
1546 static int
1547 fdt_cmd_pwd(int argc, char *argv[])
1548 {
1549         char line[FDT_CWD_LEN];
1550
1551         pager_open();
1552         sprintf(line, "%s\n", cwd);
1553         pager_output(line);
1554         pager_close();
1555         return (CMD_OK);
1556 }
1557
1558 static int
1559 fdt_cmd_mres(int argc, char *argv[])
1560 {
1561         uint64_t start, size;
1562         int i, total;
1563         char line[80];
1564
1565         pager_open();
1566         total = fdt_num_mem_rsv(fdtp);
1567         if (total > 0) {
1568                 pager_output("Reserved memory regions:\n");
1569                 for (i = 0; i < total; i++) {
1570                         fdt_get_mem_rsv(fdtp, i, &start, &size);
1571                         sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n", 
1572                             i, start, size);
1573                         pager_output(line);
1574                 }
1575         } else
1576                 pager_output("No reserved memory regions\n");
1577         pager_close();
1578
1579         return (CMD_OK);
1580 }
1581
1582 static int
1583 fdt_cmd_nyi(int argc, char *argv[])
1584 {
1585
1586         printf("command not yet implemented\n");
1587         return (CMD_ERROR);
1588 }