Merge tag 'uml-for-linus-6.14-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux.git] / drivers / firmware / efi / libstub / x86-stub.c
blob863910e9eefc3f651035af4ebbae92fc6a83d130
1 // SPDX-License-Identifier: GPL-2.0-only
3 /* -----------------------------------------------------------------------
5 * Copyright 2011 Intel Corporation; author Matt Fleming
7 * ----------------------------------------------------------------------- */
9 #include <linux/efi.h>
10 #include <linux/pci.h>
11 #include <linux/stddef.h>
13 #include <asm/efi.h>
14 #include <asm/e820/types.h>
15 #include <asm/setup.h>
16 #include <asm/desc.h>
17 #include <asm/boot.h>
18 #include <asm/kaslr.h>
19 #include <asm/sev.h>
21 #include "efistub.h"
22 #include "x86-stub.h"
24 extern char _bss[], _ebss[];
26 const efi_system_table_t *efi_system_table;
27 const efi_dxe_services_table_t *efi_dxe_table;
28 static efi_loaded_image_t *image = NULL;
29 static efi_memory_attribute_protocol_t *memattr;
31 typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
32 union sev_memory_acceptance_protocol {
33 struct {
34 efi_status_t (__efiapi * allow_unaccepted_memory)(
35 sev_memory_acceptance_protocol_t *);
37 struct {
38 u32 allow_unaccepted_memory;
39 } mixed_mode;
42 static efi_status_t
43 preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
45 struct pci_setup_rom *rom __free(efi_pool) = NULL;
46 efi_status_t status;
47 unsigned long size;
48 uint64_t romsize;
49 void *romimage;
52 * Some firmware images contain EFI function pointers at the place where
53 * the romimage and romsize fields are supposed to be. Typically the EFI
54 * code is mapped at high addresses, translating to an unrealistically
55 * large romsize. The UEFI spec limits the size of option ROMs to 16
56 * MiB so we reject any ROMs over 16 MiB in size to catch this.
58 romimage = efi_table_attr(pci, romimage);
59 romsize = efi_table_attr(pci, romsize);
60 if (!romimage || !romsize || romsize > SZ_16M)
61 return EFI_INVALID_PARAMETER;
63 size = romsize + sizeof(*rom);
65 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
66 (void **)&rom);
67 if (status != EFI_SUCCESS) {
68 efi_err("Failed to allocate memory for 'rom'\n");
69 return status;
72 memset(rom, 0, sizeof(*rom));
74 rom->data.type = SETUP_PCI;
75 rom->data.len = size - sizeof(struct setup_data);
76 rom->data.next = 0;
77 rom->pcilen = 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");
84 return status;
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");
92 return status;
95 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
96 &rom->device, &rom->function);
98 if (status != EFI_SUCCESS)
99 return status;
101 memcpy(rom->romdata, romimage, romsize);
102 *__rom = no_free_ptr(rom);
103 return EFI_SUCCESS;
107 * There's no way to return an informative status from this function,
108 * because any analysis (and printing of error messages) needs to be
109 * done directly at the EFI function call-site.
111 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
112 * just didn't find any PCI devices, but there's no way to tell outside
113 * the context of the call.
115 static void setup_efi_pci(struct boot_params *params)
117 efi_status_t status;
118 efi_handle_t *pci_handle __free(efi_pool) = NULL;
119 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
120 struct setup_data *data;
121 unsigned long num;
122 efi_handle_t h;
124 status = efi_bs_call(locate_handle_buffer, EFI_LOCATE_BY_PROTOCOL,
125 &pci_proto, NULL, &num, &pci_handle);
126 if (status != EFI_SUCCESS)
127 return;
129 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
131 while (data && data->next)
132 data = (struct setup_data *)(unsigned long)data->next;
134 for_each_efi_handle(h, pci_handle, num) {
135 efi_pci_io_protocol_t *pci = NULL;
136 struct pci_setup_rom *rom;
138 status = efi_bs_call(handle_protocol, h, &pci_proto,
139 (void **)&pci);
140 if (status != EFI_SUCCESS || !pci)
141 continue;
143 status = preserve_pci_rom_image(pci, &rom);
144 if (status != EFI_SUCCESS)
145 continue;
147 if (data)
148 data->next = (unsigned long)rom;
149 else
150 params->hdr.setup_data = (unsigned long)rom;
152 data = (struct setup_data *)rom;
156 static void retrieve_apple_device_properties(struct boot_params *boot_params)
158 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
159 struct setup_data *data, *new;
160 efi_status_t status;
161 u32 size = 0;
162 apple_properties_protocol_t *p;
164 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
165 if (status != EFI_SUCCESS)
166 return;
168 if (efi_table_attr(p, version) != 0x10000) {
169 efi_err("Unsupported properties proto version\n");
170 return;
173 efi_call_proto(p, get_all, NULL, &size);
174 if (!size)
175 return;
177 do {
178 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
179 size + sizeof(struct setup_data),
180 (void **)&new);
181 if (status != EFI_SUCCESS) {
182 efi_err("Failed to allocate memory for 'properties'\n");
183 return;
186 status = efi_call_proto(p, get_all, new->data, &size);
188 if (status == EFI_BUFFER_TOO_SMALL)
189 efi_bs_call(free_pool, new);
190 } while (status == EFI_BUFFER_TOO_SMALL);
192 new->type = SETUP_APPLE_PROPERTIES;
193 new->len = size;
194 new->next = 0;
196 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
197 if (!data) {
198 boot_params->hdr.setup_data = (unsigned long)new;
199 } else {
200 while (data->next)
201 data = (struct setup_data *)(unsigned long)data->next;
202 data->next = (unsigned long)new;
206 static bool apple_match_product_name(void)
208 static const char type1_product_matches[][15] = {
209 "MacBookPro11,3",
210 "MacBookPro11,5",
211 "MacBookPro13,3",
212 "MacBookPro14,3",
213 "MacBookPro15,1",
214 "MacBookPro15,3",
215 "MacBookPro16,1",
216 "MacBookPro16,4",
218 const struct efi_smbios_type1_record *record;
219 const u8 *product;
221 record = (struct efi_smbios_type1_record *)efi_get_smbios_record(1);
222 if (!record)
223 return false;
225 product = efi_get_smbios_string(record, product_name);
226 if (!product)
227 return false;
229 for (int i = 0; i < ARRAY_SIZE(type1_product_matches); i++) {
230 if (!strcmp(product, type1_product_matches[i]))
231 return true;
234 return false;
237 static void apple_set_os(void)
239 struct {
240 unsigned long version;
241 efi_status_t (__efiapi *set_os_version)(const char *);
242 efi_status_t (__efiapi *set_os_vendor)(const char *);
243 } *set_os;
244 efi_status_t status;
246 if (!efi_is_64bit() || !apple_match_product_name())
247 return;
249 status = efi_bs_call(locate_protocol, &APPLE_SET_OS_PROTOCOL_GUID, NULL,
250 (void **)&set_os);
251 if (status != EFI_SUCCESS)
252 return;
254 if (set_os->version >= 2) {
255 status = set_os->set_os_vendor("Apple Inc.");
256 if (status != EFI_SUCCESS)
257 efi_err("Failed to set OS vendor via apple_set_os\n");
260 if (set_os->version > 0) {
261 /* The version being set doesn't seem to matter */
262 status = set_os->set_os_version("Mac OS X 10.9");
263 if (status != EFI_SUCCESS)
264 efi_err("Failed to set OS version via apple_set_os\n");
268 efi_status_t efi_adjust_memory_range_protection(unsigned long start,
269 unsigned long size)
271 efi_status_t status;
272 efi_gcd_memory_space_desc_t desc;
273 unsigned long end, next;
274 unsigned long rounded_start, rounded_end;
275 unsigned long unprotect_start, unprotect_size;
277 rounded_start = rounddown(start, EFI_PAGE_SIZE);
278 rounded_end = roundup(start + size, EFI_PAGE_SIZE);
280 if (memattr != NULL) {
281 status = efi_call_proto(memattr, set_memory_attributes,
282 rounded_start,
283 rounded_end - rounded_start,
284 EFI_MEMORY_RO);
285 if (status != EFI_SUCCESS) {
286 efi_warn("Failed to set EFI_MEMORY_RO attribute\n");
287 return status;
290 status = efi_call_proto(memattr, clear_memory_attributes,
291 rounded_start,
292 rounded_end - rounded_start,
293 EFI_MEMORY_XP);
294 if (status != EFI_SUCCESS)
295 efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");
296 return status;
299 if (efi_dxe_table == NULL)
300 return EFI_SUCCESS;
303 * Don't modify memory region attributes, they are
304 * already suitable, to lower the possibility to
305 * encounter firmware bugs.
308 for (end = start + size; start < end; start = next) {
310 status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
312 if (status != EFI_SUCCESS)
313 break;
315 next = desc.base_address + desc.length;
318 * Only system memory is suitable for trampoline/kernel image placement,
319 * so only this type of memory needs its attributes to be modified.
322 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
323 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
324 continue;
326 unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
327 unprotect_size = min(rounded_end, next) - unprotect_start;
329 status = efi_dxe_call(set_memory_space_attributes,
330 unprotect_start, unprotect_size,
331 EFI_MEMORY_WB);
333 if (status != EFI_SUCCESS) {
334 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
335 unprotect_start,
336 unprotect_start + unprotect_size,
337 status);
338 break;
341 return EFI_SUCCESS;
344 static void setup_unaccepted_memory(void)
346 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
347 sev_memory_acceptance_protocol_t *proto;
348 efi_status_t status;
350 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
351 return;
354 * Enable unaccepted memory before calling exit boot services in order
355 * for the UEFI to not accept all memory on EBS.
357 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
358 (void **)&proto);
359 if (status != EFI_SUCCESS)
360 return;
362 status = efi_call_proto(proto, allow_unaccepted_memory);
363 if (status != EFI_SUCCESS)
364 efi_err("Memory acceptance protocol failed\n");
367 static efi_char16_t *efistub_fw_vendor(void)
369 unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);
371 return (efi_char16_t *)vendor;
374 static const efi_char16_t apple[] = L"Apple";
376 static void setup_quirks(struct boot_params *boot_params)
378 if (!memcmp(efistub_fw_vendor(), apple, sizeof(apple))) {
379 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
380 retrieve_apple_device_properties(boot_params);
382 apple_set_os();
386 static void setup_graphics(struct boot_params *boot_params)
388 struct screen_info *si = memset(&boot_params->screen_info, 0, sizeof(*si));
390 efi_setup_gop(si);
393 static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
395 efi_bs_call(exit, handle, status, 0, NULL);
396 for(;;)
397 asm("hlt");
400 void __noreturn efi_stub_entry(efi_handle_t handle,
401 efi_system_table_t *sys_table_arg,
402 struct boot_params *boot_params);
405 * Because the x86 boot code expects to be passed a boot_params we
406 * need to create one ourselves (usually the bootloader would create
407 * one for us).
409 efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
410 efi_system_table_t *sys_table_arg)
412 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
413 struct boot_params *boot_params;
414 struct setup_header *hdr;
415 efi_status_t status;
416 unsigned long alloc;
417 char *cmdline_ptr;
419 efi_system_table = sys_table_arg;
421 /* Check if we were booted by the EFI firmware */
422 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
423 efi_exit(handle, EFI_INVALID_PARAMETER);
425 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
426 if (status != EFI_SUCCESS) {
427 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
428 efi_exit(handle, status);
431 status = efi_allocate_pages(PARAM_SIZE, &alloc, ULONG_MAX);
432 if (status != EFI_SUCCESS)
433 efi_exit(handle, status);
435 boot_params = memset((void *)alloc, 0x0, PARAM_SIZE);
436 hdr = &boot_params->hdr;
438 /* Assign the setup_header fields that the kernel actually cares about */
439 hdr->root_flags = 1;
440 hdr->vid_mode = 0xffff;
442 hdr->type_of_loader = 0x21;
443 hdr->initrd_addr_max = INT_MAX;
445 /* Convert unicode cmdline to ascii */
446 cmdline_ptr = efi_convert_cmdline(image);
447 if (!cmdline_ptr) {
448 efi_free(PARAM_SIZE, alloc);
449 efi_exit(handle, EFI_OUT_OF_RESOURCES);
452 efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,
453 &boot_params->ext_cmd_line_ptr);
455 efi_stub_entry(handle, sys_table_arg, boot_params);
456 /* not reached */
459 static void add_e820ext(struct boot_params *params,
460 struct setup_data *e820ext, u32 nr_entries)
462 struct setup_data *data;
464 e820ext->type = SETUP_E820_EXT;
465 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
466 e820ext->next = 0;
468 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
470 while (data && data->next)
471 data = (struct setup_data *)(unsigned long)data->next;
473 if (data)
474 data->next = (unsigned long)e820ext;
475 else
476 params->hdr.setup_data = (unsigned long)e820ext;
479 static efi_status_t
480 setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
482 struct boot_e820_entry *entry = params->e820_table;
483 struct efi_info *efi = &params->efi_info;
484 struct boot_e820_entry *prev = NULL;
485 u32 nr_entries;
486 u32 nr_desc;
487 int i;
489 nr_entries = 0;
490 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
492 for (i = 0; i < nr_desc; i++) {
493 efi_memory_desc_t *d;
494 unsigned int e820_type = 0;
495 unsigned long m = efi->efi_memmap;
497 #ifdef CONFIG_X86_64
498 m |= (u64)efi->efi_memmap_hi << 32;
499 #endif
501 d = efi_memdesc_ptr(m, efi->efi_memdesc_size, i);
502 switch (d->type) {
503 case EFI_RESERVED_TYPE:
504 case EFI_RUNTIME_SERVICES_CODE:
505 case EFI_RUNTIME_SERVICES_DATA:
506 case EFI_MEMORY_MAPPED_IO:
507 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
508 case EFI_PAL_CODE:
509 e820_type = E820_TYPE_RESERVED;
510 break;
512 case EFI_UNUSABLE_MEMORY:
513 e820_type = E820_TYPE_UNUSABLE;
514 break;
516 case EFI_ACPI_RECLAIM_MEMORY:
517 e820_type = E820_TYPE_ACPI;
518 break;
520 case EFI_LOADER_CODE:
521 case EFI_LOADER_DATA:
522 case EFI_BOOT_SERVICES_CODE:
523 case EFI_BOOT_SERVICES_DATA:
524 case EFI_CONVENTIONAL_MEMORY:
525 if (efi_soft_reserve_enabled() &&
526 (d->attribute & EFI_MEMORY_SP))
527 e820_type = E820_TYPE_SOFT_RESERVED;
528 else
529 e820_type = E820_TYPE_RAM;
530 break;
532 case EFI_ACPI_MEMORY_NVS:
533 e820_type = E820_TYPE_NVS;
534 break;
536 case EFI_PERSISTENT_MEMORY:
537 e820_type = E820_TYPE_PMEM;
538 break;
540 case EFI_UNACCEPTED_MEMORY:
541 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
542 continue;
543 e820_type = E820_TYPE_RAM;
544 process_unaccepted_memory(d->phys_addr,
545 d->phys_addr + PAGE_SIZE * d->num_pages);
546 break;
547 default:
548 continue;
551 /* Merge adjacent mappings */
552 if (prev && prev->type == e820_type &&
553 (prev->addr + prev->size) == d->phys_addr) {
554 prev->size += d->num_pages << 12;
555 continue;
558 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
559 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
560 sizeof(struct setup_data);
562 if (!e820ext || e820ext_size < need)
563 return EFI_BUFFER_TOO_SMALL;
565 /* boot_params map full, switch to e820 extended */
566 entry = (struct boot_e820_entry *)e820ext->data;
569 entry->addr = d->phys_addr;
570 entry->size = d->num_pages << PAGE_SHIFT;
571 entry->type = e820_type;
572 prev = entry++;
573 nr_entries++;
576 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
577 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
579 add_e820ext(params, e820ext, nr_e820ext);
580 nr_entries -= nr_e820ext;
583 params->e820_entries = (u8)nr_entries;
585 return EFI_SUCCESS;
588 static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
589 u32 *e820ext_size)
591 efi_status_t status;
592 unsigned long size;
594 size = sizeof(struct setup_data) +
595 sizeof(struct e820_entry) * nr_desc;
597 if (*e820ext) {
598 efi_bs_call(free_pool, *e820ext);
599 *e820ext = NULL;
600 *e820ext_size = 0;
603 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
604 (void **)e820ext);
605 if (status == EFI_SUCCESS)
606 *e820ext_size = size;
608 return status;
611 static efi_status_t allocate_e820(struct boot_params *params,
612 struct setup_data **e820ext,
613 u32 *e820ext_size)
615 struct efi_boot_memmap *map __free(efi_pool) = NULL;
616 efi_status_t status;
617 __u32 nr_desc;
619 status = efi_get_memory_map(&map, false);
620 if (status != EFI_SUCCESS)
621 return status;
623 nr_desc = map->map_size / map->desc_size;
624 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
625 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
626 EFI_MMAP_NR_SLACK_SLOTS;
628 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
629 if (status != EFI_SUCCESS)
630 return status;
633 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
634 return allocate_unaccepted_bitmap(nr_desc, map);
636 return EFI_SUCCESS;
639 struct exit_boot_struct {
640 struct boot_params *boot_params;
641 struct efi_info *efi;
644 static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
645 void *priv)
647 const char *signature;
648 struct exit_boot_struct *p = priv;
650 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
651 : EFI32_LOADER_SIGNATURE;
652 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
654 efi_set_u64_split((unsigned long)efi_system_table,
655 &p->efi->efi_systab, &p->efi->efi_systab_hi);
656 p->efi->efi_memdesc_size = map->desc_size;
657 p->efi->efi_memdesc_version = map->desc_ver;
658 efi_set_u64_split((unsigned long)map->map,
659 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
660 p->efi->efi_memmap_size = map->map_size;
662 return EFI_SUCCESS;
665 static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
667 struct setup_data *e820ext = NULL;
668 __u32 e820ext_size = 0;
669 efi_status_t status;
670 struct exit_boot_struct priv;
672 priv.boot_params = boot_params;
673 priv.efi = &boot_params->efi_info;
675 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
676 if (status != EFI_SUCCESS)
677 return status;
679 /* Might as well exit boot services now */
680 status = efi_exit_boot_services(handle, &priv, exit_boot_func);
681 if (status != EFI_SUCCESS)
682 return status;
684 /* Historic? */
685 boot_params->alt_mem_k = 32 * 1024;
687 status = setup_e820(boot_params, e820ext, e820ext_size);
688 if (status != EFI_SUCCESS)
689 return status;
691 return EFI_SUCCESS;
694 static bool have_unsupported_snp_features(void)
696 u64 unsupported;
698 unsupported = snp_get_unsupported_features(sev_get_status());
699 if (unsupported) {
700 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
701 unsupported);
702 return true;
704 return false;
707 static void efi_get_seed(void *seed, int size)
709 efi_get_random_bytes(size, seed);
712 * This only updates seed[0] when running on 32-bit, but in that case,
713 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
715 *(unsigned long *)seed ^= kaslr_get_random_long("EFI");
718 static void error(char *str)
720 efi_warn("Decompression failed: %s\n", str);
723 static const char *cmdline_memmap_override;
725 static efi_status_t parse_options(const char *cmdline)
727 static const char opts[][14] = {
728 "mem=", "memmap=", "hugepages="
731 for (int i = 0; i < ARRAY_SIZE(opts); i++) {
732 const char *p = strstr(cmdline, opts[i]);
734 if (p == cmdline || (p > cmdline && isspace(p[-1]))) {
735 cmdline_memmap_override = opts[i];
736 break;
740 return efi_parse_options(cmdline);
743 static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
745 unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
746 unsigned long addr, alloc_size, entry;
747 efi_status_t status;
748 u32 seed[2] = {};
750 /* determine the required size of the allocation */
751 alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
752 MIN_KERNEL_ALIGN);
754 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
755 u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
756 static const efi_char16_t ami[] = L"American Megatrends";
758 efi_get_seed(seed, sizeof(seed));
760 virt_addr += (range * seed[1]) >> 32;
761 virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
764 * Older Dell systems with AMI UEFI firmware v2.0 may hang
765 * while decompressing the kernel if physical address
766 * randomization is enabled.
768 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
770 if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&
771 !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
772 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
773 seed[0] = 0;
774 } else if (cmdline_memmap_override) {
775 efi_info("%s detected on the kernel command line - disabling physical KASLR\n",
776 cmdline_memmap_override);
777 seed[0] = 0;
780 boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
783 status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
784 seed[0], EFI_LOADER_CODE,
785 LOAD_PHYSICAL_ADDR,
786 EFI_X86_KERNEL_ALLOC_LIMIT);
787 if (status != EFI_SUCCESS)
788 return status;
790 entry = decompress_kernel((void *)addr, virt_addr, error);
791 if (entry == ULONG_MAX) {
792 efi_free(alloc_size, addr);
793 return EFI_LOAD_ERROR;
796 *kernel_entry = addr + entry;
798 return efi_adjust_memory_range_protection(addr, kernel_text_size);
801 static void __noreturn enter_kernel(unsigned long kernel_addr,
802 struct boot_params *boot_params)
804 /* enter decompressed kernel with boot_params pointer in RSI/ESI */
805 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
807 unreachable();
811 * On success, this routine will jump to the relocated image directly and never
812 * return. On failure, it will exit to the firmware via efi_exit() instead of
813 * returning.
815 void __noreturn efi_stub_entry(efi_handle_t handle,
816 efi_system_table_t *sys_table_arg,
817 struct boot_params *boot_params)
819 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
820 struct setup_header *hdr = &boot_params->hdr;
821 const struct linux_efi_initrd *initrd = NULL;
822 unsigned long kernel_entry;
823 efi_status_t status;
825 boot_params_ptr = boot_params;
827 efi_system_table = sys_table_arg;
828 /* Check if we were booted by the EFI firmware */
829 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
830 efi_exit(handle, EFI_INVALID_PARAMETER);
832 if (have_unsupported_snp_features())
833 efi_exit(handle, EFI_UNSUPPORTED);
835 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
836 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
837 if (efi_dxe_table &&
838 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
839 efi_warn("Ignoring DXE services table: invalid signature\n");
840 efi_dxe_table = NULL;
844 /* grab the memory attributes protocol if it exists */
845 efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
847 status = efi_setup_5level_paging();
848 if (status != EFI_SUCCESS) {
849 efi_err("efi_setup_5level_paging() failed!\n");
850 goto fail;
853 #ifdef CONFIG_CMDLINE_BOOL
854 status = parse_options(CONFIG_CMDLINE);
855 if (status != EFI_SUCCESS) {
856 efi_err("Failed to parse options\n");
857 goto fail;
859 #endif
860 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
861 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
862 ((u64)boot_params->ext_cmd_line_ptr << 32));
863 status = parse_options((char *)cmdline_paddr);
864 if (status != EFI_SUCCESS) {
865 efi_err("Failed to parse options\n");
866 goto fail;
870 if (efi_mem_encrypt > 0)
871 hdr->xloadflags |= XLF_MEM_ENCRYPTION;
873 status = efi_decompress_kernel(&kernel_entry);
874 if (status != EFI_SUCCESS) {
875 efi_err("Failed to decompress kernel\n");
876 goto fail;
880 * At this point, an initrd may already have been loaded by the
881 * bootloader and passed via bootparams. We permit an initrd loaded
882 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
884 * If the device path is not present, any command-line initrd=
885 * arguments will be processed only if image is not NULL, which will be
886 * the case only if we were loaded via the PE entry point.
888 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
889 &initrd);
890 if (status != EFI_SUCCESS)
891 goto fail;
892 if (initrd && initrd->size > 0) {
893 efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
894 &boot_params->ext_ramdisk_image);
895 efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
896 &boot_params->ext_ramdisk_size);
901 * If the boot loader gave us a value for secure_boot then we use that,
902 * otherwise we ask the BIOS.
904 if (boot_params->secure_boot == efi_secureboot_mode_unset)
905 boot_params->secure_boot = efi_get_secureboot();
907 /* Ask the firmware to clear memory on unclean shutdown */
908 efi_enable_reset_attack_mitigation();
910 efi_random_get_seed();
912 efi_retrieve_eventlog();
914 setup_graphics(boot_params);
916 setup_efi_pci(boot_params);
918 setup_quirks(boot_params);
920 setup_unaccepted_memory();
922 status = exit_boot(boot_params, handle);
923 if (status != EFI_SUCCESS) {
924 efi_err("exit_boot() failed!\n");
925 goto fail;
929 * Call the SEV init code while still running with the firmware's
930 * GDT/IDT, so #VC exceptions will be handled by EFI.
932 sev_enable(boot_params);
934 efi_5level_switch();
936 enter_kernel(kernel_entry, boot_params);
937 fail:
938 efi_err("efi_stub_entry() failed!\n");
940 efi_exit(handle, status);
943 #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
944 void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
945 struct boot_params *boot_params)
947 memset(_bss, 0, _ebss - _bss);
948 efi_stub_entry(handle, sys_table_arg, boot_params);
951 #ifndef CONFIG_EFI_MIXED
952 extern __alias(efi_handover_entry)
953 void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
954 struct boot_params *boot_params);
956 extern __alias(efi_handover_entry)
957 void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
958 struct boot_params *boot_params);
959 #endif
960 #endif