Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

Project Request

Return
Naglis Radavicius
Naglis

posted on 2012/02/29 03:21:15 PM CET

Storage

MMC/SD FAT16 FAT free clusters calculate

Hello there,

maybe someone can tell me where is my mistake when I calculate free clusters in FAT table.
I have read tons info about FAT16, but anyway something wrong in my code. I compare my results with results giving WinHex or simple Win. So my code :

.........
ptrInt = &DataBuffer[0x0B];
BytesPerSector = *ptrInt;

SectorsPerCluster = DataBuffer[0x0D];

ptrInt = &DataBuffer[0x0E];
ReservedSectors = *ptrInt;

NumberFats = DataBuffer[0x10];

ptrInt = &DataBuffer[0x16];
SectorsPerFat = *ptrInt;

ptrLong = &DataBuffer[0x20];
NumberSectors = *ptrLong;

ptrInt = &DataBuffer[0x1C];
HidenSectors = *ptrInt;


FirstFatSector = HidenSectors + ReservedSectors;
LastFatSector = FirstFatSector + SectorsperFat;
Root_Dir_Start = ReservedSectors + NumberFats * SectorsPerFat;
First_Data_Sector = Root_Dir_Start + 32;
Usable_Clusters = (NumberSectors - (First_Data_Sector + 1)) / SectorsPerCluster; // Total available clusters
Capacity = BytesPerSector * SectorsPerCluster * Usable_Clusters;

there any problems - formulas works great,
but there I'm stuck :

Sector = FirstFatSector;
freeClusters = 0;

while( Sector < LastFatSector )
{
Mmc_Read_Sector(Sector, DataBuffer);

for(i=0; i<256; i++) // i - FAT entries. 256 - in FAT any entry is 2 bytes long, so 512/2 = 256
{
if((DataBuffer [i*2]== 0x00) && (DataBuffer [(i*2)+ 1]== 0x00))
freeClusters++;
}
Sector++;
}

......

result ==> freeClusters = 61450 - a little to big!

but WinHex show me another value - 61243.

So I'll be deeply thankful