2025-10-13 01:54:03 -07:00
|
|
|
use crate::context::SproutContext;
|
2025-10-14 12:47:33 -07:00
|
|
|
use crate::entries::EntryDeclaration;
|
2025-10-13 01:54:03 -07:00
|
|
|
use crate::generators::bls::entry::BlsEntry;
|
|
|
|
|
use crate::utils;
|
|
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
use uefi::CString16;
|
|
|
|
|
use uefi::fs::{FileSystem, Path};
|
|
|
|
|
use uefi::proto::device_path::text::{AllowShortcuts, DisplayOnly};
|
|
|
|
|
use uefi::proto::media::fs::SimpleFileSystem;
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// BLS entry parser.
|
2025-10-14 12:47:33 -07:00
|
|
|
mod entry;
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// The default path to the BLS entries directory.
|
|
|
|
|
const BLS_TEMPLATE_PATH: &str = "\\loader\\entries";
|
|
|
|
|
|
|
|
|
|
/// The configuration of the BLS generator.
|
|
|
|
|
/// The BLS uses the Bootloader Specification to produce
|
|
|
|
|
/// entries from an input template.
|
2025-10-13 01:54:03 -07:00
|
|
|
#[derive(Serialize, Deserialize, Default, Clone)]
|
|
|
|
|
pub struct BlsConfiguration {
|
2025-10-20 00:06:46 -07:00
|
|
|
/// The entry to use for as a template.
|
2025-10-13 01:54:03 -07:00
|
|
|
pub entry: EntryDeclaration,
|
2025-10-20 00:06:46 -07:00
|
|
|
/// The path to the BLS entries directory.
|
2025-10-13 01:54:03 -07:00
|
|
|
#[serde(default = "default_bls_path")]
|
|
|
|
|
pub path: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn default_bls_path() -> String {
|
2025-10-20 00:06:46 -07:00
|
|
|
BLS_TEMPLATE_PATH.to_string()
|
2025-10-13 01:54:03 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-20 11:33:33 -07:00
|
|
|
// TODO(azenla): remove this once variable substitution is implemented.
|
|
|
|
|
/// This function is used to remove the `tuned_initrd` variable from entry values.
|
|
|
|
|
/// Fedora uses tuned which adds an initrd that shouldn't be used.
|
|
|
|
|
fn quirk_initrd_remove_tuned(input: String) -> String {
|
|
|
|
|
input.replace("$tuned_initrd", "").trim().to_string()
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Generates entries from the BLS entries directory using the specified `bls` configuration and
|
|
|
|
|
/// `context`. The BLS conversion is best-effort and will ignore any unsupported entries.
|
2025-10-13 01:54:03 -07:00
|
|
|
pub fn generate(
|
|
|
|
|
context: Rc<SproutContext>,
|
|
|
|
|
bls: &BlsConfiguration,
|
|
|
|
|
) -> Result<Vec<(Rc<SproutContext>, EntryDeclaration)>> {
|
|
|
|
|
let mut entries = Vec::new();
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Stamp the path to the BLS entries directory.
|
2025-10-13 01:54:03 -07:00
|
|
|
let path = context.stamp(&bls.path);
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Resolve the path to the BLS entries directory.
|
2025-10-13 01:54:03 -07:00
|
|
|
let resolved = utils::resolve_path(context.root().loaded_image_path()?, &path)
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to resolve bls path")?;
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Open exclusive access to the BLS filesystem.
|
2025-10-13 01:54:03 -07:00
|
|
|
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(resolved.filesystem_handle)
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to open bls filesystem")?;
|
2025-10-13 01:54:03 -07:00
|
|
|
let mut fs = FileSystem::new(fs);
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Convert the subpath to the BLS entries directory to a string.
|
2025-10-13 01:54:03 -07:00
|
|
|
let sub_text_path = resolved
|
|
|
|
|
.sub_path
|
|
|
|
|
.to_string(DisplayOnly(false), AllowShortcuts(false))
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to convert subpath to string")?;
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Produce a path to the BLS entries directory.
|
2025-10-13 01:54:03 -07:00
|
|
|
let entries_path = Path::new(&sub_text_path);
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Read the BLS entries directory.
|
2025-10-13 01:54:03 -07:00
|
|
|
let entries_iter = fs
|
|
|
|
|
.read_dir(entries_path)
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to read bls entries")?;
|
2025-10-13 01:54:03 -07:00
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// For each entry in the BLS entries directory, parse the entry and add it to the list.
|
2025-10-13 01:54:03 -07:00
|
|
|
for entry in entries_iter {
|
2025-10-20 00:06:46 -07:00
|
|
|
// Unwrap the entry file info.
|
|
|
|
|
let entry = entry.context("unable to read bls item entry")?;
|
|
|
|
|
|
|
|
|
|
// Skip items that are not regular files.
|
2025-10-13 01:54:03 -07:00
|
|
|
if !entry.is_regular_file() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Get the file name of the filesystem item.
|
2025-10-13 01:54:03 -07:00
|
|
|
let name = entry.file_name().to_string();
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Ignore files that are not .conf files.
|
2025-10-13 01:54:03 -07:00
|
|
|
if !name.ends_with(".conf") {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Produce the full path to the entry file.
|
2025-10-13 01:54:03 -07:00
|
|
|
let full_entry_path = CString16::try_from(format!("{}\\{}", sub_text_path, name).as_str())
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to construct full entry path")?;
|
2025-10-13 01:54:03 -07:00
|
|
|
let full_entry_path = Path::new(&full_entry_path);
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Read the entry file.
|
2025-10-13 01:54:03 -07:00
|
|
|
let content = fs
|
|
|
|
|
.read(full_entry_path)
|
2025-10-14 12:47:33 -07:00
|
|
|
.context("unable to read bls file")?;
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Parse the entry file as a UTF-8 string.
|
2025-10-14 12:47:33 -07:00
|
|
|
let content = String::from_utf8(content).context("unable to read bls entry as utf8")?;
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Parse the entry file as a BLS entry.
|
2025-10-14 12:47:33 -07:00
|
|
|
let entry = BlsEntry::from_str(&content).context("unable to parse bls entry")?;
|
2025-10-13 01:54:03 -07:00
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Ignore entries that are not valid for Sprout.
|
2025-10-13 01:54:03 -07:00
|
|
|
if !entry.is_valid() {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Produce a new sprout context for the entry with the extracted values.
|
2025-10-13 01:54:03 -07:00
|
|
|
let mut context = context.fork();
|
2025-10-20 11:33:33 -07:00
|
|
|
|
|
|
|
|
let title = entry.title().unwrap_or(name);
|
|
|
|
|
let chainload = entry.chainload_path().unwrap_or_default();
|
|
|
|
|
let options = entry.options().unwrap_or_default();
|
|
|
|
|
|
|
|
|
|
// Put the initrd through a quirk modifier to support Fedora.
|
|
|
|
|
let initrd = quirk_initrd_remove_tuned(entry.initrd_path().unwrap_or_default());
|
|
|
|
|
|
|
|
|
|
context.set("title", title);
|
|
|
|
|
context.set("chainload", chainload);
|
|
|
|
|
context.set("options", options);
|
|
|
|
|
context.set("initrd", initrd);
|
2025-10-13 01:54:03 -07:00
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Add the entry to the list with a frozen context.
|
2025-10-13 01:54:03 -07:00
|
|
|
entries.push((context.freeze(), bls.entry.clone()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(entries)
|
|
|
|
|
}
|