mirror of
https://github.com/edera-dev/sprout.git
synced 2025-12-19 19:00:18 +00:00
begin documenting some functions and align error messages
This commit is contained in:
41
src/utils.rs
41
src/utils.rs
@@ -9,6 +9,8 @@ use uefi::{CString16, Handle};
|
||||
pub mod framebuffer;
|
||||
pub mod linux_media_initrd;
|
||||
|
||||
/// Parses the input [path] as a [DevicePath].
|
||||
/// Uses the [DevicePathFromText] protocol exclusively, and will fail if it cannot acquire the protocol.
|
||||
pub fn text_to_device_path(path: &str) -> Result<PoolDevicePath> {
|
||||
let path = CString16::try_from(path).context("unable to convert path to CString16")?;
|
||||
let device_path_from_text = uefi::boot::open_protocol_exclusive::<DevicePathFromText>(
|
||||
@@ -22,6 +24,9 @@ pub fn text_to_device_path(path: &str) -> Result<PoolDevicePath> {
|
||||
.context("unable to convert text to device path")
|
||||
}
|
||||
|
||||
/// Grabs the root part of the [path].
|
||||
/// For example, given "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/\EFI\BOOT\BOOTX64.efi"
|
||||
/// it will give "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)"
|
||||
pub fn device_path_root(path: &DevicePath) -> Result<String> {
|
||||
let mut path = path
|
||||
.node_iter()
|
||||
@@ -44,6 +49,9 @@ pub fn device_path_root(path: &DevicePath) -> Result<String> {
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Grabs the part of the [path] after the root.
|
||||
/// For example, given "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/\EFI\BOOT\BOOTX64.efi"
|
||||
/// it will give "\EFI\BOOT\BOOTX64.efi"
|
||||
pub fn device_path_subpath(path: &DevicePath) -> Result<String> {
|
||||
let path = path
|
||||
.node_iter()
|
||||
@@ -65,15 +73,27 @@ pub fn device_path_subpath(path: &DevicePath) -> Result<String> {
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Represents the components of a resolved path.
|
||||
pub struct ResolvedPath {
|
||||
/// The root path of the resolved path. This is the device itself.
|
||||
/// For example, "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/"
|
||||
pub root_path: Box<DevicePath>,
|
||||
/// The subpath of the resolved path. This is the path to the file.
|
||||
/// For example, "\EFI\BOOT\BOOTX64.efi"
|
||||
pub sub_path: Box<DevicePath>,
|
||||
/// The full path of the resolved path. This is the safest path to use.
|
||||
/// For example, "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/\EFI\BOOT\BOOTX64.efi"
|
||||
pub full_path: Box<DevicePath>,
|
||||
/// The handle of the filesystem containing the path.
|
||||
/// This can be used to acquire a [SimpleFileSystem] protocol to read the file.
|
||||
pub filesystem_handle: Handle,
|
||||
}
|
||||
|
||||
/// Resolve a path specified by [input] to its various components.
|
||||
/// Uses [default_root_path] as the base root if one is not specified in the path.
|
||||
/// Returns [ResolvedPath] which contains the resolved components.
|
||||
pub fn resolve_path(default_root_path: &DevicePath, input: &str) -> Result<ResolvedPath> {
|
||||
let mut path = text_to_device_path(input).context("failed to convert text to path")?;
|
||||
let mut path = text_to_device_path(input).context("unable to convert text to path")?;
|
||||
let path_has_device = path
|
||||
.node_iter()
|
||||
.next()
|
||||
@@ -91,21 +111,21 @@ pub fn resolve_path(default_root_path: &DevicePath, input: &str) -> Result<Resol
|
||||
input.insert_str(
|
||||
0,
|
||||
device_path_root(default_root_path)
|
||||
.context("failed to get loaded image device root")?
|
||||
.context("unable to get loaded image device root")?
|
||||
.as_str(),
|
||||
);
|
||||
path = text_to_device_path(input.as_str()).context("failed to convert text to path")?;
|
||||
path = text_to_device_path(input.as_str()).context("unable to convert text to path")?;
|
||||
}
|
||||
|
||||
let path = path.to_boxed();
|
||||
let root = device_path_root(path.as_ref()).context("failed to convert root to path")?;
|
||||
let root = device_path_root(path.as_ref()).context("unable to convert root to path")?;
|
||||
let root_path = text_to_device_path(root.as_str())
|
||||
.context("failed to convert root to path")?
|
||||
.context("unable to convert root to path")?
|
||||
.to_boxed();
|
||||
let mut root_path = root_path.as_ref();
|
||||
let handle = uefi::boot::locate_device_path::<SimpleFileSystem>(&mut root_path)
|
||||
.context("failed to locate filesystem device path")?;
|
||||
let subpath = device_path_subpath(path.deref()).context("failed to get device subpath")?;
|
||||
.context("unable to locate filesystem device path")?;
|
||||
let subpath = device_path_subpath(path.deref()).context("unable to get device subpath")?;
|
||||
Ok(ResolvedPath {
|
||||
root_path: root_path.to_boxed(),
|
||||
sub_path: text_to_device_path(subpath.as_str())?.to_boxed(),
|
||||
@@ -114,6 +134,13 @@ pub fn resolve_path(default_root_path: &DevicePath, input: &str) -> Result<Resol
|
||||
})
|
||||
}
|
||||
|
||||
/// Read the contents of a file at the location specified with the [input] path.
|
||||
/// Internally, this uses [resolve_path] to resolve the path to its various components.
|
||||
/// [resolve_path] is passed the [default_root_path] which should specify a base root.
|
||||
///
|
||||
/// This acquires exclusive protocol access to the [SimpleFileSystem] protocol of the resolved
|
||||
/// filesystem handle, so care must be taken to call this function outside a scope with
|
||||
/// the filesystem handle protocol acquired.
|
||||
pub fn read_file_contents(default_root_path: &DevicePath, input: &str) -> Result<Vec<u8>> {
|
||||
let resolved = resolve_path(default_root_path, input)?;
|
||||
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(resolved.filesystem_handle)
|
||||
|
||||
Reference in New Issue
Block a user