Skip to content

Commit

Permalink
protocol: add {set,get}_current_image
Browse files Browse the repository at this point in the history
Some EFI functions (namely, AllocatePool) need different arguments
depending on the currently loaded image. Exposing this API detail to
Rust consumers is unnecessary, so keep a LoadedImageProtocol struct
around representing the currently loaded image.
  • Loading branch information
csssuf committed Jun 10, 2017
1 parent ba36774 commit 8904d88
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn status_str() {
}

/// Type for EFI_MEMORY_TYPE
#[derive(PartialEq, PartialOrd, Debug)]
#[derive(PartialEq, PartialOrd, Debug, Clone, Copy)]
#[repr(C)]
pub enum MemoryType {
Reserved = 0,
Expand Down
25 changes: 23 additions & 2 deletions src/protocol/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use base::{Handle, MemoryType};
use base::{Handle, MemoryType, Status};
use guid::Guid;
use void::NotYetDef;

Expand All @@ -13,6 +13,8 @@ pub trait Protocol {
/// GUID for UEFI protocol for loaded images
pub static EFI_LOADED_IMAGE_PROTOCOL_GUID: Guid = Guid(0x5B1B31A1, 0x9562, 0x11d2, [0x8E,0x3F,0x00,0xA0,0xC9,0x69,0x72,0x3B]);

static mut THIS_LOADED_IMAGE: *const LoadedImageProtocol = 0 as *const LoadedImageProtocol;

#[derive(Debug)]
#[repr(C)]
pub struct LoadedImageProtocol {
Expand All @@ -27,7 +29,7 @@ pub struct LoadedImageProtocol {
pub image_base: usize,
pub image_size: u64,
image_code_type: MemoryType,
image_data_type: MemoryType,
pub image_data_type: MemoryType,

//unload: unsafe extern "win64" fn(handle: ::base::Handle),
unload: *const NotYetDef,
Expand All @@ -39,3 +41,22 @@ impl Protocol for LoadedImageProtocol {
}
}

pub fn set_current_image(handle: Handle) -> Result<&'static LoadedImageProtocol, Status> {
let st = ::get_system_table();

let loaded_image_proto: Result<&'static LoadedImageProtocol, Status> = st.boot_services().handle_protocol(handle);
if let Ok(image) = loaded_image_proto {
unsafe {
THIS_LOADED_IMAGE = image;
}
}

loaded_image_proto
}

pub fn get_current_image() -> &'static LoadedImageProtocol {
unsafe {
&*THIS_LOADED_IMAGE
}
}

0 comments on commit 8904d88

Please sign in to comment.