aboutsummaryrefslogtreecommitdiffstats
path: root/src/boot
diff options
context:
space:
mode:
authorkotorifan <kotorifan05@gmail.com>2026-03-07 17:14:14 +0100
committerkotorifan <kotorifan05@gmail.com>2026-03-07 17:14:14 +0100
commitf81dee10ef19f0c82eb89d0e528bcbfb7a38b016 (patch)
tree32e733d4ad2471419bdc9e1dd112d1d6a5b90958 /src/boot
parent857f2d6fe4a77a485d9ed46c0c80eecf61142607 (diff)
downloadkotori-os-f81dee10ef19f0c82eb89d0e528bcbfb7a38b016.tar.gz
Kernel loads now, bootloader works kind of
Diffstat (limited to 'src/boot')
-rw-r--r--src/boot/boot.stage1.asm1
-rw-r--r--src/boot/boot.stage2.a20.asm135
-rw-r--r--src/boot/boot.stage2.asm40
-rw-r--r--src/boot/boot.stage2.print.asm54
4 files changed, 78 insertions, 152 deletions
diff --git a/src/boot/boot.stage1.asm b/src/boot/boot.stage1.asm
index a67e7fa..6bed463 100644
--- a/src/boot/boot.stage1.asm
+++ b/src/boot/boot.stage1.asm
@@ -30,6 +30,7 @@ _start:
int 0x13 ; Read disk
jc _disk_read_err
+ mov dl, [drive]
jmp 0x0000:STAGE2_ADDR
diff --git a/src/boot/boot.stage2.a20.asm b/src/boot/boot.stage2.a20.asm
index 3c69d79..d0b52c4 100644
--- a/src/boot/boot.stage2.a20.asm
+++ b/src/boot/boot.stage2.a20.asm
@@ -1,147 +1,46 @@
- ;; boot.stage2.a20.asm
- section .stage2
+ ;; boot.stage2.a20.asm
_enable_a20:
- call _check_a20
- test ax, ax
- jnz .end
+ cli
- call _enable_a20_bios
- call _check_a20
- test ax, ax
- jnz .end
-
- call _enable_a20_keyb
- call _check_a20
- test ax, ax
- jnz .end
-
- call _enable_a20_io92
- call _check_a20
- test ax, ax
- jnz .end
-
- .halt: hlt
- jmp .halt
- .end:
- ret
-
-_check_a20:
- pushf
- push ds
- push es
- push di
- push si
- cli ; Clear Interrupts
-
- xor ax, ax
- mov es, ax
- not ax
- mov ds, ax
- ;; 0500 and 0510 are guaranteed to be free
- mov di, 0x0500
- mov si, 0x0510
-
- ;; Save the original values
- mov dl, byte [es:di]
- push dx
- mov dl, byte [ds:si]
- push dx
-
- mov byte [es:di], 0x00
- mov byte [ds:si], 0xFF
- cmp byte [es:di], 0xFF
-
- mov ax, 0 ; The A20 is disabled
- je .a20_disabled
- mov ax, 1 ; The A20 is enabled
- .a20_disabled:
- pop dx
- mov byte [ds:si], dl
- pop dx
- mov byte [es:di], dl
-
- pop si
- pop di
- pop es
- pop ds
- popf
- sti ; Enable interrupts again
- ret
-
-_enable_a20_bios:
- mov ax, 0x2401
- int 0x15
- jc .fast_gate
- test ah, ah
- jnz .failed
- call _check_a20
- test ax, ax
- jnz .done
-
- .fast_gate:
- in al, 0x92
- test al, 2
- jnz .done
-
- or al, 2
- and al, 0xfe
- out 0x92, al
-
- call _check_a20
- test ax, ax
- jnz .done
-
- .done:
- mov ax, 1
- ret
-
- .failed:
- mov ax, 0
- ret
-
-_enable_a20_keyb:
- cli
+ ;; Disable keyboard
call .wait_io1
mov al, 0xad
out 0x64, al
+ ;; Read from keyboard controller
call .wait_io1
- mov al, 0x0d0
+ mov al, 0xd0
out 0x64, al
call .wait_io2
in al, 0x60
- push eax
+ push eax ; Save current value
+
+ ;; Write back with the A20 bit set
call .wait_io1
mov al, 0xd1
out 0x64, al
call .wait_io1
+ pop eax
+ or al, 2 ; Set A20 bit
+
+ ;; Enable keyboard
+ call .wait_io1
mov al, 0xae
out 0x64, al
-
- sti
- ret
+ call .wait_io1
+
.wait_io1:
in al, 0x64
test al, 2
jnz .wait_io1
ret
-
- .wait_io2:
+
+ .wait_io2:
in al, 0x64
test al, 1
jz .wait_io2
ret
-
-_enable_a20_io92:
- in al, 0x92 ; Read from port 0x92
- test al, 2 ; Check if bit 1
- jnz .end
- or al, 2
- and al, 0xfe
- out 0x92, al
- .end:
- ret
diff --git a/src/boot/boot.stage2.asm b/src/boot/boot.stage2.asm
index 52c73e5..968bedc 100644
--- a/src/boot/boot.stage2.asm
+++ b/src/boot/boot.stage2.asm
@@ -3,6 +3,8 @@
[bits 16]
[org 0x7e00]
+ %include "common.asm"
+
jmp 0x0000:_s2_entry
_s2_entry:
@@ -10,6 +12,19 @@ _s2_entry:
xor ax, ax
mov ds, ax
+ mov si, DAP_kernel
+ mov ah, 0x42
+ int 0x13
+ jc _kernel_load_err
+ jmp _kernel_loaded
+
+_kernel_load_err:
+ mov si, boot_kernel_err
+ call _print_string
+
+ jmp $
+
+_kernel_loaded:
mov si, boot_s2_msg
call _print_string
@@ -27,21 +42,34 @@ _s2_entry:
_protected_mode:
mov ax, DATA_SEG32
mov ds, ax
+ mov ss, ax
mov esp, 0x90000
+ and esp, 0xFFFFFFF0 ; Align to 16-byte boundary
mov esi, boot_protmode_msg
call _print_string_pm_vga
call 0x10000
-
- jmp $
+boot_s2_msg:
+ db "Entering Stage 2", 13, 10, 0
+boot_protmode_msg:
+ db "Enabling Protected Mode", 13, 10, 0
+boot_kernel_err:
+ db "Error loading kernel from disk", 13, 10, 0
+
+
+ align 16
+DAP_kernel:
+ db 0x10
+ db 0
+ dw 1 ; Number of sectors
+ dw 0x0000
+ dw 0x1000 ; Offset
+ dq 66 ; Starting sector
+
%include "boot.stage1.print.asm"
%include "boot.stage2.a20.asm"
%include "boot.stage2.gdt32.asm"
%include "boot.stage2.print.asm"
-boot_s2_msg:
- db "Entering Stage 2", 13, 10, 0
-boot_protmode_msg:
- db "Enabling Protected Mode", 13, 10, 0
diff --git a/src/boot/boot.stage2.print.asm b/src/boot/boot.stage2.print.asm
index d567d6d..4527b87 100644
--- a/src/boot/boot.stage2.print.asm
+++ b/src/boot/boot.stage2.print.asm
@@ -1,39 +1,37 @@
;; boot.stage2.print.asm
-
- section .stage2
%define VGA_BUFFER 0xb8000
%define WB_COLOR 0xf
_print_string_pm_vga:
- mov eax, [esi]
- lea esi, [esi+1]
- cmp al, 0
- jne .print_char_pm_vga
- add byte [pos_x], 0
- add byte [pos_y], 1
- ret
+ pusha
+ mov edi, VGA_BUFFER
+ mov ah, WB_COLOR
+ xor ecx, ecx
+ xor edx, edx
- .print_char_pm_vga:
- mov ah, WB_COLOR ; White on black
- mov ecx, eax
- movzx eax, byte [pos_y]
- mov edx, 160
- mul edx
- movzx ebx, byte [pos_x]
- shl ebx, 1
-
- mov edi, VGA_BUFFER ; Video memory start
- add edi, ebx ; Add X offset
- add edi, eax ; Add Y offset
-
- mov eax, ecx ; Restore char
- mov word [ds:edi], ax
- add byte [pos_x], 1 ; Advance to right
- ret
+ .print_loop:
+ lodsb
+ test al, al
+ jz .done
-pos_x: db 0
-pos_y: db 0
+ push eax
+ mov eax, edx
+ imul eax, eax, 160
+ lea edi, [VGA_BUFFER + eax]
+ lea edi, [edi + ecx * 2]
+ pop eax
+ ;; Advance cursor
+ inc cl
+ cmp cl, 80
+ jb .print_loop
+ xor cl, cl
+ inc dl
+
+ jmp .print_loop
+ .done:
+ popa
+ ret