2025-10-13 00:55:11 -07:00
|
|
|
use crate::context::SproutContext;
|
|
|
|
|
use crate::utils;
|
2025-10-13 16:23:08 -07:00
|
|
|
use anyhow::{Context, Result, anyhow, bail};
|
2025-10-13 00:55:11 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::ops::Deref;
|
|
|
|
|
use std::rc::Rc;
|
2025-10-13 16:23:08 -07:00
|
|
|
use std::str::FromStr;
|
2025-10-13 00:55:11 -07:00
|
|
|
use uefi::fs::{FileSystem, Path};
|
|
|
|
|
use uefi::proto::device_path::DevicePath;
|
|
|
|
|
use uefi::proto::media::file::{File, FileSystemVolumeLabel};
|
|
|
|
|
use uefi::proto::media::fs::SimpleFileSystem;
|
2025-10-28 21:05:22 -04:00
|
|
|
use uefi::{CString16, Guid};
|
2025-10-13 00:55:11 -07:00
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
/// The filesystem device match extractor.
|
|
|
|
|
/// This extractor finds a filesystem using some search criteria and returns
|
|
|
|
|
/// the device root path that can concatenated with subpaths to access files
|
|
|
|
|
/// on a particular filesystem.
|
2025-10-27 23:39:55 -04:00
|
|
|
/// The fallback value can be used to provide a value if no match is found.
|
2025-10-28 00:09:11 -04:00
|
|
|
///
|
2025-10-28 00:19:38 -04:00
|
|
|
/// This extractor requires all the criteria to match. If no criteria is provided,
|
|
|
|
|
/// an error is returned.
|
2025-10-27 03:37:09 -04:00
|
|
|
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
|
2025-10-13 01:02:51 -07:00
|
|
|
pub struct FilesystemDeviceMatchExtractor {
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Matches a filesystem that has the specified label.
|
2025-10-13 01:02:51 -07:00
|
|
|
#[serde(default, rename = "has-label")]
|
|
|
|
|
pub has_label: Option<String>,
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Matches a filesystem that has the specified item.
|
|
|
|
|
/// An item is either a directory or file.
|
2025-10-13 01:02:51 -07:00
|
|
|
#[serde(default, rename = "has-item")]
|
|
|
|
|
pub has_item: Option<String>,
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Matches a filesystem that has the specified partition UUID.
|
2025-10-13 16:23:08 -07:00
|
|
|
#[serde(default, rename = "has-partition-uuid")]
|
|
|
|
|
pub has_partition_uuid: Option<String>,
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Matches a filesystem that has the specified partition type UUID.
|
2025-10-14 01:45:49 -07:00
|
|
|
#[serde(default, rename = "has-partition-type-uuid")]
|
|
|
|
|
pub has_partition_type_uuid: Option<String>,
|
2025-10-19 23:03:28 -07:00
|
|
|
/// The fallback value to use if no filesystem matches the criteria.
|
2025-10-13 16:23:08 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub fallback: Option<String>,
|
2025-10-13 00:55:11 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
/// Extract a filesystem device path using the specified `context` and `extractor` configuration.
|
2025-10-13 00:55:11 -07:00
|
|
|
pub fn extract(
|
|
|
|
|
context: Rc<SproutContext>,
|
2025-10-13 01:02:51 -07:00
|
|
|
extractor: &FilesystemDeviceMatchExtractor,
|
2025-10-13 00:55:11 -07:00
|
|
|
) -> Result<String> {
|
2025-10-28 17:10:28 -04:00
|
|
|
// If no criteria are provided, bail with an error.
|
|
|
|
|
if extractor.has_label.is_none()
|
|
|
|
|
&& extractor.has_item.is_none()
|
|
|
|
|
&& extractor.has_partition_uuid.is_none()
|
|
|
|
|
&& extractor.has_partition_type_uuid.is_none()
|
|
|
|
|
{
|
|
|
|
|
bail!("at least one criteria is required for filesystem-device-match");
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Find all the filesystems inside the UEFI stack.
|
2025-10-13 00:55:11 -07:00
|
|
|
let handles = uefi::boot::find_handles::<SimpleFileSystem>()
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to find filesystem handles")?;
|
2025-10-19 23:03:28 -07:00
|
|
|
|
|
|
|
|
// Iterate over all the filesystems and check if they match the criteria.
|
2025-10-13 00:55:11 -07:00
|
|
|
for handle in handles {
|
2025-10-19 23:03:28 -07:00
|
|
|
// This defines whether a match has been found.
|
2025-10-13 16:23:08 -07:00
|
|
|
let mut has_match = false;
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Check if the partition info matches partition uuid criteria.
|
2025-10-28 21:05:22 -04:00
|
|
|
if let Some(ref has_partition_uuid) = extractor.has_partition_uuid {
|
|
|
|
|
// Parse the partition uuid from the extractor.
|
2025-10-13 16:23:08 -07:00
|
|
|
let parsed_uuid = Guid::from_str(has_partition_uuid)
|
2025-10-14 12:47:33 -07:00
|
|
|
.map_err(|e| anyhow!("unable to parse has-partition-uuid: {}", e))?;
|
2025-10-28 21:05:22 -04:00
|
|
|
|
|
|
|
|
// Fetch the root of the device.
|
|
|
|
|
let root = uefi::boot::open_protocol_exclusive::<DevicePath>(handle)
|
|
|
|
|
.context("unable to fetch the device path of the filesystem")?
|
|
|
|
|
.deref()
|
|
|
|
|
.to_boxed();
|
|
|
|
|
|
|
|
|
|
// Fetch the partition uuid for this filesystem.
|
|
|
|
|
let partition_uuid = utils::partition_guid(&root, utils::PartitionGuidForm::Partition)
|
|
|
|
|
.context("unable to fetch the partition uuid of the filesystem")?;
|
|
|
|
|
|
|
|
|
|
// Compare the partition uuid to the parsed uuid.
|
|
|
|
|
// If it does not match, continue to the next filesystem.
|
|
|
|
|
if partition_uuid != Some(parsed_uuid) {
|
2025-10-13 16:23:08 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
has_match = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Check if the partition info matches partition type uuid criteria.
|
2025-10-28 21:05:22 -04:00
|
|
|
if let Some(ref has_partition_type_uuid) = extractor.has_partition_type_uuid {
|
|
|
|
|
// Parse the partition type uuid from the extractor.
|
2025-10-14 01:45:49 -07:00
|
|
|
let parsed_uuid = Guid::from_str(has_partition_type_uuid)
|
2025-10-14 12:47:33 -07:00
|
|
|
.map_err(|e| anyhow!("unable to parse has-partition-type-uuid: {}", e))?;
|
2025-10-28 21:05:22 -04:00
|
|
|
|
|
|
|
|
// Fetch the root of the device.
|
|
|
|
|
let root = uefi::boot::open_protocol_exclusive::<DevicePath>(handle)
|
|
|
|
|
.context("unable to fetch the device path of the filesystem")?
|
|
|
|
|
.deref()
|
|
|
|
|
.to_boxed();
|
|
|
|
|
|
|
|
|
|
// Fetch the partition uuid for this filesystem.
|
|
|
|
|
let partition_type_uuid =
|
|
|
|
|
utils::partition_guid(&root, utils::PartitionGuidForm::Partition)
|
|
|
|
|
.context("unable to fetch the partition uuid of the filesystem")?;
|
|
|
|
|
// Compare the partition type uuid to the parsed uuid.
|
|
|
|
|
// If it does not match, continue to the next filesystem.
|
|
|
|
|
if partition_type_uuid != Some(parsed_uuid) {
|
2025-10-14 01:45:49 -07:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
has_match = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Open the filesystem protocol for this handle.
|
2025-10-13 00:55:11 -07:00
|
|
|
let mut filesystem = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(handle)
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to open filesystem protocol")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Check if the filesystem matches label criteria.
|
2025-10-13 01:02:51 -07:00
|
|
|
if let Some(ref label) = extractor.has_label {
|
2025-10-13 00:55:11 -07:00
|
|
|
let want_label = CString16::try_from(context.stamp(label).as_str())
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to convert label to CString16")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
let mut root = filesystem
|
|
|
|
|
.open_volume()
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to open filesystem volume")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
let label = root
|
|
|
|
|
.get_boxed_info::<FileSystemVolumeLabel>()
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to get filesystem volume label")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
|
|
|
|
|
if label.volume_label() != want_label {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-10-13 16:23:08 -07:00
|
|
|
has_match = true;
|
2025-10-13 00:55:11 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Check if the filesystem matches item criteria.
|
2025-10-13 01:02:51 -07:00
|
|
|
if let Some(ref item) = extractor.has_item {
|
2025-10-13 00:55:11 -07:00
|
|
|
let want_item = CString16::try_from(context.stamp(item).as_str())
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to convert item to CString16")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
let mut filesystem = FileSystem::new(filesystem);
|
2025-10-19 23:03:28 -07:00
|
|
|
|
|
|
|
|
// Check the metadata of the item.
|
2025-10-13 00:55:11 -07:00
|
|
|
let metadata = filesystem.metadata(Path::new(&want_item));
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Ignore filesystem errors as we can't do anything useful with the error.
|
2025-10-13 00:55:11 -07:00
|
|
|
if metadata.is_err() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let metadata = metadata?;
|
2025-10-19 23:03:28 -07:00
|
|
|
// Only check directories and files.
|
2025-10-13 00:55:11 -07:00
|
|
|
if !(metadata.is_directory() || metadata.is_regular_file()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-10-13 16:23:08 -07:00
|
|
|
has_match = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// If there is no match, continue to the next filesystem.
|
2025-10-13 16:23:08 -07:00
|
|
|
if !has_match {
|
|
|
|
|
continue;
|
2025-10-13 00:55:11 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// If we have a match, return the device root path.
|
2025-10-13 00:55:11 -07:00
|
|
|
let path = uefi::boot::open_protocol_exclusive::<DevicePath>(handle)
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to open filesystem device path")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
let path = path.deref();
|
2025-10-19 23:03:28 -07:00
|
|
|
// Acquire the device path root as a string.
|
2025-10-14 12:47:33 -07:00
|
|
|
return utils::device_path_root(path).context("unable to get device path root");
|
2025-10-13 00:55:11 -07:00
|
|
|
}
|
2025-10-13 16:23:08 -07:00
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// If there is a fallback value, use it at this point.
|
2025-10-13 16:23:08 -07:00
|
|
|
if let Some(fallback) = &extractor.fallback {
|
|
|
|
|
return Ok(fallback.clone());
|
|
|
|
|
}
|
2025-10-19 23:03:28 -07:00
|
|
|
|
|
|
|
|
// Without a fallback, we can't continue, so bail.
|
2025-10-13 16:23:08 -07:00
|
|
|
bail!("unable to find matching filesystem")
|
2025-10-13 00:55:11 -07:00
|
|
|
}
|