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