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/elf.h>
40 #include <machine/metadata.h>
41 #include <machine/psl.h>
42 #include <machine/specialreg.h>
47 #include "bootstrap.h"
48 #include "framebuffer.h"
49 #include "loader_efi.h"
53 static const char howto_switches[] = "aCdrgDmphsv";
54 static int howto_masks[] = {
55 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
56 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
60 bi_getboothowto(char *kargs)
69 /* Get the boot options from the environment first. */
70 for (i = 0; howto_names[i].ev != NULL; i++) {
71 if (getenv(howto_names[i].ev) != NULL)
72 howto |= howto_names[i].mask;
75 console = getenv("console");
76 if (console != NULL) {
77 if (strcmp(console, "comconsole") == 0)
79 if (strcmp(console, "nullconsole") == 0)
87 opts = strchr(kargs, '-');
88 while (opts != NULL) {
89 while (*(++opts) != '\0') {
90 sw = strchr(howto_switches, *opts);
93 howto |= howto_masks[sw - howto_switches];
95 opts = strchr(opts, '-');
102 * Copy the environment into the load area starting at (addr).
103 * Each variable is formatted as <name>=<value>, with a single nul
104 * separating each variable, and a double nul terminating the environment.
107 bi_copyenv(vm_offset_t start)
110 vm_offset_t addr, last;
115 /* Traverse the environment. */
116 for (ep = environ; ep != NULL; ep = ep->ev_next) {
117 len = strlen(ep->ev_name);
118 if (archsw.arch_copyin(ep->ev_name, addr, len) != len)
121 if (archsw.arch_copyin("=", addr, 1) != 1)
124 if (ep->ev_value != NULL) {
125 len = strlen(ep->ev_value);
126 if (archsw.arch_copyin(ep->ev_value, addr, len) != len)
130 if (archsw.arch_copyin("", addr, 1) != 1)
135 if (archsw.arch_copyin("", last++, 1) != 1)
141 * Copy module-related data into the load area, where it can be
142 * used as a directory for loaded modules.
144 * Module data is presented in a self-describing format. Each datum
145 * is preceded by a 32-bit identifier and a 32-bit size field.
147 * Currently, the following data are saved:
149 * MOD_NAME (variable) module name (string)
150 * MOD_TYPE (variable) module type (string)
151 * MOD_ARGS (variable) module parameters (string)
152 * MOD_ADDR sizeof(vm_offset_t) module load address
153 * MOD_SIZE sizeof(size_t) module size
154 * MOD_METADATA (variable) type-specific metadata
156 #define COPY32(v, a, c) { \
159 archsw.arch_copyin(&x, a, sizeof(x)); \
163 #define MOD_STR(t, a, s, c) { \
165 COPY32(strlen(s) + 1, a, c); \
167 archsw.arch_copyin(s, a, strlen(s) + 1); \
168 a += roundup(strlen(s) + 1, sizeof(u_long)); \
171 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
172 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
173 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
175 #define MOD_VAR(t, a, s, c) { \
177 COPY32(sizeof(s), a, c); \
179 archsw.arch_copyin(&s, a, sizeof(s)); \
180 a += roundup(sizeof(s), sizeof(u_long)); \
183 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
184 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
186 #define MOD_METADATA(a, mm, c) { \
187 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
188 COPY32(mm->md_size, a, c); \
190 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
191 a += roundup(mm->md_size, sizeof(u_long)); \
194 #define MOD_END(a, c) { \
195 COPY32(MODINFO_END, a, c); \
200 bi_copymodules(vm_offset_t addr)
202 struct preloaded_file *fp;
203 struct file_metadata *md;
208 /* Start with the first module on the list, should be the kernel. */
209 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
210 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
211 MOD_TYPE(addr, fp->f_type, c);
213 MOD_ARGS(addr, fp->f_args, c);
215 MOD_ADDR(addr, v, c);
217 MOD_SIZE(addr, v, c);
218 for (md = fp->f_metadata; md != NULL; md = md->md_next)
219 if (!(md->md_type & MODINFOMD_NOCOPY))
220 MOD_METADATA(addr, md, c);
227 bi_load_efi_data(struct preloaded_file *kfp)
229 EFI_MEMORY_DESCRIPTOR *mm;
230 EFI_PHYSICAL_ADDRESS addr;
233 UINTN mmsz, pages, sz;
235 struct efi_map_header *efihdr;
238 if (efi_find_framebuffer(&efifb) == 0) {
239 printf("EFI framebuffer information:\n");
240 printf("addr, size 0x%lx, 0x%lx\n", efifb.fb_addr,
242 printf("dimensions %d x %d\n", efifb.fb_width,
244 printf("stride %d\n", efifb.fb_stride);
245 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
246 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
247 efifb.fb_mask_reserved);
249 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
252 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
255 * Allocate enough pages to hold the bootinfo block and the memory
256 * map EFI will return to us. The memory map has an unknown size,
257 * so we have to determine that first. Note that the AllocatePages
258 * call can itself modify the memory map, so we have to take that
259 * into account as well. The changes to the memory map are caused
260 * by splitting a range of free memory into two (AFAICT), so that
261 * one is marked as being loader data.
264 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
266 sz = (sz + 0xf) & ~0xf;
267 pages = EFI_SIZE_TO_PAGES(sz + efisz);
268 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, pages,
270 if (EFI_ERROR(status)) {
271 printf("%s: AllocatePages() returned 0x%lx\n", __func__,
277 * Read the memory map and stash it after bootinfo. Align the
278 * memory map on a 16-byte boundary (the bootinfo block is page
281 efihdr = (struct efi_map_header *)addr;
282 mm = (void *)((uint8_t *)efihdr + efisz);
283 sz = (EFI_PAGE_SIZE * pages) - efisz;
284 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
285 if (EFI_ERROR(status)) {
286 printf("%s: GetMemoryMap() returned 0x%lx\n", __func__,
291 efihdr->memory_size = sz;
292 efihdr->descriptor_size = mmsz;
293 efihdr->descriptor_version = mmver;
295 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz, efihdr);
301 * Load the information expected by an amd64 kernel.
303 * - The 'boothowto' argument is constructed.
304 * - The 'bootdev' argument is constructed.
305 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
306 * - The kernel environment is copied into kernel space.
307 * - Module metadata are formatted and placed in kernel space.
310 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
312 struct preloaded_file *xp, *kfp;
313 struct devdesc *rootdev;
314 struct file_metadata *md;
322 howto = bi_getboothowto(args);
325 * Allow the environment variable 'rootdev' to override the supplied
326 * device. This should perhaps go to MI code and/or have $rootdev
327 * tested/set by MI code before launching the kernel.
329 rootdevname = getenv("rootdev");
330 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
331 if (rootdev == NULL) {
332 printf("Can't determine root device.\n");
336 /* Try reading the /etc/fstab file to select the root device */
337 getrootmount(efi_fmtdev((void *)rootdev));
340 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
341 if (addr < (xp->f_addr + xp->f_size))
342 addr = xp->f_addr + xp->f_size;
345 /* Pad to a page boundary. */
346 addr = roundup(addr, PAGE_SIZE);
348 /* Copy our environment. */
350 addr = bi_copyenv(addr);
352 /* Pad to a page boundary. */
353 addr = roundup(addr, PAGE_SIZE);
355 kfp = file_findfile(NULL, "elf kernel");
357 kfp = file_findfile(NULL, "elf64 kernel");
359 panic("can't find kernel file");
360 kernend = 0; /* fill it in later */
361 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
362 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
363 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
365 bi_load_efi_data(kfp);
367 /* Figure out the size and location of the metadata. */
369 size = bi_copymodules(0);
370 kernend = roundup(addr + size, PAGE_SIZE);
373 /* patch MODINFOMD_KERNEND */
374 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
375 bcopy(&kernend, md->md_data, sizeof kernend);
377 /* Copy module list and metadata. */
378 (void)bi_copymodules(addr);