bootinfo.c revision 293304
1/*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * Copyright (c) 2004, 2006 Marcel Moolenaar 4 * Copyright (c) 2014 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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. 15 * 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 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: stable/10/sys/boot/efi/loader/bootinfo.c 293304 2016-01-07 04:02:37Z emaste $"); 31 32#include <stand.h> 33#include <string.h> 34#include <sys/param.h> 35#include <sys/reboot.h> 36#include <sys/linker.h> 37#include <sys/boot.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> 43 44#include <efi.h> 45#include <efilib.h> 46 47#include "bootstrap.h" 48#include "framebuffer.h" 49#include "loader_efi.h" 50 51static const char howto_switches[] = "aCdrgDmphsv"; 52static int howto_masks[] = { 53 RB_ASKNAME, RB_CDROM, RB_KDB, RB_DFLTROOT, RB_GDB, RB_MULTIPLE, 54 RB_MUTE, RB_PAUSE, RB_SERIAL, RB_SINGLE, RB_VERBOSE 55}; 56 57static int 58bi_getboothowto(char *kargs) 59{ 60 const char *sw; 61 char *opts; 62 char *console; 63 int howto, i; 64 65 howto = 0; 66 67 /* Get the boot options from the environment first. */ 68 for (i = 0; howto_names[i].ev != NULL; i++) { 69 if (getenv(howto_names[i].ev) != NULL) 70 howto |= howto_names[i].mask; 71 } 72 73 console = getenv("console"); 74 if (console != NULL) { 75 if (strcmp(console, "comconsole") == 0) 76 howto |= RB_SERIAL; 77 if (strcmp(console, "nullconsole") == 0) 78 howto |= RB_MUTE; 79 } 80 81 /* Parse kargs */ 82 if (kargs == NULL) 83 return (howto); 84 85 opts = strchr(kargs, '-'); 86 while (opts != NULL) { 87 while (*(++opts) != '\0') { 88 sw = strchr(howto_switches, *opts); 89 if (sw == NULL) 90 break; 91 howto |= howto_masks[sw - howto_switches]; 92 } 93 opts = strchr(opts, '-'); 94 } 95 96 return (howto); 97} 98 99/* 100 * Copy the environment into the load area starting at (addr). 101 * Each variable is formatted as <name>=<value>, with a single nul 102 * separating each variable, and a double nul terminating the environment. 103 */ 104static vm_offset_t 105bi_copyenv(vm_offset_t start) 106{ 107 struct env_var *ep; 108 vm_offset_t addr, last; 109 size_t len; 110 111 addr = last = start; 112 113 /* Traverse the environment. */ 114 for (ep = environ; ep != NULL; ep = ep->ev_next) { 115 len = strlen(ep->ev_name); 116 if (archsw.arch_copyin(ep->ev_name, addr, len) != len) 117 break; 118 addr += len; 119 if (archsw.arch_copyin("=", addr, 1) != 1) 120 break; 121 addr++; 122 if (ep->ev_value != NULL) { 123 len = strlen(ep->ev_value); 124 if (archsw.arch_copyin(ep->ev_value, addr, len) != len) 125 break; 126 addr += len; 127 } 128 if (archsw.arch_copyin("", addr, 1) != 1) 129 break; 130 last = ++addr; 131 } 132 133 if (archsw.arch_copyin("", last++, 1) != 1) 134 last = start; 135 return(last); 136} 137 138/* 139 * Copy module-related data into the load area, where it can be 140 * used as a directory for loaded modules. 141 * 142 * Module data is presented in a self-describing format. Each datum 143 * is preceded by a 32-bit identifier and a 32-bit size field. 144 * 145 * Currently, the following data are saved: 146 * 147 * MOD_NAME (variable) module name (string) 148 * MOD_TYPE (variable) module type (string) 149 * MOD_ARGS (variable) module parameters (string) 150 * MOD_ADDR sizeof(vm_offset_t) module load address 151 * MOD_SIZE sizeof(size_t) module size 152 * MOD_METADATA (variable) type-specific metadata 153 */ 154#define COPY32(v, a, c) { \ 155 uint32_t x = (v); \ 156 if (c) \ 157 archsw.arch_copyin(&x, a, sizeof(x)); \ 158 a += sizeof(x); \ 159} 160 161#define MOD_STR(t, a, s, c) { \ 162 COPY32(t, a, c); \ 163 COPY32(strlen(s) + 1, a, c); \ 164 if (c) \ 165 archsw.arch_copyin(s, a, strlen(s) + 1); \ 166 a += roundup(strlen(s) + 1, sizeof(u_long)); \ 167} 168 169#define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c) 170#define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c) 171#define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c) 172 173#define MOD_VAR(t, a, s, c) { \ 174 COPY32(t, a, c); \ 175 COPY32(sizeof(s), a, c); \ 176 if (c) \ 177 archsw.arch_copyin(&s, a, sizeof(s)); \ 178 a += roundup(sizeof(s), sizeof(u_long)); \ 179} 180 181#define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c) 182#define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c) 183 184#define MOD_METADATA(a, mm, c) { \ 185 COPY32(MODINFO_METADATA | mm->md_type, a, c); \ 186 COPY32(mm->md_size, a, c); \ 187 if (c) \ 188 archsw.arch_copyin(mm->md_data, a, mm->md_size); \ 189 a += roundup(mm->md_size, sizeof(u_long)); \ 190} 191 192#define MOD_END(a, c) { \ 193 COPY32(MODINFO_END, a, c); \ 194 COPY32(0, a, c); \ 195} 196 197static vm_offset_t 198bi_copymodules(vm_offset_t addr) 199{ 200 struct preloaded_file *fp; 201 struct file_metadata *md; 202 int c; 203 uint64_t v; 204 205 c = addr != 0; 206 /* Start with the first module on the list, should be the kernel. */ 207 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) { 208 MOD_NAME(addr, fp->f_name, c); /* This must come first. */ 209 MOD_TYPE(addr, fp->f_type, c); 210 if (fp->f_args) 211 MOD_ARGS(addr, fp->f_args, c); 212 v = fp->f_addr; 213 MOD_ADDR(addr, v, c); 214 v = fp->f_size; 215 MOD_SIZE(addr, v, c); 216 for (md = fp->f_metadata; md != NULL; md = md->md_next) 217 if (!(md->md_type & MODINFOMD_NOCOPY)) 218 MOD_METADATA(addr, md, c); 219 } 220 MOD_END(addr, c); 221 return(addr); 222} 223 224static int 225bi_load_efi_data(struct preloaded_file *kfp) 226{ 227 EFI_MEMORY_DESCRIPTOR *mm; 228 EFI_PHYSICAL_ADDRESS addr; 229 EFI_STATUS status; 230 size_t efisz; 231 UINTN efi_mapkey; 232 UINTN mmsz, pages, retry, sz; 233 UINT32 mmver; 234 struct efi_map_header *efihdr; 235 struct efi_fb efifb; 236 237 if (efi_find_framebuffer(&efifb) == 0) { 238 printf("EFI framebuffer information:\n"); 239 printf("addr, size 0x%lx, 0x%lx\n", efifb.fb_addr, 240 efifb.fb_size); 241 printf("dimensions %d x %d\n", efifb.fb_width, 242 efifb.fb_height); 243 printf("stride %d\n", efifb.fb_stride); 244 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n", 245 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue, 246 efifb.fb_mask_reserved); 247 248 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb); 249 } 250 251 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 252 253 /* 254 * It is possible that the first call to ExitBootServices may change 255 * the map key. Fetch a new map key and retry ExitBootServices in that 256 * case. 257 */ 258 for (retry = 2; retry > 0; retry--) { 259 /* 260 * Allocate enough pages to hold the bootinfo block and the 261 * memory map EFI will return to us. The memory map has an 262 * unknown size, so we have to determine that first. Note that 263 * the AllocatePages call can itself modify the memory map, so 264 * we have to take that into account as well. The changes to 265 * the memory map are caused by splitting a range of free 266 * memory into two (AFAICT), so that one is marked as being 267 * loader data. 268 */ 269 sz = 0; 270 BS->GetMemoryMap(&sz, NULL, &efi_mapkey, &mmsz, &mmver); 271 sz += mmsz; 272 sz = (sz + 0xf) & ~0xf; 273 pages = EFI_SIZE_TO_PAGES(sz + efisz); 274 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 275 pages, &addr); 276 if (EFI_ERROR(status)) { 277 printf("%s: AllocatePages error %lu\n", __func__, 278 (unsigned long)(status & ~EFI_ERROR_MASK)); 279 return (ENOMEM); 280 } 281 282 /* 283 * Read the memory map and stash it after bootinfo. Align the 284 * memory map on a 16-byte boundary (the bootinfo block is page 285 * aligned). 286 */ 287 efihdr = (struct efi_map_header *)addr; 288 mm = (void *)((uint8_t *)efihdr + efisz); 289 sz = (EFI_PAGE_SIZE * pages) - efisz; 290 291 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &mmsz, &mmver); 292 if (EFI_ERROR(status)) { 293 printf("%s: GetMemoryMap error %lu\n", __func__, 294 (unsigned long)(status & ~EFI_ERROR_MASK)); 295 return (EINVAL); 296 } 297 status = BS->ExitBootServices(IH, efi_mapkey); 298 if (EFI_ERROR(status) == 0) { 299 efihdr->memory_size = sz; 300 efihdr->descriptor_size = mmsz; 301 efihdr->descriptor_version = mmver; 302 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz, 303 efihdr); 304 return (0); 305 } 306 BS->FreePages(addr, pages); 307 } 308 printf("ExitBootServices error %lu\n", 309 (unsigned long)(status & ~EFI_ERROR_MASK)); 310 return (EINVAL); 311} 312 313/* 314 * Load the information expected by an amd64 kernel. 315 * 316 * - The 'boothowto' argument is constructed. 317 * - The 'bootdev' argument is constructed. 318 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 319 * - The kernel environment is copied into kernel space. 320 * - Module metadata are formatted and placed in kernel space. 321 */ 322int 323bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp) 324{ 325 struct preloaded_file *xp, *kfp; 326 struct devdesc *rootdev; 327 struct file_metadata *md; 328 vm_offset_t addr; 329 uint64_t kernend; 330 uint64_t envp; 331 vm_offset_t size; 332 char *rootdevname; 333 int howto; 334 335 howto = bi_getboothowto(args); 336 337 /* 338 * Allow the environment variable 'rootdev' to override the supplied 339 * device. This should perhaps go to MI code and/or have $rootdev 340 * tested/set by MI code before launching the kernel. 341 */ 342 rootdevname = getenv("rootdev"); 343 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL); 344 if (rootdev == NULL) { 345 printf("Can't determine root device.\n"); 346 return(EINVAL); 347 } 348 349 /* Try reading the /etc/fstab file to select the root device */ 350 getrootmount(efi_fmtdev((void *)rootdev)); 351 352 addr = 0; 353 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 354 if (addr < (xp->f_addr + xp->f_size)) 355 addr = xp->f_addr + xp->f_size; 356 } 357 358 /* Pad to a page boundary. */ 359 addr = roundup(addr, PAGE_SIZE); 360 361 /* Copy our environment. */ 362 envp = addr; 363 addr = bi_copyenv(addr); 364 365 /* Pad to a page boundary. */ 366 addr = roundup(addr, PAGE_SIZE); 367 368 kfp = file_findfile(NULL, "elf kernel"); 369 if (kfp == NULL) 370 kfp = file_findfile(NULL, "elf64 kernel"); 371 if (kfp == NULL) 372 panic("can't find kernel file"); 373 kernend = 0; /* fill it in later */ 374 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof howto, &howto); 375 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof envp, &envp); 376 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof kernend, &kernend); 377 378 bi_load_efi_data(kfp); 379 380 /* Figure out the size and location of the metadata. */ 381 *modulep = addr; 382 size = bi_copymodules(0); 383 kernend = roundup(addr + size, PAGE_SIZE); 384 *kernendp = kernend; 385 386 /* patch MODINFOMD_KERNEND */ 387 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 388 bcopy(&kernend, md->md_data, sizeof kernend); 389 390 /* Copy module list and metadata. */ 391 (void)bi_copymodules(addr); 392 393 return (0); 394} 395