summaryrefslogtreecommitdiff
path: root/boot/amd64/generic/mbr/mbr.S
blob: 7cd74843d8571f0da0176c59b4d4c056b6b5a11c (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 * Copyright (c) 2026, Apollo Telephone Laboratories.
 * Provided under the BSD-3 clause.
 */

#define BOOT_SEG       0x7C0    /* Segment we reside on startup */
#define BOOT_RELOC_SEG 0x7A0    /* Relocation segment */
#define RDAR_MAGIC     ">RDAR!"
#define RDAR_MAGIC_LEN 6
#define BOOT_PATH      "s2.bin"
#define BOOT_PATH_LEN  6

    .text
    .code16
_start:
    ljmp $BOOT_SEG,$main
main:
    cli
    cld

    /* Set segment registers */
    xor %ax, %ax
    mov %ax, %fs
    mov %ax, %gs
    mov %ax, %ss
    mov %ax, %es
    mov $BOOT_SEG, %ax
    mov %ax, %ds

    /* Save the drive */
    mov %dl, drive_num

    /*
     * The second stage bootloader expects to be loaded to 0x7C00 but since we are
     * already executing at this address we'll need to relocate ourselves first before
     * we clobber it.
     */
    mov $BOOT_SEG, %ax
    mov %ax, %ds
    mov $BOOT_RELOC_SEG, %ax
    mov %ax, %es
    xor %si, %si
    xor %di, %di
    mov $0x200, %cx
    cld
    rep movsb

    jmp $BOOT_RELOC_SEG,$1f
1:  mov $0x7C00, %sp

    /* Load two sectors */
    movw $2, dap_blkcnt
    movl $1, dap_lba_low
    call disk_read_lba

    mov $BOOT_RELOC_SEG, %ax
    mov %ax, %ds

    /* Get stage2 */
    call find_stage2

    /* Obtain the data offset */
    mov $BOOT_SEG, %ax
    mov %ax, %ds
    add $40, %si
    movw (%si), %si

    mov $BOOT_SEG, %ax
    mov %ax, %ds
    mov %ax, %es
    xor %di, %di
    mov $0x200, %cx
    cld
    rep movsb

    /* Jump to stage 2 */
    xor %ax, %ax
    mov %ax, %ds
    ljmp $0x0000,$0x7C00

/*
 * Write a character to the boot console
 *
 * %al:  Character to write
 */
putc:
    mov $0x0E, %ah
    xor %bh, %bh
    int $0x10
    ret

/*
 * Write a string to the boot console
 *
 * %si:  Pointer to string to write
 */
puts:
    lodsb
    or %al, %al
    jz 1f
    call putc
    jmp puts
1:  ret

/*
 * Locate stage2 on the disk
 *
 * Stage 2 data descriptor is returned in %SI
 */
find_stage2:
    push %ds
    push %es

    /* Verify that magic is correct */
    mov $BOOT_SEG, %ax
    mov %ax, %ds
    xor %bx, %bx    /* Tracks %SI */
    xor %si, %si
    mov $BOOT_RELOC_SEG, %ax
    mov %ax, %es
    mov $rdar_magic, %di
    mov $RDAR_MAGIC_LEN, %cx
    rep cmpsb
    jne .bad_magic

    /* Iterate every data descriptor */
    clc              /* Carry set if found */
1:  movw %ds:6, %cx  /* Add data descriptor size */
    mov %bx, %si
    mov %si, %dx
    add %cx, %bx
    mov $rdar_magic, %di
    mov $RDAR_MAGIC_LEN, %cx
    rep cmpsb
    jne .not_found

    /* Does the path match? */
    mov %dx, %si
    add $8, %si
    mov $stage2_path, %di
    mov $BOOT_PATH_LEN, %cx
    rep cmpsb
    jne 1b

    mov %dx, %si
    stc
    pop %es
    pop %ds
    jnc .not_found
    ret
.not_found:
    pop %ds
    mov $not_found_msg, %si
    call puts
1:  cli
    hlt
    jmp 1b

.bad_magic:
    /* Restore %DS */
    mov $BOOT_RELOC_SEG, %ax
    mov %ax, %ds

    mov $bad_magic_msg, %si
    call puts
1:  cli
    hlt
    jmp 1b

/*
 * Read sectors from the disk
 */
disk_read_lba:
    pusha
    mov $dap, %si
    mov $0x42, %ah
    mov drive_num, %dl
    int $0x13
    jc .error
    popa
    ret
.error:
    mov $disk_err_msg, %si
    call puts
1:  cli
    hlt
    jmp 1b

/* Global stores */
rdar_magic: .ascii RDAR_MAGIC
bad_magic_msg: .ascii "bad RDAR magic\r\n\0"
not_found_msg: .ascii "stage 2 not found\r\n\0"
disk_err_msg: .ascii "\r\n!!!disk error!!!\r\n\0"
stage2_path: .ascii BOOT_PATH
drive_num:  .byte 0

dap:
    .byte 0x10
    .byte 0x00
dap_blkcnt:
    .word 0
dap_destptr:
    .word 0x7C00
dap_destseg:
    .word 0x0000
dap_lba_low:
    .long 0
dap_lba_high:
    .long 0

    .org 510
    .byte 0x55
    .byte 0xAA