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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
#include <Uefi.h>
#include <Library/UefiLib.h>
#include <Library/UefiApplicationEntryPoint.h>
#define DATA_SIZE 1000
EFI_MEMORY_DESCRIPTOR getNextElement(EFI_MEMORY_DESCRIPTOR *memoryMap) { // TODO
return *memoryMap;
}
const CHAR16 *memory_types[] =
{
L"EfiReservedMemoryType",
L"EfiLoaderCode",
L"EfiLoaderData",
L"EfiBootServicesCode",
L"EfiBootServicesData",
L"EfiRuntimeServicesCode",
L"EfiRuntimeServicesData",
L"EfiConventionalMemory",
L"EfiUnusableMemory",
L"EfiACPIReclaimMemory",
L"EfiACPIMemoryNVS",
L"EfiMemoryMappedIO",
L"EfiMemoryMappedIOPortSpace",
L"EfiPalCode",
};
EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
Print(L"Application to get Memory Map\n\n");
UINTN MemoryMapSize = 0;
EFI_MEMORY_DESCRIPTOR *memoryMap = NULL;
UINTN LocalMapKey;
UINTN DescriptorSize;
UINT32 DescriptorVersion;
EFI_STATUS Status;
EFI_BOOT_SERVICES *bs = SystemTable->BootServices;
/* Allocate the buffer for the MemoryMap. As the memory map changes every time we query it, the
size also may change. So we allocate a new buffer until it has sufficient space for the MemoryMap.
*/
Status = bs->GetMemoryMap(&MemoryMapSize, memoryMap, &LocalMapKey, &DescriptorSize, &DescriptorVersion);
do {
bs->FreePool(memoryMap);
Status = bs->AllocatePool(EfiLoaderData, (UINTN) (MemoryMapSize + 1) , (void **)&memoryMap);
Status = bs->GetMemoryMap(&MemoryMapSize, memoryMap, &LocalMapKey, &DescriptorSize, &DescriptorVersion);
} while (Status == EFI_BUFFER_TOO_SMALL);
if (Status != EFI_SUCCESS) {
Print(L"Status = %r\n", Status);
return Status;
}
Print(L"memoryMap location: %p\n", memoryMap);
//Print(L"Size of first element (and all other elements): %d\n", sizeof(secondElement));
Print(L"DescriptorSize: %d\n", DescriptorSize);
Print(L"DescriptorVersion: %d\n\n\n", DescriptorVersion);
UINT8 *startOfMemoryMap = (UINT8 *)memoryMap;
UINT8 *endOfMemoryMap = startOfMemoryMap + MemoryMapSize;
UINT8 *offset = startOfMemoryMap;
UINT32 counter = 0;
UINT64 totalPages = 0;
EFI_MEMORY_DESCRIPTOR *desc = NULL;
while(offset < endOfMemoryMap) {
desc = (EFI_MEMORY_DESCRIPTOR *)offset;
Print(L"Map %d:\n", counter);
Print(L" Type: %X, %s\n", desc->Type, memory_types[desc->Type]);
Print(L" PhysicalStart: %X\n", desc->PhysicalStart);
Print(L" VirtualStart: %X\n", desc->VirtualStart);
Print(L" NumberOfPages: %X (4k)\n", desc->NumberOfPages);
Print(L" Attribute: %X\n", desc->Attribute);
totalPages += desc->NumberOfPages;
offset += DescriptorSize;
counter++;
}
bs->FreePool(memoryMap);
MemoryMapSize = totalPages * 4096;
Print(L"Memory detected: %d MB\n\n", MemoryMapSize / 1024 / 1024);
return EFI_SUCCESS;
}
|