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$"); 31 32 #include <stand.h> 33 #include <string.h> 34 #include <sys/param.h> 35 #include <sys/linker.h> 36 #include <sys/reboot.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 43 #include <efi.h> 44 #include <efilib.h> 45 46 #include "bootstrap.h" 47 #include "loader_efi.h" 48 49 #if defined(__amd64__) 50 #include <machine/specialreg.h> 51 #endif 52 53 #include "gfx_fb.h" 54 55 #if defined(LOADER_FDT_SUPPORT) 56 #include <fdt_platform.h> 57 #endif 58 59 #ifdef LOADER_GELI_SUPPORT 60 #include "geliboot.h" 61 #endif 62 63 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, 64 bool exit_bs); 65 66 static int 67 bi_getboothowto(char *kargs) 68 { 69 const char *sw, *tmp; 70 char *opts; 71 char *console; 72 int howto, speed, port; 73 char buf[50]; 74 75 howto = boot_parse_cmdline(kargs); 76 howto |= boot_env_to_howto(); 77 78 console = getenv("console"); 79 if (console != NULL) { 80 if (strcmp(console, "comconsole") == 0) 81 howto |= RB_SERIAL; 82 if (strcmp(console, "nullconsole") == 0) 83 howto |= RB_MUTE; 84 #if defined(__i386__) || defined(__amd64__) 85 if (strcmp(console, "efi") == 0 && 86 getenv("efi_8250_uid") != NULL && 87 getenv("hw.uart.console") == NULL) { 88 /* 89 * If we found a 8250 com port and com speed, we need to 90 * tell the kernel where the serial port is, and how 91 * fast. Ideally, we'd get the port from ACPI, but that 92 * isn't running in the loader. Do the next best thing 93 * by allowing it to be set by a loader.conf variable, 94 * either a EFI specific one, or the compatible 95 * comconsole_port if not. PCI support is needed, but 96 * for that we'd ideally refactor the 97 * libi386/comconsole.c code to have identical behavior. 98 * We only try to set the port for cases where we saw 99 * the Serial(x) node when parsing, otherwise 100 * specialized hardware that has Uart nodes will have a 101 * bogus address set. 102 * But if someone specifically setup hw.uart.console, 103 * don't override that. 104 */ 105 speed = -1; 106 port = -1; 107 tmp = getenv("efi_com_speed"); 108 if (tmp != NULL) 109 speed = strtol(tmp, NULL, 0); 110 tmp = getenv("efi_com_port"); 111 if (tmp == NULL) 112 tmp = getenv("comconsole_port"); 113 if (tmp != NULL) 114 port = strtol(tmp, NULL, 0); 115 if (speed != -1 && port != -1) { 116 snprintf(buf, sizeof(buf), "io:%d,br:%d", port, 117 speed); 118 env_setenv("hw.uart.console", EV_VOLATILE, buf, 119 NULL, NULL); 120 } 121 } 122 #endif 123 } 124 125 return (howto); 126 } 127 128 /* 129 * Copy the environment into the load area starting at (addr). 130 * Each variable is formatted as <name>=<value>, with a single nul 131 * separating each variable, and a double nul terminating the environment. 132 */ 133 static vm_offset_t 134 bi_copyenv(vm_offset_t start) 135 { 136 struct env_var *ep; 137 vm_offset_t addr, last; 138 size_t len; 139 140 addr = last = start; 141 142 /* Traverse the environment. */ 143 for (ep = environ; ep != NULL; ep = ep->ev_next) { 144 len = strlen(ep->ev_name); 145 if ((size_t)archsw.arch_copyin(ep->ev_name, addr, len) != len) 146 break; 147 addr += len; 148 if (archsw.arch_copyin("=", addr, 1) != 1) 149 break; 150 addr++; 151 if (ep->ev_value != NULL) { 152 len = strlen(ep->ev_value); 153 if ((size_t)archsw.arch_copyin(ep->ev_value, addr, len) != len) 154 break; 155 addr += len; 156 } 157 if (archsw.arch_copyin("", addr, 1) != 1) 158 break; 159 last = ++addr; 160 } 161 162 if (archsw.arch_copyin("", last++, 1) != 1) 163 last = start; 164 return(last); 165 } 166 167 /* 168 * Copy module-related data into the load area, where it can be 169 * used as a directory for loaded modules. 170 * 171 * Module data is presented in a self-describing format. Each datum 172 * is preceded by a 32-bit identifier and a 32-bit size field. 173 * 174 * Currently, the following data are saved: 175 * 176 * MOD_NAME (variable) module name (string) 177 * MOD_TYPE (variable) module type (string) 178 * MOD_ARGS (variable) module parameters (string) 179 * MOD_ADDR sizeof(vm_offset_t) module load address 180 * MOD_SIZE sizeof(size_t) module size 181 * MOD_METADATA (variable) type-specific metadata 182 */ 183 #define COPY32(v, a, c) { \ 184 uint32_t x = (v); \ 185 if (c) \ 186 archsw.arch_copyin(&x, a, sizeof(x)); \ 187 a += sizeof(x); \ 188 } 189 190 #define MOD_STR(t, a, s, c) { \ 191 COPY32(t, a, c); \ 192 COPY32(strlen(s) + 1, a, c); \ 193 if (c) \ 194 archsw.arch_copyin(s, a, strlen(s) + 1); \ 195 a += roundup(strlen(s) + 1, sizeof(u_long)); \ 196 } 197 198 #define MOD_NAME(a, s, c) MOD_STR(MODINFO_NAME, a, s, c) 199 #define MOD_TYPE(a, s, c) MOD_STR(MODINFO_TYPE, a, s, c) 200 #define MOD_ARGS(a, s, c) MOD_STR(MODINFO_ARGS, a, s, c) 201 202 #define MOD_VAR(t, a, s, c) { \ 203 COPY32(t, a, c); \ 204 COPY32(sizeof(s), a, c); \ 205 if (c) \ 206 archsw.arch_copyin(&s, a, sizeof(s)); \ 207 a += roundup(sizeof(s), sizeof(u_long)); \ 208 } 209 210 #define MOD_ADDR(a, s, c) MOD_VAR(MODINFO_ADDR, a, s, c) 211 #define MOD_SIZE(a, s, c) MOD_VAR(MODINFO_SIZE, a, s, c) 212 213 #define MOD_METADATA(a, mm, c) { \ 214 COPY32(MODINFO_METADATA | mm->md_type, a, c); \ 215 COPY32(mm->md_size, a, c); \ 216 if (c) \ 217 archsw.arch_copyin(mm->md_data, a, mm->md_size); \ 218 a += roundup(mm->md_size, sizeof(u_long)); \ 219 } 220 221 #define MOD_END(a, c) { \ 222 COPY32(MODINFO_END, a, c); \ 223 COPY32(0, a, c); \ 224 } 225 226 static vm_offset_t 227 bi_copymodules(vm_offset_t addr) 228 { 229 struct preloaded_file *fp; 230 struct file_metadata *md; 231 int c; 232 uint64_t v; 233 234 c = addr != 0; 235 /* Start with the first module on the list, should be the kernel. */ 236 for (fp = file_findfile(NULL, NULL); fp != NULL; fp = fp->f_next) { 237 MOD_NAME(addr, fp->f_name, c); /* This must come first. */ 238 MOD_TYPE(addr, fp->f_type, c); 239 if (fp->f_args) 240 MOD_ARGS(addr, fp->f_args, c); 241 v = fp->f_addr; 242 #if defined(__arm__) 243 v -= __elfN(relocation_offset); 244 #endif 245 MOD_ADDR(addr, v, c); 246 v = fp->f_size; 247 MOD_SIZE(addr, v, c); 248 for (md = fp->f_metadata; md != NULL; md = md->md_next) 249 if (!(md->md_type & MODINFOMD_NOCOPY)) 250 MOD_METADATA(addr, md, c); 251 } 252 MOD_END(addr, c); 253 return(addr); 254 } 255 256 static EFI_STATUS 257 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver) 258 { 259 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap; 260 EFI_STATUS ret; 261 int curr, ndesc, nset; 262 263 nset = 0; 264 desc = mm; 265 ndesc = sz / mmsz; 266 vmap = malloc(sz); 267 if (vmap == NULL) 268 /* This isn't really an EFI error case, but pretend it is */ 269 return (EFI_OUT_OF_RESOURCES); 270 viter = vmap; 271 for (curr = 0; curr < ndesc; 272 curr++, desc = NextMemoryDescriptor(desc, mmsz)) { 273 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) { 274 ++nset; 275 desc->VirtualStart = desc->PhysicalStart; 276 *viter = *desc; 277 viter = NextMemoryDescriptor(viter, mmsz); 278 } 279 } 280 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap); 281 free(vmap); 282 return (ret); 283 } 284 285 static int 286 bi_load_efi_data(struct preloaded_file *kfp, bool exit_bs) 287 { 288 EFI_MEMORY_DESCRIPTOR *mm; 289 EFI_PHYSICAL_ADDRESS addr = 0; 290 EFI_STATUS status; 291 const char *efi_novmap; 292 size_t efisz; 293 UINTN efi_mapkey; 294 UINTN dsz, pages, retry, sz; 295 UINT32 mmver; 296 struct efi_map_header *efihdr; 297 bool do_vmap; 298 299 #if defined(__amd64__) || defined(__aarch64__) 300 struct efi_fb efifb; 301 302 efifb.fb_addr = gfx_state.tg_fb.fb_addr; 303 efifb.fb_size = gfx_state.tg_fb.fb_size; 304 efifb.fb_height = gfx_state.tg_fb.fb_height; 305 efifb.fb_width = gfx_state.tg_fb.fb_width; 306 efifb.fb_stride = gfx_state.tg_fb.fb_stride; 307 efifb.fb_mask_red = gfx_state.tg_fb.fb_mask_red; 308 efifb.fb_mask_green = gfx_state.tg_fb.fb_mask_green; 309 efifb.fb_mask_blue = gfx_state.tg_fb.fb_mask_blue; 310 efifb.fb_mask_reserved = gfx_state.tg_fb.fb_mask_reserved; 311 312 printf("EFI framebuffer information:\n"); 313 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr, efifb.fb_size); 314 printf("dimensions %d x %d\n", efifb.fb_width, efifb.fb_height); 315 printf("stride %d\n", efifb.fb_stride); 316 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n", 317 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue, 318 efifb.fb_mask_reserved); 319 320 if (efifb.fb_addr != 0) 321 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb); 322 #endif 323 324 do_vmap = true; 325 efi_novmap = getenv("efi_disable_vmap"); 326 if (efi_novmap != NULL) 327 do_vmap = strcasecmp(efi_novmap, "YES") != 0; 328 329 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 330 331 /* 332 * Assign size of EFI_MEMORY_DESCRIPTOR to keep compatible with 333 * u-boot which doesn't fill this value when buffer for memory 334 * descriptors is too small (eg. 0 to obtain memory map size) 335 */ 336 dsz = sizeof(EFI_MEMORY_DESCRIPTOR); 337 338 /* 339 * Allocate enough pages to hold the bootinfo block and the 340 * memory map EFI will return to us. The memory map has an 341 * unknown size, so we have to determine that first. Note that 342 * the AllocatePages call can itself modify the memory map, so 343 * we have to take that into account as well. The changes to 344 * the memory map are caused by splitting a range of free 345 * memory into two, so that one is marked as being loader 346 * data. 347 */ 348 349 sz = 0; 350 mm = NULL; 351 352 /* 353 * Matthew Garrett has observed at least one system changing the 354 * memory map when calling ExitBootServices, causing it to return an 355 * error, probably because callbacks are allocating memory. 356 * So we need to retry calling it at least once. 357 */ 358 for (retry = 2; retry > 0; retry--) { 359 for (;;) { 360 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &dsz, &mmver); 361 if (!EFI_ERROR(status)) 362 break; 363 364 if (status != EFI_BUFFER_TOO_SMALL) { 365 printf("%s: GetMemoryMap error %lu\n", __func__, 366 EFI_ERROR_CODE(status)); 367 return (EINVAL); 368 } 369 370 if (addr != 0) 371 BS->FreePages(addr, pages); 372 373 /* Add 10 descriptors to the size to allow for 374 * fragmentation caused by calling AllocatePages */ 375 sz += (10 * dsz); 376 pages = EFI_SIZE_TO_PAGES(sz + efisz); 377 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 378 pages, &addr); 379 if (EFI_ERROR(status)) { 380 printf("%s: AllocatePages error %lu\n", __func__, 381 EFI_ERROR_CODE(status)); 382 return (ENOMEM); 383 } 384 385 /* 386 * Read the memory map and stash it after bootinfo. Align the 387 * memory map on a 16-byte boundary (the bootinfo block is page 388 * aligned). 389 */ 390 efihdr = (struct efi_map_header *)(uintptr_t)addr; 391 mm = (void *)((uint8_t *)efihdr + efisz); 392 sz = (EFI_PAGE_SIZE * pages) - efisz; 393 } 394 395 if (!exit_bs) 396 break; 397 status = efi_exit_boot_services(efi_mapkey); 398 if (!EFI_ERROR(status)) 399 break; 400 } 401 402 if (retry == 0) { 403 BS->FreePages(addr, pages); 404 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status)); 405 return (EINVAL); 406 } 407 408 /* 409 * This may be disabled by setting efi_disable_vmap in 410 * loader.conf(5). By default we will setup the virtual 411 * map entries. 412 */ 413 414 if (do_vmap) 415 efi_do_vmap(mm, sz, dsz, mmver); 416 efihdr->memory_size = sz; 417 efihdr->descriptor_size = dsz; 418 efihdr->descriptor_version = mmver; 419 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz, 420 efihdr); 421 422 return (0); 423 } 424 425 /* 426 * Load the information expected by an amd64 kernel. 427 * 428 * - The 'boothowto' argument is constructed. 429 * - The 'bootdev' argument is constructed. 430 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 431 * - The kernel environment is copied into kernel space. 432 * - Module metadata are formatted and placed in kernel space. 433 */ 434 int 435 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs) 436 { 437 struct preloaded_file *xp, *kfp; 438 struct devdesc *rootdev; 439 struct file_metadata *md; 440 vm_offset_t addr; 441 uint64_t kernend, module; 442 uint64_t envp; 443 vm_offset_t size; 444 char *rootdevname; 445 int howto; 446 #if defined(LOADER_FDT_SUPPORT) 447 vm_offset_t dtbp; 448 int dtb_size; 449 #endif 450 #if defined(__arm__) 451 vm_offset_t vaddr; 452 size_t i; 453 /* 454 * These metadata addreses must be converted for kernel after 455 * relocation. 456 */ 457 uint32_t mdt[] = { 458 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND, 459 MODINFOMD_ENVP, MODINFOMD_FONT, 460 #if defined(LOADER_FDT_SUPPORT) 461 MODINFOMD_DTBP 462 #endif 463 }; 464 #endif 465 466 howto = bi_getboothowto(args); 467 468 /* 469 * Allow the environment variable 'rootdev' to override the supplied 470 * device. This should perhaps go to MI code and/or have $rootdev 471 * tested/set by MI code before launching the kernel. 472 */ 473 rootdevname = getenv("rootdev"); 474 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL); 475 if (rootdev == NULL) { 476 printf("Can't determine root device.\n"); 477 return(EINVAL); 478 } 479 480 /* Try reading the /etc/fstab file to select the root device */ 481 getrootmount(efi_fmtdev((void *)rootdev)); 482 483 addr = 0; 484 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 485 if (addr < (xp->f_addr + xp->f_size)) 486 addr = xp->f_addr + xp->f_size; 487 } 488 489 /* Pad to a page boundary. */ 490 addr = roundup(addr, PAGE_SIZE); 491 492 addr = build_font_module(addr); 493 494 /* Pad to a page boundary. */ 495 addr = roundup(addr, PAGE_SIZE); 496 497 /* Copy our environment. */ 498 envp = addr; 499 addr = bi_copyenv(addr); 500 501 /* Pad to a page boundary. */ 502 addr = roundup(addr, PAGE_SIZE); 503 504 #if defined(LOADER_FDT_SUPPORT) 505 /* Handle device tree blob */ 506 dtbp = addr; 507 dtb_size = fdt_copy(addr); 508 509 /* Pad to a page boundary */ 510 if (dtb_size) 511 addr += roundup(dtb_size, PAGE_SIZE); 512 #endif 513 514 kfp = file_findfile(NULL, "elf kernel"); 515 if (kfp == NULL) 516 kfp = file_findfile(NULL, "elf64 kernel"); 517 if (kfp == NULL) 518 panic("can't find kernel file"); 519 kernend = 0; /* fill it in later */ 520 521 /* Figure out the size and location of the metadata. */ 522 module = *modulep = addr; 523 524 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof(howto), &howto); 525 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof(envp), &envp); 526 #if defined(LOADER_FDT_SUPPORT) 527 if (dtb_size) 528 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof(dtbp), &dtbp); 529 else 530 printf("WARNING! Trying to fire up the kernel, but no " 531 "device tree blob found!\n"); 532 #endif 533 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof(kernend), &kernend); 534 #ifdef MODINFOMD_MODULEP 535 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof(module), &module); 536 #endif 537 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof(ST), &ST); 538 #ifdef LOADER_GELI_SUPPORT 539 geli_export_key_metadata(kfp); 540 #endif 541 bi_load_efi_data(kfp, exit_bs); 542 543 size = bi_copymodules(0); 544 kernend = roundup(addr + size, PAGE_SIZE); 545 *kernendp = kernend; 546 547 /* patch MODINFOMD_KERNEND */ 548 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 549 bcopy(&kernend, md->md_data, sizeof kernend); 550 551 #if defined(__arm__) 552 *modulep -= __elfN(relocation_offset); 553 554 /* Do relocation fixup on metadata of each module. */ 555 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 556 for (i = 0; i < nitems(mdt); i++) { 557 md = file_findmetadata(xp, mdt[i]); 558 if (md) { 559 bcopy(md->md_data, &vaddr, sizeof vaddr); 560 vaddr -= __elfN(relocation_offset); 561 bcopy(&vaddr, md->md_data, sizeof vaddr); 562 } 563 } 564 } 565 #endif 566 567 /* Copy module list and metadata. */ 568 (void)bi_copymodules(addr); 569 570 return (0); 571 } 572