From 35f3e16135b371e11d540a8b6bd5395cb40b2c96 Mon Sep 17 00:00:00 2001 From: kotorifan Date: Sat, 31 Jan 2026 17:13:17 +0100 Subject: Whatever... --- .gitignore | 3 ++- make.sh | 24 ++++++++++++------- src/boot/boot.stage1.asm | 52 ++++++++++++++++++++++++++++-------------- src/boot/boot.stage1.print.asm | 21 ----------------- src/boot/boot.stage2.a20.asm | 4 ++-- src/boot/boot.stage2.asm | 5 +++- src/boot/boot.stage2.gdt32.asm | 6 ++--- src/boot/boot.stage2.pm.asm | 20 ++++++++++++++-- src/kernel/kernel.asm | 10 ++++++++ src/kernel/kernel.print.asm | 21 +++++++++++++++++ 10 files changed, 111 insertions(+), 55 deletions(-) delete mode 100644 src/boot/boot.stage1.print.asm create mode 100644 src/kernel/kernel.asm create mode 100644 src/kernel/kernel.print.asm diff --git a/.gitignore b/.gitignore index d285671..3137744 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dst/ -disk.iso \ No newline at end of file +disk.iso +disk.img \ No newline at end of file diff --git a/make.sh b/make.sh index c2a1019..221eb53 100755 --- a/make.sh +++ b/make.sh @@ -1,6 +1,7 @@ #!/bin/sh AS_BOOT="nasm -fbin -Isrc/boot/" +AS_KERN="nasm -fbin -Isrc/boot/ -Isrc/kernel/" SRC_DIR="src" DST_DIR="dst" IMG_FILE="disk.img" @@ -10,6 +11,7 @@ NAME="kotoriforth" clean() { rm -f $IMG_FILE + rm -f $ISO_FILE rm -rf $DST_DIR } @@ -18,15 +20,20 @@ build() 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 + $AS_BOOT $SRC_DIR/boot/boot.stage2.asm -o $DST_DIR/s2_boot.bin + $AS_KERN $SRC_DIR/kernel/kernel.asm -o $DST_DIR/kernel.bin + dd if=/dev/zero of=disk.img bs=512 count=2048 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)) - + echo "Written s1_boot.bin to $IMG_FILE" + dd if=$DST_DIR/s2_boot.bin of=$IMG_FILE conv=notrunc bs=512 seek=1 + echo "Written s2_boot.bin to $IMG_FILE" + S2_SECTORS=$(( ($(wc -c < "$DST_DIR/s2_boot.bin") + 511) / 512 )) + KERNEL_OFFSET=$((1 + S2_SECTORS)) + dd if=$DST_DIR/kernel.bin of=$IMG_FILE conv=notrunc bs=512 \ + seek=$KERNEL_OFFSET status=none + echo "Written kernel.bin to $IMG_FILE" xorriso -as mkisofs \ - -b $DST_DIR/$IMG_FILE \ + -b $IMG_FILE \ -no-emul-boot \ -boot-load-size 4 \ -boot-info-table \ @@ -35,7 +42,8 @@ build() -R \ -V "$NAME" \ -o $ISO_FILE \ - $ISO_FILE + "$(dirname "$IMG_FILE")" + if [ -f $ISO_FILE ]; then echo "Bootable ISO created" diff --git a/src/boot/boot.stage1.asm b/src/boot/boot.stage1.asm index 801860e..5fa3b7a 100644 --- a/src/boot/boot.stage1.asm +++ b/src/boot/boot.stage1.asm @@ -1,11 +1,11 @@ ;; boot.stage1.asm - %include "boot.common.asm" - %include "boot.stage1.print.asm" [org 0x7c00] [bits 16] - %define READ_SECTORS_NUM 64 + %include "boot.common.asm" + + %define READ_SECTORS_NUM 1 %define BOOT_LOAD_ADDR 0x7c00 %define SECTOR_SIZE 512 @@ -26,9 +26,10 @@ _start: clc + mov [drive], dl mov si, disk_addr_packet mov ah, 0x42 ; BIOS extended read function - mov dl, 0x80 ; Drive number + mov dl, [drive] ; Drive number int 0x13 ; BIOS disk services jc _disk_read_err @@ -44,21 +45,38 @@ _disk_read_err: hlt jmp _disk_read_err +_print_string: + pusha + mov ah, 0x0e + mov bh, 0x00 + +.print_loop: + lodsb + test al, al + je .print_return + int 0x10 + jmp .print_loop + +.print_return: + popa + ret + _end: - hlt - jmp _end + hlt + jmp _end +drive: db 0 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 0x00 - dw READ_SECTORS_NUM - dw 0x0000 - dw 0x7e00 - dq 1 - - ;; Padding and magic number - times 510 - ($ - $$) db 0 - dw 0xaa55 - + db 0x10 + db 0x00 + dw READ_SECTORS_NUM + dw 0x7e00 + dw 0x0000 + dq 1 + + ;; Padding and magic number + times 510 - ($ - $$) db 0 + dw 0xaa55 + diff --git a/src/boot/boot.stage1.print.asm b/src/boot/boot.stage1.print.asm deleted file mode 100644 index aff2e0d..0000000 --- a/src/boot/boot.stage1.print.asm +++ /dev/null @@ -1,21 +0,0 @@ - ;; 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/boot.stage2.a20.asm b/src/boot/boot.stage2.a20.asm index 6a9c250..8fa3d37 100644 --- a/src/boot/boot.stage2.a20.asm +++ b/src/boot/boot.stage2.a20.asm @@ -22,7 +22,7 @@ _enable_a20: .halt: hlt jmp .halt - .end + .end: ret _check_a20: @@ -143,5 +143,5 @@ _enable_a20_io92: or al, 2 and al, 0xfe out 0x92, al - .end + .end: ret diff --git a/src/boot/boot.stage2.asm b/src/boot/boot.stage2.asm index 56ab73a..8f4885b 100644 --- a/src/boot/boot.stage2.asm +++ b/src/boot/boot.stage2.asm @@ -5,7 +5,10 @@ %include "boot.stage2.a20.asm" %include "boot.stage2.pm.asm" + + %define KERNEL_OFFSET 0x1000 _s2_entry: call _enable_a20 - call _enter_pm + call _enable_pm [bits 32] + diff --git a/src/boot/boot.stage2.gdt32.asm b/src/boot/boot.stage2.gdt32.asm index f9f27a5..f766d82 100644 --- a/src/boot/boot.stage2.gdt32.asm +++ b/src/boot/boot.stage2.gdt32.asm @@ -1,4 +1,4 @@ -y ;; boot.stage2.gdt32.asm + ;; boot.stage2.gdt32.asm GDT32: @@ -25,5 +25,5 @@ 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 + CODE_SEG32 equ GDT32.code - GDT32 + DATA_SEG32 equ GDT32.data - GDT32 diff --git a/src/boot/boot.stage2.pm.asm b/src/boot/boot.stage2.pm.asm index 77f5f61..8897be6 100644 --- a/src/boot/boot.stage2.pm.asm +++ b/src/boot/boot.stage2.pm.asm @@ -4,9 +4,25 @@ [bits 16] %include "boot.stage2.gdt32.asm" -_ +_enable_pm: cli lgdt [GDT32_ptr] - ;; Enable Protected Mode + mov eax, cr0 + or eax, 1 + mov cr0, eax + + jmp CODE_SEG32:_protected_mode + +[bits 32] +_protected_mode: + mov ax, DATA_SEG32 + mov ds, ax + mov ss, ax + mov es, ax + mov fs, ax + mov gs, ax + + ret + diff --git a/src/kernel/kernel.asm b/src/kernel/kernel.asm new file mode 100644 index 0000000..244be91 --- /dev/null +++ b/src/kernel/kernel.asm @@ -0,0 +1,10 @@ + ;; kernel.asm + [bits 32] + [org 0x1000] + +%define VGA_COLOR_BLACK 0 +%define VGA_COLOR_GREEN 2 +%define VGA_COLOR_RED 4 + +_kernel_entry: + diff --git a/src/kernel/kernel.print.asm b/src/kernel/kernel.print.asm new file mode 100644 index 0000000..d7a6a7d --- /dev/null +++ b/src/kernel/kernel.print.asm @@ -0,0 +1,21 @@ + ;; kernel.print.asm +%define VGA_BUFFER 0xB8000 +_terminal_getidx: + push ax + shl dh, 1 + mov al, VGA_WIDTH + mul dl + mov dl, al + shl dl, 1 + add dl, dh + mov dh, 0 + pop ax + ret + +_terminal_set_color: + shl dl, 4 + or dl, dh + mov [terminal_color], dl + ret + + -- cgit v1.3