Page 1 of 1

[SOLVED] Bootloader - problem with reading floppy (int 13h)

Posted: Fri Jan 28, 2011 3:26 pm
by norfavrell
Hey. I'm new to this forum so let me introduce myself ;) I'm 17 years old student, with 7 years of experience of web developing and programming :) I recently got interested in low level programming and thus I started making an OS. It always fascinated me how OSs work plus I'm considering making small OS as part of my programming coursework later during the year.

I found myself in a bit of a trouble. Trying to implement multi-stage bootloader, I developed code to read root directory, FATs, locate file etc. It didn't work however. I started experimenting and came to the conclusion that it's reading floppy thats failing. I developed a small bootloader that is just meant to read sector onto 0x1000 and execute it (pretty much like one from tutorial). It also didn't work. Using GDB i determined that BIOS reported that operation was successful (CF flag clear, AL as expected) but the buffer address remained clear.

Code: Select all

[bits 16]
[org 0x0]
start: jmp loader
times 0x0b-$+start db 0
bpbBytesPerSector:		dw 512
bpbSectorsPerCluster:	db 1
bpbReservedSectors:		dw 1		;bootsector
bpbNumberOfFats:		db 2		;FAT12 has 2 fats
bpbRootEntries:			dw 224		;224 root directories is max for floppy
bpbTotalSectors:		dw 2880		;max 2880 sectors per floppy
bpbMedia:				db 0xf0		;singled sided,9 sectors per fat, 80 tracks, movable
bpbSectorsPerFat:		dw 9
bpbSectorsPerTrack:		dw 18
bpbHeadsPerCylinder:	dw 2
bpbHiddenSectors:		dd 0
bpbTotalSectorsBig:		dd 0
bsDriveNumber:			db 0
bsUnused:				db 0
bsExtBootSignature:		db 0x29		;MS/PC-DOS version 4.0 Bios Parameter Block (BPB)
bsSerialNumber:			dd 0x74fe574
bsVolumeLable:			db "TestOS     "
bsFileSystem:			db "FAT12   "
;*****************************
;******** ENTRY POINT ********
;*****************************
loader:
;****** RESET FLOPPY *********
ResetLoop:
	mov ah,0x00						;function 0 - reset
	mov dl,0x00						;drive number
	int 0x13
	jc ResetLoop					;error - try again
	
	mov		ax, 0x1000				;setup buffer address
	mov		es, ax
	xor		bx, bx
	
	mov		ah, 0x02				;read floppy sector function
	mov		al, 0x01				;number of sectors to read
	mov		ch, 0x01				;track
	mov		cl, 0x02				;start sector
	mov		dh, 0x00				;head number
	mov		dl, 0x00				;drive number
	
	mov byte[0x7c00],0x00			;clear space used for debug (ensure no random vals)
	mov byte[0x7c01],0x00
	
	int 0x13
	jc carry
	jmp end
carry:								;there was an error -> set data in RAM for debug
	mov byte[0x7c00],0xAA
end:
	mov byte[0x7c01],al				;save number of readed sectors to RAM for debug
	jmp 0x1000:0x00					;jump to new sector
	
;**** REST OF THE SECTOR *****
times 510-($-$$) 	db 0
dw 0xAA55
int 0x18							;this should be read by bootloader

Memory inspection revealed:

Code: Select all

0x7c00:	0x00	0x01	0x00	0x00	0x00	0x00	0x00	0x00
Which is as expected.

Code: Select all

0x1000:	0x00	0x00	0x00	0x00	0x00	0x00	0x00	0x00

Which means that in fact data wasn't loaded.

Floppy image shows that instruction is at correct address:

Code: Select all

0x200:	0xCD 0x18 0xFF 0x00 0x00 0x00 0x00 0x00
It's probably something easy but i spent last 2 days trying to find the answer, and experimenting with the code: so far without result. If you happen to know what's wrong please tell me ;)

Thanks in advance,
Bart

Re: Bootloader - problem with reading floppy (int 13h)

Posted: Fri Jan 28, 2011 3:32 pm
by Artlav
Just in case:
mov ax, 0x1000
mov es, ax

That points to absolute address 0x10000.
Do you check that, or 0x1000?

jmp 0x1000:0x00 Suggests you are aware of it.
Does the code get executed out there?

What are you running it in with debug? Bochs?

Re: Bootloader - problem with reading floppy (int 13h)

Posted: Fri Jan 28, 2011 3:39 pm
by norfavrell
Thanks for fast answer. Yes I did check segment 0x1000 (at absolute 0x10000). For debug I'm using bochs with grub stub. I also tested that bootloader on virtualbox (just in case it was internal problem with bochs; yes i know its unlikely but i still though its worth checking.. turns out it doesnt work in virtualbox either).

Thanks,
Bart

Re: Bootloader - problem with reading floppy (int 13h)

Posted: Fri Jan 28, 2011 3:46 pm
by Artlav
Wait a sec, why mov ch, 0x01 ?
Isn't that cylinder 1, i.e. sector 18 or so absolute?
What if you use track 0?

Re: Bootloader - problem with reading floppy (int 13h)

Posted: Fri Jan 28, 2011 3:49 pm
by norfavrell
O.0'!
It worked indeed. I spent nearly 13 hours trying to work it out and it was such a stupid mistake. Since sectors are indexed from 1 i thought that so are tracks. This is a really really stupid mistake of me. Sorry about that.

Thank you a lot for help you saved me quite possibly many many more hours trying to make it work.
Thanks :)
Bart