|
| 1 | +//! Load file support protocols. |
| 2 | +
|
| 3 | +#[cfg(feature = "alloc")] |
| 4 | +use alloc::vec::Vec; |
| 5 | + |
| 6 | +use crate::proto::device_path::{FfiDevicePath, DevicePath}; |
| 7 | +use crate::proto::unsafe_protocol; |
| 8 | +use crate::{Result, Status}; |
| 9 | +use core::ffi::c_void; |
| 10 | +use core::ptr; |
| 11 | + |
| 12 | +/// The UEFI LoadFile2 protocol. |
| 13 | +/// |
| 14 | +/// This protocol has a single method to load a file according to some |
| 15 | +/// device path. |
| 16 | +/// |
| 17 | +/// This interface is implemented by many devices, e.g. network and filesystems. |
| 18 | +#[derive(Debug)] |
| 19 | +#[repr(C)] |
| 20 | +#[unsafe_protocol("4006c0c1-fcb3-403e-996d-4a6c8724e06d")] |
| 21 | +pub struct LoadFile2Protocol { |
| 22 | + load_file: unsafe extern "efiapi" fn( |
| 23 | + this: &mut LoadFile2Protocol, |
| 24 | + file_path: *const FfiDevicePath, |
| 25 | + boot_policy: bool, |
| 26 | + buffer_size: *mut usize, |
| 27 | + buffer: *mut c_void, |
| 28 | + ) -> Status, |
| 29 | +} |
| 30 | + |
| 31 | +impl LoadFile2Protocol { |
| 32 | + /// Load file addressed by provided device path |
| 33 | + pub fn load_file(&mut self, |
| 34 | + file_path: &DevicePath, |
| 35 | + boot_policy: bool, |
| 36 | + buffer_size: &mut usize, |
| 37 | + buffer: *mut c_void |
| 38 | + ) -> Status { |
| 39 | + unsafe { |
| 40 | + (self.load_file)(self, |
| 41 | + file_path.as_ffi_ptr(), |
| 42 | + boot_policy, |
| 43 | + buffer_size as *mut usize, |
| 44 | + buffer |
| 45 | + ) |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + #[cfg(feature = "alloc")] |
| 50 | + /// Load file addressed by the provided device path. |
| 51 | + pub fn load_file_in_heap(&mut self, |
| 52 | + file_path: &DevicePath, |
| 53 | + boot_policy: bool, |
| 54 | + ) -> Result<Vec<u8>> { |
| 55 | + let mut buffer_size: usize = 0; |
| 56 | + let mut status: Status; |
| 57 | + unsafe { |
| 58 | + status = (self.load_file)(self, |
| 59 | + file_path.as_ffi_ptr(), |
| 60 | + boot_policy, |
| 61 | + ptr::addr_of_mut!(buffer_size), |
| 62 | + ptr::null_mut() |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if status.is_error() { |
| 67 | + return Err(status.into()); |
| 68 | + } |
| 69 | + |
| 70 | + let mut buffer: Vec<u8> = Vec::with_capacity(buffer_size); |
| 71 | + unsafe { |
| 72 | + status = (self.load_file)(self, |
| 73 | + file_path.as_ffi_ptr(), |
| 74 | + boot_policy, |
| 75 | + ptr::addr_of_mut!(buffer_size), |
| 76 | + buffer.as_mut_ptr() as *mut c_void |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + if status.is_error() { |
| 81 | + return Err(status.into()); |
| 82 | + } |
| 83 | + |
| 84 | + Ok(buffer) |
| 85 | + } |
| 86 | +} |
0 commit comments