Skip to content

Commit 918e6d9

Browse files
Move Status to uefi-raw
1 parent 2ec41b2 commit 918e6d9

File tree

3 files changed

+134
-133
lines changed

3 files changed

+134
-133
lines changed

uefi-raw/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@
1616

1717
#[macro_use]
1818
mod enums;
19+
20+
mod status;
21+
22+
pub use status::Status;

uefi-raw/src/status.rs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
use core::fmt::Debug;
2+
3+
newtype_enum! {
4+
/// UEFI uses status codes in order to report successes, errors, and warnings.
5+
///
6+
/// The spec allows implementation-specific status codes, so the `Status`
7+
/// constants are not a comprehensive list of all possible values.
8+
#[must_use]
9+
pub enum Status: usize => {
10+
/// The operation completed successfully.
11+
SUCCESS = 0,
12+
13+
/// The string contained characters that could not be rendered and were skipped.
14+
WARN_UNKNOWN_GLYPH = 1,
15+
/// The handle was closed, but the file was not deleted.
16+
WARN_DELETE_FAILURE = 2,
17+
/// The handle was closed, but the data to the file was not flushed properly.
18+
WARN_WRITE_FAILURE = 3,
19+
/// The resulting buffer was too small, and the data was truncated.
20+
WARN_BUFFER_TOO_SMALL = 4,
21+
/// The data has not been updated within the timeframe set by local policy.
22+
WARN_STALE_DATA = 5,
23+
/// The resulting buffer contains UEFI-compliant file system.
24+
WARN_FILE_SYSTEM = 6,
25+
/// The operation will be processed across a system reset.
26+
WARN_RESET_REQUIRED = 7,
27+
28+
/// The image failed to load.
29+
LOAD_ERROR = Self::ERROR_BIT | 1,
30+
/// A parameter was incorrect.
31+
INVALID_PARAMETER = Self::ERROR_BIT | 2,
32+
/// The operation is not supported.
33+
UNSUPPORTED = Self::ERROR_BIT | 3,
34+
/// The buffer was not the proper size for the request.
35+
BAD_BUFFER_SIZE = Self::ERROR_BIT | 4,
36+
/// The buffer is not large enough to hold the requested data.
37+
/// The required buffer size is returned in the appropriate parameter.
38+
BUFFER_TOO_SMALL = Self::ERROR_BIT | 5,
39+
/// There is no data pending upon return.
40+
NOT_READY = Self::ERROR_BIT | 6,
41+
/// The physical device reported an error while attempting the operation.
42+
DEVICE_ERROR = Self::ERROR_BIT | 7,
43+
/// The device cannot be written to.
44+
WRITE_PROTECTED = Self::ERROR_BIT | 8,
45+
/// A resource has run out.
46+
OUT_OF_RESOURCES = Self::ERROR_BIT | 9,
47+
/// An inconstency was detected on the file system.
48+
VOLUME_CORRUPTED = Self::ERROR_BIT | 10,
49+
/// There is no more space on the file system.
50+
VOLUME_FULL = Self::ERROR_BIT | 11,
51+
/// The device does not contain any medium to perform the operation.
52+
NO_MEDIA = Self::ERROR_BIT | 12,
53+
/// The medium in the device has changed since the last access.
54+
MEDIA_CHANGED = Self::ERROR_BIT | 13,
55+
/// The item was not found.
56+
NOT_FOUND = Self::ERROR_BIT | 14,
57+
/// Access was denied.
58+
ACCESS_DENIED = Self::ERROR_BIT | 15,
59+
/// The server was not found or did not respond to the request.
60+
NO_RESPONSE = Self::ERROR_BIT | 16,
61+
/// A mapping to a device does not exist.
62+
NO_MAPPING = Self::ERROR_BIT | 17,
63+
/// The timeout time expired.
64+
TIMEOUT = Self::ERROR_BIT | 18,
65+
/// The protocol has not been started.
66+
NOT_STARTED = Self::ERROR_BIT | 19,
67+
/// The protocol has already been started.
68+
ALREADY_STARTED = Self::ERROR_BIT | 20,
69+
/// The operation was aborted.
70+
ABORTED = Self::ERROR_BIT | 21,
71+
/// An ICMP error occurred during the network operation.
72+
ICMP_ERROR = Self::ERROR_BIT | 22,
73+
/// A TFTP error occurred during the network operation.
74+
TFTP_ERROR = Self::ERROR_BIT | 23,
75+
/// A protocol error occurred during the network operation.
76+
PROTOCOL_ERROR = Self::ERROR_BIT | 24,
77+
/// The function encountered an internal version that was
78+
/// incompatible with a version requested by the caller.
79+
INCOMPATIBLE_VERSION = Self::ERROR_BIT | 25,
80+
/// The function was not performed due to a security violation.
81+
SECURITY_VIOLATION = Self::ERROR_BIT | 26,
82+
/// A CRC error was detected.
83+
CRC_ERROR = Self::ERROR_BIT | 27,
84+
/// Beginning or end of media was reached
85+
END_OF_MEDIA = Self::ERROR_BIT | 28,
86+
/// The end of the file was reached.
87+
END_OF_FILE = Self::ERROR_BIT | 31,
88+
/// The language specified was invalid.
89+
INVALID_LANGUAGE = Self::ERROR_BIT | 32,
90+
/// The security status of the data is unknown or compromised and
91+
/// the data must be updated or replaced to restore a valid security status.
92+
COMPROMISED_DATA = Self::ERROR_BIT | 33,
93+
/// There is an address conflict address allocation
94+
IP_ADDRESS_CONFLICT = Self::ERROR_BIT | 34,
95+
/// A HTTP error occurred during the network operation.
96+
HTTP_ERROR = Self::ERROR_BIT | 35,
97+
}}
98+
99+
impl Status {
100+
/// Bit indicating that an UEFI status code is an error.
101+
pub const ERROR_BIT: usize = 1 << (core::mem::size_of::<usize>() * 8 - 1);
102+
103+
/// Returns true if status code indicates success.
104+
#[inline]
105+
#[must_use]
106+
pub fn is_success(self) -> bool {
107+
self == Status::SUCCESS
108+
}
109+
110+
/// Returns true if status code indicates a warning.
111+
#[inline]
112+
#[must_use]
113+
pub fn is_warning(self) -> bool {
114+
(self != Status::SUCCESS) && (self.0 & Self::ERROR_BIT == 0)
115+
}
116+
117+
/// Returns true if the status code indicates an error.
118+
#[inline]
119+
#[must_use]
120+
pub const fn is_error(self) -> bool {
121+
self.0 & Self::ERROR_BIT != 0
122+
}
123+
}
124+
125+
impl core::fmt::Display for Status {
126+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
127+
Debug::fmt(self, f)
128+
}
129+
}

uefi/src/result/status.rs

Lines changed: 1 addition & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,7 @@
11
use super::{Error, Result};
22
use core::fmt::Debug;
33

4-
/// Bit indicating that an UEFI status code is an error
5-
const ERROR_BIT: usize = 1 << (core::mem::size_of::<usize>() * 8 - 1);
6-
7-
newtype_enum! {
8-
/// UEFI uses status codes in order to report successes, errors, and warnings.
9-
///
10-
/// The spec allows implementation-specific status codes, so the `Status`
11-
/// constants are not a comprehensive list of all possible values.
12-
///
13-
/// For a convenient integration into the Rust ecosystem, there are several
14-
/// methods to convert a Status into a [`uefi::Result`]:
15-
/// - [`Status::to_result_with`]
16-
/// - [`Status::to_result_with_val`]
17-
/// - [`Status::to_result_with_err`]
18-
#[must_use]
19-
pub enum Status: usize => {
20-
/// The operation completed successfully.
21-
SUCCESS = 0,
22-
23-
/// The string contained characters that could not be rendered and were skipped.
24-
WARN_UNKNOWN_GLYPH = 1,
25-
/// The handle was closed, but the file was not deleted.
26-
WARN_DELETE_FAILURE = 2,
27-
/// The handle was closed, but the data to the file was not flushed properly.
28-
WARN_WRITE_FAILURE = 3,
29-
/// The resulting buffer was too small, and the data was truncated.
30-
WARN_BUFFER_TOO_SMALL = 4,
31-
/// The data has not been updated within the timeframe set by local policy.
32-
WARN_STALE_DATA = 5,
33-
/// The resulting buffer contains UEFI-compliant file system.
34-
WARN_FILE_SYSTEM = 6,
35-
/// The operation will be processed across a system reset.
36-
WARN_RESET_REQUIRED = 7,
37-
38-
/// The image failed to load.
39-
LOAD_ERROR = ERROR_BIT | 1,
40-
/// A parameter was incorrect.
41-
INVALID_PARAMETER = ERROR_BIT | 2,
42-
/// The operation is not supported.
43-
UNSUPPORTED = ERROR_BIT | 3,
44-
/// The buffer was not the proper size for the request.
45-
BAD_BUFFER_SIZE = ERROR_BIT | 4,
46-
/// The buffer is not large enough to hold the requested data.
47-
/// The required buffer size is returned in the appropriate parameter.
48-
BUFFER_TOO_SMALL = ERROR_BIT | 5,
49-
/// There is no data pending upon return.
50-
NOT_READY = ERROR_BIT | 6,
51-
/// The physical device reported an error while attempting the operation.
52-
DEVICE_ERROR = ERROR_BIT | 7,
53-
/// The device cannot be written to.
54-
WRITE_PROTECTED = ERROR_BIT | 8,
55-
/// A resource has run out.
56-
OUT_OF_RESOURCES = ERROR_BIT | 9,
57-
/// An inconstency was detected on the file system.
58-
VOLUME_CORRUPTED = ERROR_BIT | 10,
59-
/// There is no more space on the file system.
60-
VOLUME_FULL = ERROR_BIT | 11,
61-
/// The device does not contain any medium to perform the operation.
62-
NO_MEDIA = ERROR_BIT | 12,
63-
/// The medium in the device has changed since the last access.
64-
MEDIA_CHANGED = ERROR_BIT | 13,
65-
/// The item was not found.
66-
NOT_FOUND = ERROR_BIT | 14,
67-
/// Access was denied.
68-
ACCESS_DENIED = ERROR_BIT | 15,
69-
/// The server was not found or did not respond to the request.
70-
NO_RESPONSE = ERROR_BIT | 16,
71-
/// A mapping to a device does not exist.
72-
NO_MAPPING = ERROR_BIT | 17,
73-
/// The timeout time expired.
74-
TIMEOUT = ERROR_BIT | 18,
75-
/// The protocol has not been started.
76-
NOT_STARTED = ERROR_BIT | 19,
77-
/// The protocol has already been started.
78-
ALREADY_STARTED = ERROR_BIT | 20,
79-
/// The operation was aborted.
80-
ABORTED = ERROR_BIT | 21,
81-
/// An ICMP error occurred during the network operation.
82-
ICMP_ERROR = ERROR_BIT | 22,
83-
/// A TFTP error occurred during the network operation.
84-
TFTP_ERROR = ERROR_BIT | 23,
85-
/// A protocol error occurred during the network operation.
86-
PROTOCOL_ERROR = ERROR_BIT | 24,
87-
/// The function encountered an internal version that was
88-
/// incompatible with a version requested by the caller.
89-
INCOMPATIBLE_VERSION = ERROR_BIT | 25,
90-
/// The function was not performed due to a security violation.
91-
SECURITY_VIOLATION = ERROR_BIT | 26,
92-
/// A CRC error was detected.
93-
CRC_ERROR = ERROR_BIT | 27,
94-
/// Beginning or end of media was reached
95-
END_OF_MEDIA = ERROR_BIT | 28,
96-
/// The end of the file was reached.
97-
END_OF_FILE = ERROR_BIT | 31,
98-
/// The language specified was invalid.
99-
INVALID_LANGUAGE = ERROR_BIT | 32,
100-
/// The security status of the data is unknown or compromised and
101-
/// the data must be updated or replaced to restore a valid security status.
102-
COMPROMISED_DATA = ERROR_BIT | 33,
103-
/// There is an address conflict address allocation
104-
IP_ADDRESS_CONFLICT = ERROR_BIT | 34,
105-
/// A HTTP error occurred during the network operation.
106-
HTTP_ERROR = ERROR_BIT | 35,
107-
}}
108-
109-
impl Status {
110-
/// Returns true if status code indicates success.
111-
#[inline]
112-
#[must_use]
113-
pub fn is_success(self) -> bool {
114-
self == Status::SUCCESS
115-
}
116-
117-
/// Returns true if status code indicates a warning.
118-
#[inline]
119-
#[must_use]
120-
pub fn is_warning(self) -> bool {
121-
(self != Status::SUCCESS) && (self.0 & ERROR_BIT == 0)
122-
}
123-
124-
/// Returns true if the status code indicates an error.
125-
#[inline]
126-
#[must_use]
127-
pub const fn is_error(self) -> bool {
128-
self.0 & ERROR_BIT != 0
129-
}
130-
}
4+
pub use uefi_raw::Status;
1315

1326
/// Extension trait which provides some convenience methods for [`Status`].
1337
pub trait StatusExt {
@@ -208,12 +82,6 @@ impl StatusExt for Status {
20882
}
20983
}
21084

211-
impl core::fmt::Display for Status {
212-
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
213-
Debug::fmt(self, f)
214-
}
215-
}
216-
21785
#[cfg(test)]
21886
mod tests {
21987
use super::*;

0 commit comments

Comments
 (0)