]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
boot: modernize measure.c
authorLennart Poettering <lennart@poettering.net>
Mon, 20 Sep 2021 11:33:18 +0000 (13:33 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 20 Sep 2021 20:18:12 +0000 (22:18 +0200)
Let's use _cleanup_freepool_, compound literals for initialization,
OFFSETOF() and let's remove some unnecessary casts.

No change in behaviour.

src/boot/efi/measure.c

index 112acd78c982ba561c3c7b81e5795d35f0dcc9ef..fbca67bbf155fa05ed6eff071ff06cf2fbdef165 100644 (file)
 #include "macro-fundamental.h"
 #include "measure.h"
 #include "missing_efi.h"
+#include "util.h"
 
-static EFI_STATUS tpm1_measure_to_pcr_and_event_log(const EFI_TCG *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer,
-                                                    UINTN buffer_size, const CHAR16 *description) {
-        EFI_STATUS status;
-        TCG_PCR_EVENT *tcg_event;
-        UINT32 event_number;
+static EFI_STATUS tpm1_measure_to_pcr_and_event_log(
+                const EFI_TCG *tcg,
+                UINT32 pcrindex,
+                EFI_PHYSICAL_ADDRESS buffer,
+                UINTN buffer_size,
+                const CHAR16 *description) {
+
+        _cleanup_freepool_ TCG_PCR_EVENT *tcg_event = NULL;
         EFI_PHYSICAL_ADDRESS event_log_last;
+        UINT32 event_number = 1;
         UINTN desc_len;
 
         assert(tcg);
         assert(description);
 
         desc_len = StrSize(description);
-        tcg_event = AllocateZeroPool(desc_len + sizeof(TCG_PCR_EVENT));
-
+        tcg_event = AllocateZeroPool(OFFSETOF(TCG_PCR_EVENT, Event) + desc_len);
         if (!tcg_event)
                 return EFI_OUT_OF_RESOURCES;
 
-        tcg_event->EventSize = desc_len;
-        CopyMem((VOID *) & tcg_event->Event[0], (VOID *) description, desc_len);
-
-        tcg_event->PCRIndex = pcrindex;
-        tcg_event->EventType = EV_IPL;
-
-        event_number = 1;
-        status = uefi_call_wrapper(tcg->HashLogExtendEvent, 7,
-                                   (EFI_TCG *) tcg, buffer, buffer_size, TCG_ALG_SHA, tcg_event, &event_number, &event_log_last);
-
-        if (EFI_ERROR(status))
-                return status;
-
-        uefi_call_wrapper(BS->FreePool, 1, tcg_event);
-
-        return EFI_SUCCESS;
+        *tcg_event = (TCG_PCR_EVENT) {
+                .EventSize = desc_len,
+                .PCRIndex = pcrindex,
+                .EventType = EV_IPL,
+        };
+        CopyMem(tcg_event->Event, description, desc_len);
+
+        return uefi_call_wrapper(
+                        tcg->HashLogExtendEvent, 7,
+                        (EFI_TCG *) tcg,
+                        buffer, buffer_size,
+                        TCG_ALG_SHA,
+                        tcg_event,
+                        &event_number,
+                        &event_log_last);
 }
 
-static EFI_STATUS tpm2_measure_to_pcr_and_event_log(const EFI_TCG2 *tcg, UINT32 pcrindex, const EFI_PHYSICAL_ADDRESS buffer,
-                                                    UINT64 buffer_size, const CHAR16 *description) {
-        EFI_STATUS status;
-        EFI_TCG2_EVENT *tcg_event;
+static EFI_STATUS tpm2_measure_to_pcr_and_event_log(
+                EFI_TCG2 *tcg,
+                UINT32 pcrindex,
+                EFI_PHYSICAL_ADDRESS buffer,
+                UINT64 buffer_size,
+                const CHAR16 *description) {
+
+        _cleanup_freepool_ EFI_TCG2_EVENT *tcg_event = NULL;
         UINTN desc_len;
 
         assert(tcg);
         assert(description);
 
         desc_len = StrSize(description);
-        tcg_event = AllocateZeroPool(sizeof(*tcg_event) - sizeof(tcg_event->Event) + desc_len);
-
+        tcg_event = AllocateZeroPool(OFFSETOF(EFI_TCG2_EVENT, Event) + desc_len);
         if (!tcg_event)
                 return EFI_OUT_OF_RESOURCES;
 
-        tcg_event->Size = sizeof(*tcg_event) - sizeof(tcg_event->Event) + desc_len;
-        tcg_event->Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER);
-        tcg_event->Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION;
-        tcg_event->Header.PCRIndex = pcrindex;
-        tcg_event->Header.EventType = EV_IPL;
-
-        CopyMem((VOID *) tcg_event->Event, (VOID *) description, desc_len);
-
-        status = uefi_call_wrapper(tcg->HashLogExtendEvent, 5, (EFI_TCG2 *) tcg, 0, buffer, (UINT64) buffer_size, tcg_event);
-
-        uefi_call_wrapper(BS->FreePool, 1, tcg_event);
-
-        if (EFI_ERROR(status))
-                return status;
-
-        return EFI_SUCCESS;
+        *tcg_event = (EFI_TCG2_EVENT) {
+                .Size = OFFSETOF(EFI_TCG2_EVENT, Event) + desc_len,
+                .Header.HeaderSize = sizeof(EFI_TCG2_EVENT_HEADER),
+                .Header.HeaderVersion = EFI_TCG2_EVENT_HEADER_VERSION,
+                .Header.PCRIndex = pcrindex,
+                .Header.EventType = EV_IPL,
+        };
+
+        CopyMem(tcg_event->Event, description, desc_len);
+
+        return uefi_call_wrapper(
+                        tcg->HashLogExtendEvent, 5,
+                        tcg,
+                        0,
+                        buffer, buffer_size,
+                        tcg_event);
 }
 
-static EFI_TCG * tcg1_interface_check(void) {
+static EFI_TCG *tcg1_interface_check(void) {
+        EFI_PHYSICAL_ADDRESS event_log_location, event_log_last_entry;
+        TCG_BOOT_SERVICE_CAPABILITY capability = {
+                .Size = sizeof(capability),
+        };
         EFI_STATUS status;
-        EFI_TCG *tcg;
-        TCG_BOOT_SERVICE_CAPABILITY capability;
         UINT32 features;
-        EFI_PHYSICAL_ADDRESS event_log_location;
-        EFI_PHYSICAL_ADDRESS event_log_last_entry;
+        EFI_TCG *tcg;
 
         status = LibLocateProtocol((EFI_GUID*) EFI_TCG_GUID, (void **) &tcg);
-
         if (EFI_ERROR(status))
                 return NULL;
 
-        capability.Size = (UINT8) sizeof(capability);
-        status = uefi_call_wrapper(tcg->StatusCheck, 5, tcg, &capability, &features, &event_log_location, &event_log_last_entry);
-
+        status = uefi_call_wrapper(
+                        tcg->StatusCheck, 5,
+                        tcg,
+                        &capability,
+                        &features,
+                        &event_log_location,
+                        &event_log_last_entry);
         if (EFI_ERROR(status))
                 return NULL;
 
@@ -106,25 +116,24 @@ static EFI_TCG * tcg1_interface_check(void) {
 }
 
 static EFI_TCG2 * tcg2_interface_check(void) {
+        EFI_TCG2_BOOT_SERVICE_CAPABILITY capability = {
+                .Size = sizeof(capability),
+        };
         EFI_STATUS status;
         EFI_TCG2 *tcg;
-        EFI_TCG2_BOOT_SERVICE_CAPABILITY capability;
 
         status = LibLocateProtocol((EFI_GUID*) EFI_TCG2_GUID, (void **) &tcg);
-
         if (EFI_ERROR(status))
                 return NULL;
 
-        capability.Size = (UINT8) sizeof(EFI_TCG2_BOOT_SERVICE_CAPABILITY);
         status = uefi_call_wrapper(tcg->GetCapability, 2, tcg, &capability);
-
         if (EFI_ERROR(status))
                 return NULL;
 
         if (capability.StructureVersion.Major == 1 &&
             capability.StructureVersion.Minor == 0) {
-                TCG_BOOT_SERVICE_CAPABILITY *caps_1_0;
-                caps_1_0 = (TCG_BOOT_SERVICE_CAPABILITY *)&capability;
+                TCG_BOOT_SERVICE_CAPABILITY *caps_1_0 =
+                        (TCG_BOOT_SERVICE_CAPABILITY*) &capability;
                 if (caps_1_0->TPMPresentFlag)
                         return tcg;
         }