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 static bool apple_match_product_name(void) 229 { 230 static const char type1_product_matches[][15] = { 231 "MacBookPro11,3", 232 "MacBookPro11,5", 233 "MacBookPro13,3", 234 "MacBookPro14,3", 235 "MacBookPro15,1", 236 "MacBookPro15,3", 237 "MacBookPro16,1", 238 "MacBookPro16,4", 239 }; 240 const struct efi_smbios_type1_record *record; 241 const u8 *product; 242 243 record = (struct efi_smbios_type1_record *)efi_get_smbios_record(1); 244 if (!record) 245 return false; 246 247 product = efi_get_smbios_string(record, product_name); 248 if (!product) 249 return false; 250 251 for (int i = 0; i < ARRAY_SIZE(type1_product_matches); i++) { 252 if (!strcmp(product, type1_product_matches[i])) 253 return true; 254 } 255 256 return false; 257 } 258 259 static void apple_set_os(void) 260 { 261 struct { 262 unsigned long version; 263 efi_status_t (__efiapi *set_os_version)(const char *); 264 efi_status_t (__efiapi *set_os_vendor)(const char *); 265 } *set_os; 266 efi_status_t status; 267 268 if (!efi_is_64bit() || !apple_match_product_name()) 269 return; 270 271 status = efi_bs_call(locate_protocol, &APPLE_SET_OS_PROTOCOL_GUID, NULL, 272 (void **)&set_os); 273 if (status != EFI_SUCCESS) 274 return; 275 276 if (set_os->version >= 2) { 277 status = set_os->set_os_vendor("Apple Inc."); 278 if (status != EFI_SUCCESS) 279 efi_err("Failed to set OS vendor via apple_set_os\n"); 280 } 281 282 if (set_os->version > 0) { 283 /* The version being set doesn't seem to matter */ 284 status = set_os->set_os_version("Mac OS X 10.9"); 285 if (status != EFI_SUCCESS) 286 efi_err("Failed to set OS version via apple_set_os\n"); 287 } 288 } 289 290 efi_status_t efi_adjust_memory_range_protection(unsigned long start, 291 unsigned long size) 292 { 293 efi_status_t status; 294 efi_gcd_memory_space_desc_t desc; 295 unsigned long end, next; 296 unsigned long rounded_start, rounded_end; 297 unsigned long unprotect_start, unprotect_size; 298 299 rounded_start = rounddown(start, EFI_PAGE_SIZE); 300 rounded_end = roundup(start + size, EFI_PAGE_SIZE); 301 302 if (memattr != NULL) { 303 status = efi_call_proto(memattr, set_memory_attributes, 304 rounded_start, 305 rounded_end - rounded_start, 306 EFI_MEMORY_RO); 307 if (status != EFI_SUCCESS) { 308 efi_warn("Failed to set EFI_MEMORY_RO attribute\n"); 309 return status; 310 } 311 312 status = efi_call_proto(memattr, clear_memory_attributes, 313 rounded_start, 314 rounded_end - rounded_start, 315 EFI_MEMORY_XP); 316 if (status != EFI_SUCCESS) 317 efi_warn("Failed to clear EFI_MEMORY_XP attribute\n"); 318 return status; 319 } 320 321 if (efi_dxe_table == NULL) 322 return EFI_SUCCESS; 323 324 /* 325 * Don't modify memory region attributes, they are 326 * already suitable, to lower the possibility to 327 * encounter firmware bugs. 328 */ 329 330 for (end = start + size; start < end; start = next) { 331 332 status = efi_dxe_call(get_memory_space_descriptor, start, &desc); 333 334 if (status != EFI_SUCCESS) 335 break; 336 337 next = desc.base_address + desc.length; 338 339 /* 340 * Only system memory is suitable for trampoline/kernel image placement, 341 * so only this type of memory needs its attributes to be modified. 342 */ 343 344 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory || 345 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0) 346 continue; 347 348 unprotect_start = max(rounded_start, (unsigned long)desc.base_address); 349 unprotect_size = min(rounded_end, next) - unprotect_start; 350 351 status = efi_dxe_call(set_memory_space_attributes, 352 unprotect_start, unprotect_size, 353 EFI_MEMORY_WB); 354 355 if (status != EFI_SUCCESS) { 356 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n", 357 unprotect_start, 358 unprotect_start + unprotect_size, 359 status); 360 break; 361 } 362 } 363 return EFI_SUCCESS; 364 } 365 366 static void setup_unaccepted_memory(void) 367 { 368 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID; 369 sev_memory_acceptance_protocol_t *proto; 370 efi_status_t status; 371 372 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) 373 return; 374 375 /* 376 * Enable unaccepted memory before calling exit boot services in order 377 * for the UEFI to not accept all memory on EBS. 378 */ 379 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL, 380 (void **)&proto); 381 if (status != EFI_SUCCESS) 382 return; 383 384 status = efi_call_proto(proto, allow_unaccepted_memory); 385 if (status != EFI_SUCCESS) 386 efi_err("Memory acceptance protocol failed\n"); 387 } 388 389 static efi_char16_t *efistub_fw_vendor(void) 390 { 391 unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor); 392 393 return (efi_char16_t *)vendor; 394 } 395 396 static const efi_char16_t apple[] = L"Apple"; 397 398 static void setup_quirks(struct boot_params *boot_params) 399 { 400 if (!memcmp(efistub_fw_vendor(), apple, sizeof(apple))) { 401 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) 402 retrieve_apple_device_properties(boot_params); 403 404 apple_set_os(); 405 } 406 } 407 408 /* 409 * See if we have Universal Graphics Adapter (UGA) protocol 410 */ 411 static efi_status_t 412 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) 413 { 414 efi_status_t status; 415 u32 width, height; 416 void **uga_handle = NULL; 417 efi_uga_draw_protocol_t *uga = NULL, *first_uga; 418 efi_handle_t handle; 419 int i; 420 421 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 422 (void **)&uga_handle); 423 if (status != EFI_SUCCESS) 424 return status; 425 426 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 427 uga_proto, NULL, &size, uga_handle); 428 if (status != EFI_SUCCESS) 429 goto free_handle; 430 431 height = 0; 432 width = 0; 433 434 first_uga = NULL; 435 for_each_efi_handle(handle, uga_handle, size, i) { 436 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; 437 u32 w, h, depth, refresh; 438 void *pciio; 439 440 status = efi_bs_call(handle_protocol, handle, uga_proto, 441 (void **)&uga); 442 if (status != EFI_SUCCESS) 443 continue; 444 445 pciio = NULL; 446 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio); 447 448 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh); 449 if (status == EFI_SUCCESS && (!first_uga || pciio)) { 450 width = w; 451 height = h; 452 453 /* 454 * Once we've found a UGA supporting PCIIO, 455 * don't bother looking any further. 456 */ 457 if (pciio) 458 break; 459 460 first_uga = uga; 461 } 462 } 463 464 if (!width && !height) 465 goto free_handle; 466 467 /* EFI framebuffer */ 468 si->orig_video_isVGA = VIDEO_TYPE_EFI; 469 470 si->lfb_depth = 32; 471 si->lfb_width = width; 472 si->lfb_height = height; 473 474 si->red_size = 8; 475 si->red_pos = 16; 476 si->green_size = 8; 477 si->green_pos = 8; 478 si->blue_size = 8; 479 si->blue_pos = 0; 480 si->rsvd_size = 8; 481 si->rsvd_pos = 24; 482 483 free_handle: 484 efi_bs_call(free_pool, uga_handle); 485 486 return status; 487 } 488 489 static void setup_graphics(struct boot_params *boot_params) 490 { 491 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 492 struct screen_info *si; 493 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; 494 efi_status_t status; 495 unsigned long size; 496 void **gop_handle = NULL; 497 void **uga_handle = NULL; 498 499 si = &boot_params->screen_info; 500 memset(si, 0, sizeof(*si)); 501 502 size = 0; 503 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 504 &graphics_proto, NULL, &size, gop_handle); 505 if (status == EFI_BUFFER_TOO_SMALL) 506 status = efi_setup_gop(si, &graphics_proto, size); 507 508 if (status != EFI_SUCCESS) { 509 size = 0; 510 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 511 &uga_proto, NULL, &size, uga_handle); 512 if (status == EFI_BUFFER_TOO_SMALL) 513 setup_uga(si, &uga_proto, size); 514 } 515 } 516 517 518 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status) 519 { 520 efi_bs_call(exit, handle, status, 0, NULL); 521 for(;;) 522 asm("hlt"); 523 } 524 525 void __noreturn efi_stub_entry(efi_handle_t handle, 526 efi_system_table_t *sys_table_arg, 527 struct boot_params *boot_params); 528 529 /* 530 * Because the x86 boot code expects to be passed a boot_params we 531 * need to create one ourselves (usually the bootloader would create 532 * one for us). 533 */ 534 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 535 efi_system_table_t *sys_table_arg) 536 { 537 static struct boot_params boot_params __page_aligned_bss; 538 struct setup_header *hdr = &boot_params.hdr; 539 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; 540 int options_size = 0; 541 efi_status_t status; 542 char *cmdline_ptr; 543 544 efi_system_table = sys_table_arg; 545 546 /* Check if we were booted by the EFI firmware */ 547 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 548 efi_exit(handle, EFI_INVALID_PARAMETER); 549 550 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image); 551 if (status != EFI_SUCCESS) { 552 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); 553 efi_exit(handle, status); 554 } 555 556 /* Assign the setup_header fields that the kernel actually cares about */ 557 hdr->root_flags = 1; 558 hdr->vid_mode = 0xffff; 559 560 hdr->type_of_loader = 0x21; 561 hdr->initrd_addr_max = INT_MAX; 562 563 /* Convert unicode cmdline to ascii */ 564 cmdline_ptr = efi_convert_cmdline(image, &options_size); 565 if (!cmdline_ptr) 566 efi_exit(handle, EFI_OUT_OF_RESOURCES); 567 568 efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr, 569 &boot_params.ext_cmd_line_ptr); 570 571 efi_stub_entry(handle, sys_table_arg, &boot_params); 572 /* not reached */ 573 } 574 575 static void add_e820ext(struct boot_params *params, 576 struct setup_data *e820ext, u32 nr_entries) 577 { 578 struct setup_data *data; 579 580 e820ext->type = SETUP_E820_EXT; 581 e820ext->len = nr_entries * sizeof(struct boot_e820_entry); 582 e820ext->next = 0; 583 584 data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 585 586 while (data && data->next) 587 data = (struct setup_data *)(unsigned long)data->next; 588 589 if (data) 590 data->next = (unsigned long)e820ext; 591 else 592 params->hdr.setup_data = (unsigned long)e820ext; 593 } 594 595 static efi_status_t 596 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) 597 { 598 struct boot_e820_entry *entry = params->e820_table; 599 struct efi_info *efi = ¶ms->efi_info; 600 struct boot_e820_entry *prev = NULL; 601 u32 nr_entries; 602 u32 nr_desc; 603 int i; 604 605 nr_entries = 0; 606 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; 607 608 for (i = 0; i < nr_desc; i++) { 609 efi_memory_desc_t *d; 610 unsigned int e820_type = 0; 611 unsigned long m = efi->efi_memmap; 612 613 #ifdef CONFIG_X86_64 614 m |= (u64)efi->efi_memmap_hi << 32; 615 #endif 616 617 d = efi_memdesc_ptr(m, efi->efi_memdesc_size, i); 618 switch (d->type) { 619 case EFI_RESERVED_TYPE: 620 case EFI_RUNTIME_SERVICES_CODE: 621 case EFI_RUNTIME_SERVICES_DATA: 622 case EFI_MEMORY_MAPPED_IO: 623 case EFI_MEMORY_MAPPED_IO_PORT_SPACE: 624 case EFI_PAL_CODE: 625 e820_type = E820_TYPE_RESERVED; 626 break; 627 628 case EFI_UNUSABLE_MEMORY: 629 e820_type = E820_TYPE_UNUSABLE; 630 break; 631 632 case EFI_ACPI_RECLAIM_MEMORY: 633 e820_type = E820_TYPE_ACPI; 634 break; 635 636 case EFI_LOADER_CODE: 637 case EFI_LOADER_DATA: 638 case EFI_BOOT_SERVICES_CODE: 639 case EFI_BOOT_SERVICES_DATA: 640 case EFI_CONVENTIONAL_MEMORY: 641 if (efi_soft_reserve_enabled() && 642 (d->attribute & EFI_MEMORY_SP)) 643 e820_type = E820_TYPE_SOFT_RESERVED; 644 else 645 e820_type = E820_TYPE_RAM; 646 break; 647 648 case EFI_ACPI_MEMORY_NVS: 649 e820_type = E820_TYPE_NVS; 650 break; 651 652 case EFI_PERSISTENT_MEMORY: 653 e820_type = E820_TYPE_PMEM; 654 break; 655 656 case EFI_UNACCEPTED_MEMORY: 657 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) 658 continue; 659 e820_type = E820_TYPE_RAM; 660 process_unaccepted_memory(d->phys_addr, 661 d->phys_addr + PAGE_SIZE * d->num_pages); 662 break; 663 default: 664 continue; 665 } 666 667 /* Merge adjacent mappings */ 668 if (prev && prev->type == e820_type && 669 (prev->addr + prev->size) == d->phys_addr) { 670 prev->size += d->num_pages << 12; 671 continue; 672 } 673 674 if (nr_entries == ARRAY_SIZE(params->e820_table)) { 675 u32 need = (nr_desc - i) * sizeof(struct e820_entry) + 676 sizeof(struct setup_data); 677 678 if (!e820ext || e820ext_size < need) 679 return EFI_BUFFER_TOO_SMALL; 680 681 /* boot_params map full, switch to e820 extended */ 682 entry = (struct boot_e820_entry *)e820ext->data; 683 } 684 685 entry->addr = d->phys_addr; 686 entry->size = d->num_pages << PAGE_SHIFT; 687 entry->type = e820_type; 688 prev = entry++; 689 nr_entries++; 690 } 691 692 if (nr_entries > ARRAY_SIZE(params->e820_table)) { 693 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); 694 695 add_e820ext(params, e820ext, nr_e820ext); 696 nr_entries -= nr_e820ext; 697 } 698 699 params->e820_entries = (u8)nr_entries; 700 701 return EFI_SUCCESS; 702 } 703 704 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, 705 u32 *e820ext_size) 706 { 707 efi_status_t status; 708 unsigned long size; 709 710 size = sizeof(struct setup_data) + 711 sizeof(struct e820_entry) * nr_desc; 712 713 if (*e820ext) { 714 efi_bs_call(free_pool, *e820ext); 715 *e820ext = NULL; 716 *e820ext_size = 0; 717 } 718 719 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 720 (void **)e820ext); 721 if (status == EFI_SUCCESS) 722 *e820ext_size = size; 723 724 return status; 725 } 726 727 static efi_status_t allocate_e820(struct boot_params *params, 728 struct setup_data **e820ext, 729 u32 *e820ext_size) 730 { 731 struct efi_boot_memmap *map; 732 efi_status_t status; 733 __u32 nr_desc; 734 735 status = efi_get_memory_map(&map, false); 736 if (status != EFI_SUCCESS) 737 return status; 738 739 nr_desc = map->map_size / map->desc_size; 740 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) { 741 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) + 742 EFI_MMAP_NR_SLACK_SLOTS; 743 744 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); 745 } 746 747 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS) 748 status = allocate_unaccepted_bitmap(nr_desc, map); 749 750 efi_bs_call(free_pool, map); 751 return status; 752 } 753 754 struct exit_boot_struct { 755 struct boot_params *boot_params; 756 struct efi_info *efi; 757 }; 758 759 static efi_status_t exit_boot_func(struct efi_boot_memmap *map, 760 void *priv) 761 { 762 const char *signature; 763 struct exit_boot_struct *p = priv; 764 765 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE 766 : EFI32_LOADER_SIGNATURE; 767 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); 768 769 efi_set_u64_split((unsigned long)efi_system_table, 770 &p->efi->efi_systab, &p->efi->efi_systab_hi); 771 p->efi->efi_memdesc_size = map->desc_size; 772 p->efi->efi_memdesc_version = map->desc_ver; 773 efi_set_u64_split((unsigned long)map->map, 774 &p->efi->efi_memmap, &p->efi->efi_memmap_hi); 775 p->efi->efi_memmap_size = map->map_size; 776 777 return EFI_SUCCESS; 778 } 779 780 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) 781 { 782 struct setup_data *e820ext = NULL; 783 __u32 e820ext_size = 0; 784 efi_status_t status; 785 struct exit_boot_struct priv; 786 787 priv.boot_params = boot_params; 788 priv.efi = &boot_params->efi_info; 789 790 status = allocate_e820(boot_params, &e820ext, &e820ext_size); 791 if (status != EFI_SUCCESS) 792 return status; 793 794 /* Might as well exit boot services now */ 795 status = efi_exit_boot_services(handle, &priv, exit_boot_func); 796 if (status != EFI_SUCCESS) 797 return status; 798 799 /* Historic? */ 800 boot_params->alt_mem_k = 32 * 1024; 801 802 status = setup_e820(boot_params, e820ext, e820ext_size); 803 if (status != EFI_SUCCESS) 804 return status; 805 806 return EFI_SUCCESS; 807 } 808 809 static bool have_unsupported_snp_features(void) 810 { 811 u64 unsupported; 812 813 unsupported = snp_get_unsupported_features(sev_get_status()); 814 if (unsupported) { 815 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n", 816 unsupported); 817 return true; 818 } 819 return false; 820 } 821 822 static void efi_get_seed(void *seed, int size) 823 { 824 efi_get_random_bytes(size, seed); 825 826 /* 827 * This only updates seed[0] when running on 32-bit, but in that case, 828 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit. 829 */ 830 *(unsigned long *)seed ^= kaslr_get_random_long("EFI"); 831 } 832 833 static void error(char *str) 834 { 835 efi_warn("Decompression failed: %s\n", str); 836 } 837 838 static const char *cmdline_memmap_override; 839 840 static efi_status_t parse_options(const char *cmdline) 841 { 842 static const char opts[][14] = { 843 "mem=", "memmap=", "hugepages=" 844 }; 845 846 for (int i = 0; i < ARRAY_SIZE(opts); i++) { 847 const char *p = strstr(cmdline, opts[i]); 848 849 if (p == cmdline || (p > cmdline && isspace(p[-1]))) { 850 cmdline_memmap_override = opts[i]; 851 break; 852 } 853 } 854 855 return efi_parse_options(cmdline); 856 } 857 858 static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry) 859 { 860 unsigned long virt_addr = LOAD_PHYSICAL_ADDR; 861 unsigned long addr, alloc_size, entry; 862 efi_status_t status; 863 u32 seed[2] = {}; 864 865 /* determine the required size of the allocation */ 866 alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size), 867 MIN_KERNEL_ALIGN); 868 869 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) { 870 u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size; 871 static const efi_char16_t ami[] = L"American Megatrends"; 872 873 efi_get_seed(seed, sizeof(seed)); 874 875 virt_addr += (range * seed[1]) >> 32; 876 virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1); 877 878 /* 879 * Older Dell systems with AMI UEFI firmware v2.0 may hang 880 * while decompressing the kernel if physical address 881 * randomization is enabled. 882 * 883 * https://bugzilla.kernel.org/show_bug.cgi?id=218173 884 */ 885 if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION && 886 !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) { 887 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n"); 888 seed[0] = 0; 889 } else if (cmdline_memmap_override) { 890 efi_info("%s detected on the kernel command line - disabling physical KASLR\n", 891 cmdline_memmap_override); 892 seed[0] = 0; 893 } 894 895 boot_params_ptr->hdr.loadflags |= KASLR_FLAG; 896 } 897 898 status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr, 899 seed[0], EFI_LOADER_CODE, 900 LOAD_PHYSICAL_ADDR, 901 EFI_X86_KERNEL_ALLOC_LIMIT); 902 if (status != EFI_SUCCESS) 903 return status; 904 905 entry = decompress_kernel((void *)addr, virt_addr, error); 906 if (entry == ULONG_MAX) { 907 efi_free(alloc_size, addr); 908 return EFI_LOAD_ERROR; 909 } 910 911 *kernel_entry = addr + entry; 912 913 return efi_adjust_memory_range_protection(addr, kernel_text_size); 914 } 915 916 static void __noreturn enter_kernel(unsigned long kernel_addr, 917 struct boot_params *boot_params) 918 { 919 /* enter decompressed kernel with boot_params pointer in RSI/ESI */ 920 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params)); 921 922 unreachable(); 923 } 924 925 /* 926 * On success, this routine will jump to the relocated image directly and never 927 * return. On failure, it will exit to the firmware via efi_exit() instead of 928 * returning. 929 */ 930 void __noreturn efi_stub_entry(efi_handle_t handle, 931 efi_system_table_t *sys_table_arg, 932 struct boot_params *boot_params) 933 { 934 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID; 935 struct setup_header *hdr = &boot_params->hdr; 936 const struct linux_efi_initrd *initrd = NULL; 937 unsigned long kernel_entry; 938 efi_status_t status; 939 940 boot_params_ptr = boot_params; 941 942 efi_system_table = sys_table_arg; 943 /* Check if we were booted by the EFI firmware */ 944 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 945 efi_exit(handle, EFI_INVALID_PARAMETER); 946 947 if (have_unsupported_snp_features()) 948 efi_exit(handle, EFI_UNSUPPORTED); 949 950 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) { 951 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID); 952 if (efi_dxe_table && 953 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) { 954 efi_warn("Ignoring DXE services table: invalid signature\n"); 955 efi_dxe_table = NULL; 956 } 957 } 958 959 /* grab the memory attributes protocol if it exists */ 960 efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr); 961 962 status = efi_setup_5level_paging(); 963 if (status != EFI_SUCCESS) { 964 efi_err("efi_setup_5level_paging() failed!\n"); 965 goto fail; 966 } 967 968 #ifdef CONFIG_CMDLINE_BOOL 969 status = parse_options(CONFIG_CMDLINE); 970 if (status != EFI_SUCCESS) { 971 efi_err("Failed to parse options\n"); 972 goto fail; 973 } 974 #endif 975 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) { 976 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr | 977 ((u64)boot_params->ext_cmd_line_ptr << 32)); 978 status = parse_options((char *)cmdline_paddr); 979 if (status != EFI_SUCCESS) { 980 efi_err("Failed to parse options\n"); 981 goto fail; 982 } 983 } 984 985 if (efi_mem_encrypt > 0) 986 hdr->xloadflags |= XLF_MEM_ENCRYPTION; 987 988 status = efi_decompress_kernel(&kernel_entry); 989 if (status != EFI_SUCCESS) { 990 efi_err("Failed to decompress kernel\n"); 991 goto fail; 992 } 993 994 /* 995 * At this point, an initrd may already have been loaded by the 996 * bootloader and passed via bootparams. We permit an initrd loaded 997 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it. 998 * 999 * If the device path is not present, any command-line initrd= 1000 * arguments will be processed only if image is not NULL, which will be 1001 * the case only if we were loaded via the PE entry point. 1002 */ 1003 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX, 1004 &initrd); 1005 if (status != EFI_SUCCESS) 1006 goto fail; 1007 if (initrd && initrd->size > 0) { 1008 efi_set_u64_split(initrd->base, &hdr->ramdisk_image, 1009 &boot_params->ext_ramdisk_image); 1010 efi_set_u64_split(initrd->size, &hdr->ramdisk_size, 1011 &boot_params->ext_ramdisk_size); 1012 } 1013 1014 1015 /* 1016 * If the boot loader gave us a value for secure_boot then we use that, 1017 * otherwise we ask the BIOS. 1018 */ 1019 if (boot_params->secure_boot == efi_secureboot_mode_unset) 1020 boot_params->secure_boot = efi_get_secureboot(); 1021 1022 /* Ask the firmware to clear memory on unclean shutdown */ 1023 efi_enable_reset_attack_mitigation(); 1024 1025 efi_random_get_seed(); 1026 1027 efi_retrieve_eventlog(); 1028 1029 setup_graphics(boot_params); 1030 1031 setup_efi_pci(boot_params); 1032 1033 setup_quirks(boot_params); 1034 1035 setup_unaccepted_memory(); 1036 1037 status = exit_boot(boot_params, handle); 1038 if (status != EFI_SUCCESS) { 1039 efi_err("exit_boot() failed!\n"); 1040 goto fail; 1041 } 1042 1043 /* 1044 * Call the SEV init code while still running with the firmware's 1045 * GDT/IDT, so #VC exceptions will be handled by EFI. 1046 */ 1047 sev_enable(boot_params); 1048 1049 efi_5level_switch(); 1050 1051 enter_kernel(kernel_entry, boot_params); 1052 fail: 1053 efi_err("efi_stub_entry() failed!\n"); 1054 1055 efi_exit(handle, status); 1056 } 1057 1058 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL 1059 void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, 1060 struct boot_params *boot_params) 1061 { 1062 memset(_bss, 0, _ebss - _bss); 1063 efi_stub_entry(handle, sys_table_arg, boot_params); 1064 } 1065 1066 #ifndef CONFIG_EFI_MIXED 1067 extern __alias(efi_handover_entry) 1068 void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, 1069 struct boot_params *boot_params); 1070 1071 extern __alias(efi_handover_entry) 1072 void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg, 1073 struct boot_params *boot_params); 1074 #endif 1075 #endif 1076