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