Skip to content

Commit 4b70566

Browse files
authored
Merge pull request #8 from csssuf/allocate-pool
Add allocate_pool function
2 parents ba36774 + 877e6b7 commit 4b70566

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn status_str() {
153153
}
154154

155155
/// Type for EFI_MEMORY_TYPE
156-
#[derive(PartialEq, PartialOrd, Debug)]
156+
#[derive(PartialEq, PartialOrd, Debug, Clone, Copy)]
157157
#[repr(C)]
158158
pub enum MemoryType {
159159
Reserved = 0,

src/bootservices.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use void::{NotYetDef, CVoid};
55
use base::{Event, Handle, Handles, MemoryType, Status};
66
use event::{EventType, EventNotify, TimerDelay};
77
use task::TPL;
8-
use protocol::{DevicePathProtocol, Protocol};
8+
use protocol::{DevicePathProtocol, Protocol, get_current_image};
99
use guid;
1010
use table;
1111

@@ -68,6 +68,17 @@ pub struct BootServices {
6868
}
6969

7070
impl BootServices {
71+
/// Allocate `size` bytes of memory using type `T`.
72+
pub fn allocate_pool<T>(&self, size: usize) -> Result<*mut T, Status> {
73+
let mut ptr: *mut u8 = 0 as *mut u8;
74+
75+
let result = unsafe { (self.allocate_pool)(get_current_image().image_data_type, size, &mut ptr) };
76+
if result != Status::Success {
77+
return Err(result);
78+
}
79+
Ok(ptr as *mut T)
80+
}
81+
7182
pub fn free_pool<T>(&self, p: *const T) {
7283
unsafe {
7384
(self.free_pool)(p as *mut CVoid);

src/protocol/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use base::{Handle, MemoryType};
1+
use base::{Handle, MemoryType, Status};
22
use guid::Guid;
33
use void::NotYetDef;
44

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

16+
static mut THIS_LOADED_IMAGE: *const LoadedImageProtocol = 0 as *const LoadedImageProtocol;
17+
1618
#[derive(Debug)]
1719
#[repr(C)]
1820
pub struct LoadedImageProtocol {
@@ -27,7 +29,7 @@ pub struct LoadedImageProtocol {
2729
pub image_base: usize,
2830
pub image_size: u64,
2931
image_code_type: MemoryType,
30-
image_data_type: MemoryType,
32+
pub image_data_type: MemoryType,
3133

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

44+
pub fn set_current_image(handle: Handle) -> Result<&'static LoadedImageProtocol, Status> {
45+
let st = ::get_system_table();
46+
47+
let loaded_image_proto: Result<&'static LoadedImageProtocol, Status> = st.boot_services().handle_protocol(handle);
48+
if let Ok(image) = loaded_image_proto {
49+
unsafe {
50+
THIS_LOADED_IMAGE = image;
51+
}
52+
}
53+
54+
loaded_image_proto
55+
}
56+
57+
pub fn get_current_image() -> &'static LoadedImageProtocol {
58+
unsafe {
59+
&*THIS_LOADED_IMAGE
60+
}
61+
}
62+

0 commit comments

Comments
 (0)