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/reboot.h>
36 #include <sys/linker.h>
38 #include <machine/cpufunc.h>
39 #include <machine/metadata.h>
40 #include <machine/psl.h>
41 #include <machine/specialreg.h>
46 #include "bootstrap.h"
47 #include "framebuffer.h"
52 static const char howto_switches[] = "aCdrgDmphsv";
53 static int howto_masks[] = {
54 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
55 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
59 bi_getboothowto(char *kargs)
68 /* Get the boot options from the environment first. */
69 for (i = 0; howto_names[i].ev != NULL; i++) {
70 if (getenv(howto_names[i].ev) != NULL)
71 howto |= howto_names[i].mask;
74 console = getenv("console");
75 if (console != NULL) {
76 if (strcmp(console, "comconsole") == 0)
78 if (strcmp(console, "nullconsole") == 0)
86 opts = strchr(kargs, '-');
87 while (opts != NULL) {
88 while (*(++opts) != '\0') {
89 sw = strchr(howto_switches, *opts);
92 howto |= howto_masks[sw - howto_switches];
94 opts = strchr(opts, '-');
101 * Copy the environment into the load area starting at (addr).
102 * Each variable is formatted as <name>=<value>, with a single nul
103 * separating each variable, and a double nul terminating the environment.
106 bi_copyenv(vm_offset_t start)
109 vm_offset_t addr, last;
114 /* Traverse the environment. */
115 for (ep = environ; ep != NULL; ep = ep->ev_next) {
116 len = strlen(ep->ev_name);
117 if (x86_efi_copyin(ep->ev_name, addr, len) != len)
120 if (x86_efi_copyin("=", addr, 1) != 1)
123 if (ep->ev_value != NULL) {
124 len = strlen(ep->ev_value);
125 if (x86_efi_copyin(ep->ev_value, addr, len) != len)
129 if (x86_efi_copyin("", addr, 1) != 1)
134 if (x86_efi_copyin("", last++, 1) != 1)
140 * Copy module-related data into the load area, where it can be
141 * used as a directory for loaded modules.
143 * Module data is presented in a self-describing format. Each datum
144 * is preceded by a 32-bit identifier and a 32-bit size field.
146 * Currently, the following data are saved:
148 * MOD_NAME (variable) module name (string)
149 * MOD_TYPE (variable) module type (string)
150 * MOD_ARGS (variable) module parameters (string)
151 * MOD_ADDR sizeof(vm_offset_t) module load address
152 * MOD_SIZE sizeof(size_t) module size
153 * MOD_METADATA (variable) type-specific metadata
155 #define COPY32(v, a, c) { \
158 x86_efi_copyin(&x, a, sizeof(x)); \
162 #define MOD_STR(t, a, s, c) { \
164 COPY32(strlen(s) + 1, a, c); \
166 x86_efi_copyin(s, a, strlen(s) + 1); \
167 a += roundup(strlen(s) + 1, sizeof(uint64_t)); \
170 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
171 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
172 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
174 #define MOD_VAR(t, a, s, c) { \
176 COPY32(sizeof(s), a, c); \
178 x86_efi_copyin(&s, a, sizeof(s)); \
179 a += roundup(sizeof(s), sizeof(uint64_t)); \
182 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
183 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
185 #define MOD_METADATA(a, mm, c) { \
186 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
187 COPY32(mm->md_size, a, c); \
189 x86_efi_copyin(mm->md_data, a, mm->md_size); \
190 a += roundup(mm->md_size, sizeof(uint64_t)); \
193 #define MOD_END(a, c) { \
194 COPY32(MODINFO_END, a, c); \
199 bi_copymodules(vm_offset_t addr)
201 struct preloaded_file *fp;
202 struct file_metadata *md;
207 /* Start with the first module on the list, should be the kernel. */
208 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
209 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
210 MOD_TYPE(addr, fp->f_type, c);
212 MOD_ARGS(addr, fp->f_args, c);
214 MOD_ADDR(addr, v, c);
216 MOD_SIZE(addr, v, c);
217 for (md = fp->f_metadata; md != NULL; md = md->md_next)
218 if (!(md->md_type & MODINFOMD_NOCOPY))
219 MOD_METADATA(addr, md, c);
226 bi_load_efi_data(struct preloaded_file *kfp)
228 EFI_MEMORY_DESCRIPTOR *mm;
229 EFI_PHYSICAL_ADDRESS addr;
232 UINTN mmsz, pages, sz;
234 struct efi_map_header *efihdr;
237 if (efi_find_framebuffer(&efifb) == 0)
238 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
240 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
243 * Allocate enough pages to hold the bootinfo block and the memory
244 * map EFI will return to us. The memory map has an unknown size,
245 * so we have to determine that first. Note that the AllocatePages
246 * call can itself modify the memory map, so we have to take that
247 * into account as well. The changes to the memory map are caused
248 * by splitting a range of free memory into two (AFAICT), so that
249 * one is marked as being loader data.
252 BS->GetMemoryMap(&sz, NULL, &x86_efi_mapkey, &mmsz, &mmver);
254 sz = (sz + 0xf) & ~0xf;
255 pages = EFI_SIZE_TO_PAGES(sz + efisz);
256 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
258 if (EFI_ERROR(status)) {
259 printf("%s: AllocatePages() returned 0x%lx\n", __func__,
265 * Read the memory map and stash it after bootinfo. Align the
266 * memory map on a 16-byte boundary (the bootinfo block is page
269 efihdr = (struct efi_map_header *)addr;
270 mm = (void *)((uint8_t *)efihdr + efisz);
271 sz = (EFI_PAGE_SIZE * pages) - efisz;
272 status = BS->GetMemoryMap(&sz, mm, &x86_efi_mapkey, &mmsz, &mmver);
273 if (EFI_ERROR(status)) {
274 printf("%s: GetMemoryMap() returned 0x%lx\n", __func__,
279 efihdr->memory_size = sz;
280 efihdr->descriptor_size = mmsz;
281 efihdr->descriptor_version = mmver;
283 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz, efihdr);
289 * Load the information expected by an amd64 kernel.
291 * - The 'boothowto' argument is constructed.
292 * - The 'bootdev' argument is constructed.
293 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
294 * - The kernel environment is copied into kernel space.
295 * - Module metadata are formatted and placed in kernel space.
298 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
300 struct preloaded_file *xp, *kfp;
301 struct devdesc *rootdev;
302 struct file_metadata *md;
310 howto = bi_getboothowto(args);
313 * Allow the environment variable 'rootdev' to override the supplied
314 * device. This should perhaps go to MI code and/or have $rootdev
315 * tested/set by MI code before launching the kernel.
317 rootdevname = getenv("rootdev");
318 x86_efi_getdev((void**)(&rootdev), rootdevname, NULL);
319 if (rootdev == NULL) {
320 printf("Can't determine root device.\n");
324 /* Try reading the /etc/fstab file to select the root device */
325 getrootmount(x86_efi_fmtdev((void *)rootdev));
328 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
329 if (addr < (xp->f_addr + xp->f_size))
330 addr = xp->f_addr + xp->f_size;
333 /* Pad to a page boundary. */
334 addr = roundup(addr, PAGE_SIZE);
336 /* Copy our environment. */
338 addr = bi_copyenv(addr);
340 /* Pad to a page boundary. */
341 addr = roundup(addr, PAGE_SIZE);
343 kfp = file_findfile(NULL, "elf kernel");
345 kfp = file_findfile(NULL, "elf64 kernel");
347 panic("can't find kernel file");
348 kernend = 0; /* fill it in later */
349 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
350 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
351 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
353 bi_load_efi_data(kfp);
355 /* Figure out the size and location of the metadata. */
357 size = bi_copymodules(0);
358 kernend = roundup(addr + size, PAGE_SIZE);
361 /* patch MODINFOMD_KERNEND */
362 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
363 bcopy(&kernend, md->md_data, sizeof kernend);
365 /* Copy module list and metadata. */
366 (void)bi_copymodules(addr);