@@ -15,44 +15,22 @@ use crate::data_types::{Align, PhysicalAddress, UnalignedSlice};
15
15
use crate :: proto:: unsafe_protocol;
16
16
use crate :: util:: { ptr_write_unaligned_and_add, usize_from_u32} ;
17
17
use crate :: { Error , Result , Status , StatusExt } ;
18
- use bitflags:: bitflags;
19
18
use core:: fmt:: { self , Debug , Formatter } ;
20
19
use core:: marker:: PhantomData ;
21
20
use core:: { mem, ptr, slice} ;
22
21
use ptr_meta:: { Pointee , PtrExt } ;
22
+ use uefi_raw:: protocol:: tcg:: v2:: { Tcg2EventHeader as EventHeader , Tcg2Protocol } ;
23
23
24
24
#[ cfg( feature = "alloc" ) ]
25
25
use { crate :: mem:: make_boxed, alloc:: boxed:: Box } ;
26
26
27
27
#[ cfg( all( feature = "unstable" , feature = "alloc" ) ) ]
28
28
use alloc:: alloc:: Global ;
29
29
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
+ } ;
56
34
57
35
/// Information about the protocol and the TPM device.
58
36
///
@@ -128,31 +106,6 @@ impl BootServiceCapability {
128
106
}
129
107
}
130
108
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
-
156
109
/// Event type passed to [`Tcg::hash_log_extend_event`].
157
110
///
158
111
/// Layout compatible with the C type `EFI_TCG2_EVENT`.
@@ -205,7 +158,7 @@ impl PcrEventInputs {
205
158
EventHeader {
206
159
header_size : u32:: try_from ( mem:: size_of :: < EventHeader > ( ) ) . unwrap ( ) ,
207
160
header_version : 1 ,
208
- pcr_index,
161
+ pcr_index : pcr_index . 0 ,
209
162
event_type,
210
163
} ,
211
164
) ;
@@ -609,58 +562,18 @@ impl<'a> Iterator for EventLogIter<'a> {
609
562
///
610
563
/// The corresponding C type is `EFI_TCG2_PROTOCOL`.
611
564
#[ 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 ) ;
658
568
659
569
impl Tcg {
660
570
/// Get information about the protocol and TPM device.
661
571
pub fn get_capability ( & mut self ) -> Result < BootServiceCapability > {
662
572
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
+ }
664
577
}
665
578
666
579
/// Get the V1 event log. This provides events in the same format as a V1
@@ -671,8 +584,8 @@ impl Tcg {
671
584
let mut truncated = 0 ;
672
585
673
586
let status = unsafe {
674
- ( self . get_event_log ) (
675
- self ,
587
+ ( self . 0 . get_event_log ) (
588
+ & mut self . 0 ,
676
589
EventLogFormat :: TCG_1_2 ,
677
590
& mut location,
678
591
& mut last_entry,
@@ -700,8 +613,8 @@ impl Tcg {
700
613
let mut truncated = 0 ;
701
614
702
615
let status = unsafe {
703
- ( self . get_event_log ) (
704
- self ,
616
+ ( self . 0 . get_event_log ) (
617
+ & mut self . 0 ,
705
618
EventLogFormat :: TCG_2 ,
706
619
& mut location,
707
620
& mut last_entry,
@@ -735,13 +648,13 @@ impl Tcg {
735
648
let event: * const PcrEventInputs = event;
736
649
let ( event, _event_size) = PtrExt :: to_raw_parts ( event) ;
737
650
unsafe {
738
- ( self . hash_log_extend_event ) (
739
- self ,
651
+ ( self . 0 . hash_log_extend_event ) (
652
+ & mut self . 0 ,
740
653
flags,
741
654
data_to_hash. as_ptr ( ) as PhysicalAddress ,
742
655
// OK to unwrap, usize fits in u64.
743
656
u64:: try_from ( data_to_hash. len ( ) ) . unwrap ( ) ,
744
- event,
657
+ event. cast ( ) ,
745
658
)
746
659
. to_result ( )
747
660
}
@@ -767,8 +680,8 @@ impl Tcg {
767
680
. map_err ( |_| Error :: from ( Status :: BAD_BUFFER_SIZE ) ) ?;
768
681
769
682
unsafe {
770
- ( self . submit_command ) (
771
- self ,
683
+ ( self . 0 . submit_command ) (
684
+ & mut self . 0 ,
772
685
input_parameter_block_len,
773
686
input_parameter_block. as_ptr ( ) ,
774
687
output_parameter_block_len,
@@ -781,18 +694,19 @@ impl Tcg {
781
694
/// Get a bitmap of the active PCR banks. Each bank corresponds to a hash
782
695
/// algorithm.
783
696
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 ( ) ;
785
698
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) } ;
787
700
788
- status. to_result_with_val ( || active_pcr_banks)
701
+ status. to_result_with_val ( || HashAlgorithm :: from_bits_retain ( active_pcr_banks. bits ( ) ) )
789
702
}
790
703
791
704
/// Set the active PCR banks. Each bank corresponds to a hash
792
705
/// algorithm. This change will not take effect until the system is
793
706
/// rebooted twice.
794
707
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 ( )
796
710
}
797
711
798
712
/// Get the stored result of calling [`Tcg::set_active_pcr_banks`] in a
@@ -809,7 +723,11 @@ impl Tcg {
809
723
let mut response = 0 ;
810
724
811
725
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
+ )
813
731
} ;
814
732
815
733
status. to_result_with_val ( || {
@@ -842,7 +760,7 @@ mod tests {
842
760
EventHeader {
843
761
header_size: 14 ,
844
762
header_version: 1 ,
845
- pcr_index: PcrIndex ( 4 ) ,
763
+ pcr_index: 4 ,
846
764
event_type: EventType :: IPL ,
847
765
}
848
766
) ;
0 commit comments