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. Board vendors use both fdtaddr and
316 s = ub_env_get("fdtaddr");
318 s = ub_env_get("fdt_addr");
319 if (s != NULL && *s != '\0') {
320 hdr = (struct fdt_header *)strtoul(s, &p, 16);
322 if (fdt_load_dtb_addr(hdr) == 0) {
323 printf("Using DTB provided by U-Boot at "
324 "address 0x%p.\n", hdr);
331 * If the U-boot environment contains a variable giving the name of a
332 * file, use it if we can load and validate it.
334 s = ub_env_get("fdtfile");
336 s = ub_env_get("fdt_file");
337 if (s != NULL && *s != '\0') {
338 if (fdt_load_dtb_file(s) == 0) {
339 printf("Loaded DTB from file '%s'.\n", s);
344 /* If there is a dtb compiled into the kernel, use it. */
345 if ((va = fdt_find_static_dtb()) != 0) {
346 if (fdt_load_dtb(va) == 0) {
347 printf("Using DTB compiled into kernel.\n");
352 command_errmsg = "No device tree blob found!\n";
356 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
357 (cellbuf), (lim), (cellsize), 0);
359 /* Force using base 16 */
360 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
361 (cellbuf), (lim), (cellsize), 16);
364 _fdt_strtovect(char *str, void *cellbuf, int lim, unsigned char cellsize,
368 char *end = str + strlen(str) - 2;
369 uint32_t *u32buf = NULL;
370 uint8_t *u8buf = NULL;
373 if (cellsize == sizeof(uint32_t))
374 u32buf = (uint32_t *)cellbuf;
376 u8buf = (uint8_t *)cellbuf;
383 /* Skip white whitespace(s)/separators */
384 while (!isxdigit(*buf) && buf < end)
389 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
392 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
394 if (cnt + 1 <= lim - 1)
399 /* Find another number */
400 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
406 #define TMP_MAX_ETH 8
409 fixup_ethernet(const char *env, char *ethstr, int *eth_no, int len)
415 /* Extract interface number */
416 i = strtol(env + 3, &end, 10);
417 if (end == (env + 3))
418 /* 'ethaddr' means interface 0 address */
426 str = ub_env_get(env);
428 /* Convert macaddr string into a vector of uints */
429 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
432 strncpy(ethstr + 8, env + 3, i);
434 /* Set actual property to a value from vect */
435 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
436 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
438 /* Clear ethernet..XXXX.. string */
439 bzero(ethstr + 8, len - 8);
446 fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
448 int lo, o = 0, o2, maxo = 0, depth;
449 const uint32_t zero = 0;
451 /* We want to modify every subnode of /cpus */
452 o = fdt_path_offset(fdtp, "/cpus");
456 /* maxo should contain offset of node next to /cpus */
460 maxo = fdt_next_node(fdtp, maxo, &depth);
462 /* Find CPU frequency properties */
463 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
464 &zero, sizeof(uint32_t));
466 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
471 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
473 o = fdt_node_offset_by_prop_value(fdtp, lo,
474 "clock-frequency", &zero, sizeof(uint32_t));
476 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
477 &zero, sizeof(uint32_t));
479 /* We're only interested in /cpus subnode(s) */
483 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
486 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
494 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
496 int cells_in_tuple, i, tuples, tuple_size;
497 uint32_t cur_start, cur_size;
499 cells_in_tuple = (addr_cells + size_cells);
500 tuple_size = cells_in_tuple * sizeof(uint32_t);
501 tuples = len / tuple_size;
505 for (i = 0; i < tuples; i++) {
507 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
509 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
512 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
514 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
519 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
520 i, cur_start, cur_size);
526 fixup_memory(struct sys_info *si)
528 struct mem_region *curmr;
529 uint32_t addr_cells, size_cells;
530 uint32_t *addr_cellsp, *reg, *size_cellsp;
531 int err, i, len, memory, realmrno, root;
533 uint64_t rstart, rsize;
536 root = fdt_path_offset(fdtp, "/");
538 sprintf(command_errbuf, "Could not find root node !");
542 memory = fdt_path_offset(fdtp, "/memory");
544 /* Create proper '/memory' node. */
545 memory = fdt_add_subnode(fdtp, root, "memory");
547 sprintf(command_errbuf, "Could not fixup '/memory' "
548 "node, error code : %d!\n", memory);
552 err = fdt_setprop(fdtp, memory, "device_type", "memory",
559 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
561 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
563 if (addr_cellsp == NULL || size_cellsp == NULL) {
564 sprintf(command_errbuf, "Could not fixup '/memory' node : "
565 "%s %s property not found in root node!\n",
566 (!addr_cellsp) ? "#address-cells" : "",
567 (!size_cellsp) ? "#size-cells" : "");
571 addr_cells = fdt32_to_cpu(*addr_cellsp);
572 size_cells = fdt32_to_cpu(*size_cellsp);
575 * Convert memreserve data to memreserve property
576 * Check if property already exists
578 reserved = fdt_num_mem_rsv(fdtp);
580 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
581 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
582 sb = buf = (uint8_t *)malloc(len);
588 for (i = 0; i < reserved; i++) {
590 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
593 /* Ensure endianess, and put cells into a buffer */
596 cpu_to_fdt64(rstart);
599 cpu_to_fdt32(rstart);
601 buf += sizeof(uint32_t) * addr_cells;
609 buf += sizeof(uint32_t) * size_cells;
614 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
615 printf("Could not fixup 'memreserve' property.\n");
620 /* Count valid memory regions entries in sysinfo. */
621 realmrno = si->mr_no;
622 for (i = 0; i < si->mr_no; i++)
623 if (si->mr[i].start == 0 && si->mr[i].size == 0)
627 sprintf(command_errbuf, "Could not fixup '/memory' node : "
628 "sysinfo doesn't contain valid memory regions info!\n");
632 if ((reg = (uint32_t *)fdt_getprop(fdtp, memory, "reg",
635 if (fdt_reg_valid(reg, len, addr_cells, size_cells) == 0)
637 * Do not apply fixup if existing 'reg' property
643 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
644 sb = buf = (uint8_t *)malloc(len);
650 for (i = 0; i < si->mr_no; i++) {
652 if (curmr->size != 0) {
653 /* Ensure endianess, and put cells into a buffer */
656 cpu_to_fdt64(curmr->start);
659 cpu_to_fdt32(curmr->start);
661 buf += sizeof(uint32_t) * addr_cells;
664 cpu_to_fdt64(curmr->size);
667 cpu_to_fdt32(curmr->size);
669 buf += sizeof(uint32_t) * size_cells;
674 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
675 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
681 fixup_stdout(const char *env)
687 const struct fdt_property *prop;
690 str = ub_env_get(env);
691 ptr = (char *)str + strlen(str) - 1;
692 while (ptr > str && isdigit(*(str - 1)))
698 serialno = (int)strtol(ptr, NULL, 0);
699 no = fdt_path_offset(fdtp, "/chosen");
703 prop = fdt_get_property(fdtp, no, "stdout", &len);
705 /* If /chosen/stdout does not extist, create it */
706 if (prop == NULL || (prop != NULL && len == 0)) {
708 bzero(tmp, 10 * sizeof(char));
709 strcpy((char *)&tmp, "serial");
711 /* Serial number too long */
714 strncpy((char *)tmp + 6, ptr, 3);
715 sero = fdt_path_offset(fdtp, (const char *)tmp);
718 * If serial device we're trying to assign
719 * stdout to doesn't exist in DT -- return.
723 fdt_setprop(fdtp, no, "stdout", &tmp,
724 strlen((char *)&tmp) + 1);
725 fdt_setprop(fdtp, no, "stdin", &tmp,
726 strlen((char *)&tmp) + 1);
731 * Locate the blob, fix it up and return its location.
738 int chosen, eth_no, len;
746 debugf("fdt_fixup()\n");
748 if (fdtp == NULL && fdt_setup_fdtp() != 0)
751 /* Create /chosen node (if not exists) */
752 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
754 chosen = fdt_add_subnode(fdtp, 0, "chosen");
756 /* Value assigned to fixup-applied does not matter. */
757 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
760 /* Acquire sys_info */
761 si = ub_get_sys_info();
763 while ((env = ub_env_enum(env)) != NULL) {
764 if (strncmp(env, "eth", 3) == 0 &&
765 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
767 * Handle Ethernet addrs: parse uboot env eth%daddr
772 * Check how many chars we will need to store
773 * maximal eth iface number.
775 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
779 * Reserve mem for string "ethernet" and len
780 * chars for iface no.
782 ethstr = (char *)malloc(len * sizeof(char));
783 bzero(ethstr, len * sizeof(char));
784 strcpy(ethstr, "ethernet0");
788 fixup_ethernet(env, ethstr, ð_no, len);
790 } else if (strcmp(env, "consoledev") == 0)
794 /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
795 fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
797 /* Fixup memory regions */
800 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
805 * Copy DTB blob to specified location and return size
808 fdt_copy(vm_offset_t va)
811 debugf("fdt_copy va 0x%08x\n", va);
813 err = fdt_setup_fdtp();
815 printf("No valid device tree blob found!\n");
820 if (fdt_fixup() == 0)
824 /* Overwrite the FDT with the fixed version. */
825 /* XXX Is this really appropriate? */
826 COPYIN(fdtp, fdtp_va, fdtp_size);
828 COPYIN(fdtp, va, fdtp_size);
835 command_fdt_internal(int argc, char *argv[])
843 command_errmsg = "usage is 'fdt <command> [<args>]";
848 * Validate fdt <command>.
850 cmd = strdup(argv[1]);
853 while (!(commands[i].name == NULL)) {
854 if (strcmp(cmd, commands[i].name) == 0) {
856 cmdh = commands[i].handler;
857 flags = commands[i].flags;
863 command_errmsg = "unknown command";
867 if (flags & CMD_REQUIRES_BLOB) {
869 * Check if uboot env vars were parsed already. If not, do it now.
871 if (fdt_fixup() == 0)
876 * Call command handler.
878 err = (*cmdh)(argc, argv);
884 fdt_cmd_addr(int argc, char *argv[])
886 struct preloaded_file *fp;
887 struct fdt_header *hdr;
896 sprintf(command_errbuf, "no address specified");
900 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
902 sprintf(command_errbuf, "Invalid address: %s", addr);
906 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
915 fdt_cmd_cd(int argc, char *argv[])
918 char tmp[FDT_CWD_LEN];
921 path = (argc > 2) ? argv[2] : "/";
923 if (path[0] == '/') {
925 if (len >= FDT_CWD_LEN)
928 /* Handle path specification relative to cwd */
929 len = strlen(cwd) + strlen(path) + 1;
930 if (len >= FDT_CWD_LEN)
939 o = fdt_path_offset(fdtp, path);
941 sprintf(command_errbuf, "could not find node: '%s'", path);
949 sprintf(command_errbuf, "path too long: %d, max allowed: %d",
950 len, FDT_CWD_LEN - 1);
955 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
961 command_errmsg = "no device tree blob pointer?!";
965 ver = fdt_version(fdtp);
967 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
969 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
971 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
973 sprintf(line, " off_dt_struct = 0x%08x\n",
974 fdt_off_dt_struct(fdtp));
976 sprintf(line, " off_dt_strings = 0x%08x\n",
977 fdt_off_dt_strings(fdtp));
979 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
980 fdt_off_mem_rsvmap(fdtp));
982 sprintf(line, " version = %d\n", ver);
984 sprintf(line, " last compatible version = %d\n",
985 fdt_last_comp_version(fdtp));
988 sprintf(line, " boot_cpuid = %d\n",
989 fdt_boot_cpuid_phys(fdtp));
993 sprintf(line, " size_dt_strings = %d\n",
994 fdt_size_dt_strings(fdtp));
998 sprintf(line, " size_dt_struct = %d\n",
999 fdt_size_dt_struct(fdtp));
1008 fdt_cmd_ls(int argc, char *argv[])
1010 const char *prevname[FDT_MAX_DEPTH] = { NULL };
1013 int i, o, depth, len;
1015 path = (argc > 2) ? argv[2] : NULL;
1019 o = fdt_path_offset(fdtp, path);
1021 sprintf(command_errbuf, "could not find node: '%s'", path);
1026 (o >= 0) && (depth >= 0);
1027 o = fdt_next_node(fdtp, o, &depth)) {
1029 name = fdt_get_name(fdtp, o, &len);
1031 if (depth > FDT_MAX_DEPTH) {
1032 printf("max depth exceeded: %d\n", depth);
1036 prevname[depth] = name;
1038 /* Skip root (i = 1) when printing devices */
1039 for (i = 1; i <= depth; i++) {
1040 if (prevname[i] == NULL)
1043 if (strcmp(cwd, "/") == 0)
1045 printf("%s", prevname[i]);
1057 return (c >= ' ' && c <= 0x7e);
1061 fdt_isprint(const void *data, int len, int *count)
1070 d = (const char *)data;
1071 if (d[len - 1] != '\0')
1076 for (i = 0; i < len; i++) {
1078 if (isprint(ch) || (ch == '\0' && i > 0)) {
1093 fdt_data_str(const void *data, int len, int count, char **buf)
1100 * Calculate the length for the string and allocate memory.
1102 * Note that 'len' already includes at least one terminator.
1107 * Each token had already a terminator buried in 'len', but we
1108 * only need one eventually, don't count space for these.
1110 buf_len -= count - 1;
1112 /* Each consecutive token requires a ", " separator. */
1113 buf_len += count * 2;
1116 /* Add some space for surrounding double quotes. */
1117 buf_len += count * 2;
1119 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1120 b = (char *)malloc(buf_len);
1121 tmp = (char *)malloc(buf_len);
1133 * Now that we have space, format the string.
1137 d = (const char *)data + i;
1140 sprintf(tmp, "\"%s\"%s", d,
1141 (i + l) < len ? ", " : "");
1157 fdt_data_cell(const void *data, int len, char **buf)
1163 /* Number of cells */
1167 * Calculate the length for the string and allocate memory.
1170 /* Each byte translates to 2 output characters */
1173 /* Each consecutive cell requires a " " separator. */
1174 l += (count - 1) * 1;
1176 /* Each cell will have a "0x" prefix */
1178 /* Space for surrounding <> and terminator */
1181 b = (char *)malloc(l);
1182 tmp = (char *)malloc(l);
1194 for (i = 0; i < len; i += 4) {
1195 c = (const uint32_t *)((const uint8_t *)data + i);
1196 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1197 i < (len - 4) ? " " : "");
1211 fdt_data_bytes(const void *data, int len, char **buf)
1218 * Calculate the length for the string and allocate memory.
1221 /* Each byte translates to 2 output characters */
1224 /* Each consecutive byte requires a " " separator. */
1226 /* Each byte will have a "0x" prefix */
1228 /* Space for surrounding [] and terminator. */
1231 b = (char *)malloc(l);
1232 tmp = (char *)malloc(l);
1244 for (i = 0, d = data; i < len; i++) {
1245 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1259 fdt_data_fmt(const void *data, int len, char **buf)
1268 if (fdt_isprint(data, len, &count))
1269 return (fdt_data_str(data, len, count, buf));
1271 else if ((len % 4) == 0)
1272 return (fdt_data_cell(data, len, buf));
1275 return (fdt_data_bytes(data, len, buf));
1279 fdt_prop(int offset)
1282 const struct fdt_property *prop;
1288 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1292 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1293 len = fdt32_to_cpu(prop->len);
1298 /* Property without value */
1299 line = (char *)malloc(strlen(name) + 2);
1304 sprintf(line, "%s\n", name);
1309 * Process property with value
1313 if (fdt_data_fmt(data, len, &buf) != 0) {
1318 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1321 sprintf(command_errbuf, "could not allocate space for string");
1326 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1344 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1346 uint32_t cells[100];
1349 const struct fdt_property *p;
1351 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1355 /* Adding inexistant value in mode 1 is forbidden */
1356 sprintf(command_errbuf, "property already exists!");
1359 } else if (mode == 0) {
1360 sprintf(command_errbuf, "property does not exist!");
1363 len = strlen(value);
1365 buf = (char *)value;
1373 len = fdt_strtovect(buf, (void *)&cells, 100,
1376 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1377 len * sizeof(uint32_t));
1381 len = fdt_strtovect(buf, (void *)&cells, 100,
1384 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1385 len * sizeof(uint8_t));
1389 /* Default -- string */
1390 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1395 if (rv == -FDT_ERR_NOSPACE)
1396 sprintf(command_errbuf,
1397 "Device tree blob is too small!\n");
1399 sprintf(command_errbuf,
1400 "Could not add/modify property!\n");
1405 /* Merge strings from argv into a single string */
1407 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1415 for (i = start; i < argc; i++)
1416 sz += strlen(argv[i]);
1418 /* Additional bytes for whitespaces between args */
1421 buf = (char *)malloc(sizeof(char) * sz);
1422 bzero(buf, sizeof(char) * sz);
1425 sprintf(command_errbuf, "could not allocate space "
1431 for (i = start, idx = 0; i < argc; i++) {
1432 strcpy(buf + idx, argv[i]);
1433 idx += strlen(argv[i]);
1442 /* Extract offset and name of node/property from a given path */
1444 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1447 char *path = *pathp, *name = NULL, *subpath = NULL;
1449 subpath = strrchr(path, '/');
1450 if (subpath == NULL) {
1451 o = fdt_path_offset(fdtp, cwd);
1453 path = (char *)&cwd;
1456 if (strlen(path) == 0)
1460 o = fdt_path_offset(fdtp, path);
1463 if (strlen(name) == 0) {
1464 sprintf(command_errbuf, "name not specified");
1468 sprintf(command_errbuf, "could not find node: '%s'", path);
1478 fdt_cmd_prop(int argc, char *argv[])
1480 char *path, *propname, *value;
1481 int o, next, depth, rv;
1484 path = (argc > 2) ? argv[2] : NULL;
1489 /* Merge property value strings into one */
1490 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1501 /* If value is specified -- try to modify prop. */
1502 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1505 rv = fdt_modprop(o, propname, value, 0);
1511 /* User wants to display properties */
1512 o = fdt_path_offset(fdtp, path);
1515 sprintf(command_errbuf, "could not find node: '%s'", path);
1521 while (depth >= 0) {
1522 tag = fdt_next_tag(fdtp, o, &next);
1528 /* Don't process properties of nested nodes */
1531 if (fdt_prop(o) != 0) {
1532 sprintf(command_errbuf, "could not process "
1538 case FDT_BEGIN_NODE:
1540 if (depth > FDT_MAX_DEPTH) {
1541 printf("warning: nesting too deep: %d\n",
1550 * This is the end of our starting node, force
1563 fdt_cmd_mkprop(int argc, char *argv[])
1566 char *path, *propname, *value;
1568 path = (argc > 2) ? argv[2] : NULL;
1573 /* Merge property value strings into one */
1574 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1579 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1582 if (fdt_modprop(o, propname, value, 1))
1589 fdt_cmd_rm(int argc, char *argv[])
1592 char *path = NULL, *propname;
1597 sprintf(command_errbuf, "no node/property name specified");
1601 o = fdt_path_offset(fdtp, path);
1603 /* If node not found -- try to find & delete property */
1604 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1607 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1608 sprintf(command_errbuf, "could not delete"
1609 "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1610 "(property/node does not exist)" : "");
1616 /* If node exists -- remove node */
1617 rv = fdt_del_node(fdtp, o);
1619 sprintf(command_errbuf, "could not delete node");
1626 fdt_cmd_mknode(int argc, char *argv[])
1629 char *path = NULL, *nodename = NULL;
1634 sprintf(command_errbuf, "no node name specified");
1638 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1641 rv = fdt_add_subnode(fdtp, o, nodename);
1644 if (rv == -FDT_ERR_NOSPACE)
1645 sprintf(command_errbuf,
1646 "Device tree blob is too small!\n");
1648 sprintf(command_errbuf,
1649 "Could not add node!\n");
1656 fdt_cmd_pwd(int argc, char *argv[])
1658 char line[FDT_CWD_LEN];
1661 sprintf(line, "%s\n", cwd);
1668 fdt_cmd_mres(int argc, char *argv[])
1670 uint64_t start, size;
1675 total = fdt_num_mem_rsv(fdtp);
1677 pager_output("Reserved memory regions:\n");
1678 for (i = 0; i < total; i++) {
1679 fdt_get_mem_rsv(fdtp, i, &start, &size);
1680 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1685 pager_output("No reserved memory regions\n");
1692 fdt_cmd_nyi(int argc, char *argv[])
1695 printf("command not yet implemented\n");