Skip to content

Commit 4962281

Browse files
committed
Measure memory map size before freeing memory to avoid an endless loop, and remove the extra unnecessary variable
1 parent 8cbf6b3 commit 4962281

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

uefi/src/main.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ fn efi_main(image: Handle, st: SystemTable<Boot>) -> Status {
6969

7070
fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {
7171
// temporarily clone the y table for printing panics
72-
7372
unsafe {
7473
*SYSTEM_TABLE.get() = Some(st.unsafe_clone());
7574
}
@@ -118,28 +117,25 @@ fn main_inner(image: Handle, mut st: SystemTable<Boot>) -> Status {
118117
}
119118
let mmap_storage = {
120119
let mut memory_map_size = st.boot_services().memory_map_size();
121-
let mut target_size = memory_map_size.map_size + (8 * memory_map_size.entry_size);
122120
loop {
123121
let ptr = st
124122
.boot_services()
125-
.allocate_pool(MemoryType::LOADER_DATA, target_size)
123+
.allocate_pool(MemoryType::LOADER_DATA, memory_map_size.map_size)
126124
.expect("Failed to allocate memory for mmap storage");
127-
let storage = unsafe { slice::from_raw_parts_mut(ptr, target_size) };
125+
126+
let storage = unsafe { slice::from_raw_parts_mut(ptr, memory_map_size.map_size) };
127+
128128
if st.boot_services().memory_map(storage).is_ok() {
129129
break storage;
130130
}
131-
// allocated memory region was not big enough -> free it again
132-
st.boot_services()
133-
.free_pool(ptr)
134-
.expect("Failed to free temporary memory for memory map!");
135131

136132
// By measuring the size here, we can find out exactly how much we need.
137133
// We may hit this code twice, if the map allocation ends up spanning more pages.
138134
memory_map_size = st.boot_services().memory_map_size();
139-
140-
let next_target_size = memory_map_size.map_size + (8 * memory_map_size.entry_size);
141-
target_size = next_target_size;
142-
135+
// allocated memory region was not big enough -> free it again
136+
st.boot_services()
137+
.free_pool(ptr)
138+
.expect("Failed to free temporary memory for memory map!");
143139
}
144140
};
145141

@@ -265,7 +261,7 @@ fn locate_and_open_protocol<P: ProtocolPointer>(
265261
st: &SystemTable<Boot>,
266262
) -> Option<ScopedProtocol<P>> {
267263
let this = st.boot_services();
268-
let mut device_path = open_device_path_protocol(image, st)?;
264+
let device_path = open_device_path_protocol(image, st)?;
269265
let mut device_path = device_path.deref();
270266

271267
let fs_handle = this.locate_device_path::<P>(&mut device_path);

0 commit comments

Comments
 (0)