FAT sometimes returns invalid value
Posted: Mon Oct 12, 2009 11:55 am
Hello everyone,
Perhaps my math is wrong but something is wrong in my FAT parsing code in my boot libraries FAT12 code.
Whenever the contents on the disk gets larger, sometimes the value returned from the FAT (in NextCluster) is invalid. That is, it will contain a value that is greater then the maximum sectors on disk thus causing the system to crash when it tries to read it (or return 0 - file loaded is corrupt.)This is starting to become more and more of an issue and has been going on for months. One thing that I have noticed was that, when the value returned was invalid, it was obtaining the value from FAT [511], the last byte of the sector.
Disk reading is done using the Bios int 0x13 function 2, thus there is no chance that a disk driver is at fault. It also works most of the time, but is increasingly starting to appear as the system grows larger thus becoming more and more of a problem.
Does anyone have any suggestions? Has anyone seen this before? I am happy to post more code if needed.
Thank you in advance
Perhaps my math is wrong but something is wrong in my FAT parsing code in my boot libraries FAT12 code.
Whenever the contents on the disk gets larger, sometimes the value returned from the FAT (in NextCluster) is invalid. That is, it will contain a value that is greater then the maximum sectors on disk thus causing the system to crash when it tries to read it (or return 0 - file loaded is corrupt.)
Code: Select all
FsysFatRead (OUT OPT PBYTE Buffer,
IN UINT Length) {
BYTE FAT [SECTOR_SIZE];
UINT FAT_Offset = 0, FAT_Sector = 0;
USHORT NextCluster = 0, EntryOffset = 0;
//! *snip, just tests for valid parms, eof, locating the next sector & reading it
//! find location of fat and next entry
FAT_Offset = _FileInfo.CurrentCluster + ( _FileInfo.CurrentCluster / 2 ); // multiply by 1.5
FAT_Sector = 1 + ( FAT_Offset / SECTOR_SIZE );
EntryOffset = FAT_Offset % SECTOR_SIZE;
//! read the FAT sector
ArchDiskRawRead (_Drive, FAT_Sector, FAT);
//! read entry for next cluster
NextCluster = *( PUSHORT ) &FAT [ EntryOffset ];
//! test if entry is odd or even
if( _FileInfo.CurrentCluster & 0x0001 )
NextCluster >>= 4; //grab high 12 bits
else
NextCluster &= 0x0FFF; //grab low 12 bits
//! Sometimes reads that the next cluster is > maximum sector count?? :/
BlpDisplayString ("\n\rFSysFatRead: Next Cluster=0x%x", NextCluster);
// *snip, test for FAT valid values
_FileInfo.CurrentCluster = NextCluster;
}
Disk reading is done using the Bios int 0x13 function 2, thus there is no chance that a disk driver is at fault. It also works most of the time, but is increasingly starting to appear as the system grows larger thus becoming more and more of a problem.
Does anyone have any suggestions? Has anyone seen this before? I am happy to post more code if needed.
Thank you in advance