2025-10-11 14:35:29 -07:00
|
|
|
use anyhow::{Context, Result};
|
2025-10-01 16:45:04 -07:00
|
|
|
use std::os::uefi as uefi_std;
|
|
|
|
|
|
2025-10-14 12:47:33 -07:00
|
|
|
/// Initializes the UEFI environment.
|
|
|
|
|
///
|
|
|
|
|
/// This fetches the system table and current image handle from uefi_std and injects
|
|
|
|
|
/// them into the uefi crate.
|
2025-10-11 14:35:29 -07:00
|
|
|
pub fn init() -> Result<()> {
|
2025-10-19 23:03:28 -07:00
|
|
|
// Acquire the system table and image handle from the uefi_std environment.
|
2025-10-01 16:45:04 -07:00
|
|
|
let system_table = uefi_std::env::system_table();
|
|
|
|
|
let image_handle = uefi_std::env::image_handle();
|
|
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// SAFETY: The UEFI variables above come from the Rust std.
|
|
|
|
|
// These variables are not-null and calling the uefi crates with these values is validated
|
2025-10-01 16:45:04 -07:00
|
|
|
// to be corrected by hand.
|
|
|
|
|
unsafe {
|
2025-10-19 23:03:28 -07:00
|
|
|
// Set the system table and image handle.
|
2025-10-01 16:45:04 -07:00
|
|
|
uefi::table::set_system_table(system_table.as_ptr().cast());
|
|
|
|
|
let handle = uefi::Handle::from_ptr(image_handle.as_ptr().cast())
|
2025-10-11 14:35:29 -07:00
|
|
|
.context("unable to resolve image handle")?;
|
2025-10-01 16:45:04 -07:00
|
|
|
uefi::boot::set_image_handle(handle);
|
|
|
|
|
}
|
2025-10-01 19:15:42 -07:00
|
|
|
|
2025-10-19 23:03:28 -07:00
|
|
|
// Initialize the uefi logger mechanism and other helpers.
|
2025-10-14 12:47:33 -07:00
|
|
|
uefi::helpers::init().context("unable to initialize uefi")?;
|
2025-10-11 14:35:29 -07:00
|
|
|
Ok(())
|
2025-10-01 16:45:04 -07:00
|
|
|
}
|