FAT32: get next cluster number.

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.
Post Reply
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

FAT32: get next cluster number.

Post 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
Enjoy my life!------A fish with a tattooed retina
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: FAT32: get next cluster number.

Post by Combuster »

real mode
mov eax,[edx+tmpfat]
Segment limit error
"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 ]
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: FAT32: get next cluster number.

Post by qw »

Did you clear the 4 high bits in EAX before calling this function? In FAT32 only the low 28 bits are significant.
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: FAT32: get next cluster number.

Post 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.
Enjoy my life!------A fish with a tattooed retina
Post Reply