Ninja-based buildsystem now building a running kernel!

This commit is contained in:
Justin C. Miller
2019-02-02 21:35:39 -08:00
parent acdca19f59
commit bc01a37452
8 changed files with 75 additions and 24 deletions

View File

@@ -27,7 +27,7 @@ SECTIONS
*(.note.*)
}
.bss ALIGN(0x1000) : {
.bss ALIGN(16) : {
__bss_start = .;
*(.bss)
__bss_end = .;

View File

@@ -87,6 +87,8 @@ loader_load_elf(
{
EFI_STATUS status;
con_debug(L"Opening kernel file %s\r\n", (CHAR16 *)kernel_name);
EFI_FILE_PROTOCOL *file = NULL;
status = root->Open(root, &file, (CHAR16 *)kernel_name, EFI_FILE_MODE_READ,
EFI_FILE_READ_ONLY | EFI_FILE_HIDDEN | EFI_FILE_SYSTEM);
@@ -107,6 +109,8 @@ loader_load_elf(
status = file->Read(file, &length, &header);
CHECK_EFI_STATUS_OR_RETURN(status, L"Reading ELF header");
con_debug(L"Read %u bytes of ELF header\r\n", length);
if (length < sizeof(struct elf_header))
CHECK_EFI_STATUS_OR_RETURN(EFI_LOAD_ERROR, L"Incomplete read of ELF header");
@@ -131,10 +135,14 @@ loader_load_elf(
header.machine != 0x3e)
CHECK_EFI_STATUS_OR_RETURN(EFI_LOAD_ERROR, L"ELF load error: wrong machine architecture");
con_debug(L"ELF is valid, entrypoint %lu\r\n", header.entrypoint);
data->kernel_entry = (void *)header.entrypoint;
struct elf_program_header prog_header;
for (int i = 0; i < header.ph_num; ++i) {
con_debug(L"Reading ELF program header %d\r\n", i);
status = file->SetPosition(file, header.ph_offset + i * header.ph_entsize);
CHECK_EFI_STATUS_OR_RETURN(status, L"Setting ELF file position");
@@ -156,6 +164,8 @@ loader_load_elf(
struct elf_section_header sec_header;
for (int i = 0; i < header.sh_num; ++i) {
con_debug(L"Reading ELF section header %d ", i);
status = file->SetPosition(file, header.sh_offset + i * header.sh_entsize);
CHECK_EFI_STATUS_OR_RETURN(status, L"Setting ELF file position");
@@ -163,11 +173,15 @@ loader_load_elf(
status = file->Read(file, &length, &sec_header);
CHECK_EFI_STATUS_OR_RETURN(status, L"Reading ELF section header");
if ((sec_header.flags & ELF_SHF_ALLOC) == 0) continue;
if ((sec_header.flags & ELF_SHF_ALLOC) == 0) {
con_debug(L"SHF_ALLOC section\r\n");
continue;
}
void *addr = (void *)(sec_header.addr - KERNEL_VIRT_ADDRESS);
if (sec_header.type == ELF_ST_PROGBITS) {
con_debug(L"PROGBITS section\r\n");
status = file->SetPosition(file, sec_header.offset);
CHECK_EFI_STATUS_OR_RETURN(status, L"Setting ELF file position");
@@ -175,7 +189,10 @@ loader_load_elf(
status = file->Read(file, &length, addr);
CHECK_EFI_STATUS_OR_RETURN(status, L"Reading file");
} else if (sec_header.type == ELF_ST_NOBITS) {
con_debug(L"NOBITS section\r\n");
bootsvc->SetMem(addr, sec_header.size, 0);
} else {
con_debug(L"other section\r\n");
}
}

View File

@@ -32,3 +32,9 @@ const CHAR16 *util_error_message(EFI_STATUS status);
: "r"((uint64_t)s), "r"((uint64_t)d), "r"((uint64_t)__LINE__) \
: "rax", "rdx", "r8", "r9", "r10"); \
}
#ifdef BOOTLOADER_DEBUG
#define con_debug(...) con_printf(L"DEBUG: " __VA_ARGS__)
#else
#define con_debug(...)
#endif

View File

@@ -3,17 +3,17 @@ extern g_gdtr
global idt_write
idt_write:
lidt [g_idtr]
lidt [rel g_idtr]
ret
global idt_load
idt_load:
sidt [g_idtr]
sidt [rel g_idtr]
ret
global gdt_write
gdt_write:
lgdt [g_gdtr]
lgdt [rel g_gdtr]
mov ax, si ; second arg is data segment
mov ds, ax
mov es, ax
@@ -30,6 +30,6 @@ gdt_write:
global gdt_load
gdt_load:
sgdt [g_gdtr]
sgdt [rel g_gdtr]
ret