Detecting Memory (x86): Difference between revisions

Add section about EFI memory map
[unchecked revision][unchecked revision]
(→‎Manual Probing: Further explanation about what are "unpredictable results" and "damage your system permanently".)
(Add section about EFI memory map)
Line 255:
{{Warning|This could possibly damage your computer.}}
Use BIOS to get a memory map, or use [[GRUB]] (which calls BIOS for you). Memory probing can have results that are '''unpredictable by nature''' because memory probing is unsupported by vendors.
 
==== What about on UEFI? ====
On UEFI, you BootServices->GetMemoryMap. This function is easier to use then E820 and is the solution on new UEFI machines. Basically, to use, first you call once to get the size of the memory map. Then you allocate a buffer of that size, and then call again to get the map itself. For example:
<source lang="c">
UINTN mapSize, mapKey, descSize, descVer = 0;
BS->GetMemoryMap(&mapSize, NULL, &mapKey, &descSize, (UINT32*)&descVer);
mapSize += 16 * sizeof(EFI_MEMORY_DESCRIPTOR);
QWORD memMap = (QWORD)AllocMem(mapSize);
BS->GetMemoryMap(&mapSize, (EFI_MEMORY_DESCRIPTOR*)memMap, &mapKey, &descSize, (UINT32*)&descVer);
</source>
It returns an array of EFI_MEMORY_DESCRIPTORs. They have the following format (taken from GNU EFI):
<source lang="c">
typedef struct {
UINT32 Type; // Field size is 32 bits followed by 32 bit pad
UINT32 Pad;
EFI_PHYSICAL_ADDRESS PhysicalStart; // Field size is 64 bits
EFI_VIRTUAL_ADDRESS VirtualStart; // Field size is 64 bits
UINT64 NumberOfPages; // Field size is 64 bits
UINT64 Attribute; // Field size is 64 bits
} EFI_MEMORY_DESCRIPTOR;
</source>
TODO: Explain types, and go into more detail about it.
 
===== Theoretical introduction =====
Anonymous user