[feat] BootLoader -> Kernel

This commit is contained in:
2026-05-29 19:25:56 +08:00
Unverified
parent c31fb52ea3
commit 2496349657
7 changed files with 302 additions and 37 deletions
+13
View File
@@ -0,0 +1,13 @@
#include <efi.h>
#include <common.h>
EFI_SYSTEM_TABLE *ST = NULL;
extern "C" void kernel_main();
extern "C" void _start(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
(void)ImageHandle;
ST = SystemTable;
kernel_main();
while (1) ASM ("hlt");
}
+45
View File
@@ -0,0 +1,45 @@
OUTPUT_FORMAT("elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)
ENTRY(_start)
PHDRS
{
text PT_LOAD FLAGS(5);
rodata PT_LOAD FLAGS(4);
data PT_LOAD FLAGS(6);
}
SECTIONS
{
. = 0x100000 + SIZEOF_HEADERS;
.text : ALIGN(4096) {
*(.text*)
*(.gnu.linkonce.t.*)
} :text
.rodata : ALIGN(4096) {
*(.rodata*)
*(.gnu.linkonce.r.*)
} :rodata
.data : ALIGN(4096) {
*(.data*)
*(.gnu.linkonce.d.*)
} :data
.bss : ALIGN(4096) {
__bss_start = .;
*(.bss*)
*(COMMON)
__bss_end = .;
} :data
/DISCARD/ : {
*(.comment)
*(.note.*)
*(.eh_frame*)
*(.gcc_except_table*)
*(.note.GNU-stack)
}
}
+9 -9
View File
@@ -1,5 +1,4 @@
#include <efi.h>
#include <efilib.h>
#include <graphics/context.h>
#include <graphics/draw.h>
#include <fonts/pixel_font.h>
@@ -8,12 +7,14 @@
#include <memory/pmm.h>
#include <memory/heap.h>
extern EFI_SYSTEM_TABLE *ST;
inline void init_gop() {
// 初始化 GOP
EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
EFI_GRAPHICS_OUTPUT_PROTOCOL *GOP;
uefi_call_wrapper((void*)ST->BootServices->SetWatchdogTimer, 4, 0, 0, 0, NULL);
uefi_call_wrapper((void*)ST->BootServices->LocateProtocol, 3, &gop_guid, NULL, (void **)&GOP);
uefi_call_wrapper(ST->BootServices->SetWatchdogTimer, 4, 0, 0, 0, NULL);
uefi_call_wrapper(ST->BootServices->LocateProtocol, 3, &gop_guid, NULL, (void **)&GOP);
gfx_init(GOP);
}
@@ -23,7 +24,7 @@ inline void init_serial() {
EFI_GUID gEfiSerialIoProtocolGuid = EFI_SERIAL_IO_PROTOCOL_GUID;
EFI_HANDLE *SerialHandles = NULL;
UINTN NumSerials = 0;
EFI_STATUS status = uefi_call_wrapper((void*)ST->BootServices->LocateHandleBuffer, 5,
EFI_STATUS status = uefi_call_wrapper(ST->BootServices->LocateHandleBuffer, 5,
ByProtocol,
&gEfiSerialIoProtocolGuid,
NULL,
@@ -31,7 +32,7 @@ inline void init_serial() {
&SerialHandles
);
if (status == EFI_SUCCESS && NumSerials > 0) {
status = uefi_call_wrapper((void*)ST->BootServices->HandleProtocol, 3,
status = uefi_call_wrapper(ST->BootServices->HandleProtocol, 3,
SerialHandles[0], &gEfiSerialIoProtocolGuid, (void **)&SerialIo);
if (status == EFI_SUCCESS) {
serial_init(SerialIo);
@@ -46,11 +47,10 @@ extern "C" void kernel_main() {
init_gop();
init_serial();
// 都用uefi_call_wrapper,不用会PF,不知道为什么
uefi_call_wrapper((void*)ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, 5);
uefi_call_wrapper((void*)ST->ConOut->OutputString, 2, ST->ConOut, L"Kernel is running!\n");
uefi_call_wrapper(ST->ConOut->SetCursorPosition, 3, ST->ConOut, 0, 5);
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, (CHAR16*)L"Kernel is running!\n");
uefi_call_wrapper((void*)ST->ConOut->ClearScreen, 1, ST->ConOut);
uefi_call_wrapper(ST->ConOut->ClearScreen, 1, ST->ConOut);
serial_write("\n\n"); // 防止和前面串了serial.log看不清
// init memory managers
+8 -7
View File
@@ -1,7 +1,8 @@
#include <memory/pmm.h>
#include <efilib.h>
#include <serial.h>
extern EFI_SYSTEM_TABLE *ST;
pmm_t g_pmm;
static inline void bitmap_set(UINTN idx) {
@@ -31,7 +32,7 @@ EFI_STATUS pmm_init() {
UINT32 desc_version;
EFI_STATUS status = uefi_call_wrapper(
(void*)ST->BootServices->GetMemoryMap, 5,
ST->BootServices->GetMemoryMap, 5,
&map_size, NULL, &map_key, &desc_size, &desc_version
);
@@ -39,17 +40,17 @@ EFI_STATUS pmm_init() {
EFI_MEMORY_DESCRIPTOR* mem_map = NULL;
status = uefi_call_wrapper(
(void*)ST->BootServices->AllocatePool, 3,
ST->BootServices->AllocatePool, 3,
EfiLoaderData, map_size, (void**)&mem_map
);
if (EFI_ERROR(status)) return status;
status = uefi_call_wrapper(
(void*)ST->BootServices->GetMemoryMap, 5,
ST->BootServices->GetMemoryMap, 5,
&map_size, mem_map, &map_key, &desc_size, &desc_version
);
if (EFI_ERROR(status)) {
uefi_call_wrapper((void*)ST->BootServices->FreePool, 1, mem_map);
uefi_call_wrapper(ST->BootServices->FreePool, 1, mem_map);
return status;
}
@@ -96,7 +97,7 @@ EFI_STATUS pmm_init() {
if (bitmap_addr == 0) {
serial_write("PMM: ERROR - no space for bitmap!\n");
uefi_call_wrapper((void*)ST->BootServices->FreePool, 1, mem_map);
uefi_call_wrapper(ST->BootServices->FreePool, 1, mem_map);
return EFI_OUT_OF_RESOURCES;
}
@@ -155,7 +156,7 @@ EFI_STATUS pmm_init() {
}
if (prev) *(void**)prev = NULL;
uefi_call_wrapper((void*)ST->BootServices->FreePool, 1, mem_map);
uefi_call_wrapper(ST->BootServices->FreePool, 1, mem_map);
serial_write("PMM: init OK, total ");
serial_write_hex(total_pages);
+8 -7
View File
@@ -1,28 +1,29 @@
#include <serial.h>
#include <efilib.h>
extern EFI_SYSTEM_TABLE *ST;
serial_context g_serial;
void serial_init(EFI_SERIAL_IO_PROTOCOL *SerialIo) {
g_serial.SerialIo = SerialIo;
uefi_call_wrapper((void*)SerialIo->Reset, 1, SerialIo);
uefi_call_wrapper(SerialIo->Reset, 1, SerialIo);
}
void serial_write_char(char c) {
if (!g_serial.SerialIo) {
uefi_call_wrapper((void*)ST->ConOut->OutputString, 2, ST->ConOut, L"serial: null io\n");
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, (CHAR16*)L"serial: null io\n");
return;
}
UINTN size = 1;
EFI_STATUS status = uefi_call_wrapper((void*)g_serial.SerialIo->Write, 3, g_serial.SerialIo, &size, &c);
EFI_STATUS status = uefi_call_wrapper(g_serial.SerialIo->Write, 3, g_serial.SerialIo, &size, &c);
if (status != EFI_SUCCESS) {
uefi_call_wrapper((void*)ST->ConOut->OutputString, 2, ST->ConOut, L"serial: write failed\n");
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, (CHAR16*)L"serial: write failed\n");
}
}
void serial_write(const char *str) {
if (!g_serial.SerialIo) {
uefi_call_wrapper((void*)ST->ConOut->OutputString, 2, ST->ConOut, L"serial: null io\n");
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, (CHAR16*)L"serial: null io\n");
return;
}
while (*str) {
@@ -47,7 +48,7 @@ char serial_read_char() {
if (!g_serial.SerialIo) return 0;
char c = 0;
UINTN size = 1;
EFI_STATUS status = uefi_call_wrapper((void*)g_serial.SerialIo->Read, 3, g_serial.SerialIo, &size, &c);
EFI_STATUS status = uefi_call_wrapper(g_serial.SerialIo->Read, 3, g_serial.SerialIo, &size, &c);
if (status != EFI_SUCCESS || size == 0) return 0;
return c;
}