1 // SPDX-License-Identifier: GPL-2.0
3 * Helpers for early access to EFI configuration table.
5 * Originally derived from arch/x86/boot/compressed/acpi.c
10 #include <asm/bootparam.h>
13 * efi_get_type - Given a pointer to boot_params, determine the type of EFI environment.
15 * @bp: pointer to boot_params
17 * Return: EFI_TYPE_{32,64} for valid EFI environments, EFI_TYPE_NONE otherwise.
19 enum efi_type efi_get_type(struct boot_params *bp)
26 sig = (char *)&ei->efi_loader_signature;
28 if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
30 } else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) {
33 debug_putstr("No EFI environment detected.\n");
39 * Existing callers like acpi.c treat this case as an indicator to
40 * fall-through to non-EFI, rather than an error, so maintain that
41 * functionality here as well.
43 if (ei->efi_systab_hi || ei->efi_memmap_hi) {
44 debug_putstr("EFI system table is located above 4GB and cannot be accessed.\n");
53 * efi_get_system_table - Given a pointer to boot_params, retrieve the physical address
54 * of the EFI system table.
56 * @bp: pointer to boot_params
58 * Return: EFI system table address on success. On error, return 0.
60 unsigned long efi_get_system_table(struct boot_params *bp)
62 unsigned long sys_tbl_pa;
66 /* Get systab from boot params. */
69 sys_tbl_pa = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32);
71 sys_tbl_pa = ei->efi_systab;
74 debug_putstr("EFI system table not found.");
82 * EFI config table address changes to virtual address after boot, which may
83 * not be accessible for the kexec'd kernel. To address this, kexec provides
84 * the initial physical address via a struct setup_data entry, which is
85 * checked for here, along with some sanity checks.
87 static struct efi_setup_data *get_kexec_setup_data(struct boot_params *bp,
91 struct efi_setup_data *esd = NULL;
92 struct setup_data *data;
95 pa_data = bp->hdr.setup_data;
97 data = (struct setup_data *)pa_data;
98 if (data->type == SETUP_EFI) {
99 esd = (struct efi_setup_data *)(pa_data + sizeof(struct setup_data));
103 pa_data = data->next;
107 * Original ACPI code falls back to attempting normal EFI boot in these
108 * cases, so maintain existing behavior by indicating non-kexec
109 * environment to the caller, but print them for debugging.
111 if (esd && !esd->tables) {
112 debug_putstr("kexec EFI environment missing valid configuration table.\n");
122 * efi_get_conf_table - Given a pointer to boot_params, locate and return the physical
123 * address of EFI configuration table.
125 * @bp: pointer to boot_params
126 * @cfg_tbl_pa: location to store physical address of config table
127 * @cfg_tbl_len: location to store number of config table entries
129 * Return: 0 on success. On error, return params are left unchanged.
131 int efi_get_conf_table(struct boot_params *bp, unsigned long *cfg_tbl_pa,
132 unsigned int *cfg_tbl_len)
134 unsigned long sys_tbl_pa;
138 if (!cfg_tbl_pa || !cfg_tbl_len)
141 sys_tbl_pa = efi_get_system_table(bp);
145 /* Handle EFI bitness properly */
146 et = efi_get_type(bp);
147 if (et == EFI_TYPE_64) {
148 efi_system_table_64_t *stbl = (efi_system_table_64_t *)sys_tbl_pa;
149 struct efi_setup_data *esd;
151 /* kexec provides an alternative EFI conf table, check for it. */
152 esd = get_kexec_setup_data(bp, et);
154 *cfg_tbl_pa = esd ? esd->tables : stbl->tables;
155 *cfg_tbl_len = stbl->nr_tables;
156 } else if (et == EFI_TYPE_32) {
157 efi_system_table_32_t *stbl = (efi_system_table_32_t *)sys_tbl_pa;
159 *cfg_tbl_pa = stbl->tables;
160 *cfg_tbl_len = stbl->nr_tables;
168 /* Get vendor table address/guid from EFI config table at the given index */
169 static int get_vendor_table(void *cfg_tbl, unsigned int idx,
170 unsigned long *vendor_tbl_pa,
171 efi_guid_t *vendor_tbl_guid,
174 if (et == EFI_TYPE_64) {
175 efi_config_table_64_t *tbl_entry = (efi_config_table_64_t *)cfg_tbl + idx;
177 if (!IS_ENABLED(CONFIG_X86_64) && tbl_entry->table >> 32) {
178 debug_putstr("Error: EFI config table entry located above 4GB.\n");
182 *vendor_tbl_pa = tbl_entry->table;
183 *vendor_tbl_guid = tbl_entry->guid;
185 } else if (et == EFI_TYPE_32) {
186 efi_config_table_32_t *tbl_entry = (efi_config_table_32_t *)cfg_tbl + idx;
188 *vendor_tbl_pa = tbl_entry->table;
189 *vendor_tbl_guid = tbl_entry->guid;
198 * efi_find_vendor_table - Given EFI config table, search it for the physical
199 * address of the vendor table associated with GUID.
201 * @bp: pointer to boot_params
202 * @cfg_tbl_pa: pointer to EFI configuration table
203 * @cfg_tbl_len: number of entries in EFI configuration table
204 * @guid: GUID of vendor table
206 * Return: vendor table address on success. On error, return 0.
208 unsigned long efi_find_vendor_table(struct boot_params *bp,
209 unsigned long cfg_tbl_pa,
210 unsigned int cfg_tbl_len,
216 et = efi_get_type(bp);
217 if (et == EFI_TYPE_NONE)
220 for (i = 0; i < cfg_tbl_len; i++) {
221 unsigned long vendor_tbl_pa;
222 efi_guid_t vendor_tbl_guid;
225 ret = get_vendor_table((void *)cfg_tbl_pa, i,
227 &vendor_tbl_guid, et);
231 if (!efi_guidcmp(guid, vendor_tbl_guid))
232 return vendor_tbl_pa;