Skip to content

Commit 149ce6f

Browse files
committed
multiboot2: fix handling of efi memory map
1 parent 0e70808 commit 149ce6f

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

multiboot2/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ impl<'a> BootInformation<'a> {
302302
// If the EFIBootServicesNotExited is present, then we should not use
303303
// the memory map, as it could still be in use.
304304
match self.get_tag::<EFIBootServicesNotExitedTag>() {
305-
Some(_tag) => None,
305+
Some(_tag) => {
306+
log::debug!("The EFI memory map is present but the UEFI Boot Services Not Existed Tag is present. Returning None.");
307+
None
308+
}
306309
None => self.get_tag::<EFIMemoryMapTag>(),
307310
}
308311
}
@@ -1447,18 +1450,17 @@ mod tests {
14471450
}
14481451

14491452
#[test]
1450-
#[cfg_attr(miri, ignore)]
14511453
fn efi_memory_map() {
14521454
#[repr(C, align(8))]
1453-
struct Bytes([u8; 72]);
1455+
struct Bytes([u8; 80]);
14541456
// test that the EFI memory map is detected.
14551457
let bytes: Bytes = Bytes([
1456-
72, 0, 0, 0, // size
1458+
80, 0, 0, 0, // size
14571459
0, 0, 0, 0, // reserved
14581460
17, 0, 0, 0, // EFI memory map type
1459-
56, 0, 0, 0, // EFI memory map size
1461+
64, 0, 0, 0, // EFI memory map size
14601462
48, 0, 0, 0, // EFI descriptor size
1461-
1, 0, 0, 0, // EFI descriptor version, don't think this matters.
1463+
1, 0, 0, 0, // EFI descriptor version
14621464
7, 0, 0, 0, // Type: EfiConventionalMemory
14631465
0, 0, 0, 0, // Padding
14641466
0, 0, 16, 0, // Physical Address: should be 0x100000
@@ -1469,6 +1471,8 @@ mod tests {
14691471
0, 0, 0, 0, // Extension of pages
14701472
0, 0, 0, 0, // Attributes of this memory range.
14711473
0, 0, 0, 0, // Extension of attributes
1474+
0, 0, 0, 0, // More padding to extend the efi mmap to `desc_size`.
1475+
0, 0, 0, 0, // padding/alignment for end tag
14721476
0, 0, 0, 0, // end tag type.
14731477
8, 0, 0, 0, // end tag size.
14741478
]);

multiboot2/src/memory_map.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,13 @@ pub struct EFIMemoryAreaIter<'a> {
361361

362362
impl<'a> EFIMemoryAreaIter<'a> {
363363
fn new(mmap_tag: &'a EFIMemoryMapTag) -> Self {
364+
let desc_size = mmap_tag.desc_size as usize;
365+
let mmap_len = mmap_tag.memory_map.len();
366+
assert_eq!(mmap_len % desc_size, 0, "memory map length must be a multiple of `desc_size` by definition. The MBI seems to be corrupt.");
364367
Self {
365368
mmap_tag,
366369
i: 0,
367-
entries: mmap_tag.memory_map.len() / mmap_tag.desc_size as usize,
370+
entries: mmap_len / desc_size,
368371
phantom: PhantomData,
369372
}
370373
}

0 commit comments

Comments
 (0)