aboutsummaryrefslogtreecommitdiffstats
path: root/src/boot/boot.stage1.asm
blob: d8dc84ab0f6687a245e6599130f95f5626060945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    ;; boot.stage1.asm 
    [bits 16]

    global _start

    %include "boot.stage1.print.asm"

    %define READ_SECTORS_NUM 64
    %define BOOT_LOAD_ADDR 0x7c00
    %define STAGE2_ADDR 0x7e00
    %define SECTOR_SIZE 512

section .boot_sector

_start: 
    ;; Setup segment registers
    xor ax, ax
    mov ds, ax
    mov es, ax  
    mov ss, ax
    mov gs, ax
    mov fs, ax

    mov si, DAP
    mov ah, 0x42                ; Extended read function
    mov dl, 0x80                ; Drive Number (starts at 0x80, thus it is 1)
    int 0x13
    jc _disk_read_err

_ignore_disk_read_err:  
    mov si, _jmp_msg
    call _print_string
    jmp 0x0000:STAGE2_ADDR

_disk_read_err: 
    cmp word [DAP.num_sectors], READ_SECTORS_NUM
    jle _ignore_disk_read_err

    mov si, _disk_read_err_msg
    call _print_string

    .halt: hlt
    jmp .halt

    align 16
DAP:    
    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