devkitPro / libfat

FAT library for GBA, DS, Gamecube & Wii
http://devkitpro.org/viewforum.php?f=24
54 stars 36 forks source link

Wrong root sectors count determined for FAT12 and FAT16? #2

Open FrankHB opened 9 years ago

FrankHB commented 9 years ago

From Microsoft FAT Specification Section 3.5:

  1. First, determine the count of sectors occupied by the root directory:
RootDirSectors = ((BPB_RootEntCnt * 32) + (BPB_BytsPerSec – 1)) / BPB_BytsPerSec

Note that on a FAT32 volume, the BPB_RootEntCnt value is always 0. Therefore, on a FAT32 volume, RootDirSectors is always 0.

In source/partition.c, line 250:

    partition->dataStart = partition->rootDirStart +
        (( u8array_to_u16(sectorBuffer, BPB_rootEntries) * DIR_ENTRY_DATA_SIZE) / partition->bytesPerSector);

It seems that the correct code would be:

    partition->dataStart = partition->rootDirStart +
        (( u8array_to_u16(sectorBuffer, BPB_rootEntries) * DIR_ENTRY_DATA_SIZE + (partition->bytesPerSector - 1)) / partition->bytesPerSector);

Not tested yet, not sure.