Loading second stage boot-loader triggers CF

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.
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Loading second stage boot-loader triggers CF

Post by Dulci »

Greetings, sorry for a noobish question, but I have written part of a bootloader, but whenever I try to load my second stage bootloader, the cf gets set. Here's my code.

Code: Select all

%macro Print 2
	mov ah, 0xa
	mov al, [%1]
	mov bl, 0xf
	mov bh, 0
	mov cx, %2
	int 0x10
%endmacro

%macro Printr 2
	mov al, %1
	mov bl, 0xf
	mov bh, 0
	mov cl, %2
	mov ah, 0xa
	int 0x10
%endmacro	

[ORG 0x7c00]
[BITS 16]

jmp 0x0000:bootstart

bootstart:
	; Set our data segment
	xor ax, ax
	mov ds, ax
	mov es, ax
	; Clear interrupts
	cli
	Print greeting, 1
	
	jmp reset
	; 512 bytes per sector
	; 18 sectors per track
	; 63 tracks
	

reset:
	mov		ah, 0
	xor 	dl, dl
	int		0x13
	jmp 	readCD
	
readCDerror:
	Print error, 2
	
readCD:
	mov ah, 0x2
	mov al, 0x59
	mov dh, 0
	mov ax, 0x500
	mov es, ax
	xor bx, bx
	xor ch, ch
	xor cl, cl
	xor dl, dl
	int 0x13
	jc readCDerror

	jmp 0x500:0x0 
	
data:
	greeting   db 'G'
	error   db 'E'
	load   db 'L'
times 510-($-$$) db 0
dw 0x55AA
I then turn this into an iso with mkisofs -no-emul-boot -o /path/to/OS/source/build/CUBE.iso -A CUBE -b bootstart.img /path/to/OS/source/build/bin and I dd the second stage bootloader into the first couple bytes of the iso, since the first 8000 bytes are unoccupied. The bootloader loads up fine, but it shows EE (which is the error code for the readCDerror section), and I don't understand why.
Gigasoft
Member
Member
Posts: 856
Joined: Sat Nov 21, 2009 5:11 pm

Re: Loading second stage boot-loader triggers CF

Post by Gigasoft »

You overwrote AX. Change it to:

Code: Select all

push word 0x500
pop es
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: Loading second stage boot-loader triggers CF

Post by Dulci »

Ah, I feel quite stupid for having missed that, thank you! I'm still getting the error though. I have changed that segment to

Code: Select all

	mov ah, 0x2
	mov al, 0x59
	mov dh, 0
	push word 0x500
	pop es
	xor bx, bx
	xor ch, ch
	xor cl, cl
	xor dl, dl
	int 0x13
	jc readCDerror
however, this is still returning an error. I thought maybe it was because al was larger than 18, but even when I set al to 1 it still sets cf.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Loading second stage boot-loader triggers CF

Post by Combuster »

You're passing the first floppy drive as the device argument for a no-emulation CD which need not have replaced device 0 - try using the DL value the bios gives you and see if that matters.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: Loading second stage boot-loader triggers CF

Post by Dulci »

Okay, just tried that, not getting any difference though. After the cary flag gets set, dl is 224 and ah is 1, which means Invalid Command, right?
felipe
Member
Member
Posts: 27
Joined: Tue Sep 28, 2010 8:19 am

Re: Loading second stage boot-loader triggers CF

Post by felipe »

Aren't you using 0 for the sector number? They start at 1.
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: Loading second stage boot-loader triggers CF

Post by Dulci »

>.< Sorry, didn't know sector number started at 1. Changed the lower portion of my code to

Code: Select all


reset:
	mov		ah, 0					; reset floppy disk function
	int		0x13					; call BIOS
	jmp 	readCD

readCDerror:
	xor 	ch, ch
	xor		cl, cl
	printr [error], ah
	jmp 	reset

readCD: ; Read 0x7700 bytes from the disk to point 0x500 in memory from beginning of cd
	mov ah, 0x2 ; What we want to do with 0x13
	mov al, 0x59 ; How much we want to read
	mov dh, 0 ; Drive head
	push word 0x500
	pop es ; Set es to 500, where we want to buffer to
	xor bx, bx ; buffer is at 500:0, es:bx
	mov ch, 1 ; Set track to 1
	mov cl, 1 ; sector to 1
	int 0x13
	jc readCDerror
	
jumpOut:
	jmp 0x500:0x0 
Still returning that invalid command error though.
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: Loading second stage boot-loader triggers CF

Post by Dulci »

Okay, so when DL is 0 it returns 128 (80h) instead of 1 through ah, which makes me think that the drive number the BIOS passes me is unrecognized.
felipe
Member
Member
Posts: 27
Joined: Tue Sep 28, 2010 8:19 am

Re: Loading second stage boot-loader triggers CF

Post by felipe »

Why did you set the track also to 1? You should look here for the specs: http://www.ctyme.com/intr/rb-0607.htm

Another things that might be causing the problem:

* try to give the drive a few chances to reset as in

Code: Select all

reset:
   mov      ah, 0               ; reset floppy disk function
   setc  ; in case a buggy bios doesn't set it on failure
   int      0x13               ; call BIOS
   jc reset
   jmp    readCD

* are you sure dl has the right drive number, you should save it when the bios passes control to your bootloader so that
you can use it later, instead of using 0 directly, unless you are sure it is 0


I hope this helps

Felipe R.
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: Loading second stage boot-loader triggers CF

Post by Dulci »

I have adjusted the reset: block, however, there is still the bug. I was trying track one to see if it would produce a slight difference form track 0 (although I do all my testing with track 0 as well). I am not sure I have the right drive number, I have tried 0, however, as Combuster pointed out, since I am not using emulation that won't work, and it returned 0x80 through ah. The value that dl is given by the bios is 224 which gives me the ah=1 return code, so I think what's happening is dl isn't being assigned properly. I am unsure what to assign to dl though.
felipe
Member
Member
Posts: 27
Joined: Tue Sep 28, 2010 8:19 am

Re: Loading second stage boot-loader triggers CF

Post by felipe »

Dulci wrote:The value that dl is given by the bios is 224 which gives me the ah=1 return code
You mean you are asking the BIOS for the drive? What I meant is to actually save the value of dl at the beginning of the bootloader (it is supposed to be left with the right one) and then use it whenever you need.

Try that to see if it works.
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: Loading second stage boot-loader triggers CF

Post by Dulci »

I don't rewrite the value of dl anywhere in the program, and int 10 ah=0xA doesn't rewrite DL, and int 13 ah=2/ah=0 doesn't rewrite it either. However, I tried what you said, I store dl into a place in memory, and then I grab it out of memory when I use int 0x13 ah=2, and dl is 97 (which is different from before when it was 224), however, its still setting the cary flag.

edit: Does anyone know what dl should be set to under these conditions?
felipe
Member
Member
Posts: 27
Joined: Tue Sep 28, 2010 8:19 am

Re: Loading second stage boot-loader triggers CF

Post by felipe »

I'm out of ideas, although that number 97 does strike me as odd, I might be wrong but I though drive numbers were supposed to be small integers. How are you testing your code, I mean which emulator or is it real hardware?
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: Loading second stage boot-loader triggers CF

Post by qw »

Ralf Brown's Interrupt List wrote:Apparently some BIOSes or intercepting resident software have bugs that may destroy DX on return or not properly set the Carry flag.
Dulci
Posts: 17
Joined: Tue Oct 05, 2010 4:34 pm

Re: Loading second stage boot-loader triggers CF

Post by Dulci »

@felipe yes, its very odd. I'm using qemu and virtual box.

@Hobbes, so if I'm understanding that correctly, interrupts may mess up DX, and CF may be set for no apparent reason?
Post Reply