Skip to content

boot: Add freestanding get_image_file_system #1322

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

Merged
merged 2 commits into from
Aug 12, 2024
Merged
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
6 changes: 1 addition & 5 deletions uefi-test-runner/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ fn efi_main(image: Handle, mut st: SystemTable<Boot>) -> Status {
// Check the `uefi::system` module.
check_system(&st);

// Test all the boot services.
let bt = st.boot_services();

// Try retrieving a handle to the file system the image was booted from.
bt.get_image_file_system(image)
.expect("Failed to retrieve boot file system");
uefi::boot::get_image_file_system(image).expect("Failed to retrieve boot file system");

boot::test(&st);

Expand Down
33 changes: 29 additions & 4 deletions uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::data_types::PhysicalAddress;
use crate::mem::memory_map::{MemoryMapBackingMemory, MemoryMapKey, MemoryMapMeta, MemoryMapOwned};
use crate::polyfill::maybe_uninit_slice_assume_init_ref;
use crate::proto::device_path::DevicePath;
use crate::proto::loaded_image::LoadedImage;
use crate::proto::media::fs::SimpleFileSystem;
use crate::proto::{Protocol, ProtocolPointer};
use crate::table::Revision;
use crate::util::opt_nonnull_to_ptr;
Expand All @@ -22,10 +24,7 @@ use uefi_raw::table::boot::InterfaceType;
use {alloc::vec::Vec, uefi::ResultExt};

#[cfg(doc)]
use {
crate::proto::device_path::LoadedImageDevicePath, crate::proto::loaded_image::LoadedImage,
crate::proto::media::fs::SimpleFileSystem,
};
use crate::proto::device_path::LoadedImageDevicePath;

pub use uefi::table::boot::{
AllocateType, EventNotifyFn, LoadImageSource, OpenProtocolAttributes, OpenProtocolParams,
Expand Down Expand Up @@ -1156,6 +1155,32 @@ pub fn stall(microseconds: usize) {
}
}

/// Retrieves a [`SimpleFileSystem`] protocol associated with the device the given
/// image was loaded from.
///
/// # Errors
///
/// This function can return errors from [`open_protocol_exclusive`] and
/// [`locate_device_path`]. See those functions for more details.
///
/// * [`Status::INVALID_PARAMETER`]
/// * [`Status::UNSUPPORTED`]
/// * [`Status::ACCESS_DENIED`]
/// * [`Status::ALREADY_STARTED`]
/// * [`Status::NOT_FOUND`]
pub fn get_image_file_system(image_handle: Handle) -> Result<ScopedProtocol<SimpleFileSystem>> {
let loaded_image = open_protocol_exclusive::<LoadedImage>(image_handle)?;

let device_handle = loaded_image
.device()
.ok_or(Error::new(Status::UNSUPPORTED, ()))?;
let device_path = open_protocol_exclusive::<DevicePath>(device_handle)?;

let device_handle = locate_device_path::<SimpleFileSystem>(&mut &*device_path)?;

open_protocol_exclusive(device_handle)
}

/// Protocol interface [`Guids`][Guid] that are installed on a [`Handle`] as
/// returned by [`protocols_per_handle`].
#[derive(Debug)]
Expand Down