@@ -8,11 +8,6 @@ use crate::os::uefi;
88use crate :: os:: uefi:: ffi:: { OsStrExt , OsStringExt } ;
99use crate :: ptr:: NonNull ;
1010
11- pub ( crate ) const BOOT_SERVICES_ERROR : io:: Error =
12- const_io_error ! ( io:: ErrorKind :: Other , "failed to acquire boot services" , ) ;
13- pub ( crate ) const RUNTIME_SERVICES_ERROR : io:: Error =
14- const_io_error ! ( io:: ErrorKind :: Other , "failed to acquire runtime services" , ) ;
15-
1611/// Get the Protocol for current system handle.
1712/// Note: Some protocols need to be manually freed. It is the callers responsibility to do so.
1813pub ( crate ) fn get_current_handle_protocol < T > ( protocol_guid : Guid ) -> Option < NonNull < T > > {
@@ -42,7 +37,7 @@ impl Event {
4237 notify_function : Option < EventNotify > ,
4338 notify_context : Option < NonNull < crate :: ffi:: c_void > > ,
4439 ) -> io:: Result < Self > {
45- let boot_services = get_boot_services ( ) . ok_or ( BOOT_SERVICES_ERROR ) ? ;
40+ let boot_services = boot_services ( ) ;
4641
4742 let mut event: r_efi:: efi:: Event = crate :: ptr:: null_mut ( ) ;
4843 let notify_context = match notify_context {
@@ -69,7 +64,7 @@ impl Event {
6964 }
7065
7166 pub ( crate ) fn wait ( & self ) -> io:: Result < ( ) > {
72- let boot_services = get_boot_services ( ) . ok_or ( BOOT_SERVICES_ERROR ) ? ;
67+ let boot_services = boot_services ( ) ;
7368
7469 let mut index = 0usize ;
7570 let r = unsafe {
@@ -91,10 +86,9 @@ impl Event {
9186
9287impl Drop for Event {
9388 fn drop ( & mut self ) {
94- if let Some ( boot_services) = get_boot_services ( ) {
95- // Always returns EFI_SUCCESS
96- let _ = unsafe { ( ( * boot_services. as_ptr ( ) ) . close_event ) ( self . inner . as_ptr ( ) ) } ;
97- }
89+ let boot_services = boot_services ( ) ;
90+ // Always returns EFI_SUCCESS
91+ let _ = unsafe { ( ( * boot_services. as_ptr ( ) ) . close_event ) ( self . inner . as_ptr ( ) ) } ;
9892 }
9993}
10094
@@ -203,7 +197,7 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result<Vec<NonNull<crate::ff
203197 if r. is_error ( ) { Err ( status_to_io_error ( r) ) } else { Ok ( ( ) ) }
204198 }
205199
206- let boot_services = get_boot_services ( ) . ok_or ( BOOT_SERVICES_ERROR ) ? ;
200+ let boot_services = boot_services ( ) ;
207201 let mut buf_len = 0usize ;
208202
209203 match inner ( & mut guid, boot_services, & mut buf_len, crate :: ptr:: null_mut ( ) ) {
@@ -235,7 +229,7 @@ pub(crate) fn open_protocol<T>(
235229 handle : NonNull < crate :: ffi:: c_void > ,
236230 mut protocol_guid : Guid ,
237231) -> io:: Result < NonNull < T > > {
238- let boot_services = get_boot_services ( ) . ok_or ( BOOT_SERVICES_ERROR ) ? ;
232+ let boot_services = boot_services ( ) ;
239233 let system_handle = uefi:: env:: image_handle ( ) ;
240234 let mut protocol: MaybeUninit < * mut T > = MaybeUninit :: uninit ( ) ;
241235
@@ -376,7 +370,7 @@ pub(crate) fn install_protocol<T>(
376370 mut guid : r_efi:: efi:: Guid ,
377371 interface : & mut T ,
378372) -> io:: Result < ( ) > {
379- let boot_services = get_boot_services ( ) . ok_or ( BOOT_SERVICES_ERROR ) ? ;
373+ let boot_services = boot_services ( ) ;
380374 let r = unsafe {
381375 ( ( * boot_services. as_ptr ( ) ) . install_protocol_interface ) (
382376 handle,
@@ -393,7 +387,7 @@ pub(crate) fn uninstall_protocol<T>(
393387 mut guid : r_efi:: efi:: Guid ,
394388 interface : & mut T ,
395389) -> io:: Result < ( ) > {
396- let boot_services = get_boot_services ( ) . ok_or ( BOOT_SERVICES_ERROR ) ? ;
390+ let boot_services = boot_services ( ) ;
397391 let r = unsafe {
398392 ( ( * boot_services. as_ptr ( ) ) . uninstall_protocol_interface ) (
399393 handle,
@@ -464,17 +458,24 @@ where
464458}
465459
466460/// Get the BootServices Pointer.
467- pub ( crate ) fn get_boot_services ( ) -> Option < NonNull < r_efi:: efi:: BootServices > > {
461+ pub ( crate ) fn boot_services ( ) -> NonNull < r_efi:: efi:: BootServices > {
468462 let system_table: NonNull < r_efi:: efi:: SystemTable > = uefi:: env:: system_table ( ) . cast ( ) ;
469463 let boot_services = unsafe { ( * system_table. as_ptr ( ) ) . boot_services } ;
464+ NonNull :: new ( boot_services) . unwrap ( )
465+ }
466+ /// Get the BootServices Pointer.
467+ /// This function is mostly intended for places where panic is not an option
468+ pub ( crate ) fn try_boot_services ( ) -> Option < NonNull < r_efi:: efi:: BootServices > > {
469+ let system_table: NonNull < r_efi:: efi:: SystemTable > = uefi:: env:: try_system_table ( ) ?. cast ( ) ;
470+ let boot_services = unsafe { ( * system_table. as_ptr ( ) ) . boot_services } ;
470471 NonNull :: new ( boot_services)
471472}
472473
473474/// Get the RuntimeServices Pointer.
474- pub ( crate ) fn get_runtime_services ( ) -> Option < NonNull < r_efi:: efi:: RuntimeServices > > {
475+ pub ( crate ) fn runtime_services ( ) -> NonNull < r_efi:: efi:: RuntimeServices > {
475476 let system_table: NonNull < r_efi:: efi:: SystemTable > = uefi:: env:: system_table ( ) . cast ( ) ;
476477 let runtime_services = unsafe { ( * system_table. as_ptr ( ) ) . runtime_services } ;
477- NonNull :: new ( runtime_services)
478+ NonNull :: new ( runtime_services) . unwrap ( )
478479}
479480
480481// Create UCS-2 Vector from OsStr
0 commit comments