2 * Copyright (C) 2014 Nathan Whitehorn
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <fdt_platform.h>
32 #include "bootstrap.h"
33 #include "host_syscall.h"
37 add_node_to_fdt(void *buffer, const char *path, int fdt_offset)
39 int child_offset, fd, pfd, error, dentsize;
45 struct host_dirent64 *dent;
48 fd = host_open(path, O_RDONLY, 0);
50 dentsize = host_getdents64(fd, dents, sizeof(dents));
53 for (dent = (struct host_dirent64 *)dents;
54 (char *)dent < dents + dentsize;
55 dent = (struct host_dirent64 *)((void *)dent + dent->d_reclen)) {
56 sprintf(subpath, "%s/%s", path, dent->d_name);
57 if (strcmp(dent->d_name, ".") == 0 ||
58 strcmp(dent->d_name, "..") == 0)
60 d_type = dent->d_type;
61 if (d_type == HOST_DT_DIR) {
62 child_offset = fdt_add_subnode(buffer, fdt_offset,
64 if (child_offset < 0) {
65 printf("Error %d adding node %s/%s, skipping\n",
66 child_offset, path, dent->d_name);
69 add_node_to_fdt(buffer, subpath, child_offset);
71 propbuf = malloc(1024);
73 pfd = host_open(subpath, O_RDONLY, 0);
75 proplen = host_read(pfd, propbuf, 1024);
78 error = fdt_setprop(buffer, fdt_offset, dent->d_name,
82 printf("Error %d adding property %s to "
83 "node %d\n", error, dent->d_name,
93 fdt_platform_load_dtb(void)
96 size_t buflen = 409600;
100 * Should load /sys/firmware/fdt if it exists, otherwise we walk the
101 * tree from /proc/device-tree. The former is much easier than the
102 * latter and also the newer interface. But as long as we support the
103 * PS3 boot, we'll need the latter due to that kernel's age. It likely
104 * would be better to script the decision between the two, but that
105 * turns out to be tricky...
107 buffer = malloc(buflen);
108 fd = host_open("/sys/firmware/fdt", O_RDONLY, 0);
110 buflen = host_read(fd, buffer, buflen);
113 fdt_create_empty_tree(buffer, buflen);
114 add_node_to_fdt(buffer, "/proc/device-tree",
115 fdt_path_offset(buffer, "/"));
117 fdt_arch_fixups(buffer);
121 fdt_load_dtb_addr(buffer);
128 fdt_platform_load_overlays(void)
130 fdt_load_dtb_overlays(NULL);
134 fdt_platform_fixups(void)
136 fdt_apply_overlays();