posix-uefi/examples/0D_memmap/memmap.c

54 lines
1.7 KiB
C

#include <uefi.h>
/**
* List memory map
*/
int main(int argc, char **argv)
{
(void)argc;
(void)argv;
efi_status_t status;
efi_memory_descriptor_t *memory_map = NULL, *mement;
uintn_t memory_map_size=0, map_key=0, desc_size=0;
const char *types[] = {
"EfiReservedMemoryType",
"EfiLoaderCode",
"EfiLoaderData",
"EfiBootServicesCode",
"EfiBootServicesData",
"EfiRuntimeServicesCode",
"EfiRuntimeServicesData",
"EfiConventionalMemory",
"EfiUnusableMemory",
"EfiACPIReclaimMemory",
"EfiACPIMemoryNVS",
"EfiMemoryMappedIO",
"EfiMemoryMappedIOPortSpace",
"EfiPalCode"
};
/* get the memory map */
status = BS->GetMemoryMap(&memory_map_size, NULL, &map_key, &desc_size, NULL);
if(status != EFI_BUFFER_TOO_SMALL || !memory_map_size) goto err;
/* in worst case malloc allocates two blocks, and each block might split a record into three, that's 4 additional records */
memory_map_size += 4 * desc_size;
memory_map = (efi_memory_descriptor_t*)malloc(memory_map_size);
if(!memory_map) {
fprintf(stderr, "unable to allocate memory\n");
return 1;
}
status = BS->GetMemoryMap(&memory_map_size, memory_map, &map_key, &desc_size, NULL);
if(EFI_ERROR(status)) {
err: fprintf(stderr, "Unable to get memory map\n");
return 0;
}
printf("Address Size Type\n");
for(mement = memory_map; (uint8_t*)mement < (uint8_t*)memory_map + memory_map_size;
mement = NextMemoryDescriptor(mement, desc_size)) {
printf("%016x %8d %02x %s\n", mement->PhysicalStart, mement->NumberOfPages, mement->Type, types[mement->Type]);
}
free(memory_map);
return 0;
}