Skip to content

Commit ed52093

Browse files
retragerbradford
authored andcommitted
efi: Fix get_memory_map to update descriptor info
The UEFI Spec does not imply when the descriptor_size and descriptor_version should be updated. However, a recent Linux kernel (at least v5.8) expects these values are filled even if the memory_map_size is too small. This assumption causes divided-by-0 exception during the boot and system reboot. This commit adds null checks and fixes the behavior. This fix is to address issue #107. Signed-off-by: Akira Moroo <retrage01@gmail.com>
1 parent 1312461 commit ed52093

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/efi/mod.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,22 @@ pub extern "win64" fn get_memory_map(
369369
descriptor_size: *mut usize,
370370
descriptor_version: *mut u32,
371371
) -> Status {
372+
if memory_map_size.is_null() {
373+
return Status::INVALID_PARAMETER;
374+
}
375+
376+
if !descriptor_size.is_null() {
377+
unsafe {
378+
*descriptor_size = size_of::<MemoryDescriptor>();
379+
}
380+
}
381+
382+
if !descriptor_version.is_null() {
383+
unsafe {
384+
*descriptor_version = efi::MEMORY_DESCRIPTOR_VERSION;
385+
}
386+
}
387+
372388
let count = ALLOCATOR.borrow().get_descriptor_count();
373389
let map_size = size_of::<MemoryDescriptor>() * count;
374390
if unsafe { *memory_map_size } < map_size {
@@ -378,14 +394,16 @@ pub extern "win64" fn get_memory_map(
378394
return Status::BUFFER_TOO_SMALL;
379395
}
380396

397+
if key.is_null() {
398+
return Status::INVALID_PARAMETER;
399+
}
400+
381401
let out =
382402
unsafe { core::slice::from_raw_parts_mut(out as *mut alloc::MemoryDescriptor, count) };
383403
let count = ALLOCATOR.borrow().get_descriptors(out);
384404
let map_size = size_of::<MemoryDescriptor>() * count;
385405
unsafe {
386406
*memory_map_size = map_size;
387-
*descriptor_version = efi::MEMORY_DESCRIPTOR_VERSION;
388-
*descriptor_size = size_of::<MemoryDescriptor>();
389407
*key = ALLOCATOR.borrow().get_map_key();
390408
}
391409

0 commit comments

Comments
 (0)