FAT file system-Note

来源:互联网 发布:土地建设数据库 编辑:程序博客网 时间:2024/06/05 10:23
A FAT32 FAT entry is actually only a 28-bit entry. The high 4 bits of a FAT32 FAT entry are reserved.

  because the BPB_BytsPerSec value is always divisible by 2 and 4, you never have to worry about a FAT16 or FAT32 FAT entry spanning over a sector boundary
(this is not true of FAT12)

    if (FATType == FAT12)
        FATOffset = N + (N / 2);   
/* Multiply by 1.5 without using floating point, the divide by 2 rounds DOWN */
   
    ThisFATSecNum = BPB_ResvdSecCnt + (FATOffset / BPB_BytsPerSec);
    ThisFATEntOffset = REM(FATOffset / BPB_BytsPerSec);

  If(ThisFATEntOffset == (BPB_BytsPerSec – 1)) {
    /* This cluster access spans a sector boundary in the FAT      */
    /* There are a number of strategies to handling this. The      */
    /* easiest is to always load FAT sectors into memory           */
    /* in pairs if the volume is FAT12 (if you want to load        */
    /* FAT sector N, you also load FAT sector N+1 immediately      */
    /* following it in memory unless sector N is the last FAT      */
    /* sector). It is assumed that this is the strategy used here  */
    /* which makes this if test for a sector boundary span         */
    /* unnecessary.                                                */
}

access the FAT entry as a WORD just as we do for FAT16, but if the cluster number is EVEN,
we only want the low 12-bits of the 16-bits we fetch; and if the cluster number is ODD,
we only want the high 12-bits of the 16-bits we fetch:
Read:
FAT12ClusEntryVal = *((WORD *) &SecBuff[ThisFATEntOffset]);
 If(N & 0x0001)
     FAT12ClusEntryVal = FAT12ClusEntryVal >> 4; /* Cluster number is ODD */
 Else
     FAT12ClusEntryVal = FAT12ClusEntryVal & 0x0FFF; /* Cluster number is EVEN */

Write:
If(N & 0x0001) {
    FAT12ClusEntryVal = FAT12ClusEntryVal << 4; /* Cluster number is ODD */
    *((WORD *) &SecBuff[ThisFATEntOffset]) =
        (*((WORD *) &SecBuff[ThisFATEntOffset])) & 0x000F;
} Else {
    FAT12ClusEntryVal = FAT12ClusEntryVal & 0x0FFF; /* Cluster number is EVEN */
    *((WORD *) &SecBuff[ThisFATEntOffset]) =
        (*((WORD *) &SecBuff[ThisFATEntOffset])) & 0xF000;
}
*((WORD *) &SecBuff[ThisFATEntOffset]) =
    (*((WORD *) &SecBuff[ThisFATEntOffset])) | FAT12ClusEntryVal;

The way the data of a file is associated with the file is as follows. In the directory entry,
the cluster number of the first cluster of the file is recorded.
The first cluster (extent) of the file is the data associated with this first cluster number,
and the location of that data on the volume is computed from the cluster number as described earlier (computation of FirstSectorofCluster)
原创粉丝点击