95 lines
2.2 KiB
NASM
95 lines
2.2 KiB
NASM
; File: boot.asm
|
|
; Stage 1 Bootloader (Master Boot Record - exactly 512 bytes)
|
|
; Job: Load Stage 2 from disk and jump to it
|
|
|
|
[org 0x7c00]
|
|
[bits 16]
|
|
|
|
STAGE2_OFFSET equ 0x7e00 ; Load stage2 right after boot sector
|
|
|
|
start:
|
|
; Set up segments
|
|
xor ax, ax
|
|
mov ds, ax
|
|
mov es, ax
|
|
mov ss, ax
|
|
mov sp, 0x7c00
|
|
|
|
; IMPORTANT: Save boot drive number (BIOS passes it in DL)
|
|
push dx
|
|
|
|
; Clear screen
|
|
mov ah, 0x00
|
|
mov al, 0x03
|
|
int 0x10
|
|
|
|
; Print loading message
|
|
mov si, msg_loading
|
|
call print_string
|
|
|
|
; Load Stage 2 from disk
|
|
; Read 5 sectors (sectors 2-6) - gives us 2.5KB for Stage 2
|
|
pop dx ; Restore boot drive
|
|
push dx ; Save it again for stage 2
|
|
mov bx, STAGE2_OFFSET ; Destination
|
|
mov dh, 5 ; Number of sectors
|
|
; DL already has drive number
|
|
call load_disk
|
|
|
|
; Jump to Stage 2 (DL still has boot drive number)
|
|
pop dx ; Pass drive number to stage 2 in DL
|
|
jmp STAGE2_OFFSET
|
|
|
|
; ============================================================
|
|
; LOAD DISK - Read sectors using BIOS INT 0x13
|
|
; ============================================================
|
|
load_disk:
|
|
push dx
|
|
|
|
mov ah, 0x02 ; Read function
|
|
mov al, dh ; Number of sectors
|
|
mov ch, 0 ; Cylinder 0
|
|
mov cl, 2 ; Start at sector 2
|
|
mov dh, 0 ; Head 0
|
|
|
|
int 0x13
|
|
jc disk_error
|
|
|
|
pop dx
|
|
cmp al, dh
|
|
jne disk_error
|
|
ret
|
|
|
|
disk_error:
|
|
mov si, msg_error
|
|
call print_string
|
|
jmp $
|
|
|
|
; ============================================================
|
|
; PRINT STRING
|
|
; ============================================================
|
|
print_string:
|
|
pusha
|
|
mov ah, 0x0e
|
|
.loop:
|
|
lodsb
|
|
test al, al
|
|
jz .done
|
|
int 0x10
|
|
jmp .loop
|
|
.done:
|
|
popa
|
|
ret
|
|
|
|
; ============================================================
|
|
; DATA
|
|
; ============================================================
|
|
msg_loading: db '[STAGE1] Loading SoraOS...', 0x0D, 0x0A, 0
|
|
msg_error: db '[ERROR] Disk read failed!', 0x0D, 0x0A, 0
|
|
|
|
; ============================================================
|
|
; BOOT SIGNATURE
|
|
; ============================================================
|
|
times 510-($-$$) db 0
|
|
dw 0xAA55
|