1 // SPDX-License-Identifier: GPL-2.0-only 2 3 /* ----------------------------------------------------------------------- 4 * 5 * Copyright 2011 Intel Corporation; author Matt Fleming 6 * 7 * ----------------------------------------------------------------------- */ 8 9 #include <linux/efi.h> 10 #include <linux/pci.h> 11 #include <linux/stddef.h> 12 13 #include <asm/efi.h> 14 #include <asm/e820/types.h> 15 #include <asm/setup.h> 16 #include <asm/desc.h> 17 #include <asm/boot.h> 18 #include <asm/kaslr.h> 19 #include <asm/sev.h> 20 21 #include "efistub.h" 22 #include "x86-stub.h" 23 24 extern char _bss[], _ebss[]; 25 26 const efi_system_table_t *efi_system_table; 27 const efi_dxe_services_table_t *efi_dxe_table; 28 static efi_loaded_image_t *image = NULL; 29 static efi_memory_attribute_protocol_t *memattr; 30 31 typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t; 32 union sev_memory_acceptance_protocol { 33 struct { 34 efi_status_t (__efiapi * allow_unaccepted_memory)( 35 sev_memory_acceptance_protocol_t *); 36 }; 37 struct { 38 u32 allow_unaccepted_memory; 39 } mixed_mode; 40 }; 41 42 static efi_status_t 43 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) 44 { 45 struct pci_setup_rom *rom = NULL; 46 efi_status_t status; 47 unsigned long size; 48 uint64_t romsize; 49 void *romimage; 50 51 /* 52 * Some firmware images contain EFI function pointers at the place where 53 * the romimage and romsize fields are supposed to be. Typically the EFI 54 * code is mapped at high addresses, translating to an unrealistically 55 * large romsize. The UEFI spec limits the size of option ROMs to 16 56 * MiB so we reject any ROMs over 16 MiB in size to catch this. 57 */ 58 romimage = efi_table_attr(pci, romimage); 59 romsize = efi_table_attr(pci, romsize); 60 if (!romimage || !romsize || romsize > SZ_16M) 61 return EFI_INVALID_PARAMETER; 62 63 size = romsize + sizeof(*rom); 64 65 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 66 (void **)&rom); 67 if (status != EFI_SUCCESS) { 68 efi_err("Failed to allocate memory for 'rom'\n"); 69 return status; 70 } 71 72 memset(rom, 0, sizeof(*rom)); 73 74 rom->data.type = SETUP_PCI; 75 rom->data.len = size - sizeof(struct setup_data); 76 rom->data.next = 0; 77 rom->pcilen = romsize; 78 *__rom = rom; 79 80 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 81 PCI_VENDOR_ID, 1, &rom->vendor); 82 83 if (status != EFI_SUCCESS) { 84 efi_err("Failed to read rom->vendor\n"); 85 goto free_struct; 86 } 87 88 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 89 PCI_DEVICE_ID, 1, &rom->devid); 90 91 if (status != EFI_SUCCESS) { 92 efi_err("Failed to read rom->devid\n"); 93 goto free_struct; 94 } 95 96 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus, 97 &rom->device, &rom->function); 98 99 if (status != EFI_SUCCESS) 100 goto free_struct; 101 102 memcpy(rom->romdata, romimage, romsize); 103 return status; 104 105 free_struct: 106 efi_bs_call(free_pool, rom); 107 return status; 108 } 109 110 /* 111 * There's no way to return an informative status from this function, 112 * because any analysis (and printing of error messages) needs to be 113 * done directly at the EFI function call-site. 114 * 115 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we 116 * just didn't find any PCI devices, but there's no way to tell outside 117 * the context of the call. 118 */ 119 static void setup_efi_pci(struct boot_params *params) 120 { 121 efi_status_t status; 122 void **pci_handle = NULL; 123 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; 124 unsigned long size = 0; 125 struct setup_data *data; 126 efi_handle_t h; 127 int i; 128 129 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 130 &pci_proto, NULL, &size, pci_handle); 131 132 if (status == EFI_BUFFER_TOO_SMALL) { 133 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 134 (void **)&pci_handle); 135 136 if (status != EFI_SUCCESS) { 137 efi_err("Failed to allocate memory for 'pci_handle'\n"); 138 return; 139 } 140 141 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 142 &pci_proto, NULL, &size, pci_handle); 143 } 144 145 if (status != EFI_SUCCESS) 146 goto free_handle; 147 148 data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 149 150 while (data && data->next) 151 data = (struct setup_data *)(unsigned long)data->next; 152 153 for_each_efi_handle(h, pci_handle, size, i) { 154 efi_pci_io_protocol_t *pci = NULL; 155 struct pci_setup_rom *rom; 156 157 status = efi_bs_call(handle_protocol, h, &pci_proto, 158 (void **)&pci); 159 if (status != EFI_SUCCESS || !pci) 160 continue; 161 162 status = preserve_pci_rom_image(pci, &rom); 163 if (status != EFI_SUCCESS) 164 continue; 165 166 if (data) 167 data->next = (unsigned long)rom; 168 else 169 params->hdr.setup_data = (unsigned long)rom; 170 171 data = (struct setup_data *)rom; 172 } 173 174 free_handle: 175 efi_bs_call(free_pool, pci_handle); 176 } 177 178 static void retrieve_apple_device_properties(struct boot_params *boot_params) 179 { 180 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID; 181 struct setup_data *data, *new; 182 efi_status_t status; 183 u32 size = 0; 184 apple_properties_protocol_t *p; 185 186 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p); 187 if (status != EFI_SUCCESS) 188 return; 189 190 if (efi_table_attr(p, version) != 0x10000) { 191 efi_err("Unsupported properties proto version\n"); 192 return; 193 } 194 195 efi_call_proto(p, get_all, NULL, &size); 196 if (!size) 197 return; 198 199 do { 200 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, 201 size + sizeof(struct setup_data), 202 (void **)&new); 203 if (status != EFI_SUCCESS) { 204 efi_err("Failed to allocate memory for 'properties'\n"); 205 return; 206 } 207 208 status = efi_call_proto(p, get_all, new->data, &size); 209 210 if (status == EFI_BUFFER_TOO_SMALL) 211 efi_bs_call(free_pool, new); 212 } while (status == EFI_BUFFER_TOO_SMALL); 213 214 new->type = SETUP_APPLE_PROPERTIES; 215 new->len = size; 216 new->next = 0; 217 218 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; 219 if (!data) { 220 boot_params->hdr.setup_data = (unsigned long)new; 221 } else { 222 while (data->next) 223 data = (struct setup_data *)(unsigned long)data->next; 224 data->next = (unsigned long)new; 225 } 226 } 227 228 efi_status_t efi_adjust_memory_range_protection(unsigned long start, 229 unsigned long size) 230 { 231 efi_status_t status; 232 efi_gcd_memory_space_desc_t desc; 233 unsigned long end, next; 234 unsigned long rounded_start, rounded_end; 235 unsigned long unprotect_start, unprotect_size; 236 237 rounded_start = rounddown(start, EFI_PAGE_SIZE); 238 rounded_end = roundup(start + size, EFI_PAGE_SIZE); 239 240 if (memattr != NULL) { 241 status = efi_call_proto(memattr, clear_memory_attributes, 242 rounded_start, 243 rounded_end - rounded_start, 244 EFI_MEMORY_XP); 245 if (status != EFI_SUCCESS) 246 efi_warn("Failed to clear EFI_MEMORY_XP attribute\n"); 247 return status; 248 } 249 250 if (efi_dxe_table == NULL) 251 return EFI_SUCCESS; 252 253 /* 254 * Don't modify memory region attributes, they are 255 * already suitable, to lower the possibility to 256 * encounter firmware bugs. 257 */ 258 259 for (end = start + size; start < end; start = next) { 260 261 status = efi_dxe_call(get_memory_space_descriptor, start, &desc); 262 263 if (status != EFI_SUCCESS) 264 break; 265 266 next = desc.base_address + desc.length; 267 268 /* 269 * Only system memory is suitable for trampoline/kernel image placement, 270 * so only this type of memory needs its attributes to be modified. 271 */ 272 273 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory || 274 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0) 275 continue; 276 277 unprotect_start = max(rounded_start, (unsigned long)desc.base_address); 278 unprotect_size = min(rounded_end, next) - unprotect_start; 279 280 status = efi_dxe_call(set_memory_space_attributes, 281 unprotect_start, unprotect_size, 282 EFI_MEMORY_WB); 283 284 if (status != EFI_SUCCESS) { 285 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n", 286 unprotect_start, 287 unprotect_start + unprotect_size, 288 status); 289 break; 290 } 291 } 292 return EFI_SUCCESS; 293 } 294 295 static void setup_unaccepted_memory(void) 296 { 297 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID; 298 sev_memory_acceptance_protocol_t *proto; 299 efi_status_t status; 300 301 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) 302 return; 303 304 /* 305 * Enable unaccepted memory before calling exit boot services in order 306 * for the UEFI to not accept all memory on EBS. 307 */ 308 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL, 309 (void **)&proto); 310 if (status != EFI_SUCCESS) 311 return; 312 313 status = efi_call_proto(proto, allow_unaccepted_memory); 314 if (status != EFI_SUCCESS) 315 efi_err("Memory acceptance protocol failed\n"); 316 } 317 318 static efi_char16_t *efistub_fw_vendor(void) 319 { 320 unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor); 321 322 return (efi_char16_t *)vendor; 323 } 324 325 static const efi_char16_t apple[] = L"Apple"; 326 327 static void setup_quirks(struct boot_params *boot_params) 328 { 329 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) && 330 !memcmp(efistub_fw_vendor(), apple, sizeof(apple))) 331 retrieve_apple_device_properties(boot_params); 332 } 333 334 /* 335 * See if we have Universal Graphics Adapter (UGA) protocol 336 */ 337 static efi_status_t 338 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) 339 { 340 efi_status_t status; 341 u32 width, height; 342 void **uga_handle = NULL; 343 efi_uga_draw_protocol_t *uga = NULL, *first_uga; 344 efi_handle_t handle; 345 int i; 346 347 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 348 (void **)&uga_handle); 349 if (status != EFI_SUCCESS) 350 return status; 351 352 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 353 uga_proto, NULL, &size, uga_handle); 354 if (status != EFI_SUCCESS) 355 goto free_handle; 356 357 height = 0; 358 width = 0; 359 360 first_uga = NULL; 361 for_each_efi_handle(handle, uga_handle, size, i) { 362 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; 363 u32 w, h, depth, refresh; 364 void *pciio; 365 366 status = efi_bs_call(handle_protocol, handle, uga_proto, 367 (void **)&uga); 368 if (status != EFI_SUCCESS) 369 continue; 370 371 pciio = NULL; 372 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio); 373 374 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh); 375 if (status == EFI_SUCCESS && (!first_uga || pciio)) { 376 width = w; 377 height = h; 378 379 /* 380 * Once we've found a UGA supporting PCIIO, 381 * don't bother looking any further. 382 */ 383 if (pciio) 384 break; 385 386 first_uga = uga; 387 } 388 } 389 390 if (!width && !height) 391 goto free_handle; 392 393 /* EFI framebuffer */ 394 si->orig_video_isVGA = VIDEO_TYPE_EFI; 395 396 si->lfb_depth = 32; 397 si->lfb_width = width; 398 si->lfb_height = height; 399 400 si->red_size = 8; 401 si->red_pos = 16; 402 si->green_size = 8; 403 si->green_pos = 8; 404 si->blue_size = 8; 405 si->blue_pos = 0; 406 si->rsvd_size = 8; 407 si->rsvd_pos = 24; 408 409 free_handle: 410 efi_bs_call(free_pool, uga_handle); 411 412 return status; 413 } 414 415 static void setup_graphics(struct boot_params *boot_params) 416 { 417 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 418 struct screen_info *si; 419 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; 420 efi_status_t status; 421 unsigned long size; 422 void **gop_handle = NULL; 423 void **uga_handle = NULL; 424 425 si = &boot_params->screen_info; 426 memset(si, 0, sizeof(*si)); 427 428 size = 0; 429 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 430 &graphics_proto, NULL, &size, gop_handle); 431 if (status == EFI_BUFFER_TOO_SMALL) 432 status = efi_setup_gop(si, &graphics_proto, size); 433 434 if (status != EFI_SUCCESS) { 435 size = 0; 436 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 437 &uga_proto, NULL, &size, uga_handle); 438 if (status == EFI_BUFFER_TOO_SMALL) 439 setup_uga(si, &uga_proto, size); 440 } 441 } 442 443 444 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status) 445 { 446 efi_bs_call(exit, handle, status, 0, NULL); 447 for(;;) 448 asm("hlt"); 449 } 450 451 void __noreturn efi_stub_entry(efi_handle_t handle, 452 efi_system_table_t *sys_table_arg, 453 struct boot_params *boot_params); 454 455 /* 456 * Because the x86 boot code expects to be passed a boot_params we 457 * need to create one ourselves (usually the bootloader would create 458 * one for us). 459 */ 460 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 461 efi_system_table_t *sys_table_arg) 462 { 463 static struct boot_params boot_params __page_aligned_bss; 464 struct setup_header *hdr = &boot_params.hdr; 465 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; 466 int options_size = 0; 467 efi_status_t status; 468 char *cmdline_ptr; 469 470 if (efi_is_native()) 471 memset(_bss, 0, _ebss - _bss); 472 473 efi_system_table = sys_table_arg; 474 475 /* Check if we were booted by the EFI firmware */ 476 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 477 efi_exit(handle, EFI_INVALID_PARAMETER); 478 479 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image); 480 if (status != EFI_SUCCESS) { 481 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); 482 efi_exit(handle, status); 483 } 484 485 /* Assign the setup_header fields that the kernel actually cares about */ 486 hdr->root_flags = 1; 487 hdr->vid_mode = 0xffff; 488 489 hdr->type_of_loader = 0x21; 490 hdr->initrd_addr_max = INT_MAX; 491 492 /* Convert unicode cmdline to ascii */ 493 cmdline_ptr = efi_convert_cmdline(image, &options_size); 494 if (!cmdline_ptr) 495 goto fail; 496 497 efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr, 498 &boot_params.ext_cmd_line_ptr); 499 500 efi_stub_entry(handle, sys_table_arg, &boot_params); 501 /* not reached */ 502 503 fail: 504 efi_exit(handle, status); 505 } 506 507 static void add_e820ext(struct boot_params *params, 508 struct setup_data *e820ext, u32 nr_entries) 509 { 510 struct setup_data *data; 511 512 e820ext->type = SETUP_E820_EXT; 513 e820ext->len = nr_entries * sizeof(struct boot_e820_entry); 514 e820ext->next = 0; 515 516 data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 517 518 while (data && data->next) 519 data = (struct setup_data *)(unsigned long)data->next; 520 521 if (data) 522 data->next = (unsigned long)e820ext; 523 else 524 params->hdr.setup_data = (unsigned long)e820ext; 525 } 526 527 static efi_status_t 528 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) 529 { 530 struct boot_e820_entry *entry = params->e820_table; 531 struct efi_info *efi = ¶ms->efi_info; 532 struct boot_e820_entry *prev = NULL; 533 u32 nr_entries; 534 u32 nr_desc; 535 int i; 536 537 nr_entries = 0; 538 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; 539 540 for (i = 0; i < nr_desc; i++) { 541 efi_memory_desc_t *d; 542 unsigned int e820_type = 0; 543 unsigned long m = efi->efi_memmap; 544 545 #ifdef CONFIG_X86_64 546 m |= (u64)efi->efi_memmap_hi << 32; 547 #endif 548 549 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); 550 switch (d->type) { 551 case EFI_RESERVED_TYPE: 552 case EFI_RUNTIME_SERVICES_CODE: 553 case EFI_RUNTIME_SERVICES_DATA: 554 case EFI_MEMORY_MAPPED_IO: 555 case EFI_MEMORY_MAPPED_IO_PORT_SPACE: 556 case EFI_PAL_CODE: 557 e820_type = E820_TYPE_RESERVED; 558 break; 559 560 case EFI_UNUSABLE_MEMORY: 561 e820_type = E820_TYPE_UNUSABLE; 562 break; 563 564 case EFI_ACPI_RECLAIM_MEMORY: 565 e820_type = E820_TYPE_ACPI; 566 break; 567 568 case EFI_LOADER_CODE: 569 case EFI_LOADER_DATA: 570 case EFI_BOOT_SERVICES_CODE: 571 case EFI_BOOT_SERVICES_DATA: 572 case EFI_CONVENTIONAL_MEMORY: 573 if (efi_soft_reserve_enabled() && 574 (d->attribute & EFI_MEMORY_SP)) 575 e820_type = E820_TYPE_SOFT_RESERVED; 576 else 577 e820_type = E820_TYPE_RAM; 578 break; 579 580 case EFI_ACPI_MEMORY_NVS: 581 e820_type = E820_TYPE_NVS; 582 break; 583 584 case EFI_PERSISTENT_MEMORY: 585 e820_type = E820_TYPE_PMEM; 586 break; 587 588 case EFI_UNACCEPTED_MEMORY: 589 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) 590 continue; 591 e820_type = E820_TYPE_RAM; 592 process_unaccepted_memory(d->phys_addr, 593 d->phys_addr + PAGE_SIZE * d->num_pages); 594 break; 595 default: 596 continue; 597 } 598 599 /* Merge adjacent mappings */ 600 if (prev && prev->type == e820_type && 601 (prev->addr + prev->size) == d->phys_addr) { 602 prev->size += d->num_pages << 12; 603 continue; 604 } 605 606 if (nr_entries == ARRAY_SIZE(params->e820_table)) { 607 u32 need = (nr_desc - i) * sizeof(struct e820_entry) + 608 sizeof(struct setup_data); 609 610 if (!e820ext || e820ext_size < need) 611 return EFI_BUFFER_TOO_SMALL; 612 613 /* boot_params map full, switch to e820 extended */ 614 entry = (struct boot_e820_entry *)e820ext->data; 615 } 616 617 entry->addr = d->phys_addr; 618 entry->size = d->num_pages << PAGE_SHIFT; 619 entry->type = e820_type; 620 prev = entry++; 621 nr_entries++; 622 } 623 624 if (nr_entries > ARRAY_SIZE(params->e820_table)) { 625 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); 626 627 add_e820ext(params, e820ext, nr_e820ext); 628 nr_entries -= nr_e820ext; 629 } 630 631 params->e820_entries = (u8)nr_entries; 632 633 return EFI_SUCCESS; 634 } 635 636 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, 637 u32 *e820ext_size) 638 { 639 efi_status_t status; 640 unsigned long size; 641 642 size = sizeof(struct setup_data) + 643 sizeof(struct e820_entry) * nr_desc; 644 645 if (*e820ext) { 646 efi_bs_call(free_pool, *e820ext); 647 *e820ext = NULL; 648 *e820ext_size = 0; 649 } 650 651 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 652 (void **)e820ext); 653 if (status == EFI_SUCCESS) 654 *e820ext_size = size; 655 656 return status; 657 } 658 659 static efi_status_t allocate_e820(struct boot_params *params, 660 struct setup_data **e820ext, 661 u32 *e820ext_size) 662 { 663 struct efi_boot_memmap *map; 664 efi_status_t status; 665 __u32 nr_desc; 666 667 status = efi_get_memory_map(&map, false); 668 if (status != EFI_SUCCESS) 669 return status; 670 671 nr_desc = map->map_size / map->desc_size; 672 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) { 673 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) + 674 EFI_MMAP_NR_SLACK_SLOTS; 675 676 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); 677 } 678 679 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS) 680 status = allocate_unaccepted_bitmap(nr_desc, map); 681 682 efi_bs_call(free_pool, map); 683 return status; 684 } 685 686 struct exit_boot_struct { 687 struct boot_params *boot_params; 688 struct efi_info *efi; 689 }; 690 691 static efi_status_t exit_boot_func(struct efi_boot_memmap *map, 692 void *priv) 693 { 694 const char *signature; 695 struct exit_boot_struct *p = priv; 696 697 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE 698 : EFI32_LOADER_SIGNATURE; 699 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); 700 701 efi_set_u64_split((unsigned long)efi_system_table, 702 &p->efi->efi_systab, &p->efi->efi_systab_hi); 703 p->efi->efi_memdesc_size = map->desc_size; 704 p->efi->efi_memdesc_version = map->desc_ver; 705 efi_set_u64_split((unsigned long)map->map, 706 &p->efi->efi_memmap, &p->efi->efi_memmap_hi); 707 p->efi->efi_memmap_size = map->map_size; 708 709 return EFI_SUCCESS; 710 } 711 712 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) 713 { 714 struct setup_data *e820ext = NULL; 715 __u32 e820ext_size = 0; 716 efi_status_t status; 717 struct exit_boot_struct priv; 718 719 priv.boot_params = boot_params; 720 priv.efi = &boot_params->efi_info; 721 722 status = allocate_e820(boot_params, &e820ext, &e820ext_size); 723 if (status != EFI_SUCCESS) 724 return status; 725 726 /* Might as well exit boot services now */ 727 status = efi_exit_boot_services(handle, &priv, exit_boot_func); 728 if (status != EFI_SUCCESS) 729 return status; 730 731 /* Historic? */ 732 boot_params->alt_mem_k = 32 * 1024; 733 734 status = setup_e820(boot_params, e820ext, e820ext_size); 735 if (status != EFI_SUCCESS) 736 return status; 737 738 return EFI_SUCCESS; 739 } 740 741 static bool have_unsupported_snp_features(void) 742 { 743 u64 unsupported; 744 745 unsupported = snp_get_unsupported_features(sev_get_status()); 746 if (unsupported) { 747 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n", 748 unsupported); 749 return true; 750 } 751 return false; 752 } 753 754 static void efi_get_seed(void *seed, int size) 755 { 756 efi_get_random_bytes(size, seed); 757 758 /* 759 * This only updates seed[0] when running on 32-bit, but in that case, 760 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit. 761 */ 762 *(unsigned long *)seed ^= kaslr_get_random_long("EFI"); 763 } 764 765 static void error(char *str) 766 { 767 efi_warn("Decompression failed: %s\n", str); 768 } 769 770 static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry) 771 { 772 unsigned long virt_addr = LOAD_PHYSICAL_ADDR; 773 unsigned long addr, alloc_size, entry; 774 efi_status_t status; 775 u32 seed[2] = {}; 776 777 /* determine the required size of the allocation */ 778 alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size), 779 MIN_KERNEL_ALIGN); 780 781 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) { 782 u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size; 783 static const efi_char16_t ami[] = L"American Megatrends"; 784 785 efi_get_seed(seed, sizeof(seed)); 786 787 virt_addr += (range * seed[1]) >> 32; 788 virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1); 789 790 /* 791 * Older Dell systems with AMI UEFI firmware v2.0 may hang 792 * while decompressing the kernel if physical address 793 * randomization is enabled. 794 * 795 * https://bugzilla.kernel.org/show_bug.cgi?id=218173 796 */ 797 if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION && 798 !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) { 799 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n"); 800 seed[0] = 0; 801 } 802 } 803 804 status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr, 805 seed[0], EFI_LOADER_CODE, 806 LOAD_PHYSICAL_ADDR, 807 EFI_X86_KERNEL_ALLOC_LIMIT); 808 if (status != EFI_SUCCESS) 809 return status; 810 811 entry = decompress_kernel((void *)addr, virt_addr, error); 812 if (entry == ULONG_MAX) { 813 efi_free(alloc_size, addr); 814 return EFI_LOAD_ERROR; 815 } 816 817 *kernel_entry = addr + entry; 818 819 return efi_adjust_memory_range_protection(addr, kernel_total_size); 820 } 821 822 static void __noreturn enter_kernel(unsigned long kernel_addr, 823 struct boot_params *boot_params) 824 { 825 /* enter decompressed kernel with boot_params pointer in RSI/ESI */ 826 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params)); 827 828 unreachable(); 829 } 830 831 /* 832 * On success, this routine will jump to the relocated image directly and never 833 * return. On failure, it will exit to the firmware via efi_exit() instead of 834 * returning. 835 */ 836 void __noreturn efi_stub_entry(efi_handle_t handle, 837 efi_system_table_t *sys_table_arg, 838 struct boot_params *boot_params) 839 { 840 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID; 841 struct setup_header *hdr = &boot_params->hdr; 842 const struct linux_efi_initrd *initrd = NULL; 843 unsigned long kernel_entry; 844 efi_status_t status; 845 846 boot_params_pointer = boot_params; 847 848 efi_system_table = sys_table_arg; 849 /* Check if we were booted by the EFI firmware */ 850 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 851 efi_exit(handle, EFI_INVALID_PARAMETER); 852 853 if (have_unsupported_snp_features()) 854 efi_exit(handle, EFI_UNSUPPORTED); 855 856 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) { 857 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID); 858 if (efi_dxe_table && 859 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) { 860 efi_warn("Ignoring DXE services table: invalid signature\n"); 861 efi_dxe_table = NULL; 862 } 863 } 864 865 /* grab the memory attributes protocol if it exists */ 866 efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr); 867 868 status = efi_setup_5level_paging(); 869 if (status != EFI_SUCCESS) { 870 efi_err("efi_setup_5level_paging() failed!\n"); 871 goto fail; 872 } 873 874 #ifdef CONFIG_CMDLINE_BOOL 875 status = efi_parse_options(CONFIG_CMDLINE); 876 if (status != EFI_SUCCESS) { 877 efi_err("Failed to parse options\n"); 878 goto fail; 879 } 880 #endif 881 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) { 882 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr | 883 ((u64)boot_params->ext_cmd_line_ptr << 32)); 884 status = efi_parse_options((char *)cmdline_paddr); 885 if (status != EFI_SUCCESS) { 886 efi_err("Failed to parse options\n"); 887 goto fail; 888 } 889 } 890 891 status = efi_decompress_kernel(&kernel_entry); 892 if (status != EFI_SUCCESS) { 893 efi_err("Failed to decompress kernel\n"); 894 goto fail; 895 } 896 897 /* 898 * At this point, an initrd may already have been loaded by the 899 * bootloader and passed via bootparams. We permit an initrd loaded 900 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it. 901 * 902 * If the device path is not present, any command-line initrd= 903 * arguments will be processed only if image is not NULL, which will be 904 * the case only if we were loaded via the PE entry point. 905 */ 906 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX, 907 &initrd); 908 if (status != EFI_SUCCESS) 909 goto fail; 910 if (initrd && initrd->size > 0) { 911 efi_set_u64_split(initrd->base, &hdr->ramdisk_image, 912 &boot_params->ext_ramdisk_image); 913 efi_set_u64_split(initrd->size, &hdr->ramdisk_size, 914 &boot_params->ext_ramdisk_size); 915 } 916 917 918 /* 919 * If the boot loader gave us a value for secure_boot then we use that, 920 * otherwise we ask the BIOS. 921 */ 922 if (boot_params->secure_boot == efi_secureboot_mode_unset) 923 boot_params->secure_boot = efi_get_secureboot(); 924 925 /* Ask the firmware to clear memory on unclean shutdown */ 926 efi_enable_reset_attack_mitigation(); 927 928 efi_random_get_seed(); 929 930 efi_retrieve_tpm2_eventlog(); 931 932 setup_graphics(boot_params); 933 934 setup_efi_pci(boot_params); 935 936 setup_quirks(boot_params); 937 938 setup_unaccepted_memory(); 939 940 status = exit_boot(boot_params, handle); 941 if (status != EFI_SUCCESS) { 942 efi_err("exit_boot() failed!\n"); 943 goto fail; 944 } 945 946 /* 947 * Call the SEV init code while still running with the firmware's 948 * GDT/IDT, so #VC exceptions will be handled by EFI. 949 */ 950 sev_enable(boot_params); 951 952 efi_5level_switch(); 953 954 enter_kernel(kernel_entry, boot_params); 955 fail: 956 efi_err("efi_stub_entry() failed!\n"); 957 958 efi_exit(handle, status); 959 } 960 961 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL 962 void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, 963 struct boot_params *boot_params) 964 { 965 memset(_bss, 0, _ebss - _bss); 966 efi_stub_entry(handle, sys_table_arg, boot_params); 967 } 968 969 #ifndef CONFIG_EFI_MIXED 970 extern __alias(efi_handover_entry) 971 void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, 972 struct boot_params *boot_params); 973 974 extern __alias(efi_handover_entry) 975 void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, 976 struct boot_params *boot_params); 977 #endif 978 #endif 979