Skip to content

Commit 36c628f

Browse files
committed
Refactor std::os::uefi
Removed almost all non-public APIs out of `std::os::uefi`. Signed-off-by: Ayush <ayushsingh1325@gmail.com>
1 parent 7262c22 commit 36c628f

File tree

15 files changed

+103
-214
lines changed

15 files changed

+103
-214
lines changed

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

Lines changed: 2 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22
33
use super::raw::{BootServices, RuntimeServices, SystemTable};
44
use crate::ffi::c_void;
5-
use crate::io;
6-
use crate::mem::MaybeUninit;
75
use crate::ptr::NonNull;
86
use crate::sync::atomic::{AtomicPtr, Ordering};
9-
use r_efi::efi::{Guid, Handle};
10-
use r_efi::system;
117

128
static GLOBAL_SYSTEM_TABLE: AtomicPtr<SystemTable> = AtomicPtr::new(crate::ptr::null_mut());
139
static GLOBAL_SYSTEM_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut());
1410

1511
/// Initializes Global Atomic Pointers to SystemTable and Handle.
1612
/// Should only be called once in the program execution under normal circumstances.
1713
/// The caller should ensure that the pointers are valid.
18-
pub(crate) fn init_globals(handle: NonNull<c_void>, system_table: NonNull<SystemTable>) {
14+
#[unstable(feature = "uefi_std", issue = "100499")]
15+
pub fn init_globals(handle: NonNull<c_void>, system_table: NonNull<SystemTable>) {
1916
GLOBAL_SYSTEM_TABLE.store(system_table.as_ptr(), Ordering::SeqCst);
2017
GLOBAL_SYSTEM_HANDLE.store(handle.as_ptr(), Ordering::SeqCst);
2118
}
@@ -47,84 +44,3 @@ pub fn get_runtime_services() -> Option<NonNull<RuntimeServices>> {
4744
let runtime_services = unsafe { (*system_table.as_ptr()).runtime_services };
4845
NonNull::new(runtime_services)
4946
}
50-
51-
/// Open Protocol on a handle
52-
/// Implemented using `EFI_BOOT_SERVICES.OpenProtocol()`
53-
pub(crate) fn open_protocol<T>(
54-
handle: NonNull<c_void>,
55-
mut protocol_guid: Guid,
56-
) -> io::Result<NonNull<T>> {
57-
let boot_services = get_boot_services()
58-
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Failed to get BootServices"))?;
59-
let system_handle = get_system_handle()
60-
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Failed to get System Handle"))?;
61-
let mut protocol: MaybeUninit<*mut T> = MaybeUninit::uninit();
62-
63-
let r = unsafe {
64-
((*boot_services.as_ptr()).open_protocol)(
65-
handle.as_ptr(),
66-
&mut protocol_guid,
67-
protocol.as_mut_ptr().cast(),
68-
system_handle.as_ptr(),
69-
crate::ptr::null_mut(),
70-
system::OPEN_PROTOCOL_GET_PROTOCOL,
71-
)
72-
};
73-
74-
if r.is_error() {
75-
Err(super::io::status_to_io_error(r))
76-
} else {
77-
NonNull::new(unsafe { protocol.assume_init() })
78-
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Null Protocol"))
79-
}
80-
}
81-
82-
// Locate handles with a particular protocol GUID
83-
/// Implemented using `EFI_BOOT_SERVICES.LocateHandles()`
84-
pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<c_void>>> {
85-
fn inner(
86-
guid: &mut Guid,
87-
boot_services: NonNull<BootServices>,
88-
buf_size: &mut usize,
89-
buf: *mut Handle,
90-
) -> io::Result<()> {
91-
let r = unsafe {
92-
((*boot_services.as_ptr()).locate_handle)(
93-
r_efi::efi::BY_PROTOCOL,
94-
guid,
95-
crate::ptr::null_mut(),
96-
buf_size,
97-
buf,
98-
)
99-
};
100-
101-
if r.is_error() { Err(super::io::status_to_io_error(r)) } else { Ok(()) }
102-
}
103-
104-
let boot_services = get_boot_services().ok_or(io::error::const_io_error!(
105-
io::ErrorKind::Other,
106-
"Unable to acquire boot services"
107-
))?;
108-
let mut buf_len = 0usize;
109-
110-
match inner(&mut guid, boot_services, &mut buf_len, crate::ptr::null_mut()) {
111-
Ok(()) => unreachable!(),
112-
Err(e) => match e.kind() {
113-
io::ErrorKind::FileTooLarge => {}
114-
_ => return Err(e),
115-
},
116-
}
117-
118-
// The returned buf_len is in bytes
119-
let mut buf: Vec<Handle> = Vec::with_capacity(buf_len / crate::mem::size_of::<Handle>());
120-
match inner(&mut guid, boot_services, &mut buf_len, buf.as_mut_ptr()) {
121-
Ok(()) => {
122-
// SAFETY: This is safe because the call will succeed only if buf_len >= required
123-
// length. Also, on success, the `buf_len` is updated with the size of bufferv (in
124-
// bytes) written
125-
unsafe { buf.set_len(buf_len / crate::mem::size_of::<Handle>()) };
126-
Ok(buf.iter().filter_map(|x| NonNull::new(*x)).collect())
127-
}
128-
Err(e) => Err(e),
129-
}
130-
}

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

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,4 @@
44

55
pub mod env;
66
pub mod ffi;
7-
pub mod io;
8-
pub mod net;
97
pub mod raw;

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

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

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

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::io;
55
use crate::mem::{self, MaybeUninit};
66
use crate::ops::{Deref, DerefMut};
77
use crate::os::uefi;
8-
use crate::os::uefi::io::status_to_io_error;
98
use crate::ptr::{NonNull, Unique};
109

1110
pub const BOOT_SERVICES_ERROR: io::Error =
@@ -286,3 +285,58 @@ pub(crate) fn open_protocol<T>(
286285
.ok_or(io::error::const_io_error!(io::ErrorKind::Other, "Null Protocol"))
287286
}
288287
}
288+
289+
pub(crate) fn status_to_io_error(s: r_efi::efi::Status) -> io::Error {
290+
use io::ErrorKind;
291+
use r_efi::efi::Status;
292+
293+
match s {
294+
Status::INVALID_PARAMETER => {
295+
io::error::const_io_error!(ErrorKind::InvalidInput, "EFI_INVALID_PARAMETER")
296+
}
297+
Status::UNSUPPORTED => {
298+
io::error::const_io_error!(ErrorKind::Unsupported, "EFI_UNSUPPORTED")
299+
}
300+
Status::BAD_BUFFER_SIZE => {
301+
io::error::const_io_error!(ErrorKind::InvalidData, "EFI_BAD_BUFFER_SIZE")
302+
}
303+
Status::INVALID_LANGUAGE => {
304+
io::error::const_io_error!(ErrorKind::InvalidData, "EFI_INVALID_LANGUAGE")
305+
}
306+
Status::CRC_ERROR => io::error::const_io_error!(ErrorKind::InvalidData, "EFI_CRC_ERROR"),
307+
Status::BUFFER_TOO_SMALL => {
308+
io::error::const_io_error!(ErrorKind::FileTooLarge, "EFI_BUFFER_TOO_SMALL")
309+
}
310+
Status::NOT_READY => io::error::const_io_error!(ErrorKind::ResourceBusy, "EFI_NOT_READY"),
311+
Status::WRITE_PROTECTED => {
312+
io::error::const_io_error!(ErrorKind::ReadOnlyFilesystem, "EFI_WRITE_PROTECTED")
313+
}
314+
Status::VOLUME_FULL => {
315+
io::error::const_io_error!(ErrorKind::StorageFull, "EFI_VOLUME_FULL")
316+
}
317+
Status::MEDIA_CHANGED => {
318+
io::error::const_io_error!(ErrorKind::StaleNetworkFileHandle, "EFI_MEDIA_CHANGED")
319+
}
320+
Status::NOT_FOUND => io::error::const_io_error!(ErrorKind::NotFound, "EFI_NOT_FOUND"),
321+
Status::ACCESS_DENIED => {
322+
io::error::const_io_error!(ErrorKind::PermissionDenied, "EFI_ACCESS_DENIED")
323+
}
324+
Status::SECURITY_VIOLATION => {
325+
io::error::const_io_error!(ErrorKind::PermissionDenied, "EFI_SECURITY_VIOLATION")
326+
}
327+
Status::NO_RESPONSE => {
328+
io::error::const_io_error!(ErrorKind::HostUnreachable, "EFI_NO_RESPONSE")
329+
}
330+
Status::TIMEOUT => io::error::const_io_error!(ErrorKind::TimedOut, "EFI_TIMEOUT"),
331+
Status::END_OF_FILE => {
332+
io::error::const_io_error!(ErrorKind::UnexpectedEof, "EFI_END_OF_FILE")
333+
}
334+
Status::IP_ADDRESS_CONFLICT => {
335+
io::error::const_io_error!(ErrorKind::AddrInUse, "EFI_IP_ADDRESS_CONFLICT")
336+
}
337+
Status::HTTP_ERROR => {
338+
io::error::const_io_error!(ErrorKind::NetworkUnreachable, "EFI_HTTP_ERROR")
339+
}
340+
_ => io::Error::new(ErrorKind::Uncategorized, format!("Status: {}", s.as_usize())),
341+
}
342+
}

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,10 @@ mod uefi_fs {
518518
use crate::io;
519519
use crate::mem::MaybeUninit;
520520
use crate::os::uefi::ffi::{OsStrExt, OsStringExt};
521-
use crate::os::uefi::io::status_to_io_error;
522521
use crate::path::{Path, PathBuf};
523522
use crate::ptr::NonNull;
524523
use crate::sys::uefi::alloc::POOL_ALIGNMENT;
525-
use crate::sys::uefi::common::{self, VariableBox};
524+
use crate::sys::uefi::common::{self, status_to_io_error, VariableBox};
526525
use r_efi::protocols::file;
527526

528527
// Wrapper around File Protocol. Automatically closes file/directories on being dropped.
@@ -592,14 +591,13 @@ mod uefi_fs {
592591
fn get_volume_from_prefix(prefix: &OsStr) -> io::Result<Self> {
593592
use r_efi::protocols::{device_path, simple_file_system};
594593

595-
let handles =
596-
match crate::os::uefi::env::locate_handles(simple_file_system::PROTOCOL_GUID) {
597-
Ok(x) => x,
598-
Err(e) => return Err(e),
599-
};
594+
let handles = match common::locate_handles(simple_file_system::PROTOCOL_GUID) {
595+
Ok(x) => x,
596+
Err(e) => return Err(e),
597+
};
600598
for handle in handles {
601599
let mut volume_device_path =
602-
match crate::os::uefi::env::open_protocol(handle, device_path::PROTOCOL_GUID) {
600+
match common::open_protocol(handle, device_path::PROTOCOL_GUID) {
603601
Ok(x) => x,
604602
Err(_) => continue,
605603
};

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub fn decode_error_kind(code: i32) -> crate::io::ErrorKind {
7070
use r_efi::efi::Status;
7171

7272
if let Ok(code) = usize::try_from(code) {
73-
uefi::io::status_to_io_error(Status::from_usize(code)).kind()
73+
common::status_to_io_error(Status::from_usize(code)).kind()
7474
} else {
7575
ErrorKind::Uncategorized
7676
}
@@ -126,10 +126,9 @@ unsafe fn get_random() -> Option<u64> {
126126
use r_efi::protocols::rng;
127127

128128
let mut buf = [0u8; 8];
129-
let handles = uefi::env::locate_handles(rng::PROTOCOL_GUID).ok()?;
129+
let handles = common::locate_handles(rng::PROTOCOL_GUID).ok()?;
130130
for handle in handles {
131-
if let Ok(protocol) = uefi::env::open_protocol::<rng::Protocol>(handle, rng::PROTOCOL_GUID)
132-
{
131+
if let Ok(protocol) = common::open_protocol::<rng::Protocol>(handle, rng::PROTOCOL_GUID) {
133132
let r = unsafe {
134133
((*protocol.as_ptr()).get_rng)(
135134
protocol.as_ptr(),

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@ mod tcp6;
66
pub use implementation::*;
77

88
mod uefi_service_binding {
9+
use super::super::common::{self, status_to_io_error};
910
use crate::io;
1011
use crate::mem::MaybeUninit;
11-
use crate::os::uefi;
12-
use crate::os::uefi::io::status_to_io_error;
1312
use crate::ptr::NonNull;
1413
use r_efi::protocols::service_binding;
1514

@@ -29,7 +28,7 @@ mod uefi_service_binding {
2928

3029
pub fn create_child(&self) -> io::Result<NonNull<crate::ffi::c_void>> {
3130
let service_binding_protocol: NonNull<service_binding::Protocol> =
32-
uefi::env::open_protocol(self.handle, self.service_binding_guid)?;
31+
common::open_protocol(self.handle, self.service_binding_guid)?;
3332
let mut child_handle: MaybeUninit<r_efi::efi::Handle> = MaybeUninit::uninit();
3433
let r = unsafe {
3534
((*service_binding_protocol.as_ptr()).create_child)(
@@ -48,7 +47,7 @@ mod uefi_service_binding {
4847

4948
pub fn destroy_child(&self, child_handle: NonNull<crate::ffi::c_void>) -> io::Result<()> {
5049
let service_binding_protocol: NonNull<service_binding::Protocol> =
51-
uefi::env::open_protocol(self.handle, self.service_binding_guid)?;
50+
common::open_protocol(self.handle, self.service_binding_guid)?;
5251
let r = unsafe {
5352
((*service_binding_protocol.as_ptr()).destroy_child)(
5453
service_binding_protocol.as_ptr(),
@@ -60,3 +59,13 @@ mod uefi_service_binding {
6059
}
6160
}
6261
}
62+
63+
#[inline]
64+
pub(crate) fn ipv4_to_r_efi(ip: &crate::net::Ipv4Addr) -> r_efi::efi::Ipv4Address {
65+
r_efi::efi::Ipv4Address { addr: ip.octets() }
66+
}
67+
68+
#[inline]
69+
pub(crate) fn ipv4_from_r_efi(ip: r_efi::efi::Ipv4Address) -> crate::net::Ipv4Addr {
70+
crate::net::Ipv4Addr::from(ip.addr)
71+
}

library/std/src/sys/uefi/net/tcp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
//! In the future, should probably desing networking around SIMPLE_NETWOR_PROTOCOL
33
44
use super::{tcp4, uefi_service_binding};
5+
use crate::sys::uefi::common;
56
use crate::{
67
io::{self, IoSlice, IoSliceMut},
78
net::{Ipv4Addr, Shutdown, SocketAddr, SocketAddrV4},
8-
os::uefi,
99
sys::unsupported,
1010
};
1111
use r_efi::protocols;
@@ -19,7 +19,7 @@ impl TcpProtocol {
1919
match addr {
2020
SocketAddr::V4(x) => {
2121
let handles =
22-
uefi::env::locate_handles(protocols::tcp4::SERVICE_BINDING_PROTOCOL_GUID)?;
22+
common::locate_handles(protocols::tcp4::SERVICE_BINDING_PROTOCOL_GUID)?;
2323

2424
// Try all handles
2525
for handle in handles {
@@ -64,7 +64,7 @@ impl TcpProtocol {
6464
match addr {
6565
SocketAddr::V4(addr) => {
6666
let handles =
67-
uefi::env::locate_handles(protocols::tcp4::SERVICE_BINDING_PROTOCOL_GUID)?;
67+
common::locate_handles(protocols::tcp4::SERVICE_BINDING_PROTOCOL_GUID)?;
6868

6969
// Try all handles
7070
for handle in handles {

0 commit comments

Comments
 (0)