@@ -262,16 +262,7 @@ impl BootServices {
262
262
& mut entry_version,
263
263
)
264
264
}
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) )
275
266
}
276
267
277
268
/// Allocates from a memory pool. The pointer will be 8-byte aligned.
@@ -1640,40 +1631,46 @@ pub struct MemoryMapSize {
1640
1631
/// always use `entry_size` as step-size when interfacing with the memory map on
1641
1632
/// a low level.
1642
1633
///
1643
- ///
1644
- ///
1645
1634
/// [0]: https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
1646
1635
#[ derive( Debug ) ]
1647
1636
pub struct MemoryMap < ' buf > {
1648
1637
key : MemoryMapKey ,
1638
+ /// The backing memory. Might be slightly larger than the actual map.
1649
1639
buf : & ' buf mut [ u8 ] ,
1650
1640
/// Usually bound to the size of a [`MemoryDescriptor`] but can indicate if
1651
1641
/// this field is ever extended by a new UEFI standard.
1652
1642
desc_size : usize ,
1653
- len : usize ,
1643
+ map_size : usize ,
1644
+ num_entries : usize ,
1654
1645
}
1655
1646
1656
1647
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 {
1664
1659
assert ! ( !buf. is_empty( ) ) ;
1660
+ assert ! ( map_size <= buf. len( ) ) ;
1665
1661
assert_eq ! (
1666
1662
buf. len( ) % desc_size,
1667
1663
0 ,
1668
1664
"The buffer length must be a multiple of the desc_size"
1669
1665
) ;
1670
1666
assert ! ( desc_size >= mem:: size_of:: <MemoryDescriptor >( ) ) ;
1671
- let len = buf . len ( ) / desc_size;
1667
+ let num_entries = map_size / desc_size;
1672
1668
MemoryMap {
1673
- key : MemoryMapKey ( 0 ) ,
1669
+ key,
1674
1670
buf,
1671
+ map_size,
1675
1672
desc_size,
1676
- len ,
1673
+ num_entries ,
1677
1674
}
1678
1675
}
1679
1676
@@ -1687,7 +1684,7 @@ impl<'buf> MemoryMap<'buf> {
1687
1684
/// This operation is optional and should be invoked only once.
1688
1685
pub fn sort ( & mut self ) {
1689
1686
unsafe {
1690
- self . qsort ( 0 , self . len - 1 ) ;
1687
+ self . qsort ( 0 , self . num_entries - 1 ) ;
1691
1688
}
1692
1689
}
1693
1690
@@ -1772,7 +1769,7 @@ impl<'buf> MemoryMap<'buf> {
1772
1769
/// Returns a reference to the [`MemoryDescriptor`] at `index` or `None` if out of bounds.
1773
1770
#[ must_use]
1774
1771
pub fn get ( & self , index : usize ) -> Option < & ' buf MemoryDescriptor > {
1775
- if index >= self . len {
1772
+ if index >= self . num_entries {
1776
1773
return None ;
1777
1774
}
1778
1775
@@ -1790,7 +1787,7 @@ impl<'buf> MemoryMap<'buf> {
1790
1787
/// Returns a mut reference to the [`MemoryDescriptor`] at `index` or `None` if out of bounds.
1791
1788
#[ must_use]
1792
1789
pub fn get_mut ( & mut self , index : usize ) -> Option < & ' buf mut MemoryDescriptor > {
1793
- if index >= self . len {
1790
+ if index >= self . num_entries {
1794
1791
return None ;
1795
1792
}
1796
1793
@@ -1832,7 +1829,7 @@ impl<'buf> Iterator for MemoryMapIter<'buf> {
1832
1829
type Item = & ' buf MemoryDescriptor ;
1833
1830
1834
1831
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 ;
1836
1833
1837
1834
( sz, Some ( sz) )
1838
1835
}
@@ -1848,7 +1845,7 @@ impl<'buf> Iterator for MemoryMapIter<'buf> {
1848
1845
1849
1846
impl ExactSizeIterator for MemoryMapIter < ' _ > {
1850
1847
fn len ( & self ) -> usize {
1851
- self . memory_map . len
1848
+ self . memory_map . num_entries
1852
1849
}
1853
1850
}
1854
1851
@@ -1980,6 +1977,7 @@ pub struct ProtocolSearchKey(NonNull<c_void>);
1980
1977
#[ cfg( test) ]
1981
1978
mod tests {
1982
1979
use core:: mem:: { size_of, size_of_val} ;
1980
+ use uefi:: table:: boot:: MemoryMapKey ;
1983
1981
1984
1982
use crate :: table:: boot:: { MemoryAttribute , MemoryMap , MemoryType } ;
1985
1983
@@ -1992,7 +1990,7 @@ mod tests {
1992
1990
}
1993
1991
} ;
1994
1992
1995
- MemoryMap :: from_raw ( byte_buffer, size_of :: < MemoryDescriptor > ( ) )
1993
+ MemoryMap :: from_raw ( byte_buffer. len ( ) , byte_buffer , size_of :: < MemoryDescriptor > ( ) , MemoryMapKey ( 0 ) )
1996
1994
}
1997
1995
1998
1996
#[ test]
0 commit comments