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)
75 howto = boot_parse_cmdline(kargs);
76 howto |= boot_env_to_howto();
78 console = getenv("console");
79 if (console != NULL) {
80 if (strcmp(console, "comconsole") == 0)
82 if (strcmp(console, "nullconsole") == 0)
90 * Copy the environment into the load area starting at (addr).
91 * Each variable is formatted as <name>=<value>, with a single nul
92 * separating each variable, and a double nul terminating the environment.
95 bi_copyenv(vm_offset_t start)
98 vm_offset_t addr, last;
103 /* Traverse the environment. */
104 for (ep = environ; ep != NULL; ep = ep->ev_next) {
105 len = strlen(ep->ev_name);
106 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
109 if (archsw.arch_copyin("=", addr, 1) != 1)
112 if (ep->ev_value != NULL) {
113 len = strlen(ep->ev_value);
114 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
118 if (archsw.arch_copyin("", addr, 1) != 1)
123 if (archsw.arch_copyin("", last++, 1) != 1)
129 * Copy module-related data into the load area, where it can be
130 * used as a directory for loaded modules.
132 * Module data is presented in a self-describing format. Each datum
133 * is preceded by a 32-bit identifier and a 32-bit size field.
135 * Currently, the following data are saved:
137 * MOD_NAME (variable) module name (string)
138 * MOD_TYPE (variable) module type (string)
139 * MOD_ARGS (variable) module parameters (string)
140 * MOD_ADDR sizeof(vm_offset_t) module load address
141 * MOD_SIZE sizeof(size_t) module size
142 * MOD_METADATA (variable) type-specific metadata
144 #define COPY32(v, a, c) { \
147 archsw.arch_copyin(&x, a, sizeof(x)); \
151 #define MOD_STR(t, a, s, c) { \
153 COPY32(strlen(s) + 1, a, c); \
155 archsw.arch_copyin(s, a, strlen(s) + 1); \
156 a += roundup(strlen(s) + 1, sizeof(u_long)); \
159 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
160 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
161 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
163 #define MOD_VAR(t, a, s, c) { \
165 COPY32(sizeof(s), a, c); \
167 archsw.arch_copyin(&s, a, sizeof(s)); \
168 a += roundup(sizeof(s), sizeof(u_long)); \
171 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
172 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
174 #define MOD_METADATA(a, mm, c) { \
175 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
176 COPY32(mm->md_size, a, c); \
178 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
179 a += roundup(mm->md_size, sizeof(u_long)); \
182 #define MOD_END(a, c) { \
183 COPY32(MODINFO_END, a, c); \
188 bi_copymodules(vm_offset_t addr)
190 struct preloaded_file *fp;
191 struct file_metadata *md;
196 /* Start with the first module on the list, should be the kernel. */
197 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
198 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
199 MOD_TYPE(addr, fp->f_type, c);
201 MOD_ARGS(addr, fp->f_args, c);
204 v -= __elfN(relocation_offset);
206 MOD_ADDR(addr, v, c);
208 MOD_SIZE(addr, v, c);
209 for (md = fp->f_metadata; md != NULL; md = md->md_next)
210 if (!(md->md_type & MODINFOMD_NOCOPY))
211 MOD_METADATA(addr, md, c);
218 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
220 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap;
222 int curr, ndesc, nset;
229 /* This isn't really an EFI error case, but pretend it is */
230 return (EFI_OUT_OF_RESOURCES);
232 for (curr = 0; curr < ndesc;
233 curr++, desc = NextMemoryDescriptor(desc, mmsz)) {
234 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
236 desc->VirtualStart = desc->PhysicalStart;
238 viter = NextMemoryDescriptor(viter, mmsz);
241 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap);
247 bi_load_efi_data(struct preloaded_file *kfp)
249 EFI_MEMORY_DESCRIPTOR *mm;
250 EFI_PHYSICAL_ADDRESS addr;
252 const char *efi_novmap;
255 UINTN mmsz, pages, retry, sz;
257 struct efi_map_header *efihdr;
260 #if defined(__amd64__) || defined(__aarch64__)
263 if (efi_find_framebuffer(&efifb) == 0) {
264 printf("EFI framebuffer information:\n");
265 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr,
267 printf("dimensions %d x %d\n", efifb.fb_width,
269 printf("stride %d\n", efifb.fb_stride);
270 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
271 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
272 efifb.fb_mask_reserved);
274 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
279 efi_novmap = getenv("efi_disable_vmap");
280 if (efi_novmap != NULL)
281 do_vmap = strcasecmp(efi_novmap, "YES") != 0;
283 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
286 * Assgin size of EFI_MEMORY_DESCRIPTOR to keep compatible with
287 * u-boot which doesn't fill this value when buffer for memory
288 * descriptors is too small (eg. 0 to obtain memory map size)
290 mmsz = sizeof(EFI_MEMORY_DESCRIPTOR);
293 * It is possible that the first call to ExitBootServices may change
294 * the map key. Fetch a new map key and retry ExitBootServices in that
297 for (retry = 2; retry > 0; retry--) {
299 * Allocate enough pages to hold the bootinfo block and the
300 * memory map EFI will return to us. The memory map has an
301 * unknown size, so we have to determine that first. Note that
302 * the AllocatePages call can itself modify the memory map, so
303 * we have to take that into account as well. The changes to
304 * the memory map are caused by splitting a range of free
305 * memory into two (AFAICT), so that one is marked as being
309 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
311 sz = (sz + 0xf) & ~0xf;
312 pages = EFI_SIZE_TO_PAGES(sz + efisz);
313 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
315 if (EFI_ERROR(status)) {
316 printf("%s: AllocatePages error %lu\n", __func__,
317 EFI_ERROR_CODE(status));
322 * Read the memory map and stash it after bootinfo. Align the
323 * memory map on a 16-byte boundary (the bootinfo block is page
326 efihdr = (struct efi_map_header *)addr;
327 mm = (void *)((uint8_t *)efihdr + efisz);
328 sz = (EFI_PAGE_SIZE * pages) - efisz;
330 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
331 if (EFI_ERROR(status)) {
332 printf("%s: GetMemoryMap error %lu\n", __func__,
333 EFI_ERROR_CODE(status));
336 status = BS->ExitBootServices(IH, efi_mapkey);
337 if (EFI_ERROR(status) == 0) {
339 * This may be disabled by setting efi_disable_vmap in
340 * loader.conf(5). By default we will setup the virtual
344 efi_do_vmap(mm, sz, mmsz, mmver);
345 efihdr->memory_size = sz;
346 efihdr->descriptor_size = mmsz;
347 efihdr->descriptor_version = mmver;
348 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
352 BS->FreePages(addr, pages);
354 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
359 * Load the information expected by an amd64 kernel.
361 * - The 'boothowto' argument is constructed.
362 * - The 'bootdev' argument is constructed.
363 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
364 * - The kernel environment is copied into kernel space.
365 * - Module metadata are formatted and placed in kernel space.
368 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
370 struct preloaded_file *xp, *kfp;
371 struct devdesc *rootdev;
372 struct file_metadata *md;
379 #if defined(LOADER_FDT_SUPPORT)
387 * These metadata addreses must be converted for kernel after
391 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
393 #if defined(LOADER_FDT_SUPPORT)
399 howto = bi_getboothowto(args);
402 * Allow the environment variable 'rootdev' to override the supplied
403 * device. This should perhaps go to MI code and/or have $rootdev
404 * tested/set by MI code before launching the kernel.
406 rootdevname = getenv("rootdev");
407 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
408 if (rootdev == NULL) {
409 printf("Can't determine root device.\n");
413 /* Try reading the /etc/fstab file to select the root device */
414 getrootmount(efi_fmtdev((void *)rootdev));
417 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
418 if (addr < (xp->f_addr + xp->f_size))
419 addr = xp->f_addr + xp->f_size;
422 /* Pad to a page boundary. */
423 addr = roundup(addr, PAGE_SIZE);
425 /* Copy our environment. */
427 addr = bi_copyenv(addr);
429 /* Pad to a page boundary. */
430 addr = roundup(addr, PAGE_SIZE);
432 #if defined(LOADER_FDT_SUPPORT)
433 /* Handle device tree blob */
435 dtb_size = fdt_copy(addr);
437 /* Pad to a page boundary */
439 addr += roundup(dtb_size, PAGE_SIZE);
442 kfp = file_findfile(NULL, "elf kernel");
444 kfp = file_findfile(NULL, "elf64 kernel");
446 panic("can't find kernel file");
447 kernend = 0; /* fill it in later */
448 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
449 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
450 #if defined(LOADER_FDT_SUPPORT)
452 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
454 printf("WARNING! Trying to fire up the kernel, but no "
455 "device tree blob found!\n");
457 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
458 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
459 #ifdef LOADER_GELI_SUPPORT
460 geli_export_key_metadata(kfp);
462 bi_load_efi_data(kfp);
464 /* Figure out the size and location of the metadata. */
466 size = bi_copymodules(0);
467 kernend = roundup(addr + size, PAGE_SIZE);
470 /* patch MODINFOMD_KERNEND */
471 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
472 bcopy(&kernend, md->md_data, sizeof kernend);
475 *modulep -= __elfN(relocation_offset);
477 /* Do relocation fixup on metadata of each module. */
478 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
479 for (i = 0; i < nitems(mdt); i++) {
480 md = file_findmetadata(xp, mdt[i]);
482 bcopy(md->md_data, &vaddr, sizeof vaddr);
483 vaddr -= __elfN(relocation_offset);
484 bcopy(&vaddr, md->md_data, sizeof vaddr);
490 /* Copy module list and metadata. */
491 (void)bi_copymodules(addr);