Page 1 of 1

FAT32: get next cluster number.

Posted: Thu Jun 10, 2010 10:55 pm
by lemonyii
hi
i've been working on FAT32 usb boot program so far. But now i found that my function of getting next cluster num does not work.

Code: Select all

;================================================	
getfat:		;return eax = fat[eax]
	pushad
	xor		ebx,ebx
	mov		bx,[BPB_BytsPerSec]
	shr		bx,2		;bx/4,every entry 4 bytes
	div		ebx             
	mov		bx,[BPB_RsvdSecCnt]
	add		eax,ebx     ;eax = sector
	mov		cx,1
	mov		edi,tmpfat
	call	readsectors        ;read cx sectors start at eax to edi
	shl		edx,2        ;edx*4 = offset
	mov		eax,[edx+tmpfat]
	xor		edx,edx
	mov		dx,sp
	mov		[edx+28],eax  ;store it in the eax in stack
	popad
	ret
i wrote it according to FAT: General Overview of On-Disk Form, Version 1.02, May 5, 1999, Microsoft Corporation

Code: Select all

ThisFATSecNum = BPB_ResvdSecCnt + (FATOffset / BPB_BytsPerSec);
ThisFATEntOffset = REM(FATOffset / BPB_BytsPerSec);
notice it is under real mode not protected mode.
i tested it with formated disk and i wrote a program in C++ and it got the right answer in the same method.
tmpfat in the code is 0x90000
eax=3 should output eax=4 but it 0 here.
i test it on real machine(because i hope to boot with a USB disk) so i can't figure out if it is always 0.
could anyone help me figure out where is the problem?
thx

Re: FAT32: get next cluster number.

Posted: Fri Jun 11, 2010 2:01 am
by Combuster
real mode
mov eax,[edx+tmpfat]
Segment limit error

Re: FAT32: get next cluster number.

Posted: Fri Jun 11, 2010 3:13 am
by qw
Did you clear the 4 high bits in EAX before calling this function? In FAT32 only the low 28 bits are significant.

Re: FAT32: get next cluster number.

Posted: Fri Jun 11, 2010 9:22 pm
by lemonyii
Segment limit error
yeah, that's the point. I misunderstood the 32bit prefix to be the same as in protected mode.
and 28bits, absolutely.
thank you.