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

View File

@@ -1,17 +1,16 @@
/*
* kernel.c Kernel entry point and interactive shell loop.
*
* kmain() is called by the UEFI loader (main.c) after the ELF kernel
* has been mapped into memory. It initialises subsystems (IDT, memory,
* tasks), prints a welcome banner, and enters an interactive read-
* eval-print loop that dispatches typed commands via commands.c.
* kmain() is called by the loader (main.c) after the ELF kernel has been
* mapped into memory. It initialises subsystems (IDT, memory, tasks),
* prints a welcome banner, and enters an interactive read-eval-print
* loop that dispatches typed commands via commands.c.
*
* While waiting for keyboard input, the shell yields to the cooperative
* scheduler so that background tasks can make progress.
*/
#include <efi.h>
#include "kernel_types.h"
#include "boot_info.h"
#include "commands.h"
#include "idt.h"
@@ -26,14 +25,21 @@
} \
} while (0)
/* Simple text attribute helper (modelled after UEFI TEXT_ATTR). */
#define TEXT_ATTR(fg, bg) (UINTN)(((fg) & 0x0F) | (((bg) & 0x0F) << 4))
/* Basic colour constants (compatible with UEFI's palette). */
#define COLOR_BLACK 0x0
#define COLOR_LIGHTGREEN 0xA
/* ================================================================
* Kernel entry point
* ================================================================ */
void kmain(BootInfo *Boot)
{
EFI_INPUT_KEY Key;
EFI_STATUS Status;
KeyEvent Key;
KSTATUS Status;
UINTN read_errors = 0;
if (Boot == NULL) {
@@ -42,15 +48,17 @@ void kmain(BootInfo *Boot)
if (Boot->clear_screen != NULL) {
Status = Boot->clear_screen();
if (EFI_ERROR(Status)) {
SAFE_PRINT(Boot, L"clear_screen failed: %r\n\r", Status);
if (Status != 0) {
SAFE_PRINT(Boot, L"clear_screen failed (status=%ld)\n\r",
(UINT64)Status);
}
}
if (Boot->set_attribute != NULL) {
Status = Boot->set_attribute(EFI_TEXT_ATTR(EFI_LIGHTGREEN, EFI_BLACK));
if (EFI_ERROR(Status)) {
SAFE_PRINT(Boot, L"set_attribute failed: %r\n\r", Status);
Status = Boot->set_attribute(TEXT_ATTR(COLOR_LIGHTGREEN, COLOR_BLACK));
if (Status != 0) {
SAFE_PRINT(Boot, L"set_attribute failed (status=%ld)\n\r",
(UINT64)Status);
}
}
@@ -64,26 +72,18 @@ void kmain(BootInfo *Boot)
SAFE_PRINT(Boot, L"================================================\n\r");
SAFE_PRINT(Boot, L"\n\r");
SAFE_PRINT(Boot, L"System Information:\n\r");
if (Boot->SystemTable != NULL) {
SAFE_PRINT(Boot, L" UEFI Firmware Vendor: %s\n\r",
Boot->SystemTable->FirmwareVendor);
SAFE_PRINT(Boot, L" UEFI Firmware Revision: %d.%d\n\r",
Boot->SystemTable->FirmwareRevision >> 16,
Boot->SystemTable->FirmwareRevision & 0xFFFF);
if (Boot->firmware_vendor != NULL) {
SAFE_PRINT(Boot, L" Firmware Vendor: %s\n\r", Boot->firmware_vendor);
SAFE_PRINT(Boot, L" Firmware Revision: %d.%d\n\r",
Boot->firmware_major, Boot->firmware_minor);
} else {
SAFE_PRINT(Boot, L" UEFI System Table: Unavailable\n\r");
SAFE_PRINT(Boot, L" Firmware information: Unavailable\n\r");
}
SAFE_PRINT(Boot, L"\n\r");
SAFE_PRINT(Boot, L"Available Services:\n\r");
SAFE_PRINT(Boot, L" - Console Input/Output: %s\n\r",
(Boot->read_key != NULL && Boot->print != NULL) ? L"Active" : L"Unavailable");
SAFE_PRINT(Boot, L" - Runtime Services: %s\n\r",
(Boot->SystemTable != NULL &&
Boot->SystemTable->RuntimeServices != NULL) ? L"Active" : L"Unavailable");
SAFE_PRINT(Boot, L" - Boot Services: %s\n\r",
(Boot->SystemTable != NULL &&
Boot->SystemTable->BootServices != NULL) ? L"Active" : L"Unavailable");
SAFE_PRINT(Boot, L"\n\r");
SAFE_PRINT(Boot, L"Type 'help' for a list of commands.\n\r\n\r");
@@ -97,7 +97,7 @@ void kmain(BootInfo *Boot)
/* Try non-blocking read first; yield to other tasks while idle */
if (Boot->try_read_key != NULL) {
Status = Boot->try_read_key(&Key);
if (Status == EFI_NOT_READY) {
if (Status != 0) {
task_yield();
continue;
}
@@ -108,16 +108,17 @@ void kmain(BootInfo *Boot)
break;
}
if (EFI_ERROR(Status)) {
if (Status != 0) {
read_errors++;
if (read_errors == 1 || (read_errors % 64) == 0) {
SAFE_PRINT(Boot, L"read_key failed: %r\n\r", Status);
SAFE_PRINT(Boot, L"read_key failed (status=%ld)\n\r",
(UINT64)Status);
}
continue;
}
read_errors = 0;
if (Key.UnicodeChar == L'\r' || Key.UnicodeChar == L'\n') {
if (Key.unicode_char == L'\r' || Key.unicode_char == L'\n') {
/* Enter pressed: execute the buffered command */
line[len] = L'\0';
SAFE_PRINT(Boot, L"\n\r");
@@ -126,17 +127,17 @@ void kmain(BootInfo *Boot)
/* Reset for next command */
len = 0;
SAFE_PRINT(Boot, L"-> ");
} else if (Key.ScanCode == 0x08 || Key.UnicodeChar == L'\b' || Key.UnicodeChar == 0x7F) {
} else if (Key.scan_code == 0x08 || Key.unicode_char == L'\b' || Key.unicode_char == 0x7F) {
/* Backspace */
if (len > 0) {
len--;
SAFE_PRINT(Boot, L"\b \b");
}
} else if (Key.UnicodeChar >= 32 && Key.UnicodeChar < 127) {
} else if (Key.unicode_char >= 32 && Key.unicode_char < 127) {
/* Printable ASCII */
if (len < (sizeof(line) / sizeof(line[0]) - 1)) {
line[len++] = Key.UnicodeChar;
SAFE_PRINT(Boot, L"%c", Key.UnicodeChar);
line[len++] = Key.unicode_char;
SAFE_PRINT(Boot, L"%c", Key.unicode_char);
}
}
}