2025-10-11 14:11:31 -07:00
|
|
|
use crate::actions::ActionDeclaration;
|
2025-10-12 22:39:56 -07:00
|
|
|
use crate::drivers::DriverDeclaration;
|
2025-10-14 12:47:33 -07:00
|
|
|
use crate::entries::EntryDeclaration;
|
2025-10-13 00:55:11 -07:00
|
|
|
use crate::extractors::ExtractorDeclaration;
|
2025-10-11 14:11:31 -07:00
|
|
|
use crate::generators::GeneratorDeclaration;
|
2025-10-14 12:47:33 -07:00
|
|
|
use crate::phases::PhasesConfiguration;
|
2025-10-01 21:30:43 -07:00
|
|
|
use crate::utils;
|
2025-10-11 14:35:29 -07:00
|
|
|
use anyhow::Result;
|
2025-10-19 02:24:11 -07:00
|
|
|
use anyhow::{Context, bail};
|
2025-10-01 18:25:49 -07:00
|
|
|
use serde::{Deserialize, Serialize};
|
2025-10-04 23:12:01 -07:00
|
|
|
use std::collections::BTreeMap;
|
2025-10-13 00:55:11 -07:00
|
|
|
use std::ops::Deref;
|
2025-10-19 02:24:11 -07:00
|
|
|
use toml::Value;
|
2025-10-13 00:55:11 -07:00
|
|
|
use uefi::proto::device_path::LoadedImageDevicePath;
|
2025-10-01 18:25:49 -07:00
|
|
|
|
2025-10-04 23:12:01 -07:00
|
|
|
#[derive(Serialize, Deserialize, Default, Clone)]
|
2025-10-01 18:25:49 -07:00
|
|
|
pub struct RootConfiguration {
|
2025-10-05 00:09:53 -07:00
|
|
|
#[serde(default = "latest_version")]
|
2025-10-04 23:12:01 -07:00
|
|
|
pub version: u32,
|
2025-10-01 18:25:49 -07:00
|
|
|
#[serde(default)]
|
2025-10-04 23:12:01 -07:00
|
|
|
pub values: BTreeMap<String, String>,
|
|
|
|
|
#[serde(default)]
|
2025-10-12 22:39:56 -07:00
|
|
|
pub drivers: BTreeMap<String, DriverDeclaration>,
|
|
|
|
|
#[serde(default)]
|
2025-10-13 00:55:11 -07:00
|
|
|
pub extractors: BTreeMap<String, ExtractorDeclaration>,
|
|
|
|
|
#[serde(default)]
|
2025-10-04 23:12:01 -07:00
|
|
|
pub actions: BTreeMap<String, ActionDeclaration>,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub entries: BTreeMap<String, EntryDeclaration>,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub generators: BTreeMap<String, GeneratorDeclaration>,
|
|
|
|
|
#[serde(default)]
|
|
|
|
|
pub phases: PhasesConfiguration,
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-14 12:47:33 -07:00
|
|
|
pub fn latest_version() -> u32 {
|
|
|
|
|
1
|
2025-10-01 18:25:49 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-19 02:24:11 -07:00
|
|
|
fn load_raw_config() -> Result<Vec<u8>> {
|
2025-10-13 00:55:11 -07:00
|
|
|
let current_image_device_path_protocol =
|
|
|
|
|
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(uefi::boot::image_handle())
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to get loaded image device path")?;
|
2025-10-13 00:55:11 -07:00
|
|
|
let path = current_image_device_path_protocol.deref().to_boxed();
|
|
|
|
|
|
|
|
|
|
let content = utils::read_file_contents(&path, "sprout.toml")
|
|
|
|
|
.context("unable to read sprout.toml file")?;
|
2025-10-19 02:24:11 -07:00
|
|
|
Ok(content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn load() -> Result<RootConfiguration> {
|
|
|
|
|
let content = load_raw_config()?;
|
|
|
|
|
let value: Value = toml::from_slice(&content).context("unable to parse sprout.toml file")?;
|
|
|
|
|
|
|
|
|
|
let version = value
|
|
|
|
|
.get("version")
|
|
|
|
|
.cloned()
|
|
|
|
|
.unwrap_or_else(|| Value::Integer(latest_version() as i64));
|
|
|
|
|
|
|
|
|
|
let version: u32 = version
|
|
|
|
|
.try_into()
|
|
|
|
|
.context("unable to get configuration version")?;
|
|
|
|
|
|
|
|
|
|
if version != latest_version() {
|
|
|
|
|
bail!("unsupported configuration version: {}", version);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let config: RootConfiguration = value
|
|
|
|
|
.try_into()
|
|
|
|
|
.context("unable to parse sprout.toml file")?;
|
2025-10-11 14:35:29 -07:00
|
|
|
Ok(config)
|
2025-10-01 18:25:49 -07:00
|
|
|
}
|