?? 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: 10.05.2003
//#########################################################################
// 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 "protos.h"
#include "dos.h"
#include "mydefs.h"
#include "serial.h"
unsigned long FirstDataSector;
unsigned long FirstRootSector;
unsigned long FirstFatSector;
unsigned long FATSz;
unsigned long FAT32RootCluster;
unsigned char FATtype;
unsigned char secPerCluster;
unsigned long CountofClusters;
unsigned long CurrentDirCluster;
unsigned long RootDirSectors;
unsigned int RootEntrys;
//use some global variables for faster file access
unsigned long FileFirstCluster; //first cluster of file
unsigned long FileCurrentSector; //sector with last data read/written
unsigned long FileCurrentCluster; //actual cluster in use
unsigned long File1stClusterSector; //1st sector of cluster in use
unsigned long FileClusterCount; //clusters read
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 FileName[8];
unsigned char FileExt[3];
unsigned long CurrentSector;
unsigned long CurrentCluster;
unsigned long endofclusterchain; //value for END_OF_CLUSTERCHAIN
unsigned char lo; //show long name entrys on=1;
unsigned long maxcluster;
//###########################################################
unsigned long AllocCluster(unsigned long currentcluster)
//###########################################################
{
unsigned long cluster;
// 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
if(FATtype==FAT12) WriteClusterNumber(cluster,0xFFF);
if(FATtype==FAT16) WriteClusterNumber(cluster,0xFFFF);
if(FATtype==FAT32) WriteClusterNumber(cluster,0x0FFFFFFF);
}
return cluster;
}
//###########################################################
// go through the FAT to find a free cluster
unsigned long FindFreeCluster(unsigned long currentcluster)
//###########################################################
{
unsigned long cluster;
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)
// search til end of FAT
while(cluster<maxcluster)
{
if(GetNextClusterNumber(cluster)==0) break;
cluster++;
}
// if we have not found a free cluster til end of FAT
// lets start a new search at beginning of FAT
if(cluster>=maxcluster)
{
cluster=2; // first possible free cluster
while(cluster<=currentcluster) // search til we come to where we have started
{
if(GetNextClusterNumber(cluster)==0) break;
cluster++;
}
if(cluster>=currentcluster) return DISK_FULL; // no free cluster found
}
if(cluster>=maxcluster) return DISK_FULL;
return cluster;
}
//############################################################
// write a cluster number into fat cluster chain
unsigned char WriteClusterNumber(unsigned long cluster, unsigned long number)
//############################################################
{
unsigned int tmp, secoffset;
unsigned long fatoffset, sector;
unsigned char *p,lo,hi;
if(cluster<maxcluster) //we need to check this ;-)
{
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 FirstFatSector
sector=FirstFatSector + fatoffset;
tmp=(unsigned int)number;
if(cluster & 0x01) tmp<<=4; //shift to right position
lo=(unsigned char)tmp;
hi=(unsigned char)(tmp>>8);
CFReadSector(sector,inbuff); //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
{
p=&inbuff[BYTE_PER_SEC-1]; //keep first part of cluster number
if(cluster & 0x01)
{
*p&=0x0F;
*p|=lo;
}
else *p=lo;
CFWriteSector(sector,inbuff);
CFReadSector(sector+1,inbuff ); //read next FAT sector
p=&inbuff[0]; //second part of cluster number
if(cluster & 0x01) *p=hi;
else
{
*p&=0xF0;
*p|=hi;
}
CFWriteSector(sector+1,inbuff);
}
else
{
p=&inbuff[secoffset];
if(cluster & 0x01)
{
*p&=0x0F;
*p++|=lo;
*p=hi;
}
else
{
*p++=lo;
*p&=0xF0;
*p|=hi;
}
CFWriteSector(sector,inbuff);
}
}//if(FATtype==FAT12)
if(FATtype==FAT16)
{
//two bytes per FAT entry
sector=FirstFatSector + (cluster * 2) / BYTE_PER_SEC;
CFReadSector(sector, inbuff);
p=&inbuff[(cluster * 2) % BYTE_PER_SEC];
*p++=(unsigned char)(number);
*p =(unsigned char)(number >> 8);
CFWriteSector(sector, inbuff);
}// if(FATtype==FAT16)
if(FATtype==FAT32)
{
//four bytes per FAT entry
sector=FirstFatSector + (cluster * 4) / BYTE_PER_SEC;
CFReadSector(sector, inbuff);
p=&inbuff[(cluster * 4) % BYTE_PER_SEC];
number&=0x0FFFFFFF;
*p++=(unsigned char)(number);
*p++=(unsigned char)(number >> 8);
*p++=(unsigned char)(number >> 16);
*p =(unsigned char)(number >> 24);
CFWriteSector(sector, inbuff);
}// if(FATtype==FAT32)
}
return 0;
}
//############################################################
// get next clusternumber of fat cluster chain
unsigned long GetNextClusterNumber(unsigned long cluster)
//############################################################
{
unsigned int tmp, secoffset;
unsigned long fatoffset;
union Convert *cv;
if(cluster<maxcluster) //we need to check this ;-)
{
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 FirstFatSector
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -