Refactor boot_info.h and related files for improved abstraction and consistency. Updated function signatures to use generic types, replaced UEFI-specific types with kernel types, and enhanced documentation for clarity. Adjusted kernel entry point and service wrappers to align with new structure.

This commit is contained in:
2026-02-27 19:53:40 +00:00
parent 13a281fa4f
commit a3edb854f4
10 changed files with 181 additions and 103 deletions

76
main.c
View File

@@ -5,7 +5,7 @@
* firmware via the PE32+ entry point. It:
* 1. Reads kernel.elf from the EFI System Partition
* 2. Parses the ELF64 headers and maps PT_LOAD segments into memory
* 3. Populates a BootInfo struct with UEFI service wrappers
* 3. Populates a BootInfo struct with generic service wrappers
* 4. Jumps to the kernel entry point (kmain)
*/
@@ -227,26 +227,44 @@ static EFI_STATUS load_elf_kernel(VOID *Image, UINTN Size, UINT64 *EntryOut)
* UEFI service wrappers (passed to the kernel via BootInfo)
* ================================================================ */
static EFI_STATUS loader_clear_screen(void)
static KSTATUS loader_clear_screen(void)
{
return uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
EFI_STATUS Status = uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
return (KSTATUS)Status;
}
static EFI_STATUS loader_set_attribute(UINTN Attribute)
static KSTATUS loader_set_attribute(UINTN Attribute)
{
return uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, Attribute);
EFI_STATUS Status = uefi_call_wrapper(ST->ConOut->SetAttribute, 2, ST->ConOut, Attribute);
return (KSTATUS)Status;
}
static EFI_STATUS loader_read_key(EFI_INPUT_KEY *Key)
static KSTATUS loader_read_key(KeyEvent *Key)
{
EFI_STATUS Status;
EFI_INPUT_KEY EfiKey;
UINTN Index = 0;
uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &Index);
return uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, Key);
Status = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &EfiKey);
if (!EFI_ERROR(Status) && Key != NULL) {
Key->scan_code = EfiKey.ScanCode;
Key->unicode_char = EfiKey.UnicodeChar;
}
return (KSTATUS)Status;
}
static EFI_STATUS loader_try_read_key(EFI_INPUT_KEY *Key)
static KSTATUS loader_try_read_key(KeyEvent *Key)
{
return uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, Key);
EFI_STATUS Status;
EFI_INPUT_KEY EfiKey;
Status = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &EfiKey);
if (!EFI_ERROR(Status) && Key != NULL) {
Key->scan_code = EfiKey.ScanCode;
Key->unicode_char = EfiKey.UnicodeChar;
}
return (KSTATUS)Status;
}
static void loader_shutdown(void)
@@ -254,15 +272,23 @@ static void loader_shutdown(void)
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetShutdown, EFI_SUCCESS, 0, NULL);
}
static EFI_STATUS loader_alloc_pages(UINTN pages, EFI_PHYSICAL_ADDRESS *addr)
static KSTATUS loader_alloc_pages(UINTN pages, UINT64 *addr)
{
return uefi_call_wrapper(BS->AllocatePages, 4,
AllocateAnyPages, EfiLoaderData, pages, addr);
EFI_PHYSICAL_ADDRESS Phys = 0;
EFI_STATUS Status = uefi_call_wrapper(BS->AllocatePages, 4,
AllocateAnyPages, EfiLoaderData,
pages, &Phys);
if (!EFI_ERROR(Status) && addr != NULL) {
*addr = (UINT64)Phys;
}
return (KSTATUS)Status;
}
static EFI_STATUS loader_free_pages(EFI_PHYSICAL_ADDRESS addr, UINTN pages)
static KSTATUS loader_free_pages(UINT64 addr, UINTN pages)
{
return uefi_call_wrapper(BS->FreePages, 2, addr, pages);
EFI_STATUS Status = uefi_call_wrapper(BS->FreePages, 2,
(EFI_PHYSICAL_ADDRESS)addr, pages);
return (KSTATUS)Status;
}
/* ================================================================
@@ -297,16 +323,18 @@ efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable)
return Status;
}
/* Populate the BootInfo struct with UEFI service wrappers */
Boot.SystemTable = ST;
Boot.print = Print;
Boot.clear_screen = loader_clear_screen;
Boot.set_attribute = loader_set_attribute;
Boot.read_key = loader_read_key;
Boot.try_read_key = loader_try_read_key;
Boot.shutdown = loader_shutdown;
Boot.alloc_pages = loader_alloc_pages;
Boot.free_pages = loader_free_pages;
/* Populate the BootInfo struct with generic UEFI-backed services */
Boot.print = Print;
Boot.clear_screen = loader_clear_screen;
Boot.set_attribute = loader_set_attribute;
Boot.read_key = loader_read_key;
Boot.try_read_key = loader_try_read_key;
Boot.shutdown = loader_shutdown;
Boot.alloc_pages = loader_alloc_pages;
Boot.free_pages = loader_free_pages;
Boot.firmware_vendor = SystemTable->FirmwareVendor;
Boot.firmware_major = (SystemTable->FirmwareRevision >> 16) & 0xFFFF;
Boot.firmware_minor = SystemTable->FirmwareRevision & 0xFFFF;
/* Jump to the kernel this should not return */
EntryFn = (KernelEntryFn)(UINTN)KernelEntry;