aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkotorifan <kotorifan05@gmail.com>2026-01-31 13:32:57 +0100
committerkotorifan <kotorifan05@gmail.com>2026-02-04 09:01:10 +0100
commitdfe4a33865ef01c5068804f77da844cdebd87f00 (patch)
tree46dd2fe032a0dc0358737147cbfe5cfd1ab60a56
parente0030889e85b070576c40917c649df3338a384df (diff)
downloadkotori-os-dfe4a33865ef01c5068804f77da844cdebd87f00.tar.gz
Added stage 1 of the bootloader + make.sh
-rw-r--r--make.sh35
-rw-r--r--src/boot.common.asm0
-rw-r--r--src/boot.stage1.asm56
-rw-r--r--src/boot.stage1.print.asm21
-rw-r--r--src/boot.stage2.asm2
-rw-r--r--src/boot.stage2.gdt32.asm29
6 files changed, 143 insertions, 0 deletions
diff --git a/make.sh b/make.sh
index e69de29..bbddce8 100644
--- a/make.sh
+++ b/make.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+AS_BOOT=nasm -fbin
+SRC_DIR=src
+DST_DIR=dst
+IMG_FILE=disk.iso
+
+clean()
+{
+ rm -f $IMG_FILE
+ rm -rf $DST_DIR
+}
+
+build()
+{
+ clean()
+ $AS_BOOT $SRC_DIR/boot/boot.stage1.asm -o $DST_DIR/s1_boot.bin
+}
+
+run()
+{
+ qemu-system-x86_64 -cdrom $IMG_FILE
+}
+
+debug()
+{
+ qemu-system-x86_64 -cdrom $IMG_FILE -S -s
+}
+case $1 in
+ "clean") clean ;;
+ "build") build ;;
+ "run") run ;;
+ "debug") debug ;;
+ *) echo "Use clean|build|run|debug" ;;
+esac
diff --git a/src/boot.common.asm b/src/boot.common.asm
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/boot.common.asm
diff --git a/src/boot.stage1.asm b/src/boot.stage1.asm
new file mode 100644
index 0000000..c5bab26
--- /dev/null
+++ b/src/boot.stage1.asm
@@ -0,0 +1,56 @@
+ ;; boot.stage1.asm
+
+ %include "boot.common.asm"
+ %include "boot.stage1.print.asm"
+ [org 0x7c00]
+ [bits 16]
+
+ %define READ_SECTORS_NUM 64
+ %define BOOT_LOAD_ADDR 0x7c00
+ %define SECTOR_SIZE 512
+
+ entry _start
+
+_start:
+ ;; Disable interrupts
+ cli
+
+ ;; Setup segment registers
+ xor ax, ax
+ mov ds, ax
+ mov es, ax
+ mov ss, ax
+
+ ;; Adjust Stack
+ mov sp, _start
+
+ mov si, [disk_addr_packet]
+ mov ah, 0x42 ; BIOS extended read function
+ mov dl, 0x80 ; Drive number
+ int 0x13 ; BIOS disk services
+ jc _disk_read_err
+
+ jmp 0x0000:0x7e00
+
+_disk_read_err:
+ hlt
+ jmp _disk_read_err
+
+_end:
+ hlt
+ jmp _end
+
+disk_read_err_msg: db "Failed to read disk", 13, 10, 0
+disk_read_success_msg: db "Read the disk successfully", 13, 10, 0
+disk_addr_packet:
+ db 0x10
+ db 0x0
+DAP_sectors_num:
+ dw READ_SECTORS_NUM ; Number of read sectors
+ dd (BOOT_LOAD_ADDR + SECTOR_SIZE) ; Destination address
+ dq 1 ; First sector after the boot sector
+
+ ;; Padding and magic number
+ times 510 - ($ - $$) db 0
+ dw 0xaa55
+
diff --git a/src/boot.stage1.print.asm b/src/boot.stage1.print.asm
new file mode 100644
index 0000000..106a4df
--- /dev/null
+++ b/src/boot.stage1.print.asm
@@ -0,0 +1,21 @@
+ ;; boot.stage1.print.asm
+
+ [bits 16]
+_print_string:
+ pusha
+ mov ah, 0x0e
+
+_print_string_loop:
+ cmp byte [bx], 0
+ je print_string_return
+
+ mov al, [bx]
+ int 0x10
+
+ inc bx
+ jmp print_string_loop
+
+_print_string_return:
+ popa
+ ret
+ align 4
diff --git a/src/boot.stage2.asm b/src/boot.stage2.asm
new file mode 100644
index 0000000..7eeefe7
--- /dev/null
+++ b/src/boot.stage2.asm
@@ -0,0 +1,2 @@
+ ;; boot.stage2.asm
+
diff --git a/src/boot.stage2.gdt32.asm b/src/boot.stage2.gdt32.asm
new file mode 100644
index 0000000..f9f27a5
--- /dev/null
+++ b/src/boot.stage2.gdt32.asm
@@ -0,0 +1,29 @@
+y ;; boot.stage2.gdt32.asm
+
+
+GDT32:
+ .null:
+ dd 0x0
+ dd 0x0
+ .code:
+ dw 0xffff
+ dw 0x0000
+ db 0x00
+ db 10011010b
+ db 11001111b
+ db 0x00
+ .data:
+ dw 0xffff
+ dw 0x0000
+ db 0x00
+ db 10010010b
+ db 11001111b
+ db 0x00
+GDT32_end:
+
+GDT32_ptr:
+ .limit: dw GDT32_end - GDT32 - 1
+ .base: dd GDT32
+
+ CODE_SEG32 equ gdt32_code_segment - gdt32_start
+ DATA_SEG32 equ gdt32_data_segment - gdt32_start