1 // SPDX-License-Identifier: GPL-2.0-only
3 /* -----------------------------------------------------------------------
5 * Copyright 2011 Intel Corporation; author Matt Fleming
7 * ----------------------------------------------------------------------- */
10 #include <linux/pci.h>
11 #include <linux/stddef.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
22 /* Maximum physical address for 64-bit kernel with 4-level paging */
23 #define MAXMEM_X86_64_4LEVEL (1ull << 46)
25 const efi_system_table_t *efi_system_table;
26 const efi_dxe_services_table_t *efi_dxe_table;
27 u32 image_offset __section(".data");
28 static efi_loaded_image_t *image = NULL;
30 typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
31 union sev_memory_acceptance_protocol {
33 efi_status_t (__efiapi * allow_unaccepted_memory)(
34 sev_memory_acceptance_protocol_t *);
37 u32 allow_unaccepted_memory;
42 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
44 struct pci_setup_rom *rom = NULL;
51 * Some firmware images contain EFI function pointers at the place where
52 * the romimage and romsize fields are supposed to be. Typically the EFI
53 * code is mapped at high addresses, translating to an unrealistically
54 * large romsize. The UEFI spec limits the size of option ROMs to 16
55 * MiB so we reject any ROMs over 16 MiB in size to catch this.
57 romimage = efi_table_attr(pci, romimage);
58 romsize = efi_table_attr(pci, romsize);
59 if (!romimage || !romsize || romsize > SZ_16M)
60 return EFI_INVALID_PARAMETER;
62 size = romsize + sizeof(*rom);
64 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
66 if (status != EFI_SUCCESS) {
67 efi_err("Failed to allocate memory for 'rom'\n");
71 memset(rom, 0, sizeof(*rom));
73 rom->data.type = SETUP_PCI;
74 rom->data.len = size - sizeof(struct setup_data);
76 rom->pcilen = pci->romsize;
79 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
80 PCI_VENDOR_ID, 1, &rom->vendor);
82 if (status != EFI_SUCCESS) {
83 efi_err("Failed to read rom->vendor\n");
87 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
88 PCI_DEVICE_ID, 1, &rom->devid);
90 if (status != EFI_SUCCESS) {
91 efi_err("Failed to read rom->devid\n");
95 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
96 &rom->device, &rom->function);
98 if (status != EFI_SUCCESS)
101 memcpy(rom->romdata, romimage, romsize);
105 efi_bs_call(free_pool, rom);
110 * There's no way to return an informative status from this function,
111 * because any analysis (and printing of error messages) needs to be
112 * done directly at the EFI function call-site.
114 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
115 * just didn't find any PCI devices, but there's no way to tell outside
116 * the context of the call.
118 static void setup_efi_pci(struct boot_params *params)
121 void **pci_handle = NULL;
122 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
123 unsigned long size = 0;
124 struct setup_data *data;
128 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
129 &pci_proto, NULL, &size, pci_handle);
131 if (status == EFI_BUFFER_TOO_SMALL) {
132 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
133 (void **)&pci_handle);
135 if (status != EFI_SUCCESS) {
136 efi_err("Failed to allocate memory for 'pci_handle'\n");
140 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
141 &pci_proto, NULL, &size, pci_handle);
144 if (status != EFI_SUCCESS)
147 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
149 while (data && data->next)
150 data = (struct setup_data *)(unsigned long)data->next;
152 for_each_efi_handle(h, pci_handle, size, i) {
153 efi_pci_io_protocol_t *pci = NULL;
154 struct pci_setup_rom *rom;
156 status = efi_bs_call(handle_protocol, h, &pci_proto,
158 if (status != EFI_SUCCESS || !pci)
161 status = preserve_pci_rom_image(pci, &rom);
162 if (status != EFI_SUCCESS)
166 data->next = (unsigned long)rom;
168 params->hdr.setup_data = (unsigned long)rom;
170 data = (struct setup_data *)rom;
174 efi_bs_call(free_pool, pci_handle);
177 static void retrieve_apple_device_properties(struct boot_params *boot_params)
179 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
180 struct setup_data *data, *new;
183 apple_properties_protocol_t *p;
185 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
186 if (status != EFI_SUCCESS)
189 if (efi_table_attr(p, version) != 0x10000) {
190 efi_err("Unsupported properties proto version\n");
194 efi_call_proto(p, get_all, NULL, &size);
199 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
200 size + sizeof(struct setup_data),
202 if (status != EFI_SUCCESS) {
203 efi_err("Failed to allocate memory for 'properties'\n");
207 status = efi_call_proto(p, get_all, new->data, &size);
209 if (status == EFI_BUFFER_TOO_SMALL)
210 efi_bs_call(free_pool, new);
211 } while (status == EFI_BUFFER_TOO_SMALL);
213 new->type = SETUP_APPLE_PROPERTIES;
217 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
219 boot_params->hdr.setup_data = (unsigned long)new;
222 data = (struct setup_data *)(unsigned long)data->next;
223 data->next = (unsigned long)new;
227 void efi_adjust_memory_range_protection(unsigned long start,
231 efi_gcd_memory_space_desc_t desc;
232 unsigned long end, next;
233 unsigned long rounded_start, rounded_end;
234 unsigned long unprotect_start, unprotect_size;
236 if (efi_dxe_table == NULL)
239 rounded_start = rounddown(start, EFI_PAGE_SIZE);
240 rounded_end = roundup(start + size, EFI_PAGE_SIZE);
243 * Don't modify memory region attributes, they are
244 * already suitable, to lower the possibility to
245 * encounter firmware bugs.
248 for (end = start + size; start < end; start = next) {
250 status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
252 if (status != EFI_SUCCESS)
255 next = desc.base_address + desc.length;
258 * Only system memory is suitable for trampoline/kernel image placement,
259 * so only this type of memory needs its attributes to be modified.
262 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
263 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
266 unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
267 unprotect_size = min(rounded_end, next) - unprotect_start;
269 status = efi_dxe_call(set_memory_space_attributes,
270 unprotect_start, unprotect_size,
273 if (status != EFI_SUCCESS) {
274 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
276 unprotect_start + unprotect_size,
282 extern const u8 startup_32[], startup_64[];
285 setup_memory_protection(unsigned long image_base, unsigned long image_size)
288 if (image_base != (unsigned long)startup_32)
289 efi_adjust_memory_range_protection(image_base, image_size);
292 * Clear protection flags on a whole range of possible
293 * addresses used for KASLR. We don't need to do that
294 * on x86_64, since KASLR/extraction is performed after
295 * dedicated identity page tables are built and we only
296 * need to remove possible protection on relocated image
297 * itself disregarding further relocations.
299 efi_adjust_memory_range_protection(LOAD_PHYSICAL_ADDR,
300 KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR);
304 static void setup_unaccepted_memory(void)
306 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
307 sev_memory_acceptance_protocol_t *proto;
310 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
314 * Enable unaccepted memory before calling exit boot services in order
315 * for the UEFI to not accept all memory on EBS.
317 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
319 if (status != EFI_SUCCESS)
322 status = efi_call_proto(proto, allow_unaccepted_memory);
323 if (status != EFI_SUCCESS)
324 efi_err("Memory acceptance protocol failed\n");
327 static const efi_char16_t apple[] = L"Apple";
329 static void setup_quirks(struct boot_params *boot_params,
330 unsigned long image_base,
331 unsigned long image_size)
333 efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
334 efi_table_attr(efi_system_table, fw_vendor);
336 if (!memcmp(fw_vendor, apple, sizeof(apple))) {
337 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
338 retrieve_apple_device_properties(boot_params);
341 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES))
342 setup_memory_protection(image_base, image_size);
346 * See if we have Universal Graphics Adapter (UGA) protocol
349 setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
353 void **uga_handle = NULL;
354 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
358 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
359 (void **)&uga_handle);
360 if (status != EFI_SUCCESS)
363 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
364 uga_proto, NULL, &size, uga_handle);
365 if (status != EFI_SUCCESS)
372 for_each_efi_handle(handle, uga_handle, size, i) {
373 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
374 u32 w, h, depth, refresh;
377 status = efi_bs_call(handle_protocol, handle, uga_proto,
379 if (status != EFI_SUCCESS)
383 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
385 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
386 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
391 * Once we've found a UGA supporting PCIIO,
392 * don't bother looking any further.
401 if (!width && !height)
404 /* EFI framebuffer */
405 si->orig_video_isVGA = VIDEO_TYPE_EFI;
408 si->lfb_width = width;
409 si->lfb_height = height;
421 efi_bs_call(free_pool, uga_handle);
426 static void setup_graphics(struct boot_params *boot_params)
428 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
429 struct screen_info *si;
430 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
433 void **gop_handle = NULL;
434 void **uga_handle = NULL;
436 si = &boot_params->screen_info;
437 memset(si, 0, sizeof(*si));
440 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
441 &graphics_proto, NULL, &size, gop_handle);
442 if (status == EFI_BUFFER_TOO_SMALL)
443 status = efi_setup_gop(si, &graphics_proto, size);
445 if (status != EFI_SUCCESS) {
447 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
448 &uga_proto, NULL, &size, uga_handle);
449 if (status == EFI_BUFFER_TOO_SMALL)
450 setup_uga(si, &uga_proto, size);
455 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
457 efi_bs_call(exit, handle, status, 0, NULL);
462 void __noreturn efi_stub_entry(efi_handle_t handle,
463 efi_system_table_t *sys_table_arg,
464 struct boot_params *boot_params);
467 * Because the x86 boot code expects to be passed a boot_params we
468 * need to create one ourselves (usually the bootloader would create
471 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
472 efi_system_table_t *sys_table_arg)
474 struct boot_params *boot_params;
475 struct setup_header *hdr;
477 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
478 int options_size = 0;
482 efi_system_table = sys_table_arg;
484 /* Check if we were booted by the EFI firmware */
485 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
486 efi_exit(handle, EFI_INVALID_PARAMETER);
488 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
489 if (status != EFI_SUCCESS) {
490 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
491 efi_exit(handle, status);
494 image_base = efi_table_attr(image, image_base);
495 image_offset = (void *)startup_32 - image_base;
497 status = efi_allocate_pages(sizeof(struct boot_params),
498 (unsigned long *)&boot_params, ULONG_MAX);
499 if (status != EFI_SUCCESS) {
500 efi_err("Failed to allocate lowmem for boot params\n");
501 efi_exit(handle, status);
504 memset(boot_params, 0x0, sizeof(struct boot_params));
506 hdr = &boot_params->hdr;
508 /* Copy the setup header from the second sector to boot_params */
509 memcpy(&hdr->jump, image_base + 512,
510 sizeof(struct setup_header) - offsetof(struct setup_header, jump));
513 * Fill out some of the header fields ourselves because the
514 * EFI firmware loader doesn't load the first sector.
517 hdr->vid_mode = 0xffff;
518 hdr->boot_flag = 0xAA55;
520 hdr->type_of_loader = 0x21;
522 /* Convert unicode cmdline to ascii */
523 cmdline_ptr = efi_convert_cmdline(image, &options_size);
527 efi_set_u64_split((unsigned long)cmdline_ptr,
528 &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
530 hdr->ramdisk_image = 0;
531 hdr->ramdisk_size = 0;
534 * Disregard any setup data that was provided by the bootloader:
535 * setup_data could be pointing anywhere, and we have no way of
536 * authenticating or validating the payload.
540 efi_stub_entry(handle, sys_table_arg, boot_params);
544 efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
546 efi_exit(handle, status);
549 static void add_e820ext(struct boot_params *params,
550 struct setup_data *e820ext, u32 nr_entries)
552 struct setup_data *data;
554 e820ext->type = SETUP_E820_EXT;
555 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
558 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
560 while (data && data->next)
561 data = (struct setup_data *)(unsigned long)data->next;
564 data->next = (unsigned long)e820ext;
566 params->hdr.setup_data = (unsigned long)e820ext;
570 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
572 struct boot_e820_entry *entry = params->e820_table;
573 struct efi_info *efi = ¶ms->efi_info;
574 struct boot_e820_entry *prev = NULL;
580 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
582 for (i = 0; i < nr_desc; i++) {
583 efi_memory_desc_t *d;
584 unsigned int e820_type = 0;
585 unsigned long m = efi->efi_memmap;
588 m |= (u64)efi->efi_memmap_hi << 32;
591 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
593 case EFI_RESERVED_TYPE:
594 case EFI_RUNTIME_SERVICES_CODE:
595 case EFI_RUNTIME_SERVICES_DATA:
596 case EFI_MEMORY_MAPPED_IO:
597 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
599 e820_type = E820_TYPE_RESERVED;
602 case EFI_UNUSABLE_MEMORY:
603 e820_type = E820_TYPE_UNUSABLE;
606 case EFI_ACPI_RECLAIM_MEMORY:
607 e820_type = E820_TYPE_ACPI;
610 case EFI_LOADER_CODE:
611 case EFI_LOADER_DATA:
612 case EFI_BOOT_SERVICES_CODE:
613 case EFI_BOOT_SERVICES_DATA:
614 case EFI_CONVENTIONAL_MEMORY:
615 if (efi_soft_reserve_enabled() &&
616 (d->attribute & EFI_MEMORY_SP))
617 e820_type = E820_TYPE_SOFT_RESERVED;
619 e820_type = E820_TYPE_RAM;
622 case EFI_ACPI_MEMORY_NVS:
623 e820_type = E820_TYPE_NVS;
626 case EFI_PERSISTENT_MEMORY:
627 e820_type = E820_TYPE_PMEM;
630 case EFI_UNACCEPTED_MEMORY:
631 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) {
633 "The system has unaccepted memory, but kernel does not support it\nConsider enabling CONFIG_UNACCEPTED_MEMORY\n");
636 e820_type = E820_TYPE_RAM;
637 process_unaccepted_memory(d->phys_addr,
638 d->phys_addr + PAGE_SIZE * d->num_pages);
644 /* Merge adjacent mappings */
645 if (prev && prev->type == e820_type &&
646 (prev->addr + prev->size) == d->phys_addr) {
647 prev->size += d->num_pages << 12;
651 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
652 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
653 sizeof(struct setup_data);
655 if (!e820ext || e820ext_size < need)
656 return EFI_BUFFER_TOO_SMALL;
658 /* boot_params map full, switch to e820 extended */
659 entry = (struct boot_e820_entry *)e820ext->data;
662 entry->addr = d->phys_addr;
663 entry->size = d->num_pages << PAGE_SHIFT;
664 entry->type = e820_type;
669 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
670 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
672 add_e820ext(params, e820ext, nr_e820ext);
673 nr_entries -= nr_e820ext;
676 params->e820_entries = (u8)nr_entries;
681 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
687 size = sizeof(struct setup_data) +
688 sizeof(struct e820_entry) * nr_desc;
691 efi_bs_call(free_pool, *e820ext);
696 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
698 if (status == EFI_SUCCESS)
699 *e820ext_size = size;
704 static efi_status_t allocate_e820(struct boot_params *params,
705 struct setup_data **e820ext,
708 struct efi_boot_memmap *map;
712 status = efi_get_memory_map(&map, false);
713 if (status != EFI_SUCCESS)
716 nr_desc = map->map_size / map->desc_size;
717 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
718 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
719 EFI_MMAP_NR_SLACK_SLOTS;
721 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
724 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
725 status = allocate_unaccepted_bitmap(nr_desc, map);
727 efi_bs_call(free_pool, map);
731 struct exit_boot_struct {
732 struct boot_params *boot_params;
733 struct efi_info *efi;
736 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
739 const char *signature;
740 struct exit_boot_struct *p = priv;
742 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
743 : EFI32_LOADER_SIGNATURE;
744 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
746 efi_set_u64_split((unsigned long)efi_system_table,
747 &p->efi->efi_systab, &p->efi->efi_systab_hi);
748 p->efi->efi_memdesc_size = map->desc_size;
749 p->efi->efi_memdesc_version = map->desc_ver;
750 efi_set_u64_split((unsigned long)map->map,
751 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
752 p->efi->efi_memmap_size = map->map_size;
757 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
759 struct setup_data *e820ext = NULL;
760 __u32 e820ext_size = 0;
762 struct exit_boot_struct priv;
764 priv.boot_params = boot_params;
765 priv.efi = &boot_params->efi_info;
767 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
768 if (status != EFI_SUCCESS)
771 /* Might as well exit boot services now */
772 status = efi_exit_boot_services(handle, &priv, exit_boot_func);
773 if (status != EFI_SUCCESS)
777 boot_params->alt_mem_k = 32 * 1024;
779 status = setup_e820(boot_params, e820ext, e820ext_size);
780 if (status != EFI_SUCCESS)
786 static void __noreturn enter_kernel(unsigned long kernel_addr,
787 struct boot_params *boot_params)
789 /* enter decompressed kernel with boot_params pointer in RSI/ESI */
790 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
796 * On success, this routine will jump to the relocated image directly and never
797 * return. On failure, it will exit to the firmware via efi_exit() instead of
800 void __noreturn efi_stub_entry(efi_handle_t handle,
801 efi_system_table_t *sys_table_arg,
802 struct boot_params *boot_params)
804 unsigned long bzimage_addr = (unsigned long)startup_32;
805 unsigned long buffer_start, buffer_end;
806 struct setup_header *hdr = &boot_params->hdr;
807 const struct linux_efi_initrd *initrd = NULL;
810 efi_system_table = sys_table_arg;
811 /* Check if we were booted by the EFI firmware */
812 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
813 efi_exit(handle, EFI_INVALID_PARAMETER);
815 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
817 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
818 efi_warn("Ignoring DXE services table: invalid signature\n");
819 efi_dxe_table = NULL;
822 status = efi_setup_5level_paging();
823 if (status != EFI_SUCCESS) {
824 efi_err("efi_setup_5level_paging() failed!\n");
829 * If the kernel isn't already loaded at a suitable address,
832 * It must be loaded above LOAD_PHYSICAL_ADDR.
834 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
835 * is defined as the macro MAXMEM, but unfortunately that is not a
836 * compile-time constant if 5-level paging is configured, so we instead
837 * define our own macro for use here.
839 * For 32-bit, the maximum address is complicated to figure out, for
840 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
843 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
844 * loaded by LoadImage, but rather by a bootloader that called the
845 * handover entry. The reason we must always relocate in this case is
846 * to handle the case of systemd-boot booting a unified kernel image,
847 * which is a PE executable that contains the bzImage and an initrd as
848 * COFF sections. The initrd section is placed after the bzImage
849 * without ensuring that there are at least init_size bytes available
850 * for the bzImage, and thus the compressed kernel's startup code may
851 * overwrite the initrd unless it is moved out of the way.
854 buffer_start = ALIGN(bzimage_addr - image_offset,
855 hdr->kernel_alignment);
856 buffer_end = buffer_start + hdr->init_size;
858 if ((buffer_start < LOAD_PHYSICAL_ADDR) ||
859 (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE) ||
860 (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
861 (image_offset == 0)) {
864 status = efi_relocate_kernel(&bzimage_addr,
865 (unsigned long)_bss - bzimage_addr,
868 hdr->kernel_alignment,
870 if (status != EFI_SUCCESS) {
871 efi_err("efi_relocate_kernel() failed!\n");
875 * Now that we've copied the kernel elsewhere, we no longer
876 * have a set up block before startup_32(), so reset image_offset
877 * to zero in case it was set earlier.
882 #ifdef CONFIG_CMDLINE_BOOL
883 status = efi_parse_options(CONFIG_CMDLINE);
884 if (status != EFI_SUCCESS) {
885 efi_err("Failed to parse options\n");
889 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
890 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
891 ((u64)boot_params->ext_cmd_line_ptr << 32));
892 status = efi_parse_options((char *)cmdline_paddr);
893 if (status != EFI_SUCCESS) {
894 efi_err("Failed to parse options\n");
900 * At this point, an initrd may already have been loaded by the
901 * bootloader and passed via bootparams. We permit an initrd loaded
902 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
904 * If the device path is not present, any command-line initrd=
905 * arguments will be processed only if image is not NULL, which will be
906 * the case only if we were loaded via the PE entry point.
908 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
910 if (status != EFI_SUCCESS)
912 if (initrd && initrd->size > 0) {
913 efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
914 &boot_params->ext_ramdisk_image);
915 efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
916 &boot_params->ext_ramdisk_size);
921 * If the boot loader gave us a value for secure_boot then we use that,
922 * otherwise we ask the BIOS.
924 if (boot_params->secure_boot == efi_secureboot_mode_unset)
925 boot_params->secure_boot = efi_get_secureboot();
927 /* Ask the firmware to clear memory on unclean shutdown */
928 efi_enable_reset_attack_mitigation();
930 efi_random_get_seed();
932 efi_retrieve_tpm2_eventlog();
934 setup_graphics(boot_params);
936 setup_efi_pci(boot_params);
938 setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start);
940 setup_unaccepted_memory();
942 status = exit_boot(boot_params, handle);
943 if (status != EFI_SUCCESS) {
944 efi_err("exit_boot() failed!\n");
950 if (IS_ENABLED(CONFIG_X86_64))
951 bzimage_addr += startup_64 - startup_32;
953 enter_kernel(bzimage_addr, boot_params);
955 efi_err("efi_stub_entry() failed!\n");
957 efi_exit(handle, status);
960 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
961 void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
962 struct boot_params *boot_params)
964 extern char _bss[], _ebss[];
966 memset(_bss, 0, _ebss - _bss);
967 efi_stub_entry(handle, sys_table_arg, boot_params);
970 #ifndef CONFIG_EFI_MIXED
971 extern __alias(efi_handover_entry)
972 void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
973 struct boot_params *boot_params);
975 extern __alias(efi_handover_entry)
976 void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
977 struct boot_params *boot_params);