Skip to content

Commit

Permalink
uefi: process: Fixes from PR
Browse files Browse the repository at this point in the history
- Update system table crc32
- Fix unsound use of Box
- Free exit data
- Code improvements
- Introduce OwnedTable

Signed-off-by: Ayush Singh <ayushdevel1325@gmail.com>
  • Loading branch information
Ayush1325 committed May 16, 2024
1 parent bba9b48 commit a132340
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 91 deletions.
75 changes: 54 additions & 21 deletions library/std/src/sys/pal/uefi/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,33 +292,25 @@ impl Drop for DevicePath {
}
}

pub(crate) struct Protocol<T> {
pub(crate) struct OwnedProtocol<T> {
guid: r_efi::efi::Guid,
handle: NonNull<crate::ffi::c_void>,
protocol: Box<T>,
protocol: *mut T,
}

impl<T> Protocol<T> {
const fn new(
guid: r_efi::efi::Guid,
handle: NonNull<crate::ffi::c_void>,
protocol: Box<T>,
) -> Self {
Self { guid, handle, protocol }
}

impl<T> OwnedProtocol<T> {
pub(crate) fn create(protocol: T, mut guid: r_efi::efi::Guid) -> io::Result<Self> {
let boot_services: NonNull<r_efi::efi::BootServices> =
boot_services().ok_or(BOOT_SERVICES_UNAVAILABLE)?.cast();
let mut protocol = Box::new(protocol);
let protocol: *mut T = Box::into_raw(Box::new(protocol));
let mut handle: r_efi::efi::Handle = crate::ptr::null_mut();

let r = unsafe {
((*boot_services.as_ptr()).install_protocol_interface)(
&mut handle,
&mut guid,
r_efi::efi::NATIVE_INTERFACE,
protocol.as_mut() as *mut T as *mut crate::ffi::c_void,
protocol as *mut crate::ffi::c_void,
)
};

Expand All @@ -329,37 +321,78 @@ impl<T> Protocol<T> {
let handle = NonNull::new(handle)
.ok_or(io::const_io_error!(io::ErrorKind::Uncategorized, "found null handle"))?;

Ok(Self::new(guid, handle, protocol))
Ok(Self { guid, handle, protocol })
}

pub(crate) fn handle(&self) -> NonNull<crate::ffi::c_void> {
self.handle
}
}

impl<T> Drop for Protocol<T> {
impl<T> Drop for OwnedProtocol<T> {
fn drop(&mut self) {
if let Some(bt) = boot_services() {
let bt: NonNull<r_efi::efi::BootServices> = bt.cast();
unsafe {
((*bt.as_ptr()).uninstall_protocol_interface)(
self.handle.as_ptr(),
&mut self.guid,
self.protocol.as_mut() as *mut T as *mut crate::ffi::c_void,
self.protocol as *mut crate::ffi::c_void,
)
};
}

let _ = unsafe { Box::from_raw(self.protocol) };
}
}

impl<T> AsRef<T> for Protocol<T> {
impl<T> AsRef<T> for OwnedProtocol<T> {
fn as_ref(&self) -> &T {
&self.protocol
unsafe { self.protocol.as_ref().unwrap() }
}
}

pub(crate) struct OwnedTable<T> {
layout: crate::alloc::Layout,
ptr: *mut T,
}

impl<T> OwnedTable<T> {
pub(crate) fn from_table_header(hdr: &r_efi::efi::TableHeader) -> Self {
let header_size = hdr.header_size as usize;
let layout = crate::alloc::Layout::from_size_align(header_size, 8).unwrap();
let ptr = unsafe { crate::alloc::alloc(layout) as *mut T };
Self { layout, ptr }
}

pub(crate) const fn as_ptr(&self) -> *const T {
self.ptr
}

pub(crate) const fn as_mut_ptr(&self) -> *mut T {
self.ptr
}
}

impl OwnedTable<r_efi::efi::SystemTable> {
pub(crate) fn from_table(tbl: *const r_efi::efi::SystemTable) -> Self {
let hdr = unsafe { (*tbl).hdr };

let owned_tbl = Self::from_table_header(&hdr);
unsafe {
crate::ptr::copy_nonoverlapping(
tbl as *const u8,
owned_tbl.as_mut_ptr() as *mut u8,
hdr.header_size as usize,
)
};

owned_tbl
}
}

impl<T> AsMut<T> for Protocol<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.protocol
impl<T> Drop for OwnedTable<T> {
fn drop(&mut self) {
unsafe { crate::alloc::dealloc(self.ptr as *mut u8, self.layout) };
}
}
Loading

0 comments on commit a132340

Please sign in to comment.