@@ -189,38 +189,49 @@ impl BootServices {
189189 pub fn memory_map_size ( & self ) -> MemoryMapSize {
190190 let mut map_size = 0 ;
191191 let mut map_key = MemoryMapKey ( 0 ) ;
192- let mut entry_size = 0 ;
192+ let mut desc_size = 0 ;
193193 let mut entry_version = 0 ;
194194
195195 let status = unsafe {
196196 ( self . 0 . get_memory_map ) (
197197 & mut map_size,
198198 ptr:: null_mut ( ) ,
199199 & mut map_key. 0 ,
200- & mut entry_size ,
200+ & mut desc_size ,
201201 & mut entry_version,
202202 )
203203 } ;
204204 assert_eq ! ( status, Status :: BUFFER_TOO_SMALL ) ;
205205
206+ assert_eq ! (
207+ map_size % desc_size,
208+ 0 ,
209+ "Memory map must be a multiple of the reported descriptor size."
210+ ) ;
211+
206212 MemoryMapSize {
207- entry_size ,
213+ desc_size ,
208214 map_size,
209215 }
210216 }
211217
212- /// Retrieves the current memory map.
218+ /// Stores the current UEFI memory map in the provided buffer .
213219 ///
214- /// The allocated buffer should be big enough to contain the memory map,
215- /// and a way of estimating how big it should be is by calling `memory_map_size`.
220+ /// The allocated buffer must be at least aligned to a [`MemoryDescriptor`]
221+ /// and should be big enough to store the whole map. To estimating how big
222+ /// the map will be, you can call [`Self::memory_map_size`].
216223 ///
217- /// The buffer must be aligned like a `MemoryDescriptor`.
224+ /// The memory map contains entries of type [`MemoryDescriptor`]. However,
225+ /// the relevant step size is always the reported `desc_size` but never
226+ /// `size_of::<MemoryDescriptor>()`.
218227 ///
219228 /// The returned key is a unique identifier of the current configuration of
220229 /// memory. Any allocations or such will change the memory map's key.
221230 ///
222231 /// If you want to store the resulting memory map without having to keep
223232 /// the buffer around, you can use `.copied().collect()` on the iterator.
233+ /// Note that this will change the current memory map again, if the UEFI
234+ /// allocator is used under the hood.
224235 ///
225236 /// # Errors
226237 ///
@@ -233,7 +244,7 @@ impl BootServices {
233244 MemoryDescriptor :: assert_aligned ( buffer) ;
234245 let map_buffer = buffer. as_mut_ptr ( ) . cast :: < MemoryDescriptor > ( ) ;
235246 let mut map_key = MemoryMapKey ( 0 ) ;
236- let mut entry_size = 0 ;
247+ let mut desc_size = 0 ;
237248 let mut entry_version = 0 ;
238249
239250 assert_eq ! (
@@ -247,17 +258,17 @@ impl BootServices {
247258 & mut map_size,
248259 map_buffer,
249260 & mut map_key. 0 ,
250- & mut entry_size ,
261+ & mut desc_size ,
251262 & mut entry_version,
252263 )
253264 }
254265 . to_result_with_val ( move || {
255- let len = map_size / entry_size ;
266+ let len = map_size / desc_size ;
256267
257268 MemoryMap {
258269 key : map_key,
259270 buf : buffer,
260- entry_size ,
271+ desc_size ,
261272 len,
262273 }
263274 } )
@@ -1613,7 +1624,7 @@ pub struct MemoryMapKey(usize);
16131624#[ derive( Debug ) ]
16141625pub struct MemoryMapSize {
16151626 /// Size of a single memory descriptor in bytes
1616- pub entry_size : usize ,
1627+ pub desc_size : usize ,
16171628 /// Size of the entire memory map in bytes
16181629 pub map_size : usize ,
16191630}
@@ -1642,7 +1653,7 @@ pub struct MemoryMap<'buf> {
16421653 buf : & ' buf mut [ u8 ] ,
16431654 /// Usually bound to the size of a [`MemoryDescriptor`] but can indicate if
16441655 /// this field is ever extended by a new UEFI standard.
1645- entry_size : usize ,
1656+ desc_size : usize ,
16461657 len : usize ,
16471658}
16481659
@@ -1653,13 +1664,19 @@ impl<'buf> MemoryMap<'buf> {
16531664 ///
16541665 /// This allows parsing a memory map provided by a kernel after boot
16551666 /// services have already exited.
1656- pub fn from_raw ( buf : & ' buf mut [ u8 ] , entry_size : usize ) -> Self {
1657- assert ! ( entry_size >= mem:: size_of:: <MemoryDescriptor >( ) ) ;
1658- let len = buf. len ( ) / entry_size;
1667+ pub fn from_raw ( buf : & ' buf mut [ u8 ] , desc_size : usize ) -> Self {
1668+ assert ! ( !buf. is_empty( ) ) ;
1669+ assert_eq ! (
1670+ buf. len( ) % desc_size,
1671+ 0 ,
1672+ "The buffer length must be a multiple of the desc_size"
1673+ ) ;
1674+ assert ! ( desc_size >= mem:: size_of:: <MemoryDescriptor >( ) ) ;
1675+ let len = buf. len ( ) / desc_size;
16591676 MemoryMap {
16601677 key : MemoryMapKey ( 0 ) ,
16611678 buf,
1662- entry_size ,
1679+ desc_size ,
16631680 len,
16641681 }
16651682 }
@@ -1727,15 +1744,15 @@ impl<'buf> MemoryMap<'buf> {
17271744
17281745 unsafe {
17291746 ptr:: swap_nonoverlapping (
1730- base. add ( index1 * self . entry_size ) ,
1731- base. add ( index2 * self . entry_size ) ,
1732- self . entry_size ,
1747+ base. add ( index1 * self . desc_size ) ,
1748+ base. add ( index2 * self . desc_size ) ,
1749+ self . desc_size ,
17331750 ) ;
17341751 }
17351752 }
17361753
17371754 fn get_element_phys_addr ( & self , index : usize ) -> PhysicalAddress {
1738- let offset = index. checked_mul ( self . entry_size ) . unwrap ( ) ;
1755+ let offset = index. checked_mul ( self . desc_size ) . unwrap ( ) ;
17391756 let elem = unsafe { & * self . buf . as_ptr ( ) . add ( offset) . cast :: < MemoryDescriptor > ( ) } ;
17401757 elem. phys_start
17411758 }
@@ -1767,7 +1784,7 @@ impl<'buf> MemoryMap<'buf> {
17671784 & * self
17681785 . buf
17691786 . as_ptr ( )
1770- . add ( self . entry_size * index)
1787+ . add ( self . desc_size * index)
17711788 . cast :: < MemoryDescriptor > ( )
17721789 } ;
17731790
@@ -1785,7 +1802,7 @@ impl<'buf> MemoryMap<'buf> {
17851802 & mut * self
17861803 . buf
17871804 . as_mut_ptr ( )
1788- . add ( self . entry_size * index)
1805+ . add ( self . desc_size * index)
17891806 . cast :: < MemoryDescriptor > ( )
17901807 } ;
17911808
0 commit comments