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 "modinfo.h" 48 #include "loader_efi.h" 49 50 #if defined(__amd64__) 51 #include <machine/specialreg.h> 52 #endif 53 54 #include "gfx_fb.h" 55 56 #if defined(LOADER_FDT_SUPPORT) 57 #include <fdt_platform.h> 58 #endif 59 60 #ifdef LOADER_GELI_SUPPORT 61 #include "geliboot.h" 62 #endif 63 64 int bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, 65 bool exit_bs); 66 67 static int 68 bi_getboothowto(char *kargs) 69 { 70 const char *sw, *tmp; 71 char *opts; 72 char *console; 73 int howto, speed, port; 74 char buf[50]; 75 76 howto = boot_parse_cmdline(kargs); 77 howto |= boot_env_to_howto(); 78 79 console = getenv("console"); 80 if (console != NULL) { 81 if (strcmp(console, "comconsole") == 0) 82 howto |= RB_SERIAL; 83 if (strcmp(console, "nullconsole") == 0) 84 howto |= RB_MUTE; 85 #if defined(__i386__) || defined(__amd64__) 86 if (strcmp(console, "efi") == 0 && 87 getenv("efi_8250_uid") != NULL && 88 getenv("hw.uart.console") == NULL) { 89 /* 90 * If we found a 8250 com port and com speed, we need to 91 * tell the kernel where the serial port is, and how 92 * fast. Ideally, we'd get the port from ACPI, but that 93 * isn't running in the loader. Do the next best thing 94 * by allowing it to be set by a loader.conf variable, 95 * either a EFI specific one, or the compatible 96 * comconsole_port if not. PCI support is needed, but 97 * for that we'd ideally refactor the 98 * libi386/comconsole.c code to have identical behavior. 99 * We only try to set the port for cases where we saw 100 * the Serial(x) node when parsing, otherwise 101 * specialized hardware that has Uart nodes will have a 102 * bogus address set. 103 * But if someone specifically setup hw.uart.console, 104 * don't override that. 105 */ 106 speed = -1; 107 port = -1; 108 tmp = getenv("efi_com_speed"); 109 if (tmp != NULL) 110 speed = strtol(tmp, NULL, 0); 111 tmp = getenv("efi_com_port"); 112 if (tmp == NULL) 113 tmp = getenv("comconsole_port"); 114 if (tmp != NULL) 115 port = strtol(tmp, NULL, 0); 116 if (speed != -1 && port != -1) { 117 snprintf(buf, sizeof(buf), "io:%d,br:%d", port, 118 speed); 119 env_setenv("hw.uart.console", EV_VOLATILE, buf, 120 NULL, NULL); 121 } 122 } 123 #endif 124 } 125 126 return (howto); 127 } 128 129 static EFI_STATUS 130 efi_do_vmap(EFI_MEMORY_DESCRIPTOR *mm, UINTN sz, UINTN mmsz, UINT32 mmver) 131 { 132 EFI_MEMORY_DESCRIPTOR *desc, *viter, *vmap; 133 EFI_STATUS ret; 134 int curr, ndesc, nset; 135 136 nset = 0; 137 desc = mm; 138 ndesc = sz / mmsz; 139 vmap = malloc(sz); 140 if (vmap == NULL) 141 /* This isn't really an EFI error case, but pretend it is */ 142 return (EFI_OUT_OF_RESOURCES); 143 viter = vmap; 144 for (curr = 0; curr < ndesc; 145 curr++, desc = NextMemoryDescriptor(desc, mmsz)) { 146 if ((desc->Attribute & EFI_MEMORY_RUNTIME) != 0) { 147 ++nset; 148 desc->VirtualStart = desc->PhysicalStart; 149 *viter = *desc; 150 viter = NextMemoryDescriptor(viter, mmsz); 151 } 152 } 153 ret = RS->SetVirtualAddressMap(nset * mmsz, mmsz, mmver, vmap); 154 free(vmap); 155 return (ret); 156 } 157 158 static int 159 bi_load_efi_data(struct preloaded_file *kfp, bool exit_bs) 160 { 161 EFI_MEMORY_DESCRIPTOR *mm; 162 EFI_PHYSICAL_ADDRESS addr = 0; 163 EFI_STATUS status; 164 const char *efi_novmap; 165 size_t efisz; 166 UINTN efi_mapkey; 167 UINTN dsz, pages, retry, sz; 168 UINT32 mmver; 169 struct efi_map_header *efihdr; 170 bool do_vmap; 171 172 #if defined(__amd64__) || defined(__aarch64__) 173 struct efi_fb efifb; 174 175 efifb.fb_addr = gfx_state.tg_fb.fb_addr; 176 efifb.fb_size = gfx_state.tg_fb.fb_size; 177 efifb.fb_height = gfx_state.tg_fb.fb_height; 178 efifb.fb_width = gfx_state.tg_fb.fb_width; 179 efifb.fb_stride = gfx_state.tg_fb.fb_stride; 180 efifb.fb_mask_red = gfx_state.tg_fb.fb_mask_red; 181 efifb.fb_mask_green = gfx_state.tg_fb.fb_mask_green; 182 efifb.fb_mask_blue = gfx_state.tg_fb.fb_mask_blue; 183 efifb.fb_mask_reserved = gfx_state.tg_fb.fb_mask_reserved; 184 185 printf("EFI framebuffer information:\n"); 186 printf("addr, size 0x%jx, 0x%jx\n", efifb.fb_addr, efifb.fb_size); 187 printf("dimensions %d x %d\n", efifb.fb_width, efifb.fb_height); 188 printf("stride %d\n", efifb.fb_stride); 189 printf("masks 0x%08x, 0x%08x, 0x%08x, 0x%08x\n", 190 efifb.fb_mask_red, efifb.fb_mask_green, efifb.fb_mask_blue, 191 efifb.fb_mask_reserved); 192 193 if (efifb.fb_addr != 0) 194 file_addmetadata(kfp, MODINFOMD_EFI_FB, sizeof(efifb), &efifb); 195 #endif 196 197 do_vmap = true; 198 efi_novmap = getenv("efi_disable_vmap"); 199 if (efi_novmap != NULL) 200 do_vmap = strcasecmp(efi_novmap, "YES") != 0; 201 202 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 203 204 /* 205 * Assign size of EFI_MEMORY_DESCRIPTOR to keep compatible with 206 * u-boot which doesn't fill this value when buffer for memory 207 * descriptors is too small (eg. 0 to obtain memory map size) 208 */ 209 dsz = sizeof(EFI_MEMORY_DESCRIPTOR); 210 211 /* 212 * Allocate enough pages to hold the bootinfo block and the 213 * memory map EFI will return to us. The memory map has an 214 * unknown size, so we have to determine that first. Note that 215 * the AllocatePages call can itself modify the memory map, so 216 * we have to take that into account as well. The changes to 217 * the memory map are caused by splitting a range of free 218 * memory into two, so that one is marked as being loader 219 * data. 220 */ 221 222 sz = 0; 223 mm = NULL; 224 225 /* 226 * Matthew Garrett has observed at least one system changing the 227 * memory map when calling ExitBootServices, causing it to return an 228 * error, probably because callbacks are allocating memory. 229 * So we need to retry calling it at least once. 230 */ 231 for (retry = 2; retry > 0; retry--) { 232 for (;;) { 233 status = BS->GetMemoryMap(&sz, mm, &efi_mapkey, &dsz, &mmver); 234 if (!EFI_ERROR(status)) 235 break; 236 237 if (status != EFI_BUFFER_TOO_SMALL) { 238 printf("%s: GetMemoryMap error %lu\n", __func__, 239 EFI_ERROR_CODE(status)); 240 return (EINVAL); 241 } 242 243 if (addr != 0) 244 BS->FreePages(addr, pages); 245 246 /* Add 10 descriptors to the size to allow for 247 * fragmentation caused by calling AllocatePages */ 248 sz += (10 * dsz); 249 pages = EFI_SIZE_TO_PAGES(sz + efisz); 250 status = BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 251 pages, &addr); 252 if (EFI_ERROR(status)) { 253 printf("%s: AllocatePages error %lu\n", __func__, 254 EFI_ERROR_CODE(status)); 255 return (ENOMEM); 256 } 257 258 /* 259 * Read the memory map and stash it after bootinfo. Align the 260 * memory map on a 16-byte boundary (the bootinfo block is page 261 * aligned). 262 */ 263 efihdr = (struct efi_map_header *)(uintptr_t)addr; 264 mm = (void *)((uint8_t *)efihdr + efisz); 265 sz = (EFI_PAGE_SIZE * pages) - efisz; 266 } 267 268 if (!exit_bs) 269 break; 270 status = efi_exit_boot_services(efi_mapkey); 271 if (!EFI_ERROR(status)) 272 break; 273 } 274 275 if (retry == 0) { 276 BS->FreePages(addr, pages); 277 printf("ExitBootServices error %lu\n", EFI_ERROR_CODE(status)); 278 return (EINVAL); 279 } 280 281 /* 282 * This may be disabled by setting efi_disable_vmap in 283 * loader.conf(5). By default we will setup the virtual 284 * map entries. 285 */ 286 287 if (do_vmap) 288 efi_do_vmap(mm, sz, dsz, mmver); 289 efihdr->memory_size = sz; 290 efihdr->descriptor_size = dsz; 291 efihdr->descriptor_version = mmver; 292 file_addmetadata(kfp, MODINFOMD_EFI_MAP, efisz + sz, 293 efihdr); 294 295 return (0); 296 } 297 298 /* 299 * Load the information expected by an amd64 kernel. 300 * 301 * - The 'boothowto' argument is constructed. 302 * - The 'bootdev' argument is constructed. 303 * - The 'bootinfo' struct is constructed, and copied into the kernel space. 304 * - The kernel environment is copied into kernel space. 305 * - Module metadata are formatted and placed in kernel space. 306 */ 307 int 308 bi_load(char *args, vm_offset_t *modulep, vm_offset_t *kernendp, bool exit_bs) 309 { 310 struct preloaded_file *xp, *kfp; 311 struct devdesc *rootdev; 312 struct file_metadata *md; 313 vm_offset_t addr; 314 uint64_t kernend, module; 315 uint64_t envp; 316 vm_offset_t size; 317 char *rootdevname; 318 int howto; 319 #if defined(LOADER_FDT_SUPPORT) 320 vm_offset_t dtbp; 321 int dtb_size; 322 #endif 323 #if defined(__arm__) 324 vm_offset_t vaddr; 325 size_t i; 326 /* 327 * These metadata addreses must be converted for kernel after 328 * relocation. 329 */ 330 uint32_t mdt[] = { 331 MODINFOMD_SSYM, MODINFOMD_ESYM, MODINFOMD_KERNEND, 332 MODINFOMD_ENVP, MODINFOMD_FONT, 333 #if defined(LOADER_FDT_SUPPORT) 334 MODINFOMD_DTBP 335 #endif 336 }; 337 #endif 338 339 howto = bi_getboothowto(args); 340 341 /* 342 * Allow the environment variable 'rootdev' to override the supplied 343 * device. This should perhaps go to MI code and/or have $rootdev 344 * tested/set by MI code before launching the kernel. 345 */ 346 rootdevname = getenv("rootdev"); 347 archsw.arch_getdev((void**)(&rootdev), rootdevname, NULL); 348 if (rootdev == NULL) { 349 printf("Can't determine root device.\n"); 350 return(EINVAL); 351 } 352 353 /* Try reading the /etc/fstab file to select the root device */ 354 getrootmount(devformat(rootdev)); 355 356 addr = 0; 357 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 358 if (addr < (xp->f_addr + xp->f_size)) 359 addr = xp->f_addr + xp->f_size; 360 } 361 362 /* Pad to a page boundary. */ 363 addr = roundup(addr, PAGE_SIZE); 364 365 addr = build_font_module(addr); 366 367 /* Pad to a page boundary. */ 368 addr = roundup(addr, PAGE_SIZE); 369 370 /* Copy our environment. */ 371 envp = addr; 372 addr = md_copyenv(addr); 373 374 /* Pad to a page boundary. */ 375 addr = roundup(addr, PAGE_SIZE); 376 377 #if defined(LOADER_FDT_SUPPORT) 378 /* Handle device tree blob */ 379 dtbp = addr; 380 dtb_size = fdt_copy(addr); 381 382 /* Pad to a page boundary */ 383 if (dtb_size) 384 addr += roundup(dtb_size, PAGE_SIZE); 385 #endif 386 387 kfp = file_findfile(NULL, "elf kernel"); 388 if (kfp == NULL) 389 kfp = file_findfile(NULL, "elf64 kernel"); 390 if (kfp == NULL) 391 panic("can't find kernel file"); 392 kernend = 0; /* fill it in later */ 393 394 /* Figure out the size and location of the metadata. */ 395 module = *modulep = addr; 396 397 file_addmetadata(kfp, MODINFOMD_HOWTO, sizeof(howto), &howto); 398 file_addmetadata(kfp, MODINFOMD_ENVP, sizeof(envp), &envp); 399 #if defined(LOADER_FDT_SUPPORT) 400 if (dtb_size) 401 file_addmetadata(kfp, MODINFOMD_DTBP, sizeof(dtbp), &dtbp); 402 else 403 printf("WARNING! Trying to fire up the kernel, but no " 404 "device tree blob found!\n"); 405 #endif 406 file_addmetadata(kfp, MODINFOMD_KERNEND, sizeof(kernend), &kernend); 407 #ifdef MODINFOMD_MODULEP 408 file_addmetadata(kfp, MODINFOMD_MODULEP, sizeof(module), &module); 409 #endif 410 file_addmetadata(kfp, MODINFOMD_FW_HANDLE, sizeof(ST), &ST); 411 #ifdef LOADER_GELI_SUPPORT 412 geli_export_key_metadata(kfp); 413 #endif 414 bi_load_efi_data(kfp, exit_bs); 415 416 size = md_copymodules(0, true); 417 kernend = roundup(addr + size, PAGE_SIZE); 418 *kernendp = kernend; 419 420 /* patch MODINFOMD_KERNEND */ 421 md = file_findmetadata(kfp, MODINFOMD_KERNEND); 422 bcopy(&kernend, md->md_data, sizeof kernend); 423 424 #if defined(__arm__) 425 *modulep -= __elfN(relocation_offset); 426 427 /* Do relocation fixup on metadata of each module. */ 428 for (xp = file_findfile(NULL, NULL); xp != NULL; xp = xp->f_next) { 429 for (i = 0; i < nitems(mdt); i++) { 430 md = file_findmetadata(xp, mdt[i]); 431 if (md) { 432 bcopy(md->md_data, &vaddr, sizeof vaddr); 433 vaddr -= __elfN(relocation_offset); 434 bcopy(&vaddr, md->md_data, sizeof vaddr); 435 } 436 } 437 } 438 #endif 439 440 /* Copy module list and metadata. */ 441 (void)md_copymodules(addr, true); 442 443 return (0); 444 } 445