1c2d0b470SArd Biesheuvel // SPDX-License-Identifier: GPL-2.0-only
2c2d0b470SArd Biesheuvel 
3c2d0b470SArd Biesheuvel /* -----------------------------------------------------------------------
4c2d0b470SArd Biesheuvel  *
5c2d0b470SArd Biesheuvel  *   Copyright 2011 Intel Corporation; author Matt Fleming
6c2d0b470SArd Biesheuvel  *
7c2d0b470SArd Biesheuvel  * ----------------------------------------------------------------------- */
8c2d0b470SArd Biesheuvel 
9c2d0b470SArd Biesheuvel #include <linux/efi.h>
10c2d0b470SArd Biesheuvel #include <linux/pci.h>
1159476f80SArvind Sankar #include <linux/stddef.h>
12c2d0b470SArd Biesheuvel 
13c2d0b470SArd Biesheuvel #include <asm/efi.h>
14c2d0b470SArd Biesheuvel #include <asm/e820/types.h>
15c2d0b470SArd Biesheuvel #include <asm/setup.h>
16c2d0b470SArd Biesheuvel #include <asm/desc.h>
17c2d0b470SArd Biesheuvel #include <asm/boot.h>
18a1b87d54SArd Biesheuvel #include <asm/kaslr.h>
1931c77a50SArd Biesheuvel #include <asm/sev.h>
20c2d0b470SArd Biesheuvel 
21c2d0b470SArd Biesheuvel #include "efistub.h"
22cb1c9e02SArd Biesheuvel #include "x86-stub.h"
23c2d0b470SArd Biesheuvel 
24ae863aa1SArd Biesheuvel extern char _bss[], _ebss[];
25ae863aa1SArd Biesheuvel 
26ccc27ae7SArd Biesheuvel const efi_system_table_t *efi_system_table;
273ba75c13SBaskov Evgeniy const efi_dxe_services_table_t *efi_dxe_table;
28987053a3SArvind Sankar static efi_loaded_image_t *image = NULL;
2911078876SArd Biesheuvel static efi_memory_attribute_protocol_t *memattr;
30c2d0b470SArd Biesheuvel 
31c0461bd1SDionna Glaze typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
32c0461bd1SDionna Glaze union sev_memory_acceptance_protocol {
33c0461bd1SDionna Glaze 	struct {
34c0461bd1SDionna Glaze 		efi_status_t (__efiapi * allow_unaccepted_memory)(
35c0461bd1SDionna Glaze 			sev_memory_acceptance_protocol_t *);
36c0461bd1SDionna Glaze 	};
37c0461bd1SDionna Glaze 	struct {
38c0461bd1SDionna Glaze 		u32 allow_unaccepted_memory;
39c0461bd1SDionna Glaze 	} mixed_mode;
40c0461bd1SDionna Glaze };
41c0461bd1SDionna Glaze 
42c2d0b470SArd Biesheuvel static efi_status_t
preserve_pci_rom_image(efi_pci_io_protocol_t * pci,struct pci_setup_rom ** __rom)43c2d0b470SArd Biesheuvel preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
44c2d0b470SArd Biesheuvel {
45c2d0b470SArd Biesheuvel 	struct pci_setup_rom *rom = NULL;
46c2d0b470SArd Biesheuvel 	efi_status_t status;
47c2d0b470SArd Biesheuvel 	unsigned long size;
48c2d0b470SArd Biesheuvel 	uint64_t romsize;
49c2d0b470SArd Biesheuvel 	void *romimage;
50c2d0b470SArd Biesheuvel 
51c2d0b470SArd Biesheuvel 	/*
52c2d0b470SArd Biesheuvel 	 * Some firmware images contain EFI function pointers at the place where
53c2d0b470SArd Biesheuvel 	 * the romimage and romsize fields are supposed to be. Typically the EFI
54c2d0b470SArd Biesheuvel 	 * code is mapped at high addresses, translating to an unrealistically
55c2d0b470SArd Biesheuvel 	 * large romsize. The UEFI spec limits the size of option ROMs to 16
56c2d0b470SArd Biesheuvel 	 * MiB so we reject any ROMs over 16 MiB in size to catch this.
57c2d0b470SArd Biesheuvel 	 */
58c2d0b470SArd Biesheuvel 	romimage = efi_table_attr(pci, romimage);
59c2d0b470SArd Biesheuvel 	romsize = efi_table_attr(pci, romsize);
60c2d0b470SArd Biesheuvel 	if (!romimage || !romsize || romsize > SZ_16M)
61c2d0b470SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
62c2d0b470SArd Biesheuvel 
63c2d0b470SArd Biesheuvel 	size = romsize + sizeof(*rom);
64c2d0b470SArd Biesheuvel 
65c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
66c2d0b470SArd Biesheuvel 			     (void **)&rom);
67c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
6836bdd0a7SArvind Sankar 		efi_err("Failed to allocate memory for 'rom'\n");
69c2d0b470SArd Biesheuvel 		return status;
70c2d0b470SArd Biesheuvel 	}
71c2d0b470SArd Biesheuvel 
72c2d0b470SArd Biesheuvel 	memset(rom, 0, sizeof(*rom));
73c2d0b470SArd Biesheuvel 
74c2d0b470SArd Biesheuvel 	rom->data.type	= SETUP_PCI;
75c2d0b470SArd Biesheuvel 	rom->data.len	= size - sizeof(struct setup_data);
76c2d0b470SArd Biesheuvel 	rom->data.next	= 0;
778b94da92SMikel Rychliski 	rom->pcilen	= romsize;
78c2d0b470SArd Biesheuvel 	*__rom = rom;
79c2d0b470SArd Biesheuvel 
80c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
81c2d0b470SArd Biesheuvel 				PCI_VENDOR_ID, 1, &rom->vendor);
82c2d0b470SArd Biesheuvel 
83c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
8436bdd0a7SArvind Sankar 		efi_err("Failed to read rom->vendor\n");
85c2d0b470SArd Biesheuvel 		goto free_struct;
86c2d0b470SArd Biesheuvel 	}
87c2d0b470SArd Biesheuvel 
88c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
89c2d0b470SArd Biesheuvel 				PCI_DEVICE_ID, 1, &rom->devid);
90c2d0b470SArd Biesheuvel 
91c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
9236bdd0a7SArvind Sankar 		efi_err("Failed to read rom->devid\n");
93c2d0b470SArd Biesheuvel 		goto free_struct;
94c2d0b470SArd Biesheuvel 	}
95c2d0b470SArd Biesheuvel 
96c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
97c2d0b470SArd Biesheuvel 				&rom->device, &rom->function);
98c2d0b470SArd Biesheuvel 
99c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
100c2d0b470SArd Biesheuvel 		goto free_struct;
101c2d0b470SArd Biesheuvel 
102c2d0b470SArd Biesheuvel 	memcpy(rom->romdata, romimage, romsize);
103c2d0b470SArd Biesheuvel 	return status;
104c2d0b470SArd Biesheuvel 
105c2d0b470SArd Biesheuvel free_struct:
106c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, rom);
107c2d0b470SArd Biesheuvel 	return status;
108c2d0b470SArd Biesheuvel }
109c2d0b470SArd Biesheuvel 
110c2d0b470SArd Biesheuvel /*
111c2d0b470SArd Biesheuvel  * There's no way to return an informative status from this function,
112c2d0b470SArd Biesheuvel  * because any analysis (and printing of error messages) needs to be
113c2d0b470SArd Biesheuvel  * done directly at the EFI function call-site.
114c2d0b470SArd Biesheuvel  *
115c2d0b470SArd Biesheuvel  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
116c2d0b470SArd Biesheuvel  * just didn't find any PCI devices, but there's no way to tell outside
117c2d0b470SArd Biesheuvel  * the context of the call.
118c2d0b470SArd Biesheuvel  */
setup_efi_pci(struct boot_params * params)119c2d0b470SArd Biesheuvel static void setup_efi_pci(struct boot_params *params)
120c2d0b470SArd Biesheuvel {
121c2d0b470SArd Biesheuvel 	efi_status_t status;
122c2d0b470SArd Biesheuvel 	void **pci_handle = NULL;
123c2d0b470SArd Biesheuvel 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
124c2d0b470SArd Biesheuvel 	unsigned long size = 0;
125c2d0b470SArd Biesheuvel 	struct setup_data *data;
126c2d0b470SArd Biesheuvel 	efi_handle_t h;
127c2d0b470SArd Biesheuvel 	int i;
128c2d0b470SArd Biesheuvel 
129c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
130c2d0b470SArd Biesheuvel 			     &pci_proto, NULL, &size, pci_handle);
131c2d0b470SArd Biesheuvel 
132c2d0b470SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL) {
133c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
134c2d0b470SArd Biesheuvel 				     (void **)&pci_handle);
135c2d0b470SArd Biesheuvel 
136c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
13736bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'pci_handle'\n");
138c2d0b470SArd Biesheuvel 			return;
139c2d0b470SArd Biesheuvel 		}
140c2d0b470SArd Biesheuvel 
141c2d0b470SArd Biesheuvel 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
142c2d0b470SArd Biesheuvel 				     &pci_proto, NULL, &size, pci_handle);
143c2d0b470SArd Biesheuvel 	}
144c2d0b470SArd Biesheuvel 
145c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
146c2d0b470SArd Biesheuvel 		goto free_handle;
147c2d0b470SArd Biesheuvel 
148c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
149c2d0b470SArd Biesheuvel 
150c2d0b470SArd Biesheuvel 	while (data && data->next)
151c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
152c2d0b470SArd Biesheuvel 
153c2d0b470SArd Biesheuvel 	for_each_efi_handle(h, pci_handle, size, i) {
154c2d0b470SArd Biesheuvel 		efi_pci_io_protocol_t *pci = NULL;
155c2d0b470SArd Biesheuvel 		struct pci_setup_rom *rom;
156c2d0b470SArd Biesheuvel 
157c2d0b470SArd Biesheuvel 		status = efi_bs_call(handle_protocol, h, &pci_proto,
158c2d0b470SArd Biesheuvel 				     (void **)&pci);
159c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS || !pci)
160c2d0b470SArd Biesheuvel 			continue;
161c2d0b470SArd Biesheuvel 
162c2d0b470SArd Biesheuvel 		status = preserve_pci_rom_image(pci, &rom);
163c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
164c2d0b470SArd Biesheuvel 			continue;
165c2d0b470SArd Biesheuvel 
166c2d0b470SArd Biesheuvel 		if (data)
167c2d0b470SArd Biesheuvel 			data->next = (unsigned long)rom;
168c2d0b470SArd Biesheuvel 		else
169c2d0b470SArd Biesheuvel 			params->hdr.setup_data = (unsigned long)rom;
170c2d0b470SArd Biesheuvel 
171c2d0b470SArd Biesheuvel 		data = (struct setup_data *)rom;
172c2d0b470SArd Biesheuvel 	}
173c2d0b470SArd Biesheuvel 
174c2d0b470SArd Biesheuvel free_handle:
175c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, pci_handle);
176c2d0b470SArd Biesheuvel }
177c2d0b470SArd Biesheuvel 
retrieve_apple_device_properties(struct boot_params * boot_params)178c2d0b470SArd Biesheuvel static void retrieve_apple_device_properties(struct boot_params *boot_params)
179c2d0b470SArd Biesheuvel {
180c2d0b470SArd Biesheuvel 	efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
181c2d0b470SArd Biesheuvel 	struct setup_data *data, *new;
182c2d0b470SArd Biesheuvel 	efi_status_t status;
183c2d0b470SArd Biesheuvel 	u32 size = 0;
184c2d0b470SArd Biesheuvel 	apple_properties_protocol_t *p;
185c2d0b470SArd Biesheuvel 
186c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
187c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
188c2d0b470SArd Biesheuvel 		return;
189c2d0b470SArd Biesheuvel 
190c2d0b470SArd Biesheuvel 	if (efi_table_attr(p, version) != 0x10000) {
19136bdd0a7SArvind Sankar 		efi_err("Unsupported properties proto version\n");
192c2d0b470SArd Biesheuvel 		return;
193c2d0b470SArd Biesheuvel 	}
194c2d0b470SArd Biesheuvel 
195c2d0b470SArd Biesheuvel 	efi_call_proto(p, get_all, NULL, &size);
196c2d0b470SArd Biesheuvel 	if (!size)
197c2d0b470SArd Biesheuvel 		return;
198c2d0b470SArd Biesheuvel 
199c2d0b470SArd Biesheuvel 	do {
200c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
201c2d0b470SArd Biesheuvel 				     size + sizeof(struct setup_data),
202c2d0b470SArd Biesheuvel 				     (void **)&new);
203c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
20436bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'properties'\n");
205c2d0b470SArd Biesheuvel 			return;
206c2d0b470SArd Biesheuvel 		}
207c2d0b470SArd Biesheuvel 
208c2d0b470SArd Biesheuvel 		status = efi_call_proto(p, get_all, new->data, &size);
209c2d0b470SArd Biesheuvel 
210c2d0b470SArd Biesheuvel 		if (status == EFI_BUFFER_TOO_SMALL)
211c2d0b470SArd Biesheuvel 			efi_bs_call(free_pool, new);
212c2d0b470SArd Biesheuvel 	} while (status == EFI_BUFFER_TOO_SMALL);
213c2d0b470SArd Biesheuvel 
214c2d0b470SArd Biesheuvel 	new->type = SETUP_APPLE_PROPERTIES;
215c2d0b470SArd Biesheuvel 	new->len  = size;
216c2d0b470SArd Biesheuvel 	new->next = 0;
217c2d0b470SArd Biesheuvel 
218c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
219c2d0b470SArd Biesheuvel 	if (!data) {
220c2d0b470SArd Biesheuvel 		boot_params->hdr.setup_data = (unsigned long)new;
221c2d0b470SArd Biesheuvel 	} else {
222c2d0b470SArd Biesheuvel 		while (data->next)
223c2d0b470SArd Biesheuvel 			data = (struct setup_data *)(unsigned long)data->next;
224c2d0b470SArd Biesheuvel 		data->next = (unsigned long)new;
225c2d0b470SArd Biesheuvel 	}
226c2d0b470SArd Biesheuvel }
227c2d0b470SArd Biesheuvel 
efi_adjust_memory_range_protection(unsigned long start,unsigned long size)228c756fd5dSArd Biesheuvel efi_status_t efi_adjust_memory_range_protection(unsigned long start,
229cb1c9e02SArd Biesheuvel 						unsigned long size)
23082e0d6d7SBaskov Evgeniy {
23182e0d6d7SBaskov Evgeniy 	efi_status_t status;
23282e0d6d7SBaskov Evgeniy 	efi_gcd_memory_space_desc_t desc;
23382e0d6d7SBaskov Evgeniy 	unsigned long end, next;
23482e0d6d7SBaskov Evgeniy 	unsigned long rounded_start, rounded_end;
23582e0d6d7SBaskov Evgeniy 	unsigned long unprotect_start, unprotect_size;
23682e0d6d7SBaskov Evgeniy 
23782e0d6d7SBaskov Evgeniy 	rounded_start = rounddown(start, EFI_PAGE_SIZE);
23882e0d6d7SBaskov Evgeniy 	rounded_end = roundup(start + size, EFI_PAGE_SIZE);
23982e0d6d7SBaskov Evgeniy 
24011078876SArd Biesheuvel 	if (memattr != NULL) {
241ccde70aaSArd Biesheuvel 		status = efi_call_proto(memattr, set_memory_attributes,
242ccde70aaSArd Biesheuvel 					rounded_start,
243ccde70aaSArd Biesheuvel 					rounded_end - rounded_start,
244ccde70aaSArd Biesheuvel 					EFI_MEMORY_RO);
245ccde70aaSArd Biesheuvel 		if (status != EFI_SUCCESS) {
246ccde70aaSArd Biesheuvel 			efi_warn("Failed to set EFI_MEMORY_RO attribute\n");
247ccde70aaSArd Biesheuvel 			return status;
248ccde70aaSArd Biesheuvel 		}
249ccde70aaSArd Biesheuvel 
250c756fd5dSArd Biesheuvel 		status = efi_call_proto(memattr, clear_memory_attributes,
251c756fd5dSArd Biesheuvel 					rounded_start,
252c756fd5dSArd Biesheuvel 					rounded_end - rounded_start,
253c756fd5dSArd Biesheuvel 					EFI_MEMORY_XP);
254c756fd5dSArd Biesheuvel 		if (status != EFI_SUCCESS)
255c756fd5dSArd Biesheuvel 			efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");
256c756fd5dSArd Biesheuvel 		return status;
25711078876SArd Biesheuvel 	}
25811078876SArd Biesheuvel 
25911078876SArd Biesheuvel 	if (efi_dxe_table == NULL)
260c756fd5dSArd Biesheuvel 		return EFI_SUCCESS;
26111078876SArd Biesheuvel 
26282e0d6d7SBaskov Evgeniy 	/*
26382e0d6d7SBaskov Evgeniy 	 * Don't modify memory region attributes, they are
26482e0d6d7SBaskov Evgeniy 	 * already suitable, to lower the possibility to
26582e0d6d7SBaskov Evgeniy 	 * encounter firmware bugs.
26682e0d6d7SBaskov Evgeniy 	 */
26782e0d6d7SBaskov Evgeniy 
26882e0d6d7SBaskov Evgeniy 	for (end = start + size; start < end; start = next) {
26982e0d6d7SBaskov Evgeniy 
27082e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
27182e0d6d7SBaskov Evgeniy 
27282e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS)
273c756fd5dSArd Biesheuvel 			break;
27482e0d6d7SBaskov Evgeniy 
27582e0d6d7SBaskov Evgeniy 		next = desc.base_address + desc.length;
27682e0d6d7SBaskov Evgeniy 
27782e0d6d7SBaskov Evgeniy 		/*
27882e0d6d7SBaskov Evgeniy 		 * Only system memory is suitable for trampoline/kernel image placement,
27982e0d6d7SBaskov Evgeniy 		 * so only this type of memory needs its attributes to be modified.
28082e0d6d7SBaskov Evgeniy 		 */
28182e0d6d7SBaskov Evgeniy 
28282e0d6d7SBaskov Evgeniy 		if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
28382e0d6d7SBaskov Evgeniy 		    (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
28482e0d6d7SBaskov Evgeniy 			continue;
28582e0d6d7SBaskov Evgeniy 
28682e0d6d7SBaskov Evgeniy 		unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
28782e0d6d7SBaskov Evgeniy 		unprotect_size = min(rounded_end, next) - unprotect_start;
28882e0d6d7SBaskov Evgeniy 
28982e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(set_memory_space_attributes,
29082e0d6d7SBaskov Evgeniy 				      unprotect_start, unprotect_size,
29182e0d6d7SBaskov Evgeniy 				      EFI_MEMORY_WB);
29282e0d6d7SBaskov Evgeniy 
29382e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS) {
29431f1a0edSArd Biesheuvel 			efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
29582e0d6d7SBaskov Evgeniy 				 unprotect_start,
29682e0d6d7SBaskov Evgeniy 				 unprotect_start + unprotect_size,
29731f1a0edSArd Biesheuvel 				 status);
298c756fd5dSArd Biesheuvel 			break;
29982e0d6d7SBaskov Evgeniy 		}
30082e0d6d7SBaskov Evgeniy 	}
301c756fd5dSArd Biesheuvel 	return EFI_SUCCESS;
30282e0d6d7SBaskov Evgeniy }
30382e0d6d7SBaskov Evgeniy 
setup_unaccepted_memory(void)304c0461bd1SDionna Glaze static void setup_unaccepted_memory(void)
305c0461bd1SDionna Glaze {
306c0461bd1SDionna Glaze 	efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
307c0461bd1SDionna Glaze 	sev_memory_acceptance_protocol_t *proto;
308c0461bd1SDionna Glaze 	efi_status_t status;
309c0461bd1SDionna Glaze 
310c0461bd1SDionna Glaze 	if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
311c0461bd1SDionna Glaze 		return;
312c0461bd1SDionna Glaze 
313c0461bd1SDionna Glaze 	/*
314c0461bd1SDionna Glaze 	 * Enable unaccepted memory before calling exit boot services in order
315c0461bd1SDionna Glaze 	 * for the UEFI to not accept all memory on EBS.
316c0461bd1SDionna Glaze 	 */
317c0461bd1SDionna Glaze 	status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
318c0461bd1SDionna Glaze 			     (void **)&proto);
319c0461bd1SDionna Glaze 	if (status != EFI_SUCCESS)
320c0461bd1SDionna Glaze 		return;
321c0461bd1SDionna Glaze 
322c0461bd1SDionna Glaze 	status = efi_call_proto(proto, allow_unaccepted_memory);
323c0461bd1SDionna Glaze 	if (status != EFI_SUCCESS)
324c0461bd1SDionna Glaze 		efi_err("Memory acceptance protocol failed\n");
325c0461bd1SDionna Glaze }
326c0461bd1SDionna Glaze 
efistub_fw_vendor(void)327800f84d8SArd Biesheuvel static efi_char16_t *efistub_fw_vendor(void)
328800f84d8SArd Biesheuvel {
329800f84d8SArd Biesheuvel 	unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);
330800f84d8SArd Biesheuvel 
331800f84d8SArd Biesheuvel 	return (efi_char16_t *)vendor;
332800f84d8SArd Biesheuvel }
333800f84d8SArd Biesheuvel 
334c2d0b470SArd Biesheuvel static const efi_char16_t apple[] = L"Apple";
335c2d0b470SArd Biesheuvel 
setup_quirks(struct boot_params * boot_params)336a1b87d54SArd Biesheuvel static void setup_quirks(struct boot_params *boot_params)
337c2d0b470SArd Biesheuvel {
338800f84d8SArd Biesheuvel 	if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) &&
339800f84d8SArd Biesheuvel 	    !memcmp(efistub_fw_vendor(), apple, sizeof(apple)))
340c2d0b470SArd Biesheuvel 		retrieve_apple_device_properties(boot_params);
341c2d0b470SArd Biesheuvel }
342c2d0b470SArd Biesheuvel 
343c2d0b470SArd Biesheuvel /*
344c2d0b470SArd Biesheuvel  * See if we have Universal Graphics Adapter (UGA) protocol
345c2d0b470SArd Biesheuvel  */
346c2d0b470SArd Biesheuvel static efi_status_t
setup_uga(struct screen_info * si,efi_guid_t * uga_proto,unsigned long size)347c2d0b470SArd Biesheuvel setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
348c2d0b470SArd Biesheuvel {
349c2d0b470SArd Biesheuvel 	efi_status_t status;
350c2d0b470SArd Biesheuvel 	u32 width, height;
351c2d0b470SArd Biesheuvel 	void **uga_handle = NULL;
352c2d0b470SArd Biesheuvel 	efi_uga_draw_protocol_t *uga = NULL, *first_uga;
353c2d0b470SArd Biesheuvel 	efi_handle_t handle;
354c2d0b470SArd Biesheuvel 	int i;
355c2d0b470SArd Biesheuvel 
356c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
357c2d0b470SArd Biesheuvel 			     (void **)&uga_handle);
358c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
359c2d0b470SArd Biesheuvel 		return status;
360c2d0b470SArd Biesheuvel 
361c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
362c2d0b470SArd Biesheuvel 			     uga_proto, NULL, &size, uga_handle);
363c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
364c2d0b470SArd Biesheuvel 		goto free_handle;
365c2d0b470SArd Biesheuvel 
366c2d0b470SArd Biesheuvel 	height = 0;
367c2d0b470SArd Biesheuvel 	width = 0;
368c2d0b470SArd Biesheuvel 
369c2d0b470SArd Biesheuvel 	first_uga = NULL;
370c2d0b470SArd Biesheuvel 	for_each_efi_handle(handle, uga_handle, size, i) {
371c2d0b470SArd Biesheuvel 		efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
372c2d0b470SArd Biesheuvel 		u32 w, h, depth, refresh;
373c2d0b470SArd Biesheuvel 		void *pciio;
374c2d0b470SArd Biesheuvel 
375c2d0b470SArd Biesheuvel 		status = efi_bs_call(handle_protocol, handle, uga_proto,
376c2d0b470SArd Biesheuvel 				     (void **)&uga);
377c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
378c2d0b470SArd Biesheuvel 			continue;
379c2d0b470SArd Biesheuvel 
380c2d0b470SArd Biesheuvel 		pciio = NULL;
381c2d0b470SArd Biesheuvel 		efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
382c2d0b470SArd Biesheuvel 
383c2d0b470SArd Biesheuvel 		status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
384c2d0b470SArd Biesheuvel 		if (status == EFI_SUCCESS && (!first_uga || pciio)) {
385c2d0b470SArd Biesheuvel 			width = w;
386c2d0b470SArd Biesheuvel 			height = h;
387c2d0b470SArd Biesheuvel 
388c2d0b470SArd Biesheuvel 			/*
389c2d0b470SArd Biesheuvel 			 * Once we've found a UGA supporting PCIIO,
390c2d0b470SArd Biesheuvel 			 * don't bother looking any further.
391c2d0b470SArd Biesheuvel 			 */
392c2d0b470SArd Biesheuvel 			if (pciio)
393c2d0b470SArd Biesheuvel 				break;
394c2d0b470SArd Biesheuvel 
395c2d0b470SArd Biesheuvel 			first_uga = uga;
396c2d0b470SArd Biesheuvel 		}
397c2d0b470SArd Biesheuvel 	}
398c2d0b470SArd Biesheuvel 
399c2d0b470SArd Biesheuvel 	if (!width && !height)
400c2d0b470SArd Biesheuvel 		goto free_handle;
401c2d0b470SArd Biesheuvel 
402c2d0b470SArd Biesheuvel 	/* EFI framebuffer */
403c2d0b470SArd Biesheuvel 	si->orig_video_isVGA	= VIDEO_TYPE_EFI;
404c2d0b470SArd Biesheuvel 
405c2d0b470SArd Biesheuvel 	si->lfb_depth		= 32;
406c2d0b470SArd Biesheuvel 	si->lfb_width		= width;
407c2d0b470SArd Biesheuvel 	si->lfb_height		= height;
408c2d0b470SArd Biesheuvel 
409c2d0b470SArd Biesheuvel 	si->red_size		= 8;
410c2d0b470SArd Biesheuvel 	si->red_pos		= 16;
411c2d0b470SArd Biesheuvel 	si->green_size		= 8;
412c2d0b470SArd Biesheuvel 	si->green_pos		= 8;
413c2d0b470SArd Biesheuvel 	si->blue_size		= 8;
414c2d0b470SArd Biesheuvel 	si->blue_pos		= 0;
415c2d0b470SArd Biesheuvel 	si->rsvd_size		= 8;
416c2d0b470SArd Biesheuvel 	si->rsvd_pos		= 24;
417c2d0b470SArd Biesheuvel 
418c2d0b470SArd Biesheuvel free_handle:
419c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, uga_handle);
420c2d0b470SArd Biesheuvel 
421c2d0b470SArd Biesheuvel 	return status;
422c2d0b470SArd Biesheuvel }
423c2d0b470SArd Biesheuvel 
setup_graphics(struct boot_params * boot_params)424c2d0b470SArd Biesheuvel static void setup_graphics(struct boot_params *boot_params)
425c2d0b470SArd Biesheuvel {
426c2d0b470SArd Biesheuvel 	efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
427c2d0b470SArd Biesheuvel 	struct screen_info *si;
428c2d0b470SArd Biesheuvel 	efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
429c2d0b470SArd Biesheuvel 	efi_status_t status;
430c2d0b470SArd Biesheuvel 	unsigned long size;
431c2d0b470SArd Biesheuvel 	void **gop_handle = NULL;
432c2d0b470SArd Biesheuvel 	void **uga_handle = NULL;
433c2d0b470SArd Biesheuvel 
434c2d0b470SArd Biesheuvel 	si = &boot_params->screen_info;
435c2d0b470SArd Biesheuvel 	memset(si, 0, sizeof(*si));
436c2d0b470SArd Biesheuvel 
437c2d0b470SArd Biesheuvel 	size = 0;
438c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
439c2d0b470SArd Biesheuvel 			     &graphics_proto, NULL, &size, gop_handle);
440c2d0b470SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL)
441c2d0b470SArd Biesheuvel 		status = efi_setup_gop(si, &graphics_proto, size);
442c2d0b470SArd Biesheuvel 
443c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
444c2d0b470SArd Biesheuvel 		size = 0;
445c2d0b470SArd Biesheuvel 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
446c2d0b470SArd Biesheuvel 				     &uga_proto, NULL, &size, uga_handle);
447c2d0b470SArd Biesheuvel 		if (status == EFI_BUFFER_TOO_SMALL)
448c2d0b470SArd Biesheuvel 			setup_uga(si, &uga_proto, size);
449c2d0b470SArd Biesheuvel 	}
450c2d0b470SArd Biesheuvel }
451c2d0b470SArd Biesheuvel 
4523b8f44fcSArd Biesheuvel 
efi_exit(efi_handle_t handle,efi_status_t status)4533b8f44fcSArd Biesheuvel static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
4543b8f44fcSArd Biesheuvel {
4553b8f44fcSArd Biesheuvel 	efi_bs_call(exit, handle, status, 0, NULL);
456f3fa0efcSArd Biesheuvel 	for(;;)
457f3fa0efcSArd Biesheuvel 		asm("hlt");
4583b8f44fcSArd Biesheuvel }
4593b8f44fcSArd Biesheuvel 
460c2d0b470SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle,
461c2d0b470SArd Biesheuvel 			       efi_system_table_t *sys_table_arg,
462c2d0b470SArd Biesheuvel 			       struct boot_params *boot_params);
463c2d0b470SArd Biesheuvel 
464c2d0b470SArd Biesheuvel /*
465c2d0b470SArd Biesheuvel  * Because the x86 boot code expects to be passed a boot_params we
466c2d0b470SArd Biesheuvel  * need to create one ourselves (usually the bootloader would create
467c2d0b470SArd Biesheuvel  * one for us).
468c2d0b470SArd Biesheuvel  */
efi_pe_entry(efi_handle_t handle,efi_system_table_t * sys_table_arg)469c2d0b470SArd Biesheuvel efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
470c2d0b470SArd Biesheuvel 				   efi_system_table_t *sys_table_arg)
471c2d0b470SArd Biesheuvel {
4728117961dSArd Biesheuvel 	static struct boot_params boot_params __page_aligned_bss;
4738117961dSArd Biesheuvel 	struct setup_header *hdr = &boot_params.hdr;
474c2d0b470SArd Biesheuvel 	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
475c2d0b470SArd Biesheuvel 	int options_size = 0;
476c2d0b470SArd Biesheuvel 	efi_status_t status;
477c2d0b470SArd Biesheuvel 	char *cmdline_ptr;
478c2d0b470SArd Biesheuvel 
4795ad5dcfdSArd Biesheuvel 	if (efi_is_native())
480ae863aa1SArd Biesheuvel 		memset(_bss, 0, _ebss - _bss);
481ae863aa1SArd Biesheuvel 
482ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
483c2d0b470SArd Biesheuvel 
484c2d0b470SArd Biesheuvel 	/* Check if we were booted by the EFI firmware */
485ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
4863b8f44fcSArd Biesheuvel 		efi_exit(handle, EFI_INVALID_PARAMETER);
487c2d0b470SArd Biesheuvel 
4880347d8c2SArvind Sankar 	status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
489c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
49036bdd0a7SArvind Sankar 		efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
4913b8f44fcSArd Biesheuvel 		efi_exit(handle, status);
492c2d0b470SArd Biesheuvel 	}
493c2d0b470SArd Biesheuvel 
4948117961dSArd Biesheuvel 	/* Assign the setup_header fields that the kernel actually cares about */
495c2d0b470SArd Biesheuvel 	hdr->root_flags	= 1;
496c2d0b470SArd Biesheuvel 	hdr->vid_mode	= 0xffff;
497c2d0b470SArd Biesheuvel 
498c2d0b470SArd Biesheuvel 	hdr->type_of_loader = 0x21;
4995110da79SArd Biesheuvel 	hdr->initrd_addr_max = INT_MAX;
500c2d0b470SArd Biesheuvel 
501c2d0b470SArd Biesheuvel 	/* Convert unicode cmdline to ascii */
50227cd5511SArd Biesheuvel 	cmdline_ptr = efi_convert_cmdline(image, &options_size);
503c2d0b470SArd Biesheuvel 	if (!cmdline_ptr)
504c2d0b470SArd Biesheuvel 		goto fail;
505c2d0b470SArd Biesheuvel 
5068117961dSArd Biesheuvel 	efi_set_u64_split((unsigned long)cmdline_ptr, &hdr->cmd_line_ptr,
5078117961dSArd Biesheuvel 			  &boot_params.ext_cmd_line_ptr);
508c2d0b470SArd Biesheuvel 
5098117961dSArd Biesheuvel 	efi_stub_entry(handle, sys_table_arg, &boot_params);
510c2d0b470SArd Biesheuvel 	/* not reached */
511c2d0b470SArd Biesheuvel 
512c2d0b470SArd Biesheuvel fail:
5133b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
514c2d0b470SArd Biesheuvel }
515c2d0b470SArd Biesheuvel 
add_e820ext(struct boot_params * params,struct setup_data * e820ext,u32 nr_entries)516c2d0b470SArd Biesheuvel static void add_e820ext(struct boot_params *params,
517c2d0b470SArd Biesheuvel 			struct setup_data *e820ext, u32 nr_entries)
518c2d0b470SArd Biesheuvel {
519c2d0b470SArd Biesheuvel 	struct setup_data *data;
520c2d0b470SArd Biesheuvel 
521c2d0b470SArd Biesheuvel 	e820ext->type = SETUP_E820_EXT;
522c2d0b470SArd Biesheuvel 	e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
523c2d0b470SArd Biesheuvel 	e820ext->next = 0;
524c2d0b470SArd Biesheuvel 
525c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
526c2d0b470SArd Biesheuvel 
527c2d0b470SArd Biesheuvel 	while (data && data->next)
528c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
529c2d0b470SArd Biesheuvel 
530c2d0b470SArd Biesheuvel 	if (data)
531c2d0b470SArd Biesheuvel 		data->next = (unsigned long)e820ext;
532c2d0b470SArd Biesheuvel 	else
533c2d0b470SArd Biesheuvel 		params->hdr.setup_data = (unsigned long)e820ext;
534c2d0b470SArd Biesheuvel }
535c2d0b470SArd Biesheuvel 
536c2d0b470SArd Biesheuvel static efi_status_t
setup_e820(struct boot_params * params,struct setup_data * e820ext,u32 e820ext_size)537c2d0b470SArd Biesheuvel setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
538c2d0b470SArd Biesheuvel {
539c2d0b470SArd Biesheuvel 	struct boot_e820_entry *entry = params->e820_table;
540c2d0b470SArd Biesheuvel 	struct efi_info *efi = &params->efi_info;
541c2d0b470SArd Biesheuvel 	struct boot_e820_entry *prev = NULL;
542c2d0b470SArd Biesheuvel 	u32 nr_entries;
543c2d0b470SArd Biesheuvel 	u32 nr_desc;
544c2d0b470SArd Biesheuvel 	int i;
545c2d0b470SArd Biesheuvel 
546c2d0b470SArd Biesheuvel 	nr_entries = 0;
547c2d0b470SArd Biesheuvel 	nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
548c2d0b470SArd Biesheuvel 
549c2d0b470SArd Biesheuvel 	for (i = 0; i < nr_desc; i++) {
550c2d0b470SArd Biesheuvel 		efi_memory_desc_t *d;
551c2d0b470SArd Biesheuvel 		unsigned int e820_type = 0;
552c2d0b470SArd Biesheuvel 		unsigned long m = efi->efi_memmap;
553c2d0b470SArd Biesheuvel 
554c2d0b470SArd Biesheuvel #ifdef CONFIG_X86_64
555c2d0b470SArd Biesheuvel 		m |= (u64)efi->efi_memmap_hi << 32;
556c2d0b470SArd Biesheuvel #endif
557c2d0b470SArd Biesheuvel 
558c2d0b470SArd Biesheuvel 		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
559c2d0b470SArd Biesheuvel 		switch (d->type) {
560c2d0b470SArd Biesheuvel 		case EFI_RESERVED_TYPE:
561c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_CODE:
562c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_DATA:
563c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO:
564c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
565c2d0b470SArd Biesheuvel 		case EFI_PAL_CODE:
566c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_RESERVED;
567c2d0b470SArd Biesheuvel 			break;
568c2d0b470SArd Biesheuvel 
569c2d0b470SArd Biesheuvel 		case EFI_UNUSABLE_MEMORY:
570c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_UNUSABLE;
571c2d0b470SArd Biesheuvel 			break;
572c2d0b470SArd Biesheuvel 
573c2d0b470SArd Biesheuvel 		case EFI_ACPI_RECLAIM_MEMORY:
574c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_ACPI;
575c2d0b470SArd Biesheuvel 			break;
576c2d0b470SArd Biesheuvel 
577c2d0b470SArd Biesheuvel 		case EFI_LOADER_CODE:
578c2d0b470SArd Biesheuvel 		case EFI_LOADER_DATA:
579c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_CODE:
580c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_DATA:
581c2d0b470SArd Biesheuvel 		case EFI_CONVENTIONAL_MEMORY:
582c2d0b470SArd Biesheuvel 			if (efi_soft_reserve_enabled() &&
583c2d0b470SArd Biesheuvel 			    (d->attribute & EFI_MEMORY_SP))
584c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_SOFT_RESERVED;
585c2d0b470SArd Biesheuvel 			else
586c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_RAM;
587c2d0b470SArd Biesheuvel 			break;
588c2d0b470SArd Biesheuvel 
589c2d0b470SArd Biesheuvel 		case EFI_ACPI_MEMORY_NVS:
590c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_NVS;
591c2d0b470SArd Biesheuvel 			break;
592c2d0b470SArd Biesheuvel 
593c2d0b470SArd Biesheuvel 		case EFI_PERSISTENT_MEMORY:
594c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_PMEM;
595c2d0b470SArd Biesheuvel 			break;
596c2d0b470SArd Biesheuvel 
597745e3ed8SKirill A. Shutemov 		case EFI_UNACCEPTED_MEMORY:
598ff07186bSNikolay Borisov 			if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
599745e3ed8SKirill A. Shutemov 				continue;
600745e3ed8SKirill A. Shutemov 			e820_type = E820_TYPE_RAM;
601745e3ed8SKirill A. Shutemov 			process_unaccepted_memory(d->phys_addr,
602745e3ed8SKirill A. Shutemov 						  d->phys_addr + PAGE_SIZE * d->num_pages);
603745e3ed8SKirill A. Shutemov 			break;
604c2d0b470SArd Biesheuvel 		default:
605c2d0b470SArd Biesheuvel 			continue;
606c2d0b470SArd Biesheuvel 		}
607c2d0b470SArd Biesheuvel 
608c2d0b470SArd Biesheuvel 		/* Merge adjacent mappings */
609c2d0b470SArd Biesheuvel 		if (prev && prev->type == e820_type &&
610c2d0b470SArd Biesheuvel 		    (prev->addr + prev->size) == d->phys_addr) {
611c2d0b470SArd Biesheuvel 			prev->size += d->num_pages << 12;
612c2d0b470SArd Biesheuvel 			continue;
613c2d0b470SArd Biesheuvel 		}
614c2d0b470SArd Biesheuvel 
615c2d0b470SArd Biesheuvel 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
616c2d0b470SArd Biesheuvel 			u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
617c2d0b470SArd Biesheuvel 				   sizeof(struct setup_data);
618c2d0b470SArd Biesheuvel 
619c2d0b470SArd Biesheuvel 			if (!e820ext || e820ext_size < need)
620c2d0b470SArd Biesheuvel 				return EFI_BUFFER_TOO_SMALL;
621c2d0b470SArd Biesheuvel 
622c2d0b470SArd Biesheuvel 			/* boot_params map full, switch to e820 extended */
623c2d0b470SArd Biesheuvel 			entry = (struct boot_e820_entry *)e820ext->data;
624c2d0b470SArd Biesheuvel 		}
625c2d0b470SArd Biesheuvel 
626c2d0b470SArd Biesheuvel 		entry->addr = d->phys_addr;
627c2d0b470SArd Biesheuvel 		entry->size = d->num_pages << PAGE_SHIFT;
628c2d0b470SArd Biesheuvel 		entry->type = e820_type;
629c2d0b470SArd Biesheuvel 		prev = entry++;
630c2d0b470SArd Biesheuvel 		nr_entries++;
631c2d0b470SArd Biesheuvel 	}
632c2d0b470SArd Biesheuvel 
633c2d0b470SArd Biesheuvel 	if (nr_entries > ARRAY_SIZE(params->e820_table)) {
634c2d0b470SArd Biesheuvel 		u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
635c2d0b470SArd Biesheuvel 
636c2d0b470SArd Biesheuvel 		add_e820ext(params, e820ext, nr_e820ext);
637c2d0b470SArd Biesheuvel 		nr_entries -= nr_e820ext;
638c2d0b470SArd Biesheuvel 	}
639c2d0b470SArd Biesheuvel 
640c2d0b470SArd Biesheuvel 	params->e820_entries = (u8)nr_entries;
641c2d0b470SArd Biesheuvel 
642c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
643c2d0b470SArd Biesheuvel }
644c2d0b470SArd Biesheuvel 
alloc_e820ext(u32 nr_desc,struct setup_data ** e820ext,u32 * e820ext_size)645c2d0b470SArd Biesheuvel static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
646c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
647c2d0b470SArd Biesheuvel {
648c2d0b470SArd Biesheuvel 	efi_status_t status;
649c2d0b470SArd Biesheuvel 	unsigned long size;
650c2d0b470SArd Biesheuvel 
651c2d0b470SArd Biesheuvel 	size = sizeof(struct setup_data) +
652c2d0b470SArd Biesheuvel 		sizeof(struct e820_entry) * nr_desc;
653c2d0b470SArd Biesheuvel 
654c2d0b470SArd Biesheuvel 	if (*e820ext) {
655c2d0b470SArd Biesheuvel 		efi_bs_call(free_pool, *e820ext);
656c2d0b470SArd Biesheuvel 		*e820ext = NULL;
657c2d0b470SArd Biesheuvel 		*e820ext_size = 0;
658c2d0b470SArd Biesheuvel 	}
659c2d0b470SArd Biesheuvel 
660c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
661c2d0b470SArd Biesheuvel 			     (void **)e820ext);
662c2d0b470SArd Biesheuvel 	if (status == EFI_SUCCESS)
663c2d0b470SArd Biesheuvel 		*e820ext_size = size;
664c2d0b470SArd Biesheuvel 
665c2d0b470SArd Biesheuvel 	return status;
666c2d0b470SArd Biesheuvel }
667c2d0b470SArd Biesheuvel 
allocate_e820(struct boot_params * params,struct setup_data ** e820ext,u32 * e820ext_size)668c2d0b470SArd Biesheuvel static efi_status_t allocate_e820(struct boot_params *params,
669c2d0b470SArd Biesheuvel 				  struct setup_data **e820ext,
670c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
671c2d0b470SArd Biesheuvel {
6722e9f46eeSKirill A. Shutemov 	struct efi_boot_memmap *map;
673c2d0b470SArd Biesheuvel 	efi_status_t status;
6742e9f46eeSKirill A. Shutemov 	__u32 nr_desc;
675c2d0b470SArd Biesheuvel 
6762e9f46eeSKirill A. Shutemov 	status = efi_get_memory_map(&map, false);
677c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
678c2d0b470SArd Biesheuvel 		return status;
6792e9f46eeSKirill A. Shutemov 
6802e9f46eeSKirill A. Shutemov 	nr_desc = map->map_size / map->desc_size;
6812e9f46eeSKirill A. Shutemov 	if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
6822e9f46eeSKirill A. Shutemov 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
6832e9f46eeSKirill A. Shutemov 				 EFI_MMAP_NR_SLACK_SLOTS;
6842e9f46eeSKirill A. Shutemov 
6852e9f46eeSKirill A. Shutemov 		status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
686c2d0b470SArd Biesheuvel 	}
687c2d0b470SArd Biesheuvel 
688745e3ed8SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
689745e3ed8SKirill A. Shutemov 		status = allocate_unaccepted_bitmap(nr_desc, map);
690745e3ed8SKirill A. Shutemov 
6912e9f46eeSKirill A. Shutemov 	efi_bs_call(free_pool, map);
6922e9f46eeSKirill A. Shutemov 	return status;
693c2d0b470SArd Biesheuvel }
694c2d0b470SArd Biesheuvel 
695c2d0b470SArd Biesheuvel struct exit_boot_struct {
696c2d0b470SArd Biesheuvel 	struct boot_params	*boot_params;
697c2d0b470SArd Biesheuvel 	struct efi_info		*efi;
698c2d0b470SArd Biesheuvel };
699c2d0b470SArd Biesheuvel 
exit_boot_func(struct efi_boot_memmap * map,void * priv)700c2d0b470SArd Biesheuvel static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
701c2d0b470SArd Biesheuvel 				   void *priv)
702c2d0b470SArd Biesheuvel {
703c2d0b470SArd Biesheuvel 	const char *signature;
704c2d0b470SArd Biesheuvel 	struct exit_boot_struct *p = priv;
705c2d0b470SArd Biesheuvel 
706c2d0b470SArd Biesheuvel 	signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
707c2d0b470SArd Biesheuvel 				   : EFI32_LOADER_SIGNATURE;
708c2d0b470SArd Biesheuvel 	memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
709c2d0b470SArd Biesheuvel 
710eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)efi_system_table,
711eed4e019SArvind Sankar 			  &p->efi->efi_systab, &p->efi->efi_systab_hi);
712eab31265SArd Biesheuvel 	p->efi->efi_memdesc_size	= map->desc_size;
713eab31265SArd Biesheuvel 	p->efi->efi_memdesc_version	= map->desc_ver;
714eab31265SArd Biesheuvel 	efi_set_u64_split((unsigned long)map->map,
715eed4e019SArvind Sankar 			  &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
716eab31265SArd Biesheuvel 	p->efi->efi_memmap_size		= map->map_size;
717c2d0b470SArd Biesheuvel 
718c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
719c2d0b470SArd Biesheuvel }
720c2d0b470SArd Biesheuvel 
exit_boot(struct boot_params * boot_params,void * handle)721c2d0b470SArd Biesheuvel static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
722c2d0b470SArd Biesheuvel {
723c2d0b470SArd Biesheuvel 	struct setup_data *e820ext = NULL;
724c2d0b470SArd Biesheuvel 	__u32 e820ext_size = 0;
725c2d0b470SArd Biesheuvel 	efi_status_t status;
726c2d0b470SArd Biesheuvel 	struct exit_boot_struct priv;
727c2d0b470SArd Biesheuvel 
728c2d0b470SArd Biesheuvel 	priv.boot_params	= boot_params;
729c2d0b470SArd Biesheuvel 	priv.efi		= &boot_params->efi_info;
730c2d0b470SArd Biesheuvel 
731c2d0b470SArd Biesheuvel 	status = allocate_e820(boot_params, &e820ext, &e820ext_size);
732c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
733c2d0b470SArd Biesheuvel 		return status;
734c2d0b470SArd Biesheuvel 
735c2d0b470SArd Biesheuvel 	/* Might as well exit boot services now */
736eab31265SArd Biesheuvel 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
737c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
738c2d0b470SArd Biesheuvel 		return status;
739c2d0b470SArd Biesheuvel 
740c2d0b470SArd Biesheuvel 	/* Historic? */
741c2d0b470SArd Biesheuvel 	boot_params->alt_mem_k	= 32 * 1024;
742c2d0b470SArd Biesheuvel 
743c2d0b470SArd Biesheuvel 	status = setup_e820(boot_params, e820ext, e820ext_size);
744c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
745c2d0b470SArd Biesheuvel 		return status;
746c2d0b470SArd Biesheuvel 
747c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
748c2d0b470SArd Biesheuvel }
749c2d0b470SArd Biesheuvel 
have_unsupported_snp_features(void)75031c77a50SArd Biesheuvel static bool have_unsupported_snp_features(void)
75131c77a50SArd Biesheuvel {
75231c77a50SArd Biesheuvel 	u64 unsupported;
75331c77a50SArd Biesheuvel 
75431c77a50SArd Biesheuvel 	unsupported = snp_get_unsupported_features(sev_get_status());
75531c77a50SArd Biesheuvel 	if (unsupported) {
75631c77a50SArd Biesheuvel 		efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
75731c77a50SArd Biesheuvel 			unsupported);
75831c77a50SArd Biesheuvel 		return true;
75931c77a50SArd Biesheuvel 	}
76031c77a50SArd Biesheuvel 	return false;
76131c77a50SArd Biesheuvel }
76231c77a50SArd Biesheuvel 
efi_get_seed(void * seed,int size)763a1b87d54SArd Biesheuvel static void efi_get_seed(void *seed, int size)
764a1b87d54SArd Biesheuvel {
765a1b87d54SArd Biesheuvel 	efi_get_random_bytes(size, seed);
766a1b87d54SArd Biesheuvel 
767a1b87d54SArd Biesheuvel 	/*
768a1b87d54SArd Biesheuvel 	 * This only updates seed[0] when running on 32-bit, but in that case,
769a1b87d54SArd Biesheuvel 	 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
770a1b87d54SArd Biesheuvel 	 */
771a1b87d54SArd Biesheuvel 	*(unsigned long *)seed ^= kaslr_get_random_long("EFI");
772a1b87d54SArd Biesheuvel }
773a1b87d54SArd Biesheuvel 
error(char * str)774a1b87d54SArd Biesheuvel static void error(char *str)
775a1b87d54SArd Biesheuvel {
776a1b87d54SArd Biesheuvel 	efi_warn("Decompression failed: %s\n", str);
777a1b87d54SArd Biesheuvel }
778a1b87d54SArd Biesheuvel 
7792a2f9b87SArd Biesheuvel static const char *cmdline_memmap_override;
7802a2f9b87SArd Biesheuvel 
parse_options(const char * cmdline)7812a2f9b87SArd Biesheuvel static efi_status_t parse_options(const char *cmdline)
7822a2f9b87SArd Biesheuvel {
7832a2f9b87SArd Biesheuvel 	static const char opts[][14] = {
7842a2f9b87SArd Biesheuvel 		"mem=", "memmap=", "efi_fake_mem=", "hugepages="
7852a2f9b87SArd Biesheuvel 	};
7862a2f9b87SArd Biesheuvel 
7872a2f9b87SArd Biesheuvel 	for (int i = 0; i < ARRAY_SIZE(opts); i++) {
7882a2f9b87SArd Biesheuvel 		const char *p = strstr(cmdline, opts[i]);
7892a2f9b87SArd Biesheuvel 
7902a2f9b87SArd Biesheuvel 		if (p == cmdline || (p > cmdline && isspace(p[-1]))) {
7912a2f9b87SArd Biesheuvel 			cmdline_memmap_override = opts[i];
7922a2f9b87SArd Biesheuvel 			break;
7932a2f9b87SArd Biesheuvel 		}
7942a2f9b87SArd Biesheuvel 	}
7952a2f9b87SArd Biesheuvel 
7962a2f9b87SArd Biesheuvel 	return efi_parse_options(cmdline);
7972a2f9b87SArd Biesheuvel }
7982a2f9b87SArd Biesheuvel 
efi_decompress_kernel(unsigned long * kernel_entry)799a1b87d54SArd Biesheuvel static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
800a1b87d54SArd Biesheuvel {
801a1b87d54SArd Biesheuvel 	unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
802a1b87d54SArd Biesheuvel 	unsigned long addr, alloc_size, entry;
803a1b87d54SArd Biesheuvel 	efi_status_t status;
804a1b87d54SArd Biesheuvel 	u32 seed[2] = {};
805a1b87d54SArd Biesheuvel 
806a1b87d54SArd Biesheuvel 	/* determine the required size of the allocation */
807a1b87d54SArd Biesheuvel 	alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
808a1b87d54SArd Biesheuvel 			   MIN_KERNEL_ALIGN);
809a1b87d54SArd Biesheuvel 
810a1b87d54SArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
811a1b87d54SArd Biesheuvel 		u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
812800f84d8SArd Biesheuvel 		static const efi_char16_t ami[] = L"American Megatrends";
813a1b87d54SArd Biesheuvel 
814a1b87d54SArd Biesheuvel 		efi_get_seed(seed, sizeof(seed));
815a1b87d54SArd Biesheuvel 
816a1b87d54SArd Biesheuvel 		virt_addr += (range * seed[1]) >> 32;
817a1b87d54SArd Biesheuvel 		virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
818800f84d8SArd Biesheuvel 
819800f84d8SArd Biesheuvel 		/*
820800f84d8SArd Biesheuvel 		 * Older Dell systems with AMI UEFI firmware v2.0 may hang
821800f84d8SArd Biesheuvel 		 * while decompressing the kernel if physical address
822800f84d8SArd Biesheuvel 		 * randomization is enabled.
823800f84d8SArd Biesheuvel 		 *
824800f84d8SArd Biesheuvel 		 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
825800f84d8SArd Biesheuvel 		 */
826800f84d8SArd Biesheuvel 		if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&
827800f84d8SArd Biesheuvel 		    !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
828800f84d8SArd Biesheuvel 			efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
829800f84d8SArd Biesheuvel 			seed[0] = 0;
8302a2f9b87SArd Biesheuvel 		} else if (cmdline_memmap_override) {
8312a2f9b87SArd Biesheuvel 			efi_info("%s detected on the kernel command line - disabling physical KASLR\n",
8322a2f9b87SArd Biesheuvel 				 cmdline_memmap_override);
8332a2f9b87SArd Biesheuvel 			seed[0] = 0;
834800f84d8SArd Biesheuvel 		}
835a1b87d54SArd Biesheuvel 	}
836a1b87d54SArd Biesheuvel 
837a1b87d54SArd Biesheuvel 	status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
838a1b87d54SArd Biesheuvel 				  seed[0], EFI_LOADER_CODE,
8390e7ca435SArd Biesheuvel 				  LOAD_PHYSICAL_ADDR,
840a1b87d54SArd Biesheuvel 				  EFI_X86_KERNEL_ALLOC_LIMIT);
841a1b87d54SArd Biesheuvel 	if (status != EFI_SUCCESS)
842a1b87d54SArd Biesheuvel 		return status;
843a1b87d54SArd Biesheuvel 
844a1b87d54SArd Biesheuvel 	entry = decompress_kernel((void *)addr, virt_addr, error);
845a1b87d54SArd Biesheuvel 	if (entry == ULONG_MAX) {
846a1b87d54SArd Biesheuvel 		efi_free(alloc_size, addr);
847a1b87d54SArd Biesheuvel 		return EFI_LOAD_ERROR;
848a1b87d54SArd Biesheuvel 	}
849a1b87d54SArd Biesheuvel 
850a1b87d54SArd Biesheuvel 	*kernel_entry = addr + entry;
851a1b87d54SArd Biesheuvel 
852ccde70aaSArd Biesheuvel 	return efi_adjust_memory_range_protection(addr, kernel_text_size);
853a1b87d54SArd Biesheuvel }
854a1b87d54SArd Biesheuvel 
enter_kernel(unsigned long kernel_addr,struct boot_params * boot_params)855d2d7a54fSArd Biesheuvel static void __noreturn enter_kernel(unsigned long kernel_addr,
856d2d7a54fSArd Biesheuvel 				    struct boot_params *boot_params)
857d2d7a54fSArd Biesheuvel {
858d2d7a54fSArd Biesheuvel 	/* enter decompressed kernel with boot_params pointer in RSI/ESI */
859d2d7a54fSArd Biesheuvel 	asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
860d2d7a54fSArd Biesheuvel 
861d2d7a54fSArd Biesheuvel 	unreachable();
862d2d7a54fSArd Biesheuvel }
863d2d7a54fSArd Biesheuvel 
864c2d0b470SArd Biesheuvel /*
865d2d7a54fSArd Biesheuvel  * On success, this routine will jump to the relocated image directly and never
866d2d7a54fSArd Biesheuvel  * return.  On failure, it will exit to the firmware via efi_exit() instead of
867d2d7a54fSArd Biesheuvel  * returning.
868c2d0b470SArd Biesheuvel  */
efi_stub_entry(efi_handle_t handle,efi_system_table_t * sys_table_arg,struct boot_params * boot_params)869df9215f1SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle,
870c2d0b470SArd Biesheuvel 			       efi_system_table_t *sys_table_arg,
871c2d0b470SArd Biesheuvel 			       struct boot_params *boot_params)
872c2d0b470SArd Biesheuvel {
87311078876SArd Biesheuvel 	efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
874c2d0b470SArd Biesheuvel 	struct setup_header *hdr = &boot_params->hdr;
875f4dc7fffSArd Biesheuvel 	const struct linux_efi_initrd *initrd = NULL;
876a1b87d54SArd Biesheuvel 	unsigned long kernel_entry;
877c2d0b470SArd Biesheuvel 	efi_status_t status;
878c2d0b470SArd Biesheuvel 
879db772413SArd Biesheuvel 	boot_params_pointer = boot_params;
880db772413SArd Biesheuvel 
881ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
882c2d0b470SArd Biesheuvel 	/* Check if we were booted by the EFI firmware */
883ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
8843b8f44fcSArd Biesheuvel 		efi_exit(handle, EFI_INVALID_PARAMETER);
885c2d0b470SArd Biesheuvel 
88631c77a50SArd Biesheuvel 	if (have_unsupported_snp_features())
88731c77a50SArd Biesheuvel 		efi_exit(handle, EFI_UNSUPPORTED);
88831c77a50SArd Biesheuvel 
88911078876SArd Biesheuvel 	if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
8903ba75c13SBaskov Evgeniy 		efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
8913ba75c13SBaskov Evgeniy 		if (efi_dxe_table &&
8923ba75c13SBaskov Evgeniy 		    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
8933ba75c13SBaskov Evgeniy 			efi_warn("Ignoring DXE services table: invalid signature\n");
8943ba75c13SBaskov Evgeniy 			efi_dxe_table = NULL;
8953ba75c13SBaskov Evgeniy 		}
89611078876SArd Biesheuvel 	}
89711078876SArd Biesheuvel 
89811078876SArd Biesheuvel 	/* grab the memory attributes protocol if it exists */
89911078876SArd Biesheuvel 	efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
9003ba75c13SBaskov Evgeniy 
901cb1c9e02SArd Biesheuvel 	status = efi_setup_5level_paging();
902cb1c9e02SArd Biesheuvel 	if (status != EFI_SUCCESS) {
903cb1c9e02SArd Biesheuvel 		efi_err("efi_setup_5level_paging() failed!\n");
904cb1c9e02SArd Biesheuvel 		goto fail;
905cb1c9e02SArd Biesheuvel 	}
906cb1c9e02SArd Biesheuvel 
9077dde67f2SArvind Sankar #ifdef CONFIG_CMDLINE_BOOL
9082a2f9b87SArd Biesheuvel 	status = parse_options(CONFIG_CMDLINE);
909055042beSArvind Sankar 	if (status != EFI_SUCCESS) {
910055042beSArvind Sankar 		efi_err("Failed to parse options\n");
911055042beSArvind Sankar 		goto fail;
912055042beSArvind Sankar 	}
9137dde67f2SArvind Sankar #endif
9147dde67f2SArvind Sankar 	if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
9157dde67f2SArvind Sankar 		unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
916c2d0b470SArd Biesheuvel 					       ((u64)boot_params->ext_cmd_line_ptr << 32));
9172a2f9b87SArd Biesheuvel 		status = parse_options((char *)cmdline_paddr);
918055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
919055042beSArvind Sankar 			efi_err("Failed to parse options\n");
920055042beSArvind Sankar 			goto fail;
921055042beSArvind Sankar 		}
9227dde67f2SArvind Sankar 	}
923c2d0b470SArd Biesheuvel 
92404654531SArd Biesheuvel 	if (efi_mem_encrypt > 0)
92504654531SArd Biesheuvel 		hdr->xloadflags |= XLF_MEM_ENCRYPTION;
92604654531SArd Biesheuvel 
927a1b87d54SArd Biesheuvel 	status = efi_decompress_kernel(&kernel_entry);
928a1b87d54SArd Biesheuvel 	if (status != EFI_SUCCESS) {
929a1b87d54SArd Biesheuvel 		efi_err("Failed to decompress kernel\n");
930a1b87d54SArd Biesheuvel 		goto fail;
931a1b87d54SArd Biesheuvel 	}
932a1b87d54SArd Biesheuvel 
933c2d0b470SArd Biesheuvel 	/*
934987053a3SArvind Sankar 	 * At this point, an initrd may already have been loaded by the
935987053a3SArvind Sankar 	 * bootloader and passed via bootparams. We permit an initrd loaded
936987053a3SArvind Sankar 	 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
937987053a3SArvind Sankar 	 *
938987053a3SArvind Sankar 	 * If the device path is not present, any command-line initrd=
939987053a3SArvind Sankar 	 * arguments will be processed only if image is not NULL, which will be
940987053a3SArvind Sankar 	 * the case only if we were loaded via the PE entry point.
941ec93fc37SArd Biesheuvel 	 */
942f4dc7fffSArd Biesheuvel 	status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
943f4dc7fffSArd Biesheuvel 				 &initrd);
94420287d56SArd Biesheuvel 	if (status != EFI_SUCCESS)
945ec93fc37SArd Biesheuvel 		goto fail;
946f4dc7fffSArd Biesheuvel 	if (initrd && initrd->size > 0) {
947f4dc7fffSArd Biesheuvel 		efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
948eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_image);
949f4dc7fffSArd Biesheuvel 		efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
950eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_size);
95179d3219dSArd Biesheuvel 	}
952ec93fc37SArd Biesheuvel 
953f4dc7fffSArd Biesheuvel 
954ec93fc37SArd Biesheuvel 	/*
955c2d0b470SArd Biesheuvel 	 * If the boot loader gave us a value for secure_boot then we use that,
956c2d0b470SArd Biesheuvel 	 * otherwise we ask the BIOS.
957c2d0b470SArd Biesheuvel 	 */
958c2d0b470SArd Biesheuvel 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
959c2d0b470SArd Biesheuvel 		boot_params->secure_boot = efi_get_secureboot();
960c2d0b470SArd Biesheuvel 
961c2d0b470SArd Biesheuvel 	/* Ask the firmware to clear memory on unclean shutdown */
962c2d0b470SArd Biesheuvel 	efi_enable_reset_attack_mitigation();
963c2d0b470SArd Biesheuvel 
964c2d0b470SArd Biesheuvel 	efi_random_get_seed();
965c2d0b470SArd Biesheuvel 
966c2d0b470SArd Biesheuvel 	efi_retrieve_tpm2_eventlog();
967c2d0b470SArd Biesheuvel 
968c2d0b470SArd Biesheuvel 	setup_graphics(boot_params);
969c2d0b470SArd Biesheuvel 
970c2d0b470SArd Biesheuvel 	setup_efi_pci(boot_params);
971c2d0b470SArd Biesheuvel 
972a1b87d54SArd Biesheuvel 	setup_quirks(boot_params);
973c2d0b470SArd Biesheuvel 
974c0461bd1SDionna Glaze 	setup_unaccepted_memory();
975c0461bd1SDionna Glaze 
976c2d0b470SArd Biesheuvel 	status = exit_boot(boot_params, handle);
977c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
97836bdd0a7SArvind Sankar 		efi_err("exit_boot() failed!\n");
979c2d0b470SArd Biesheuvel 		goto fail;
980c2d0b470SArd Biesheuvel 	}
981c2d0b470SArd Biesheuvel 
982a1b87d54SArd Biesheuvel 	/*
983a1b87d54SArd Biesheuvel 	 * Call the SEV init code while still running with the firmware's
984a1b87d54SArd Biesheuvel 	 * GDT/IDT, so #VC exceptions will be handled by EFI.
985a1b87d54SArd Biesheuvel 	 */
986a1b87d54SArd Biesheuvel 	sev_enable(boot_params);
987a1b87d54SArd Biesheuvel 
988cb1c9e02SArd Biesheuvel 	efi_5level_switch();
989cb1c9e02SArd Biesheuvel 
990a1b87d54SArd Biesheuvel 	enter_kernel(kernel_entry, boot_params);
991c2d0b470SArd Biesheuvel fail:
992df9215f1SArd Biesheuvel 	efi_err("efi_stub_entry() failed!\n");
993c2d0b470SArd Biesheuvel 
9943b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
995c2d0b470SArd Biesheuvel }
996df9215f1SArd Biesheuvel 
997df9215f1SArd Biesheuvel #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
efi_handover_entry(efi_handle_t handle,efi_system_table_t * sys_table_arg,struct boot_params * boot_params)998d7156b98SArd Biesheuvel void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
999d7156b98SArd Biesheuvel 			struct boot_params *boot_params)
1000d7156b98SArd Biesheuvel {
1001d7156b98SArd Biesheuvel 	memset(_bss, 0, _ebss - _bss);
1002d7156b98SArd Biesheuvel 	efi_stub_entry(handle, sys_table_arg, boot_params);
1003d7156b98SArd Biesheuvel }
1004d7156b98SArd Biesheuvel 
1005df9215f1SArd Biesheuvel #ifndef CONFIG_EFI_MIXED
1006d7156b98SArd Biesheuvel extern __alias(efi_handover_entry)
1007df9215f1SArd Biesheuvel void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
1008df9215f1SArd Biesheuvel 		      struct boot_params *boot_params);
1009df9215f1SArd Biesheuvel 
1010d7156b98SArd Biesheuvel extern __alias(efi_handover_entry)
1011df9215f1SArd Biesheuvel void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
1012df9215f1SArd Biesheuvel 		      struct boot_params *boot_params);
1013df9215f1SArd Biesheuvel #endif
1014df9215f1SArd Biesheuvel #endif
1015