Skip to content

Commit d84c8e3

Browse files
committed
Fixes from PR
1. Make Errors constant 2. Fix UCS-2 conversions: char is UTF-32, and not UTF-8. Thus converting UCS-2 to and from char can be done using basic u32 to conversions. 3. Use String and str intenally in `os_str` Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
1 parent 1f52c23 commit d84c8e3

File tree

21 files changed

+354
-433
lines changed

21 files changed

+354
-433
lines changed

library/std/src/io/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub(crate) use error::const_io_error;
287287
mod buffered;
288288
pub(crate) mod copy;
289289
mod cursor;
290-
mod error;
290+
pub(crate) mod error;
291291
mod impls;
292292
pub mod prelude;
293293
mod readbuf;
@@ -2406,11 +2406,7 @@ impl<T: BufRead, U: BufRead> BufRead for Chain<T, U> {
24062406
}
24072407

24082408
fn consume(&mut self, amt: usize) {
2409-
if !self.done_first {
2410-
self.first.consume(amt)
2411-
} else {
2412-
self.second.consume(amt)
2413-
}
2409+
if !self.done_first { self.first.consume(amt) } else { self.second.consume(amt) }
24142410
}
24152411
}
24162412

library/std/src/os/uefi/env.rs

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::io;
66
use crate::mem::MaybeUninit;
77
use crate::ptr::NonNull;
88
use crate::sync::atomic::{AtomicPtr, Ordering};
9-
use r_efi::efi::{Guid, Handle, Status};
9+
use r_efi::efi::{Guid, Handle};
1010
use r_efi::system;
1111

1212
static GLOBAL_SYSTEM_TABLE: AtomicPtr<SystemTable> = AtomicPtr::new(crate::ptr::null_mut());
@@ -48,35 +48,14 @@ pub fn get_runtime_services() -> Option<NonNull<RuntimeServices>> {
4848
NonNull::new(runtime_services)
4949
}
5050

51-
/// Get the Protocol for current system handle.
52-
/// Note: Some protocols need to be manually freed. It is the callers responsibility to do so.
53-
pub(crate) fn get_current_handle_protocol<T>(protocol_guid: &mut Guid) -> Option<NonNull<T>> {
54-
let system_handle = get_system_handle()?;
55-
get_handle_protocol(system_handle, protocol_guid)
56-
}
57-
58-
pub(crate) fn get_handle_protocol<T>(
59-
handle: NonNull<c_void>,
60-
protocol_guid: &mut Guid,
61-
) -> Option<NonNull<T>> {
62-
let boot_services = get_boot_services()?;
63-
let mut protocol: *mut c_void = crate::ptr::null_mut();
64-
65-
let r = unsafe {
66-
((*boot_services.as_ptr()).handle_protocol)(handle.as_ptr(), protocol_guid, &mut protocol)
67-
};
68-
69-
if r.is_error() { None } else { NonNull::new(protocol.cast()) }
70-
}
71-
7251
pub(crate) fn open_protocol<T>(
7352
handle: NonNull<c_void>,
7453
mut protocol_guid: Guid,
7554
) -> io::Result<NonNull<T>> {
7655
let boot_services = get_boot_services()
77-
.ok_or(io::Error::new(io::ErrorKind::Other, "Failed to get BootServices"))?;
56+
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Failed to get BootServices"))?;
7857
let system_handle = get_system_handle()
79-
.ok_or(io::Error::new(io::ErrorKind::Other, "Failed to get System Handle"))?;
58+
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Failed to get System Handle"))?;
8059
let mut protocol: MaybeUninit<*mut T> = MaybeUninit::uninit();
8160

8261
let r = unsafe {
@@ -91,27 +70,10 @@ pub(crate) fn open_protocol<T>(
9170
};
9271

9372
if r.is_error() {
94-
match r {
95-
Status::INVALID_PARAMETER => {
96-
Err(io::Error::new(io::ErrorKind::InvalidInput, "EFI_INVALID_PARAMETER"))
97-
}
98-
Status::UNSUPPORTED => {
99-
Err(io::Error::new(io::ErrorKind::Unsupported, "Handle does not support Protocol"))
100-
}
101-
Status::ACCESS_DENIED => {
102-
Err(io::Error::new(io::ErrorKind::PermissionDenied, "EFI_ACCESS_DENIED"))
103-
}
104-
Status::ALREADY_STARTED => {
105-
Err(io::Error::new(io::ErrorKind::Other, "EFI_ALREADY_STARTED"))
106-
}
107-
_ => Err(io::Error::new(
108-
io::ErrorKind::Uncategorized,
109-
format!("Status: {}", r.as_usize()),
110-
)),
111-
}
73+
Err(super::io::status_to_io_error(r))
11274
} else {
11375
NonNull::new(unsafe { protocol.assume_init() })
114-
.ok_or(io::Error::new(io::ErrorKind::Other, "Null Protocol"))
76+
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Null Protocol"))
11577
}
11678
}
11779

@@ -132,30 +94,13 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<c_void>>>
13294
)
13395
};
13496

135-
if r.is_error() {
136-
match r {
137-
Status::NOT_FOUND => {
138-
Err(io::Error::new(io::ErrorKind::NotFound, "No handles match the search"))
139-
}
140-
Status::BUFFER_TOO_SMALL => Err(io::Error::new(
141-
io::ErrorKind::InvalidData,
142-
"The BufferSize is too small for the result",
143-
)),
144-
Status::INVALID_PARAMETER => {
145-
Err(io::Error::new(io::ErrorKind::InvalidInput, "EFI_INVALID_PARAMETER"))
146-
}
147-
_ => Err(io::Error::new(
148-
io::ErrorKind::Uncategorized,
149-
format!("Status: {}", r.as_usize()),
150-
)),
151-
}
152-
} else {
153-
Ok(())
154-
}
97+
if r.is_error() { Err(super::io::status_to_io_error(r)) } else { Ok(()) }
15598
}
15699

157-
let boot_services = get_boot_services()
158-
.ok_or(io::Error::new(io::ErrorKind::Other, "Unable to acquire boot services"))?;
100+
let boot_services = get_boot_services().ok_or(io::error::const_io_error!(
101+
io::ErrorKind::Other,
102+
"Unable to acquire boot services"
103+
))?;
159104
let mut buf_len = 0usize;
160105

161106
match inner(&mut guid, boot_services, &mut buf_len, crate::ptr::null_mut()) {

library/std/src/os/uefi/io.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use r_efi::efi::Status;
2+
3+
use crate::io::{self, ErrorKind};
4+
5+
pub(crate) fn status_to_io_error(s: r_efi::efi::Status) -> io::Error {
6+
match s {
7+
Status::INVALID_PARAMETER => {
8+
io::error::const_io_error!(ErrorKind::InvalidInput, "EFI_INVALID_PARAMETER")
9+
}
10+
Status::UNSUPPORTED => {
11+
io::error::const_io_error!(ErrorKind::Unsupported, "EFI_UNSUPPORTED")
12+
}
13+
Status::BAD_BUFFER_SIZE => {
14+
io::error::const_io_error!(ErrorKind::InvalidData, "EFI_BAD_BUFFER_SIZE")
15+
}
16+
Status::INVALID_LANGUAGE => {
17+
io::error::const_io_error!(ErrorKind::InvalidData, "EFI_INVALID_LANGUAGE")
18+
}
19+
Status::CRC_ERROR => io::error::const_io_error!(ErrorKind::InvalidData, "EFI_CRC_ERROR"),
20+
Status::BUFFER_TOO_SMALL => {
21+
io::error::const_io_error!(ErrorKind::FileTooLarge, "EFI_BUFFER_TOO_SMALL")
22+
}
23+
Status::NOT_READY => io::error::const_io_error!(ErrorKind::ResourceBusy, "EFI_NOT_READY"),
24+
Status::WRITE_PROTECTED => {
25+
io::error::const_io_error!(ErrorKind::ReadOnlyFilesystem, "EFI_WRITE_PROTECTED")
26+
}
27+
Status::VOLUME_FULL => {
28+
io::error::const_io_error!(ErrorKind::StorageFull, "EFI_VOLUME_FULL")
29+
}
30+
Status::MEDIA_CHANGED => {
31+
io::error::const_io_error!(ErrorKind::StaleNetworkFileHandle, "EFI_MEDIA_CHANGED")
32+
}
33+
Status::NOT_FOUND => io::error::const_io_error!(ErrorKind::NotFound, "EFI_NOT_FOUND"),
34+
Status::ACCESS_DENIED => {
35+
io::error::const_io_error!(ErrorKind::PermissionDenied, "EFI_ACCESS_DENIED")
36+
}
37+
Status::SECURITY_VIOLATION => {
38+
io::error::const_io_error!(ErrorKind::PermissionDenied, "EFI_SECURITY_VIOLATION")
39+
}
40+
Status::NO_RESPONSE => {
41+
io::error::const_io_error!(ErrorKind::HostUnreachable, "EFI_NO_RESPONSE")
42+
}
43+
Status::TIMEOUT => io::error::const_io_error!(ErrorKind::TimedOut, "EFI_TIMEOUT"),
44+
Status::END_OF_FILE => {
45+
io::error::const_io_error!(ErrorKind::UnexpectedEof, "EFI_END_OF_FILE")
46+
}
47+
Status::IP_ADDRESS_CONFLICT => {
48+
io::error::const_io_error!(ErrorKind::AddrInUse, "EFI_IP_ADDRESS_CONFLICT")
49+
}
50+
Status::HTTP_ERROR => {
51+
io::error::const_io_error!(ErrorKind::NetworkUnreachable, "EFI_HTTP_ERROR")
52+
}
53+
_ => io::Error::new(ErrorKind::Uncategorized, format!("Status: {}", s.as_usize())),
54+
}
55+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
pub mod env;
66
pub mod ffi;
7+
pub mod io;
78
pub mod net;
89
pub mod path;
910
pub mod raw;
10-
pub mod thread;

library/std/src/os/uefi/path.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ impl TryFrom<NonNull<device_path::Protocol>> for PathBuf {
4444
let mut path = String::new();
4545
for c in ucs2_iter {
4646
let ch = char::from(ucs2::Ucs2Char::from_u16(u16::from(c)).ok_or(
47-
io::Error::new(io::ErrorKind::InvalidData, "Invalid Device Path Text"),
47+
io::error::const_io_error!(
48+
io::ErrorKind::InvalidData,
49+
"Invalid Device Path Text"
50+
),
4851
)?);
4952
path.push(ch);
5053
len += 1;
@@ -58,7 +61,7 @@ impl TryFrom<NonNull<device_path::Protocol>> for PathBuf {
5861
}
5962
return Ok(PathBuf::from(path));
6063
}
61-
Err(crate::io::Error::new(
64+
Err(crate::io::error::const_io_error!(
6265
crate::io::ErrorKind::InvalidData,
6366
"Failed to Convert to text representation",
6467
))
@@ -92,7 +95,7 @@ impl TryFrom<&OsStr> for DevicePath {
9295
};
9396
let device_path = match NonNull::new(device_path) {
9497
None => {
95-
return Err(io::Error::new(
98+
return Err(io::error::const_io_error!(
9699
io::ErrorKind::Uncategorized,
97100
"Null DevicePath Returned",
98101
));
@@ -105,7 +108,7 @@ impl TryFrom<&OsStr> for DevicePath {
105108
.unwrap();
106109
return Ok(DevicePath::new(device_path, layout));
107110
}
108-
Err(crate::io::Error::new(
111+
Err(crate::io::error::const_io_error!(
109112
crate::io::ErrorKind::InvalidData,
110113
"Failed to Convert to text representation",
111114
))

library/std/src/os/uefi/raw.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,17 @@ impl<T> VariableSizeType<T> {
2121
}
2222

2323
pub(crate) fn from_size(size: usize) -> io::Result<Self> {
24-
let layout = Layout::from_size_align(size, Self::ALIGNMENT)
25-
.map_err(|_| io::Error::new(io::ErrorKind::Uncategorized, "Invalid buffer size"))?;
24+
let layout = Layout::from_size_align(size, Self::ALIGNMENT).map_err(|_| {
25+
io::error::const_io_error!(io::ErrorKind::Uncategorized, "Invalid buffer size")
26+
})?;
2627
let inner: NonNull<T> = Global
2728
.allocate(layout)
28-
.map_err(|_| io::Error::new(io::ErrorKind::Uncategorized, "Failed to allocate Buffer"))?
29+
.map_err(|_| {
30+
io::error::const_io_error!(
31+
io::ErrorKind::Uncategorized,
32+
"Failed to allocate Buffer"
33+
)
34+
})?
2935
.cast();
3036
Ok(Self::new(inner, layout))
3137
}

library/std/src/os/uefi/thread.rs

Lines changed: 0 additions & 108 deletions
This file was deleted.

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use super::common;
12
use crate::ffi::OsString;
23
use crate::fmt;
34
use crate::num::NonZeroU16;
4-
use crate::os::uefi::{self, ffi::OsStringExt};
5+
use crate::os::uefi::ffi::OsStringExt;
56
use crate::sys_common::ucs2::Ucs2Units;
67
use crate::vec;
78
use core::iter;
@@ -14,7 +15,7 @@ pub fn args() -> Args {
1415
use r_efi::efi::protocols::loaded_image;
1516

1617
let mut protocol_guid = loaded_image::PROTOCOL_GUID;
17-
match uefi::env::get_current_handle_protocol::<loaded_image::Protocol>(&mut protocol_guid) {
18+
match common::get_current_handle_protocol::<loaded_image::Protocol>(&mut protocol_guid) {
1819
Some(x) => {
1920
let lp_cmd_line = unsafe { (*x.as_ptr()).load_options as *const u16 };
2021
let parsed_args_list =

0 commit comments

Comments
 (0)