Skip to content

Commit baa7b48

Browse files
uefi: Use uefi_raw's LoadedImageProtocol to implement LoadedImage
1 parent 2fdff0f commit baa7b48

File tree

2 files changed

+27
-46
lines changed

2 files changed

+27
-46
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Changed
66
- `Input::wait_for_key_event` now returns an `Option<Event>`, and is no longer `const`.
7-
- `LoadedImage::device` now returns an `Option<Handle>`.
7+
- `LoadedImage::device` now returns an `Option<Handle>` and is no longer `const`.
88

99
## uefi-macros - [Unreleased]
1010

uefi/src/proto/loaded_image.rs

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
11
//! `LoadedImage` protocol.
22
33
use crate::data_types::FromSliceWithNulError;
4-
use crate::proto::device_path::{DevicePath, FfiDevicePath};
4+
use crate::proto::device_path::DevicePath;
55
use crate::proto::unsafe_protocol;
66
use crate::table::boot::MemoryType;
77
use crate::util::usize_from_u32;
88
use crate::{CStr16, Handle, Status};
99
use core::ffi::c_void;
1010
use core::{mem, slice};
11+
use uefi_raw::protocol::loaded_image::LoadedImageProtocol;
1112

1213
/// 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);
3817

3918
/// Errors that can be raised during parsing of the load options.
4019
#[derive(Debug)]
@@ -52,8 +31,8 @@ pub enum LoadOptionsError {
5231
impl LoadedImage {
5332
/// Returns a handle to the storage device on which the image is located.
5433
#[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) }
5736
}
5837

5938
/// Get a reference to the `file_path` portion of the DeviceHandle that the
@@ -67,10 +46,10 @@ impl LoadedImage {
6746
/// [`LoadedImageDevicePath`]: crate::proto::device_path::LoadedImageDevicePath
6847
#[must_use]
6948
pub fn file_path(&self) -> Option<&DevicePath> {
70-
if self.file_path.is_null() {
49+
if self.0.file_path.is_null() {
7150
None
7251
} else {
73-
unsafe { Some(DevicePath::from_ffi_ptr(self.file_path)) }
52+
unsafe { Some(DevicePath::from_ffi_ptr(self.0.file_path.cast())) }
7453
}
7554
}
7655

@@ -83,18 +62,18 @@ impl LoadedImage {
8362
/// [`&CStr16`]: `CStr16`
8463
/// [`load_options_as_bytes`]: `Self::load_options_as_bytes`
8564
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);
8766

88-
if self.load_options.is_null() {
67+
if self.0.load_options.is_null() {
8968
Err(LoadOptionsError::NotSet)
9069
} 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)
9271
{
9372
Err(LoadOptionsError::NotAligned)
9473
} else {
9574
let s = unsafe {
9675
slice::from_raw_parts(
97-
self.load_options.cast::<u16>(),
76+
self.0.load_options.cast::<u16>(),
9877
load_options_size / mem::size_of::<u16>(),
9978
)
10079
};
@@ -114,13 +93,13 @@ impl LoadedImage {
11493
/// [`load_options_as_cstr16`]: `Self::load_options_as_cstr16`
11594
#[must_use]
11695
pub fn load_options_as_bytes(&self) -> Option<&[u8]> {
117-
if self.load_options.is_null() {
96+
if self.0.load_options.is_null() {
11897
None
11998
} else {
12099
unsafe {
121100
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),
124103
))
125104
}
126105
}
@@ -150,8 +129,8 @@ impl LoadedImage {
150129
///
151130
/// [shim]: https://github.com/rhboot/shim/blob/4d64389c6c941d21548b06423b8131c872e3c3c7/pe.c#L1143
152131
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;
155134
}
156135

157136
/// Set the callback handler to unload the image.
@@ -171,7 +150,9 @@ impl LoadedImage {
171150
&mut self,
172151
unload: extern "efiapi" fn(image_handle: Handle) -> Status,
173152
) {
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);
175156
}
176157

177158
/// Set the load options for the image. This can be used prior to
@@ -186,14 +167,14 @@ impl LoadedImage {
186167
/// load options data is not owned by `LoadedImage`. The caller
187168
/// must ensure that the memory lives long enough.
188169
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;
191172
}
192173

193174
/// Returns the base address and the size in bytes of the loaded image.
194175
#[must_use]
195176
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)
197178
}
198179

199180
/// Get the memory type of the image's code sections.
@@ -204,7 +185,7 @@ impl LoadedImage {
204185
/// - `MemoryType::RUNTIME_SERVICES_CODE` for UEFI runtime drivers
205186
#[must_use]
206187
pub fn code_type(&self) -> MemoryType {
207-
self.image_code_type
188+
self.0.image_code_type
208189
}
209190

210191
/// Get the memory type of the image's data sections.
@@ -215,6 +196,6 @@ impl LoadedImage {
215196
/// - `MemoryType::RUNTIME_SERVICES_DATA` for UEFI runtime drivers
216197
#[must_use]
217198
pub fn data_type(&self) -> MemoryType {
218-
self.image_data_type
199+
self.0.image_data_type
219200
}
220201
}

0 commit comments

Comments
 (0)