2025-10-01 18:25:49 -07:00
|
|
|
use crate::config::ChainloaderConfiguration;
|
2025-10-02 00:24:19 -07:00
|
|
|
use crate::utils;
|
2025-10-01 19:15:42 -07:00
|
|
|
use log::info;
|
2025-10-02 00:24:19 -07:00
|
|
|
use uefi::CString16;
|
|
|
|
|
use uefi::proto::device_path::LoadedImageDevicePath;
|
2025-10-01 21:30:43 -07:00
|
|
|
use uefi::proto::loaded_image::LoadedImage;
|
2025-10-01 16:45:04 -07:00
|
|
|
|
2025-10-01 18:25:49 -07:00
|
|
|
pub fn chainloader(configuration: ChainloaderConfiguration) {
|
2025-10-01 16:45:04 -07:00
|
|
|
let sprout_image = uefi::boot::image_handle();
|
|
|
|
|
let image_device_path_protocol =
|
|
|
|
|
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(sprout_image)
|
2025-10-01 19:15:42 -07:00
|
|
|
.expect("unable to open loaded image device path protocol");
|
2025-10-01 16:45:04 -07:00
|
|
|
|
2025-10-02 00:24:19 -07:00
|
|
|
let mut full_path = utils::device_path_root(&image_device_path_protocol);
|
2025-10-01 18:25:49 -07:00
|
|
|
full_path.push_str(&configuration.path);
|
2025-10-01 16:45:04 -07:00
|
|
|
|
2025-10-01 19:15:42 -07:00
|
|
|
info!("path={}", full_path);
|
2025-10-01 16:45:04 -07:00
|
|
|
|
2025-10-02 00:24:19 -07:00
|
|
|
let device_path = utils::text_to_device_path(&full_path);
|
2025-10-01 16:45:04 -07:00
|
|
|
|
|
|
|
|
let image = uefi::boot::load_image(
|
|
|
|
|
sprout_image,
|
|
|
|
|
uefi::boot::LoadImageSource::FromDevicePath {
|
|
|
|
|
device_path: &device_path,
|
|
|
|
|
boot_policy: uefi::proto::BootPolicy::ExactMatch,
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
.expect("failed to load image");
|
2025-10-01 19:15:42 -07:00
|
|
|
|
2025-10-02 00:24:19 -07:00
|
|
|
let mut loaded_image_protocol = uefi::boot::open_protocol_exclusive::<LoadedImage>(image)
|
2025-10-01 19:15:42 -07:00
|
|
|
.expect("unable to open loaded image protocol");
|
|
|
|
|
|
2025-10-02 00:24:19 -07:00
|
|
|
let options = configuration.options.join(" ");
|
|
|
|
|
if !options.is_empty() {
|
|
|
|
|
let options = Box::new(
|
|
|
|
|
CString16::try_from(&options[..])
|
|
|
|
|
.expect("unable to convert chainloader options to CString16"),
|
|
|
|
|
);
|
|
|
|
|
info!("options={}", options);
|
|
|
|
|
|
|
|
|
|
if options.num_bytes() > u32::MAX as usize {
|
|
|
|
|
panic!("chainloader options too large");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SAFETY: options size is checked to validate it is safe to pass.
|
|
|
|
|
// Additionally, the pointer is allocated and retained on the heap which makes
|
|
|
|
|
// passing the options pointer safe to the next image.
|
|
|
|
|
unsafe {
|
|
|
|
|
loaded_image_protocol
|
|
|
|
|
.set_load_options(options.as_ptr() as *const u8, options.num_bytes() as u32);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (base, size) = loaded_image_protocol.info();
|
2025-10-01 19:15:42 -07:00
|
|
|
info!("loaded image base={:#x} size={:#x}", base.addr(), size);
|
2025-10-01 16:45:04 -07:00
|
|
|
uefi::boot::start_image(image).expect("failed to start image");
|
|
|
|
|
}
|