Skip to content

Commit df763a5

Browse files
committed
uefi: change signature of exit_boot_services
People probably want to use 99% of the time LOADER_DATA here. Therefore, we indicate in our function signature, that people should use the default (`None`) if they are unsure what to do here. LOADER_DATA is also what Linux uses [0]. [0] https://elixir.bootlin.com/linux/v6.13.7/source/drivers/firmware/efi/libstub/mem.c#L24
1 parent a0c4b71 commit df763a5

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

Diff for: uefi-test-runner/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern crate alloc;
1010

1111
use alloc::string::ToString;
1212
use alloc::vec::Vec;
13-
use uefi::mem::memory_map::{MemoryMap, MemoryType};
13+
use uefi::mem::memory_map::MemoryMap;
1414
use uefi::prelude::*;
1515
use uefi::proto::console::serial::Serial;
1616
use uefi::proto::device_path::build::{self, DevicePathBuilder};
@@ -209,7 +209,7 @@ fn shutdown() -> ! {
209209
info!("Testing complete, exiting boot services...");
210210

211211
// Exit boot services as a proof that it works :)
212-
let mmap = unsafe { uefi::boot::exit_boot_services(MemoryType::LOADER_DATA) };
212+
let mmap = unsafe { uefi::boot::exit_boot_services(None) };
213213

214214
info!("Memory Map:");
215215
for desc in mmap.entries() {

Diff for: uefi/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
`&self`.
1717
- **Breaking:** The `pxe::Mode` struct is now opaque. Use method calls to access
1818
mode data instead of direct field access.
19+
- **Breaking:** `exit_boot_services` now consumes a `Option<MemoryType>` which
20+
defaults to the recommended value of `MemoryType::LOADER_DATA`.
1921
- `boot::memory_map()` will never return `Status::BUFFER_TOO_SMALL` from now on,
2022
as this is considered a hard internal error where users can't do anything
2123
about it anyway. It will panic instead.

Diff for: uefi/src/boot.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ unsafe fn get_memory_map_and_exit_boot_services(buf: &mut [u8]) -> Result<Memory
12601260

12611261
/// Convenient wrapper to exit UEFI boot services along with corresponding
12621262
/// essential steps to get the memory map.
1263-
///
1263+
///
12641264
/// This wrapper ensures a safe and spec-compliant transition from UEFI boot
12651265
/// services phase to the runtime services phase by retrieving the system
12661266
/// memory map and invoking `ExitBootServices()` with the correct memory map
@@ -1282,6 +1282,13 @@ unsafe fn get_memory_map_and_exit_boot_services(buf: &mut [u8]) -> Result<Memory
12821282
/// `global_allocator` feature is enabled, attempting to use the allocator
12831283
/// after exiting boot services will panic.
12841284
///
1285+
/// # Arguments
1286+
/// - `custom_memory_type`: The [`MemoryType`] for the UEFI allocation that will
1287+
/// store the final memory map. If you pass `None`, this defaults to the
1288+
/// recommended default value of [`MemoryType::LOADER_DATA`]. If you want a
1289+
/// specific memory region for the memory map, you can pass the desired
1290+
/// [`MemoryType`].
1291+
///
12851292
/// # Safety
12861293
///
12871294
/// The caller is responsible for ensuring that no references to
@@ -1312,7 +1319,10 @@ unsafe fn get_memory_map_and_exit_boot_services(buf: &mut [u8]) -> Result<Memory
13121319
/// [`Output`]: crate::proto::console::text::Output
13131320
/// [`PoolString`]: crate::proto::device_path::text::PoolString
13141321
#[must_use]
1315-
pub unsafe fn exit_boot_services(memory_type: MemoryType) -> MemoryMapOwned {
1322+
pub unsafe fn exit_boot_services(custom_memory_type: Option<MemoryType>) -> MemoryMapOwned {
1323+
// LOADER_DATA is the default and also used by the Linux kernel:
1324+
// https://elixir.bootlin.com/linux/v6.13.7/source/drivers/firmware/efi/libstub/mem.c#L24
1325+
let memory_type = custom_memory_type.unwrap_or(MemoryType::LOADER_DATA);
13161326
crate::helpers::exit();
13171327

13181328
let mut buf = MemoryMapBackingMemory::new(memory_type).expect("Failed to allocate memory");

0 commit comments

Comments
 (0)