2 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * This software was developed by Semihalf under sponsorship from
6 * the FreeBSD Foundation.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/linker.h>
38 #include <machine/elf.h>
40 #include "bootstrap.h"
41 #include "fdt_platform.h"
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 COPYOUT(s,d,l) archsw.arch_copyout(s, d, l)
56 #define COPYIN(s,d,l) archsw.arch_copyin(s, d, l)
58 #define FDT_STATIC_DTB_SYMBOL "fdt_static_dtb"
60 #define CMD_REQUIRES_BLOB 0x01
62 /* Location of FDT yet to be loaded. */
63 /* This may be in read-only memory, so can't be manipulated directly. */
64 static struct fdt_header *fdt_to_load = NULL;
65 /* Location of FDT on heap. */
66 /* This is the copy we actually manipulate. */
67 static struct fdt_header *fdtp = NULL;
68 /* Size of FDT blob */
69 static size_t fdtp_size = 0;
70 /* Location of FDT in kernel or module. */
71 /* This won't be set if FDT is loaded from disk or memory. */
72 /* If it is set, we'll update it when fdt_copy() gets called. */
73 static vm_offset_t fdtp_va = 0;
75 static int fdt_load_dtb(vm_offset_t va);
77 static int fdt_cmd_nyi(int argc, char *argv[]);
79 static int fdt_cmd_addr(int argc, char *argv[]);
80 static int fdt_cmd_mkprop(int argc, char *argv[]);
81 static int fdt_cmd_cd(int argc, char *argv[]);
82 static int fdt_cmd_hdr(int argc, char *argv[]);
83 static int fdt_cmd_ls(int argc, char *argv[]);
84 static int fdt_cmd_prop(int argc, char *argv[]);
85 static int fdt_cmd_pwd(int argc, char *argv[]);
86 static int fdt_cmd_rm(int argc, char *argv[]);
87 static int fdt_cmd_mknode(int argc, char *argv[]);
88 static int fdt_cmd_mres(int argc, char *argv[]);
90 typedef int cmdf_t(int, char *[]);
98 static const struct cmdtab commands[] = {
99 { "addr", &fdt_cmd_addr, 0 },
100 { "alias", &fdt_cmd_nyi, 0 },
101 { "cd", &fdt_cmd_cd, CMD_REQUIRES_BLOB },
102 { "header", &fdt_cmd_hdr, CMD_REQUIRES_BLOB },
103 { "ls", &fdt_cmd_ls, CMD_REQUIRES_BLOB },
104 { "mknode", &fdt_cmd_mknode, CMD_REQUIRES_BLOB },
105 { "mkprop", &fdt_cmd_mkprop, CMD_REQUIRES_BLOB },
106 { "mres", &fdt_cmd_mres, CMD_REQUIRES_BLOB },
107 { "prop", &fdt_cmd_prop, CMD_REQUIRES_BLOB },
108 { "pwd", &fdt_cmd_pwd, CMD_REQUIRES_BLOB },
109 { "rm", &fdt_cmd_rm, CMD_REQUIRES_BLOB },
113 static char cwd[FDT_CWD_LEN] = "/";
116 fdt_find_static_dtb()
121 vm_offset_t strtab, symtab, fdt_start;
123 struct preloaded_file *kfp;
124 struct file_metadata *md;
128 debugf("fdt_find_static_dtb()\n");
130 sym_count = symtab = strtab = 0;
133 offs = __elfN(relocation_offset);
135 kfp = file_findfile(NULL, NULL);
139 /* Locate the dynamic symbols and strtab. */
140 md = file_findmetadata(kfp, MODINFOMD_ELFHDR);
143 ehdr = (Elf_Ehdr *)md->md_data;
145 md = file_findmetadata(kfp, MODINFOMD_SHDR);
148 shdr = (Elf_Shdr *)md->md_data;
150 for (i = 0; i < ehdr->e_shnum; ++i) {
151 if (shdr[i].sh_type == SHT_DYNSYM && symtab == 0) {
152 symtab = shdr[i].sh_addr + offs;
153 sym_count = shdr[i].sh_size / sizeof(Elf_Sym);
154 } else if (shdr[i].sh_type == SHT_STRTAB && strtab == 0) {
155 strtab = shdr[i].sh_addr + offs;
160 * The most efficent way to find a symbol would be to calculate a
161 * hash, find proper bucket and chain, and thus find a symbol.
162 * However, that would involve code duplication (e.g. for hash
163 * function). So we're using simpler and a bit slower way: we're
164 * iterating through symbols, searching for the one which name is
165 * 'equal' to 'fdt_static_dtb'. To speed up the process a little bit,
166 * we are eliminating symbols type of which is not STT_NOTYPE, or(and)
167 * those which binding attribute is not STB_GLOBAL.
170 while (sym_count > 0 && fdt_start == 0) {
171 COPYOUT(symtab, &sym, sizeof(sym));
172 symtab += sizeof(sym);
174 if (ELF_ST_BIND(sym.st_info) != STB_GLOBAL ||
175 ELF_ST_TYPE(sym.st_info) != STT_NOTYPE)
177 strp = strdupout(strtab + sym.st_name);
178 if (strcmp(strp, FDT_STATIC_DTB_SYMBOL) == 0)
179 fdt_start = (vm_offset_t)sym.st_value + offs;
186 fdt_load_dtb(vm_offset_t va)
188 struct fdt_header header;
191 debugf("fdt_load_dtb(0x%08jx)\n", (uintmax_t)va);
193 COPYOUT(va, &header, sizeof(header));
194 err = fdt_check_header(&header);
196 if (err == -FDT_ERR_BADVERSION)
197 sprintf(command_errbuf,
198 "incompatible blob version: %d, should be: %d",
199 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
202 sprintf(command_errbuf, "error validating blob: %s",
208 * Release previous blob
213 fdtp_size = fdt_totalsize(&header);
214 fdtp = malloc(fdtp_size);
217 command_errmsg = "can't allocate memory for device tree copy";
222 COPYOUT(va, fdtp, fdtp_size);
223 debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
229 fdt_load_dtb_addr(struct fdt_header *header)
233 debugf("fdt_load_dtb_addr(0x%p)\n", header);
235 fdtp_size = fdt_totalsize(header);
236 err = fdt_check_header(header);
238 sprintf(command_errbuf, "error validating blob: %s",
243 if ((fdtp = malloc(fdtp_size)) == NULL) {
244 command_errmsg = "can't allocate memory for device tree copy";
248 fdtp_va = 0; // Don't write this back into module or kernel.
249 bcopy(header, fdtp, fdtp_size);
254 fdt_load_dtb_file(const char * filename)
256 struct preloaded_file *bfp, *oldbfp;
259 debugf("fdt_load_dtb_file(%s)\n", filename);
261 oldbfp = file_findfile(NULL, "dtb");
263 /* Attempt to load and validate a new dtb from a file. */
264 if ((bfp = file_loadraw(filename, "dtb", 1)) == NULL) {
265 sprintf(command_errbuf, "failed to load file '%s'", filename);
268 if ((err = fdt_load_dtb(bfp->f_addr)) != 0) {
273 /* A new dtb was validated, discard any previous file. */
275 file_discard(oldbfp);
282 struct preloaded_file *bfp;
285 debugf("fdt_setup_fdtp()\n");
287 /* If we already loaded a file, use it. */
288 if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
289 if (fdt_load_dtb(bfp->f_addr) == 0) {
290 printf("Using DTB from loaded file '%s'.\n",
296 /* If we were given the address of a valid blob in memory, use it. */
297 if (fdt_to_load != NULL) {
298 if (fdt_load_dtb_addr(fdt_to_load) == 0) {
299 printf("Using DTB from memory address 0x%08X.\n",
300 (unsigned int)fdt_to_load);
305 if (fdt_platform_load_dtb() == 0)
308 /* If there is a dtb compiled into the kernel, use it. */
309 if ((va = fdt_find_static_dtb()) != 0) {
310 if (fdt_load_dtb(va) == 0) {
311 printf("Using DTB compiled into kernel.\n");
316 command_errmsg = "No device tree blob found!\n";
320 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
321 (cellbuf), (lim), (cellsize), 0);
323 /* Force using base 16 */
324 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
325 (cellbuf), (lim), (cellsize), 16);
328 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize,
331 const char *buf = str;
332 const char *end = str + strlen(str) - 2;
333 uint32_t *u32buf = NULL;
334 uint8_t *u8buf = NULL;
337 if (cellsize == sizeof(uint32_t))
338 u32buf = (uint32_t *)cellbuf;
340 u8buf = (uint8_t *)cellbuf;
347 /* Skip white whitespace(s)/separators */
348 while (!isxdigit(*buf) && buf < end)
353 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
356 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
358 if (cnt + 1 <= lim - 1)
363 /* Find another number */
364 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
371 fdt_fixup_ethernet(const char *str, char *ethstr, int len)
375 /* Convert macaddr string into a vector of uints */
376 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
377 /* Set actual property to a value from vect */
378 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
379 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
383 fdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
385 int lo, o = 0, o2, maxo = 0, depth;
386 const uint32_t zero = 0;
388 /* We want to modify every subnode of /cpus */
389 o = fdt_path_offset(fdtp, "/cpus");
393 /* maxo should contain offset of node next to /cpus */
397 maxo = fdt_next_node(fdtp, maxo, &depth);
399 /* Find CPU frequency properties */
400 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
401 &zero, sizeof(uint32_t));
403 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
408 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
410 o = fdt_node_offset_by_prop_value(fdtp, lo,
411 "clock-frequency", &zero, sizeof(uint32_t));
413 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
414 &zero, sizeof(uint32_t));
416 /* We're only interested in /cpus subnode(s) */
420 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
423 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
431 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
433 int cells_in_tuple, i, tuples, tuple_size;
434 uint32_t cur_start, cur_size;
436 cells_in_tuple = (addr_cells + size_cells);
437 tuple_size = cells_in_tuple * sizeof(uint32_t);
438 tuples = len / tuple_size;
442 for (i = 0; i < tuples; i++) {
444 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
446 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
449 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
451 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
456 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
457 i, cur_start, cur_size);
463 fdt_fixup_memory(struct fdt_mem_region *region, size_t num)
465 struct fdt_mem_region *curmr;
466 uint32_t addr_cells, size_cells;
467 uint32_t *addr_cellsp, *size_cellsp;
468 int err, i, len, memory, root;
471 uint64_t rstart, rsize;
474 root = fdt_path_offset(fdtp, "/");
476 sprintf(command_errbuf, "Could not find root node !");
480 memory = fdt_path_offset(fdtp, "/memory");
482 /* Create proper '/memory' node. */
483 memory = fdt_add_subnode(fdtp, root, "memory");
485 sprintf(command_errbuf, "Could not fixup '/memory' "
486 "node, error code : %d!\n", memory);
490 err = fdt_setprop(fdtp, memory, "device_type", "memory",
497 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
499 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
501 if (addr_cellsp == NULL || size_cellsp == NULL) {
502 sprintf(command_errbuf, "Could not fixup '/memory' node : "
503 "%s %s property not found in root node!\n",
504 (!addr_cellsp) ? "#address-cells" : "",
505 (!size_cellsp) ? "#size-cells" : "");
509 addr_cells = fdt32_to_cpu(*addr_cellsp);
510 size_cells = fdt32_to_cpu(*size_cellsp);
513 * Convert memreserve data to memreserve property
514 * Check if property already exists
516 reserved = fdt_num_mem_rsv(fdtp);
518 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
519 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
520 sb = buf = (uint8_t *)malloc(len);
526 for (i = 0; i < reserved; i++) {
527 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
530 /* Ensure endianess, and put cells into a buffer */
533 cpu_to_fdt64(rstart);
536 cpu_to_fdt32(rstart);
538 buf += sizeof(uint32_t) * addr_cells;
546 buf += sizeof(uint32_t) * size_cells;
551 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
552 printf("Could not fixup 'memreserve' property.\n");
557 /* Count valid memory regions entries in sysinfo. */
559 for (i = 0; i < num; i++)
560 if (region[i].start == 0 && region[i].size == 0)
564 sprintf(command_errbuf, "Could not fixup '/memory' node : "
565 "sysinfo doesn't contain valid memory regions info!\n");
569 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
570 sb = buf = (uint8_t *)malloc(len);
576 for (i = 0; i < num; i++) {
578 if (curmr->size != 0) {
579 /* Ensure endianess, and put cells into a buffer */
582 cpu_to_fdt64(curmr->start);
585 cpu_to_fdt32(curmr->start);
587 buf += sizeof(uint32_t) * addr_cells;
590 cpu_to_fdt64(curmr->size);
593 cpu_to_fdt32(curmr->size);
595 buf += sizeof(uint32_t) * size_cells;
600 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
601 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
607 fdt_fixup_stdout(const char *str)
612 const struct fdt_property *prop;
615 ptr = (char *)str + strlen(str) - 1;
616 while (ptr > str && isdigit(*(str - 1)))
622 serialno = (int)strtol(ptr, NULL, 0);
623 no = fdt_path_offset(fdtp, "/chosen");
627 prop = fdt_get_property(fdtp, no, "stdout", &len);
629 /* If /chosen/stdout does not extist, create it */
630 if (prop == NULL || (prop != NULL && len == 0)) {
632 bzero(tmp, 10 * sizeof(char));
633 strcpy((char *)&tmp, "serial");
635 /* Serial number too long */
638 strncpy((char *)tmp + 6, ptr, 3);
639 sero = fdt_path_offset(fdtp, (const char *)tmp);
642 * If serial device we're trying to assign
643 * stdout to doesn't exist in DT -- return.
647 fdt_setprop(fdtp, no, "stdout", &tmp,
648 strlen((char *)&tmp) + 1);
649 fdt_setprop(fdtp, no, "stdin", &tmp,
650 strlen((char *)&tmp) + 1);
655 * Locate the blob, fix it up and return its location.
664 debugf("fdt_fixup()\n");
666 if (fdtp == NULL && fdt_setup_fdtp() != 0)
669 /* Create /chosen node (if not exists) */
670 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
672 chosen = fdt_add_subnode(fdtp, 0, "chosen");
674 /* Value assigned to fixup-applied does not matter. */
675 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
678 fdt_platform_fixups();
680 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
685 * Copy DTB blob to specified location and return size
688 fdt_copy(vm_offset_t va)
691 debugf("fdt_copy va 0x%08x\n", va);
693 err = fdt_setup_fdtp();
695 printf("No valid device tree blob found!\n");
700 if (fdt_fixup() == 0)
704 /* Overwrite the FDT with the fixed version. */
705 /* XXX Is this really appropriate? */
706 COPYIN(fdtp, fdtp_va, fdtp_size);
708 COPYIN(fdtp, va, fdtp_size);
715 command_fdt_internal(int argc, char *argv[])
723 command_errmsg = "usage is 'fdt <command> [<args>]";
728 * Validate fdt <command>.
730 cmd = strdup(argv[1]);
733 while (!(commands[i].name == NULL)) {
734 if (strcmp(cmd, commands[i].name) == 0) {
736 cmdh = commands[i].handler;
737 flags = commands[i].flags;
743 command_errmsg = "unknown command";
747 if (flags & CMD_REQUIRES_BLOB) {
749 * Check if uboot env vars were parsed already. If not, do it now.
751 if (fdt_fixup() == 0)
756 * Call command handler.
758 err = (*cmdh)(argc, argv);
764 fdt_cmd_addr(int argc, char *argv[])
766 struct preloaded_file *fp;
767 struct fdt_header *hdr;
776 sprintf(command_errbuf, "no address specified");
780 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
782 sprintf(command_errbuf, "Invalid address: %s", addr);
786 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
795 fdt_cmd_cd(int argc, char *argv[])
798 char tmp[FDT_CWD_LEN];
801 path = (argc > 2) ? argv[2] : "/";
803 if (path[0] == '/') {
805 if (len >= FDT_CWD_LEN)
808 /* Handle path specification relative to cwd */
809 len = strlen(cwd) + strlen(path) + 1;
810 if (len >= FDT_CWD_LEN)
819 o = fdt_path_offset(fdtp, path);
821 sprintf(command_errbuf, "could not find node: '%s'", path);
829 sprintf(command_errbuf, "path too long: %d, max allowed: %d",
830 len, FDT_CWD_LEN - 1);
835 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
841 command_errmsg = "no device tree blob pointer?!";
845 ver = fdt_version(fdtp);
847 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
849 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
851 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
853 sprintf(line, " off_dt_struct = 0x%08x\n",
854 fdt_off_dt_struct(fdtp));
856 sprintf(line, " off_dt_strings = 0x%08x\n",
857 fdt_off_dt_strings(fdtp));
859 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
860 fdt_off_mem_rsvmap(fdtp));
862 sprintf(line, " version = %d\n", ver);
864 sprintf(line, " last compatible version = %d\n",
865 fdt_last_comp_version(fdtp));
868 sprintf(line, " boot_cpuid = %d\n",
869 fdt_boot_cpuid_phys(fdtp));
873 sprintf(line, " size_dt_strings = %d\n",
874 fdt_size_dt_strings(fdtp));
878 sprintf(line, " size_dt_struct = %d\n",
879 fdt_size_dt_struct(fdtp));
888 fdt_cmd_ls(int argc, char *argv[])
890 const char *prevname[FDT_MAX_DEPTH] = { NULL };
893 int i, o, depth, len;
895 path = (argc > 2) ? argv[2] : NULL;
899 o = fdt_path_offset(fdtp, path);
901 sprintf(command_errbuf, "could not find node: '%s'", path);
906 (o >= 0) && (depth >= 0);
907 o = fdt_next_node(fdtp, o, &depth)) {
909 name = fdt_get_name(fdtp, o, &len);
911 if (depth > FDT_MAX_DEPTH) {
912 printf("max depth exceeded: %d\n", depth);
916 prevname[depth] = name;
918 /* Skip root (i = 1) when printing devices */
919 for (i = 1; i <= depth; i++) {
920 if (prevname[i] == NULL)
923 if (strcmp(cwd, "/") == 0)
925 printf("%s", prevname[i]);
937 return (c >= ' ' && c <= 0x7e);
941 fdt_isprint(const void *data, int len, int *count)
950 d = (const char *)data;
951 if (d[len - 1] != '\0')
956 for (i = 0; i < len; i++) {
958 if (isprint(ch) || (ch == '\0' && i > 0)) {
973 fdt_data_str(const void *data, int len, int count, char **buf)
980 * Calculate the length for the string and allocate memory.
982 * Note that 'len' already includes at least one terminator.
987 * Each token had already a terminator buried in 'len', but we
988 * only need one eventually, don't count space for these.
990 buf_len -= count - 1;
992 /* Each consecutive token requires a ", " separator. */
993 buf_len += count * 2;
996 /* Add some space for surrounding double quotes. */
997 buf_len += count * 2;
999 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1000 b = (char *)malloc(buf_len);
1001 tmp = (char *)malloc(buf_len);
1013 * Now that we have space, format the string.
1017 d = (const char *)data + i;
1020 sprintf(tmp, "\"%s\"%s", d,
1021 (i + l) < len ? ", " : "");
1037 fdt_data_cell(const void *data, int len, char **buf)
1043 /* Number of cells */
1047 * Calculate the length for the string and allocate memory.
1050 /* Each byte translates to 2 output characters */
1053 /* Each consecutive cell requires a " " separator. */
1054 l += (count - 1) * 1;
1056 /* Each cell will have a "0x" prefix */
1058 /* Space for surrounding <> and terminator */
1061 b = (char *)malloc(l);
1062 tmp = (char *)malloc(l);
1074 for (i = 0; i < len; i += 4) {
1075 c = (const uint32_t *)((const uint8_t *)data + i);
1076 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1077 i < (len - 4) ? " " : "");
1091 fdt_data_bytes(const void *data, int len, char **buf)
1098 * Calculate the length for the string and allocate memory.
1101 /* Each byte translates to 2 output characters */
1104 /* Each consecutive byte requires a " " separator. */
1106 /* Each byte 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, d = data; i < len; i++) {
1125 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1139 fdt_data_fmt(const void *data, int len, char **buf)
1148 if (fdt_isprint(data, len, &count))
1149 return (fdt_data_str(data, len, count, buf));
1151 else if ((len % 4) == 0)
1152 return (fdt_data_cell(data, len, buf));
1155 return (fdt_data_bytes(data, len, buf));
1159 fdt_prop(int offset)
1162 const struct fdt_property *prop;
1168 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1172 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1173 len = fdt32_to_cpu(prop->len);
1178 /* Property without value */
1179 line = (char *)malloc(strlen(name) + 2);
1184 sprintf(line, "%s\n", name);
1189 * Process property with value
1193 if (fdt_data_fmt(data, len, &buf) != 0) {
1198 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1201 sprintf(command_errbuf, "could not allocate space for string");
1206 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1224 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1226 uint32_t cells[100];
1229 const struct fdt_property *p;
1231 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1235 /* Adding inexistant value in mode 1 is forbidden */
1236 sprintf(command_errbuf, "property already exists!");
1239 } else if (mode == 0) {
1240 sprintf(command_errbuf, "property does not exist!");
1243 len = strlen(value);
1253 len = fdt_strtovect(buf, (void *)&cells, 100,
1256 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1257 len * sizeof(uint32_t));
1261 len = fdt_strtovect(buf, (void *)&cells, 100,
1264 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1265 len * sizeof(uint8_t));
1269 /* Default -- string */
1270 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1275 if (rv == -FDT_ERR_NOSPACE)
1276 sprintf(command_errbuf,
1277 "Device tree blob is too small!\n");
1279 sprintf(command_errbuf,
1280 "Could not add/modify property!\n");
1285 /* Merge strings from argv into a single string */
1287 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1295 for (i = start; i < argc; i++)
1296 sz += strlen(argv[i]);
1298 /* Additional bytes for whitespaces between args */
1301 buf = (char *)malloc(sizeof(char) * sz);
1303 sprintf(command_errbuf, "could not allocate space "
1307 bzero(buf, sizeof(char) * sz);
1310 for (i = start, idx = 0; i < argc; i++) {
1311 strcpy(buf + idx, argv[i]);
1312 idx += strlen(argv[i]);
1321 /* Extract offset and name of node/property from a given path */
1323 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1326 char *path = *pathp, *name = NULL, *subpath = NULL;
1328 subpath = strrchr(path, '/');
1329 if (subpath == NULL) {
1330 o = fdt_path_offset(fdtp, cwd);
1332 path = (char *)&cwd;
1335 if (strlen(path) == 0)
1339 o = fdt_path_offset(fdtp, path);
1342 if (strlen(name) == 0) {
1343 sprintf(command_errbuf, "name not specified");
1347 sprintf(command_errbuf, "could not find node: '%s'", path);
1357 fdt_cmd_prop(int argc, char *argv[])
1359 char *path, *propname, *value;
1360 int o, next, depth, rv;
1363 path = (argc > 2) ? argv[2] : NULL;
1368 /* Merge property value strings into one */
1369 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1380 /* If value is specified -- try to modify prop. */
1381 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1384 rv = fdt_modprop(o, propname, value, 0);
1390 /* User wants to display properties */
1391 o = fdt_path_offset(fdtp, path);
1394 sprintf(command_errbuf, "could not find node: '%s'", path);
1400 while (depth >= 0) {
1401 tag = fdt_next_tag(fdtp, o, &next);
1407 /* Don't process properties of nested nodes */
1410 if (fdt_prop(o) != 0) {
1411 sprintf(command_errbuf, "could not process "
1417 case FDT_BEGIN_NODE:
1419 if (depth > FDT_MAX_DEPTH) {
1420 printf("warning: nesting too deep: %d\n",
1429 * This is the end of our starting node, force
1442 fdt_cmd_mkprop(int argc, char *argv[])
1445 char *path, *propname, *value;
1447 path = (argc > 2) ? argv[2] : NULL;
1452 /* Merge property value strings into one */
1453 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1458 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1461 if (fdt_modprop(o, propname, value, 1))
1468 fdt_cmd_rm(int argc, char *argv[])
1471 char *path = NULL, *propname;
1476 sprintf(command_errbuf, "no node/property name specified");
1480 o = fdt_path_offset(fdtp, path);
1482 /* If node not found -- try to find & delete property */
1483 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1486 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1487 sprintf(command_errbuf, "could not delete"
1488 "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1489 "(property/node does not exist)" : "");
1495 /* If node exists -- remove node */
1496 rv = fdt_del_node(fdtp, o);
1498 sprintf(command_errbuf, "could not delete node");
1505 fdt_cmd_mknode(int argc, char *argv[])
1508 char *path = NULL, *nodename = NULL;
1513 sprintf(command_errbuf, "no node name specified");
1517 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1520 rv = fdt_add_subnode(fdtp, o, nodename);
1523 if (rv == -FDT_ERR_NOSPACE)
1524 sprintf(command_errbuf,
1525 "Device tree blob is too small!\n");
1527 sprintf(command_errbuf,
1528 "Could not add node!\n");
1535 fdt_cmd_pwd(int argc, char *argv[])
1537 char line[FDT_CWD_LEN];
1540 sprintf(line, "%s\n", cwd);
1547 fdt_cmd_mres(int argc, char *argv[])
1549 uint64_t start, size;
1554 total = fdt_num_mem_rsv(fdtp);
1556 pager_output("Reserved memory regions:\n");
1557 for (i = 0; i < total; i++) {
1558 fdt_get_mem_rsv(fdtp, i, &start, &size);
1559 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1564 pager_output("No reserved memory regions\n");
1571 fdt_cmd_nyi(int argc, char *argv[])
1574 printf("command not yet implemented\n");