Skip to content

Commit 21d03f6

Browse files
uefi: Use global system table in MemoryMapBackingMemory::new
Move `get_memory_map` from `uefi::table::boot` to `uefi::boot`, and update `MemoryMapBackingMemory::new` accordingly.
1 parent 5227fc0 commit 21d03f6

File tree

3 files changed

+47
-48
lines changed

3 files changed

+47
-48
lines changed

uefi/src/boot.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,51 @@ pub unsafe fn free_pool(ptr: NonNull<u8>) -> Result {
178178
unsafe { (bt.free_pool)(ptr.as_ptr()) }.to_result()
179179
}
180180

181+
/// Queries the `get_memory_map` function of UEFI to retrieve the current
182+
/// size of the map. Returns a [`MemoryMapMeta`].
183+
///
184+
/// It is recommended to add a few more bytes for a subsequent allocation
185+
/// for the memory map, as the memory map itself also needs heap memory,
186+
/// and other allocations might occur before that call.
187+
#[must_use]
188+
pub(crate) fn memory_map_size() -> MemoryMapMeta {
189+
let bt = boot_services_raw_panicking();
190+
let bt = unsafe { bt.as_ref() };
191+
192+
let mut map_size = 0;
193+
let mut map_key = MemoryMapKey(0);
194+
let mut desc_size = 0;
195+
let mut desc_version = 0;
196+
197+
let status = unsafe {
198+
(bt.get_memory_map)(
199+
&mut map_size,
200+
ptr::null_mut(),
201+
&mut map_key.0,
202+
&mut desc_size,
203+
&mut desc_version,
204+
)
205+
};
206+
assert_eq!(status, Status::BUFFER_TOO_SMALL);
207+
208+
assert_eq!(
209+
map_size % desc_size,
210+
0,
211+
"Memory map must be a multiple of the reported descriptor size."
212+
);
213+
214+
let mmm = MemoryMapMeta {
215+
desc_size,
216+
map_size,
217+
map_key,
218+
desc_version,
219+
};
220+
221+
mmm.assert_sanity_checks();
222+
223+
mmm
224+
}
225+
181226
/// Stores the current UEFI memory map in an UEFI-heap allocated buffer
182227
/// and returns a [`MemoryMapOwned`].
183228
///

uefi/src/mem/memory_map/impl_.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
44
use super::*;
55
use crate::boot;
6-
use crate::table::system_table_boot;
76
use core::fmt::{Debug, Display, Formatter};
87
use core::ops::{Index, IndexMut};
98
use core::ptr::NonNull;
@@ -275,12 +274,9 @@ impl MemoryMapBackingMemory {
275274
/// - `memory_type`: The memory type for the memory map allocation.
276275
/// Typically, [`MemoryType::LOADER_DATA`] for regular UEFI applications.
277276
pub(crate) fn new(memory_type: MemoryType) -> crate::Result<Self> {
278-
let st = system_table_boot().expect("Should have boot services activated");
279-
let bs = st.boot_services();
280-
281-
let memory_map_meta = bs.memory_map_size();
277+
let memory_map_meta = boot::memory_map_size();
282278
let len = Self::safe_allocation_size_hint(memory_map_meta);
283-
let ptr = bs.allocate_pool(memory_type, len)?.as_ptr();
279+
let ptr = boot::allocate_pool(memory_type, len)?.as_ptr();
284280

285281
// Should be fine as UEFI always has allocations with a guaranteed
286282
// alignment of 8 bytes.

uefi/src/table/boot.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -168,48 +168,6 @@ impl BootServices {
168168
unsafe { (self.0.free_pages)(addr, count) }.to_result()
169169
}
170170

171-
/// Queries the `get_memory_map` function of UEFI to retrieve the current
172-
/// size of the map. Returns a [`MemoryMapMeta`].
173-
///
174-
/// It is recommended to add a few more bytes for a subsequent allocation
175-
/// for the memory map, as the memory map itself also needs heap memory,
176-
/// and other allocations might occur before that call.
177-
#[must_use]
178-
pub(crate) fn memory_map_size(&self) -> MemoryMapMeta {
179-
let mut map_size = 0;
180-
let mut map_key = MemoryMapKey(0);
181-
let mut desc_size = 0;
182-
let mut desc_version = 0;
183-
184-
let status = unsafe {
185-
(self.0.get_memory_map)(
186-
&mut map_size,
187-
ptr::null_mut(),
188-
&mut map_key.0,
189-
&mut desc_size,
190-
&mut desc_version,
191-
)
192-
};
193-
assert_eq!(status, Status::BUFFER_TOO_SMALL);
194-
195-
assert_eq!(
196-
map_size % desc_size,
197-
0,
198-
"Memory map must be a multiple of the reported descriptor size."
199-
);
200-
201-
let mmm = MemoryMapMeta {
202-
desc_size,
203-
map_size,
204-
map_key,
205-
desc_version,
206-
};
207-
208-
mmm.assert_sanity_checks();
209-
210-
mmm
211-
}
212-
213171
/// Stores the current UEFI memory map in an UEFI-heap allocated buffer
214172
/// and returns a [`MemoryMapOwned`].
215173
///

0 commit comments

Comments
 (0)