2025-10-30 02:36:14 -04:00
|
|
|
use crate::platform::timer::PlatformTimer;
|
2025-10-30 12:44:07 -04:00
|
|
|
use crate::utils::device_path_subpath;
|
2025-10-28 23:23:12 -04:00
|
|
|
use anyhow::{Context, Result};
|
2025-10-30 12:44:07 -04:00
|
|
|
use uefi::proto::device_path::DevicePath;
|
2025-10-28 23:23:12 -04:00
|
|
|
use uefi::{CString16, Guid, guid};
|
|
|
|
|
use uefi_raw::table::runtime::{VariableAttributes, VariableVendor};
|
2025-10-28 21:05:22 -04:00
|
|
|
|
2025-10-30 12:50:36 -04:00
|
|
|
/// The name of the bootloader to tell the system.
|
|
|
|
|
const LOADER_NAME: &str = "Sprout";
|
|
|
|
|
|
2025-10-28 21:05:22 -04:00
|
|
|
/// Bootloader Interface support.
|
|
|
|
|
pub struct BootloaderInterface;
|
|
|
|
|
|
|
|
|
|
impl BootloaderInterface {
|
2025-10-28 23:23:12 -04:00
|
|
|
/// Bootloader Interface GUID from https://systemd.io/BOOT_LOADER_INTERFACE
|
|
|
|
|
const VENDOR: VariableVendor = VariableVendor(guid!("4a67b082-0a4c-41cf-b6c7-440b29bb8c4f"));
|
|
|
|
|
|
2025-10-28 21:05:22 -04:00
|
|
|
/// Tell the system that Sprout was initialized at the current time.
|
2025-10-30 02:51:52 -04:00
|
|
|
pub fn mark_init(timer: &PlatformTimer) -> Result<()> {
|
|
|
|
|
Self::mark_time("LoaderTimeInitUSec", timer)
|
2025-10-28 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tell the system that Sprout is about to execute the boot entry.
|
2025-10-30 02:36:14 -04:00
|
|
|
pub fn mark_exec(timer: &PlatformTimer) -> Result<()> {
|
2025-10-30 02:51:52 -04:00
|
|
|
Self::mark_time("LoaderTimeExecUSec", timer)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-30 13:27:58 -04:00
|
|
|
/// Tell the system that Sprout is about to display the menu.
|
|
|
|
|
pub fn mark_menu(timer: &PlatformTimer) -> Result<()> {
|
|
|
|
|
Self::mark_time("LoaderTimeMenuUsec", timer)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-30 02:51:52 -04:00
|
|
|
/// Tell the system about the current time as measured by the platform timer.
|
|
|
|
|
/// Sets the variable specified by `key` to the number of microseconds.
|
|
|
|
|
fn mark_time(key: &str, timer: &PlatformTimer) -> Result<()> {
|
|
|
|
|
// Measure the elapsed time since the hardware timer was started.
|
|
|
|
|
let elapsed = timer.elapsed_since_lifetime();
|
|
|
|
|
Self::set_cstr16(key, &elapsed.as_micros().to_string())
|
2025-10-28 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
2025-10-30 12:50:36 -04:00
|
|
|
/// Tell the system what loader is being used.
|
|
|
|
|
pub fn set_loader_info() -> Result<()> {
|
|
|
|
|
Self::set_cstr16("LoaderInfo", LOADER_NAME)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-30 12:44:07 -04:00
|
|
|
/// Tell the system the relative path to the partition root of the current bootloader.
|
|
|
|
|
pub fn set_loader_path(path: &DevicePath) -> Result<()> {
|
|
|
|
|
let subpath = device_path_subpath(path).context("unable to get loader path subpath")?;
|
|
|
|
|
Self::set_cstr16("LoaderImageIdentifier", &subpath)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-28 21:05:22 -04:00
|
|
|
/// Tell the system what the partition GUID of the ESP Sprout was booted from is.
|
2025-10-28 23:23:12 -04:00
|
|
|
pub fn set_partition_guid(guid: &Guid) -> Result<()> {
|
|
|
|
|
Self::set_cstr16("LoaderDevicePartUUID", &guid.to_string())
|
2025-10-28 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tell the system what boot entries are available.
|
2025-10-28 23:23:12 -04:00
|
|
|
pub fn set_entries<N: AsRef<str>>(entries: impl Iterator<Item = N>) -> Result<()> {
|
|
|
|
|
// Entries are stored as a null-terminated list of CString16 strings back to back.
|
|
|
|
|
// Iterate over the entries and convert them to CString16 placing them into data.
|
|
|
|
|
let mut data = Vec::new();
|
|
|
|
|
for entry in entries {
|
2025-10-30 12:44:07 -04:00
|
|
|
// Convert the entry to CString16 little endian.
|
|
|
|
|
let encoded = entry
|
|
|
|
|
.as_ref()
|
|
|
|
|
.encode_utf16()
|
|
|
|
|
.flat_map(|c| c.to_le_bytes())
|
|
|
|
|
.collect::<Vec<u8>>();
|
2025-10-28 23:23:12 -04:00
|
|
|
// Write the bytes (including the null terminator) into the data buffer.
|
2025-10-30 12:44:07 -04:00
|
|
|
data.extend_from_slice(&encoded);
|
2025-10-28 23:23:12 -04:00
|
|
|
}
|
|
|
|
|
Self::set("LoaderEntries", &data)
|
2025-10-28 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tell the system what the default boot entry is.
|
2025-10-28 23:23:12 -04:00
|
|
|
pub fn set_default_entry(entry: String) -> Result<()> {
|
|
|
|
|
Self::set_cstr16("LoaderEntryDefault", &entry)
|
2025-10-28 21:05:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Tell the system what the selected boot entry is.
|
2025-10-28 23:23:12 -04:00
|
|
|
pub fn set_selected_entry(entry: String) -> Result<()> {
|
|
|
|
|
Self::set_cstr16("LoaderEntrySelected", &entry)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-30 11:47:35 -04:00
|
|
|
/// Tell the system about the UEFI firmware we are running on.
|
|
|
|
|
pub fn set_firmware_info() -> Result<()> {
|
|
|
|
|
// Format the firmware information string into something human-readable.
|
|
|
|
|
let firmware_info = format!(
|
|
|
|
|
"{} {}.{:02}",
|
|
|
|
|
uefi::system::firmware_vendor(),
|
|
|
|
|
uefi::system::firmware_revision() >> 16,
|
|
|
|
|
uefi::system::firmware_revision() & 0xFFFFF,
|
|
|
|
|
);
|
|
|
|
|
Self::set_cstr16("LoaderFirmwareInfo", &firmware_info)?;
|
|
|
|
|
|
|
|
|
|
// Format the firmware revision into something human-readable.
|
|
|
|
|
let firmware_type = format!("UEFI {:02}", uefi::system::firmware_revision());
|
|
|
|
|
Self::set_cstr16("LoaderFirmwareType", &firmware_type)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-28 23:23:12 -04:00
|
|
|
/// The [VariableAttributes] for bootloader interface variables.
|
|
|
|
|
fn attributes() -> VariableAttributes {
|
|
|
|
|
VariableAttributes::BOOTSERVICE_ACCESS | VariableAttributes::RUNTIME_ACCESS
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Set a bootloader interface variable specified by `key` to `value`.
|
|
|
|
|
fn set(key: &str, value: &[u8]) -> Result<()> {
|
|
|
|
|
let name =
|
|
|
|
|
CString16::try_from(key).context("unable to convert variable name to CString16")?;
|
|
|
|
|
uefi::runtime::set_variable(&name, &Self::VENDOR, Self::attributes(), value)
|
|
|
|
|
.with_context(|| format!("unable to set efi variable {}", key))?;
|
2025-10-28 21:05:22 -04:00
|
|
|
Ok(())
|
|
|
|
|
}
|
2025-10-28 23:23:12 -04:00
|
|
|
|
|
|
|
|
/// Set a bootloader interface variable specified by `key` to `value`, converting the value to
|
|
|
|
|
/// a [CString16].
|
|
|
|
|
fn set_cstr16(key: &str, value: &str) -> Result<()> {
|
2025-10-30 12:44:07 -04:00
|
|
|
// Encode the value as a CString16 little endian.
|
|
|
|
|
let encoded = value
|
|
|
|
|
.encode_utf16()
|
|
|
|
|
.flat_map(|c| c.to_le_bytes())
|
|
|
|
|
.collect::<Vec<u8>>();
|
|
|
|
|
Self::set(key, &encoded)
|
2025-10-28 23:23:12 -04:00
|
|
|
}
|
2025-10-28 21:05:22 -04:00
|
|
|
}
|