Skip to content

can't get size of memory_map when it's a failed Result #1539

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

Closed
balenamiaa opened this issue Feb 12, 2025 · 4 comments
Closed

can't get size of memory_map when it's a failed Result #1539

balenamiaa opened this issue Feb 12, 2025 · 4 comments

Comments

@balenamiaa
Copy link

In case the status is Status::BUFFER_TOO_SMALL, map_size should be available. Perhaps through the Error's optional ErrData? Also, memory_map_size is internal, so, the only way seems to be directly using the raw methods in the boot table.

let memory_map = loop {
    let mut memory_map = uefi::boot::memory_map(MemoryType::LOADER_DATA);
    log::info!("memory_map: {:?}", memory_map);

    match memory_map {
        Ok(e) => break e,
        Err(e) => {
            if e.status() == Status::BUFFER_TOO_SMALL {
                log::info!("Buffer too small. Allocating more memory...");
                let memory_map_size = ???;
                memory_map = uefi::boot::allocate_pool(MemoryType::LOADER_DATA, ???);
            }
        }
    }
};
@phip1611
Copy link
Member

Hey! Did you actually experience situations where uefi::boot::memory_map(MemoryType::LOADER_DATA) returned Status::BUFFER_TOO_SMALL or is this just defensive programming?

Actually, uefi::boot::memory_map doesn't expect users to handle the Status::BUFFER_TOO_SMALL use-case due to advanced internal logic. I'm updating the doc

@balenamiaa
Copy link
Author

Hi! Yes, I have. Inside of a ExitBootServices hook that's called by winload.efi. What is meant by advanced internal logic? Regardless, thanks for the documentation update!

@phip1611
Copy link
Member

phip1611 commented Feb 14, 2025

Hey,

as you can see in #1540, you are not supposed to handle Status::BUFFER_TOO_SMALL when you use uefi::boot::memory_map().

The reason for that is that memory_map() internally already contains logic to have a large enough buffer. This complexity is taken away from users.

Does that help?

@balenamiaa
Copy link
Author

Hello! I understand that uefi::boot::memory_map() internally handles buffer sizing, but in my particular case, I needed more control.

I do not call/control uefi::boot::memory_map(); winload.efi calls ExitBootServices with the assumption the memory map is not stale, and it will be if allocations happen in what is supposed to be "ExitBootServices" (the hook) before calling the real function, While OslFwpKernelSetupPhase1 itself handles stale memory maps, taking extra precautions seemed safer then.

Anyway, as this is something rare, I don't think there's a need to complicate the API - as long as the raw tables continue to be exposed.

Thanks for your time and input on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants