Skip to content

Commit 052b053

Browse files
committed
Expand sys/os for UEFI
- Implement current_exe() and getcwd() Signed-off-by: Ayush Singh <[email protected]>
1 parent 04521fd commit 052b053

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

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

+48-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@
1010
//! - More information about protocols can be found [here](https://edk2-docs.gitbook.io/edk-ii-uefi-driver-writer-s-guide/3_foundation/36_protocols_and_handles)
1111
1212
use r_efi::efi::{self, Guid};
13+
use r_efi::protocols::{device_path, device_path_to_text};
1314

15+
use crate::alloc::Layout;
16+
use crate::ffi::OsString;
1417
use crate::mem::{size_of, MaybeUninit};
1518
use crate::os::uefi;
19+
use crate::os::uefi::ffi::OsStringExt;
1620
use crate::ptr::NonNull;
21+
use crate::sys_common::wstr::WStrUnits;
1722
use crate::{
1823
io::{self, const_io_error},
1924
os::uefi::env::boot_services,
@@ -142,7 +147,47 @@ pub(crate) unsafe fn close_event(evt: NonNull<crate::ffi::c_void>) -> io::Result
142147

143148
/// Get the Protocol for current system handle.
144149
/// Note: Some protocols need to be manually freed. It is the callers responsibility to do so.
145-
pub(crate) fn image_handle_protocol<T>(protocol_guid: Guid) -> Option<NonNull<T>> {
146-
let system_handle = uefi::env::try_image_handle()?;
147-
open_protocol(system_handle, protocol_guid).ok()
150+
pub(crate) fn image_handle_protocol<T>(protocol_guid: Guid) -> io::Result<NonNull<T>> {
151+
let system_handle = uefi::env::try_image_handle().ok_or(io::const_io_error!(
152+
io::ErrorKind::NotFound,
153+
"Protocol not found in Image handle"
154+
))?;
155+
open_protocol(system_handle, protocol_guid)
156+
}
157+
158+
pub(crate) fn device_path_to_text(path: *mut device_path::Protocol) -> io::Result<OsString> {
159+
let device_path_to_text_handles = locate_handles(device_path_to_text::PROTOCOL_GUID)?;
160+
for handle in device_path_to_text_handles {
161+
let Ok(protocol) = open_protocol::<device_path_to_text::Protocol>(
162+
handle,
163+
device_path_to_text::PROTOCOL_GUID,
164+
) else {
165+
continue;
166+
};
167+
168+
let path_ptr: *mut r_efi::efi::Char16 = unsafe {
169+
((*protocol.as_ptr()).convert_device_path_to_text)(
170+
path,
171+
r_efi::efi::Boolean::FALSE,
172+
r_efi::efi::Boolean::FALSE,
173+
)
174+
};
175+
176+
// SAFETY: Error only occurs if path is NULL. Thus no need to dealloc path_ptr
177+
let path_units = unsafe {
178+
WStrUnits::new(path_ptr)
179+
.ok_or(io::const_io_error!(io::ErrorKind::InvalidData, "Invalid path"))
180+
}?;
181+
182+
let path_vec = path_units.map(Into::into).collect::<Vec<u16>>();
183+
unsafe {
184+
crate::alloc::dealloc(
185+
path_ptr.cast(),
186+
Layout::from_size_align_unchecked(path_vec.len() * size_of::<u16>(), 8),
187+
);
188+
}
189+
return Ok(OsString::from_wide(&path_vec));
190+
}
191+
192+
Err(io::const_io_error!(io::ErrorKind::NotFound, "No device path to text protocol found"))
148193
}

library/std/src/sys/pal/uefi/os.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{unsupported, RawOsError};
1+
use super::{helpers, unsupported, RawOsError};
22
use crate::error::Error as StdError;
33
use crate::ffi::{OsStr, OsString};
44
use crate::fmt;
@@ -7,6 +7,7 @@ use crate::marker::PhantomData;
77
use crate::os::uefi;
88
use crate::path::{self, PathBuf};
99
use crate::ptr::NonNull;
10+
use r_efi::efi::protocols::{device_path, loaded_image_device_path};
1011
use r_efi::efi::Status;
1112

1213
pub fn errno() -> RawOsError {
@@ -124,7 +125,9 @@ pub fn error_string(errno: RawOsError) -> String {
124125
}
125126

126127
pub fn getcwd() -> io::Result<PathBuf> {
127-
unsupported()
128+
let mut p = current_exe()?;
129+
p.pop();
130+
Ok(p)
128131
}
129132

130133
pub fn chdir(_: &path::Path) -> io::Result<()> {
@@ -164,7 +167,10 @@ impl fmt::Display for JoinPathsError {
164167
impl StdError for JoinPathsError {}
165168

166169
pub fn current_exe() -> io::Result<PathBuf> {
167-
unsupported()
170+
let protocol = helpers::image_handle_protocol::<device_path::Protocol>(
171+
loaded_image_device_path::PROTOCOL_GUID,
172+
)?;
173+
helpers::device_path_to_text(protocol.as_ptr()).map(PathBuf::from)
168174
}
169175

170176
pub struct Env(!);

0 commit comments

Comments
 (0)