📄 fat.c
字号:
//###########################################################
// File: fat.c
//
// For FAT12, FAT16 and FAT32
// FAT32 not tested yet
// Only for first Partition
// Only for drives with 512 bytes per sector (the most)
//
// Based on a White Paper from MS
// FAT: General Overview of On-Disk Format
// Version 1.03, December 6, 2000
//
// MBR MasterBootRecord
// PC intern 4
// M.Tischer
// Data Becker
//
// Organisation of a FAT formatted drive:
// ======================================
// MBR including Partitiontable's
// Hidden Sectors
//
// First Partition
// Bootsector 1
// Reserved Sectors
// FAT1
// FAT2
// ...
// FATn
// RootDirectory (not used if FAT32)
// Data Region
//
// Second Partition
// Bootsector 2
// Reserved Sectors
// .....
//
//#########################################################################
// Last change: 09.05.2004
//#########################################################################
// holger.klabunde@t-online.de
// http://home.t-online.de/home/holger.klabunde/homepage.htm
//#########################################################################
// Compiler: AVR-GCC 3.2
//#########################################################################
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dos.h"
unsigned char dirbuf[BYTE_PER_SEC]; //buffer for directory sectors
unsigned char iob[BYTE_PER_SEC]; //file i/o buffer
#ifdef USE_FATBUFFER
unsigned char fatbuf[BYTE_PER_SEC]; //buffer for FAT sectors
#endif
unsigned long FirstDataSector;
unsigned long FirstRootSector;
unsigned long FATFirstSector;
unsigned char FATtype;
#ifdef USE_FAT32
unsigned long FAT32RootCluster;
#endif
#ifdef USE_FATBUFFER
unsigned long FATCurrentSector;
#ifdef DOS_WRITE
unsigned char FATStatus; // only for FAT write buffering
#endif
#endif
unsigned char secPerCluster;
#ifdef USE_FAT32
unsigned long FirstDirCluster;
#else
unsigned int FirstDirCluster;
#endif
unsigned long RootDirSectors;
//use some global variables for file access
#ifdef USE_FAT32
unsigned long FileFirstCluster; //first cluster of file
unsigned long FileCurrentCluster; //actual cluster in use
#else
unsigned int FileFirstCluster; //first cluster of file
unsigned int FileCurrentCluster; //actual cluster in use
#endif
unsigned long FileCurrentSector; //sector with last data read/written
unsigned long File1stClusterSector; //1st sector of cluster in use
unsigned long FileClusterCount; //clusters read (not really)
unsigned long FileBytePerCluster; //bytes per cluster
unsigned long FileDirSector; //sector with dir entry of file
unsigned char FileDirOffset; //offset to file entry in FileDirSector
unsigned long FileSize;
unsigned long FilePosition; //actual position when reading file
unsigned char FileFlag; //read or write
unsigned char FileAttr; //file attribute
unsigned char FileName[8];
unsigned char FileExt[3];
#ifdef USE_FAT32
unsigned long endofclusterchain; //value for END_OF_CLUSTERCHAIN
unsigned long maxcluster; // last usable cluster+1
#else
unsigned int endofclusterchain; //value for END_OF_CLUSTERCHAIN
unsigned int maxcluster; // last usable cluster+1
#endif
unsigned long maxsect; // last sector on drive
#ifdef USE_FATBUFFER
//############################################################
void UpdateFATBuffer(unsigned long newsector)
//############################################################
{
if(newsector!=FATCurrentSector) // do we need to update the FAT buffer ?
{
#ifdef DOS_WRITE
if(FATStatus>0)
{
WriteSector(FATCurrentSector,fatbuf); // write the old FAT buffer
FATStatus=0; // flag FAT buffer is save
}
#endif
ReadSector(newsector,fatbuf); //read FAT sector
FATCurrentSector=newsector;
}
}
#endif
//############################################################
// get next clusternumber of fat cluster chain
#ifdef USE_FAT32
unsigned long GetNextClusterNumber(unsigned long cluster)
#else
unsigned int GetNextClusterNumber(unsigned int cluster)
#endif
//############################################################
{
#ifdef USE_FAT12
unsigned int tmp, secoffset;
unsigned long fatoffset;
#endif
union Convert *cv;
if(cluster<maxcluster) //we need to check this ;-)
{
#ifdef USE_FAT12
if(FATtype==FAT12)
{
//FAT12 has 1.5 Bytes per FAT entry
fatoffset= (cluster * 3) >>1 ; //multiply by 1.5 (rounds down)
secoffset = fatoffset % BYTE_PER_SEC; //we need this for later
fatoffset= fatoffset / BYTE_PER_SEC; //sector offset from FATFirstSector
#ifdef USE_FATBUFFER
UpdateFATBuffer(FATFirstSector + fatoffset);
if(secoffset == (BYTE_PER_SEC-1)) //if this is the case, cluster number is
//on a sector boundary. read the next sector too
{
tmp=(unsigned int)fatbuf[BYTE_PER_SEC-1]; //keep first part of cluster number
UpdateFATBuffer(FATFirstSector + fatoffset +1 ); //read next FAT sector
tmp+=(unsigned int)fatbuf[0] << 8; //second part of cluster number
}
else
{
cv=(union Convert *)&fatbuf[secoffset];
tmp=cv->ui;
}
#else //#ifdef USE_FATBUFFER
ReadSector(FATFirstSector + fatoffset,dirbuf); //read FAT sector
if(secoffset == (BYTE_PER_SEC-1)) //if this is the case, cluster number is
//on a sector boundary. read the next sector too
{
tmp=(unsigned int)dirbuf[BYTE_PER_SEC-1]; //keep first part of cluster number
ReadSector(FATFirstSector + fatoffset +1,dirbuf ); //read next FAT sector
tmp+=(unsigned int)dirbuf[0] << 8; //second part of cluster number
}
else
{
cv=(union Convert *)&dirbuf[secoffset];
tmp=cv->ui;
}
#endif //#ifdef USE_FATBUFFER
if(cluster & 0x01) tmp>>=4; //shift to right position
else tmp&=0xFFF; //delete high nibble
return (tmp);
}//if(FATtype==FAT12)
#endif //#ifdef USE_FAT12
#ifdef USE_FAT16
if(FATtype==FAT16)
{
//two bytes per FAT entry
#ifdef USE_FATBUFFER
UpdateFATBuffer(FATFirstSector + (cluster * 2) / BYTE_PER_SEC);
cv=(union Convert *)&fatbuf[(cluster * 2) % BYTE_PER_SEC];
#else //#ifdef USE_FATBUFFER
ReadSector(FATFirstSector + (cluster * 2) / BYTE_PER_SEC, dirbuf);
cv=(union Convert *)&dirbuf[(cluster * 2) % BYTE_PER_SEC];
#endif //#ifdef USE_FATBUFFER
return(cv->ui);
}//if(FATtype==FAT16)
#endif //#ifdef USE_FAT16
#ifdef USE_FAT32
if(FATtype==FAT32)
{
//four bytes per FAT entry
#ifdef USE_FATBUFFER
UpdateFATBuffer(FATFirstSector + (cluster * 4) / BYTE_PER_SEC);
cv=(union Convert *)&fatbuf[(cluster * 4) % BYTE_PER_SEC];
#else //#ifdef USE_FATBUFFER
ReadSector(FATFirstSector + (cluster * 4) / BYTE_PER_SEC, dirbuf);
cv=(union Convert *)&dirbuf[(cluster * 4) % BYTE_PER_SEC];
#endif //#ifdef USE_FATBUFFER
return( cv->ul & 0x0FFFFFFF );
}//if(FATtype==FAT32)
#endif //#ifdef USE_FAT32
}
// return 0; //this means free cluster. don't do it
return DISK_FULL; //return impossible cluster number
}
//###########################################################
//first datacluster is cluster 2 !
#ifdef USE_FAT32
unsigned long GetFirstSectorOfCluster(unsigned long n)
#else
unsigned long GetFirstSectorOfCluster(unsigned int n)
#endif
//###########################################################
{
return (((n - 2) * secPerCluster) + FirstDataSector);
}
#ifdef DOS_WRITE
//###########################################################
#ifdef USE_FAT32
unsigned long AllocCluster(unsigned long currentcluster)
#else
unsigned int AllocCluster(unsigned int currentcluster)
#endif
//###########################################################
{
#ifdef USE_FAT32
unsigned long cluster;
#else
unsigned int cluster;
#endif
// do this if you want to search from beginning of FAT
// cluster=FindFreeCluster(0); //get next free cluster number
cluster=FindFreeCluster(currentcluster); // get next free cluster number
if(cluster!=DISK_FULL && cluster<=maxcluster) // disk full ?
{
// insert new cluster number into chain
// currentcluster=0 means: this is a new cluster chain
if(currentcluster>0) WriteClusterNumber(currentcluster,cluster);
// mark end of cluster chain
#ifdef USE_FAT12
if(FATtype==FAT12) WriteClusterNumber(cluster,0xFFF);
#endif
#ifdef USE_FAT16
if(FATtype==FAT16) WriteClusterNumber(cluster,0xFFFF);
#endif
#ifdef USE_FAT32
if(FATtype==FAT32) WriteClusterNumber(cluster,0x0FFFFFFF);
#endif
}
return cluster;
}
#endif //DOS_WRITE
#ifdef DOS_WRITE
//###########################################################
// go through the FAT to find a free cluster
#ifdef USE_FAT32
unsigned long FindFreeCluster(unsigned long currentcluster)
#else
unsigned int FindFreeCluster(unsigned int currentcluster)
#endif
//###########################################################
{
#ifdef USE_FAT32
unsigned long cluster;
#else
unsigned int cluster;
#endif
cluster=currentcluster+1; // its a good idea to look here first
// maybe we do not need to search the whole FAT
// and can speed up free cluster search
// if you do not want this call FindFreeCluster(0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -