Skip to content

Commit da48cc2

Browse files
committed
uefi: use MemoryMap::from_raw everywhere and don't create it explicitly
Unify the construction.
1 parent 37716c7 commit da48cc2

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

uefi/src/table/boot.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -262,16 +262,7 @@ impl BootServices {
262262
&mut entry_version,
263263
)
264264
}
265-
.to_result_with_val(move || {
266-
let len = map_size / desc_size;
267-
268-
MemoryMap {
269-
key: map_key,
270-
buf: buffer,
271-
desc_size,
272-
len,
273-
}
274-
})
265+
.to_result_with_val(move || MemoryMap::from_raw(map_size, buffer, desc_size, map_key))
275266
}
276267

277268
/// Allocates from a memory pool. The pointer will be 8-byte aligned.
@@ -1640,40 +1631,46 @@ pub struct MemoryMapSize {
16401631
/// always use `entry_size` as step-size when interfacing with the memory map on
16411632
/// a low level.
16421633
///
1643-
///
1644-
///
16451634
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
16461635
#[derive(Debug)]
16471636
pub struct MemoryMap<'buf> {
16481637
key: MemoryMapKey,
1638+
/// The backing memory. Might be slightly larger than the actual map.
16491639
buf: &'buf mut [u8],
16501640
/// Usually bound to the size of a [`MemoryDescriptor`] but can indicate if
16511641
/// this field is ever extended by a new UEFI standard.
16521642
desc_size: usize,
1653-
len: usize,
1643+
map_size: usize,
1644+
num_entries: usize,
16541645
}
16551646

16561647
impl<'buf> MemoryMap<'buf> {
1657-
/// Creates a [`MemoryMap`] from the given buffer and entry size.
1658-
/// The entry size is usually bound to the size of a [`MemoryDescriptor`]
1659-
/// but can indicate if this field is ever extended by a new UEFI standard.
1660-
///
1661-
/// This allows parsing a memory map provided by a kernel after boot
1662-
/// services have already exited.
1663-
pub fn from_raw(buf: &'buf mut [u8], desc_size: usize) -> Self {
1648+
/// Creates a [`MemoryMap`] from the given buffer, and the reported
1649+
/// memory map size, descriptor size, and [`MemoryMapKey`] from UEFI.
1650+
///
1651+
/// This map technically owns the buffer (although not modeled yet by type
1652+
/// system).
1653+
pub fn from_raw(
1654+
map_size: usize,
1655+
buf: &'buf mut [u8],
1656+
desc_size: usize,
1657+
key: MemoryMapKey,
1658+
) -> Self {
16641659
assert!(!buf.is_empty());
1660+
assert!(map_size <= buf.len());
16651661
assert_eq!(
16661662
buf.len() % desc_size,
16671663
0,
16681664
"The buffer length must be a multiple of the desc_size"
16691665
);
16701666
assert!(desc_size >= mem::size_of::<MemoryDescriptor>());
1671-
let len = buf.len() / desc_size;
1667+
let num_entries = map_size / desc_size;
16721668
MemoryMap {
1673-
key: MemoryMapKey(0),
1669+
key,
16741670
buf,
1671+
map_size,
16751672
desc_size,
1676-
len,
1673+
num_entries,
16771674
}
16781675
}
16791676

@@ -1687,7 +1684,7 @@ impl<'buf> MemoryMap<'buf> {
16871684
/// This operation is optional and should be invoked only once.
16881685
pub fn sort(&mut self) {
16891686
unsafe {
1690-
self.qsort(0, self.len - 1);
1687+
self.qsort(0, self.num_entries - 1);
16911688
}
16921689
}
16931690

@@ -1772,7 +1769,7 @@ impl<'buf> MemoryMap<'buf> {
17721769
/// Returns a reference to the [`MemoryDescriptor`] at `index` or `None` if out of bounds.
17731770
#[must_use]
17741771
pub fn get(&self, index: usize) -> Option<&'buf MemoryDescriptor> {
1775-
if index >= self.len {
1772+
if index >= self.num_entries {
17761773
return None;
17771774
}
17781775

@@ -1790,7 +1787,7 @@ impl<'buf> MemoryMap<'buf> {
17901787
/// Returns a mut reference to the [`MemoryDescriptor`] at `index` or `None` if out of bounds.
17911788
#[must_use]
17921789
pub fn get_mut(&mut self, index: usize) -> Option<&'buf mut MemoryDescriptor> {
1793-
if index >= self.len {
1790+
if index >= self.num_entries {
17941791
return None;
17951792
}
17961793

@@ -1832,7 +1829,7 @@ impl<'buf> Iterator for MemoryMapIter<'buf> {
18321829
type Item = &'buf MemoryDescriptor;
18331830

18341831
fn size_hint(&self) -> (usize, Option<usize>) {
1835-
let sz = self.memory_map.len - self.index;
1832+
let sz = self.memory_map.num_entries - self.index;
18361833

18371834
(sz, Some(sz))
18381835
}
@@ -1848,7 +1845,7 @@ impl<'buf> Iterator for MemoryMapIter<'buf> {
18481845

18491846
impl ExactSizeIterator for MemoryMapIter<'_> {
18501847
fn len(&self) -> usize {
1851-
self.memory_map.len
1848+
self.memory_map.num_entries
18521849
}
18531850
}
18541851

@@ -1980,6 +1977,7 @@ pub struct ProtocolSearchKey(NonNull<c_void>);
19801977
#[cfg(test)]
19811978
mod tests {
19821979
use core::mem::{size_of, size_of_val};
1980+
use uefi::table::boot::MemoryMapKey;
19831981

19841982
use crate::table::boot::{MemoryAttribute, MemoryMap, MemoryType};
19851983

@@ -1992,7 +1990,7 @@ mod tests {
19921990
}
19931991
};
19941992

1995-
MemoryMap::from_raw(byte_buffer, size_of::<MemoryDescriptor>())
1993+
MemoryMap::from_raw(byte_buffer.len(), byte_buffer, size_of::<MemoryDescriptor>(), MemoryMapKey(0))
19961994
}
19971995

19981996
#[test]

0 commit comments

Comments
 (0)