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