Proper kernel
This commit is contained in:
140
kernel.c
140
kernel.c
@@ -1,69 +1,109 @@
|
||||
#include <efi.h>
|
||||
|
||||
#include "boot_info.h"
|
||||
#include "commands.h"
|
||||
|
||||
#define SAFE_PRINT(Boot, ...) \
|
||||
do { \
|
||||
if ((Boot) != NULL && (Boot)->print != NULL) { \
|
||||
(Boot)->print(__VA_ARGS__); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
void kmain(BootInfo *Boot)
|
||||
{
|
||||
EFI_INPUT_KEY Key;
|
||||
EFI_STATUS Status;
|
||||
UINTN read_errors = 0;
|
||||
|
||||
Boot->clear_screen();
|
||||
Boot->set_attribute(EFI_TEXT_ATTR(EFI_LIGHTGREEN, EFI_BLACK));
|
||||
if (Boot == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
Boot->print(L"================================================\n\r");
|
||||
Boot->print(L" Welcome to Simple UEFI Operating System!\n\r");
|
||||
Boot->print(L"================================================\n\r");
|
||||
Boot->print(L"\n\r");
|
||||
Boot->print(L"System Information:\n\r");
|
||||
Boot->print(L" UEFI Firmware Vendor: %s\n\r", Boot->SystemTable->FirmwareVendor);
|
||||
Boot->print(L" UEFI Firmware Revision: %d.%d\n\r",
|
||||
Boot->SystemTable->FirmwareRevision >> 16,
|
||||
Boot->SystemTable->FirmwareRevision & 0xFFFF);
|
||||
Boot->print(L"\n\r");
|
||||
if (Boot->clear_screen != NULL) {
|
||||
Status = Boot->clear_screen();
|
||||
if (EFI_ERROR(Status)) {
|
||||
SAFE_PRINT(Boot, L"clear_screen failed: %r\n\r", Status);
|
||||
}
|
||||
}
|
||||
|
||||
Boot->print(L"Available Services:\n\r");
|
||||
Boot->print(L" - Console Input/Output: Active\n\r");
|
||||
Boot->print(L" - Runtime Services: Active\n\r");
|
||||
Boot->print(L" - Boot Services: Active\n\r");
|
||||
Boot->print(L"\n\r");
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Boot->print(L"Commands:\n\r");
|
||||
Boot->print(L" 'h' - Display help\n\r");
|
||||
Boot->print(L" 'i' - Display system info\n\r");
|
||||
Boot->print(L" 'c' - Clear screen\n\r");
|
||||
Boot->print(L" 'q' - Shutdown\n\r");
|
||||
Boot->print(L"\n\r");
|
||||
Boot->print(L"Press a key to start...\n\r");
|
||||
SAFE_PRINT(Boot, L"================================================\n\r");
|
||||
SAFE_PRINT(Boot, L" Welcome to Simple UEFI Operating System!\n\r");
|
||||
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);
|
||||
} else {
|
||||
SAFE_PRINT(Boot, L" UEFI System Table: 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");
|
||||
|
||||
// Simple line buffer
|
||||
CHAR16 line[128];
|
||||
UINTN len = 0;
|
||||
|
||||
SAFE_PRINT(Boot, L"-> ");
|
||||
|
||||
while (TRUE) {
|
||||
if (EFI_ERROR(Boot->read_key(&Key))) {
|
||||
continue;
|
||||
if (Boot->read_key == NULL) {
|
||||
SAFE_PRINT(Boot, L"Console input unavailable.\n\r");
|
||||
break;
|
||||
}
|
||||
|
||||
if (Key.UnicodeChar == L'q' || Key.UnicodeChar == L'Q') {
|
||||
Boot->print(L"\n\rShutting down...\n\r");
|
||||
Boot->shutdown();
|
||||
} else if (Key.UnicodeChar == L'h' || Key.UnicodeChar == L'H') {
|
||||
Boot->print(L"\n\r=== Help ===\n\r");
|
||||
Boot->print(L"This is a minimal UEFI operating system.\n\r");
|
||||
Boot->print(L"It demonstrates basic UEFI functionality.\n\r");
|
||||
Boot->print(L"\n\r");
|
||||
} else if (Key.UnicodeChar == L'i' || Key.UnicodeChar == L'I') {
|
||||
Boot->print(L"\n\r=== System Info ===\n\r");
|
||||
Boot->print(L"Firmware Vendor: %s\n\r", Boot->SystemTable->FirmwareVendor);
|
||||
Boot->print(L"Firmware Revision: %d.%d\n\r",
|
||||
Boot->SystemTable->FirmwareRevision >> 16,
|
||||
Boot->SystemTable->FirmwareRevision & 0xFFFF);
|
||||
Boot->print(L"UEFI Specification: %d.%d\n\r",
|
||||
Boot->SystemTable->Hdr.Revision >> 16,
|
||||
Boot->SystemTable->Hdr.Revision & 0xFFFF);
|
||||
Boot->print(L"\n\r");
|
||||
} else if (Key.UnicodeChar == L'c' || Key.UnicodeChar == L'C') {
|
||||
Boot->clear_screen();
|
||||
Boot->print(L"Screen cleared. Press 'h' for help.\n\r");
|
||||
Status = Boot->read_key(&Key);
|
||||
if (EFI_ERROR(Status)) {
|
||||
read_errors++;
|
||||
if (read_errors == 1 || (read_errors % 64) == 0) {
|
||||
SAFE_PRINT(Boot, L"read_key failed: %r\n\r", Status);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
read_errors = 0;
|
||||
|
||||
if (Key.UnicodeChar == L'\r' || Key.UnicodeChar == L'\n') {
|
||||
// Enter pressed: terminate string and handle
|
||||
line[len] = L'\0';
|
||||
SAFE_PRINT(Boot, L"\n\r");
|
||||
execute_command(Boot, line);
|
||||
|
||||
// Reset for next command
|
||||
len = 0;
|
||||
SAFE_PRINT(Boot, L"-> ");
|
||||
} else if (Key.UnicodeChar == L'\b' || Key.UnicodeChar == 0x7F) {
|
||||
// Backspace
|
||||
if (len > 0) {
|
||||
len--;
|
||||
SAFE_PRINT(Boot, L"\b \b");
|
||||
}
|
||||
} else if (Key.UnicodeChar >= 32 && Key.UnicodeChar < 127) {
|
||||
Boot->print(L"%c", Key.UnicodeChar);
|
||||
} else if (Key.UnicodeChar == L'\r') {
|
||||
Boot->print(L"\n\r> ");
|
||||
if (len < (sizeof(line) / sizeof(line[0]) - 1)) {
|
||||
line[len++] = Key.UnicodeChar;
|
||||
SAFE_PRINT(Boot, L"%c", Key.UnicodeChar);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user