2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Semihalf under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/linker.h>
38 #include <machine/elf.h>
40 #include "bootstrap.h"
41 #include "fdt_platform.h"
42 #include "fdt_overlay.h"
45 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
46 printf(fmt,##args); } while (0)
48 #define debugf(fmt, args...)
51 #define FDT_CWD_LEN 256
52 #define FDT_MAX_DEPTH 12
54 #define FDT_PROP_SEP " = "
56 #define COPYOUT(s,d,l) archsw.arch_copyout(s, d, l)
57 #define COPYIN(s,d,l) archsw.arch_copyin(s, d, l)
59 #define FDT_STATIC_DTB_SYMBOL "fdt_static_dtb"
61 #define CMD_REQUIRES_BLOB 0x01
63 /* Location of FDT yet to be loaded. */
64 /* This may be in read-only memory, so can't be manipulated directly. */
65 static struct fdt_header *fdt_to_load = NULL;
66 /* Location of FDT on heap. */
67 /* This is the copy we actually manipulate. */
68 static struct fdt_header *fdtp = NULL;
69 /* Size of FDT blob */
70 static size_t fdtp_size = 0;
71 /* Location of FDT in kernel or module. */
72 /* This won't be set if FDT is loaded from disk or memory. */
73 /* If it is set, we'll update it when fdt_copy() gets called. */
74 static vm_offset_t fdtp_va = 0;
76 static int fdt_load_dtb(vm_offset_t va);
78 static int fdt_cmd_nyi(int argc, char *argv[]);
80 static int fdt_cmd_addr(int argc, char *argv[]);
81 static int fdt_cmd_mkprop(int argc, char *argv[]);
82 static int fdt_cmd_cd(int argc, char *argv[]);
83 static int fdt_cmd_hdr(int argc, char *argv[]);
84 static int fdt_cmd_ls(int argc, char *argv[]);
85 static int fdt_cmd_prop(int argc, char *argv[]);
86 static int fdt_cmd_pwd(int argc, char *argv[]);
87 static int fdt_cmd_rm(int argc, char *argv[]);
88 static int fdt_cmd_mknode(int argc, char *argv[]);
89 static int fdt_cmd_mres(int argc, char *argv[]);
91 typedef int cmdf_t(int, char *[]);
99 static const struct cmdtab commands[] = {
100 { "addr", &fdt_cmd_addr, 0 },
101 { "alias", &fdt_cmd_nyi, 0 },
102 { "cd", &fdt_cmd_cd, CMD_REQUIRES_BLOB },
103 { "header", &fdt_cmd_hdr, CMD_REQUIRES_BLOB },
104 { "ls", &fdt_cmd_ls, CMD_REQUIRES_BLOB },
105 { "mknode", &fdt_cmd_mknode, CMD_REQUIRES_BLOB },
106 { "mkprop", &fdt_cmd_mkprop, CMD_REQUIRES_BLOB },
107 { "mres", &fdt_cmd_mres, CMD_REQUIRES_BLOB },
108 { "prop", &fdt_cmd_prop, CMD_REQUIRES_BLOB },
109 { "pwd", &fdt_cmd_pwd, CMD_REQUIRES_BLOB },
110 { "rm", &fdt_cmd_rm, CMD_REQUIRES_BLOB },
114 static char cwd[FDT_CWD_LEN] = "/";
117 fdt_find_static_dtb()
122 vm_offset_t strtab, symtab, fdt_start;
124 struct preloaded_file *kfp;
125 struct file_metadata *md;
129 debugf("fdt_find_static_dtb()\n");
131 sym_count = symtab = strtab = 0;
134 offs = __elfN(relocation_offset);
136 kfp = file_findfile(NULL, NULL);
140 /* Locate the dynamic symbols and strtab. */
141 md = file_findmetadata(kfp, MODINFOMD_ELFHDR);
144 ehdr = (Elf_Ehdr *)md->md_data;
146 md = file_findmetadata(kfp, MODINFOMD_SHDR);
149 shdr = (Elf_Shdr *)md->md_data;
151 for (i = 0; i < ehdr->e_shnum; ++i) {
152 if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) {
153 symtab = shdr[i].sh_addr + offs;
154 sym_count = shdr[i].sh_size / sizeof(Elf_Sym);
155 } else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) {
156 strtab = shdr[i].sh_addr + offs;
161 * The most efficient way to find a symbol would be to calculate a
162 * hash, find proper bucket and chain, and thus find a symbol.
163 * However, that would involve code duplication (e.g. for hash
164 * function). So we're using simpler and a bit slower way: we're
165 * iterating through symbols, searching for the one which name is
166 * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit,
167 * we are eliminating symbols type of which is not STT_NOTYPE, or(and)
168 * those which binding attribute is not STB_GLOBAL.
171 while (sym_count > 0 && fdt_start == 0) {
172 COPYOUT(symtab, &sym, sizeof(sym));
173 symtab += sizeof(sym);
175 if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
176 ELF_ST_TYPE(sym.st_info) != STT_NOTYPE)
178 strp = strdupout(strtab + sym.st_name);
179 if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0)
180 fdt_start = (vm_offset_t)sym.st_value + offs;
187 fdt_load_dtb(vm_offset_t va)
189 struct fdt_header header;
192 debugf("fdt_load_dtb(0x%08jx)\n", (uintmax_t)va);
194 COPYOUT(va, &header, sizeof(header));
195 err = fdt_check_header(&header);
197 if (err == -FDT_ERR_BADVERSION) {
198 snprintf(command_errbuf, sizeof(command_errbuf),
199 "incompatible blob version: %d, should be: %d",
200 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
202 snprintf(command_errbuf, sizeof(command_errbuf),
203 "error validating blob: %s", fdt_strerror(err));
209 * Release previous blob
214 fdtp_size = fdt_totalsize(&header);
215 fdtp = malloc(fdtp_size);
218 command_errmsg = "can't allocate memory for device tree copy";
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);
230 fdt_load_dtb_addr(struct fdt_header *header)
234 debugf("fdt_load_dtb_addr(%p)\n", header);
236 fdtp_size = fdt_totalsize(header);
237 err = fdt_check_header(header);
239 snprintf(command_errbuf, sizeof(command_errbuf),
240 "error validating blob: %s", fdt_strerror(err));
244 if ((fdtp = malloc(fdtp_size)) == NULL) {
245 command_errmsg = "can't allocate memory for device tree copy";
249 fdtp_va = 0; // Don't write this back into module or kernel.
250 bcopy(header, fdtp, fdtp_size);
255 fdt_load_dtb_file(const char * filename)
257 struct preloaded_file *bfp, *oldbfp;
260 debugf("fdt_load_dtb_file(%s)\n", filename);
262 oldbfp = file_findfile(NULL, "dtb");
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);
270 if ((err = fdt_load_dtb(bfp->f_addr)) != 0) {
275 /* A new dtb was validated, discard any previous file. */
277 file_discard(oldbfp);
282 fdt_load_dtb_overlay(const char * filename)
284 struct preloaded_file *bfp, *oldbfp;
285 struct fdt_header header;
288 debugf("fdt_load_dtb_overlay(%s)\n", filename);
290 oldbfp = file_findfile(filename, "dtbo");
292 /* Attempt to load and validate a new dtb from a file. */
293 if ((bfp = file_loadraw(filename, "dtbo", 1)) == NULL) {
294 printf("failed to load file '%s'\n", filename);
298 COPYOUT(bfp->f_addr, &header, sizeof(header));
299 err = fdt_check_header(&header);
303 if (err == -FDT_ERR_BADVERSION)
304 printf("incompatible blob version: %d, should be: %d\n",
305 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
308 printf("error validating blob: %s\n",
313 /* A new dtb was validated, discard any previous file. */
315 file_discard(oldbfp);
321 fdt_load_dtb_overlays(const char * filenames)
327 debugf("fdt_load_dtb_overlay(%s)\n", filenames);
329 names = strdup(filenames);
334 comaptr = strchr(name, ',');
337 fdt_load_dtb_overlay(name);
348 struct preloaded_file *fp;
349 size_t overlays_size, max_overlay_size, new_fdtp_size;
354 if ((fdtp == NULL) || (fdtp_size == 0))
358 max_overlay_size = 0;
359 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) {
360 if (max_overlay_size < fp->f_size)
361 max_overlay_size = fp->f_size;
362 overlays_size += fp->f_size;
365 /* Nothing to apply */
366 if (overlays_size == 0)
369 /* It's actually more than enough */
370 new_fdtp_size = fdtp_size + overlays_size;
371 new_fdtp = malloc(new_fdtp_size);
372 if (new_fdtp == NULL) {
373 printf("failed to allocate memory for DTB blob with overlays\n");
377 overlay = malloc(max_overlay_size);
378 if (overlay == NULL) {
379 printf("failed to allocate memory for DTB blob with overlays\n");
384 rv = fdt_open_into(fdtp, new_fdtp, new_fdtp_size);
386 printf("failed to open DTB blob for applying overlays\n");
392 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) {
393 printf("applying DTB overlay '%s'\n", fp->f_name);
394 COPYOUT(fp->f_addr, overlay, fp->f_size);
395 fdt_overlay_apply(new_fdtp, overlay, fp->f_size);
400 fdtp_size = new_fdtp_size;
408 struct preloaded_file *bfp;
411 debugf("fdt_setup_fdtp()\n");
413 /* If we already loaded a file, use it. */
414 if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
415 if (fdt_load_dtb(bfp->f_addr) == 0) {
416 printf("Using DTB from loaded file '%s'.\n",
422 /* If we were given the address of a valid blob in memory, use it. */
423 if (fdt_to_load != NULL) {
424 if (fdt_load_dtb_addr(fdt_to_load) == 0) {
425 printf("Using DTB from memory address %p.\n",
431 if (fdt_platform_load_dtb() == 0)
434 /* If there is a dtb compiled into the kernel, use it. */
435 if ((va = fdt_find_static_dtb()) != 0) {
436 if (fdt_load_dtb(va) == 0) {
437 printf("Using DTB compiled into kernel.\n");
442 command_errmsg = "No device tree blob found!\n";
446 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
447 (cellbuf), (lim), (cellsize), 0);
449 /* Force using base 16 */
450 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
451 (cellbuf), (lim), (cellsize), 16);
454 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize,
457 const char *buf = str;
458 const char *end = str + strlen(str) - 2;
459 uint32_t *u32buf = NULL;
460 uint8_t *u8buf = NULL;
463 if (cellsize == sizeof(uint32_t))
464 u32buf = (uint32_t *)cellbuf;
466 u8buf = (uint8_t *)cellbuf;
473 /* Skip white whitespace(s)/separators */
474 while (!isxdigit(*buf) && buf < end)
479 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
482 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
484 if (cnt + 1 <= lim - 1)
489 /* Find another number */
490 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
497 fdt_fixup_ethernet(const char *str, char *ethstr, int len)
501 /* Convert macaddr string into a vector of uints */
502 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
503 /* Set actual property to a value from vect */
504 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
505 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
509 fdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
511 int lo, o = 0, o2, maxo = 0, depth;
512 const uint32_t zero = 0;
514 /* We want to modify every subnode of /cpus */
515 o = fdt_path_offset(fdtp, "/cpus");
519 /* maxo should contain offset of node next to /cpus */
523 maxo = fdt_next_node(fdtp, maxo, &depth);
525 /* Find CPU frequency properties */
526 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
527 &zero, sizeof(uint32_t));
529 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
534 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
536 o = fdt_node_offset_by_prop_value(fdtp, lo,
537 "clock-frequency", &zero, sizeof(uint32_t));
539 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
540 &zero, sizeof(uint32_t));
542 /* We're only interested in /cpus subnode(s) */
546 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
549 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
558 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
560 int cells_in_tuple, i, tuples, tuple_size;
561 uint32_t cur_start, cur_size;
563 cells_in_tuple = (addr_cells + size_cells);
564 tuple_size = cells_in_tuple * sizeof(uint32_t);
565 tuples = len / tuple_size;
569 for (i = 0; i < tuples; i++) {
571 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
573 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
576 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
578 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
583 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
584 i, cur_start, cur_size);
591 fdt_fixup_memory(struct fdt_mem_region *region, size_t num)
593 struct fdt_mem_region *curmr;
594 uint32_t addr_cells, size_cells;
595 uint32_t *addr_cellsp, *size_cellsp;
596 int err, i, len, memory, root;
599 uint64_t rstart, rsize;
602 root = fdt_path_offset(fdtp, "/");
604 sprintf(command_errbuf, "Could not find root node !");
608 memory = fdt_path_offset(fdtp, "/memory");
610 /* Create proper '/memory' node. */
611 memory = fdt_add_subnode(fdtp, root, "memory");
613 snprintf(command_errbuf, sizeof(command_errbuf),
614 "Could not fixup '/memory' "
615 "node, error code : %d!\n", memory);
619 err = fdt_setprop(fdtp, memory, "device_type", "memory",
626 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
628 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
630 if (addr_cellsp == NULL || size_cellsp == NULL) {
631 snprintf(command_errbuf, sizeof(command_errbuf),
632 "Could not fixup '/memory' node : "
633 "%s %s property not found in root node!\n",
634 (!addr_cellsp) ? "#address-cells" : "",
635 (!size_cellsp) ? "#size-cells" : "");
639 addr_cells = fdt32_to_cpu(*addr_cellsp);
640 size_cells = fdt32_to_cpu(*size_cellsp);
643 * Convert memreserve data to memreserve property
644 * Check if property already exists
646 reserved = fdt_num_mem_rsv(fdtp);
648 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
649 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
650 sb = buf = (uint8_t *)malloc(len);
656 for (i = 0; i < reserved; i++) {
657 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
660 /* Ensure endianness, and put cells into a buffer */
663 cpu_to_fdt64(rstart);
666 cpu_to_fdt32(rstart);
668 buf += sizeof(uint32_t) * addr_cells;
676 buf += sizeof(uint32_t) * size_cells;
681 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
682 printf("Could not fixup 'memreserve' property.\n");
687 /* Count valid memory regions entries in sysinfo. */
689 for (i = 0; i < num; i++)
690 if (region[i].start == 0 && region[i].size == 0)
694 sprintf(command_errbuf, "Could not fixup '/memory' node : "
695 "sysinfo doesn't contain valid memory regions info!\n");
699 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
700 sb = buf = (uint8_t *)malloc(len);
706 for (i = 0; i < num; i++) {
708 if (curmr->size != 0) {
709 /* Ensure endianness, and put cells into a buffer */
712 cpu_to_fdt64(curmr->start);
715 cpu_to_fdt32(curmr->start);
717 buf += sizeof(uint32_t) * addr_cells;
720 cpu_to_fdt64(curmr->size);
723 cpu_to_fdt32(curmr->size);
725 buf += sizeof(uint32_t) * size_cells;
730 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
731 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
737 fdt_fixup_stdout(const char *str)
742 const struct fdt_property *prop;
745 ptr = (char *)str + strlen(str) - 1;
746 while (ptr > str && isdigit(*(str - 1)))
752 serialno = (int)strtol(ptr, NULL, 0);
753 no = fdt_path_offset(fdtp, "/chosen");
757 prop = fdt_get_property(fdtp, no, "stdout", &len);
759 /* If /chosen/stdout does not extist, create it */
760 if (prop == NULL || (prop != NULL && len == 0)) {
762 bzero(tmp, 10 * sizeof(char));
763 strcpy((char *)&tmp, "serial");
765 /* Serial number too long */
768 strncpy((char *)tmp + 6, ptr, 3);
769 sero = fdt_path_offset(fdtp, (const char *)tmp);
772 * If serial device we're trying to assign
773 * stdout to doesn't exist in DT -- return.
777 fdt_setprop(fdtp, no, "stdout", &tmp,
778 strlen((char *)&tmp) + 1);
779 fdt_setprop(fdtp, no, "stdin", &tmp,
780 strlen((char *)&tmp) + 1);
785 * Locate the blob, fix it up and return its location.
794 debugf("fdt_fixup()\n");
796 if (fdtp == NULL && fdt_setup_fdtp() != 0)
799 /* Create /chosen node (if not exists) */
800 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
802 chosen = fdt_add_subnode(fdtp, 0, "chosen");
804 /* Value assigned to fixup-applied does not matter. */
805 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
808 fdt_platform_fixups();
810 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
815 * Copy DTB blob to specified location and return size
818 fdt_copy(vm_offset_t va)
821 debugf("fdt_copy va 0x%08x\n", va);
823 err = fdt_setup_fdtp();
825 printf("No valid device tree blob found!\n");
830 if (fdt_fixup() == 0)
834 /* Overwrite the FDT with the fixed version. */
835 /* XXX Is this really appropriate? */
836 COPYIN(fdtp, fdtp_va, fdtp_size);
838 COPYIN(fdtp, va, fdtp_size);
845 command_fdt_internal(int argc, char *argv[])
853 command_errmsg = "usage is 'fdt <command> [<args>]";
858 * Validate fdt <command>.
860 cmd = strdup(argv[1]);
863 while (!(commands[i].name == NULL)) {
864 if (strcmp(cmd, commands[i].name) == 0) {
866 cmdh = commands[i].handler;
867 flags = commands[i].flags;
873 command_errmsg = "unknown command";
877 if (flags & CMD_REQUIRES_BLOB) {
879 * Check if uboot env vars were parsed already. If not, do it now.
881 if (fdt_fixup() == 0)
886 * Call command handler.
888 err = (*cmdh)(argc, argv);
894 fdt_cmd_addr(int argc, char *argv[])
896 struct preloaded_file *fp;
897 struct fdt_header *hdr;
906 sprintf(command_errbuf, "no address specified");
910 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
912 snprintf(command_errbuf, sizeof(command_errbuf),
913 "Invalid address: %s", addr);
917 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
926 fdt_cmd_cd(int argc, char *argv[])
929 char tmp[FDT_CWD_LEN];
932 path = (argc > 2) ? argv[2] : "/";
934 if (path[0] == '/') {
936 if (len >= FDT_CWD_LEN)
939 /* Handle path specification relative to cwd */
940 len = strlen(cwd) + strlen(path) + 1;
941 if (len >= FDT_CWD_LEN)
950 o = fdt_path_offset(fdtp, path);
952 snprintf(command_errbuf, sizeof(command_errbuf),
953 "could not find node: '%s'", path);
961 snprintf(command_errbuf, sizeof(command_errbuf),
962 "path too long: %d, max allowed: %d", len, FDT_CWD_LEN - 1);
967 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
973 command_errmsg = "no device tree blob pointer?!";
977 ver = fdt_version(fdtp);
979 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
980 if (pager_output(line))
982 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
983 if (pager_output(line))
985 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
986 if (pager_output(line))
988 sprintf(line, " off_dt_struct = 0x%08x\n",
989 fdt_off_dt_struct(fdtp));
990 if (pager_output(line))
992 sprintf(line, " off_dt_strings = 0x%08x\n",
993 fdt_off_dt_strings(fdtp));
994 if (pager_output(line))
996 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
997 fdt_off_mem_rsvmap(fdtp));
998 if (pager_output(line))
1000 sprintf(line, " version = %d\n", ver);
1001 if (pager_output(line))
1003 sprintf(line, " last compatible version = %d\n",
1004 fdt_last_comp_version(fdtp));
1005 if (pager_output(line))
1008 sprintf(line, " boot_cpuid = %d\n",
1009 fdt_boot_cpuid_phys(fdtp));
1010 if (pager_output(line))
1014 sprintf(line, " size_dt_strings = %d\n",
1015 fdt_size_dt_strings(fdtp));
1016 if (pager_output(line))
1020 sprintf(line, " size_dt_struct = %d\n",
1021 fdt_size_dt_struct(fdtp));
1022 if (pager_output(line))
1032 fdt_cmd_ls(int argc, char *argv[])
1034 const char *prevname[FDT_MAX_DEPTH] = { NULL };
1039 path = (argc > 2) ? argv[2] : NULL;
1043 o = fdt_path_offset(fdtp, path);
1045 snprintf(command_errbuf, sizeof(command_errbuf),
1046 "could not find node: '%s'", path);
1051 (o >= 0) && (depth >= 0);
1052 o = fdt_next_node(fdtp, o, &depth)) {
1054 name = fdt_get_name(fdtp, o, NULL);
1056 if (depth > FDT_MAX_DEPTH) {
1057 printf("max depth exceeded: %d\n", depth);
1061 prevname[depth] = name;
1063 /* Skip root (i = 1) when printing devices */
1064 for (i = 1; i <= depth; i++) {
1065 if (prevname[i] == NULL)
1068 if (strcmp(cwd, "/") == 0)
1070 printf("%s", prevname[i]);
1082 return (c >= ' ' && c <= 0x7e);
1086 fdt_isprint(const void *data, int len, int *count)
1095 d = (const char *)data;
1096 if (d[len - 1] != '\0')
1101 for (i = 0; i < len; i++) {
1103 if (isprint(ch) || (ch == '\0' && i > 0)) {
1118 fdt_data_str(const void *data, int len, int count, char **buf)
1125 * Calculate the length for the string and allocate memory.
1127 * Note that 'len' already includes at least one terminator.
1132 * Each token had already a terminator buried in 'len', but we
1133 * only need one eventually, don't count space for these.
1135 buf_len -= count - 1;
1137 /* Each consecutive token requires a ", " separator. */
1138 buf_len += count * 2;
1141 /* Add some space for surrounding double quotes. */
1142 buf_len += count * 2;
1144 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1145 b = (char *)malloc(buf_len);
1146 tmp = (char *)malloc(buf_len);
1158 * Now that we have space, format the string.
1162 d = (const char *)data + i;
1165 sprintf(tmp, "\"%s\"%s", d,
1166 (i + l) < len ? ", " : "");
1182 fdt_data_cell(const void *data, int len, char **buf)
1188 /* Number of cells */
1192 * Calculate the length for the string and allocate memory.
1195 /* Each byte translates to 2 output characters */
1198 /* Each consecutive cell requires a " " separator. */
1199 l += (count - 1) * 1;
1201 /* Each cell will have a "0x" prefix */
1203 /* Space for surrounding <> and terminator */
1206 b = (char *)malloc(l);
1207 tmp = (char *)malloc(l);
1219 for (i = 0; i < len; i += 4) {
1220 c = (const uint32_t *)((const uint8_t *)data + i);
1221 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1222 i < (len - 4) ? " " : "");
1236 fdt_data_bytes(const void *data, int len, char **buf)
1243 * Calculate the length for the string and allocate memory.
1246 /* Each byte translates to 2 output characters */
1249 /* Each consecutive byte requires a " " separator. */
1251 /* Each byte will have a "0x" prefix */
1253 /* Space for surrounding [] and terminator. */
1256 b = (char *)malloc(l);
1257 tmp = (char *)malloc(l);
1269 for (i = 0, d = data; i < len; i++) {
1270 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1284 fdt_data_fmt(const void *data, int len, char **buf)
1293 if (fdt_isprint(data, len, &count))
1294 return (fdt_data_str(data, len, count, buf));
1296 else if ((len % 4) == 0)
1297 return (fdt_data_cell(data, len, buf));
1300 return (fdt_data_bytes(data, len, buf));
1304 fdt_prop(int offset)
1307 const struct fdt_property *prop;
1313 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1317 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1318 len = fdt32_to_cpu(prop->len);
1323 /* Property without value */
1324 line = (char *)malloc(strlen(name) + 2);
1329 sprintf(line, "%s\n", name);
1334 * Process property with value
1338 if (fdt_data_fmt(data, len, &buf) != 0) {
1343 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1346 sprintf(command_errbuf, "could not allocate space for string");
1351 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1369 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1371 uint32_t cells[100];
1374 const struct fdt_property *p;
1376 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1380 /* Adding inexistant value in mode 1 is forbidden */
1381 sprintf(command_errbuf, "property already exists!");
1384 } else if (mode == 0) {
1385 sprintf(command_errbuf, "property does not exist!");
1388 len = strlen(value);
1398 len = fdt_strtovect(buf, (void *)&cells, 100,
1401 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1402 len * sizeof(uint32_t));
1406 len = fdt_strtovect(buf, (void *)&cells, 100,
1409 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1410 len * sizeof(uint8_t));
1414 /* Default -- string */
1415 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1420 if (rv == -FDT_ERR_NOSPACE)
1421 sprintf(command_errbuf,
1422 "Device tree blob is too small!\n");
1424 sprintf(command_errbuf,
1425 "Could not add/modify property!\n");
1430 /* Merge strings from argv into a single string */
1432 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1440 for (i = start; i < argc; i++)
1441 sz += strlen(argv[i]);
1443 /* Additional bytes for whitespaces between args */
1446 buf = (char *)malloc(sizeof(char) * sz);
1448 sprintf(command_errbuf, "could not allocate space "
1452 bzero(buf, sizeof(char) * sz);
1455 for (i = start, idx = 0; i < argc; i++) {
1456 strcpy(buf + idx, argv[i]);
1457 idx += strlen(argv[i]);
1466 /* Extract offset and name of node/property from a given path */
1468 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1471 char *path = *pathp, *name = NULL, *subpath = NULL;
1473 subpath = strrchr(path, '/');
1474 if (subpath == NULL) {
1475 o = fdt_path_offset(fdtp, cwd);
1477 path = (char *)&cwd;
1480 if (strlen(path) == 0)
1484 o = fdt_path_offset(fdtp, path);
1487 if (strlen(name) == 0) {
1488 sprintf(command_errbuf, "name not specified");
1492 snprintf(command_errbuf, sizeof(command_errbuf),
1493 "could not find node: '%s'", path);
1503 fdt_cmd_prop(int argc, char *argv[])
1505 char *path, *propname, *value;
1506 int o, next, depth, rv;
1509 path = (argc > 2) ? argv[2] : NULL;
1514 /* Merge property value strings into one */
1515 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1526 /* If value is specified -- try to modify prop. */
1527 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1530 rv = fdt_modprop(o, propname, value, 0);
1536 /* User wants to display properties */
1537 o = fdt_path_offset(fdtp, path);
1540 snprintf(command_errbuf, sizeof(command_errbuf),
1541 "could not find node: '%s'", path);
1547 while (depth >= 0) {
1548 tag = fdt_next_tag(fdtp, o, &next);
1554 /* Don't process properties of nested nodes */
1557 if (fdt_prop(o) != 0) {
1558 sprintf(command_errbuf, "could not process "
1564 case FDT_BEGIN_NODE:
1566 if (depth > FDT_MAX_DEPTH) {
1567 printf("warning: nesting too deep: %d\n",
1576 * This is the end of our starting node, force
1589 fdt_cmd_mkprop(int argc, char *argv[])
1592 char *path, *propname, *value;
1594 path = (argc > 2) ? argv[2] : NULL;
1599 /* Merge property value strings into one */
1600 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1605 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1608 if (fdt_modprop(o, propname, value, 1))
1615 fdt_cmd_rm(int argc, char *argv[])
1618 char *path = NULL, *propname;
1623 sprintf(command_errbuf, "no node/property name specified");
1627 o = fdt_path_offset(fdtp, path);
1629 /* If node not found -- try to find & delete property */
1630 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1633 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1634 snprintf(command_errbuf, sizeof(command_errbuf),
1635 "could not delete %s\n",
1636 (rv == -FDT_ERR_NOTFOUND) ?
1637 "(property/node does not exist)" : "");
1643 /* If node exists -- remove node */
1644 rv = fdt_del_node(fdtp, o);
1646 sprintf(command_errbuf, "could not delete node");
1653 fdt_cmd_mknode(int argc, char *argv[])
1656 char *path = NULL, *nodename = NULL;
1661 sprintf(command_errbuf, "no node name specified");
1665 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1668 rv = fdt_add_subnode(fdtp, o, nodename);
1671 if (rv == -FDT_ERR_NOSPACE)
1672 sprintf(command_errbuf,
1673 "Device tree blob is too small!\n");
1675 sprintf(command_errbuf,
1676 "Could not add node!\n");
1683 fdt_cmd_pwd(int argc, char *argv[])
1685 char line[FDT_CWD_LEN];
1688 sprintf(line, "%s\n", cwd);
1695 fdt_cmd_mres(int argc, char *argv[])
1697 uint64_t start, size;
1702 total = fdt_num_mem_rsv(fdtp);
1704 if (pager_output("Reserved memory regions:\n"))
1706 for (i = 0; i < total; i++) {
1707 fdt_get_mem_rsv(fdtp, i, &start, &size);
1708 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1710 if (pager_output(line))
1714 pager_output("No reserved memory regions\n");
1722 fdt_cmd_nyi(int argc, char *argv[])
1725 printf("command not yet implemented\n");