boot/drives.s (view raw)
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
.section .text
.code16
# read hard drive parameters
.global read_drive_params
read_drive_params:
mov $drive_params_msg, %si
call print
movw $0x0000, %ax
movw %ax, %es
movw $0x0000, %di
movb $0x08, %ah
lea bootdriv_id, %si
movb (%si), %dl
int $0x13
xor %ax, %ax
movb %dl, %al
lea num_drives, %si
movb %al, (%si)
xor %ax, %ax
movb %dh, %al
inc %ax
lea num_heads, %si
movw %ax, (%si)
xor %ax, %ax
movb %cl, %al
shl $2, %ax
movb %ch, %al
inc %ax
lea num_cylinders, %si
movw %ax, (%si)
xor %ax, %ax
movb %cl, %al
and $0x003F, %ax
lea num_sectors, %si
movw %ax, (%si)
mov $done_msg, %si
call print
# print debug information
mov $num_drives_msg, %si
call print
lea num_drives, %si
xorw %ax, %ax
movb (%si), %al
call print_hex_int16
mov $newline, %si
call print
mov $bootdriv_id_msg, %si
call print
lea bootdriv_id, %si
xorw %ax, %ax
movb (%si), %al
call print_hex_int16
mov $newline, %si
call print
mov $num_heads_msg, %si
call print
lea num_heads, %si
xorw %ax, %ax
movw (%si), %ax
call print_hex_int16
mov $newline, %si
call print
mov $num_cylinders_msg, %si
call print
lea num_cylinders, %si
xorw %ax, %ax
movw (%si), %ax
call print_hex_int16
mov $newline, %si
call print
mov $num_sectors_msg, %si
call print
lea num_sectors, %si
xorw %ax, %ax
movw (%si), %ax
call print_hex_int16
mov $newline, %si
call print
ret
# reset bootdrive
.global reset_drive
reset_drive:
mov $10, %cx
reset_drive_loop:
push %cx
movw $reset_msg, %si
call print
lea bootdriv_id, %si
movb (%si), %dl
movb $0x00, %ah
int $0x13
pop %cx
jnc reset_end
loop reset_drive_loop
jc error
reset_end:
movw $done_msg, %si
call print
ret
# load second stage into memory starting at adress 0x10000
# (NOTE: this code can only be jumped to after loading the global descriptor table
# because it uses absolute adresses larger than 16 bit)
# (NOTE2: this routine only loads 0x48 sectors of the second stage into memory
# and is in general pretty whacky. should be replaced with sth more serious)
# arguments:
# - ax: first sector (LBA)
# - cx: last sector (LBA)
# - bx: address offset
.global load_sectors
load_sectors:
movw $load_msg, %si
call print
movw $0x1000, %bx
movw %bx, %es
load_sectors_loop:
push %ax
push %cx
push %bx
# call print_hex_int16
movw %bx, %di
# divide LBA by number of sectors
lea num_sectors, %si
xor %dx, %dx
movw (%si), %bx
divw %bx
andw $0x003F, %dx
movb %dl, %cl
incb %cl
xor %dx, %dx
lea num_heads, %si
movw (%si), %bx
divw %bx
movb %al, %ch
movb %ah, %al
xorb %ah, %ah
shl $6, %al
orb %al, %cl
movb %dl, %dh
lea bootdriv_id, %si
movb (%si), %dl
movw $0x0000, %bx
# movw $load_debug_arrow, %si
# call print
movb $0x02, %ah # function 0x02: read a sector
movb $0x01, %al # sectors to read count
# dl (drive) keep as is
pusha
int $0x13
jc load_error
movw $load_msg_dot, %si
call print
popa
pop %bx
pop %cx
pop %ax
inc %ax
movw %es, %bx
add $0x20, %bx
movw %bx, %es
cmp %ax, %cx
jne load_sectors_loop
load_end:
movw $0x0000, %si # buffer address
movw $0x1000, %ax
movw %ax, %es
;call print
movw $done_msg, %si
call print
ret
load_error:
movw $load_debug_fail, %si
call print
popa
movw $load_debug_reg_ax, %si
call print
call print_hex_int16
movw %bx, %ax
movw $load_debug_reg_bx, %si
call print
call print_hex_int16
movw %cx, %ax
movw $load_debug_reg_cx, %si
call print
call print_hex_int16
movw %dx, %ax
movw $load_debug_reg_dx, %si
call print
call print_hex_int16
movw %es, %ax
movw $load_debug_reg_dx, %si
call print
call print_hex_int16
# TODO: dump hex values of buffer
load_error_loop:
jmp load_error_loop
.section .data
drive_params_msg:
.ascii "Reading drive parameters..."
.byte 0
reset_msg:
.ascii "Resetting bootdrive..."
.byte 0
load_msg:
.ascii "Loading kernel"
# .byte 13
# .byte 10
.byte 0
load_msg_dot:
.ascii "."
.byte 0
load_debug_msg:
.ascii " Load: (sector) 0x"
.byte 0
load_debug_colon:
.ascii ":"
.byte 0
load_debug_arrow:
.ascii " -> (address) "
.byte 0
load_debug_return:
.ascii " done, (ret) 0x"
.byte 0
load_debug_fail:
.ascii " FAILED"
.byte 13
.byte 10
.byte 0
load_debug_reg_ax:
.ascii " AX=0x"
.byte 0
load_debug_reg_bx:
.ascii " BX=0x"
.byte 0
load_debug_reg_cx:
.ascii " CX=0x"
.byte 0
load_debug_reg_dx:
.ascii " DX=0x"
.byte 0
bootdriv_id_msg:
.ascii "- Boot drive ID: 0x"
.byte 0
num_drives_msg:
.ascii "- Number of drives: 0x"
.byte 0
num_sectors_msg:
.ascii "- Sectors per Track: 0x"
.byte 0
num_cylinders_msg:
.ascii "- Total cylinders: 0x"
.byte 0
num_heads_msg:
.ascii "- Total drive heads: 0x"
.byte 0
newline:
.byte 13
.byte 10
.byte 0
.section .bss
.global bootdriv_id
bootdriv_id:
.byte 0
num_drives:
.byte 0
num_sectors:
.word 0
num_cylinders:
.word 0
num_heads:
.word 0