Skip to content

Commit 7cc0992

Browse files
uefi: Use uefi_raw for the TCG v2 protocol
1 parent 50ba817 commit 7cc0992

File tree

1 file changed

+33
-115
lines changed

1 file changed

+33
-115
lines changed

uefi/src/proto/tcg/v2.rs

Lines changed: 33 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,22 @@ use crate::data_types::{Align, PhysicalAddress, UnalignedSlice};
1515
use crate::proto::unsafe_protocol;
1616
use crate::util::{ptr_write_unaligned_and_add, usize_from_u32};
1717
use crate::{Error, Result, Status, StatusExt};
18-
use bitflags::bitflags;
1918
use core::fmt::{self, Debug, Formatter};
2019
use core::marker::PhantomData;
2120
use core::{mem, ptr, slice};
2221
use ptr_meta::{Pointee, PtrExt};
22+
use uefi_raw::protocol::tcg::v2::{Tcg2EventHeader as EventHeader, Tcg2Protocol};
2323

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

2727
#[cfg(all(feature = "unstable", feature = "alloc"))]
2828
use alloc::alloc::Global;
2929

30-
/// Version information.
31-
///
32-
/// Layout compatible with the C type `EFI_TG2_VERSION`.
33-
#[repr(C)]
34-
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
35-
pub struct Version {
36-
/// Major version.
37-
pub major: u8,
38-
/// Minor version.
39-
pub minor: u8,
40-
}
41-
42-
bitflags! {
43-
/// Event log formats supported by the firmware.
44-
///
45-
/// Corresponds to the C typedef `EFI_TCG2_EVENT_ALGORITHM_BITMAP`.
46-
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
47-
#[repr(transparent)]
48-
pub struct EventLogFormat: u32 {
49-
/// Firmware supports the SHA-1 log format.
50-
const TCG_1_2 = 0x0000_0001;
51-
52-
/// Firmware supports the crypto-agile log format.
53-
const TCG_2 = 0x0000_0002;
54-
}
55-
}
30+
pub use uefi_raw::protocol::tcg::v2::{
31+
Tcg2EventLogFormat as EventLogFormat, Tcg2HashAlgorithmBitmap,
32+
Tcg2HashLogExtendEventFlags as HashLogExtendEventFlags, Tcg2Version as Version,
33+
};
5634

5735
/// Information about the protocol and the TPM device.
5836
///
@@ -128,31 +106,6 @@ impl BootServiceCapability {
128106
}
129107
}
130108

131-
bitflags! {
132-
/// Flags for the [`Tcg::hash_log_extend_event`] function.
133-
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
134-
#[repr(transparent)]
135-
pub struct HashLogExtendEventFlags: u64 {
136-
/// Extend an event but don't log it.
137-
const EFI_TCG2_EXTEND_ONLY = 0x0000_0000_0000_0001;
138-
139-
/// Use when measuring a PE/COFF image.
140-
const PE_COFF_IMAGE = 0x0000_0000_0000_0010;
141-
}
142-
}
143-
144-
/// Header used in [`PcrEventInputs`].
145-
///
146-
/// Layout compatible with the C type `EFI_TCG2_EVENT_HEADER`.
147-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
148-
#[repr(C, packed)]
149-
struct EventHeader {
150-
header_size: u32,
151-
header_version: u16,
152-
pcr_index: PcrIndex,
153-
event_type: EventType,
154-
}
155-
156109
/// Event type passed to [`Tcg::hash_log_extend_event`].
157110
///
158111
/// Layout compatible with the C type `EFI_TCG2_EVENT`.
@@ -205,7 +158,7 @@ impl PcrEventInputs {
205158
EventHeader {
206159
header_size: u32::try_from(mem::size_of::<EventHeader>()).unwrap(),
207160
header_version: 1,
208-
pcr_index,
161+
pcr_index: pcr_index.0,
209162
event_type,
210163
},
211164
);
@@ -609,58 +562,18 @@ impl<'a> Iterator for EventLogIter<'a> {
609562
///
610563
/// The corresponding C type is `EFI_TCG2_PROTOCOL`.
611564
#[derive(Debug)]
612-
#[repr(C)]
613-
#[unsafe_protocol("607f766c-7455-42be-930b-e4d76db2720f")]
614-
pub struct Tcg {
615-
get_capability: unsafe extern "efiapi" fn(
616-
this: *mut Tcg,
617-
protocol_capability: *mut BootServiceCapability,
618-
) -> Status,
619-
620-
get_event_log: unsafe extern "efiapi" fn(
621-
this: *mut Tcg,
622-
event_log_format: EventLogFormat,
623-
event_log_location: *mut PhysicalAddress,
624-
event_log_last_entry: *mut PhysicalAddress,
625-
event_log_truncated: *mut u8,
626-
) -> Status,
627-
628-
hash_log_extend_event: unsafe extern "efiapi" fn(
629-
this: *mut Tcg,
630-
flags: HashLogExtendEventFlags,
631-
data_to_hash: PhysicalAddress,
632-
data_to_hash_len: u64,
633-
// Use `()` here rather than `PcrEventInputs` so that it's a
634-
// thin pointer.
635-
event: *const (),
636-
) -> Status,
637-
638-
submit_command: unsafe extern "efiapi" fn(
639-
this: *mut Tcg,
640-
input_parameter_block_size: u32,
641-
input_parameter_block: *const u8,
642-
output_parameter_block_size: u32,
643-
output_parameter_block: *mut u8,
644-
) -> Status,
645-
646-
get_active_pcr_banks:
647-
unsafe extern "efiapi" fn(this: *mut Tcg, active_pcr_banks: *mut HashAlgorithm) -> Status,
648-
649-
set_active_pcr_banks:
650-
unsafe extern "efiapi" fn(this: *mut Tcg, active_pcr_banks: HashAlgorithm) -> Status,
651-
652-
get_result_of_set_active_pcr_banks: unsafe extern "efiapi" fn(
653-
this: *mut Tcg,
654-
operation_present: *mut u32,
655-
response: *mut u32,
656-
) -> Status,
657-
}
565+
#[repr(transparent)]
566+
#[unsafe_protocol(Tcg2Protocol::GUID)]
567+
pub struct Tcg(Tcg2Protocol);
658568

659569
impl Tcg {
660570
/// Get information about the protocol and TPM device.
661571
pub fn get_capability(&mut self) -> Result<BootServiceCapability> {
662572
let mut capability = BootServiceCapability::default();
663-
unsafe { (self.get_capability)(self, &mut capability).to_result_with_val(|| capability) }
573+
unsafe {
574+
(self.0.get_capability)(&mut self.0, ptr::from_mut(&mut capability).cast())
575+
.to_result_with_val(|| capability)
576+
}
664577
}
665578

666579
/// Get the V1 event log. This provides events in the same format as a V1
@@ -671,8 +584,8 @@ impl Tcg {
671584
let mut truncated = 0;
672585

673586
let status = unsafe {
674-
(self.get_event_log)(
675-
self,
587+
(self.0.get_event_log)(
588+
&mut self.0,
676589
EventLogFormat::TCG_1_2,
677590
&mut location,
678591
&mut last_entry,
@@ -700,8 +613,8 @@ impl Tcg {
700613
let mut truncated = 0;
701614

702615
let status = unsafe {
703-
(self.get_event_log)(
704-
self,
616+
(self.0.get_event_log)(
617+
&mut self.0,
705618
EventLogFormat::TCG_2,
706619
&mut location,
707620
&mut last_entry,
@@ -735,13 +648,13 @@ impl Tcg {
735648
let event: *const PcrEventInputs = event;
736649
let (event, _event_size) = PtrExt::to_raw_parts(event);
737650
unsafe {
738-
(self.hash_log_extend_event)(
739-
self,
651+
(self.0.hash_log_extend_event)(
652+
&mut self.0,
740653
flags,
741654
data_to_hash.as_ptr() as PhysicalAddress,
742655
// OK to unwrap, usize fits in u64.
743656
u64::try_from(data_to_hash.len()).unwrap(),
744-
event,
657+
event.cast(),
745658
)
746659
.to_result()
747660
}
@@ -767,8 +680,8 @@ impl Tcg {
767680
.map_err(|_| Error::from(Status::BAD_BUFFER_SIZE))?;
768681

769682
unsafe {
770-
(self.submit_command)(
771-
self,
683+
(self.0.submit_command)(
684+
&mut self.0,
772685
input_parameter_block_len,
773686
input_parameter_block.as_ptr(),
774687
output_parameter_block_len,
@@ -781,18 +694,19 @@ impl Tcg {
781694
/// Get a bitmap of the active PCR banks. Each bank corresponds to a hash
782695
/// algorithm.
783696
pub fn get_active_pcr_banks(&mut self) -> Result<HashAlgorithm> {
784-
let mut active_pcr_banks = HashAlgorithm::empty();
697+
let mut active_pcr_banks = Tcg2HashAlgorithmBitmap::empty();
785698

786-
let status = unsafe { (self.get_active_pcr_banks)(self, &mut active_pcr_banks) };
699+
let status = unsafe { (self.0.get_active_pcr_banks)(&mut self.0, &mut active_pcr_banks) };
787700

788-
status.to_result_with_val(|| active_pcr_banks)
701+
status.to_result_with_val(|| HashAlgorithm::from_bits_retain(active_pcr_banks.bits()))
789702
}
790703

791704
/// Set the active PCR banks. Each bank corresponds to a hash
792705
/// algorithm. This change will not take effect until the system is
793706
/// rebooted twice.
794707
pub fn set_active_pcr_banks(&mut self, active_pcr_banks: HashAlgorithm) -> Result {
795-
unsafe { (self.set_active_pcr_banks)(self, active_pcr_banks) }.to_result()
708+
let active_pcr_banks = Tcg2HashAlgorithmBitmap::from_bits_retain(active_pcr_banks.bits());
709+
unsafe { (self.0.set_active_pcr_banks)(&mut self.0, active_pcr_banks) }.to_result()
796710
}
797711

798712
/// Get the stored result of calling [`Tcg::set_active_pcr_banks`] in a
@@ -809,7 +723,11 @@ impl Tcg {
809723
let mut response = 0;
810724

811725
let status = unsafe {
812-
(self.get_result_of_set_active_pcr_banks)(self, &mut operation_present, &mut response)
726+
(self.0.get_result_of_set_active_pcr_banks)(
727+
&mut self.0,
728+
&mut operation_present,
729+
&mut response,
730+
)
813731
};
814732

815733
status.to_result_with_val(|| {
@@ -842,7 +760,7 @@ mod tests {
842760
EventHeader {
843761
header_size: 14,
844762
header_version: 1,
845-
pcr_index: PcrIndex(4),
763+
pcr_index: 4,
846764
event_type: EventType::IPL,
847765
}
848766
);

0 commit comments

Comments
 (0)