@@ -189,38 +189,49 @@ impl BootServices {
189
189
pub fn memory_map_size ( & self ) -> MemoryMapSize {
190
190
let mut map_size = 0 ;
191
191
let mut map_key = MemoryMapKey ( 0 ) ;
192
- let mut entry_size = 0 ;
192
+ let mut desc_size = 0 ;
193
193
let mut entry_version = 0 ;
194
194
195
195
let status = unsafe {
196
196
( self . 0 . get_memory_map ) (
197
197
& mut map_size,
198
198
ptr:: null_mut ( ) ,
199
199
& mut map_key. 0 ,
200
- & mut entry_size ,
200
+ & mut desc_size ,
201
201
& mut entry_version,
202
202
)
203
203
} ;
204
204
assert_eq ! ( status, Status :: BUFFER_TOO_SMALL ) ;
205
205
206
+ assert_eq ! (
207
+ map_size % desc_size,
208
+ 0 ,
209
+ "Memory map must be a multiple of the reported descriptor size."
210
+ ) ;
211
+
206
212
MemoryMapSize {
207
- entry_size ,
213
+ desc_size ,
208
214
map_size,
209
215
}
210
216
}
211
217
212
- /// Retrieves the current memory map.
218
+ /// Stores the current UEFI memory map in the provided buffer .
213
219
///
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`].
216
223
///
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>()`.
218
227
///
219
228
/// The returned key is a unique identifier of the current configuration of
220
229
/// memory. Any allocations or such will change the memory map's key.
221
230
///
222
231
/// If you want to store the resulting memory map without having to keep
223
232
/// 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.
224
235
///
225
236
/// # Errors
226
237
///
@@ -233,7 +244,7 @@ impl BootServices {
233
244
MemoryDescriptor :: assert_aligned ( buffer) ;
234
245
let map_buffer = buffer. as_mut_ptr ( ) . cast :: < MemoryDescriptor > ( ) ;
235
246
let mut map_key = MemoryMapKey ( 0 ) ;
236
- let mut entry_size = 0 ;
247
+ let mut desc_size = 0 ;
237
248
let mut entry_version = 0 ;
238
249
239
250
assert_eq ! (
@@ -247,17 +258,17 @@ impl BootServices {
247
258
& mut map_size,
248
259
map_buffer,
249
260
& mut map_key. 0 ,
250
- & mut entry_size ,
261
+ & mut desc_size ,
251
262
& mut entry_version,
252
263
)
253
264
}
254
265
. to_result_with_val ( move || {
255
- let len = map_size / entry_size ;
266
+ let len = map_size / desc_size ;
256
267
257
268
MemoryMap {
258
269
key : map_key,
259
270
buf : buffer,
260
- entry_size ,
271
+ desc_size ,
261
272
len,
262
273
}
263
274
} )
@@ -1609,7 +1620,7 @@ pub struct MemoryMapKey(usize);
1609
1620
#[ derive( Debug ) ]
1610
1621
pub struct MemoryMapSize {
1611
1622
/// Size of a single memory descriptor in bytes
1612
- pub entry_size : usize ,
1623
+ pub desc_size : usize ,
1613
1624
/// Size of the entire memory map in bytes
1614
1625
pub map_size : usize ,
1615
1626
}
@@ -1638,7 +1649,7 @@ pub struct MemoryMap<'buf> {
1638
1649
buf : & ' buf mut [ u8 ] ,
1639
1650
/// Usually bound to the size of a [`MemoryDescriptor`] but can indicate if
1640
1651
/// this field is ever extended by a new UEFI standard.
1641
- entry_size : usize ,
1652
+ desc_size : usize ,
1642
1653
len : usize ,
1643
1654
}
1644
1655
@@ -1649,13 +1660,19 @@ impl<'buf> MemoryMap<'buf> {
1649
1660
///
1650
1661
/// This allows parsing a memory map provided by a kernel after boot
1651
1662
/// services have already exited.
1652
- pub fn from_raw ( buf : & ' buf mut [ u8 ] , entry_size : usize ) -> Self {
1653
- assert ! ( entry_size >= mem:: size_of:: <MemoryDescriptor >( ) ) ;
1654
- let len = buf. len ( ) / entry_size;
1663
+ pub fn from_raw ( buf : & ' buf mut [ u8 ] , desc_size : usize ) -> Self {
1664
+ assert ! ( !buf. is_empty( ) ) ;
1665
+ assert_eq ! (
1666
+ buf. len( ) % desc_size,
1667
+ 0 ,
1668
+ "The buffer length must be a multiple of the desc_size"
1669
+ ) ;
1670
+ assert ! ( desc_size >= mem:: size_of:: <MemoryDescriptor >( ) ) ;
1671
+ let len = buf. len ( ) / desc_size;
1655
1672
MemoryMap {
1656
1673
key : MemoryMapKey ( 0 ) ,
1657
1674
buf,
1658
- entry_size ,
1675
+ desc_size ,
1659
1676
len,
1660
1677
}
1661
1678
}
@@ -1723,15 +1740,15 @@ impl<'buf> MemoryMap<'buf> {
1723
1740
1724
1741
unsafe {
1725
1742
ptr:: swap_nonoverlapping (
1726
- base. add ( index1 * self . entry_size ) ,
1727
- base. add ( index2 * self . entry_size ) ,
1728
- self . entry_size ,
1743
+ base. add ( index1 * self . desc_size ) ,
1744
+ base. add ( index2 * self . desc_size ) ,
1745
+ self . desc_size ,
1729
1746
) ;
1730
1747
}
1731
1748
}
1732
1749
1733
1750
fn get_element_phys_addr ( & self , index : usize ) -> PhysicalAddress {
1734
- let offset = index. checked_mul ( self . entry_size ) . unwrap ( ) ;
1751
+ let offset = index. checked_mul ( self . desc_size ) . unwrap ( ) ;
1735
1752
let elem = unsafe { & * self . buf . as_ptr ( ) . add ( offset) . cast :: < MemoryDescriptor > ( ) } ;
1736
1753
elem. phys_start
1737
1754
}
@@ -1763,7 +1780,7 @@ impl<'buf> MemoryMap<'buf> {
1763
1780
& * self
1764
1781
. buf
1765
1782
. as_ptr ( )
1766
- . add ( self . entry_size * index)
1783
+ . add ( self . desc_size * index)
1767
1784
. cast :: < MemoryDescriptor > ( )
1768
1785
} ;
1769
1786
@@ -1781,7 +1798,7 @@ impl<'buf> MemoryMap<'buf> {
1781
1798
& mut * self
1782
1799
. buf
1783
1800
. as_mut_ptr ( )
1784
- . add ( self . entry_size * index)
1801
+ . add ( self . desc_size * index)
1785
1802
. cast :: < MemoryDescriptor > ( )
1786
1803
} ;
1787
1804
0 commit comments