1st sec of cluster 2 is calculated as follows;

rootdirsecs = ((BPB_RootEntCnt*32)+(BPB_BytesPerSec-1))/BPB_bps;
if(fatsz16 !=0) fatsz = fatsz16;-# secs occupied by FAT
if(fatsz32 !=0) fatsz = fatsz32;

fds = reservedsec + (numFATS*fatsz) + rootdirsecs;

fsoc = ((N-2)*spc)+fds;

num_datasecs = tot_sec - (res + (numfat*fatsz) + rootdirsecs);

num_clusters = num_datasecs / secperclus;

if(num_clusters < 4085) fat12;
if(num_clusters < 65525) fat16;
else fat32;

max_clust_num = num_clusters + 1;

num_clusters  including two reserved clusters = num_clusters + 2;


given valid clust_no  N, where in FAT is entry: 
if (fat16) fatoff = N*2;
else fatoff = N*4;

fat_sec_containing_entry_for_N = res + (fatoff/bps);
fat_sec_off_containing_entry_for_N = REM(fatoff/bps);


now, read this sec into mem secBuff;
if(fat16) *((WORD*)&secBuff[fat_sec_off_containing_entry_for_N];
if(fat32) *((DWORD*)&secBuff[fat_sec_off_containing_entry_for_N];


to set the contents of the same cluster,

If(FATType == FAT16)
*((WORD *) &SecBuff[ThisFATEntOffset]) = FAT16ClusEntryVal;
Else {
FAT32ClusEntryVal = FAT32ClusEntryVal & 0x0FFFFFFF;
*((DWORD *) &SecBuff[ThisFATEntOffset]) =
(*((DWORD *) &SecBuff[ThisFATEntOffset])) & 0xF0000000;
*((DWORD *) &SecBuff[ThisFATEntOffset]) =
(*((DWORD *) &SecBuff[ThisFATEntOffset])) | FAT32ClusEntryVal;
}


to check the EOC mark in the FAT for a file:
(assume FATContent is the contents of the cluster entry in the FAT being checked to see whether it is an EOC mark):

IsEOF = FALSE;
If(FATType == FAT12) {
If(FATContent >= 0x0FF8)
IsEOF = TRUE;
} else if(FATType == FAT16) {
If(FATContent >= 0xFFF8)
IsEOF = TRUE;
} else if (FATType == FAT32) {
If(FATContent >= 0x0FFFFFF8)
IsEOF = TRUE;
}

Microsoft operating system FAT drivers use the
EOC value 0x0FFF for FAT12, 0xFFFF for FAT16, and 0x0FFFFFFF for FAT32 when they set the
contents of a cluster to the EOC mark.

There is also a special BAD CLUSTER mark. Any cluster that contains the BAD CLUSTER
value in its FAT entry is a cluster that should not be placed on the free list because it is prone to disk
errors. The BAD CLUSTER value is 0x0FF7 for FAT12, 0xFFF7 for FAT16, and 0x0FFFFFF7 for
FAT32.



EOC = 0x0FFFFFFF;
BAD CLUSTER = 0x0FFFFFF7
FREE CLUSTER = 0x0

all renamed dirs or files have both long & short dir entries 0xe5
both short & long names have both short & long dir entry.


kernel support for files:
	
kernel has a file table that keeps track of all opened files in the system.
fat table that contains a copy of file dir entries of files most recently accesed.

struct process{
...
...
struct  file *fd[OPEN_MAX]; //fds opened by the process.
...
...
}

on open sys_call, the kernel will place the pointer to the file table in fdt & returns its index which is fd.


