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(char *str, void *cellbuf, int lim, unsigned char cellsize,
371 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)
418 /* Extract interface number */
419 i = strtol(env + 3, &end, 10);
420 if (end == (env + 3))
421 /* 'ethaddr' means interface 0 address */
429 str = ub_env_get(env);
431 /* Convert macaddr string into a vector of uints */
432 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
435 strncpy(ethstr + 8, env + 3, i);
437 /* Set actual property to a value from vect */
438 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
439 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
441 /* Clear ethernet..XXXX.. string */
442 bzero(ethstr + 8, len - 8);
449 fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
451 int lo, o = 0, o2, maxo = 0, depth;
452 const uint32_t zero = 0;
454 /* We want to modify every subnode of /cpus */
455 o = fdt_path_offset(fdtp, "/cpus");
459 /* maxo should contain offset of node next to /cpus */
463 maxo = fdt_next_node(fdtp, maxo, &depth);
465 /* Find CPU frequency properties */
466 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
467 &zero, sizeof(uint32_t));
469 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
474 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
476 o = fdt_node_offset_by_prop_value(fdtp, lo,
477 "clock-frequency", &zero, sizeof(uint32_t));
479 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
480 &zero, sizeof(uint32_t));
482 /* We're only interested in /cpus subnode(s) */
486 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
489 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
497 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
499 int cells_in_tuple, i, tuples, tuple_size;
500 uint32_t cur_start, cur_size;
502 cells_in_tuple = (addr_cells + size_cells);
503 tuple_size = cells_in_tuple * sizeof(uint32_t);
504 tuples = len / tuple_size;
508 for (i = 0; i < tuples; i++) {
510 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
512 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
515 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
517 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
522 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
523 i, cur_start, cur_size);
529 fixup_memory(struct sys_info *si)
531 struct mem_region *curmr;
532 uint32_t addr_cells, size_cells;
533 uint32_t *addr_cellsp, *reg, *size_cellsp;
534 int err, i, len, memory, realmrno, root;
536 uint64_t rstart, rsize;
539 root = fdt_path_offset(fdtp, "/");
541 sprintf(command_errbuf, "Could not find root node !");
545 memory = fdt_path_offset(fdtp, "/memory");
547 /* Create proper '/memory' node. */
548 memory = fdt_add_subnode(fdtp, root, "memory");
550 sprintf(command_errbuf, "Could not fixup '/memory' "
551 "node, error code : %d!\n", memory);
555 err = fdt_setprop(fdtp, memory, "device_type", "memory",
562 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
564 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
566 if (addr_cellsp == NULL || size_cellsp == NULL) {
567 sprintf(command_errbuf, "Could not fixup '/memory' node : "
568 "%s %s property not found in root node!\n",
569 (!addr_cellsp) ? "#address-cells" : "",
570 (!size_cellsp) ? "#size-cells" : "");
574 addr_cells = fdt32_to_cpu(*addr_cellsp);
575 size_cells = fdt32_to_cpu(*size_cellsp);
578 * Convert memreserve data to memreserve property
579 * Check if property already exists
581 reserved = fdt_num_mem_rsv(fdtp);
583 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
584 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
585 sb = buf = (uint8_t *)malloc(len);
591 for (i = 0; i < reserved; i++) {
593 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
596 /* Ensure endianess, and put cells into a buffer */
599 cpu_to_fdt64(rstart);
602 cpu_to_fdt32(rstart);
604 buf += sizeof(uint32_t) * addr_cells;
612 buf += sizeof(uint32_t) * size_cells;
617 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
618 printf("Could not fixup 'memreserve' property.\n");
623 /* Count valid memory regions entries in sysinfo. */
624 realmrno = si->mr_no;
625 for (i = 0; i < si->mr_no; i++)
626 if (si->mr[i].start == 0 && si->mr[i].size == 0)
630 sprintf(command_errbuf, "Could not fixup '/memory' node : "
631 "sysinfo doesn't contain valid memory regions info!\n");
635 if ((reg = (uint32_t *)fdt_getprop(fdtp, memory, "reg",
638 if (fdt_reg_valid(reg, len, addr_cells, size_cells) == 0)
640 * Do not apply fixup if existing 'reg' property
646 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
647 sb = buf = (uint8_t *)malloc(len);
653 for (i = 0; i < si->mr_no; i++) {
655 if (curmr->size != 0) {
656 /* Ensure endianess, and put cells into a buffer */
659 cpu_to_fdt64(curmr->start);
662 cpu_to_fdt32(curmr->start);
664 buf += sizeof(uint32_t) * addr_cells;
667 cpu_to_fdt64(curmr->size);
670 cpu_to_fdt32(curmr->size);
672 buf += sizeof(uint32_t) * size_cells;
677 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
678 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
684 fixup_stdout(const char *env)
690 const struct fdt_property *prop;
693 str = ub_env_get(env);
694 ptr = (char *)str + strlen(str) - 1;
695 while (ptr > str && isdigit(*(str - 1)))
701 serialno = (int)strtol(ptr, NULL, 0);
702 no = fdt_path_offset(fdtp, "/chosen");
706 prop = fdt_get_property(fdtp, no, "stdout", &len);
708 /* If /chosen/stdout does not extist, create it */
709 if (prop == NULL || (prop != NULL && len == 0)) {
711 bzero(tmp, 10 * sizeof(char));
712 strcpy((char *)&tmp, "serial");
714 /* Serial number too long */
717 strncpy((char *)tmp + 6, ptr, 3);
718 sero = fdt_path_offset(fdtp, (const char *)tmp);
721 * If serial device we're trying to assign
722 * stdout to doesn't exist in DT -- return.
726 fdt_setprop(fdtp, no, "stdout", &tmp,
727 strlen((char *)&tmp) + 1);
728 fdt_setprop(fdtp, no, "stdin", &tmp,
729 strlen((char *)&tmp) + 1);
734 * Locate the blob, fix it up and return its location.
741 int chosen, eth_no, len;
749 debugf("fdt_fixup()\n");
751 if (fdtp == NULL && fdt_setup_fdtp() != 0)
754 /* Create /chosen node (if not exists) */
755 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
757 chosen = fdt_add_subnode(fdtp, 0, "chosen");
759 /* Value assigned to fixup-applied does not matter. */
760 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
763 /* Acquire sys_info */
764 si = ub_get_sys_info();
766 while ((env = ub_env_enum(env)) != NULL) {
767 if (strncmp(env, "eth", 3) == 0 &&
768 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
770 * Handle Ethernet addrs: parse uboot env eth%daddr
775 * Check how many chars we will need to store
776 * maximal eth iface number.
778 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
782 * Reserve mem for string "ethernet" and len
783 * chars for iface no.
785 ethstr = (char *)malloc(len * sizeof(char));
786 bzero(ethstr, len * sizeof(char));
787 strcpy(ethstr, "ethernet0");
791 fixup_ethernet(env, ethstr, ð_no, len);
793 } else if (strcmp(env, "consoledev") == 0)
797 /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
798 fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
800 /* Fixup memory regions */
803 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
808 * Copy DTB blob to specified location and return size
811 fdt_copy(vm_offset_t va)
814 debugf("fdt_copy va 0x%08x\n", va);
816 err = fdt_setup_fdtp();
818 printf("No valid device tree blob found!\n");
823 if (fdt_fixup() == 0)
827 /* Overwrite the FDT with the fixed version. */
828 /* XXX Is this really appropriate? */
829 COPYIN(fdtp, fdtp_va, fdtp_size);
831 COPYIN(fdtp, va, fdtp_size);
838 command_fdt_internal(int argc, char *argv[])
846 command_errmsg = "usage is 'fdt <command> [<args>]";
851 * Validate fdt <command>.
853 cmd = strdup(argv[1]);
856 while (!(commands[i].name == NULL)) {
857 if (strcmp(cmd, commands[i].name) == 0) {
859 cmdh = commands[i].handler;
860 flags = commands[i].flags;
866 command_errmsg = "unknown command";
870 if (flags & CMD_REQUIRES_BLOB) {
872 * Check if uboot env vars were parsed already. If not, do it now.
874 if (fdt_fixup() == 0)
879 * Call command handler.
881 err = (*cmdh)(argc, argv);
887 fdt_cmd_addr(int argc, char *argv[])
889 struct preloaded_file *fp;
890 struct fdt_header *hdr;
899 sprintf(command_errbuf, "no address specified");
903 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
905 sprintf(command_errbuf, "Invalid address: %s", addr);
909 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
918 fdt_cmd_cd(int argc, char *argv[])
921 char tmp[FDT_CWD_LEN];
924 path = (argc > 2) ? argv[2] : "/";
926 if (path[0] == '/') {
928 if (len >= FDT_CWD_LEN)
931 /* Handle path specification relative to cwd */
932 len = strlen(cwd) + strlen(path) + 1;
933 if (len >= FDT_CWD_LEN)
942 o = fdt_path_offset(fdtp, path);
944 sprintf(command_errbuf, "could not find node: '%s'", path);
952 sprintf(command_errbuf, "path too long: %d, max allowed: %d",
953 len, FDT_CWD_LEN - 1);
958 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
964 command_errmsg = "no device tree blob pointer?!";
968 ver = fdt_version(fdtp);
970 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
972 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
974 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
976 sprintf(line, " off_dt_struct = 0x%08x\n",
977 fdt_off_dt_struct(fdtp));
979 sprintf(line, " off_dt_strings = 0x%08x\n",
980 fdt_off_dt_strings(fdtp));
982 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
983 fdt_off_mem_rsvmap(fdtp));
985 sprintf(line, " version = %d\n", ver);
987 sprintf(line, " last compatible version = %d\n",
988 fdt_last_comp_version(fdtp));
991 sprintf(line, " boot_cpuid = %d\n",
992 fdt_boot_cpuid_phys(fdtp));
996 sprintf(line, " size_dt_strings = %d\n",
997 fdt_size_dt_strings(fdtp));
1001 sprintf(line, " size_dt_struct = %d\n",
1002 fdt_size_dt_struct(fdtp));
1011 fdt_cmd_ls(int argc, char *argv[])
1013 const char *prevname[FDT_MAX_DEPTH] = { NULL };
1016 int i, o, depth, len;
1018 path = (argc > 2) ? argv[2] : NULL;
1022 o = fdt_path_offset(fdtp, path);
1024 sprintf(command_errbuf, "could not find node: '%s'", path);
1029 (o >= 0) && (depth >= 0);
1030 o = fdt_next_node(fdtp, o, &depth)) {
1032 name = fdt_get_name(fdtp, o, &len);
1034 if (depth > FDT_MAX_DEPTH) {
1035 printf("max depth exceeded: %d\n", depth);
1039 prevname[depth] = name;
1041 /* Skip root (i = 1) when printing devices */
1042 for (i = 1; i <= depth; i++) {
1043 if (prevname[i] == NULL)
1046 if (strcmp(cwd, "/") == 0)
1048 printf("%s", prevname[i]);
1060 return (c >= ' ' && c <= 0x7e);
1064 fdt_isprint(const void *data, int len, int *count)
1073 d = (const char *)data;
1074 if (d[len - 1] != '\0')
1079 for (i = 0; i < len; i++) {
1081 if (isprint(ch) || (ch == '\0' && i > 0)) {
1096 fdt_data_str(const void *data, int len, int count, char **buf)
1103 * Calculate the length for the string and allocate memory.
1105 * Note that 'len' already includes at least one terminator.
1110 * Each token had already a terminator buried in 'len', but we
1111 * only need one eventually, don't count space for these.
1113 buf_len -= count - 1;
1115 /* Each consecutive token requires a ", " separator. */
1116 buf_len += count * 2;
1119 /* Add some space for surrounding double quotes. */
1120 buf_len += count * 2;
1122 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1123 b = (char *)malloc(buf_len);
1124 tmp = (char *)malloc(buf_len);
1136 * Now that we have space, format the string.
1140 d = (const char *)data + i;
1143 sprintf(tmp, "\"%s\"%s", d,
1144 (i + l) < len ? ", " : "");
1160 fdt_data_cell(const void *data, int len, char **buf)
1166 /* Number of cells */
1170 * Calculate the length for the string and allocate memory.
1173 /* Each byte translates to 2 output characters */
1176 /* Each consecutive cell requires a " " separator. */
1177 l += (count - 1) * 1;
1179 /* Each cell will have a "0x" prefix */
1181 /* Space for surrounding <> and terminator */
1184 b = (char *)malloc(l);
1185 tmp = (char *)malloc(l);
1197 for (i = 0; i < len; i += 4) {
1198 c = (const uint32_t *)((const uint8_t *)data + i);
1199 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1200 i < (len - 4) ? " " : "");
1214 fdt_data_bytes(const void *data, int len, char **buf)
1221 * Calculate the length for the string and allocate memory.
1224 /* Each byte translates to 2 output characters */
1227 /* Each consecutive byte requires a " " separator. */
1229 /* Each byte will have a "0x" prefix */
1231 /* Space for surrounding [] and terminator. */
1234 b = (char *)malloc(l);
1235 tmp = (char *)malloc(l);
1247 for (i = 0, d = data; i < len; i++) {
1248 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1262 fdt_data_fmt(const void *data, int len, char **buf)
1271 if (fdt_isprint(data, len, &count))
1272 return (fdt_data_str(data, len, count, buf));
1274 else if ((len % 4) == 0)
1275 return (fdt_data_cell(data, len, buf));
1278 return (fdt_data_bytes(data, len, buf));
1282 fdt_prop(int offset)
1285 const struct fdt_property *prop;
1291 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1295 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1296 len = fdt32_to_cpu(prop->len);
1301 /* Property without value */
1302 line = (char *)malloc(strlen(name) + 2);
1307 sprintf(line, "%s\n", name);
1312 * Process property with value
1316 if (fdt_data_fmt(data, len, &buf) != 0) {
1321 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1324 sprintf(command_errbuf, "could not allocate space for string");
1329 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1347 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1349 uint32_t cells[100];
1352 const struct fdt_property *p;
1354 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1358 /* Adding inexistant value in mode 1 is forbidden */
1359 sprintf(command_errbuf, "property already exists!");
1362 } else if (mode == 0) {
1363 sprintf(command_errbuf, "property does not exist!");
1366 len = strlen(value);
1368 buf = (char *)value;
1376 len = fdt_strtovect(buf, (void *)&cells, 100,
1379 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1380 len * sizeof(uint32_t));
1384 len = fdt_strtovect(buf, (void *)&cells, 100,
1387 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1388 len * sizeof(uint8_t));
1392 /* Default -- string */
1393 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1398 if (rv == -FDT_ERR_NOSPACE)
1399 sprintf(command_errbuf,
1400 "Device tree blob is too small!\n");
1402 sprintf(command_errbuf,
1403 "Could not add/modify property!\n");
1408 /* Merge strings from argv into a single string */
1410 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1418 for (i = start; i < argc; i++)
1419 sz += strlen(argv[i]);
1421 /* Additional bytes for whitespaces between args */
1424 buf = (char *)malloc(sizeof(char) * sz);
1425 bzero(buf, sizeof(char) * sz);
1428 sprintf(command_errbuf, "could not allocate space "
1434 for (i = start, idx = 0; i < argc; i++) {
1435 strcpy(buf + idx, argv[i]);
1436 idx += strlen(argv[i]);
1445 /* Extract offset and name of node/property from a given path */
1447 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1450 char *path = *pathp, *name = NULL, *subpath = NULL;
1452 subpath = strrchr(path, '/');
1453 if (subpath == NULL) {
1454 o = fdt_path_offset(fdtp, cwd);
1456 path = (char *)&cwd;
1459 if (strlen(path) == 0)
1463 o = fdt_path_offset(fdtp, path);
1466 if (strlen(name) == 0) {
1467 sprintf(command_errbuf, "name not specified");
1471 sprintf(command_errbuf, "could not find node: '%s'", path);
1481 fdt_cmd_prop(int argc, char *argv[])
1483 char *path, *propname, *value;
1484 int o, next, depth, rv;
1487 path = (argc > 2) ? argv[2] : NULL;
1492 /* Merge property value strings into one */
1493 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1504 /* If value is specified -- try to modify prop. */
1505 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1508 rv = fdt_modprop(o, propname, value, 0);
1514 /* User wants to display properties */
1515 o = fdt_path_offset(fdtp, path);
1518 sprintf(command_errbuf, "could not find node: '%s'", path);
1524 while (depth >= 0) {
1525 tag = fdt_next_tag(fdtp, o, &next);
1531 /* Don't process properties of nested nodes */
1534 if (fdt_prop(o) != 0) {
1535 sprintf(command_errbuf, "could not process "
1541 case FDT_BEGIN_NODE:
1543 if (depth > FDT_MAX_DEPTH) {
1544 printf("warning: nesting too deep: %d\n",
1553 * This is the end of our starting node, force
1566 fdt_cmd_mkprop(int argc, char *argv[])
1569 char *path, *propname, *value;
1571 path = (argc > 2) ? argv[2] : NULL;
1576 /* Merge property value strings into one */
1577 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1582 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1585 if (fdt_modprop(o, propname, value, 1))
1592 fdt_cmd_rm(int argc, char *argv[])
1595 char *path = NULL, *propname;
1600 sprintf(command_errbuf, "no node/property name specified");
1604 o = fdt_path_offset(fdtp, path);
1606 /* If node not found -- try to find & delete property */
1607 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1610 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1611 sprintf(command_errbuf, "could not delete"
1612 "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1613 "(property/node does not exist)" : "");
1619 /* If node exists -- remove node */
1620 rv = fdt_del_node(fdtp, o);
1622 sprintf(command_errbuf, "could not delete node");
1629 fdt_cmd_mknode(int argc, char *argv[])
1632 char *path = NULL, *nodename = NULL;
1637 sprintf(command_errbuf, "no node name specified");
1641 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1644 rv = fdt_add_subnode(fdtp, o, nodename);
1647 if (rv == -FDT_ERR_NOSPACE)
1648 sprintf(command_errbuf,
1649 "Device tree blob is too small!\n");
1651 sprintf(command_errbuf,
1652 "Could not add node!\n");
1659 fdt_cmd_pwd(int argc, char *argv[])
1661 char line[FDT_CWD_LEN];
1664 sprintf(line, "%s\n", cwd);
1671 fdt_cmd_mres(int argc, char *argv[])
1673 uint64_t start, size;
1678 total = fdt_num_mem_rsv(fdtp);
1680 pager_output("Reserved memory regions:\n");
1681 for (i = 0; i < total; i++) {
1682 fdt_get_mem_rsv(fdtp, i, &start, &size);
1683 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1688 pager_output("No reserved memory regions\n");
1695 fdt_cmd_nyi(int argc, char *argv[])
1698 printf("command not yet implemented\n");