2025-10-19 20:30:09 -07:00
|
|
|
use crate::config::{RootConfiguration, latest_version};
|
2025-10-20 18:17:29 -07:00
|
|
|
use crate::options::SproutOptions;
|
2025-10-19 20:30:09 -07:00
|
|
|
use crate::utils;
|
|
|
|
|
use anyhow::{Context, Result, bail};
|
2025-10-20 18:17:29 -07:00
|
|
|
use log::info;
|
2025-10-19 20:23:55 -07:00
|
|
|
use std::ops::Deref;
|
|
|
|
|
use toml::Value;
|
|
|
|
|
use uefi::proto::device_path::LoadedImageDevicePath;
|
|
|
|
|
|
2025-10-20 18:17:29 -07:00
|
|
|
/// Loads the raw configuration from the sprout config file as data.
|
|
|
|
|
fn load_raw_config(options: &SproutOptions) -> Result<Vec<u8>> {
|
2025-10-20 00:06:46 -07:00
|
|
|
// Open the LoadedImageDevicePath protocol to get the path to the current image.
|
2025-10-19 20:23:55 -07:00
|
|
|
let current_image_device_path_protocol =
|
|
|
|
|
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(uefi::boot::image_handle())
|
|
|
|
|
.context("unable to get loaded image device path")?;
|
2025-10-20 00:06:46 -07:00
|
|
|
// Acquire the device path as a boxed device path.
|
2025-10-19 20:23:55 -07:00
|
|
|
let path = current_image_device_path_protocol.deref().to_boxed();
|
2025-10-20 18:17:29 -07:00
|
|
|
|
|
|
|
|
info!("configuration file: {}", options.config);
|
|
|
|
|
|
|
|
|
|
// Read the contents of the sprout config file.
|
|
|
|
|
let content = utils::read_file_contents(&path, &options.config)
|
|
|
|
|
.context("unable to read sprout config file")?;
|
|
|
|
|
// Return the contents of the sprout config file.
|
2025-10-19 20:23:55 -07:00
|
|
|
Ok(content)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Loads the [RootConfiguration] for Sprout.
|
2025-10-20 18:17:29 -07:00
|
|
|
pub fn load(options: &SproutOptions) -> Result<RootConfiguration> {
|
|
|
|
|
// Load the raw configuration from the sprout config file.
|
|
|
|
|
let content = load_raw_config(options)?;
|
2025-10-20 00:06:46 -07:00
|
|
|
// Parse the raw configuration into a toml::Value which can represent any TOML file.
|
2025-10-20 18:17:29 -07:00
|
|
|
let value: Value = toml::from_slice(&content).context("unable to parse sprout config file")?;
|
2025-10-19 20:23:55 -07:00
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Check the version of the configuration without parsing the full configuration.
|
2025-10-19 20:23:55 -07:00
|
|
|
let version = value
|
|
|
|
|
.get("version")
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or_else(|| Value::Integer(latest_version() as i64));
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Parse the version into an u32.
|
2025-10-19 20:23:55 -07:00
|
|
|
let version: u32 = version
|
|
|
|
|
.try_into()
|
|
|
|
|
.context("unable to get configuration version")?;
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Check if the version is supported.
|
2025-10-19 20:23:55 -07:00
|
|
|
if version != latest_version() {
|
|
|
|
|
bail!("unsupported configuration version: {}", version);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// If the version is supported, parse the full configuration.
|
2025-10-19 20:23:55 -07:00
|
|
|
let config: RootConfiguration = value
|
|
|
|
|
.try_into()
|
|
|
|
|
.context("unable to parse sprout.toml file")?;
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Return the parsed configuration.
|
2025-10-19 20:23:55 -07:00
|
|
|
Ok(config)
|
|
|
|
|
}
|