Skip to content

Commit 0552ba6

Browse files
committed
Use RawOsError for UEFI
Some changes from this commit will probably be converted to its own PR. Signed-off-by: Ayush Singh <[email protected]>
1 parent 4003646 commit 0552ba6

File tree

4 files changed

+261
-178
lines changed

4 files changed

+261
-178
lines changed

library/std/src/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@ pub fn log_wrapper<F: Fn(f64) -> f64>(n: f64, log_fn: F) -> f64 {
114114
log_fn(n)
115115
}
116116

117+
#[cfg(not(target_os = "uefi"))]
117118
pub type RawOsError = i32;

library/std/src/sys/uefi/helpers.rs

+4-172
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<crate::ff
4343
)
4444
};
4545

46-
if r.is_error() { Err(status_to_io_error(r)) } else { Ok(()) }
46+
if r.is_error() { Err(crate::io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
4747
}
4848

4949
let boot_services = boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
@@ -99,181 +99,13 @@ pub(crate) fn open_protocol<T>(
9999
};
100100

101101
if r.is_error() {
102-
Err(status_to_io_error(r))
102+
Err(crate::io::Error::from_raw_os_error(r.as_usize()))
103103
} else {
104104
NonNull::new(unsafe { protocol.assume_init() })
105105
.ok_or(const_io_error!(io::ErrorKind::Other, "null protocol"))
106106
}
107107
}
108108

109-
pub(crate) fn status_to_io_error(s: r_efi::efi::Status) -> io::Error {
110-
use io::ErrorKind;
111-
use r_efi::efi::Status;
112-
113-
// Keep the List in Alphabetical Order
114-
// The Messages are taken from UEFI Specification Appendix D - Status Codes
115-
match s {
116-
Status::ABORTED => {
117-
const_io_error!(ErrorKind::ConnectionAborted, "The operation was aborted.")
118-
}
119-
Status::ACCESS_DENIED => {
120-
const_io_error!(ErrorKind::PermissionDenied, "Access was denied.")
121-
}
122-
Status::ALREADY_STARTED => {
123-
const_io_error!(ErrorKind::Other, "The protocol has already been started.")
124-
}
125-
Status::BAD_BUFFER_SIZE => {
126-
const_io_error!(
127-
ErrorKind::InvalidData,
128-
"The buffer was not the proper size for the request."
129-
)
130-
}
131-
Status::BUFFER_TOO_SMALL => {
132-
const_io_error!(
133-
ErrorKind::FileTooLarge,
134-
"The buffer is not large enough to hold the requested data. The required buffer size is returned in the appropriate parameter when this error occurs."
135-
)
136-
}
137-
Status::COMPROMISED_DATA => {
138-
const_io_error!(
139-
ErrorKind::Other,
140-
"The security status of the data is unknown or compromised and the data must be updated or replaced to restore a valid security status."
141-
)
142-
}
143-
Status::CONNECTION_FIN => {
144-
const_io_error!(
145-
ErrorKind::Other,
146-
"The receiving operation fails because the communication peer has closed the connection and there is no more data in the receive buffer of the instance."
147-
)
148-
}
149-
Status::CONNECTION_REFUSED => {
150-
const_io_error!(
151-
ErrorKind::ConnectionRefused,
152-
"The receiving or transmission operation fails because this connection is refused."
153-
)
154-
}
155-
Status::CONNECTION_RESET => {
156-
const_io_error!(
157-
ErrorKind::ConnectionReset,
158-
"The connect fails because the connection is reset either by instance itself or the communication peer."
159-
)
160-
}
161-
Status::CRC_ERROR => const_io_error!(ErrorKind::Other, "A CRC error was detected."),
162-
Status::DEVICE_ERROR => const_io_error!(
163-
ErrorKind::Other,
164-
"The physical device reported an error while attempting the operation."
165-
),
166-
Status::END_OF_FILE => {
167-
const_io_error!(ErrorKind::UnexpectedEof, "The end of the file was reached.")
168-
}
169-
Status::END_OF_MEDIA => {
170-
const_io_error!(ErrorKind::Other, "Beginning or end of media was reached")
171-
}
172-
Status::HOST_UNREACHABLE => {
173-
const_io_error!(ErrorKind::HostUnreachable, "The remote host is not reachable.")
174-
}
175-
Status::HTTP_ERROR => {
176-
const_io_error!(ErrorKind::Other, "A HTTP error occurred during the network operation.")
177-
}
178-
Status::ICMP_ERROR => {
179-
const_io_error!(
180-
ErrorKind::Other,
181-
"An ICMP error occurred during the network operation."
182-
)
183-
}
184-
Status::INCOMPATIBLE_VERSION => {
185-
const_io_error!(
186-
ErrorKind::Other,
187-
"The function encountered an internal version that was incompatible with a version requested by the caller."
188-
)
189-
}
190-
Status::INVALID_LANGUAGE => {
191-
const_io_error!(ErrorKind::InvalidData, "The language specified was invalid.")
192-
}
193-
Status::INVALID_PARAMETER => {
194-
const_io_error!(ErrorKind::InvalidInput, "A parameter was incorrect.")
195-
}
196-
Status::IP_ADDRESS_CONFLICT => {
197-
const_io_error!(ErrorKind::AddrInUse, "There is an address conflict address allocation")
198-
}
199-
Status::LOAD_ERROR => {
200-
const_io_error!(ErrorKind::Other, "The image failed to load.")
201-
}
202-
Status::MEDIA_CHANGED => {
203-
const_io_error!(
204-
ErrorKind::Other,
205-
"The medium in the device has changed since the last access."
206-
)
207-
}
208-
Status::NETWORK_UNREACHABLE => {
209-
const_io_error!(
210-
ErrorKind::NetworkUnreachable,
211-
"The network containing the remote host is not reachable."
212-
)
213-
}
214-
Status::NO_MAPPING => {
215-
const_io_error!(ErrorKind::Other, "A mapping to a device does not exist.")
216-
}
217-
Status::NO_MEDIA => {
218-
const_io_error!(
219-
ErrorKind::Other,
220-
"The device does not contain any medium to perform the operation."
221-
)
222-
}
223-
Status::NO_RESPONSE => {
224-
const_io_error!(
225-
ErrorKind::HostUnreachable,
226-
"The server was not found or did not respond to the request."
227-
)
228-
}
229-
Status::NOT_FOUND => const_io_error!(ErrorKind::NotFound, "The item was not found."),
230-
Status::NOT_READY => {
231-
const_io_error!(ErrorKind::ResourceBusy, "There is no data pending upon return.")
232-
}
233-
Status::NOT_STARTED => {
234-
const_io_error!(ErrorKind::Other, "The protocol has not been started.")
235-
}
236-
Status::OUT_OF_RESOURCES => {
237-
const_io_error!(ErrorKind::OutOfMemory, "A resource has run out.")
238-
}
239-
Status::PROTOCOL_ERROR => {
240-
const_io_error!(
241-
ErrorKind::Other,
242-
"A protocol error occurred during the network operation."
243-
)
244-
}
245-
Status::PROTOCOL_UNREACHABLE => {
246-
const_io_error!(ErrorKind::Other, "An ICMP protocol unreachable error is received.")
247-
}
248-
Status::SECURITY_VIOLATION => {
249-
const_io_error!(
250-
ErrorKind::PermissionDenied,
251-
"The function was not performed due to a security violation."
252-
)
253-
}
254-
Status::TFTP_ERROR => {
255-
const_io_error!(ErrorKind::Other, "A TFTP error occurred during the network operation.")
256-
}
257-
Status::TIMEOUT => const_io_error!(ErrorKind::TimedOut, "The timeout time expired."),
258-
Status::UNSUPPORTED => {
259-
const_io_error!(ErrorKind::Unsupported, "The operation is not supported.")
260-
}
261-
Status::VOLUME_FULL => {
262-
const_io_error!(ErrorKind::StorageFull, "There is no more space on the file system.")
263-
}
264-
Status::VOLUME_CORRUPTED => {
265-
const_io_error!(
266-
ErrorKind::Other,
267-
"An inconstancy was detected on the file system causing the operating to fail."
268-
)
269-
}
270-
Status::WRITE_PROTECTED => {
271-
const_io_error!(ErrorKind::ReadOnlyFilesystem, "The device cannot be written to.")
272-
}
273-
_ => io::Error::new(ErrorKind::Uncategorized, format!("Status: {}", s.as_usize())),
274-
}
275-
}
276-
277109
pub(crate) fn create_event(
278110
signal: u32,
279111
tpl: efi::Tpl,
@@ -288,7 +120,7 @@ pub(crate) fn create_event(
288120
(create_event)(signal, tpl, handler, context, &mut exit_boot_service_event)
289121
};
290122
if r.is_error() {
291-
Err(status_to_io_error(r))
123+
Err(crate::io::Error::from_raw_os_error(r.as_usize()))
292124
} else {
293125
NonNull::new(exit_boot_service_event)
294126
.ok_or(const_io_error!(io::ErrorKind::Other, "null protocol"))
@@ -305,5 +137,5 @@ pub(crate) unsafe fn close_event(evt: NonNull<crate::ffi::c_void>) -> io::Result
305137
(close_event)(evt.as_ptr())
306138
};
307139

308-
if r.is_error() { Err(status_to_io_error(r)) } else { Ok(()) }
140+
if r.is_error() { Err(crate::io::Error::from_raw_os_error(r.as_usize())) } else { Ok(()) }
309141
}

library/std/src/sys/uefi/mod.rs

+43-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub mod locks;
2929
pub mod net;
3030
#[path = "../unsupported/once.rs"]
3131
pub mod once;
32-
#[path = "../unsupported/os.rs"]
3332
pub mod os;
3433
#[path = "../windows/os_str.rs"]
3534
pub mod os_str;
@@ -52,6 +51,8 @@ mod helpers;
5251
#[cfg(test)]
5352
mod tests;
5453

54+
pub type RawOsError = usize;
55+
5556
use crate::cell::Cell;
5657
use crate::io as std_io;
5758
use crate::os::uefi;
@@ -110,14 +111,50 @@ pub const fn unsupported_err() -> std_io::Error {
110111
std_io::const_io_error!(std_io::ErrorKind::Unsupported, "operation not supported on UEFI",)
111112
}
112113

113-
pub fn decode_error_kind(code: i32) -> crate::io::ErrorKind {
114+
pub fn decode_error_kind(code: RawOsError) -> crate::io::ErrorKind {
114115
use crate::io::ErrorKind;
115116
use r_efi::efi::Status;
116117

117-
if let Ok(code) = usize::try_from(code) {
118-
helpers::status_to_io_error(Status::from_usize(code)).kind()
119-
} else {
120-
ErrorKind::Uncategorized
118+
match r_efi::efi::Status::from_usize(code) {
119+
Status::ALREADY_STARTED
120+
| Status::COMPROMISED_DATA
121+
| Status::CONNECTION_FIN
122+
| Status::CRC_ERROR
123+
| Status::DEVICE_ERROR
124+
| Status::END_OF_MEDIA
125+
| Status::HTTP_ERROR
126+
| Status::ICMP_ERROR
127+
| Status::INCOMPATIBLE_VERSION
128+
| Status::LOAD_ERROR
129+
| Status::MEDIA_CHANGED
130+
| Status::NO_MAPPING
131+
| Status::NO_MEDIA
132+
| Status::NOT_STARTED
133+
| Status::PROTOCOL_ERROR
134+
| Status::PROTOCOL_UNREACHABLE
135+
| Status::TFTP_ERROR
136+
| Status::VOLUME_CORRUPTED => ErrorKind::Other,
137+
Status::BAD_BUFFER_SIZE | Status::INVALID_LANGUAGE => ErrorKind::InvalidData,
138+
Status::ABORTED => ErrorKind::ConnectionAborted,
139+
Status::ACCESS_DENIED => ErrorKind::PermissionDenied,
140+
Status::BUFFER_TOO_SMALL => ErrorKind::FileTooLarge,
141+
Status::CONNECTION_REFUSED => ErrorKind::ConnectionRefused,
142+
Status::CONNECTION_RESET => ErrorKind::ConnectionReset,
143+
Status::END_OF_FILE => ErrorKind::UnexpectedEof,
144+
Status::HOST_UNREACHABLE => ErrorKind::HostUnreachable,
145+
Status::INVALID_PARAMETER => ErrorKind::InvalidInput,
146+
Status::IP_ADDRESS_CONFLICT => ErrorKind::AddrInUse,
147+
Status::NETWORK_UNREACHABLE => ErrorKind::NetworkUnreachable,
148+
Status::NO_RESPONSE => ErrorKind::HostUnreachable,
149+
Status::NOT_FOUND => ErrorKind::NotFound,
150+
Status::NOT_READY => ErrorKind::ResourceBusy,
151+
Status::OUT_OF_RESOURCES => ErrorKind::OutOfMemory,
152+
Status::SECURITY_VIOLATION => ErrorKind::PermissionDenied,
153+
Status::TIMEOUT => ErrorKind::TimedOut,
154+
Status::UNSUPPORTED => ErrorKind::Unsupported,
155+
Status::VOLUME_FULL => ErrorKind::StorageFull,
156+
Status::WRITE_PROTECTED => ErrorKind::ReadOnlyFilesystem,
157+
_ => ErrorKind::Uncategorized,
121158
}
122159
}
123160

0 commit comments

Comments
 (0)