aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkotorifan <kotorifan05@gmail.com>2026-01-31 15:22:00 +0100
committerkotorifan <kotorifan05@gmail.com>2026-02-04 09:01:10 +0100
commite5c28a46a731a79d02266aa690a90a8f809d0f70 (patch)
tree3dfc555f1ca98c97b3f48f035e897e3d0cca07ba
parentdfe4a33865ef01c5068804f77da844cdebd87f00 (diff)
downloadkotori-os-e5c28a46a731a79d02266aa690a90a8f809d0f70.tar.gz
A20 enable code added
-rwxr-xr-x[-rw-r--r--]make.sh42
-rw-r--r--src/boot.stage2.asm2
-rw-r--r--src/boot/boot.common.asm (renamed from src/boot.common.asm)0
-rw-r--r--src/boot/boot.stage1.asm (renamed from src/boot.stage1.asm)24
-rw-r--r--src/boot/boot.stage1.print.asm (renamed from src/boot.stage1.print.asm)4
-rw-r--r--src/boot/boot.stage2.a20.asm147
-rw-r--r--src/boot/boot.stage2.asm11
-rw-r--r--src/boot/boot.stage2.gdt32.asm (renamed from src/boot.stage2.gdt32.asm)0
-rw-r--r--src/boot/boot.stage2.pm.asm12
9 files changed, 222 insertions, 20 deletions
diff --git a/make.sh b/make.sh
index bbddce8..c2a1019 100644..100755
--- a/make.sh
+++ b/make.sh
@@ -1,9 +1,11 @@
#!/bin/sh
-AS_BOOT=nasm -fbin
-SRC_DIR=src
-DST_DIR=dst
-IMG_FILE=disk.iso
+AS_BOOT="nasm -fbin -Isrc/boot/"
+SRC_DIR="src"
+DST_DIR="dst"
+IMG_FILE="disk.img"
+ISO_FILE="disk.iso"
+NAME="kotoriforth"
clean()
{
@@ -13,18 +15,42 @@ clean()
build()
{
- clean()
- $AS_BOOT $SRC_DIR/boot/boot.stage1.asm -o $DST_DIR/s1_boot.bin
+ clean
+ mkdir -p $DST_DIR/boot
+ $AS_BOOT $SRC_DIR/boot/boot.stage1.asm -o $DST_DIR/s1_boot.bin
+
+ dd if=/dev/zero of=$IMG_FILE bs=1M count=2
+ dd if=$DST_DIR/s1_boot.bin of=$IMG_FILE conv=notrunc bs=512 count=1
+ dd of=$DST_DIR/s2_boot.bin of=$IMG_FILE conv=notrunc bs=512 seek=1
+ S2_SECTORS=$(($(wc -c < "$BOOT_S2") / 512))
+ dd of=$DST_DIR/kernel.bin of=$IMG_FILE conv=notrunc bs=512 $((1 + S2_SECTORS))
+
+ xorriso -as mkisofs \
+ -b $DST_DIR/$IMG_FILE \
+ -no-emul-boot \
+ -boot-load-size 4 \
+ -boot-info-table \
+ -iso-level 3 \
+ -J \
+ -R \
+ -V "$NAME" \
+ -o $ISO_FILE \
+ $ISO_FILE
+
+ if [ -f $ISO_FILE ]; then
+ echo "Bootable ISO created"
+ fi
+
}
run()
{
- qemu-system-x86_64 -cdrom $IMG_FILE
+ qemu-system-i386 -cdrom $IMG_FILE
}
debug()
{
- qemu-system-x86_64 -cdrom $IMG_FILE -S -s
+ qemu-system-i386 -cdrom $IMG_FILE -S -s
}
case $1 in
"clean") clean ;;
diff --git a/src/boot.stage2.asm b/src/boot.stage2.asm
deleted file mode 100644
index 7eeefe7..0000000
--- a/src/boot.stage2.asm
+++ /dev/null
@@ -1,2 +0,0 @@
- ;; boot.stage2.asm
-
diff --git a/src/boot.common.asm b/src/boot/boot.common.asm
index e69de29..e69de29 100644
--- a/src/boot.common.asm
+++ b/src/boot/boot.common.asm
diff --git a/src/boot.stage1.asm b/src/boot/boot.stage1.asm
index c5bab26..801860e 100644
--- a/src/boot.stage1.asm
+++ b/src/boot/boot.stage1.asm
@@ -9,7 +9,7 @@
%define BOOT_LOAD_ADDR 0x7c00
%define SECTOR_SIZE 512
- entry _start
+ global _start
_start:
;; Disable interrupts
@@ -23,16 +23,24 @@ _start:
;; Adjust Stack
mov sp, _start
+
+ clc
- mov si, [disk_addr_packet]
+ 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
+ mov si, disk_read_success_msg
+ call _print_string
+
jmp 0x0000:0x7e00
_disk_read_err:
+ mov si, disk_read_err_msg
+ call _print_string
+
hlt
jmp _disk_read_err
@@ -43,12 +51,12 @@ _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
+ db 0x10
+ db 0x00
+ dw READ_SECTORS_NUM
+ dw 0x0000
+ dw 0x7e00
+ dq 1
;; Padding and magic number
times 510 - ($ - $$) db 0
diff --git a/src/boot.stage1.print.asm b/src/boot/boot.stage1.print.asm
index 106a4df..aff2e0d 100644
--- a/src/boot.stage1.print.asm
+++ b/src/boot/boot.stage1.print.asm
@@ -7,13 +7,13 @@ _print_string:
_print_string_loop:
cmp byte [bx], 0
- je print_string_return
+ je _print_string_return
mov al, [bx]
int 0x10
inc bx
- jmp print_string_loop
+ jmp _print_string_loop
_print_string_return:
popa
diff --git a/src/boot/boot.stage2.a20.asm b/src/boot/boot.stage2.a20.asm
new file mode 100644
index 0000000..6a9c250
--- /dev/null
+++ b/src/boot/boot.stage2.a20.asm
@@ -0,0 +1,147 @@
+ ;; boot.stage2.a20.asm
+
+_enable_a20:
+ call _check_a20
+ test ax, ax
+ jnz .end
+
+ 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
+ call .wait_io1
+ mov al, 0xad
+ out 0x64, al
+
+ call .wait_io1
+ mov al, 0x0d0
+ out 0x64, al
+
+ call .wait_io2
+ in al, 0x60
+ push eax
+
+ call .wait_io1
+ mov al, 0xd1
+ out 0x64, al
+
+ call .wait_io1
+ mov al, 0xae
+ out 0x64, al
+
+ sti
+ ret
+
+ .wait_io1:
+ in al, 0x64
+ test al, 2
+ jnz .wait_io1
+ ret
+
+ .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
new file mode 100644
index 0000000..56ab73a
--- /dev/null
+++ b/src/boot/boot.stage2.asm
@@ -0,0 +1,11 @@
+ ;; boot.stage2.asm
+
+ [bits 16]
+ [org 0x7e00]
+
+ %include "boot.stage2.a20.asm"
+ %include "boot.stage2.pm.asm"
+_s2_entry:
+ call _enable_a20
+ call _enter_pm
+ [bits 32]
diff --git a/src/boot.stage2.gdt32.asm b/src/boot/boot.stage2.gdt32.asm
index f9f27a5..f9f27a5 100644
--- a/src/boot.stage2.gdt32.asm
+++ b/src/boot/boot.stage2.gdt32.asm
diff --git a/src/boot/boot.stage2.pm.asm b/src/boot/boot.stage2.pm.asm
new file mode 100644
index 0000000..77f5f61
--- /dev/null
+++ b/src/boot/boot.stage2.pm.asm
@@ -0,0 +1,12 @@
+ ;; boot.stage2.pm.asm
+
+
+ [bits 16]
+ %include "boot.stage2.gdt32.asm"
+
+_
+ cli
+ lgdt [GDT32_ptr]
+
+
+ ;; Enable Protected Mode