From 929e57f0b5c8a49a35f8b8cab3c2eae32817a3dd Mon Sep 17 00:00:00 2001 From: kotorifan Date: Mon, 2 Feb 2026 14:18:37 +0100 Subject: Stage 1 seems to work now. Replace bin with elf; Added Linker. --- src/boot/boot.stage1.asm | 119 +++++++++++++---------------------------------- src/boot/boot.stage2.asm | 10 +++- src/kernel/kernel.asm | 4 +- 3 files changed, 44 insertions(+), 89 deletions(-) (limited to 'src') diff --git a/src/boot/boot.stage1.asm b/src/boot/boot.stage1.asm index 70e2f17..d8dc84a 100644 --- a/src/boot/boot.stage1.asm +++ b/src/boot/boot.stage1.asm @@ -1,112 +1,59 @@ - ;; boot.stage1.asm - - [org 0x7c00] + ;; boot.stage1.asm [bits 16] - %include "boot.common.asm" + global _start - %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 + %include "boot.stage1.print.asm" + %define READ_SECTORS_NUM 64 + %define BOOT_LOAD_ADDR 0x7c00 + %define STAGE2_ADDR 0x7e00 + %define SECTOR_SIZE 512 - global _start +section .boot_sector _start: - jmp 0x0000:.setup_segments - .setup_segments: - ;; Disable interrupts - cli - ;; Setup segment registers xor ax, ax mov ds, ax mov es, ax mov ss, ax mov gs, ax + mov fs, ax - mov sp, LOAD_ADDRESS ; Adjust stack - - ;; Save disk - mov [drive], dl ; Get drive ID from BIOS - - 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 + mov ah, 0x42 ; Extended read function + mov dl, 0x80 ; Drive Number (starts at 0x80, thus it is 1) int 0x13 - jc .disk_read_err - ret - .disk_read_err: - mov si, disk_read_err_msg - call _print_string - .halt: hlt - jmp .halt + jc _disk_read_err -_print_string: - pusha - mov ah, 0x0e - mov bh, 0x00 +_ignore_disk_read_err: + mov si, _jmp_msg + call _print_string + jmp 0x0000:STAGE2_ADDR - .print_loop: - lodsb - test al, al - je .print_return - int 0x10 - jmp .print_loop +_disk_read_err: + cmp word [DAP.num_sectors], READ_SECTORS_NUM + jle _ignore_disk_read_err - .print_return: - popa - ret + mov si, _disk_read_err_msg + call _print_string -_end: - hlt - jmp _end + .halt: hlt + jmp .halt -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 -boot_msg: db "Booting", 13, 10, 0 + align 16 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 + db 0x10 ; Size of packet + db 0 ; Zero + .num_sectors: + dw READ_SECTORS_NUM ; Number of sectors to be read + dd (BOOT_LOAD_ADDR + SECTOR_SIZE) ; Destination address + dq 1 ; Which sector to start at + +_disk_read_err_msg: db "Disk error", 13, 10, 0 +_jmp_msg: db "Jumping to Stage 2", 13, 10, 0 - ;; 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 97a2d05..759b784 100644 --- a/src/boot/boot.stage2.asm +++ b/src/boot/boot.stage2.asm @@ -1,13 +1,19 @@ ;; boot.stage2.asm [bits 16] - [org 0x0000] %include "boot.stage2.a20.asm" %include "boot.stage2.pm.asm" + %include "boot.stage1.print.asm" + +section .stage2 - %define KERNEL_OFFSET 0x1000 _s2_entry: + mov si, boot_stage2_msg + call _print_string call _enable_a20 call _enable_pm [bits 32] + + +boot_stage2_msg: db "Entering Stage 2", 13, 10, 0 diff --git a/src/kernel/kernel.asm b/src/kernel/kernel.asm index 244be91..15da780 100644 --- a/src/kernel/kernel.asm +++ b/src/kernel/kernel.asm @@ -1,10 +1,12 @@ ;; kernel.asm [bits 32] - [org 0x1000] + %define VGA_COLOR_BLACK 0 %define VGA_COLOR_GREEN 2 %define VGA_COLOR_RED 4 +section .kernel + _kernel_entry: -- cgit v1.3