Reading Sectors with BIOS

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Goober

Reading Sectors with BIOS

Post by Goober »

No matter what I do, this always returns an error (Control data access mark detected). I'm just trying to read the second sector of the floppy disk into 0x1000:0x0000. Does anyone see something wrong with what I'm setting in my registers? Thanks.

Code: Select all

mov dl,0x00
mov bx,0x1000
mov es,bx
mov bx,0x0000
mov ch,0x01
mov cl,0x02
mov dh,0x01
mov al,0x01
mov cl,0x02
mov ah,0x02
int 0x13
Description of the bios interrupt:
AH = 02h
AL = number of sectors to read (must be nonzero)
CH = low eight bits of cylinder number
CL = sector number 1-63 (bits 0-5)
high two bits of cylinder (bits 6-7, hard disk only)
DH = head number
DL = drive number (bit 7 set for hard disk)
ES:BX -> data buffer
AxelDominatoR

Re:Reading Sectors with BIOS

Post by AxelDominatoR »

Head Number, DH, starts from 0 IIRC so you could try:

Code: Select all

mov    dh, 0x00
Axel
Goober

Re:Reading Sectors with BIOS

Post by Goober »

No change.

Thanks anyways, axel. Any other ideas?
Ushma

Re:Reading Sectors with BIOS

Post by Ushma »

dh = ch = 0;

have you tried this on bochs?
or with a different disk?
or whatever else?

There could be something wrong other than the code.
Goober

Re:Reading Sectors with BIOS

Post by Goober »

I've tried it with multiple floppies, in bochs and on multiple machines. Maybe it could be something done or not done before I try to load the sector? All that comes before this is clearing the segment registers and setting up the stack.
Goober

Re:Reading Sectors with BIOS

Post by Goober »

I was just thinking, maybe it's how I'm writing the ionformation to the disk in the first place? The bootsector is 512 bytes and the kernel is 88 bytes. I'm writing it with partcopy like this:

partcopy boot.bin 0 200 -f0 0
partcopy kernel.bin 0 58 -f0 200
GLneo

Re:Reading Sectors with BIOS

Post by GLneo »

if you like it might help to post your boot code ;)
Goober

Re:Reading Sectors with BIOS

Post by Goober »

Here it is all together.

Code: Select all

; PURE Bootloader
; _______________

; Set this up as our bootloader
org 0x7C00
use16

; Clear segment registers
mov ax,cs
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax

; Set up stack
mov si,msg3
call print
call br
cli
mov ax,0x0000
mov ss,ax
mov sp,0x8000
sti

; Reset floppy drive
mov ah,0x00
mov dl,0x00
int 0x13

; Load the second sector into RAM at 0x1000:0x0000  (64KB)
mov si,msg1
call print
call br
mov dl,0x00
mov bx,0x1000
mov es,bx
mov bx,0x0000
mov ch,0x01
mov cl,0x02
mov dh,0x00
mov al,0x01
mov cl,0x02
mov ah,0x02
int 0x13

; Test for read error
cmp ah,0x00
jne .noerror
mov si,error1
call print
call br
mov bh,ah
mov al,bh
mov ah,0x0E
int 0x10
hlt
.noerror:

; Set the kernel as our data segment
mov ax, 0x1000
mov ds, ax

; Jump to the kernel
mov si,msg2
call print
call br
jmp 0x1000:0x0000

; Data Crap
; _________

; print - print a string
; SI - points to string
print:
mov bh,0x00
mov ah,0x0E
.loop:
lodsb
or al,al
jz .stop
int 0x10
jmp .loop
.stop:
ret

; br - line break
; no parameters
br:
mov ah,0x0E
mov al,0x0A
int 0x10
mov al,0x0D
int 0x10
ret

; Strings
msg1: db "[Boot] Reading kernel from disk...", 0
error1: db "[Boot] Read error", 0
msg2: db "[Boot] Jumping to kernel...", 0
msg3: db "[Boot] Setting up system stack...",0

; Fill up the sector
times   510 - ($-$$) db 0

; Boot sig
db 0x55,0xAA
Dex4u

Re:Reading Sectors with BIOS

Post by Dex4u »

Have you tryed this ?.

Code: Select all

jnc .noerror
Goober

Re:Reading Sectors with BIOS

Post by Goober »

That makes it crash after "reading kernel from disk...." without showeing the "read error".
Goober

Re:Reading Sectors with BIOS

Post by Goober »

Sorry for the double post.

I changed it to je though, cause that was a typo, and it didn't give a read error, so I think it's a problem jumping to the kernel. Is

jmp 0x1000:0x0000

correct?
Dex4u

Re:Reading Sectors with BIOS

Post by Dex4u »

You did not leave the cmp in did you ?, as it should not of crashed .
Goober

Re:Reading Sectors with BIOS

Post by Goober »

No, I didn't leave the cmp in.

Since it doesn't get to the part where it displays "Jumping to the kernel", I guess it does crash there though.
Goober

Re:Reading Sectors with BIOS

Post by Goober »

Again, sorry for so many posts, but I'm just discovering things now.

It crashes once when I set that area as the data segment

mov ax, 0x1000
mov ds, ax

and if I comment that out, it crashes again when it tries to jump to where we loaded the kernel.

jmp 0x1000:0x0000

I can't understand why though!
DennisCGc

Re:Reading Sectors with BIOS

Post by DennisCGc »

Hi,

I'm going to guess a bit.

First,

Code: Select all

; Set the kernel as our data segment
mov ax, 0x1000
mov ds, ax

; Jump to the kernel
mov si,msg2
call print
call br
jmp 0x1000:0x0000
You're doing something wrong here. First you set ds to 0x1000, then you actually want to load msg2 from 0x0:0x7*** (assuming your BIOS loaded it to 0x0:0x7c00) ?
I suggest you should do this instead:

Code: Select all

; Jump to the kernel
mov si,msg2
call print
call br
; Set the kernel as our data segment
mov ax, 0x1000
mov ds, ax
jmp 0x1000:0x0000
Second; you said it crashed when you jumped to 0x1000:0x0? Maybe your kernel is wrong. AFAICT, your bootsector should work.

Oh, one tip; NEVER EVER ASSUME CS=0 AND IP=0x7C00. It is a classic fault. CS could be 0x7c0 and IP could be then 0x0. Guess what happens then ;) You never know with some BIOSes.

You can do this at the beginning of your bootsector:

Code: Select all

; Set this up as our bootloader
org 0x7C00
use16

jmp 0x0:begin        ;jump to 0x0:0x7c00+begin

begin:
; Clear segment registers
mov ax,cs
mov ds,ax
mov es,ax
mov fs,ax
mov gs,ax
HTH a bit,

DennisCGc.

PS. As I indicated, your kernel might be wrong. Maybe you could post SOME snippits. (the first part is essential).
Post Reply