|
| 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 | +} |
0 commit comments