exec.c revision 1.14 1 1.14 riastrad /* $NetBSD: exec.c,v 1.14 2020/05/14 19:20:08 riastradh Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.10 thorpej * Copyright (c) 2019 Jason R. Thorpe
5 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
6 1.1 jmcneill * All rights reserved.
7 1.1 jmcneill *
8 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
9 1.1 jmcneill * modification, are permitted provided that the following conditions
10 1.1 jmcneill * are met:
11 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
12 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
13 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
15 1.1 jmcneill * documentation and/or other materials provided with the distribution.
16 1.1 jmcneill *
17 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 1.1 jmcneill * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 1.1 jmcneill * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 1.1 jmcneill * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 1.1 jmcneill * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 1.1 jmcneill * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 1.1 jmcneill * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 1.1 jmcneill * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 1.1 jmcneill * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 jmcneill * SUCH DAMAGE.
28 1.1 jmcneill */
29 1.1 jmcneill
30 1.1 jmcneill #include "efiboot.h"
31 1.10 thorpej #include "efienv.h"
32 1.1 jmcneill #include "efifdt.h"
33 1.7 jmcneill #include "efiacpi.h"
34 1.14 riastrad #include "efirng.h"
35 1.1 jmcneill
36 1.6 jmcneill #include <sys/reboot.h>
37 1.1 jmcneill
38 1.13 jmcneill extern char twiddle_toggle;
39 1.13 jmcneill
40 1.3 jmcneill u_long load_offset = 0;
41 1.3 jmcneill
42 1.4 jmcneill #define FDT_SPACE (4 * 1024 * 1024)
43 1.4 jmcneill #define FDT_ALIGN ((2 * 1024 * 1024) - 1)
44 1.4 jmcneill
45 1.14 riastrad static EFI_PHYSICAL_ADDRESS initrd_addr, dtb_addr, rndseed_addr, efirng_addr;
46 1.14 riastrad static u_long initrd_size = 0, dtb_size = 0, rndseed_size = 0, efirng_size = 0;
47 1.4 jmcneill
48 1.4 jmcneill static int
49 1.10 thorpej load_file(const char *path, u_long extra, bool quiet_errors,
50 1.10 thorpej EFI_PHYSICAL_ADDRESS *paddr, u_long *psize)
51 1.4 jmcneill {
52 1.4 jmcneill EFI_STATUS status;
53 1.4 jmcneill struct stat st;
54 1.4 jmcneill ssize_t len;
55 1.10 thorpej ssize_t expectedlen;
56 1.4 jmcneill int fd;
57 1.4 jmcneill
58 1.4 jmcneill if (strlen(path) == 0)
59 1.4 jmcneill return 0;
60 1.4 jmcneill
61 1.4 jmcneill fd = open(path, 0);
62 1.4 jmcneill if (fd < 0) {
63 1.10 thorpej if (!quiet_errors) {
64 1.10 thorpej printf("boot: failed to open %s: %s\n", path,
65 1.10 thorpej strerror(errno));
66 1.10 thorpej }
67 1.4 jmcneill return errno;
68 1.4 jmcneill }
69 1.4 jmcneill if (fstat(fd, &st) < 0) {
70 1.4 jmcneill printf("boot: failed to fstat %s: %s\n", path, strerror(errno));
71 1.4 jmcneill close(fd);
72 1.4 jmcneill return errno;
73 1.4 jmcneill }
74 1.4 jmcneill if (st.st_size == 0) {
75 1.10 thorpej if (!quiet_errors) {
76 1.10 thorpej printf("boot: empty file %s\n", path);
77 1.10 thorpej }
78 1.4 jmcneill close(fd);
79 1.4 jmcneill return EINVAL;
80 1.4 jmcneill }
81 1.4 jmcneill
82 1.10 thorpej expectedlen = st.st_size;
83 1.10 thorpej *psize = st.st_size + extra;
84 1.4 jmcneill
85 1.4 jmcneill #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
86 1.5 jmcneill *paddr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
87 1.4 jmcneill status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
88 1.5 jmcneill EFI_SIZE_TO_PAGES(*psize), paddr);
89 1.4 jmcneill #else
90 1.5 jmcneill *paddr = 0;
91 1.4 jmcneill status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
92 1.5 jmcneill EFI_SIZE_TO_PAGES(*psize), paddr);
93 1.4 jmcneill #endif
94 1.4 jmcneill if (EFI_ERROR(status)) {
95 1.5 jmcneill printf("Failed to allocate %lu bytes for %s (error %lu)\n",
96 1.9 jmcneill *psize, path, (u_long)status);
97 1.4 jmcneill close(fd);
98 1.10 thorpej *paddr = 0;
99 1.4 jmcneill return ENOMEM;
100 1.4 jmcneill }
101 1.4 jmcneill
102 1.4 jmcneill printf("boot: loading %s ", path);
103 1.10 thorpej len = read(fd, (void *)(uintptr_t)*paddr, expectedlen);
104 1.4 jmcneill close(fd);
105 1.4 jmcneill
106 1.10 thorpej if (len != expectedlen) {
107 1.10 thorpej if (len < 0) {
108 1.4 jmcneill printf(": %s\n", strerror(errno));
109 1.10 thorpej } else {
110 1.10 thorpej printf(": returned %ld (expected %ld)\n", len,
111 1.10 thorpej expectedlen);
112 1.10 thorpej }
113 1.4 jmcneill return EIO;
114 1.4 jmcneill }
115 1.4 jmcneill
116 1.4 jmcneill printf("done.\n");
117 1.4 jmcneill
118 1.5 jmcneill efi_dcache_flush(*paddr, *psize);
119 1.4 jmcneill
120 1.4 jmcneill return 0;
121 1.4 jmcneill }
122 1.4 jmcneill
123 1.10 thorpej static const char default_efibootplist_path[] = "/etc/efiboot.plist";
124 1.10 thorpej
125 1.10 thorpej /* This is here because load_file() is here. */
126 1.10 thorpej void
127 1.10 thorpej load_efibootplist(bool default_fallback)
128 1.10 thorpej {
129 1.10 thorpej EFI_PHYSICAL_ADDRESS plist_addr = 0;
130 1.10 thorpej u_long plist_size = 0;
131 1.10 thorpej prop_dictionary_t plist = NULL, oplist = NULL;
132 1.10 thorpej bool load_quietly = false;
133 1.13 jmcneill bool old_twiddle_toggle = twiddle_toggle;
134 1.10 thorpej
135 1.10 thorpej const char *path = get_efibootplist_path();
136 1.10 thorpej if (path == NULL || strlen(path) == 0) {
137 1.10 thorpej if (!default_fallback)
138 1.10 thorpej return;
139 1.10 thorpej path = default_efibootplist_path;
140 1.10 thorpej load_quietly = true;
141 1.10 thorpej }
142 1.10 thorpej
143 1.13 jmcneill twiddle_toggle = load_quietly;
144 1.13 jmcneill
145 1.10 thorpej /*
146 1.10 thorpej * Fudge the size so we can ensure the resulting buffer
147 1.10 thorpej * is NUL-terminated for convenience.
148 1.10 thorpej */
149 1.10 thorpej if (load_file(path, 1, load_quietly, &plist_addr, &plist_size) != 0 ||
150 1.10 thorpej plist_addr == 0) {
151 1.10 thorpej /* Error messages have already been displayed. */
152 1.10 thorpej goto out;
153 1.10 thorpej }
154 1.10 thorpej char *plist_buf = (char *)((uintptr_t)plist_addr);
155 1.10 thorpej plist_buf[plist_size - 1] = '\0';
156 1.10 thorpej
157 1.10 thorpej plist = prop_dictionary_internalize(plist_buf);
158 1.10 thorpej if (plist == NULL) {
159 1.10 thorpej printf("boot: unable to parse plist '%s'\n", path);
160 1.10 thorpej goto out;
161 1.10 thorpej }
162 1.10 thorpej
163 1.10 thorpej out:
164 1.10 thorpej oplist = efibootplist;
165 1.10 thorpej
166 1.13 jmcneill twiddle_toggle = old_twiddle_toggle;
167 1.13 jmcneill
168 1.10 thorpej /*
169 1.10 thorpej * If we had a failure, create an empty one for
170 1.10 thorpej * convenience. But a failure should not clobber
171 1.10 thorpej * an in-memory plist we already have.
172 1.10 thorpej */
173 1.10 thorpej if (plist == NULL &&
174 1.10 thorpej (oplist == NULL || prop_dictionary_count(oplist) == 0))
175 1.10 thorpej plist = prop_dictionary_create();
176 1.10 thorpej
177 1.10 thorpej #ifdef EFIBOOT_DEBUG
178 1.10 thorpej printf(">> load_efibootplist: oplist = 0x%lx, plist = 0x%lx\n",
179 1.10 thorpej (u_long)oplist, (u_long)plist);
180 1.10 thorpej #endif
181 1.10 thorpej
182 1.10 thorpej if (plist_addr) {
183 1.10 thorpej uefi_call_wrapper(BS->FreePages, 2, plist_addr,
184 1.10 thorpej EFI_SIZE_TO_PAGES(plist_size));
185 1.10 thorpej }
186 1.10 thorpej
187 1.10 thorpej if (plist) {
188 1.10 thorpej efibootplist = plist;
189 1.10 thorpej efi_env_from_efibootplist();
190 1.10 thorpej
191 1.10 thorpej if (oplist)
192 1.10 thorpej prop_object_release(oplist);
193 1.10 thorpej }
194 1.10 thorpej }
195 1.10 thorpej
196 1.10 thorpej static void
197 1.10 thorpej apply_overlay(void *dtbo)
198 1.10 thorpej {
199 1.10 thorpej
200 1.10 thorpej if (!efi_fdt_overlay_is_compatible(dtbo)) {
201 1.10 thorpej printf("boot: incompatible overlay\n");
202 1.10 thorpej }
203 1.10 thorpej
204 1.10 thorpej int fdterr;
205 1.10 thorpej
206 1.10 thorpej if (efi_fdt_overlay_apply(dtbo, &fdterr) != 0) {
207 1.10 thorpej printf("boot: error %d applying overlay\n", fdterr);
208 1.10 thorpej }
209 1.10 thorpej }
210 1.10 thorpej
211 1.10 thorpej static void
212 1.10 thorpej apply_overlay_file(const char *path)
213 1.10 thorpej {
214 1.10 thorpej EFI_PHYSICAL_ADDRESS dtbo_addr;
215 1.10 thorpej u_long dtbo_size;
216 1.10 thorpej
217 1.10 thorpej if (strlen(path) == 0)
218 1.10 thorpej return;
219 1.10 thorpej
220 1.10 thorpej if (load_file(path, 0, false, &dtbo_addr, &dtbo_size) != 0 ||
221 1.10 thorpej dtbo_addr == 0) {
222 1.10 thorpej /* Error messages have already been displayed. */
223 1.10 thorpej goto out;
224 1.10 thorpej }
225 1.10 thorpej
226 1.10 thorpej apply_overlay((void *)(uintptr_t)dtbo_addr);
227 1.10 thorpej
228 1.10 thorpej out:
229 1.10 thorpej if (dtbo_addr) {
230 1.10 thorpej uefi_call_wrapper(BS->FreePages, 2, dtbo_addr,
231 1.10 thorpej EFI_SIZE_TO_PAGES(dtbo_size));
232 1.10 thorpej }
233 1.10 thorpej }
234 1.10 thorpej
235 1.10 thorpej #define DT_OVERLAYS_PROP "device-tree-overlays"
236 1.10 thorpej
237 1.10 thorpej static void
238 1.10 thorpej load_fdt_overlays(void)
239 1.10 thorpej {
240 1.10 thorpej /*
241 1.10 thorpej * We support loading device tree overlays specified in efiboot.plist
242 1.10 thorpej * using the following schema:
243 1.10 thorpej *
244 1.10 thorpej * <key>device-tree-overlays</key>
245 1.10 thorpej * <array>
246 1.10 thorpej * <string>/path/to/some/overlay.dtbo</string>
247 1.10 thorpej * <string>hd0e:/some/other/overlay.dtbo</string>
248 1.10 thorpej * </array>
249 1.10 thorpej *
250 1.10 thorpej * The overlays are loaded in array order.
251 1.10 thorpej */
252 1.10 thorpej prop_array_t overlays = prop_dictionary_get(efibootplist,
253 1.10 thorpej DT_OVERLAYS_PROP);
254 1.10 thorpej if (overlays == NULL) {
255 1.10 thorpej #ifdef EFIBOOT_DEBUG
256 1.10 thorpej printf("boot: no device-tree-overlays\n");
257 1.10 thorpej #endif
258 1.10 thorpej return;
259 1.10 thorpej }
260 1.10 thorpej if (prop_object_type(overlays) != PROP_TYPE_ARRAY) {
261 1.10 thorpej printf("boot: invalid %s\n", DT_OVERLAYS_PROP);
262 1.10 thorpej return;
263 1.10 thorpej }
264 1.10 thorpej
265 1.10 thorpej prop_object_iterator_t iter = prop_array_iterator(overlays);
266 1.10 thorpej prop_string_t pathobj;
267 1.10 thorpej while ((pathobj = prop_object_iterator_next(iter)) != NULL) {
268 1.10 thorpej if (prop_object_type(pathobj) != PROP_TYPE_STRING) {
269 1.10 thorpej printf("boot: invalid %s entry\n", DT_OVERLAYS_PROP);
270 1.10 thorpej continue;
271 1.10 thorpej }
272 1.10 thorpej apply_overlay_file(prop_string_cstring_nocopy(pathobj));
273 1.10 thorpej }
274 1.10 thorpej prop_object_iterator_release(iter);
275 1.10 thorpej }
276 1.10 thorpej
277 1.14 riastrad static void
278 1.14 riastrad generate_efirng(void)
279 1.14 riastrad {
280 1.14 riastrad EFI_PHYSICAL_ADDRESS addr;
281 1.14 riastrad u_long size = EFI_PAGE_SIZE;
282 1.14 riastrad EFI_STATUS status;
283 1.14 riastrad
284 1.14 riastrad /* Check whether the RNG is available before bothering. */
285 1.14 riastrad if (!efi_rng_available())
286 1.14 riastrad return;
287 1.14 riastrad
288 1.14 riastrad /*
289 1.14 riastrad * Allocate a page. This is the smallest unit we can pass into
290 1.14 riastrad * the kernel conveniently.
291 1.14 riastrad */
292 1.14 riastrad #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
293 1.14 riastrad addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
294 1.14 riastrad status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress,
295 1.14 riastrad EfiLoaderData, EFI_SIZE_TO_PAGES(size), &addr);
296 1.14 riastrad #else
297 1.14 riastrad addr = 0;
298 1.14 riastrad status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages,
299 1.14 riastrad EfiLoaderData, EFI_SIZE_TO_PAGES(size), &addr);
300 1.14 riastrad #endif
301 1.14 riastrad if (EFI_ERROR(status)) {
302 1.14 riastrad Print(L"Failed to allocate page for EFI RNG output: %r\n",
303 1.14 riastrad status);
304 1.14 riastrad return;
305 1.14 riastrad }
306 1.14 riastrad
307 1.14 riastrad /* Fill the page with whatever the EFI RNG will do. */
308 1.14 riastrad if (efi_rng((void *)(uintptr_t)addr, size)) {
309 1.14 riastrad uefi_call_wrapper(BS->FreePages, 2, addr, size);
310 1.14 riastrad return;
311 1.14 riastrad }
312 1.14 riastrad
313 1.14 riastrad /* Success! */
314 1.14 riastrad efirng_addr = addr;
315 1.14 riastrad efirng_size = size;
316 1.14 riastrad }
317 1.14 riastrad
318 1.1 jmcneill int
319 1.1 jmcneill exec_netbsd(const char *fname, const char *args)
320 1.1 jmcneill {
321 1.1 jmcneill EFI_PHYSICAL_ADDRESS addr;
322 1.1 jmcneill u_long marks[MARK_MAX], alloc_size;
323 1.1 jmcneill EFI_STATUS status;
324 1.6 jmcneill int fd, ohowto;
325 1.1 jmcneill
326 1.10 thorpej load_file(get_initrd_path(), 0, false, &initrd_addr, &initrd_size);
327 1.10 thorpej load_file(get_dtb_path(), 0, false, &dtb_addr, &dtb_size);
328 1.14 riastrad generate_efirng();
329 1.4 jmcneill
330 1.1 jmcneill memset(marks, 0, sizeof(marks));
331 1.6 jmcneill ohowto = howto;
332 1.6 jmcneill howto |= AB_SILENT;
333 1.1 jmcneill fd = loadfile(fname, marks, COUNT_KERNEL | LOAD_NOTE);
334 1.6 jmcneill howto = ohowto;
335 1.1 jmcneill if (fd < 0) {
336 1.1 jmcneill printf("boot: %s: %s\n", fname, strerror(errno));
337 1.1 jmcneill return EIO;
338 1.1 jmcneill }
339 1.1 jmcneill close(fd);
340 1.1 jmcneill marks[MARK_END] = (((u_long) marks[MARK_END] + sizeof(int) - 1)) & (-sizeof(int));
341 1.4 jmcneill alloc_size = marks[MARK_END] - marks[MARK_START] + FDT_SPACE + EFIBOOT_ALIGN;
342 1.1 jmcneill
343 1.1 jmcneill #ifdef EFIBOOT_ALLOCATE_MAX_ADDRESS
344 1.1 jmcneill addr = EFIBOOT_ALLOCATE_MAX_ADDRESS;
345 1.1 jmcneill status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateMaxAddress, EfiLoaderData,
346 1.1 jmcneill EFI_SIZE_TO_PAGES(alloc_size), &addr);
347 1.1 jmcneill #else
348 1.1 jmcneill addr = 0;
349 1.1 jmcneill status = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, EfiLoaderData,
350 1.1 jmcneill EFI_SIZE_TO_PAGES(alloc_size), &addr);
351 1.1 jmcneill #endif
352 1.1 jmcneill if (EFI_ERROR(status)) {
353 1.1 jmcneill printf("Failed to allocate %lu bytes for kernel image (error %lu)\n",
354 1.9 jmcneill alloc_size, (u_long)status);
355 1.1 jmcneill return ENOMEM;
356 1.1 jmcneill }
357 1.1 jmcneill
358 1.1 jmcneill memset(marks, 0, sizeof(marks));
359 1.3 jmcneill load_offset = (addr + EFIBOOT_ALIGN) & ~(EFIBOOT_ALIGN - 1);
360 1.1 jmcneill fd = loadfile(fname, marks, LOAD_KERNEL);
361 1.1 jmcneill if (fd < 0) {
362 1.1 jmcneill printf("boot: %s: %s\n", fname, strerror(errno));
363 1.1 jmcneill goto cleanup;
364 1.1 jmcneill }
365 1.1 jmcneill close(fd);
366 1.3 jmcneill load_offset = 0;
367 1.1 jmcneill
368 1.7 jmcneill #ifdef EFIBOOT_ACPI
369 1.7 jmcneill if (efi_acpi_available()) {
370 1.7 jmcneill efi_acpi_create_fdt();
371 1.7 jmcneill } else
372 1.7 jmcneill #endif
373 1.9 jmcneill if (dtb_addr && efi_fdt_set_data((void *)(uintptr_t)dtb_addr) != 0) {
374 1.5 jmcneill printf("boot: invalid DTB data\n");
375 1.5 jmcneill goto cleanup;
376 1.5 jmcneill }
377 1.5 jmcneill
378 1.1 jmcneill if (efi_fdt_size() > 0) {
379 1.12 riastrad /*
380 1.12 riastrad * Load the rndseed as late as possible -- after we
381 1.12 riastrad * have committed to using fdt and executing this
382 1.12 riastrad * kernel -- so that it doesn't hang around in memory
383 1.12 riastrad * if we have to bail or the kernel won't use it.
384 1.12 riastrad */
385 1.12 riastrad load_file(get_rndseed_path(), 0, false,
386 1.12 riastrad &rndseed_addr, &rndseed_size);
387 1.12 riastrad
388 1.4 jmcneill efi_fdt_init((marks[MARK_END] + FDT_ALIGN) & ~FDT_ALIGN, FDT_ALIGN + 1);
389 1.10 thorpej load_fdt_overlays();
390 1.4 jmcneill efi_fdt_initrd(initrd_addr, initrd_size);
391 1.12 riastrad efi_fdt_rndseed(rndseed_addr, rndseed_size);
392 1.14 riastrad efi_fdt_efirng(efirng_addr, efirng_size);
393 1.2 jmcneill efi_fdt_bootargs(args);
394 1.11 jmcneill #ifdef EFIBOOT_ACPI
395 1.11 jmcneill if (efi_acpi_available())
396 1.11 jmcneill efi_fdt_gop();
397 1.11 jmcneill #endif
398 1.4 jmcneill efi_fdt_memory_map();
399 1.8 jmcneill }
400 1.8 jmcneill
401 1.8 jmcneill efi_cleanup();
402 1.8 jmcneill
403 1.8 jmcneill if (efi_fdt_size() > 0) {
404 1.4 jmcneill efi_fdt_fini();
405 1.1 jmcneill }
406 1.1 jmcneill
407 1.1 jmcneill efi_boot_kernel(marks);
408 1.1 jmcneill
409 1.1 jmcneill /* This should not happen.. */
410 1.1 jmcneill printf("boot returned\n");
411 1.1 jmcneill
412 1.1 jmcneill cleanup:
413 1.1 jmcneill uefi_call_wrapper(BS->FreePages, 2, addr, EFI_SIZE_TO_PAGES(alloc_size));
414 1.4 jmcneill if (initrd_addr) {
415 1.4 jmcneill uefi_call_wrapper(BS->FreePages, 2, initrd_addr, EFI_SIZE_TO_PAGES(initrd_size));
416 1.4 jmcneill initrd_addr = 0;
417 1.4 jmcneill initrd_size = 0;
418 1.4 jmcneill }
419 1.5 jmcneill if (dtb_addr) {
420 1.5 jmcneill uefi_call_wrapper(BS->FreePages, 2, dtb_addr, EFI_SIZE_TO_PAGES(dtb_size));
421 1.5 jmcneill dtb_addr = 0;
422 1.5 jmcneill dtb_size = 0;
423 1.5 jmcneill }
424 1.1 jmcneill return EIO;
425 1.1 jmcneill }
426