@@ -56,6 +56,11 @@ static mut SYSTEM_TABLE: Option<SystemTable<Boot>> = None;
56
56
#[ cfg( feature = "logger" ) ]
57
57
static mut LOGGER : Option < uefi:: logger:: Logger > = None ;
58
58
59
+ #[ must_use]
60
+ fn system_table_opt ( ) -> Option < SystemTable < Boot > > {
61
+ unsafe { SYSTEM_TABLE . as_ref ( ) . map ( |table| table. unsafe_clone ( ) ) }
62
+ }
63
+
59
64
/// Obtains a pointer to the system table.
60
65
///
61
66
/// This is meant to be used by higher-level libraries,
@@ -66,25 +71,20 @@ static mut LOGGER: Option<uefi::logger::Logger> = None;
66
71
/// The returned pointer is only valid until boot services are exited.
67
72
#[ must_use]
68
73
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" )
75
75
}
76
76
77
77
/// Initialize the UEFI utility library.
78
78
///
79
79
/// This must be called as early as possible,
80
80
/// before trying to use logging or memory allocation capabilities.
81
81
pub fn init ( st : & mut SystemTable < Boot > ) -> Result < Option < Event > > {
82
- unsafe {
82
+ if system_table_opt ( ) . is_some ( ) {
83
83
// 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
+ }
87
86
87
+ unsafe {
88
88
// Setup the system table singleton
89
89
SYSTEM_TABLE = Some ( st. unsafe_clone ( ) ) ;
90
90
@@ -111,15 +111,10 @@ pub fn init(st: &mut SystemTable<Boot>) -> Result<Option<Event>> {
111
111
// Internal function for print macros.
112
112
#[ doc( hidden) ]
113
113
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" ) ;
123
118
}
124
119
125
120
/// Prints to the standard output.
@@ -201,7 +196,7 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
201
196
println ! ( "[PANIC]: {}" , info) ;
202
197
203
198
// 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 ( ) {
205
200
st. boot_services ( ) . stall ( 10_000_000 ) ;
206
201
} else {
207
202
let mut dummy = 0u64 ;
@@ -222,7 +217,7 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
222
217
qemu_exit_handle. exit_failure( ) ;
223
218
} else {
224
219
// 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 ( ) {
226
221
use uefi:: table:: runtime:: ResetType ;
227
222
st. runtime_services( )
228
223
. reset( ResetType :: SHUTDOWN , uefi:: Status :: ABORTED , None ) ;
0 commit comments