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"
36 add_node_to_fdt(void *buffer, const char *path, int fdt_offset)
38 int child_offset, fd, pfd, error, dentsize;
44 unsigned long d_fileno;
46 unsigned short d_reclen;
51 struct host_dent *dent;
54 fd = host_open(path, O_RDONLY, 0);
56 dentsize = host_getdents(fd, dents, sizeof(dents));
59 for (dent = (struct host_dent *)dents;
60 (char *)dent < dents + dentsize;
61 dent = (struct host_dent *)((void *)dent + dent->d_reclen)) {
62 sprintf(subpath, "%s/%s", path, dent->d_name);
63 if (strcmp(dent->d_name, ".") == 0 ||
64 strcmp(dent->d_name, "..") == 0)
66 d_type = *((char *)(dent) + dent->d_reclen - 1);
67 if (d_type == 4 /* DT_DIR */) {
68 child_offset = fdt_add_subnode(buffer, fdt_offset,
70 if (child_offset < 0) {
71 printf("Error %d adding node %s/%s, skipping\n",
72 child_offset, path, dent->d_name);
76 add_node_to_fdt(buffer, subpath, child_offset);
78 propbuf = malloc(1024);
80 pfd = host_open(subpath, O_RDONLY, 0);
82 proplen = host_read(pfd, propbuf, 1024);
85 error = fdt_setprop(buffer, fdt_offset, dent->d_name,
89 printf("Error %d adding property %s to "
90 "node %d\n", error, dent->d_name,
99 /* Fix up wrong values added to the device tree by prom_init() in Linux */
102 fdt_linux_fixups(void *fdtp)
108 * Remove /memory/available properties, which reflect long-gone OF
112 offset = fdt_path_offset(fdtp, "/memory@0");
114 fdt_delprop(fdtp, offset, "available");
117 * Add reservations for OPAL and RTAS state if present
120 offset = fdt_path_offset(fdtp, "/ibm,opal");
122 const uint64_t *base, *size;
123 base = fdt_getprop(fdtp, offset, "opal-base-address",
125 size = fdt_getprop(fdtp, offset, "opal-runtime-size",
127 if (base != NULL && size != NULL)
128 fdt_add_mem_rsv(fdtp, fdt64_to_cpu(*base),
129 fdt64_to_cpu(*size));
131 offset = fdt_path_offset(fdtp, "/rtas");
133 const uint32_t *base, *size;
134 base = fdt_getprop(fdtp, offset, "linux,rtas-base", &len);
135 size = fdt_getprop(fdtp, offset, "rtas-size", &len);
136 if (base != NULL && size != NULL)
137 fdt_add_mem_rsv(fdtp, fdt32_to_cpu(*base),
138 fdt32_to_cpu(*size));
142 * Patch up /chosen nodes so that the stored handles mean something,
145 offset = fdt_path_offset(fdtp, "/chosen");
147 fdt_delprop(fdtp, offset, "cpu"); /* This node not meaningful */
149 offset = fdt_path_offset(fdtp, "/chosen");
150 prop = fdt_getprop(fdtp, offset, "linux,stdout-package", &len);
152 fdt_setprop(fdtp, offset, "stdout", prop, len);
153 offset = fdt_path_offset(fdtp, "/chosen");
154 fdt_setprop(fdtp, offset, "stdin", prop, len);
160 fdt_platform_load_dtb(void)
163 size_t buflen = 409600;
165 buffer = malloc(buflen);
166 fdt_create_empty_tree(buffer, buflen);
167 add_node_to_fdt(buffer, "/proc/device-tree",
168 fdt_path_offset(buffer, "/"));
169 fdt_linux_fixups(buffer);
173 fdt_load_dtb_addr(buffer);
180 fdt_platform_load_overlays(void)
186 fdt_platform_fixups(void)