@@ -5,6 +5,7 @@ use crate::table::Header;
5
5
use crate :: { Char16 , Event , Guid , Handle , PhysicalAddress , Status , VirtualAddress } ;
6
6
use bitflags:: bitflags;
7
7
use core:: ffi:: c_void;
8
+ use core:: mem;
8
9
9
10
/// Table of pointers to all the boot services.
10
11
#[ derive( Debug ) ]
@@ -363,6 +364,25 @@ pub struct MemoryDescriptor {
363
364
impl MemoryDescriptor {
364
365
/// Memory descriptor version number.
365
366
pub const VERSION : u32 = 1 ;
367
+
368
+ /// Returns a best-effort **guess** of the `desc_size` that UEFI will
369
+ /// report for a [`MemoryDescriptor`]. You may use this in unit testing. In
370
+ /// integration tests and real world scenarios, refrain from using this, and
371
+ /// use the actual reported `desc_size` reported from `UEFI#GetMemoryMap()`
372
+ /// instead.
373
+ ///
374
+ /// # Safety
375
+ /// Unsafe because you should always use the `desc_size` that is associated
376
+ /// with the memory map.
377
+ #[ must_use]
378
+ pub const unsafe fn uefi_desc_size ( ) -> usize {
379
+ let size_base = mem:: size_of :: < Self > ( ) ;
380
+ // Taken from https://github.com/tianocore/edk2/blob/7142e648416ff5d3eac6c6d607874805f5de0ca8/MdeModulePkg/Core/PiSmmCore/Page.c#L1059
381
+ let desc_size_diff = mem:: size_of :: < u64 > ( ) - size_base % mem:: size_of :: < u64 > ( ) ;
382
+ let desc_size = size_base + desc_size_diff;
383
+ assert ! ( desc_size > size_base) ;
384
+ desc_size
385
+ }
366
386
}
367
387
368
388
impl Default for MemoryDescriptor {
@@ -466,3 +486,17 @@ pub enum Tpl: usize => {
466
486
/// Even processor interrupts are disable at this level.
467
487
HIGH_LEVEL = 31 ,
468
488
} }
489
+
490
+ #[ cfg( test) ]
491
+ mod tests {
492
+ use super :: * ;
493
+
494
+ /// This refers to the ABI of the MemoryDescriptor version 1 a
495
+ #[ test]
496
+ fn test_abi_v1 ( ) {
497
+ assert_eq ! ( MemoryDescriptor :: VERSION , 1 ) ;
498
+ assert_eq ! ( mem:: size_of:: <MemoryDescriptor >( ) , 40 ) ;
499
+ // This is what GetMemoryMap from UEFI usually returns.
500
+ assert_eq ! ( unsafe { MemoryDescriptor :: uefi_desc_size( ) } , 48 ) ;
501
+ }
502
+ }
0 commit comments