Message ID | 20210820151933.22401-27-brijesh.singh@amd.com |
---|---|
State | New |
Headers | show |
Series | [Part1,v5,01/38] x86/mm: Add sev_feature_enabled() helper | expand |
On Fri, Aug 20, 2021 at 10:19:21AM -0500, Brijesh Singh wrote: > From: Michael Roth <michael.roth@amd.com> > > Future patches for SEV-SNP-validated CPUID will also require early > parsing of the EFI configuration. Move the related code into a set of > helpers that can be re-used for that purpose. > > Signed-off-by: Michael Roth <michael.roth@amd.com> > Signed-off-by: Brijesh Singh <brijesh.singh@amd.com> > --- > arch/x86/boot/compressed/Makefile | 1 + > arch/x86/boot/compressed/acpi.c | 113 +++++-------------- > arch/x86/boot/compressed/efi.c | 178 ++++++++++++++++++++++++++++++ > arch/x86/boot/compressed/misc.h | 43 ++++++++ > 4 files changed, 251 insertions(+), 84 deletions(-) > create mode 100644 arch/x86/boot/compressed/efi.c Ok, better, but this patch needs splitting. And I have a good idea how: in at least three patches: 1. Add efi_get_system_table() and use it 2. Add efi_get_conf_table() and use it 3. Add efi_find_vendor_table() and use it This will facilitate review immensely. Also, here's a diff ontop of what to do also, style-wise. - change how you look for the preferred vendor table along with commenting what you do - shorten variable names so that you don't have so many line breaks. Thx. diff --git a/arch/x86/boot/compressed/acpi.c b/arch/x86/boot/compressed/acpi.c index 3a3f997d7210..c22b21e94a95 100644 --- a/arch/x86/boot/compressed/acpi.c +++ b/arch/x86/boot/compressed/acpi.c @@ -20,27 +20,29 @@ */ struct mem_vector immovable_mem[MAX_NUMNODES*2]; -/* - * Search EFI system tables for RSDP. If both ACPI_20_TABLE_GUID and - * ACPI_TABLE_GUID are found, take the former, which has more features. - */ static acpi_physical_address -__efi_get_rsdp_addr(unsigned long config_table_pa, - unsigned int config_table_len, bool efi_64) +__efi_get_rsdp_addr(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len, bool efi_64) { acpi_physical_address rsdp_addr = 0; + #ifdef CONFIG_EFI int ret; - ret = efi_find_vendor_table(config_table_pa, config_table_len, - ACPI_20_TABLE_GUID, efi_64, - (unsigned long *)&rsdp_addr); - if (ret == -ENOENT) - ret = efi_find_vendor_table(config_table_pa, config_table_len, - ACPI_TABLE_GUID, efi_64, - (unsigned long *)&rsdp_addr); + /* + * Search EFI system tables for RSDP. Preferred is ACPI_20_TABLE_GUID to + * ACPI_TABLE_GUID because it has more features. + */ + ret = efi_find_vendor_table(cfg_tbl_pa, cfg_tbl_len, ACPI_20_TABLE_GUID, + efi_64, (unsigned long *)&rsdp_addr); + if (!ret) + return rsdp_addr; + + /* No ACPI_20_TABLE_GUID found, fallback to ACPI_TABLE_GUID. */ + ret = efi_find_vendor_table(cfg_tbl_pa, cfg_tbl_len, ACPI_TABLE_GUID, + efi_64, (unsigned long *)&rsdp_addr); if (ret) debug_putstr("Error getting RSDP address.\n"); + #endif return rsdp_addr; } @@ -100,18 +102,16 @@ static acpi_physical_address kexec_get_rsdp_addr(void) { return 0; } static acpi_physical_address efi_get_rsdp_addr(void) { #ifdef CONFIG_EFI - unsigned long config_table_pa = 0; - unsigned int config_table_len; + unsigned long cfg_tbl_pa = 0; + unsigned int cfg_tbl_len; bool efi_64; int ret; - ret = efi_get_conf_table(boot_params, &config_table_pa, - &config_table_len, &efi_64); - if (ret || !config_table_pa) + ret = efi_get_conf_table(boot_params, &cfg_tbl_pa, &cfg_tbl_len, &efi_64); + if (ret || !cfg_tbl_pa) error("EFI config table not found."); - return __efi_get_rsdp_addr(config_table_pa, config_table_len, - efi_64); + return __efi_get_rsdp_addr(cfg_tbl_pa, cfg_tbl_len, efi_64); #else return 0; #endif diff --git a/arch/x86/boot/compressed/efi.c b/arch/x86/boot/compressed/efi.c index 16ff5cb9a1fb..7ed31b943c04 100644 --- a/arch/x86/boot/compressed/efi.c +++ b/arch/x86/boot/compressed/efi.c @@ -12,14 +12,14 @@ #include <asm/efi.h> /* Get vendor table address/guid from EFI config table at the given index */ -static int get_vendor_table(void *conf_table, unsigned int idx, +static int get_vendor_table(void *cfg_tbl, unsigned int idx, unsigned long *vendor_table_pa, efi_guid_t *vendor_table_guid, bool efi_64) { if (efi_64) { efi_config_table_64_t *table_entry = - (efi_config_table_64_t *)conf_table + idx; + (efi_config_table_64_t *)cfg_tbl + idx; if (!IS_ENABLED(CONFIG_X86_64) && table_entry->table >> 32) { @@ -32,7 +32,7 @@ static int get_vendor_table(void *conf_table, unsigned int idx, } else { efi_config_table_32_t *table_entry = - (efi_config_table_32_t *)conf_table + idx; + (efi_config_table_32_t *)cfg_tbl + idx; *vendor_table_pa = table_entry->table; *vendor_table_guid = table_entry->guid; @@ -45,27 +45,25 @@ static int get_vendor_table(void *conf_table, unsigned int idx, * Given EFI config table, search it for the physical address of the vendor * table associated with GUID. * - * @conf_table: pointer to EFI configuration table - * @conf_table_len: number of entries in EFI configuration table + * @cfg_tbl: pointer to EFI configuration table + * @cfg_tbl_len: number of entries in EFI configuration table * @guid: GUID of vendor table * @efi_64: true if using 64-bit EFI * @vendor_table_pa: location to store physical address of vendor table * * Returns 0 on success. On error, return params are left unchanged. */ -int -efi_find_vendor_table(unsigned long conf_table_pa, unsigned int conf_table_len, - efi_guid_t guid, bool efi_64, - unsigned long *vendor_table_pa) +int efi_find_vendor_table(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len, + efi_guid_t guid, bool efi_64, unsigned long *vendor_table_pa) { unsigned int i; - for (i = 0; i < conf_table_len; i++) { + for (i = 0; i < cfg_tbl_len; i++) { unsigned long vendor_table_pa_tmp; efi_guid_t vendor_table_guid; int ret; - if (get_vendor_table((void *)conf_table_pa, i, + if (get_vendor_table((void *)cfg_tbl_pa, i, &vendor_table_pa_tmp, &vendor_table_guid, efi_64)) return -EINVAL; @@ -88,9 +86,8 @@ efi_find_vendor_table(unsigned long conf_table_pa, unsigned int conf_table_len, * * Returns 0 on success. On error, return params are left unchanged. */ -int -efi_get_system_table(struct boot_params *boot_params, - unsigned long *sys_table_pa, bool *is_efi_64) +int efi_get_system_table(struct boot_params *boot_params, unsigned long *sys_table_pa, + bool *is_efi_64) { unsigned long sys_table; struct efi_info *ei; @@ -137,22 +134,19 @@ efi_get_system_table(struct boot_params *boot_params, * address EFI configuration table. * * @boot_params: pointer to boot_params - * @conf_table_pa: location to store physical address of config table - * @conf_table_len: location to store number of config table entries + * @cfg_tbl_pa: location to store physical address of config table + * @cfg_tbl_len: location to store number of config table entries * @is_efi_64: location to store whether using 64-bit EFI or not * * Returns 0 on success. On error, return params are left unchanged. */ -int -efi_get_conf_table(struct boot_params *boot_params, - unsigned long *conf_table_pa, - unsigned int *conf_table_len, - bool *is_efi_64) +int efi_get_conf_table(struct boot_params *boot_params, unsigned long *cfg_tbl_pa, + unsigned int *cfg_tbl_len, bool *is_efi_64) { unsigned long sys_table_pa = 0; int ret; - if (!conf_table_pa || !conf_table_len || !is_efi_64) + if (!cfg_tbl_pa || !cfg_tbl_len || !is_efi_64) return -EINVAL; ret = efi_get_system_table(boot_params, &sys_table_pa, is_efi_64); @@ -164,14 +158,14 @@ efi_get_conf_table(struct boot_params *boot_params, efi_system_table_64_t *stbl = (efi_system_table_64_t *)sys_table_pa; - *conf_table_pa = stbl->tables; - *conf_table_len = stbl->nr_tables; + *cfg_tbl_pa = stbl->tables; + *cfg_tbl_len = stbl->nr_tables; } else { efi_system_table_32_t *stbl = (efi_system_table_32_t *)sys_table_pa; - *conf_table_pa = stbl->tables; - *conf_table_len = stbl->nr_tables; + *cfg_tbl_pa = stbl->tables; + *cfg_tbl_len = stbl->nr_tables; } return 0; -- Regards/Gruss, Boris. https://people.kernel.org/tglx/notes-about-netiquette
On Wed, Aug 25, 2021 at 05:18:20PM +0200, Borislav Petkov wrote: > On Fri, Aug 20, 2021 at 10:19:21AM -0500, Brijesh Singh wrote: > > From: Michael Roth <michael.roth@amd.com> > > > > Future patches for SEV-SNP-validated CPUID will also require early > > parsing of the EFI configuration. Move the related code into a set of > > helpers that can be re-used for that purpose. > > > > Signed-off-by: Michael Roth <michael.roth@amd.com> > > Signed-off-by: Brijesh Singh <brijesh.singh@amd.com> > > --- > > arch/x86/boot/compressed/Makefile | 1 + > > arch/x86/boot/compressed/acpi.c | 113 +++++-------------- > > arch/x86/boot/compressed/efi.c | 178 ++++++++++++++++++++++++++++++ > > arch/x86/boot/compressed/misc.h | 43 ++++++++ > > 4 files changed, 251 insertions(+), 84 deletions(-) > > create mode 100644 arch/x86/boot/compressed/efi.c > > Ok, better, but this patch needs splitting. And I have a good idea how: > in at least three patches: > > 1. Add efi_get_system_table() and use it > 2. Add efi_get_conf_table() and use it > 3. Add efi_find_vendor_table() and use it > > This will facilitate review immensely. Ok, that makes sense. > > Also, here's a diff ontop of what to do also, style-wise. > > - change how you look for the preferred vendor table along with commenting what you do > - shorten variable names so that you don't have so many line breaks. Thanks for the suggestions, I'll incorporate those changes in the next spin as well. > > Thx. > > diff --git a/arch/x86/boot/compressed/acpi.c b/arch/x86/boot/compressed/acpi.c > index 3a3f997d7210..c22b21e94a95 100644 > --- a/arch/x86/boot/compressed/acpi.c > +++ b/arch/x86/boot/compressed/acpi.c > @@ -20,27 +20,29 @@ > */ > struct mem_vector immovable_mem[MAX_NUMNODES*2]; > > -/* > - * Search EFI system tables for RSDP. If both ACPI_20_TABLE_GUID and > - * ACPI_TABLE_GUID are found, take the former, which has more features. > - */ > static acpi_physical_address > -__efi_get_rsdp_addr(unsigned long config_table_pa, > - unsigned int config_table_len, bool efi_64) > +__efi_get_rsdp_addr(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len, bool efi_64) > { > acpi_physical_address rsdp_addr = 0; > + > #ifdef CONFIG_EFI > int ret; > > - ret = efi_find_vendor_table(config_table_pa, config_table_len, > - ACPI_20_TABLE_GUID, efi_64, > - (unsigned long *)&rsdp_addr); > - if (ret == -ENOENT) > - ret = efi_find_vendor_table(config_table_pa, config_table_len, > - ACPI_TABLE_GUID, efi_64, > - (unsigned long *)&rsdp_addr); > + /* > + * Search EFI system tables for RSDP. Preferred is ACPI_20_TABLE_GUID to > + * ACPI_TABLE_GUID because it has more features. > + */ > + ret = efi_find_vendor_table(cfg_tbl_pa, cfg_tbl_len, ACPI_20_TABLE_GUID, > + efi_64, (unsigned long *)&rsdp_addr); > + if (!ret) > + return rsdp_addr; > + > + /* No ACPI_20_TABLE_GUID found, fallback to ACPI_TABLE_GUID. */ > + ret = efi_find_vendor_table(cfg_tbl_pa, cfg_tbl_len, ACPI_TABLE_GUID, > + efi_64, (unsigned long *)&rsdp_addr); > if (ret) > debug_putstr("Error getting RSDP address.\n"); > + > #endif > return rsdp_addr; > } > @@ -100,18 +102,16 @@ static acpi_physical_address kexec_get_rsdp_addr(void) { return 0; } > static acpi_physical_address efi_get_rsdp_addr(void) > { > #ifdef CONFIG_EFI > - unsigned long config_table_pa = 0; > - unsigned int config_table_len; > + unsigned long cfg_tbl_pa = 0; > + unsigned int cfg_tbl_len; > bool efi_64; > int ret; > > - ret = efi_get_conf_table(boot_params, &config_table_pa, > - &config_table_len, &efi_64); > - if (ret || !config_table_pa) > + ret = efi_get_conf_table(boot_params, &cfg_tbl_pa, &cfg_tbl_len, &efi_64); > + if (ret || !cfg_tbl_pa) > error("EFI config table not found."); > > - return __efi_get_rsdp_addr(config_table_pa, config_table_len, > - efi_64); > + return __efi_get_rsdp_addr(cfg_tbl_pa, cfg_tbl_len, efi_64); > #else > return 0; > #endif > diff --git a/arch/x86/boot/compressed/efi.c b/arch/x86/boot/compressed/efi.c > index 16ff5cb9a1fb..7ed31b943c04 100644 > --- a/arch/x86/boot/compressed/efi.c > +++ b/arch/x86/boot/compressed/efi.c > @@ -12,14 +12,14 @@ > #include <asm/efi.h> > > /* Get vendor table address/guid from EFI config table at the given index */ > -static int get_vendor_table(void *conf_table, unsigned int idx, > +static int get_vendor_table(void *cfg_tbl, unsigned int idx, > unsigned long *vendor_table_pa, > efi_guid_t *vendor_table_guid, > bool efi_64) > { > if (efi_64) { > efi_config_table_64_t *table_entry = > - (efi_config_table_64_t *)conf_table + idx; > + (efi_config_table_64_t *)cfg_tbl + idx; > > if (!IS_ENABLED(CONFIG_X86_64) && > table_entry->table >> 32) { > @@ -32,7 +32,7 @@ static int get_vendor_table(void *conf_table, unsigned int idx, > > } else { > efi_config_table_32_t *table_entry = > - (efi_config_table_32_t *)conf_table + idx; > + (efi_config_table_32_t *)cfg_tbl + idx; > > *vendor_table_pa = table_entry->table; > *vendor_table_guid = table_entry->guid; > @@ -45,27 +45,25 @@ static int get_vendor_table(void *conf_table, unsigned int idx, > * Given EFI config table, search it for the physical address of the vendor > * table associated with GUID. > * > - * @conf_table: pointer to EFI configuration table > - * @conf_table_len: number of entries in EFI configuration table > + * @cfg_tbl: pointer to EFI configuration table > + * @cfg_tbl_len: number of entries in EFI configuration table > * @guid: GUID of vendor table > * @efi_64: true if using 64-bit EFI > * @vendor_table_pa: location to store physical address of vendor table > * > * Returns 0 on success. On error, return params are left unchanged. > */ > -int > -efi_find_vendor_table(unsigned long conf_table_pa, unsigned int conf_table_len, > - efi_guid_t guid, bool efi_64, > - unsigned long *vendor_table_pa) > +int efi_find_vendor_table(unsigned long cfg_tbl_pa, unsigned int cfg_tbl_len, > + efi_guid_t guid, bool efi_64, unsigned long *vendor_table_pa) > { > unsigned int i; > > - for (i = 0; i < conf_table_len; i++) { > + for (i = 0; i < cfg_tbl_len; i++) { > unsigned long vendor_table_pa_tmp; > efi_guid_t vendor_table_guid; > int ret; > > - if (get_vendor_table((void *)conf_table_pa, i, > + if (get_vendor_table((void *)cfg_tbl_pa, i, > &vendor_table_pa_tmp, > &vendor_table_guid, efi_64)) > return -EINVAL; > @@ -88,9 +86,8 @@ efi_find_vendor_table(unsigned long conf_table_pa, unsigned int conf_table_len, > * > * Returns 0 on success. On error, return params are left unchanged. > */ > -int > -efi_get_system_table(struct boot_params *boot_params, > - unsigned long *sys_table_pa, bool *is_efi_64) > +int efi_get_system_table(struct boot_params *boot_params, unsigned long *sys_table_pa, > + bool *is_efi_64) > { > unsigned long sys_table; > struct efi_info *ei; > @@ -137,22 +134,19 @@ efi_get_system_table(struct boot_params *boot_params, > * address EFI configuration table. > * > * @boot_params: pointer to boot_params > - * @conf_table_pa: location to store physical address of config table > - * @conf_table_len: location to store number of config table entries > + * @cfg_tbl_pa: location to store physical address of config table > + * @cfg_tbl_len: location to store number of config table entries > * @is_efi_64: location to store whether using 64-bit EFI or not > * > * Returns 0 on success. On error, return params are left unchanged. > */ > -int > -efi_get_conf_table(struct boot_params *boot_params, > - unsigned long *conf_table_pa, > - unsigned int *conf_table_len, > - bool *is_efi_64) > +int efi_get_conf_table(struct boot_params *boot_params, unsigned long *cfg_tbl_pa, > + unsigned int *cfg_tbl_len, bool *is_efi_64) > { > unsigned long sys_table_pa = 0; > int ret; > > - if (!conf_table_pa || !conf_table_len || !is_efi_64) > + if (!cfg_tbl_pa || !cfg_tbl_len || !is_efi_64) > return -EINVAL; > > ret = efi_get_system_table(boot_params, &sys_table_pa, is_efi_64); > @@ -164,14 +158,14 @@ efi_get_conf_table(struct boot_params *boot_params, > efi_system_table_64_t *stbl = > (efi_system_table_64_t *)sys_table_pa; > > - *conf_table_pa = stbl->tables; > - *conf_table_len = stbl->nr_tables; > + *cfg_tbl_pa = stbl->tables; > + *cfg_tbl_len = stbl->nr_tables; > } else { > efi_system_table_32_t *stbl = > (efi_system_table_32_t *)sys_table_pa; > > - *conf_table_pa = stbl->tables; > - *conf_table_len = stbl->nr_tables; > + *cfg_tbl_pa = stbl->tables; > + *cfg_tbl_len = stbl->nr_tables; > } > > return 0; > > -- > Regards/Gruss, > Boris. > > https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fpeople.kernel.org%2Ftglx%2Fnotes-about-netiquette&data=04%7C01%7CMichael.Roth%40amd.com%7Cb4091a3e85cd463b1cb608d967db81ec%7C3dd8961fe4884e608e11a82d994e183d%7C0%7C0%7C637655014764920013%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=rvxyc%2FAURXMBWX5lkUHQoM1a%2FlGt4ZtcfGCJZ1TjIQU%3D&reserved=0
diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index 431bf7f846c3..d364192c2367 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -100,6 +100,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o +vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi.o efi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.a $(obj)/vmlinux: $(vmlinux-objs-y) $(efi-obj-y) FORCE diff --git a/arch/x86/boot/compressed/acpi.c b/arch/x86/boot/compressed/acpi.c index 8bcbcee54aa1..3a3f997d7210 100644 --- a/arch/x86/boot/compressed/acpi.c +++ b/arch/x86/boot/compressed/acpi.c @@ -25,41 +25,22 @@ struct mem_vector immovable_mem[MAX_NUMNODES*2]; * ACPI_TABLE_GUID are found, take the former, which has more features. */ static acpi_physical_address -__efi_get_rsdp_addr(unsigned long config_tables, unsigned int nr_tables, - bool efi_64) +__efi_get_rsdp_addr(unsigned long config_table_pa, + unsigned int config_table_len, bool efi_64) { acpi_physical_address rsdp_addr = 0; - #ifdef CONFIG_EFI - int i; - - /* Get EFI tables from systab. */ - for (i = 0; i < nr_tables; i++) { - acpi_physical_address table; - efi_guid_t guid; - - if (efi_64) { - efi_config_table_64_t *tbl = (efi_config_table_64_t *)config_tables + i; - - guid = tbl->guid; - table = tbl->table; - - if (!IS_ENABLED(CONFIG_X86_64) && table >> 32) { - debug_putstr("Error getting RSDP address: EFI config table located above 4GB.\n"); - return 0; - } - } else { - efi_config_table_32_t *tbl = (efi_config_table_32_t *)config_tables + i; - - guid = tbl->guid; - table = tbl->table; - } + int ret; - if (!(efi_guidcmp(guid, ACPI_TABLE_GUID))) - rsdp_addr = table; - else if (!(efi_guidcmp(guid, ACPI_20_TABLE_GUID))) - return table; - } + ret = efi_find_vendor_table(config_table_pa, config_table_len, + ACPI_20_TABLE_GUID, efi_64, + (unsigned long *)&rsdp_addr); + if (ret == -ENOENT) + ret = efi_find_vendor_table(config_table_pa, config_table_len, + ACPI_TABLE_GUID, efi_64, + (unsigned long *)&rsdp_addr); + if (ret) + debug_putstr("Error getting RSDP address.\n"); #endif return rsdp_addr; } @@ -87,7 +68,9 @@ static acpi_physical_address kexec_get_rsdp_addr(void) efi_system_table_64_t *systab; struct efi_setup_data *esd; struct efi_info *ei; + bool efi_64; char *sig; + int ret; esd = (struct efi_setup_data *)get_kexec_setup_data_addr(); if (!esd) @@ -98,18 +81,16 @@ static acpi_physical_address kexec_get_rsdp_addr(void) return 0; } - ei = &boot_params->efi_info; - sig = (char *)&ei->efi_loader_signature; - if (strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) { + /* Get systab from boot params. */ + ret = efi_get_system_table(boot_params, (unsigned long *)&systab, &efi_64); + if (ret) + error("EFI system table not found in kexec boot_params."); + + if (!efi_64) { debug_putstr("Wrong kexec EFI loader signature.\n"); return 0; } - /* Get systab from boot params. */ - systab = (efi_system_table_64_t *) (ei->efi_systab | ((__u64)ei->efi_systab_hi << 32)); - if (!systab) - error("EFI system table not found in kexec boot_params."); - return __efi_get_rsdp_addr((unsigned long)esd->tables, systab->nr_tables, true); } #else @@ -119,54 +100,18 @@ static acpi_physical_address kexec_get_rsdp_addr(void) { return 0; } static acpi_physical_address efi_get_rsdp_addr(void) { #ifdef CONFIG_EFI - unsigned long systab, config_tables; - unsigned int nr_tables; - struct efi_info *ei; + unsigned long config_table_pa = 0; + unsigned int config_table_len; bool efi_64; - char *sig; - - ei = &boot_params->efi_info; - sig = (char *)&ei->efi_loader_signature; - - if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) { - efi_64 = true; - } else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) { - efi_64 = false; - } else { - debug_putstr("Wrong EFI loader signature.\n"); - return 0; - } - - /* Get systab from boot params. */ -#ifdef CONFIG_X86_64 - systab = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32); -#else - if (ei->efi_systab_hi || ei->efi_memmap_hi) { - debug_putstr("Error getting RSDP address: EFI system table located above 4GB.\n"); - return 0; - } - systab = ei->efi_systab; -#endif - if (!systab) - error("EFI system table not found."); - - /* Handle EFI bitness properly */ - if (efi_64) { - efi_system_table_64_t *stbl = (efi_system_table_64_t *)systab; - - config_tables = stbl->tables; - nr_tables = stbl->nr_tables; - } else { - efi_system_table_32_t *stbl = (efi_system_table_32_t *)systab; - - config_tables = stbl->tables; - nr_tables = stbl->nr_tables; - } + int ret; - if (!config_tables) - error("EFI config tables not found."); + ret = efi_get_conf_table(boot_params, &config_table_pa, + &config_table_len, &efi_64); + if (ret || !config_table_pa) + error("EFI config table not found."); - return __efi_get_rsdp_addr(config_tables, nr_tables, efi_64); + return __efi_get_rsdp_addr(config_table_pa, config_table_len, + efi_64); #else return 0; #endif diff --git a/arch/x86/boot/compressed/efi.c b/arch/x86/boot/compressed/efi.c new file mode 100644 index 000000000000..16ff5cb9a1fb --- /dev/null +++ b/arch/x86/boot/compressed/efi.c @@ -0,0 +1,178 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Helpers for early access to EFI configuration table + * + * Copyright (C) 2021 Advanced Micro Devices, Inc. + * + * Author: Michael Roth <michael.roth@amd.com> + */ + +#include "misc.h" +#include <linux/efi.h> +#include <asm/efi.h> + +/* Get vendor table address/guid from EFI config table at the given index */ +static int get_vendor_table(void *conf_table, unsigned int idx, + unsigned long *vendor_table_pa, + efi_guid_t *vendor_table_guid, + bool efi_64) +{ + if (efi_64) { + efi_config_table_64_t *table_entry = + (efi_config_table_64_t *)conf_table + idx; + + if (!IS_ENABLED(CONFIG_X86_64) && + table_entry->table >> 32) { + debug_putstr("Error: EFI config table entry located above 4GB.\n"); + return -EINVAL; + } + + *vendor_table_pa = table_entry->table; + *vendor_table_guid = table_entry->guid; + + } else { + efi_config_table_32_t *table_entry = + (efi_config_table_32_t *)conf_table + idx; + + *vendor_table_pa = table_entry->table; + *vendor_table_guid = table_entry->guid; + } + + return 0; +} + +/** + * Given EFI config table, search it for the physical address of the vendor + * table associated with GUID. + * + * @conf_table: pointer to EFI configuration table + * @conf_table_len: number of entries in EFI configuration table + * @guid: GUID of vendor table + * @efi_64: true if using 64-bit EFI + * @vendor_table_pa: location to store physical address of vendor table + * + * Returns 0 on success. On error, return params are left unchanged. + */ +int +efi_find_vendor_table(unsigned long conf_table_pa, unsigned int conf_table_len, + efi_guid_t guid, bool efi_64, + unsigned long *vendor_table_pa) +{ + unsigned int i; + + for (i = 0; i < conf_table_len; i++) { + unsigned long vendor_table_pa_tmp; + efi_guid_t vendor_table_guid; + int ret; + + if (get_vendor_table((void *)conf_table_pa, i, + &vendor_table_pa_tmp, + &vendor_table_guid, efi_64)) + return -EINVAL; + + if (!efi_guidcmp(guid, vendor_table_guid)) { + *vendor_table_pa = vendor_table_pa_tmp; + return 0; + } + } + + return -ENOENT; +} + +/** + * Given boot_params, retrieve the physical address of EFI system table. + * + * @boot_params: pointer to boot_params + * @sys_table_pa: location to store physical address of system table + * @is_efi_64: location to store whether using 64-bit EFI or not + * + * Returns 0 on success. On error, return params are left unchanged. + */ +int +efi_get_system_table(struct boot_params *boot_params, + unsigned long *sys_table_pa, bool *is_efi_64) +{ + unsigned long sys_table; + struct efi_info *ei; + bool efi_64; + char *sig; + + if (!sys_table_pa || !is_efi_64) + return -EINVAL; + + ei = &boot_params->efi_info; + sig = (char *)&ei->efi_loader_signature; + + if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) { + efi_64 = true; + } else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) { + efi_64 = false; + } else { + debug_putstr("Wrong EFI loader signature.\n"); + return -ENOENT; + } + + /* Get systab from boot params. */ +#ifdef CONFIG_X86_64 + sys_table = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32); +#else + if (ei->efi_systab_hi || ei->efi_memmap_hi) { + debug_putstr("Error: EFI system table located above 4GB.\n"); + return -EINVAL; + } + sys_table = ei->efi_systab; +#endif + if (!sys_table) { + debug_putstr("EFI system table not found."); + return -ENOENT; + } + + *sys_table_pa = sys_table; + *is_efi_64 = efi_64; + return 0; +} + +/** + * Given boot_params, locate EFI system table from it and return the physical + * address EFI configuration table. + * + * @boot_params: pointer to boot_params + * @conf_table_pa: location to store physical address of config table + * @conf_table_len: location to store number of config table entries + * @is_efi_64: location to store whether using 64-bit EFI or not + * + * Returns 0 on success. On error, return params are left unchanged. + */ +int +efi_get_conf_table(struct boot_params *boot_params, + unsigned long *conf_table_pa, + unsigned int *conf_table_len, + bool *is_efi_64) +{ + unsigned long sys_table_pa = 0; + int ret; + + if (!conf_table_pa || !conf_table_len || !is_efi_64) + return -EINVAL; + + ret = efi_get_system_table(boot_params, &sys_table_pa, is_efi_64); + if (ret) + return ret; + + /* Handle EFI bitness properly */ + if (*is_efi_64) { + efi_system_table_64_t *stbl = + (efi_system_table_64_t *)sys_table_pa; + + *conf_table_pa = stbl->tables; + *conf_table_len = stbl->nr_tables; + } else { + efi_system_table_32_t *stbl = + (efi_system_table_32_t *)sys_table_pa; + + *conf_table_pa = stbl->tables; + *conf_table_len = stbl->nr_tables; + } + + return 0; +} diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 822e0c254b9a..16b092fd7aa1 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -21,6 +21,7 @@ #include <linux/screen_info.h> #include <linux/elf.h> #include <linux/io.h> +#include <linux/efi.h> #include <asm/page.h> #include <asm/boot.h> #include <asm/bootparam.h> @@ -174,4 +175,46 @@ void boot_stage2_vc(void); unsigned long sev_verify_cbit(unsigned long cr3); +#ifdef CONFIG_EFI +/* helpers for early EFI config table access */ +int +efi_find_vendor_table(unsigned long conf_table_pa, unsigned int conf_table_len, + efi_guid_t guid, bool efi_64, + unsigned long *vendor_table_pa); + +int efi_get_system_table(struct boot_params *boot_params, + unsigned long *sys_table_pa, + bool *is_efi_64); + +int efi_get_conf_table(struct boot_params *boot_params, + unsigned long *conf_table_pa, + unsigned int *conf_table_len, + bool *is_efi_64); +#else +static inline int +efi_find_vendor_table(unsigned long conf_table_pa, unsigned int conf_table_len, + efi_guid_t guid, bool efi_64, + unsigned long *vendor_table_pa) +{ + return -ENOENT; +} + +static inline int +efi_get_system_table(struct boot_params *boot_params, + unsigned long *sys_table_pa, + bool *is_efi_64) +{ + return -ENOENT; +} + +static inline int +efi_get_conf_table(struct boot_params *boot_params, + unsigned long *conf_table_pa, + unsigned int *conf_table_len, + bool *is_efi_64) +{ + return -ENOENT; +} +#endif /* CONFIG_EFI */ + #endif /* BOOT_COMPRESSED_MISC_H */