Page 1 of 1

Help with FAT32 stage1 bootloader

Posted: Sun Oct 11, 2009 11:54 am
by djsilence
Hi, guys. I'm writing my own FAT32 bootloader.

If anybody can help, please do it, so:

that is my stage1 code:

Code: Select all

segment	.text

		org	0x7c00	

Entry:		jmp	short real_start
		nop

%define bsBytesPerSec	bp+0x0b
%define bsSecPerClust	bp+0x0d
%define bsResSectors	bp+0x0e
%define bsFATs		bp+0x10
%define sectPerTrack	bp+0x18	
%define xsectPerFat	bp+0x24	
%define xrootClst	bp+0x2c	
%define drive		bp+0x40	
%define data_start	bp+0x4c		
					

		times	0x5a-$+$$ db 0

real_start:	cld
		cli
		sub	ax, ax
		mov	ds, ax
		mov	bp, 0x7c00

		mov	ax, 0x1FE0
		mov	es, ax
		mov	si, bp
		mov	di, bp
		mov	cx, 0x0100
		rep	movsw		
		jmp	word 0x1FE0:cont

cont:		mov	ds, ax
		mov	ss, ax		
                lea     sp, [bp-0x20]
		sti
		mov	[drive], dl	

		mov	si, msg_1
		call	print

		mov 	ah, 0x41
		mov 	bx, 0x55AA
		mov 	dl, 0x80
		int 	0x13
		jc 	short .err

		mov 	eax, [xsectPerFat]
		imul 	dword [bsFATs]
		add	ax, word [bsResSectors]
		add	ax, word 0x003F
		mov	[data_start], eax

		mov 	eax, [data_start]
		mov	bx, 0x7E00
		call 	readDisk

		mov	ax, 0x7E00

	.nent:
		mov	cx, 11
		mov 	si, filename
		mov 	di, ax
		repe	cmpsb
		jz	.done

		add	ax, 0x20
		jmp 	.nent
	
	.done:
		add	ax, 0x1A
		mov	bx, word [eax]
		xor	eax, eax
		mov 	ax, bx

		mov	[cur_cl], ax
		call 	read_file

		push	dword 0x00000500
		retf

		cli
		hlt

	.err:
		mov 	si, msg_2
		call 	print
		cli
		hlt

;eax - cluster number (in)
;eax - sector number (out)
convert_cl:
		cmp	eax, 0x0ffffff8
		jnb	end_of_chain

		dec	eax
		dec	eax
		imul 	byte [bsSecPerClust]
		add	eax, [data_start]
		ret

 end_of_chain:	stc
		ret

cur_cl dd 0
next_cl dd 0
read_file:
		; load FAT sector (just one)
		mov	bx, 0x300
		xor	eax, eax
		mov	ax, [bsResSectors]
		add	ax, 0x3F
		call	readDisk

		mov	bx, 0x500
	.read:	
		mov	eax, [cur_cl]
		call 	convert_cl
		jc	.succ
		call	readCluster
	
		mov	eax, dword 4
		imul	dword [cur_cl]
		add	eax, 0x300
		xor	edx, edx
		mov	edx, [eax]
		mov 	[cur_cl], edx
		
		call 	.read	

	.succ:
		ret
		
;eax - sector number	
;bx - address
readCluster:
		mov 	cl, byte [bsSecPerClust]

	.read:		
		call	readDisk
		dec	cl
		cmp 	cl, 0
		jne	.read

		ret		

readDisk:	push	dx
		push	si
		push	di

read_next:	push	eax	
		mov	di, sp	

		push	byte 0
				
		push	byte 0	
		push	eax	
		push	es	
		push	bx	
		push	byte 1	
		push	byte 16	
		mov	si, sp
		mov	dl, [drive]
		mov	ah, 42h	
		int	0x13	

		mov	sp, di	
				
		pop	eax	

		jnc	read_ok		

		push	ax		
		xor	ah, ah		
		int	0x13
		pop	ax
		jmp	read_next

read_ok:	inc 	eax
		add	bx, word [bsBytesPerSec]
		jnc	no_incr_es

		mov	dx, es
		add	dh, 0x10	
		mov	es, dx

no_incr_es:	pop	di
		pop 	si
		pop	dx
		ret

printchar:	xor	bx, bx		
		mov	ah, 0x0e	
		int	0x10		
print:		lodsb			
		cmp	al, 0		
		jne	printchar	
		ret		

msg_1 db "SYOS is starting...",0

msg_2	db "Error while loading stage 2 bootloader", 0x0D, 0x0A

       times 0x01F1-$+$$ db 0

filename	db "KRNLDR  SYS"

sign		dw 0, 0xAA55
Saying the truth it loads everything great (when I do the next mov si, 0x500 call print), it displays contentof that address space and that is correct.... but after jump nothing happens...

My second stage for this moment looks like this:

Code: Select all

org 0x500
bits 16

jmp entry

msg_1 db "Stage 2 started...", 0x0d, 0x0a, 0x00

entry:
        cli
        xor ax, ax
        mov ds, ax
        mov es, ax
        mov fs, ax
        mov gs, ax
        mov ss, ax
        mov sp, 0xFFFF
        sti

        mov si, msg_1
        call print

        cli
        hlt

printchar:	xor	bx, bx		
		mov	ah, 0x0e	
		int	0x10		
print:		lodsb			
		cmp	al, 0		
		jne	printchar	
		ret

I get nothing displayed except stage 1 messages... what wrong is here?

Thanks great, Daniel.

Re: Help with FAT32 stage1 bootloader

Posted: Tue Oct 13, 2009 1:50 am
by hade12345
try

Code: Select all

push word 0x500
retf

Re: Help with FAT32 stage1 bootloader

Posted: Tue Oct 13, 2009 1:46 pm
by Combuster
why are you suggesting guaranteed broken code? :shock:

Re: Help with FAT32 stage1 bootloader

Posted: Tue Oct 13, 2009 1:52 pm
by f2
godoflaziness wrote:try

Code: Select all

push word 0x500
retf
:shock: Where is the segment?

Try this:

Code: Select all

     jmp    0x0000:0x0500

Re: Help with FAT32 stage1 bootloader

Posted: Wed Oct 14, 2009 6:44 am
by djsilence
Still get same problem... See, i've tried to do:

push dword 0x00000500
retf

and

push word 0x0050
push word 0x0000
retf

and

push word 0x0000
push word 0x0500
retf

and

jmp 0x500

and

jmp 0x0000:0x0500

and

jmp 0x0050:0x0000

everything is the same...

Re: Help with FAT32 stage1 bootloader

Posted: Wed Oct 14, 2009 6:46 am
by djsilence
Combuster, if the code isn't broken then what?? I understand that loading my file is right, but why cannot I jump there?? and what is different in my bootloader from others? (i mean I just load code and jump to it)...

Re: Help with FAT32 stage1 bootloader

Posted: Wed Oct 14, 2009 7:00 am
by f2
Sectors are loaded by the BIOS at address ES:BX. I see you haveve set ES = 0x1FE0 at the begining
of your code. So, the BIOS loads the first sector at 0x1FE0:0x0500. I think ES must be 0.

Re: Help with FAT32 stage1 bootloader

Posted: Wed Oct 14, 2009 7:20 am
by djsilence
I'm very glad of getting something other from previous result!!!

I made my es to 0 write after selfcopying bootcode.

But at this moment my bochs write such exception: No bootable device. This exceptions is caused just before jmp instruction! (if i put cli, hlt before jmp - no exception)... What does such error mean???

If I do not change es, but jump to 0x1FE0:0x0500 (and make org of stage 2 equal to 0x1FE0:0x0500 and set segment registers to 0x1FE0) then I get nothing printed from stage 2 (like was before...)... ((