Skip to content

Commit 8a95f09

Browse files
committed
Fixes from PR
Signed-off-by: Ayush Singh <[email protected]>
1 parent 8226547 commit 8a95f09

File tree

5 files changed

+46
-39
lines changed

5 files changed

+46
-39
lines changed

std/src/os/uefi/env.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! UEFI-specific extensions to the primitives in `std::env` module
22
3+
#![unstable(feature = "uefi_std", issue = "100499")]
4+
35
use crate::ffi::c_void;
46
use crate::ptr::NonNull;
57
use crate::sync::atomic::{AtomicPtr, Ordering};
@@ -22,21 +24,18 @@ static GLOBALS: OnceLock<(AtomicPtr<c_void>, AtomicPtr<c_void>)> = OnceLock::new
2224
/// standard library is loaded.
2325
///
2426
/// This function must not be called more than once.
25-
#[unstable(feature = "uefi_std", issue = "100499")]
2627
pub unsafe fn init_globals(handle: NonNull<c_void>, system_table: NonNull<c_void>) {
2728
GLOBALS.set((AtomicPtr::new(system_table.as_ptr()), AtomicPtr::new(handle.as_ptr()))).unwrap()
2829
}
2930

3031
/// Get the SystemTable Pointer.
31-
/// Note: This function panics if the System Table and Image Handle is Not initialized
32-
#[unstable(feature = "uefi_std", issue = "100499")]
32+
/// Note: This function panics if the System Table or Image Handle is not initialized
3333
pub fn system_table() -> NonNull<c_void> {
3434
try_system_table().unwrap()
3535
}
3636

37-
/// Get the SystemHandle Pointer.
38-
/// Note: This function panics if the System Table and Image Handle is Not initialized
39-
#[unstable(feature = "uefi_std", issue = "100499")]
37+
/// Get the ImageHandle Pointer.
38+
/// Note: This function panics if the System Table or Image Handle is not initialized
4039
pub fn image_handle() -> NonNull<c_void> {
4140
try_image_handle().unwrap()
4241
}

std/src/sys/uefi/alloc.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@ unsafe impl GlobalAlloc for System {
1313
Some(x) => x.as_ptr() as *mut _,
1414
};
1515

16-
if layout.size() > 0 {
17-
unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) }
18-
} else {
19-
layout.dangling().as_ptr()
20-
}
16+
// The caller must ensure non-0 layout
17+
unsafe { r_efi_alloc::raw::alloc(system_table, layout, MEMORY_TYPE) }
2118
}
2219

2320
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
2421
let system_table = match crate::os::uefi::env::try_system_table() {
2522
None => handle_alloc_error(layout),
2623
Some(x) => x.as_ptr() as *mut _,
2724
};
28-
if layout.size() > 0 {
29-
unsafe { r_efi_alloc::raw::dealloc(system_table, ptr, layout) }
30-
}
25+
// The caller must ensure non-0 layout
26+
unsafe { r_efi_alloc::raw::dealloc(system_table, ptr, layout) }
3127
}
3228
}

std/src/sys/uefi/common.rs renamed to std/src/sys/uefi/helpers.rs

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
//! Contains most of the shared UEFI specific stuff. Some of this might be moved to `std::os::uefi`
22
//! if needed but no point in adding extra public API when there is not Std support for UEFI in the
33
//! first place
4+
//!
5+
//! Some Nomenclature
6+
//! * Protocol:
7+
//! - Protocols serve to enable communication between separately built modules, including drivers.
8+
//! - Every protocol has a GUID associated with it. The GUID serves as the name for the protocol.
9+
//! - Protocols are produced and consumed.
10+
//! - 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)
411
512
use r_efi::efi::Guid;
613

714
use crate::io::{self, const_io_error};
8-
use crate::mem::MaybeUninit;
15+
use crate::mem::{size_of, MaybeUninit};
916
use crate::os::uefi;
1017
use crate::ptr::NonNull;
1118

12-
// Locate handles with a particular protocol GUID
19+
/// Locate Handles with a particular Protocol GUID
1320
/// Implemented using `EFI_BOOT_SERVICES.LocateHandles()`
21+
///
22+
/// Returns an array of [Handles](r_efi::efi::Handle) that support a specified protocol.
1423
pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<crate::ffi::c_void>>> {
1524
fn inner(
1625
guid: &mut Guid,
@@ -34,6 +43,8 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<crate::ff
3443
let boot_services = boot_services();
3544
let mut buf_len = 0usize;
3645

46+
// This should always fail since the size of buffer is 0. This call should update the buf_len
47+
// variable with the required buffer length
3748
match inner(&mut guid, boot_services, &mut buf_len, crate::ptr::null_mut()) {
3849
Ok(()) => unreachable!(),
3950
Err(e) => match e.kind() {
@@ -44,21 +55,23 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<crate::ff
4455

4556
// The returned buf_len is in bytes
4657
let mut buf: Vec<r_efi::efi::Handle> =
47-
Vec::with_capacity(buf_len / crate::mem::size_of::<r_efi::efi::Handle>());
58+
Vec::with_capacity(buf_len / size_of::<r_efi::efi::Handle>());
4859
match inner(&mut guid, boot_services, &mut buf_len, buf.as_mut_ptr()) {
4960
Ok(()) => {
50-
// SAFETY: This is safe because the call will succeed only if buf_len >= required
51-
// length. Also, on success, the `buf_len` is updated with the size of bufferv (in
52-
// bytes) written
53-
unsafe { buf.set_len(buf_len / crate::mem::size_of::<r_efi::efi::Handle>()) };
54-
Ok(buf.iter().filter_map(|x| NonNull::new(*x)).collect())
61+
// This is safe because the call will succeed only if buf_len >= required length.
62+
// Also, on success, the `buf_len` is updated with the size of bufferv (in bytes) written
63+
unsafe { buf.set_len(buf_len / size_of::<r_efi::efi::Handle>()) };
64+
Ok(buf.into_iter().filter_map(|x| NonNull::new(x)).collect())
5565
}
5666
Err(e) => Err(e),
5767
}
5868
}
5969

60-
/// Open Protocol on a handle
61-
/// Implemented using `EFI_BOOT_SERVICES.OpenProtocol()`
70+
/// Open Protocol on a handle.
71+
/// Internally just a call to `EFI_BOOT_SERVICES.OpenProtocol()`.
72+
///
73+
/// Queries a handle to determine if it supports a specified protocol. If the protocol is
74+
/// supported by the handle, it opens the protocol on behalf of the calling agent.
6275
pub(crate) fn open_protocol<T>(
6376
handle: NonNull<crate::ffi::c_void>,
6477
mut protocol_guid: Guid,
@@ -256,9 +269,7 @@ pub(crate) fn status_to_io_error(s: r_efi::efi::Status) -> io::Error {
256269

257270
/// Get the BootServices Pointer.
258271
pub(crate) fn boot_services() -> NonNull<r_efi::efi::BootServices> {
259-
let system_table: NonNull<r_efi::efi::SystemTable> = uefi::env::system_table().cast();
260-
let boot_services = unsafe { (*system_table.as_ptr()).boot_services };
261-
NonNull::new(boot_services).unwrap()
272+
try_boot_services().unwrap()
262273
}
263274
/// Get the BootServices Pointer.
264275
/// This function is mostly intended for places where panic is not an option

std/src/sys/uefi/mod.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub mod thread_local_key;
4747
#[path = "../unsupported/time.rs"]
4848
pub mod time;
4949

50-
pub(crate) mod common;
50+
pub(crate) mod helpers;
5151

5252
#[cfg(test)]
5353
mod tests;
@@ -60,18 +60,20 @@ pub mod memchr {
6060
pub use core::slice::memchr::{memchr, memrchr};
6161
}
6262

63-
// SAFETY: must be called only once during runtime initialization.
64-
// SAFETY: argc must be 2.
65-
// SAFETY: argv must be &[Handle, *mut SystemTable].
66-
pub unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {
63+
/// # SAFETY
64+
/// - must be called only once during runtime initialization.
65+
/// - argc must be 2.
66+
/// - argv must be &[Handle, *mut SystemTable].
67+
pub(crate) unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {
6768
assert_eq!(argc, 2);
6869
let image_handle = unsafe { NonNull::new(*argv as *mut crate::ffi::c_void).unwrap() };
6970
let system_table = unsafe { NonNull::new(*argv.add(1) as *mut crate::ffi::c_void).unwrap() };
7071
unsafe { crate::os::uefi::env::init_globals(image_handle, system_table) };
7172
}
7273

73-
// SAFETY: must be called only once during runtime cleanup.
74-
// NOTE: this is not guaranteed to run, for example when the program aborts.
74+
/// # SAFETY
75+
/// this is not guaranteed to run, for example when the program aborts.
76+
/// - must be called only once during runtime cleanup.
7577
pub unsafe fn cleanup() {}
7678

7779
#[inline]
@@ -89,15 +91,15 @@ pub fn decode_error_kind(code: i32) -> crate::io::ErrorKind {
8991
use r_efi::efi::Status;
9092

9193
if let Ok(code) = usize::try_from(code) {
92-
common::status_to_io_error(Status::from_usize(code)).kind()
94+
helpers::status_to_io_error(Status::from_usize(code)).kind()
9395
} else {
9496
ErrorKind::Uncategorized
9597
}
9698
}
9799

98100
pub fn abort_internal() -> ! {
99101
if let (Some(boot_services), Some(handle)) =
100-
(common::try_boot_services(), uefi::env::try_image_handle())
102+
(helpers::try_boot_services(), uefi::env::try_image_handle())
101103
{
102104
let _ = unsafe {
103105
((*boot_services.as_ptr()).exit)(
@@ -130,9 +132,9 @@ fn get_random() -> Option<(u64, u64)> {
130132
use r_efi::protocols::rng;
131133

132134
let mut buf = [0u8; 16];
133-
let handles = common::locate_handles(rng::PROTOCOL_GUID).ok()?;
135+
let handles = helpers::locate_handles(rng::PROTOCOL_GUID).ok()?;
134136
for handle in handles {
135-
if let Ok(protocol) = common::open_protocol::<rng::Protocol>(handle, rng::PROTOCOL_GUID) {
137+
if let Ok(protocol) = helpers::open_protocol::<rng::Protocol>(handle, rng::PROTOCOL_GUID) {
136138
let r = unsafe {
137139
((*protocol.as_ptr()).get_rng)(
138140
protocol.as_ptr(),

std/src/sys_common/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ cfg_if::cfg_if! {
4444

4545
cfg_if::cfg_if! {
4646
if #[cfg(any(target_os = "l4re",
47-
target_os = "hermit",
4847
target_os = "uefi",
4948
feature = "restricted-std",
5049
all(target_family = "wasm", not(target_os = "emscripten")),

0 commit comments

Comments
 (0)