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 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
61 extern EFI_SYSTEM_TABLE *ST;
64 bi_getboothowto(char *kargs)
71 howto = boot_parse_cmdline(kargs);
72 howto |= boot_env_to_howto();
74 console = getenv("console");
75 if (console != NULL) {
76 if (strcmp(console, "comconsole") == 0)
78 if (strcmp(console, "nullconsole") == 0)
86 * Copy the environment into the load area starting at (addr).
87 * Each variable is formatted as <name>=<value>, with a single nul
88 * separating each variable, and a double nul terminating the environment.
91 bi_copyenv(vm_offset_t start)
94 vm_offset_t addr, last;
99 /* Traverse the environment. */
100 for (ep = environ; ep != NULL; ep = ep->ev_next) {
101 len = strlen(ep->ev_name);
102 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
105 if (archsw.arch_copyin("=", addr, 1) != 1)
108 if (ep->ev_value != NULL) {
109 len = strlen(ep->ev_value);
110 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
114 if (archsw.arch_copyin("", addr, 1) != 1)
119 if (archsw.arch_copyin("", last++, 1) != 1)
125 * Copy module-related data into the load area, where it can be
126 * used as a directory for loaded modules.
128 * Module data is presented in a self-describing format. Each datum
129 * is preceded by a 32-bit identifier and a 32-bit size field.
131 * Currently, the following data are saved:
133 * MOD_NAME (variable) module name (string)
134 * MOD_TYPE (variable) module type (string)
135 * MOD_ARGS (variable) module parameters (string)
136 * MOD_ADDR sizeof(vm_offset_t) module load address
137 * MOD_SIZE sizeof(size_t) module size
138 * MOD_METADATA (variable) type-specific metadata
140 #define COPY32(v, a, c) { \
143 archsw.arch_copyin(&x, a, sizeof(x)); \
147 #define MOD_STR(t, a, s, c) { \
149 COPY32(strlen(s) + 1, a, c); \
151 archsw.arch_copyin(s, a, strlen(s) + 1); \
152 a += roundup(strlen(s) + 1, sizeof(u_long)); \
155 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
156 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
157 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
159 #define MOD_VAR(t, a, s, c) { \
161 COPY32(sizeof(s), a, c); \
163 archsw.arch_copyin(&s, a, sizeof(s)); \
164 a += roundup(sizeof(s), sizeof(u_long)); \
167 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
168 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
170 #define MOD_METADATA(a, mm, c) { \
171 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
172 COPY32(mm->md_size, a, c); \
174 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
175 a += roundup(mm->md_size, sizeof(u_long)); \
178 #define MOD_END(a, c) { \
179 COPY32(MODINFO_END, a, c); \
184 bi_copymodules(vm_offset_t addr)
186 struct preloaded_file *fp;
187 struct file_metadata *md;
192 /* Start with the first module on the list, should be the kernel. */
193 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
194 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
195 MOD_TYPE(addr, fp->f_type, c);
197 MOD_ARGS(addr, fp->f_args, c);
200 v -= __elfN(relocation_offset);
202 MOD_ADDR(addr, v, c);
204 MOD_SIZE(addr, v, c);
205 for (md = fp->f_metadata; md != NULL; md = md->md_next)
206 if (!(md->md_type & MODINFOMD_NOCOPY))
207 MOD_METADATA(addr, md, c);
214 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver)
216 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap;
218 int curr, ndesc, nset;
225 /* This isn't really an EFI error case, but pretend it is */
226 return (EFI_OUT_OF_RESOURCES);
228 for (curr = 0; curr < ndesc;
229 curr++, desc = NextMemoryDescriptor(desc, mmsz)) {
230 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) {
232 desc->VirtualStart = desc->PhysicalStart;
234 viter = NextMemoryDescriptor(viter, mmsz);
237 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap);
243 bi_load_efi_data(struct preloaded_file *kfp)
245 EFI_MEMORY_DESCRIPTOR *mm;
246 EFI_PHYSICAL_ADDRESS addr;
248 const char *efi_novmap;
251 UINTN mmsz, pages, retry, sz;
253 struct efi_map_header *efihdr;
256 #if defined(__amd64__) || defined(__aarch64__)
259 if (efi_find_framebuffer(&efifb) == 0) {
260 printf("EFI framebuffer information:\n");
261 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr,
263 printf("dimensions %d x %d\n", efifb.fb_width,
265 printf("stride %d\n", efifb.fb_stride);
266 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
267 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
268 efifb.fb_mask_reserved);
270 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
275 efi_novmap = getenv("efi_disable_vmap");
276 if (efi_novmap != NULL)
277 do_vmap = strcasecmp(efi_novmap, "YES") != 0;
279 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
282 * Assgin size of EFI_MEMORY_DESCRIPTOR to keep compatible with
283 * u-boot which doesn't fill this value when buffer for memory
284 * descriptors is too small (eg. 0 to obtain memory map size)
286 mmsz = sizeof(EFI_MEMORY_DESCRIPTOR);
289 * It is possible that the first call to ExitBootServices may change
290 * the map key. Fetch a new map key and retry ExitBootServices in that
293 for (retry = 2; retry > 0; retry--) {
295 * Allocate enough pages to hold the bootinfo block and the
296 * memory map EFI will return to us. The memory map has an
297 * unknown size, so we have to determine that first. Note that
298 * the AllocatePages call can itself modify the memory map, so
299 * we have to take that into account as well. The changes to
300 * the memory map are caused by splitting a range of free
301 * memory into two (AFAICT), so that one is marked as being
305 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
307 sz = (sz + 0xf) & ~0xf;
308 pages = EFI_SIZE_TO_PAGES(sz + efisz);
309 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
311 if (EFI_ERROR(status)) {
312 printf("%s: AllocatePages error %lu\n", __func__,
313 EFI_ERROR_CODE(status));
318 * Read the memory map and stash it after bootinfo. Align the
319 * memory map on a 16-byte boundary (the bootinfo block is page
322 efihdr = (struct efi_map_header *)(uintptr_t)addr;
323 mm = (void *)((uint8_t *)efihdr + efisz);
324 sz = (EFI_PAGE_SIZE * pages) - efisz;
326 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
327 if (EFI_ERROR(status)) {
328 printf("%s: GetMemoryMap error %lu\n", __func__,
329 EFI_ERROR_CODE(status));
332 status = BS->ExitBootServices(IH, efi_mapkey);
333 if (EFI_ERROR(status) == 0) {
335 * This may be disabled by setting efi_disable_vmap in
336 * loader.conf(5). By default we will setup the virtual
340 efi_do_vmap(mm, sz, mmsz, mmver);
341 efihdr->memory_size = sz;
342 efihdr->descriptor_size = mmsz;
343 efihdr->descriptor_version = mmver;
344 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
348 BS->FreePages(addr, pages);
350 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
355 * Load the information expected by an amd64 kernel.
357 * - The 'boothowto' argument is constructed.
358 * - The 'bootdev' argument is constructed.
359 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
360 * - The kernel environment is copied into kernel space.
361 * - Module metadata are formatted and placed in kernel space.
364 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
366 struct preloaded_file *xp, *kfp;
367 struct devdesc *rootdev;
368 struct file_metadata *md;
375 #if defined(LOADER_FDT_SUPPORT)
383 * These metadata addreses must be converted for kernel after
387 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
389 #if defined(LOADER_FDT_SUPPORT)
395 howto = bi_getboothowto(args);
398 * Allow the environment variable 'rootdev' to override the supplied
399 * device. This should perhaps go to MI code and/or have $rootdev
400 * tested/set by MI code before launching the kernel.
402 rootdevname = getenv("rootdev");
403 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
404 if (rootdev == NULL) {
405 printf("Can't determine root device.\n");
409 /* Try reading the /etc/fstab file to select the root device */
410 getrootmount(efi_fmtdev((void *)rootdev));
413 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
414 if (addr < (xp->f_addr + xp->f_size))
415 addr = xp->f_addr + xp->f_size;
418 /* Pad to a page boundary. */
419 addr = roundup(addr, PAGE_SIZE);
421 /* Copy our environment. */
423 addr = bi_copyenv(addr);
425 /* Pad to a page boundary. */
426 addr = roundup(addr, PAGE_SIZE);
428 #if defined(LOADER_FDT_SUPPORT)
429 /* Handle device tree blob */
431 dtb_size = fdt_copy(addr);
433 /* Pad to a page boundary */
435 addr += roundup(dtb_size, PAGE_SIZE);
438 kfp = file_findfile(NULL, "elf kernel");
440 kfp = file_findfile(NULL, "elf64 kernel");
442 panic("can't find kernel file");
443 kernend = 0; /* fill it in later */
444 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
445 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
446 #if defined(LOADER_FDT_SUPPORT)
448 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
450 printf("WARNING! Trying to fire up the kernel, but no "
451 "device tree blob found!\n");
453 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
454 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
456 bi_load_efi_data(kfp);
458 /* Figure out the size and location of the metadata. */
460 size = bi_copymodules(0);
461 kernend = roundup(addr + size, PAGE_SIZE);
464 /* patch MODINFOMD_KERNEND */
465 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
466 bcopy(&kernend, md->md_data, sizeof kernend);
469 *modulep -= __elfN(relocation_offset);
471 /* Do relocation fixup on metadata of each module. */
472 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
473 for (i = 0; i < nitems(mdt); i++) {
474 md = file_findmetadata(xp, mdt[i]);
476 bcopy(md->md_data, &vaddr, sizeof vaddr);
477 vaddr -= __elfN(relocation_offset);
478 bcopy(&vaddr, md->md_data, sizeof vaddr);
484 /* Copy module list and metadata. */
485 (void)bi_copymodules(addr);