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>
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;
63 static const char howto_switches[] = "aCdrgDmphsv";
64 static int howto_masks[] = {
65 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE,
66 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE
70 bi_getboothowto(char *kargs)
79 /* Get the boot options from the environment first. */
80 for (i = 0; howto_names[i].ev != NULL; i++) {
81 if (getenv(howto_names[i].ev) != NULL)
82 howto |= howto_names[i].mask;
85 console = getenv("console");
86 if (console != NULL) {
87 if (strcmp(console, "comconsole") == 0)
89 if (strcmp(console, "nullconsole") == 0)
97 opts = strchr(kargs, '-');
98 while (opts != NULL) {
99 while (*(++opts) != '\0') {
100 sw = strchr(howto_switches, *opts);
103 howto |= howto_masks[sw - howto_switches];
105 opts = strchr(opts, '-');
112 * Copy the environment into the load area starting at (addr).
113 * Each variable is formatted as <name>=<value>, with a single nul
114 * separating each variable, and a double nul terminating the environment.
117 bi_copyenv(vm_offset_t start)
120 vm_offset_t addr, last;
125 /* Traverse the environment. */
126 for (ep = environ; ep != NULL; ep = ep->ev_next) {
127 len = strlen(ep->ev_name);
128 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len)
131 if (archsw.arch_copyin("=", addr, 1) != 1)
134 if (ep->ev_value != NULL) {
135 len = strlen(ep->ev_value);
136 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len)
140 if (archsw.arch_copyin("", addr, 1) != 1)
145 if (archsw.arch_copyin("", last++, 1) != 1)
151 * Copy module-related data into the load area, where it can be
152 * used as a directory for loaded modules.
154 * Module data is presented in a self-describing format. Each datum
155 * is preceded by a 32-bit identifier and a 32-bit size field.
157 * Currently, the following data are saved:
159 * MOD_NAME (variable) module name (string)
160 * MOD_TYPE (variable) module type (string)
161 * MOD_ARGS (variable) module parameters (string)
162 * MOD_ADDR sizeof(vm_offset_t) module load address
163 * MOD_SIZE sizeof(size_t) module size
164 * MOD_METADATA (variable) type-specific metadata
166 #define COPY32(v, a, c) { \
169 archsw.arch_copyin(&x, a, sizeof(x)); \
173 #define MOD_STR(t, a, s, c) { \
175 COPY32(strlen(s) + 1, a, c); \
177 archsw.arch_copyin(s, a, strlen(s) + 1); \
178 a += roundup(strlen(s) + 1, sizeof(u_long)); \
181 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c)
182 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c)
183 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c)
185 #define MOD_VAR(t, a, s, c) { \
187 COPY32(sizeof(s), a, c); \
189 archsw.arch_copyin(&s, a, sizeof(s)); \
190 a += roundup(sizeof(s), sizeof(u_long)); \
193 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c)
194 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c)
196 #define MOD_METADATA(a, mm, c) { \
197 COPY32(MODINFO_METADATA | mm->md_type, a, c); \
198 COPY32(mm->md_size, a, c); \
200 archsw.arch_copyin(mm->md_data, a, mm->md_size); \
201 a += roundup(mm->md_size, sizeof(u_long)); \
204 #define MOD_END(a, c) { \
205 COPY32(MODINFO_END, a, c); \
210 bi_copymodules(vm_offset_t addr)
212 struct preloaded_file *fp;
213 struct file_metadata *md;
218 /* Start with the first module on the list, should be the kernel. */
219 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) {
220 MOD_NAME(addr, fp->f_name, c); /* This must come first. */
221 MOD_TYPE(addr, fp->f_type, c);
223 MOD_ARGS(addr, fp->f_args, c);
226 v -= __elfN(relocation_offset);
228 MOD_ADDR(addr, v, c);
230 MOD_SIZE(addr, v, c);
231 for (md = fp->f_metadata; md != NULL; md = md->md_next)
232 if (!(md->md_type & MODINFOMD_NOCOPY))
233 MOD_METADATA(addr, md, c);
240 bi_load_efi_data(struct preloaded_file *kfp)
242 EFI_MEMORY_DESCRIPTOR *mm;
243 EFI_PHYSICAL_ADDRESS addr;
247 UINTN mmsz, pages, retry, sz;
249 struct efi_map_header *efihdr;
251 #if defined(__amd64__) || defined(__aarch64__)
254 if (efi_find_framebuffer(&efifb) == 0) {
255 printf("EFI framebuffer information:\n");
256 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr,
258 printf("dimensions %d x %d\n", efifb.fb_width,
260 printf("stride %d\n", efifb.fb_stride);
261 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n",
262 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue,
263 efifb.fb_mask_reserved);
265 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb);
269 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
272 * Assgin size of EFI_MEMORY_DESCRIPTOR to keep compatible with
273 * u-boot which doesn't fill this value when buffer for memory
274 * descriptors is too small (eg. 0 to obtain memory map size)
276 mmsz = sizeof(EFI_MEMORY_DESCRIPTOR);
279 * It is possible that the first call to ExitBootServices may change
280 * the map key. Fetch a new map key and retry ExitBootServices in that
283 for (retry = 2; retry > 0; retry--) {
285 * Allocate enough pages to hold the bootinfo block and the
286 * memory map EFI will return to us. The memory map has an
287 * unknown size, so we have to determine that first. Note that
288 * the AllocatePages call can itself modify the memory map, so
289 * we have to take that into account as well. The changes to
290 * the memory map are caused by splitting a range of free
291 * memory into two (AFAICT), so that one is marked as being
295 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver);
297 sz = (sz + 0xf) & ~0xf;
298 pages = EFI_SIZE_TO_PAGES(sz + efisz);
299 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData,
301 if (EFI_ERROR(status)) {
302 printf("%s: AllocatePages error %lu\n", __func__,
303 EFI_ERROR_CODE(status));
308 * Read the memory map and stash it after bootinfo. Align the
309 * memory map on a 16-byte boundary (the bootinfo block is page
312 efihdr = (struct efi_map_header *)addr;
313 mm = (void *)((uint8_t *)efihdr + efisz);
314 sz = (EFI_PAGE_SIZE * pages) - efisz;
316 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver);
317 if (EFI_ERROR(status)) {
318 printf("%s: GetMemoryMap error %lu\n", __func__,
319 EFI_ERROR_CODE(status));
322 status = BS->ExitBootServices(IH, efi_mapkey);
323 if (EFI_ERROR(status) == 0) {
324 efihdr->memory_size = sz;
325 efihdr->descriptor_size = mmsz;
326 efihdr->descriptor_version = mmver;
327 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz,
331 BS->FreePages(addr, pages);
333 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status));
338 * Load the information expected by an amd64 kernel.
340 * - The 'boothowto' argument is constructed.
341 * - The 'bootdev' argument is constructed.
342 * - The 'bootinfo' struct is constructed, and copied into the kernel space.
343 * - The kernel environment is copied into kernel space.
344 * - Module metadata are formatted and placed in kernel space.
347 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp)
349 struct preloaded_file *xp, *kfp;
350 struct devdesc *rootdev;
351 struct file_metadata *md;
358 #if defined(LOADER_FDT_SUPPORT)
366 * These metadata addreses must be converted for kernel after
370 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND,
372 #if defined(LOADER_FDT_SUPPORT)
378 howto = bi_getboothowto(args);
381 * Allow the environment variable 'rootdev' to override the supplied
382 * device. This should perhaps go to MI code and/or have $rootdev
383 * tested/set by MI code before launching the kernel.
385 rootdevname = getenv("rootdev");
386 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL);
387 if (rootdev == NULL) {
388 printf("Can't determine root device.\n");
392 /* Try reading the /etc/fstab file to select the root device */
393 getrootmount(efi_fmtdev((void *)rootdev));
396 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
397 if (addr < (xp->f_addr + xp->f_size))
398 addr = xp->f_addr + xp->f_size;
401 /* Pad to a page boundary. */
402 addr = roundup(addr, PAGE_SIZE);
404 /* Copy our environment. */
406 addr = bi_copyenv(addr);
408 /* Pad to a page boundary. */
409 addr = roundup(addr, PAGE_SIZE);
411 #if defined(LOADER_FDT_SUPPORT)
412 /* Handle device tree blob */
414 dtb_size = fdt_copy(addr);
416 /* Pad to a page boundary */
418 addr += roundup(dtb_size, PAGE_SIZE);
421 kfp = file_findfile(NULL, "elf kernel");
423 kfp = file_findfile(NULL, "elf64 kernel");
425 panic("can't find kernel file");
426 kernend = 0; /* fill it in later */
427 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto);
428 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp);
429 #if defined(LOADER_FDT_SUPPORT)
431 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof dtbp, &dtbp);
433 printf("WARNING! Trying to fire up the kernel, but no "
434 "device tree blob found!\n");
436 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend);
437 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof ST, &ST);
439 bi_load_efi_data(kfp);
441 /* Figure out the size and location of the metadata. */
443 size = bi_copymodules(0);
444 kernend = roundup(addr + size, PAGE_SIZE);
447 /* patch MODINFOMD_KERNEND */
448 md = file_findmetadata(kfp, MODINFOMD_KERNEND);
449 bcopy(&kernend, md->md_data, sizeof kernend);
452 *modulep -= __elfN(relocation_offset);
454 /* Do relocation fixup on metadata of each module. */
455 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) {
456 for (i = 0; i < nitems(mdt); i++) {
457 md = file_findmetadata(xp, mdt[i]);
459 bcopy(md->md_data, &vaddr, sizeof vaddr);
460 vaddr -= __elfN(relocation_offset);
461 bcopy(&vaddr, md->md_data, sizeof vaddr);
467 /* Copy module list and metadata. */
468 (void)bi_copymodules(addr);