xref: /openbmc/linux/arch/x86/boot/compressed/efi.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
17c4146e8SMichael Roth // SPDX-License-Identifier: GPL-2.0
27c4146e8SMichael Roth /*
37c4146e8SMichael Roth  * Helpers for early access to EFI configuration table.
47c4146e8SMichael Roth  *
57c4146e8SMichael Roth  * Originally derived from arch/x86/boot/compressed/acpi.c
67c4146e8SMichael Roth  */
77c4146e8SMichael Roth 
87c4146e8SMichael Roth #include "misc.h"
97c4146e8SMichael Roth 
107c4146e8SMichael Roth /**
117c4146e8SMichael Roth  * efi_get_type - Given a pointer to boot_params, determine the type of EFI environment.
127c4146e8SMichael Roth  *
137c4146e8SMichael Roth  * @bp:         pointer to boot_params
147c4146e8SMichael Roth  *
157c4146e8SMichael Roth  * Return: EFI_TYPE_{32,64} for valid EFI environments, EFI_TYPE_NONE otherwise.
167c4146e8SMichael Roth  */
efi_get_type(struct boot_params * bp)177c4146e8SMichael Roth enum efi_type efi_get_type(struct boot_params *bp)
187c4146e8SMichael Roth {
197c4146e8SMichael Roth 	struct efi_info *ei;
207c4146e8SMichael Roth 	enum efi_type et;
217c4146e8SMichael Roth 	const char *sig;
227c4146e8SMichael Roth 
237c4146e8SMichael Roth 	ei = &bp->efi_info;
247c4146e8SMichael Roth 	sig = (char *)&ei->efi_loader_signature;
257c4146e8SMichael Roth 
267c4146e8SMichael Roth 	if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
277c4146e8SMichael Roth 		et = EFI_TYPE_64;
287c4146e8SMichael Roth 	} else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) {
297c4146e8SMichael Roth 		et = EFI_TYPE_32;
307c4146e8SMichael Roth 	} else {
317c4146e8SMichael Roth 		debug_putstr("No EFI environment detected.\n");
327c4146e8SMichael Roth 		et = EFI_TYPE_NONE;
337c4146e8SMichael Roth 	}
347c4146e8SMichael Roth 
357c4146e8SMichael Roth #ifndef CONFIG_X86_64
367c4146e8SMichael Roth 	/*
377c4146e8SMichael Roth 	 * Existing callers like acpi.c treat this case as an indicator to
387c4146e8SMichael Roth 	 * fall-through to non-EFI, rather than an error, so maintain that
397c4146e8SMichael Roth 	 * functionality here as well.
407c4146e8SMichael Roth 	 */
417c4146e8SMichael Roth 	if (ei->efi_systab_hi || ei->efi_memmap_hi) {
427c4146e8SMichael Roth 		debug_putstr("EFI system table is located above 4GB and cannot be accessed.\n");
437c4146e8SMichael Roth 		et = EFI_TYPE_NONE;
447c4146e8SMichael Roth 	}
457c4146e8SMichael Roth #endif
467c4146e8SMichael Roth 
477c4146e8SMichael Roth 	return et;
487c4146e8SMichael Roth }
4958f3e6b7SMichael Roth 
5058f3e6b7SMichael Roth /**
5158f3e6b7SMichael Roth  * efi_get_system_table - Given a pointer to boot_params, retrieve the physical address
5258f3e6b7SMichael Roth  *                        of the EFI system table.
5358f3e6b7SMichael Roth  *
5458f3e6b7SMichael Roth  * @bp:         pointer to boot_params
5558f3e6b7SMichael Roth  *
5658f3e6b7SMichael Roth  * Return: EFI system table address on success. On error, return 0.
5758f3e6b7SMichael Roth  */
efi_get_system_table(struct boot_params * bp)5858f3e6b7SMichael Roth unsigned long efi_get_system_table(struct boot_params *bp)
5958f3e6b7SMichael Roth {
6058f3e6b7SMichael Roth 	unsigned long sys_tbl_pa;
6158f3e6b7SMichael Roth 	struct efi_info *ei;
6258f3e6b7SMichael Roth 	enum efi_type et;
6358f3e6b7SMichael Roth 
6458f3e6b7SMichael Roth 	/* Get systab from boot params. */
6558f3e6b7SMichael Roth 	ei = &bp->efi_info;
6658f3e6b7SMichael Roth #ifdef CONFIG_X86_64
6758f3e6b7SMichael Roth 	sys_tbl_pa = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32);
6858f3e6b7SMichael Roth #else
6958f3e6b7SMichael Roth 	sys_tbl_pa = ei->efi_systab;
7058f3e6b7SMichael Roth #endif
7158f3e6b7SMichael Roth 	if (!sys_tbl_pa) {
7258f3e6b7SMichael Roth 		debug_putstr("EFI system table not found.");
7358f3e6b7SMichael Roth 		return 0;
7458f3e6b7SMichael Roth 	}
7558f3e6b7SMichael Roth 
7658f3e6b7SMichael Roth 	return sys_tbl_pa;
7758f3e6b7SMichael Roth }
7861c14cedSMichael Roth 
79*824f3778SMichael Roth /*
80*824f3778SMichael Roth  * EFI config table address changes to virtual address after boot, which may
81*824f3778SMichael Roth  * not be accessible for the kexec'd kernel. To address this, kexec provides
82*824f3778SMichael Roth  * the initial physical address via a struct setup_data entry, which is
83*824f3778SMichael Roth  * checked for here, along with some sanity checks.
84*824f3778SMichael Roth  */
get_kexec_setup_data(struct boot_params * bp,enum efi_type et)85*824f3778SMichael Roth static struct efi_setup_data *get_kexec_setup_data(struct boot_params *bp,
86*824f3778SMichael Roth 						   enum efi_type et)
87*824f3778SMichael Roth {
88*824f3778SMichael Roth #ifdef CONFIG_X86_64
89*824f3778SMichael Roth 	struct efi_setup_data *esd = NULL;
90*824f3778SMichael Roth 	struct setup_data *data;
91*824f3778SMichael Roth 	u64 pa_data;
92*824f3778SMichael Roth 
93*824f3778SMichael Roth 	pa_data = bp->hdr.setup_data;
94*824f3778SMichael Roth 	while (pa_data) {
95*824f3778SMichael Roth 		data = (struct setup_data *)pa_data;
96*824f3778SMichael Roth 		if (data->type == SETUP_EFI) {
97*824f3778SMichael Roth 			esd = (struct efi_setup_data *)(pa_data + sizeof(struct setup_data));
98*824f3778SMichael Roth 			break;
99*824f3778SMichael Roth 		}
100*824f3778SMichael Roth 
101*824f3778SMichael Roth 		pa_data = data->next;
102*824f3778SMichael Roth 	}
103*824f3778SMichael Roth 
104*824f3778SMichael Roth 	/*
105*824f3778SMichael Roth 	 * Original ACPI code falls back to attempting normal EFI boot in these
106*824f3778SMichael Roth 	 * cases, so maintain existing behavior by indicating non-kexec
107*824f3778SMichael Roth 	 * environment to the caller, but print them for debugging.
108*824f3778SMichael Roth 	 */
109*824f3778SMichael Roth 	if (esd && !esd->tables) {
110*824f3778SMichael Roth 		debug_putstr("kexec EFI environment missing valid configuration table.\n");
111*824f3778SMichael Roth 		return NULL;
112*824f3778SMichael Roth 	}
113*824f3778SMichael Roth 
114*824f3778SMichael Roth 	return esd;
115*824f3778SMichael Roth #endif
116*824f3778SMichael Roth 	return NULL;
117*824f3778SMichael Roth }
118*824f3778SMichael Roth 
11961c14cedSMichael Roth /**
12061c14cedSMichael Roth  * efi_get_conf_table - Given a pointer to boot_params, locate and return the physical
12161c14cedSMichael Roth  *                      address of EFI configuration table.
12261c14cedSMichael Roth  *
12361c14cedSMichael Roth  * @bp:                 pointer to boot_params
12461c14cedSMichael Roth  * @cfg_tbl_pa:         location to store physical address of config table
12561c14cedSMichael Roth  * @cfg_tbl_len:        location to store number of config table entries
12661c14cedSMichael Roth  *
12761c14cedSMichael Roth  * Return: 0 on success. On error, return params are left unchanged.
12861c14cedSMichael Roth  */
efi_get_conf_table(struct boot_params * bp,unsigned long * cfg_tbl_pa,unsigned int * cfg_tbl_len)12961c14cedSMichael Roth int efi_get_conf_table(struct boot_params *bp, unsigned long *cfg_tbl_pa,
13061c14cedSMichael Roth 		       unsigned int *cfg_tbl_len)
13161c14cedSMichael Roth {
13261c14cedSMichael Roth 	unsigned long sys_tbl_pa;
13361c14cedSMichael Roth 	enum efi_type et;
13461c14cedSMichael Roth 	int ret;
13561c14cedSMichael Roth 
13661c14cedSMichael Roth 	if (!cfg_tbl_pa || !cfg_tbl_len)
13761c14cedSMichael Roth 		return -EINVAL;
13861c14cedSMichael Roth 
13961c14cedSMichael Roth 	sys_tbl_pa = efi_get_system_table(bp);
14061c14cedSMichael Roth 	if (!sys_tbl_pa)
14161c14cedSMichael Roth 		return -EINVAL;
14261c14cedSMichael Roth 
14361c14cedSMichael Roth 	/* Handle EFI bitness properly */
14461c14cedSMichael Roth 	et = efi_get_type(bp);
14561c14cedSMichael Roth 	if (et == EFI_TYPE_64) {
14661c14cedSMichael Roth 		efi_system_table_64_t *stbl = (efi_system_table_64_t *)sys_tbl_pa;
147*824f3778SMichael Roth 		struct efi_setup_data *esd;
14861c14cedSMichael Roth 
149*824f3778SMichael Roth 		/* kexec provides an alternative EFI conf table, check for it. */
150*824f3778SMichael Roth 		esd = get_kexec_setup_data(bp, et);
151*824f3778SMichael Roth 
152*824f3778SMichael Roth 		*cfg_tbl_pa = esd ? esd->tables : stbl->tables;
15361c14cedSMichael Roth 		*cfg_tbl_len = stbl->nr_tables;
15461c14cedSMichael Roth 	} else if (et == EFI_TYPE_32) {
15561c14cedSMichael Roth 		efi_system_table_32_t *stbl = (efi_system_table_32_t *)sys_tbl_pa;
15661c14cedSMichael Roth 
15761c14cedSMichael Roth 		*cfg_tbl_pa = stbl->tables;
15861c14cedSMichael Roth 		*cfg_tbl_len = stbl->nr_tables;
15961c14cedSMichael Roth 	} else {
16061c14cedSMichael Roth 		return -EINVAL;
16161c14cedSMichael Roth 	}
16261c14cedSMichael Roth 
16361c14cedSMichael Roth 	return 0;
16461c14cedSMichael Roth }
165dee602ddSMichael Roth 
166dee602ddSMichael Roth /* Get vendor table address/guid from EFI config table at the given index */
get_vendor_table(void * cfg_tbl,unsigned int idx,unsigned long * vendor_tbl_pa,efi_guid_t * vendor_tbl_guid,enum efi_type et)167dee602ddSMichael Roth static int get_vendor_table(void *cfg_tbl, unsigned int idx,
168dee602ddSMichael Roth 			    unsigned long *vendor_tbl_pa,
169dee602ddSMichael Roth 			    efi_guid_t *vendor_tbl_guid,
170dee602ddSMichael Roth 			    enum efi_type et)
171dee602ddSMichael Roth {
172dee602ddSMichael Roth 	if (et == EFI_TYPE_64) {
173dee602ddSMichael Roth 		efi_config_table_64_t *tbl_entry = (efi_config_table_64_t *)cfg_tbl + idx;
174dee602ddSMichael Roth 
175dee602ddSMichael Roth 		if (!IS_ENABLED(CONFIG_X86_64) && tbl_entry->table >> 32) {
176dee602ddSMichael Roth 			debug_putstr("Error: EFI config table entry located above 4GB.\n");
177dee602ddSMichael Roth 			return -EINVAL;
178dee602ddSMichael Roth 		}
179dee602ddSMichael Roth 
180dee602ddSMichael Roth 		*vendor_tbl_pa = tbl_entry->table;
181dee602ddSMichael Roth 		*vendor_tbl_guid = tbl_entry->guid;
182dee602ddSMichael Roth 
183dee602ddSMichael Roth 	} else if (et == EFI_TYPE_32) {
184dee602ddSMichael Roth 		efi_config_table_32_t *tbl_entry = (efi_config_table_32_t *)cfg_tbl + idx;
185dee602ddSMichael Roth 
186dee602ddSMichael Roth 		*vendor_tbl_pa = tbl_entry->table;
187dee602ddSMichael Roth 		*vendor_tbl_guid = tbl_entry->guid;
188dee602ddSMichael Roth 	} else {
189dee602ddSMichael Roth 		return -EINVAL;
190dee602ddSMichael Roth 	}
191dee602ddSMichael Roth 
192dee602ddSMichael Roth 	return 0;
193dee602ddSMichael Roth }
194dee602ddSMichael Roth 
195dee602ddSMichael Roth /**
196dee602ddSMichael Roth  * efi_find_vendor_table - Given EFI config table, search it for the physical
197dee602ddSMichael Roth  *                         address of the vendor table associated with GUID.
198dee602ddSMichael Roth  *
199dee602ddSMichael Roth  * @bp:                pointer to boot_params
200dee602ddSMichael Roth  * @cfg_tbl_pa:        pointer to EFI configuration table
201dee602ddSMichael Roth  * @cfg_tbl_len:       number of entries in EFI configuration table
202dee602ddSMichael Roth  * @guid:              GUID of vendor table
203dee602ddSMichael Roth  *
204dee602ddSMichael Roth  * Return: vendor table address on success. On error, return 0.
205dee602ddSMichael Roth  */
efi_find_vendor_table(struct boot_params * bp,unsigned long cfg_tbl_pa,unsigned int cfg_tbl_len,efi_guid_t guid)206dee602ddSMichael Roth unsigned long efi_find_vendor_table(struct boot_params *bp,
207dee602ddSMichael Roth 				    unsigned long cfg_tbl_pa,
208dee602ddSMichael Roth 				    unsigned int cfg_tbl_len,
209dee602ddSMichael Roth 				    efi_guid_t guid)
210dee602ddSMichael Roth {
211dee602ddSMichael Roth 	enum efi_type et;
212dee602ddSMichael Roth 	unsigned int i;
213dee602ddSMichael Roth 
214dee602ddSMichael Roth 	et = efi_get_type(bp);
215dee602ddSMichael Roth 	if (et == EFI_TYPE_NONE)
216dee602ddSMichael Roth 		return 0;
217dee602ddSMichael Roth 
218dee602ddSMichael Roth 	for (i = 0; i < cfg_tbl_len; i++) {
219dee602ddSMichael Roth 		unsigned long vendor_tbl_pa;
220dee602ddSMichael Roth 		efi_guid_t vendor_tbl_guid;
221dee602ddSMichael Roth 		int ret;
222dee602ddSMichael Roth 
223dee602ddSMichael Roth 		ret = get_vendor_table((void *)cfg_tbl_pa, i,
224dee602ddSMichael Roth 				       &vendor_tbl_pa,
225dee602ddSMichael Roth 				       &vendor_tbl_guid, et);
226dee602ddSMichael Roth 		if (ret)
227dee602ddSMichael Roth 			return 0;
228dee602ddSMichael Roth 
229dee602ddSMichael Roth 		if (!efi_guidcmp(guid, vendor_tbl_guid))
230dee602ddSMichael Roth 			return vendor_tbl_pa;
231dee602ddSMichael Roth 	}
232dee602ddSMichael Roth 
233dee602ddSMichael Roth 	return 0;
234dee602ddSMichael Roth }
235