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 (strcmp(console, "efi") == 0) {
87 * If we found a com port and com speed, we need to tell
88 * the kernel where the serial port is, and how
89 * fast. Ideally, we'd get the port from ACPI, but that
90 * isn't running in the loader. Do the next best thing
91 * by allowing it to be set by a loader.conf variable,
92 * either a EFI specific one, or the compatible
93 * comconsole_port if not. PCI support is needed, but
94 * for that we'd ideally refactor the
95 * libi386/comconsole.c code to have identical behavior.
97 tmp = getenv("efi_com_speed");
99 speed = strtol(tmp, NULL, 0);
100 tmp = getenv("efi_com_port");
102 tmp = getenv("comconsole_port");
103 /* XXX fallback to EFI variable set in rc.d? */
105 port = strtol(tmp, NULL, 0);
108 snprintf(buf, sizeof(buf), "io:%d,br:%d", port,
110 env_setenv("hw.uart.console", EV_VOLATILE, buf,
120 * Copy the environment into the load area starting at (addr).
121 * Each variable is formatted as <name>=<value>, with a single nul
122 * separating each variable, and a double nul terminating the environment.
125 bi_copyenv(vm_offset_t start)
128 vm_offset_t addr, last;
133 /* Traverse the environment. */
134 for (ep = environ; ep != NULL; ep = ep->ev_next) {
135 len = strlen(ep->ev_name);
136 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
139 if (archsw.arch_copyin("=", addr, 1) != 1)
142 if (ep->ev_value != NULL) {
143 len = strlen(ep->ev_value);
144 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
148 if (archsw.arch_copyin("", addr, 1) != 1)
153 if (archsw.arch_copyin("", last++, 1) != 1)
159 * Copy module-related data into the load area, where it can be
160 * used as a directory for loaded modules.
162 * Module data is presented in a self-describing format. Each datum
163 * is preceded by a 32-bit identifier and a 32-bit size field.
165 * Currently, the following data are saved:
167 * MOD_NAME (variable) module name (string)
168 * MOD_TYPE (variable) module type (string)
169 * MOD_ARGS (variable) module parameters (string)
170 * MOD_ADDR sizeof(vm_offset_t) module load address
171 * MOD_SIZE sizeof(size_t) module size
172 * MOD_METADATA (variable) type-specific metadata
174 #define COPY32(v, a, c) { \
177 archsw.arch_copyin(&x, a, sizeof(x)); \
181 #define MOD_STR(t, a, s, c) { \
183 COPY32(strlen(s) + 1, a, c); \
185 archsw.arch_copyin(s, a, strlen(s) + 1); \
186 a += roundup(strlen(s) + 1, sizeof(u_long)); \
189 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
190 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
191 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
193 #define MOD_VAR(t, a, s, c) { \
195 COPY32(sizeof(s), a, c); \
197 archsw.arch_copyin(&s, a, sizeof(s)); \
198 a += roundup(sizeof(s), sizeof(u_long)); \
201 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
202 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
204 #define MOD_METADATA(a, mm, c) { \
205 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
206 COPY32(mm->md_size, a, c); \
208 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
209 a += roundup(mm->md_size, sizeof(u_long)); \
212 #define MOD_END(a, c) { \
213 COPY32(MODINFO_END, a, c); \
218 bi_copymodules(vm_offset_t addr)
220 struct preloaded_file *fp;
221 struct file_metadata *md;
226 /* Start with the first module on the list, should be the kernel. */
227 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
228 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
229 MOD_TYPE(addr, fp->f_type, c);
231 MOD_ARGS(addr, fp->f_args, c);
234 v -= __elfN(relocation_offset);
236 MOD_ADDR(addr, v, c);
238 MOD_SIZE(addr, v, c);
239 for (md = fp->f_metadata; md != NULL; md = md->md_next)
240 if (!(md->md_type & MODINFOMD_NOCOPY))
241 MOD_METADATA(addr, md, c);
248 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
250 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap;
252 int curr, ndesc, nset;
259 /* This isn't really an EFI error case, but pretend it is */
260 return (EFI_OUT_OF_RESOURCES);
262 for (curr = 0; curr < ndesc;
263 curr++, desc = NextMemoryDescriptor(desc, mmsz)) {
264 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
266 desc->VirtualStart = desc->PhysicalStart;
268 viter = NextMemoryDescriptor(viter, mmsz);
271 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap);
277 bi_load_efi_data(struct preloaded_file *kfp)
279 EFI_MEMORY_DESCRIPTOR *mm;
280 EFI_PHYSICAL_ADDRESS addr;
282 const char *efi_novmap;
285 UINTN mmsz, pages, retry, sz;
287 struct efi_map_header *efihdr;
290 #if defined(__amd64__) || defined(__aarch64__)
293 if (efi_find_framebuffer(&efifb) == 0) {
294 printf("EFI framebuffer information:\n");
295 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr,
297 printf("dimensions %d x %d\n", efifb.fb_width,
299 printf("stride %d\n", efifb.fb_stride);
300 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
301 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
302 efifb.fb_mask_reserved);
304 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
309 efi_novmap = getenv("efi_disable_vmap");
310 if (efi_novmap != NULL)
311 do_vmap = strcasecmp(efi_novmap, "YES") != 0;
313 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
316 * Assgin size of EFI_MEMORY_DESCRIPTOR to keep compatible with
317 * u-boot which doesn't fill this value when buffer for memory
318 * descriptors is too small (eg. 0 to obtain memory map size)
320 mmsz = sizeof(EFI_MEMORY_DESCRIPTOR);
323 * It is possible that the first call to ExitBootServices may change
324 * the map key. Fetch a new map key and retry ExitBootServices in that
327 for (retry = 2; retry > 0; retry--) {
329 * Allocate enough pages to hold the bootinfo block and the
330 * memory map EFI will return to us. The memory map has an
331 * unknown size, so we have to determine that first. Note that
332 * the AllocatePages call can itself modify the memory map, so
333 * we have to take that into account as well. The changes to
334 * the memory map are caused by splitting a range of free
335 * memory into two (AFAICT), so that one is marked as being
339 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
341 sz = (sz + 0xf) & ~0xf;
342 pages = EFI_SIZE_TO_PAGES(sz + efisz);
343 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
345 if (EFI_ERROR(status)) {
346 printf("%s: AllocatePages error %lu\n", __func__,
347 EFI_ERROR_CODE(status));
352 * Read the memory map and stash it after bootinfo. Align the
353 * memory map on a 16-byte boundary (the bootinfo block is page
356 efihdr = (struct efi_map_header *)addr;
357 mm = (void *)((uint8_t *)efihdr + efisz);
358 sz = (EFI_PAGE_SIZE * pages) - efisz;
360 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
361 if (EFI_ERROR(status)) {
362 printf("%s: GetMemoryMap error %lu\n", __func__,
363 EFI_ERROR_CODE(status));
366 status = BS->ExitBootServices(IH, efi_mapkey);
367 if (EFI_ERROR(status) == 0) {
369 * This may be disabled by setting efi_disable_vmap in
370 * loader.conf(5). By default we will setup the virtual
374 efi_do_vmap(mm, sz, mmsz, mmver);
375 efihdr->memory_size = sz;
376 efihdr->descriptor_size = mmsz;
377 efihdr->descriptor_version = mmver;
378 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
382 BS->FreePages(addr, pages);
384 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
389 * Load the information expected by an amd64 kernel.
391 * - The 'boothowto' argument is constructed.
392 * - The 'bootdev' argument is constructed.
393 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
394 * - The kernel environment is copied into kernel space.
395 * - Module metadata are formatted and placed in kernel space.
398 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
400 struct preloaded_file *xp, *kfp;
401 struct devdesc *rootdev;
402 struct file_metadata *md;
409 #if defined(LOADER_FDT_SUPPORT)
417 * These metadata addreses must be converted for kernel after
421 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
423 #if defined(LOADER_FDT_SUPPORT)
429 howto = bi_getboothowto(args);
432 * Allow the environment variable 'rootdev' to override the supplied
433 * device. This should perhaps go to MI code and/or have $rootdev
434 * tested/set by MI code before launching the kernel.
436 rootdevname = getenv("rootdev");
437 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
438 if (rootdev == NULL) {
439 printf("Can't determine root device.\n");
443 /* Try reading the /etc/fstab file to select the root device */
444 getrootmount(efi_fmtdev((void *)rootdev));
447 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
448 if (addr < (xp->f_addr + xp->f_size))
449 addr = xp->f_addr + xp->f_size;
452 /* Pad to a page boundary. */
453 addr = roundup(addr, PAGE_SIZE);
455 /* Copy our environment. */
457 addr = bi_copyenv(addr);
459 /* Pad to a page boundary. */
460 addr = roundup(addr, PAGE_SIZE);
462 #if defined(LOADER_FDT_SUPPORT)
463 /* Handle device tree blob */
465 dtb_size = fdt_copy(addr);
467 /* Pad to a page boundary */
469 addr += roundup(dtb_size, PAGE_SIZE);
472 kfp = file_findfile(NULL, "elf kernel");
474 kfp = file_findfile(NULL, "elf64 kernel");
476 panic("can't find kernel file");
477 kernend = 0; /* fill it in later */
478 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
479 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
480 #if defined(LOADER_FDT_SUPPORT)
482 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
484 printf("WARNING! Trying to fire up the kernel, but no "
485 "device tree blob found!\n");
487 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
488 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
489 #ifdef LOADER_GELI_SUPPORT
490 geli_export_key_metadata(kfp);
492 bi_load_efi_data(kfp);
494 /* Figure out the size and location of the metadata. */
496 size = bi_copymodules(0);
497 kernend = roundup(addr + size, PAGE_SIZE);
500 /* patch MODINFOMD_KERNEND */
501 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
502 bcopy(&kernend, md->md_data, sizeof kernend);
505 *modulep -= __elfN(relocation_offset);
507 /* Do relocation fixup on metadata of each module. */
508 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
509 for (i = 0; i < nitems(mdt); i++) {
510 md = file_findmetadata(xp, mdt[i]);
512 bcopy(md->md_data, &vaddr, sizeof vaddr);
513 vaddr -= __elfN(relocation_offset);
514 bcopy(&vaddr, md->md_data, sizeof vaddr);
520 /* Copy module list and metadata. */
521 (void)bi_copymodules(addr);