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; 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 hdr = &((struct boot_params *)image_base)->hdr; 396 397 status = efi_allocate_pages(0x4000, (unsigned long *)&boot_params, ULONG_MAX); 398 if (status != EFI_SUCCESS) { 399 efi_printk("Failed to allocate lowmem for boot params\n"); 400 efi_exit(handle, status); 401 } 402 403 memset(boot_params, 0x0, 0x4000); 404 405 hdr = &boot_params->hdr; 406 407 /* Copy the second sector to boot_params */ 408 memcpy(&hdr->jump, image_base + 512, 512); 409 410 /* 411 * Fill out some of the header fields ourselves because the 412 * EFI firmware loader doesn't load the first sector. 413 */ 414 hdr->root_flags = 1; 415 hdr->vid_mode = 0xffff; 416 hdr->boot_flag = 0xAA55; 417 418 hdr->type_of_loader = 0x21; 419 420 /* Convert unicode cmdline to ascii */ 421 cmdline_ptr = efi_convert_cmdline(image, &options_size, ULONG_MAX); 422 if (!cmdline_ptr) 423 goto fail; 424 425 hdr->cmd_line_ptr = (unsigned long)cmdline_ptr; 426 /* Fill in upper bits of command line address, NOP on 32 bit */ 427 boot_params->ext_cmd_line_ptr = (u64)(unsigned long)cmdline_ptr >> 32; 428 429 hdr->ramdisk_image = 0; 430 hdr->ramdisk_size = 0; 431 432 if (efi_is_native()) { 433 status = efi_parse_options(cmdline_ptr); 434 if (status != EFI_SUCCESS) 435 goto fail2; 436 437 if (!noinitrd()) { 438 status = efi_load_initrd(image, &ramdisk_addr, 439 &ramdisk_size, 440 hdr->initrd_addr_max, 441 ULONG_MAX); 442 if (status != EFI_SUCCESS) 443 goto fail2; 444 hdr->ramdisk_image = ramdisk_addr & 0xffffffff; 445 hdr->ramdisk_size = ramdisk_size & 0xffffffff; 446 boot_params->ext_ramdisk_image = (u64)ramdisk_addr >> 32; 447 boot_params->ext_ramdisk_size = (u64)ramdisk_size >> 32; 448 } 449 } 450 451 efi_stub_entry(handle, sys_table, boot_params); 452 /* not reached */ 453 454 fail2: 455 efi_free(options_size, (unsigned long)cmdline_ptr); 456 fail: 457 efi_free(0x4000, (unsigned long)boot_params); 458 459 efi_exit(handle, status); 460 } 461 462 static void add_e820ext(struct boot_params *params, 463 struct setup_data *e820ext, u32 nr_entries) 464 { 465 struct setup_data *data; 466 467 e820ext->type = SETUP_E820_EXT; 468 e820ext->len = nr_entries * sizeof(struct boot_e820_entry); 469 e820ext->next = 0; 470 471 data = (struct setup_data *)(unsigned long)params->hdr.setup_data; 472 473 while (data && data->next) 474 data = (struct setup_data *)(unsigned long)data->next; 475 476 if (data) 477 data->next = (unsigned long)e820ext; 478 else 479 params->hdr.setup_data = (unsigned long)e820ext; 480 } 481 482 static efi_status_t 483 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size) 484 { 485 struct boot_e820_entry *entry = params->e820_table; 486 struct efi_info *efi = ¶ms->efi_info; 487 struct boot_e820_entry *prev = NULL; 488 u32 nr_entries; 489 u32 nr_desc; 490 int i; 491 492 nr_entries = 0; 493 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size; 494 495 for (i = 0; i < nr_desc; i++) { 496 efi_memory_desc_t *d; 497 unsigned int e820_type = 0; 498 unsigned long m = efi->efi_memmap; 499 500 #ifdef CONFIG_X86_64 501 m |= (u64)efi->efi_memmap_hi << 32; 502 #endif 503 504 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i); 505 switch (d->type) { 506 case EFI_RESERVED_TYPE: 507 case EFI_RUNTIME_SERVICES_CODE: 508 case EFI_RUNTIME_SERVICES_DATA: 509 case EFI_MEMORY_MAPPED_IO: 510 case EFI_MEMORY_MAPPED_IO_PORT_SPACE: 511 case EFI_PAL_CODE: 512 e820_type = E820_TYPE_RESERVED; 513 break; 514 515 case EFI_UNUSABLE_MEMORY: 516 e820_type = E820_TYPE_UNUSABLE; 517 break; 518 519 case EFI_ACPI_RECLAIM_MEMORY: 520 e820_type = E820_TYPE_ACPI; 521 break; 522 523 case EFI_LOADER_CODE: 524 case EFI_LOADER_DATA: 525 case EFI_BOOT_SERVICES_CODE: 526 case EFI_BOOT_SERVICES_DATA: 527 case EFI_CONVENTIONAL_MEMORY: 528 if (efi_soft_reserve_enabled() && 529 (d->attribute & EFI_MEMORY_SP)) 530 e820_type = E820_TYPE_SOFT_RESERVED; 531 else 532 e820_type = E820_TYPE_RAM; 533 break; 534 535 case EFI_ACPI_MEMORY_NVS: 536 e820_type = E820_TYPE_NVS; 537 break; 538 539 case EFI_PERSISTENT_MEMORY: 540 e820_type = E820_TYPE_PMEM; 541 break; 542 543 default: 544 continue; 545 } 546 547 /* Merge adjacent mappings */ 548 if (prev && prev->type == e820_type && 549 (prev->addr + prev->size) == d->phys_addr) { 550 prev->size += d->num_pages << 12; 551 continue; 552 } 553 554 if (nr_entries == ARRAY_SIZE(params->e820_table)) { 555 u32 need = (nr_desc - i) * sizeof(struct e820_entry) + 556 sizeof(struct setup_data); 557 558 if (!e820ext || e820ext_size < need) 559 return EFI_BUFFER_TOO_SMALL; 560 561 /* boot_params map full, switch to e820 extended */ 562 entry = (struct boot_e820_entry *)e820ext->data; 563 } 564 565 entry->addr = d->phys_addr; 566 entry->size = d->num_pages << PAGE_SHIFT; 567 entry->type = e820_type; 568 prev = entry++; 569 nr_entries++; 570 } 571 572 if (nr_entries > ARRAY_SIZE(params->e820_table)) { 573 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table); 574 575 add_e820ext(params, e820ext, nr_e820ext); 576 nr_entries -= nr_e820ext; 577 } 578 579 params->e820_entries = (u8)nr_entries; 580 581 return EFI_SUCCESS; 582 } 583 584 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext, 585 u32 *e820ext_size) 586 { 587 efi_status_t status; 588 unsigned long size; 589 590 size = sizeof(struct setup_data) + 591 sizeof(struct e820_entry) * nr_desc; 592 593 if (*e820ext) { 594 efi_bs_call(free_pool, *e820ext); 595 *e820ext = NULL; 596 *e820ext_size = 0; 597 } 598 599 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size, 600 (void **)e820ext); 601 if (status == EFI_SUCCESS) 602 *e820ext_size = size; 603 604 return status; 605 } 606 607 static efi_status_t allocate_e820(struct boot_params *params, 608 struct setup_data **e820ext, 609 u32 *e820ext_size) 610 { 611 unsigned long map_size, desc_size, buff_size; 612 struct efi_boot_memmap boot_map; 613 efi_memory_desc_t *map; 614 efi_status_t status; 615 __u32 nr_desc; 616 617 boot_map.map = ↦ 618 boot_map.map_size = &map_size; 619 boot_map.desc_size = &desc_size; 620 boot_map.desc_ver = NULL; 621 boot_map.key_ptr = NULL; 622 boot_map.buff_size = &buff_size; 623 624 status = efi_get_memory_map(&boot_map); 625 if (status != EFI_SUCCESS) 626 return status; 627 628 nr_desc = buff_size / desc_size; 629 630 if (nr_desc > ARRAY_SIZE(params->e820_table)) { 631 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table); 632 633 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size); 634 if (status != EFI_SUCCESS) 635 return status; 636 } 637 638 return EFI_SUCCESS; 639 } 640 641 struct exit_boot_struct { 642 struct boot_params *boot_params; 643 struct efi_info *efi; 644 }; 645 646 static efi_status_t exit_boot_func(struct efi_boot_memmap *map, 647 void *priv) 648 { 649 const char *signature; 650 struct exit_boot_struct *p = priv; 651 652 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE 653 : EFI32_LOADER_SIGNATURE; 654 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32)); 655 656 p->efi->efi_systab = (unsigned long)efi_system_table(); 657 p->efi->efi_memdesc_size = *map->desc_size; 658 p->efi->efi_memdesc_version = *map->desc_ver; 659 p->efi->efi_memmap = (unsigned long)*map->map; 660 p->efi->efi_memmap_size = *map->map_size; 661 662 #ifdef CONFIG_X86_64 663 p->efi->efi_systab_hi = (unsigned long)efi_system_table() >> 32; 664 p->efi->efi_memmap_hi = (unsigned long)*map->map >> 32; 665 #endif 666 667 return EFI_SUCCESS; 668 } 669 670 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle) 671 { 672 unsigned long map_sz, key, desc_size, buff_size; 673 efi_memory_desc_t *mem_map; 674 struct setup_data *e820ext = NULL; 675 __u32 e820ext_size = 0; 676 efi_status_t status; 677 __u32 desc_version; 678 struct efi_boot_memmap map; 679 struct exit_boot_struct priv; 680 681 map.map = &mem_map; 682 map.map_size = &map_sz; 683 map.desc_size = &desc_size; 684 map.desc_ver = &desc_version; 685 map.key_ptr = &key; 686 map.buff_size = &buff_size; 687 priv.boot_params = boot_params; 688 priv.efi = &boot_params->efi_info; 689 690 status = allocate_e820(boot_params, &e820ext, &e820ext_size); 691 if (status != EFI_SUCCESS) 692 return status; 693 694 /* Might as well exit boot services now */ 695 status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func); 696 if (status != EFI_SUCCESS) 697 return status; 698 699 /* Historic? */ 700 boot_params->alt_mem_k = 32 * 1024; 701 702 status = setup_e820(boot_params, e820ext, e820ext_size); 703 if (status != EFI_SUCCESS) 704 return status; 705 706 return EFI_SUCCESS; 707 } 708 709 /* 710 * On success, we return the address of startup_32, which has potentially been 711 * relocated by efi_relocate_kernel. 712 * On failure, we exit to the firmware via efi_exit instead of returning. 713 */ 714 unsigned long efi_main(efi_handle_t handle, 715 efi_system_table_t *sys_table_arg, 716 struct boot_params *boot_params) 717 { 718 unsigned long bzimage_addr = (unsigned long)startup_32; 719 unsigned long buffer_start, buffer_end; 720 struct setup_header *hdr = &boot_params->hdr; 721 efi_status_t status; 722 unsigned long cmdline_paddr; 723 724 sys_table = sys_table_arg; 725 726 /* Check if we were booted by the EFI firmware */ 727 if (sys_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE) 728 efi_exit(handle, EFI_INVALID_PARAMETER); 729 730 /* 731 * If the kernel isn't already loaded at a suitable address, 732 * relocate it. 733 * 734 * It must be loaded above LOAD_PHYSICAL_ADDR. 735 * 736 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This 737 * is defined as the macro MAXMEM, but unfortunately that is not a 738 * compile-time constant if 5-level paging is configured, so we instead 739 * define our own macro for use here. 740 * 741 * For 32-bit, the maximum address is complicated to figure out, for 742 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what 743 * KASLR uses. 744 * 745 * Also relocate it if image_offset is zero, i.e. we weren't loaded by 746 * LoadImage, but we are not aligned correctly. 747 */ 748 749 buffer_start = ALIGN(bzimage_addr - image_offset, 750 hdr->kernel_alignment); 751 buffer_end = buffer_start + hdr->init_size; 752 753 if ((buffer_start < LOAD_PHYSICAL_ADDR) || 754 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) || 755 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) || 756 (image_offset == 0 && !IS_ALIGNED(bzimage_addr, 757 hdr->kernel_alignment))) { 758 status = efi_relocate_kernel(&bzimage_addr, 759 hdr->init_size, hdr->init_size, 760 hdr->pref_address, 761 hdr->kernel_alignment, 762 LOAD_PHYSICAL_ADDR); 763 if (status != EFI_SUCCESS) { 764 efi_printk("efi_relocate_kernel() failed!\n"); 765 goto fail; 766 } 767 /* 768 * Now that we've copied the kernel elsewhere, we no longer 769 * have a set up block before startup_32(), so reset image_offset 770 * to zero in case it was set earlier. 771 */ 772 image_offset = 0; 773 } 774 775 /* 776 * efi_pe_entry() may have been called before efi_main(), in which 777 * case this is the second time we parse the cmdline. This is ok, 778 * parsing the cmdline multiple times does not have side-effects. 779 */ 780 cmdline_paddr = ((u64)hdr->cmd_line_ptr | 781 ((u64)boot_params->ext_cmd_line_ptr << 32)); 782 efi_parse_options((char *)cmdline_paddr); 783 784 /* 785 * At this point, an initrd may already have been loaded, either by 786 * the bootloader and passed via bootparams, or loaded from a initrd= 787 * command line option by efi_pe_entry() above. In either case, we 788 * permit an initrd loaded from the LINUX_EFI_INITRD_MEDIA_GUID device 789 * path to supersede it. 790 */ 791 if (!noinitrd()) { 792 unsigned long addr, size; 793 794 status = efi_load_initrd_dev_path(&addr, &size, ULONG_MAX); 795 if (status == EFI_SUCCESS) { 796 hdr->ramdisk_image = (u32)addr; 797 hdr->ramdisk_size = (u32)size; 798 boot_params->ext_ramdisk_image = (u64)addr >> 32; 799 boot_params->ext_ramdisk_size = (u64)size >> 32; 800 } else if (status != EFI_NOT_FOUND) { 801 efi_printk("efi_load_initrd_dev_path() failed!\n"); 802 goto fail; 803 } 804 } 805 806 /* 807 * If the boot loader gave us a value for secure_boot then we use that, 808 * otherwise we ask the BIOS. 809 */ 810 if (boot_params->secure_boot == efi_secureboot_mode_unset) 811 boot_params->secure_boot = efi_get_secureboot(); 812 813 /* Ask the firmware to clear memory on unclean shutdown */ 814 efi_enable_reset_attack_mitigation(); 815 816 efi_random_get_seed(); 817 818 efi_retrieve_tpm2_eventlog(); 819 820 setup_graphics(boot_params); 821 822 setup_efi_pci(boot_params); 823 824 setup_quirks(boot_params); 825 826 status = exit_boot(boot_params, handle); 827 if (status != EFI_SUCCESS) { 828 efi_printk("exit_boot() failed!\n"); 829 goto fail; 830 } 831 832 return bzimage_addr; 833 fail: 834 efi_printk("efi_main() failed!\n"); 835 836 efi_exit(handle, status); 837 } 838