1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-25 01:34:28 +03:00

boot: modernize measure.c

Let's use _cleanup_freepool_, compound literals for initialization,
OFFSETOF() and let's remove some unnecessary casts.

No change in behaviour.
This commit is contained in:
Lennart Poettering 2021-09-20 13:33:18 +02:00
parent b4f25c649d
commit ff3aa8d1e0

View File

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