summaryrefslogtreecommitdiff
path: root/mbr/stage1.asm
blob: ea51f22a2e6ead1c4190e5ccac41cca899e49c87 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
[org 0x7C00]

_start:
    xor ax, ax
    mov ds, ax
    mov es, ax
    mov ss, ax
    mov fs, ax
    mov gs, ax

    cli                         ;; Disable all interrupts
    cld                         ;; Strings shall increment
    mov [boot_drive], dl        ;; Save the boot drive

    mov si, boot_sig
    call print_str
    call enable_a20
    call disk_load

    jmp 0x0000:0x8000
    hlt
    jmp $

;;
;; Write a string to the screen
;;
;; [SI]: Base address of string
;;
print_str:
    lodsb               ;; Load *(SI++) -> AL
    or al, al           ;; Null byte?
    jz .done            ;; Yup, finish
    mov ah, 0x0E        ;; Print char
    int 0x10            ;; BIOS call
    jmp print_str       ;; Loop again until NUL
.done:
    ret

;;
;; Enable the A20 gate to access the full
;; 16 megs
;;
enable_a20:
    in al, 0x92         ;; Check the state
    test al, 2          ;; is it already set?
    jnz .done           ;; Yes, no need to re-enable
    or al, 2            ;; No, enable it
    out 0x92, al        ;; Write the new value back
.done:
    ret

DAP:
    .size:      db 0x10
    .reserved:  db 0x00
    .count:     dw 1
    .buffer:    dw 0x8000
    .segment:   dw 0
    .lba_lo:    dd 1
    .lba_hi:    dd 0
disk_load:
    mov si, DAP
    mov ah, 0x42
    mov dl, [boot_drive]
    int 0x13
    jc .disk_err
    ret
.disk_err:
    mov si, disk_errmsg
    call print_str
.hang:
    hlt
    jmp .hang

boot_sig: db "* beepboot stage0", 0x0A, 0x0D, 0x00
disk_errmsg: db "[!] failed to read disk", 0x0A, 0x0D, 0x00
boot_drive: db 0

times 510 - ($ - $$) db 0
dw 0xAA55