Skip to content

Commit 2e0be2a

Browse files
tcg: PcrEvent/PcrEventInputs: add new_in_box constructors
1 parent 6c726f3 commit 2e0be2a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

uefi/src/proto/tcg/v1.rs

+38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ 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+
24+
#[cfg(all(feature = "unstable", feature = "alloc"))]
25+
use {alloc::alloc::Global, core::alloc::Allocator};
26+
2127
/// 20-byte SHA-1 digest.
2228
pub type Sha1Digest = [u8; 20];
2329

@@ -165,6 +171,32 @@ impl PcrEvent {
165171
}
166172
}
167173

174+
/// Create a new `PcrEvent` in a [`Box`].
175+
///
176+
/// # Errors
177+
///
178+
/// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too
179+
/// large.
180+
#[cfg(feature = "alloc")]
181+
pub fn new_in_box(
182+
pcr_index: PcrIndex,
183+
event_type: EventType,
184+
digest: Sha1Digest,
185+
event_data: &[u8],
186+
) -> Result<Box<Self>> {
187+
#[cfg(not(feature = "unstable"))]
188+
{
189+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data))
190+
}
191+
#[cfg(feature = "unstable")]
192+
{
193+
make_boxed(
194+
|buf| Self::new_in_buffer(buf, pcr_index, event_type, digest, event_data),
195+
Global,
196+
)
197+
}
198+
}
199+
168200
/// PCR index for the event.
169201
#[must_use]
170202
pub fn pcr_index(&self) -> PcrIndex {
@@ -575,6 +607,12 @@ mod tests {
575607
// Event data
576608
0x14, 0x15, 0x16, 0x17,
577609
]);
610+
611+
// Check that `new_in_box` gives the same value.
612+
assert_eq!(
613+
event,
614+
&*PcrEvent::new_in_box(PcrIndex(4), EventType::IPL, digest, &data).unwrap()
615+
);
578616
}
579617

580618
#[test]

uefi/src/proto/tcg/v2.rs

+37
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ 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+
27+
#[cfg(all(feature = "unstable", feature = "alloc"))]
28+
use {alloc::alloc::Global, core::alloc::Allocator};
29+
2430
/// Version information.
2531
///
2632
/// Layout compatible with the C type `EFI_TG2_VERSION`.
@@ -210,6 +216,31 @@ impl PcrEventInputs {
210216
Ok(&mut *ptr)
211217
}
212218
}
219+
220+
/// Create a new `PcrEventInputs` in a [`Box`].
221+
///
222+
/// # Errors
223+
///
224+
/// Returns [`Status::INVALID_PARAMETER`] if the `event_data` size is too
225+
/// large.
226+
#[cfg(feature = "alloc")]
227+
pub fn new_in_box(
228+
pcr_index: PcrIndex,
229+
event_type: EventType,
230+
event_data: &[u8],
231+
) -> Result<Box<Self>> {
232+
#[cfg(not(feature = "unstable"))]
233+
{
234+
make_boxed(|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data))
235+
}
236+
#[cfg(feature = "unstable")]
237+
{
238+
make_boxed(
239+
|buf| Self::new_in_buffer(buf, pcr_index, event_type, event_data),
240+
Global,
241+
)
242+
}
243+
}
213244
}
214245

215246
impl Align for PcrEventInputs {
@@ -838,6 +869,12 @@ mod tests {
838869
// Event data
839870
0x12, 0x13, 0x14, 0x15,
840871
]);
872+
873+
// Check that `new_in_box` gives the same value.
874+
assert_eq!(
875+
event,
876+
&*PcrEventInputs::new_in_box(PcrIndex(4), EventType::IPL, &event_data).unwrap()
877+
);
841878
}
842879

843880
#[test]

0 commit comments

Comments
 (0)