summaryrefslogtreecommitdiff
path: root/core/memmap.c
blob: f02e5380b77a8dbfcb737e1bc8667009d6d79d32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
 * Copyright (c) 2026, Apollo Telephone Laboratories.
 * Provided under the BSD-3 clause.
 */

#include <sboot/memmap.h>

EFI_STATUS
SBootGetMemmap(OUT PLATFORM_MEMMAP *Map)
{
    EFI_STATUS Status;

    if (Map == NULL) {
        return EFI_INVALID_PARAMETER;
    }

    Map->Map = NULL;
    Map->MapSize = 0;
    Map->MapKey = 0;
    Map->DescriptorSize = 0;
    Map->DescriptorVersion = 0;

    Status = uefi_call_wrapper(
        gBS->GetMemoryMap,
        5,
        &Map->MapSize,
        Map->Map,
        &Map->MapKey,
        &Map->DescriptorSize,
        &Map->DescriptorVersion
    );

    Map->Map = AllocatePool(Map->MapSize);
    if (Map->Map == NULL) {
        return EFI_OUT_OF_RESOURCES;
    }

    /*
     * We have to call this twice as the act of allocating memory
     * alters the memory map.
     */
    for (UINTN Idx = 0; Idx < 2; ++Idx) {
        Status = uefi_call_wrapper(
            gBS->GetMemoryMap,
            5,
            &Map->MapSize,
            Map->Map,
            &Map->MapKey,
            &Map->DescriptorSize,
            &Map->DescriptorVersion
        );

        if (!EFI_ERROR(Status)) {
            break;
        }
    }

    if (EFI_ERROR(Status)) {
        Print(L"fatal: failed to get memory map\n");
    }

    return EFI_SUCCESS;
}