Skip to content

Commit 59b9d0b

Browse files
Move the global image handle to uefi::boot
The BootServices methods just pass through to the uefi::boot version now.
1 parent 479868f commit 59b9d0b

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

uefi/src/boot.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,46 @@
33
//! These functions will panic if called after exiting boot services.
44
55
use crate::data_types::PhysicalAddress;
6+
use core::ffi::c_void;
67
use core::ptr::{self, NonNull};
7-
use uefi::{table, Result, StatusExt};
8+
use core::sync::atomic::{AtomicPtr, Ordering};
9+
use uefi::{table, Handle, Result, StatusExt};
810

911
#[cfg(doc)]
1012
use uefi::Status;
1113

1214
pub use uefi::table::boot::AllocateType;
1315
pub use uefi_raw::table::boot::MemoryType;
1416

17+
/// Global image handle. This is only set by [`set_image_handle`], and it is
18+
/// only read by [`image_handle`].
19+
static IMAGE_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
20+
21+
/// Get the [`Handle`] of the currently-executing image.
22+
#[must_use]
23+
pub fn image_handle() -> Handle {
24+
let ptr = IMAGE_HANDLE.load(Ordering::Acquire);
25+
// Safety: the image handle must be valid. We know it is, because it was set
26+
// by `set_image_handle`, which has that same safety requirement.
27+
unsafe { Handle::from_ptr(ptr) }.expect("set_image_handle has not been called")
28+
}
29+
30+
/// Update the global image [`Handle`].
31+
///
32+
/// This is called automatically in the `main` entry point as part of
33+
/// [`uefi::entry`]. It should not be called at any other point in time, unless
34+
/// the executable does not use [`uefi::entry`], in which case it should be
35+
/// called once before calling other boot services functions.
36+
///
37+
/// # Safety
38+
///
39+
/// This function should only be called as described above, and the
40+
/// `image_handle` must be a valid image [`Handle`]. The safety guarantees of
41+
/// `open_protocol_exclusive` rely on the global image handle being correct.
42+
pub unsafe fn set_image_handle(image_handle: Handle) {
43+
IMAGE_HANDLE.store(image_handle.as_ptr(), Ordering::Release);
44+
}
45+
1546
fn boot_services_raw_panicking() -> NonNull<uefi_raw::table::boot::BootServices> {
1647
let st = table::system_table_raw_panicking();
1748
// SAFETY: valid per requirements of `set_system_table`.

uefi/src/table/boot.rs

+6-22
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@ use core::fmt::Debug;
1919
use core::mem::{self, MaybeUninit};
2020
use core::ops::{Deref, DerefMut};
2121
use core::ptr::NonNull;
22-
use core::sync::atomic::{AtomicPtr, Ordering};
2322
use core::{ptr, slice};
2423

25-
/// Global image handle. This is only set by `BootServices::set_image_handle`,
26-
/// and it is only read by `BootServices::image_handle`.
27-
static IMAGE_HANDLE: AtomicPtr<c_void> = AtomicPtr::new(ptr::null_mut());
28-
2924
/// Size in bytes of a UEFI page.
3025
///
3126
/// Note that this is not necessarily the processor's page size. The UEFI page
@@ -84,32 +79,21 @@ pub struct BootServices(uefi_raw::table::boot::BootServices);
8479

8580
impl BootServices {
8681
/// Get the [`Handle`] of the currently-executing image.
82+
#[must_use]
8783
pub fn image_handle(&self) -> Handle {
88-
let ptr = IMAGE_HANDLE.load(Ordering::Acquire);
89-
// Safety: the image handle must be valid. We know it is, because it was
90-
// set by `set_image_handle`, which has that same safety requirement.
91-
unsafe { Handle::from_ptr(ptr) }.expect("set_image_handle has not been called")
84+
uefi::boot::image_handle()
9285
}
9386

9487
/// Update the global image [`Handle`].
9588
///
96-
/// This is called automatically in the `main` entry point as part
97-
/// of [`uefi::entry`]. It should not be called at any other
98-
/// point in time, unless the executable does not use
99-
/// [`uefi::entry`], in which case it should be called once
100-
/// before calling other `BootServices` functions.
89+
/// This is the same as calling [`uefi::boot::set_image_handle`]. See that
90+
/// function for details.
10191
///
10292
/// # Safety
10393
///
104-
/// This function should only be called as described above,
105-
/// and the `image_handle` must be a valid image [`Handle`]. Then
106-
/// safety guarantees of [`BootServices::open_protocol_exclusive`]
107-
/// rely on the global image handle being correct.
94+
/// See [`uefi::boot::set_image_handle`] for safety requirements.
10895
pub unsafe fn set_image_handle(&self, image_handle: Handle) {
109-
// As with `image_handle`, `&self` isn't actually used, but it
110-
// enforces that this function is only called while boot
111-
// services are active.
112-
IMAGE_HANDLE.store(image_handle.as_ptr(), Ordering::Release);
96+
uefi::boot::set_image_handle(image_handle)
11397
}
11498

11599
/// Raises a task's priority level and returns its previous level.

0 commit comments

Comments
 (0)