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 12 #include <asm/efi.h> 13 #include <asm/e820/types.h> 14 #include <asm/setup.h> 15 #include <asm/desc.h> 16 #include <asm/boot.h> 17 18 #include "efistub.h" 19 20 /* Maximum physical address for 64-bit kernel with 4-level paging */ 21 #define MAXMEM_X86_64_4LEVEL (1ull << 46) 22 23 static efi_system_table_t *sys_table __efistub_global; 24 extern const bool efi_is64; 25 extern u32 image_offset; 26 27 __pure efi_system_table_t *efi_system_table(void) 28 { 29 return sys_table; 30 } 31 32 __attribute_const__ bool efi_is_64bit(void) 33 { 34 if (IS_ENABLED(CONFIG_EFI_MIXED)) 35 return efi_is64; 36 return IS_ENABLED(CONFIG_X86_64); 37 } 38 39 static efi_status_t 40 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom) 41 { 42 struct pci_setup_rom *rom = NULL; 43 efi_status_t status; 44 unsigned long size; 45 uint64_t romsize; 46 void *romimage; 47 48 /* 49 * Some firmware images contain EFI function pointers at the place where 50 * the romimage and romsize fields are supposed to be. Typically the EFI 51 * code is mapped at high addresses, translating to an unrealistically 52 * large romsize. The UEFI spec limits the size of option ROMs to 16 53 * MiB so we reject any ROMs over 16 MiB in size to catch this. 54 */ 55 romimage = efi_table_attr(pci, romimage); 56 romsize = efi_table_attr(pci, romsize); 57 if (!romimage || !romsize || romsize > SZ_16M) 58 return EFI_INVALID_PARAMETER; 59 60 size = romsize + sizeof(*rom); 61 62 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 63 (void **)&rom); 64 if (status != EFI_SUCCESS) { 65 efi_printk("Failed to allocate memory for 'rom'\n"); 66 return status; 67 } 68 69 memset(rom, 0, sizeof(*rom)); 70 71 rom->data.type = SETUP_PCI; 72 rom->data.len = size - sizeof(struct setup_data); 73 rom->data.next = 0; 74 rom->pcilen = pci->romsize; 75 *__rom = rom; 76 77 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 78 PCI_VENDOR_ID, 1, &rom->vendor); 79 80 if (status != EFI_SUCCESS) { 81 efi_printk("Failed to read rom->vendor\n"); 82 goto free_struct; 83 } 84 85 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16, 86 PCI_DEVICE_ID, 1, &rom->devid); 87 88 if (status != EFI_SUCCESS) { 89 efi_printk("Failed to read rom->devid\n"); 90 goto free_struct; 91 } 92 93 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus, 94 &rom->device, &rom->function); 95 96 if (status != EFI_SUCCESS) 97 goto free_struct; 98 99 memcpy(rom->romdata, romimage, romsize); 100 return status; 101 102 free_struct: 103 efi_bs_call(free_pool, rom); 104 return status; 105 } 106 107 /* 108 * There's no way to return an informative status from this function, 109 * because any analysis (and printing of error messages) needs to be 110 * done directly at the EFI function call-site. 111 * 112 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we 113 * just didn't find any PCI devices, but there's no way to tell outside 114 * the context of the call. 115 */ 116 static void setup_efi_pci(struct boot_params *params) 117 { 118 efi_status_t status; 119 void **pci_handle = NULL; 120 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID; 121 unsigned long size = 0; 122 struct setup_data *data; 123 efi_handle_t h; 124 int i; 125 126 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 127 &pci_proto, NULL, &size, pci_handle); 128 129 if (status == EFI_BUFFER_TOO_SMALL) { 130 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 131 (void **)&pci_handle); 132 133 if (status != EFI_SUCCESS) { 134 efi_printk("Failed to allocate memory for 'pci_handle'\n"); 135 return; 136 } 137 138 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 139 &pci_proto, NULL, &size, pci_handle); 140 } 141 142 if (status != EFI_SUCCESS) 143 goto free_handle; 144 145 data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 146 147 while (data && data->next) 148 data = (struct setup_data *)(unsigned long)data->next; 149 150 for_each_efi_handle(h, pci_handle, size, i) { 151 efi_pci_io_protocol_t *pci = NULL; 152 struct pci_setup_rom *rom; 153 154 status = efi_bs_call(handle_protocol, h, &pci_proto, 155 (void **)&pci); 156 if (status != EFI_SUCCESS || !pci) 157 continue; 158 159 status = preserve_pci_rom_image(pci, &rom); 160 if (status != EFI_SUCCESS) 161 continue; 162 163 if (data) 164 data->next = (unsigned long)rom; 165 else 166 params->hdr.setup_data = (unsigned long)rom; 167 168 data = (struct setup_data *)rom; 169 } 170 171 free_handle: 172 efi_bs_call(free_pool, pci_handle); 173 } 174 175 static void retrieve_apple_device_properties(struct boot_params *boot_params) 176 { 177 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID; 178 struct setup_data *data, *new; 179 efi_status_t status; 180 u32 size = 0; 181 apple_properties_protocol_t *p; 182 183 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p); 184 if (status != EFI_SUCCESS) 185 return; 186 187 if (efi_table_attr(p, version) != 0x10000) { 188 efi_printk("Unsupported properties proto version\n"); 189 return; 190 } 191 192 efi_call_proto(p, get_all, NULL, &size); 193 if (!size) 194 return; 195 196 do { 197 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, 198 size + sizeof(struct setup_data), 199 (void **)&new); 200 if (status != EFI_SUCCESS) { 201 efi_printk("Failed to allocate memory for 'properties'\n"); 202 return; 203 } 204 205 status = efi_call_proto(p, get_all, new->data, &size); 206 207 if (status == EFI_BUFFER_TOO_SMALL) 208 efi_bs_call(free_pool, new); 209 } while (status == EFI_BUFFER_TOO_SMALL); 210 211 new->type = SETUP_APPLE_PROPERTIES; 212 new->len = size; 213 new->next = 0; 214 215 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data; 216 if (!data) { 217 boot_params->hdr.setup_data = (unsigned long)new; 218 } else { 219 while (data->next) 220 data = (struct setup_data *)(unsigned long)data->next; 221 data->next = (unsigned long)new; 222 } 223 } 224 225 static const efi_char16_t apple[] = L"Apple"; 226 227 static void setup_quirks(struct boot_params *boot_params) 228 { 229 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long) 230 efi_table_attr(efi_system_table(), fw_vendor); 231 232 if (!memcmp(fw_vendor, apple, sizeof(apple))) { 233 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES)) 234 retrieve_apple_device_properties(boot_params); 235 } 236 } 237 238 /* 239 * See if we have Universal Graphics Adapter (UGA) protocol 240 */ 241 static efi_status_t 242 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size) 243 { 244 efi_status_t status; 245 u32 width, height; 246 void **uga_handle = NULL; 247 efi_uga_draw_protocol_t *uga = NULL, *first_uga; 248 efi_handle_t handle; 249 int i; 250 251 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 252 (void **)&uga_handle); 253 if (status != EFI_SUCCESS) 254 return status; 255 256 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 257 uga_proto, NULL, &size, uga_handle); 258 if (status != EFI_SUCCESS) 259 goto free_handle; 260 261 height = 0; 262 width = 0; 263 264 first_uga = NULL; 265 for_each_efi_handle(handle, uga_handle, size, i) { 266 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID; 267 u32 w, h, depth, refresh; 268 void *pciio; 269 270 status = efi_bs_call(handle_protocol, handle, uga_proto, 271 (void **)&uga); 272 if (status != EFI_SUCCESS) 273 continue; 274 275 pciio = NULL; 276 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio); 277 278 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh); 279 if (status == EFI_SUCCESS && (!first_uga || pciio)) { 280 width = w; 281 height = h; 282 283 /* 284 * Once we've found a UGA supporting PCIIO, 285 * don't bother looking any further. 286 */ 287 if (pciio) 288 break; 289 290 first_uga = uga; 291 } 292 } 293 294 if (!width && !height) 295 goto free_handle; 296 297 /* EFI framebuffer */ 298 si->orig_video_isVGA = VIDEO_TYPE_EFI; 299 300 si->lfb_depth = 32; 301 si->lfb_width = width; 302 si->lfb_height = height; 303 304 si->red_size = 8; 305 si->red_pos = 16; 306 si->green_size = 8; 307 si->green_pos = 8; 308 si->blue_size = 8; 309 si->blue_pos = 0; 310 si->rsvd_size = 8; 311 si->rsvd_pos = 24; 312 313 free_handle: 314 efi_bs_call(free_pool, uga_handle); 315 316 return status; 317 } 318 319 static void setup_graphics(struct boot_params *boot_params) 320 { 321 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; 322 struct screen_info *si; 323 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID; 324 efi_status_t status; 325 unsigned long size; 326 void **gop_handle = NULL; 327 void **uga_handle = NULL; 328 329 si = &boot_params->screen_info; 330 memset(si, 0, sizeof(*si)); 331 332 size = 0; 333 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 334 &graphics_proto, NULL, &size, gop_handle); 335 if (status == EFI_BUFFER_TOO_SMALL) 336 status = efi_setup_gop(si, &graphics_proto, size); 337 338 if (status != EFI_SUCCESS) { 339 size = 0; 340 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL, 341 &uga_proto, NULL, &size, uga_handle); 342 if (status == EFI_BUFFER_TOO_SMALL) 343 setup_uga(si, &uga_proto, size); 344 } 345 } 346 347 348 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status) 349 { 350 efi_bs_call(exit, handle, status, 0, NULL); 351 for(;;) 352 asm("hlt"); 353 } 354 355 void startup_32(struct boot_params *boot_params); 356 357 void __noreturn efi_stub_entry(efi_handle_t handle, 358 efi_system_table_t *sys_table_arg, 359 struct boot_params *boot_params); 360 361 /* 362 * Because the x86 boot code expects to be passed a boot_params we 363 * need to create one ourselves (usually the bootloader would create 364 * one for us). 365 */ 366 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle, 367 efi_system_table_t *sys_table_arg) 368 { 369 struct boot_params *boot_params; 370 struct setup_header *hdr; 371 efi_loaded_image_t *image; 372 void *image_base; 373 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID; 374 int options_size = 0; 375 efi_status_t status; 376 char *cmdline_ptr; 377 unsigned long ramdisk_addr; 378 unsigned long ramdisk_size; 379 380 sys_table = sys_table_arg; 381 382 /* Check if we were booted by the EFI firmware */ 383 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 384 efi_exit(handle, EFI_INVALID_PARAMETER); 385 386 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image); 387 if (status != EFI_SUCCESS) { 388 efi_printk("Failed to get handle for LOADED_IMAGE_PROTOCOL\n"); 389 efi_exit(handle, status); 390 } 391 392 image_base = efi_table_attr(image, image_base); 393 image_offset = (void *)startup_32 - image_base; 394 395 status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params, ULONG_MAX); 396 if (status != EFI_SUCCESS) { 397 efi_printk("Failed to allocate lowmem for boot params\n"); 398 efi_exit(handle, status); 399 } 400 401 memset(boot_params, 0x0, 0x4000); 402 403 hdr = &boot_params->hdr; 404 405 /* Copy the second sector to boot_params */ 406 memcpy(&hdr->jump, image_base + 512, 512); 407 408 /* 409 * Fill out some of the header fields ourselves because the 410 * EFI firmware loader doesn't load the first sector. 411 */ 412 hdr->root_flags = 1; 413 hdr->vid_mode = 0xffff; 414 hdr->boot_flag = 0xAA55; 415 416 hdr->type_of_loader = 0x21; 417 418 /* Convert unicode cmdline to ascii */ 419 cmdline_ptr = efi_convert_cmdline(image, &options_size, ULONG_MAX); 420 if (!cmdline_ptr) 421 goto fail; 422 423 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; 424 /* Fill in upper bits of command line address, NOP on 32 bit */ 425 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; 426 427 hdr->ramdisk_image = 0; 428 hdr->ramdisk_size = 0; 429 430 if (efi_is_native()) { 431 status = efi_parse_options(cmdline_ptr); 432 if (status != EFI_SUCCESS) 433 goto fail2; 434 435 if (!noinitrd()) { 436 status = efi_load_initrd(image, &ramdisk_addr, 437 &ramdisk_size, 438 hdr->initrd_addr_max, 439 ULONG_MAX); 440 if (status != EFI_SUCCESS) 441 goto fail2; 442 hdr->ramdisk_image = ramdisk_addr & 0xffffffff; 443 hdr->ramdisk_size = ramdisk_size & 0xffffffff; 444 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; 445 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; 446 } 447 } 448 449 efi_stub_entry(handle, sys_table, boot_params); 450 /* not reached */ 451 452 fail2: 453 efi_free(options_size, (unsigned long)cmdline_ptr); 454 fail: 455 efi_free(0x4000, (unsigned long)boot_params); 456 457 efi_exit(handle, status); 458 } 459 460 static void add_e820ext(struct boot_params *params, 461 struct setup_data *e820ext, u32 nr_entries) 462 { 463 struct setup_data *data; 464 465 e820ext->type = SETUP_E820_EXT; 466 e820ext->len = nr_entries * sizeof(struct boot_e820_entry); 467 e820ext->next = 0; 468 469 data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 470 471 while (data && data->next) 472 data = (struct setup_data *)(unsigned long)data->next; 473 474 if (data) 475 data->next = (unsigned long)e820ext; 476 else 477 params->hdr.setup_data = (unsigned long)e820ext; 478 } 479 480 static efi_status_t 481 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) 482 { 483 struct boot_e820_entry *entry = params->e820_table; 484 struct efi_info *efi = ¶ms->efi_info; 485 struct boot_e820_entry *prev = NULL; 486 u32 nr_entries; 487 u32 nr_desc; 488 int i; 489 490 nr_entries = 0; 491 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; 492 493 for (i = 0; i < nr_desc; i++) { 494 efi_memory_desc_t *d; 495 unsigned int e820_type = 0; 496 unsigned long m = efi->efi_memmap; 497 498 #ifdef CONFIG_X86_64 499 m |= (u64)efi->efi_memmap_hi << 32; 500 #endif 501 502 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); 503 switch (d->type) { 504 case EFI_RESERVED_TYPE: 505 case EFI_RUNTIME_SERVICES_CODE: 506 case EFI_RUNTIME_SERVICES_DATA: 507 case EFI_MEMORY_MAPPED_IO: 508 case EFI_MEMORY_MAPPED_IO_PORT_SPACE: 509 case EFI_PAL_CODE: 510 e820_type = E820_TYPE_RESERVED; 511 break; 512 513 case EFI_UNUSABLE_MEMORY: 514 e820_type = E820_TYPE_UNUSABLE; 515 break; 516 517 case EFI_ACPI_RECLAIM_MEMORY: 518 e820_type = E820_TYPE_ACPI; 519 break; 520 521 case EFI_LOADER_CODE: 522 case EFI_LOADER_DATA: 523 case EFI_BOOT_SERVICES_CODE: 524 case EFI_BOOT_SERVICES_DATA: 525 case EFI_CONVENTIONAL_MEMORY: 526 if (efi_soft_reserve_enabled() && 527 (d->attribute & EFI_MEMORY_SP)) 528 e820_type = E820_TYPE_SOFT_RESERVED; 529 else 530 e820_type = E820_TYPE_RAM; 531 break; 532 533 case EFI_ACPI_MEMORY_NVS: 534 e820_type = E820_TYPE_NVS; 535 break; 536 537 case EFI_PERSISTENT_MEMORY: 538 e820_type = E820_TYPE_PMEM; 539 break; 540 541 default: 542 continue; 543 } 544 545 /* Merge adjacent mappings */ 546 if (prev && prev->type == e820_type && 547 (prev->addr + prev->size) == d->phys_addr) { 548 prev->size += d->num_pages << 12; 549 continue; 550 } 551 552 if (nr_entries == ARRAY_SIZE(params->e820_table)) { 553 u32 need = (nr_desc - i) * sizeof(struct e820_entry) + 554 sizeof(struct setup_data); 555 556 if (!e820ext || e820ext_size < need) 557 return EFI_BUFFER_TOO_SMALL; 558 559 /* boot_params map full, switch to e820 extended */ 560 entry = (struct boot_e820_entry *)e820ext->data; 561 } 562 563 entry->addr = d->phys_addr; 564 entry->size = d->num_pages << PAGE_SHIFT; 565 entry->type = e820_type; 566 prev = entry++; 567 nr_entries++; 568 } 569 570 if (nr_entries > ARRAY_SIZE(params->e820_table)) { 571 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); 572 573 add_e820ext(params, e820ext, nr_e820ext); 574 nr_entries -= nr_e820ext; 575 } 576 577 params->e820_entries = (u8)nr_entries; 578 579 return EFI_SUCCESS; 580 } 581 582 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, 583 u32 *e820ext_size) 584 { 585 efi_status_t status; 586 unsigned long size; 587 588 size = sizeof(struct setup_data) + 589 sizeof(struct e820_entry) * nr_desc; 590 591 if (*e820ext) { 592 efi_bs_call(free_pool, *e820ext); 593 *e820ext = NULL; 594 *e820ext_size = 0; 595 } 596 597 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 598 (void **)e820ext); 599 if (status == EFI_SUCCESS) 600 *e820ext_size = size; 601 602 return status; 603 } 604 605 static efi_status_t allocate_e820(struct boot_params *params, 606 struct setup_data **e820ext, 607 u32 *e820ext_size) 608 { 609 unsigned long map_size, desc_size, buff_size; 610 struct efi_boot_memmap boot_map; 611 efi_memory_desc_t *map; 612 efi_status_t status; 613 __u32 nr_desc; 614 615 boot_map.map = ↦ 616 boot_map.map_size = &map_size; 617 boot_map.desc_size = &desc_size; 618 boot_map.desc_ver = NULL; 619 boot_map.key_ptr = NULL; 620 boot_map.buff_size = &buff_size; 621 622 status = efi_get_memory_map(&boot_map); 623 if (status != EFI_SUCCESS) 624 return status; 625 626 nr_desc = buff_size / desc_size; 627 628 if (nr_desc > ARRAY_SIZE(params->e820_table)) { 629 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); 630 631 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); 632 if (status != EFI_SUCCESS) 633 return status; 634 } 635 636 return EFI_SUCCESS; 637 } 638 639 struct exit_boot_struct { 640 struct boot_params *boot_params; 641 struct efi_info *efi; 642 }; 643 644 static efi_status_t exit_boot_func(struct efi_boot_memmap *map, 645 void *priv) 646 { 647 const char *signature; 648 struct exit_boot_struct *p = priv; 649 650 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE 651 : EFI32_LOADER_SIGNATURE; 652 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); 653 654 p->efi->efi_systab = (unsigned long)efi_system_table(); 655 p->efi->efi_memdesc_size = *map->desc_size; 656 p->efi->efi_memdesc_version = *map->desc_ver; 657 p->efi->efi_memmap = (unsigned long)*map->map; 658 p->efi->efi_memmap_size = *map->map_size; 659 660 #ifdef CONFIG_X86_64 661 p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32; 662 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32; 663 #endif 664 665 return EFI_SUCCESS; 666 } 667 668 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) 669 { 670 unsigned long map_sz, key, desc_size, buff_size; 671 efi_memory_desc_t *mem_map; 672 struct setup_data *e820ext = NULL; 673 __u32 e820ext_size = 0; 674 efi_status_t status; 675 __u32 desc_version; 676 struct efi_boot_memmap map; 677 struct exit_boot_struct priv; 678 679 map.map = &mem_map; 680 map.map_size = &map_sz; 681 map.desc_size = &desc_size; 682 map.desc_ver = &desc_version; 683 map.key_ptr = &key; 684 map.buff_size = &buff_size; 685 priv.boot_params = boot_params; 686 priv.efi = &boot_params->efi_info; 687 688 status = allocate_e820(boot_params, &e820ext, &e820ext_size); 689 if (status != EFI_SUCCESS) 690 return status; 691 692 /* Might as well exit boot services now */ 693 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func); 694 if (status != EFI_SUCCESS) 695 return status; 696 697 /* Historic? */ 698 boot_params->alt_mem_k = 32 * 1024; 699 700 status = setup_e820(boot_params, e820ext, e820ext_size); 701 if (status != EFI_SUCCESS) 702 return status; 703 704 return EFI_SUCCESS; 705 } 706 707 /* 708 * On success, we return the address of startup_32, which has potentially been 709 * relocated by efi_relocate_kernel. 710 * On failure, we exit to the firmware via efi_exit instead of returning. 711 */ 712 unsigned long efi_main(efi_handle_t handle, 713 efi_system_table_t *sys_table_arg, 714 struct boot_params *boot_params) 715 { 716 unsigned long bzimage_addr = (unsigned long)startup_32; 717 unsigned long buffer_start, buffer_end; 718 struct setup_header *hdr = &boot_params->hdr; 719 efi_status_t status; 720 unsigned long cmdline_paddr; 721 722 sys_table = sys_table_arg; 723 724 /* Check if we were booted by the EFI firmware */ 725 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 726 efi_exit(handle, EFI_INVALID_PARAMETER); 727 728 /* 729 * If the kernel isn't already loaded at a suitable address, 730 * relocate it. 731 * 732 * It must be loaded above LOAD_PHYSICAL_ADDR. 733 * 734 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This 735 * is defined as the macro MAXMEM, but unfortunately that is not a 736 * compile-time constant if 5-level paging is configured, so we instead 737 * define our own macro for use here. 738 * 739 * For 32-bit, the maximum address is complicated to figure out, for 740 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what 741 * KASLR uses. 742 * 743 * Also relocate it if image_offset is zero, i.e. the kernel wasn't 744 * loaded by LoadImage, but rather by a bootloader that called the 745 * handover entry. The reason we must always relocate in this case is 746 * to handle the case of systemd-boot booting a unified kernel image, 747 * which is a PE executable that contains the bzImage and an initrd as 748 * COFF sections. The initrd section is placed after the bzImage 749 * without ensuring that there are at least init_size bytes available 750 * for the bzImage, and thus the compressed kernel's startup code may 751 * overwrite the initrd unless it is moved out of the way. 752 */ 753 754 buffer_start = ALIGN(bzimage_addr - image_offset, 755 hdr->kernel_alignment); 756 buffer_end = buffer_start + hdr->init_size; 757 758 if ((buffer_start < LOAD_PHYSICAL_ADDR) || 759 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) || 760 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) || 761 (image_offset == 0)) { 762 status = efi_relocate_kernel(&bzimage_addr, 763 hdr->init_size, hdr->init_size, 764 hdr->pref_address, 765 hdr->kernel_alignment, 766 LOAD_PHYSICAL_ADDR); 767 if (status != EFI_SUCCESS) { 768 efi_printk("efi_relocate_kernel() failed!\n"); 769 goto fail; 770 } 771 /* 772 * Now that we've copied the kernel elsewhere, we no longer 773 * have a set up block before startup_32(), so reset image_offset 774 * to zero in case it was set earlier. 775 */ 776 image_offset = 0; 777 } 778 779 /* 780 * efi_pe_entry() may have been called before efi_main(), in which 781 * case this is the second time we parse the cmdline. This is ok, 782 * parsing the cmdline multiple times does not have side-effects. 783 */ 784 cmdline_paddr = ((u64)hdr->cmd_line_ptr | 785 ((u64)boot_params->ext_cmd_line_ptr << 32)); 786 efi_parse_options((char *)cmdline_paddr); 787 788 /* 789 * At this point, an initrd may already have been loaded, either by 790 * the bootloader and passed via bootparams, or loaded from a initrd= 791 * command line option by efi_pe_entry() above. In either case, we 792 * permit an initrd loaded from the LINUX_EFI_INITRD_MEDIA_GUID device 793 * path to supersede it. 794 */ 795 if (!noinitrd()) { 796 unsigned long addr, size; 797 798 status = efi_load_initrd_dev_path(&addr, &size, ULONG_MAX); 799 if (status == EFI_SUCCESS) { 800 hdr->ramdisk_image = (u32)addr; 801 hdr->ramdisk_size = (u32)size; 802 boot_params->ext_ramdisk_image = (u64)addr >> 32; 803 boot_params->ext_ramdisk_size = (u64)size >> 32; 804 } else if (status != EFI_NOT_FOUND) { 805 efi_printk("efi_load_initrd_dev_path() failed!\n"); 806 goto fail; 807 } 808 } 809 810 /* 811 * If the boot loader gave us a value for secure_boot then we use that, 812 * otherwise we ask the BIOS. 813 */ 814 if (boot_params->secure_boot == efi_secureboot_mode_unset) 815 boot_params->secure_boot = efi_get_secureboot(); 816 817 /* Ask the firmware to clear memory on unclean shutdown */ 818 efi_enable_reset_attack_mitigation(); 819 820 efi_random_get_seed(); 821 822 efi_retrieve_tpm2_eventlog(); 823 824 setup_graphics(boot_params); 825 826 setup_efi_pci(boot_params); 827 828 setup_quirks(boot_params); 829 830 status = exit_boot(boot_params, handle); 831 if (status != EFI_SUCCESS) { 832 efi_printk("exit_boot() failed!\n"); 833 goto fail; 834 } 835 836 return bzimage_addr; 837 fail: 838 efi_printk("efi_main() failed!\n"); 839 840 efi_exit(handle, status); 841 } 842