1use crate::raw;
19use num_enum::{FromPrimitive, IntoPrimitive};
20use std::fmt;
21
22pub type Result<T> = std::result::Result<T, Error>;
36
37#[derive(Clone)]
42pub struct Error {
43 kind: ErrorKind,
44 origin: Option<ErrorOrigin>,
45}
46
47#[derive(
50 Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, FromPrimitive, IntoPrimitive,
51)]
52#[repr(u32)]
53pub enum ErrorKind {
54 Generic = raw::TEEC_ERROR_GENERIC,
56 AccessDenied = raw::TEEC_ERROR_ACCESS_DENIED,
58 Cancel = raw::TEEC_ERROR_CANCEL,
60 AccessConflict = raw::TEEC_ERROR_ACCESS_CONFLICT,
62 ExcessData = raw::TEEC_ERROR_EXCESS_DATA,
64 BadFormat = raw::TEEC_ERROR_BAD_FORMAT,
66 BadParameters = raw::TEEC_ERROR_BAD_PARAMETERS,
68 BadState = raw::TEEC_ERROR_BAD_STATE,
70 ItemNotFound = raw::TEEC_ERROR_ITEM_NOT_FOUND,
72 NotImplemented = raw::TEEC_ERROR_NOT_IMPLEMENTED,
74 NotSupported = raw::TEEC_ERROR_NOT_SUPPORTED,
76 NoData = raw::TEEC_ERROR_NO_DATA,
78 OutOfMemory = raw::TEEC_ERROR_OUT_OF_MEMORY,
80 Busy = raw::TEEC_ERROR_BUSY,
82 Communication = raw::TEEC_ERROR_COMMUNICATION,
84 Security = raw::TEEC_ERROR_SECURITY,
86 ShortBuffer = raw::TEEC_ERROR_SHORT_BUFFER,
88 ExternalCancel = raw::TEEC_ERROR_EXTERNAL_CANCEL,
90 TargetDead = raw::TEEC_ERROR_TARGET_DEAD,
92 #[default]
94 Unknown,
95}
96
97impl ErrorKind {
98 pub(crate) fn as_str(&self) -> &'static str {
99 match self {
100 ErrorKind::Generic => "Non-specific cause.",
101 ErrorKind::AccessDenied => "Access privileges are not sufficient.",
102 ErrorKind::Cancel => "The operation was canceled.",
103 ErrorKind::AccessConflict => "Concurrent accesses caused conflict.",
104 ErrorKind::ExcessData => "Too much data for the requested operation was passed.",
105 ErrorKind::BadFormat => "Input data was of invalid format.",
106 ErrorKind::BadParameters => "Input parameters were invalid.",
107 ErrorKind::BadState => "Operation is not valid in the current state.",
108 ErrorKind::ItemNotFound => "The requested data item is not found.",
109 ErrorKind::NotImplemented => {
110 "The requested operation should exist but is not yet implemented."
111 }
112 ErrorKind::NotSupported => {
113 "The requested operation is valid but is not supported in this implementation."
114 }
115 ErrorKind::NoData => "Expected data was missing.",
116 ErrorKind::OutOfMemory => "System ran out of resources.",
117 ErrorKind::Busy => "The system is busy working on something else.",
118 ErrorKind::Communication => "Communication with a remote party failed.",
119 ErrorKind::Security => "A security fault was detected.",
120 ErrorKind::ShortBuffer => "The supplied buffer is too short for the generated output.",
121 ErrorKind::ExternalCancel => "Undocumented.",
122 ErrorKind::TargetDead => "Trusted Application has panicked during the operation.",
123 ErrorKind::Unknown => "Unknown error.",
124 }
125 }
126}
127
128impl Error {
129 pub fn new(kind: ErrorKind) -> Error {
130 Error { kind, origin: None }
131 }
132 pub fn from_raw_error(code: u32) -> Error {
143 Error {
144 kind: ErrorKind::from(code),
145 origin: None,
146 }
147 }
148
149 pub fn with_origin(mut self, origin: ErrorOrigin) -> Self {
150 self.origin = Some(origin);
151 self
152 }
153
154 pub fn kind(&self) -> ErrorKind {
164 self.kind
165 }
166
167 pub fn origin(&self) -> Option<ErrorOrigin> {
169 self.origin.clone()
170 }
171
172 pub fn raw_code(&self) -> u32 {
174 self.kind.into()
175 }
176
177 pub fn message(&self) -> &str {
179 self.kind().as_str()
180 }
181}
182
183impl fmt::Debug for Error {
184 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
185 write!(
186 fmt,
187 "{} (error code 0x{:x}, origin 0x{:x})",
188 self.message(),
189 self.raw_code(),
190 self.origin().map(|v| v.into()).unwrap_or(0_u32),
191 )
192 }
193}
194
195impl fmt::Display for Error {
196 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
197 fmt::Debug::fmt(self, f)
198 }
199}
200
201impl std::error::Error for Error {
202 fn description(&self) -> &str {
203 self.message()
204 }
205}
206
207impl From<ErrorKind> for Error {
208 #[inline]
209 fn from(kind: ErrorKind) -> Error {
210 Error { kind, origin: None }
211 }
212}
213
214#[derive(Clone, Debug, Eq, PartialEq, FromPrimitive, IntoPrimitive)]
215#[repr(u32)]
216pub enum ErrorOrigin {
217 API = raw::TEEC_ORIGIN_API,
218 COMMS = raw::TEEC_ORIGIN_COMMS,
219 TEE = raw::TEEC_ORIGIN_TEE,
220 TA = raw::TEEC_ORIGIN_TRUSTED_APP,
221 #[default]
222 UNKNOWN,
223}