Skip to content

Commit 0754b1f

Browse files
Merge pull request #1469 from nicholasbishop/bishop-raw-tcg
Add TCG protocols to `uefi-raw` and use them from `uefi`
2 parents 3317438 + 7cc0992 commit 0754b1f

File tree

9 files changed

+355
-226
lines changed

9 files changed

+355
-226
lines changed

uefi-raw/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Changed
44

55
- Added `protocol::string::UnicodeCollationProtocol`.
6+
- Added `protocol::tcg` module, containing the TCG v1 and v2 protocols.
67

78

89
# uefi-raw - 0.9.0 (2024-10-23)

uefi-raw/src/protocol/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ pub mod network;
1818
pub mod rng;
1919
pub mod shell_params;
2020
pub mod string;
21+
pub mod tcg;

uefi-raw/src/protocol/tcg.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! [TCG] (Trusted Computing Group) protocols.
2+
//!
3+
//! These protocols provide access to the [TPM][tpm] (Trusted Platform Module).
4+
//!
5+
//! There are two versions of the protocol. The original protocol is in
6+
//! the [`v1`] module. It is used with TPM 1.1 and 1.2 devices. The
7+
//! newer protocol in the [`v2`] module is generally provided for TPM
8+
//! 2.0 devices, although the spec indicates it can be used for older
9+
//! TPM versions as well.
10+
//!
11+
//! [TCG]: https://trustedcomputinggroup.org/
12+
//! [TPM]: https://en.wikipedia.org/wiki/Trusted_Platform_Module
13+
14+
pub mod v1;
15+
pub mod v2;
16+
17+
mod enums;
18+
pub use enums::*;

uefi/src/proto/tcg/enums.rs renamed to uefi-raw/src/protocol/tcg/enums.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
// Providing docstrings for each constant would be a lot of work, so
2-
// allow missing docs. Each type-level doc links to the relevant spec to
3-
// provide more info.
4-
//
5-
// Setting this at the module level so that we don't have to write it
6-
// above each constant. That's also why these enums are in a separate
7-
// module instead of `super`, since we don't want to allow missing docs
8-
// too broadly.
9-
#![allow(missing_docs)]
10-
111
newtype_enum! {
122
/// Algorithm identifiers.
133
///

uefi-raw/src/protocol/tcg/v1.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//! [TCG] (Trusted Computing Group) protocol for [TPM] (Trusted Platform
2+
//! Module) 1.1 and 1.2.
3+
//!
4+
//! This protocol is defined in the [TCG EFI Protocol Specification _for
5+
//! TPM Family 1.1 or 1.2_][spec].
6+
//!
7+
//! [spec]: https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/
8+
//! [TCG]: https://trustedcomputinggroup.org/
9+
//! [TPM]: https://en.wikipedia.org/wiki/Trusted_Platform_Module
10+
11+
use crate::{guid, Guid, PhysicalAddress, Status};
12+
use core::ffi::c_void;
13+
14+
/// Information about the protocol and the TPM device.
15+
#[repr(C)]
16+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
17+
pub struct TcgBootServiceCapability {
18+
pub size: u8,
19+
pub structure_version: TcgVersion,
20+
pub protocol_spec_version: TcgVersion,
21+
pub hash_algorithm_bitmap: u8,
22+
pub tpm_present_flag: u8,
23+
pub tpm_deactivated_flag: u8,
24+
}
25+
26+
/// Version information.
27+
#[repr(C)]
28+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
29+
pub struct TcgVersion {
30+
/// Major version.
31+
pub major: u8,
32+
/// Minor version.
33+
pub minor: u8,
34+
35+
pub rev_major: u8,
36+
pub rev_minor: u8,
37+
}
38+
39+
/// Protocol for interacting with TPM 1.1 and 1.2 devices.
40+
#[derive(Debug)]
41+
#[repr(C)]
42+
pub struct TcgProtocol {
43+
pub status_check: unsafe extern "efiapi" fn(
44+
this: *mut Self,
45+
protocol_capability: *mut TcgBootServiceCapability,
46+
feature_flags: *mut u32,
47+
event_log_location: *mut PhysicalAddress,
48+
event_log_last_entry: *mut PhysicalAddress,
49+
) -> Status,
50+
51+
pub hash_all: unsafe extern "efiapi" fn(
52+
this: *mut Self,
53+
hash_data: *const u8,
54+
hash_data_len: u64,
55+
algorithm_id: u32,
56+
hashed_data_len: *mut u64,
57+
hashed_data_result: *mut *mut u8,
58+
) -> Status,
59+
60+
pub log_event: unsafe extern "efiapi" fn(
61+
this: *mut Self,
62+
event: *const c_void,
63+
event_number: *mut u32,
64+
flags: u32,
65+
) -> Status,
66+
67+
pub pass_through_to_tpm: unsafe extern "efiapi" fn(
68+
this: *mut Self,
69+
tpm_input_parameter_block_size: u32,
70+
tpm_input_parameter_block: *const u8,
71+
tpm_output_parameter_block_size: u32,
72+
tpm_output_parameter_block: *mut u8,
73+
) -> Status,
74+
75+
pub hash_log_extend_event: unsafe extern "efiapi" fn(
76+
this: *mut Self,
77+
hash_data: PhysicalAddress,
78+
hash_data_len: u64,
79+
algorithm_id: u32,
80+
event: *mut c_void,
81+
event_number: *mut u32,
82+
event_log_last_entry: *mut PhysicalAddress,
83+
) -> Status,
84+
}
85+
86+
impl TcgProtocol {
87+
pub const GUID: Guid = guid!("f541796d-a62e-4954-a775-9584f61b9cdd");
88+
}

uefi-raw/src/protocol/tcg/v2.rs

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
//! [TCG] (Trusted Computing Group) protocol for [TPM] (Trusted Platform
2+
//! Module) 2.0.
3+
//!
4+
//! This protocol is defined in the [TCG EFI Protocol Specification _TPM
5+
//! Family 2.0_][spec]. It is generally implemented only for TPM 2.0
6+
//! devices, but the spec indicates it can also be used for older TPM
7+
//! devices.
8+
//!
9+
//! [spec]: https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/
10+
//! [TCG]: https://trustedcomputinggroup.org/
11+
//! [TPM]: https://en.wikipedia.org/wiki/Trusted_Platform_Module
12+
13+
use super::EventType;
14+
use crate::{guid, Guid, PhysicalAddress, Status};
15+
use bitflags::bitflags;
16+
use core::ffi::c_void;
17+
18+
/// Version information.
19+
#[repr(C)]
20+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
21+
pub struct Tcg2Version {
22+
/// Major version.
23+
pub major: u8,
24+
/// Minor version.
25+
pub minor: u8,
26+
}
27+
28+
bitflags! {
29+
/// Event log formats supported by the firmware.
30+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
31+
#[repr(transparent)]
32+
pub struct Tcg2EventLogBitmap: u32 {
33+
/// Firmware supports the SHA-1 log format.
34+
const TCG_1_2 = 0x0000_0001;
35+
36+
/// Firmware supports the crypto-agile log format.
37+
const TCG_2 = 0x0000_0002;
38+
}
39+
}
40+
41+
/// Event log formats supported by the firmware.
42+
pub type Tcg2EventLogFormat = Tcg2EventLogBitmap;
43+
44+
bitflags! {
45+
/// Hash algorithms the protocol can provide.
46+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
47+
#[repr(transparent)]
48+
pub struct Tcg2HashAlgorithmBitmap: u32 {
49+
/// SHA-1 hash.
50+
const SHA1 = 0x0000_0001;
51+
52+
/// SHA-256 hash.
53+
const SHA256 = 0x0000_0002;
54+
55+
/// SHA-384 hash.
56+
const SHA384 = 0x0000_0004;
57+
58+
/// SHA-512 hash.
59+
const SHA512 = 0x0000_0008;
60+
61+
/// SM3-256 hash.
62+
const SM3_256 = 0x0000_0010;
63+
}
64+
}
65+
66+
/// Information about the protocol and the TPM device.
67+
#[repr(C)]
68+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
69+
pub struct Tcg2BootServiceCapability {
70+
/// Size of this structure.
71+
pub size: u8,
72+
73+
/// Version of the EFI TCG2 protocol.
74+
pub structure_version: Tcg2Version,
75+
76+
/// Version of the EFI TCG2 protocol.
77+
pub protocol_version: Tcg2Version,
78+
79+
/// Bitmap of supported hash algorithms.
80+
pub hash_algorithm_bitmap: Tcg2HashAlgorithmBitmap,
81+
82+
/// Event log formats supported by the firmware.
83+
pub supported_event_logs: Tcg2EventLogBitmap,
84+
85+
/// Whether the TPM is present or not.
86+
pub tpm_present_flag: u8,
87+
88+
/// Maximum size (in bytes) of a command that can be sent to the TPM.
89+
pub max_command_size: u16,
90+
91+
/// Maximum size (in bytes) of a response that can be provided by the TPM.
92+
pub max_response_size: u16,
93+
94+
/// Manufacturer ID.
95+
///
96+
/// See the [TCG Vendor ID registry].
97+
///
98+
/// [TCG Vendor ID registry]: https://trustedcomputinggroup.org/resource/vendor-id-registry/
99+
pub manufacturer_id: u32,
100+
101+
/// Maximum number of supported PCR banks (hashing algorithms).
102+
pub number_of_pcr_banks: u32,
103+
104+
/// Bitmap of currently-active PCR banks (hashing algorithms). This
105+
/// is a subset of the supported algorithms in [`hash_algorithm_bitmap`].
106+
///
107+
/// [`hash_algorithm_bitmap`]: Self::hash_algorithm_bitmap
108+
pub active_pcr_banks: Tcg2HashAlgorithmBitmap,
109+
}
110+
111+
bitflags! {
112+
/// Flags for the [`Tcg::hash_log_extend_event`] function.
113+
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
114+
#[repr(transparent)]
115+
pub struct Tcg2HashLogExtendEventFlags: u64 {
116+
/// Extend an event but don't log it.
117+
const EFI_TCG2_EXTEND_ONLY = 0x0000_0000_0000_0001;
118+
119+
/// Use when measuring a PE/COFF image.
120+
const PE_COFF_IMAGE = 0x0000_0000_0000_0010;
121+
}
122+
}
123+
124+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
125+
#[repr(C, packed)]
126+
pub struct Tcg2EventHeader {
127+
pub header_size: u32,
128+
pub header_version: u16,
129+
pub pcr_index: u32,
130+
pub event_type: EventType,
131+
}
132+
133+
/// Protocol for interacting with TPM devices.
134+
///
135+
/// This protocol can be used for interacting with older TPM 1.1/1.2
136+
/// devices, but most firmware only uses it for TPM 2.0.
137+
///
138+
/// The corresponding C type is `EFI_TCG2_PROTOCOL`.
139+
#[derive(Debug)]
140+
#[repr(C)]
141+
pub struct Tcg2Protocol {
142+
pub get_capability: unsafe extern "efiapi" fn(
143+
this: *mut Self,
144+
protocol_capability: *mut Tcg2BootServiceCapability,
145+
) -> Status,
146+
147+
pub get_event_log: unsafe extern "efiapi" fn(
148+
this: *mut Self,
149+
event_log_format: Tcg2EventLogFormat,
150+
event_log_location: *mut PhysicalAddress,
151+
event_log_last_entry: *mut PhysicalAddress,
152+
event_log_truncated: *mut u8,
153+
) -> Status,
154+
155+
pub hash_log_extend_event: unsafe extern "efiapi" fn(
156+
this: *mut Self,
157+
flags: Tcg2HashLogExtendEventFlags,
158+
data_to_hash: PhysicalAddress,
159+
data_to_hash_len: u64,
160+
event: *const c_void,
161+
) -> Status,
162+
163+
pub submit_command: unsafe extern "efiapi" fn(
164+
this: *mut Self,
165+
input_parameter_block_size: u32,
166+
input_parameter_block: *const u8,
167+
output_parameter_block_size: u32,
168+
output_parameter_block: *mut u8,
169+
) -> Status,
170+
171+
pub get_active_pcr_banks: unsafe extern "efiapi" fn(
172+
this: *mut Self,
173+
active_pcr_banks: *mut Tcg2HashAlgorithmBitmap,
174+
) -> Status,
175+
176+
pub set_active_pcr_banks: unsafe extern "efiapi" fn(
177+
this: *mut Self,
178+
active_pcr_banks: Tcg2HashAlgorithmBitmap,
179+
) -> Status,
180+
181+
pub get_result_of_set_active_pcr_banks: unsafe extern "efiapi" fn(
182+
this: *mut Self,
183+
operation_present: *mut u32,
184+
response: *mut u32,
185+
) -> Status,
186+
}
187+
188+
impl Tcg2Protocol {
189+
pub const GUID: Guid = guid!("607f766c-7455-42be-930b-e4d76db2720f");
190+
}

uefi/src/proto/tcg/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
pub mod v1;
1515
pub mod v2;
1616

17-
mod enums;
18-
pub use enums::*;
17+
pub use uefi_raw::protocol::tcg::{AlgorithmId, EventType};
1918

2019
use bitflags::bitflags;
2120

0 commit comments

Comments
 (0)