-# Poll status register while controller busy.
-
- mov edx, 0x1f7
-1: in al, dx
- test al, 0x80
- jnz 1b
-
-# Read a single sector.
-
- mov edx, 0x1f2
- mov al, 1
- out dx, al
-
-# Sector number to write in low 28 bits.
-# LBA mode, device 0 in top 4 bits.
-
- mov eax, ebx
- and eax, 0x0fffffff
- or eax, 0xe0000000
-
-# Dump eax to ports 0x1f3...0x1f6.
-
- mov ecx, 4
-1: inc dx
- out dx, al
- shr eax, 8
- loop 1b
-
-# READ command to command register.
-
- inc dx
- mov al, 0x20
- out dx, al
-
-# Poll status register while controller busy.
-
-1: in al, dx
- test al, 0x80
- jnz 1b
-
-# Poll status register until data ready.
-
-1: in al, dx
- test al, 0x08
- jz 1b
-
-# Transfer sector.
-
- mov ecx, 256
- mov edx, 0x1f0
- rep insw
-
-# Next sector.
-
- inc ebx
- cmp ebx, KERNEL_LOAD_PAGES*8 + 1
- jnz read_sector
-
-#### Jump to kernel entry point.
-
- mov eax, LOADER_PHYS_BASE + LOADER_KERN_BASE
- call eax
- jmp panic
-
-#### GDT
-
-gdt:
- .quad 0x0000000000000000 # null seg
- .quad 0x00cf9a000000ffff # code seg
- .quad 0x00cf92000000ffff # data seg
+read_harddrv_sector:
+ sub eax, eax
+ push eax # LBA sector number [32:63]
+ push ebx # LBA sector number [0:31]
+ mov ax, es
+ shl eax, 4
+ push eax # Buffer linear address
+ push 1 # Transfer one sector
+ push 16 # Packet size
+ mov ah, 0x42 # Extended read
+ mov si, sp # DS:SI -> packet
+ int 0x13 # Error code in CF
+success:
+ popa
+ ret # Error code in CF
+
+read_floppy_sector:
+ #define HEADS 2
+ #define SECTORS 36
+
+ # In: BX = LBA sector number, DL = drive.
+ # Out: BL = drive, DX = cylinder, AL = head, AH = sector.
+ sub ax, ax
+ xchg dx, bx # DX = LBA sector number, BL = drive
+ mov cx, HEADS * SECTORS
+ div cx # AX = cyl, DX = hd + (sec-1) * SECTORS
+ xchg ax, dx # DX = cyl, AX = hd + (sec-1) * SECTORS
+ mov cl, SECTORS
+ div cl # AL = head, AH = sector - 1
+ inc ah
+
+ # Read sector.
+ mov ch, dl # CH = cylinder
+ mov cl, ah # CL = sector
+ mov dh, al # DH = head
+ mov dl, bl # DL = drive
+ mov ax, 0x0201 # AH = function, AL = sectors to read
+ sub bx, bx # ES:BX -> buffer
+ pusha
+ int 0x13
+ popa
+ jnc success
+
+ # Reset floppy drive motor, try again.
+ sub ah, ah
+ int 0x13
+ popa
+ jmp read_sector