Skip to content

proto/media: add LoadFile2Protocol #945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions uefi-raw/src/protocol/media.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use crate::{guid, Guid, Status};
use crate::protocol::device_path::DevicePathProtocol;
use core::ffi::c_void;

/// The UEFI LoadFile2 protocol.
///
/// This protocol has a single method to load a file according to some
/// device path.
///
/// This interface is implemented by many devices, e.g. network and filesystems.
#[derive(Debug)]
#[repr(C)]
pub struct LoadFile2 {
pub load_file: unsafe extern "efiapi" fn(
this: &mut LoadFile2,
file_path: *const DevicePathProtocol,
boot_policy: bool,
buffer_size: *mut usize,
buffer: *mut c_void,
) -> Status,
}

impl LoadFile2 {
pub const GUID: Guid = guid!("4006c0c1-fcb3-403e-996d-4a6c8724e06d");
}
79 changes: 79 additions & 0 deletions uefi/src/proto/media/load_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! Load file support protocols.

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use crate::proto::device_path::{FfiDevicePath, DevicePath};
use crate::proto::unsafe_protocol;
use crate::{Result, Status};
use core::ffi::c_void;
use core::ptr;

/// The UEFI LoadFile2 protocol.
///
/// This protocol has a single method to load a file according to some
/// device path.
///
/// This interface is implemented by many devices, e.g. network and filesystems.
#[derive(Debug)]
#[repr(transparent)]
#[unsafe_protocol(uefi_raw::protocol::media::LoadFile2::GUID)]
pub struct LoadFile2(uefi_raw::protocol::media::LoadFile2);

impl LoadFile2 {
/// Load file addressed by provided device path
pub fn load_file(&mut self,
file_path: &DevicePath,
buffer: &mut [u8]
) -> Result<(), usize> {
let mut buffer_size = buffer.len();
unsafe {
(self.0.load_file)(self,
file_path,
false,
buffer_size,
buffer.as_mut_ptr()
)
}.to_result_with(
|| debug_assert_eq!(buffer_size, buffer.len()),
|_| buffer_size
)
}

#[cfg(feature = "alloc")]
/// Load file addressed by the provided device path.
pub fn load_file_to_vec(&mut self,
file_path: &DevicePath,
) -> Result<Vec<u8>> {
let mut buffer_size: usize = 0;
let mut status: Status;
unsafe {
status = (self.0.load_file)(self,
file_path.as_ffi_ptr(),
false,
ptr::addr_of_mut!(buffer_size),
ptr::null_mut()
);
}

if status.is_error() {
return Err(status.into());
}

let mut buffer: Vec<u8> = Vec::with_capacity(buffer_size);
unsafe {
status = (self.0.load_file)(self,
file_path.as_ffi_ptr(),
false,
ptr::addr_of_mut!(buffer_size),
buffer.as_mut_ptr() as *mut c_void
);
}

if status.is_error() {
return Err(status.into());
}

Ok(buffer)
}
}
1 change: 1 addition & 0 deletions uefi/src/proto/media/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ pub mod block;
pub mod disk;
pub mod fs;
pub mod partition;
pub mod load_file;