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>
18c2d0b470SArd Biesheuvel 
19c2d0b470SArd Biesheuvel #include "efistub.h"
20c2d0b470SArd Biesheuvel 
21d5cdf4cfSArvind Sankar /* Maximum physical address for 64-bit kernel with 4-level paging */
22d5cdf4cfSArvind Sankar #define MAXMEM_X86_64_4LEVEL (1ull << 46)
23d5cdf4cfSArvind Sankar 
24ccc27ae7SArd Biesheuvel const efi_system_table_t *efi_system_table;
253ba75c13SBaskov Evgeniy const efi_dxe_services_table_t *efi_dxe_table;
261887c9b6SArvind Sankar extern u32 image_offset;
27987053a3SArvind Sankar static efi_loaded_image_t *image = NULL;
28c2d0b470SArd Biesheuvel 
29c2d0b470SArd Biesheuvel static efi_status_t
30c2d0b470SArd Biesheuvel preserve_pci_rom_image(efi_pci_io_protocol_t *pci, struct pci_setup_rom **__rom)
31c2d0b470SArd Biesheuvel {
32c2d0b470SArd Biesheuvel 	struct pci_setup_rom *rom = NULL;
33c2d0b470SArd Biesheuvel 	efi_status_t status;
34c2d0b470SArd Biesheuvel 	unsigned long size;
35c2d0b470SArd Biesheuvel 	uint64_t romsize;
36c2d0b470SArd Biesheuvel 	void *romimage;
37c2d0b470SArd Biesheuvel 
38c2d0b470SArd Biesheuvel 	/*
39c2d0b470SArd Biesheuvel 	 * Some firmware images contain EFI function pointers at the place where
40c2d0b470SArd Biesheuvel 	 * the romimage and romsize fields are supposed to be. Typically the EFI
41c2d0b470SArd Biesheuvel 	 * code is mapped at high addresses, translating to an unrealistically
42c2d0b470SArd Biesheuvel 	 * large romsize. The UEFI spec limits the size of option ROMs to 16
43c2d0b470SArd Biesheuvel 	 * MiB so we reject any ROMs over 16 MiB in size to catch this.
44c2d0b470SArd Biesheuvel 	 */
45c2d0b470SArd Biesheuvel 	romimage = efi_table_attr(pci, romimage);
46c2d0b470SArd Biesheuvel 	romsize = efi_table_attr(pci, romsize);
47c2d0b470SArd Biesheuvel 	if (!romimage || !romsize || romsize > SZ_16M)
48c2d0b470SArd Biesheuvel 		return EFI_INVALID_PARAMETER;
49c2d0b470SArd Biesheuvel 
50c2d0b470SArd Biesheuvel 	size = romsize + sizeof(*rom);
51c2d0b470SArd Biesheuvel 
52c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
53c2d0b470SArd Biesheuvel 			     (void **)&rom);
54c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
5536bdd0a7SArvind Sankar 		efi_err("Failed to allocate memory for 'rom'\n");
56c2d0b470SArd Biesheuvel 		return status;
57c2d0b470SArd Biesheuvel 	}
58c2d0b470SArd Biesheuvel 
59c2d0b470SArd Biesheuvel 	memset(rom, 0, sizeof(*rom));
60c2d0b470SArd Biesheuvel 
61c2d0b470SArd Biesheuvel 	rom->data.type	= SETUP_PCI;
62c2d0b470SArd Biesheuvel 	rom->data.len	= size - sizeof(struct setup_data);
63c2d0b470SArd Biesheuvel 	rom->data.next	= 0;
64c2d0b470SArd Biesheuvel 	rom->pcilen	= pci->romsize;
65c2d0b470SArd Biesheuvel 	*__rom = rom;
66c2d0b470SArd Biesheuvel 
67c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
68c2d0b470SArd Biesheuvel 				PCI_VENDOR_ID, 1, &rom->vendor);
69c2d0b470SArd Biesheuvel 
70c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
7136bdd0a7SArvind Sankar 		efi_err("Failed to read rom->vendor\n");
72c2d0b470SArd Biesheuvel 		goto free_struct;
73c2d0b470SArd Biesheuvel 	}
74c2d0b470SArd Biesheuvel 
75c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, pci.read, EfiPciIoWidthUint16,
76c2d0b470SArd Biesheuvel 				PCI_DEVICE_ID, 1, &rom->devid);
77c2d0b470SArd Biesheuvel 
78c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
7936bdd0a7SArvind Sankar 		efi_err("Failed to read rom->devid\n");
80c2d0b470SArd Biesheuvel 		goto free_struct;
81c2d0b470SArd Biesheuvel 	}
82c2d0b470SArd Biesheuvel 
83c2d0b470SArd Biesheuvel 	status = efi_call_proto(pci, get_location, &rom->segment, &rom->bus,
84c2d0b470SArd Biesheuvel 				&rom->device, &rom->function);
85c2d0b470SArd Biesheuvel 
86c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
87c2d0b470SArd Biesheuvel 		goto free_struct;
88c2d0b470SArd Biesheuvel 
89c2d0b470SArd Biesheuvel 	memcpy(rom->romdata, romimage, romsize);
90c2d0b470SArd Biesheuvel 	return status;
91c2d0b470SArd Biesheuvel 
92c2d0b470SArd Biesheuvel free_struct:
93c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, rom);
94c2d0b470SArd Biesheuvel 	return status;
95c2d0b470SArd Biesheuvel }
96c2d0b470SArd Biesheuvel 
97c2d0b470SArd Biesheuvel /*
98c2d0b470SArd Biesheuvel  * There's no way to return an informative status from this function,
99c2d0b470SArd Biesheuvel  * because any analysis (and printing of error messages) needs to be
100c2d0b470SArd Biesheuvel  * done directly at the EFI function call-site.
101c2d0b470SArd Biesheuvel  *
102c2d0b470SArd Biesheuvel  * For example, EFI_INVALID_PARAMETER could indicate a bug or maybe we
103c2d0b470SArd Biesheuvel  * just didn't find any PCI devices, but there's no way to tell outside
104c2d0b470SArd Biesheuvel  * the context of the call.
105c2d0b470SArd Biesheuvel  */
106c2d0b470SArd Biesheuvel static void setup_efi_pci(struct boot_params *params)
107c2d0b470SArd Biesheuvel {
108c2d0b470SArd Biesheuvel 	efi_status_t status;
109c2d0b470SArd Biesheuvel 	void **pci_handle = NULL;
110c2d0b470SArd Biesheuvel 	efi_guid_t pci_proto = EFI_PCI_IO_PROTOCOL_GUID;
111c2d0b470SArd Biesheuvel 	unsigned long size = 0;
112c2d0b470SArd Biesheuvel 	struct setup_data *data;
113c2d0b470SArd Biesheuvel 	efi_handle_t h;
114c2d0b470SArd Biesheuvel 	int i;
115c2d0b470SArd Biesheuvel 
116c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
117c2d0b470SArd Biesheuvel 			     &pci_proto, NULL, &size, pci_handle);
118c2d0b470SArd Biesheuvel 
119c2d0b470SArd Biesheuvel 	if (status == EFI_BUFFER_TOO_SMALL) {
120c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
121c2d0b470SArd Biesheuvel 				     (void **)&pci_handle);
122c2d0b470SArd Biesheuvel 
123c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
12436bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'pci_handle'\n");
125c2d0b470SArd Biesheuvel 			return;
126c2d0b470SArd Biesheuvel 		}
127c2d0b470SArd Biesheuvel 
128c2d0b470SArd Biesheuvel 		status = efi_bs_call(locate_handle, EFI_LOCATE_BY_PROTOCOL,
129c2d0b470SArd Biesheuvel 				     &pci_proto, NULL, &size, pci_handle);
130c2d0b470SArd Biesheuvel 	}
131c2d0b470SArd Biesheuvel 
132c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
133c2d0b470SArd Biesheuvel 		goto free_handle;
134c2d0b470SArd Biesheuvel 
135c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
136c2d0b470SArd Biesheuvel 
137c2d0b470SArd Biesheuvel 	while (data && data->next)
138c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
139c2d0b470SArd Biesheuvel 
140c2d0b470SArd Biesheuvel 	for_each_efi_handle(h, pci_handle, size, i) {
141c2d0b470SArd Biesheuvel 		efi_pci_io_protocol_t *pci = NULL;
142c2d0b470SArd Biesheuvel 		struct pci_setup_rom *rom;
143c2d0b470SArd Biesheuvel 
144c2d0b470SArd Biesheuvel 		status = efi_bs_call(handle_protocol, h, &pci_proto,
145c2d0b470SArd Biesheuvel 				     (void **)&pci);
146c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS || !pci)
147c2d0b470SArd Biesheuvel 			continue;
148c2d0b470SArd Biesheuvel 
149c2d0b470SArd Biesheuvel 		status = preserve_pci_rom_image(pci, &rom);
150c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
151c2d0b470SArd Biesheuvel 			continue;
152c2d0b470SArd Biesheuvel 
153c2d0b470SArd Biesheuvel 		if (data)
154c2d0b470SArd Biesheuvel 			data->next = (unsigned long)rom;
155c2d0b470SArd Biesheuvel 		else
156c2d0b470SArd Biesheuvel 			params->hdr.setup_data = (unsigned long)rom;
157c2d0b470SArd Biesheuvel 
158c2d0b470SArd Biesheuvel 		data = (struct setup_data *)rom;
159c2d0b470SArd Biesheuvel 	}
160c2d0b470SArd Biesheuvel 
161c2d0b470SArd Biesheuvel free_handle:
162c2d0b470SArd Biesheuvel 	efi_bs_call(free_pool, pci_handle);
163c2d0b470SArd Biesheuvel }
164c2d0b470SArd Biesheuvel 
165c2d0b470SArd Biesheuvel static void retrieve_apple_device_properties(struct boot_params *boot_params)
166c2d0b470SArd Biesheuvel {
167c2d0b470SArd Biesheuvel 	efi_guid_t guid = APPLE_PROPERTIES_PROTOCOL_GUID;
168c2d0b470SArd Biesheuvel 	struct setup_data *data, *new;
169c2d0b470SArd Biesheuvel 	efi_status_t status;
170c2d0b470SArd Biesheuvel 	u32 size = 0;
171c2d0b470SArd Biesheuvel 	apple_properties_protocol_t *p;
172c2d0b470SArd Biesheuvel 
173c2d0b470SArd Biesheuvel 	status = efi_bs_call(locate_protocol, &guid, NULL, (void **)&p);
174c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
175c2d0b470SArd Biesheuvel 		return;
176c2d0b470SArd Biesheuvel 
177c2d0b470SArd Biesheuvel 	if (efi_table_attr(p, version) != 0x10000) {
17836bdd0a7SArvind Sankar 		efi_err("Unsupported properties proto version\n");
179c2d0b470SArd Biesheuvel 		return;
180c2d0b470SArd Biesheuvel 	}
181c2d0b470SArd Biesheuvel 
182c2d0b470SArd Biesheuvel 	efi_call_proto(p, get_all, NULL, &size);
183c2d0b470SArd Biesheuvel 	if (!size)
184c2d0b470SArd Biesheuvel 		return;
185c2d0b470SArd Biesheuvel 
186c2d0b470SArd Biesheuvel 	do {
187c2d0b470SArd Biesheuvel 		status = efi_bs_call(allocate_pool, EFI_LOADER_DATA,
188c2d0b470SArd Biesheuvel 				     size + sizeof(struct setup_data),
189c2d0b470SArd Biesheuvel 				     (void **)&new);
190c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
19136bdd0a7SArvind Sankar 			efi_err("Failed to allocate memory for 'properties'\n");
192c2d0b470SArd Biesheuvel 			return;
193c2d0b470SArd Biesheuvel 		}
194c2d0b470SArd Biesheuvel 
195c2d0b470SArd Biesheuvel 		status = efi_call_proto(p, get_all, new->data, &size);
196c2d0b470SArd Biesheuvel 
197c2d0b470SArd Biesheuvel 		if (status == EFI_BUFFER_TOO_SMALL)
198c2d0b470SArd Biesheuvel 			efi_bs_call(free_pool, new);
199c2d0b470SArd Biesheuvel 	} while (status == EFI_BUFFER_TOO_SMALL);
200c2d0b470SArd Biesheuvel 
201c2d0b470SArd Biesheuvel 	new->type = SETUP_APPLE_PROPERTIES;
202c2d0b470SArd Biesheuvel 	new->len  = size;
203c2d0b470SArd Biesheuvel 	new->next = 0;
204c2d0b470SArd Biesheuvel 
205c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)boot_params->hdr.setup_data;
206c2d0b470SArd Biesheuvel 	if (!data) {
207c2d0b470SArd Biesheuvel 		boot_params->hdr.setup_data = (unsigned long)new;
208c2d0b470SArd Biesheuvel 	} else {
209c2d0b470SArd Biesheuvel 		while (data->next)
210c2d0b470SArd Biesheuvel 			data = (struct setup_data *)(unsigned long)data->next;
211c2d0b470SArd Biesheuvel 		data->next = (unsigned long)new;
212c2d0b470SArd Biesheuvel 	}
213c2d0b470SArd Biesheuvel }
214c2d0b470SArd Biesheuvel 
21582e0d6d7SBaskov Evgeniy static void
21682e0d6d7SBaskov Evgeniy adjust_memory_range_protection(unsigned long start, unsigned long size)
21782e0d6d7SBaskov Evgeniy {
21882e0d6d7SBaskov Evgeniy 	efi_status_t status;
21982e0d6d7SBaskov Evgeniy 	efi_gcd_memory_space_desc_t desc;
22082e0d6d7SBaskov Evgeniy 	unsigned long end, next;
22182e0d6d7SBaskov Evgeniy 	unsigned long rounded_start, rounded_end;
22282e0d6d7SBaskov Evgeniy 	unsigned long unprotect_start, unprotect_size;
22382e0d6d7SBaskov Evgeniy 	int has_system_memory = 0;
22482e0d6d7SBaskov Evgeniy 
22582e0d6d7SBaskov Evgeniy 	if (efi_dxe_table == NULL)
22682e0d6d7SBaskov Evgeniy 		return;
22782e0d6d7SBaskov Evgeniy 
22882e0d6d7SBaskov Evgeniy 	rounded_start = rounddown(start, EFI_PAGE_SIZE);
22982e0d6d7SBaskov Evgeniy 	rounded_end = roundup(start + size, EFI_PAGE_SIZE);
23082e0d6d7SBaskov Evgeniy 
23182e0d6d7SBaskov Evgeniy 	/*
23282e0d6d7SBaskov Evgeniy 	 * Don't modify memory region attributes, they are
23382e0d6d7SBaskov Evgeniy 	 * already suitable, to lower the possibility to
23482e0d6d7SBaskov Evgeniy 	 * encounter firmware bugs.
23582e0d6d7SBaskov Evgeniy 	 */
23682e0d6d7SBaskov Evgeniy 
23782e0d6d7SBaskov Evgeniy 	for (end = start + size; start < end; start = next) {
23882e0d6d7SBaskov Evgeniy 
23982e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(get_memory_space_descriptor, start, &desc);
24082e0d6d7SBaskov Evgeniy 
24182e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS)
24282e0d6d7SBaskov Evgeniy 			return;
24382e0d6d7SBaskov Evgeniy 
24482e0d6d7SBaskov Evgeniy 		next = desc.base_address + desc.length;
24582e0d6d7SBaskov Evgeniy 
24682e0d6d7SBaskov Evgeniy 		/*
24782e0d6d7SBaskov Evgeniy 		 * Only system memory is suitable for trampoline/kernel image placement,
24882e0d6d7SBaskov Evgeniy 		 * so only this type of memory needs its attributes to be modified.
24982e0d6d7SBaskov Evgeniy 		 */
25082e0d6d7SBaskov Evgeniy 
25182e0d6d7SBaskov Evgeniy 		if (desc.gcd_memory_type != EfiGcdMemoryTypeSystemMemory ||
25282e0d6d7SBaskov Evgeniy 		    (desc.attributes & (EFI_MEMORY_RO | EFI_MEMORY_XP)) == 0)
25382e0d6d7SBaskov Evgeniy 			continue;
25482e0d6d7SBaskov Evgeniy 
25582e0d6d7SBaskov Evgeniy 		unprotect_start = max(rounded_start, (unsigned long)desc.base_address);
25682e0d6d7SBaskov Evgeniy 		unprotect_size = min(rounded_end, next) - unprotect_start;
25782e0d6d7SBaskov Evgeniy 
25882e0d6d7SBaskov Evgeniy 		status = efi_dxe_call(set_memory_space_attributes,
25982e0d6d7SBaskov Evgeniy 				      unprotect_start, unprotect_size,
26082e0d6d7SBaskov Evgeniy 				      EFI_MEMORY_WB);
26182e0d6d7SBaskov Evgeniy 
26282e0d6d7SBaskov Evgeniy 		if (status != EFI_SUCCESS) {
263*31f1a0edSArd Biesheuvel 			efi_warn("Unable to unprotect memory range [%08lx,%08lx]: %lx\n",
26482e0d6d7SBaskov Evgeniy 				 unprotect_start,
26582e0d6d7SBaskov Evgeniy 				 unprotect_start + unprotect_size,
266*31f1a0edSArd Biesheuvel 				 status);
26782e0d6d7SBaskov Evgeniy 		}
26882e0d6d7SBaskov Evgeniy 	}
26982e0d6d7SBaskov Evgeniy }
27082e0d6d7SBaskov Evgeniy 
27182e0d6d7SBaskov Evgeniy /*
27282e0d6d7SBaskov Evgeniy  * Trampoline takes 2 pages and can be loaded in first megabyte of memory
27382e0d6d7SBaskov Evgeniy  * with its end placed between 128k and 640k where BIOS might start.
27482e0d6d7SBaskov Evgeniy  * (see arch/x86/boot/compressed/pgtable_64.c)
27582e0d6d7SBaskov Evgeniy  *
27682e0d6d7SBaskov Evgeniy  * We cannot find exact trampoline placement since memory map
27782e0d6d7SBaskov Evgeniy  * can be modified by UEFI, and it can alter the computed address.
27882e0d6d7SBaskov Evgeniy  */
27982e0d6d7SBaskov Evgeniy 
28082e0d6d7SBaskov Evgeniy #define TRAMPOLINE_PLACEMENT_BASE ((128 - 8)*1024)
28182e0d6d7SBaskov Evgeniy #define TRAMPOLINE_PLACEMENT_SIZE (640*1024 - (128 - 8)*1024)
28282e0d6d7SBaskov Evgeniy 
28382e0d6d7SBaskov Evgeniy void startup_32(struct boot_params *boot_params);
28482e0d6d7SBaskov Evgeniy 
28582e0d6d7SBaskov Evgeniy static void
28682e0d6d7SBaskov Evgeniy setup_memory_protection(unsigned long image_base, unsigned long image_size)
28782e0d6d7SBaskov Evgeniy {
28882e0d6d7SBaskov Evgeniy 	/*
28982e0d6d7SBaskov Evgeniy 	 * Allow execution of possible trampoline used
29082e0d6d7SBaskov Evgeniy 	 * for switching between 4- and 5-level page tables
29182e0d6d7SBaskov Evgeniy 	 * and relocated kernel image.
29282e0d6d7SBaskov Evgeniy 	 */
29382e0d6d7SBaskov Evgeniy 
29482e0d6d7SBaskov Evgeniy 	adjust_memory_range_protection(TRAMPOLINE_PLACEMENT_BASE,
29582e0d6d7SBaskov Evgeniy 				       TRAMPOLINE_PLACEMENT_SIZE);
29682e0d6d7SBaskov Evgeniy 
29782e0d6d7SBaskov Evgeniy #ifdef CONFIG_64BIT
29882e0d6d7SBaskov Evgeniy 	if (image_base != (unsigned long)startup_32)
29982e0d6d7SBaskov Evgeniy 		adjust_memory_range_protection(image_base, image_size);
30082e0d6d7SBaskov Evgeniy #else
30182e0d6d7SBaskov Evgeniy 	/*
30282e0d6d7SBaskov Evgeniy 	 * Clear protection flags on a whole range of possible
30382e0d6d7SBaskov Evgeniy 	 * addresses used for KASLR. We don't need to do that
30482e0d6d7SBaskov Evgeniy 	 * on x86_64, since KASLR/extraction is performed after
30582e0d6d7SBaskov Evgeniy 	 * dedicated identity page tables are built and we only
30682e0d6d7SBaskov Evgeniy 	 * need to remove possible protection on relocated image
30782e0d6d7SBaskov Evgeniy 	 * itself disregarding further relocations.
30882e0d6d7SBaskov Evgeniy 	 */
30982e0d6d7SBaskov Evgeniy 	adjust_memory_range_protection(LOAD_PHYSICAL_ADDR,
31082e0d6d7SBaskov Evgeniy 				       KERNEL_IMAGE_SIZE - LOAD_PHYSICAL_ADDR);
31182e0d6d7SBaskov Evgeniy #endif
31282e0d6d7SBaskov Evgeniy }
31382e0d6d7SBaskov Evgeniy 
314c2d0b470SArd Biesheuvel static const efi_char16_t apple[] = L"Apple";
315c2d0b470SArd Biesheuvel 
31682e0d6d7SBaskov Evgeniy static void setup_quirks(struct boot_params *boot_params,
31782e0d6d7SBaskov Evgeniy 			 unsigned long image_base,
31882e0d6d7SBaskov Evgeniy 			 unsigned long image_size)
319c2d0b470SArd Biesheuvel {
320c2d0b470SArd Biesheuvel 	efi_char16_t *fw_vendor = (efi_char16_t *)(unsigned long)
321ccc27ae7SArd Biesheuvel 		efi_table_attr(efi_system_table, fw_vendor);
322c2d0b470SArd Biesheuvel 
323c2d0b470SArd Biesheuvel 	if (!memcmp(fw_vendor, apple, sizeof(apple))) {
324c2d0b470SArd Biesheuvel 		if (IS_ENABLED(CONFIG_APPLE_PROPERTIES))
325c2d0b470SArd Biesheuvel 			retrieve_apple_device_properties(boot_params);
326c2d0b470SArd Biesheuvel 	}
32782e0d6d7SBaskov Evgeniy 
32882e0d6d7SBaskov Evgeniy 	if (IS_ENABLED(CONFIG_EFI_DXE_MEM_ATTRIBUTES))
32982e0d6d7SBaskov Evgeniy 		setup_memory_protection(image_base, image_size);
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 	image_offset = (void *)startup_32 - image_base;
4831887c9b6SArvind Sankar 
484019512f1SArvind Sankar 	status = efi_allocate_pages(sizeof(struct boot_params),
485019512f1SArvind Sankar 				    (unsigned long *)&boot_params, ULONG_MAX);
486c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
48736bdd0a7SArvind Sankar 		efi_err("Failed to allocate lowmem for boot params\n");
4883b8f44fcSArd Biesheuvel 		efi_exit(handle, status);
489c2d0b470SArd Biesheuvel 	}
490c2d0b470SArd Biesheuvel 
491019512f1SArvind Sankar 	memset(boot_params, 0x0, sizeof(struct boot_params));
492c2d0b470SArd Biesheuvel 
493c2d0b470SArd Biesheuvel 	hdr = &boot_params->hdr;
494c2d0b470SArd Biesheuvel 
49559476f80SArvind Sankar 	/* Copy the setup header from the second sector to boot_params */
49659476f80SArvind Sankar 	memcpy(&hdr->jump, image_base + 512,
49759476f80SArvind Sankar 	       sizeof(struct setup_header) - offsetof(struct setup_header, jump));
498c2d0b470SArd Biesheuvel 
499c2d0b470SArd Biesheuvel 	/*
500c2d0b470SArd Biesheuvel 	 * Fill out some of the header fields ourselves because the
501c2d0b470SArd Biesheuvel 	 * EFI firmware loader doesn't load the first sector.
502c2d0b470SArd Biesheuvel 	 */
503c2d0b470SArd Biesheuvel 	hdr->root_flags	= 1;
504c2d0b470SArd Biesheuvel 	hdr->vid_mode	= 0xffff;
505c2d0b470SArd Biesheuvel 	hdr->boot_flag	= 0xAA55;
506c2d0b470SArd Biesheuvel 
507c2d0b470SArd Biesheuvel 	hdr->type_of_loader = 0x21;
508c2d0b470SArd Biesheuvel 
509c2d0b470SArd Biesheuvel 	/* Convert unicode cmdline to ascii */
51027cd5511SArd Biesheuvel 	cmdline_ptr = efi_convert_cmdline(image, &options_size);
511c2d0b470SArd Biesheuvel 	if (!cmdline_ptr)
512c2d0b470SArd Biesheuvel 		goto fail;
513c2d0b470SArd Biesheuvel 
514eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)cmdline_ptr,
515eed4e019SArvind Sankar 			  &hdr->cmd_line_ptr, &boot_params->ext_cmd_line_ptr);
516c2d0b470SArd Biesheuvel 
517c2d0b470SArd Biesheuvel 	hdr->ramdisk_image = 0;
518c2d0b470SArd Biesheuvel 	hdr->ramdisk_size = 0;
519c2d0b470SArd Biesheuvel 
520ccc27ae7SArd Biesheuvel 	efi_stub_entry(handle, sys_table_arg, boot_params);
521c2d0b470SArd Biesheuvel 	/* not reached */
522c2d0b470SArd Biesheuvel 
523c2d0b470SArd Biesheuvel fail:
524019512f1SArvind Sankar 	efi_free(sizeof(struct boot_params), (unsigned long)boot_params);
525c2d0b470SArd Biesheuvel 
5263b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
527c2d0b470SArd Biesheuvel }
528c2d0b470SArd Biesheuvel 
529c2d0b470SArd Biesheuvel static void add_e820ext(struct boot_params *params,
530c2d0b470SArd Biesheuvel 			struct setup_data *e820ext, u32 nr_entries)
531c2d0b470SArd Biesheuvel {
532c2d0b470SArd Biesheuvel 	struct setup_data *data;
533c2d0b470SArd Biesheuvel 
534c2d0b470SArd Biesheuvel 	e820ext->type = SETUP_E820_EXT;
535c2d0b470SArd Biesheuvel 	e820ext->len  = nr_entries * sizeof(struct boot_e820_entry);
536c2d0b470SArd Biesheuvel 	e820ext->next = 0;
537c2d0b470SArd Biesheuvel 
538c2d0b470SArd Biesheuvel 	data = (struct setup_data *)(unsigned long)params->hdr.setup_data;
539c2d0b470SArd Biesheuvel 
540c2d0b470SArd Biesheuvel 	while (data && data->next)
541c2d0b470SArd Biesheuvel 		data = (struct setup_data *)(unsigned long)data->next;
542c2d0b470SArd Biesheuvel 
543c2d0b470SArd Biesheuvel 	if (data)
544c2d0b470SArd Biesheuvel 		data->next = (unsigned long)e820ext;
545c2d0b470SArd Biesheuvel 	else
546c2d0b470SArd Biesheuvel 		params->hdr.setup_data = (unsigned long)e820ext;
547c2d0b470SArd Biesheuvel }
548c2d0b470SArd Biesheuvel 
549c2d0b470SArd Biesheuvel static efi_status_t
550c2d0b470SArd Biesheuvel setup_e820(struct boot_params *params, struct setup_data *e820ext, u32 e820ext_size)
551c2d0b470SArd Biesheuvel {
552c2d0b470SArd Biesheuvel 	struct boot_e820_entry *entry = params->e820_table;
553c2d0b470SArd Biesheuvel 	struct efi_info *efi = &params->efi_info;
554c2d0b470SArd Biesheuvel 	struct boot_e820_entry *prev = NULL;
555c2d0b470SArd Biesheuvel 	u32 nr_entries;
556c2d0b470SArd Biesheuvel 	u32 nr_desc;
557c2d0b470SArd Biesheuvel 	int i;
558c2d0b470SArd Biesheuvel 
559c2d0b470SArd Biesheuvel 	nr_entries = 0;
560c2d0b470SArd Biesheuvel 	nr_desc = efi->efi_memmap_size / efi->efi_memdesc_size;
561c2d0b470SArd Biesheuvel 
562c2d0b470SArd Biesheuvel 	for (i = 0; i < nr_desc; i++) {
563c2d0b470SArd Biesheuvel 		efi_memory_desc_t *d;
564c2d0b470SArd Biesheuvel 		unsigned int e820_type = 0;
565c2d0b470SArd Biesheuvel 		unsigned long m = efi->efi_memmap;
566c2d0b470SArd Biesheuvel 
567c2d0b470SArd Biesheuvel #ifdef CONFIG_X86_64
568c2d0b470SArd Biesheuvel 		m |= (u64)efi->efi_memmap_hi << 32;
569c2d0b470SArd Biesheuvel #endif
570c2d0b470SArd Biesheuvel 
571c2d0b470SArd Biesheuvel 		d = efi_early_memdesc_ptr(m, efi->efi_memdesc_size, i);
572c2d0b470SArd Biesheuvel 		switch (d->type) {
573c2d0b470SArd Biesheuvel 		case EFI_RESERVED_TYPE:
574c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_CODE:
575c2d0b470SArd Biesheuvel 		case EFI_RUNTIME_SERVICES_DATA:
576c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO:
577c2d0b470SArd Biesheuvel 		case EFI_MEMORY_MAPPED_IO_PORT_SPACE:
578c2d0b470SArd Biesheuvel 		case EFI_PAL_CODE:
579c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_RESERVED;
580c2d0b470SArd Biesheuvel 			break;
581c2d0b470SArd Biesheuvel 
582c2d0b470SArd Biesheuvel 		case EFI_UNUSABLE_MEMORY:
583c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_UNUSABLE;
584c2d0b470SArd Biesheuvel 			break;
585c2d0b470SArd Biesheuvel 
586c2d0b470SArd Biesheuvel 		case EFI_ACPI_RECLAIM_MEMORY:
587c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_ACPI;
588c2d0b470SArd Biesheuvel 			break;
589c2d0b470SArd Biesheuvel 
590c2d0b470SArd Biesheuvel 		case EFI_LOADER_CODE:
591c2d0b470SArd Biesheuvel 		case EFI_LOADER_DATA:
592c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_CODE:
593c2d0b470SArd Biesheuvel 		case EFI_BOOT_SERVICES_DATA:
594c2d0b470SArd Biesheuvel 		case EFI_CONVENTIONAL_MEMORY:
595c2d0b470SArd Biesheuvel 			if (efi_soft_reserve_enabled() &&
596c2d0b470SArd Biesheuvel 			    (d->attribute & EFI_MEMORY_SP))
597c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_SOFT_RESERVED;
598c2d0b470SArd Biesheuvel 			else
599c2d0b470SArd Biesheuvel 				e820_type = E820_TYPE_RAM;
600c2d0b470SArd Biesheuvel 			break;
601c2d0b470SArd Biesheuvel 
602c2d0b470SArd Biesheuvel 		case EFI_ACPI_MEMORY_NVS:
603c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_NVS;
604c2d0b470SArd Biesheuvel 			break;
605c2d0b470SArd Biesheuvel 
606c2d0b470SArd Biesheuvel 		case EFI_PERSISTENT_MEMORY:
607c2d0b470SArd Biesheuvel 			e820_type = E820_TYPE_PMEM;
608c2d0b470SArd Biesheuvel 			break;
609c2d0b470SArd Biesheuvel 
610c2d0b470SArd Biesheuvel 		default:
611c2d0b470SArd Biesheuvel 			continue;
612c2d0b470SArd Biesheuvel 		}
613c2d0b470SArd Biesheuvel 
614c2d0b470SArd Biesheuvel 		/* Merge adjacent mappings */
615c2d0b470SArd Biesheuvel 		if (prev && prev->type == e820_type &&
616c2d0b470SArd Biesheuvel 		    (prev->addr + prev->size) == d->phys_addr) {
617c2d0b470SArd Biesheuvel 			prev->size += d->num_pages << 12;
618c2d0b470SArd Biesheuvel 			continue;
619c2d0b470SArd Biesheuvel 		}
620c2d0b470SArd Biesheuvel 
621c2d0b470SArd Biesheuvel 		if (nr_entries == ARRAY_SIZE(params->e820_table)) {
622c2d0b470SArd Biesheuvel 			u32 need = (nr_desc - i) * sizeof(struct e820_entry) +
623c2d0b470SArd Biesheuvel 				   sizeof(struct setup_data);
624c2d0b470SArd Biesheuvel 
625c2d0b470SArd Biesheuvel 			if (!e820ext || e820ext_size < need)
626c2d0b470SArd Biesheuvel 				return EFI_BUFFER_TOO_SMALL;
627c2d0b470SArd Biesheuvel 
628c2d0b470SArd Biesheuvel 			/* boot_params map full, switch to e820 extended */
629c2d0b470SArd Biesheuvel 			entry = (struct boot_e820_entry *)e820ext->data;
630c2d0b470SArd Biesheuvel 		}
631c2d0b470SArd Biesheuvel 
632c2d0b470SArd Biesheuvel 		entry->addr = d->phys_addr;
633c2d0b470SArd Biesheuvel 		entry->size = d->num_pages << PAGE_SHIFT;
634c2d0b470SArd Biesheuvel 		entry->type = e820_type;
635c2d0b470SArd Biesheuvel 		prev = entry++;
636c2d0b470SArd Biesheuvel 		nr_entries++;
637c2d0b470SArd Biesheuvel 	}
638c2d0b470SArd Biesheuvel 
639c2d0b470SArd Biesheuvel 	if (nr_entries > ARRAY_SIZE(params->e820_table)) {
640c2d0b470SArd Biesheuvel 		u32 nr_e820ext = nr_entries - ARRAY_SIZE(params->e820_table);
641c2d0b470SArd Biesheuvel 
642c2d0b470SArd Biesheuvel 		add_e820ext(params, e820ext, nr_e820ext);
643c2d0b470SArd Biesheuvel 		nr_entries -= nr_e820ext;
644c2d0b470SArd Biesheuvel 	}
645c2d0b470SArd Biesheuvel 
646c2d0b470SArd Biesheuvel 	params->e820_entries = (u8)nr_entries;
647c2d0b470SArd Biesheuvel 
648c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
649c2d0b470SArd Biesheuvel }
650c2d0b470SArd Biesheuvel 
651c2d0b470SArd Biesheuvel static efi_status_t alloc_e820ext(u32 nr_desc, struct setup_data **e820ext,
652c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
653c2d0b470SArd Biesheuvel {
654c2d0b470SArd Biesheuvel 	efi_status_t status;
655c2d0b470SArd Biesheuvel 	unsigned long size;
656c2d0b470SArd Biesheuvel 
657c2d0b470SArd Biesheuvel 	size = sizeof(struct setup_data) +
658c2d0b470SArd Biesheuvel 		sizeof(struct e820_entry) * nr_desc;
659c2d0b470SArd Biesheuvel 
660c2d0b470SArd Biesheuvel 	if (*e820ext) {
661c2d0b470SArd Biesheuvel 		efi_bs_call(free_pool, *e820ext);
662c2d0b470SArd Biesheuvel 		*e820ext = NULL;
663c2d0b470SArd Biesheuvel 		*e820ext_size = 0;
664c2d0b470SArd Biesheuvel 	}
665c2d0b470SArd Biesheuvel 
666c2d0b470SArd Biesheuvel 	status = efi_bs_call(allocate_pool, EFI_LOADER_DATA, size,
667c2d0b470SArd Biesheuvel 			     (void **)e820ext);
668c2d0b470SArd Biesheuvel 	if (status == EFI_SUCCESS)
669c2d0b470SArd Biesheuvel 		*e820ext_size = size;
670c2d0b470SArd Biesheuvel 
671c2d0b470SArd Biesheuvel 	return status;
672c2d0b470SArd Biesheuvel }
673c2d0b470SArd Biesheuvel 
674c2d0b470SArd Biesheuvel static efi_status_t allocate_e820(struct boot_params *params,
675c2d0b470SArd Biesheuvel 				  struct setup_data **e820ext,
676c2d0b470SArd Biesheuvel 				  u32 *e820ext_size)
677c2d0b470SArd Biesheuvel {
678fd626195SLenny Szubowicz 	unsigned long map_size, desc_size, map_key;
679c2d0b470SArd Biesheuvel 	efi_status_t status;
680fd626195SLenny Szubowicz 	__u32 nr_desc, desc_version;
681c2d0b470SArd Biesheuvel 
682fd626195SLenny Szubowicz 	/* Only need the size of the mem map and size of each mem descriptor */
683fd626195SLenny Szubowicz 	map_size = 0;
684fd626195SLenny Szubowicz 	status = efi_bs_call(get_memory_map, &map_size, NULL, &map_key,
685fd626195SLenny Szubowicz 			     &desc_size, &desc_version);
686fd626195SLenny Szubowicz 	if (status != EFI_BUFFER_TOO_SMALL)
687fd626195SLenny Szubowicz 		return (status != EFI_SUCCESS) ? status : EFI_UNSUPPORTED;
688c2d0b470SArd Biesheuvel 
689fd626195SLenny Szubowicz 	nr_desc = map_size / desc_size + EFI_MMAP_NR_SLACK_SLOTS;
690c2d0b470SArd Biesheuvel 
691c2d0b470SArd Biesheuvel 	if (nr_desc > ARRAY_SIZE(params->e820_table)) {
692c2d0b470SArd Biesheuvel 		u32 nr_e820ext = nr_desc - ARRAY_SIZE(params->e820_table);
693c2d0b470SArd Biesheuvel 
694c2d0b470SArd Biesheuvel 		status = alloc_e820ext(nr_e820ext, e820ext, e820ext_size);
695c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS)
696c2d0b470SArd Biesheuvel 			return status;
697c2d0b470SArd Biesheuvel 	}
698c2d0b470SArd Biesheuvel 
699c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
700c2d0b470SArd Biesheuvel }
701c2d0b470SArd Biesheuvel 
702c2d0b470SArd Biesheuvel struct exit_boot_struct {
703c2d0b470SArd Biesheuvel 	struct boot_params	*boot_params;
704c2d0b470SArd Biesheuvel 	struct efi_info		*efi;
705c2d0b470SArd Biesheuvel };
706c2d0b470SArd Biesheuvel 
707c2d0b470SArd Biesheuvel static efi_status_t exit_boot_func(struct efi_boot_memmap *map,
708c2d0b470SArd Biesheuvel 				   void *priv)
709c2d0b470SArd Biesheuvel {
710c2d0b470SArd Biesheuvel 	const char *signature;
711c2d0b470SArd Biesheuvel 	struct exit_boot_struct *p = priv;
712c2d0b470SArd Biesheuvel 
713c2d0b470SArd Biesheuvel 	signature = efi_is_64bit() ? EFI64_LOADER_SIGNATURE
714c2d0b470SArd Biesheuvel 				   : EFI32_LOADER_SIGNATURE;
715c2d0b470SArd Biesheuvel 	memcpy(&p->efi->efi_loader_signature, signature, sizeof(__u32));
716c2d0b470SArd Biesheuvel 
717eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)efi_system_table,
718eed4e019SArvind Sankar 			  &p->efi->efi_systab, &p->efi->efi_systab_hi);
719c2d0b470SArd Biesheuvel 	p->efi->efi_memdesc_size	= *map->desc_size;
720c2d0b470SArd Biesheuvel 	p->efi->efi_memdesc_version	= *map->desc_ver;
721eed4e019SArvind Sankar 	efi_set_u64_split((unsigned long)*map->map,
722eed4e019SArvind Sankar 			  &p->efi->efi_memmap, &p->efi->efi_memmap_hi);
723c2d0b470SArd Biesheuvel 	p->efi->efi_memmap_size		= *map->map_size;
724c2d0b470SArd Biesheuvel 
725c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
726c2d0b470SArd Biesheuvel }
727c2d0b470SArd Biesheuvel 
728c2d0b470SArd Biesheuvel static efi_status_t exit_boot(struct boot_params *boot_params, void *handle)
729c2d0b470SArd Biesheuvel {
730c2d0b470SArd Biesheuvel 	unsigned long map_sz, key, desc_size, buff_size;
731c2d0b470SArd Biesheuvel 	efi_memory_desc_t *mem_map;
732c2d0b470SArd Biesheuvel 	struct setup_data *e820ext = NULL;
733c2d0b470SArd Biesheuvel 	__u32 e820ext_size = 0;
734c2d0b470SArd Biesheuvel 	efi_status_t status;
735c2d0b470SArd Biesheuvel 	__u32 desc_version;
736c2d0b470SArd Biesheuvel 	struct efi_boot_memmap map;
737c2d0b470SArd Biesheuvel 	struct exit_boot_struct priv;
738c2d0b470SArd Biesheuvel 
739c2d0b470SArd Biesheuvel 	map.map			= &mem_map;
740c2d0b470SArd Biesheuvel 	map.map_size		= &map_sz;
741c2d0b470SArd Biesheuvel 	map.desc_size		= &desc_size;
742c2d0b470SArd Biesheuvel 	map.desc_ver		= &desc_version;
743c2d0b470SArd Biesheuvel 	map.key_ptr		= &key;
744c2d0b470SArd Biesheuvel 	map.buff_size		= &buff_size;
745c2d0b470SArd Biesheuvel 	priv.boot_params	= boot_params;
746c2d0b470SArd Biesheuvel 	priv.efi		= &boot_params->efi_info;
747c2d0b470SArd Biesheuvel 
748c2d0b470SArd Biesheuvel 	status = allocate_e820(boot_params, &e820ext, &e820ext_size);
749c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
750c2d0b470SArd Biesheuvel 		return status;
751c2d0b470SArd Biesheuvel 
752c2d0b470SArd Biesheuvel 	/* Might as well exit boot services now */
753c2d0b470SArd Biesheuvel 	status = efi_exit_boot_services(handle, &map, &priv, exit_boot_func);
754c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
755c2d0b470SArd Biesheuvel 		return status;
756c2d0b470SArd Biesheuvel 
757c2d0b470SArd Biesheuvel 	/* Historic? */
758c2d0b470SArd Biesheuvel 	boot_params->alt_mem_k	= 32 * 1024;
759c2d0b470SArd Biesheuvel 
760c2d0b470SArd Biesheuvel 	status = setup_e820(boot_params, e820ext, e820ext_size);
761c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS)
762c2d0b470SArd Biesheuvel 		return status;
763c2d0b470SArd Biesheuvel 
764c2d0b470SArd Biesheuvel 	return EFI_SUCCESS;
765c2d0b470SArd Biesheuvel }
766c2d0b470SArd Biesheuvel 
767c2d0b470SArd Biesheuvel /*
7688acf63efSArvind Sankar  * On success, we return the address of startup_32, which has potentially been
7698acf63efSArvind Sankar  * relocated by efi_relocate_kernel.
7708acf63efSArvind Sankar  * On failure, we exit to the firmware via efi_exit instead of returning.
771c2d0b470SArd Biesheuvel  */
7728acf63efSArvind Sankar unsigned long efi_main(efi_handle_t handle,
773c2d0b470SArd Biesheuvel 			     efi_system_table_t *sys_table_arg,
774c2d0b470SArd Biesheuvel 			     struct boot_params *boot_params)
775c2d0b470SArd Biesheuvel {
776c2d0b470SArd Biesheuvel 	unsigned long bzimage_addr = (unsigned long)startup_32;
777d5cdf4cfSArvind Sankar 	unsigned long buffer_start, buffer_end;
778c2d0b470SArd Biesheuvel 	struct setup_header *hdr = &boot_params->hdr;
77920287d56SArd Biesheuvel 	unsigned long addr, size;
780c2d0b470SArd Biesheuvel 	efi_status_t status;
781c2d0b470SArd Biesheuvel 
782ccc27ae7SArd Biesheuvel 	efi_system_table = sys_table_arg;
783c2d0b470SArd Biesheuvel 	/* Check if we were booted by the EFI firmware */
784ccc27ae7SArd Biesheuvel 	if (efi_system_table->hdr.signature != EFI_SYSTEM_TABLE_SIGNATURE)
7853b8f44fcSArd Biesheuvel 		efi_exit(handle, EFI_INVALID_PARAMETER);
786c2d0b470SArd Biesheuvel 
7873ba75c13SBaskov Evgeniy 	efi_dxe_table = get_efi_config_table(EFI_DXE_SERVICES_TABLE_GUID);
7883ba75c13SBaskov Evgeniy 	if (efi_dxe_table &&
7893ba75c13SBaskov Evgeniy 	    efi_dxe_table->hdr.signature != EFI_DXE_SERVICES_TABLE_SIGNATURE) {
7903ba75c13SBaskov Evgeniy 		efi_warn("Ignoring DXE services table: invalid signature\n");
7913ba75c13SBaskov Evgeniy 		efi_dxe_table = NULL;
7923ba75c13SBaskov Evgeniy 	}
7933ba75c13SBaskov Evgeniy 
794c2d0b470SArd Biesheuvel 	/*
795d5cdf4cfSArvind Sankar 	 * If the kernel isn't already loaded at a suitable address,
796d5cdf4cfSArvind Sankar 	 * relocate it.
797d5cdf4cfSArvind Sankar 	 *
798d5cdf4cfSArvind Sankar 	 * It must be loaded above LOAD_PHYSICAL_ADDR.
799d5cdf4cfSArvind Sankar 	 *
800d5cdf4cfSArvind Sankar 	 * The maximum address for 64-bit is 1 << 46 for 4-level paging. This
801d5cdf4cfSArvind Sankar 	 * is defined as the macro MAXMEM, but unfortunately that is not a
802d5cdf4cfSArvind Sankar 	 * compile-time constant if 5-level paging is configured, so we instead
803d5cdf4cfSArvind Sankar 	 * define our own macro for use here.
804d5cdf4cfSArvind Sankar 	 *
805d5cdf4cfSArvind Sankar 	 * For 32-bit, the maximum address is complicated to figure out, for
806d5cdf4cfSArvind Sankar 	 * now use KERNEL_IMAGE_SIZE, which will be 512MiB, the same as what
807d5cdf4cfSArvind Sankar 	 * KASLR uses.
808d5cdf4cfSArvind Sankar 	 *
80921cb9b41SArvind Sankar 	 * Also relocate it if image_offset is zero, i.e. the kernel wasn't
81021cb9b41SArvind Sankar 	 * loaded by LoadImage, but rather by a bootloader that called the
81121cb9b41SArvind Sankar 	 * handover entry. The reason we must always relocate in this case is
81221cb9b41SArvind Sankar 	 * to handle the case of systemd-boot booting a unified kernel image,
81321cb9b41SArvind Sankar 	 * which is a PE executable that contains the bzImage and an initrd as
81421cb9b41SArvind Sankar 	 * COFF sections. The initrd section is placed after the bzImage
81521cb9b41SArvind Sankar 	 * without ensuring that there are at least init_size bytes available
81621cb9b41SArvind Sankar 	 * for the bzImage, and thus the compressed kernel's startup code may
81721cb9b41SArvind Sankar 	 * overwrite the initrd unless it is moved out of the way.
818c2d0b470SArd Biesheuvel 	 */
819d5cdf4cfSArvind Sankar 
820d5cdf4cfSArvind Sankar 	buffer_start = ALIGN(bzimage_addr - image_offset,
821d5cdf4cfSArvind Sankar 			     hdr->kernel_alignment);
822d5cdf4cfSArvind Sankar 	buffer_end = buffer_start + hdr->init_size;
823d5cdf4cfSArvind Sankar 
824d5cdf4cfSArvind Sankar 	if ((buffer_start < LOAD_PHYSICAL_ADDR)				     ||
825d5cdf4cfSArvind Sankar 	    (IS_ENABLED(CONFIG_X86_32) && buffer_end > KERNEL_IMAGE_SIZE)    ||
826d5cdf4cfSArvind Sankar 	    (IS_ENABLED(CONFIG_X86_64) && buffer_end > MAXMEM_X86_64_4LEVEL) ||
82721cb9b41SArvind Sankar 	    (image_offset == 0)) {
828688eb282SArvind Sankar 		extern char _bss[];
829688eb282SArvind Sankar 
830c2d0b470SArd Biesheuvel 		status = efi_relocate_kernel(&bzimage_addr,
831688eb282SArvind Sankar 					     (unsigned long)_bss - bzimage_addr,
832688eb282SArvind Sankar 					     hdr->init_size,
833c2d0b470SArd Biesheuvel 					     hdr->pref_address,
834c2d0b470SArd Biesheuvel 					     hdr->kernel_alignment,
835c2d0b470SArd Biesheuvel 					     LOAD_PHYSICAL_ADDR);
836c2d0b470SArd Biesheuvel 		if (status != EFI_SUCCESS) {
83736bdd0a7SArvind Sankar 			efi_err("efi_relocate_kernel() failed!\n");
838c2d0b470SArd Biesheuvel 			goto fail;
839c2d0b470SArd Biesheuvel 		}
8401887c9b6SArvind Sankar 		/*
8411887c9b6SArvind Sankar 		 * Now that we've copied the kernel elsewhere, we no longer
8421887c9b6SArvind Sankar 		 * have a set up block before startup_32(), so reset image_offset
8431887c9b6SArvind Sankar 		 * to zero in case it was set earlier.
8441887c9b6SArvind Sankar 		 */
8451887c9b6SArvind Sankar 		image_offset = 0;
846c2d0b470SArd Biesheuvel 	}
847c2d0b470SArd Biesheuvel 
8487dde67f2SArvind Sankar #ifdef CONFIG_CMDLINE_BOOL
849055042beSArvind Sankar 	status = efi_parse_options(CONFIG_CMDLINE);
850055042beSArvind Sankar 	if (status != EFI_SUCCESS) {
851055042beSArvind Sankar 		efi_err("Failed to parse options\n");
852055042beSArvind Sankar 		goto fail;
853055042beSArvind Sankar 	}
8547dde67f2SArvind Sankar #endif
8557dde67f2SArvind Sankar 	if (!IS_ENABLED(CONFIG_CMDLINE_OVERRIDE)) {
8567dde67f2SArvind Sankar 		unsigned long cmdline_paddr = ((u64)hdr->cmd_line_ptr |
857c2d0b470SArd Biesheuvel 					       ((u64)boot_params->ext_cmd_line_ptr << 32));
858055042beSArvind Sankar 		status = efi_parse_options((char *)cmdline_paddr);
859055042beSArvind Sankar 		if (status != EFI_SUCCESS) {
860055042beSArvind Sankar 			efi_err("Failed to parse options\n");
861055042beSArvind Sankar 			goto fail;
862055042beSArvind Sankar 		}
8637dde67f2SArvind Sankar 	}
864c2d0b470SArd Biesheuvel 
865c2d0b470SArd Biesheuvel 	/*
866987053a3SArvind Sankar 	 * At this point, an initrd may already have been loaded by the
867987053a3SArvind Sankar 	 * bootloader and passed via bootparams. We permit an initrd loaded
868987053a3SArvind Sankar 	 * from the LINUX_EFI_INITRD_MEDIA_GUID device path to supersede it.
869987053a3SArvind Sankar 	 *
870987053a3SArvind Sankar 	 * If the device path is not present, any command-line initrd=
871987053a3SArvind Sankar 	 * arguments will be processed only if image is not NULL, which will be
872987053a3SArvind Sankar 	 * the case only if we were loaded via the PE entry point.
873ec93fc37SArd Biesheuvel 	 */
87420287d56SArd Biesheuvel 	status = efi_load_initrd(image, &addr, &size, hdr->initrd_addr_max,
87520287d56SArd Biesheuvel 				 ULONG_MAX);
87620287d56SArd Biesheuvel 	if (status != EFI_SUCCESS)
877ec93fc37SArd Biesheuvel 		goto fail;
878e9524fb9SArvind Sankar 	if (size > 0) {
879eed4e019SArvind Sankar 		efi_set_u64_split(addr, &hdr->ramdisk_image,
880eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_image);
881eed4e019SArvind Sankar 		efi_set_u64_split(size, &hdr->ramdisk_size,
882eed4e019SArvind Sankar 				  &boot_params->ext_ramdisk_size);
88379d3219dSArd Biesheuvel 	}
884ec93fc37SArd Biesheuvel 
885ec93fc37SArd Biesheuvel 	/*
886c2d0b470SArd Biesheuvel 	 * If the boot loader gave us a value for secure_boot then we use that,
887c2d0b470SArd Biesheuvel 	 * otherwise we ask the BIOS.
888c2d0b470SArd Biesheuvel 	 */
889c2d0b470SArd Biesheuvel 	if (boot_params->secure_boot == efi_secureboot_mode_unset)
890c2d0b470SArd Biesheuvel 		boot_params->secure_boot = efi_get_secureboot();
891c2d0b470SArd Biesheuvel 
892c2d0b470SArd Biesheuvel 	/* Ask the firmware to clear memory on unclean shutdown */
893c2d0b470SArd Biesheuvel 	efi_enable_reset_attack_mitigation();
894c2d0b470SArd Biesheuvel 
895c2d0b470SArd Biesheuvel 	efi_random_get_seed();
896c2d0b470SArd Biesheuvel 
897c2d0b470SArd Biesheuvel 	efi_retrieve_tpm2_eventlog();
898c2d0b470SArd Biesheuvel 
899c2d0b470SArd Biesheuvel 	setup_graphics(boot_params);
900c2d0b470SArd Biesheuvel 
901c2d0b470SArd Biesheuvel 	setup_efi_pci(boot_params);
902c2d0b470SArd Biesheuvel 
90382e0d6d7SBaskov Evgeniy 	setup_quirks(boot_params, bzimage_addr, buffer_end - buffer_start);
904c2d0b470SArd Biesheuvel 
905c2d0b470SArd Biesheuvel 	status = exit_boot(boot_params, handle);
906c2d0b470SArd Biesheuvel 	if (status != EFI_SUCCESS) {
90736bdd0a7SArvind Sankar 		efi_err("exit_boot() failed!\n");
908c2d0b470SArd Biesheuvel 		goto fail;
909c2d0b470SArd Biesheuvel 	}
910c2d0b470SArd Biesheuvel 
9118acf63efSArvind Sankar 	return bzimage_addr;
912c2d0b470SArd Biesheuvel fail:
91336bdd0a7SArvind Sankar 	efi_err("efi_main() failed!\n");
914c2d0b470SArd Biesheuvel 
9153b8f44fcSArd Biesheuvel 	efi_exit(handle, status);
916c2d0b470SArd Biesheuvel }
917