Skip to content

Commit 06d9715

Browse files
committed
uefi: Add LoadFile and LoadFile2
1 parent d4b1d1b commit 06d9715

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

uefi/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
the implementations `MemoryMapRef`, `MemoryMapRefMut`, and `MemoryMapOwned`.
1616
This comes with some changes. Read below. We recommend to directly use the
1717
implementations instead of the traits.
18+
- Added `LoadFile` and `LoadFile2` which abstracts over the `LOAD_FILE` and
19+
`LOAD_FILE2` protocols.
1820

1921
## Changed
2022
- **Breaking:** `uefi::helpers::init` no longer takes an argument.

uefi/src/proto/media/load_file.rs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//! LoadFile and LoadFile2 protocols.
2+
3+
use crate::proto::device_path::DevicePath;
4+
use crate::proto::unsafe_protocol;
5+
use crate::StatusExt;
6+
#[cfg(all(feature = "alloc", feature = "unstable"))]
7+
use alloc::alloc::Global;
8+
use uefi::Result;
9+
use uefi_raw::protocol::media::{LoadFile2Protocol, LoadFileProtocol};
10+
#[cfg(feature = "alloc")]
11+
use {crate::mem::make_boxed, alloc::boxed::Box};
12+
13+
/// TODO.
14+
#[derive(Debug)]
15+
#[repr(transparent)]
16+
#[unsafe_protocol(LoadFileProtocol::GUID)]
17+
pub struct LoadFile(LoadFileProtocol);
18+
19+
impl LoadFile {
20+
/// TODO.
21+
#[cfg(feature = "alloc")]
22+
pub fn load_file<'a>(
23+
&mut self,
24+
file_path: &DevicePath,
25+
boot_policy: bool,
26+
) -> Result<Box<[u8]>> {
27+
log::debug!("hello");
28+
let this = core::ptr::addr_of_mut!(*self).cast();
29+
30+
let fetch_data_fn = |buf: &'a mut [u8]| {
31+
log::debug!("hello");
32+
let mut size = buf.len();
33+
let status = unsafe {
34+
(self.0.load_file)(
35+
this,
36+
file_path.as_ffi_ptr().cast(),
37+
boot_policy,
38+
&mut size,
39+
buf.as_mut_ptr().cast(),
40+
)
41+
};
42+
status.to_result_with_err(|_| Some(size)).map(|_| buf)
43+
};
44+
45+
#[cfg(not(feature = "unstable"))]
46+
let file: Box<[u8]> = make_boxed::<[u8], _>(fetch_data_fn)?;
47+
48+
#[cfg(feature = "unstable")]
49+
let file = make_boxed::<[u8], _, _>(fetch_data_fn, Global)?;
50+
51+
Ok(file)
52+
}
53+
}
54+
55+
/// TODO.
56+
#[derive(Debug)]
57+
#[repr(transparent)]
58+
#[unsafe_protocol(LoadFile2Protocol::GUID)]
59+
pub struct LoadFile2(LoadFile2Protocol);
60+
61+
impl LoadFile2 {
62+
/// TODO.
63+
pub fn load_file<'a>(&mut self, file_path: &DevicePath) -> Result<Box<[u8]>> {
64+
let this = core::ptr::addr_of_mut!(*self).cast();
65+
66+
let fetch_data_fn = |buf: &'a mut [u8]| {
67+
log::debug!("hello");
68+
let mut size = buf.len();
69+
let status = unsafe {
70+
(self.0.load_file)(
71+
this,
72+
file_path.as_ffi_ptr().cast(),
73+
false, /* always false - see spec */
74+
&mut size,
75+
buf.as_mut_ptr().cast(),
76+
)
77+
};
78+
log::debug!("hello");
79+
status.to_result_with_err(|_| Some(size)).map(|_| buf)
80+
};
81+
82+
#[cfg(not(feature = "unstable"))]
83+
let file: Box<[u8]> = make_boxed::<[u8], _>(fetch_data_fn)?;
84+
85+
#[cfg(feature = "unstable")]
86+
let file = make_boxed::<[u8], _, _>(fetch_data_fn, Global)?;
87+
88+
Ok(file)
89+
}
90+
}

uefi/src/proto/media/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ pub mod file;
99
pub mod block;
1010
pub mod disk;
1111
pub mod fs;
12+
pub mod load_file;
1213
pub mod partition;

0 commit comments

Comments
 (0)