I wrote uefi app and it get compiled successfully but when I used qemu to test it, I got incorrect numbers amount of memory (256 MB).
definition of EFI_MEMORY_DESCRIPTOR:
///
/// Definition of an EFI memory descriptor.
///
typedef struct {
///
/// Type of the memory region.
/// Type EFI_MEMORY_TYPE is defined in the
/// AllocatePages() function description.
///
UINT32 Type;
///
/// Physical address of the first byte in the memory region. PhysicalStart must be
/// aligned on a 4 KiB boundary, and must not be above 0xfffffffffffff000. Type
/// EFI_PHYSICAL_ADDRESS is defined in the AllocatePages() function description
///
EFI_PHYSICAL_ADDRESS PhysicalStart;
///
/// Virtual address of the first byte in the memory region.
/// VirtualStart must be aligned on a 4 KiB boundary,
/// and must not be above 0xfffffffffffff000.
///
EFI_VIRTUAL_ADDRESS VirtualStart;
///
/// NumberOfPagesNumber of 4 KiB pages in the memory region.
/// NumberOfPages must not be 0, and must not be any value
/// that would represent a memory page with a start address,
/// either physical or virtual, above 0xfffffffffffff000.
///
UINT64 NumberOfPages;
///
/// Attributes of the memory region that describe the bit mask of capabilities
/// for that memory region, and not necessarily the current settings for that
/// memory region.
///
UINT64 Attribute;
} EFI_MEMORY_DESCRIPTOR;
Code:
UINT64
GMS(EFI_MEMORY_DESCRIPTOR* MMap, UINT64 MMAPE, UINT64 MMAPDESCSIZE)
{
static UINT64 MemSizeB = 0;
if (MemSizeB > 0) return MemSizeB;
for (unsigned int i = 0; i < MMAPE; i++){
EFI_MEMORY_DESCRIPTOR *desc = (EFI_MEMORY_DESCRIPTOR*)((UINT64*)MMap + i * MMAPDESCSIZE);
MemSizeB += desc->NumberOfPages * EFI_PAGE_SIZE;
}
return MemSizeB;
}
EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_MEMORY_DESCRIPTOR* Map = NULL;
UINTN MapSize, MapKey, DescSize;
UINT32 DescVer;
{
SystemTable->BootServices->GetMemoryMap(&MapSize, Map, &MapKey, &DescSize, &DescVer);
SystemTable->BootServices->AllocatePool(EfiLoaderData, MapSize, (void**)&Map);
SystemTable->BootServices->GetMemoryMap(&MapSize, Map, &MapKey, &DescSize, &DescVer);
}
UINT64 MapEntries = MapSize / DescSize;
Print(L"Map Entries: %dn", MapEntries);
Print(L"Map Size: %dn", MapSize);
Print(L"Descriptor Size: %dn", DescSize);
Print(L"Size of memory: %dn", GMS(Map, MapEntries, DescSize));
return EFI_SUCCESS;
}
I use GCC for compiling and edk2 (edk2-stable202411) for creating UEFI app.
Big thank to someone who could help me
tried using EFI_PAGE_SIZE
but problem didn’t quit