;; Calling format:
;; void raw_write (u8 *buf, u32 off, u32 drive, u8 sector)
raw_write:
push ebp
mov ebp, esp
push ebx
push esi
mov ebx, [ebp+16] ; al will be written to 1F6
and ebx, 0x1
shl ebx, 0x4
or ebx, 0x40
mov dx, 0x1F2 ; get sector number
mov al, 1
out dx, al
mov eax, [ebp+12] ; eax = offset
inc dx
out dx, al ; LBA low
shr eax, 8
inc dx
out dx, al ; LBA mid
shr eax, 8
inc dx
out dx, al ; LBA high
shr eax, 8
and eax, 0xF
; or al, bl
or al, 0xE0
mov dx, 0x1F6
out dx, al ; bl calculated above, plus LBA veryhigh
mov al, 0x30 ; WRITE
inc dx
out dx, al
.chkstat:
mov ecx, 1000000 ; Timeout of about one second (I/O usually takes one
usec)
in al, dx ; Status
and al, 0x88
jz .return
cmp al, 0x80
jnz .next
loop .chkstat
;; print a message saying we timed out
jmp .return
.next:
mov dx, 0x1F0
mov ecx, 256 ; write 256 * number of sectors words
mov esi, [ebp + 8] ; buffer pointer
rep outsw
;; print a message saying we're done
.return:
pop esi
pop ebx
mov esp, ebp
pop ebp
ret
global raw_read
;; Calling format:
;; void raw_read (u8 *buf, u32 off, u32 drive)
raw_read:
push ebp
mov ebp, esp
push ebx
push edi
mov ebx, [ebp+16] ; al will be written to 1F6
and ebx, 0x1
shl ebx, 0x4
or ebx, 0x40
mov dx, 0x1F2 ; get sector number
mov al, 1
out dx, al
mov eax, [ebp+12] ; eax = offset
inc dx
out dx, al ; LBA low
shr eax, 8
inc dx
out dx, al ; LBA mid
shr eax, 8
inc dx
out dx, al ; LBA high
shr eax, 8
and eax, 0xF
; or al, bl
or al, 0xE0
mov dx, 0x1F6
out dx, al ; bl calculated above, plus LBA veryhigh
mov al, 0x20 ; READ
inc dx
out dx, al
.chkstat:
mov ecx, 1000000 ; Timeout of about one second (I/O usually takes one
usec)
in al, dx ; Status
and al, 0x88
jz .return
cmp al, 0x80
jnz .next
loop .chkstat
;; print a message saying we timed out
jmp .return
.next:
mov dx, 0x1f0
mov ecx, 256 ; read 256 * number of sectors words
mov edi, [ebp + 8] ; buffer pointer
rep insw
;; print a message saying we're done
.return:
pop edi
pop ebx
mov esp, ebp
pop ebp
ret