Skip to content

Commit 6cb90ce

Browse files
committed
uefi: Add LoadFile and LoadFile2
1 parent 7c31963 commit 6cb90ce

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-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

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

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)