Skip to content

Commit 14149aa

Browse files
Steven Xunicholasbishop
authored andcommitted
Change memory_map_size to return entry size as well
1 parent bc21bb9 commit 14149aa

File tree

3 files changed

+28
-18
lines changed

3 files changed

+28
-18
lines changed

src/table/boot.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,13 @@ impl BootServices {
218218
(self.free_pages)(addr, count).into()
219219
}
220220

221-
/// Retrieves the size, in bytes, of the current memory map.
221+
/// Returns struct which contains the size of a single memory descriptor
222+
/// as well as the size of the current memory map.
222223
///
223-
/// A buffer of this size will be capable of holding the whole current memory map,
224-
/// including padding. Note, however, that allocations will increase the size of the
225-
/// memory map, therefore it is better to allocate some extra space.
226-
pub fn memory_map_size(&self) -> usize {
224+
/// Note that the size of the memory map can increase any time an allocation happens,
225+
/// so when creating a buffer to put the memory map into, it's recommended to allocate a few extra
226+
/// elements worth of space above the size of the current memory map.
227+
pub fn memory_map_size(&self) -> MemoryMapSize {
227228
let mut map_size = 0;
228229
let mut map_key = MemoryMapKey(0);
229230
let mut entry_size = 0;
@@ -240,7 +241,10 @@ impl BootServices {
240241
};
241242
assert_eq!(status, Status::BUFFER_TOO_SMALL);
242243

243-
map_size
244+
MemoryMapSize {
245+
entry_size,
246+
map_size,
247+
}
244248
}
245249

246250
/// Retrieves the current memory map.
@@ -1297,6 +1301,14 @@ bitflags! {
12971301
#[repr(C)]
12981302
pub struct MemoryMapKey(usize);
12991303

1304+
/// A structure containing the size of a memory descriptor and the size of the memory map
1305+
pub struct MemoryMapSize {
1306+
/// Size of a single memory descriptor in bytes
1307+
pub entry_size: usize,
1308+
/// Size of the entire memory map in bytes
1309+
pub map_size: usize,
1310+
}
1311+
13001312
/// An iterator of memory descriptors
13011313
#[derive(Debug, Clone)]
13021314
struct MemoryMapIter<'buf> {

uefi-test-runner/src/boot/memory.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use uefi::prelude::*;
2-
use uefi::table::boot::{AllocateType, BootServices, MemoryDescriptor, MemoryType};
2+
use uefi::table::boot::{AllocateType, BootServices, MemoryType};
33

44
use crate::alloc::vec::Vec;
5-
use core::mem;
65

76
pub fn test(bt: &BootServices) {
87
info!("Testing memory functions");
@@ -26,7 +25,7 @@ fn allocate_pages(bt: &BootServices) {
2625

2726
assert_eq!(pgs % 4096, 0, "Page pointer is not page-aligned");
2827

29-
// Reinterprete the page as an array of bytes
28+
// Reinterpret the page as an array of bytes
3029
let buf = unsafe { &mut *(pgs as *mut [u8; 4096]) };
3130

3231
// If these don't fail then we properly allocated some memory.
@@ -84,13 +83,13 @@ fn memmove(bt: &BootServices) {
8483
fn memory_map(bt: &BootServices) {
8584
info!("Testing memory map functions");
8685

87-
// Get an estimate of the memory map size.
88-
let map_sz = bt.memory_map_size();
86+
// Get the memory descriptor size and an estimate of the memory map size
87+
let sizes = bt.memory_map_size();
8988

90-
// 8 extra descriptors should be enough.
91-
let buf_sz = map_sz + 8 * mem::size_of::<MemoryDescriptor>();
89+
// 2 extra descriptors should be enough.
90+
let buf_sz = sizes.map_size + 2 * sizes.entry_size;
9291

93-
// We will use vectors for convencience.
92+
// We will use vectors for convenience.
9493
let mut buffer = vec![0_u8; buf_sz];
9594

9695
let (_key, desc_iter) = bt

uefi-test-runner/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ extern crate log;
88
extern crate alloc;
99

1010
use alloc::string::String;
11-
use core::mem;
1211
use uefi::prelude::*;
1312
use uefi::proto::console::serial::Serial;
14-
use uefi::table::boot::{MemoryDescriptor, OpenProtocolAttributes, OpenProtocolParams};
13+
use uefi::table::boot::{OpenProtocolAttributes, OpenProtocolParams};
1514

1615
mod boot;
1716
mod proto;
@@ -141,8 +140,8 @@ fn shutdown(image: uefi::Handle, mut st: SystemTable<Boot>) -> ! {
141140
}
142141

143142
// Exit boot services as a proof that it works :)
144-
let max_mmap_size =
145-
st.boot_services().memory_map_size() + 8 * mem::size_of::<MemoryDescriptor>();
143+
let sizes = st.boot_services().memory_map_size();
144+
let max_mmap_size = sizes.map_size + 2 * sizes.entry_size;
146145
let mut mmap_storage = vec![0; max_mmap_size].into_boxed_slice();
147146
let (st, _iter) = st
148147
.exit_boot_services(image, &mut mmap_storage[..])

0 commit comments

Comments
 (0)