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: head/sys/boot/efi/loader/bootinfo.c 293724 2016-01-12 02:17:39Z smh $");
34 #include <sys/param.h>
35 #include <sys/reboot.h>
36 #include <sys/linker.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(__x86_64__)
50 #include <machine/specialreg.h>
51 #include "framebuffer.h"
54 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp);
56 extern EFI_SYSTEM_TABLE *ST;
58 static const char howto_switches[] = "aCdrgmphsv";
59 static int howto_masks[] = {
60 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB,
61 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
65 bi_getboothowto(char *kargs)
74 /* Get the boot options from the environment first. */
75 for (i = 0; howto_names[i].ev != NULL; i++) {
76 if (getenv(howto_names[i].ev) != NULL)
77 howto |= howto_names[i].mask;
80 console = getenv("console");
81 if (console != NULL) {
82 if (strcmp(console, "comconsole") == 0)
84 if (strcmp(console, "vidconsole") == 0)
86 if (strcmp(console, "nullconsole") == 0)
94 opts = strchr(kargs, '-');
95 while (opts != NULL) {
96 while (*(++opts) != '\0') {
97 sw = strchr(howto_switches, *opts);
100 howto |= howto_masks[sw - howto_switches];
102 opts = strchr(opts, '-');
109 * Copy the environment into the load area starting at (addr).
110 * Each variable is formatted as <name>=<value>, with a single nul
111 * separating each variable, and a double nul terminating the environment.
114 bi_copyenv(vm_offset_t start)
117 vm_offset_t addr, last;
122 /* Traverse the environment. */
123 for (ep = environ; ep != NULL; ep = ep->ev_next) {
124 len = strlen(ep->ev_name);
125 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
128 if (archsw.arch_copyin("=", addr, 1) != 1)
131 if (ep->ev_value != NULL) {
132 len = strlen(ep->ev_value);
133 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
137 if (archsw.arch_copyin("", addr, 1) != 1)
142 if (archsw.arch_copyin("", last++, 1) != 1)
148 * Copy module-related data into the load area, where it can be
149 * used as a directory for loaded modules.
151 * Module data is presented in a self-describing format. Each datum
152 * is preceded by a 32-bit identifier and a 32-bit size field.
154 * Currently, the following data are saved:
156 * MOD_NAME (variable) module name (string)
157 * MOD_TYPE (variable) module type (string)
158 * MOD_ARGS (variable) module parameters (string)
159 * MOD_ADDR sizeof(vm_offset_t) module load address
160 * MOD_SIZE sizeof(size_t) module size
161 * MOD_METADATA (variable) type-specific metadata
163 #define COPY32(v, a, c) { \
166 archsw.arch_copyin(&x, a, sizeof(x)); \
170 #define MOD_STR(t, a, s, c) { \
172 COPY32(strlen(s) + 1, a, c); \
174 archsw.arch_copyin(s, a, strlen(s) + 1); \
175 a += roundup(strlen(s) + 1, sizeof(u_long)); \
178 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
179 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
180 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
182 #define MOD_VAR(t, a, s, c) { \
184 COPY32(sizeof(s), a, c); \
186 archsw.arch_copyin(&s, a, sizeof(s)); \
187 a += roundup(sizeof(s), sizeof(u_long)); \
190 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
191 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
193 #define MOD_METADATA(a, mm, c) { \
194 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
195 COPY32(mm->md_size, a, c); \
197 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
198 a += roundup(mm->md_size, sizeof(u_long)); \
201 #define MOD_END(a, c) { \
202 COPY32(MODINFO_END, a, c); \
207 bi_copymodules(vm_offset_t addr)
209 struct preloaded_file *fp;
210 struct file_metadata *md;
215 /* Start with the first module on the list, should be the kernel. */
216 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
217 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
218 MOD_TYPE(addr, fp->f_type, c);
220 MOD_ARGS(addr, fp->f_args, c);
222 MOD_ADDR(addr, v, c);
224 MOD_SIZE(addr, v, c);
225 for (md = fp->f_metadata; md != NULL; md = md->md_next)
226 if (!(md->md_type & MODINFOMD_NOCOPY))
227 MOD_METADATA(addr, md, c);
234 bi_load_efi_data(struct preloaded_file *kfp)
236 EFI_MEMORY_DESCRIPTOR *mm;
237 EFI_PHYSICAL_ADDRESS addr;
241 UINTN mmsz, pages, retry, sz;
243 struct efi_map_header *efihdr;
245 #if defined(__x86_64__)
248 if (efi_find_framebuffer(&efifb) == 0) {
249 printf("EFI framebuffer information:\n");
250 printf("addr, size 0x%lx, 0x%lx\n", efifb.fb_addr,
252 printf("dimensions %d x %d\n", efifb.fb_width,
254 printf("stride %d\n", efifb.fb_stride);
255 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
256 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
257 efifb.fb_mask_reserved);
259 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
263 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
266 * It is possible that the first call to ExitBootServices may change
267 * the map key. Fetch a new map key and retry ExitBootServices in that
270 for (retry = 2; retry > 0; retry--) {
272 * Allocate enough pages to hold the bootinfo block and the
273 * memory map EFI will return to us. The memory map has an
274 * unknown size, so we have to determine that first. Note that
275 * the AllocatePages call can itself modify the memory map, so
276 * we have to take that into account as well. The changes to
277 * the memory map are caused by splitting a range of free
278 * memory into two (AFAICT), so that one is marked as being
282 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
284 sz = (sz + 0xf) & ~0xf;
285 pages = EFI_SIZE_TO_PAGES(sz + efisz);
286 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
288 if (EFI_ERROR(status)) {
289 printf("%s: AllocatePages error %lu\n", __func__,
290 EFI_ERROR_CODE(status));
295 * Read the memory map and stash it after bootinfo. Align the
296 * memory map on a 16-byte boundary (the bootinfo block is page
299 efihdr = (struct efi_map_header *)addr;
300 mm = (void *)((uint8_t *)efihdr + efisz);
301 sz = (EFI_PAGE_SIZE * pages) - efisz;
303 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
304 if (EFI_ERROR(status)) {
305 printf("%s: GetMemoryMap error %lu\n", __func__,
306 EFI_ERROR_CODE(status));
309 status = BS->ExitBootServices(IH, efi_mapkey);
310 if (EFI_ERROR(status) == 0) {
311 efihdr->memory_size = sz;
312 efihdr->descriptor_size = mmsz;
313 efihdr->descriptor_version = mmver;
314 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
318 BS->FreePages(addr, pages);
320 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
325 * Load the information expected by an amd64 kernel.
327 * - The 'boothowto' argument is constructed.
328 * - The 'bootdev' argument is constructed.
329 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
330 * - The kernel environment is copied into kernel space.
331 * - Module metadata are formatted and placed in kernel space.
334 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
336 struct preloaded_file *xp, *kfp;
337 struct devdesc *rootdev;
338 struct file_metadata *md;
346 howto = bi_getboothowto(args);
349 * Allow the environment variable 'rootdev' to override the supplied
350 * device. This should perhaps go to MI code and/or have $rootdev
351 * tested/set by MI code before launching the kernel.
353 rootdevname = getenv("rootdev");
354 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
355 if (rootdev == NULL) {
356 printf("Can't determine root device.\n");
360 /* Try reading the /etc/fstab file to select the root device */
361 getrootmount(efi_fmtdev((void *)rootdev));
364 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
365 if (addr < (xp->f_addr + xp->f_size))
366 addr = xp->f_addr + xp->f_size;
369 /* Pad to a page boundary. */
370 addr = roundup(addr, PAGE_SIZE);
372 /* Copy our environment. */
374 addr = bi_copyenv(addr);
376 /* Pad to a page boundary. */
377 addr = roundup(addr, PAGE_SIZE);
379 kfp = file_findfile(NULL, "elf kernel");
381 kfp = file_findfile(NULL, "elf64 kernel");
383 panic("can't find kernel file");
384 kernend = 0; /* fill it in later */
385 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
386 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
387 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
388 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
390 bi_load_efi_data(kfp);
392 /* Figure out the size and location of the metadata. */
394 size = bi_copymodules(0);
395 kernend = roundup(addr + size, PAGE_SIZE);
398 /* patch MODINFOMD_KERNEND */
399 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
400 bcopy(&kernend, md->md_data, sizeof kernend);
402 /* Copy module list and metadata. */
403 (void)bi_copymodules(addr);