lsefimmap.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* lsefimemmap.c - Display memory map. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/types.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/efi/api.h>
  23. #include <grub/efi/efi.h>
  24. #include <grub/command.h>
  25. GRUB_MOD_LICENSE ("GPLv3+");
  26. #define ADD_MEMORY_DESCRIPTOR(desc, size) \
  27. ((grub_efi_memory_descriptor_t *) ((char *) (desc) + (size)))
  28. static grub_err_t
  29. grub_cmd_lsefimmap (grub_command_t cmd __attribute__ ((unused)),
  30. int argc __attribute__ ((unused)),
  31. char **args __attribute__ ((unused)))
  32. {
  33. grub_efi_uintn_t map_size;
  34. grub_efi_memory_descriptor_t *memory_map;
  35. grub_efi_memory_descriptor_t *memory_map_end;
  36. grub_efi_memory_descriptor_t *desc;
  37. grub_efi_uintn_t desc_size;
  38. map_size = 0;
  39. if (grub_efi_get_memory_map (&map_size, NULL, NULL, &desc_size, 0) < 0)
  40. return 0;
  41. memory_map = grub_malloc (map_size);
  42. if (memory_map == NULL)
  43. return grub_errno;
  44. if (grub_efi_get_memory_map (&map_size, memory_map, NULL, &desc_size, 0) <= 0)
  45. goto fail;
  46. grub_printf
  47. ("Type Physical start - end #Pages "
  48. " Size Attributes\n");
  49. memory_map_end = ADD_MEMORY_DESCRIPTOR (memory_map, map_size);
  50. for (desc = memory_map;
  51. desc < memory_map_end;
  52. desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size))
  53. {
  54. grub_efi_uint64_t size;
  55. grub_efi_uint64_t attr;
  56. static const char types_str[][9] =
  57. {
  58. "reserved",
  59. "ldr-code",
  60. "ldr-data",
  61. "BS-code ",
  62. "BS-data ",
  63. "RT-code ",
  64. "RT-data ",
  65. "conv-mem",
  66. "unusable",
  67. "ACPI-rec",
  68. "ACPI-nvs",
  69. "MMIO ",
  70. "IO-ports",
  71. "PAL-code",
  72. "persist ",
  73. };
  74. if (desc->type < ARRAY_SIZE (types_str))
  75. grub_printf ("%s ", types_str[desc->type]);
  76. else
  77. grub_printf ("Unk %02x ", desc->type);
  78. grub_printf (" %016" PRIxGRUB_UINT64_T "-%016" PRIxGRUB_UINT64_T
  79. " %08" PRIxGRUB_UINT64_T,
  80. desc->physical_start,
  81. desc->physical_start + (desc->num_pages << 12) - 1,
  82. desc->num_pages);
  83. size = desc->num_pages << 12; /* 4 KiB page size */
  84. /*
  85. * Since size is a multiple of 4 KiB, no need to handle units
  86. * of just Bytes (which would use a mask of 0x3ff).
  87. *
  88. * 14 characters would support the largest possible number of 4 KiB
  89. * pages that are not a multiple of larger units (e.g., MiB):
  90. * 17592186044415 (0xffffff_fffff000), but that uses a lot of
  91. * whitespace for a rare case. 6 characters usually suffices;
  92. * columns will be off if not, but this is preferable to rounding.
  93. */
  94. if (size & 0xfffff)
  95. grub_printf (" %6" PRIuGRUB_UINT64_T "KiB", size >> 10);
  96. else if (size & 0x3fffffff)
  97. grub_printf (" %6" PRIuGRUB_UINT64_T "MiB", size >> 20);
  98. else if (size & 0xffffffffff)
  99. grub_printf (" %6" PRIuGRUB_UINT64_T "GiB", size >> 30);
  100. else if (size & 0x3ffffffffffff)
  101. grub_printf (" %6" PRIuGRUB_UINT64_T "TiB", size >> 40);
  102. else if (size & 0xfffffffffffffff)
  103. grub_printf (" %6" PRIuGRUB_UINT64_T "PiB", size >> 50);
  104. else
  105. grub_printf (" %6" PRIuGRUB_UINT64_T "EiB", size >> 60);
  106. attr = desc->attribute;
  107. if (attr & GRUB_EFI_MEMORY_RUNTIME)
  108. grub_printf (" RT");
  109. if (attr & GRUB_EFI_MEMORY_UC)
  110. grub_printf (" UC");
  111. if (attr & GRUB_EFI_MEMORY_WC)
  112. grub_printf (" WC");
  113. if (attr & GRUB_EFI_MEMORY_WT)
  114. grub_printf (" WT");
  115. if (attr & GRUB_EFI_MEMORY_WB)
  116. grub_printf (" WB");
  117. if (attr & GRUB_EFI_MEMORY_UCE)
  118. grub_printf (" UCE");
  119. if (attr & GRUB_EFI_MEMORY_WP)
  120. grub_printf (" WP");
  121. if (attr & GRUB_EFI_MEMORY_RP)
  122. grub_printf (" RP");
  123. if (attr & GRUB_EFI_MEMORY_XP)
  124. grub_printf (" XP");
  125. if (attr & GRUB_EFI_MEMORY_NV)
  126. grub_printf (" NV");
  127. if (attr & GRUB_EFI_MEMORY_MORE_RELIABLE)
  128. grub_printf (" MR");
  129. if (attr & GRUB_EFI_MEMORY_RO)
  130. grub_printf (" RO");
  131. grub_printf ("\n");
  132. }
  133. fail:
  134. grub_free (memory_map);
  135. return 0;
  136. }
  137. static grub_command_t cmd;
  138. GRUB_MOD_INIT(lsefimmap)
  139. {
  140. cmd = grub_register_command ("lsefimmap", grub_cmd_lsefimmap,
  141. "", "Display EFI memory map.");
  142. }
  143. GRUB_MOD_FINI(lsefimmap)
  144. {
  145. grub_unregister_command (cmd);
  146. }