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"
44 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
45 printf(fmt,##args); } while (0)
47 #define debugf(fmt, args...)
50 #define FDT_CWD_LEN 256
51 #define FDT_MAX_DEPTH 6
53 #define FDT_PROP_SEP " = "
55 #define STR(number) #number
56 #define STRINGIFY(number) STR(number)
58 #define COPYOUT(s,d,l) archsw.arch_copyout(s, d, l)
59 #define COPYIN(s,d,l) archsw.arch_copyin(s, d, l)
61 #define FDT_STATIC_DTB_SYMBOL "fdt_static_dtb"
63 #define CMD_REQUIRES_BLOB 0x01
65 /* Location of FDT yet to be loaded. */
66 /* This may be in read-only memory, so can't be manipulated directly. */
67 static struct fdt_header *fdt_to_load = NULL;
68 /* Location of FDT on heap. */
69 /* This is the copy we actually manipulate. */
70 static struct fdt_header *fdtp = NULL;
71 /* Size of FDT blob */
72 static size_t fdtp_size = 0;
73 /* Location of FDT in kernel or module. */
74 /* This won't be set if FDT is loaded from disk or memory. */
75 /* If it is set, we'll update it when fdt_copy() gets called. */
76 static vm_offset_t fdtp_va = 0;
78 static int fdt_load_dtb(vm_offset_t va);
80 static int fdt_cmd_nyi(int argc, char *argv[]);
82 static int fdt_cmd_addr(int argc, char *argv[]);
83 static int fdt_cmd_mkprop(int argc, char *argv[]);
84 static int fdt_cmd_cd(int argc, char *argv[]);
85 static int fdt_cmd_hdr(int argc, char *argv[]);
86 static int fdt_cmd_ls(int argc, char *argv[]);
87 static int fdt_cmd_prop(int argc, char *argv[]);
88 static int fdt_cmd_pwd(int argc, char *argv[]);
89 static int fdt_cmd_rm(int argc, char *argv[]);
90 static int fdt_cmd_mknode(int argc, char *argv[]);
91 static int fdt_cmd_mres(int argc, char *argv[]);
93 typedef int cmdf_t(int, char *[]);
101 static const struct cmdtab commands[] = {
102 { "addr", &fdt_cmd_addr, 0 },
103 { "alias", &fdt_cmd_nyi, 0 },
104 { "cd", &fdt_cmd_cd, CMD_REQUIRES_BLOB },
105 { "header", &fdt_cmd_hdr, CMD_REQUIRES_BLOB },
106 { "ls", &fdt_cmd_ls, CMD_REQUIRES_BLOB },
107 { "mknode", &fdt_cmd_mknode, CMD_REQUIRES_BLOB },
108 { "mkprop", &fdt_cmd_mkprop, CMD_REQUIRES_BLOB },
109 { "mres", &fdt_cmd_mres, CMD_REQUIRES_BLOB },
110 { "prop", &fdt_cmd_prop, CMD_REQUIRES_BLOB },
111 { "pwd", &fdt_cmd_pwd, CMD_REQUIRES_BLOB },
112 { "rm", &fdt_cmd_rm, CMD_REQUIRES_BLOB },
116 static char cwd[FDT_CWD_LEN] = "/";
119 fdt_find_static_dtb()
124 vm_offset_t strtab, symtab, fdt_start;
126 struct preloaded_file *kfp;
127 struct file_metadata *md;
131 debugf("fdt_find_static_dtb()\n");
133 sym_count = symtab = strtab = 0;
136 offs = __elfN(relocation_offset);
138 kfp = file_findfile(NULL, NULL);
142 /* Locate the dynamic symbols and strtab. */
143 md = file_findmetadata(kfp, MODINFOMD_ELFHDR);
146 ehdr = (Elf_Ehdr *)md->md_data;
148 md = file_findmetadata(kfp, MODINFOMD_SHDR);
151 shdr = (Elf_Shdr *)md->md_data;
153 for (i = 0; i < ehdr->e_shnum; ++i) {
154 if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) {
155 symtab = shdr[i].sh_addr + offs;
156 sym_count = shdr[i].sh_size / sizeof(Elf_Sym);
157 } else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) {
158 strtab = shdr[i].sh_addr + offs;
163 * The most efficent way to find a symbol would be to calculate a
164 * hash, find proper bucket and chain, and thus find a symbol.
165 * However, that would involve code duplication (e.g. for hash
166 * function). So we're using simpler and a bit slower way: we're
167 * iterating through symbols, searching for the one which name is
168 * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit,
169 * we are eliminating symbols type of which is not STT_NOTYPE, or(and)
170 * those which binding attribute is not STB_GLOBAL.
173 while (sym_count > 0 && fdt_start == 0) {
174 COPYOUT(symtab, &sym, sizeof(sym));
175 symtab += sizeof(sym);
177 if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
178 ELF_ST_TYPE(sym.st_info) != STT_NOTYPE)
180 strp = strdupout(strtab + sym.st_name);
181 if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0)
182 fdt_start = (vm_offset_t)sym.st_value + offs;
189 fdt_load_dtb(vm_offset_t va)
191 struct fdt_header header;
194 debugf("fdt_load_dtb(0x%08jx)\n", (uintmax_t)va);
196 COPYOUT(va, &header, sizeof(header));
197 err = fdt_check_header(&header);
199 if (err == -FDT_ERR_BADVERSION)
200 sprintf(command_errbuf,
201 "incompatible blob version: %d, should be: %d",
202 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
205 sprintf(command_errbuf, "error validating blob: %s",
211 * Release previous blob
216 fdtp_size = fdt_totalsize(&header);
217 fdtp = malloc(fdtp_size);
220 command_errmsg = "can't allocate memory for device tree copy";
225 COPYOUT(va, fdtp, fdtp_size);
226 debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
232 fdt_load_dtb_addr(struct fdt_header *header)
236 debugf("fdt_load_dtb_addr(0x%p)\n", header);
238 fdtp_size = fdt_totalsize(header);
239 err = fdt_check_header(header);
241 sprintf(command_errbuf, "error validating blob: %s",
246 if ((fdtp = malloc(fdtp_size)) == NULL) {
247 command_errmsg = "can't allocate memory for device tree copy";
251 fdtp_va = 0; // Don't write this back into module or kernel.
252 bcopy(header, fdtp, fdtp_size);
257 fdt_load_dtb_file(const char * filename)
259 struct preloaded_file *bfp, *oldbfp;
262 debugf("fdt_load_dtb_file(%s)\n", filename);
264 oldbfp = file_findfile(NULL, "dtb");
266 /* Attempt to load and validate a new dtb from a file. */
267 if ((bfp = file_loadraw(filename, "dtb")) == NULL) {
268 sprintf(command_errbuf, "failed to load file '%s'", filename);
271 if ((err = fdt_load_dtb(bfp->f_addr)) != 0) {
276 /* A new dtb was validated, discard any previous file. */
278 file_discard(oldbfp);
285 struct preloaded_file *bfp;
286 struct fdt_header *hdr;
291 debugf("fdt_setup_fdtp()\n");
293 /* If we already loaded a file, use it. */
294 if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
295 if (fdt_load_dtb(bfp->f_addr) == 0) {
296 printf("Using DTB from loaded file '%s'.\n",
302 /* If we were given the address of a valid blob in memory, use it. */
303 if (fdt_to_load != NULL) {
304 if (fdt_load_dtb_addr(fdt_to_load) == 0) {
305 printf("Using DTB from memory address 0x%08X.\n",
306 (unsigned int)fdt_to_load);
312 * If the U-boot environment contains a variable giving the address of a
313 * valid blob in memory, use it. The U-boot README says the right
314 * variable for fdt data loaded into ram is fdt_addr_r, so try that
315 * first. Board vendors also use both fdtaddr and fdt_addr names.
317 s = ub_env_get("fdt_addr_r");
319 s = ub_env_get("fdtaddr");
321 s = ub_env_get("fdt_addr");
322 if (s != NULL && *s != '\0') {
323 hdr = (struct fdt_header *)strtoul(s, &p, 16);
325 if (fdt_load_dtb_addr(hdr) == 0) {
326 printf("Using DTB provided by U-Boot at "
327 "address 0x%p.\n", hdr);
334 * If the U-boot environment contains a variable giving the name of a
335 * file, use it if we can load and validate it.
337 s = ub_env_get("fdtfile");
339 s = ub_env_get("fdt_file");
340 if (s != NULL && *s != '\0') {
341 if (fdt_load_dtb_file(s) == 0) {
342 printf("Loaded DTB from file '%s'.\n", s);
347 /* If there is a dtb compiled into the kernel, use it. */
348 if ((va = fdt_find_static_dtb()) != 0) {
349 if (fdt_load_dtb(va) == 0) {
350 printf("Using DTB compiled into kernel.\n");
355 command_errmsg = "No device tree blob found!\n";
359 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
360 (cellbuf), (lim), (cellsize), 0);
362 /* Force using base 16 */
363 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
364 (cellbuf), (lim), (cellsize), 16);
367 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize,
370 const char *buf = str;
371 const char *end = str + strlen(str) - 2;
372 uint32_t *u32buf = NULL;
373 uint8_t *u8buf = NULL;
376 if (cellsize == sizeof(uint32_t))
377 u32buf = (uint32_t *)cellbuf;
379 u8buf = (uint8_t *)cellbuf;
386 /* Skip white whitespace(s)/separators */
387 while (!isxdigit(*buf) && buf < end)
392 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
395 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
397 if (cnt + 1 <= lim - 1)
402 /* Find another number */
403 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
409 #define TMP_MAX_ETH 8
412 fixup_ethernet(const char *env, char *ethstr, int *eth_no, int len)
419 /* Extract interface number */
420 i = strtol(env + 3, &end, 10);
421 if (end == (env + 3))
422 /* 'ethaddr' means interface 0 address */
430 str = ub_env_get(env);
432 /* Convert macaddr string into a vector of uints */
433 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
436 strncpy(ethstr + 8, env + 3, i);
438 /* Set actual property to a value from vect */
439 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
440 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
442 /* Clear ethernet..XXXX.. string */
443 bzero(ethstr + 8, len - 8);
450 fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
452 int lo, o = 0, o2, maxo = 0, depth;
453 const uint32_t zero = 0;
455 /* We want to modify every subnode of /cpus */
456 o = fdt_path_offset(fdtp, "/cpus");
460 /* maxo should contain offset of node next to /cpus */
464 maxo = fdt_next_node(fdtp, maxo, &depth);
466 /* Find CPU frequency properties */
467 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
468 &zero, sizeof(uint32_t));
470 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
475 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
477 o = fdt_node_offset_by_prop_value(fdtp, lo,
478 "clock-frequency", &zero, sizeof(uint32_t));
480 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
481 &zero, sizeof(uint32_t));
483 /* We're only interested in /cpus subnode(s) */
487 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
490 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
498 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
500 int cells_in_tuple, i, tuples, tuple_size;
501 uint32_t cur_start, cur_size;
503 cells_in_tuple = (addr_cells + size_cells);
504 tuple_size = cells_in_tuple * sizeof(uint32_t);
505 tuples = len / tuple_size;
509 for (i = 0; i < tuples; i++) {
511 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
513 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
516 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
518 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
523 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
524 i, cur_start, cur_size);
530 fixup_memory(struct sys_info *si)
532 struct mem_region *curmr;
533 uint32_t addr_cells, size_cells;
534 uint32_t *addr_cellsp, *reg, *size_cellsp;
535 int err, i, len, memory, realmrno, root;
537 uint64_t rstart, rsize;
540 root = fdt_path_offset(fdtp, "/");
542 sprintf(command_errbuf, "Could not find root node !");
546 memory = fdt_path_offset(fdtp, "/memory");
548 /* Create proper '/memory' node. */
549 memory = fdt_add_subnode(fdtp, root, "memory");
551 sprintf(command_errbuf, "Could not fixup '/memory' "
552 "node, error code : %d!\n", memory);
556 err = fdt_setprop(fdtp, memory, "device_type", "memory",
563 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
565 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
567 if (addr_cellsp == NULL || size_cellsp == NULL) {
568 sprintf(command_errbuf, "Could not fixup '/memory' node : "
569 "%s %s property not found in root node!\n",
570 (!addr_cellsp) ? "#address-cells" : "",
571 (!size_cellsp) ? "#size-cells" : "");
575 addr_cells = fdt32_to_cpu(*addr_cellsp);
576 size_cells = fdt32_to_cpu(*size_cellsp);
579 * Convert memreserve data to memreserve property
580 * Check if property already exists
582 reserved = fdt_num_mem_rsv(fdtp);
584 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
585 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
586 sb = buf = (uint8_t *)malloc(len);
592 for (i = 0; i < reserved; i++) {
594 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
597 /* Ensure endianess, and put cells into a buffer */
600 cpu_to_fdt64(rstart);
603 cpu_to_fdt32(rstart);
605 buf += sizeof(uint32_t) * addr_cells;
613 buf += sizeof(uint32_t) * size_cells;
618 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
619 printf("Could not fixup 'memreserve' property.\n");
624 /* Count valid memory regions entries in sysinfo. */
625 realmrno = si->mr_no;
626 for (i = 0; i < si->mr_no; i++)
627 if (si->mr[i].start == 0 && si->mr[i].size == 0)
631 sprintf(command_errbuf, "Could not fixup '/memory' node : "
632 "sysinfo doesn't contain valid memory regions info!\n");
636 if ((reg = (uint32_t *)fdt_getprop(fdtp, memory, "reg",
639 if (fdt_reg_valid(reg, len, addr_cells, size_cells) == 0)
641 * Do not apply fixup if existing 'reg' property
647 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
648 sb = buf = (uint8_t *)malloc(len);
654 for (i = 0; i < si->mr_no; i++) {
656 if (curmr->size != 0) {
657 /* Ensure endianess, and put cells into a buffer */
660 cpu_to_fdt64(curmr->start);
663 cpu_to_fdt32(curmr->start);
665 buf += sizeof(uint32_t) * addr_cells;
668 cpu_to_fdt64(curmr->size);
671 cpu_to_fdt32(curmr->size);
673 buf += sizeof(uint32_t) * size_cells;
678 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
679 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
685 fixup_stdout(const char *env)
691 const struct fdt_property *prop;
694 str = ub_env_get(env);
695 ptr = (char *)str + strlen(str) - 1;
696 while (ptr > str && isdigit(*(str - 1)))
702 serialno = (int)strtol(ptr, NULL, 0);
703 no = fdt_path_offset(fdtp, "/chosen");
707 prop = fdt_get_property(fdtp, no, "stdout", &len);
709 /* If /chosen/stdout does not extist, create it */
710 if (prop == NULL || (prop != NULL && len == 0)) {
712 bzero(tmp, 10 * sizeof(char));
713 strcpy((char *)&tmp, "serial");
715 /* Serial number too long */
718 strncpy((char *)tmp + 6, ptr, 3);
719 sero = fdt_path_offset(fdtp, (const char *)tmp);
722 * If serial device we're trying to assign
723 * stdout to doesn't exist in DT -- return.
727 fdt_setprop(fdtp, no, "stdout", &tmp,
728 strlen((char *)&tmp) + 1);
729 fdt_setprop(fdtp, no, "stdin", &tmp,
730 strlen((char *)&tmp) + 1);
735 * Locate the blob, fix it up and return its location.
742 int chosen, eth_no, len;
750 debugf("fdt_fixup()\n");
752 if (fdtp == NULL && fdt_setup_fdtp() != 0)
755 /* Create /chosen node (if not exists) */
756 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
758 chosen = fdt_add_subnode(fdtp, 0, "chosen");
760 /* Value assigned to fixup-applied does not matter. */
761 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
764 /* Acquire sys_info */
765 si = ub_get_sys_info();
767 while ((env = ub_env_enum(env)) != NULL) {
768 if (strncmp(env, "eth", 3) == 0 &&
769 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
771 * Handle Ethernet addrs: parse uboot env eth%daddr
776 * Check how many chars we will need to store
777 * maximal eth iface number.
779 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
783 * Reserve mem for string "ethernet" and len
784 * chars for iface no.
786 ethstr = (char *)malloc(len * sizeof(char));
787 bzero(ethstr, len * sizeof(char));
788 strcpy(ethstr, "ethernet0");
792 fixup_ethernet(env, ethstr, ð_no, len);
794 } else if (strcmp(env, "consoledev") == 0)
798 /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
799 fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
801 /* Fixup memory regions */
804 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
809 * Copy DTB blob to specified location and return size
812 fdt_copy(vm_offset_t va)
815 debugf("fdt_copy va 0x%08x\n", va);
817 err = fdt_setup_fdtp();
819 printf("No valid device tree blob found!\n");
824 if (fdt_fixup() == 0)
828 /* Overwrite the FDT with the fixed version. */
829 /* XXX Is this really appropriate? */
830 COPYIN(fdtp, fdtp_va, fdtp_size);
832 COPYIN(fdtp, va, fdtp_size);
839 command_fdt_internal(int argc, char *argv[])
847 command_errmsg = "usage is 'fdt <command> [<args>]";
852 * Validate fdt <command>.
854 cmd = strdup(argv[1]);
857 while (!(commands[i].name == NULL)) {
858 if (strcmp(cmd, commands[i].name) == 0) {
860 cmdh = commands[i].handler;
861 flags = commands[i].flags;
867 command_errmsg = "unknown command";
871 if (flags & CMD_REQUIRES_BLOB) {
873 * Check if uboot env vars were parsed already. If not, do it now.
875 if (fdt_fixup() == 0)
880 * Call command handler.
882 err = (*cmdh)(argc, argv);
888 fdt_cmd_addr(int argc, char *argv[])
890 struct preloaded_file *fp;
891 struct fdt_header *hdr;
900 sprintf(command_errbuf, "no address specified");
904 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
906 sprintf(command_errbuf, "Invalid address: %s", addr);
910 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
919 fdt_cmd_cd(int argc, char *argv[])
922 char tmp[FDT_CWD_LEN];
925 path = (argc > 2) ? argv[2] : "/";
927 if (path[0] == '/') {
929 if (len >= FDT_CWD_LEN)
932 /* Handle path specification relative to cwd */
933 len = strlen(cwd) + strlen(path) + 1;
934 if (len >= FDT_CWD_LEN)
943 o = fdt_path_offset(fdtp, path);
945 sprintf(command_errbuf, "could not find node: '%s'", path);
953 sprintf(command_errbuf, "path too long: %d, max allowed: %d",
954 len, FDT_CWD_LEN - 1);
959 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
965 command_errmsg = "no device tree blob pointer?!";
969 ver = fdt_version(fdtp);
971 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
973 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
975 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
977 sprintf(line, " off_dt_struct = 0x%08x\n",
978 fdt_off_dt_struct(fdtp));
980 sprintf(line, " off_dt_strings = 0x%08x\n",
981 fdt_off_dt_strings(fdtp));
983 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
984 fdt_off_mem_rsvmap(fdtp));
986 sprintf(line, " version = %d\n", ver);
988 sprintf(line, " last compatible version = %d\n",
989 fdt_last_comp_version(fdtp));
992 sprintf(line, " boot_cpuid = %d\n",
993 fdt_boot_cpuid_phys(fdtp));
997 sprintf(line, " size_dt_strings = %d\n",
998 fdt_size_dt_strings(fdtp));
1002 sprintf(line, " size_dt_struct = %d\n",
1003 fdt_size_dt_struct(fdtp));
1012 fdt_cmd_ls(int argc, char *argv[])
1014 const char *prevname[FDT_MAX_DEPTH] = { NULL };
1017 int i, o, depth, len;
1019 path = (argc > 2) ? argv[2] : NULL;
1023 o = fdt_path_offset(fdtp, path);
1025 sprintf(command_errbuf, "could not find node: '%s'", path);
1030 (o >= 0) && (depth >= 0);
1031 o = fdt_next_node(fdtp, o, &depth)) {
1033 name = fdt_get_name(fdtp, o, &len);
1035 if (depth > FDT_MAX_DEPTH) {
1036 printf("max depth exceeded: %d\n", depth);
1040 prevname[depth] = name;
1042 /* Skip root (i = 1) when printing devices */
1043 for (i = 1; i <= depth; i++) {
1044 if (prevname[i] == NULL)
1047 if (strcmp(cwd, "/") == 0)
1049 printf("%s", prevname[i]);
1061 return (c >= ' ' && c <= 0x7e);
1065 fdt_isprint(const void *data, int len, int *count)
1074 d = (const char *)data;
1075 if (d[len - 1] != '\0')
1080 for (i = 0; i < len; i++) {
1082 if (isprint(ch) || (ch == '\0' && i > 0)) {
1097 fdt_data_str(const void *data, int len, int count, char **buf)
1104 * Calculate the length for the string and allocate memory.
1106 * Note that 'len' already includes at least one terminator.
1111 * Each token had already a terminator buried in 'len', but we
1112 * only need one eventually, don't count space for these.
1114 buf_len -= count - 1;
1116 /* Each consecutive token requires a ", " separator. */
1117 buf_len += count * 2;
1120 /* Add some space for surrounding double quotes. */
1121 buf_len += count * 2;
1123 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1124 b = (char *)malloc(buf_len);
1125 tmp = (char *)malloc(buf_len);
1137 * Now that we have space, format the string.
1141 d = (const char *)data + i;
1144 sprintf(tmp, "\"%s\"%s", d,
1145 (i + l) < len ? ", " : "");
1161 fdt_data_cell(const void *data, int len, char **buf)
1167 /* Number of cells */
1171 * Calculate the length for the string and allocate memory.
1174 /* Each byte translates to 2 output characters */
1177 /* Each consecutive cell requires a " " separator. */
1178 l += (count - 1) * 1;
1180 /* Each cell will have a "0x" prefix */
1182 /* Space for surrounding <> and terminator */
1185 b = (char *)malloc(l);
1186 tmp = (char *)malloc(l);
1198 for (i = 0; i < len; i += 4) {
1199 c = (const uint32_t *)((const uint8_t *)data + i);
1200 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1201 i < (len - 4) ? " " : "");
1215 fdt_data_bytes(const void *data, int len, char **buf)
1222 * Calculate the length for the string and allocate memory.
1225 /* Each byte translates to 2 output characters */
1228 /* Each consecutive byte requires a " " separator. */
1230 /* Each byte will have a "0x" prefix */
1232 /* Space for surrounding [] and terminator. */
1235 b = (char *)malloc(l);
1236 tmp = (char *)malloc(l);
1248 for (i = 0, d = data; i < len; i++) {
1249 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1263 fdt_data_fmt(const void *data, int len, char **buf)
1272 if (fdt_isprint(data, len, &count))
1273 return (fdt_data_str(data, len, count, buf));
1275 else if ((len % 4) == 0)
1276 return (fdt_data_cell(data, len, buf));
1279 return (fdt_data_bytes(data, len, buf));
1283 fdt_prop(int offset)
1286 const struct fdt_property *prop;
1292 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1296 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1297 len = fdt32_to_cpu(prop->len);
1302 /* Property without value */
1303 line = (char *)malloc(strlen(name) + 2);
1308 sprintf(line, "%s\n", name);
1313 * Process property with value
1317 if (fdt_data_fmt(data, len, &buf) != 0) {
1322 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1325 sprintf(command_errbuf, "could not allocate space for string");
1330 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1348 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1350 uint32_t cells[100];
1353 const struct fdt_property *p;
1355 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1359 /* Adding inexistant value in mode 1 is forbidden */
1360 sprintf(command_errbuf, "property already exists!");
1363 } else if (mode == 0) {
1364 sprintf(command_errbuf, "property does not exist!");
1367 len = strlen(value);
1377 len = fdt_strtovect(buf, (void *)&cells, 100,
1380 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1381 len * sizeof(uint32_t));
1385 len = fdt_strtovect(buf, (void *)&cells, 100,
1388 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1389 len * sizeof(uint8_t));
1393 /* Default -- string */
1394 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1399 if (rv == -FDT_ERR_NOSPACE)
1400 sprintf(command_errbuf,
1401 "Device tree blob is too small!\n");
1403 sprintf(command_errbuf,
1404 "Could not add/modify property!\n");
1409 /* Merge strings from argv into a single string */
1411 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1419 for (i = start; i < argc; i++)
1420 sz += strlen(argv[i]);
1422 /* Additional bytes for whitespaces between args */
1425 buf = (char *)malloc(sizeof(char) * sz);
1426 bzero(buf, sizeof(char) * sz);
1429 sprintf(command_errbuf, "could not allocate space "
1435 for (i = start, idx = 0; i < argc; i++) {
1436 strcpy(buf + idx, argv[i]);
1437 idx += strlen(argv[i]);
1446 /* Extract offset and name of node/property from a given path */
1448 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1451 char *path = *pathp, *name = NULL, *subpath = NULL;
1453 subpath = strrchr(path, '/');
1454 if (subpath == NULL) {
1455 o = fdt_path_offset(fdtp, cwd);
1457 path = (char *)&cwd;
1460 if (strlen(path) == 0)
1464 o = fdt_path_offset(fdtp, path);
1467 if (strlen(name) == 0) {
1468 sprintf(command_errbuf, "name not specified");
1472 sprintf(command_errbuf, "could not find node: '%s'", path);
1482 fdt_cmd_prop(int argc, char *argv[])
1484 char *path, *propname, *value;
1485 int o, next, depth, rv;
1488 path = (argc > 2) ? argv[2] : NULL;
1493 /* Merge property value strings into one */
1494 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1505 /* If value is specified -- try to modify prop. */
1506 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1509 rv = fdt_modprop(o, propname, value, 0);
1515 /* User wants to display properties */
1516 o = fdt_path_offset(fdtp, path);
1519 sprintf(command_errbuf, "could not find node: '%s'", path);
1525 while (depth >= 0) {
1526 tag = fdt_next_tag(fdtp, o, &next);
1532 /* Don't process properties of nested nodes */
1535 if (fdt_prop(o) != 0) {
1536 sprintf(command_errbuf, "could not process "
1542 case FDT_BEGIN_NODE:
1544 if (depth > FDT_MAX_DEPTH) {
1545 printf("warning: nesting too deep: %d\n",
1554 * This is the end of our starting node, force
1567 fdt_cmd_mkprop(int argc, char *argv[])
1570 char *path, *propname, *value;
1572 path = (argc > 2) ? argv[2] : NULL;
1577 /* Merge property value strings into one */
1578 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1583 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1586 if (fdt_modprop(o, propname, value, 1))
1593 fdt_cmd_rm(int argc, char *argv[])
1596 char *path = NULL, *propname;
1601 sprintf(command_errbuf, "no node/property name specified");
1605 o = fdt_path_offset(fdtp, path);
1607 /* If node not found -- try to find & delete property */
1608 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1611 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1612 sprintf(command_errbuf, "could not delete"
1613 "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1614 "(property/node does not exist)" : "");
1620 /* If node exists -- remove node */
1621 rv = fdt_del_node(fdtp, o);
1623 sprintf(command_errbuf, "could not delete node");
1630 fdt_cmd_mknode(int argc, char *argv[])
1633 char *path = NULL, *nodename = NULL;
1638 sprintf(command_errbuf, "no node name specified");
1642 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1645 rv = fdt_add_subnode(fdtp, o, nodename);
1648 if (rv == -FDT_ERR_NOSPACE)
1649 sprintf(command_errbuf,
1650 "Device tree blob is too small!\n");
1652 sprintf(command_errbuf,
1653 "Could not add node!\n");
1660 fdt_cmd_pwd(int argc, char *argv[])
1662 char line[FDT_CWD_LEN];
1665 sprintf(line, "%s\n", cwd);
1672 fdt_cmd_mres(int argc, char *argv[])
1674 uint64_t start, size;
1679 total = fdt_num_mem_rsv(fdtp);
1681 pager_output("Reserved memory regions:\n");
1682 for (i = 0; i < total; i++) {
1683 fdt_get_mem_rsv(fdtp, i, &start, &size);
1684 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1689 pager_output("No reserved memory regions\n");
1696 fdt_cmd_nyi(int argc, char *argv[])
1699 printf("command not yet implemented\n");