refactor(kernel/efi): improve code and add more debug messages

This commit is contained in:
EnderIce2 2025-04-19 19:18:45 +00:00
parent fe8682aa85
commit 80c313b02d
Signed by: enderice2
GPG Key ID: FEB6B8A8507BA62E
5 changed files with 191 additions and 17 deletions

View File

@ -120,6 +120,13 @@ License information can be found in the [LICENSES.md](LICENSES.md) file.
## UART
- [Interfacing the Serial / RS232 Port V5.0](http://www.senet.com.au/~cpeacock)
## UEFI
- [U-Boot EFI Commands](https://docs.u-boot.org/en/latest/usage/cmd/efi.html)
- [UEFI Specification 2.10](https://uefi.org/sites/default/files/resources/UEFI_Spec_2_10_Aug29.pdf)
- [UEFI Boot Process Overview](https://gist.github.com/Velocet/d394281d96191e235ff46a8aa2018d80)
- [Rust OS Development: UEFI](https://blog.malware.re/2023/09/01/rust-os-part2/index.html)
- [GUIDs Database](https://github.com/DSecurity/efiSeek/blob/master/data/guids-db.ini)
---
Special thanks to all contributors and the creators of the referenced projects and resources!

View File

@ -23,7 +23,8 @@
"kernel/tty",
"kernel/std",
"kernel/vfs",
"kernel/memory"
"kernel/memory",
"kernel/efi"
]
}
}

View File

@ -24,18 +24,26 @@ extern struct BootInfo bInfo;
VOID SearchSMBIOS(EFI_SYSTEM_TABLE *SystemTable)
{
EFI_GUID Smbios3TableGuid = {0xf2fd1544, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94}};
EFI_GUID SmbiosTableGuid = {0xEB9D2D31, 0x2D88, 0x11D3, {0x9A, 0x16, 0x00, 0x90, 0x27, 0x3F, 0xC1, 0x4D}};
EFI_GUID Smbios3TableGuid = SMBIOS3_TABLE_GUID;
EFI_GUID SmbiosTableGuid = SMBIOS_TABLE_GUID;
for (UINTN i = 0; i < SystemTable->NumberOfTableEntries; i++)
{
EFI_CONFIGURATION_TABLE *config = &SystemTable->ConfigurationTable[i];
if (CompareGuid(&config->VendorGuid, &Smbios3TableGuid) ||
CompareGuid(&config->VendorGuid, &SmbiosTableGuid))
/* Can a device have multiple smbios tables? If so, use SMBIOS 3.0 over <2.0 */
if (CompareGuid(&config->VendorGuid, &SmbiosTableGuid))
{
bInfo.SMBIOSPtr = config->VendorTable;
debug("SMBIOS EPS found at address: %#lx", bInfo.SMBIOSPtr);
debug("SMBIOS found at address: %#lx", bInfo.SMBIOSPtr);
continue;
}
if (CompareGuid(&config->VendorGuid, &Smbios3TableGuid))
{
bInfo.SMBIOSPtr = config->VendorTable;
debug("SMBIOS3 found at address: %#lx", bInfo.SMBIOSPtr);
return;
}
}
@ -43,8 +51,7 @@ VOID SearchSMBIOS(EFI_SYSTEM_TABLE *SystemTable)
VOID SearchRSDP(EFI_SYSTEM_TABLE *SystemTable)
{
EFI_GUID AcpiTableGuid = {0x8868E871, 0xE4F1, 0x11D3, {0xBC, 0x22, 0x00, 0x80, 0xC7, 0x3C, 0x88, 0x81}};
EFI_GUID AcpiTableGuid = EFI_ACPI_TABLE_GUID;
for (UINTN i = 0; i < SystemTable->NumberOfTableEntries; i++)
{
EFI_CONFIGURATION_TABLE *config = &SystemTable->ConfigurationTable[i];
@ -84,10 +91,27 @@ VOID InitializeMemoryEntries(EFI_MEMORY_DESCRIPTOR *MemoryMap, UINTN NumberOfEnt
"MemoryMappedIOPortSpace",
"PalCode",
"PersistentMemory",
"MaxMemoryType"};
"MaxMemoryType",
"out of bounds?!"};
size_t type = desc->Type;
if (type > sizeof(EFI_MEMORY_TYPE_STRINGS) / sizeof(EFI_MEMORY_TYPE_STRINGS[0]))
{
type = 16;
debug("oh uh, %d is out of bounds!!! t:%#lx p:%#lx v:%#lx n:%lu a:%lx",
i, desc->Type,
desc->PhysicalStart,
desc->VirtualStart,
desc->NumberOfPages,
desc->Attribute);
}
debug("Entry %d: Type: %s, PhysicalStart: %p, VirtualStart: %p, NumberOfPages: %lu, Attribute: %lx",
i, EFI_MEMORY_TYPE_STRINGS[desc->Type], desc->PhysicalStart, desc->VirtualStart, desc->NumberOfPages, desc->Attribute);
i, EFI_MEMORY_TYPE_STRINGS[type],
desc->PhysicalStart,
desc->VirtualStart,
desc->NumberOfPages,
desc->Attribute);
#endif
}
}
@ -125,6 +149,90 @@ VOID InitializeMemoryNoBS()
EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
#ifdef DEBUG
if (bInfo.EFI.Info.ST)
{
EFI_GUID EfiAcpi20Table = EFI_ACPI_20_TABLE_GUID;
EFI_GUID AcpiTable = ACPI_TABLE_GUID;
EFI_GUID SalSystemTable = SAL_SYSTEM_TABLE_GUID;
EFI_GUID SmbiosTable = SMBIOS_TABLE_GUID;
EFI_GUID Smbios3Table = SMBIOS3_TABLE_GUID;
EFI_GUID MpsTable = MPS_TABLE_GUID;
EFI_GUID EfiAcpiTable = EFI_ACPI_TABLE_GUID;
EFI_GUID EfiLzmaCompressed = EFI_LZMA_COMPRESSED_GUID;
EFI_GUID EfiDxeServices = EFI_DXE_SERVICES_GUID;
EFI_GUID EfiHobList = EFI_HOB_LIST_GUID;
EFI_GUID EfiMemoryType = _EFI_MEMORY_TYPE_GUID;
EFI_GUID EfiDebugImageInfoTable = EFI_DEBUG_IMAGE_INFO_TABLE_GUID;
EFI_GUID EfiMemStatusCodeRec = EFI_MEM_STATUS_CODE_REC_GUID;
EFI_GUID EfiGuidEfiAcpi1 = EFI_GUID_EFI_ACPI1_GUID;
EFI_GUID EfiMemoryAttributesTable = EFI_MEMORY_ATTRIBUTES_TABLE_GUID;
EFI_GUID EfiHiiDatabaseProtocol = EFI_HII_DATABASE_PROTOCOL_GUID;
EFI_GUID EfiHiiConfigRoutingProtocol = EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID;
EFI_GUID TCG2FinalEventsTable = TCG2_FINAL_EVENTS_TABLE_GUID;
EFI_GUID EfiImageSecurityDatabase = EFI_IMAGE_SECURITY_DATABASE_GUID;
debug("there are %d configuration tables", SystemTable->NumberOfTableEntries);
for (UINTN i = 0; i < SystemTable->NumberOfTableEntries; i++)
{
EFI_CONFIGURATION_TABLE *config = &SystemTable->ConfigurationTable[i];
EFI_GUID *g = &config->VendorGuid;
void *addr = config->VendorTable;
const char *guid_str = NULL;
if (CompareGuid(g, &EfiAcpi20Table))
guid_str = "EFI ACPI 2.0 Table";
else if (CompareGuid(g, &AcpiTable))
guid_str = "ACPI Table";
else if (CompareGuid(g, &SalSystemTable))
guid_str = "SAL System Table";
else if (CompareGuid(g, &SmbiosTable))
guid_str = "SMBIOS Table";
else if (CompareGuid(g, &Smbios3Table))
guid_str = "SMBIOS 3 Table";
else if (CompareGuid(g, &MpsTable))
guid_str = "MPS Table";
else if (CompareGuid(g, &EfiAcpiTable))
guid_str = "EFI ACPI Table";
else if (CompareGuid(g, &EfiLzmaCompressed))
guid_str = "EFI LZMA Compressed";
else if (CompareGuid(g, &EfiDxeServices))
guid_str = "EFI DXE Services";
else if (CompareGuid(g, &EfiHobList))
guid_str = "EFI HOB List";
else if (CompareGuid(g, &EfiMemoryType))
guid_str = "EFI Memory Type";
else if (CompareGuid(g, &EfiDebugImageInfoTable))
guid_str = "EFI Debug Image Info Table";
else if (CompareGuid(g, &EfiMemStatusCodeRec))
guid_str = "EFI Memory Status Code Record";
else if (CompareGuid(g, &EfiGuidEfiAcpi1))
guid_str = "EFI ACPI 1.0 Table";
else if (CompareGuid(g, &EfiMemoryAttributesTable))
guid_str = "EFI Memory Attributes Table";
else if (CompareGuid(g, &EfiHiiDatabaseProtocol))
guid_str = "EFI HII Database Protocol";
else if (CompareGuid(g, &EfiHiiConfigRoutingProtocol))
guid_str = "EFI HII Config Routing Protocol";
else if (CompareGuid(g, &TCG2FinalEventsTable))
guid_str = "TCG2 Final Events Table";
else if (CompareGuid(g, &EfiImageSecurityDatabase))
guid_str = "EFI Image Security Database";
else
guid_str = "(unknown)";
debug("%016lx %08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x %s",
(UINT64)(uintptr_t)addr,
g->Data1, g->Data2, g->Data3,
g->Data4[0], g->Data4[1],
g->Data4[2], g->Data4[3],
g->Data4[4], g->Data4[5],
g->Data4[6], g->Data4[7],
guid_str);
}
}
#endif
if (bInfo.EFI.Info.ST == 1)
{
SearchSMBIOS(SystemTable);

View File

@ -78,7 +78,11 @@ enum EFI_MEMORY_TYPE
EfiMemoryMappedIOPortSpace,
EfiPalCode,
EfiPersistentMemory,
EfiMaxMemoryType
EfiMaxMemoryType,
MEMORY_TYPE_OEM_RESERVED_MIN = 0x70000000,
MEMORY_TYPE_OEM_RESERVED_MAX = 0x7FFFFFFF,
MEMORY_TYPE_OS_RESERVED_MIN = 0x80000000,
MEMORY_TYPE_OS_RESERVED_MAX = 0xFFFFFFFF
};
enum EFI_ALLOCATE_TYPE

View File

@ -17,17 +17,71 @@
#pragma once
#define ACPI_20_TABLE_GUID \
{0x8868e871, 0xe4f1, 0x11d3, {0xbc, 0x22, 0x0, 0x80, 0xc7, 0x3c, 0x88, 0x81}}
/* This part is from "4.6.1.1 Industry Standard Configuration Tables" */
#define EFI_ACPI_20_TABLE_GUID \
{0x8868e871, 0xe4f1, 0x11d3, {0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81}}
#define ACPI_TABLE_GUID \
{0xeb9d2d30, 0x2d88, 0x11d3, {0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
{0xeb9d2d30, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
#define SAL_SYSTEM_TABLE_GUID \
{0xeb9d2d32, 0x2d88, 0x11d3, {0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
{0xeb9d2d32, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
#define SMBIOS_TABLE_GUID \
{0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
{0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
#define SMBIOS3_TABLE_GUID \
{0xf2fd1544, 0x9794, 0x4a2c, {0x99, 0x2e, 0xe5, 0xbb, 0xcf, 0x20, 0xe3, 0x94}}
#define MPS_TABLE_GUID \
{0xeb9d2d2f, 0x2d88, 0x11d3, {0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
{0xeb9d2d2f, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
//
// ACPI 2.0 or newer tables should use EFI_ACPI_TABLE_GUID
//
#define EFI_ACPI_TABLE_GUID \
{0x8868e871, 0xe4f1, 0x11d3, {0xbc, 0x22, 0x00, 0x80, 0xc7, 0x3c, 0x88, 0x81}}
// #define EFI_ACPI_20_TABLE_GUID EFI_ACPI_TABLE_GUID
#define ACPI_TABLE_GUID \
{0xeb9d2d30, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
#define ACPI_10_TABLE_GUID ACPI_TABLE_GUID *
#define EFI_LZMA_COMPRESSED_GUID \
{0xee4e5898, 0x3914, 0x4259, {0x9d, 0x6e, 0xdc, 0x7b, 0xd7, 0x94, 0x03, 0xcf}}
#define EFI_DXE_SERVICES_GUID \
{0x05ad34ba, 0x6f02, 0x4214, {0x95, 0x2e, 0x4d, 0xa0, 0x39, 0x8e, 0x2b, 0xb9}}
#define EFI_HOB_LIST_GUID \
{0x7739f24c, 0x93d7, 0x11d4, {0x9a, 0x3a, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
#define _EFI_MEMORY_TYPE_GUID \
{0x4c19049f, 0x4137, 0x4dd3, {0x9c, 0x10, 0x8b, 0x97, 0xa8, 0x3f, 0xfd, 0xfa}}
#define EFI_DEBUG_IMAGE_INFO_TABLE_GUID \
{0x49152e77, 0x1ada, 0x4764, {0xb7, 0xa2, 0x7a, 0xfe, 0xfe, 0xd9, 0x5e, 0x8b}}
#define EFI_MEM_STATUS_CODE_REC_GUID \
{0x060cc026, 0x4c0d, 0x4dda, {0x8f, 0x41, 0x59, 0x5f, 0xef, 0x00, 0xa5, 0x02}}
#define EFI_GUID_EFI_ACPI1_GUID \
{0xeb9d2d30, 0x2d88, 0x11d3, {0x9a, 0x16, 0x00, 0x90, 0x27, 0x3f, 0xc1, 0x4d}}
#define EFI_MEMORY_ATTRIBUTES_TABLE_GUID \
{0xdcfa911d, 0x26eb, 0x469f, {0xa2, 0x20, 0x38, 0xb7, 0xdc, 0x46, 0x12, 0x20}}
#define EFI_HII_DATABASE_PROTOCOL_GUID \
{0xef9fc172, 0xa1b2, 0x4693, {0xb3, 0x27, 0x6d, 0x32, 0xfc, 0x41, 0x60, 0x42}}
#define EFI_HII_CONFIG_ROUTING_PROTOCOL_GUID \
{0x587e72d7, 0xcc50, 0x4f79, {0x82, 0x09, 0xca, 0x29, 0x1f, 0xc1, 0xa1, 0x0f}}
#define TCG2_FINAL_EVENTS_TABLE_GUID \
{0x1e2ed096, 0x30e2, 0x4254, {0xbd, 0x89, 0x86, 0x3b, 0xbe, 0xf8, 0x23, 0x25}}
#define EFI_IMAGE_SECURITY_DATABASE_GUID \
{0xd719b2cb, 0x3d3a, 0x4596, {0xa3, 0xbc, 0xda, 0xd0, 0x0e, 0x67, 0x65, 0x6f}}