From d069fc23bdeba69a2f0c83d53fa9d32530430c3b Mon Sep 17 00:00:00 2001 From: Hjin-BF Date: Fri, 27 Feb 2026 22:22:48 +0200 Subject: [PATCH] Initial bootloader and kernel loader --- .gitignore | 4 +++ Makefile | 23 +++++++++++++++ linker.ld | 27 +++++++++++++++++ sys/arch/x86_64/boot.s | 17 +++++++++++ sys/arch/x86_64/bootloader.asm | 54 ++++++++++++++++++++++++++++++++++ sys/arch/x86_64/kernel.asm | 19 ++++++++++++ sys/kern/main.c | 16 ++++++++++ 7 files changed, 160 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 linker.ld create mode 100644 sys/arch/x86_64/boot.s create mode 100644 sys/arch/x86_64/bootloader.asm create mode 100644 sys/arch/x86_64/kernel.asm create mode 100644 sys/kern/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..962f7aa --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/ +*.o +*.bin +*.iso diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0fd4164 --- /dev/null +++ b/Makefile @@ -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 diff --git a/linker.ld b/linker.ld new file mode 100644 index 0000000..b54c328 --- /dev/null +++ b/linker.ld @@ -0,0 +1,27 @@ +ENTRY(_start) + +SECTIONS +{ + . = 1M; + + .text : + { + *(.multiboot) + *(.text*) + } + + .rodata : + { + *(.rodata*) + } + + .data : + { + *(.data*) + } + + .bss : + { + *(.bss*) + } +} diff --git a/sys/arch/x86_64/boot.s b/sys/arch/x86_64/boot.s new file mode 100644 index 0000000..4cf8b2e --- /dev/null +++ b/sys/arch/x86_64/boot.s @@ -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 diff --git a/sys/arch/x86_64/bootloader.asm b/sys/arch/x86_64/bootloader.asm new file mode 100644 index 0000000..974b6d7 --- /dev/null +++ b/sys/arch/x86_64/bootloader.asm @@ -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 diff --git a/sys/arch/x86_64/kernel.asm b/sys/arch/x86_64/kernel.asm new file mode 100644 index 0000000..6c94ca4 --- /dev/null +++ b/sys/arch/x86_64/kernel.asm @@ -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 diff --git a/sys/kern/main.c b/sys/kern/main.c new file mode 100644 index 0000000..2f0f747 --- /dev/null +++ b/sys/kern/main.c @@ -0,0 +1,16 @@ +#include + +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"); + } +}