0001
0002
0003
0004
0005
0006
0007
0008 #include "misc.h"
0009
0010
0011
0012
0013
0014
0015
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
0038
0039
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
0052
0053
0054
0055
0056
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
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
0081
0082
0083
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
0106
0107
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
0121
0122
0123
0124
0125
0126
0127
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
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
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
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
0197
0198
0199
0200
0201
0202
0203
0204
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 }