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>
18*31c77a50SArd Biesheuvel #include <asm/sev.h>
19c2d0b470SArd Biesheuvel 
20c2d0b470SArd Biesheuvel #include "efistub.h"
21cb1c9e02SArd Biesheuvel #include "x86-stub.h"
22c2d0b470SArd Biesheuvel 
23d5cdf4cfSArvind Sankar /* Maximum physical address for 64-bit kernel with 4-level paging */
24d5cdf4cfSArvind Sankar #define MAXMEM_X86_64_4LEVEL (1ull << 46)
25d5cdf4cfSArvind Sankar 
26ccc27ae7SArd Biesheuvel const efi_system_table_t *efi_system_table;
273ba75c13SBaskov Evgeniy const efi_dxe_services_table_t *efi_dxe_table;
284b520162SArd Biesheuvel u32 image_offset __section(".data");
29987053a3SArvind Sankar static efi_loaded_image_t *image = NULL;
3011078876SArd Biesheuvel static efi_memory_attribute_protocol_t *memattr;
31c2d0b470SArd Biesheuvel 
32c0461bd1SDionna Glaze typedef union sev_memory_acceptance_protocol sev_memory_acceptance_protocol_t;
33c0461bd1SDionna Glaze union sev_memory_acceptance_protocol {
34c0461bd1SDionna Glaze 	struct {
35c0461bd1SDionna Glaze 		efi_status_t (__efiapi * allow_unaccepted_memory)(
36c0461bd1SDionna Glaze 			sev_memory_acceptance_protocol_t *);
37c0461bd1SDionna Glaze 	};
38c0461bd1SDionna Glaze 	struct {
39c0461bd1SDionna Glaze 		u32 allow_unaccepted_memory;
40c0461bd1SDionna Glaze 	} mixed_mode;
41c0461bd1SDionna Glaze };
42c0461bd1SDionna Glaze 
43c2d0b470SArd Biesheuvel static efi_status_t
44c2d0b470SArd Biesheuvel preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
45c2d0b470SArd Biesheuvel {
46c2d0b470SArd Biesheuvel 	struct pci_setup_rom *rom = NULL;
47c2d0b470SArd Biesheuvel 	efi_status_t status;
48c2d0b470SArd Biesheuvel 	unsigned long size;
49c2d0b470SArd Biesheuvel 	uint64_t romsize;
50c2d0b470SArd Biesheuvel 	void *romimage;
51c2d0b470SArd Biesheuvel 
52c2d0b470SArd Biesheuvel 	/*
53c2d0b470SArd Biesheuvel 	 * Some firmware images contain EFI function pointers at the place where
54c2d0b470SArd Biesheuvel 	 * the romimage and romsize fields are supposed to be. Typically the EFI
55c2d0b470SArd Biesheuvel 	 * code is mapped at high addresses, translating to an unrealistically
56c2d0b470SArd Biesheuvel 	 * large romsize. The UEFI spec limits the size of option ROMs to 16
57c2d0b470SArd Biesheuvel 	 * MiB so we reject any ROMs over 16 MiB in size to catch this.
58c2d0b470SArd Biesheuvel 	 */
59c2d0b470SArd Biesheuvel 	romimage = efi_table_attr(pci, romimage);
60c2d0b470SArd Biesheuvel 	romsize = efi_table_attr(pci, romsize);
61c2d0b470SArd Biesheuvel 	if (!romimage || !romsize || romsize > SZ_16M)
62c2d0b470SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
63c2d0b470SArd Biesheuvel 
64c2d0b470SArd Biesheuvel 	size = romsize + sizeof(*rom);
65c2d0b470SArd Biesheuvel 
66c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
67c2d0b470SArd Biesheuvel 			     (void **)&rom);
68c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
6936bdd0a7SArvind Sankar 		efi_err("Failed to allocate memory for 'rom'\n");
70c2d0b470SArd Biesheuvel 		return status;
71c2d0b470SArd Biesheuvel 	}
72c2d0b470SArd Biesheuvel 
73c2d0b470SArd Biesheuvel 	memset(rom, 0, sizeof(*rom));
74c2d0b470SArd Biesheuvel 
75c2d0b470SArd Biesheuvel 	rom->data.type	= SETUP_PCI;
76c2d0b470SArd Biesheuvel 	rom->data.len	= size - sizeof(struct setup_data);
77c2d0b470SArd Biesheuvel 	rom->data.next	= 0;
78c2d0b470SArd Biesheuvel 	rom->pcilen	= pci->romsize;
79c2d0b470SArd Biesheuvel 	*__rom = rom;
80c2d0b470SArd Biesheuvel 
81c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
82c2d0b470SArd Biesheuvel 				PCI_VENDOR_ID, 1, &rom->vendor);
83c2d0b470SArd Biesheuvel 
84c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
8536bdd0a7SArvind Sankar 		efi_err("Failed to read rom->vendor\n");
86c2d0b470SArd Biesheuvel 		goto free_struct;
87c2d0b470SArd Biesheuvel 	}
88c2d0b470SArd Biesheuvel 
89c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
90c2d0b470SArd Biesheuvel 				PCI_DEVICE_ID, 1, &rom->devid);
91c2d0b470SArd Biesheuvel 
92c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
9336bdd0a7SArvind Sankar 		efi_err("Failed to read rom->devid\n");
94c2d0b470SArd Biesheuvel 		goto free_struct;
95c2d0b470SArd Biesheuvel 	}
96c2d0b470SArd Biesheuvel 
97c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
98c2d0b470SArd Biesheuvel 				&rom->device, &rom->function);
99c2d0b470SArd Biesheuvel 
100c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
101c2d0b470SArd Biesheuvel 		goto free_struct;
102c2d0b470SArd Biesheuvel 
103c2d0b470SArd Biesheuvel 	memcpy(rom->romdata, romimage, romsize);
104c2d0b470SArd Biesheuvel 	return status;
105c2d0b470SArd Biesheuvel 
106c2d0b470SArd Biesheuvel free_struct:
107c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, rom);
108c2d0b470SArd Biesheuvel 	return status;
109c2d0b470SArd Biesheuvel }
110c2d0b470SArd Biesheuvel 
111c2d0b470SArd Biesheuvel /*
112c2d0b470SArd Biesheuvel  * There's no way to return an informative status from this function,
113c2d0b470SArd Biesheuvel  * because any analysis (and printing of error messages) needs to be
114c2d0b470SArd Biesheuvel  * done directly at the EFI function call-site.
115c2d0b470SArd Biesheuvel  *
116c2d0b470SArd Biesheuvel  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
117c2d0b470SArd Biesheuvel  * just didn't find any PCI devices, but there's no way to tell outside
118c2d0b470SArd Biesheuvel  * the context of the call.
119c2d0b470SArd Biesheuvel  */
120c2d0b470SArd Biesheuvel static void setup_efi_pci(struct boot_params *params)
121c2d0b470SArd Biesheuvel {
122c2d0b470SArd Biesheuvel 	efi_status_t status;
123c2d0b470SArd Biesheuvel 	void **pci_handle = NULL;
124c2d0b470SArd Biesheuvel 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
125c2d0b470SArd Biesheuvel 	unsigned long size = 0;
126c2d0b470SArd Biesheuvel 	struct setup_data *data;
127c2d0b470SArd Biesheuvel 	efi_handle_t h;
128c2d0b470SArd Biesheuvel 	int i;
129c2d0b470SArd Biesheuvel 
130c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
131c2d0b470SArd Biesheuvel 			     &pci_proto, NULL, &size, pci_handle);
132c2d0b470SArd Biesheuvel 
133c2d0b470SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL) {
134c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
135c2d0b470SArd Biesheuvel 				     (void **)&pci_handle);
136c2d0b470SArd Biesheuvel 
137c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
13836bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'pci_handle'\n");
139c2d0b470SArd Biesheuvel 			return;
140c2d0b470SArd Biesheuvel 		}
141c2d0b470SArd Biesheuvel 
142c2d0b470SArd Biesheuvel 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
143c2d0b470SArd Biesheuvel 				     &pci_proto, NULL, &size, pci_handle);
144c2d0b470SArd Biesheuvel 	}
145c2d0b470SArd Biesheuvel 
146c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
147c2d0b470SArd Biesheuvel 		goto free_handle;
148c2d0b470SArd Biesheuvel 
149c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
150c2d0b470SArd Biesheuvel 
151c2d0b470SArd Biesheuvel 	while (data && data->next)
152c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
153c2d0b470SArd Biesheuvel 
154c2d0b470SArd Biesheuvel 	for_each_efi_handle(h, pci_handle, size, i) {
155c2d0b470SArd Biesheuvel 		efi_pci_io_protocol_t *pci = NULL;
156c2d0b470SArd Biesheuvel 		struct pci_setup_rom *rom;
157c2d0b470SArd Biesheuvel 
158c2d0b470SArd Biesheuvel 		status = efi_bs_call(handle_protocol, h, &pci_proto,
159c2d0b470SArd Biesheuvel 				     (void **)&pci);
160c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS || !pci)
161c2d0b470SArd Biesheuvel 			continue;
162c2d0b470SArd Biesheuvel 
163c2d0b470SArd Biesheuvel 		status = preserve_pci_rom_image(pci, &rom);
164c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
165c2d0b470SArd Biesheuvel 			continue;
166c2d0b470SArd Biesheuvel 
167c2d0b470SArd Biesheuvel 		if (data)
168c2d0b470SArd Biesheuvel 			data->next = (unsigned long)rom;
169c2d0b470SArd Biesheuvel 		else
170c2d0b470SArd Biesheuvel 			params->hdr.setup_data = (unsigned long)rom;
171c2d0b470SArd Biesheuvel 
172c2d0b470SArd Biesheuvel 		data = (struct setup_data *)rom;
173c2d0b470SArd Biesheuvel 	}
174c2d0b470SArd Biesheuvel 
175c2d0b470SArd Biesheuvel free_handle:
176c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, pci_handle);
177c2d0b470SArd Biesheuvel }
178c2d0b470SArd Biesheuvel 
179c2d0b470SArd Biesheuvel static void retrieve_apple_device_properties(struct boot_params *boot_params)
180c2d0b470SArd Biesheuvel {
181c2d0b470SArd Biesheuvel 	efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
182c2d0b470SArd Biesheuvel 	struct setup_data *data, *new;
183c2d0b470SArd Biesheuvel 	efi_status_t status;
184c2d0b470SArd Biesheuvel 	u32 size = 0;
185c2d0b470SArd Biesheuvel 	apple_properties_protocol_t *p;
186c2d0b470SArd Biesheuvel 
187c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
188c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
189c2d0b470SArd Biesheuvel 		return;
190c2d0b470SArd Biesheuvel 
191c2d0b470SArd Biesheuvel 	if (efi_table_attr(p, version) != 0x10000) {
19236bdd0a7SArvind Sankar 		efi_err("Unsupported properties proto version\n");
193c2d0b470SArd Biesheuvel 		return;
194c2d0b470SArd Biesheuvel 	}
195c2d0b470SArd Biesheuvel 
196c2d0b470SArd Biesheuvel 	efi_call_proto(p, get_all, NULL, &size);
197c2d0b470SArd Biesheuvel 	if (!size)
198c2d0b470SArd Biesheuvel 		return;
199c2d0b470SArd Biesheuvel 
200c2d0b470SArd Biesheuvel 	do {
201c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
202c2d0b470SArd Biesheuvel 				     size + sizeof(struct setup_data),
203c2d0b470SArd Biesheuvel 				     (void **)&new);
204c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
20536bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'properties'\n");
206c2d0b470SArd Biesheuvel 			return;
207c2d0b470SArd Biesheuvel 		}
208c2d0b470SArd Biesheuvel 
209c2d0b470SArd Biesheuvel 		status = efi_call_proto(p, get_all, new->data, &size);
210c2d0b470SArd Biesheuvel 
211c2d0b470SArd Biesheuvel 		if (status == EFI_BUFFER_TOO_SMALL)
212c2d0b470SArd Biesheuvel 			efi_bs_call(free_pool, new);
213c2d0b470SArd Biesheuvel 	} while (status == EFI_BUFFER_TOO_SMALL);
214c2d0b470SArd Biesheuvel 
215c2d0b470SArd Biesheuvel 	new->type = SETUP_APPLE_PROPERTIES;
216c2d0b470SArd Biesheuvel 	new->len  = size;
217c2d0b470SArd Biesheuvel 	new->next = 0;
218c2d0b470SArd Biesheuvel 
219c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
220c2d0b470SArd Biesheuvel 	if (!data) {
221c2d0b470SArd Biesheuvel 		boot_params->hdr.setup_data = (unsigned long)new;
222c2d0b470SArd Biesheuvel 	} else {
223c2d0b470SArd Biesheuvel 		while (data->next)
224c2d0b470SArd Biesheuvel 			data = (struct setup_data *)(unsigned long)data->next;
225c2d0b470SArd Biesheuvel 		data->next = (unsigned long)new;
226c2d0b470SArd Biesheuvel 	}
227c2d0b470SArd Biesheuvel }
228c2d0b470SArd Biesheuvel 
229cb1c9e02SArd Biesheuvel void efi_adjust_memory_range_protection(unsigned long start,
230cb1c9e02SArd Biesheuvel 					unsigned long size)
23182e0d6d7SBaskov Evgeniy {
23282e0d6d7SBaskov Evgeniy 	efi_status_t status;
23382e0d6d7SBaskov Evgeniy 	efi_gcd_memory_space_desc_t desc;
23482e0d6d7SBaskov Evgeniy 	unsigned long end, next;
23582e0d6d7SBaskov Evgeniy 	unsigned long rounded_start, rounded_end;
23682e0d6d7SBaskov Evgeniy 	unsigned long unprotect_start, unprotect_size;
23782e0d6d7SBaskov Evgeniy 
23882e0d6d7SBaskov Evgeniy 	rounded_start = rounddown(start, EFI_PAGE_SIZE);
23982e0d6d7SBaskov Evgeniy 	rounded_end = roundup(start + size, EFI_PAGE_SIZE);
24082e0d6d7SBaskov Evgeniy 
24111078876SArd Biesheuvel 	if (memattr != NULL) {
24211078876SArd Biesheuvel 		efi_call_proto(memattr, clear_memory_attributes, rounded_start,
24311078876SArd Biesheuvel 			       rounded_end - rounded_start, EFI_MEMORY_XP);
24411078876SArd Biesheuvel 		return;
24511078876SArd Biesheuvel 	}
24611078876SArd Biesheuvel 
24711078876SArd Biesheuvel 	if (efi_dxe_table == NULL)
24811078876SArd Biesheuvel 		return;
24911078876SArd Biesheuvel 
25082e0d6d7SBaskov Evgeniy 	/*
25182e0d6d7SBaskov Evgeniy 	 * Don't modify memory region attributes, they are
25282e0d6d7SBaskov Evgeniy 	 * already suitable, to lower the possibility to
25382e0d6d7SBaskov Evgeniy 	 * encounter firmware bugs.
25482e0d6d7SBaskov Evgeniy 	 */
25582e0d6d7SBaskov Evgeniy 
25682e0d6d7SBaskov Evgeniy 	for (end = start + size; start < end; start = next) {
25782e0d6d7SBaskov Evgeniy 
25882e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
25982e0d6d7SBaskov Evgeniy 
26082e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS)
26182e0d6d7SBaskov Evgeniy 			return;
26282e0d6d7SBaskov Evgeniy 
26382e0d6d7SBaskov Evgeniy 		next = desc.base_address + desc.length;
26482e0d6d7SBaskov Evgeniy 
26582e0d6d7SBaskov Evgeniy 		/*
26682e0d6d7SBaskov Evgeniy 		 * Only system memory is suitable for trampoline/kernel image placement,
26782e0d6d7SBaskov Evgeniy 		 * so only this type of memory needs its attributes to be modified.
26882e0d6d7SBaskov Evgeniy 		 */
26982e0d6d7SBaskov Evgeniy 
27082e0d6d7SBaskov Evgeniy 		if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
27182e0d6d7SBaskov Evgeniy 		    (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
27282e0d6d7SBaskov Evgeniy 			continue;
27382e0d6d7SBaskov Evgeniy 
27482e0d6d7SBaskov Evgeniy 		unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
27582e0d6d7SBaskov Evgeniy 		unprotect_size = min(rounded_end, next) - unprotect_start;
27682e0d6d7SBaskov Evgeniy 
27782e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(set_memory_space_attributes,
27882e0d6d7SBaskov Evgeniy 				      unprotect_start, unprotect_size,
27982e0d6d7SBaskov Evgeniy 				      EFI_MEMORY_WB);
28082e0d6d7SBaskov Evgeniy 
28182e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS) {
28231f1a0edSArd Biesheuvel 			efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
28382e0d6d7SBaskov Evgeniy 				 unprotect_start,
28482e0d6d7SBaskov Evgeniy 				 unprotect_start + unprotect_size,
28531f1a0edSArd Biesheuvel 				 status);
28682e0d6d7SBaskov Evgeniy 		}
28782e0d6d7SBaskov Evgeniy 	}
28882e0d6d7SBaskov Evgeniy }
28982e0d6d7SBaskov Evgeniy 
290d2d7a54fSArd Biesheuvel extern const u8 startup_32[], startup_64[];
29182e0d6d7SBaskov Evgeniy 
29282e0d6d7SBaskov Evgeniy static void
29382e0d6d7SBaskov Evgeniy setup_memory_protection(unsigned long image_base, unsigned long image_size)
29482e0d6d7SBaskov Evgeniy {
29582e0d6d7SBaskov Evgeniy #ifdef CONFIG_64BIT
29682e0d6d7SBaskov Evgeniy 	if (image_base != (unsigned long)startup_32)
297cb1c9e02SArd Biesheuvel 		efi_adjust_memory_range_protection(image_base, image_size);
29882e0d6d7SBaskov Evgeniy #else
29982e0d6d7SBaskov Evgeniy 	/*
30082e0d6d7SBaskov Evgeniy 	 * Clear protection flags on a whole range of possible
30182e0d6d7SBaskov Evgeniy 	 * addresses used for KASLR. We don't need to do that
30282e0d6d7SBaskov Evgeniy 	 * on x86_64, since KASLR/extraction is performed after
30382e0d6d7SBaskov Evgeniy 	 * dedicated identity page tables are built and we only
30482e0d6d7SBaskov Evgeniy 	 * need to remove possible protection on relocated image
30582e0d6d7SBaskov Evgeniy 	 * itself disregarding further relocations.
30682e0d6d7SBaskov Evgeniy 	 */
307cb1c9e02SArd Biesheuvel 	efi_adjust_memory_range_protection(LOAD_PHYSICAL_ADDR,
30882e0d6d7SBaskov Evgeniy 					   KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR);
30982e0d6d7SBaskov Evgeniy #endif
31082e0d6d7SBaskov Evgeniy }
31182e0d6d7SBaskov Evgeniy 
312c0461bd1SDionna Glaze static void setup_unaccepted_memory(void)
313c0461bd1SDionna Glaze {
314c0461bd1SDionna Glaze 	efi_guid_t mem_acceptance_proto = OVMF_SEV_MEMORY_ACCEPTANCE_PROTOCOL_GUID;
315c0461bd1SDionna Glaze 	sev_memory_acceptance_protocol_t *proto;
316c0461bd1SDionna Glaze 	efi_status_t status;
317c0461bd1SDionna Glaze 
318c0461bd1SDionna Glaze 	if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY))
319c0461bd1SDionna Glaze 		return;
320c0461bd1SDionna Glaze 
321c0461bd1SDionna Glaze 	/*
322c0461bd1SDionna Glaze 	 * Enable unaccepted memory before calling exit boot services in order
323c0461bd1SDionna Glaze 	 * for the UEFI to not accept all memory on EBS.
324c0461bd1SDionna Glaze 	 */
325c0461bd1SDionna Glaze 	status = efi_bs_call(locate_protocol, &mem_acceptance_proto, NULL,
326c0461bd1SDionna Glaze 			     (void **)&proto);
327c0461bd1SDionna Glaze 	if (status != EFI_SUCCESS)
328c0461bd1SDionna Glaze 		return;
329c0461bd1SDionna Glaze 
330c0461bd1SDionna Glaze 	status = efi_call_proto(proto, allow_unaccepted_memory);
331c0461bd1SDionna Glaze 	if (status != EFI_SUCCESS)
332c0461bd1SDionna Glaze 		efi_err("Memory acceptance protocol failed\n");
333c0461bd1SDionna Glaze }
334c0461bd1SDionna Glaze 
335c2d0b470SArd Biesheuvel static const efi_char16_t apple[] = L"Apple";
336c2d0b470SArd Biesheuvel 
33782e0d6d7SBaskov Evgeniy static void setup_quirks(struct boot_params *boot_params,
33882e0d6d7SBaskov Evgeniy 			 unsigned long image_base,
33982e0d6d7SBaskov Evgeniy 			 unsigned long image_size)
340c2d0b470SArd Biesheuvel {
341c2d0b470SArd Biesheuvel 	efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
342ccc27ae7SArd Biesheuvel 		efi_table_attr(efi_system_table, fw_vendor);
343c2d0b470SArd Biesheuvel 
344c2d0b470SArd Biesheuvel 	if (!memcmp(fw_vendor, apple, sizeof(apple))) {
345c2d0b470SArd Biesheuvel 		if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
346c2d0b470SArd Biesheuvel 			retrieve_apple_device_properties(boot_params);
347c2d0b470SArd Biesheuvel 	}
34882e0d6d7SBaskov Evgeniy 
34982e0d6d7SBaskov Evgeniy 	if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES))
35082e0d6d7SBaskov Evgeniy 		setup_memory_protection(image_base, image_size);
351c2d0b470SArd Biesheuvel }
352c2d0b470SArd Biesheuvel 
353c2d0b470SArd Biesheuvel /*
354c2d0b470SArd Biesheuvel  * See if we have Universal Graphics Adapter (UGA) protocol
355c2d0b470SArd Biesheuvel  */
356c2d0b470SArd Biesheuvel static efi_status_t
357c2d0b470SArd Biesheuvel setup_uga(struct screen_info *si, efi_guid_t *uga_proto, unsigned long size)
358c2d0b470SArd Biesheuvel {
359c2d0b470SArd Biesheuvel 	efi_status_t status;
360c2d0b470SArd Biesheuvel 	u32 width, height;
361c2d0b470SArd Biesheuvel 	void **uga_handle = NULL;
362c2d0b470SArd Biesheuvel 	efi_uga_draw_protocol_t *uga = NULL, *first_uga;
363c2d0b470SArd Biesheuvel 	efi_handle_t handle;
364c2d0b470SArd Biesheuvel 	int i;
365c2d0b470SArd Biesheuvel 
366c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
367c2d0b470SArd Biesheuvel 			     (void **)&uga_handle);
368c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
369c2d0b470SArd Biesheuvel 		return status;
370c2d0b470SArd Biesheuvel 
371c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
372c2d0b470SArd Biesheuvel 			     uga_proto, NULL, &size, uga_handle);
373c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
374c2d0b470SArd Biesheuvel 		goto free_handle;
375c2d0b470SArd Biesheuvel 
376c2d0b470SArd Biesheuvel 	height = 0;
377c2d0b470SArd Biesheuvel 	width = 0;
378c2d0b470SArd Biesheuvel 
379c2d0b470SArd Biesheuvel 	first_uga = NULL;
380c2d0b470SArd Biesheuvel 	for_each_efi_handle(handle, uga_handle, size, i) {
381c2d0b470SArd Biesheuvel 		efi_guid_t pciio_proto = EFI_PCI_IO_PROTOCOL_GUID;
382c2d0b470SArd Biesheuvel 		u32 w, h, depth, refresh;
383c2d0b470SArd Biesheuvel 		void *pciio;
384c2d0b470SArd Biesheuvel 
385c2d0b470SArd Biesheuvel 		status = efi_bs_call(handle_protocol, handle, uga_proto,
386c2d0b470SArd Biesheuvel 				     (void **)&uga);
387c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
388c2d0b470SArd Biesheuvel 			continue;
389c2d0b470SArd Biesheuvel 
390c2d0b470SArd Biesheuvel 		pciio = NULL;
391c2d0b470SArd Biesheuvel 		efi_bs_call(handle_protocol, handle, &pciio_proto, &pciio);
392c2d0b470SArd Biesheuvel 
393c2d0b470SArd Biesheuvel 		status = efi_call_proto(uga, get_mode, &w, &h, &depth, &refresh);
394c2d0b470SArd Biesheuvel 		if (status == EFI_SUCCESS && (!first_uga || pciio)) {
395c2d0b470SArd Biesheuvel 			width = w;
396c2d0b470SArd Biesheuvel 			height = h;
397c2d0b470SArd Biesheuvel 
398c2d0b470SArd Biesheuvel 			/*
399c2d0b470SArd Biesheuvel 			 * Once we've found a UGA supporting PCIIO,
400c2d0b470SArd Biesheuvel 			 * don't bother looking any further.
401c2d0b470SArd Biesheuvel 			 */
402c2d0b470SArd Biesheuvel 			if (pciio)
403c2d0b470SArd Biesheuvel 				break;
404c2d0b470SArd Biesheuvel 
405c2d0b470SArd Biesheuvel 			first_uga = uga;
406c2d0b470SArd Biesheuvel 		}
407c2d0b470SArd Biesheuvel 	}
408c2d0b470SArd Biesheuvel 
409c2d0b470SArd Biesheuvel 	if (!width && !height)
410c2d0b470SArd Biesheuvel 		goto free_handle;
411c2d0b470SArd Biesheuvel 
412c2d0b470SArd Biesheuvel 	/* EFI framebuffer */
413c2d0b470SArd Biesheuvel 	si->orig_video_isVGA	= VIDEO_TYPE_EFI;
414c2d0b470SArd Biesheuvel 
415c2d0b470SArd Biesheuvel 	si->lfb_depth		= 32;
416c2d0b470SArd Biesheuvel 	si->lfb_width		= width;
417c2d0b470SArd Biesheuvel 	si->lfb_height		= height;
418c2d0b470SArd Biesheuvel 
419c2d0b470SArd Biesheuvel 	si->red_size		= 8;
420c2d0b470SArd Biesheuvel 	si->red_pos		= 16;
421c2d0b470SArd Biesheuvel 	si->green_size		= 8;
422c2d0b470SArd Biesheuvel 	si->green_pos		= 8;
423c2d0b470SArd Biesheuvel 	si->blue_size		= 8;
424c2d0b470SArd Biesheuvel 	si->blue_pos		= 0;
425c2d0b470SArd Biesheuvel 	si->rsvd_size		= 8;
426c2d0b470SArd Biesheuvel 	si->rsvd_pos		= 24;
427c2d0b470SArd Biesheuvel 
428c2d0b470SArd Biesheuvel free_handle:
429c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, uga_handle);
430c2d0b470SArd Biesheuvel 
431c2d0b470SArd Biesheuvel 	return status;
432c2d0b470SArd Biesheuvel }
433c2d0b470SArd Biesheuvel 
434c2d0b470SArd Biesheuvel static void setup_graphics(struct boot_params *boot_params)
435c2d0b470SArd Biesheuvel {
436c2d0b470SArd Biesheuvel 	efi_guid_t graphics_proto = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
437c2d0b470SArd Biesheuvel 	struct screen_info *si;
438c2d0b470SArd Biesheuvel 	efi_guid_t uga_proto = EFI_UGA_PROTOCOL_GUID;
439c2d0b470SArd Biesheuvel 	efi_status_t status;
440c2d0b470SArd Biesheuvel 	unsigned long size;
441c2d0b470SArd Biesheuvel 	void **gop_handle = NULL;
442c2d0b470SArd Biesheuvel 	void **uga_handle = NULL;
443c2d0b470SArd Biesheuvel 
444c2d0b470SArd Biesheuvel 	si = &boot_params->screen_info;
445c2d0b470SArd Biesheuvel 	memset(si, 0, sizeof(*si));
446c2d0b470SArd Biesheuvel 
447c2d0b470SArd Biesheuvel 	size = 0;
448c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
449c2d0b470SArd Biesheuvel 			     &graphics_proto, NULL, &size, gop_handle);
450c2d0b470SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL)
451c2d0b470SArd Biesheuvel 		status = efi_setup_gop(si, &graphics_proto, size);
452c2d0b470SArd Biesheuvel 
453c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
454c2d0b470SArd Biesheuvel 		size = 0;
455c2d0b470SArd Biesheuvel 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
456c2d0b470SArd Biesheuvel 				     &uga_proto, NULL, &size, uga_handle);
457c2d0b470SArd Biesheuvel 		if (status == EFI_BUFFER_TOO_SMALL)
458c2d0b470SArd Biesheuvel 			setup_uga(si, &uga_proto, size);
459c2d0b470SArd Biesheuvel 	}
460c2d0b470SArd Biesheuvel }
461c2d0b470SArd Biesheuvel 
4623b8f44fcSArd Biesheuvel 
4633b8f44fcSArd Biesheuvel static void __noreturn efi_exit(efi_handle_t handle, efi_status_t status)
4643b8f44fcSArd Biesheuvel {
4653b8f44fcSArd Biesheuvel 	efi_bs_call(exit, handle, status, 0, NULL);
466f3fa0efcSArd Biesheuvel 	for(;;)
467f3fa0efcSArd Biesheuvel 		asm("hlt");
4683b8f44fcSArd Biesheuvel }
4693b8f44fcSArd Biesheuvel 
470c2d0b470SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle,
471c2d0b470SArd Biesheuvel 			       efi_system_table_t *sys_table_arg,
472c2d0b470SArd Biesheuvel 			       struct boot_params *boot_params);
473c2d0b470SArd Biesheuvel 
474c2d0b470SArd Biesheuvel /*
475c2d0b470SArd Biesheuvel  * Because the x86 boot code expects to be passed a boot_params we
476c2d0b470SArd Biesheuvel  * need to create one ourselves (usually the bootloader would create
477c2d0b470SArd Biesheuvel  * one for us).
478c2d0b470SArd Biesheuvel  */
479c2d0b470SArd Biesheuvel efi_status_t __efiapi efi_pe_entry(efi_handle_t handle,
480c2d0b470SArd Biesheuvel 				   efi_system_table_t *sys_table_arg)
481c2d0b470SArd Biesheuvel {
482c2d0b470SArd Biesheuvel 	struct boot_params *boot_params;
483c2d0b470SArd Biesheuvel 	struct setup_header *hdr;
4841887c9b6SArvind Sankar 	void *image_base;
485c2d0b470SArd Biesheuvel 	efi_guid_t proto = LOADED_IMAGE_PROTOCOL_GUID;
486c2d0b470SArd Biesheuvel 	int options_size = 0;
487c2d0b470SArd Biesheuvel 	efi_status_t status;
488c2d0b470SArd Biesheuvel 	char *cmdline_ptr;
489c2d0b470SArd Biesheuvel 
490ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
491c2d0b470SArd Biesheuvel 
492c2d0b470SArd Biesheuvel 	/* Check if we were booted by the EFI firmware */
493ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
4943b8f44fcSArd Biesheuvel 		efi_exit(handle, EFI_INVALID_PARAMETER);
495c2d0b470SArd Biesheuvel 
4960347d8c2SArvind Sankar 	status = efi_bs_call(handle_protocol, handle, &proto, (void **)&image);
497c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
49836bdd0a7SArvind Sankar 		efi_err("Failed to get handle for LOADED_IMAGE_PROTOCOL\n");
4993b8f44fcSArd Biesheuvel 		efi_exit(handle, status);
500c2d0b470SArd Biesheuvel 	}
501c2d0b470SArd Biesheuvel 
5021887c9b6SArvind Sankar 	image_base = efi_table_attr(image, image_base);
5031887c9b6SArvind Sankar 	image_offset = (void *)startup_32 - image_base;
5041887c9b6SArvind Sankar 
505019512f1SArvind Sankar 	status = efi_allocate_pages(sizeof(struct boot_params),
506019512f1SArvind Sankar 				    (unsigned long *)&boot_params, ULONG_MAX);
507c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
50836bdd0a7SArvind Sankar 		efi_err("Failed to allocate lowmem for boot params\n");
5093b8f44fcSArd Biesheuvel 		efi_exit(handle, status);
510c2d0b470SArd Biesheuvel 	}
511c2d0b470SArd Biesheuvel 
512019512f1SArvind Sankar 	memset(boot_params, 0x0, sizeof(struct boot_params));
513c2d0b470SArd Biesheuvel 
514c2d0b470SArd Biesheuvel 	hdr = &boot_params->hdr;
515c2d0b470SArd Biesheuvel 
51659476f80SArvind Sankar 	/* Copy the setup header from the second sector to boot_params */
51759476f80SArvind Sankar 	memcpy(&hdr->jump, image_base + 512,
51859476f80SArvind Sankar 	       sizeof(struct setup_header) - offsetof(struct setup_header, jump));
519c2d0b470SArd Biesheuvel 
520c2d0b470SArd Biesheuvel 	/*
521c2d0b470SArd Biesheuvel 	 * Fill out some of the header fields ourselves because the
522c2d0b470SArd Biesheuvel 	 * EFI firmware loader doesn't load the first sector.
523c2d0b470SArd Biesheuvel 	 */
524c2d0b470SArd Biesheuvel 	hdr->root_flags	= 1;
525c2d0b470SArd Biesheuvel 	hdr->vid_mode	= 0xffff;
526c2d0b470SArd Biesheuvel 	hdr->boot_flag	= 0xAA55;
527c2d0b470SArd Biesheuvel 
528c2d0b470SArd Biesheuvel 	hdr->type_of_loader = 0x21;
529c2d0b470SArd Biesheuvel 
530c2d0b470SArd Biesheuvel 	/* Convert unicode cmdline to ascii */
53127cd5511SArd Biesheuvel 	cmdline_ptr = efi_convert_cmdline(image, &options_size);
532c2d0b470SArd Biesheuvel 	if (!cmdline_ptr)
533c2d0b470SArd Biesheuvel 		goto fail;
534c2d0b470SArd Biesheuvel 
535eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)cmdline_ptr,
536eed4e019SArvind Sankar 			  &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
537c2d0b470SArd Biesheuvel 
538c2d0b470SArd Biesheuvel 	hdr->ramdisk_image = 0;
539c2d0b470SArd Biesheuvel 	hdr->ramdisk_size = 0;
540c2d0b470SArd Biesheuvel 
54163bf28ceSArd Biesheuvel 	/*
54263bf28ceSArd Biesheuvel 	 * Disregard any setup data that was provided by the bootloader:
54363bf28ceSArd Biesheuvel 	 * setup_data could be pointing anywhere, and we have no way of
54463bf28ceSArd Biesheuvel 	 * authenticating or validating the payload.
54563bf28ceSArd Biesheuvel 	 */
54663bf28ceSArd Biesheuvel 	hdr->setup_data = 0;
54763bf28ceSArd Biesheuvel 
548ccc27ae7SArd Biesheuvel 	efi_stub_entry(handle, sys_table_arg, boot_params);
549c2d0b470SArd Biesheuvel 	/* not reached */
550c2d0b470SArd Biesheuvel 
551c2d0b470SArd Biesheuvel fail:
552019512f1SArvind Sankar 	efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
553c2d0b470SArd Biesheuvel 
5543b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
555c2d0b470SArd Biesheuvel }
556c2d0b470SArd Biesheuvel 
557c2d0b470SArd Biesheuvel static void add_e820ext(struct boot_params *params,
558c2d0b470SArd Biesheuvel 			struct setup_data *e820ext, u32 nr_entries)
559c2d0b470SArd Biesheuvel {
560c2d0b470SArd Biesheuvel 	struct setup_data *data;
561c2d0b470SArd Biesheuvel 
562c2d0b470SArd Biesheuvel 	e820ext->type = SETUP_E820_EXT;
563c2d0b470SArd Biesheuvel 	e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
564c2d0b470SArd Biesheuvel 	e820ext->next = 0;
565c2d0b470SArd Biesheuvel 
566c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
567c2d0b470SArd Biesheuvel 
568c2d0b470SArd Biesheuvel 	while (data && data->next)
569c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
570c2d0b470SArd Biesheuvel 
571c2d0b470SArd Biesheuvel 	if (data)
572c2d0b470SArd Biesheuvel 		data->next = (unsigned long)e820ext;
573c2d0b470SArd Biesheuvel 	else
574c2d0b470SArd Biesheuvel 		params->hdr.setup_data = (unsigned long)e820ext;
575c2d0b470SArd Biesheuvel }
576c2d0b470SArd Biesheuvel 
577c2d0b470SArd Biesheuvel static efi_status_t
578c2d0b470SArd Biesheuvel setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
579c2d0b470SArd Biesheuvel {
580c2d0b470SArd Biesheuvel 	struct boot_e820_entry *entry = params->e820_table;
581c2d0b470SArd Biesheuvel 	struct efi_info *efi = &params->efi_info;
582c2d0b470SArd Biesheuvel 	struct boot_e820_entry *prev = NULL;
583c2d0b470SArd Biesheuvel 	u32 nr_entries;
584c2d0b470SArd Biesheuvel 	u32 nr_desc;
585c2d0b470SArd Biesheuvel 	int i;
586c2d0b470SArd Biesheuvel 
587c2d0b470SArd Biesheuvel 	nr_entries = 0;
588c2d0b470SArd Biesheuvel 	nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
589c2d0b470SArd Biesheuvel 
590c2d0b470SArd Biesheuvel 	for (i = 0; i < nr_desc; i++) {
591c2d0b470SArd Biesheuvel 		efi_memory_desc_t *d;
592c2d0b470SArd Biesheuvel 		unsigned int e820_type = 0;
593c2d0b470SArd Biesheuvel 		unsigned long m = efi->efi_memmap;
594c2d0b470SArd Biesheuvel 
595c2d0b470SArd Biesheuvel #ifdef CONFIG_X86_64
596c2d0b470SArd Biesheuvel 		m |= (u64)efi->efi_memmap_hi << 32;
597c2d0b470SArd Biesheuvel #endif
598c2d0b470SArd Biesheuvel 
599c2d0b470SArd Biesheuvel 		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
600c2d0b470SArd Biesheuvel 		switch (d->type) {
601c2d0b470SArd Biesheuvel 		case EFI_RESERVED_TYPE:
602c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_CODE:
603c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_DATA:
604c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO:
605c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
606c2d0b470SArd Biesheuvel 		case EFI_PAL_CODE:
607c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_RESERVED;
608c2d0b470SArd Biesheuvel 			break;
609c2d0b470SArd Biesheuvel 
610c2d0b470SArd Biesheuvel 		case EFI_UNUSABLE_MEMORY:
611c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_UNUSABLE;
612c2d0b470SArd Biesheuvel 			break;
613c2d0b470SArd Biesheuvel 
614c2d0b470SArd Biesheuvel 		case EFI_ACPI_RECLAIM_MEMORY:
615c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_ACPI;
616c2d0b470SArd Biesheuvel 			break;
617c2d0b470SArd Biesheuvel 
618c2d0b470SArd Biesheuvel 		case EFI_LOADER_CODE:
619c2d0b470SArd Biesheuvel 		case EFI_LOADER_DATA:
620c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_CODE:
621c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_DATA:
622c2d0b470SArd Biesheuvel 		case EFI_CONVENTIONAL_MEMORY:
623c2d0b470SArd Biesheuvel 			if (efi_soft_reserve_enabled() &&
624c2d0b470SArd Biesheuvel 			    (d->attribute & EFI_MEMORY_SP))
625c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_SOFT_RESERVED;
626c2d0b470SArd Biesheuvel 			else
627c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_RAM;
628c2d0b470SArd Biesheuvel 			break;
629c2d0b470SArd Biesheuvel 
630c2d0b470SArd Biesheuvel 		case EFI_ACPI_MEMORY_NVS:
631c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_NVS;
632c2d0b470SArd Biesheuvel 			break;
633c2d0b470SArd Biesheuvel 
634c2d0b470SArd Biesheuvel 		case EFI_PERSISTENT_MEMORY:
635c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_PMEM;
636c2d0b470SArd Biesheuvel 			break;
637c2d0b470SArd Biesheuvel 
638745e3ed8SKirill A. Shutemov 		case EFI_UNACCEPTED_MEMORY:
639745e3ed8SKirill A. Shutemov 			if (!IS_ENABLED(CONFIG_UNACCEPTED_MEMORY)) {
640745e3ed8SKirill A. Shutemov 				efi_warn_once(
641745e3ed8SKirill A. Shutemov "The system has unaccepted memory,  but kernel does not support it\nConsider enabling CONFIG_UNACCEPTED_MEMORY\n");
642745e3ed8SKirill A. Shutemov 				continue;
643745e3ed8SKirill A. Shutemov 			}
644745e3ed8SKirill A. Shutemov 			e820_type = E820_TYPE_RAM;
645745e3ed8SKirill A. Shutemov 			process_unaccepted_memory(d->phys_addr,
646745e3ed8SKirill A. Shutemov 						  d->phys_addr + PAGE_SIZE * d->num_pages);
647745e3ed8SKirill A. Shutemov 			break;
648c2d0b470SArd Biesheuvel 		default:
649c2d0b470SArd Biesheuvel 			continue;
650c2d0b470SArd Biesheuvel 		}
651c2d0b470SArd Biesheuvel 
652c2d0b470SArd Biesheuvel 		/* Merge adjacent mappings */
653c2d0b470SArd Biesheuvel 		if (prev && prev->type == e820_type &&
654c2d0b470SArd Biesheuvel 		    (prev->addr + prev->size) == d->phys_addr) {
655c2d0b470SArd Biesheuvel 			prev->size += d->num_pages << 12;
656c2d0b470SArd Biesheuvel 			continue;
657c2d0b470SArd Biesheuvel 		}
658c2d0b470SArd Biesheuvel 
659c2d0b470SArd Biesheuvel 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
660c2d0b470SArd Biesheuvel 			u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
661c2d0b470SArd Biesheuvel 				   sizeof(struct setup_data);
662c2d0b470SArd Biesheuvel 
663c2d0b470SArd Biesheuvel 			if (!e820ext || e820ext_size < need)
664c2d0b470SArd Biesheuvel 				return EFI_BUFFER_TOO_SMALL;
665c2d0b470SArd Biesheuvel 
666c2d0b470SArd Biesheuvel 			/* boot_params map full, switch to e820 extended */
667c2d0b470SArd Biesheuvel 			entry = (struct boot_e820_entry *)e820ext->data;
668c2d0b470SArd Biesheuvel 		}
669c2d0b470SArd Biesheuvel 
670c2d0b470SArd Biesheuvel 		entry->addr = d->phys_addr;
671c2d0b470SArd Biesheuvel 		entry->size = d->num_pages << PAGE_SHIFT;
672c2d0b470SArd Biesheuvel 		entry->type = e820_type;
673c2d0b470SArd Biesheuvel 		prev = entry++;
674c2d0b470SArd Biesheuvel 		nr_entries++;
675c2d0b470SArd Biesheuvel 	}
676c2d0b470SArd Biesheuvel 
677c2d0b470SArd Biesheuvel 	if (nr_entries > ARRAY_SIZE(params->e820_table)) {
678c2d0b470SArd Biesheuvel 		u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
679c2d0b470SArd Biesheuvel 
680c2d0b470SArd Biesheuvel 		add_e820ext(params, e820ext, nr_e820ext);
681c2d0b470SArd Biesheuvel 		nr_entries -= nr_e820ext;
682c2d0b470SArd Biesheuvel 	}
683c2d0b470SArd Biesheuvel 
684c2d0b470SArd Biesheuvel 	params->e820_entries = (u8)nr_entries;
685c2d0b470SArd Biesheuvel 
686c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
687c2d0b470SArd Biesheuvel }
688c2d0b470SArd Biesheuvel 
689c2d0b470SArd Biesheuvel static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
690c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
691c2d0b470SArd Biesheuvel {
692c2d0b470SArd Biesheuvel 	efi_status_t status;
693c2d0b470SArd Biesheuvel 	unsigned long size;
694c2d0b470SArd Biesheuvel 
695c2d0b470SArd Biesheuvel 	size = sizeof(struct setup_data) +
696c2d0b470SArd Biesheuvel 		sizeof(struct e820_entry) * nr_desc;
697c2d0b470SArd Biesheuvel 
698c2d0b470SArd Biesheuvel 	if (*e820ext) {
699c2d0b470SArd Biesheuvel 		efi_bs_call(free_pool, *e820ext);
700c2d0b470SArd Biesheuvel 		*e820ext = NULL;
701c2d0b470SArd Biesheuvel 		*e820ext_size = 0;
702c2d0b470SArd Biesheuvel 	}
703c2d0b470SArd Biesheuvel 
704c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
705c2d0b470SArd Biesheuvel 			     (void **)e820ext);
706c2d0b470SArd Biesheuvel 	if (status == EFI_SUCCESS)
707c2d0b470SArd Biesheuvel 		*e820ext_size = size;
708c2d0b470SArd Biesheuvel 
709c2d0b470SArd Biesheuvel 	return status;
710c2d0b470SArd Biesheuvel }
711c2d0b470SArd Biesheuvel 
712c2d0b470SArd Biesheuvel static efi_status_t allocate_e820(struct boot_params *params,
713c2d0b470SArd Biesheuvel 				  struct setup_data **e820ext,
714c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
715c2d0b470SArd Biesheuvel {
7162e9f46eeSKirill A. Shutemov 	struct efi_boot_memmap *map;
717c2d0b470SArd Biesheuvel 	efi_status_t status;
7182e9f46eeSKirill A. Shutemov 	__u32 nr_desc;
719c2d0b470SArd Biesheuvel 
7202e9f46eeSKirill A. Shutemov 	status = efi_get_memory_map(&map, false);
721c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
722c2d0b470SArd Biesheuvel 		return status;
7232e9f46eeSKirill A. Shutemov 
7242e9f46eeSKirill A. Shutemov 	nr_desc = map->map_size / map->desc_size;
7252e9f46eeSKirill A. Shutemov 	if (nr_desc > ARRAY_SIZE(params->e820_table) - EFI_MMAP_NR_SLACK_SLOTS) {
7262e9f46eeSKirill A. Shutemov 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table) +
7272e9f46eeSKirill A. Shutemov 				 EFI_MMAP_NR_SLACK_SLOTS;
7282e9f46eeSKirill A. Shutemov 
7292e9f46eeSKirill A. Shutemov 		status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
730c2d0b470SArd Biesheuvel 	}
731c2d0b470SArd Biesheuvel 
732745e3ed8SKirill A. Shutemov 	if (IS_ENABLED(CONFIG_UNACCEPTED_MEMORY) && status == EFI_SUCCESS)
733745e3ed8SKirill A. Shutemov 		status = allocate_unaccepted_bitmap(nr_desc, map);
734745e3ed8SKirill A. Shutemov 
7352e9f46eeSKirill A. Shutemov 	efi_bs_call(free_pool, map);
7362e9f46eeSKirill A. Shutemov 	return status;
737c2d0b470SArd Biesheuvel }
738c2d0b470SArd Biesheuvel 
739c2d0b470SArd Biesheuvel struct exit_boot_struct {
740c2d0b470SArd Biesheuvel 	struct boot_params	*boot_params;
741c2d0b470SArd Biesheuvel 	struct efi_info		*efi;
742c2d0b470SArd Biesheuvel };
743c2d0b470SArd Biesheuvel 
744c2d0b470SArd Biesheuvel static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
745c2d0b470SArd Biesheuvel 				   void *priv)
746c2d0b470SArd Biesheuvel {
747c2d0b470SArd Biesheuvel 	const char *signature;
748c2d0b470SArd Biesheuvel 	struct exit_boot_struct *p = priv;
749c2d0b470SArd Biesheuvel 
750c2d0b470SArd Biesheuvel 	signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
751c2d0b470SArd Biesheuvel 				   : EFI32_LOADER_SIGNATURE;
752c2d0b470SArd Biesheuvel 	memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
753c2d0b470SArd Biesheuvel 
754eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)efi_system_table,
755eed4e019SArvind Sankar 			  &p->efi->efi_systab, &p->efi->efi_systab_hi);
756eab31265SArd Biesheuvel 	p->efi->efi_memdesc_size	= map->desc_size;
757eab31265SArd Biesheuvel 	p->efi->efi_memdesc_version	= map->desc_ver;
758eab31265SArd Biesheuvel 	efi_set_u64_split((unsigned long)map->map,
759eed4e019SArvind Sankar 			  &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
760eab31265SArd Biesheuvel 	p->efi->efi_memmap_size		= map->map_size;
761c2d0b470SArd Biesheuvel 
762c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
763c2d0b470SArd Biesheuvel }
764c2d0b470SArd Biesheuvel 
765c2d0b470SArd Biesheuvel static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
766c2d0b470SArd Biesheuvel {
767c2d0b470SArd Biesheuvel 	struct setup_data *e820ext = NULL;
768c2d0b470SArd Biesheuvel 	__u32 e820ext_size = 0;
769c2d0b470SArd Biesheuvel 	efi_status_t status;
770c2d0b470SArd Biesheuvel 	struct exit_boot_struct priv;
771c2d0b470SArd Biesheuvel 
772c2d0b470SArd Biesheuvel 	priv.boot_params	= boot_params;
773c2d0b470SArd Biesheuvel 	priv.efi		= &boot_params->efi_info;
774c2d0b470SArd Biesheuvel 
775c2d0b470SArd Biesheuvel 	status = allocate_e820(boot_params, &e820ext, &e820ext_size);
776c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
777c2d0b470SArd Biesheuvel 		return status;
778c2d0b470SArd Biesheuvel 
779c2d0b470SArd Biesheuvel 	/* Might as well exit boot services now */
780eab31265SArd Biesheuvel 	status = efi_exit_boot_services(handle, &priv, exit_boot_func);
781c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
782c2d0b470SArd Biesheuvel 		return status;
783c2d0b470SArd Biesheuvel 
784c2d0b470SArd Biesheuvel 	/* Historic? */
785c2d0b470SArd Biesheuvel 	boot_params->alt_mem_k	= 32 * 1024;
786c2d0b470SArd Biesheuvel 
787c2d0b470SArd Biesheuvel 	status = setup_e820(boot_params, e820ext, e820ext_size);
788c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
789c2d0b470SArd Biesheuvel 		return status;
790c2d0b470SArd Biesheuvel 
791c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
792c2d0b470SArd Biesheuvel }
793c2d0b470SArd Biesheuvel 
794*31c77a50SArd Biesheuvel static bool have_unsupported_snp_features(void)
795*31c77a50SArd Biesheuvel {
796*31c77a50SArd Biesheuvel 	u64 unsupported;
797*31c77a50SArd Biesheuvel 
798*31c77a50SArd Biesheuvel 	unsupported = snp_get_unsupported_features(sev_get_status());
799*31c77a50SArd Biesheuvel 	if (unsupported) {
800*31c77a50SArd Biesheuvel 		efi_err("Unsupported SEV-SNP features detected: 0x%llx\n",
801*31c77a50SArd Biesheuvel 			unsupported);
802*31c77a50SArd Biesheuvel 		return true;
803*31c77a50SArd Biesheuvel 	}
804*31c77a50SArd Biesheuvel 	return false;
805*31c77a50SArd Biesheuvel }
806*31c77a50SArd Biesheuvel 
807d2d7a54fSArd Biesheuvel static void __noreturn enter_kernel(unsigned long kernel_addr,
808d2d7a54fSArd Biesheuvel 				    struct boot_params *boot_params)
809d2d7a54fSArd Biesheuvel {
810d2d7a54fSArd Biesheuvel 	/* enter decompressed kernel with boot_params pointer in RSI/ESI */
811d2d7a54fSArd Biesheuvel 	asm("jmp *%0"::"r"(kernel_addr), "S"(boot_params));
812d2d7a54fSArd Biesheuvel 
813d2d7a54fSArd Biesheuvel 	unreachable();
814d2d7a54fSArd Biesheuvel }
815d2d7a54fSArd Biesheuvel 
816c2d0b470SArd Biesheuvel /*
817d2d7a54fSArd Biesheuvel  * On success, this routine will jump to the relocated image directly and never
818d2d7a54fSArd Biesheuvel  * return.  On failure, it will exit to the firmware via efi_exit() instead of
819d2d7a54fSArd Biesheuvel  * returning.
820c2d0b470SArd Biesheuvel  */
821df9215f1SArd Biesheuvel void __noreturn efi_stub_entry(efi_handle_t handle,
822c2d0b470SArd Biesheuvel 			       efi_system_table_t *sys_table_arg,
823c2d0b470SArd Biesheuvel 			       struct boot_params *boot_params)
824c2d0b470SArd Biesheuvel {
82511078876SArd Biesheuvel 	efi_guid_t guid = EFI_MEMORY_ATTRIBUTE_PROTOCOL_GUID;
826c2d0b470SArd Biesheuvel 	unsigned long bzimage_addr = (unsigned long)startup_32;
827d5cdf4cfSArvind Sankar 	unsigned long buffer_start, buffer_end;
828c2d0b470SArd Biesheuvel 	struct setup_header *hdr = &boot_params->hdr;
829f4dc7fffSArd Biesheuvel 	const struct linux_efi_initrd *initrd = NULL;
830c2d0b470SArd Biesheuvel 	efi_status_t status;
831c2d0b470SArd Biesheuvel 
832ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
833c2d0b470SArd Biesheuvel 	/* Check if we were booted by the EFI firmware */
834ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
8353b8f44fcSArd Biesheuvel 		efi_exit(handle, EFI_INVALID_PARAMETER);
836c2d0b470SArd Biesheuvel 
837*31c77a50SArd Biesheuvel 	if (have_unsupported_snp_features())
838*31c77a50SArd Biesheuvel 		efi_exit(handle, EFI_UNSUPPORTED);
839*31c77a50SArd Biesheuvel 
84011078876SArd Biesheuvel 	if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES)) {
8413ba75c13SBaskov Evgeniy 		efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
8423ba75c13SBaskov Evgeniy 		if (efi_dxe_table &&
8433ba75c13SBaskov Evgeniy 		    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
8443ba75c13SBaskov Evgeniy 			efi_warn("Ignoring DXE services table: invalid signature\n");
8453ba75c13SBaskov Evgeniy 			efi_dxe_table = NULL;
8463ba75c13SBaskov Evgeniy 		}
84711078876SArd Biesheuvel 	}
84811078876SArd Biesheuvel 
84911078876SArd Biesheuvel 	/* grab the memory attributes protocol if it exists */
85011078876SArd Biesheuvel 	efi_bs_call(locate_protocol, &guid, NULL, (void **)&memattr);
8513ba75c13SBaskov Evgeniy 
852cb1c9e02SArd Biesheuvel 	status = efi_setup_5level_paging();
853cb1c9e02SArd Biesheuvel 	if (status != EFI_SUCCESS) {
854cb1c9e02SArd Biesheuvel 		efi_err("efi_setup_5level_paging() failed!\n");
855cb1c9e02SArd Biesheuvel 		goto fail;
856cb1c9e02SArd Biesheuvel 	}
857cb1c9e02SArd Biesheuvel 
858c2d0b470SArd Biesheuvel 	/*
859d5cdf4cfSArvind Sankar 	 * If the kernel isn't already loaded at a suitable address,
860d5cdf4cfSArvind Sankar 	 * relocate it.
861d5cdf4cfSArvind Sankar 	 *
862d5cdf4cfSArvind Sankar 	 * It must be loaded above LOAD_PHYSICAL_ADDR.
863d5cdf4cfSArvind Sankar 	 *
864d5cdf4cfSArvind Sankar 	 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
865d5cdf4cfSArvind Sankar 	 * is defined as the macro MAXMEM, but unfortunately that is not a
866d5cdf4cfSArvind Sankar 	 * compile-time constant if 5-level paging is configured, so we instead
867d5cdf4cfSArvind Sankar 	 * define our own macro for use here.
868d5cdf4cfSArvind Sankar 	 *
869d5cdf4cfSArvind Sankar 	 * For 32-bit, the maximum address is complicated to figure out, for
870d5cdf4cfSArvind Sankar 	 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
871d5cdf4cfSArvind Sankar 	 * KASLR uses.
872d5cdf4cfSArvind Sankar 	 *
87321cb9b41SArvind Sankar 	 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
87421cb9b41SArvind Sankar 	 * loaded by LoadImage, but rather by a bootloader that called the
87521cb9b41SArvind Sankar 	 * handover entry. The reason we must always relocate in this case is
87621cb9b41SArvind Sankar 	 * to handle the case of systemd-boot booting a unified kernel image,
87721cb9b41SArvind Sankar 	 * which is a PE executable that contains the bzImage and an initrd as
87821cb9b41SArvind Sankar 	 * COFF sections. The initrd section is placed after the bzImage
87921cb9b41SArvind Sankar 	 * without ensuring that there are at least init_size bytes available
88021cb9b41SArvind Sankar 	 * for the bzImage, and thus the compressed kernel's startup code may
88121cb9b41SArvind Sankar 	 * overwrite the initrd unless it is moved out of the way.
882c2d0b470SArd Biesheuvel 	 */
883d5cdf4cfSArvind Sankar 
884d5cdf4cfSArvind Sankar 	buffer_start = ALIGN(bzimage_addr - image_offset,
885d5cdf4cfSArvind Sankar 			     hdr->kernel_alignment);
886d5cdf4cfSArvind Sankar 	buffer_end = buffer_start + hdr->init_size;
887d5cdf4cfSArvind Sankar 
888d5cdf4cfSArvind Sankar 	if ((buffer_start < LOAD_PHYSICAL_ADDR)				     ||
889d5cdf4cfSArvind Sankar 	    (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE)    ||
890d5cdf4cfSArvind Sankar 	    (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
89121cb9b41SArvind Sankar 	    (image_offset == 0)) {
892688eb282SArvind Sankar 		extern char _bss[];
893688eb282SArvind Sankar 
894c2d0b470SArd Biesheuvel 		status = efi_relocate_kernel(&bzimage_addr,
895688eb282SArvind Sankar 					     (unsigned long)_bss - bzimage_addr,
896688eb282SArvind Sankar 					     hdr->init_size,
897c2d0b470SArd Biesheuvel 					     hdr->pref_address,
898c2d0b470SArd Biesheuvel 					     hdr->kernel_alignment,
899c2d0b470SArd Biesheuvel 					     LOAD_PHYSICAL_ADDR);
900c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
90136bdd0a7SArvind Sankar 			efi_err("efi_relocate_kernel() failed!\n");
902c2d0b470SArd Biesheuvel 			goto fail;
903c2d0b470SArd Biesheuvel 		}
9041887c9b6SArvind Sankar 		/*
9051887c9b6SArvind Sankar 		 * Now that we've copied the kernel elsewhere, we no longer
9061887c9b6SArvind Sankar 		 * have a set up block before startup_32(), so reset image_offset
9071887c9b6SArvind Sankar 		 * to zero in case it was set earlier.
9081887c9b6SArvind Sankar 		 */
9091887c9b6SArvind Sankar 		image_offset = 0;
910c2d0b470SArd Biesheuvel 	}
911c2d0b470SArd Biesheuvel 
9127dde67f2SArvind Sankar #ifdef CONFIG_CMDLINE_BOOL
913055042beSArvind Sankar 	status = efi_parse_options(CONFIG_CMDLINE);
914055042beSArvind Sankar 	if (status != EFI_SUCCESS) {
915055042beSArvind Sankar 		efi_err("Failed to parse options\n");
916055042beSArvind Sankar 		goto fail;
917055042beSArvind Sankar 	}
9187dde67f2SArvind Sankar #endif
9197dde67f2SArvind Sankar 	if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
9207dde67f2SArvind Sankar 		unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
921c2d0b470SArd Biesheuvel 					       ((u64)boot_params->ext_cmd_line_ptr << 32));
922055042beSArvind Sankar 		status = efi_parse_options((char *)cmdline_paddr);
923055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
924055042beSArvind Sankar 			efi_err("Failed to parse options\n");
925055042beSArvind Sankar 			goto fail;
926055042beSArvind Sankar 		}
9277dde67f2SArvind Sankar 	}
928c2d0b470SArd Biesheuvel 
929c2d0b470SArd Biesheuvel 	/*
930987053a3SArvind Sankar 	 * At this point, an initrd may already have been loaded by the
931987053a3SArvind Sankar 	 * bootloader and passed via bootparams. We permit an initrd loaded
932987053a3SArvind Sankar 	 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
933987053a3SArvind Sankar 	 *
934987053a3SArvind Sankar 	 * If the device path is not present, any command-line initrd=
935987053a3SArvind Sankar 	 * arguments will be processed only if image is not NULL, which will be
936987053a3SArvind Sankar 	 * the case only if we were loaded via the PE entry point.
937ec93fc37SArd Biesheuvel 	 */
938f4dc7fffSArd Biesheuvel 	status = efi_load_initrd(image, hdr->initrd_addr_max, ULONG_MAX,
939f4dc7fffSArd Biesheuvel 				 &initrd);
94020287d56SArd Biesheuvel 	if (status != EFI_SUCCESS)
941ec93fc37SArd Biesheuvel 		goto fail;
942f4dc7fffSArd Biesheuvel 	if (initrd && initrd->size > 0) {
943f4dc7fffSArd Biesheuvel 		efi_set_u64_split(initrd->base, &hdr->ramdisk_image,
944eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_image);
945f4dc7fffSArd Biesheuvel 		efi_set_u64_split(initrd->size, &hdr->ramdisk_size,
946eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_size);
94779d3219dSArd Biesheuvel 	}
948ec93fc37SArd Biesheuvel 
949f4dc7fffSArd Biesheuvel 
950ec93fc37SArd Biesheuvel 	/*
951c2d0b470SArd Biesheuvel 	 * If the boot loader gave us a value for secure_boot then we use that,
952c2d0b470SArd Biesheuvel 	 * otherwise we ask the BIOS.
953c2d0b470SArd Biesheuvel 	 */
954c2d0b470SArd Biesheuvel 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
955c2d0b470SArd Biesheuvel 		boot_params->secure_boot = efi_get_secureboot();
956c2d0b470SArd Biesheuvel 
957c2d0b470SArd Biesheuvel 	/* Ask the firmware to clear memory on unclean shutdown */
958c2d0b470SArd Biesheuvel 	efi_enable_reset_attack_mitigation();
959c2d0b470SArd Biesheuvel 
960c2d0b470SArd Biesheuvel 	efi_random_get_seed();
961c2d0b470SArd Biesheuvel 
962c2d0b470SArd Biesheuvel 	efi_retrieve_tpm2_eventlog();
963c2d0b470SArd Biesheuvel 
964c2d0b470SArd Biesheuvel 	setup_graphics(boot_params);
965c2d0b470SArd Biesheuvel 
966c2d0b470SArd Biesheuvel 	setup_efi_pci(boot_params);
967c2d0b470SArd Biesheuvel 
96882e0d6d7SBaskov Evgeniy 	setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start);
969c2d0b470SArd Biesheuvel 
970c0461bd1SDionna Glaze 	setup_unaccepted_memory();
971c0461bd1SDionna Glaze 
972c2d0b470SArd Biesheuvel 	status = exit_boot(boot_params, handle);
973c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
97436bdd0a7SArvind Sankar 		efi_err("exit_boot() failed!\n");
975c2d0b470SArd Biesheuvel 		goto fail;
976c2d0b470SArd Biesheuvel 	}
977c2d0b470SArd Biesheuvel 
978cb1c9e02SArd Biesheuvel 	efi_5level_switch();
979cb1c9e02SArd Biesheuvel 
980d2d7a54fSArd Biesheuvel 	if (IS_ENABLED(CONFIG_X86_64))
981d2d7a54fSArd Biesheuvel 		bzimage_addr += startup_64 - startup_32;
982d2d7a54fSArd Biesheuvel 
983d2d7a54fSArd Biesheuvel 	enter_kernel(bzimage_addr, boot_params);
984c2d0b470SArd Biesheuvel fail:
985df9215f1SArd Biesheuvel 	efi_err("efi_stub_entry() failed!\n");
986c2d0b470SArd Biesheuvel 
9873b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
988c2d0b470SArd Biesheuvel }
989df9215f1SArd Biesheuvel 
990df9215f1SArd Biesheuvel #ifdef CONFIG_EFI_HANDOVER_PROTOCOL
991d7156b98SArd Biesheuvel void efi_handover_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
992d7156b98SArd Biesheuvel 			struct boot_params *boot_params)
993d7156b98SArd Biesheuvel {
994d7156b98SArd Biesheuvel 	extern char _bss[], _ebss[];
995d7156b98SArd Biesheuvel 
996d7156b98SArd Biesheuvel 	memset(_bss, 0, _ebss - _bss);
997d7156b98SArd Biesheuvel 	efi_stub_entry(handle, sys_table_arg, boot_params);
998d7156b98SArd Biesheuvel }
999d7156b98SArd Biesheuvel 
1000df9215f1SArd Biesheuvel #ifndef CONFIG_EFI_MIXED
1001d7156b98SArd Biesheuvel extern __alias(efi_handover_entry)
1002df9215f1SArd Biesheuvel void efi32_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
1003df9215f1SArd Biesheuvel 		      struct boot_params *boot_params);
1004df9215f1SArd Biesheuvel 
1005d7156b98SArd Biesheuvel extern __alias(efi_handover_entry)
1006df9215f1SArd Biesheuvel void efi64_stub_entry(efi_handle_t handle, efi_system_table_t *sys_table_arg,
1007df9215f1SArd Biesheuvel 		      struct boot_params *boot_params);
1008df9215f1SArd Biesheuvel #endif
1009df9215f1SArd Biesheuvel #endif
1010