Skip to content

Commit 50ba817

Browse files
uefi: Use uefi_raw for the TCG v1 protocol
1 parent 32b148f commit 50ba817

File tree

1 file changed

+23
-99
lines changed

1 file changed

+23
-99
lines changed

uefi/src/proto/tcg/v1.rs

Lines changed: 23 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,60 @@ use core::fmt::{self, Debug, Formatter};
1717
use core::marker::PhantomData;
1818
use core::{mem, ptr};
1919
use ptr_meta::Pointee;
20+
use uefi_raw::protocol::tcg::v1::{TcgBootServiceCapability, TcgProtocol};
2021

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

2425
#[cfg(all(feature = "unstable", feature = "alloc"))]
2526
use alloc::alloc::Global;
2627

28+
pub use uefi_raw::protocol::tcg::v1::TcgVersion as Version;
29+
2730
/// 20-byte SHA-1 digest.
2831
pub type Sha1Digest = [u8; 20];
2932

30-
/// This corresponds to the `AlgorithmId` enum, but in the v1 spec it's `u32`
31-
/// instead of `u16`.
32-
#[allow(non_camel_case_types)]
33-
type TCG_ALGORITHM_ID = u32;
34-
3533
/// Information about the protocol and the TPM device.
3634
///
3735
/// Layout compatible with the C type `TCG_EFI_BOOT_SERVICE_CAPABILITY`.
3836
#[repr(C)]
3937
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
40-
pub struct BootServiceCapability {
41-
size: u8,
42-
structure_version: Version,
43-
protocol_spec_version: Version,
44-
hash_algorithm_bitmap: u8,
45-
tpm_present_flag: u8,
46-
tpm_deactivated_flag: u8,
47-
}
38+
pub struct BootServiceCapability(TcgBootServiceCapability);
4839

4940
impl BootServiceCapability {
5041
/// Version of the `BootServiceCapability` structure.
5142
#[must_use]
5243
pub const fn structure_version(&self) -> Version {
53-
self.structure_version
44+
self.0.structure_version
5445
}
5546

5647
/// Version of the `Tcg` protocol.
5748
#[must_use]
5849
pub const fn protocol_spec_version(&self) -> Version {
59-
self.protocol_spec_version
50+
self.0.protocol_spec_version
6051
}
6152

6253
/// Supported hash algorithms.
6354
#[must_use]
6455
pub fn hash_algorithm(&self) -> HashAlgorithm {
6556
// Safety: the value should always be 0x1 (indicating SHA-1), but
6657
// we don't care if it's some unexpected value.
67-
HashAlgorithm::from_bits_retain(u32::from(self.hash_algorithm_bitmap))
58+
HashAlgorithm::from_bits_retain(u32::from(self.0.hash_algorithm_bitmap))
6859
}
6960

7061
/// Whether the TPM device is present.
7162
#[must_use]
7263
pub const fn tpm_present(&self) -> bool {
73-
self.tpm_present_flag != 0
64+
self.0.tpm_present_flag != 0
7465
}
7566

7667
/// Whether the TPM device is deactivated.
7768
#[must_use]
7869
pub const fn tpm_deactivated(&self) -> bool {
79-
self.tpm_deactivated_flag != 0
70+
self.0.tpm_deactivated_flag != 0
8071
}
8172
}
8273

83-
/// Version information.
84-
///
85-
/// Layout compatible with the C type `TCG_VERSION`.
86-
#[repr(C)]
87-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
88-
pub struct Version {
89-
/// Major version.
90-
pub major: u8,
91-
/// Minor version.
92-
pub minor: u8,
93-
94-
// Leave these two fields undocumented since it's not clear what
95-
// they are for. The spec doesn't say, and they were removed in the
96-
// v2 spec.
97-
#[allow(missing_docs)]
98-
pub rev_major: u8,
99-
#[allow(missing_docs)]
100-
pub rev_minor: u8,
101-
}
102-
10374
/// Entry in the [`EventLog`].
10475
///
10576
/// Layout compatible with the C type `TCG_PCR_EVENT`.
@@ -362,58 +333,9 @@ impl<'a> Iterator for EventLogIter<'a> {
362333
///
363334
/// The corresponding C type is `EFI_TCG_PROTOCOL`.
364335
#[derive(Debug)]
365-
#[repr(C)]
366-
#[unsafe_protocol("f541796d-a62e-4954-a775-9584f61b9cdd")]
367-
pub struct Tcg {
368-
status_check: unsafe extern "efiapi" fn(
369-
this: *mut Tcg,
370-
protocol_capability: *mut BootServiceCapability,
371-
feature_flags: *mut u32,
372-
event_log_location: *mut PhysicalAddress,
373-
event_log_last_entry: *mut PhysicalAddress,
374-
) -> Status,
375-
376-
// Note: we do not currently expose this function because the spec
377-
// for this is not well written. The function allocates memory, but
378-
// the spec doesn't say how to free it. Most likely
379-
// `EFI_BOOT_SERVICES.FreePool` would work, but this is not
380-
// mentioned in the spec so it is unsafe to rely on.
381-
//
382-
// Also, this function is not that useful in practice for a couple
383-
// reasons. First, it takes an algorithm ID, but only SHA-1 is
384-
// supported with TPM v1. Second, TPMs are not cryptographic
385-
// accelerators, so it is very likely faster to calculate the hash
386-
// on the CPU, e.g. with the `sha1` crate.
387-
hash_all: unsafe extern "efiapi" fn() -> Status,
388-
389-
log_event: unsafe extern "efiapi" fn(
390-
this: *mut Tcg,
391-
// The spec does not guarantee that the `event` will not be mutated
392-
// through the pointer, but it seems reasonable to assume and makes the
393-
// public interface clearer, so use a const pointer.
394-
event: *const FfiPcrEvent,
395-
event_number: *mut u32,
396-
flags: u32,
397-
) -> Status,
398-
399-
pass_through_to_tpm: unsafe extern "efiapi" fn(
400-
this: *mut Tcg,
401-
tpm_input_parameter_block_size: u32,
402-
tpm_input_parameter_block: *const u8,
403-
tpm_output_parameter_block_size: u32,
404-
tpm_output_parameter_block: *mut u8,
405-
) -> Status,
406-
407-
hash_log_extend_event: unsafe extern "efiapi" fn(
408-
this: *mut Tcg,
409-
hash_data: PhysicalAddress,
410-
hash_data_len: u64,
411-
algorithm_id: TCG_ALGORITHM_ID,
412-
event: *mut FfiPcrEvent,
413-
event_number: *mut u32,
414-
event_log_last_entry: *mut PhysicalAddress,
415-
) -> Status,
416-
}
336+
#[repr(transparent)]
337+
#[unsafe_protocol(TcgProtocol::GUID)]
338+
pub struct Tcg(TcgProtocol);
417339

418340
/// Return type of [`Tcg::status_check`].
419341
#[derive(Debug)]
@@ -433,14 +355,14 @@ impl Tcg {
433355
/// Get information about the protocol and TPM device, as well as
434356
/// the TPM event log.
435357
pub fn status_check(&mut self) -> Result<StatusCheck> {
436-
let mut protocol_capability = BootServiceCapability::default();
358+
let mut protocol_capability = TcgBootServiceCapability::default();
437359
let mut feature_flags = 0;
438360
let mut event_log_location = 0;
439361
let mut event_log_last_entry = 0;
440362

441363
let status = unsafe {
442-
(self.status_check)(
443-
self,
364+
(self.0.status_check)(
365+
&mut self.0,
444366
&mut protocol_capability,
445367
&mut feature_flags,
446368
&mut event_log_location,
@@ -461,7 +383,7 @@ impl Tcg {
461383
};
462384

463385
Ok(StatusCheck {
464-
protocol_capability,
386+
protocol_capability: BootServiceCapability(protocol_capability),
465387
feature_flags,
466388
event_log,
467389
})
@@ -487,7 +409,9 @@ impl Tcg {
487409

488410
let event_ptr: *const PcrEvent = event;
489411

490-
unsafe { (self.log_event)(self, event_ptr.cast(), &mut event_number, flags).to_result() }
412+
unsafe {
413+
(self.0.log_event)(&mut self.0, event_ptr.cast(), &mut event_number, flags).to_result()
414+
}
491415
}
492416

493417
/// Extend a PCR and add an entry to the event log.
@@ -517,8 +441,8 @@ impl Tcg {
517441
let event_ptr: *mut PcrEvent = event;
518442

519443
unsafe {
520-
(self.hash_log_extend_event)(
521-
self,
444+
(self.0.hash_log_extend_event)(
445+
&mut self.0,
522446
hash_data,
523447
hash_data_len,
524448
AlgorithmId::SHA1.0.into(),
@@ -550,8 +474,8 @@ impl Tcg {
550474
.map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?;
551475

552476
unsafe {
553-
(self.pass_through_to_tpm)(
554-
self,
477+
(self.0.pass_through_to_tpm)(
478+
&mut self.0,
555479
input_parameter_block_len,
556480
input_parameter_block.as_ptr(),
557481
output_parameter_block_len,

0 commit comments

Comments
 (0)