lsefimmap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. };
  73. if (desc->type < ARRAY_SIZE (types_str))
  74. grub_printf ("%s ", types_str[desc->type]);
  75. else
  76. grub_printf ("Unk %02x ", desc->type);
  77. grub_printf (" %016" PRIxGRUB_UINT64_T "-%016" PRIxGRUB_UINT64_T
  78. " %08" PRIxGRUB_UINT64_T,
  79. desc->physical_start,
  80. desc->physical_start + (desc->num_pages << 12) - 1,
  81. desc->num_pages);
  82. size = desc->num_pages;
  83. size <<= (12 - 10);
  84. if (size < 1024)
  85. grub_printf (" %4" PRIuGRUB_UINT64_T "KB", size);
  86. else
  87. {
  88. size /= 1024;
  89. if (size < 1024)
  90. grub_printf (" %4" PRIuGRUB_UINT64_T "MB", size);
  91. else
  92. {
  93. size /= 1024;
  94. grub_printf (" %4" PRIuGRUB_UINT64_T "GB", size);
  95. }
  96. }
  97. attr = desc->attribute;
  98. if (attr & GRUB_EFI_MEMORY_RUNTIME)
  99. grub_printf (" RT");
  100. if (attr & GRUB_EFI_MEMORY_UC)
  101. grub_printf (" UC");
  102. if (attr & GRUB_EFI_MEMORY_WC)
  103. grub_printf (" WC");
  104. if (attr & GRUB_EFI_MEMORY_WT)
  105. grub_printf (" WT");
  106. if (attr & GRUB_EFI_MEMORY_WB)
  107. grub_printf (" WB");
  108. if (attr & GRUB_EFI_MEMORY_UCE)
  109. grub_printf (" UCE");
  110. if (attr & GRUB_EFI_MEMORY_WP)
  111. grub_printf (" WP");
  112. if (attr & GRUB_EFI_MEMORY_RP)
  113. grub_printf (" RP");
  114. if (attr & GRUB_EFI_MEMORY_XP)
  115. grub_printf (" XP");
  116. grub_printf ("\n");
  117. }
  118. fail:
  119. grub_free (memory_map);
  120. return 0;
  121. }
  122. static grub_command_t cmd;
  123. GRUB_MOD_INIT(lsefimmap)
  124. {
  125. cmd = grub_register_command ("lsefimmap", grub_cmd_lsefimmap,
  126. "", "Display EFI memory map.");
  127. }
  128. GRUB_MOD_FINI(lsefimmap)
  129. {
  130. grub_unregister_command (cmd);
  131. }