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 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 efficent 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 COPYOUT(va, &header, sizeof(header));
193 err = fdt_check_header(&header);
195 if (err == -FDT_ERR_BADVERSION)
196 sprintf(command_errbuf,
197 "incompatible blob version: %d, should be: %d",
198 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
201 sprintf(command_errbuf, "error validating blob: %s",
207 * Release previous blob
212 fdtp_size = fdt_totalsize(&header);
213 fdtp = malloc(fdtp_size);
216 command_errmsg = "can't allocate memory for device tree copy";
221 COPYOUT(va, fdtp, fdtp_size);
222 debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
228 fdt_load_dtb_addr(struct fdt_header *header)
231 // TODO: Verify that there really is an FDT at
232 // the specified location.
233 fdtp_size = fdt_totalsize(header);
235 if ((fdtp = malloc(fdtp_size)) == NULL) {
236 command_errmsg = "can't allocate memory for device tree copy";
240 fdtp_va = 0; // Don't write this back into module or kernel.
241 bcopy(header, fdtp, fdtp_size);
248 struct preloaded_file *bfp;
249 struct fdt_header *hdr;
254 if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
255 printf("Using DTB from loaded file.\n");
256 return fdt_load_dtb(bfp->f_addr);
259 if (fdt_to_load != NULL) {
260 printf("Using DTB from memory address 0x%08X.\n",
261 (unsigned int)fdt_to_load);
262 return fdt_load_dtb_addr(fdt_to_load);
265 s = ub_env_get("fdtaddr");
266 if (s != NULL && *s != '\0') {
267 hdr = (struct fdt_header *)strtoul(s, &p, 16);
269 printf("Using DTB provided by U-Boot.\n");
270 return fdt_load_dtb_addr(hdr);
274 if ((va = fdt_find_static_dtb()) != 0) {
275 printf("Using DTB compiled into kernel.\n");
276 return (fdt_load_dtb(va));
279 command_errmsg = "no device tree blob found!";
283 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
284 (cellbuf), (lim), (cellsize), 0);
286 /* Force using base 16 */
287 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
288 (cellbuf), (lim), (cellsize), 16);
291 _fdt_strtovect(char *str, void *cellbuf, int lim, unsigned char cellsize,
295 char *end = str + strlen(str) - 2;
296 uint32_t *u32buf = NULL;
297 uint8_t *u8buf = NULL;
300 if (cellsize == sizeof(uint32_t))
301 u32buf = (uint32_t *)cellbuf;
303 u8buf = (uint8_t *)cellbuf;
310 /* Skip white whitespace(s)/separators */
311 while (!isxdigit(*buf) && buf < end)
316 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
319 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
321 if (cnt + 1 <= lim - 1)
326 /* Find another number */
327 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
333 #define TMP_MAX_ETH 8
336 fixup_ethernet(const char *env, char *ethstr, int *eth_no, int len)
342 /* Extract interface number */
343 i = strtol(env + 3, &end, 10);
344 if (end == (env + 3))
345 /* 'ethaddr' means interface 0 address */
353 str = ub_env_get(env);
355 /* Convert macaddr string into a vector of uints */
356 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
359 strncpy(ethstr + 8, env + 3, i);
361 /* Set actual property to a value from vect */
362 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
363 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
365 /* Clear ethernet..XXXX.. string */
366 bzero(ethstr + 8, len - 8);
373 fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
375 int lo, o = 0, o2, maxo = 0, depth;
376 const uint32_t zero = 0;
378 /* We want to modify every subnode of /cpus */
379 o = fdt_path_offset(fdtp, "/cpus");
383 /* maxo should contain offset of node next to /cpus */
387 maxo = fdt_next_node(fdtp, maxo, &depth);
389 /* Find CPU frequency properties */
390 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
391 &zero, sizeof(uint32_t));
393 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
398 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
400 o = fdt_node_offset_by_prop_value(fdtp, lo,
401 "clock-frequency", &zero, sizeof(uint32_t));
403 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
404 &zero, sizeof(uint32_t));
406 /* We're only interested in /cpus subnode(s) */
410 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
413 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
421 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
423 int cells_in_tuple, i, tuples, tuple_size;
424 uint32_t cur_start, cur_size;
426 cells_in_tuple = (addr_cells + size_cells);
427 tuple_size = cells_in_tuple * sizeof(uint32_t);
428 tuples = len / tuple_size;
432 for (i = 0; i < tuples; i++) {
434 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
436 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
439 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
441 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
446 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
447 i, cur_start, cur_size);
453 fixup_memory(struct sys_info *si)
455 struct mem_region *curmr;
456 uint32_t addr_cells, size_cells;
457 uint32_t *addr_cellsp, *reg, *size_cellsp;
458 int err, i, len, memory, realmrno, root;
460 uint64_t rstart, rsize;
463 root = fdt_path_offset(fdtp, "/");
465 sprintf(command_errbuf, "Could not find root node !");
469 memory = fdt_path_offset(fdtp, "/memory");
471 /* Create proper '/memory' node. */
472 memory = fdt_add_subnode(fdtp, root, "memory");
474 sprintf(command_errbuf, "Could not fixup '/memory' "
475 "node, error code : %d!\n", memory);
479 err = fdt_setprop(fdtp, memory, "device_type", "memory",
486 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
488 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
490 if (addr_cellsp == NULL || size_cellsp == NULL) {
491 sprintf(command_errbuf, "Could not fixup '/memory' node : "
492 "%s %s property not found in root node!\n",
493 (!addr_cellsp) ? "#address-cells" : "",
494 (!size_cellsp) ? "#size-cells" : "");
498 addr_cells = fdt32_to_cpu(*addr_cellsp);
499 size_cells = fdt32_to_cpu(*size_cellsp);
502 * Convert memreserve data to memreserve property
503 * Check if property already exists
505 reserved = fdt_num_mem_rsv(fdtp);
507 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
508 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
509 sb = buf = (uint8_t *)malloc(len);
515 for (i = 0; i < reserved; i++) {
517 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
520 /* Ensure endianess, and put cells into a buffer */
523 cpu_to_fdt64(rstart);
526 cpu_to_fdt32(rstart);
528 buf += sizeof(uint32_t) * addr_cells;
536 buf += sizeof(uint32_t) * size_cells;
541 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
542 printf("Could not fixup 'memreserve' property.\n");
547 /* Count valid memory regions entries in sysinfo. */
548 realmrno = si->mr_no;
549 for (i = 0; i < si->mr_no; i++)
550 if (si->mr[i].start == 0 && si->mr[i].size == 0)
554 sprintf(command_errbuf, "Could not fixup '/memory' node : "
555 "sysinfo doesn't contain valid memory regions info!\n");
559 if ((reg = (uint32_t *)fdt_getprop(fdtp, memory, "reg",
562 if (fdt_reg_valid(reg, len, addr_cells, size_cells) == 0)
564 * Do not apply fixup if existing 'reg' property
570 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
571 sb = buf = (uint8_t *)malloc(len);
577 for (i = 0; i < si->mr_no; i++) {
579 if (curmr->size != 0) {
580 /* Ensure endianess, and put cells into a buffer */
583 cpu_to_fdt64(curmr->start);
586 cpu_to_fdt32(curmr->start);
588 buf += sizeof(uint32_t) * addr_cells;
591 cpu_to_fdt64(curmr->size);
594 cpu_to_fdt32(curmr->size);
596 buf += sizeof(uint32_t) * size_cells;
601 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
602 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
608 fixup_stdout(const char *env)
614 const struct fdt_property *prop;
617 str = ub_env_get(env);
618 ptr = (char *)str + strlen(str) - 1;
619 while (ptr > str && isdigit(*(str - 1)))
625 serialno = (int)strtol(ptr, NULL, 0);
626 no = fdt_path_offset(fdtp, "/chosen");
630 prop = fdt_get_property(fdtp, no, "stdout", &len);
632 /* If /chosen/stdout does not extist, create it */
633 if (prop == NULL || (prop != NULL && len == 0)) {
635 bzero(tmp, 10 * sizeof(char));
636 strcpy((char *)&tmp, "serial");
638 /* Serial number too long */
641 strncpy((char *)tmp + 6, ptr, 3);
642 sero = fdt_path_offset(fdtp, (const char *)tmp);
645 * If serial device we're trying to assign
646 * stdout to doesn't exist in DT -- return.
650 fdt_setprop(fdtp, no, "stdout", &tmp,
651 strlen((char *)&tmp) + 1);
652 fdt_setprop(fdtp, no, "stdin", &tmp,
653 strlen((char *)&tmp) + 1);
658 * Locate the blob, fix it up and return its location.
665 int chosen, err, eth_no, len;
674 err = fdt_setup_fdtp();
676 sprintf(command_errbuf, "No valid device tree blob found!");
681 /* Create /chosen node (if not exists) */
682 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
684 chosen = fdt_add_subnode(fdtp, 0, "chosen");
686 /* Value assigned to fixup-applied does not matter. */
687 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
690 /* Acquire sys_info */
691 si = ub_get_sys_info();
693 while ((env = ub_env_enum(env)) != NULL) {
694 if (strncmp(env, "eth", 3) == 0 &&
695 strncmp(env + (strlen(env) - 4), "addr", 4) == 0) {
697 * Handle Ethernet addrs: parse uboot env eth%daddr
702 * Check how many chars we will need to store
703 * maximal eth iface number.
705 len = strlen(STRINGIFY(TMP_MAX_ETH)) +
709 * Reserve mem for string "ethernet" and len
710 * chars for iface no.
712 ethstr = (char *)malloc(len * sizeof(char));
713 bzero(ethstr, len * sizeof(char));
714 strcpy(ethstr, "ethernet0");
718 fixup_ethernet(env, ethstr, ð_no, len);
720 } else if (strcmp(env, "consoledev") == 0)
724 /* Modify cpu(s) and bus clock frequenties in /cpus node [Hz] */
725 fixup_cpubusfreqs(si->clk_cpu, si->clk_bus);
727 /* Fixup memory regions */
730 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
735 * Copy DTB blob to specified location and return size
738 fdt_copy(vm_offset_t va)
743 err = fdt_setup_fdtp();
745 printf("No valid device tree blob found!");
750 if (fdt_fixup() == 0)
754 /* Overwrite the FDT with the fixed version. */
755 /* XXX Is this really appropriate? */
756 COPYIN(fdtp, fdtp_va, fdtp_size);
758 COPYIN(fdtp, va, fdtp_size);
765 command_fdt_internal(int argc, char *argv[])
773 command_errmsg = "usage is 'fdt <command> [<args>]";
778 * Validate fdt <command>.
780 cmd = strdup(argv[1]);
783 while (!(commands[i].name == NULL)) {
784 if (strcmp(cmd, commands[i].name) == 0) {
786 cmdh = commands[i].handler;
787 flags = commands[i].flags;
793 command_errmsg = "unknown command";
797 if (flags & CMD_REQUIRES_BLOB) {
799 * Check if uboot env vars were parsed already. If not, do it now.
801 if (fdt_fixup() == 0)
806 * Call command handler.
808 err = (*cmdh)(argc, argv);
814 fdt_cmd_addr(int argc, char *argv[])
816 struct preloaded_file *fp;
817 struct fdt_header *hdr;
826 sprintf(command_errbuf, "no address specified");
830 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
832 sprintf(command_errbuf, "Invalid address: %s", addr);
836 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
845 fdt_cmd_cd(int argc, char *argv[])
848 char tmp[FDT_CWD_LEN];
851 path = (argc > 2) ? argv[2] : "/";
853 if (path[0] == '/') {
855 if (len >= FDT_CWD_LEN)
858 /* Handle path specification relative to cwd */
859 len = strlen(cwd) + strlen(path) + 1;
860 if (len >= FDT_CWD_LEN)
869 o = fdt_path_offset(fdtp, path);
871 sprintf(command_errbuf, "could not find node: '%s'", path);
879 sprintf(command_errbuf, "path too long: %d, max allowed: %d",
880 len, FDT_CWD_LEN - 1);
885 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
891 command_errmsg = "no device tree blob pointer?!";
895 ver = fdt_version(fdtp);
897 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
899 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
901 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
903 sprintf(line, " off_dt_struct = 0x%08x\n",
904 fdt_off_dt_struct(fdtp));
906 sprintf(line, " off_dt_strings = 0x%08x\n",
907 fdt_off_dt_strings(fdtp));
909 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
910 fdt_off_mem_rsvmap(fdtp));
912 sprintf(line, " version = %d\n", ver);
914 sprintf(line, " last compatible version = %d\n",
915 fdt_last_comp_version(fdtp));
918 sprintf(line, " boot_cpuid = %d\n",
919 fdt_boot_cpuid_phys(fdtp));
923 sprintf(line, " size_dt_strings = %d\n",
924 fdt_size_dt_strings(fdtp));
928 sprintf(line, " size_dt_struct = %d\n",
929 fdt_size_dt_struct(fdtp));
938 fdt_cmd_ls(int argc, char *argv[])
940 const char *prevname[FDT_MAX_DEPTH] = { NULL };
943 int i, o, depth, len;
945 path = (argc > 2) ? argv[2] : NULL;
949 o = fdt_path_offset(fdtp, path);
951 sprintf(command_errbuf, "could not find node: '%s'", path);
956 (o >= 0) && (depth >= 0);
957 o = fdt_next_node(fdtp, o, &depth)) {
959 name = fdt_get_name(fdtp, o, &len);
961 if (depth > FDT_MAX_DEPTH) {
962 printf("max depth exceeded: %d\n", depth);
966 prevname[depth] = name;
968 /* Skip root (i = 1) when printing devices */
969 for (i = 1; i <= depth; i++) {
970 if (prevname[i] == NULL)
973 if (strcmp(cwd, "/") == 0)
975 printf("%s", prevname[i]);
987 return (c >= ' ' && c <= 0x7e);
991 fdt_isprint(const void *data, int len, int *count)
1000 d = (const char *)data;
1001 if (d[len - 1] != '\0')
1006 for (i = 0; i < len; i++) {
1008 if (isprint(ch) || (ch == '\0' && i > 0)) {
1023 fdt_data_str(const void *data, int len, int count, char **buf)
1030 * Calculate the length for the string and allocate memory.
1032 * Note that 'len' already includes at least one terminator.
1037 * Each token had already a terminator buried in 'len', but we
1038 * only need one eventually, don't count space for these.
1040 buf_len -= count - 1;
1042 /* Each consecutive token requires a ", " separator. */
1043 buf_len += count * 2;
1046 /* Add some space for surrounding double quotes. */
1047 buf_len += count * 2;
1049 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1050 b = (char *)malloc(buf_len);
1051 tmp = (char *)malloc(buf_len);
1063 * Now that we have space, format the string.
1067 d = (const char *)data + i;
1070 sprintf(tmp, "\"%s\"%s", d,
1071 (i + l) < len ? ", " : "");
1087 fdt_data_cell(const void *data, int len, char **buf)
1093 /* Number of cells */
1097 * Calculate the length for the string and allocate memory.
1100 /* Each byte translates to 2 output characters */
1103 /* Each consecutive cell requires a " " separator. */
1104 l += (count - 1) * 1;
1106 /* Each cell will have a "0x" prefix */
1108 /* Space for surrounding <> and terminator */
1111 b = (char *)malloc(l);
1112 tmp = (char *)malloc(l);
1124 for (i = 0; i < len; i += 4) {
1125 c = (const uint32_t *)((const uint8_t *)data + i);
1126 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1127 i < (len - 4) ? " " : "");
1141 fdt_data_bytes(const void *data, int len, char **buf)
1148 * Calculate the length for the string and allocate memory.
1151 /* Each byte translates to 2 output characters */
1154 /* Each consecutive byte requires a " " separator. */
1156 /* Each byte will have a "0x" prefix */
1158 /* Space for surrounding [] and terminator. */
1161 b = (char *)malloc(l);
1162 tmp = (char *)malloc(l);
1174 for (i = 0, d = data; i < len; i++) {
1175 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1189 fdt_data_fmt(const void *data, int len, char **buf)
1198 if (fdt_isprint(data, len, &count))
1199 return (fdt_data_str(data, len, count, buf));
1201 else if ((len % 4) == 0)
1202 return (fdt_data_cell(data, len, buf));
1205 return (fdt_data_bytes(data, len, buf));
1209 fdt_prop(int offset)
1212 const struct fdt_property *prop;
1218 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1222 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1223 len = fdt32_to_cpu(prop->len);
1228 /* Property without value */
1229 line = (char *)malloc(strlen(name) + 2);
1234 sprintf(line, "%s\n", name);
1239 * Process property with value
1243 if (fdt_data_fmt(data, len, &buf) != 0) {
1248 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1251 sprintf(command_errbuf, "could not allocate space for string");
1256 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1274 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1276 uint32_t cells[100];
1279 const struct fdt_property *p;
1281 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1285 /* Adding inexistant value in mode 1 is forbidden */
1286 sprintf(command_errbuf, "property already exists!");
1289 } else if (mode == 0) {
1290 sprintf(command_errbuf, "property does not exist!");
1293 len = strlen(value);
1295 buf = (char *)value;
1303 len = fdt_strtovect(buf, (void *)&cells, 100,
1306 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1307 len * sizeof(uint32_t));
1311 len = fdt_strtovect(buf, (void *)&cells, 100,
1314 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1315 len * sizeof(uint8_t));
1319 /* Default -- string */
1320 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1325 if (rv == -FDT_ERR_NOSPACE)
1326 sprintf(command_errbuf,
1327 "Device tree blob is too small!\n");
1329 sprintf(command_errbuf,
1330 "Could not add/modify property!\n");
1335 /* Merge strings from argv into a single string */
1337 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1345 for (i = start; i < argc; i++)
1346 sz += strlen(argv[i]);
1348 /* Additional bytes for whitespaces between args */
1351 buf = (char *)malloc(sizeof(char) * sz);
1352 bzero(buf, sizeof(char) * sz);
1355 sprintf(command_errbuf, "could not allocate space "
1361 for (i = start, idx = 0; i < argc; i++) {
1362 strcpy(buf + idx, argv[i]);
1363 idx += strlen(argv[i]);
1372 /* Extract offset and name of node/property from a given path */
1374 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1377 char *path = *pathp, *name = NULL, *subpath = NULL;
1379 subpath = strrchr(path, '/');
1380 if (subpath == NULL) {
1381 o = fdt_path_offset(fdtp, cwd);
1383 path = (char *)&cwd;
1386 if (strlen(path) == 0)
1390 o = fdt_path_offset(fdtp, path);
1393 if (strlen(name) == 0) {
1394 sprintf(command_errbuf, "name not specified");
1398 sprintf(command_errbuf, "could not find node: '%s'", path);
1408 fdt_cmd_prop(int argc, char *argv[])
1410 char *path, *propname, *value;
1411 int o, next, depth, rv;
1414 path = (argc > 2) ? argv[2] : NULL;
1419 /* Merge property value strings into one */
1420 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1431 /* If value is specified -- try to modify prop. */
1432 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1435 rv = fdt_modprop(o, propname, value, 0);
1441 /* User wants to display properties */
1442 o = fdt_path_offset(fdtp, path);
1445 sprintf(command_errbuf, "could not find node: '%s'", path);
1451 while (depth >= 0) {
1452 tag = fdt_next_tag(fdtp, o, &next);
1458 /* Don't process properties of nested nodes */
1461 if (fdt_prop(o) != 0) {
1462 sprintf(command_errbuf, "could not process "
1468 case FDT_BEGIN_NODE:
1470 if (depth > FDT_MAX_DEPTH) {
1471 printf("warning: nesting too deep: %d\n",
1480 * This is the end of our starting node, force
1493 fdt_cmd_mkprop(int argc, char *argv[])
1496 char *path, *propname, *value;
1498 path = (argc > 2) ? argv[2] : NULL;
1503 /* Merge property value strings into one */
1504 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1509 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1512 if (fdt_modprop(o, propname, value, 1))
1519 fdt_cmd_rm(int argc, char *argv[])
1522 char *path = NULL, *propname;
1527 sprintf(command_errbuf, "no node/property name specified");
1531 o = fdt_path_offset(fdtp, path);
1533 /* If node not found -- try to find & delete property */
1534 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1537 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1538 sprintf(command_errbuf, "could not delete"
1539 "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1540 "(property/node does not exist)" : "");
1546 /* If node exists -- remove node */
1547 rv = fdt_del_node(fdtp, o);
1549 sprintf(command_errbuf, "could not delete node");
1556 fdt_cmd_mknode(int argc, char *argv[])
1559 char *path = NULL, *nodename = NULL;
1564 sprintf(command_errbuf, "no node name specified");
1568 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1571 rv = fdt_add_subnode(fdtp, o, nodename);
1574 if (rv == -FDT_ERR_NOSPACE)
1575 sprintf(command_errbuf,
1576 "Device tree blob is too small!\n");
1578 sprintf(command_errbuf,
1579 "Could not add node!\n");
1586 fdt_cmd_pwd(int argc, char *argv[])
1588 char line[FDT_CWD_LEN];
1591 sprintf(line, "%s\n", cwd);
1598 fdt_cmd_mres(int argc, char *argv[])
1600 uint64_t start, size;
1605 total = fdt_num_mem_rsv(fdtp);
1607 pager_output("Reserved memory regions:\n");
1608 for (i = 0; i < total; i++) {
1609 fdt_get_mem_rsv(fdtp, i, &start, &size);
1610 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1615 pager_output("No reserved memory regions\n");
1622 fdt_cmd_nyi(int argc, char *argv[])
1625 printf("command not yet implemented\n");