Skip to content

uefi: clarify situation with boot::memory_map() and Status::BUFFER_TOO_SMALL #1540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions uefi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
- **Breaking:** Removed `BootPolicyError` as `BootPolicy` construction is no
longer fallible. `BootPolicy` now tightly integrates the new `Boolean` type
of `uefi-raw`.
- `boot::memory_map()` will never return `Status::BUFFER_TOO_SMALL` from now on,
as this is considered a hard internal error where users can't do anything
about it anyway. It will panic instead.


# uefi - 0.34.1 (2025-02-07)

Expand Down
28 changes: 25 additions & 3 deletions uefi/src/boot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ pub(crate) fn memory_map_size() -> MemoryMapMeta {
/// Stores the current UEFI memory map in an UEFI-heap allocated buffer
/// and returns a [`MemoryMapOwned`].
///
/// The implementation tries to mitigate some UEFI pitfalls, such as getting
/// the right allocation size for the memory map to prevent
/// [`Status::BUFFER_TOO_SMALL`].
///
/// # Parameters
///
/// - `mt`: The memory type for the backing memory on the UEFI heap.
Expand All @@ -255,12 +259,30 @@ pub(crate) fn memory_map_size() -> MemoryMapMeta {
///
/// # Errors
///
/// * [`Status::BUFFER_TOO_SMALL`]
/// * [`Status::INVALID_PARAMETER`]
/// * [`Status::INVALID_PARAMETER`]: Invalid [`MemoryType`]
/// * [`Status::OUT_OF_RESOURCES`]: allocation failed.
///
/// # Panics
///
/// Panics if the memory map can't be retrieved because of
/// [`Status::BUFFER_TOO_SMALL`]. This behaviour was chosen explicitly as
/// callers can't do anything about it anyway.
pub fn memory_map(mt: MemoryType) -> Result<MemoryMapOwned> {
let mut buffer = MemoryMapBackingMemory::new(mt)?;

let meta = get_memory_map(buffer.as_mut_slice())?;
let meta = get_memory_map(buffer.as_mut_slice());

if let Err(e) = &meta {
// We don't want to confuse users and let them think they should handle
// this, as they can't do anything about it anyway.
//
// This path won't be taken in OOM situations, but only if for unknown
// reasons, we failed to properly allocate the memory map.
if e.status() == Status::BUFFER_TOO_SMALL {
panic!("Failed to get a proper allocation for the memory map");
}
}
let meta = meta?;

Ok(MemoryMapOwned::from_initialized_mem(buffer, meta))
}
Expand Down