|
|
@@ -73,64 +73,153 @@ print:
|
|
|
; NOTE: QEMU's HD only supports LBA mode. So this MBR can't load it successfully. :-)
|
|
|
|
|
|
read_it:
|
|
|
- ; BIOS 0x13, AH = 0x02: read disk in CHS mode
|
|
|
- ; AL: sectors will read
|
|
|
- ; CH: cylinder
|
|
|
- ; CL: sector start
|
|
|
- ; DH: head
|
|
|
- ; DL: driver
|
|
|
- ; BX: buffer address (segment register ES)
|
|
|
+ ; we try LBA first because some disks supports wrong CHS. (bad sector per track)
|
|
|
+ ; if this boot disk doesn't support LBA mode, it should be a floppy.
|
|
|
+ mov ax, 0x07e0
|
|
|
+ mov es, ax
|
|
|
+
|
|
|
+ mov ax, 1
|
|
|
+ mov cl, 1
|
|
|
+ xor bx, bx
|
|
|
+
|
|
|
+ call read_lba
|
|
|
+
|
|
|
+ jnc using_lba
|
|
|
+
|
|
|
+ ; so we use CHS to read now.
|
|
|
|
|
|
- ; segment address
|
|
|
mov ax, 0x07e0
|
|
|
mov es, ax
|
|
|
|
|
|
- mov ch, 0
|
|
|
- mov dh, 0
|
|
|
+ xor ax, ax
|
|
|
+
|
|
|
+.read_it:
|
|
|
+ xor bx, bx
|
|
|
+
|
|
|
+ inc ax
|
|
|
+ mov cl, 1
|
|
|
+
|
|
|
+ push ax
|
|
|
+ push bx
|
|
|
+
|
|
|
+ call read_chs
|
|
|
+
|
|
|
+ pop bx
|
|
|
+ pop ax
|
|
|
+
|
|
|
+ mov bx, es
|
|
|
+ add bx, 512 / 16
|
|
|
+ mov es, bx
|
|
|
+
|
|
|
+ cmp ax, 1024
|
|
|
+
|
|
|
+ jb .read_it
|
|
|
+
|
|
|
+read_chs:
|
|
|
+ pushaw
|
|
|
+
|
|
|
+ ; LBA address in AX, and sectors count in CL, buffer in ES:BX
|
|
|
+
|
|
|
+ ; LBA to CHS:
|
|
|
+ ; TEMP = LBA / SECTORS_PER_TRACK
|
|
|
+ ; SECTOR = (LBA % SECTORS_PER_TRACK) + 1
|
|
|
+ ; CYLINDER = TEMP >> 1
|
|
|
+ ; HEAD = TEMP & 1
|
|
|
+
|
|
|
+ ; DIV: unsigned divide
|
|
|
+ ; 12345 / 123:
|
|
|
+ ; mov ax, 12345 : dividend
|
|
|
+ ; mov bl, 123 : divisor
|
|
|
+ ; AL: quotient
|
|
|
+ ; AH: remainder
|
|
|
+
|
|
|
+ ; 18: sectors per track for 1.44M floppy (and for my u-disk)
|
|
|
+
|
|
|
+ ; save bx first
|
|
|
+ push bx
|
|
|
+
|
|
|
+ mov bl, 18
|
|
|
+ div bl
|
|
|
+
|
|
|
+ ; BIOS standard interrupt: 0x13 AH = 0x02: read disk in CHS mode
|
|
|
+ ; AL: sectors count
|
|
|
+ ; CH: cylinder number low
|
|
|
+ ; CL: 0-5b: sector number, 6-7b: cylinder number (disk only)
|
|
|
+ ; DH: head
|
|
|
+ ; DL: driver
|
|
|
+ ; ES:BX: buffer
|
|
|
+
|
|
|
+ ; SECTOR = AH + 1
|
|
|
+ ; but CL used to save sectors count, so we save it first.
|
|
|
+ push cx
|
|
|
+
|
|
|
+ ; we inc ah because it's shorter than inc cl
|
|
|
+
|
|
|
+ inc ah
|
|
|
+ mov cl, ah
|
|
|
+
|
|
|
+ ; CH: CYLINDER = (LBA / SECTORS_PER_TRACK) >> 1
|
|
|
+
|
|
|
|
|
|
- ; sector 1 is MBR (this code)
|
|
|
- mov cl, 2
|
|
|
+ mov dh, al
|
|
|
+ and dh, 1
|
|
|
|
|
|
-.retry:
|
|
|
- mov ah, 0x02
|
|
|
+ shr al, 1
|
|
|
+ mov ch, al
|
|
|
|
|
|
- ; only read one sectors
|
|
|
- mov al, 1
|
|
|
- mov bx, 0x0000
|
|
|
+ ; for 'push cx'
|
|
|
+ pop ax
|
|
|
|
|
|
+ ; for 'push bx'
|
|
|
+ pop bx
|
|
|
+
|
|
|
mov dl, [BOOTDRIVER]
|
|
|
|
|
|
+ ; okay, now we read it
|
|
|
+
|
|
|
+ mov ah, 2
|
|
|
int 0x13
|
|
|
|
|
|
jc read_failed
|
|
|
|
|
|
-.next_sector:
|
|
|
- mov ax, es
|
|
|
- ; add 512 for memory address
|
|
|
- add ax, 512 / 16
|
|
|
+ popaw
|
|
|
+
|
|
|
+ ret
|
|
|
+
|
|
|
+
|
|
|
+using_lba:
|
|
|
+ ; put message on screen
|
|
|
+ mov si, lamsg
|
|
|
+ call print
|
|
|
+
|
|
|
+ ; the first sector (LBA address starts at 0) read,
|
|
|
+ ; so we read start at address 1
|
|
|
+
|
|
|
+ ; and we should add address
|
|
|
+
|
|
|
+ mov ax, 0x07e0
|
|
|
mov es, ax
|
|
|
|
|
|
- ; sector++
|
|
|
- add cl, 1
|
|
|
- ; if sector > 18
|
|
|
- cmp cl, 18
|
|
|
- jbe .retry
|
|
|
+ mov ax, 1
|
|
|
+
|
|
|
+.read_loop:
|
|
|
+ mov bx, es
|
|
|
+ add bx, 512 / 16
|
|
|
+ mov es, bx
|
|
|
+
|
|
|
+ inc ax
|
|
|
|
|
|
- ; next head
|
|
|
mov cl, 1
|
|
|
- ; head++
|
|
|
- add dh, 1
|
|
|
- ; if head == 2 (head can only be 0 or 1)
|
|
|
- cmp dh, 2
|
|
|
- jb .retry
|
|
|
-
|
|
|
- ; next cylinder
|
|
|
- mov dh, 0
|
|
|
- add ch, 1
|
|
|
+
|
|
|
+ xor bx, bx
|
|
|
+
|
|
|
+ call read_lba
|
|
|
|
|
|
- ; if cylinder == CYLINDERS_NEED
|
|
|
- cmp ch, cyldsneed
|
|
|
- jb .retry
|
|
|
+ ; 1024 sectors = 512 KB,
|
|
|
+ ; I think this is enough anytime for our kernel.
|
|
|
+
|
|
|
+ cmp ax, 1024
|
|
|
+ jb .read_loop
|
|
|
|
|
|
ret
|
|
|
|
|
|
@@ -143,8 +232,38 @@ read_failed:
|
|
|
jmp .die
|
|
|
|
|
|
|
|
|
+read_lba:
|
|
|
+ pushaw
|
|
|
+
|
|
|
+ push dword 0x0000
|
|
|
+ push dword eax
|
|
|
+
|
|
|
+ ; buffer address
|
|
|
+ push word es
|
|
|
+ push word bx
|
|
|
+
|
|
|
+ ; sectors count
|
|
|
+ push word cx
|
|
|
+
|
|
|
+ ; package size
|
|
|
+ push word 16
|
|
|
+
|
|
|
+ mov ah, 0x42
|
|
|
+ mov dl, [BOOTDRIVER]
|
|
|
+ mov si, sp
|
|
|
+
|
|
|
+ int 0x13
|
|
|
+
|
|
|
+ add sp, 16
|
|
|
+
|
|
|
+ popaw
|
|
|
+
|
|
|
+ ret
|
|
|
+
|
|
|
+
|
|
|
boot_msg: db "Loading System...", 0x0d, 0x0a, 0x00
|
|
|
read_fail_msg: db "panic: read disk failed. ", 0x0d, 0x0a, 0x00
|
|
|
+lamsg: db "LBA mode is avaliable. ", 0x0d, 0x0a, 0x00
|
|
|
|
|
|
|
|
|
times 510 - ($ - $$) db 0x00
|