From 50b0b0d351e892d57a562a28dd8362b1e8cd76a9 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 11 Nov 2022 16:05:03 +0100 Subject: [PATCH 1/2] efi: add efi_guid_equal() helper --- src/boot/efi/devicetree.c | 2 +- src/boot/efi/part-discovery.c | 2 +- src/boot/efi/random-seed.c | 3 +-- src/boot/efi/util.h | 4 ++++ src/boot/efi/vmm.c | 4 ++-- 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/boot/efi/devicetree.c b/src/boot/efi/devicetree.c index 0312670613..daba5582aa 100644 --- a/src/boot/efi/devicetree.c +++ b/src/boot/efi/devicetree.c @@ -10,7 +10,7 @@ static void *get_dtb_table(void) { for (UINTN i = 0; i < ST->NumberOfTableEntries; i++) - if (memcmp(&EfiDtbTableGuid, &ST->ConfigurationTable[i].VendorGuid, sizeof(EfiDtbTableGuid)) == 0) + if (efi_guid_equal(&ST->ConfigurationTable[i].VendorGuid, &EfiDtbTableGuid)) return ST->ConfigurationTable[i].VendorTable; return NULL; } diff --git a/src/boot/efi/part-discovery.c b/src/boot/efi/part-discovery.c index 14479c06ea..2659a5b6b4 100644 --- a/src/boot/efi/part-discovery.c +++ b/src/boot/efi/part-discovery.c @@ -134,7 +134,7 @@ static EFI_STATUS try_gpt( EFI_PARTITION_ENTRY *entry = (EFI_PARTITION_ENTRY *) ((uint8_t *) entries + gpt.gpt_header.SizeOfPartitionEntry * i); - if (memcmp(&entry->PartitionTypeGUID, type, sizeof(entry->PartitionTypeGUID)) != 0) + if (!efi_guid_equal(&entry->PartitionTypeGUID, type)) continue; if (entry->EndingLBA < entry->StartingLBA) /* Bogus? */ diff --git a/src/boot/efi/random-seed.c b/src/boot/efi/random-seed.c index 22ba1c5a30..bdaed2625a 100644 --- a/src/boot/efi/random-seed.c +++ b/src/boot/efi/random-seed.c @@ -142,8 +142,7 @@ EFI_STATUS process_random_seed(EFI_FILE *root_dir) { sha256_process_bytes(HASH_LABEL, sizeof(HASH_LABEL) - 1, &hash); for (size_t i = 0; i < ST->NumberOfTableEntries; ++i) - if (memcmp(&(const EFI_GUID)LINUX_EFI_RANDOM_SEED_TABLE_GUID, - &ST->ConfigurationTable[i].VendorGuid, sizeof(EFI_GUID)) == 0) { + if (efi_guid_equal(&ST->ConfigurationTable[i].VendorGuid, &(const EFI_GUID) LINUX_EFI_RANDOM_SEED_TABLE_GUID)) { previous_seed_table = ST->ConfigurationTable[i].VendorTable; break; } diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index fb525ba636..bacc114114 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -242,3 +242,7 @@ static inline bool in_hypervisor(void) { return false; } #endif + +static inline bool efi_guid_equal(const EFI_GUID *a, const EFI_GUID *b) { + return memcmp(a, b, sizeof(EFI_GUID)) == 0; +} diff --git a/src/boot/efi/vmm.c b/src/boot/efi/vmm.c index 2260b217b7..10d4a75ab2 100644 --- a/src/boot/efi/vmm.c +++ b/src/boot/efi/vmm.c @@ -18,7 +18,7 @@ /* detect direct boot */ bool is_direct_boot(EFI_HANDLE device) { EFI_STATUS err; - VENDOR_DEVICE_PATH *dp; + VENDOR_DEVICE_PATH *dp; /* NB: Alignment of this structure might be quirky! */ err = BS->HandleProtocol(device, &DevicePathProtocol, (void **) &dp); if (err != EFI_SUCCESS) @@ -27,7 +27,7 @@ bool is_direct_boot(EFI_HANDLE device) { /* 'qemu -kernel systemd-bootx64.efi' */ if (dp->Header.Type == MEDIA_DEVICE_PATH && dp->Header.SubType == MEDIA_VENDOR_DP && - memcmp(&dp->Guid, &(EFI_GUID)QEMU_KERNEL_LOADER_FS_MEDIA_GUID, sizeof(EFI_GUID)) == 0) + memcmp(&dp->Guid, &(EFI_GUID)QEMU_KERNEL_LOADER_FS_MEDIA_GUID, sizeof(EFI_GUID)) == 0) /* Don't change to efi_guid_equal() because EFI device path objects are not necessarily aligned! */ return true; /* loaded from firmware volume (sd-boot added to ovmf) */ From a04709c1ac81b28b1a4144166991ac56be94cfcd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 14 Dec 2022 18:48:52 +0100 Subject: [PATCH 2/2] efi: add common implementation for loop finding EFI configuration tables --- src/boot/efi/devicetree.c | 11 ++--------- src/boot/efi/random-seed.c | 6 +----- src/boot/efi/util.c | 8 ++++++++ src/boot/efi/util.h | 2 ++ 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/src/boot/efi/devicetree.c b/src/boot/efi/devicetree.c index daba5582aa..12015fce6b 100644 --- a/src/boot/efi/devicetree.c +++ b/src/boot/efi/devicetree.c @@ -8,13 +8,6 @@ #define FDT_V1_SIZE (7*4) -static void *get_dtb_table(void) { - for (UINTN i = 0; i < ST->NumberOfTableEntries; i++) - if (efi_guid_equal(&ST->ConfigurationTable[i].VendorGuid, &EfiDtbTableGuid)) - return ST->ConfigurationTable[i].VendorTable; - return NULL; -} - static EFI_STATUS devicetree_allocate(struct devicetree_state *state, UINTN size) { UINTN pages = DIV_ROUND_UP(size, EFI_PAGE_SIZE); EFI_STATUS err; @@ -81,7 +74,7 @@ EFI_STATUS devicetree_install(struct devicetree_state *state, EFI_FILE *root_dir assert(root_dir); assert(name); - state->orig = get_dtb_table(); + state->orig = find_configuration_table(&EfiDtbTableGuid); if (!state->orig) return EFI_UNSUPPORTED; @@ -121,7 +114,7 @@ EFI_STATUS devicetree_install_from_memory(struct devicetree_state *state, assert(state); assert(dtb_buffer && dtb_length > 0); - state->orig = get_dtb_table(); + state->orig = find_configuration_table(&EfiDtbTableGuid); if (!state->orig) return EFI_UNSUPPORTED; diff --git a/src/boot/efi/random-seed.c b/src/boot/efi/random-seed.c index bdaed2625a..5b0693dfe1 100644 --- a/src/boot/efi/random-seed.c +++ b/src/boot/efi/random-seed.c @@ -141,11 +141,7 @@ EFI_STATUS process_random_seed(EFI_FILE *root_dir) { /* Some basic domain separation in case somebody uses this data elsewhere */ sha256_process_bytes(HASH_LABEL, sizeof(HASH_LABEL) - 1, &hash); - for (size_t i = 0; i < ST->NumberOfTableEntries; ++i) - if (efi_guid_equal(&ST->ConfigurationTable[i].VendorGuid, &(const EFI_GUID) LINUX_EFI_RANDOM_SEED_TABLE_GUID)) { - previous_seed_table = ST->ConfigurationTable[i].VendorTable; - break; - } + previous_seed_table = find_configuration_table(&(const EFI_GUID) LINUX_EFI_RANDOM_SEED_TABLE_GUID); if (!previous_seed_table) { size = 0; sha256_process_bytes(&size, sizeof(size), &hash); diff --git a/src/boot/efi/util.c b/src/boot/efi/util.c index 1f07fbc38c..0a6bb59dce 100644 --- a/src/boot/efi/util.c +++ b/src/boot/efi/util.c @@ -747,3 +747,11 @@ bool in_hypervisor(void) { return !!(ecx & 0x80000000U); } #endif + +void *find_configuration_table(const EFI_GUID *guid) { + for (UINTN i = 0; i < ST->NumberOfTableEntries; i++) + if (efi_guid_equal(&ST->ConfigurationTable[i].VendorGuid, guid)) + return ST->ConfigurationTable[i].VendorTable; + + return NULL; +} diff --git a/src/boot/efi/util.h b/src/boot/efi/util.h index bacc114114..699f1a2c7c 100644 --- a/src/boot/efi/util.h +++ b/src/boot/efi/util.h @@ -246,3 +246,5 @@ static inline bool in_hypervisor(void) { static inline bool efi_guid_equal(const EFI_GUID *a, const EFI_GUID *b) { return memcmp(a, b, sizeof(EFI_GUID)) == 0; } + +void *find_configuration_table(const EFI_GUID *guid);