2026-03-25 15:56:02 -04:00
|
|
|
use crate::{actions, context::SproutContext};
|
2025-11-03 02:04:21 -05:00
|
|
|
use alloc::rc::Rc;
|
2026-03-25 15:56:02 -04:00
|
|
|
use alloc::string::String;
|
2025-11-03 02:04:21 -05:00
|
|
|
use alloc::{format, vec};
|
2025-11-02 23:28:31 -05:00
|
|
|
use anyhow::{Context, Result};
|
|
|
|
|
use edera_sprout_config::actions::chainload::ChainloadConfiguration;
|
|
|
|
|
use edera_sprout_config::actions::edera::EderaConfiguration;
|
2026-03-25 15:56:02 -04:00
|
|
|
use edera_sprout_parsing::{build_xen_config, combine_options, empty_is_none};
|
2025-11-03 20:47:21 -05:00
|
|
|
use eficore::media_loader::{
|
|
|
|
|
MediaLoaderHandle,
|
|
|
|
|
constants::xen::{
|
|
|
|
|
XEN_EFI_CONFIG_MEDIA_GUID, XEN_EFI_KERNEL_MEDIA_GUID, XEN_EFI_RAMDISK_MEDIA_GUID,
|
|
|
|
|
},
|
|
|
|
|
};
|
2025-11-02 23:28:31 -05:00
|
|
|
use uefi::Guid;
|
2025-10-14 22:36:22 -07:00
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Builds a configuration string for the Xen EFI stub using the specified `configuration`.
|
2026-03-25 15:56:02 -04:00
|
|
|
fn make_xen_config(context: Rc<SproutContext>, configuration: &EderaConfiguration) -> String {
|
|
|
|
|
let xen_options = combine_options(context.stamp_iter(configuration.xen_options.iter()));
|
|
|
|
|
let kernel_options = combine_options(context.stamp_iter(configuration.kernel_options.iter()));
|
|
|
|
|
build_xen_config(&xen_options, &kernel_options)
|
2025-10-14 22:36:22 -07:00
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Register a media loader for some `text` with the vendor `guid`.
|
|
|
|
|
/// `what` should indicate some identifying value for error messages
|
|
|
|
|
/// like `config` or `kernel`.
|
|
|
|
|
/// Provides a [MediaLoaderHandle] that can be used to unregister the media loader.
|
2025-10-14 22:36:22 -07:00
|
|
|
fn register_media_loader_text(guid: Guid, what: &str, text: String) -> Result<MediaLoaderHandle> {
|
|
|
|
|
MediaLoaderHandle::register(guid, text.as_bytes().to_vec().into_boxed_slice())
|
|
|
|
|
.context(format!("unable to register {} media loader", what)) /* */
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Register a media loader for the file `path` with the vendor `guid`.
|
|
|
|
|
/// `what` should indicate some identifying value for error messages
|
|
|
|
|
/// like `config` or `kernel`.
|
|
|
|
|
/// Provides a [MediaLoaderHandle] that can be used to unregister the media loader.
|
2025-10-14 22:36:22 -07:00
|
|
|
fn register_media_loader_file(
|
|
|
|
|
context: &Rc<SproutContext>,
|
|
|
|
|
guid: Guid,
|
|
|
|
|
what: &str,
|
|
|
|
|
path: &str,
|
|
|
|
|
) -> Result<MediaLoaderHandle> {
|
2025-10-20 00:06:46 -07:00
|
|
|
// Stamp the path to the file.
|
2025-10-14 22:36:22 -07:00
|
|
|
let path = context.stamp(path);
|
2025-10-20 00:06:46 -07:00
|
|
|
// Read the file contents.
|
2025-11-03 20:47:21 -05:00
|
|
|
let content =
|
|
|
|
|
eficore::path::read_file_contents(Some(context.root().loaded_image_path()?), &path)
|
|
|
|
|
.context(format!("unable to read {} file", what))?;
|
2025-10-20 00:06:46 -07:00
|
|
|
// Register the media loader.
|
2025-10-14 22:36:22 -07:00
|
|
|
let handle = MediaLoaderHandle::register(guid, content.into_boxed_slice())
|
|
|
|
|
.context(format!("unable to register {} media loader", what))?;
|
|
|
|
|
Ok(handle)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
/// Executes the edera action which will boot the Edera hypervisor with the specified
|
|
|
|
|
/// `configuration` and `context`. This action uses Edera-specific Xen EFI stub functionality.
|
2025-10-14 22:36:22 -07:00
|
|
|
pub fn edera(context: Rc<SproutContext>, configuration: &EderaConfiguration) -> Result<()> {
|
2025-10-20 00:06:46 -07:00
|
|
|
// Build the Xen config file content for this configuration.
|
2026-03-25 15:56:02 -04:00
|
|
|
let config = make_xen_config(context.clone(), configuration);
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Register the media loader for the config.
|
2025-10-18 23:26:05 -07:00
|
|
|
let config = register_media_loader_text(XEN_EFI_CONFIG_MEDIA_GUID, "config", config)
|
|
|
|
|
.context("unable to register config media loader")?;
|
2025-10-20 00:06:46 -07:00
|
|
|
|
|
|
|
|
// Register the media loaders for the kernel.
|
2025-10-14 22:36:22 -07:00
|
|
|
let kernel = register_media_loader_file(
|
|
|
|
|
&context,
|
|
|
|
|
XEN_EFI_KERNEL_MEDIA_GUID,
|
|
|
|
|
"kernel",
|
|
|
|
|
&configuration.kernel,
|
2025-10-18 23:26:05 -07:00
|
|
|
)
|
|
|
|
|
.context("unable to register kernel media loader")?;
|
2025-10-14 22:36:22 -07:00
|
|
|
|
2025-12-20 22:01:04 -08:00
|
|
|
// Create a vector of media loaders to drop them only after this function completes.
|
2025-10-14 22:36:22 -07:00
|
|
|
let mut media_loaders = vec![config, kernel];
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Register the initrd if it is provided.
|
2026-03-25 15:56:02 -04:00
|
|
|
if let Some(initrd) = empty_is_none(configuration.initrd.as_ref()) {
|
2025-10-14 22:36:22 -07:00
|
|
|
let initrd =
|
2025-10-18 23:26:05 -07:00
|
|
|
register_media_loader_file(&context, XEN_EFI_RAMDISK_MEDIA_GUID, "initrd", initrd)
|
|
|
|
|
.context("unable to register initrd media loader")?;
|
2025-10-14 22:36:22 -07:00
|
|
|
media_loaders.push(initrd);
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 00:06:46 -07:00
|
|
|
// Chainload to the Xen EFI stub.
|
2025-10-14 22:36:22 -07:00
|
|
|
let result = actions::chainload::chainload(
|
|
|
|
|
context.clone(),
|
|
|
|
|
&ChainloadConfiguration {
|
|
|
|
|
path: configuration.xen.clone(),
|
|
|
|
|
options: vec![],
|
|
|
|
|
linux_initrd: None,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.context("unable to chainload to xen");
|
|
|
|
|
|
2025-12-20 22:01:04 -08:00
|
|
|
// Explicitly drop the media loaders to clarify when they should be unregistered.
|
|
|
|
|
drop(media_loaders);
|
2025-10-14 22:36:22 -07:00
|
|
|
|
|
|
|
|
result
|
|
|
|
|
}
|