1#[allow(unused_imports)]
6use crate::{tpm_enum, TpmErrorKind, TpmNotDiscriminant, TpmParse};
7use core::{
8 convert::TryFrom,
9 fmt::{self, Debug, Display, Formatter},
10};
11
12pub const TPM_RC_VER1: u32 = 0x0100;
13pub const TPM_RC_FMT1: u32 = 0x0080;
14pub const TPM_RC_WARN: u32 = 0x0900;
15pub const TPM_RC_P_BIT: u32 = 1 << 6;
16pub const TPM_RC_N_SHIFT: u32 = 8;
17pub const TPM_RC_FMT1_ERROR_MASK: u32 = 0x003F;
18
19const MAX_HANDLE_INDEX: u8 = 7;
20const SESSION_INDEX_OFFSET: u8 = 8;
21
22#[derive(Debug, PartialEq, Eq, Copy, Clone)]
23pub enum TpmRcIndex {
24 Parameter(u8),
25 Handle(u8),
26 Session(u8),
27}
28
29impl Display for TpmRcIndex {
30 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
31 match self {
32 Self::Parameter(i) => write!(f, "parameter[{i}]"),
33 Self::Handle(i) => write!(f, "handle[{i}]"),
34 Self::Session(i) => write!(f, "session[{i}]"),
35 }
36 }
37}
38
39tpm_enum! {
40 #[derive(Debug, PartialEq, Eq, Copy, Clone)]
41 #[allow(clippy::upper_case_acronyms)]
42 pub enum TpmRcBase(u32) {
43 (Success, 0x0000, "TPM_RC_SUCCESS"),
44 (BadTag, 0x001E, "TPM_RC_BAD_TAG"),
45 (Initialize, TPM_RC_VER1, "TPM_RC_INITIALIZE"),
46 (Failure, TPM_RC_VER1 | 0x001, "TPM_RC_FAILURE"),
47 (AuthMissing, TPM_RC_VER1 | 0x025, "TPM_RC_AUTH_MISSING"),
48 (CommandSize, TPM_RC_VER1 | 0x042, "TPM_RC_COMMAND_SIZE"),
49 (Sensitive, TPM_RC_VER1 | 0x055, "TPM_RC_SENSITIVE"),
50 (Asymmetric, TPM_RC_FMT1 | 0x001, "TPM_RC_ASYMMETRIC"),
51 (Attributes, TPM_RC_FMT1 | 0x002, "TPM_RC_ATTRIBUTES"),
52 (Value, TPM_RC_FMT1 | 0x004, "TPM_RC_VALUE"),
53 (Handle, TPM_RC_FMT1 | 0x00B, "TPM_RC_HANDLE"),
54 (AuthFail, TPM_RC_FMT1 | 0x00E, "TPM_RC_AUTH_FAIL"),
55 (BadAuth, TPM_RC_FMT1 | 0x022, "TPM_RC_BAD_AUTH"),
56 (Curve, TPM_RC_FMT1 | 0x026, "TPM_RC_CURVE"),
57 (ContextGap, TPM_RC_WARN | 0x001, "TPM_RC_CONTEXT_GAP"),
58 (NvUnavailable, TPM_RC_WARN | 0x023, "TPM_RC_NV_UNAVAILABLE"),
59 }
60}
61
62fn get_base_code(value: u32) -> u32 {
64 if (value & TPM_RC_FMT1) != 0 {
65 TPM_RC_FMT1 | (value & TPM_RC_FMT1_ERROR_MASK)
66 } else {
67 value
68 }
69}
70
71#[must_use]
72#[derive(Debug, PartialEq, Eq, Copy, Clone)]
73pub struct TpmRc(u32);
74impl TpmRc {
75 pub fn base(self) -> Result<TpmRcBase, TpmErrorKind> {
83 TpmRcBase::try_from(get_base_code(self.0)).map_err(|()| TpmErrorKind::Unreachable)
84 }
85
86 #[must_use]
87 pub fn index(self) -> Option<TpmRcIndex> {
88 let value = self.0;
89 if (value & TPM_RC_FMT1) == 0 {
90 return None;
91 }
92 let is_parameter = (value & TPM_RC_P_BIT) != 0;
93 let n = ((value >> TPM_RC_N_SHIFT) & 0b1111) as u8;
94
95 match (is_parameter, n) {
96 (_, 0) => None,
97 (true, num) => Some(TpmRcIndex::Parameter(num)),
98 (false, num @ 1..=MAX_HANDLE_INDEX) => Some(TpmRcIndex::Handle(num)),
99 (false, num) => Some(TpmRcIndex::Session(num - SESSION_INDEX_OFFSET)),
100 }
101 }
102
103 #[must_use]
104 pub fn value(self) -> u32 {
105 self.0
106 }
107 #[must_use]
108 pub fn is_warning(self) -> bool {
109 (self.0 & TPM_RC_WARN) == TPM_RC_WARN
110 }
111 #[must_use]
112 pub fn is_error(self) -> bool {
113 !self.is_warning() && self.0 != 0
114 }
115}
116
117impl crate::TpmSized for TpmRc {
118 const SIZE: usize = core::mem::size_of::<u32>();
119 fn len(&self) -> usize {
120 Self::SIZE
121 }
122}
123
124impl crate::TpmBuild for TpmRc {
125 fn build(&self, writer: &mut crate::TpmWriter) -> crate::TpmResult<()> {
126 self.0.build(writer)
127 }
128}
129
130impl crate::TpmParse for TpmRc {
131 fn parse(buf: &[u8]) -> crate::TpmResult<(Self, &[u8])> {
132 let (val, remainder) = u32::parse(buf)?;
133 let rc = Self::try_from(val)?;
134 Ok((rc, remainder))
135 }
136}
137
138impl TryFrom<u32> for TpmRc {
139 type Error = TpmErrorKind;
140 fn try_from(value: u32) -> Result<Self, Self::Error> {
141 let base_code = get_base_code(value);
142 TpmRcBase::try_from(base_code).map_err(|()| {
143 TpmErrorKind::NotDiscriminant(
144 "TpmRcBase",
145 TpmNotDiscriminant::Unsigned(u64::from(base_code)),
146 )
147 })?;
148 Ok(Self(value))
149 }
150}
151
152impl From<TpmRcBase> for TpmRc {
153 fn from(value: TpmRcBase) -> Self {
154 Self(value as u32)
155 }
156}
157
158impl Display for TpmRc {
159 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
160 if let Ok(base) = self.base() {
161 if let Some(index) = self.index() {
162 write!(f, "[{base}, {index}]")
163 } else {
164 write!(f, "{base}")
165 }
166 } else {
167 write!(f, "TPM_RC_UNKNOWN(0x{:08X})", self.0)
168 }
169 }
170}