Skip to content

Commit 0a771bf

Browse files
tcg: PcrEvent/PcrEventInputs: add new_in_box constructors
1 parent 4f3ccc2 commit 0a771bf

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

uefi/src/proto/tcg/v1.rs

+25
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ use core::marker::PhantomData;
1818
use core::{mem, ptr};
1919
use ptr_meta::Pointee;
2020

21+
#[cfg(feature = "alloc")]
22+
use {crate::mem::make_boxed, alloc::boxed::Box};
23+
2124
/// 20-byte SHA-1 digest.
2225
pub type Sha1Digest = [u8; 20];
2326

@@ -165,6 +168,22 @@ impl PcrEvent {
165168
}
166169
}
167170

171+
/// Create a new `PcrEvent` in a [`Box`].
172+
///
173+
/// # Errors
174+
///
175+
/// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too
176+
/// large.
177+
#[cfg(feature = "alloc")]
178+
pub fn new_in_box<'buf>(
179+
pcr_index: PcrIndex,
180+
event_type: EventType,
181+
digest: Sha1Digest,
182+
event_data: &[u8],
183+
) -> Result<Box<Self>> {
184+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data))
185+
}
186+
168187
/// PCR index for the event.
169188
#[must_use]
170189
pub fn pcr_index(&self) -> PcrIndex {
@@ -575,6 +594,12 @@ mod tests {
575594
// Event data
576595
0x14, 0x15, 0x16, 0x17,
577596
]);
597+
598+
// Check that `new_in_box` gives the same value.
599+
assert_eq!(
600+
event,
601+
&*PcrEvent::new_in_box(PcrIndex(4), EventType::IPL, digest, &data).unwrap()
602+
);
578603
}
579604

580605
#[test]

uefi/src/proto/tcg/v2.rs

+24
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ use core::marker::PhantomData;
2121
use core::{mem, ptr, slice};
2222
use ptr_meta::{Pointee, PtrExt};
2323

24+
#[cfg(feature = "alloc")]
25+
use {crate::mem::make_boxed, alloc::boxed::Box};
26+
2427
/// Version information.
2528
///
2629
/// Layout compatible with the C type `EFI_TG2_VERSION`.
@@ -210,6 +213,21 @@ impl PcrEventInputs {
210213
Ok(&mut *ptr)
211214
}
212215
}
216+
217+
/// Create a new `PcrEventInputs` in a [`Box`].
218+
///
219+
/// # Errors
220+
///
221+
/// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too
222+
/// large.
223+
#[cfg(feature = "alloc")]
224+
pub fn new_in_box<'buf>(
225+
pcr_index: PcrIndex,
226+
event_type: EventType,
227+
event_data: &[u8],
228+
) -> Result<Box<Self>> {
229+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data))
230+
}
213231
}
214232

215233
impl Align for PcrEventInputs {
@@ -838,6 +856,12 @@ mod tests {
838856
// Event data
839857
0x12, 0x13, 0x14, 0x15,
840858
]);
859+
860+
// Check that `new_in_box` gives the same value.
861+
assert_eq!(
862+
event,
863+
&*PcrEventInputs::new_in_box(PcrIndex(4), EventType::IPL, &event_data).unwrap()
864+
);
841865
}
842866

843867
#[test]

0 commit comments

Comments
 (0)