1
+ use alloc:: string:: ToString ;
1
2
use uefi:: proto:: console:: text:: Output ;
2
- use uefi:: table:: boot:: { BootServices , SearchType } ;
3
- use uefi:: Identify ;
3
+ use uefi:: proto:: device_path:: text:: { AllowShortcuts , DevicePathToText , DisplayOnly } ;
4
+ use uefi:: proto:: loaded_image:: LoadedImage ;
5
+ use uefi:: table:: boot:: { BootServices , LoadImageSource , SearchType } ;
6
+ use uefi:: { CString16 , Identify } ;
7
+
8
+ mod memory;
9
+ mod misc;
4
10
5
11
pub fn test ( bt : & BootServices ) {
6
12
info ! ( "Testing boot services" ) ;
7
13
memory:: test ( bt) ;
8
14
misc:: test ( bt) ;
9
15
test_locate_handle_buffer ( bt) ;
16
+ test_load_image ( bt) ;
10
17
}
11
18
12
- mod memory;
13
- mod misc;
14
-
15
19
fn test_locate_handle_buffer ( bt : & BootServices ) {
16
20
info ! ( "Testing the `locate_handle_buffer` function" ) ;
17
21
@@ -34,3 +38,53 @@ fn test_locate_handle_buffer(bt: &BootServices) {
34
38
) ;
35
39
}
36
40
}
41
+
42
+ /// This test loads the "self image" again into memory using the `load_image`
43
+ /// boot service function. The image is not started but just loaded into memory.
44
+ ///
45
+ /// It transitively tests the protocols
46
+ /// * `DevicePathToText`
47
+ /// * `LoadedImage`
48
+ /// which are required as helpers.
49
+ fn test_load_image ( bt : & BootServices ) {
50
+ info ! ( "Testing the `load_image` function" ) ;
51
+
52
+ let loaded_image = bt
53
+ . open_protocol_exclusive :: < LoadedImage > ( bt. image_handle ( ) )
54
+ . expect ( "should open LoadedImage protocol" ) ;
55
+
56
+ let image_device_path = loaded_image. file_path ( ) . expect ( "should have file path" ) ;
57
+
58
+ let & device_path_to_text_handle = bt
59
+ . locate_handle_buffer ( SearchType :: ByProtocol ( & DevicePathToText :: GUID ) )
60
+ . expect ( "Should be able to loace handle for DevicePathToText" )
61
+ . first ( )
62
+ . expect ( "should locate DevicePathToText protocol handle" ) ;
63
+
64
+ let device_path_to_text = bt
65
+ . open_protocol_exclusive :: < DevicePathToText > ( device_path_to_text_handle)
66
+ . expect ( "should open DevicePathToText protocol" ) ;
67
+
68
+ let device_path_text = device_path_to_text
69
+ . convert_device_path_to_text (
70
+ bt,
71
+ image_device_path,
72
+ DisplayOnly ( true ) ,
73
+ AllowShortcuts ( false ) ,
74
+ )
75
+ . expect ( "should convert to human-readable path" ) ;
76
+ let device_path_text = CString16 :: from ( & * device_path_text) ;
77
+ let device_path_text = device_path_text. to_string ( ) . to_uppercase ( ) ;
78
+
79
+ // On x86_64, this will be `\EFI\BOOT\BOOTX64.EFI` for example.
80
+ assert ! ( device_path_text. as_str( ) . starts_with( r"\EFI\BOOT\BOOT" ) ) ;
81
+ assert ! ( device_path_text. as_str( ) . ends_with( ".EFI" ) ) ;
82
+
83
+ let load_source = LoadImageSource :: FromFilePath {
84
+ file_path : image_device_path,
85
+ from_boot_manager : false ,
86
+ } ;
87
+ let _ = bt
88
+ . load_image ( bt. image_handle ( ) , load_source)
89
+ . expect ( "should load image" ) ;
90
+ }
0 commit comments