Skip to content

Commit aba21d7

Browse files
uefi-services: Reduce direct SYSTEM_TABLE access
Add `system_table_opt` as the only direct read of `SYSTEM_TABLE`. Replace reads of the global with either `system_table_opt` or `system_table`.
1 parent f0fe055 commit aba21d7

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

uefi-services/src/lib.rs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ static mut SYSTEM_TABLE: Option<SystemTable<Boot>> = None;
5656
#[cfg(feature = "logger")]
5757
static mut LOGGER: Option<uefi::logger::Logger> = None;
5858

59+
#[must_use]
60+
fn system_table_opt() -> Option<SystemTable<Boot>> {
61+
unsafe { SYSTEM_TABLE.as_ref().map(|table| table.unsafe_clone()) }
62+
}
63+
5964
/// Obtains a pointer to the system table.
6065
///
6166
/// This is meant to be used by higher-level libraries,
@@ -66,25 +71,20 @@ static mut LOGGER: Option<uefi::logger::Logger> = None;
6671
/// The returned pointer is only valid until boot services are exited.
6772
#[must_use]
6873
pub fn system_table() -> SystemTable<Boot> {
69-
unsafe {
70-
let table_ref = SYSTEM_TABLE
71-
.as_ref()
72-
.expect("The system table handle is not available");
73-
table_ref.unsafe_clone()
74-
}
74+
system_table_opt().expect("The system table handle is not available")
7575
}
7676

7777
/// Initialize the UEFI utility library.
7878
///
7979
/// This must be called as early as possible,
8080
/// before trying to use logging or memory allocation capabilities.
8181
pub fn init(st: &mut SystemTable<Boot>) -> Result<Option<Event>> {
82-
unsafe {
82+
if system_table_opt().is_some() {
8383
// Avoid double initialization.
84-
if SYSTEM_TABLE.is_some() {
85-
return Status::SUCCESS.to_result_with_val(|| None);
86-
}
84+
return Status::SUCCESS.to_result_with_val(|| None);
85+
}
8786

87+
unsafe {
8888
// Setup the system table singleton
8989
SYSTEM_TABLE = Some(st.unsafe_clone());
9090

@@ -111,15 +111,10 @@ pub fn init(st: &mut SystemTable<Boot>) -> Result<Option<Event>> {
111111
// Internal function for print macros.
112112
#[doc(hidden)]
113113
pub fn _print(args: core::fmt::Arguments) {
114-
unsafe {
115-
let st = SYSTEM_TABLE
116-
.as_mut()
117-
.expect("The system table handle is not available");
118-
119-
st.stdout()
120-
.write_fmt(args)
121-
.expect("Failed to write to stdout");
122-
}
114+
system_table()
115+
.stdout()
116+
.write_fmt(args)
117+
.expect("Failed to write to stdout");
123118
}
124119

125120
/// Prints to the standard output.
@@ -201,7 +196,7 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
201196
println!("[PANIC]: {}", info);
202197

203198
// Give the user some time to read the message
204-
if let Some(st) = unsafe { SYSTEM_TABLE.as_ref() } {
199+
if let Some(st) = system_table_opt() {
205200
st.boot_services().stall(10_000_000);
206201
} else {
207202
let mut dummy = 0u64;
@@ -222,7 +217,7 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
222217
qemu_exit_handle.exit_failure();
223218
} else {
224219
// If the system table is available, use UEFI's standard shutdown mechanism
225-
if let Some(st) = unsafe { SYSTEM_TABLE.as_ref() } {
220+
if let Some(st) = system_table_opt() {
226221
use uefi::table::runtime::ResetType;
227222
st.runtime_services()
228223
.reset(ResetType::SHUTDOWN, uefi::Status::ABORTED, None);

0 commit comments

Comments
 (0)