2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2004, 2006 Marcel Moolenaar
4 * Copyright (c) 2014 The FreeBSD Foundation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/linker.h>
36 #include <sys/reboot.h>
38 #include <machine/cpufunc.h>
39 #include <machine/elf.h>
40 #include <machine/metadata.h>
41 #include <machine/psl.h>
46 #include "bootstrap.h"
47 #include "loader_efi.h"
49 #if defined(__amd64__)
50 #include <machine/specialreg.h>
53 #include "framebuffer.h"
55 #if defined(LOADER_FDT_SUPPORT)
56 #include <fdt_platform.h>
59 #ifdef LOADER_GELI_SUPPORT
63 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
65 extern EFI_SYSTEM_TABLE *ST;
68 bi_getboothowto(char *kargs)
73 int howto, speed, port;
76 howto = boot_parse_cmdline(kargs);
77 howto |= boot_env_to_howto();
79 console = getenv("console");
80 if (console != NULL) {
81 if (strcmp(console, "comconsole") == 0)
83 if (strcmp(console, "nullconsole") == 0)
85 #if defined(__i386__) || defined(__amd64__)
86 if (strcmp(console, "efi") == 0 &&
87 getenv("efi_8250_uid") != NULL &&
88 getenv("hw.uart.console") == NULL) {
90 * If we found a 8250 com port and com speed, we need to
91 * tell the kernel where the serial port is, and how
92 * fast. Ideally, we'd get the port from ACPI, but that
93 * isn't running in the loader. Do the next best thing
94 * by allowing it to be set by a loader.conf variable,
95 * either a EFI specific one, or the compatible
96 * comconsole_port if not. PCI support is needed, but
97 * for that we'd ideally refactor the
98 * libi386/comconsole.c code to have identical behavior.
99 * We only try to set the port for cases where we saw
100 * the Serial(x) node when parsing, otherwise
101 * specialized hardware that has Uart nodes will have a
103 * But if someone specifically setup hw.uart.console,
104 * don't override that.
108 tmp = getenv("efi_com_speed");
110 speed = strtol(tmp, NULL, 0);
111 tmp = getenv("efi_com_port");
113 tmp = getenv("comconsole_port");
115 port = strtol(tmp, NULL, 0);
116 if (speed != -1 && port != -1) {
117 snprintf(buf, sizeof(buf), "io:%d,br:%d", port,
119 env_setenv("hw.uart.console", EV_VOLATILE, buf,
130 * Copy the environment into the load area starting at (addr).
131 * Each variable is formatted as <name>=<value>, with a single nul
132 * separating each variable, and a double nul terminating the environment.
135 bi_copyenv(vm_offset_t start)
138 vm_offset_t addr, last;
143 /* Traverse the environment. */
144 for (ep = environ; ep != NULL; ep = ep->ev_next) {
145 len = strlen(ep->ev_name);
146 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
149 if (archsw.arch_copyin("=", addr, 1) != 1)
152 if (ep->ev_value != NULL) {
153 len = strlen(ep->ev_value);
154 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
158 if (archsw.arch_copyin("", addr, 1) != 1)
163 if (archsw.arch_copyin("", last++, 1) != 1)
169 * Copy module-related data into the load area, where it can be
170 * used as a directory for loaded modules.
172 * Module data is presented in a self-describing format. Each datum
173 * is preceded by a 32-bit identifier and a 32-bit size field.
175 * Currently, the following data are saved:
177 * MOD_NAME (variable) module name (string)
178 * MOD_TYPE (variable) module type (string)
179 * MOD_ARGS (variable) module parameters (string)
180 * MOD_ADDR sizeof(vm_offset_t) module load address
181 * MOD_SIZE sizeof(size_t) module size
182 * MOD_METADATA (variable) type-specific metadata
184 #define COPY32(v, a, c) { \
187 archsw.arch_copyin(&x, a, sizeof(x)); \
191 #define MOD_STR(t, a, s, c) { \
193 COPY32(strlen(s) + 1, a, c); \
195 archsw.arch_copyin(s, a, strlen(s) + 1); \
196 a += roundup(strlen(s) + 1, sizeof(u_long)); \
199 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
200 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
201 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
203 #define MOD_VAR(t, a, s, c) { \
205 COPY32(sizeof(s), a, c); \
207 archsw.arch_copyin(&s, a, sizeof(s)); \
208 a += roundup(sizeof(s), sizeof(u_long)); \
211 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
212 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
214 #define MOD_METADATA(a, mm, c) { \
215 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
216 COPY32(mm->md_size, a, c); \
218 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
219 a += roundup(mm->md_size, sizeof(u_long)); \
222 #define MOD_END(a, c) { \
223 COPY32(MODINFO_END, a, c); \
228 bi_copymodules(vm_offset_t addr)
230 struct preloaded_file *fp;
231 struct file_metadata *md;
236 /* Start with the first module on the list, should be the kernel. */
237 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
238 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
239 MOD_TYPE(addr, fp->f_type, c);
241 MOD_ARGS(addr, fp->f_args, c);
244 v -= __elfN(relocation_offset);
246 MOD_ADDR(addr, v, c);
248 MOD_SIZE(addr, v, c);
249 for (md = fp->f_metadata; md != NULL; md = md->md_next)
250 if (!(md->md_type & MODINFOMD_NOCOPY))
251 MOD_METADATA(addr, md, c);
258 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
260 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap;
262 int curr, ndesc, nset;
269 /* This isn't really an EFI error case, but pretend it is */
270 return (EFI_OUT_OF_RESOURCES);
272 for (curr = 0; curr < ndesc;
273 curr++, desc = NextMemoryDescriptor(desc, mmsz)) {
274 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
276 desc->VirtualStart = desc->PhysicalStart;
278 viter = NextMemoryDescriptor(viter, mmsz);
281 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap);
287 bi_load_efi_data(struct preloaded_file *kfp)
289 EFI_MEMORY_DESCRIPTOR *mm;
290 EFI_PHYSICAL_ADDRESS addr;
292 const char *efi_novmap;
295 UINTN mmsz, pages, retry, sz;
297 struct efi_map_header *efihdr;
300 #if defined(__amd64__) || defined(__aarch64__)
303 if (efi_find_framebuffer(&efifb) == 0) {
304 printf("EFI framebuffer information:\n");
305 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr,
307 printf("dimensions %d x %d\n", efifb.fb_width,
309 printf("stride %d\n", efifb.fb_stride);
310 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
311 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
312 efifb.fb_mask_reserved);
314 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
319 efi_novmap = getenv("efi_disable_vmap");
320 if (efi_novmap != NULL)
321 do_vmap = strcasecmp(efi_novmap, "YES") != 0;
323 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
326 * Assgin size of EFI_MEMORY_DESCRIPTOR to keep compatible with
327 * u-boot which doesn't fill this value when buffer for memory
328 * descriptors is too small (eg. 0 to obtain memory map size)
330 mmsz = sizeof(EFI_MEMORY_DESCRIPTOR);
333 * It is possible that the first call to ExitBootServices may change
334 * the map key. Fetch a new map key and retry ExitBootServices in that
337 for (retry = 2; retry > 0; retry--) {
339 * Allocate enough pages to hold the bootinfo block and the
340 * memory map EFI will return to us. The memory map has an
341 * unknown size, so we have to determine that first. Note that
342 * the AllocatePages call can itself modify the memory map, so
343 * we have to take that into account as well. The changes to
344 * the memory map are caused by splitting a range of free
345 * memory into two (AFAICT), so that one is marked as being
349 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
351 sz = (sz + 0xf) & ~0xf;
352 pages = EFI_SIZE_TO_PAGES(sz + efisz);
353 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
355 if (EFI_ERROR(status)) {
356 printf("%s: AllocatePages error %lu\n", __func__,
357 EFI_ERROR_CODE(status));
362 * Read the memory map and stash it after bootinfo. Align the
363 * memory map on a 16-byte boundary (the bootinfo block is page
366 efihdr = (struct efi_map_header *)addr;
367 mm = (void *)((uint8_t *)efihdr + efisz);
368 sz = (EFI_PAGE_SIZE * pages) - efisz;
370 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
371 if (EFI_ERROR(status)) {
372 printf("%s: GetMemoryMap error %lu\n", __func__,
373 EFI_ERROR_CODE(status));
376 status = BS->ExitBootServices(IH, efi_mapkey);
377 if (EFI_ERROR(status) == 0) {
379 * This may be disabled by setting efi_disable_vmap in
380 * loader.conf(5). By default we will setup the virtual
384 efi_do_vmap(mm, sz, mmsz, mmver);
385 efihdr->memory_size = sz;
386 efihdr->descriptor_size = mmsz;
387 efihdr->descriptor_version = mmver;
388 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
392 BS->FreePages(addr, pages);
394 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
399 * Load the information expected by an amd64 kernel.
401 * - The 'boothowto' argument is constructed.
402 * - The 'bootdev' argument is constructed.
403 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
404 * - The kernel environment is copied into kernel space.
405 * - Module metadata are formatted and placed in kernel space.
408 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
410 struct preloaded_file *xp, *kfp;
411 struct devdesc *rootdev;
412 struct file_metadata *md;
419 #if defined(LOADER_FDT_SUPPORT)
427 * These metadata addreses must be converted for kernel after
431 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
433 #if defined(LOADER_FDT_SUPPORT)
439 howto = bi_getboothowto(args);
442 * Allow the environment variable 'rootdev' to override the supplied
443 * device. This should perhaps go to MI code and/or have $rootdev
444 * tested/set by MI code before launching the kernel.
446 rootdevname = getenv("rootdev");
447 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
448 if (rootdev == NULL) {
449 printf("Can't determine root device.\n");
453 /* Try reading the /etc/fstab file to select the root device */
454 getrootmount(efi_fmtdev((void *)rootdev));
457 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
458 if (addr < (xp->f_addr + xp->f_size))
459 addr = xp->f_addr + xp->f_size;
462 /* Pad to a page boundary. */
463 addr = roundup(addr, PAGE_SIZE);
465 /* Copy our environment. */
467 addr = bi_copyenv(addr);
469 /* Pad to a page boundary. */
470 addr = roundup(addr, PAGE_SIZE);
472 #if defined(LOADER_FDT_SUPPORT)
473 /* Handle device tree blob */
475 dtb_size = fdt_copy(addr);
477 /* Pad to a page boundary */
479 addr += roundup(dtb_size, PAGE_SIZE);
482 kfp = file_findfile(NULL, "elf kernel");
484 kfp = file_findfile(NULL, "elf64 kernel");
486 panic("can't find kernel file");
487 kernend = 0; /* fill it in later */
488 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
489 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
490 #if defined(LOADER_FDT_SUPPORT)
492 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
494 printf("WARNING! Trying to fire up the kernel, but no "
495 "device tree blob found!\n");
497 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
498 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
499 #ifdef LOADER_GELI_SUPPORT
500 geli_export_key_metadata(kfp);
502 bi_load_efi_data(kfp);
504 /* Figure out the size and location of the metadata. */
506 size = bi_copymodules(0);
507 kernend = roundup(addr + size, PAGE_SIZE);
510 /* patch MODINFOMD_KERNEND */
511 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
512 bcopy(&kernend, md->md_data, sizeof kernend);
515 *modulep -= __elfN(relocation_offset);
517 /* Do relocation fixup on metadata of each module. */
518 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
519 for (i = 0; i < nitems(mdt); i++) {
520 md = file_findmetadata(xp, mdt[i]);
522 bcopy(md->md_data, &vaddr, sizeof vaddr);
523 vaddr -= __elfN(relocation_offset);
524 bcopy(&vaddr, md->md_data, sizeof vaddr);
530 /* Copy module list and metadata. */
531 (void)bi_copymodules(addr);