mirror of
https://github.com/edera-dev/sprout.git
synced 2026-11-04 23:52:22 +00:00
* feat(bls): read PE sections without loading the whole image * feat(bls): produce entries from unified kernel images * feat(bls): boot unified kernel images as type 2 entries Scan the EFI/Linux directory, or the new uki-path option of the BLS generator, for .efi files and turn each one into an entry using the metadata in its PE sections. Autoconfigure now also detects a disk that only has unified kernel images. An image with the id of a type 1 entry is skipped, and boot counting works on the .efi file name. * fix(boot): rename boot counter files in place instead of copying them * fix(bls): skip hidden files and keep unified kernel images when the type 1 scan fails Check file name suffixes without slicing by byte offset, drop the PE section bytes once an image has been parsed, and only fail the generator on a type 1 error when unified kernel images are disabled.
This commit is contained in:
9 files changed
+948
-95
No files matched your search
@@ -9,9 +9,9 @@ use alloc::{
|
||||
};
|
||||
use anyhow::{Context, Result};
|
||||
use core::{cmp::Ordering, str::FromStr};
|
||||
use edera_sprout_bls::{BlsEntry, BootCounter, sort_bls};
|
||||
use edera_sprout_bls::{BlsEntry, BootCounter, sort_bls, strip_extension};
|
||||
use edera_sprout_config::generators::bls::BlsConfiguration;
|
||||
use log::warn;
|
||||
use log::{info, warn};
|
||||
use uefi::{
|
||||
cstr16,
|
||||
fs::{FileSystem, PathBuf},
|
||||
@@ -19,6 +19,15 @@ use uefi::{
|
||||
proto::media::fs::SimpleFileSystem,
|
||||
};
|
||||
|
||||
mod uki;
|
||||
|
||||
/// The PE machine type of the images that can boot on this machine.
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
const PE_MACHINE: u16 = edera_sprout_bls::PE_MACHINE_X86_64;
|
||||
/// The PE machine type of the images that can boot on this machine.
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
const PE_MACHINE: u16 = edera_sprout_bls::PE_MACHINE_AARCH64;
|
||||
|
||||
/// The number of initrd slots set on each BLS entry.
|
||||
/// Entries set `initrd-0` through `initrd-7`, and slots without an initrd are empty.
|
||||
pub const BLS_INITRD_SLOTS: usize = 8;
|
||||
@@ -41,18 +50,84 @@ fn sort_entries(a: &(BlsEntry, BootableEntry), b: &(BlsEntry, BootableEntry)) ->
|
||||
sort_bls(a_bls, a_boot.name(), b_bls, b_boot.name())
|
||||
}
|
||||
|
||||
/// 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.
|
||||
pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Vec<BootableEntry>> {
|
||||
/// Produces the bootable entry for the BLS `entry` with the id `name` and the `initrds`.
|
||||
fn bootable_entry(
|
||||
context: &Rc<SproutContext>,
|
||||
bls: &BlsConfiguration,
|
||||
name: &str,
|
||||
entry: &BlsEntry,
|
||||
initrds: &[String],
|
||||
) -> BootableEntry {
|
||||
// Produce a new sprout context for the entry with the extracted values.
|
||||
let mut context = context.fork();
|
||||
|
||||
let title_base = entry.title().unwrap_or_else(|| name.to_string());
|
||||
let chainload = entry.chainload_path().unwrap_or_default();
|
||||
let options = entry.options().unwrap_or_default();
|
||||
let version = entry.version().unwrap_or_default();
|
||||
let machine_id = entry.machine_id().unwrap_or_default();
|
||||
|
||||
// Combine the title with the version if a version is present, except if it already contains it.
|
||||
// Sometimes BLS will have a version in the title already, and this makes it unique.
|
||||
let title_full = if !version.is_empty() && !title_base.contains(&version) {
|
||||
format!("{} {}", title_base, version)
|
||||
} else {
|
||||
title_base.clone()
|
||||
};
|
||||
|
||||
context.set("title-base", title_base);
|
||||
context.set("title", title_full);
|
||||
context.set("chainload", chainload);
|
||||
context.set("options", options);
|
||||
// The command line and kernel version embedded in a unified kernel image.
|
||||
context.set("cmdline", entry.cmdline.clone().unwrap_or_default());
|
||||
context.set("uname", entry.uname.clone().unwrap_or_default());
|
||||
// The initrd value keeps the last initrd, which is what it held before
|
||||
// multiple initrds were supported.
|
||||
context.set("initrd", initrds.last().cloned().unwrap_or_default());
|
||||
|
||||
// Set every initrd slot, even the unused ones. An unset slot would let "$initrd-1"
|
||||
// match "$initrd" instead, producing the initrd value followed by "-1".
|
||||
for slot in 0..BLS_INITRD_SLOTS {
|
||||
context.set(
|
||||
format!("initrd-{}", slot),
|
||||
initrds.get(slot).cloned().unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
|
||||
context.set("version", version);
|
||||
context.set("machine-id", machine_id);
|
||||
|
||||
// Produce a new bootable entry.
|
||||
let mut boot = BootableEntry::new(
|
||||
name.to_string(),
|
||||
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.
|
||||
if bls.pin_names {
|
||||
boot.mark_pin_name();
|
||||
}
|
||||
|
||||
boot
|
||||
}
|
||||
|
||||
/// Generates the BLS Type #1 entries from the entries directory under `path`.
|
||||
/// The conversion is best-effort and will ignore any unsupported entries.
|
||||
fn generate_type1(
|
||||
context: &Rc<SproutContext>,
|
||||
bls: &BlsConfiguration,
|
||||
path: &str,
|
||||
) -> Result<Vec<(BlsEntry, BootableEntry)>> {
|
||||
let mut entries = Vec::new();
|
||||
|
||||
// Stamp the path to the BLS directory.
|
||||
let path = context.stamp(&bls.path);
|
||||
|
||||
// Resolve the path to the BLS directory.
|
||||
let bls_resolved =
|
||||
eficore::path::resolve_path(Some(context.root().loaded_image_path()?), &path)
|
||||
.context("unable to resolve bls path")?;
|
||||
let bls_resolved = eficore::path::resolve_path(Some(context.root().loaded_image_path()?), path)
|
||||
.context("unable to resolve bls path")?;
|
||||
|
||||
// Construct a filesystem path to the BLS entries directory.
|
||||
let mut entries_path = PathBuf::from(
|
||||
@@ -94,26 +169,22 @@ pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Ve
|
||||
}
|
||||
|
||||
// Get the file name of the filesystem item.
|
||||
let mut name = entry.file_name().to_string();
|
||||
let file_name = entry.file_name().to_string();
|
||||
|
||||
// Ignore files that are not .conf files.
|
||||
if !name.to_lowercase().ends_with(".conf") {
|
||||
// Ignore hidden files, such as the AppleDouble files that macOS writes, and files that
|
||||
// are not .conf files. Files named just ".conf" are not valid entry files.
|
||||
let Some((stem, extension)) = strip_extension(&file_name, ".conf") else {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Remove the .conf extension.
|
||||
name.truncate(name.len() - 5);
|
||||
|
||||
// Skip over files that are named just ".conf" as they are not valid entry files.
|
||||
if name.is_empty() {
|
||||
};
|
||||
if file_name.starts_with('.') || stem.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let extension = extension.to_string();
|
||||
|
||||
// Split the boot counter off the file name. The entry id never includes the counter, so
|
||||
// that it stays the same as tries are consumed.
|
||||
let file_name = entry.file_name().to_string();
|
||||
let (id, boot_counter) = {
|
||||
let (id, counter) = BootCounter::parse(&name);
|
||||
let (id, counter) = BootCounter::parse(stem);
|
||||
(id.to_string(), counter)
|
||||
};
|
||||
let name = id;
|
||||
@@ -169,60 +240,7 @@ pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Ve
|
||||
continue;
|
||||
}
|
||||
|
||||
// Produce a new sprout context for the entry with the extracted values.
|
||||
let mut context = context.fork();
|
||||
|
||||
let title_base = entry.title().unwrap_or_else(|| name.clone());
|
||||
let chainload = entry.chainload_path().unwrap_or_default();
|
||||
let options = entry.options().unwrap_or_default();
|
||||
let version = entry.version().unwrap_or_default();
|
||||
let machine_id = entry.machine_id().unwrap_or_default();
|
||||
|
||||
// Combine the title with the version if a version is present, except if it already contains it.
|
||||
// Sometimes BLS will have a version in the title already, and this makes it unique.
|
||||
let title_full = if !version.is_empty() && !title_base.contains(&version) {
|
||||
format!("{} {}", title_base, version)
|
||||
} else {
|
||||
title_base.clone()
|
||||
};
|
||||
|
||||
context.set("title-base", title_base);
|
||||
context.set("title", title_full);
|
||||
context.set("chainload", chainload);
|
||||
context.set("options", options);
|
||||
// The initrd value keeps the last initrd, which is what it held before
|
||||
// multiple initrds were supported.
|
||||
context.set("initrd", initrds.last().cloned().unwrap_or_default());
|
||||
|
||||
// Set every initrd slot, even the unused ones. An unset slot would let "$initrd-1"
|
||||
// match "$initrd" instead, producing the initrd value followed by "-1".
|
||||
for slot in 0..BLS_INITRD_SLOTS {
|
||||
context.set(
|
||||
format!("initrd-{}", slot),
|
||||
initrds.get(slot).cloned().unwrap_or_default(),
|
||||
);
|
||||
}
|
||||
|
||||
context.set("version", version);
|
||||
context.set("machine-id", machine_id);
|
||||
|
||||
// Keep the id for the boot counter, as the name is moved into the bootable entry.
|
||||
let boot_id = name.clone();
|
||||
|
||||
// Produce a new bootable entry.
|
||||
let mut boot = 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.
|
||||
if bls.pin_names {
|
||||
boot.mark_pin_name();
|
||||
}
|
||||
let mut boot = bootable_entry(context, bls, &name, &entry, &initrds);
|
||||
|
||||
// Record where the boot counter lives so a try can be consumed when this entry boots.
|
||||
if let Some(counter) = boot_counter {
|
||||
@@ -230,8 +248,9 @@ pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Ve
|
||||
counter,
|
||||
filesystem: bls_resolved.filesystem_handle,
|
||||
directory: entries_path.clone(),
|
||||
id: boot_id,
|
||||
id: name.clone(),
|
||||
file_name,
|
||||
extension,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -239,6 +258,120 @@ pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Ve
|
||||
entries.push((entry, boot));
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
}
|
||||
|
||||
/// Generates the BLS Type #2 entries from the unified kernel images in the `uki_path` directory.
|
||||
/// An image with the same id as one of the Type #1 `entries` is skipped.
|
||||
fn generate_type2(
|
||||
context: &Rc<SproutContext>,
|
||||
bls: &BlsConfiguration,
|
||||
uki_path: &str,
|
||||
entries: &[(BlsEntry, BootableEntry)],
|
||||
) -> Result<Vec<(BlsEntry, BootableEntry)>> {
|
||||
let resolved = eficore::path::resolve_path(Some(context.root().loaded_image_path()?), uki_path)
|
||||
.context("unable to resolve uki path")?;
|
||||
let directory = resolved
|
||||
.sub_path
|
||||
.to_string16(DisplayOnly(false), AllowShortcuts(false))
|
||||
.context("unable to convert uki path to string")?;
|
||||
let directory_name = directory.to_string();
|
||||
|
||||
let mut found: Vec<(BlsEntry, BootableEntry)> = Vec::new();
|
||||
for image in uki::scan(resolved.filesystem_handle, &directory_name)? {
|
||||
// Only unified kernel images are entries. Images built for another architecture, and
|
||||
// other EFI programs that have no os-release section, are skipped.
|
||||
if image.machine != PE_MACHINE {
|
||||
info!(
|
||||
"skipping {} as it is built for another architecture",
|
||||
image.file_name
|
||||
);
|
||||
continue;
|
||||
}
|
||||
if !image.has_osrel {
|
||||
info!(
|
||||
"skipping {} as it has no .osrel section, so it is not a unified kernel image",
|
||||
image.file_name
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
// The id is the file name without the extension and the boot counter.
|
||||
let file_name = image.file_name;
|
||||
let Some((stem, extension)) = strip_extension(&file_name, ".efi") else {
|
||||
continue;
|
||||
};
|
||||
let extension = extension.to_string();
|
||||
let (id, boot_counter) = BootCounter::parse(stem);
|
||||
if id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let id = id.to_string();
|
||||
|
||||
if entries
|
||||
.iter()
|
||||
.chain(found.iter())
|
||||
.any(|(_, boot)| boot.name() == id)
|
||||
{
|
||||
warn!(
|
||||
"unified kernel image {} has the same id as another entry, skipping",
|
||||
file_name
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut entry = image.entry;
|
||||
entry.boot_counter = boot_counter;
|
||||
|
||||
let mut boot = bootable_entry(context, bls, &id, &entry, &[]);
|
||||
if let Some(counter) = boot_counter {
|
||||
boot.set_boot_counter(BootCounterTarget {
|
||||
counter,
|
||||
filesystem: resolved.filesystem_handle,
|
||||
directory: PathBuf::from(directory.clone()),
|
||||
id,
|
||||
file_name,
|
||||
extension,
|
||||
});
|
||||
}
|
||||
found.push((entry, boot));
|
||||
}
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
/// Generates entries from the BLS entries directory and the unified kernel image directory
|
||||
/// using the specified `bls` configuration and `context`. The BLS conversion is best-effort
|
||||
/// and will ignore any unsupported entries.
|
||||
pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Vec<BootableEntry>> {
|
||||
// Stamp the path to the BLS directory.
|
||||
let path = context.stamp(&bls.path);
|
||||
|
||||
let uki_path = bls.uki_path_for(&path);
|
||||
|
||||
// A problem reading the Type #1 entries only stops the unified kernel images when there
|
||||
// are none to add, so that either kind of entry can still boot.
|
||||
let mut entries = match generate_type1(&context, bls, &path) {
|
||||
Ok(entries) => entries,
|
||||
Err(error) if uki_path.is_some() => {
|
||||
warn!("unable to generate bls entries: {:#}", error);
|
||||
Vec::new()
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
|
||||
// Add the unified kernel images, unless that is disabled. A problem scanning them
|
||||
// should not prevent booting the other entries.
|
||||
if let Some(uki_path) = uki_path {
|
||||
let uki_path = context.stamp(&uki_path);
|
||||
match generate_type2(&context, bls, &uki_path, &entries) {
|
||||
Ok(found) => entries.extend(found),
|
||||
Err(error) => warn!(
|
||||
"unable to generate unified kernel image entries: {:#}",
|
||||
error
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
// Sort all the entries according to the BLS sort system.
|
||||
entries.sort_by(sort_entries);
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
use alloc::{
|
||||
format,
|
||||
string::{String, ToString},
|
||||
vec::Vec,
|
||||
};
|
||||
use anyhow::{Context, Result, anyhow, bail};
|
||||
use edera_sprout_bls::{BlsEntry, PeImage, ReadAt, UKI_SECTIONS, read_pe, strip_extension};
|
||||
use log::warn;
|
||||
use uefi::{
|
||||
CString16, Handle, Status,
|
||||
proto::media::file::{File, FileAttribute, FileInfo, FileMode, FileType, RegularFile},
|
||||
proto::media::fs::SimpleFileSystem,
|
||||
};
|
||||
|
||||
/// A unified kernel image that was found, with the metadata Sprout reads.
|
||||
pub struct UkiFile {
|
||||
/// The name of the file, such as `fedora+3.efi`.
|
||||
pub file_name: String,
|
||||
/// The machine type of the image.
|
||||
pub machine: u16,
|
||||
/// Whether the image has an `.osrel` section, which unified kernel images always have.
|
||||
pub has_osrel: bool,
|
||||
/// The entry made from the sections of the image.
|
||||
pub entry: BlsEntry,
|
||||
}
|
||||
|
||||
/// Reads a file at arbitrary offsets, so that a large image isn't loaded to read its headers.
|
||||
struct FileReader(RegularFile);
|
||||
|
||||
impl ReadAt for FileReader {
|
||||
fn read_at(&mut self, offset: u64, buf: &mut [u8]) -> Result<()> {
|
||||
self.0
|
||||
.set_position(offset)
|
||||
.map_err(|error| anyhow!("unable to seek: {}", error))?;
|
||||
let mut done = 0;
|
||||
while done < buf.len() {
|
||||
let read = self
|
||||
.0
|
||||
.read(&mut buf[done..])
|
||||
.map_err(|error| anyhow!("unable to read: {}", error))?;
|
||||
if read == 0 {
|
||||
bail!("unexpected end of file");
|
||||
}
|
||||
done += read;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Finds the `.efi` files in `directory` on `filesystem` and reads their sections.
|
||||
/// A missing directory has no images. Files that can't be read as a PE image are skipped,
|
||||
/// as one broken file should not prevent booting any of the other entries.
|
||||
pub fn scan(filesystem: Handle, directory: &str) -> Result<Vec<UkiFile>> {
|
||||
let mut fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(filesystem)
|
||||
.context("unable to open the unified kernel image filesystem")?;
|
||||
let mut root = fs
|
||||
.open_volume()
|
||||
.context("unable to open the unified kernel image volume")?;
|
||||
|
||||
let name = CString16::try_from(directory).context("invalid unified kernel image path")?;
|
||||
let handle = match root.open(&name, FileMode::Read, FileAttribute::empty()) {
|
||||
Ok(handle) => handle,
|
||||
Err(error) if error.status() == Status::NOT_FOUND => return Ok(Vec::new()),
|
||||
Err(error) => return Err(error).context("unable to open the unified kernel image path"),
|
||||
};
|
||||
let FileType::Dir(mut directory_file) = handle
|
||||
.into_type()
|
||||
.context("unable to inspect the unified kernel image path")?
|
||||
else {
|
||||
bail!("the unified kernel image path is not a directory");
|
||||
};
|
||||
|
||||
// Collect the file names first, as files can't be opened while the directory is read.
|
||||
let mut names = Vec::new();
|
||||
while let Some(info) = directory_file
|
||||
.read_entry_boxed()
|
||||
.context("unable to read the unified kernel image directory")?
|
||||
{
|
||||
let info: &FileInfo = &info;
|
||||
if info.attribute().contains(FileAttribute::DIRECTORY) {
|
||||
continue;
|
||||
}
|
||||
let file_name = info.file_name().to_string();
|
||||
// Hidden files, such as the AppleDouble files that macOS writes, are not images.
|
||||
if !file_name.starts_with('.') && strip_extension(&file_name, ".efi").is_some() {
|
||||
names.push(file_name);
|
||||
}
|
||||
}
|
||||
|
||||
let mut found = Vec::new();
|
||||
for file_name in names {
|
||||
let path = format!("{}\\{}", directory, file_name);
|
||||
match read_image(&mut root, &path) {
|
||||
Ok(image) => found.push(UkiFile {
|
||||
file_name,
|
||||
machine: image.machine,
|
||||
has_osrel: image.sections.contains_key(".osrel"),
|
||||
// The sections are dropped here, so only the small entry is kept per image.
|
||||
entry: BlsEntry::from_uki(&image.sections, &path),
|
||||
}),
|
||||
Err(error) => warn!("unable to read unified kernel image {}: {:#}", path, error),
|
||||
}
|
||||
}
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
/// Reads the sections of the PE image at `path`.
|
||||
fn read_image(root: &mut uefi::proto::media::file::Directory, path: &str) -> Result<PeImage> {
|
||||
let name = CString16::try_from(path).context("invalid path")?;
|
||||
let handle = root
|
||||
.open(&name, FileMode::Read, FileAttribute::empty())
|
||||
.context("unable to open")?;
|
||||
let Some(file) = handle.into_regular_file() else {
|
||||
bail!("not a regular file");
|
||||
};
|
||||
read_pe(&mut FileReader(file), &UKI_SECTIONS)
|
||||
}
|
||||
Reference in new issue
Block a user