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-13 16:23:08 -07:00
|
|
|
use uefi::proto::media::partition::PartitionInfo;
|
|
|
|
|
use uefi::{CString16, Guid};
|
|
|
|
|
use uefi_raw::Status;
|
2025-10-13 00:55:11 -07:00
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Default, Clone)]
|
2025-10-13 01:02:51 -07:00
|
|
|
pub struct FilesystemDeviceMatchExtractor {
|
|
|
|
|
#[serde(default, rename = "has-label")]
|
|
|
|
|
pub has_label: Option<String>,
|
|
|
|
|
#[serde(default, rename = "has-item")]
|
|
|
|
|
pub has_item: Option<String>,
|
2025-10-13 16:23:08 -07:00
|
|
|
#[serde(default, rename = "has-partition-uuid")]
|
|
|
|
|
pub has_partition_uuid: Option<String>,
|
2025-10-14 01:45:49 -07:00
|
|
|
#[serde(default, rename = "has-partition-type-uuid")]
|
|
|
|
|
pub has_partition_type_uuid: Option<String>,
|
2025-10-13 16:23:08 -07:00
|
|
|
#[serde(default)]
|
|
|
|
|
pub fallback: Option<String>,
|
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> {
|
|
|
|
|
let handles = uefi::boot::find_handles::<SimpleFileSystem>()
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to find filesystem handles")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
for handle in handles {
|
2025-10-13 16:23:08 -07:00
|
|
|
let mut has_match = false;
|
|
|
|
|
|
2025-10-14 01:45:49 -07:00
|
|
|
let partition_info = {
|
2025-10-13 16:23:08 -07:00
|
|
|
let partition_info = uefi::boot::open_protocol_exclusive::<PartitionInfo>(handle);
|
|
|
|
|
|
|
|
|
|
match partition_info {
|
|
|
|
|
Ok(partition_info) => {
|
|
|
|
|
if let Some(gpt) = partition_info.gpt_partition_entry() {
|
|
|
|
|
let uuid = gpt.unique_partition_guid;
|
2025-10-14 01:45:49 -07:00
|
|
|
let type_uuid = gpt.partition_type_guid;
|
|
|
|
|
Some((uuid, type_uuid.0))
|
2025-10-13 16:23:08 -07:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(error) => {
|
|
|
|
|
if error.status() == Status::NOT_FOUND || error.status() == Status::UNSUPPORTED
|
|
|
|
|
{
|
|
|
|
|
None
|
|
|
|
|
} else {
|
2025-10-14 12:47:33 -07:00
|
|
|
Err(error).context("unable to open filesystem partition info")?;
|
2025-10-13 16:23:08 -07:00
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-14 01:45:49 -07:00
|
|
|
if let Some((partition_uuid, _partition_type_guid)) = partition_info
|
2025-10-13 16:23:08 -07:00
|
|
|
&& let Some(ref has_partition_uuid) = extractor.has_partition_uuid
|
|
|
|
|
{
|
|
|
|
|
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-13 16:23:08 -07:00
|
|
|
if partition_uuid != parsed_uuid {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
has_match = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-14 01:45:49 -07:00
|
|
|
if let Some((_partition_uuid, partition_type_guid)) = partition_info
|
|
|
|
|
&& let Some(ref has_partition_type_uuid) = extractor.has_partition_type_uuid
|
|
|
|
|
{
|
|
|
|
|
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-14 01:45:49 -07:00
|
|
|
if partition_type_guid != parsed_uuid {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
has_match = true;
|
|
|
|
|
}
|
|
|
|
|
|
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-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-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);
|
|
|
|
|
let metadata = filesystem.metadata(Path::new(&want_item));
|
|
|
|
|
|
|
|
|
|
if metadata.is_err() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let metadata = metadata?;
|
|
|
|
|
if !(metadata.is_directory() || metadata.is_regular_file()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-10-13 16:23:08 -07:00
|
|
|
has_match = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !has_match {
|
|
|
|
|
continue;
|
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-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
|
|
|
|
|
|
|
|
if let Some(fallback) = &extractor.fallback {
|
|
|
|
|
return Ok(fallback.clone());
|
|
|
|
|
}
|
|
|
|
|
bail!("unable to find matching filesystem")
|
2025-10-13 00:55:11 -07:00
|
|
|
}
|