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"
42 #include "fdt_overlay.h"
45 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
46 printf(fmt,##args); } while (0)
48 #define debugf(fmt, args...)
51 #define FDT_CWD_LEN 256
52 #define FDT_MAX_DEPTH 6
54 #define FDT_PROP_SEP " = "
56 #define COPYOUT(s,d,l) archsw.arch_copyout(s, d, l)
57 #define COPYIN(s,d,l) archsw.arch_copyin(s, d, l)
59 #define FDT_STATIC_DTB_SYMBOL "fdt_static_dtb"
61 #define CMD_REQUIRES_BLOB 0x01
63 /* Location of FDT yet to be loaded. */
64 /* This may be in read-only memory, so can't be manipulated directly. */
65 static struct fdt_header *fdt_to_load = NULL;
66 /* Location of FDT on heap. */
67 /* This is the copy we actually manipulate. */
68 static struct fdt_header *fdtp = NULL;
69 /* Size of FDT blob */
70 static size_t fdtp_size = 0;
71 /* Location of FDT in kernel or module. */
72 /* This won't be set if FDT is loaded from disk or memory. */
73 /* If it is set, we'll update it when fdt_copy() gets called. */
74 static vm_offset_t fdtp_va = 0;
76 static int fdt_load_dtb(vm_offset_t va);
78 static int fdt_cmd_nyi(int argc, char *argv[]);
80 static int fdt_cmd_addr(int argc, char *argv[]);
81 static int fdt_cmd_mkprop(int argc, char *argv[]);
82 static int fdt_cmd_cd(int argc, char *argv[]);
83 static int fdt_cmd_hdr(int argc, char *argv[]);
84 static int fdt_cmd_ls(int argc, char *argv[]);
85 static int fdt_cmd_prop(int argc, char *argv[]);
86 static int fdt_cmd_pwd(int argc, char *argv[]);
87 static int fdt_cmd_rm(int argc, char *argv[]);
88 static int fdt_cmd_mknode(int argc, char *argv[]);
89 static int fdt_cmd_mres(int argc, char *argv[]);
91 typedef int cmdf_t(int, char *[]);
99 static const struct cmdtab commands[] = {
100 { "addr", &fdt_cmd_addr, 0 },
101 { "alias", &fdt_cmd_nyi, 0 },
102 { "cd", &fdt_cmd_cd, CMD_REQUIRES_BLOB },
103 { "header", &fdt_cmd_hdr, CMD_REQUIRES_BLOB },
104 { "ls", &fdt_cmd_ls, CMD_REQUIRES_BLOB },
105 { "mknode", &fdt_cmd_mknode, CMD_REQUIRES_BLOB },
106 { "mkprop", &fdt_cmd_mkprop, CMD_REQUIRES_BLOB },
107 { "mres", &fdt_cmd_mres, CMD_REQUIRES_BLOB },
108 { "prop", &fdt_cmd_prop, CMD_REQUIRES_BLOB },
109 { "pwd", &fdt_cmd_pwd, CMD_REQUIRES_BLOB },
110 { "rm", &fdt_cmd_rm, CMD_REQUIRES_BLOB },
114 static char cwd[FDT_CWD_LEN] = "/";
117 fdt_find_static_dtb()
122 vm_offset_t strtab, symtab, fdt_start;
124 struct preloaded_file *kfp;
125 struct file_metadata *md;
129 debugf("fdt_find_static_dtb()\n");
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 efficient 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 debugf("fdt_load_dtb(0x%08jx)\n", (uintmax_t)va);
194 COPYOUT(va, &header, sizeof(header));
195 err = fdt_check_header(&header);
197 if (err == -FDT_ERR_BADVERSION)
198 sprintf(command_errbuf,
199 "incompatible blob version: %d, should be: %d",
200 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
203 sprintf(command_errbuf, "error validating blob: %s",
209 * Release previous blob
214 fdtp_size = fdt_totalsize(&header);
215 fdtp = malloc(fdtp_size);
218 command_errmsg = "can't allocate memory for device tree copy";
223 COPYOUT(va, fdtp, fdtp_size);
224 debugf("DTB blob found at 0x%jx, size: 0x%jx\n", (uintmax_t)va, (uintmax_t)fdtp_size);
230 fdt_load_dtb_addr(struct fdt_header *header)
234 debugf("fdt_load_dtb_addr(%p)\n", header);
236 fdtp_size = fdt_totalsize(header);
237 err = fdt_check_header(header);
239 sprintf(command_errbuf, "error validating blob: %s",
244 if ((fdtp = malloc(fdtp_size)) == NULL) {
245 command_errmsg = "can't allocate memory for device tree copy";
249 fdtp_va = 0; // Don't write this back into module or kernel.
250 bcopy(header, fdtp, fdtp_size);
255 fdt_load_dtb_file(const char * filename)
257 struct preloaded_file *bfp, *oldbfp;
260 debugf("fdt_load_dtb_file(%s)\n", filename);
262 oldbfp = file_findfile(NULL, "dtb");
264 /* Attempt to load and validate a new dtb from a file. */
265 if ((bfp = file_loadraw(filename, "dtb", 1)) == NULL) {
266 sprintf(command_errbuf, "failed to load file '%s'", filename);
269 if ((err = fdt_load_dtb(bfp->f_addr)) != 0) {
274 /* A new dtb was validated, discard any previous file. */
276 file_discard(oldbfp);
281 fdt_load_dtb_overlay(const char * filename)
283 struct preloaded_file *bfp, *oldbfp;
284 struct fdt_header header;
287 debugf("fdt_load_dtb_overlay(%s)\n", filename);
289 oldbfp = file_findfile(filename, "dtbo");
291 /* Attempt to load and validate a new dtb from a file. */
292 if ((bfp = file_loadraw(filename, "dtbo", 1)) == NULL) {
293 printf("failed to load file '%s'\n", filename);
297 COPYOUT(bfp->f_addr, &header, sizeof(header));
298 err = fdt_check_header(&header);
302 if (err == -FDT_ERR_BADVERSION)
303 printf("incompatible blob version: %d, should be: %d\n",
304 fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
307 printf("error validating blob: %s\n",
312 /* A new dtb was validated, discard any previous file. */
314 file_discard(oldbfp);
320 fdt_load_dtb_overlays(const char * filenames)
326 debugf("fdt_load_dtb_overlay(%s)\n", filenames);
328 names = strdup(filenames);
333 comaptr = strchr(name, ',');
336 fdt_load_dtb_overlay(name);
347 struct preloaded_file *fp;
348 size_t overlays_size, max_overlay_size, new_fdtp_size;
353 if ((fdtp == NULL) || (fdtp_size == 0))
357 max_overlay_size = 0;
358 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) {
359 if (max_overlay_size < fp->f_size)
360 max_overlay_size = fp->f_size;
361 overlays_size += fp->f_size;
364 /* Nothing to apply */
365 if (overlays_size == 0)
368 /* It's actually more than enough */
369 new_fdtp_size = fdtp_size + overlays_size;
370 new_fdtp = malloc(new_fdtp_size);
371 if (new_fdtp == NULL) {
372 printf("failed to allocate memory for DTB blob with overlays\n");
376 overlay = malloc(max_overlay_size);
377 if (overlay == NULL) {
378 printf("failed to allocate memory for DTB blob with overlays\n");
383 rv = fdt_open_into(fdtp, new_fdtp, new_fdtp_size);
385 printf("failed to open DTB blob for applying overlays\n");
391 for (fp = file_findfile(NULL, "dtbo"); fp != NULL; fp = fp->f_next) {
392 printf("applying DTB overlay '%s'\n", fp->f_name);
393 COPYOUT(fp->f_addr, overlay, fp->f_size);
394 fdt_overlay_apply(new_fdtp, overlay, fp->f_size);
399 fdtp_size = new_fdtp_size;
407 struct preloaded_file *bfp;
410 debugf("fdt_setup_fdtp()\n");
412 /* If we already loaded a file, use it. */
413 if ((bfp = file_findfile(NULL, "dtb")) != NULL) {
414 if (fdt_load_dtb(bfp->f_addr) == 0) {
415 printf("Using DTB from loaded file '%s'.\n",
421 /* If we were given the address of a valid blob in memory, use it. */
422 if (fdt_to_load != NULL) {
423 if (fdt_load_dtb_addr(fdt_to_load) == 0) {
424 printf("Using DTB from memory address 0x%p.\n",
430 if (fdt_platform_load_dtb() == 0)
433 /* If there is a dtb compiled into the kernel, use it. */
434 if ((va = fdt_find_static_dtb()) != 0) {
435 if (fdt_load_dtb(va) == 0) {
436 printf("Using DTB compiled into kernel.\n");
441 command_errmsg = "No device tree blob found!\n";
445 #define fdt_strtovect(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
446 (cellbuf), (lim), (cellsize), 0);
448 /* Force using base 16 */
449 #define fdt_strtovectx(str, cellbuf, lim, cellsize) _fdt_strtovect((str), \
450 (cellbuf), (lim), (cellsize), 16);
453 _fdt_strtovect(const char *str, void *cellbuf, int lim, unsigned char cellsize,
456 const char *buf = str;
457 const char *end = str + strlen(str) - 2;
458 uint32_t *u32buf = NULL;
459 uint8_t *u8buf = NULL;
462 if (cellsize == sizeof(uint32_t))
463 u32buf = (uint32_t *)cellbuf;
465 u8buf = (uint8_t *)cellbuf;
472 /* Skip white whitespace(s)/separators */
473 while (!isxdigit(*buf) && buf < end)
478 cpu_to_fdt32((uint32_t)strtol(buf, NULL, base));
481 u8buf[cnt] = (uint8_t)strtol(buf, NULL, base);
483 if (cnt + 1 <= lim - 1)
488 /* Find another number */
489 while ((isxdigit(*buf) || *buf == 'x') && buf < end)
496 fdt_fixup_ethernet(const char *str, char *ethstr, int len)
500 /* Convert macaddr string into a vector of uints */
501 fdt_strtovectx(str, &tmp_addr, 6, sizeof(uint8_t));
502 /* Set actual property to a value from vect */
503 fdt_setprop(fdtp, fdt_path_offset(fdtp, ethstr),
504 "local-mac-address", &tmp_addr, 6 * sizeof(uint8_t));
508 fdt_fixup_cpubusfreqs(unsigned long cpufreq, unsigned long busfreq)
510 int lo, o = 0, o2, maxo = 0, depth;
511 const uint32_t zero = 0;
513 /* We want to modify every subnode of /cpus */
514 o = fdt_path_offset(fdtp, "/cpus");
518 /* maxo should contain offset of node next to /cpus */
522 maxo = fdt_next_node(fdtp, maxo, &depth);
524 /* Find CPU frequency properties */
525 o = fdt_node_offset_by_prop_value(fdtp, o, "clock-frequency",
526 &zero, sizeof(uint32_t));
528 o2 = fdt_node_offset_by_prop_value(fdtp, o, "bus-frequency", &zero,
533 while (o != -FDT_ERR_NOTFOUND && o2 != -FDT_ERR_NOTFOUND) {
535 o = fdt_node_offset_by_prop_value(fdtp, lo,
536 "clock-frequency", &zero, sizeof(uint32_t));
538 o2 = fdt_node_offset_by_prop_value(fdtp, lo, "bus-frequency",
539 &zero, sizeof(uint32_t));
541 /* We're only interested in /cpus subnode(s) */
545 fdt_setprop_inplace_cell(fdtp, lo, "clock-frequency",
548 fdt_setprop_inplace_cell(fdtp, lo, "bus-frequency",
557 fdt_reg_valid(uint32_t *reg, int len, int addr_cells, int size_cells)
559 int cells_in_tuple, i, tuples, tuple_size;
560 uint32_t cur_start, cur_size;
562 cells_in_tuple = (addr_cells + size_cells);
563 tuple_size = cells_in_tuple * sizeof(uint32_t);
564 tuples = len / tuple_size;
568 for (i = 0; i < tuples; i++) {
570 cur_start = fdt64_to_cpu(reg[i * cells_in_tuple]);
572 cur_start = fdt32_to_cpu(reg[i * cells_in_tuple]);
575 cur_size = fdt64_to_cpu(reg[i * cells_in_tuple + 2]);
577 cur_size = fdt32_to_cpu(reg[i * cells_in_tuple + 1]);
582 debugf(" reg#%d (start: 0x%0x size: 0x%0x) valid!\n",
583 i, cur_start, cur_size);
590 fdt_fixup_memory(struct fdt_mem_region *region, size_t num)
592 struct fdt_mem_region *curmr;
593 uint32_t addr_cells, size_cells;
594 uint32_t *addr_cellsp, *size_cellsp;
595 int err, i, len, memory, root;
598 uint64_t rstart, rsize;
601 root = fdt_path_offset(fdtp, "/");
603 sprintf(command_errbuf, "Could not find root node !");
607 memory = fdt_path_offset(fdtp, "/memory");
609 /* Create proper '/memory' node. */
610 memory = fdt_add_subnode(fdtp, root, "memory");
612 sprintf(command_errbuf, "Could not fixup '/memory' "
613 "node, error code : %d!\n", memory);
617 err = fdt_setprop(fdtp, memory, "device_type", "memory",
624 addr_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#address-cells",
626 size_cellsp = (uint32_t *)fdt_getprop(fdtp, root, "#size-cells", NULL);
628 if (addr_cellsp == NULL || size_cellsp == NULL) {
629 sprintf(command_errbuf, "Could not fixup '/memory' node : "
630 "%s %s property not found in root node!\n",
631 (!addr_cellsp) ? "#address-cells" : "",
632 (!size_cellsp) ? "#size-cells" : "");
636 addr_cells = fdt32_to_cpu(*addr_cellsp);
637 size_cells = fdt32_to_cpu(*size_cellsp);
640 * Convert memreserve data to memreserve property
641 * Check if property already exists
643 reserved = fdt_num_mem_rsv(fdtp);
645 (fdt_getprop(fdtp, root, "memreserve", NULL) == NULL)) {
646 len = (addr_cells + size_cells) * reserved * sizeof(uint32_t);
647 sb = buf = (uint8_t *)malloc(len);
653 for (i = 0; i < reserved; i++) {
654 if (fdt_get_mem_rsv(fdtp, i, &rstart, &rsize))
657 /* Ensure endianness, and put cells into a buffer */
660 cpu_to_fdt64(rstart);
663 cpu_to_fdt32(rstart);
665 buf += sizeof(uint32_t) * addr_cells;
673 buf += sizeof(uint32_t) * size_cells;
678 if ((err = fdt_setprop(fdtp, root, "memreserve", sb, len)) < 0)
679 printf("Could not fixup 'memreserve' property.\n");
684 /* Count valid memory regions entries in sysinfo. */
686 for (i = 0; i < num; i++)
687 if (region[i].start == 0 && region[i].size == 0)
691 sprintf(command_errbuf, "Could not fixup '/memory' node : "
692 "sysinfo doesn't contain valid memory regions info!\n");
696 len = (addr_cells + size_cells) * realmrno * sizeof(uint32_t);
697 sb = buf = (uint8_t *)malloc(len);
703 for (i = 0; i < num; i++) {
705 if (curmr->size != 0) {
706 /* Ensure endianness, and put cells into a buffer */
709 cpu_to_fdt64(curmr->start);
712 cpu_to_fdt32(curmr->start);
714 buf += sizeof(uint32_t) * addr_cells;
717 cpu_to_fdt64(curmr->size);
720 cpu_to_fdt32(curmr->size);
722 buf += sizeof(uint32_t) * size_cells;
727 if ((err = fdt_setprop(fdtp, memory, "reg", sb, len)) < 0)
728 sprintf(command_errbuf, "Could not fixup '/memory' node.\n");
734 fdt_fixup_stdout(const char *str)
739 const struct fdt_property *prop;
742 ptr = (char *)str + strlen(str) - 1;
743 while (ptr > str && isdigit(*(str - 1)))
749 serialno = (int)strtol(ptr, NULL, 0);
750 no = fdt_path_offset(fdtp, "/chosen");
754 prop = fdt_get_property(fdtp, no, "stdout", &len);
756 /* If /chosen/stdout does not extist, create it */
757 if (prop == NULL || (prop != NULL && len == 0)) {
759 bzero(tmp, 10 * sizeof(char));
760 strcpy((char *)&tmp, "serial");
762 /* Serial number too long */
765 strncpy((char *)tmp + 6, ptr, 3);
766 sero = fdt_path_offset(fdtp, (const char *)tmp);
769 * If serial device we're trying to assign
770 * stdout to doesn't exist in DT -- return.
774 fdt_setprop(fdtp, no, "stdout", &tmp,
775 strlen((char *)&tmp) + 1);
776 fdt_setprop(fdtp, no, "stdin", &tmp,
777 strlen((char *)&tmp) + 1);
782 * Locate the blob, fix it up and return its location.
791 debugf("fdt_fixup()\n");
793 if (fdtp == NULL && fdt_setup_fdtp() != 0)
796 /* Create /chosen node (if not exists) */
797 if ((chosen = fdt_subnode_offset(fdtp, 0, "chosen")) ==
799 chosen = fdt_add_subnode(fdtp, 0, "chosen");
801 /* Value assigned to fixup-applied does not matter. */
802 if (fdt_getprop(fdtp, chosen, "fixup-applied", NULL))
805 fdt_platform_fixups();
807 fdt_setprop(fdtp, chosen, "fixup-applied", NULL, 0);
812 * Copy DTB blob to specified location and return size
815 fdt_copy(vm_offset_t va)
818 debugf("fdt_copy va 0x%08x\n", va);
820 err = fdt_setup_fdtp();
822 printf("No valid device tree blob found!\n");
827 if (fdt_fixup() == 0)
831 /* Overwrite the FDT with the fixed version. */
832 /* XXX Is this really appropriate? */
833 COPYIN(fdtp, fdtp_va, fdtp_size);
835 COPYIN(fdtp, va, fdtp_size);
842 command_fdt_internal(int argc, char *argv[])
850 command_errmsg = "usage is 'fdt <command> [<args>]";
855 * Validate fdt <command>.
857 cmd = strdup(argv[1]);
860 while (!(commands[i].name == NULL)) {
861 if (strcmp(cmd, commands[i].name) == 0) {
863 cmdh = commands[i].handler;
864 flags = commands[i].flags;
870 command_errmsg = "unknown command";
874 if (flags & CMD_REQUIRES_BLOB) {
876 * Check if uboot env vars were parsed already. If not, do it now.
878 if (fdt_fixup() == 0)
883 * Call command handler.
885 err = (*cmdh)(argc, argv);
891 fdt_cmd_addr(int argc, char *argv[])
893 struct preloaded_file *fp;
894 struct fdt_header *hdr;
903 sprintf(command_errbuf, "no address specified");
907 hdr = (struct fdt_header *)strtoul(addr, &cp, 16);
909 sprintf(command_errbuf, "Invalid address: %s", addr);
913 while ((fp = file_findfile(NULL, "dtb")) != NULL) {
922 fdt_cmd_cd(int argc, char *argv[])
925 char tmp[FDT_CWD_LEN];
928 path = (argc > 2) ? argv[2] : "/";
930 if (path[0] == '/') {
932 if (len >= FDT_CWD_LEN)
935 /* Handle path specification relative to cwd */
936 len = strlen(cwd) + strlen(path) + 1;
937 if (len >= FDT_CWD_LEN)
946 o = fdt_path_offset(fdtp, path);
948 sprintf(command_errbuf, "could not find node: '%s'", path);
956 sprintf(command_errbuf, "path too long: %d, max allowed: %d",
957 len, FDT_CWD_LEN - 1);
962 fdt_cmd_hdr(int argc __unused, char *argv[] __unused)
968 command_errmsg = "no device tree blob pointer?!";
972 ver = fdt_version(fdtp);
974 sprintf(line, "\nFlattened device tree header (%p):\n", fdtp);
975 if (pager_output(line))
977 sprintf(line, " magic = 0x%08x\n", fdt_magic(fdtp));
978 if (pager_output(line))
980 sprintf(line, " size = %d\n", fdt_totalsize(fdtp));
981 if (pager_output(line))
983 sprintf(line, " off_dt_struct = 0x%08x\n",
984 fdt_off_dt_struct(fdtp));
985 if (pager_output(line))
987 sprintf(line, " off_dt_strings = 0x%08x\n",
988 fdt_off_dt_strings(fdtp));
989 if (pager_output(line))
991 sprintf(line, " off_mem_rsvmap = 0x%08x\n",
992 fdt_off_mem_rsvmap(fdtp));
993 if (pager_output(line))
995 sprintf(line, " version = %d\n", ver);
996 if (pager_output(line))
998 sprintf(line, " last compatible version = %d\n",
999 fdt_last_comp_version(fdtp));
1000 if (pager_output(line))
1003 sprintf(line, " boot_cpuid = %d\n",
1004 fdt_boot_cpuid_phys(fdtp));
1005 if (pager_output(line))
1009 sprintf(line, " size_dt_strings = %d\n",
1010 fdt_size_dt_strings(fdtp));
1011 if (pager_output(line))
1015 sprintf(line, " size_dt_struct = %d\n",
1016 fdt_size_dt_struct(fdtp));
1017 if (pager_output(line))
1027 fdt_cmd_ls(int argc, char *argv[])
1029 const char *prevname[FDT_MAX_DEPTH] = { NULL };
1032 int i, o, depth, len;
1034 path = (argc > 2) ? argv[2] : NULL;
1038 o = fdt_path_offset(fdtp, path);
1040 sprintf(command_errbuf, "could not find node: '%s'", path);
1045 (o >= 0) && (depth >= 0);
1046 o = fdt_next_node(fdtp, o, &depth)) {
1048 name = fdt_get_name(fdtp, o, &len);
1050 if (depth > FDT_MAX_DEPTH) {
1051 printf("max depth exceeded: %d\n", depth);
1055 prevname[depth] = name;
1057 /* Skip root (i = 1) when printing devices */
1058 for (i = 1; i <= depth; i++) {
1059 if (prevname[i] == NULL)
1062 if (strcmp(cwd, "/") == 0)
1064 printf("%s", prevname[i]);
1076 return (c >= ' ' && c <= 0x7e);
1080 fdt_isprint(const void *data, int len, int *count)
1089 d = (const char *)data;
1090 if (d[len - 1] != '\0')
1095 for (i = 0; i < len; i++) {
1097 if (isprint(ch) || (ch == '\0' && i > 0)) {
1112 fdt_data_str(const void *data, int len, int count, char **buf)
1119 * Calculate the length for the string and allocate memory.
1121 * Note that 'len' already includes at least one terminator.
1126 * Each token had already a terminator buried in 'len', but we
1127 * only need one eventually, don't count space for these.
1129 buf_len -= count - 1;
1131 /* Each consecutive token requires a ", " separator. */
1132 buf_len += count * 2;
1135 /* Add some space for surrounding double quotes. */
1136 buf_len += count * 2;
1138 /* Note that string being put in 'tmp' may be as big as 'buf_len'. */
1139 b = (char *)malloc(buf_len);
1140 tmp = (char *)malloc(buf_len);
1152 * Now that we have space, format the string.
1156 d = (const char *)data + i;
1159 sprintf(tmp, "\"%s\"%s", d,
1160 (i + l) < len ? ", " : "");
1176 fdt_data_cell(const void *data, int len, char **buf)
1182 /* Number of cells */
1186 * Calculate the length for the string and allocate memory.
1189 /* Each byte translates to 2 output characters */
1192 /* Each consecutive cell requires a " " separator. */
1193 l += (count - 1) * 1;
1195 /* Each cell will have a "0x" prefix */
1197 /* Space for surrounding <> and terminator */
1200 b = (char *)malloc(l);
1201 tmp = (char *)malloc(l);
1213 for (i = 0; i < len; i += 4) {
1214 c = (const uint32_t *)((const uint8_t *)data + i);
1215 sprintf(tmp, "0x%08x%s", fdt32_to_cpu(*c),
1216 i < (len - 4) ? " " : "");
1230 fdt_data_bytes(const void *data, int len, char **buf)
1237 * Calculate the length for the string and allocate memory.
1240 /* Each byte translates to 2 output characters */
1243 /* Each consecutive byte requires a " " separator. */
1245 /* Each byte will have a "0x" prefix */
1247 /* Space for surrounding [] and terminator. */
1250 b = (char *)malloc(l);
1251 tmp = (char *)malloc(l);
1263 for (i = 0, d = data; i < len; i++) {
1264 sprintf(tmp, "0x%02x%s", d[i], i < len - 1 ? " " : "");
1278 fdt_data_fmt(const void *data, int len, char **buf)
1287 if (fdt_isprint(data, len, &count))
1288 return (fdt_data_str(data, len, count, buf));
1290 else if ((len % 4) == 0)
1291 return (fdt_data_cell(data, len, buf));
1294 return (fdt_data_bytes(data, len, buf));
1298 fdt_prop(int offset)
1301 const struct fdt_property *prop;
1307 prop = fdt_offset_ptr(fdtp, offset, sizeof(*prop));
1311 name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff));
1312 len = fdt32_to_cpu(prop->len);
1317 /* Property without value */
1318 line = (char *)malloc(strlen(name) + 2);
1323 sprintf(line, "%s\n", name);
1328 * Process property with value
1332 if (fdt_data_fmt(data, len, &buf) != 0) {
1337 line = (char *)malloc(strlen(name) + strlen(FDT_PROP_SEP) +
1340 sprintf(command_errbuf, "could not allocate space for string");
1345 sprintf(line, "%s" FDT_PROP_SEP "%s\n", name, buf);
1363 fdt_modprop(int nodeoff, char *propname, void *value, char mode)
1365 uint32_t cells[100];
1368 const struct fdt_property *p;
1370 p = fdt_get_property(fdtp, nodeoff, propname, NULL);
1374 /* Adding inexistant value in mode 1 is forbidden */
1375 sprintf(command_errbuf, "property already exists!");
1378 } else if (mode == 0) {
1379 sprintf(command_errbuf, "property does not exist!");
1382 len = strlen(value);
1392 len = fdt_strtovect(buf, (void *)&cells, 100,
1395 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1396 len * sizeof(uint32_t));
1400 len = fdt_strtovect(buf, (void *)&cells, 100,
1403 rv = fdt_setprop(fdtp, nodeoff, propname, &cells,
1404 len * sizeof(uint8_t));
1408 /* Default -- string */
1409 rv = fdt_setprop_string(fdtp, nodeoff, propname, value);
1414 if (rv == -FDT_ERR_NOSPACE)
1415 sprintf(command_errbuf,
1416 "Device tree blob is too small!\n");
1418 sprintf(command_errbuf,
1419 "Could not add/modify property!\n");
1424 /* Merge strings from argv into a single string */
1426 fdt_merge_strings(int argc, char *argv[], int start, char **buffer)
1434 for (i = start; i < argc; i++)
1435 sz += strlen(argv[i]);
1437 /* Additional bytes for whitespaces between args */
1440 buf = (char *)malloc(sizeof(char) * sz);
1442 sprintf(command_errbuf, "could not allocate space "
1446 bzero(buf, sizeof(char) * sz);
1449 for (i = start, idx = 0; i < argc; i++) {
1450 strcpy(buf + idx, argv[i]);
1451 idx += strlen(argv[i]);
1460 /* Extract offset and name of node/property from a given path */
1462 fdt_extract_nameloc(char **pathp, char **namep, int *nodeoff)
1465 char *path = *pathp, *name = NULL, *subpath = NULL;
1467 subpath = strrchr(path, '/');
1468 if (subpath == NULL) {
1469 o = fdt_path_offset(fdtp, cwd);
1471 path = (char *)&cwd;
1474 if (strlen(path) == 0)
1478 o = fdt_path_offset(fdtp, path);
1481 if (strlen(name) == 0) {
1482 sprintf(command_errbuf, "name not specified");
1486 sprintf(command_errbuf, "could not find node: '%s'", path);
1496 fdt_cmd_prop(int argc, char *argv[])
1498 char *path, *propname, *value;
1499 int o, next, depth, rv;
1502 path = (argc > 2) ? argv[2] : NULL;
1507 /* Merge property value strings into one */
1508 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1519 /* If value is specified -- try to modify prop. */
1520 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1523 rv = fdt_modprop(o, propname, value, 0);
1529 /* User wants to display properties */
1530 o = fdt_path_offset(fdtp, path);
1533 sprintf(command_errbuf, "could not find node: '%s'", path);
1539 while (depth >= 0) {
1540 tag = fdt_next_tag(fdtp, o, &next);
1546 /* Don't process properties of nested nodes */
1549 if (fdt_prop(o) != 0) {
1550 sprintf(command_errbuf, "could not process "
1556 case FDT_BEGIN_NODE:
1558 if (depth > FDT_MAX_DEPTH) {
1559 printf("warning: nesting too deep: %d\n",
1568 * This is the end of our starting node, force
1581 fdt_cmd_mkprop(int argc, char *argv[])
1584 char *path, *propname, *value;
1586 path = (argc > 2) ? argv[2] : NULL;
1591 /* Merge property value strings into one */
1592 if (fdt_merge_strings(argc, argv, 3, &value) != 0)
1597 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1600 if (fdt_modprop(o, propname, value, 1))
1607 fdt_cmd_rm(int argc, char *argv[])
1610 char *path = NULL, *propname;
1615 sprintf(command_errbuf, "no node/property name specified");
1619 o = fdt_path_offset(fdtp, path);
1621 /* If node not found -- try to find & delete property */
1622 if (fdt_extract_nameloc(&path, &propname, &o) != 0)
1625 if ((rv = fdt_delprop(fdtp, o, propname)) != 0) {
1626 sprintf(command_errbuf, "could not delete"
1627 "%s\n", (rv == -FDT_ERR_NOTFOUND) ?
1628 "(property/node does not exist)" : "");
1634 /* If node exists -- remove node */
1635 rv = fdt_del_node(fdtp, o);
1637 sprintf(command_errbuf, "could not delete node");
1644 fdt_cmd_mknode(int argc, char *argv[])
1647 char *path = NULL, *nodename = NULL;
1652 sprintf(command_errbuf, "no node name specified");
1656 if (fdt_extract_nameloc(&path, &nodename, &o) != 0)
1659 rv = fdt_add_subnode(fdtp, o, nodename);
1662 if (rv == -FDT_ERR_NOSPACE)
1663 sprintf(command_errbuf,
1664 "Device tree blob is too small!\n");
1666 sprintf(command_errbuf,
1667 "Could not add node!\n");
1674 fdt_cmd_pwd(int argc, char *argv[])
1676 char line[FDT_CWD_LEN];
1679 sprintf(line, "%s\n", cwd);
1686 fdt_cmd_mres(int argc, char *argv[])
1688 uint64_t start, size;
1693 total = fdt_num_mem_rsv(fdtp);
1695 if (pager_output("Reserved memory regions:\n"))
1697 for (i = 0; i < total; i++) {
1698 fdt_get_mem_rsv(fdtp, i, &start, &size);
1699 sprintf(line, "reg#%d: (start: 0x%jx, size: 0x%jx)\n",
1701 if (pager_output(line))
1705 pager_output("No reserved memory regions\n");
1713 fdt_cmd_nyi(int argc, char *argv[])
1716 printf("command not yet implemented\n");