1
1
//! `LoadedImage` protocol.
2
2
3
3
use crate :: data_types:: FromSliceWithNulError ;
4
- use crate :: proto:: device_path:: { DevicePath , FfiDevicePath } ;
4
+ use crate :: proto:: device_path:: DevicePath ;
5
5
use crate :: proto:: unsafe_protocol;
6
6
use crate :: table:: boot:: MemoryType ;
7
7
use crate :: util:: usize_from_u32;
8
8
use crate :: { CStr16 , Handle , Status } ;
9
9
use core:: ffi:: c_void;
10
10
use core:: { mem, slice} ;
11
+ use uefi_raw:: protocol:: loaded_image:: LoadedImageProtocol ;
11
12
12
13
/// The LoadedImage protocol. This can be opened on any image handle using the `HandleProtocol` boot service.
13
- #[ repr( C ) ]
14
- #[ unsafe_protocol( "5b1b31a1-9562-11d2-8e3f-00a0c969723b" ) ]
15
- pub struct LoadedImage {
16
- revision : u32 ,
17
- parent_handle : Handle ,
18
- system_table : * const c_void ,
19
-
20
- // Source location of the image
21
- device_handle : Option < Handle > ,
22
- file_path : * const FfiDevicePath ,
23
- _reserved : * const c_void ,
24
-
25
- // Image load options
26
- load_options_size : u32 ,
27
- load_options : * const u8 ,
28
-
29
- // Location where image was loaded
30
- image_base : * const c_void ,
31
- image_size : u64 ,
32
- image_code_type : MemoryType ,
33
- image_data_type : MemoryType ,
34
- /// This is a callback that a loaded image can use to do cleanup. It is called by the
35
- /// `UnloadImage` boot service.
36
- unload : extern "efiapi" fn ( image_handle : Handle ) -> Status ,
37
- }
14
+ #[ repr( transparent) ]
15
+ #[ unsafe_protocol( LoadedImageProtocol :: GUID ) ]
16
+ pub struct LoadedImage ( LoadedImageProtocol ) ;
38
17
39
18
/// Errors that can be raised during parsing of the load options.
40
19
#[ derive( Debug ) ]
@@ -52,8 +31,8 @@ pub enum LoadOptionsError {
52
31
impl LoadedImage {
53
32
/// Returns a handle to the storage device on which the image is located.
54
33
#[ must_use]
55
- pub const fn device ( & self ) -> Option < Handle > {
56
- self . device_handle
34
+ pub fn device ( & self ) -> Option < Handle > {
35
+ unsafe { Handle :: from_ptr ( self . 0 . device_handle ) }
57
36
}
58
37
59
38
/// Get a reference to the `file_path` portion of the DeviceHandle that the
@@ -67,10 +46,10 @@ impl LoadedImage {
67
46
/// [`LoadedImageDevicePath`]: crate::proto::device_path::LoadedImageDevicePath
68
47
#[ must_use]
69
48
pub fn file_path ( & self ) -> Option < & DevicePath > {
70
- if self . file_path . is_null ( ) {
49
+ if self . 0 . file_path . is_null ( ) {
71
50
None
72
51
} else {
73
- unsafe { Some ( DevicePath :: from_ffi_ptr ( self . file_path ) ) }
52
+ unsafe { Some ( DevicePath :: from_ffi_ptr ( self . 0 . file_path . cast ( ) ) ) }
74
53
}
75
54
}
76
55
@@ -83,18 +62,18 @@ impl LoadedImage {
83
62
/// [`&CStr16`]: `CStr16`
84
63
/// [`load_options_as_bytes`]: `Self::load_options_as_bytes`
85
64
pub fn load_options_as_cstr16 ( & self ) -> Result < & CStr16 , LoadOptionsError > {
86
- let load_options_size = usize_from_u32 ( self . load_options_size ) ;
65
+ let load_options_size = usize_from_u32 ( self . 0 . load_options_size ) ;
87
66
88
- if self . load_options . is_null ( ) {
67
+ if self . 0 . load_options . is_null ( ) {
89
68
Err ( LoadOptionsError :: NotSet )
90
69
} else if ( load_options_size % mem:: size_of :: < u16 > ( ) != 0 )
91
- || ( ( ( self . load_options as usize ) % mem:: align_of :: < u16 > ( ) ) != 0 )
70
+ || ( ( ( self . 0 . load_options as usize ) % mem:: align_of :: < u16 > ( ) ) != 0 )
92
71
{
93
72
Err ( LoadOptionsError :: NotAligned )
94
73
} else {
95
74
let s = unsafe {
96
75
slice:: from_raw_parts (
97
- self . load_options . cast :: < u16 > ( ) ,
76
+ self . 0 . load_options . cast :: < u16 > ( ) ,
98
77
load_options_size / mem:: size_of :: < u16 > ( ) ,
99
78
)
100
79
} ;
@@ -114,13 +93,13 @@ impl LoadedImage {
114
93
/// [`load_options_as_cstr16`]: `Self::load_options_as_cstr16`
115
94
#[ must_use]
116
95
pub fn load_options_as_bytes ( & self ) -> Option < & [ u8 ] > {
117
- if self . load_options . is_null ( ) {
96
+ if self . 0 . load_options . is_null ( ) {
118
97
None
119
98
} else {
120
99
unsafe {
121
100
Some ( slice:: from_raw_parts (
122
- self . load_options ,
123
- usize_from_u32 ( self . load_options_size ) ,
101
+ self . 0 . load_options . cast ( ) ,
102
+ usize_from_u32 ( self . 0 . load_options_size ) ,
124
103
) )
125
104
}
126
105
}
@@ -150,8 +129,8 @@ impl LoadedImage {
150
129
///
151
130
/// [shim]: https://github.com/rhboot/shim/blob/4d64389c6c941d21548b06423b8131c872e3c3c7/pe.c#L1143
152
131
pub unsafe fn set_image ( & mut self , data : * const c_void , size : u64 ) {
153
- self . image_base = data;
154
- self . image_size = size;
132
+ self . 0 . image_base = data;
133
+ self . 0 . image_size = size;
155
134
}
156
135
157
136
/// Set the callback handler to unload the image.
@@ -171,7 +150,9 @@ impl LoadedImage {
171
150
& mut self ,
172
151
unload : extern "efiapi" fn ( image_handle : Handle ) -> Status ,
173
152
) {
174
- self . unload = unload;
153
+ type RawFn = unsafe extern "efiapi" fn ( image_handle : uefi_raw:: Handle ) -> uefi_raw:: Status ;
154
+ let unload: RawFn = mem:: transmute ( unload) ;
155
+ self . 0 . unload = Some ( unload) ;
175
156
}
176
157
177
158
/// Set the load options for the image. This can be used prior to
@@ -186,14 +167,14 @@ impl LoadedImage {
186
167
/// load options data is not owned by `LoadedImage`. The caller
187
168
/// must ensure that the memory lives long enough.
188
169
pub unsafe fn set_load_options ( & mut self , options : * const u8 , size : u32 ) {
189
- self . load_options = options;
190
- self . load_options_size = size;
170
+ self . 0 . load_options = options. cast ( ) ;
171
+ self . 0 . load_options_size = size;
191
172
}
192
173
193
174
/// Returns the base address and the size in bytes of the loaded image.
194
175
#[ must_use]
195
176
pub const fn info ( & self ) -> ( * const c_void , u64 ) {
196
- ( self . image_base , self . image_size )
177
+ ( self . 0 . image_base , self . 0 . image_size )
197
178
}
198
179
199
180
/// Get the memory type of the image's code sections.
@@ -204,7 +185,7 @@ impl LoadedImage {
204
185
/// - `MemoryType::RUNTIME_SERVICES_CODE` for UEFI runtime drivers
205
186
#[ must_use]
206
187
pub fn code_type ( & self ) -> MemoryType {
207
- self . image_code_type
188
+ self . 0 . image_code_type
208
189
}
209
190
210
191
/// Get the memory type of the image's data sections.
@@ -215,6 +196,6 @@ impl LoadedImage {
215
196
/// - `MemoryType::RUNTIME_SERVICES_DATA` for UEFI runtime drivers
216
197
#[ must_use]
217
198
pub fn data_type ( & self ) -> MemoryType {
218
- self . image_data_type
199
+ self . 0 . image_data_type
219
200
}
220
201
}
0 commit comments