@@ -17,89 +17,60 @@ use core::fmt::{self, Debug, Formatter};
17
17
use core:: marker:: PhantomData ;
18
18
use core:: { mem, ptr} ;
19
19
use ptr_meta:: Pointee ;
20
+ use uefi_raw:: protocol:: tcg:: v1:: { TcgBootServiceCapability , TcgProtocol } ;
20
21
21
22
#[ cfg( feature = "alloc" ) ]
22
23
use { crate :: mem:: make_boxed, alloc:: boxed:: Box } ;
23
24
24
25
#[ cfg( all( feature = "unstable" , feature = "alloc" ) ) ]
25
26
use alloc:: alloc:: Global ;
26
27
28
+ pub use uefi_raw:: protocol:: tcg:: v1:: TcgVersion as Version ;
29
+
27
30
/// 20-byte SHA-1 digest.
28
31
pub type Sha1Digest = [ u8 ; 20 ] ;
29
32
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
-
35
33
/// Information about the protocol and the TPM device.
36
34
///
37
35
/// Layout compatible with the C type `TCG_EFI_BOOT_SERVICE_CAPABILITY`.
38
36
#[ repr( C ) ]
39
37
#[ 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 ) ;
48
39
49
40
impl BootServiceCapability {
50
41
/// Version of the `BootServiceCapability` structure.
51
42
#[ must_use]
52
43
pub const fn structure_version ( & self ) -> Version {
53
- self . structure_version
44
+ self . 0 . structure_version
54
45
}
55
46
56
47
/// Version of the `Tcg` protocol.
57
48
#[ must_use]
58
49
pub const fn protocol_spec_version ( & self ) -> Version {
59
- self . protocol_spec_version
50
+ self . 0 . protocol_spec_version
60
51
}
61
52
62
53
/// Supported hash algorithms.
63
54
#[ must_use]
64
55
pub fn hash_algorithm ( & self ) -> HashAlgorithm {
65
56
// Safety: the value should always be 0x1 (indicating SHA-1), but
66
57
// 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 ) )
68
59
}
69
60
70
61
/// Whether the TPM device is present.
71
62
#[ must_use]
72
63
pub const fn tpm_present ( & self ) -> bool {
73
- self . tpm_present_flag != 0
64
+ self . 0 . tpm_present_flag != 0
74
65
}
75
66
76
67
/// Whether the TPM device is deactivated.
77
68
#[ must_use]
78
69
pub const fn tpm_deactivated ( & self ) -> bool {
79
- self . tpm_deactivated_flag != 0
70
+ self . 0 . tpm_deactivated_flag != 0
80
71
}
81
72
}
82
73
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
-
103
74
/// Entry in the [`EventLog`].
104
75
///
105
76
/// Layout compatible with the C type `TCG_PCR_EVENT`.
@@ -362,58 +333,9 @@ impl<'a> Iterator for EventLogIter<'a> {
362
333
///
363
334
/// The corresponding C type is `EFI_TCG_PROTOCOL`.
364
335
#[ 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 ) ;
417
339
418
340
/// Return type of [`Tcg::status_check`].
419
341
#[ derive( Debug ) ]
@@ -433,14 +355,14 @@ impl Tcg {
433
355
/// Get information about the protocol and TPM device, as well as
434
356
/// the TPM event log.
435
357
pub fn status_check ( & mut self ) -> Result < StatusCheck > {
436
- let mut protocol_capability = BootServiceCapability :: default ( ) ;
358
+ let mut protocol_capability = TcgBootServiceCapability :: default ( ) ;
437
359
let mut feature_flags = 0 ;
438
360
let mut event_log_location = 0 ;
439
361
let mut event_log_last_entry = 0 ;
440
362
441
363
let status = unsafe {
442
- ( self . status_check ) (
443
- self ,
364
+ ( self . 0 . status_check ) (
365
+ & mut self . 0 ,
444
366
& mut protocol_capability,
445
367
& mut feature_flags,
446
368
& mut event_log_location,
@@ -461,7 +383,7 @@ impl Tcg {
461
383
} ;
462
384
463
385
Ok ( StatusCheck {
464
- protocol_capability,
386
+ protocol_capability : BootServiceCapability ( protocol_capability ) ,
465
387
feature_flags,
466
388
event_log,
467
389
} )
@@ -487,7 +409,9 @@ impl Tcg {
487
409
488
410
let event_ptr: * const PcrEvent = event;
489
411
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
+ }
491
415
}
492
416
493
417
/// Extend a PCR and add an entry to the event log.
@@ -517,8 +441,8 @@ impl Tcg {
517
441
let event_ptr: * mut PcrEvent = event;
518
442
519
443
unsafe {
520
- ( self . hash_log_extend_event ) (
521
- self ,
444
+ ( self . 0 . hash_log_extend_event ) (
445
+ & mut self . 0 ,
522
446
hash_data,
523
447
hash_data_len,
524
448
AlgorithmId :: SHA1 . 0 . into ( ) ,
@@ -550,8 +474,8 @@ impl Tcg {
550
474
. map_err ( |_| Error :: from ( Status :: BAD_BUFFER_SIZE ) ) ?;
551
475
552
476
unsafe {
553
- ( self . pass_through_to_tpm ) (
554
- self ,
477
+ ( self . 0 . pass_through_to_tpm ) (
478
+ & mut self . 0 ,
555
479
input_parameter_block_len,
556
480
input_parameter_block. as_ptr ( ) ,
557
481
output_parameter_block_len,
0 commit comments