xref: /linux/arch/x86/platform/efi/quirks.c (revision cdd5b5a9761fd66d17586e4f4ba6588c70e640ea)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
226d7f65fSMatt Fleming #define pr_fmt(fmt) "efi: " fmt
326d7f65fSMatt Fleming 
4eeb9db09SSaurabh Tangri #include <linux/init.h>
5eeb9db09SSaurabh Tangri #include <linux/kernel.h>
6eeb9db09SSaurabh Tangri #include <linux/string.h>
7eeb9db09SSaurabh Tangri #include <linux/time.h>
8eeb9db09SSaurabh Tangri #include <linux/types.h>
9eeb9db09SSaurabh Tangri #include <linux/efi.h>
10eeb9db09SSaurabh Tangri #include <linux/slab.h>
11eeb9db09SSaurabh Tangri #include <linux/memblock.h>
1244be28e9SMatt Fleming #include <linux/acpi.h>
13d394f2d9SAlex Thorlton #include <linux/dmi.h>
145520b7e7SIngo Molnar 
155520b7e7SIngo Molnar #include <asm/e820/api.h>
16eeb9db09SSaurabh Tangri #include <asm/efi.h>
17eeb9db09SSaurabh Tangri #include <asm/uv/uv.h>
182959c95dSJan Kiszka #include <asm/cpu_device_id.h>
19e71b6f0bSSean Christopherson #include <asm/realmode.h>
203425d934SSai Praneeth #include <asm/reboot.h>
21eeb9db09SSaurabh Tangri 
22eeb9db09SSaurabh Tangri #define EFI_MIN_RESERVE 5120
23eeb9db09SSaurabh Tangri 
24eeb9db09SSaurabh Tangri #define EFI_DUMMY_GUID \
25eeb9db09SSaurabh Tangri 	EFI_GUID(0x4424ac57, 0xbe4b, 0x47dd, 0x9e, 0x97, 0xed, 0x50, 0xf0, 0x9f, 0x92, 0xa9)
26eeb9db09SSaurabh Tangri 
272959c95dSJan Kiszka #define QUARK_CSH_SIGNATURE		0x5f435348	/* _CSH */
282959c95dSJan Kiszka #define QUARK_SECURITY_HEADER_SIZE	0x400
292959c95dSJan Kiszka 
302959c95dSJan Kiszka /*
312959c95dSJan Kiszka  * Header prepended to the standard EFI capsule on Quark systems the are based
322959c95dSJan Kiszka  * on Intel firmware BSP.
332959c95dSJan Kiszka  * @csh_signature:	Unique identifier to sanity check signed module
342959c95dSJan Kiszka  * 			presence ("_CSH").
352959c95dSJan Kiszka  * @version:		Current version of CSH used. Should be one for Quark A0.
362959c95dSJan Kiszka  * @modulesize:		Size of the entire module including the module header
372959c95dSJan Kiszka  * 			and payload.
382959c95dSJan Kiszka  * @security_version_number_index: Index of SVN to use for validation of signed
392959c95dSJan Kiszka  * 			module.
402959c95dSJan Kiszka  * @security_version_number: Used to prevent against roll back of modules.
412959c95dSJan Kiszka  * @rsvd_module_id:	Currently unused for Clanton (Quark).
422959c95dSJan Kiszka  * @rsvd_module_vendor:	Vendor Identifier. For Intel products value is
432959c95dSJan Kiszka  * 			0x00008086.
442959c95dSJan Kiszka  * @rsvd_date:		BCD representation of build date as yyyymmdd, where
452959c95dSJan Kiszka  * 			yyyy=4 digit year, mm=1-12, dd=1-31.
462959c95dSJan Kiszka  * @headersize:		Total length of the header including including any
472959c95dSJan Kiszka  * 			padding optionally added by the signing tool.
482959c95dSJan Kiszka  * @hash_algo:		What Hash is used in the module signing.
492959c95dSJan Kiszka  * @cryp_algo:		What Crypto is used in the module signing.
502959c95dSJan Kiszka  * @keysize:		Total length of the key data including including any
512959c95dSJan Kiszka  * 			padding optionally added by the signing tool.
522959c95dSJan Kiszka  * @signaturesize:	Total length of the signature including including any
532959c95dSJan Kiszka  * 			padding optionally added by the signing tool.
542959c95dSJan Kiszka  * @rsvd_next_header:	32-bit pointer to the next Secure Boot Module in the
552959c95dSJan Kiszka  * 			chain, if there is a next header.
562959c95dSJan Kiszka  * @rsvd:		Reserved, padding structure to required size.
572959c95dSJan Kiszka  *
582959c95dSJan Kiszka  * See also QuartSecurityHeader_t in
592959c95dSJan Kiszka  * Quark_EDKII_v1.2.1.1/QuarkPlatformPkg/Include/QuarkBootRom.h
602959c95dSJan Kiszka  * from https://downloadcenter.intel.com/download/23197/Intel-Quark-SoC-X1000-Board-Support-Package-BSP
612959c95dSJan Kiszka  */
622959c95dSJan Kiszka struct quark_security_header {
632959c95dSJan Kiszka 	u32 csh_signature;
642959c95dSJan Kiszka 	u32 version;
652959c95dSJan Kiszka 	u32 modulesize;
662959c95dSJan Kiszka 	u32 security_version_number_index;
672959c95dSJan Kiszka 	u32 security_version_number;
682959c95dSJan Kiszka 	u32 rsvd_module_id;
692959c95dSJan Kiszka 	u32 rsvd_module_vendor;
702959c95dSJan Kiszka 	u32 rsvd_date;
712959c95dSJan Kiszka 	u32 headersize;
722959c95dSJan Kiszka 	u32 hash_algo;
732959c95dSJan Kiszka 	u32 cryp_algo;
742959c95dSJan Kiszka 	u32 keysize;
752959c95dSJan Kiszka 	u32 signaturesize;
762959c95dSJan Kiszka 	u32 rsvd_next_header;
772959c95dSJan Kiszka 	u32 rsvd[2];
782959c95dSJan Kiszka };
792959c95dSJan Kiszka 
8036b64976SArd Biesheuvel static const efi_char16_t efi_dummy_name[] = L"DUMMY";
81eeb9db09SSaurabh Tangri 
82eeb9db09SSaurabh Tangri static bool efi_no_storage_paranoia;
83eeb9db09SSaurabh Tangri 
84eeb9db09SSaurabh Tangri /*
85eeb9db09SSaurabh Tangri  * Some firmware implementations refuse to boot if there's insufficient
86eeb9db09SSaurabh Tangri  * space in the variable store. The implementation of garbage collection
87eeb9db09SSaurabh Tangri  * in some FW versions causes stale (deleted) variables to take up space
88eeb9db09SSaurabh Tangri  * longer than intended and space is only freed once the store becomes
89eeb9db09SSaurabh Tangri  * almost completely full.
90eeb9db09SSaurabh Tangri  *
91eeb9db09SSaurabh Tangri  * Enabling this option disables the space checks in
92eeb9db09SSaurabh Tangri  * efi_query_variable_store() and forces garbage collection.
93eeb9db09SSaurabh Tangri  *
94eeb9db09SSaurabh Tangri  * Only enable this option if deleting EFI variables does not free up
95eeb9db09SSaurabh Tangri  * space in your variable store, e.g. if despite deleting variables
96eeb9db09SSaurabh Tangri  * you're unable to create new ones.
97eeb9db09SSaurabh Tangri  */
setup_storage_paranoia(char * arg)98eeb9db09SSaurabh Tangri static int __init setup_storage_paranoia(char *arg)
99eeb9db09SSaurabh Tangri {
100eeb9db09SSaurabh Tangri 	efi_no_storage_paranoia = true;
101eeb9db09SSaurabh Tangri 	return 0;
102eeb9db09SSaurabh Tangri }
103eeb9db09SSaurabh Tangri early_param("efi_no_storage_paranoia", setup_storage_paranoia);
104eeb9db09SSaurabh Tangri 
105eeb9db09SSaurabh Tangri /*
106eeb9db09SSaurabh Tangri  * Deleting the dummy variable which kicks off garbage collection
107eeb9db09SSaurabh Tangri */
efi_delete_dummy_variable(void)108eeb9db09SSaurabh Tangri void efi_delete_dummy_variable(void)
109eeb9db09SSaurabh Tangri {
1105a58bc1bSSai Praneeth 	efi.set_variable_nonblocking((efi_char16_t *)efi_dummy_name,
11136b64976SArd Biesheuvel 				     &EFI_DUMMY_GUID,
112eeb9db09SSaurabh Tangri 				     EFI_VARIABLE_NON_VOLATILE |
113eeb9db09SSaurabh Tangri 				     EFI_VARIABLE_BOOTSERVICE_ACCESS |
1145a58bc1bSSai Praneeth 				     EFI_VARIABLE_RUNTIME_ACCESS, 0, NULL);
115eeb9db09SSaurabh Tangri }
116eeb9db09SSaurabh Tangri 
efivar_reserved_space(void)117*d86ff333SAnisse Astier u64 efivar_reserved_space(void)
118*d86ff333SAnisse Astier {
119*d86ff333SAnisse Astier 	if (efi_no_storage_paranoia)
120*d86ff333SAnisse Astier 		return 0;
121*d86ff333SAnisse Astier 	return EFI_MIN_RESERVE;
122*d86ff333SAnisse Astier }
123*d86ff333SAnisse Astier EXPORT_SYMBOL_GPL(efivar_reserved_space);
124*d86ff333SAnisse Astier 
125eeb9db09SSaurabh Tangri /*
126ca0e30dcSArd Biesheuvel  * In the nonblocking case we do not attempt to perform garbage
127ca0e30dcSArd Biesheuvel  * collection if we do not have enough free space. Rather, we do the
128ca0e30dcSArd Biesheuvel  * bare minimum check and give up immediately if the available space
129ca0e30dcSArd Biesheuvel  * is below EFI_MIN_RESERVE.
130ca0e30dcSArd Biesheuvel  *
131ca0e30dcSArd Biesheuvel  * This function is intended to be small and simple because it is
132ca0e30dcSArd Biesheuvel  * invoked from crash handler paths.
133ca0e30dcSArd Biesheuvel  */
134ca0e30dcSArd Biesheuvel static efi_status_t
query_variable_store_nonblocking(u32 attributes,unsigned long size)135ca0e30dcSArd Biesheuvel query_variable_store_nonblocking(u32 attributes, unsigned long size)
136ca0e30dcSArd Biesheuvel {
137ca0e30dcSArd Biesheuvel 	efi_status_t status;
138ca0e30dcSArd Biesheuvel 	u64 storage_size, remaining_size, max_size;
139ca0e30dcSArd Biesheuvel 
140ca0e30dcSArd Biesheuvel 	status = efi.query_variable_info_nonblocking(attributes, &storage_size,
141ca0e30dcSArd Biesheuvel 						     &remaining_size,
142ca0e30dcSArd Biesheuvel 						     &max_size);
143ca0e30dcSArd Biesheuvel 	if (status != EFI_SUCCESS)
144ca0e30dcSArd Biesheuvel 		return status;
145ca0e30dcSArd Biesheuvel 
146ca0e30dcSArd Biesheuvel 	if (remaining_size - size < EFI_MIN_RESERVE)
147ca0e30dcSArd Biesheuvel 		return EFI_OUT_OF_RESOURCES;
148ca0e30dcSArd Biesheuvel 
149ca0e30dcSArd Biesheuvel 	return EFI_SUCCESS;
150ca0e30dcSArd Biesheuvel }
151ca0e30dcSArd Biesheuvel 
152ca0e30dcSArd Biesheuvel /*
153eeb9db09SSaurabh Tangri  * Some firmware implementations refuse to boot if there's insufficient space
154eeb9db09SSaurabh Tangri  * in the variable store. Ensure that we never use more than a safe limit.
155eeb9db09SSaurabh Tangri  *
156eeb9db09SSaurabh Tangri  * Return EFI_SUCCESS if it is safe to write 'size' bytes to the variable
157eeb9db09SSaurabh Tangri  * store.
158eeb9db09SSaurabh Tangri  */
efi_query_variable_store(u32 attributes,unsigned long size,bool nonblocking)159ca0e30dcSArd Biesheuvel efi_status_t efi_query_variable_store(u32 attributes, unsigned long size,
160ca0e30dcSArd Biesheuvel 				      bool nonblocking)
161eeb9db09SSaurabh Tangri {
162eeb9db09SSaurabh Tangri 	efi_status_t status;
163eeb9db09SSaurabh Tangri 	u64 storage_size, remaining_size, max_size;
164eeb9db09SSaurabh Tangri 
165eeb9db09SSaurabh Tangri 	if (!(attributes & EFI_VARIABLE_NON_VOLATILE))
166eeb9db09SSaurabh Tangri 		return 0;
167eeb9db09SSaurabh Tangri 
168ca0e30dcSArd Biesheuvel 	if (nonblocking)
169ca0e30dcSArd Biesheuvel 		return query_variable_store_nonblocking(attributes, size);
170ca0e30dcSArd Biesheuvel 
171eeb9db09SSaurabh Tangri 	status = efi.query_variable_info(attributes, &storage_size,
172eeb9db09SSaurabh Tangri 					 &remaining_size, &max_size);
173eeb9db09SSaurabh Tangri 	if (status != EFI_SUCCESS)
174eeb9db09SSaurabh Tangri 		return status;
175eeb9db09SSaurabh Tangri 
176eeb9db09SSaurabh Tangri 	/*
177eeb9db09SSaurabh Tangri 	 * We account for that by refusing the write if permitting it would
178eeb9db09SSaurabh Tangri 	 * reduce the available space to under 5KB. This figure was provided by
179eeb9db09SSaurabh Tangri 	 * Samsung, so should be safe.
180eeb9db09SSaurabh Tangri 	 */
181eeb9db09SSaurabh Tangri 	if ((remaining_size - size < EFI_MIN_RESERVE) &&
182eeb9db09SSaurabh Tangri 		!efi_no_storage_paranoia) {
183eeb9db09SSaurabh Tangri 
184eeb9db09SSaurabh Tangri 		/*
185eeb9db09SSaurabh Tangri 		 * Triggering garbage collection may require that the firmware
186eeb9db09SSaurabh Tangri 		 * generate a real EFI_OUT_OF_RESOURCES error. We can force
187eeb9db09SSaurabh Tangri 		 * that by attempting to use more space than is available.
188eeb9db09SSaurabh Tangri 		 */
189eeb9db09SSaurabh Tangri 		unsigned long dummy_size = remaining_size + 1024;
1909f66d8d7SJia-Ju Bai 		void *dummy = kzalloc(dummy_size, GFP_KERNEL);
191eeb9db09SSaurabh Tangri 
192eeb9db09SSaurabh Tangri 		if (!dummy)
193eeb9db09SSaurabh Tangri 			return EFI_OUT_OF_RESOURCES;
194eeb9db09SSaurabh Tangri 
19536b64976SArd Biesheuvel 		status = efi.set_variable((efi_char16_t *)efi_dummy_name,
19636b64976SArd Biesheuvel 					  &EFI_DUMMY_GUID,
197eeb9db09SSaurabh Tangri 					  EFI_VARIABLE_NON_VOLATILE |
198eeb9db09SSaurabh Tangri 					  EFI_VARIABLE_BOOTSERVICE_ACCESS |
199eeb9db09SSaurabh Tangri 					  EFI_VARIABLE_RUNTIME_ACCESS,
200eeb9db09SSaurabh Tangri 					  dummy_size, dummy);
201eeb9db09SSaurabh Tangri 
202eeb9db09SSaurabh Tangri 		if (status == EFI_SUCCESS) {
203eeb9db09SSaurabh Tangri 			/*
204eeb9db09SSaurabh Tangri 			 * This should have failed, so if it didn't make sure
205eeb9db09SSaurabh Tangri 			 * that we delete it...
206eeb9db09SSaurabh Tangri 			 */
207eeb9db09SSaurabh Tangri 			efi_delete_dummy_variable();
208eeb9db09SSaurabh Tangri 		}
209eeb9db09SSaurabh Tangri 
210eeb9db09SSaurabh Tangri 		kfree(dummy);
211eeb9db09SSaurabh Tangri 
212eeb9db09SSaurabh Tangri 		/*
213eeb9db09SSaurabh Tangri 		 * The runtime code may now have triggered a garbage collection
214eeb9db09SSaurabh Tangri 		 * run, so check the variable info again
215eeb9db09SSaurabh Tangri 		 */
216eeb9db09SSaurabh Tangri 		status = efi.query_variable_info(attributes, &storage_size,
217eeb9db09SSaurabh Tangri 						 &remaining_size, &max_size);
218eeb9db09SSaurabh Tangri 
219eeb9db09SSaurabh Tangri 		if (status != EFI_SUCCESS)
220eeb9db09SSaurabh Tangri 			return status;
221eeb9db09SSaurabh Tangri 
222eeb9db09SSaurabh Tangri 		/*
223eeb9db09SSaurabh Tangri 		 * There still isn't enough room, so return an error
224eeb9db09SSaurabh Tangri 		 */
225eeb9db09SSaurabh Tangri 		if (remaining_size - size < EFI_MIN_RESERVE)
226eeb9db09SSaurabh Tangri 			return EFI_OUT_OF_RESOURCES;
227eeb9db09SSaurabh Tangri 	}
228eeb9db09SSaurabh Tangri 
229eeb9db09SSaurabh Tangri 	return EFI_SUCCESS;
230eeb9db09SSaurabh Tangri }
231eeb9db09SSaurabh Tangri EXPORT_SYMBOL_GPL(efi_query_variable_store);
232eeb9db09SSaurabh Tangri 
233eeb9db09SSaurabh Tangri /*
234816e7612SMatt Fleming  * The UEFI specification makes it clear that the operating system is
235816e7612SMatt Fleming  * free to do whatever it wants with boot services code after
236816e7612SMatt Fleming  * ExitBootServices() has been called. Ignoring this recommendation a
237816e7612SMatt Fleming  * significant bunch of EFI implementations continue calling into boot
238816e7612SMatt Fleming  * services code (SetVirtualAddressMap). In order to work around such
239816e7612SMatt Fleming  * buggy implementations we reserve boot services region during EFI
240816e7612SMatt Fleming  * init and make sure it stays executable. Then, after
241816e7612SMatt Fleming  * SetVirtualAddressMap(), it is discarded.
242816e7612SMatt Fleming  *
243816e7612SMatt Fleming  * However, some boot services regions contain data that is required
244816e7612SMatt Fleming  * by drivers, so we need to track which memory ranges can never be
245816e7612SMatt Fleming  * freed. This is done by tagging those regions with the
246816e7612SMatt Fleming  * EFI_MEMORY_RUNTIME attribute.
247816e7612SMatt Fleming  *
248816e7612SMatt Fleming  * Any driver that wants to mark a region as reserved must use
249816e7612SMatt Fleming  * efi_mem_reserve() which will insert a new EFI memory descriptor
250816e7612SMatt Fleming  * into efi.memmap (splitting existing regions if necessary) and tag
251816e7612SMatt Fleming  * it with EFI_MEMORY_RUNTIME.
252816e7612SMatt Fleming  */
efi_arch_mem_reserve(phys_addr_t addr,u64 size)253816e7612SMatt Fleming void __init efi_arch_mem_reserve(phys_addr_t addr, u64 size)
254816e7612SMatt Fleming {
2551db91035SDan Williams 	struct efi_memory_map_data data = { 0 };
256816e7612SMatt Fleming 	struct efi_mem_range mr;
257816e7612SMatt Fleming 	efi_memory_desc_t md;
258816e7612SMatt Fleming 	int num_entries;
259816e7612SMatt Fleming 	void *new;
260816e7612SMatt Fleming 
2617e1550b8SArd Biesheuvel 	if (efi_mem_desc_lookup(addr, &md) ||
2627e1550b8SArd Biesheuvel 	    md.type != EFI_BOOT_SERVICES_DATA) {
263816e7612SMatt Fleming 		pr_err("Failed to lookup EFI memory descriptor for %pa\n", &addr);
264816e7612SMatt Fleming 		return;
265816e7612SMatt Fleming 	}
266816e7612SMatt Fleming 
267816e7612SMatt Fleming 	if (addr + size > md.phys_addr + (md.num_pages << EFI_PAGE_SHIFT)) {
268816e7612SMatt Fleming 		pr_err("Region spans EFI memory descriptors, %pa\n", &addr);
269816e7612SMatt Fleming 		return;
270816e7612SMatt Fleming 	}
271816e7612SMatt Fleming 
27292dc3350SMatt Fleming 	size += addr % EFI_PAGE_SIZE;
27392dc3350SMatt Fleming 	size = round_up(size, EFI_PAGE_SIZE);
27492dc3350SMatt Fleming 	addr = round_down(addr, EFI_PAGE_SIZE);
27592dc3350SMatt Fleming 
276816e7612SMatt Fleming 	mr.range.start = addr;
27792dc3350SMatt Fleming 	mr.range.end = addr + size - 1;
278816e7612SMatt Fleming 	mr.attribute = md.attribute | EFI_MEMORY_RUNTIME;
279816e7612SMatt Fleming 
280816e7612SMatt Fleming 	num_entries = efi_memmap_split_count(&md, &mr.range);
281816e7612SMatt Fleming 	num_entries += efi.memmap.nr_map;
282816e7612SMatt Fleming 
2831db91035SDan Williams 	if (efi_memmap_alloc(num_entries, &data) != 0) {
284816e7612SMatt Fleming 		pr_err("Could not allocate boot services memmap\n");
285816e7612SMatt Fleming 		return;
286816e7612SMatt Fleming 	}
287816e7612SMatt Fleming 
2881ff2fc02STom Lendacky 	new = early_memremap_prot(data.phys_map, data.size,
2891ff2fc02STom Lendacky 				  pgprot_val(pgprot_encrypted(FIXMAP_PAGE_NORMAL)));
290816e7612SMatt Fleming 	if (!new) {
291816e7612SMatt Fleming 		pr_err("Failed to map new boot services memmap\n");
292816e7612SMatt Fleming 		return;
293816e7612SMatt Fleming 	}
294816e7612SMatt Fleming 
295816e7612SMatt Fleming 	efi_memmap_insert(&efi.memmap, new, &mr);
2961db91035SDan Williams 	early_memunmap(new, data.size);
297816e7612SMatt Fleming 
2981db91035SDan Williams 	efi_memmap_install(&data);
299af164898SDave Young 	e820__range_update(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
300af164898SDave Young 	e820__update_table(e820_table);
301816e7612SMatt Fleming }
302816e7612SMatt Fleming 
303816e7612SMatt Fleming /*
304452308deSMatt Fleming  * Helper function for efi_reserve_boot_services() to figure out if we
305452308deSMatt Fleming  * can free regions in efi_free_boot_services().
306452308deSMatt Fleming  *
307452308deSMatt Fleming  * Use this function to ensure we do not free regions owned by somebody
308452308deSMatt Fleming  * else. We must only reserve (and then free) regions:
309452308deSMatt Fleming  *
310452308deSMatt Fleming  * - Not within any part of the kernel
31109821ff1SIngo Molnar  * - Not the BIOS reserved area (E820_TYPE_RESERVED, E820_TYPE_NVS, etc)
312452308deSMatt Fleming  */
can_free_region(u64 start,u64 size)3138fe55212SSai Praneeth Prakhya static __init bool can_free_region(u64 start, u64 size)
314452308deSMatt Fleming {
315452308deSMatt Fleming 	if (start + size > __pa_symbol(_text) && start <= __pa_symbol(_end))
316452308deSMatt Fleming 		return false;
317452308deSMatt Fleming 
31809821ff1SIngo Molnar 	if (!e820__mapped_all(start, start+size, E820_TYPE_RAM))
319452308deSMatt Fleming 		return false;
320452308deSMatt Fleming 
321452308deSMatt Fleming 	return true;
322452308deSMatt Fleming }
323452308deSMatt Fleming 
efi_reserve_boot_services(void)324eeb9db09SSaurabh Tangri void __init efi_reserve_boot_services(void)
325eeb9db09SSaurabh Tangri {
32678ce248fSMatt Fleming 	efi_memory_desc_t *md;
327eeb9db09SSaurabh Tangri 
3286950e31bSDan Williams 	if (!efi_enabled(EFI_MEMMAP))
3296950e31bSDan Williams 		return;
3306950e31bSDan Williams 
33178ce248fSMatt Fleming 	for_each_efi_memory_desc(md) {
332eeb9db09SSaurabh Tangri 		u64 start = md->phys_addr;
333eeb9db09SSaurabh Tangri 		u64 size = md->num_pages << EFI_PAGE_SHIFT;
334452308deSMatt Fleming 		bool already_reserved;
335eeb9db09SSaurabh Tangri 
336eeb9db09SSaurabh Tangri 		if (md->type != EFI_BOOT_SERVICES_CODE &&
337eeb9db09SSaurabh Tangri 		    md->type != EFI_BOOT_SERVICES_DATA)
338eeb9db09SSaurabh Tangri 			continue;
339452308deSMatt Fleming 
340452308deSMatt Fleming 		already_reserved = memblock_is_region_reserved(start, size);
341452308deSMatt Fleming 
342452308deSMatt Fleming 		/*
343452308deSMatt Fleming 		 * Because the following memblock_reserve() is paired
34453ab85ebSMike Rapoport 		 * with memblock_free_late() for this region in
345452308deSMatt Fleming 		 * efi_free_boot_services(), we must be extremely
346452308deSMatt Fleming 		 * careful not to reserve, and subsequently free,
347452308deSMatt Fleming 		 * critical regions of memory (like the kernel image) or
348452308deSMatt Fleming 		 * those regions that somebody else has already
349452308deSMatt Fleming 		 * reserved.
350452308deSMatt Fleming 		 *
351452308deSMatt Fleming 		 * A good example of a critical region that must not be
352452308deSMatt Fleming 		 * freed is page zero (first 4Kb of memory), which may
353452308deSMatt Fleming 		 * contain boot services code/data but is marked
35409821ff1SIngo Molnar 		 * E820_TYPE_RESERVED by trim_bios_range().
355eeb9db09SSaurabh Tangri 		 */
356452308deSMatt Fleming 		if (!already_reserved) {
357eeb9db09SSaurabh Tangri 			memblock_reserve(start, size);
358452308deSMatt Fleming 
359452308deSMatt Fleming 			/*
360452308deSMatt Fleming 			 * If we are the first to reserve the region, no
361452308deSMatt Fleming 			 * one else cares about it. We own it and can
362452308deSMatt Fleming 			 * free it later.
363452308deSMatt Fleming 			 */
364452308deSMatt Fleming 			if (can_free_region(start, size))
365452308deSMatt Fleming 				continue;
366452308deSMatt Fleming 		}
367452308deSMatt Fleming 
368452308deSMatt Fleming 		/*
369452308deSMatt Fleming 		 * We don't own the region. We must not free it.
370452308deSMatt Fleming 		 *
371452308deSMatt Fleming 		 * Setting this bit for a boot services region really
372452308deSMatt Fleming 		 * doesn't make sense as far as the firmware is
373452308deSMatt Fleming 		 * concerned, but it does provide us with a way to tag
374452308deSMatt Fleming 		 * those regions that must not be paired with
37553ab85ebSMike Rapoport 		 * memblock_free_late().
376452308deSMatt Fleming 		 */
377452308deSMatt Fleming 		md->attribute |= EFI_MEMORY_RUNTIME;
378eeb9db09SSaurabh Tangri 	}
379eeb9db09SSaurabh Tangri }
380eeb9db09SSaurabh Tangri 
38108cfb38fSSai Praneeth Prakhya /*
38208cfb38fSSai Praneeth Prakhya  * Apart from having VA mappings for EFI boot services code/data regions,
38308cfb38fSSai Praneeth Prakhya  * (duplicate) 1:1 mappings were also created as a quirk for buggy firmware. So,
38408cfb38fSSai Praneeth Prakhya  * unmap both 1:1 and VA mappings.
38508cfb38fSSai Praneeth Prakhya  */
efi_unmap_pages(efi_memory_desc_t * md)38608cfb38fSSai Praneeth Prakhya static void __init efi_unmap_pages(efi_memory_desc_t *md)
38708cfb38fSSai Praneeth Prakhya {
38808cfb38fSSai Praneeth Prakhya 	pgd_t *pgd = efi_mm.pgd;
38908cfb38fSSai Praneeth Prakhya 	u64 pa = md->phys_addr;
39008cfb38fSSai Praneeth Prakhya 	u64 va = md->virt_addr;
39108cfb38fSSai Praneeth Prakhya 
3921debf095SSai Praneeth Prakhya 	/*
3931debf095SSai Praneeth Prakhya 	 * EFI mixed mode has all RAM mapped to access arguments while making
3941debf095SSai Praneeth Prakhya 	 * EFI runtime calls, hence don't unmap EFI boot services code/data
3951debf095SSai Praneeth Prakhya 	 * regions.
3961debf095SSai Praneeth Prakhya 	 */
397a8147dbaSArd Biesheuvel 	if (efi_is_mixed())
3981debf095SSai Praneeth Prakhya 		return;
3991debf095SSai Praneeth Prakhya 
40008cfb38fSSai Praneeth Prakhya 	if (kernel_unmap_pages_in_pgd(pgd, pa, md->num_pages))
40108cfb38fSSai Praneeth Prakhya 		pr_err("Failed to unmap 1:1 mapping for 0x%llx\n", pa);
40208cfb38fSSai Praneeth Prakhya 
40308cfb38fSSai Praneeth Prakhya 	if (kernel_unmap_pages_in_pgd(pgd, va, md->num_pages))
40408cfb38fSSai Praneeth Prakhya 		pr_err("Failed to unmap VA mapping for 0x%llx\n", va);
40508cfb38fSSai Praneeth Prakhya }
40608cfb38fSSai Praneeth Prakhya 
efi_free_boot_services(void)407eeb9db09SSaurabh Tangri void __init efi_free_boot_services(void)
408eeb9db09SSaurabh Tangri {
4091db91035SDan Williams 	struct efi_memory_map_data data = { 0 };
41078ce248fSMatt Fleming 	efi_memory_desc_t *md;
411816e7612SMatt Fleming 	int num_entries = 0;
412816e7612SMatt Fleming 	void *new, *new_md;
413eeb9db09SSaurabh Tangri 
4140e72a6a3SHans de Goede 	/* Keep all regions for /sys/kernel/debug/efi */
4150e72a6a3SHans de Goede 	if (efi_enabled(EFI_DBG))
4160e72a6a3SHans de Goede 		return;
4170e72a6a3SHans de Goede 
41878ce248fSMatt Fleming 	for_each_efi_memory_desc(md) {
419eeb9db09SSaurabh Tangri 		unsigned long long start = md->phys_addr;
420eeb9db09SSaurabh Tangri 		unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
4215bc653b7SAndy Lutomirski 		size_t rm_size;
422eeb9db09SSaurabh Tangri 
423eeb9db09SSaurabh Tangri 		if (md->type != EFI_BOOT_SERVICES_CODE &&
424816e7612SMatt Fleming 		    md->type != EFI_BOOT_SERVICES_DATA) {
425816e7612SMatt Fleming 			num_entries++;
426eeb9db09SSaurabh Tangri 			continue;
427816e7612SMatt Fleming 		}
428eeb9db09SSaurabh Tangri 
429452308deSMatt Fleming 		/* Do not free, someone else owns it: */
430816e7612SMatt Fleming 		if (md->attribute & EFI_MEMORY_RUNTIME) {
431816e7612SMatt Fleming 			num_entries++;
432eeb9db09SSaurabh Tangri 			continue;
433816e7612SMatt Fleming 		}
434eeb9db09SSaurabh Tangri 
4355bc653b7SAndy Lutomirski 		/*
43608cfb38fSSai Praneeth Prakhya 		 * Before calling set_virtual_address_map(), EFI boot services
43708cfb38fSSai Praneeth Prakhya 		 * code/data regions were mapped as a quirk for buggy firmware.
43808cfb38fSSai Praneeth Prakhya 		 * Unmap them from efi_pgd before freeing them up.
43908cfb38fSSai Praneeth Prakhya 		 */
44008cfb38fSSai Praneeth Prakhya 		efi_unmap_pages(md);
44108cfb38fSSai Praneeth Prakhya 
44208cfb38fSSai Praneeth Prakhya 		/*
4435bc653b7SAndy Lutomirski 		 * Nasty quirk: if all sub-1MB memory is used for boot
4445bc653b7SAndy Lutomirski 		 * services, we can get here without having allocated the
4455bc653b7SAndy Lutomirski 		 * real mode trampoline.  It's too late to hand boot services
4465bc653b7SAndy Lutomirski 		 * memory back to the memblock allocator, so instead
4475bc653b7SAndy Lutomirski 		 * try to manually allocate the trampoline if needed.
4485bc653b7SAndy Lutomirski 		 *
4495bc653b7SAndy Lutomirski 		 * I've seen this on a Dell XPS 13 9350 with firmware
4505bc653b7SAndy Lutomirski 		 * 1.4.4 with SGX enabled booting Linux via Fedora 24's
4515bc653b7SAndy Lutomirski 		 * grub2-efi on a hard disk.  (And no, I don't know why
4525bc653b7SAndy Lutomirski 		 * this happened, but Linux should still try to boot rather
453163b0991SIngo Molnar 		 * panicking early.)
4545bc653b7SAndy Lutomirski 		 */
4555bc653b7SAndy Lutomirski 		rm_size = real_mode_size_needed();
4565bc653b7SAndy Lutomirski 		if (rm_size && (start + rm_size) < (1<<20) && size >= rm_size) {
457f560bd19SMatteo Croce 			set_real_mode_mem(start);
4585bc653b7SAndy Lutomirski 			start += rm_size;
4595bc653b7SAndy Lutomirski 			size -= rm_size;
4605bc653b7SAndy Lutomirski 		}
4615bc653b7SAndy Lutomirski 
462f1d4d47cSMike Rapoport 		/*
463f1d4d47cSMike Rapoport 		 * Don't free memory under 1M for two reasons:
464f1d4d47cSMike Rapoport 		 * - BIOS might clobber it
465f1d4d47cSMike Rapoport 		 * - Crash kernel needs it to be reserved
466f1d4d47cSMike Rapoport 		 */
467f1d4d47cSMike Rapoport 		if (start + size < SZ_1M)
468f1d4d47cSMike Rapoport 			continue;
469f1d4d47cSMike Rapoport 		if (start < SZ_1M) {
470f1d4d47cSMike Rapoport 			size -= (SZ_1M - start);
471f1d4d47cSMike Rapoport 			start = SZ_1M;
472f1d4d47cSMike Rapoport 		}
473f1d4d47cSMike Rapoport 
47453ab85ebSMike Rapoport 		memblock_free_late(start, size);
475eeb9db09SSaurabh Tangri 	}
476816e7612SMatt Fleming 
4771ea34adbSJuergen Gross 	if (!num_entries)
4781ea34adbSJuergen Gross 		return;
4791ea34adbSJuergen Gross 
4801db91035SDan Williams 	if (efi_memmap_alloc(num_entries, &data) != 0) {
481816e7612SMatt Fleming 		pr_err("Failed to allocate new EFI memmap\n");
482816e7612SMatt Fleming 		return;
483816e7612SMatt Fleming 	}
484816e7612SMatt Fleming 
4851db91035SDan Williams 	new = memremap(data.phys_map, data.size, MEMREMAP_WB);
486816e7612SMatt Fleming 	if (!new) {
487816e7612SMatt Fleming 		pr_err("Failed to map new EFI memmap\n");
488816e7612SMatt Fleming 		return;
489816e7612SMatt Fleming 	}
490816e7612SMatt Fleming 
491816e7612SMatt Fleming 	/*
492816e7612SMatt Fleming 	 * Build a new EFI memmap that excludes any boot services
493816e7612SMatt Fleming 	 * regions that are not tagged EFI_MEMORY_RUNTIME, since those
494816e7612SMatt Fleming 	 * regions have now been freed.
495816e7612SMatt Fleming 	 */
496816e7612SMatt Fleming 	new_md = new;
497816e7612SMatt Fleming 	for_each_efi_memory_desc(md) {
498816e7612SMatt Fleming 		if (!(md->attribute & EFI_MEMORY_RUNTIME) &&
499816e7612SMatt Fleming 		    (md->type == EFI_BOOT_SERVICES_CODE ||
500816e7612SMatt Fleming 		     md->type == EFI_BOOT_SERVICES_DATA))
501816e7612SMatt Fleming 			continue;
502816e7612SMatt Fleming 
503816e7612SMatt Fleming 		memcpy(new_md, md, efi.memmap.desc_size);
504816e7612SMatt Fleming 		new_md += efi.memmap.desc_size;
505816e7612SMatt Fleming 	}
506816e7612SMatt Fleming 
507816e7612SMatt Fleming 	memunmap(new);
508816e7612SMatt Fleming 
5091db91035SDan Williams 	if (efi_memmap_install(&data) != 0) {
510816e7612SMatt Fleming 		pr_err("Could not install new EFI memmap\n");
511816e7612SMatt Fleming 		return;
512816e7612SMatt Fleming 	}
513eeb9db09SSaurabh Tangri }
514eeb9db09SSaurabh Tangri 
515eeb9db09SSaurabh Tangri /*
516eeb9db09SSaurabh Tangri  * A number of config table entries get remapped to virtual addresses
517eeb9db09SSaurabh Tangri  * after entering EFI virtual mode. However, the kexec kernel requires
518eeb9db09SSaurabh Tangri  * their physical addresses therefore we pass them via setup_data and
519eeb9db09SSaurabh Tangri  * correct those entries to their respective physical addresses here.
520eeb9db09SSaurabh Tangri  *
521eeb9db09SSaurabh Tangri  * Currently only handles smbios which is necessary for some firmware
522eeb9db09SSaurabh Tangri  * implementation.
523eeb9db09SSaurabh Tangri  */
efi_reuse_config(u64 tables,int nr_tables)524eeb9db09SSaurabh Tangri int __init efi_reuse_config(u64 tables, int nr_tables)
525eeb9db09SSaurabh Tangri {
526eeb9db09SSaurabh Tangri 	int i, sz, ret = 0;
527eeb9db09SSaurabh Tangri 	void *p, *tablep;
528eeb9db09SSaurabh Tangri 	struct efi_setup_data *data;
529eeb9db09SSaurabh Tangri 
53088447c5bSRob Bradford 	if (nr_tables == 0)
53188447c5bSRob Bradford 		return 0;
53288447c5bSRob Bradford 
533eeb9db09SSaurabh Tangri 	if (!efi_setup)
534eeb9db09SSaurabh Tangri 		return 0;
535eeb9db09SSaurabh Tangri 
536eeb9db09SSaurabh Tangri 	if (!efi_enabled(EFI_64BIT))
537eeb9db09SSaurabh Tangri 		return 0;
538eeb9db09SSaurabh Tangri 
539eeb9db09SSaurabh Tangri 	data = early_memremap(efi_setup, sizeof(*data));
540eeb9db09SSaurabh Tangri 	if (!data) {
541eeb9db09SSaurabh Tangri 		ret = -ENOMEM;
542eeb9db09SSaurabh Tangri 		goto out;
543eeb9db09SSaurabh Tangri 	}
544eeb9db09SSaurabh Tangri 
545eeb9db09SSaurabh Tangri 	if (!data->smbios)
546eeb9db09SSaurabh Tangri 		goto out_memremap;
547eeb9db09SSaurabh Tangri 
548eeb9db09SSaurabh Tangri 	sz = sizeof(efi_config_table_64_t);
549eeb9db09SSaurabh Tangri 
550eeb9db09SSaurabh Tangri 	p = tablep = early_memremap(tables, nr_tables * sz);
551eeb9db09SSaurabh Tangri 	if (!p) {
552eeb9db09SSaurabh Tangri 		pr_err("Could not map Configuration table!\n");
553eeb9db09SSaurabh Tangri 		ret = -ENOMEM;
554eeb9db09SSaurabh Tangri 		goto out_memremap;
555eeb9db09SSaurabh Tangri 	}
556eeb9db09SSaurabh Tangri 
5579cd437acSArd Biesheuvel 	for (i = 0; i < nr_tables; i++) {
558eeb9db09SSaurabh Tangri 		efi_guid_t guid;
559eeb9db09SSaurabh Tangri 
560eeb9db09SSaurabh Tangri 		guid = ((efi_config_table_64_t *)p)->guid;
561eeb9db09SSaurabh Tangri 
562eeb9db09SSaurabh Tangri 		if (!efi_guidcmp(guid, SMBIOS_TABLE_GUID))
563eeb9db09SSaurabh Tangri 			((efi_config_table_64_t *)p)->table = data->smbios;
564eeb9db09SSaurabh Tangri 		p += sz;
565eeb9db09SSaurabh Tangri 	}
56698a716b6SMatt Fleming 	early_memunmap(tablep, nr_tables * sz);
567eeb9db09SSaurabh Tangri 
568eeb9db09SSaurabh Tangri out_memremap:
56998a716b6SMatt Fleming 	early_memunmap(data, sizeof(*data));
570eeb9db09SSaurabh Tangri out:
571eeb9db09SSaurabh Tangri 	return ret;
572eeb9db09SSaurabh Tangri }
573eeb9db09SSaurabh Tangri 
efi_apply_memmap_quirks(void)574eeb9db09SSaurabh Tangri void __init efi_apply_memmap_quirks(void)
575eeb9db09SSaurabh Tangri {
576eeb9db09SSaurabh Tangri 	/*
577eeb9db09SSaurabh Tangri 	 * Once setup is done earlier, unmap the EFI memory map on mismatched
578eeb9db09SSaurabh Tangri 	 * firmware/kernel architectures since there is no support for runtime
579eeb9db09SSaurabh Tangri 	 * services.
580eeb9db09SSaurabh Tangri 	 */
581eeb9db09SSaurabh Tangri 	if (!efi_runtime_supported()) {
58226d7f65fSMatt Fleming 		pr_info("Setup done, disabling due to 32/64-bit mismatch\n");
5839479c7ceSMatt Fleming 		efi_memmap_unmap();
584eeb9db09SSaurabh Tangri 	}
585eeb9db09SSaurabh Tangri }
58644be28e9SMatt Fleming 
58744be28e9SMatt Fleming /*
58844be28e9SMatt Fleming  * For most modern platforms the preferred method of powering off is via
58944be28e9SMatt Fleming  * ACPI. However, there are some that are known to require the use of
59044be28e9SMatt Fleming  * EFI runtime services and for which ACPI does not work at all.
59144be28e9SMatt Fleming  *
59244be28e9SMatt Fleming  * Using EFI is a last resort, to be used only if no other option
59344be28e9SMatt Fleming  * exists.
59444be28e9SMatt Fleming  */
efi_reboot_required(void)59544be28e9SMatt Fleming bool efi_reboot_required(void)
59644be28e9SMatt Fleming {
59744be28e9SMatt Fleming 	if (!acpi_gbl_reduced_hardware)
59844be28e9SMatt Fleming 		return false;
59944be28e9SMatt Fleming 
60044be28e9SMatt Fleming 	efi_reboot_quirk_mode = EFI_RESET_WARM;
60144be28e9SMatt Fleming 	return true;
60244be28e9SMatt Fleming }
60344be28e9SMatt Fleming 
efi_poweroff_required(void)60444be28e9SMatt Fleming bool efi_poweroff_required(void)
60544be28e9SMatt Fleming {
60613737181SChen Yu 	return acpi_gbl_reduced_hardware || acpi_no_s5;
60744be28e9SMatt Fleming }
6082959c95dSJan Kiszka 
6092959c95dSJan Kiszka #ifdef CONFIG_EFI_CAPSULE_QUIRK_QUARK_CSH
6102959c95dSJan Kiszka 
qrk_capsule_setup_info(struct capsule_info * cap_info,void ** pkbuff,size_t hdr_bytes)6112959c95dSJan Kiszka static int qrk_capsule_setup_info(struct capsule_info *cap_info, void **pkbuff,
6122959c95dSJan Kiszka 				  size_t hdr_bytes)
6132959c95dSJan Kiszka {
6142959c95dSJan Kiszka 	struct quark_security_header *csh = *pkbuff;
6152959c95dSJan Kiszka 
6162959c95dSJan Kiszka 	/* Only process data block that is larger than the security header */
6172959c95dSJan Kiszka 	if (hdr_bytes < sizeof(struct quark_security_header))
6182959c95dSJan Kiszka 		return 0;
6192959c95dSJan Kiszka 
6202959c95dSJan Kiszka 	if (csh->csh_signature != QUARK_CSH_SIGNATURE ||
6212959c95dSJan Kiszka 	    csh->headersize != QUARK_SECURITY_HEADER_SIZE)
6222959c95dSJan Kiszka 		return 1;
6232959c95dSJan Kiszka 
6242959c95dSJan Kiszka 	/* Only process data block if EFI header is included */
6252959c95dSJan Kiszka 	if (hdr_bytes < QUARK_SECURITY_HEADER_SIZE +
6262959c95dSJan Kiszka 			sizeof(efi_capsule_header_t))
6272959c95dSJan Kiszka 		return 0;
6282959c95dSJan Kiszka 
6292959c95dSJan Kiszka 	pr_debug("Quark security header detected\n");
6302959c95dSJan Kiszka 
6312959c95dSJan Kiszka 	if (csh->rsvd_next_header != 0) {
6322959c95dSJan Kiszka 		pr_err("multiple Quark security headers not supported\n");
6332959c95dSJan Kiszka 		return -EINVAL;
6342959c95dSJan Kiszka 	}
6352959c95dSJan Kiszka 
6362959c95dSJan Kiszka 	*pkbuff += csh->headersize;
6372959c95dSJan Kiszka 	cap_info->total_size = csh->headersize;
6382959c95dSJan Kiszka 
6392959c95dSJan Kiszka 	/*
6402959c95dSJan Kiszka 	 * Update the first page pointer to skip over the CSH header.
6412959c95dSJan Kiszka 	 */
642f24c4d47SArd Biesheuvel 	cap_info->phys[0] += csh->headersize;
643f24c4d47SArd Biesheuvel 
644f24c4d47SArd Biesheuvel 	/*
645f24c4d47SArd Biesheuvel 	 * cap_info->capsule should point at a virtual mapping of the entire
646f24c4d47SArd Biesheuvel 	 * capsule, starting at the capsule header. Our image has the Quark
647f24c4d47SArd Biesheuvel 	 * security header prepended, so we cannot rely on the default vmap()
648f24c4d47SArd Biesheuvel 	 * mapping created by the generic capsule code.
649f24c4d47SArd Biesheuvel 	 * Given that the Quark firmware does not appear to care about the
650f24c4d47SArd Biesheuvel 	 * virtual mapping, let's just point cap_info->capsule at our copy
651f24c4d47SArd Biesheuvel 	 * of the capsule header.
652f24c4d47SArd Biesheuvel 	 */
653f24c4d47SArd Biesheuvel 	cap_info->capsule = &cap_info->header;
6542959c95dSJan Kiszka 
6552959c95dSJan Kiszka 	return 1;
6562959c95dSJan Kiszka }
6572959c95dSJan Kiszka 
6582959c95dSJan Kiszka static const struct x86_cpu_id efi_capsule_quirk_ids[] = {
6599595198fSThomas Gleixner 	X86_MATCH_VENDOR_FAM_MODEL(INTEL, 5, INTEL_FAM5_QUARK_X1000,
6609595198fSThomas Gleixner 				   &qrk_capsule_setup_info),
6612959c95dSJan Kiszka 	{ }
6622959c95dSJan Kiszka };
6632959c95dSJan Kiszka 
efi_capsule_setup_info(struct capsule_info * cap_info,void * kbuff,size_t hdr_bytes)6642959c95dSJan Kiszka int efi_capsule_setup_info(struct capsule_info *cap_info, void *kbuff,
6652959c95dSJan Kiszka 			   size_t hdr_bytes)
6662959c95dSJan Kiszka {
6672959c95dSJan Kiszka 	int (*quirk_handler)(struct capsule_info *, void **, size_t);
6682959c95dSJan Kiszka 	const struct x86_cpu_id *id;
6692959c95dSJan Kiszka 	int ret;
6702959c95dSJan Kiszka 
6712959c95dSJan Kiszka 	if (hdr_bytes < sizeof(efi_capsule_header_t))
6722959c95dSJan Kiszka 		return 0;
6732959c95dSJan Kiszka 
6742959c95dSJan Kiszka 	cap_info->total_size = 0;
6752959c95dSJan Kiszka 
6762959c95dSJan Kiszka 	id = x86_match_cpu(efi_capsule_quirk_ids);
6772959c95dSJan Kiszka 	if (id) {
6782959c95dSJan Kiszka 		/*
6792959c95dSJan Kiszka 		 * The quirk handler is supposed to return
6802959c95dSJan Kiszka 		 *  - a value > 0 if the setup should continue, after advancing
6812959c95dSJan Kiszka 		 *    kbuff as needed
6822959c95dSJan Kiszka 		 *  - 0 if not enough hdr_bytes are available yet
6832959c95dSJan Kiszka 		 *  - a negative error code otherwise
6842959c95dSJan Kiszka 		 */
6852959c95dSJan Kiszka 		quirk_handler = (typeof(quirk_handler))id->driver_data;
6862959c95dSJan Kiszka 		ret = quirk_handler(cap_info, &kbuff, hdr_bytes);
6872959c95dSJan Kiszka 		if (ret <= 0)
6882959c95dSJan Kiszka 			return ret;
6892959c95dSJan Kiszka 	}
6902959c95dSJan Kiszka 
6912959c95dSJan Kiszka 	memcpy(&cap_info->header, kbuff, sizeof(cap_info->header));
6922959c95dSJan Kiszka 
6932959c95dSJan Kiszka 	cap_info->total_size += cap_info->header.imagesize;
6942959c95dSJan Kiszka 
6952959c95dSJan Kiszka 	return __efi_capsule_setup_info(cap_info);
6962959c95dSJan Kiszka }
6972959c95dSJan Kiszka 
6982959c95dSJan Kiszka #endif
6993425d934SSai Praneeth 
7003425d934SSai Praneeth /*
7013425d934SSai Praneeth  * If any access by any efi runtime service causes a page fault, then,
7023425d934SSai Praneeth  * 1. If it's efi_reset_system(), reboot through BIOS.
7033425d934SSai Praneeth  * 2. If any other efi runtime service, then
7043425d934SSai Praneeth  *    a. Return error status to the efi caller process.
7053425d934SSai Praneeth  *    b. Disable EFI Runtime Services forever and
7063425d934SSai Praneeth  *    c. Freeze efi_rts_wq and schedule new process.
7073425d934SSai Praneeth  *
7083425d934SSai Praneeth  * @return: Returns, if the page fault is not handled. This function
7093425d934SSai Praneeth  * will never return if the page fault is handled successfully.
7103425d934SSai Praneeth  */
efi_crash_gracefully_on_page_fault(unsigned long phys_addr)711c46f5223SAndy Lutomirski void efi_crash_gracefully_on_page_fault(unsigned long phys_addr)
7123425d934SSai Praneeth {
7133425d934SSai Praneeth 	if (!IS_ENABLED(CONFIG_X86_64))
7143425d934SSai Praneeth 		return;
7153425d934SSai Praneeth 
7163425d934SSai Praneeth 	/*
717c46f5223SAndy Lutomirski 	 * If we get an interrupt/NMI while processing an EFI runtime service
718c46f5223SAndy Lutomirski 	 * then this is a regular OOPS, not an EFI failure.
7193425d934SSai Praneeth 	 */
720c46f5223SAndy Lutomirski 	if (in_interrupt())
721c46f5223SAndy Lutomirski 		return;
722c46f5223SAndy Lutomirski 
723c46f5223SAndy Lutomirski 	/*
724c46f5223SAndy Lutomirski 	 * Make sure that an efi runtime service caused the page fault.
725c46f5223SAndy Lutomirski 	 * READ_ONCE() because we might be OOPSing in a different thread,
726c46f5223SAndy Lutomirski 	 * and we don't want to trip KTSAN while trying to OOPS.
727c46f5223SAndy Lutomirski 	 */
728c46f5223SAndy Lutomirski 	if (READ_ONCE(efi_rts_work.efi_rts_id) == EFI_NONE ||
729c46f5223SAndy Lutomirski 	    current_work() != &efi_rts_work.work)
7303425d934SSai Praneeth 		return;
7313425d934SSai Praneeth 
7323425d934SSai Praneeth 	/*
7333425d934SSai Praneeth 	 * Address range 0x0000 - 0x0fff is always mapped in the efi_pgd, so
7343425d934SSai Praneeth 	 * page faulting on these addresses isn't expected.
7353425d934SSai Praneeth 	 */
736919aef44SQian Cai 	if (phys_addr <= 0x0fff)
7373425d934SSai Praneeth 		return;
7383425d934SSai Praneeth 
7393425d934SSai Praneeth 	/*
7403425d934SSai Praneeth 	 * Print stack trace as it might be useful to know which EFI Runtime
7413425d934SSai Praneeth 	 * Service is buggy.
7423425d934SSai Praneeth 	 */
7433425d934SSai Praneeth 	WARN(1, FW_BUG "Page fault caused by firmware at PA: 0x%lx\n",
7443425d934SSai Praneeth 	     phys_addr);
7453425d934SSai Praneeth 
7463425d934SSai Praneeth 	/*
7473425d934SSai Praneeth 	 * Buggy efi_reset_system() is handled differently from other EFI
7483425d934SSai Praneeth 	 * Runtime Services as it doesn't use efi_rts_wq. Although,
7493425d934SSai Praneeth 	 * native_machine_emergency_restart() says that machine_real_restart()
750d9f6e12fSIngo Molnar 	 * could fail, it's better not to complicate this fault handler
7513425d934SSai Praneeth 	 * because this case occurs *very* rarely and hence could be improved
7523425d934SSai Praneeth 	 * on a need by basis.
7533425d934SSai Praneeth 	 */
7545c418dc7SAnders Roxell 	if (efi_rts_work.efi_rts_id == EFI_RESET_SYSTEM) {
7553425d934SSai Praneeth 		pr_info("efi_reset_system() buggy! Reboot through BIOS\n");
7563425d934SSai Praneeth 		machine_real_restart(MRR_BIOS);
7573425d934SSai Praneeth 		return;
7583425d934SSai Praneeth 	}
7593425d934SSai Praneeth 
7603425d934SSai Praneeth 	/*
7613425d934SSai Praneeth 	 * Before calling EFI Runtime Service, the kernel has switched the
7623425d934SSai Praneeth 	 * calling process to efi_mm. Hence, switch back to task_mm.
7633425d934SSai Praneeth 	 */
7643425d934SSai Praneeth 	arch_efi_call_virt_teardown();
7653425d934SSai Praneeth 
7663425d934SSai Praneeth 	/* Signal error status to the efi caller process */
7673425d934SSai Praneeth 	efi_rts_work.status = EFI_ABORTED;
7683425d934SSai Praneeth 	complete(&efi_rts_work.efi_rts_comp);
7693425d934SSai Praneeth 
7703425d934SSai Praneeth 	clear_bit(EFI_RUNTIME_SERVICES, &efi.flags);
7713425d934SSai Praneeth 	pr_info("Froze efi_rts_wq and disabled EFI Runtime Services\n");
7723425d934SSai Praneeth 
7733425d934SSai Praneeth 	/*
7743425d934SSai Praneeth 	 * Call schedule() in an infinite loop, so that any spurious wake ups
7753425d934SSai Praneeth 	 * will never run efi_rts_wq again.
7763425d934SSai Praneeth 	 */
7773425d934SSai Praneeth 	for (;;) {
7783425d934SSai Praneeth 		set_current_state(TASK_IDLE);
7793425d934SSai Praneeth 		schedule();
7803425d934SSai Praneeth 	}
7813425d934SSai Praneeth }
782