⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat.c

📁 一个U盘的文件系统源代码!!!C语言编写
💻 C
字号:
/*******************************Copyright (c)***************************************

                              桑海为实业发展有限公司
                         自  动  化  钢  琴  研  究  所
                           http://www.pianoshw.com
                           E-mail:shw@pianoshw.com

----------------------------------文件信息-------------------------------------------

文  件  名 : FAT.C
创  建  人 : 吴应斌
创 建 日 期: 2004-6-7 9:44
功 能 描 述: SL811文件系统函数集合

************************************************************************************/
#include <COMMON.H>
#include <FAT.H>
#include <SL811.H>
#include <TPBULK.H>
#include <HAL.H>

extern XXGFLAGS bdata bXXGFlags;
extern SYS_INFO_BLOCK xdata DeviceInfo;
extern FILE_INFO xdata ThisFile;
extern unsigned char xdata DBUF[BUFFER_LENGTH];
unsigned char xdata FATBUF[512];
unsigned char xdata CurFatSector[512];

FREE_FAT_INFO xdata FreeFat;

unsigned long FirstSectorofCluster(unsigned int clusterNum)
{
	unsigned long temp;
	temp=clusterNum-2;
	temp=temp*DeviceInfo.BPB_SecPerClus;
	temp=temp+DeviceInfo.FirstDataSector;
	return temp;
}

unsigned int ThisFatSecNum(unsigned int clusterNum)
{
   unsigned int temp;
   temp=clusterNum*2;
   temp=temp/DeviceInfo.BPB_BytesPerSec;
   temp=temp+DeviceInfo.FatStartSector;
   return temp;
}

unsigned int ThisFatEntOffset(unsigned int clusterNum)
{
	unsigned int temp1,temp2;
	temp1=2*clusterNum;
	temp2=temp1/DeviceInfo.BPB_BytesPerSec;
	temp1=temp1-temp2*DeviceInfo.BPB_BytesPerSec;
	return temp1;
}

unsigned int GetNextClusterNum(unsigned int clusterNum)
{
	unsigned int xxgFatSecNum,xxgFatEntOffset;
	
	xxgFatSecNum=ThisFatSecNum(clusterNum);
	xxgFatEntOffset=ThisFatEntOffset(clusterNum);
	
	if(ThisFile.FatSectorPointer!=xxgFatSecNum)
	{	
		
		if(!RBC_Read(xxgFatSecNum,1,FATBUF))
			return 0xFFFF;
		ThisFile.FatSectorPointer=xxgFatSecNum;
	}
	
	clusterNum=FATBUF[xxgFatEntOffset+1];
	clusterNum=clusterNum<<8;
	clusterNum+=FATBUF[xxgFatEntOffset];	
	return clusterNum;
}

unsigned char DeleteClusterLink(unsigned int clusterNum)
{
	unsigned int xxgFatSecNum,xxgFatEntOffset;
	
	while((clusterNum>1)&&(clusterNum<0xFFF0))
	{
		xxgFatSecNum=ThisFatSecNum(clusterNum);
		xxgFatEntOffset=ThisFatEntOffset(clusterNum);
		
		if(RBC_Read(xxgFatSecNum,1,DBUF))
		{
			clusterNum=DBUF[xxgFatEntOffset+1];
			clusterNum=clusterNum<<8;
			clusterNum+=DBUF[xxgFatEntOffset];	
		}
		else
			return FALSE;
			
		DBUF[xxgFatEntOffset]=0x00;
		DBUF[xxgFatEntOffset+1]=0x00;	
		
		if(!RBC_Write(xxgFatSecNum,1,DBUF))
			return FALSE;
			
		if(!RBC_Write(xxgFatSecNum+DeviceInfo.BPB_FATSz16,1,DBUF))
			return FALSE;
	}
	
	return TRUE;
}

unsigned char GoToPointer(unsigned long pointer)
{
	unsigned int clusterSize;
	
	clusterSize=DeviceInfo.BPB_SecPerClus*DeviceInfo.BPB_BytesPerSec;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	while(pointer>clusterSize)
	{
		pointer-=clusterSize;	
		ThisFile.ClusterPointer=GetNextClusterNum(ThisFile.ClusterPointer);
		
		if(ThisFile.ClusterPointer==0xFFFF)
			return FALSE;
	}
	ThisFile.SectorofCluster=pointer/DeviceInfo.BPB_BytesPerSec;
	ThisFile.SectorPointer=FirstSectorofCluster(ThisFile.ClusterPointer)+ThisFile.SectorofCluster;
	ThisFile.OffsetofSector=pointer-ThisFile.SectorofCluster*DeviceInfo.BPB_BytesPerSec;
	ThisFile.FatSectorPointer=0;
	
	return TRUE;
}

unsigned int GetFreeCusterNum(void)
{
	unsigned int clusterNum,i;
	unsigned long sectorNum;
	
	clusterNum=0;
	sectorNum=DeviceInfo.FatStartSector;
	while(sectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
	{
		
		if(!RBC_Read(sectorNum,1,DBUF))
			return 0x0;
		for(i=0;i<DeviceInfo.BPB_BytesPerSec;i=i+2)
		{
		  	 if((DBUF[i]==0)&&(DBUF[i+1]==0))
		  	 {	
		  	 	DBUF[i]=0xFF;
		  	 	DBUF[i+1]=0xFF;
		  	 	
		  	 	if(!RBC_Write(sectorNum,1,DBUF))
		  	 		return 0x00;
		  	 		
		  	 	if(!RBC_Write(sectorNum+DeviceInfo.BPB_FATSz16,1,DBUF))
		  	 		return 0x00;
		  	 	
		  	 	return	clusterNum; 
		  	 }
		  	 clusterNum++;
		 }	
				
		sectorNum=2*clusterNum/DeviceInfo.BPB_BytesPerSec+DeviceInfo.FatStartSector;	
	}
	return 0x00;
}

unsigned int CreateClusterLink(unsigned int currentCluster)
{
	unsigned char bFound;
	unsigned int clusterNum;
	unsigned int xxgFatSecNum,xxgFatEntOffset;
	unsigned long temp;
	bFound=0;
	
	//第一次读FAT
	if((FreeFat.SectorNum==DeviceInfo.FatStartSector)&&(FreeFat.OffsetofSector<3))
	{	
		if(!RBC_Read(FreeFat.SectorNum,1,CurFatSector))
			return 0x00;	
	}
	temp=FreeFat.SectorNum-DeviceInfo.FatStartSector;
	temp=temp*DeviceInfo.BPB_BytesPerSec;
	temp=temp/2;
	clusterNum=temp+FreeFat.OffsetofSector/2;
		
	while(FreeFat.SectorNum<DeviceInfo.BPB_FATSz16+DeviceInfo.FatStartSector)
	{
		
		while(FreeFat.OffsetofSector<DeviceInfo.BPB_BytesPerSec)
		{
		  	 if((CurFatSector[FreeFat.OffsetofSector]==0)&&(CurFatSector[FreeFat.OffsetofSector+1]==0))
		  	 {	
		  	 	CurFatSector[FreeFat.OffsetofSector]=0xFF;
		  	 	CurFatSector[FreeFat.OffsetofSector+1]=0xFF;
		  	 	
		  	 	FreeFat.OffsetofSector=FreeFat.OffsetofSector+2;
		  	 	bXXGFlags.bits.bFatChanged=1;
		  	 	bFound=1;
		  	 	break;
		  	 }
		  	 FreeFat.OffsetofSector=FreeFat.OffsetofSector+2;
		  	 clusterNum++;
		}	
		if(bFound==1)
				break;	
				
		UpdateFat(FreeFat.SectorNum);
		
		FreeFat.SectorNum++;
		FreeFat.OffsetofSector=0;
		
		if(!RBC_Read(FreeFat.SectorNum,1,CurFatSector))
			return 0x00;	
	}

	if(bFound==0)
		return 0x00;
	
	xxgFatSecNum=ThisFatSecNum(currentCluster);
	xxgFatEntOffset=ThisFatEntOffset(currentCluster);
	
	if(xxgFatSecNum!=FreeFat.SectorNum)
	{
		RBC_Read(xxgFatSecNum,1,DBUF);
	
		DBUF[xxgFatEntOffset]=clusterNum;
		DBUF[xxgFatEntOffset+1]=clusterNum>>8;

		if(!RBC_Write(xxgFatSecNum,1,DBUF))
			return 0x00;
			
		if(!RBC_Write(xxgFatSecNum+DeviceInfo.BPB_FATSz16,1,DBUF))
			return 0x00;
	 }
	 else
	 {
	   	CurFatSector[xxgFatEntOffset]=clusterNum;
		CurFatSector[xxgFatEntOffset+1]=clusterNum>>8;
		
		bXXGFlags.bits.bFatChanged=1;
	}	
	return clusterNum;
}

void UpdateFat(unsigned long sectorNum)
{
	if(bXXGFlags.bits.bFatChanged==1)
	{
		if(!RBC_Write(sectorNum,1,CurFatSector))
			return ;
			
		if(!RBC_Write(sectorNum+DeviceInfo.BPB_FATSz16,1,CurFatSector))
				return ;
		
		bXXGFlags.bits.bFatChanged=0;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -