/* lsefimemmap.c - Display memory map. */ /* * VasEBoot -- GRand Unified Bootloader * Copyright (C) 2008 Free Software Foundation, Inc. * * VasEBoot is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VasEBoot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VasEBoot. If not, see . */ #include #include #include #include #include #include VasEBoot_MOD_LICENSE ("GPLv3+"); #define ADD_MEMORY_DESCRIPTOR(desc, size) \ ((VasEBoot_efi_memory_descriptor_t *) ((char *) (desc) + (size))) static VasEBoot_err_t VasEBoot_cmd_lsefimmap (VasEBoot_command_t cmd __attribute__ ((unused)), int argc __attribute__ ((unused)), char **args __attribute__ ((unused))) { VasEBoot_efi_uintn_t map_size; VasEBoot_efi_memory_descriptor_t *memory_map; VasEBoot_efi_memory_descriptor_t *memory_map_end; VasEBoot_efi_memory_descriptor_t *desc; VasEBoot_efi_uintn_t desc_size; map_size = 0; if (VasEBoot_efi_get_memory_map (&map_size, NULL, NULL, &desc_size, 0) < 0) return 0; memory_map = VasEBoot_malloc (map_size); if (memory_map == NULL) return VasEBoot_errno; if (VasEBoot_efi_get_memory_map (&map_size, memory_map, NULL, &desc_size, 0) <= 0) goto fail; VasEBoot_printf ("Type Physical start - end #Pages " " Size Attributes\n"); memory_map_end = ADD_MEMORY_DESCRIPTOR (memory_map, map_size); for (desc = memory_map; desc < memory_map_end; desc = ADD_MEMORY_DESCRIPTOR (desc, desc_size)) { VasEBoot_efi_uint64_t size; VasEBoot_efi_uint64_t attr; static const char types_str[][9] = { "reserved", "ldr-code", "ldr-data", "BS-code ", "BS-data ", "RT-code ", "RT-data ", "conv-mem", "unusable", "ACPI-rec", "ACPI-nvs", "MMIO ", "IO-ports", "PAL-code", "persist ", }; if (desc->type < ARRAY_SIZE (types_str)) VasEBoot_printf ("%s ", types_str[desc->type]); else VasEBoot_printf ("Unk %02x ", desc->type); VasEBoot_printf (" %016" PRIxVasEBoot_UINT64_T "-%016" PRIxVasEBoot_UINT64_T " %08" PRIxVasEBoot_UINT64_T, desc->physical_start, desc->physical_start + (desc->num_pages << 12) - 1, desc->num_pages); size = desc->num_pages << 12; /* 4 KiB page size */ /* * Since size is a multiple of 4 KiB, no need to handle units * of just Bytes (which would use a mask of 0x3ff). * * 14 characters would support the largest possible number of 4 KiB * pages that are not a multiple of larger units (e.g., MiB): * 17592186044415 (0xffffff_fffff000), but that uses a lot of * whitespace for a rare case. 6 characters usually suffices; * columns will be off if not, but this is preferable to rounding. */ if (size & 0xfffff) VasEBoot_printf (" %6" PRIuVasEBoot_UINT64_T "KiB", size >> 10); else if (size & 0x3fffffff) VasEBoot_printf (" %6" PRIuVasEBoot_UINT64_T "MiB", size >> 20); else if (size & 0xffffffffff) VasEBoot_printf (" %6" PRIuVasEBoot_UINT64_T "GiB", size >> 30); else if (size & 0x3ffffffffffff) VasEBoot_printf (" %6" PRIuVasEBoot_UINT64_T "TiB", size >> 40); else if (size & 0xfffffffffffffff) VasEBoot_printf (" %6" PRIuVasEBoot_UINT64_T "PiB", size >> 50); else VasEBoot_printf (" %6" PRIuVasEBoot_UINT64_T "EiB", size >> 60); attr = desc->attribute; if (attr & VasEBoot_EFI_MEMORY_RUNTIME) VasEBoot_printf (" RT"); if (attr & VasEBoot_EFI_MEMORY_UC) VasEBoot_printf (" UC"); if (attr & VasEBoot_EFI_MEMORY_WC) VasEBoot_printf (" WC"); if (attr & VasEBoot_EFI_MEMORY_WT) VasEBoot_printf (" WT"); if (attr & VasEBoot_EFI_MEMORY_WB) VasEBoot_printf (" WB"); if (attr & VasEBoot_EFI_MEMORY_UCE) VasEBoot_printf (" UCE"); if (attr & VasEBoot_EFI_MEMORY_WP) VasEBoot_printf (" WP"); if (attr & VasEBoot_EFI_MEMORY_RP) VasEBoot_printf (" RP"); if (attr & VasEBoot_EFI_MEMORY_XP) VasEBoot_printf (" XP"); if (attr & VasEBoot_EFI_MEMORY_NV) VasEBoot_printf (" NV"); if (attr & VasEBoot_EFI_MEMORY_MORE_RELIABLE) VasEBoot_printf (" MR"); if (attr & VasEBoot_EFI_MEMORY_RO) VasEBoot_printf (" RO"); VasEBoot_printf ("\n"); } fail: VasEBoot_free (memory_map); return 0; } static VasEBoot_command_t cmd; VasEBoot_MOD_INIT(lsefimmap) { cmd = VasEBoot_register_command ("lsefimmap", VasEBoot_cmd_lsefimmap, "", "Display EFI memory map."); } VasEBoot_MOD_FINI(lsefimmap) { VasEBoot_unregister_command (cmd); }