2020-04-27 15:56:41 +02:00
|
|
|
section .bss
|
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
disk_buffer:
|
|
|
|
resb 512
|
2020-04-27 15:56:41 +02:00
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
disk_current:
|
|
|
|
resb 1
|
|
|
|
|
|
|
|
lba_supported:
|
|
|
|
resb 1
|
|
|
|
|
2020-05-08 22:48:23 +02:00
|
|
|
; Seek state and geometry info for CHS access
|
2020-05-02 23:57:23 +02:00
|
|
|
disk_chs: ; DL, DH, CL, CH for int 13h CHS functions
|
|
|
|
resb 4
|
2020-05-06 22:28:30 +02:00
|
|
|
disk_spc: ; sectors per cylinder = heads * spt
|
2020-05-02 23:57:23 +02:00
|
|
|
resw 1
|
|
|
|
disk_spt:
|
|
|
|
resb 1
|
2020-04-27 15:56:41 +02:00
|
|
|
|
2020-05-08 22:48:23 +02:00
|
|
|
; Partition conf from MBR or dummy for floppies
|
2020-05-06 22:28:30 +02:00
|
|
|
part_offset:
|
|
|
|
resd 1
|
|
|
|
part_size:
|
|
|
|
resd 1
|
|
|
|
|
2020-05-08 22:48:23 +02:00
|
|
|
dap: ; Disk access packet for EBIOS extensions
|
|
|
|
resw 1
|
|
|
|
dap_sectors:
|
|
|
|
resw 1
|
|
|
|
dap_buffer:
|
|
|
|
resd 1
|
|
|
|
dap_sector:
|
|
|
|
resq 1
|
2020-04-28 22:09:56 +02:00
|
|
|
|
2020-04-27 15:56:41 +02:00
|
|
|
section .text
|
|
|
|
|
|
|
|
; Convert between drive number and BIOS dl
|
|
|
|
; Bidirectional mapping
|
|
|
|
; 0 <-> 0, 1 <-> 1, 2 <-> 0x80, 3 <-> 0x81
|
|
|
|
dnconv: mov cx, 7
|
|
|
|
ror dx, 1
|
|
|
|
rol dl, 1
|
|
|
|
dncl: rcl dl, 1
|
|
|
|
rcr dh, 1
|
|
|
|
loop dncl
|
|
|
|
xchg dh, dl
|
|
|
|
ret
|
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
; Select a drive for I/O
|
2020-05-08 22:48:23 +02:00
|
|
|
; This leave the first sector of the partition in the buffer
|
2020-05-02 23:57:23 +02:00
|
|
|
; IN dl 0=A, 1=B, 2=C, 3=D
|
|
|
|
select: mov byte [disk_current], 0xFF
|
|
|
|
mov byte [lba_supported], 0
|
|
|
|
call dnconv
|
|
|
|
mov byte [disk_chs], dl
|
2020-05-08 22:48:23 +02:00
|
|
|
cmp dl, 0x80
|
|
|
|
jc select_floppy
|
|
|
|
; detect EBIOS/LBA extensions
|
2020-05-02 23:57:23 +02:00
|
|
|
mov ah, 0x41
|
|
|
|
mov bx, 0x55AA
|
|
|
|
int 0x13
|
|
|
|
test cx, 1
|
2020-05-06 22:28:30 +02:00
|
|
|
jz select_hdd
|
2020-05-02 23:57:23 +02:00
|
|
|
cmp bx, 0xAA55
|
2020-05-06 22:28:30 +02:00
|
|
|
jnz select_hdd
|
2020-05-02 23:57:23 +02:00
|
|
|
mov byte [lba_supported], 1
|
2020-05-06 22:28:30 +02:00
|
|
|
jmp select_hdd
|
2020-04-27 15:56:41 +02:00
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
select_floppy:
|
2020-05-08 00:56:37 +02:00
|
|
|
push dx
|
2020-05-06 22:28:30 +02:00
|
|
|
call seek_zero
|
2020-05-02 23:57:23 +02:00
|
|
|
call read
|
2020-05-06 22:28:30 +02:00
|
|
|
; load disk geometry
|
2020-05-04 21:49:26 +02:00
|
|
|
mov ax, [disk_buffer+0x0B+bpb_spt]
|
|
|
|
mov [disk_spt], ax
|
|
|
|
mul word [disk_buffer+0x0B+bpb_nos]
|
2020-05-06 22:28:30 +02:00
|
|
|
mov [disk_spc], ax
|
|
|
|
; set partition size
|
|
|
|
; we dont rely on the 'total sectors' given in the BPB
|
|
|
|
; instead, we assume maximum cylinder
|
|
|
|
; 1023 is the maximum encodable for int 13h
|
2020-05-08 00:56:37 +02:00
|
|
|
lea di, [part_offset]
|
2020-05-06 22:28:30 +02:00
|
|
|
xor ax, ax
|
2020-05-08 00:56:37 +02:00
|
|
|
; partition offset = 0
|
|
|
|
stosw
|
|
|
|
stosw
|
|
|
|
; partition size = 0x00010000 = 32MB
|
|
|
|
stosw
|
|
|
|
inc ax
|
|
|
|
stosw
|
|
|
|
|
|
|
|
pop dx
|
2020-05-06 22:28:30 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
select_hdd:
|
2020-05-08 00:56:37 +02:00
|
|
|
push dx
|
|
|
|
; load CHS geometry for drive
|
|
|
|
mov ah, 0x8
|
|
|
|
int 0x13
|
|
|
|
xor ah, ah
|
|
|
|
mov al, cl
|
|
|
|
mov [disk_spt], ax
|
|
|
|
mul byte dh
|
|
|
|
mov [disk_spc], ax
|
|
|
|
; load offset and size of first partition
|
2020-05-06 22:28:30 +02:00
|
|
|
call seek_zero
|
|
|
|
call read
|
|
|
|
mov bx, disk_buffer+0x1BE
|
|
|
|
lea si, [bx+8]
|
|
|
|
lea di, [part_offset]
|
|
|
|
mov cx, 4
|
|
|
|
rep movsw
|
2020-05-08 00:56:37 +02:00
|
|
|
; read bpb
|
|
|
|
mov dx, [part_offset+2]
|
|
|
|
mov ax, [part_offset]
|
|
|
|
call seek
|
|
|
|
call read
|
|
|
|
pop dx
|
2020-04-27 15:56:41 +02:00
|
|
|
ret
|
2020-05-02 23:57:23 +02:00
|
|
|
|
|
|
|
; Set absolute sector number
|
|
|
|
; IN dx:ax 32-bit sector number
|
|
|
|
seek: push ax
|
|
|
|
or ax, dx
|
2020-04-27 15:56:41 +02:00
|
|
|
pop ax
|
2020-05-02 23:57:23 +02:00
|
|
|
jz seek_zero
|
|
|
|
|
2020-05-08 22:48:23 +02:00
|
|
|
test byte [lba_supported], 1
|
|
|
|
jnz seek_lba
|
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
; dx:ax = lba
|
2020-05-06 22:28:30 +02:00
|
|
|
div word [disk_spc]
|
2020-05-02 23:57:23 +02:00
|
|
|
xchg ax, dx
|
|
|
|
; dx = cylinder, ax = head * spt + sector
|
|
|
|
div byte [disk_spt]
|
|
|
|
; dx = cylinder, al = head, ah = sector
|
|
|
|
xchg dl, dh
|
|
|
|
ror dl, 1
|
|
|
|
ror dl, 1
|
|
|
|
or dl, ah
|
2020-05-04 21:49:26 +02:00
|
|
|
inc dx
|
2020-05-02 23:57:23 +02:00
|
|
|
; dh bit 0-7: cylinder 0-7
|
|
|
|
; dl bit 0-5: sector number 0-5
|
|
|
|
; dl bit 6-7: cylinder 8-9
|
|
|
|
; store
|
|
|
|
mov byte [disk_chs+1], al
|
|
|
|
mov word [disk_chs+2], dx
|
2020-04-27 15:56:41 +02:00
|
|
|
ret
|
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
seek_zero:
|
2020-05-06 22:28:30 +02:00
|
|
|
; set chs data to first sector
|
2020-05-02 23:57:23 +02:00
|
|
|
mov byte [disk_chs+1], 0
|
|
|
|
mov word [disk_chs+2], 1
|
2020-05-06 22:28:30 +02:00
|
|
|
; set lba data to first sector
|
|
|
|
xor ax, ax
|
2020-05-08 22:48:23 +02:00
|
|
|
xor dx, dx
|
|
|
|
seek_lba:
|
|
|
|
mov word [dap], 0x10
|
|
|
|
mov word [dap_sectors], 1
|
|
|
|
mov word [dap_buffer], disk_buffer
|
|
|
|
mov [dap_buffer+2], cs
|
|
|
|
lea di, [dap_sector]
|
|
|
|
stosw
|
|
|
|
mov ax, dx
|
|
|
|
stosw
|
|
|
|
xor ax, ax
|
|
|
|
stosw
|
|
|
|
stosw
|
2020-05-02 23:57:23 +02:00
|
|
|
ret
|
2020-04-27 15:56:41 +02:00
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
; Read a sector into buffer
|
2020-05-08 22:48:23 +02:00
|
|
|
read: test byte [lba_supported], 1
|
|
|
|
jnz read_lba
|
|
|
|
mov ax, 0x0201
|
2020-05-02 23:57:23 +02:00
|
|
|
mov dx, [disk_chs]
|
|
|
|
mov cx, [disk_chs+2]
|
|
|
|
lea bx, [disk_buffer]
|
2020-04-28 22:09:56 +02:00
|
|
|
push cs
|
2020-05-02 23:57:23 +02:00
|
|
|
pop es
|
2020-04-27 15:56:41 +02:00
|
|
|
int 0x13
|
2020-05-02 23:57:23 +02:00
|
|
|
ret
|
2020-04-27 15:56:41 +02:00
|
|
|
|
2020-05-08 22:48:23 +02:00
|
|
|
read_lba:
|
|
|
|
mov ah, 0x42
|
|
|
|
mov dl, [disk_chs]
|
|
|
|
lea si, [dap]
|
|
|
|
int 0x13
|
|
|
|
ret
|
|
|
|
|
2020-05-02 23:57:23 +02:00
|
|
|
; Write a sector into buffer
|
2020-05-04 21:49:26 +02:00
|
|
|
write: stc
|
2020-05-02 23:57:23 +02:00
|
|
|
ret
|