memmap.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include <uefi.h>
  2. /**
  3. * List memory map
  4. */
  5. int main(int argc, char **argv)
  6. {
  7. (void)argc;
  8. (void)argv;
  9. efi_status_t status;
  10. efi_memory_descriptor_t *memory_map = NULL, *mement;
  11. uintn_t memory_map_size=0, map_key=0, desc_size=0;
  12. const char *types[] = {
  13. "EfiReservedMemoryType",
  14. "EfiLoaderCode",
  15. "EfiLoaderData",
  16. "EfiBootServicesCode",
  17. "EfiBootServicesData",
  18. "EfiRuntimeServicesCode",
  19. "EfiRuntimeServicesData",
  20. "EfiConventionalMemory",
  21. "EfiUnusableMemory",
  22. "EfiACPIReclaimMemory",
  23. "EfiACPIMemoryNVS",
  24. "EfiMemoryMappedIO",
  25. "EfiMemoryMappedIOPortSpace",
  26. "EfiPalCode"
  27. };
  28. /* get the memory map */
  29. status = BS->GetMemoryMap(&memory_map_size, NULL, &map_key, &desc_size, NULL);
  30. if(status != EFI_BUFFER_TOO_SMALL || !memory_map_size) goto err;
  31. /* in worst case malloc allocates two blocks, and each block might split a record into three, that's 4 additional records */
  32. memory_map_size += 4 * desc_size;
  33. memory_map = (efi_memory_descriptor_t*)malloc(memory_map_size);
  34. if(!memory_map) {
  35. fprintf(stderr, "unable to allocate memory\n");
  36. return 1;
  37. }
  38. status = BS->GetMemoryMap(&memory_map_size, memory_map, &map_key, &desc_size, NULL);
  39. if(EFI_ERROR(status)) {
  40. err: fprintf(stderr, "Unable to get memory map\n");
  41. return 0;
  42. }
  43. printf("Address Size Type\n");
  44. for(mement = memory_map; (uint8_t*)mement < (uint8_t*)memory_map + memory_map_size;
  45. mement = NextMemoryDescriptor(mement, desc_size)) {
  46. printf("%016x %8d %02x %s\n", mement->PhysicalStart, mement->NumberOfPages, mement->Type, types[mement->Type]);
  47. }
  48. free(memory_map);
  49. return 0;
  50. }