mirror of
https://github.com/edera-dev/sprout.git
synced 2026-11-05 00:12:20 +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
@@ -1,12 +1,18 @@
|
||||
use alloc::{format, string::String};
|
||||
use anyhow::{Context, Result};
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use edera_sprout_bls::BootCounter;
|
||||
use uefi::{
|
||||
CString16, Handle,
|
||||
fs::{FileSystem, PathBuf},
|
||||
data_types::Align,
|
||||
fs::PathBuf,
|
||||
mem::AlignedBuffer,
|
||||
proto::media::file::{File, FileAttribute, FileInfo, FileMode},
|
||||
proto::media::fs::SimpleFileSystem,
|
||||
};
|
||||
|
||||
/// The size of the fixed part of a file info: three sizes, three timestamps and the attributes.
|
||||
const FILE_INFO_HEADER_SIZE: usize = 3 * 8 + 3 * 16 + 8;
|
||||
|
||||
/// Where the boot counter of an entry is stored, so that a try can be consumed when the
|
||||
/// entry is booted.
|
||||
#[derive(Clone)]
|
||||
@@ -21,34 +27,71 @@ pub struct BootCounterTarget {
|
||||
pub id: String,
|
||||
/// The current file name, including the counter and the extension.
|
||||
pub file_name: String,
|
||||
/// The extension of the file name, such as `.conf`, in its original case.
|
||||
pub extension: String,
|
||||
}
|
||||
|
||||
impl BootCounterTarget {
|
||||
/// Consumes one try by renaming the entry file, such as `foo+3.conf` to `foo+2-1.conf`.
|
||||
/// The file is renamed in place without copying it, as unified kernel images are large and
|
||||
/// a copy interrupted by a crash would leave a truncated image behind.
|
||||
/// Returns the path of the renamed file.
|
||||
pub fn consume(&self) -> Result<PathBuf> {
|
||||
// The extension is taken from the current file name, as it may be in any case.
|
||||
let extension = &self.file_name[self.file_name.len() - ".conf".len()..];
|
||||
let new_name = format!(
|
||||
"{}{}",
|
||||
self.counter.decremented().render(&self.id),
|
||||
extension
|
||||
self.extension
|
||||
);
|
||||
let new_name_16 =
|
||||
CString16::try_from(new_name.as_str()).context("invalid new entry file name")?;
|
||||
|
||||
let mut fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(self.filesystem)
|
||||
.context("unable to open the entry filesystem")?;
|
||||
let mut root = fs
|
||||
.open_volume()
|
||||
.context("unable to open the entry volume")?;
|
||||
|
||||
// Open the entry file for writing.
|
||||
let mut from = self.directory.clone();
|
||||
from.push(PathBuf::from(
|
||||
CString16::try_from(self.file_name.as_str()).context("invalid entry file name")?,
|
||||
));
|
||||
let mut to = self.directory.clone();
|
||||
to.push(PathBuf::from(
|
||||
CString16::try_from(new_name.as_str()).context("invalid new entry file name")?,
|
||||
));
|
||||
let from_name =
|
||||
CString16::try_from(format!("{}", from).as_str()).context("invalid entry file path")?;
|
||||
let handle = root
|
||||
.open(&from_name, FileMode::ReadWrite, FileAttribute::empty())
|
||||
.context("unable to open the entry file")?;
|
||||
let mut file = handle
|
||||
.into_regular_file()
|
||||
.ok_or_else(|| anyhow!("the entry is not a regular file"))?;
|
||||
|
||||
let fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(self.filesystem)
|
||||
.context("unable to open the entry filesystem")?;
|
||||
let mut fs = FileSystem::new(fs);
|
||||
fs.rename(&from, &to)
|
||||
// Renaming a file is done by setting its file info with a different name.
|
||||
let info = file
|
||||
.get_boxed_info::<FileInfo>()
|
||||
.context("unable to get the entry file info")?;
|
||||
// The info is a fixed header followed by the null-terminated UCS-2 name.
|
||||
let size =
|
||||
FILE_INFO_HEADER_SIZE + 2 * (new_name.len() + 1) + <FileInfo as Align>::alignment();
|
||||
let mut storage = AlignedBuffer::from_size_align(size, <FileInfo as Align>::alignment())
|
||||
.context("unable to allocate the entry file info")?;
|
||||
let renamed = FileInfo::new(
|
||||
storage.as_slice_mut(),
|
||||
info.file_size(),
|
||||
info.physical_size(),
|
||||
*info.create_time(),
|
||||
*info.last_access_time(),
|
||||
*info.modification_time(),
|
||||
info.attribute(),
|
||||
&new_name_16,
|
||||
)
|
||||
.map_err(|error| anyhow!("unable to build the renamed entry file info: {:?}", error))?;
|
||||
file.set_info(renamed)
|
||||
.context("unable to rename the entry file")?;
|
||||
file.flush()
|
||||
.context("unable to flush the renamed entry file")?;
|
||||
|
||||
let mut to = self.directory.clone();
|
||||
to.push(PathBuf::from(new_name_16));
|
||||
Ok(to)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user