Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Helpers for early access to EFI configuration table.
0004  *
0005  * Originally derived from arch/x86/boot/compressed/acpi.c
0006  */
0007 
0008 #include "misc.h"
0009 
0010 /**
0011  * efi_get_type - Given a pointer to boot_params, determine the type of EFI environment.
0012  *
0013  * @bp:         pointer to boot_params
0014  *
0015  * Return: EFI_TYPE_{32,64} for valid EFI environments, EFI_TYPE_NONE otherwise.
0016  */
0017 enum efi_type efi_get_type(struct boot_params *bp)
0018 {
0019     struct efi_info *ei;
0020     enum efi_type et;
0021     const char *sig;
0022 
0023     ei = &bp->efi_info;
0024     sig = (char *)&ei->efi_loader_signature;
0025 
0026     if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) {
0027         et = EFI_TYPE_64;
0028     } else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) {
0029         et = EFI_TYPE_32;
0030     } else {
0031         debug_putstr("No EFI environment detected.\n");
0032         et = EFI_TYPE_NONE;
0033     }
0034 
0035 #ifndef CONFIG_X86_64
0036     /*
0037      * Existing callers like acpi.c treat this case as an indicator to
0038      * fall-through to non-EFI, rather than an error, so maintain that
0039      * functionality here as well.
0040      */
0041     if (ei->efi_systab_hi || ei->efi_memmap_hi) {
0042         debug_putstr("EFI system table is located above 4GB and cannot be accessed.\n");
0043         et = EFI_TYPE_NONE;
0044     }
0045 #endif
0046 
0047     return et;
0048 }
0049 
0050 /**
0051  * efi_get_system_table - Given a pointer to boot_params, retrieve the physical address
0052  *                        of the EFI system table.
0053  *
0054  * @bp:         pointer to boot_params
0055  *
0056  * Return: EFI system table address on success. On error, return 0.
0057  */
0058 unsigned long efi_get_system_table(struct boot_params *bp)
0059 {
0060     unsigned long sys_tbl_pa;
0061     struct efi_info *ei;
0062     enum efi_type et;
0063 
0064     /* Get systab from boot params. */
0065     ei = &bp->efi_info;
0066 #ifdef CONFIG_X86_64
0067     sys_tbl_pa = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32);
0068 #else
0069     sys_tbl_pa = ei->efi_systab;
0070 #endif
0071     if (!sys_tbl_pa) {
0072         debug_putstr("EFI system table not found.");
0073         return 0;
0074     }
0075 
0076     return sys_tbl_pa;
0077 }
0078 
0079 /*
0080  * EFI config table address changes to virtual address after boot, which may
0081  * not be accessible for the kexec'd kernel. To address this, kexec provides
0082  * the initial physical address via a struct setup_data entry, which is
0083  * checked for here, along with some sanity checks.
0084  */
0085 static struct efi_setup_data *get_kexec_setup_data(struct boot_params *bp,
0086                            enum efi_type et)
0087 {
0088 #ifdef CONFIG_X86_64
0089     struct efi_setup_data *esd = NULL;
0090     struct setup_data *data;
0091     u64 pa_data;
0092 
0093     pa_data = bp->hdr.setup_data;
0094     while (pa_data) {
0095         data = (struct setup_data *)pa_data;
0096         if (data->type == SETUP_EFI) {
0097             esd = (struct efi_setup_data *)(pa_data + sizeof(struct setup_data));
0098             break;
0099         }
0100 
0101         pa_data = data->next;
0102     }
0103 
0104     /*
0105      * Original ACPI code falls back to attempting normal EFI boot in these
0106      * cases, so maintain existing behavior by indicating non-kexec
0107      * environment to the caller, but print them for debugging.
0108      */
0109     if (esd && !esd->tables) {
0110         debug_putstr("kexec EFI environment missing valid configuration table.\n");
0111         return NULL;
0112     }
0113 
0114     return esd;
0115 #endif
0116     return NULL;
0117 }
0118 
0119 /**
0120  * efi_get_conf_table - Given a pointer to boot_params, locate and return the physical
0121  *                      address of EFI configuration table.
0122  *
0123  * @bp:                 pointer to boot_params
0124  * @cfg_tbl_pa:         location to store physical address of config table
0125  * @cfg_tbl_len:        location to store number of config table entries
0126  *
0127  * Return: 0 on success. On error, return params are left unchanged.
0128  */
0129 int efi_get_conf_table(struct boot_params *bp, unsigned long *cfg_tbl_pa,
0130                unsigned int *cfg_tbl_len)
0131 {
0132     unsigned long sys_tbl_pa;
0133     enum efi_type et;
0134     int ret;
0135 
0136     if (!cfg_tbl_pa || !cfg_tbl_len)
0137         return -EINVAL;
0138 
0139     sys_tbl_pa = efi_get_system_table(bp);
0140     if (!sys_tbl_pa)
0141         return -EINVAL;
0142 
0143     /* Handle EFI bitness properly */
0144     et = efi_get_type(bp);
0145     if (et == EFI_TYPE_64) {
0146         efi_system_table_64_t *stbl = (efi_system_table_64_t *)sys_tbl_pa;
0147         struct efi_setup_data *esd;
0148 
0149         /* kexec provides an alternative EFI conf table, check for it. */
0150         esd = get_kexec_setup_data(bp, et);
0151 
0152         *cfg_tbl_pa = esd ? esd->tables : stbl->tables;
0153         *cfg_tbl_len = stbl->nr_tables;
0154     } else if (et == EFI_TYPE_32) {
0155         efi_system_table_32_t *stbl = (efi_system_table_32_t *)sys_tbl_pa;
0156 
0157         *cfg_tbl_pa = stbl->tables;
0158         *cfg_tbl_len = stbl->nr_tables;
0159     } else {
0160         return -EINVAL;
0161     }
0162 
0163     return 0;
0164 }
0165 
0166 /* Get vendor table address/guid from EFI config table at the given index */
0167 static int get_vendor_table(void *cfg_tbl, unsigned int idx,
0168                 unsigned long *vendor_tbl_pa,
0169                 efi_guid_t *vendor_tbl_guid,
0170                 enum efi_type et)
0171 {
0172     if (et == EFI_TYPE_64) {
0173         efi_config_table_64_t *tbl_entry = (efi_config_table_64_t *)cfg_tbl + idx;
0174 
0175         if (!IS_ENABLED(CONFIG_X86_64) && tbl_entry->table >> 32) {
0176             debug_putstr("Error: EFI config table entry located above 4GB.\n");
0177             return -EINVAL;
0178         }
0179 
0180         *vendor_tbl_pa = tbl_entry->table;
0181         *vendor_tbl_guid = tbl_entry->guid;
0182 
0183     } else if (et == EFI_TYPE_32) {
0184         efi_config_table_32_t *tbl_entry = (efi_config_table_32_t *)cfg_tbl + idx;
0185 
0186         *vendor_tbl_pa = tbl_entry->table;
0187         *vendor_tbl_guid = tbl_entry->guid;
0188     } else {
0189         return -EINVAL;
0190     }
0191 
0192     return 0;
0193 }
0194 
0195 /**
0196  * efi_find_vendor_table - Given EFI config table, search it for the physical
0197  *                         address of the vendor table associated with GUID.
0198  *
0199  * @bp:                pointer to boot_params
0200  * @cfg_tbl_pa:        pointer to EFI configuration table
0201  * @cfg_tbl_len:       number of entries in EFI configuration table
0202  * @guid:              GUID of vendor table
0203  *
0204  * Return: vendor table address on success. On error, return 0.
0205  */
0206 unsigned long efi_find_vendor_table(struct boot_params *bp,
0207                     unsigned long cfg_tbl_pa,
0208                     unsigned int cfg_tbl_len,
0209                     efi_guid_t guid)
0210 {
0211     enum efi_type et;
0212     unsigned int i;
0213 
0214     et = efi_get_type(bp);
0215     if (et == EFI_TYPE_NONE)
0216         return 0;
0217 
0218     for (i = 0; i < cfg_tbl_len; i++) {
0219         unsigned long vendor_tbl_pa;
0220         efi_guid_t vendor_tbl_guid;
0221         int ret;
0222 
0223         ret = get_vendor_table((void *)cfg_tbl_pa, i,
0224                        &vendor_tbl_pa,
0225                        &vendor_tbl_guid, et);
0226         if (ret)
0227             return 0;
0228 
0229         if (!efi_guidcmp(guid, vendor_tbl_guid))
0230             return vendor_tbl_pa;
0231     }
0232 
0233     return 0;
0234 }