Skip to content

Commit

Permalink
tcg: PcrEvent/PcrEventInputs: add new_in_box constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasbishop committed Jul 15, 2024
1 parent 6c726f3 commit 2e0be2a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
38 changes: 38 additions & 0 deletions uefi/src/proto/tcg/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ use core::marker::PhantomData;
use core::{mem, ptr};
use ptr_meta::Pointee;

#[cfg(feature = "alloc")]
use {crate::mem::make_boxed, alloc::boxed::Box};

#[cfg(all(feature = "unstable", feature = "alloc"))]
use {alloc::alloc::Global, core::alloc::Allocator};

/// 20-byte SHA-1 digest.
pub type Sha1Digest = [u8; 20];

Expand Down Expand Up @@ -165,6 +171,32 @@ impl PcrEvent {
}
}

/// Create a new `PcrEvent` in a [`Box`].
///
/// # Errors
///
/// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too
/// large.
#[cfg(feature = "alloc")]
pub fn new_in_box(
pcr_index: PcrIndex,
event_type: EventType,
digest: Sha1Digest,
event_data: &[u8],
) -> Result<Box<Self>> {
#[cfg(not(feature = "unstable"))]
{
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data))
}
#[cfg(feature = "unstable")]
{
make_boxed(
|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data),
Global,
)
}
}

/// PCR index for the event.
#[must_use]
pub fn pcr_index(&self) -> PcrIndex {
Expand Down Expand Up @@ -575,6 +607,12 @@ mod tests {
// Event data
0x14, 0x15, 0x16, 0x17,
]);

// Check that `new_in_box` gives the same value.
assert_eq!(
event,
&*PcrEvent::new_in_box(PcrIndex(4), EventType::IPL, digest, &data).unwrap()
);
}

#[test]
Expand Down
37 changes: 37 additions & 0 deletions uefi/src/proto/tcg/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ use core::marker::PhantomData;
use core::{mem, ptr, slice};
use ptr_meta::{Pointee, PtrExt};

#[cfg(feature = "alloc")]
use {crate::mem::make_boxed, alloc::boxed::Box};

#[cfg(all(feature = "unstable", feature = "alloc"))]
use {alloc::alloc::Global, core::alloc::Allocator};

/// Version information.
///
/// Layout compatible with the C type `EFI_TG2_VERSION`.
Expand Down Expand Up @@ -210,6 +216,31 @@ impl PcrEventInputs {
Ok(&mut *ptr)
}
}

/// Create a new `PcrEventInputs` in a [`Box`].
///
/// # Errors
///
/// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too
/// large.
#[cfg(feature = "alloc")]
pub fn new_in_box(
pcr_index: PcrIndex,
event_type: EventType,
event_data: &[u8],
) -> Result<Box<Self>> {
#[cfg(not(feature = "unstable"))]
{
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data))
}
#[cfg(feature = "unstable")]
{
make_boxed(
|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data),
Global,
)
}
}
}

impl Align for PcrEventInputs {
Expand Down Expand Up @@ -838,6 +869,12 @@ mod tests {
// Event data
0x12, 0x13, 0x14, 0x15,
]);

// Check that `new_in_box` gives the same value.
assert_eq!(
event,
&*PcrEventInputs::new_in_box(PcrIndex(4), EventType::IPL, &event_data).unwrap()
);
}

#[test]
Expand Down

0 comments on commit 2e0be2a

Please sign in to comment.