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
fdt_find_static_dtb()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 efficient 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
fdt_load_dtb(vm_offset_t va)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 snprintf(command_errbuf, sizeof (command_errbuf),
198 "incompatible blob version: %d, should be: %d",
199 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
200
201 } else {
202 snprintf(command_errbuf, sizeof (command_errbuf),
203 "error validating blob: %s", fdt_strerror(err));
204 }
205 return (1);
206 }
207
208 /*
209 * Release previous blob
210 */
211 if (fdtp)
212 free(fdtp);
213
214 fdtp_size = fdt_totalsize(&header);
215 fdtp = malloc(fdtp_size);
216
217 if (fdtp == NULL) {
218 command_errmsg = "can't allocate memory for device tree copy";
219 return (1);
220 }
221
222 fdtp_va = va;
223 COPYOUT(va, fdtp, fdtp_size);
224 debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
225
226 return (0);
227 }
228
229 int
fdt_load_dtb_addr(struct fdt_header * header)230 fdt_load_dtb_addr(struct fdt_header *header)
231 {
232 int err;
233
234 debugf("fdt_load_dtb_addr(%p)\n", header);
235
236 fdtp_size = fdt_totalsize(header);
237 err = fdt_check_header(header);
238 if (err < 0) {
239 snprintf(command_errbuf, sizeof (command_errbuf),
240 "error validating blob: %s", fdt_strerror(err));
241 return (err);
242 }
243 free(fdtp);
244 if ((fdtp = malloc(fdtp_size)) == NULL) {
245 command_errmsg = "can't allocate memory for device tree copy";
246 return (1);
247 }
248
249 fdtp_va = 0; // Don't write this back into module or kernel.
250 bcopy(header, fdtp, fdtp_size);
251 return (0);
252 }
253
254 int
fdt_load_dtb_file(const char * filename)255 fdt_load_dtb_file(const char * filename)
256 {
257 struct preloaded_file *bfp, *oldbfp;
258 int err;
259
260 debugf("fdt_load_dtb_file(%s)\n", filename);
261
262 oldbfp = file_findfile(NULL, "dtb");
263
264 /* Attempt to load and validate a new dtb from a file. */
265 if ((bfp = file_loadraw(filename, "dtb", 1)) == NULL) {
266 snprintf(command_errbuf, sizeof (command_errbuf),
267 "failed to load file '%s'", filename);
268 return (1);
269 }
270 if ((err = fdt_load_dtb(bfp->f_addr)) != 0) {
271 file_discard(bfp);
272 return (err);
273 }
274
275 /* A new dtb was validated, discard any previous file. */
276 if (oldbfp)
277 file_discard(oldbfp);
278 return (0);
279 }
280
281 int
fdt_setup_fdtp()282 fdt_setup_fdtp()
283 {
284 struct preloaded_file *bfp;
285 vm_offset_t va;
286
287 debugf("fdt_setup_fdtp()\n");
288
289 /* If we already loaded a file, use it. */
290 if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
291 if (fdt_load_dtb(bfp->f_addr) == 0) {
292 printf("Using DTB from loaded file '%s'.\n",
293 bfp->f_name);
294 return (0);
295 }
296 }
297
298 /* If we were given the address of a valid blob in memory, use it. */
299 if (fdt_to_load != NULL) {
300 if (fdt_load_dtb_addr(fdt_to_load) == 0) {
301 printf("Using DTB from memory address 0x%p.\n",
302 fdt_to_load);
303 return (0);
304 }
305 }
306
307 if (fdt_platform_load_dtb() == 0)
308 return (0);
309
310 /* If there is a dtb compiled into the kernel, use it. */
311 if ((va = fdt_find_static_dtb()) != 0) {
312 if (fdt_load_dtb(va) == 0) {
313 printf("Using DTB compiled into kernel.\n");
314 return (0);
315 }
316 }
317
318 command_errmsg = "No device tree blob found!\n";
319 return (1);
320 }
321
322 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
323 (cellbuf), (lim), (cellsize), 0);
324
325 /* Force using base 16 */
326 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
327 (cellbuf), (lim), (cellsize), 16);
328
329 static int
_fdt_strtovect(const char * str,void * cellbuf,int lim,unsigned char cellsize,uint8_t base)330 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize,
331 uint8_t base)
332 {
333 const char *buf = str;
334 const char *end = str + strlen(str) - 2;
335 uint32_t *u32buf = NULL;
336 uint8_t *u8buf = NULL;
337 int cnt = 0;
338
339 if (cellsize == sizeof(uint32_t))
340 u32buf = (uint32_t *)cellbuf;
341 else
342 u8buf = (uint8_t *)cellbuf;
343
344 if (lim == 0)
345 return (0);
346
347 while (buf < end) {
348
349 /* Skip white whitespace(s)/separators */
350 while (!isxdigit(*buf) && buf < end)
351 buf++;
352
353 if (u32buf != NULL)
354 u32buf[cnt] =
355 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
356
357 else
358 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
359
360 if (cnt + 1 <= lim - 1)
361 cnt++;
362 else
363 break;
364 buf++;
365 /* Find another number */
366 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
367 buf++;
368 }
369 return (cnt);
370 }
371
372 void
fdt_fixup_ethernet(const char * str,char * ethstr,int len)373 fdt_fixup_ethernet(const char *str, char *ethstr, int len)
374 {
375 uint8_t tmp_addr[6];
376
377 /* Convert macaddr string into a vector of uints */
378 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
379 /* Set actual property to a value from vect */
380 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
381 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
382 }
383
384 void
fdt_fixup_cpubusfreqs(unsigned long cpufreq,unsigned long busfreq)385 fdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
386 {
387 int lo, o = 0, o2, maxo = 0, depth;
388 const uint32_t zero = 0;
389
390 /* We want to modify every subnode of /cpus */
391 o = fdt_path_offset(fdtp, "/cpus");
392 if (o < 0)
393 return;
394
395 /* maxo should contain offset of node next to /cpus */
396 depth = 0;
397 maxo = o;
398 while (depth != -1)
399 maxo = fdt_next_node(fdtp, maxo, &depth);
400
401 /* Find CPU frequency properties */
402 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
403 &zero, sizeof(uint32_t));
404
405 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
406 sizeof(uint32_t));
407
408 lo = MIN(o, o2);
409
410 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
411
412 o = fdt_node_offset_by_prop_value(fdtp, lo,
413 "clock-frequency", &zero, sizeof(uint32_t));
414
415 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
416 &zero, sizeof(uint32_t));
417
418 /* We're only interested in /cpus subnode(s) */
419 if (lo > maxo)
420 break;
421
422 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
423 (uint32_t)cpufreq);
424
425 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
426 (uint32_t)busfreq);
427
428 lo = MIN(o, o2);
429 }
430 }
431
432 #ifdef notyet
433 static int
fdt_reg_valid(uint32_t * reg,int len,int addr_cells,int size_cells)434 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
435 {
436 int cells_in_tuple, i, tuples, tuple_size;
437 uint32_t cur_start, cur_size;
438
439 cells_in_tuple = (addr_cells + size_cells);
440 tuple_size = cells_in_tuple * sizeof(uint32_t);
441 tuples = len / tuple_size;
442 if (tuples == 0)
443 return (EINVAL);
444
445 for (i = 0; i < tuples; i++) {
446 if (addr_cells == 2)
447 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
448 else
449 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
450
451 if (size_cells == 2)
452 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
453 else
454 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
455
456 if (cur_size == 0)
457 return (EINVAL);
458
459 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
460 i, cur_start, cur_size);
461 }
462 return (0);
463 }
464 #endif
465
466 void
fdt_fixup_memory(struct fdt_mem_region * region,size_t num)467 fdt_fixup_memory(struct fdt_mem_region *region, size_t num)
468 {
469 struct fdt_mem_region *curmr;
470 uint32_t addr_cells, size_cells;
471 uint32_t *addr_cellsp, *size_cellsp;
472 int err, i, len, memory, root;
473 size_t realmrno;
474 uint8_t *buf, *sb;
475 uint64_t rstart, rsize;
476 int reserved;
477
478 root = fdt_path_offset(fdtp, "/");
479 if (root < 0) {
480 sprintf(command_errbuf, "Could not find root node !");
481 return;
482 }
483
484 memory = fdt_path_offset(fdtp, "/memory");
485 if (memory <= 0) {
486 /* Create proper '/memory' node. */
487 memory = fdt_add_subnode(fdtp, root, "memory");
488 if (memory <= 0) {
489 snprintf(command_errbuf, sizeof (command_errbuf),
490 "Could not fixup '/memory' "
491 "node, error code : %d!\n", memory);
492 return;
493 }
494
495 err = fdt_setprop(fdtp, memory, "device_type", "memory",
496 sizeof("memory"));
497
498 if (err < 0)
499 return;
500 }
501
502 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
503 NULL);
504 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
505
506 if (addr_cellsp == NULL || size_cellsp == NULL) {
507 snprintf(command_errbuf, sizeof (command_errbuf),
508 "Could not fixup '/memory' node : "
509 "%s %s property not found in root node!\n",
510 (!addr_cellsp) ? "#address-cells" : "",
511 (!size_cellsp) ? "#size-cells" : "");
512 return;
513 }
514
515 addr_cells = fdt32_to_cpu(*addr_cellsp);
516 size_cells = fdt32_to_cpu(*size_cellsp);
517
518 /*
519 * Convert memreserve data to memreserve property
520 * Check if property already exists
521 */
522 reserved = fdt_num_mem_rsv(fdtp);
523 if (reserved &&
524 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
525 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
526 sb = buf = (uint8_t *)malloc(len);
527 if (!buf)
528 return;
529
530 bzero(buf, len);
531
532 for (i = 0; i < reserved; i++) {
533 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
534 break;
535 if (rsize) {
536 /* Ensure endianness, and put cells into a buffer */
537 if (addr_cells == 2)
538 *(uint64_t *)buf =
539 cpu_to_fdt64(rstart);
540 else
541 *(uint32_t *)buf =
542 cpu_to_fdt32(rstart);
543
544 buf += sizeof(uint32_t) * addr_cells;
545 if (size_cells == 2)
546 *(uint64_t *)buf =
547 cpu_to_fdt64(rsize);
548 else
549 *(uint32_t *)buf =
550 cpu_to_fdt32(rsize);
551
552 buf += sizeof(uint32_t) * size_cells;
553 }
554 }
555
556 /* Set property */
557 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
558 printf("Could not fixup 'memreserve' property.\n");
559
560 free(sb);
561 }
562
563 /* Count valid memory regions entries in sysinfo. */
564 realmrno = num;
565 for (i = 0; i < num; i++)
566 if (region[i].start == 0 && region[i].size == 0)
567 realmrno--;
568
569 if (realmrno == 0) {
570 snprintf(command_errbuf, sizeof (command_errbuf),
571 "Could not fixup '/memory' node : "
572 "sysinfo doesn't contain valid memory regions info!\n");
573 return;
574 }
575
576 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
577 sb = buf = (uint8_t *)malloc(len);
578 if (!buf)
579 return;
580
581 bzero(buf, len);
582
583 for (i = 0; i < num; i++) {
584 curmr = ®ion[i];
585 if (curmr->size != 0) {
586 /* Ensure endianness, and put cells into a buffer */
587 if (addr_cells == 2)
588 *(uint64_t *)buf =
589 cpu_to_fdt64(curmr->start);
590 else
591 *(uint32_t *)buf =
592 cpu_to_fdt32(curmr->start);
593
594 buf += sizeof(uint32_t) * addr_cells;
595 if (size_cells == 2)
596 *(uint64_t *)buf =
597 cpu_to_fdt64(curmr->size);
598 else
599 *(uint32_t *)buf =
600 cpu_to_fdt32(curmr->size);
601
602 buf += sizeof(uint32_t) * size_cells;
603 }
604 }
605
606 /* Set property */
607 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
608 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
609
610 free(sb);
611 }
612
613 void
fdt_fixup_stdout(const char * str)614 fdt_fixup_stdout(const char *str)
615 {
616 char *ptr;
617 int serialno;
618 int len, no, sero;
619 const struct fdt_property *prop;
620 char *tmp[10];
621
622 ptr = (char *)str + strlen(str) - 1;
623 while (ptr > str && isdigit(*(str - 1)))
624 str--;
625
626 if (ptr == str)
627 return;
628
629 serialno = (int)strtol(ptr, NULL, 0);
630 no = fdt_path_offset(fdtp, "/chosen");
631 if (no < 0)
632 return;
633
634 prop = fdt_get_property(fdtp, no, "stdout", &len);
635
636 /* If /chosen/stdout does not extist, create it */
637 if (prop == NULL || (prop != NULL && len == 0)) {
638
639 bzero(tmp, 10 * sizeof(char));
640 strcpy((char *)&tmp, "serial");
641 if (strlen(ptr) > 3)
642 /* Serial number too long */
643 return;
644
645 strncpy((char *)tmp + 6, ptr, 3);
646 sero = fdt_path_offset(fdtp, (const char *)tmp);
647 if (sero < 0)
648 /*
649 * If serial device we're trying to assign
650 * stdout to doesn't exist in DT -- return.
651 */
652 return;
653
654 fdt_setprop(fdtp, no, "stdout", &tmp,
655 strlen((char *)&tmp) + 1);
656 fdt_setprop(fdtp, no, "stdin", &tmp,
657 strlen((char *)&tmp) + 1);
658 }
659 }
660
661 /*
662 * Locate the blob, fix it up and return its location.
663 */
664 static int
fdt_fixup(void)665 fdt_fixup(void)
666 {
667 int chosen, len;
668
669 len = 0;
670
671 debugf("fdt_fixup()\n");
672
673 if (fdtp == NULL && fdt_setup_fdtp() != 0)
674 return (0);
675
676 /* Create /chosen node (if not exists) */
677 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
678 -FDT_ERR_NOTFOUND)
679 chosen = fdt_add_subnode(fdtp, 0, "chosen");
680
681 /* Value assigned to fixup-applied does not matter. */
682 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
683 return (1);
684
685 fdt_platform_fixups();
686
687 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
688 return (1);
689 }
690
691 /*
692 * Copy DTB blob to specified location and return size
693 */
694 int
fdt_copy(vm_offset_t va)695 fdt_copy(vm_offset_t va)
696 {
697 int err;
698 debugf("fdt_copy va 0x%08x\n", va);
699 if (fdtp == NULL) {
700 err = fdt_setup_fdtp();
701 if (err) {
702 printf("No valid device tree blob found!\n");
703 return (0);
704 }
705 }
706
707 if (fdt_fixup() == 0)
708 return (0);
709
710 if (fdtp_va != 0) {
711 /* Overwrite the FDT with the fixed version. */
712 /* XXX Is this really appropriate? */
713 COPYIN(fdtp, fdtp_va, fdtp_size);
714 }
715 COPYIN(fdtp, va, fdtp_size);
716 return (fdtp_size);
717 }
718
719
720
721 int
command_fdt_internal(int argc,char * argv[])722 command_fdt_internal(int argc, char *argv[])
723 {
724 cmdf_t *cmdh;
725 int flags;
726 char *cmd;
727 int i, err;
728
729 if (argc < 2) {
730 command_errmsg = "usage is 'fdt <command> [<args>]";
731 return (CMD_ERROR);
732 }
733
734 /*
735 * Validate fdt <command>.
736 */
737 cmd = strdup(argv[1]);
738 i = 0;
739 cmdh = NULL;
740 while (!(commands[i].name == NULL)) {
741 if (strcmp(cmd, commands[i].name) == 0) {
742 /* found it */
743 cmdh = commands[i].handler;
744 flags = commands[i].flags;
745 break;
746 }
747 i++;
748 }
749 if (cmdh == NULL) {
750 command_errmsg = "unknown command";
751 return (CMD_ERROR);
752 }
753
754 if (flags & CMD_REQUIRES_BLOB) {
755 /*
756 * Check if uboot env vars were parsed already. If not, do it now.
757 */
758 if (fdt_fixup() == 0)
759 return (CMD_ERROR);
760 }
761
762 /*
763 * Call command handler.
764 */
765 err = (*cmdh)(argc, argv);
766
767 return (err);
768 }
769
770 static int
fdt_cmd_addr(int argc,char * argv[])771 fdt_cmd_addr(int argc, char *argv[])
772 {
773 struct preloaded_file *fp;
774 struct fdt_header *hdr;
775 const char *addr;
776 char *cp;
777
778 fdt_to_load = NULL;
779
780 if (argc > 2)
781 addr = argv[2];
782 else {
783 sprintf(command_errbuf, "no address specified");
784 return (CMD_ERROR);
785 }
786
787 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
788 if (cp == addr) {
789 snprintf(command_errbuf, sizeof (command_errbuf),
790 "Invalid address: %s", addr);
791 return (CMD_ERROR);
792 }
793
794 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
795 file_discard(fp);
796 }
797
798 fdt_to_load = hdr;
799 return (CMD_OK);
800 }
801
802 static int
fdt_cmd_cd(int argc,char * argv[])803 fdt_cmd_cd(int argc, char *argv[])
804 {
805 char *path;
806 char tmp[FDT_CWD_LEN];
807 int len, o;
808
809 path = (argc > 2) ? argv[2] : "/";
810
811 if (path[0] == '/') {
812 len = strlen(path);
813 if (len >= FDT_CWD_LEN)
814 goto fail;
815 } else {
816 /* Handle path specification relative to cwd */
817 len = strlen(cwd) + strlen(path) + 1;
818 if (len >= FDT_CWD_LEN)
819 goto fail;
820
821 strcpy(tmp, cwd);
822 strcat(tmp, "/");
823 strcat(tmp, path);
824 path = tmp;
825 }
826
827 o = fdt_path_offset(fdtp, path);
828 if (o < 0) {
829 snprintf(command_errbuf, sizeof (command_errbuf),
830 "could not find node: '%s'", path);
831 return (CMD_ERROR);
832 }
833
834 strcpy(cwd, path);
835 return (CMD_OK);
836
837 fail:
838 snprintf(command_errbuf, sizeof (command_errbuf),
839 "path too long: %d, max allowed: %d", len, FDT_CWD_LEN - 1);
840 return (CMD_ERROR);
841 }
842
843 static int
fdt_cmd_hdr(int argc __unused,char * argv[]__unused)844 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
845 {
846 char line[80];
847 int ver;
848
849 if (fdtp == NULL) {
850 command_errmsg = "no device tree blob pointer?!";
851 return (CMD_ERROR);
852 }
853
854 ver = fdt_version(fdtp);
855 pager_open();
856 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
857 pager_output(line);
858 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
859 pager_output(line);
860 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
861 pager_output(line);
862 sprintf(line, " off_dt_struct = 0x%08x\n",
863 fdt_off_dt_struct(fdtp));
864 pager_output(line);
865 sprintf(line, " off_dt_strings = 0x%08x\n",
866 fdt_off_dt_strings(fdtp));
867 pager_output(line);
868 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
869 fdt_off_mem_rsvmap(fdtp));
870 pager_output(line);
871 sprintf(line, " version = %d\n", ver);
872 pager_output(line);
873 sprintf(line, " last compatible version = %d\n",
874 fdt_last_comp_version(fdtp));
875 pager_output(line);
876 if (ver >= 2) {
877 sprintf(line, " boot_cpuid = %d\n",
878 fdt_boot_cpuid_phys(fdtp));
879 pager_output(line);
880 }
881 if (ver >= 3) {
882 sprintf(line, " size_dt_strings = %d\n",
883 fdt_size_dt_strings(fdtp));
884 pager_output(line);
885 }
886 if (ver >= 17) {
887 sprintf(line, " size_dt_struct = %d\n",
888 fdt_size_dt_struct(fdtp));
889 pager_output(line);
890 }
891 pager_close();
892
893 return (CMD_OK);
894 }
895
896 static int
fdt_cmd_ls(int argc,char * argv[])897 fdt_cmd_ls(int argc, char *argv[])
898 {
899 const char *prevname[FDT_MAX_DEPTH] = { NULL };
900 const char *name;
901 char *path;
902 int i, o, depth, len;
903
904 path = (argc > 2) ? argv[2] : NULL;
905 if (path == NULL)
906 path = cwd;
907
908 o = fdt_path_offset(fdtp, path);
909 if (o < 0) {
910 snprintf(command_errbuf, sizeof (command_errbuf),
911 "could not find node: '%s'", path);
912 return (CMD_ERROR);
913 }
914
915 for (depth = 0;
916 (o >= 0) && (depth >= 0);
917 o = fdt_next_node(fdtp, o, &depth)) {
918
919 name = fdt_get_name(fdtp, o, &len);
920
921 if (depth > FDT_MAX_DEPTH) {
922 printf("max depth exceeded: %d\n", depth);
923 continue;
924 }
925
926 prevname[depth] = name;
927
928 /* Skip root (i = 1) when printing devices */
929 for (i = 1; i <= depth; i++) {
930 if (prevname[i] == NULL)
931 break;
932
933 if (strcmp(cwd, "/") == 0)
934 printf("/");
935 printf("%s", prevname[i]);
936 }
937 printf("\n");
938 }
939
940 return (CMD_OK);
941 }
942
943 static __inline int
isprint(int c)944 isprint(int c)
945 {
946
947 return (c >= ' ' && c <= 0x7e);
948 }
949
950 static int
fdt_isprint(const void * data,int len,int * count)951 fdt_isprint(const void *data, int len, int *count)
952 {
953 const char *d;
954 char ch;
955 int yesno, i;
956
957 if (len == 0)
958 return (0);
959
960 d = (const char *)data;
961 if (d[len - 1] != '\0')
962 return (0);
963
964 *count = 0;
965 yesno = 1;
966 for (i = 0; i < len; i++) {
967 ch = *(d + i);
968 if (isprint(ch) || (ch == '\0' && i > 0)) {
969 /* Count strings */
970 if (ch == '\0')
971 (*count)++;
972 continue;
973 }
974
975 yesno = 0;
976 break;
977 }
978
979 return (yesno);
980 }
981
982 static int
fdt_data_str(const void * data,int len,int count,char ** buf)983 fdt_data_str(const void *data, int len, int count, char **buf)
984 {
985 char *b, *tmp;
986 const char *d;
987 int buf_len, i, l;
988
989 /*
990 * Calculate the length for the string and allocate memory.
991 *
992 * Note that 'len' already includes at least one terminator.
993 */
994 buf_len = len;
995 if (count > 1) {
996 /*
997 * Each token had already a terminator buried in 'len', but we
998 * only need one eventually, don't count space for these.
999 */
1000 buf_len -= count - 1;
1001
1002 /* Each consecutive token requires a ", " separator. */
1003 buf_len += count * 2;
1004 }
1005
1006 /* Add some space for surrounding double quotes. */
1007 buf_len += count * 2;
1008
1009 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1010 b = (char *)malloc(buf_len);
1011 tmp = (char *)malloc(buf_len);
1012 if (b == NULL)
1013 goto error;
1014
1015 if (tmp == NULL) {
1016 free(b);
1017 goto error;
1018 }
1019
1020 b[0] = '\0';
1021
1022 /*
1023 * Now that we have space, format the string.
1024 */
1025 i = 0;
1026 do {
1027 d = (const char *)data + i;
1028 l = strlen(d) + 1;
1029
1030 sprintf(tmp, "\"%s\"%s", d,
1031 (i + l) < len ? ", " : "");
1032 strcat(b, tmp);
1033
1034 i += l;
1035
1036 } while (i < len);
1037 *buf = b;
1038
1039 free(tmp);
1040
1041 return (0);
1042 error:
1043 return (1);
1044 }
1045
1046 static int
fdt_data_cell(const void * data,int len,char ** buf)1047 fdt_data_cell(const void *data, int len, char **buf)
1048 {
1049 char *b, *tmp;
1050 const uint32_t *c;
1051 int count, i, l;
1052
1053 /* Number of cells */
1054 count = len / 4;
1055
1056 /*
1057 * Calculate the length for the string and allocate memory.
1058 */
1059
1060 /* Each byte translates to 2 output characters */
1061 l = len * 2;
1062 if (count > 1) {
1063 /* Each consecutive cell requires a " " separator. */
1064 l += (count - 1) * 1;
1065 }
1066 /* Each cell will have a "0x" prefix */
1067 l += count * 2;
1068 /* Space for surrounding <> and terminator */
1069 l += 3;
1070
1071 b = (char *)malloc(l);
1072 tmp = (char *)malloc(l);
1073 if (b == NULL)
1074 goto error;
1075
1076 if (tmp == NULL) {
1077 free(b);
1078 goto error;
1079 }
1080
1081 b[0] = '\0';
1082 strcat(b, "<");
1083
1084 for (i = 0; i < len; i += 4) {
1085 c = (const uint32_t *)((const uint8_t *)data + i);
1086 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1087 i < (len - 4) ? " " : "");
1088 strcat(b, tmp);
1089 }
1090 strcat(b, ">");
1091 *buf = b;
1092
1093 free(tmp);
1094
1095 return (0);
1096 error:
1097 return (1);
1098 }
1099
1100 static int
fdt_data_bytes(const void * data,int len,char ** buf)1101 fdt_data_bytes(const void *data, int len, char **buf)
1102 {
1103 char *b, *tmp;
1104 const char *d;
1105 int i, l;
1106
1107 /*
1108 * Calculate the length for the string and allocate memory.
1109 */
1110
1111 /* Each byte translates to 2 output characters */
1112 l = len * 2;
1113 if (len > 1)
1114 /* Each consecutive byte requires a " " separator. */
1115 l += (len - 1) * 1;
1116 /* Each byte will have a "0x" prefix */
1117 l += len * 2;
1118 /* Space for surrounding [] and terminator. */
1119 l += 3;
1120
1121 b = (char *)malloc(l);
1122 tmp = (char *)malloc(l);
1123 if (b == NULL)
1124 goto error;
1125
1126 if (tmp == NULL) {
1127 free(b);
1128 goto error;
1129 }
1130
1131 b[0] = '\0';
1132 strcat(b, "[");
1133
1134 for (i = 0, d = data; i < len; i++) {
1135 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1136 strcat(b, tmp);
1137 }
1138 strcat(b, "]");
1139 *buf = b;
1140
1141 free(tmp);
1142
1143 return (0);
1144 error:
1145 return (1);
1146 }
1147
1148 static int
fdt_data_fmt(const void * data,int len,char ** buf)1149 fdt_data_fmt(const void *data, int len, char **buf)
1150 {
1151 int count;
1152
1153 if (len == 0) {
1154 *buf = NULL;
1155 return (1);
1156 }
1157
1158 if (fdt_isprint(data, len, &count))
1159 return (fdt_data_str(data, len, count, buf));
1160
1161 else if ((len % 4) == 0)
1162 return (fdt_data_cell(data, len, buf));
1163
1164 else
1165 return (fdt_data_bytes(data, len, buf));
1166 }
1167
1168 static int
fdt_prop(int offset)1169 fdt_prop(int offset)
1170 {
1171 char *line, *buf;
1172 const struct fdt_property *prop;
1173 const char *name;
1174 const void *data;
1175 int len, rv;
1176
1177 line = NULL;
1178 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1179 if (prop == NULL)
1180 return (1);
1181
1182 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1183 len = fdt32_to_cpu(prop->len);
1184
1185 rv = 0;
1186 buf = NULL;
1187 if (len == 0) {
1188 /* Property without value */
1189 line = (char *)malloc(strlen(name) + 2);
1190 if (line == NULL) {
1191 rv = 2;
1192 goto out2;
1193 }
1194 sprintf(line, "%s\n", name);
1195 goto out1;
1196 }
1197
1198 /*
1199 * Process property with value
1200 */
1201 data = prop->data;
1202
1203 if (fdt_data_fmt(data, len, &buf) != 0) {
1204 rv = 3;
1205 goto out2;
1206 }
1207
1208 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1209 strlen(buf) + 2);
1210 if (line == NULL) {
1211 sprintf(command_errbuf, "could not allocate space for string");
1212 rv = 4;
1213 goto out2;
1214 }
1215
1216 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1217
1218 out1:
1219 pager_open();
1220 pager_output(line);
1221 pager_close();
1222
1223 out2:
1224 if (buf)
1225 free(buf);
1226
1227 if (line)
1228 free(line);
1229
1230 return (rv);
1231 }
1232
1233 static int
fdt_modprop(int nodeoff,char * propname,void * value,char mode)1234 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1235 {
1236 uint32_t cells[100];
1237 const char *buf;
1238 int len, rv;
1239 const struct fdt_property *p;
1240
1241 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1242
1243 if (p != NULL) {
1244 if (mode == 1) {
1245 /* Adding inexistant value in mode 1 is forbidden */
1246 sprintf(command_errbuf, "property already exists!");
1247 return (CMD_ERROR);
1248 }
1249 } else if (mode == 0) {
1250 sprintf(command_errbuf, "property does not exist!");
1251 return (CMD_ERROR);
1252 }
1253 len = strlen(value);
1254 rv = 0;
1255 buf = value;
1256
1257 switch (*buf) {
1258 case '&':
1259 /* phandles */
1260 break;
1261 case '<':
1262 /* Data cells */
1263 len = fdt_strtovect(buf, (void *)&cells, 100,
1264 sizeof(uint32_t));
1265
1266 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1267 len * sizeof(uint32_t));
1268 break;
1269 case '[':
1270 /* Data bytes */
1271 len = fdt_strtovect(buf, (void *)&cells, 100,
1272 sizeof(uint8_t));
1273
1274 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1275 len * sizeof(uint8_t));
1276 break;
1277 case '"':
1278 default:
1279 /* Default -- string */
1280 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1281 break;
1282 }
1283
1284 if (rv != 0) {
1285 if (rv == -FDT_ERR_NOSPACE)
1286 sprintf(command_errbuf,
1287 "Device tree blob is too small!\n");
1288 else
1289 sprintf(command_errbuf,
1290 "Could not add/modify property!\n");
1291 }
1292 return (rv);
1293 }
1294
1295 /* Merge strings from argv into a single string */
1296 static int
fdt_merge_strings(int argc,char * argv[],int start,char ** buffer)1297 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1298 {
1299 char *buf;
1300 int i, idx, sz;
1301
1302 *buffer = NULL;
1303 sz = 0;
1304
1305 for (i = start; i < argc; i++)
1306 sz += strlen(argv[i]);
1307
1308 /* Additional bytes for whitespaces between args */
1309 sz += argc - start;
1310
1311 buf = (char *)malloc(sizeof(char) * sz);
1312 if (buf == NULL) {
1313 sprintf(command_errbuf, "could not allocate space "
1314 "for string");
1315 return (1);
1316 }
1317 bzero(buf, sizeof(char) * sz);
1318
1319 idx = 0;
1320 for (i = start, idx = 0; i < argc; i++) {
1321 strcpy(buf + idx, argv[i]);
1322 idx += strlen(argv[i]);
1323 buf[idx] = ' ';
1324 idx++;
1325 }
1326 buf[sz - 1] = '\0';
1327 *buffer = buf;
1328 return (0);
1329 }
1330
1331 /* Extract offset and name of node/property from a given path */
1332 static int
fdt_extract_nameloc(char ** pathp,char ** namep,int * nodeoff)1333 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1334 {
1335 int o;
1336 char *path = *pathp, *name = NULL, *subpath = NULL;
1337
1338 subpath = strrchr(path, '/');
1339 if (subpath == NULL) {
1340 o = fdt_path_offset(fdtp, cwd);
1341 name = path;
1342 path = (char *)&cwd;
1343 } else {
1344 *subpath = '\0';
1345 if (strlen(path) == 0)
1346 path = cwd;
1347
1348 name = subpath + 1;
1349 o = fdt_path_offset(fdtp, path);
1350 }
1351
1352 if (strlen(name) == 0) {
1353 sprintf(command_errbuf, "name not specified");
1354 return (1);
1355 }
1356 if (o < 0) {
1357 snprintf(command_errbuf, sizeof (command_errbuf),
1358 "could not find node: '%s'", path);
1359 return (1);
1360 }
1361 *namep = name;
1362 *nodeoff = o;
1363 *pathp = path;
1364 return (0);
1365 }
1366
1367 static int
fdt_cmd_prop(int argc,char * argv[])1368 fdt_cmd_prop(int argc, char *argv[])
1369 {
1370 char *path, *propname, *value;
1371 int o, next, depth, rv;
1372 uint32_t tag;
1373
1374 path = (argc > 2) ? argv[2] : NULL;
1375
1376 value = NULL;
1377
1378 if (argc > 3) {
1379 /* Merge property value strings into one */
1380 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1381 return (CMD_ERROR);
1382 } else
1383 value = NULL;
1384
1385 if (path == NULL)
1386 path = cwd;
1387
1388 rv = CMD_OK;
1389
1390 if (value) {
1391 /* If value is specified -- try to modify prop. */
1392 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1393 return (CMD_ERROR);
1394
1395 rv = fdt_modprop(o, propname, value, 0);
1396 if (rv)
1397 return (CMD_ERROR);
1398 return (CMD_OK);
1399
1400 }
1401 /* User wants to display properties */
1402 o = fdt_path_offset(fdtp, path);
1403
1404 if (o < 0) {
1405 snprintf(command_errbuf, sizeof (command_errbuf),
1406 "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
fdt_cmd_mkprop(int argc,char * argv[])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
fdt_cmd_rm(int argc,char * argv[])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 snprintf(command_errbuf, sizeof (command_errbuf),
1500 "could not delete %s\n",
1501 (rv == -FDT_ERR_NOTFOUND) ?
1502 "(property/node does not exist)" : "");
1503 return (CMD_ERROR);
1504
1505 } else
1506 return (CMD_OK);
1507 }
1508 /* If node exists -- remove node */
1509 rv = fdt_del_node(fdtp, o);
1510 if (rv) {
1511 sprintf(command_errbuf, "could not delete node");
1512 return (CMD_ERROR);
1513 }
1514 return (CMD_OK);
1515 }
1516
1517 static int
fdt_cmd_mknode(int argc,char * argv[])1518 fdt_cmd_mknode(int argc, char *argv[])
1519 {
1520 int o, rv;
1521 char *path = NULL, *nodename = NULL;
1522
1523 if (argc > 2)
1524 path = argv[2];
1525 else {
1526 sprintf(command_errbuf, "no node name specified");
1527 return (CMD_ERROR);
1528 }
1529
1530 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1531 return (CMD_ERROR);
1532
1533 rv = fdt_add_subnode(fdtp, o, nodename);
1534
1535 if (rv < 0) {
1536 if (rv == -FDT_ERR_NOSPACE)
1537 sprintf(command_errbuf,
1538 "Device tree blob is too small!\n");
1539 else
1540 sprintf(command_errbuf,
1541 "Could not add node!\n");
1542 return (CMD_ERROR);
1543 }
1544 return (CMD_OK);
1545 }
1546
1547 static int
fdt_cmd_pwd(int argc,char * argv[])1548 fdt_cmd_pwd(int argc, char *argv[])
1549 {
1550 char line[FDT_CWD_LEN];
1551
1552 pager_open();
1553 sprintf(line, "%s\n", cwd);
1554 pager_output(line);
1555 pager_close();
1556 return (CMD_OK);
1557 }
1558
1559 static int
fdt_cmd_mres(int argc,char * argv[])1560 fdt_cmd_mres(int argc, char *argv[])
1561 {
1562 uint64_t start, size;
1563 int i, total;
1564 char line[80];
1565
1566 pager_open();
1567 total = fdt_num_mem_rsv(fdtp);
1568 if (total > 0) {
1569 pager_output("Reserved memory regions:\n");
1570 for (i = 0; i < total; i++) {
1571 fdt_get_mem_rsv(fdtp, i, &start, &size);
1572 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1573 i, start, size);
1574 pager_output(line);
1575 }
1576 } else
1577 pager_output("No reserved memory regions\n");
1578 pager_close();
1579
1580 return (CMD_OK);
1581 }
1582
1583 static int
fdt_cmd_nyi(int argc,char * argv[])1584 fdt_cmd_nyi(int argc, char *argv[])
1585 {
1586
1587 printf("command not yet implemented\n");
1588 return (CMD_ERROR);
1589 }
1590