|
| 1 | +#include "efi.h" |
| 2 | + |
| 3 | +#define MEMORY_MAP_ENTRIES 64 |
| 4 | + |
| 5 | +EFISystemTable* _system_table; |
| 6 | +EFIMemoryDescriptor memory_map[MEMORY_MAP_ENTRIES]; |
| 7 | +EFI_UINTN memory_map_used; |
| 8 | + |
| 9 | +bool IsEqualStringWithSize(const char* s1, const char* s2, int n) { |
| 10 | + for (int i = 0; i < n; i++) { |
| 11 | + if (s1[i] != s2[i]) |
| 12 | + return false; |
| 13 | + } |
| 14 | + return true; |
| 15 | +} |
| 16 | + |
| 17 | +void EFIInit(struct EFI_SYSTEM_TABLE* system_table) { |
| 18 | + _system_table = system_table; |
| 19 | +} |
| 20 | + |
| 21 | +void EFIClearScreen() { |
| 22 | + _system_table->con_out->clear_screen(_system_table->con_out); |
| 23 | +} |
| 24 | + |
| 25 | +void EFIPutString(wchar_t* s) { |
| 26 | + _system_table->con_out->output_string(_system_table->con_out, s); |
| 27 | +} |
| 28 | + |
| 29 | +void EFIPutCString(char* s) { |
| 30 | + wchar_t buf[2]; |
| 31 | + buf[1] = 0; |
| 32 | + while (*s) { |
| 33 | + buf[0] = *s; |
| 34 | + _system_table->con_out->output_string(_system_table->con_out, buf); |
| 35 | + s++; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void EFIPutnCString(char* s, int n) { |
| 40 | + wchar_t buf[2]; |
| 41 | + buf[1] = 0; |
| 42 | + for (int i = 0; i < n; i++) { |
| 43 | + buf[0] = s[i]; |
| 44 | + _system_table->con_out->output_string(_system_table->con_out, buf); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void EFIGetMemoryMap() { |
| 49 | + EFI_UINTN map_key; |
| 50 | + EFI_UINTN desc_size; |
| 51 | + uint32_t desc_version; |
| 52 | + EFI_UINTN memory_map_byte_size = |
| 53 | + sizeof(EFIMemoryDescriptor) * MEMORY_MAP_ENTRIES; |
| 54 | + _system_table->boot_services->GetMemoryMap( |
| 55 | + &memory_map_byte_size, memory_map, &map_key, &desc_size, &desc_version); |
| 56 | + memory_map_used = memory_map_byte_size / desc_size; |
| 57 | +} |
| 58 | + |
| 59 | +void EFIPrintHex64(uint64_t value) { |
| 60 | + int i; |
| 61 | + wchar_t s[2]; |
| 62 | + s[1] = 0; |
| 63 | + for (i = 15; i > 0; i--) { |
| 64 | + if ((value >> (4 * i)) & 0xF) |
| 65 | + break; |
| 66 | + } |
| 67 | + for (; i >= 0; i--) { |
| 68 | + s[0] = (value >> (4 * i)) & 0xF; |
| 69 | + if (s[0] < 10) |
| 70 | + s[0] += '0'; |
| 71 | + else |
| 72 | + s[0] += 'A' - 10; |
| 73 | + EFIPutString(s); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void EFIPrintStringAndHex(wchar_t* s, uint64_t value) { |
| 78 | + EFIPutString(s); |
| 79 | + EFIPutString(L": 0x"); |
| 80 | + EFIPrintHex64(value); |
| 81 | + EFIPutString(L"\r\n"); |
| 82 | +} |
| 83 | + |
| 84 | +void EFIPrintMemoryDescriptor(EFIMemoryDescriptor* desc) { |
| 85 | + EFIPutString(L" Type "); |
| 86 | + EFIPrintHex64(desc->type); |
| 87 | + EFIPutString(L" Phys "); |
| 88 | + EFIPrintHex64(desc->physical_start); |
| 89 | + EFIPutString(L" Virt "); |
| 90 | + EFIPrintHex64(desc->virtual_start); |
| 91 | + EFIPutString(L" NumOfPages "); |
| 92 | + EFIPrintHex64(desc->number_of_pages); |
| 93 | + EFIPutString(L" Attr "); |
| 94 | + EFIPrintHex64(desc->attribute); |
| 95 | +} |
| 96 | + |
| 97 | +void EFIPrintMemoryMap() { |
| 98 | + EFIPrintStringAndHex(L"Map entries: ", memory_map_used); |
| 99 | + for (int i = 0; i < memory_map_used; i++) { |
| 100 | + EFIPutString(L"#"); |
| 101 | + EFIPrintHex64(i); |
| 102 | + EFIPrintMemoryDescriptor(&memory_map[i]); |
| 103 | + EFIPutString(L"\r\n"); |
| 104 | + } |
| 105 | + EFIPutString(L"NV Map entries: \r\n"); |
| 106 | + for (int i = 0; i < memory_map_used; i++) { |
| 107 | + if (!(memory_map[i].attribute & EFI_MEMORY_NV)) |
| 108 | + continue; |
| 109 | + EFIPutString(L"#"); |
| 110 | + EFIPrintHex64(i); |
| 111 | + EFIPrintMemoryDescriptor(&memory_map[i]); |
| 112 | + EFIPutString(L"\r\n"); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void* EFIGetConfigurationTableByUUID(const GUID* guid) { |
| 117 | + for (int i = 0; i < _system_table->number_of_table_entries; i++) { |
| 118 | + if (IsEqualGUID(guid, &_system_table->configuration_table[i].vendor_guid)) |
| 119 | + return _system_table->configuration_table[i].vendor_table; |
| 120 | + } |
| 121 | + return NULL; |
| 122 | +} |
0 commit comments