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 
24ccc27ae7SArd Biesheuvel const efi_system_table_t *efi_system_table;
253ba75c13SBaskov Evgeniy const efi_dxe_services_table_t *efi_dxe_table;
26987053a3SArvind Sankar static efi_loaded_image_t *image = NULL;
2711078876SArd Biesheuvel static efi_memory_attribute_protocol_t *memattr;
28c2d0b470SArd Biesheuvel 
29c0461bd1SDionna Glaze typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
30c0461bd1SDionna Glaze union sev_memory_acceptance_protocol {
31c0461bd1SDionna Glaze 	struct {
32c0461bd1SDionna Glaze 		efi_status_t (__efiapi * allow_unaccepted_memory)(
33c0461bd1SDionna Glaze 			sev_memory_acceptance_protocol_t *);
34c0461bd1SDionna Glaze 	};
35c0461bd1SDionna Glaze 	struct {
36c0461bd1SDionna Glaze 		u32 allow_unaccepted_memory;
37c0461bd1SDionna Glaze 	} mixed_mode;
38c0461bd1SDionna Glaze };
39c0461bd1SDionna Glaze 
40c2d0b470SArd Biesheuvel static efi_status_t
41c2d0b470SArd Biesheuvel preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
42c2d0b470SArd Biesheuvel {
43c2d0b470SArd Biesheuvel 	struct pci_setup_rom *rom = NULL;
44c2d0b470SArd Biesheuvel 	efi_status_t status;
45c2d0b470SArd Biesheuvel 	unsigned long size;
46c2d0b470SArd Biesheuvel 	uint64_t romsize;
47c2d0b470SArd Biesheuvel 	void *romimage;
48c2d0b470SArd Biesheuvel 
49c2d0b470SArd Biesheuvel 	/*
50c2d0b470SArd Biesheuvel 	 * Some firmware images contain EFI function pointers at the place where
51c2d0b470SArd Biesheuvel 	 * the romimage and romsize fields are supposed to be. Typically the EFI
52c2d0b470SArd Biesheuvel 	 * code is mapped at high addresses, translating to an unrealistically
53c2d0b470SArd Biesheuvel 	 * large romsize. The UEFI spec limits the size of option ROMs to 16
54c2d0b470SArd Biesheuvel 	 * MiB so we reject any ROMs over 16 MiB in size to catch this.
55c2d0b470SArd Biesheuvel 	 */
56c2d0b470SArd Biesheuvel 	romimage = efi_table_attr(pci, romimage);
57c2d0b470SArd Biesheuvel 	romsize = efi_table_attr(pci, romsize);
58c2d0b470SArd Biesheuvel 	if (!romimage || !romsize || romsize > SZ_16M)
59c2d0b470SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
60c2d0b470SArd Biesheuvel 
61c2d0b470SArd Biesheuvel 	size = romsize + sizeof(*rom);
62c2d0b470SArd Biesheuvel 
63c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
64c2d0b470SArd Biesheuvel 			     (void **)&rom);
65c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
6636bdd0a7SArvind Sankar 		efi_err("Failed to allocate memory for 'rom'\n");
67c2d0b470SArd Biesheuvel 		return status;
68c2d0b470SArd Biesheuvel 	}
69c2d0b470SArd Biesheuvel 
70c2d0b470SArd Biesheuvel 	memset(rom, 0, sizeof(*rom));
71c2d0b470SArd Biesheuvel 
72c2d0b470SArd Biesheuvel 	rom->data.type	= SETUP_PCI;
73c2d0b470SArd Biesheuvel 	rom->data.len	= size - sizeof(struct setup_data);
74c2d0b470SArd Biesheuvel 	rom->data.next	= 0;
758b94da92SMikel Rychliski 	rom->pcilen	= romsize;
76c2d0b470SArd Biesheuvel 	*__rom = rom;
77c2d0b470SArd Biesheuvel 
78c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
79c2d0b470SArd Biesheuvel 				PCI_VENDOR_ID, 1, &rom->vendor);
80c2d0b470SArd Biesheuvel 
81c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
8236bdd0a7SArvind Sankar 		efi_err("Failed to read rom->vendor\n");
83c2d0b470SArd Biesheuvel 		goto free_struct;
84c2d0b470SArd Biesheuvel 	}
85c2d0b470SArd Biesheuvel 
86c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
87c2d0b470SArd Biesheuvel 				PCI_DEVICE_ID, 1, &rom->devid);
88c2d0b470SArd Biesheuvel 
89c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
9036bdd0a7SArvind Sankar 		efi_err("Failed to read rom->devid\n");
91c2d0b470SArd Biesheuvel 		goto free_struct;
92c2d0b470SArd Biesheuvel 	}
93c2d0b470SArd Biesheuvel 
94c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
95c2d0b470SArd Biesheuvel 				&rom->device, &rom->function);
96c2d0b470SArd Biesheuvel 
97c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
98c2d0b470SArd Biesheuvel 		goto free_struct;
99c2d0b470SArd Biesheuvel 
100c2d0b470SArd Biesheuvel 	memcpy(rom->romdata, romimage, romsize);
101c2d0b470SArd Biesheuvel 	return status;
102c2d0b470SArd Biesheuvel 
103c2d0b470SArd Biesheuvel free_struct:
104c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, rom);
105c2d0b470SArd Biesheuvel 	return status;
106c2d0b470SArd Biesheuvel }
107c2d0b470SArd Biesheuvel 
108c2d0b470SArd Biesheuvel /*
109c2d0b470SArd Biesheuvel  * There's no way to return an informative status from this function,
110c2d0b470SArd Biesheuvel  * because any analysis (and printing of error messages) needs to be
111c2d0b470SArd Biesheuvel  * done directly at the EFI function call-site.
112c2d0b470SArd Biesheuvel  *
113c2d0b470SArd Biesheuvel  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
114c2d0b470SArd Biesheuvel  * just didn't find any PCI devices, but there's no way to tell outside
115c2d0b470SArd Biesheuvel  * the context of the call.
116c2d0b470SArd Biesheuvel  */
117c2d0b470SArd Biesheuvel static void setup_efi_pci(struct boot_params *params)
118c2d0b470SArd Biesheuvel {
119c2d0b470SArd Biesheuvel 	efi_status_t status;
120c2d0b470SArd Biesheuvel 	void **pci_handle = NULL;
121c2d0b470SArd Biesheuvel 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
122c2d0b470SArd Biesheuvel 	unsigned long size = 0;
123c2d0b470SArd Biesheuvel 	struct setup_data *data;
124c2d0b470SArd Biesheuvel 	efi_handle_t h;
125c2d0b470SArd Biesheuvel 	int i;
126c2d0b470SArd Biesheuvel 
127c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
128c2d0b470SArd Biesheuvel 			     &pci_proto, NULL, &size, pci_handle);
129c2d0b470SArd Biesheuvel 
130c2d0b470SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL) {
131c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
132c2d0b470SArd Biesheuvel 				     (void **)&pci_handle);
133c2d0b470SArd Biesheuvel 
134c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
13536bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'pci_handle'\n");
136c2d0b470SArd Biesheuvel 			return;
137c2d0b470SArd Biesheuvel 		}
138c2d0b470SArd Biesheuvel 
139c2d0b470SArd Biesheuvel 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
140c2d0b470SArd Biesheuvel 				     &pci_proto, NULL, &size, pci_handle);
141c2d0b470SArd Biesheuvel 	}
142c2d0b470SArd Biesheuvel 
143c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
144c2d0b470SArd Biesheuvel 		goto free_handle;
145c2d0b470SArd Biesheuvel 
146c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
147c2d0b470SArd Biesheuvel 
148c2d0b470SArd Biesheuvel 	while (data && data->next)
149c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
150c2d0b470SArd Biesheuvel 
151c2d0b470SArd Biesheuvel 	for_each_efi_handle(h, pci_handle, size, i) {
152c2d0b470SArd Biesheuvel 		efi_pci_io_protocol_t *pci = NULL;
153c2d0b470SArd Biesheuvel 		struct pci_setup_rom *rom;
154c2d0b470SArd Biesheuvel 
155c2d0b470SArd Biesheuvel 		status = efi_bs_call(handle_protocol, h, &pci_proto,
156c2d0b470SArd Biesheuvel 				     (void **)&pci);
157c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS || !pci)
158c2d0b470SArd Biesheuvel 			continue;
159c2d0b470SArd Biesheuvel 
160c2d0b470SArd Biesheuvel 		status = preserve_pci_rom_image(pci, &rom);
161c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
162c2d0b470SArd Biesheuvel 			continue;
163c2d0b470SArd Biesheuvel 
164c2d0b470SArd Biesheuvel 		if (data)
165c2d0b470SArd Biesheuvel 			data->next = (unsigned long)rom;
166c2d0b470SArd Biesheuvel 		else
167c2d0b470SArd Biesheuvel 			params->hdr.setup_data = (unsigned long)rom;
168c2d0b470SArd Biesheuvel 
169c2d0b470SArd Biesheuvel 		data = (struct setup_data *)rom;
170c2d0b470SArd Biesheuvel 	}
171c2d0b470SArd Biesheuvel 
172c2d0b470SArd Biesheuvel free_handle:
173c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, pci_handle);
174c2d0b470SArd Biesheuvel }
175c2d0b470SArd Biesheuvel 
176c2d0b470SArd Biesheuvel static void retrieve_apple_device_properties(struct boot_params *boot_params)
177c2d0b470SArd Biesheuvel {
178c2d0b470SArd Biesheuvel 	efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
179c2d0b470SArd Biesheuvel 	struct setup_data *data, *new;
180c2d0b470SArd Biesheuvel 	efi_status_t status;
181c2d0b470SArd Biesheuvel 	u32 size = 0;
182c2d0b470SArd Biesheuvel 	apple_properties_protocol_t *p;
183c2d0b470SArd Biesheuvel 
184c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
185c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
186c2d0b470SArd Biesheuvel 		return;
187c2d0b470SArd Biesheuvel 
188c2d0b470SArd Biesheuvel 	if (efi_table_attr(p, version) != 0x10000) {
18936bdd0a7SArvind Sankar 		efi_err("Unsupported properties proto version\n");
190c2d0b470SArd Biesheuvel 		return;
191c2d0b470SArd Biesheuvel 	}
192c2d0b470SArd Biesheuvel 
193c2d0b470SArd Biesheuvel 	efi_call_proto(p, get_all, NULL, &size);
194c2d0b470SArd Biesheuvel 	if (!size)
195c2d0b470SArd Biesheuvel 		return;
196c2d0b470SArd Biesheuvel 
197c2d0b470SArd Biesheuvel 	do {
198c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
199c2d0b470SArd Biesheuvel 				     size + sizeof(struct setup_data),
200c2d0b470SArd Biesheuvel 				     (void **)&new);
201c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
20236bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'properties'\n");
203c2d0b470SArd Biesheuvel 			return;
204c2d0b470SArd Biesheuvel 		}
205c2d0b470SArd Biesheuvel 
206c2d0b470SArd Biesheuvel 		status = efi_call_proto(p, get_all, new->data, &size);
207c2d0b470SArd Biesheuvel 
208c2d0b470SArd Biesheuvel 		if (status == EFI_BUFFER_TOO_SMALL)
209c2d0b470SArd Biesheuvel 			efi_bs_call(free_pool, new);
210c2d0b470SArd Biesheuvel 	} while (status == EFI_BUFFER_TOO_SMALL);
211c2d0b470SArd Biesheuvel 
212c2d0b470SArd Biesheuvel 	new->type = SETUP_APPLE_PROPERTIES;
213c2d0b470SArd Biesheuvel 	new->len  = size;
214c2d0b470SArd Biesheuvel 	new->next = 0;
215c2d0b470SArd Biesheuvel 
216c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
217c2d0b470SArd Biesheuvel 	if (!data) {
218c2d0b470SArd Biesheuvel 		boot_params->hdr.setup_data = (unsigned long)new;
219c2d0b470SArd Biesheuvel 	} else {
220c2d0b470SArd Biesheuvel 		while (data->next)
221c2d0b470SArd Biesheuvel 			data = (struct setup_data *)(unsigned long)data->next;
222c2d0b470SArd Biesheuvel 		data->next = (unsigned long)new;
223c2d0b470SArd Biesheuvel 	}
224c2d0b470SArd Biesheuvel }
225c2d0b470SArd Biesheuvel 
226*c756fd5dSArd Biesheuvel efi_status_t efi_adjust_memory_range_protection(unsigned long start,
227cb1c9e02SArd Biesheuvel 						unsigned long size)
22882e0d6d7SBaskov Evgeniy {
22982e0d6d7SBaskov Evgeniy 	efi_status_t status;
23082e0d6d7SBaskov Evgeniy 	efi_gcd_memory_space_desc_t desc;
23182e0d6d7SBaskov Evgeniy 	unsigned long end, next;
23282e0d6d7SBaskov Evgeniy 	unsigned long rounded_start, rounded_end;
23382e0d6d7SBaskov Evgeniy 	unsigned long unprotect_start, unprotect_size;
23482e0d6d7SBaskov Evgeniy 
23582e0d6d7SBaskov Evgeniy 	rounded_start = rounddown(start, EFI_PAGE_SIZE);
23682e0d6d7SBaskov Evgeniy 	rounded_end = roundup(start + size, EFI_PAGE_SIZE);
23782e0d6d7SBaskov Evgeniy 
23811078876SArd Biesheuvel 	if (memattr != NULL) {
239*c756fd5dSArd Biesheuvel 		status = efi_call_proto(memattr, clear_memory_attributes,
240*c756fd5dSArd Biesheuvel 					rounded_start,
241*c756fd5dSArd Biesheuvel 					rounded_end - rounded_start,
242*c756fd5dSArd Biesheuvel 					EFI_MEMORY_XP);
243*c756fd5dSArd Biesheuvel 		if (status != EFI_SUCCESS)
244*c756fd5dSArd Biesheuvel 			efi_warn("Failed to clear EFI_MEMORY_XP attribute\n");
245*c756fd5dSArd Biesheuvel 		return status;
24611078876SArd Biesheuvel 	}
24711078876SArd Biesheuvel 
24811078876SArd Biesheuvel 	if (efi_dxe_table == NULL)
249*c756fd5dSArd Biesheuvel 		return EFI_SUCCESS;
25011078876SArd Biesheuvel 
25182e0d6d7SBaskov Evgeniy 	/*
25282e0d6d7SBaskov Evgeniy 	 * Don't modify memory region attributes, they are
25382e0d6d7SBaskov Evgeniy 	 * already suitable, to lower the possibility to
25482e0d6d7SBaskov Evgeniy 	 * encounter firmware bugs.
25582e0d6d7SBaskov Evgeniy 	 */
25682e0d6d7SBaskov Evgeniy 
25782e0d6d7SBaskov Evgeniy 	for (end = start + size; start < end; start = next) {
25882e0d6d7SBaskov Evgeniy 
25982e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
26082e0d6d7SBaskov Evgeniy 
26182e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS)
262*c756fd5dSArd Biesheuvel 			break;
26382e0d6d7SBaskov Evgeniy 
26482e0d6d7SBaskov Evgeniy 		next = desc.base_address + desc.length;
26582e0d6d7SBaskov Evgeniy 
26682e0d6d7SBaskov Evgeniy 		/*
26782e0d6d7SBaskov Evgeniy 		 * Only system memory is suitable for trampoline/kernel image placement,
26882e0d6d7SBaskov Evgeniy 		 * so only this type of memory needs its attributes to be modified.
26982e0d6d7SBaskov Evgeniy 		 */
27082e0d6d7SBaskov Evgeniy 
27182e0d6d7SBaskov Evgeniy 		if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
27282e0d6d7SBaskov Evgeniy 		    (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
27382e0d6d7SBaskov Evgeniy 			continue;
27482e0d6d7SBaskov Evgeniy 
27582e0d6d7SBaskov Evgeniy 		unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
27682e0d6d7SBaskov Evgeniy 		unprotect_size = min(rounded_end, next) - unprotect_start;
27782e0d6d7SBaskov Evgeniy 
27882e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(set_memory_space_attributes,
27982e0d6d7SBaskov Evgeniy 				      unprotect_start, unprotect_size,
28082e0d6d7SBaskov Evgeniy 				      EFI_MEMORY_WB);
28182e0d6d7SBaskov Evgeniy 
28282e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS) {
28331f1a0edSArd Biesheuvel 			efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
28482e0d6d7SBaskov Evgeniy 				 unprotect_start,
28582e0d6d7SBaskov Evgeniy 				 unprotect_start + unprotect_size,
28631f1a0edSArd Biesheuvel 				 status);
287*c756fd5dSArd Biesheuvel 			break;
28882e0d6d7SBaskov Evgeniy 		}
28982e0d6d7SBaskov Evgeniy 	}
290*c756fd5dSArd Biesheuvel 	return EFI_SUCCESS;
29182e0d6d7SBaskov Evgeniy }
29282e0d6d7SBaskov Evgeniy 
293c0461bd1SDionna Glaze static void setup_unaccepted_memory(void)
294c0461bd1SDionna Glaze {
295c0461bd1SDionna Glaze 	efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
296c0461bd1SDionna Glaze 	sev_memory_acceptance_protocol_t *proto;
297c0461bd1SDionna Glaze 	efi_status_t status;
298c0461bd1SDionna Glaze 
299c0461bd1SDionna Glaze 	if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
300c0461bd1SDionna Glaze 		return;
301c0461bd1SDionna Glaze 
302c0461bd1SDionna Glaze 	/*
303c0461bd1SDionna Glaze 	 * Enable unaccepted memory before calling exit boot services in order
304c0461bd1SDionna Glaze 	 * for the UEFI to not accept all memory on EBS.
305c0461bd1SDionna Glaze 	 */
306c0461bd1SDionna Glaze 	status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
307c0461bd1SDionna Glaze 			     (void **)&proto);
308c0461bd1SDionna Glaze 	if (status != EFI_SUCCESS)
309c0461bd1SDionna Glaze 		return;
310c0461bd1SDionna Glaze 
311c0461bd1SDionna Glaze 	status = efi_call_proto(proto, allow_unaccepted_memory);
312c0461bd1SDionna Glaze 	if (status != EFI_SUCCESS)
313c0461bd1SDionna Glaze 		efi_err("Memory acceptance protocol failed\n");
314c0461bd1SDionna Glaze }
315c0461bd1SDionna Glaze 
316800f84d8SArd Biesheuvel static efi_char16_t *efistub_fw_vendor(void)
317800f84d8SArd Biesheuvel {
318800f84d8SArd Biesheuvel 	unsigned long vendor = efi_table_attr(efi_system_table, fw_vendor);
319800f84d8SArd Biesheuvel 
320800f84d8SArd Biesheuvel 	return (efi_char16_t *)vendor;
321800f84d8SArd Biesheuvel }
322800f84d8SArd Biesheuvel 
323c2d0b470SArd Biesheuvel static const efi_char16_t apple[] = L"Apple";
324c2d0b470SArd Biesheuvel 
325a1b87d54SArd Biesheuvel static void setup_quirks(struct boot_params *boot_params)
326c2d0b470SArd Biesheuvel {
327800f84d8SArd Biesheuvel 	if (IS_ENABLED(CONFIG_APPLE_PROPERTIES) &&
328800f84d8SArd Biesheuvel 	    !memcmp(efistub_fw_vendor(), apple, sizeof(apple)))
329c2d0b470SArd Biesheuvel 		retrieve_apple_device_properties(boot_params);
330c2d0b470SArd Biesheuvel }
331c2d0b470SArd Biesheuvel 
332c2d0b470SArd Biesheuvel /*
333c2d0b470SArd Biesheuvel  * See if we have Universal Graphics Adapter (UGA) protocol
334c2d0b470SArd Biesheuvel  */
335c2d0b470SArd Biesheuvel static efi_status_t
336c2d0b470SArd Biesheuvel setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
337c2d0b470SArd Biesheuvel {
338c2d0b470SArd Biesheuvel 	efi_status_t status;
339c2d0b470SArd Biesheuvel 	u32 width, height;
340c2d0b470SArd Biesheuvel 	void **uga_handle = NULL;
341c2d0b470SArd Biesheuvel 	efi_uga_draw_protocol_t *uga = NULL, *first_uga;
342c2d0b470SArd Biesheuvel 	efi_handle_t handle;
343c2d0b470SArd Biesheuvel 	int i;
344c2d0b470SArd Biesheuvel 
345c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
346c2d0b470SArd Biesheuvel 			     (void **)&uga_handle);
347c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
348c2d0b470SArd Biesheuvel 		return status;
349c2d0b470SArd Biesheuvel 
350c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
351c2d0b470SArd Biesheuvel 			     uga_proto, NULL, &size, uga_handle);
352c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
353c2d0b470SArd Biesheuvel 		goto free_handle;
354c2d0b470SArd Biesheuvel 
355c2d0b470SArd Biesheuvel 	height = 0;
356c2d0b470SArd Biesheuvel 	width = 0;
357c2d0b470SArd Biesheuvel 
358c2d0b470SArd Biesheuvel 	first_uga = NULL;
359c2d0b470SArd Biesheuvel 	for_each_efi_handle(handle, uga_handle, size, i) {
360c2d0b470SArd Biesheuvel 		efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
361c2d0b470SArd Biesheuvel 		u32 w, h, depth, refresh;
362c2d0b470SArd Biesheuvel 		void *pciio;
363c2d0b470SArd Biesheuvel 
364c2d0b470SArd Biesheuvel 		status = efi_bs_call(handle_protocol, handle, uga_proto,
365c2d0b470SArd Biesheuvel 				     (void **)&uga);
366c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
367c2d0b470SArd Biesheuvel 			continue;
368c2d0b470SArd Biesheuvel 
369c2d0b470SArd Biesheuvel 		pciio = NULL;
370c2d0b470SArd Biesheuvel 		efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
371c2d0b470SArd Biesheuvel 
372c2d0b470SArd Biesheuvel 		status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
373c2d0b470SArd Biesheuvel 		if (status == EFI_SUCCESS && (!first_uga || pciio)) {
374c2d0b470SArd Biesheuvel 			width = w;
375c2d0b470SArd Biesheuvel 			height = h;
376c2d0b470SArd Biesheuvel 
377c2d0b470SArd Biesheuvel 			/*
378c2d0b470SArd Biesheuvel 			 * Once we've found a UGA supporting PCIIO,
379c2d0b470SArd Biesheuvel 			 * don't bother looking any further.
380c2d0b470SArd Biesheuvel 			 */
381c2d0b470SArd Biesheuvel 			if (pciio)
382c2d0b470SArd Biesheuvel 				break;
383c2d0b470SArd Biesheuvel 
384c2d0b470SArd Biesheuvel 			first_uga = uga;
385c2d0b470SArd Biesheuvel 		}
386c2d0b470SArd Biesheuvel 	}
387c2d0b470SArd Biesheuvel 
388c2d0b470SArd Biesheuvel 	if (!width && !height)
389c2d0b470SArd Biesheuvel 		goto free_handle;
390c2d0b470SArd Biesheuvel 
391c2d0b470SArd Biesheuvel 	/* EFI framebuffer */
392c2d0b470SArd Biesheuvel 	si->orig_video_isVGA	= VIDEO_TYPE_EFI;
393c2d0b470SArd Biesheuvel 
394c2d0b470SArd Biesheuvel 	si->lfb_depth		= 32;
395c2d0b470SArd Biesheuvel 	si->lfb_width		= width;
396c2d0b470SArd Biesheuvel 	si->lfb_height		= height;
397c2d0b470SArd Biesheuvel 
398c2d0b470SArd Biesheuvel 	si->red_size		= 8;
399c2d0b470SArd Biesheuvel 	si->red_pos		= 16;
400c2d0b470SArd Biesheuvel 	si->green_size		= 8;
401c2d0b470SArd Biesheuvel 	si->green_pos		= 8;
402c2d0b470SArd Biesheuvel 	si->blue_size		= 8;
403c2d0b470SArd Biesheuvel 	si->blue_pos		= 0;
404c2d0b470SArd Biesheuvel 	si->rsvd_size		= 8;
405c2d0b470SArd Biesheuvel 	si->rsvd_pos		= 24;
406c2d0b470SArd Biesheuvel 
407c2d0b470SArd Biesheuvel free_handle:
408c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, uga_handle);
409c2d0b470SArd Biesheuvel 
410c2d0b470SArd Biesheuvel 	return status;
411c2d0b470SArd Biesheuvel }
412c2d0b470SArd Biesheuvel 
413c2d0b470SArd Biesheuvel static void setup_graphics(struct boot_params *boot_params)
414c2d0b470SArd Biesheuvel {
415c2d0b470SArd Biesheuvel 	efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
416c2d0b470SArd Biesheuvel 	struct screen_info *si;
417c2d0b470SArd Biesheuvel 	efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
418c2d0b470SArd Biesheuvel 	efi_status_t status;
419c2d0b470SArd Biesheuvel 	unsigned long size;
420c2d0b470SArd Biesheuvel 	void **gop_handle = NULL;
421c2d0b470SArd Biesheuvel 	void **uga_handle = NULL;
422c2d0b470SArd Biesheuvel 
423c2d0b470SArd Biesheuvel 	si = &boot_params->screen_info;
424c2d0b470SArd Biesheuvel 	memset(si, 0, sizeof(*si));
425c2d0b470SArd Biesheuvel 
426c2d0b470SArd Biesheuvel 	size = 0;
427c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
428c2d0b470SArd Biesheuvel 			     &graphics_proto, NULL, &size, gop_handle);
429c2d0b470SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL)
430c2d0b470SArd Biesheuvel 		status = efi_setup_gop(si, &graphics_proto, size);
431c2d0b470SArd Biesheuvel 
432c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
433c2d0b470SArd Biesheuvel 		size = 0;
434c2d0b470SArd Biesheuvel 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
435c2d0b470SArd Biesheuvel 				     &uga_proto, NULL, &size, uga_handle);
436c2d0b470SArd Biesheuvel 		if (status == EFI_BUFFER_TOO_SMALL)
437c2d0b470SArd Biesheuvel 			setup_uga(si, &uga_proto, size);
438c2d0b470SArd Biesheuvel 	}
439c2d0b470SArd Biesheuvel }
440c2d0b470SArd Biesheuvel 
4413b8f44fcSArd Biesheuvel 
4423b8f44fcSArd Biesheuvel static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
4433b8f44fcSArd Biesheuvel {
4443b8f44fcSArd Biesheuvel 	efi_bs_call(exit, handle, status, 0, NULL);
445f3fa0efcSArd Biesheuvel 	for(;;)
446f3fa0efcSArd Biesheuvel 		asm("hlt");
4473b8f44fcSArd Biesheuvel }
4483b8f44fcSArd Biesheuvel 
449c2d0b470SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle,
450c2d0b470SArd Biesheuvel 			       efi_system_table_t *sys_table_arg,
451c2d0b470SArd Biesheuvel 			       struct boot_params *boot_params);
452c2d0b470SArd Biesheuvel 
453c2d0b470SArd Biesheuvel /*
454c2d0b470SArd Biesheuvel  * Because the x86 boot code expects to be passed a boot_params we
455c2d0b470SArd Biesheuvel  * need to create one ourselves (usually the bootloader would create
456c2d0b470SArd Biesheuvel  * one for us).
457c2d0b470SArd Biesheuvel  */
458c2d0b470SArd Biesheuvel efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
459c2d0b470SArd Biesheuvel 				   efi_system_table_t *sys_table_arg)
460c2d0b470SArd Biesheuvel {
461c2d0b470SArd Biesheuvel 	struct boot_params *boot_params;
462c2d0b470SArd Biesheuvel 	struct setup_header *hdr;
4631887c9b6SArvind Sankar 	void *image_base;
464c2d0b470SArd Biesheuvel 	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
465c2d0b470SArd Biesheuvel 	int options_size = 0;
466c2d0b470SArd Biesheuvel 	efi_status_t status;
467c2d0b470SArd Biesheuvel 	char *cmdline_ptr;
468c2d0b470SArd Biesheuvel 
469ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
470c2d0b470SArd Biesheuvel 
471c2d0b470SArd Biesheuvel 	/* Check if we were booted by the EFI firmware */
472ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
4733b8f44fcSArd Biesheuvel 		efi_exit(handle, EFI_INVALID_PARAMETER);
474c2d0b470SArd Biesheuvel 
4750347d8c2SArvind Sankar 	status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
476c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
47736bdd0a7SArvind Sankar 		efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
4783b8f44fcSArd Biesheuvel 		efi_exit(handle, status);
479c2d0b470SArd Biesheuvel 	}
480c2d0b470SArd Biesheuvel 
4811887c9b6SArvind Sankar 	image_base = efi_table_attr(image, image_base);
4821887c9b6SArvind Sankar 
483019512f1SArvind Sankar 	status = efi_allocate_pages(sizeof(struct boot_params),
484019512f1SArvind Sankar 				    (unsigned long *)&boot_params, ULONG_MAX);
485c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
48636bdd0a7SArvind Sankar 		efi_err("Failed to allocate lowmem for boot params\n");
4873b8f44fcSArd Biesheuvel 		efi_exit(handle, status);
488c2d0b470SArd Biesheuvel 	}
489c2d0b470SArd Biesheuvel 
490019512f1SArvind Sankar 	memset(boot_params, 0x0, sizeof(struct boot_params));
491c2d0b470SArd Biesheuvel 
492c2d0b470SArd Biesheuvel 	hdr = &boot_params->hdr;
493c2d0b470SArd Biesheuvel 
49459476f80SArvind Sankar 	/* Copy the setup header from the second sector to boot_params */
49559476f80SArvind Sankar 	memcpy(&hdr->jump, image_base + 512,
49659476f80SArvind Sankar 	       sizeof(struct setup_header) - offsetof(struct setup_header, jump));
497c2d0b470SArd Biesheuvel 
498c2d0b470SArd Biesheuvel 	/*
499c2d0b470SArd Biesheuvel 	 * Fill out some of the header fields ourselves because the
500c2d0b470SArd Biesheuvel 	 * EFI firmware loader doesn't load the first sector.
501c2d0b470SArd Biesheuvel 	 */
502c2d0b470SArd Biesheuvel 	hdr->root_flags	= 1;
503c2d0b470SArd Biesheuvel 	hdr->vid_mode	= 0xffff;
504c2d0b470SArd Biesheuvel 	hdr->boot_flag	= 0xAA55;
505c2d0b470SArd Biesheuvel 
506c2d0b470SArd Biesheuvel 	hdr->type_of_loader = 0x21;
507c2d0b470SArd Biesheuvel 
508c2d0b470SArd Biesheuvel 	/* Convert unicode cmdline to ascii */
50927cd5511SArd Biesheuvel 	cmdline_ptr = efi_convert_cmdline(image, &options_size);
510c2d0b470SArd Biesheuvel 	if (!cmdline_ptr)
511c2d0b470SArd Biesheuvel 		goto fail;
512c2d0b470SArd Biesheuvel 
513eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)cmdline_ptr,
514eed4e019SArvind Sankar 			  &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
515c2d0b470SArd Biesheuvel 
516c2d0b470SArd Biesheuvel 	hdr->ramdisk_image = 0;
517c2d0b470SArd Biesheuvel 	hdr->ramdisk_size = 0;
518c2d0b470SArd Biesheuvel 
51963bf28ceSArd Biesheuvel 	/*
52063bf28ceSArd Biesheuvel 	 * Disregard any setup data that was provided by the bootloader:
52163bf28ceSArd Biesheuvel 	 * setup_data could be pointing anywhere, and we have no way of
52263bf28ceSArd Biesheuvel 	 * authenticating or validating the payload.
52363bf28ceSArd Biesheuvel 	 */
52463bf28ceSArd Biesheuvel 	hdr->setup_data = 0;
52563bf28ceSArd Biesheuvel 
526ccc27ae7SArd Biesheuvel 	efi_stub_entry(handle, sys_table_arg, boot_params);
527c2d0b470SArd Biesheuvel 	/* not reached */
528c2d0b470SArd Biesheuvel 
529c2d0b470SArd Biesheuvel fail:
530019512f1SArvind Sankar 	efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
531c2d0b470SArd Biesheuvel 
5323b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
533c2d0b470SArd Biesheuvel }
534c2d0b470SArd Biesheuvel 
535c2d0b470SArd Biesheuvel static void add_e820ext(struct boot_params *params,
536c2d0b470SArd Biesheuvel 			struct setup_data *e820ext, u32 nr_entries)
537c2d0b470SArd Biesheuvel {
538c2d0b470SArd Biesheuvel 	struct setup_data *data;
539c2d0b470SArd Biesheuvel 
540c2d0b470SArd Biesheuvel 	e820ext->type = SETUP_E820_EXT;
541c2d0b470SArd Biesheuvel 	e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
542c2d0b470SArd Biesheuvel 	e820ext->next = 0;
543c2d0b470SArd Biesheuvel 
544c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
545c2d0b470SArd Biesheuvel 
546c2d0b470SArd Biesheuvel 	while (data && data->next)
547c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
548c2d0b470SArd Biesheuvel 
549c2d0b470SArd Biesheuvel 	if (data)
550c2d0b470SArd Biesheuvel 		data->next = (unsigned long)e820ext;
551c2d0b470SArd Biesheuvel 	else
552c2d0b470SArd Biesheuvel 		params->hdr.setup_data = (unsigned long)e820ext;
553c2d0b470SArd Biesheuvel }
554c2d0b470SArd Biesheuvel 
555c2d0b470SArd Biesheuvel static efi_status_t
556c2d0b470SArd Biesheuvel setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
557c2d0b470SArd Biesheuvel {
558c2d0b470SArd Biesheuvel 	struct boot_e820_entry *entry = params->e820_table;
559c2d0b470SArd Biesheuvel 	struct efi_info *efi = &params->efi_info;
560c2d0b470SArd Biesheuvel 	struct boot_e820_entry *prev = NULL;
561c2d0b470SArd Biesheuvel 	u32 nr_entries;
562c2d0b470SArd Biesheuvel 	u32 nr_desc;
563c2d0b470SArd Biesheuvel 	int i;
564c2d0b470SArd Biesheuvel 
565c2d0b470SArd Biesheuvel 	nr_entries = 0;
566c2d0b470SArd Biesheuvel 	nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
567c2d0b470SArd Biesheuvel 
568c2d0b470SArd Biesheuvel 	for (i = 0; i < nr_desc; i++) {
569c2d0b470SArd Biesheuvel 		efi_memory_desc_t *d;
570c2d0b470SArd Biesheuvel 		unsigned int e820_type = 0;
571c2d0b470SArd Biesheuvel 		unsigned long m = efi->efi_memmap;
572c2d0b470SArd Biesheuvel 
573c2d0b470SArd Biesheuvel #ifdef CONFIG_X86_64
574c2d0b470SArd Biesheuvel 		m |= (u64)efi->efi_memmap_hi << 32;
575c2d0b470SArd Biesheuvel #endif
576c2d0b470SArd Biesheuvel 
577c2d0b470SArd Biesheuvel 		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
578c2d0b470SArd Biesheuvel 		switch (d->type) {
579c2d0b470SArd Biesheuvel 		case EFI_RESERVED_TYPE:
580c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_CODE:
581c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_DATA:
582c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO:
583c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
584c2d0b470SArd Biesheuvel 		case EFI_PAL_CODE:
585c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_RESERVED;
586c2d0b470SArd Biesheuvel 			break;
587c2d0b470SArd Biesheuvel 
588c2d0b470SArd Biesheuvel 		case EFI_UNUSABLE_MEMORY:
589c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_UNUSABLE;
590c2d0b470SArd Biesheuvel 			break;
591c2d0b470SArd Biesheuvel 
592c2d0b470SArd Biesheuvel 		case EFI_ACPI_RECLAIM_MEMORY:
593c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_ACPI;
594c2d0b470SArd Biesheuvel 			break;
595c2d0b470SArd Biesheuvel 
596c2d0b470SArd Biesheuvel 		case EFI_LOADER_CODE:
597c2d0b470SArd Biesheuvel 		case EFI_LOADER_DATA:
598c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_CODE:
599c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_DATA:
600c2d0b470SArd Biesheuvel 		case EFI_CONVENTIONAL_MEMORY:
601c2d0b470SArd Biesheuvel 			if (efi_soft_reserve_enabled() &&
602c2d0b470SArd Biesheuvel 			    (d->attribute & EFI_MEMORY_SP))
603c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_SOFT_RESERVED;
604c2d0b470SArd Biesheuvel 			else
605c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_RAM;
606c2d0b470SArd Biesheuvel 			break;
607c2d0b470SArd Biesheuvel 
608c2d0b470SArd Biesheuvel 		case EFI_ACPI_MEMORY_NVS:
609c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_NVS;
610c2d0b470SArd Biesheuvel 			break;
611c2d0b470SArd Biesheuvel 
612c2d0b470SArd Biesheuvel 		case EFI_PERSISTENT_MEMORY:
613c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_PMEM;
614c2d0b470SArd Biesheuvel 			break;
615c2d0b470SArd Biesheuvel 
616745e3ed8SKirill A. Shutemov 		case EFI_UNACCEPTED_MEMORY:
617ff07186bSNikolay Borisov 			if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
618745e3ed8SKirill A. Shutemov 				continue;
619745e3ed8SKirill A. Shutemov 			e820_type = E820_TYPE_RAM;
620745e3ed8SKirill A. Shutemov 			process_unaccepted_memory(d->phys_addr,
621745e3ed8SKirill A. Shutemov 						  d->phys_addr + PAGE_SIZE * d->num_pages);
622745e3ed8SKirill A. Shutemov 			break;
623c2d0b470SArd Biesheuvel 		default:
624c2d0b470SArd Biesheuvel 			continue;
625c2d0b470SArd Biesheuvel 		}
626c2d0b470SArd Biesheuvel 
627c2d0b470SArd Biesheuvel 		/* Merge adjacent mappings */
628c2d0b470SArd Biesheuvel 		if (prev && prev->type == e820_type &&
629c2d0b470SArd Biesheuvel 		    (prev->addr + prev->size) == d->phys_addr) {
630c2d0b470SArd Biesheuvel 			prev->size += d->num_pages << 12;
631c2d0b470SArd Biesheuvel 			continue;
632c2d0b470SArd Biesheuvel 		}
633c2d0b470SArd Biesheuvel 
634c2d0b470SArd Biesheuvel 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
635c2d0b470SArd Biesheuvel 			u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
636c2d0b470SArd Biesheuvel 				   sizeof(struct setup_data);
637c2d0b470SArd Biesheuvel 
638c2d0b470SArd Biesheuvel 			if (!e820ext || e820ext_size < need)
639c2d0b470SArd Biesheuvel 				return EFI_BUFFER_TOO_SMALL;
640c2d0b470SArd Biesheuvel 
641c2d0b470SArd Biesheuvel 			/* boot_params map full, switch to e820 extended */
642c2d0b470SArd Biesheuvel 			entry = (struct boot_e820_entry *)e820ext->data;
643c2d0b470SArd Biesheuvel 		}
644c2d0b470SArd Biesheuvel 
645c2d0b470SArd Biesheuvel 		entry->addr = d->phys_addr;
646c2d0b470SArd Biesheuvel 		entry->size = d->num_pages << PAGE_SHIFT;
647c2d0b470SArd Biesheuvel 		entry->type = e820_type;
648c2d0b470SArd Biesheuvel 		prev = entry++;
649c2d0b470SArd Biesheuvel 		nr_entries++;
650c2d0b470SArd Biesheuvel 	}
651c2d0b470SArd Biesheuvel 
652c2d0b470SArd Biesheuvel 	if (nr_entries > ARRAY_SIZE(params->e820_table)) {
653c2d0b470SArd Biesheuvel 		u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
654c2d0b470SArd Biesheuvel 
655c2d0b470SArd Biesheuvel 		add_e820ext(params, e820ext, nr_e820ext);
656c2d0b470SArd Biesheuvel 		nr_entries -= nr_e820ext;
657c2d0b470SArd Biesheuvel 	}
658c2d0b470SArd Biesheuvel 
659c2d0b470SArd Biesheuvel 	params->e820_entries = (u8)nr_entries;
660c2d0b470SArd Biesheuvel 
661c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
662c2d0b470SArd Biesheuvel }
663c2d0b470SArd Biesheuvel 
664c2d0b470SArd Biesheuvel static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
665c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
666c2d0b470SArd Biesheuvel {
667c2d0b470SArd Biesheuvel 	efi_status_t status;
668c2d0b470SArd Biesheuvel 	unsigned long size;
669c2d0b470SArd Biesheuvel 
670c2d0b470SArd Biesheuvel 	size = sizeof(struct setup_data) +
671c2d0b470SArd Biesheuvel 		sizeof(struct e820_entry) * nr_desc;
672c2d0b470SArd Biesheuvel 
673c2d0b470SArd Biesheuvel 	if (*e820ext) {
674c2d0b470SArd Biesheuvel 		efi_bs_call(free_pool, *e820ext);
675c2d0b470SArd Biesheuvel 		*e820ext = NULL;
676c2d0b470SArd Biesheuvel 		*e820ext_size = 0;
677c2d0b470SArd Biesheuvel 	}
678c2d0b470SArd Biesheuvel 
679c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
680c2d0b470SArd Biesheuvel 			     (void **)e820ext);
681c2d0b470SArd Biesheuvel 	if (status == EFI_SUCCESS)
682c2d0b470SArd Biesheuvel 		*e820ext_size = size;
683c2d0b470SArd Biesheuvel 
684c2d0b470SArd Biesheuvel 	return status;
685c2d0b470SArd Biesheuvel }
686c2d0b470SArd Biesheuvel 
687c2d0b470SArd Biesheuvel static efi_status_t allocate_e820(struct boot_params *params,
688c2d0b470SArd Biesheuvel 				  struct setup_data **e820ext,
689c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
690c2d0b470SArd Biesheuvel {
6912e9f46eeSKirill A. Shutemov 	struct efi_boot_memmap *map;
692c2d0b470SArd Biesheuvel 	efi_status_t status;
6932e9f46eeSKirill A. Shutemov 	__u32 nr_desc;
694c2d0b470SArd Biesheuvel 
6952e9f46eeSKirill A. Shutemov 	status = efi_get_memory_map(&map, false);
696c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
697c2d0b470SArd Biesheuvel 		return status;
6982e9f46eeSKirill A. Shutemov 
6992e9f46eeSKirill A. Shutemov 	nr_desc = map->map_size / map->desc_size;
7002e9f46eeSKirill A. Shutemov 	if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
7012e9f46eeSKirill A. Shutemov 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
7022e9f46eeSKirill A. Shutemov 				 EFI_MMAP_NR_SLACK_SLOTS;
7032e9f46eeSKirill A. Shutemov 
7042e9f46eeSKirill A. Shutemov 		status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
705c2d0b470SArd Biesheuvel 	}
706c2d0b470SArd Biesheuvel 
707745e3ed8SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
708745e3ed8SKirill A. Shutemov 		status = allocate_unaccepted_bitmap(nr_desc, map);
709745e3ed8SKirill A. Shutemov 
7102e9f46eeSKirill A. Shutemov 	efi_bs_call(free_pool, map);
7112e9f46eeSKirill A. Shutemov 	return status;
712c2d0b470SArd Biesheuvel }
713c2d0b470SArd Biesheuvel 
714c2d0b470SArd Biesheuvel struct exit_boot_struct {
715c2d0b470SArd Biesheuvel 	struct boot_params	*boot_params;
716c2d0b470SArd Biesheuvel 	struct efi_info		*efi;
717c2d0b470SArd Biesheuvel };
718c2d0b470SArd Biesheuvel 
719c2d0b470SArd Biesheuvel static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
720c2d0b470SArd Biesheuvel 				   void *priv)
721c2d0b470SArd Biesheuvel {
722c2d0b470SArd Biesheuvel 	const char *signature;
723c2d0b470SArd Biesheuvel 	struct exit_boot_struct *p = priv;
724c2d0b470SArd Biesheuvel 
725c2d0b470SArd Biesheuvel 	signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
726c2d0b470SArd Biesheuvel 				   : EFI32_LOADER_SIGNATURE;
727c2d0b470SArd Biesheuvel 	memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
728c2d0b470SArd Biesheuvel 
729eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)efi_system_table,
730eed4e019SArvind Sankar 			  &p->efi->efi_systab, &p->efi->efi_systab_hi);
731eab31265SArd Biesheuvel 	p->efi->efi_memdesc_size	= map->desc_size;
732eab31265SArd Biesheuvel 	p->efi->efi_memdesc_version	= map->desc_ver;
733eab31265SArd Biesheuvel 	efi_set_u64_split((unsigned long)map->map,
734eed4e019SArvind Sankar 			  &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
735eab31265SArd Biesheuvel 	p->efi->efi_memmap_size		= map->map_size;
736c2d0b470SArd Biesheuvel 
737c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
738c2d0b470SArd Biesheuvel }
739c2d0b470SArd Biesheuvel 
740c2d0b470SArd Biesheuvel static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
741c2d0b470SArd Biesheuvel {
742c2d0b470SArd Biesheuvel 	struct setup_data *e820ext = NULL;
743c2d0b470SArd Biesheuvel 	__u32 e820ext_size = 0;
744c2d0b470SArd Biesheuvel 	efi_status_t status;
745c2d0b470SArd Biesheuvel 	struct exit_boot_struct priv;
746c2d0b470SArd Biesheuvel 
747c2d0b470SArd Biesheuvel 	priv.boot_params	= boot_params;
748c2d0b470SArd Biesheuvel 	priv.efi		= &boot_params->efi_info;
749c2d0b470SArd Biesheuvel 
750c2d0b470SArd Biesheuvel 	status = allocate_e820(boot_params, &e820ext, &e820ext_size);
751c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
752c2d0b470SArd Biesheuvel 		return status;
753c2d0b470SArd Biesheuvel 
754c2d0b470SArd Biesheuvel 	/* Might as well exit boot services now */
755eab31265SArd Biesheuvel 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
756c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
757c2d0b470SArd Biesheuvel 		return status;
758c2d0b470SArd Biesheuvel 
759c2d0b470SArd Biesheuvel 	/* Historic? */
760c2d0b470SArd Biesheuvel 	boot_params->alt_mem_k	= 32 * 1024;
761c2d0b470SArd Biesheuvel 
762c2d0b470SArd Biesheuvel 	status = setup_e820(boot_params, e820ext, e820ext_size);
763c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
764c2d0b470SArd Biesheuvel 		return status;
765c2d0b470SArd Biesheuvel 
766c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
767c2d0b470SArd Biesheuvel }
768c2d0b470SArd Biesheuvel 
76931c77a50SArd Biesheuvel static bool have_unsupported_snp_features(void)
77031c77a50SArd Biesheuvel {
77131c77a50SArd Biesheuvel 	u64 unsupported;
77231c77a50SArd Biesheuvel 
77331c77a50SArd Biesheuvel 	unsupported = snp_get_unsupported_features(sev_get_status());
77431c77a50SArd Biesheuvel 	if (unsupported) {
77531c77a50SArd Biesheuvel 		efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
77631c77a50SArd Biesheuvel 			unsupported);
77731c77a50SArd Biesheuvel 		return true;
77831c77a50SArd Biesheuvel 	}
77931c77a50SArd Biesheuvel 	return false;
78031c77a50SArd Biesheuvel }
78131c77a50SArd Biesheuvel 
782a1b87d54SArd Biesheuvel static void efi_get_seed(void *seed, int size)
783a1b87d54SArd Biesheuvel {
784a1b87d54SArd Biesheuvel 	efi_get_random_bytes(size, seed);
785a1b87d54SArd Biesheuvel 
786a1b87d54SArd Biesheuvel 	/*
787a1b87d54SArd Biesheuvel 	 * This only updates seed[0] when running on 32-bit, but in that case,
788a1b87d54SArd Biesheuvel 	 * seed[1] is not used anyway, as there is no virtual KASLR on 32-bit.
789a1b87d54SArd Biesheuvel 	 */
790a1b87d54SArd Biesheuvel 	*(unsigned long *)seed ^= kaslr_get_random_long("EFI");
791a1b87d54SArd Biesheuvel }
792a1b87d54SArd Biesheuvel 
793a1b87d54SArd Biesheuvel static void error(char *str)
794a1b87d54SArd Biesheuvel {
795a1b87d54SArd Biesheuvel 	efi_warn("Decompression failed: %s\n", str);
796a1b87d54SArd Biesheuvel }
797a1b87d54SArd Biesheuvel 
798a1b87d54SArd Biesheuvel static efi_status_t efi_decompress_kernel(unsigned long *kernel_entry)
799a1b87d54SArd Biesheuvel {
800a1b87d54SArd Biesheuvel 	unsigned long virt_addr = LOAD_PHYSICAL_ADDR;
801a1b87d54SArd Biesheuvel 	unsigned long addr, alloc_size, entry;
802a1b87d54SArd Biesheuvel 	efi_status_t status;
803a1b87d54SArd Biesheuvel 	u32 seed[2] = {};
804a1b87d54SArd Biesheuvel 
805a1b87d54SArd Biesheuvel 	/* determine the required size of the allocation */
806a1b87d54SArd Biesheuvel 	alloc_size = ALIGN(max_t(unsigned long, output_len, kernel_total_size),
807a1b87d54SArd Biesheuvel 			   MIN_KERNEL_ALIGN);
808a1b87d54SArd Biesheuvel 
809a1b87d54SArd Biesheuvel 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE) && !efi_nokaslr) {
810a1b87d54SArd Biesheuvel 		u64 range = KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR - kernel_total_size;
811800f84d8SArd Biesheuvel 		static const efi_char16_t ami[] = L"American Megatrends";
812a1b87d54SArd Biesheuvel 
813a1b87d54SArd Biesheuvel 		efi_get_seed(seed, sizeof(seed));
814a1b87d54SArd Biesheuvel 
815a1b87d54SArd Biesheuvel 		virt_addr += (range * seed[1]) >> 32;
816a1b87d54SArd Biesheuvel 		virt_addr &= ~(CONFIG_PHYSICAL_ALIGN - 1);
817800f84d8SArd Biesheuvel 
818800f84d8SArd Biesheuvel 		/*
819800f84d8SArd Biesheuvel 		 * Older Dell systems with AMI UEFI firmware v2.0 may hang
820800f84d8SArd Biesheuvel 		 * while decompressing the kernel if physical address
821800f84d8SArd Biesheuvel 		 * randomization is enabled.
822800f84d8SArd Biesheuvel 		 *
823800f84d8SArd Biesheuvel 		 * https://bugzilla.kernel.org/show_bug.cgi?id=218173
824800f84d8SArd Biesheuvel 		 */
825800f84d8SArd Biesheuvel 		if (efi_system_table->hdr.revision <= EFI_2_00_SYSTEM_TABLE_REVISION &&
826800f84d8SArd Biesheuvel 		    !memcmp(efistub_fw_vendor(), ami, sizeof(ami))) {
827800f84d8SArd Biesheuvel 			efi_debug("AMI firmware v2.0 or older detected - disabling physical KASLR\n");
828800f84d8SArd Biesheuvel 			seed[0] = 0;
829800f84d8SArd Biesheuvel 		}
830a1b87d54SArd Biesheuvel 	}
831a1b87d54SArd Biesheuvel 
832a1b87d54SArd Biesheuvel 	status = efi_random_alloc(alloc_size, CONFIG_PHYSICAL_ALIGN, &addr,
833a1b87d54SArd Biesheuvel 				  seed[0], EFI_LOADER_CODE,
834a1b87d54SArd Biesheuvel 				  EFI_X86_KERNEL_ALLOC_LIMIT);
835a1b87d54SArd Biesheuvel 	if (status != EFI_SUCCESS)
836a1b87d54SArd Biesheuvel 		return status;
837a1b87d54SArd Biesheuvel 
838a1b87d54SArd Biesheuvel 	entry = decompress_kernel((void *)addr, virt_addr, error);
839a1b87d54SArd Biesheuvel 	if (entry == ULONG_MAX) {
840a1b87d54SArd Biesheuvel 		efi_free(alloc_size, addr);
841a1b87d54SArd Biesheuvel 		return EFI_LOAD_ERROR;
842a1b87d54SArd Biesheuvel 	}
843a1b87d54SArd Biesheuvel 
844a1b87d54SArd Biesheuvel 	*kernel_entry = addr + entry;
845a1b87d54SArd Biesheuvel 
846*c756fd5dSArd Biesheuvel 	return efi_adjust_memory_range_protection(addr, kernel_total_size);
847a1b87d54SArd Biesheuvel }
848a1b87d54SArd Biesheuvel 
849d2d7a54fSArd Biesheuvel static void __noreturn enter_kernel(unsigned long kernel_addr,
850d2d7a54fSArd Biesheuvel 				    struct boot_params *boot_params)
851d2d7a54fSArd Biesheuvel {
852d2d7a54fSArd Biesheuvel 	/* enter decompressed kernel with boot_params pointer in RSI/ESI */
853d2d7a54fSArd Biesheuvel 	asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
854d2d7a54fSArd Biesheuvel 
855d2d7a54fSArd Biesheuvel 	unreachable();
856d2d7a54fSArd Biesheuvel }
857d2d7a54fSArd Biesheuvel 
858c2d0b470SArd Biesheuvel /*
859d2d7a54fSArd Biesheuvel  * On success, this routine will jump to the relocated image directly and never
860d2d7a54fSArd Biesheuvel  * return.  On failure, it will exit to the firmware via efi_exit() instead of
861d2d7a54fSArd Biesheuvel  * returning.
862c2d0b470SArd Biesheuvel  */
863df9215f1SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle,
864c2d0b470SArd Biesheuvel 			       efi_system_table_t *sys_table_arg,
865c2d0b470SArd Biesheuvel 			       struct boot_params *boot_params)
866c2d0b470SArd Biesheuvel {
86711078876SArd Biesheuvel 	efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
868c2d0b470SArd Biesheuvel 	struct setup_header *hdr = &boot_params->hdr;
869f4dc7fffSArd Biesheuvel 	const struct linux_efi_initrd *initrd = NULL;
870a1b87d54SArd Biesheuvel 	unsigned long kernel_entry;
871c2d0b470SArd Biesheuvel 	efi_status_t status;
872c2d0b470SArd Biesheuvel 
873db772413SArd Biesheuvel 	boot_params_pointer = boot_params;
874db772413SArd Biesheuvel 
875ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
876c2d0b470SArd Biesheuvel 	/* Check if we were booted by the EFI firmware */
877ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
8783b8f44fcSArd Biesheuvel 		efi_exit(handle, EFI_INVALID_PARAMETER);
879c2d0b470SArd Biesheuvel 
88031c77a50SArd Biesheuvel 	if (have_unsupported_snp_features())
88131c77a50SArd Biesheuvel 		efi_exit(handle, EFI_UNSUPPORTED);
88231c77a50SArd Biesheuvel 
88311078876SArd Biesheuvel 	if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
8843ba75c13SBaskov Evgeniy 		efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
8853ba75c13SBaskov Evgeniy 		if (efi_dxe_table &&
8863ba75c13SBaskov Evgeniy 		    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
8873ba75c13SBaskov Evgeniy 			efi_warn("Ignoring DXE services table: invalid signature\n");
8883ba75c13SBaskov Evgeniy 			efi_dxe_table = NULL;
8893ba75c13SBaskov Evgeniy 		}
89011078876SArd Biesheuvel 	}
89111078876SArd Biesheuvel 
89211078876SArd Biesheuvel 	/* grab the memory attributes protocol if it exists */
89311078876SArd Biesheuvel 	efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
8943ba75c13SBaskov Evgeniy 
895cb1c9e02SArd Biesheuvel 	status = efi_setup_5level_paging();
896cb1c9e02SArd Biesheuvel 	if (status != EFI_SUCCESS) {
897cb1c9e02SArd Biesheuvel 		efi_err("efi_setup_5level_paging() failed!\n");
898cb1c9e02SArd Biesheuvel 		goto fail;
899cb1c9e02SArd Biesheuvel 	}
900cb1c9e02SArd Biesheuvel 
9017dde67f2SArvind Sankar #ifdef CONFIG_CMDLINE_BOOL
902055042beSArvind Sankar 	status = efi_parse_options(CONFIG_CMDLINE);
903055042beSArvind Sankar 	if (status != EFI_SUCCESS) {
904055042beSArvind Sankar 		efi_err("Failed to parse options\n");
905055042beSArvind Sankar 		goto fail;
906055042beSArvind Sankar 	}
9077dde67f2SArvind Sankar #endif
9087dde67f2SArvind Sankar 	if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
9097dde67f2SArvind Sankar 		unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
910c2d0b470SArd Biesheuvel 					       ((u64)boot_params->ext_cmd_line_ptr << 32));
911055042beSArvind Sankar 		status = efi_parse_options((char *)cmdline_paddr);
912055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
913055042beSArvind Sankar 			efi_err("Failed to parse options\n");
914055042beSArvind Sankar 			goto fail;
915055042beSArvind Sankar 		}
9167dde67f2SArvind Sankar 	}
917c2d0b470SArd Biesheuvel 
918a1b87d54SArd Biesheuvel 	status = efi_decompress_kernel(&kernel_entry);
919a1b87d54SArd Biesheuvel 	if (status != EFI_SUCCESS) {
920a1b87d54SArd Biesheuvel 		efi_err("Failed to decompress kernel\n");
921a1b87d54SArd Biesheuvel 		goto fail;
922a1b87d54SArd Biesheuvel 	}
923a1b87d54SArd Biesheuvel 
924c2d0b470SArd Biesheuvel 	/*
925987053a3SArvind Sankar 	 * At this point, an initrd may already have been loaded by the
926987053a3SArvind Sankar 	 * bootloader and passed via bootparams. We permit an initrd loaded
927987053a3SArvind Sankar 	 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
928987053a3SArvind Sankar 	 *
929987053a3SArvind Sankar 	 * If the device path is not present, any command-line initrd=
930987053a3SArvind Sankar 	 * arguments will be processed only if image is not NULL, which will be
931987053a3SArvind Sankar 	 * the case only if we were loaded via the PE entry point.
932ec93fc37SArd Biesheuvel 	 */
933f4dc7fffSArd Biesheuvel 	status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
934f4dc7fffSArd Biesheuvel 				 &initrd);
93520287d56SArd Biesheuvel 	if (status != EFI_SUCCESS)
936ec93fc37SArd Biesheuvel 		goto fail;
937f4dc7fffSArd Biesheuvel 	if (initrd && initrd->size > 0) {
938f4dc7fffSArd Biesheuvel 		efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
939eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_image);
940f4dc7fffSArd Biesheuvel 		efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
941eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_size);
94279d3219dSArd Biesheuvel 	}
943ec93fc37SArd Biesheuvel 
944f4dc7fffSArd Biesheuvel 
945ec93fc37SArd Biesheuvel 	/*
946c2d0b470SArd Biesheuvel 	 * If the boot loader gave us a value for secure_boot then we use that,
947c2d0b470SArd Biesheuvel 	 * otherwise we ask the BIOS.
948c2d0b470SArd Biesheuvel 	 */
949c2d0b470SArd Biesheuvel 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
950c2d0b470SArd Biesheuvel 		boot_params->secure_boot = efi_get_secureboot();
951c2d0b470SArd Biesheuvel 
952c2d0b470SArd Biesheuvel 	/* Ask the firmware to clear memory on unclean shutdown */
953c2d0b470SArd Biesheuvel 	efi_enable_reset_attack_mitigation();
954c2d0b470SArd Biesheuvel 
955c2d0b470SArd Biesheuvel 	efi_random_get_seed();
956c2d0b470SArd Biesheuvel 
957c2d0b470SArd Biesheuvel 	efi_retrieve_tpm2_eventlog();
958c2d0b470SArd Biesheuvel 
959c2d0b470SArd Biesheuvel 	setup_graphics(boot_params);
960c2d0b470SArd Biesheuvel 
961c2d0b470SArd Biesheuvel 	setup_efi_pci(boot_params);
962c2d0b470SArd Biesheuvel 
963a1b87d54SArd Biesheuvel 	setup_quirks(boot_params);
964c2d0b470SArd Biesheuvel 
965c0461bd1SDionna Glaze 	setup_unaccepted_memory();
966c0461bd1SDionna Glaze 
967c2d0b470SArd Biesheuvel 	status = exit_boot(boot_params, handle);
968c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
96936bdd0a7SArvind Sankar 		efi_err("exit_boot() failed!\n");
970c2d0b470SArd Biesheuvel 		goto fail;
971c2d0b470SArd Biesheuvel 	}
972c2d0b470SArd Biesheuvel 
973a1b87d54SArd Biesheuvel 	/*
974a1b87d54SArd Biesheuvel 	 * Call the SEV init code while still running with the firmware's
975a1b87d54SArd Biesheuvel 	 * GDT/IDT, so #VC exceptions will be handled by EFI.
976a1b87d54SArd Biesheuvel 	 */
977a1b87d54SArd Biesheuvel 	sev_enable(boot_params);
978a1b87d54SArd Biesheuvel 
979cb1c9e02SArd Biesheuvel 	efi_5level_switch();
980cb1c9e02SArd Biesheuvel 
981a1b87d54SArd Biesheuvel 	enter_kernel(kernel_entry, boot_params);
982c2d0b470SArd Biesheuvel fail:
983df9215f1SArd Biesheuvel 	efi_err("efi_stub_entry() failed!\n");
984c2d0b470SArd Biesheuvel 
9853b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
986c2d0b470SArd Biesheuvel }
987df9215f1SArd Biesheuvel 
988df9215f1SArd Biesheuvel #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
989d7156b98SArd Biesheuvel void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
990d7156b98SArd Biesheuvel 			struct boot_params *boot_params)
991d7156b98SArd Biesheuvel {
992d7156b98SArd Biesheuvel 	extern char _bss[], _ebss[];
993d7156b98SArd Biesheuvel 
994d7156b98SArd Biesheuvel 	memset(_bss, 0, _ebss - _bss);
995d7156b98SArd Biesheuvel 	efi_stub_entry(handle, sys_table_arg, boot_params);
996d7156b98SArd Biesheuvel }
997d7156b98SArd Biesheuvel 
998df9215f1SArd Biesheuvel #ifndef CONFIG_EFI_MIXED
999d7156b98SArd Biesheuvel extern __alias(efi_handover_entry)
1000df9215f1SArd Biesheuvel void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
1001df9215f1SArd Biesheuvel 		      struct boot_params *boot_params);
1002df9215f1SArd Biesheuvel 
1003d7156b98SArd Biesheuvel extern __alias(efi_handover_entry)
1004df9215f1SArd Biesheuvel void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
1005df9215f1SArd Biesheuvel 		      struct boot_params *boot_params);
1006df9215f1SArd Biesheuvel #endif
1007df9215f1SArd Biesheuvel #endif
1008