Skip to content

Commit 1db9069

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

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

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

+53-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::ffi::OsString;
1416
use crate::mem::{size_of, MaybeUninit};
1517
use crate::os::uefi;
18+
use crate::os::uefi::ffi::OsStringExt;
1619
use crate::ptr::NonNull;
20+
use crate::slice;
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,52 @@ 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+
// DisplayOnly
172+
r_efi::efi::Boolean::FALSE,
173+
// AllowShortcuts
174+
r_efi::efi::Boolean::FALSE,
175+
)
176+
};
177+
178+
// SAFETY: Error only occurs if path is NULL. Thus no need to dealloc path_ptr
179+
let path_len = unsafe {
180+
WStrUnits::new(path_ptr)
181+
.ok_or(io::const_io_error!(io::ErrorKind::InvalidData, "Invalid path"))?
182+
.count()
183+
};
184+
185+
let path = OsString::from_wide(unsafe { slice::from_raw_parts(path_ptr.cast(), path_len) });
186+
187+
if let Some(boot_services) = crate::os::uefi::env::boot_services() {
188+
let boot_services: NonNull<r_efi::efi::BootServices> = boot_services.cast();
189+
unsafe {
190+
((*boot_services.as_ptr()).free_pool)(path_ptr.cast());
191+
}
192+
}
193+
194+
return Ok(path);
195+
}
196+
197+
Err(io::const_io_error!(io::ErrorKind::NotFound, "No device path to text protocol found"))
148198
}

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

+6-2
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 {
@@ -164,7 +165,10 @@ impl fmt::Display for JoinPathsError {
164165
impl StdError for JoinPathsError {}
165166

166167
pub fn current_exe() -> io::Result<PathBuf> {
167-
unsupported()
168+
let protocol = helpers::image_handle_protocol::<device_path::Protocol>(
169+
loaded_image_device_path::PROTOCOL_GUID,
170+
)?;
171+
helpers::device_path_to_text(protocol.as_ptr()).map(PathBuf::from)
168172
}
169173

170174
pub struct Env(!);

0 commit comments

Comments
 (0)