|
| 1 | +//! UEFI-specific extensions to the primitives in `std::env` module |
| 2 | +
|
| 3 | +#![unstable(feature = "uefi_std", issue = "100499")] |
| 4 | + |
| 5 | +use crate::sync::atomic::{AtomicBool, AtomicPtr, Ordering}; |
| 6 | +use crate::{ffi::c_void, ptr::NonNull}; |
| 7 | + |
| 8 | +static SYSTEM_TABLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut()); |
| 9 | +static IMAGE_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(crate::ptr::null_mut()); |
| 10 | +// Flag to check if BootServices are still valid. |
| 11 | +// Start with assuming that they are not available |
| 12 | +static BOOT_SERVICES_FLAG: AtomicBool = AtomicBool::new(false); |
| 13 | + |
| 14 | +/// Initializes the global System Table and Image Handle pointers. |
| 15 | +/// |
| 16 | +/// The standard library requires access to the UEFI System Table and the Application Image Handle |
| 17 | +/// to operate. Those are provided to UEFI Applications via their application entry point. By |
| 18 | +/// calling `init_globals()`, those pointers are retained by the standard library for future use. |
| 19 | +/// Thus this function must be called before any of the standard library services are used. |
| 20 | +/// |
| 21 | +/// The pointers are never exposed to any entity outside of this application and it is guaranteed |
| 22 | +/// that, once the application exited, these pointers are never dereferenced again. |
| 23 | +/// |
| 24 | +/// Callers are required to ensure the pointers are valid for the entire lifetime of this |
| 25 | +/// application. In particular, UEFI Boot Services must not be exited while an application with the |
| 26 | +/// standard library is loaded. |
| 27 | +/// |
| 28 | +/// # SAFETY |
| 29 | +/// Calling this function more than once will panic |
| 30 | +pub(crate) unsafe fn init_globals(handle: NonNull<c_void>, system_table: NonNull<c_void>) { |
| 31 | + IMAGE_HANDLE |
| 32 | + .compare_exchange( |
| 33 | + crate::ptr::null_mut(), |
| 34 | + handle.as_ptr(), |
| 35 | + Ordering::Release, |
| 36 | + Ordering::Acquire, |
| 37 | + ) |
| 38 | + .unwrap(); |
| 39 | + SYSTEM_TABLE |
| 40 | + .compare_exchange( |
| 41 | + crate::ptr::null_mut(), |
| 42 | + system_table.as_ptr(), |
| 43 | + Ordering::Release, |
| 44 | + Ordering::Acquire, |
| 45 | + ) |
| 46 | + .unwrap(); |
| 47 | + BOOT_SERVICES_FLAG.store(true, Ordering::Release) |
| 48 | +} |
| 49 | + |
| 50 | +/// Get the SystemTable Pointer. |
| 51 | +/// If you want to use `BootServices` then please use [`boot_services`] as it performs some |
| 52 | +/// additional checks. |
| 53 | +/// |
| 54 | +/// Note: This function panics if the System Table or Image Handle is not initialized |
| 55 | +pub fn system_table() -> NonNull<c_void> { |
| 56 | + try_system_table().unwrap() |
| 57 | +} |
| 58 | + |
| 59 | +/// Get the ImageHandle Pointer. |
| 60 | +/// |
| 61 | +/// Note: This function panics if the System Table or Image Handle is not initialized |
| 62 | +pub fn image_handle() -> NonNull<c_void> { |
| 63 | + try_image_handle().unwrap() |
| 64 | +} |
| 65 | + |
| 66 | +/// Get the BootServices Pointer. |
| 67 | +/// This function also checks if `ExitBootServices` has already been called. |
| 68 | +pub fn boot_services() -> Option<NonNull<c_void>> { |
| 69 | + if BOOT_SERVICES_FLAG.load(Ordering::Acquire) { |
| 70 | + let system_table: NonNull<r_efi::efi::SystemTable> = try_system_table()?.cast(); |
| 71 | + let boot_services = unsafe { (*system_table.as_ptr()).boot_services }; |
| 72 | + NonNull::new(boot_services).map(|x| x.cast()) |
| 73 | + } else { |
| 74 | + None |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/// Get the SystemTable Pointer. |
| 79 | +/// This function is mostly intended for places where panic is not an option |
| 80 | +pub(crate) fn try_system_table() -> Option<NonNull<c_void>> { |
| 81 | + NonNull::new(SYSTEM_TABLE.load(Ordering::Acquire)) |
| 82 | +} |
| 83 | + |
| 84 | +/// Get the SystemHandle Pointer. |
| 85 | +/// This function is mostly intended for places where panicking is not an option |
| 86 | +pub(crate) fn try_image_handle() -> Option<NonNull<c_void>> { |
| 87 | + NonNull::new(IMAGE_HANDLE.load(Ordering::Acquire)) |
| 88 | +} |
| 89 | + |
| 90 | +pub(crate) fn disable_boot_services() { |
| 91 | + BOOT_SERVICES_FLAG.store(false, Ordering::Release) |
| 92 | +} |
0 commit comments