Skip to content

Add Process support for UEFI #123196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
uefi: Add process
Signed-off-by: Ayush Singh <ayushdevel1325@gmail.com>
  • Loading branch information
Ayush1325 committed Jul 19, 2024
commit a8d7121e4a02764962c85e29168bebed2116b392
72 changes: 71 additions & 1 deletion library/std/src/sys/pal/uefi/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use r_efi::efi::{self, Guid};
use r_efi::protocols::{device_path, device_path_to_text};

use crate::ffi::OsString;
use crate::ffi::{OsString, OsStr};
use crate::io::{self, const_io_error};
use crate::mem::{size_of, MaybeUninit};
use crate::os::uefi::{self, env::boot_services, ffi::OsStringExt};
Expand Down Expand Up @@ -221,3 +221,73 @@ pub(crate) fn runtime_services() -> Option<NonNull<r_efi::efi::RuntimeServices>>
let runtime_services = unsafe { (*system_table.as_ptr()).runtime_services };
NonNull::new(runtime_services)
}

pub(crate) struct DevicePath(NonNull<r_efi::protocols::device_path::Protocol>);

impl DevicePath {
pub(crate) fn from_text(p: &OsStr) -> io::Result<Self> {
fn inner(
p: &OsStr,
protocol: NonNull<r_efi::protocols::device_path_from_text::Protocol>,
) -> io::Result<DevicePath> {
let path_vec = p.encode_wide().chain(Some(0)).collect::<Vec<u16>>();
let path =
unsafe { ((*protocol.as_ptr()).convert_text_to_device_path)(path_vec.as_ptr()) };

NonNull::new(path).map(DevicePath).ok_or_else(|| {
const_io_error!(io::ErrorKind::InvalidFilename, "Invalid Device Path")
})
}

static LAST_VALID_HANDLE: AtomicPtr<crate::ffi::c_void> =
AtomicPtr::new(crate::ptr::null_mut());

if let Some(handle) = NonNull::new(LAST_VALID_HANDLE.load(Ordering::Acquire)) {
if let Ok(protocol) = open_protocol::<r_efi::protocols::device_path_from_text::Protocol>(
handle,
r_efi::protocols::device_path_from_text::PROTOCOL_GUID,
) {
return inner(p, protocol);
}
}

let handles = locate_handles(r_efi::protocols::device_path_from_text::PROTOCOL_GUID)?;
for handle in handles {
if let Ok(protocol) = open_protocol::<r_efi::protocols::device_path_from_text::Protocol>(
handle,
r_efi::protocols::device_path_from_text::PROTOCOL_GUID,
) {
LAST_VALID_HANDLE.store(handle.as_ptr(), Ordering::Release);
return inner(p, protocol);
}
}

io::Result::Err(const_io_error!(
io::ErrorKind::NotFound,
"DevicePathFromText Protocol not found"
))
}
}

impl AsRef<r_efi::protocols::device_path::Protocol> for DevicePath {
fn as_ref(&self) -> &r_efi::protocols::device_path::Protocol {
unsafe { self.0.as_ref() }
}
}

impl AsMut<r_efi::protocols::device_path::Protocol> for DevicePath {
fn as_mut(&mut self) -> &mut r_efi::protocols::device_path::Protocol {
unsafe { self.0.as_mut() }
}
}

impl Drop for DevicePath {
fn drop(&mut self) {
if let Some(bt) = boot_services() {
let bt: NonNull<r_efi::efi::BootServices> = bt.cast();
unsafe {
((*bt.as_ptr()).free_pool)(self.0.as_ptr() as *mut crate::ffi::c_void);
}
}
}
}
1 change: 0 additions & 1 deletion library/std/src/sys/pal/uefi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub mod net;
pub mod os;
#[path = "../unsupported/pipe.rs"]
pub mod pipe;
#[path = "../unsupported/process.rs"]
pub mod process;
pub mod stdio;
pub mod thread;
Expand Down
Loading