Files
sprout/src/drivers.rs

103 lines
3.7 KiB
Rust
Raw Normal View History

2025-10-12 22:39:56 -07:00
use crate::context::SproutContext;
use crate::utils;
use anyhow::{Context, Result};
use log::info;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::rc::Rc;
use uefi::boot::SearchType;
use uefi::proto::device_path::LoadedImageDevicePath;
/// Declares a driver configuration.
/// Drivers allow extending the functionality of Sprout.
/// Drivers are loaded at runtime and can provide extra functionality like filesystem support.
/// Drivers are loaded by their name, which is used to reference them in other concepts.
2025-10-12 22:39:56 -07:00
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct DriverDeclaration {
/// The filesystem path to the driver.
/// This file should be an EFI executable that can be located and executed.
2025-10-12 22:39:56 -07:00
pub path: String,
}
2025-10-24 15:54:58 -07:00
/// Loads the driver specified by the `driver` declaration.
2025-10-12 22:39:56 -07:00
fn load_driver(context: Rc<SproutContext>, driver: &DriverDeclaration) -> Result<()> {
// Acquire the handle and device path of the loaded image.
2025-10-12 22:39:56 -07:00
let sprout_image = uefi::boot::image_handle();
let image_device_path_protocol =
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(sprout_image)
.context("unable to open loaded image device path protocol")?;
// Get the device path root of the sprout image.
2025-10-12 22:39:56 -07:00
let mut full_path = utils::device_path_root(&image_device_path_protocol)?;
// Push the path of the driver from the root.
2025-10-12 22:39:56 -07:00
full_path.push_str(&context.stamp(&driver.path));
info!("driver path: {}", full_path);
// Convert the path to a device path.
2025-10-12 22:39:56 -07:00
let device_path = utils::text_to_device_path(&full_path)?;
// Load the driver image.
2025-10-12 22:39:56 -07:00
let image = uefi::boot::load_image(
sprout_image,
uefi::boot::LoadImageSource::FromDevicePath {
device_path: &device_path,
boot_policy: uefi::proto::BootPolicy::ExactMatch,
},
)
.context("unable to load image")?;
2025-10-12 22:39:56 -07:00
// Start the driver image, this is expected to return control to sprout.
// There is no guarantee that the driver will actually return control as it is
// just a standard EFI image.
uefi::boot::start_image(image).context("unable to start driver image")?;
2025-10-12 22:39:56 -07:00
Ok(())
}
/// Reconnects all handles to their controllers.
/// This is effectively a UEFI stack reload in a sense.
/// After we load all the drivers, we need to reconnect all of their handles
/// so that filesystems are recognized again.
2025-10-12 22:39:56 -07:00
fn reconnect() -> Result<()> {
// Locate all of the handles in the UEFI stack.
2025-10-12 22:39:56 -07:00
let handles = uefi::boot::locate_handle_buffer(SearchType::AllHandles)
.context("unable to locate handles buffer")?;
2025-10-12 22:39:56 -07:00
for handle in handles.iter() {
// Ignore the result as there is nothing we can do if reconnecting a controller fails.
// This is also likely to fail in some cases but should fail safely.
2025-10-12 22:39:56 -07:00
let _ = uefi::boot::connect_controller(*handle, None, None, true);
}
Ok(())
}
/// Load all the drivers specified in `drivers`.
/// There is no driver order currently. This will reconnect all the controllers
/// to all handles if at least one driver was loaded.
2025-10-12 22:39:56 -07:00
pub fn load(
context: Rc<SproutContext>,
drivers: &BTreeMap<String, DriverDeclaration>,
) -> Result<()> {
// If there are no drivers, we don't need to do anything.
2025-10-12 22:39:56 -07:00
if drivers.is_empty() {
return Ok(());
}
info!("loading drivers");
// Load all the drivers in no particular order.
2025-10-12 22:39:56 -07:00
for (name, driver) in drivers {
load_driver(context.clone(), driver).context(format!("unable to load driver: {}", name))?;
2025-10-12 22:39:56 -07:00
}
// Reconnect all the controllers to all handles.
reconnect().context("unable to reconnect drivers")?;
2025-10-12 22:39:56 -07:00
info!("loaded drivers");
// We've now loaded all the drivers, so we can return.
2025-10-12 22:39:56 -07:00
Ok(())
}