Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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#include <linux/stddef.h>
12
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>
20
21#include "efistub.h"
22#include "x86-stub.h"
23
24const efi_system_table_t *efi_system_table;
25const efi_dxe_services_table_t *efi_dxe_table;
26static efi_loaded_image_t *image = NULL;
27static efi_memory_attribute_protocol_t *memattr;
28
29typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
30union sev_memory_acceptance_protocol {
31 struct {
32 efi_status_t (__efiapi * allow_unaccepted_memory)(
33 sev_memory_acceptance_protocol_t *);
34 };
35 struct {
36 u32 allow_unaccepted_memory;
37 } mixed_mode;
38};
39
40static efi_status_t
41preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
42{
43 struct pci_setup_rom *rom = NULL;
44 efi_status_t status;
45 unsigned long size;
46 uint64_t romsize;
47 void *romimage;
48
49 /*
50 * Some firmware images contain EFI function pointers at the place where
51 * the romimage and romsize fields are supposed to be. Typically the EFI
52 * code is mapped at high addresses, translating to an unrealistically
53 * large romsize. The UEFI spec limits the size of option ROMs to 16
54 * MiB so we reject any ROMs over 16 MiB in size to catch this.
55 */
56 romimage = efi_table_attr(pci, romimage);
57 romsize = efi_table_attr(pci, romsize);
58 if (!romimage || !romsize || romsize > SZ_16M)
59 return EFI_INVALID_PARAMETER;
60
61 size = romsize + sizeof(*rom);
62
63 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
64 (void **)&rom);
65 if (status != EFI_SUCCESS) {
66 efi_err("Failed to allocate memory for 'rom'\n");
67 return status;
68 }
69
70 memset(rom, 0, sizeof(*rom));
71
72 rom->data.type = SETUP_PCI;
73 rom->data.len = size - sizeof(struct setup_data);
74 rom->data.next = 0;
75 rom->pcilen = romsize;
76 *__rom = rom;
77
78 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
79 PCI_VENDOR_ID, 1, &rom->vendor);
80
81 if (status != EFI_SUCCESS) {
82 efi_err("Failed to read rom->vendor\n");
83 goto free_struct;
84 }
85
86 status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
87 PCI_DEVICE_ID, 1, &rom->devid);
88
89 if (status != EFI_SUCCESS) {
90 efi_err("Failed to read rom->devid\n");
91 goto free_struct;
92 }
93
94 status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
95 &rom->device, &rom->function);
96
97 if (status != EFI_SUCCESS)
98 goto free_struct;
99
100 memcpy(rom->romdata, romimage, romsize);
101 return status;
102
103free_struct:
104 efi_bs_call(free_pool, rom);
105 return status;
106}
107
108/*
109 * There's no way to return an informative status from this function,
110 * because any analysis (and printing of error messages) needs to be
111 * done directly at the EFI function call-site.
112 *
113 * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
114 * just didn't find any PCI devices, but there's no way to tell outside
115 * the context of the call.
116 */
117static void setup_efi_pci(struct boot_params *params)
118{
119 efi_status_t status;
120 void **pci_handle = NULL;
121 efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
122 unsigned long size = 0;
123 struct setup_data *data;
124 efi_handle_t h;
125 int i;
126
127 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128 &pci_proto, NULL, &size, pci_handle);
129
130 if (status == EFI_BUFFER_TOO_SMALL) {
131 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
132 (void **)&pci_handle);
133
134 if (status != EFI_SUCCESS) {
135 efi_err("Failed to allocate memory for 'pci_handle'\n");
136 return;
137 }
138
139 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
140 &pci_proto, NULL, &size, pci_handle);
141 }
142
143 if (status != EFI_SUCCESS)
144 goto free_handle;
145
146 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
147
148 while (data && data->next)
149 data = (struct setup_data *)(unsigned long)data->next;
150
151 for_each_efi_handle(h, pci_handle, size, i) {
152 efi_pci_io_protocol_t *pci = NULL;
153 struct pci_setup_rom *rom;
154
155 status = efi_bs_call(handle_protocol, h, &pci_proto,
156 (void **)&pci);
157 if (status != EFI_SUCCESS || !pci)
158 continue;
159
160 status = preserve_pci_rom_image(pci, &rom);
161 if (status != EFI_SUCCESS)
162 continue;
163
164 if (data)
165 data->next = (unsigned long)rom;
166 else
167 params->hdr.setup_data = (unsigned long)rom;
168
169 data = (struct setup_data *)rom;
170 }
171
172free_handle:
173 efi_bs_call(free_pool, pci_handle);
174}
175
176static void retrieve_apple_device_properties(struct boot_params *boot_params)
177{
178 efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
179 struct setup_data *data, *new;
180 efi_status_t status;
181 u32 size = 0;
182 apple_properties_protocol_t *p;
183
184 status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
185 if (status != EFI_SUCCESS)
186 return;
187
188 if (efi_table_attr(p, version) != 0x10000) {
189 efi_err("Unsupported properties proto version\n");
190 return;
191 }
192
193 efi_call_proto(p, get_all, NULL, &size);
194 if (!size)
195 return;
196
197 do {
198 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
199 size + sizeof(struct setup_data),
200 (void **)&new);
201 if (status != EFI_SUCCESS) {
202 efi_err("Failed to allocate memory for 'properties'\n");
203 return;
204 }
205
206 status = efi_call_proto(p, get_all, new->data, &size);
207
208 if (status == EFI_BUFFER_TOO_SMALL)
209 efi_bs_call(free_pool, new);
210 } while (status == EFI_BUFFER_TOO_SMALL);
211
212 new->type = SETUP_APPLE_PROPERTIES;
213 new->len = size;
214 new->next = 0;
215
216 data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
217 if (!data) {
218 boot_params->hdr.setup_data = (unsigned long)new;
219 } else {
220 while (data->next)
221 data = (struct setup_data *)(unsigned long)data->next;
222 data->next = (unsigned long)new;
223 }
224}
225
226void efi_adjust_memory_range_protection(unsigned long start,
227 unsigned long size)
228{
229 efi_status_t status;
230 efi_gcd_memory_space_desc_t desc;
231 unsigned long end, next;
232 unsigned long rounded_start, rounded_end;
233 unsigned long unprotect_start, unprotect_size;
234
235 rounded_start = rounddown(start, EFI_PAGE_SIZE);
236 rounded_end = roundup(start + size, EFI_PAGE_SIZE);
237
238 if (memattr != NULL) {
239 efi_call_proto(memattr, clear_memory_attributes, rounded_start,
240 rounded_end - rounded_start, EFI_MEMORY_XP);
241 return;
242 }
243
244 if (efi_dxe_table == NULL)
245 return;
246
247 /*
248 * Don't modify memory region attributes, they are
249 * already suitable, to lower the possibility to
250 * encounter firmware bugs.
251 */
252
253 for (end = start + size; start < end; start = next) {
254
255 status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
256
257 if (status != EFI_SUCCESS)
258 return;
259
260 next = desc.base_address + desc.length;
261
262 /*
263 * Only system memory is suitable for trampoline/kernel image placement,
264 * so only this type of memory needs its attributes to be modified.
265 */
266
267 if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
268 (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
269 continue;
270
271 unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
272 unprotect_size = min(rounded_end, next) - unprotect_start;
273
274 status = efi_dxe_call(set_memory_space_attributes,
275 unprotect_start, unprotect_size,
276 EFI_MEMORY_WB);
277
278 if (status != EFI_SUCCESS) {
279 efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
280 unprotect_start,
281 unprotect_start + unprotect_size,
282 status);
283 }
284 }
285}
286
287static void setup_unaccepted_memory(void)
288{
289 efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
290 sev_memory_acceptance_protocol_t *proto;
291 efi_status_t status;
292
293 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
294 return;
295
296 /*
297 * Enable unaccepted memory before calling exit boot services in order
298 * for the UEFI to not accept all memory on EBS.
299 */
300 status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
301 (void **)&proto);
302 if (status != EFI_SUCCESS)
303 return;
304
305 status = efi_call_proto(proto, allow_unaccepted_memory);
306 if (status != EFI_SUCCESS)
307 efi_err("Memory acceptance protocol failed\n");
308}
309
310static efi_char16_t *efistub_fw_vendor(void)
311{
312 unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);
313
314 return (efi_char16_t *)vendor;
315}
316
317static const efi_char16_t apple[] = L"Apple";
318
319static void setup_quirks(struct boot_params *boot_params)
320{
321 if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) &&
322 !memcmp(efistub_fw_vendor(), apple, sizeof(apple)))
323 retrieve_apple_device_properties(boot_params);
324}
325
326/*
327 * See if we have Universal Graphics Adapter (UGA) protocol
328 */
329static efi_status_t
330setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
331{
332 efi_status_t status;
333 u32 width, height;
334 void **uga_handle = NULL;
335 efi_uga_draw_protocol_t *uga = NULL, *first_uga;
336 efi_handle_t handle;
337 int i;
338
339 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
340 (void **)&uga_handle);
341 if (status != EFI_SUCCESS)
342 return status;
343
344 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
345 uga_proto, NULL, &size, uga_handle);
346 if (status != EFI_SUCCESS)
347 goto free_handle;
348
349 height = 0;
350 width = 0;
351
352 first_uga = NULL;
353 for_each_efi_handle(handle, uga_handle, size, i) {
354 efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
355 u32 w, h, depth, refresh;
356 void *pciio;
357
358 status = efi_bs_call(handle_protocol, handle, uga_proto,
359 (void **)&uga);
360 if (status != EFI_SUCCESS)
361 continue;
362
363 pciio = NULL;
364 efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
365
366 status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
367 if (status == EFI_SUCCESS && (!first_uga || pciio)) {
368 width = w;
369 height = h;
370
371 /*
372 * Once we've found a UGA supporting PCIIO,
373 * don't bother looking any further.
374 */
375 if (pciio)
376 break;
377
378 first_uga = uga;
379 }
380 }
381
382 if (!width && !height)
383 goto free_handle;
384
385 /* EFI framebuffer */
386 si->orig_video_isVGA = VIDEO_TYPE_EFI;
387
388 si->lfb_depth = 32;
389 si->lfb_width = width;
390 si->lfb_height = height;
391
392 si->red_size = 8;
393 si->red_pos = 16;
394 si->green_size = 8;
395 si->green_pos = 8;
396 si->blue_size = 8;
397 si->blue_pos = 0;
398 si->rsvd_size = 8;
399 si->rsvd_pos = 24;
400
401free_handle:
402 efi_bs_call(free_pool, uga_handle);
403
404 return status;
405}
406
407static void setup_graphics(struct boot_params *boot_params)
408{
409 efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
410 struct screen_info *si;
411 efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
412 efi_status_t status;
413 unsigned long size;
414 void **gop_handle = NULL;
415 void **uga_handle = NULL;
416
417 si = &boot_params->screen_info;
418 memset(si, 0, sizeof(*si));
419
420 size = 0;
421 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
422 &graphics_proto, NULL, &size, gop_handle);
423 if (status == EFI_BUFFER_TOO_SMALL)
424 status = efi_setup_gop(si, &graphics_proto, size);
425
426 if (status != EFI_SUCCESS) {
427 size = 0;
428 status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
429 &uga_proto, NULL, &size, uga_handle);
430 if (status == EFI_BUFFER_TOO_SMALL)
431 setup_uga(si, &uga_proto, size);
432 }
433}
434
435
436static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
437{
438 efi_bs_call(exit, handle, status, 0, NULL);
439 for(;;)
440 asm("hlt");
441}
442
443void __noreturn efi_stub_entry(efi_handle_t handle,
444 efi_system_table_t *sys_table_arg,
445 struct boot_params *boot_params);
446
447/*
448 * Because the x86 boot code expects to be passed a boot_params we
449 * need to create one ourselves (usually the bootloader would create
450 * one for us).
451 */
452efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
453 efi_system_table_t *sys_table_arg)
454{
455 static struct boot_params boot_params __page_aligned_bss;
456 struct setup_header *hdr = &boot_params.hdr;
457 efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
458 int options_size = 0;
459 efi_status_t status;
460 char *cmdline_ptr;
461
462 efi_system_table = sys_table_arg;
463
464 /* Check if we were booted by the EFI firmware */
465 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
466 efi_exit(handle, EFI_INVALID_PARAMETER);
467
468 status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
469 if (status != EFI_SUCCESS) {
470 efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
471 efi_exit(handle, status);
472 }
473
474 /* Assign the setup_header fields that the kernel actually cares about */
475 hdr->root_flags = 1;
476 hdr->vid_mode = 0xffff;
477
478 hdr->type_of_loader = 0x21;
479
480 /* Convert unicode cmdline to ascii */
481 cmdline_ptr = efi_convert_cmdline(image, &options_size);
482 if (!cmdline_ptr)
483 goto fail;
484
485 efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,
486 &boot_params.ext_cmd_line_ptr);
487
488 efi_stub_entry(handle, sys_table_arg, &boot_params);
489 /* not reached */
490
491fail:
492 efi_exit(handle, status);
493}
494
495static void add_e820ext(struct boot_params *params,
496 struct setup_data *e820ext, u32 nr_entries)
497{
498 struct setup_data *data;
499
500 e820ext->type = SETUP_E820_EXT;
501 e820ext->len = nr_entries * sizeof(struct boot_e820_entry);
502 e820ext->next = 0;
503
504 data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
505
506 while (data && data->next)
507 data = (struct setup_data *)(unsigned long)data->next;
508
509 if (data)
510 data->next = (unsigned long)e820ext;
511 else
512 params->hdr.setup_data = (unsigned long)e820ext;
513}
514
515static efi_status_t
516setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
517{
518 struct boot_e820_entry *entry = params->e820_table;
519 struct efi_info *efi = ¶ms->efi_info;
520 struct boot_e820_entry *prev = NULL;
521 u32 nr_entries;
522 u32 nr_desc;
523 int i;
524
525 nr_entries = 0;
526 nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
527
528 for (i = 0; i < nr_desc; i++) {
529 efi_memory_desc_t *d;
530 unsigned int e820_type = 0;
531 unsigned long m = efi->efi_memmap;
532
533#ifdef CONFIG_X86_64
534 m |= (u64)efi->efi_memmap_hi << 32;
535#endif
536
537 d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
538 switch (d->type) {
539 case EFI_RESERVED_TYPE:
540 case EFI_RUNTIME_SERVICES_CODE:
541 case EFI_RUNTIME_SERVICES_DATA:
542 case EFI_MEMORY_MAPPED_IO:
543 case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
544 case EFI_PAL_CODE:
545 e820_type = E820_TYPE_RESERVED;
546 break;
547
548 case EFI_UNUSABLE_MEMORY:
549 e820_type = E820_TYPE_UNUSABLE;
550 break;
551
552 case EFI_ACPI_RECLAIM_MEMORY:
553 e820_type = E820_TYPE_ACPI;
554 break;
555
556 case EFI_LOADER_CODE:
557 case EFI_LOADER_DATA:
558 case EFI_BOOT_SERVICES_CODE:
559 case EFI_BOOT_SERVICES_DATA:
560 case EFI_CONVENTIONAL_MEMORY:
561 if (efi_soft_reserve_enabled() &&
562 (d->attribute & EFI_MEMORY_SP))
563 e820_type = E820_TYPE_SOFT_RESERVED;
564 else
565 e820_type = E820_TYPE_RAM;
566 break;
567
568 case EFI_ACPI_MEMORY_NVS:
569 e820_type = E820_TYPE_NVS;
570 break;
571
572 case EFI_PERSISTENT_MEMORY:
573 e820_type = E820_TYPE_PMEM;
574 break;
575
576 case EFI_UNACCEPTED_MEMORY:
577 if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
578 continue;
579 e820_type = E820_TYPE_RAM;
580 process_unaccepted_memory(d->phys_addr,
581 d->phys_addr + PAGE_SIZE * d->num_pages);
582 break;
583 default:
584 continue;
585 }
586
587 /* Merge adjacent mappings */
588 if (prev && prev->type == e820_type &&
589 (prev->addr + prev->size) == d->phys_addr) {
590 prev->size += d->num_pages << 12;
591 continue;
592 }
593
594 if (nr_entries == ARRAY_SIZE(params->e820_table)) {
595 u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
596 sizeof(struct setup_data);
597
598 if (!e820ext || e820ext_size < need)
599 return EFI_BUFFER_TOO_SMALL;
600
601 /* boot_params map full, switch to e820 extended */
602 entry = (struct boot_e820_entry *)e820ext->data;
603 }
604
605 entry->addr = d->phys_addr;
606 entry->size = d->num_pages << PAGE_SHIFT;
607 entry->type = e820_type;
608 prev = entry++;
609 nr_entries++;
610 }
611
612 if (nr_entries > ARRAY_SIZE(params->e820_table)) {
613 u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
614
615 add_e820ext(params, e820ext, nr_e820ext);
616 nr_entries -= nr_e820ext;
617 }
618
619 params->e820_entries = (u8)nr_entries;
620
621 return EFI_SUCCESS;
622}
623
624static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
625 u32 *e820ext_size)
626{
627 efi_status_t status;
628 unsigned long size;
629
630 size = sizeof(struct setup_data) +
631 sizeof(struct e820_entry) * nr_desc;
632
633 if (*e820ext) {
634 efi_bs_call(free_pool, *e820ext);
635 *e820ext = NULL;
636 *e820ext_size = 0;
637 }
638
639 status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
640 (void **)e820ext);
641 if (status == EFI_SUCCESS)
642 *e820ext_size = size;
643
644 return status;
645}
646
647static efi_status_t allocate_e820(struct boot_params *params,
648 struct setup_data **e820ext,
649 u32 *e820ext_size)
650{
651 struct efi_boot_memmap *map;
652 efi_status_t status;
653 __u32 nr_desc;
654
655 status = efi_get_memory_map(&map, false);
656 if (status != EFI_SUCCESS)
657 return status;
658
659 nr_desc = map->map_size / map->desc_size;
660 if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
661 u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
662 EFI_MMAP_NR_SLACK_SLOTS;
663
664 status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
665 }
666
667 if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
668 status = allocate_unaccepted_bitmap(nr_desc, map);
669
670 efi_bs_call(free_pool, map);
671 return status;
672}
673
674struct exit_boot_struct {
675 struct boot_params *boot_params;
676 struct efi_info *efi;
677};
678
679static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
680 void *priv)
681{
682 const char *signature;
683 struct exit_boot_struct *p = priv;
684
685 signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
686 : EFI32_LOADER_SIGNATURE;
687 memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
688
689 efi_set_u64_split((unsigned long)efi_system_table,
690 &p->efi->efi_systab, &p->efi->efi_systab_hi);
691 p->efi->efi_memdesc_size = map->desc_size;
692 p->efi->efi_memdesc_version = map->desc_ver;
693 efi_set_u64_split((unsigned long)map->map,
694 &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
695 p->efi->efi_memmap_size = map->map_size;
696
697 return EFI_SUCCESS;
698}
699
700static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
701{
702 struct setup_data *e820ext = NULL;
703 __u32 e820ext_size = 0;
704 efi_status_t status;
705 struct exit_boot_struct priv;
706
707 priv.boot_params = boot_params;
708 priv.efi = &boot_params->efi_info;
709
710 status = allocate_e820(boot_params, &e820ext, &e820ext_size);
711 if (status != EFI_SUCCESS)
712 return status;
713
714 /* Might as well exit boot services now */
715 status = efi_exit_boot_services(handle, &priv, exit_boot_func);
716 if (status != EFI_SUCCESS)
717 return status;
718
719 /* Historic? */
720 boot_params->alt_mem_k = 32 * 1024;
721
722 status = setup_e820(boot_params, e820ext, e820ext_size);
723 if (status != EFI_SUCCESS)
724 return status;
725
726 return EFI_SUCCESS;
727}
728
729static bool have_unsupported_snp_features(void)
730{
731 u64 unsupported;
732
733 unsupported = snp_get_unsupported_features(sev_get_status());
734 if (unsupported) {
735 efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
736 unsupported);
737 return true;
738 }
739 return false;
740}
741
742static void efi_get_seed(void *seed, int size)
743{
744 efi_get_random_bytes(size, seed);
745
746 /*
747 * This only updates seed[0] when running on 32-bit, but in that case,
748 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
749 */
750 *(unsigned long *)seed ^= kaslr_get_random_long("EFI");
751}
752
753static void error(char *str)
754{
755 efi_warn("Decompression failed: %s\n", str);
756}
757
758static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
759{
760 unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
761 unsigned long addr, alloc_size, entry;
762 efi_status_t status;
763 u32 seed[2] = {};
764
765 /* determine the required size of the allocation */
766 alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
767 MIN_KERNEL_ALIGN);
768
769 if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
770 u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
771 static const efi_char16_t ami[] = L"American Megatrends";
772
773 efi_get_seed(seed, sizeof(seed));
774
775 virt_addr += (range * seed[1]) >> 32;
776 virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
777
778 /*
779 * Older Dell systems with AMI UEFI firmware v2.0 may hang
780 * while decompressing the kernel if physical address
781 * randomization is enabled.
782 *
783 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
784 */
785 if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&
786 !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
787 efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
788 seed[0] = 0;
789 }
790
791 boot_params_ptr->hdr.loadflags |= KASLR_FLAG;
792 }
793
794 status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
795 seed[0], EFI_LOADER_CODE,
796 EFI_X86_KERNEL_ALLOC_LIMIT);
797 if (status != EFI_SUCCESS)
798 return status;
799
800 entry = decompress_kernel((void *)addr, virt_addr, error);
801 if (entry == ULONG_MAX) {
802 efi_free(alloc_size, addr);
803 return EFI_LOAD_ERROR;
804 }
805
806 *kernel_entry = addr + entry;
807
808 efi_adjust_memory_range_protection(addr, kernel_total_size);
809
810 return EFI_SUCCESS;
811}
812
813static void __noreturn enter_kernel(unsigned long kernel_addr,
814 struct boot_params *boot_params)
815{
816 /* enter decompressed kernel with boot_params pointer in RSI/ESI */
817 asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
818
819 unreachable();
820}
821
822/*
823 * On success, this routine will jump to the relocated image directly and never
824 * return. On failure, it will exit to the firmware via efi_exit() instead of
825 * returning.
826 */
827void __noreturn efi_stub_entry(efi_handle_t handle,
828 efi_system_table_t *sys_table_arg,
829 struct boot_params *boot_params)
830{
831 efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
832 struct setup_header *hdr = &boot_params->hdr;
833 const struct linux_efi_initrd *initrd = NULL;
834 unsigned long kernel_entry;
835 efi_status_t status;
836
837 boot_params_ptr = boot_params;
838
839 efi_system_table = sys_table_arg;
840 /* Check if we were booted by the EFI firmware */
841 if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
842 efi_exit(handle, EFI_INVALID_PARAMETER);
843
844 if (have_unsupported_snp_features())
845 efi_exit(handle, EFI_UNSUPPORTED);
846
847 if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
848 efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
849 if (efi_dxe_table &&
850 efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
851 efi_warn("Ignoring DXE services table: invalid signature\n");
852 efi_dxe_table = NULL;
853 }
854 }
855
856 /* grab the memory attributes protocol if it exists */
857 efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
858
859 status = efi_setup_5level_paging();
860 if (status != EFI_SUCCESS) {
861 efi_err("efi_setup_5level_paging() failed!\n");
862 goto fail;
863 }
864
865#ifdef CONFIG_CMDLINE_BOOL
866 status = efi_parse_options(CONFIG_CMDLINE);
867 if (status != EFI_SUCCESS) {
868 efi_err("Failed to parse options\n");
869 goto fail;
870 }
871#endif
872 if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
873 unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
874 ((u64)boot_params->ext_cmd_line_ptr << 32));
875 status = efi_parse_options((char *)cmdline_paddr);
876 if (status != EFI_SUCCESS) {
877 efi_err("Failed to parse options\n");
878 goto fail;
879 }
880 }
881
882 status = efi_decompress_kernel(&kernel_entry);
883 if (status != EFI_SUCCESS) {
884 efi_err("Failed to decompress kernel\n");
885 goto fail;
886 }
887
888 /*
889 * At this point, an initrd may already have been loaded by the
890 * bootloader and passed via bootparams. We permit an initrd loaded
891 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
892 *
893 * If the device path is not present, any command-line initrd=
894 * arguments will be processed only if image is not NULL, which will be
895 * the case only if we were loaded via the PE entry point.
896 */
897 status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
898 &initrd);
899 if (status != EFI_SUCCESS)
900 goto fail;
901 if (initrd && initrd->size > 0) {
902 efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
903 &boot_params->ext_ramdisk_image);
904 efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
905 &boot_params->ext_ramdisk_size);
906 }
907
908
909 /*
910 * If the boot loader gave us a value for secure_boot then we use that,
911 * otherwise we ask the BIOS.
912 */
913 if (boot_params->secure_boot == efi_secureboot_mode_unset)
914 boot_params->secure_boot = efi_get_secureboot();
915
916 /* Ask the firmware to clear memory on unclean shutdown */
917 efi_enable_reset_attack_mitigation();
918
919 efi_random_get_seed();
920
921 efi_retrieve_tpm2_eventlog();
922
923 setup_graphics(boot_params);
924
925 setup_efi_pci(boot_params);
926
927 setup_quirks(boot_params);
928
929 setup_unaccepted_memory();
930
931 status = exit_boot(boot_params, handle);
932 if (status != EFI_SUCCESS) {
933 efi_err("exit_boot() failed!\n");
934 goto fail;
935 }
936
937 /*
938 * Call the SEV init code while still running with the firmware's
939 * GDT/IDT, so #VC exceptions will be handled by EFI.
940 */
941 sev_enable(boot_params);
942
943 efi_5level_switch();
944
945 enter_kernel(kernel_entry, boot_params);
946fail:
947 efi_err("efi_stub_entry() failed!\n");
948
949 efi_exit(handle, status);
950}
951
952#ifdef CONFIG_EFI_HANDOVER_PROTOCOL
953void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
954 struct boot_params *boot_params)
955{
956 extern char _bss[], _ebss[];
957
958 memset(_bss, 0, _ebss - _bss);
959 efi_stub_entry(handle, sys_table_arg, boot_params);
960}
961
962#ifndef CONFIG_EFI_MIXED
963extern __alias(efi_handover_entry)
964void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
965 struct boot_params *boot_params);
966
967extern __alias(efi_handover_entry)
968void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
969 struct boot_params *boot_params);
970#endif
971#endif