From 9dbf9d0c21002983de556aa76a9d01124d556a90 Mon Sep 17 00:00:00 2001 From: kotorifan Date: Sun, 1 Feb 2026 22:18:34 +0100 Subject: Enough for today --- src/boot/boot.stage1.asm | 90 ++++++++++++++++++++++++++++++++---------------- src/boot/boot.stage2.asm | 3 +- 2 files changed, 61 insertions(+), 32 deletions(-) (limited to 'src/boot') diff --git a/src/boot/boot.stage1.asm b/src/boot/boot.stage1.asm index 5fa3b7a..70e2f17 100644 --- a/src/boot/boot.stage1.asm +++ b/src/boot/boot.stage1.asm @@ -5,13 +5,19 @@ %include "boot.common.asm" - %define READ_SECTORS_NUM 1 - %define BOOT_LOAD_ADDR 0x7c00 + %define LOAD_ADDRESS 0x7C00 + %define STAGE2_ADDRESS 0x7E00 + %define RELOCATE_ADDRESS 0x0600 %define SECTOR_SIZE 512 + %define OFFSET_RELOC (RELOCATE_ADDRESS - LOAD_ADDRESS) + %define STAGE2_SECTORS 8 + global _start _start: + jmp 0x0000:.setup_segments + .setup_segments: ;; Disable interrupts cli @@ -20,44 +26,65 @@ _start: mov ds, ax mov es, ax mov ss, ax + mov gs, ax - ;; Adjust Stack - mov sp, _start - - clc - - mov [drive], dl - mov si, disk_addr_packet - mov ah, 0x42 ; BIOS extended read function - mov dl, [drive] ; Drive number - int 0x13 ; BIOS disk services - jc _disk_read_err - - mov si, disk_read_success_msg - call _print_string + mov sp, LOAD_ADDRESS ; Adjust stack - jmp 0x0000:0x7e00 + ;; Save disk + mov [drive], dl ; Get drive ID from BIOS -_disk_read_err: + sti + + mov si, LOAD_ADDRESS + mov di, RELOCATE_ADDRESS + mov cx, SECTOR_SIZE + cld + rep movsdb + + jmp 0x0000:(next) +_read_disk_real_mode: + .start: + cmp cx, 127 + jbe .good_size + pusha + mov cx, 127 + call _read_disk_real_mode + popa + add eax, 127 + add dx, 127*512/16 + sub cx, 127 + jmp .start + + .good_size: + mov [DAP.LBA_lower], ax + mov [DAP.num_sectors], cx + mov [DAP.buf_segment], dx + mov [DAP.buf_offset], bx + mov dl, [disk] + mov si, DAP + mov ah, 0x42 + int 0x13 + jc .disk_read_err + ret + .disk_read_err: mov si, disk_read_err_msg call _print_string - - hlt - jmp _disk_read_err + .halt: hlt + jmp .halt _print_string: pusha mov ah, 0x0e mov bh, 0x00 -.print_loop: + .print_loop: lodsb test al, al je .print_return int 0x10 jmp .print_loop -.print_return: + .print_return: popa ret @@ -68,14 +95,17 @@ _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 0x7e00 - dw 0x0000 - dq 1 +boot_msg: db "Booting", 13, 10, 0 +DAP: + db 0x10 + db 0 + .num_sectors: dw 127 + .buf_offset: dw 0x0 + .buf_segment: dw 0x0 + .LBA_lower: dd 0x0 + .LBA_upper: dd 0x0 + ;; Padding and magic number times 510 - ($ - $$) db 0 dw 0xaa55 diff --git a/src/boot/boot.stage2.asm b/src/boot/boot.stage2.asm index 8f4885b..97a2d05 100644 --- a/src/boot/boot.stage2.asm +++ b/src/boot/boot.stage2.asm @@ -1,7 +1,7 @@ ;; boot.stage2.asm [bits 16] - [org 0x7e00] + [org 0x0000] %include "boot.stage2.a20.asm" %include "boot.stage2.pm.asm" @@ -11,4 +11,3 @@ _s2_entry: call _enable_a20 call _enable_pm [bits 32] - -- cgit v1.3