Initial bootloader and kernel loader

This commit is contained in:
Hjin-BF
2026-02-27 22:22:48 +02:00
parent cb85514593
commit d069fc23bd
7 changed files with 160 additions and 0 deletions
+4
View File
@@ -0,0 +1,4 @@
build/
*.o
*.bin
*.iso
+23
View File
@@ -0,0 +1,23 @@
CC = x86_64-elf-gcc
LD = x86_64-elf-ld
CFLAGS = -ffreestanding -O2 -Wall -Wextra
LDFLAGS = -T linker.ld -nostdlib
OBJS = \
sys/arch/x86_64/boot.o \
sys/kern/main.o
all: kernel.bin
kernel.bin: $(OBJS)
$(LD) $(LDFLAGS) -o kernel.bin $(OBJS)
sys/arch/x86_64/boot.o:
$(CC) $(CFLAGS) -c sys/arch/x86_64/boot.s -o sys/arch/x86_64/boot.o
sys/kern/main.o:
$(CC) $(CFLAGS) -c sys/kern/main.c -o sys/kern/main.o
clean:
rm -f $(OBJS) kernel.bin
+27
View File
@@ -0,0 +1,27 @@
ENTRY(_start)
SECTIONS
{
. = 1M;
.text :
{
*(.multiboot)
*(.text*)
}
.rodata :
{
*(.rodata*)
}
.data :
{
*(.data*)
}
.bss :
{
*(.bss*)
}
}
+17
View File
@@ -0,0 +1,17 @@
.section .multiboot
.align 4
.long 0x1BADB002
.long 0x0
.long -(0x1BADB002)
.section .text
.global _start
.type _start, @function
_start:
cli
call kernel_main
.hang:
hlt
jmp .hang
+54
View File
@@ -0,0 +1,54 @@
[org 0x7C00]
bits 16
KERNEL_OFFSET equ 0x1000
start:
cli
mov [BOOT_DRIVE], dl
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
mov sp, 0x7C00
sti
call load_kernel
jmp KERNEL_OFFSET
; -----------------------
; Зчитування kernel
; -----------------------
load_kernel:
mov bx, KERNEL_OFFSET ; куди завантажити
mov dh, 5 ; кількість секторів (тимчасово 5)
mov dl, [BOOT_DRIVE]
mov ah, 0x02 ; BIOS read sector
mov al, dh ; скільки секторів
mov ch, 0x00 ; cylinder
mov dh, 0x00 ; head
mov cl, 0x02 ; сектор 2 (після bootloader)
int 0x13
jc disk_error
ret
disk_error:
mov si, error_msg
.print:
lodsb
cmp al, 0
je $
mov ah, 0x0E
int 0x10
jmp .print
BOOT_DRIVE db 0
error_msg db "Disk read error!", 0
times 510 - ($ - $$) db 0
dw 0xAA55
+19
View File
@@ -0,0 +1,19 @@
[org 0x1000]
bits 16
start:
mov si, message
.print:
lodsb
cmp al, 0
je .hang
mov ah, 0x0E
int 0x10
jmp .print
.hang:
hlt
jmp .hang
message db "Kernel loaded successfully!", 0
+16
View File
@@ -0,0 +1,16 @@
#include <stddef.h>
void kernel_main(void) {
volatile char *video = (volatile char*) 0xB8000;
const char *msg = "LiteOS kernel started";
for (size_t i = 0; msg[i] != '\0'; i++) {
video[i * 2] = msg[i];
video[i * 2 + 1] = 0x07; // світло-сірий текст
}
while (1) {
__asm__("hlt");
}
}