1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::{
data_types::{CStr16, Char16},
proto::Protocol,
table::boot::MemoryType,
unsafe_guid, Handle, Status,
};
use core::ffi::c_void;
#[repr(C)]
#[unsafe_guid("5b1b31a1-9562-11d2-8e3f-00a0c969723b")]
#[derive(Protocol)]
pub struct LoadedImage {
revision: u32,
parent_handle: Handle,
system_table: *const c_void,
device_handle: Handle,
_file_path: *const c_void,
_reserved: *const c_void,
load_options_size: u32,
load_options: *const Char16,
image_base: usize,
image_size: u64,
image_code_type: MemoryType,
image_data_type: MemoryType,
unload: extern "efiapi" fn(image_handle: Handle) -> Status,
}
#[derive(Debug)]
pub enum LoadOptionsError {
BufferTooSmall,
NotValidUtf8,
}
impl LoadedImage {
pub fn load_options<'a>(&self, buffer: &'a mut [u8]) -> Result<&'a str, LoadOptionsError> {
let ucs2_slice = unsafe { CStr16::from_ptr(self.load_options).to_u16_slice() };
let length =
ucs2::decode(ucs2_slice, buffer).map_err(|_| LoadOptionsError::BufferTooSmall)?;
core::str::from_utf8(&buffer[0..length]).map_err(|_| LoadOptionsError::NotValidUtf8)
}
}