Skip to content

Commit e8853d2

Browse files
committed
uefi: less panics in system_table_boot/system_table_runtime
This gives callers a chance to react, whether boot services are available or not.
1 parent ecbb338 commit e8853d2

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

uefi/src/table/mod.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,42 +37,40 @@ pub unsafe fn set_system_table(ptr: *const uefi_raw::table::system::SystemTable)
3737
///
3838
/// # Panics
3939
///
40-
/// Panics if the system table has not been set with `set_system_table`, or if
41-
/// boot services are not available (e.g. if [`exit_boot_services`] has been
42-
/// called).
40+
/// Panics if the system table has not been set with [`set_system_table`].
4341
///
4442
/// [`exit_boot_services`]: SystemTable::exit_boot_services
45-
pub fn system_table_boot() -> SystemTable<Boot> {
43+
pub fn system_table_boot() -> Option<SystemTable<Boot>> {
4644
let st = SYSTEM_TABLE.load(Ordering::Acquire);
4745
assert!(!st.is_null());
4846

4947
// SAFETY: the system table is valid per the requirements of `set_system_table`.
5048
unsafe {
5149
if (*st).boot_services.is_null() {
52-
panic!("boot services are not active");
50+
None
51+
} else {
52+
Some(SystemTable::<Boot>::from_ptr(st.cast()).unwrap())
5353
}
54-
55-
SystemTable::<Boot>::from_ptr(st.cast()).unwrap()
5654
}
5755
}
5856

59-
/// Get the system table while runtime services are active.
57+
/// Get the system table while runtime services are active. This only returns
58+
/// `Some` after boot services have been exited.
6059
///
6160
/// # Panics
6261
///
63-
/// Panics if the system table has not been set with `set_system_table`, or if
64-
/// runtime services are not available.
65-
pub fn system_table_runtime() -> SystemTable<Runtime> {
62+
/// Panics if the system table has not been set with [`set_system_table`].
63+
pub fn system_table_runtime() -> Option<SystemTable<Runtime>> {
6664
let st = SYSTEM_TABLE.load(Ordering::Acquire);
6765
assert!(!st.is_null());
6866

6967
// SAFETY: the system table is valid per the requirements of `set_system_table`.
7068
unsafe {
7169
if (*st).runtime_services.is_null() {
72-
panic!("runtime services are not active");
70+
None
71+
} else {
72+
Some(SystemTable::<Runtime>::from_ptr(st.cast()).unwrap())
7373
}
74-
75-
SystemTable::<Runtime>::from_ptr(st.cast()).unwrap()
7674
}
7775
}
7876

0 commit comments

Comments
 (0)