Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read a write a sector from hard drive with int 13h

I have a simple program. It must read first sector from hard drive (not mbr), and write it to the 0 sector (mbr). But it doesnt work. I think it is connected with wrong DAP. Thanks.

    [bits   16]
    [org    0x7c00]

;clear screen
start:
    mov     ax, 0x3
    int     0x10

;reset the hard drive
    xor     ah, ah
    mov     dl, 0x80
    int     0x13
    jnz     error

;read the second sector
    mov     si, DAP
    mov     ah, 0x42
    int     0x13

    mov     si, data
    call    print_string
    jmp     $

DAP:
    db      0x10    ;size of DAP
    db      0x0     ;zero
    db      0x1     ;number of sectors to read
    db      0x0     ;zero
;point to memory
    dw      0x0     ;offset
    dw      0x0     ;segment
    dq      0x1     ;disk address

DAP2:
    db      0x10
    db      0x0
    db      0x1
    db      0x0
    dw      0x0
    dw      0x0
    dd      0x0
    dd      0x0            

print_string:
    mov     ax, 0xb800
    mov     es, ax
    xor     di, di
    mov     cx, 8
    rep     movsw
    ret
data: db 'H',2,'e',2,'l',2,'l',2
error:db 'E',2,'r',2,'r',2
    times   510 - ($ - $$) db 0
    dw      0xaa55   

UPD: new code

    [bits   16]
    [org    0x7c00]

;clear screen
start:
;    mov     ah, 0
;    push    ax
;    pop     ds
    mov     ax, 0x3
    int     0x10

;reset the hard drive
    xor     ah, ah
    mov     dl, 0x80
    int     0x13
    jc      error

;read the second sector
    mov     si, DAP
    mov     ah, 0x42
    int     0x13

    mov     si, data
    call    print_string
    jmp     $

DAP:
    db      0x10    ;size of DAP
    db      0x0     ;zero
    db      0x1     ;number of sectors to read
    db      0x0     ;zero
;point to memory
    dw      0x0     ;offset
    dw      0x8c00  ;segment
    dq      0x1     ;disk address

DAP2:
    db      0x10
    db      0x0
    db      0x1
    db      0x0
    dw      0x0
    dw      0x8c00
    dq      0x2            

print_string:
    mov     ax, 0xb800
    mov     es, ax
    xor     di, di
    mov     si, 0x8c00
    mov     cx, 8
    rep     movsw
    ret

data: db 'H',2,'e',2,'l',2,'l',2
error:db 'E',2,'r',2,'r',2
endp:
    times   510 - ($ - $$) db 0
    dw      0xaa55 

P.S. I'm using Bochs.

like image 618
Vanzef Avatar asked Mar 19 '13 11:03

Vanzef


Video Answer


1 Answers

A bit of necrophilia; hopefully your assembly skills have improved in the meantime. But just in case...

To quote @Alexey Frunze, "You need to pay attention to what you're doing". In addition to the mistakes detailed in the other answers, here are some of my observations:


Your emulator is too kind

  • Your code appears to be a bootloader. You assume the BIOS will load your code at 0x0000:0x7C00, but you cannot be sure it doesn't in fact load it at 0x07C0:0000, or any other equivalent address. Read up on segmentation.

  • You fail to initialise any segment registers. You probably get away with it because your emulator is kind, and correctly initialises cs, ds, and es to 0x0000.

You can solve both of these problems like this:

[bits 16]
[org 0x7C00]

    jmp 0x0000:start_16 ; ensure cs == 0x0000

start_16:
    ; initialise essential segment registers
    xor ax, ax
    mov ds, ax
    mov es, ax

Fundamental misunderstandings

  • In the event of an error, you jump directly to a string, rather than executable code. Lord only knows what the computer will do if that happens.

  • You check the return value (CF) of the drive reset, but not the read itself. In the event of a read fail, you should reset the drive and attempt the read again. Do this in a loop for a few attempts (say, 3) in case the drive is hiccuping. If the drive reset fails, it's likely something more serious is wrong, and you should bail.


A safer approach

I would suggest using int 0x13, ah = 0x02. You are using an extended BIOS function that may not be supported on all systems (emulator support might be flakey, not to mention the lazy BIOS implementations found on some modern-day hardware). You're in real mode - you don't need to do anything fancy. It would be best to get into protected mode with the long-term goal of writing a PM driver to handle disk I/O.

As long as you stay in real mode, here is a standalone function that will read one or more sectors from disk using simple BIOS functions. If you don't know in advance which sector(s) you need, you will have to add additional checks to take care of multitrack reads.

; read_sectors_16
;
; Reads sectors from disk into memory using BIOS services
;
; input:    dl      = drive
;           ch      = cylinder[7:0]
;           cl[7:6] = cylinder[9:8]
;           dh      = head
;           cl[5:0] = sector (1-63)
;           es:bx  -> destination
;           al      = number of sectors
;
; output:   cf (0 = success, 1 = failure)

read_sectors_16:
    pusha
    mov si, 0x02    ; maximum attempts - 1
.top:
    mov ah, 0x02    ; read sectors into memory (int 0x13, ah = 0x02)
    int 0x13
    jnc .end        ; exit if read succeeded
    dec si          ; decrement remaining attempts
    jc  .end        ; exit if maximum attempts exceeded
    xor ah, ah      ; reset disk system (int 0x13, ah = 0x00)
    int 0x13
    jnc .top        ; retry if reset succeeded, otherwise exit
.end:
    popa
    retn

Your print function assumes a colour monitor (by writing to video memory at 0xB8000). Again, you're in real mode. Keep it simple. Use a BIOS service:

; print_string_16
;
; Prints a string using BIOS services
;
; input:    ds:si -> string

print_string_16:
    pusha
    mov  ah, 0x0E    ; teletype output (int 0x10, ah = 0x0E)
    mov  bx, 0x0007  ; bh = page number (0), bl = foreground colour (light grey)
.print_char:
    lodsb            ; al = [ds:si]++
    test al, al
    jz   .end        ; exit if null-terminator found
    int  0x10        ; print character
    jmp  .print_char ; repeat for next character
.end:
    popa
    retn

Example usage

load_sector_2:
    mov  al, 0x01           ; load 1 sector
    mov  bx, 0x7E00         ; destination (might as well load it right after your bootloader)
    mov  cx, 0x0002         ; cylinder 0, sector 2
    mov  dl, [BootDrv]      ; boot drive
    xor  dh, dh             ; head 0
    call read_sectors_16
    jnc  .success           ; if carry flag is set, either the disk system wouldn't reset, or we exceeded our maximum attempts and the disk is probably shagged
    mov  si, read_failure_str
    call print_string_16
    jmp halt                ; jump to a hang routine to prevent further execution
.success:
    ; do whatever (maybe jmp 0x7E00?)


read_failure_str db 'Boot disk read failure!', 13, 10, 0

halt:
    cli
    hlt
    jmp halt

Last but not least...

Your bootloader doesn't set up a stack. The code I provided uses the stack to prevent register trashing. There is almost 30KiB available before the bootloader (< 0x7C00), so you can simply do this somewhere near the start of your bootloader:

xor ax, ax
cli         ; disable interrupts to update ss:sp atomically (AFAICT, only required for <= 286)
mov ss, ax
mov sp, 0x7C00
sti

Phew! A lot to digest. Notice I've tried to keep the standalone functions flexible, so you can re-use them in other 16-bit real mode programs. I'd suggest you try to write more modular code, and stick to this approach until you're more experienced.

For example, if you're dead set on using the extended read function, perhaps you should write a function that accepts a DAP, or a pointer to one, on the stack. Sure, you'll waste code space pushing the data there in the first place, but once it's there you can simply adjust the necessary fields for subsequent reads, rather than having lots of DAPs taking up memory. The stack space can be reclaimed later.

Don't be disheartened, assembly takes time, and monstrous attention to detail... not easy when bashing this stuff out at work, so there might be errors in my code! :)

like image 121
linguamachina Avatar answered Sep 27 '22 18:09

linguamachina