linux_bootloader/
uefi_helpers.rs

1use core::ffi::c_void;
2
3use uefi::{prelude::BootServices, proto::loaded_image::LoadedImage, Result};
4
5#[derive(Debug, Clone, Copy)]
6pub struct PeInMemory {
7    image_base: *const c_void,
8    image_size: usize,
9}
10
11impl PeInMemory {
12    /// Return a reference to the currently running image.
13    ///
14    /// # Safety
15    ///
16    /// The returned slice covers the whole loaded image in which we
17    /// currently execute. This means the safety guarantees of
18    /// [`core::slice::from_raw_parts`] that we use in this function
19    /// are only guaranteed, if we we don't mutate anything in this
20    /// range. This means no modification of global variables or
21    /// anything.
22    pub unsafe fn as_slice(&self) -> &'static [u8] {
23        unsafe { core::slice::from_raw_parts(self.image_base as *const u8, self.image_size) }
24    }
25}
26
27/// Open the currently executing image as a file.
28pub fn booted_image_file(boot_services: &BootServices) -> Result<PeInMemory> {
29    let loaded_image =
30        boot_services.open_protocol_exclusive::<LoadedImage>(boot_services.image_handle())?;
31    let (image_base, image_size) = loaded_image.info();
32
33    Ok(PeInMemory {
34        image_base,
35        image_size: usize::try_from(image_size).map_err(|_| uefi::Status::INVALID_PARAMETER)?,
36    })
37}