Skip to content
Merged
5 changes: 5 additions & 0 deletions src/actions/chainload.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::context::SproutContext;
use crate::integrations::bootloader_interface::BootloaderInterface;
use crate::utils;
use crate::utils::media_loader::MediaLoaderHandle;
use crate::utils::media_loader::constants::linux::LINUX_EFI_INITRD_MEDIA_GUID;
Expand Down Expand Up @@ -102,6 +103,10 @@ pub fn chainload(context: Rc<SproutContext>, configuration: &ChainloadConfigurat
initrd_handle = Some(handle);
}

// Mark execution of an entry in the bootloader interface.
BootloaderInterface::mark_exec(context.root().timer())
.context("unable to mark execution of boot entry in bootloader interface")?;

// Start the loaded image.
// This call might return, or it may pass full control to another image that will never return.
// Capture the result to ensure we can return an error if the image fails to start, but only
Expand Down
19 changes: 16 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::actions::ActionDeclaration;
use crate::options::SproutOptions;
use crate::platform::timer::PlatformTimer;
use anyhow::anyhow;
use anyhow::{Result, bail};
use std::cmp::Reverse;
Expand All @@ -12,22 +13,29 @@ const CONTEXT_FINALIZE_ITERATION_LIMIT: usize = 100;

/// Declares a root context for Sprout.
/// This contains data that needs to be shared across Sprout.
#[derive(Default)]
pub struct RootContext {
/// The actions that are available in Sprout.
actions: BTreeMap<String, ActionDeclaration>,
/// The device path of the loaded Sprout image.
loaded_image_path: Option<Box<DevicePath>>,
/// Platform timer started at the beginning of the boot process.
timer: PlatformTimer,
/// The global options of Sprout.
options: SproutOptions,
}

impl RootContext {
/// Creates a new root context with the `loaded_image_device_path` which will be stored
/// in the context for easy access.
pub fn new(loaded_image_device_path: Box<DevicePath>, options: SproutOptions) -> Self {
/// in the context for easy access. We also provide a `timer` which is used to measure elapsed
/// time for the bootloader.
pub fn new(
loaded_image_device_path: Box<DevicePath>,
timer: PlatformTimer,
options: SproutOptions,
) -> Self {
Self {
actions: BTreeMap::new(),
timer,
loaded_image_path: Some(loaded_image_device_path),
options,
}
Expand All @@ -43,6 +51,11 @@ impl RootContext {
&mut self.actions
}

/// Access the platform timer that is started at the beginning of the boot process.
pub fn timer(&self) -> &PlatformTimer {
&self.timer
}

/// Access the device path of the loaded Sprout image.
pub fn loaded_image_path(&self) -> Result<&DevicePath> {
self.loaded_image_path
Expand Down
12 changes: 12 additions & 0 deletions src/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct BootableEntry {
context: Rc<SproutContext>,
declaration: EntryDeclaration,
default: bool,
pin_name: bool,
}

impl BootableEntry {
Expand All @@ -44,6 +45,7 @@ impl BootableEntry {
context,
declaration,
default: false,
pin_name: false,
}
}

Expand Down Expand Up @@ -72,6 +74,11 @@ impl BootableEntry {
self.default
}

/// Fetch whether the entry is pinned, which prevents prefixing.
pub fn is_pin_name(&self) -> bool {
self.pin_name
}

/// Swap out the context of the entry.
pub fn swap_context(&mut self, context: Rc<SproutContext>) {
self.context = context;
Expand All @@ -87,6 +94,11 @@ impl BootableEntry {
self.default = true;
}

/// Mark this entry as being pinned, which prevents prefixing.
pub fn mark_pin_name(&mut self) {
self.pin_name = true;
}

/// Prepend the name of the entry with `prefix`.
pub fn prepend_name_prefix(&mut self, prefix: &str) {
self.name.insert_str(0, prefix);
Expand Down
91 changes: 33 additions & 58 deletions src/extractors/filesystem_device_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use uefi::fs::{FileSystem, Path};
use uefi::proto::device_path::DevicePath;
use uefi::proto::media::file::{File, FileSystemVolumeLabel};
use uefi::proto::media::fs::SimpleFileSystem;
use uefi::proto::media::partition::PartitionInfo;
use uefi::{CString16, Guid, Handle};
use uefi_raw::Status;
use uefi::{CString16, Guid};

/// The filesystem device match extractor.
/// This extractor finds a filesystem using some search criteria and returns
Expand Down Expand Up @@ -41,48 +39,6 @@ pub struct FilesystemDeviceMatchExtractor {
pub fallback: Option<String>,
}

/// Represents the partition UUIDs for a filesystem.
struct PartitionIds {
/// The UUID of the partition.
partition_uuid: Guid,
/// The type UUID of the partition.
type_uuid: Guid,
}

/// Fetches the partition UUIDs for the specified filesystem handle.
fn fetch_partition_uuids(handle: Handle) -> Result<Option<PartitionIds>> {
// Open the partition info protocol for this handle.
let partition_info = uefi::boot::open_protocol_exclusive::<PartitionInfo>(handle);

match partition_info {
Ok(partition_info) => {
// GPT partitions have a unique partition GUID.
// MBR does not.
if let Some(gpt) = partition_info.gpt_partition_entry() {
let uuid = gpt.unique_partition_guid;
let type_uuid = gpt.partition_type_guid;
Ok(Some(PartitionIds {
partition_uuid: uuid,
type_uuid: type_uuid.0,
}))
} else {
Ok(None)
}
}

Err(error) => {
// If the filesystem does not have a partition, that is okay.
if error.status() == Status::NOT_FOUND || error.status() == Status::UNSUPPORTED {
Ok(None)
} else {
// We should still handle other errors gracefully.
Err(error).context("unable to open filesystem partition info")?;
unreachable!()
}
}
}
}

/// Extract a filesystem device path using the specified `context` and `extractor` configuration.
pub fn extract(
context: Rc<SproutContext>,
Expand All @@ -106,30 +62,49 @@ pub fn extract(
// This defines whether a match has been found.
let mut has_match = false;

// Extract the partition info for this filesystem.
// There is no guarantee that the filesystem has a partition.
let partition_info =
fetch_partition_uuids(handle).context("unable to fetch partition info")?;

// Check if the partition info matches partition uuid criteria.
if let Some(ref partition_info) = partition_info
&& let Some(ref has_partition_uuid) = extractor.has_partition_uuid
{
if let Some(ref has_partition_uuid) = extractor.has_partition_uuid {
// Parse the partition uuid from the extractor.
let parsed_uuid = Guid::from_str(has_partition_uuid)
.map_err(|e| anyhow!("unable to parse has-partition-uuid: {}", e))?;
if partition_info.partition_uuid != parsed_uuid {

// 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) {
continue;
}
has_match = true;
}

// Check if the partition info matches partition type uuid criteria.
if let Some(ref partition_info) = partition_info
&& let Some(ref has_partition_type_uuid) = extractor.has_partition_type_uuid
{
if let Some(ref has_partition_type_uuid) = extractor.has_partition_type_uuid {
// Parse the partition type uuid from the extractor.
let parsed_uuid = Guid::from_str(has_partition_type_uuid)
.map_err(|e| anyhow!("unable to parse has-partition-type-uuid: {}", e))?;
if partition_info.type_uuid != parsed_uuid {

// 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) {
continue;
}
has_match = true;
Expand Down
19 changes: 15 additions & 4 deletions src/generators/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,16 @@ pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Ve
}

// Get the file name of the filesystem item.
let name = entry.file_name().to_string();
let mut name = entry.file_name().to_string();

// Ignore files that are not .conf files.
if !name.to_lowercase().ends_with(".conf") {
continue;
}

// Remove the .conf extension.
name.truncate(name.len() - 5);

// Create a mutable path so we can append the file name to produce the full path.
let mut full_entry_path = entries_path.to_path_buf();
full_entry_path.push(entry.file_name());
Expand Down Expand Up @@ -125,13 +128,21 @@ pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Ve
context.set("options", options);
context.set("initrd", initrd);

// Add the entry to the list with a frozen context.
entries.push(BootableEntry::new(
// Produce a new bootable entry.
let mut entry = BootableEntry::new(
name,
bls.entry.title.clone(),
context.freeze(),
bls.entry.clone(),
));
);

// Pin the entry name to prevent prefixing.
// This is needed as the bootloader interface requires the name to be
// the same as the entry file name, minus the .conf extension.
entry.mark_pin_name();

// Add the entry to the list with a frozen context.
entries.push(entry);
}

Ok(entries)
Expand Down
2 changes: 2 additions & 0 deletions src/integrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Implements support for the bootloader interface specification.
pub mod bootloader_interface;
Loading
Loading