www.pudn.com > FAT.rar > FAT.c, change:2009-08-25,size:28863b


/*******************************************************************/ 
/*                          FAT 驱动程序                           */ 
                                       
/* 作者              		日期         		注释       */ 
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ 
/* 王伟民		08/10/20			原始文件   */ 
/*******************************************************************/ 
/*说明:1、在第339、367、627、655行,可以修改你要寻找的文件类型,这里我们要找bmp文件,因此设定文件类型为bmp,拷入SD卡的文件后缀为bmp(必须小写)时才能被搜索到*/ 
/*      2、void eeprom_write_byte(BYTE addr,BYTE value)和void eeprom_write_byte(BYTE addr,BYTE value)这两个函数体没有用到、可以删除*/ 
/*******************************************************************/ 
 
#include"FAT/FAT.h" 
 
 
extern BYTE tmpBuf[512]; 
extern BYTE wriBuf[512]; 
//extern struct FileInfoStruct FileInfo;//文件信息 
DWORD FirstDirClust;    //first directory cluster 
DWORD FirstDataSector;	// The first sector number of data 
WORD BytesPerSector;	// Bytes per sector 
DWORD FATsectors;		// The amount sector a FAT occupied 
WORD SectorsPerClust;	// Sector per cluster 
DWORD FirstFATSector;	// The first FAT sector 
DWORD FirstDirSector;	// The first Dir sector 
DWORD RootDirSectors;	// The sector number a Root dir occupied  
DWORD RootDirCount;		// The count of directory in root dir 
DWORD TolSectors; 
DWORD DataSectors; 
DWORD CountofClusters; 
 
BYTE FAT32_Enable; 
WORD buf_index=0; 
WORD sector_count=0; 
 
WORD wribuf_index=0; 
WORD wrisector_count=0; 
DWORD wri_cluster; 
DWORD P; 
//DWORD tempc; 
// 
//DWORD wri_secter; 
//DWORD wri_offset; 
// 
BYTE (* FAT_ReadSector)(DWORD,BYTE *); 
BYTE (* FAT_WriteSector)(DWORD,BYTE *); 
 
//函数指针指向sd卡的读写函数 
//function pointer to the sd card read & write single block 
//wirte sector are not use in this player 
BYTE (* FAT_ReadSector)(DWORD sector, BYTE * buffer)=MMC_SD_ReadSingleBlock;//device read 
BYTE (* FAT_WriteSector)(DWORD sector, BYTE * buffer)=MMC_SD_WriteSingleBlock;//device write 
 
extern struct FileInfoStruct FileInfo;//temporarily buffer for file information 
 
//FAT初始化,不含SD的初始化,用之前应先调用sd的初始化 
BYTE FAT_Init()//Initialize of FAT  need initialize SD first 
{ 
	struct bootsector710 *bs  = 0; 
	struct bpb710        *bpb = 0; 
//	struct partsector    *ps  = 0; 
	struct partrecord    *pr  = 0; 
 
	//BYTE buffer[512]; 
	BYTE * buffer = tmpBuf; 
	DWORD hidsec=0; 
	DWORD Capacity; 
 
	Capacity = MMC_SD_ReadCapacity(); 
	if(Capacity<0xff)return 1; 
 
	if(FAT_ReadSector(0,buffer))return 1; 
	bs = (struct bootsector710 *)buffer; 
 
	pr = (struct partrecord *)((struct partsector *)buffer)->psPart;//first partition 
	hidsec = pr->prStartLBA;//the hidden sectors 
	if(hidsec >= Capacity/512) 
	{ 
		hidsec = 0; 
	} 
	else  
	{ 
		if(FAT_ReadSector(pr->prStartLBA,buffer))return 1;//read the bpb sector 
		bs = (struct bootsector710 *)buffer; 
		if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB) 
		{ 
			hidsec = 0; 
			if(FAT_ReadSector(0,buffer))return 1;//read the bpb sector 
			bs = (struct bootsector710 *)buffer;	 
		} 
	} 
 
	if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB)//对付没有bootsect的sd卡	//dead with the card which has no bootsect 
	{ 
		return 1; 
	} 
	bpb = (struct bpb710 *)bs->bsBPB; 
 
	 
	if(bpb->bpbFATsecs)//detemine thd FAT type  //do not support FAT12 
	{ 
		FAT32_Enable=0;	//FAT16 
		FATsectors		= bpb->bpbFATsecs;//FAT占用的扇区数	//the sectors number occupied by one fat talbe 
		FirstDirClust = 2; 
		TolSectors=bpb->bpbSectors; 
		//Flag_Eof=0xfff8; 
		//Flag_Bad=0xfff7; 
		//Flag_Rev=0xfff0; 
	} 
	else 
	{ 
		FAT32_Enable=1;	//FAT32 
		FATsectors		= bpb->bpbBigFATsecs;//FAT占用的扇区数	//the sectors number occupied by one fat talbe 
		FirstDirClust = bpb->bpbRootClust; 
		TolSectors=bpb->bpbSectors; 
	//	Flag_Eof=0xffffff8; 
		//Flag_Bad=0xffffff7; 
		//Flag_Rev=0xffffff0; 
	}// 
 
	BytesPerSector	= bpb->bpbBytesPerSec;//每扇区字节数 
	SectorsPerClust	= (BYTE)bpb->bpbSecPerClust;//每簇扇区数 
	FirstFATSector	= bpb->bpbResSectors+hidsec;//第一个FAT表扇区 
	RootDirCount	= bpb->bpbRootDirEnts;//根目录项数 
	RootDirSectors	= (RootDirCount*32+(BytesPerSector-1))>>9;//根目录占用的扇区数 
	FirstDirSector	= FirstFATSector+bpb->bpbFATs*FATsectors;//第一个目录扇区 
	FirstDataSector	= FirstDirSector+RootDirSectors;//第一个数据扇区 
	DataSectors=TolSectors-(bpb->bpbResSectors+(bpb->bpbFATs*FATsectors)+RootDirSectors); 
	return 0; 
} 
 
//读一个簇中的一个扇区 
//read one sector of one cluster, parameter part indicate which sector 
BYTE FAT_LoadPartCluster(DWORD cluster,WORD part,BYTE * buffer) 
{ 
	DWORD sector; 
	sector=FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
	if(FAT_ReadSector(sector+part,buffer))return 1; 
	else return 0; 
} 
//write 0ne sector of one cluster 
BYTE FAT_SetPartCluster(DWORD cluster,WORD part,BYTE * buffer) 
{ 
	DWORD sector; 
	sector=FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
	if(FAT_WriteSector(sector+part,buffer))return 1; 
	else return 0; 
} 
 
//读下一簇簇号 
//Return the cluster number of next cluster of file 
//Suitable for system which has limited RAM 
DWORD FAT_NextCluster(DWORD cluster) 
{ 
	//BYTE buffer[512]; 
	BYTE * buffer = wriBuf; 
	DWORD sector; 
	DWORD offset; 
	if(FAT32_Enable)offset = cluster/128; 
	else offset = cluster/256; 
	if(cluster<2)return 0x0ffffff8; 
	sector=FirstFATSector+offset;//calculate the actual sector 
	if(FAT_ReadSector(sector,buffer))return 0x0ffffff8;//read fat table / return 0xfff8 when error occured 
 
	if(FAT32_Enable) 
	{ 
		offset=cluster%128;//find the position 
		sector=(((DWORD *)buffer)[offset])&0X0FFFFFFF;	 
	} 
	else 
	{ 
		offset=cluster%256;//find the position 
		sector=((WORD *)buffer)[offset]; 
	} 
	return (DWORD)sector;//return the cluste number 
} 
 
 
//在给定目录下查找文件 
//Find a item in the directory which specify by the parameter "cluster" 
//Return the start cluster number 
 
BYTE FAT_FindItem(DWORD cluster, BYTE *name, struct FileInfoStruct *FileInfo) 
{ 
	BYTE *buffer; 
	DWORD tempclust; 
	DWORD sector; 
	BYTE cnt; 
	WORD offset; 
	BYTE i; 
	struct direntry *item = 0; 
     
	if((cluster==0) && (FAT32_Enable == 0))// root directory 
	{ 
		buffer=tmpBuf;//apply memory 
		if(buffer==0)return 1;//if failed 
		for(cnt=0;cnt<RootDirSectors;cnt++) 
		{ 
			if(FAT_ReadSector(FirstDirSector+cnt,buffer)){/*free(buffer);*/return 1;} 
			for(offset=0;offset<512;offset+=32) 
			{ 
				item=(struct direntry *)(&buffer[offset]); 
				if((item->deName[0] != '.')&&(item->deName[0] != 0x00) && (item->deName[0] != 0xe5) && (item->deAttributes != 0x0f)) 
				{ 
					for(i=0;i<11;i++) 
					{ 
						if(buffer[offset+i]!=name[i])break; 
					} 
					if(i==11) 
					{ 
						//return the parameter of the item 
						//wri_secter=FirstDirSector+cnt; 
						//wri_offset=offset; 
						FileInfo->StartCluster = item->deStartCluster + (((DWORD)item->deHighClust)<<16);//don't care 
						FileInfo->Size         = item->deFileSize; 
//						FileInfo->Attr         = item->deAttributes; 
						FileInfo->Sector       = FirstDirSector+cnt; 
						FileInfo->Startindex=offset; 
						FileInfo->Offset       = 0; 
						/*free(buffer);*/ 
						return 0; 
					} 
				} 
			} 
		} 
		/*free(buffer);*///release 
	} 
	else//other folders 
	{ 
		tempclust=cluster; 
		while(1) 
		{ 
			sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
			buffer=tmpBuf;//apply memory 
			if(buffer==0)return 1;//if failed 
			for(cnt=0;cnt<SectorsPerClust;cnt++) 
			{ 
				if(FAT_ReadSector(sector+cnt,buffer)){/*free(buffer);*/return 1;} 
				for(offset=0;offset<512;offset+=32) 
				{ 
					item=(struct direntry *)(&buffer[offset]); 
					if((item->deName[0] != '.')&&(item->deName[0] != 0x00) && (item->deName[0] != 0xe5) && (item->deAttributes != 0x0f)) 
					{ 
						for(i=0;i<11;i++) 
						{ 
							if(buffer[offset+i]!=name[i])break; 
						} 
						if(i==11) 
						{	//wri_secter=sector+cnt; 
							//wri_offset=offset; 
							FileInfo->StartCluster = item->deStartCluster + (((DWORD)item->deHighClust)<<16);//don't care 
							FileInfo->Size         = item->deFileSize; 
//							FileInfo->Attr         = item->deAttributes; 
							FileInfo->Sector       = sector+cnt; 
							FileInfo->Startindex=offset; 
							FileInfo->Offset       = 0; 
							/*free(buffer);*/ 
							return 0; 
						} 
					} 
				} 
			} 
			/*free(buffer);*///release 
			tempclust=FAT_NextCluster(tempclust);//next cluster 
			if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust == 0xffff))break; 
		} 
	} 
	return 1; 
} 
 
// find a directory with the given path 
DWORD FAT_OpenDir(BYTE * dir) 
{ 
	BYTE name[11]; 
	BYTE *p=dir; 
	BYTE deep=0; 
	BYTE i,j; 
	DWORD cluster=0; 
	if(FAT32_Enable)cluster = FirstDirClust; 
	if(*p != '\\')  
       return 1;  //invalid path 
	while(*p) 
	{ 
		if(*p == '\\') 
		{ 
			deep++; 
		} 
		p++; 
	} 
	p=dir; 
	for(i=0;i<deep-1;i++) 
	{ 
		p++; 
		for(j=0;j<11;j++)name[j]=0x20; 
		j=0; 
		while(*p != '\\') 
		{ 
			if((*p) >= 'a' && (*p) <= 'z')name[j] = (*p++)-0x20; 
			else name[j] = *p++; 
			j++; 
		} 
		if(FAT_FindItem(cluster,name, &FileInfo))return 1;//find the directory 
		cluster = FileInfo.StartCluster; 
	} 
	p++; 
	for(j=0;j<11;j++)name[j]=0x20; 
	j=0; 
	while(*p) 
	{ 
		if(*p>='a' && *p<='z')name[j]=(*p++)-0x20; 
		else name[j]=*p++; 
		j++; 
	} 
	if(j == 0)return cluster; 
	if((FAT_FindItem(cluster,name, &FileInfo)))return 1;//find the final directory 
	cluster = FileInfo.StartCluster; 
	return cluster; 
} 
 
 
//复制记录项信息  //copy item 
void CopyDirentruyItem(struct direntry *Desti,struct direntry *Source) 
{ 
	BYTE i; 
	/*for(i=0;i<8;i++)Desti->deName[i] = Source->deName[i]; 
	for(i=0;i<3;i++)Desti->deExtension[i] = Source->deExtension[i]; 
	Desti->deAttributes = Source->deAttributes; 
	Desti->deLowerCase = Source->deLowerCase; 
	Desti->deCHundredth = Source->deCHundredth; 
	for(i=0;i<2;i++)Desti->deCTime[i] = Source->deCTime[i]; 
	for(i=0;i<2;i++)Desti->deCDate[i] = Source->deCDate[i]; 
	for(i=0;i<2;i++)Desti->deADate[i] = Source->deADate[i]; 
	Desti->deHighClust = Source->deHighClust; 
	for(i=0;i<2;i++)Desti->deMTime[i] = Source->deMTime[i]; 
	for(i=0;i<2;i++)Desti->deMDate[i] = Source->deMDate[i]; 
	Desti->deStartCluster = Source->deStartCluster; 
	Desti->deFileSize = Source->deFileSize;*/ 
} 
/* 
填写FAT表中的某一项,向其中写入value; 
 
*/ 
BYTE SetFat(DWORD cluster,DWORD value) 
{ 
	BYTE * buffer = wriBuf; 
	DWORD sector; 
	DWORD offset; 
	sector=cluster; 
	offset=value; 
	if(FAT32_Enable)offset = cluster/128; 
	else offset = cluster/256; 
	if(cluster<2)return 1; 
	sector=FirstFATSector+offset;//calculate the actual sector 
	if(FAT_ReadSector(sector,buffer))return 1;//read fat table / return 0xfff8 when error occured 
 
	if(FAT32_Enable) 
	{ 
		offset=cluster%128;//find the position 
			 
		//*((DWORD *)(&buffer[offset]))=(*((DWORD *)(&buffer[offset])))&0XF0000000; 
			 
        //*((DWORD *)(&buffer[offset]))=(*((DWORD *)(&buffer[offset])))|(value); 
		((DWORD *)buffer)[offset]=((DWORD *)buffer)[offset]&0XF0000000; 
		((DWORD *)buffer)[offset]=((DWORD *)buffer)[offset]|(value); 
	} 
	else 
	{ 
		offset=cluster%256;//find the position 
		((WORD *)buffer)[offset]=(WORD)value; 
	} 
	if(FAT_WriteSector(sector,buffer))return 1;//fat1 
	if(FAT_WriteSector(sector+FATsectors,buffer))return 1;//fat2 
	 
	return 0; 
 
 
 
} 
/* 
 
遍历fat表寻找空闲簇号 
返回1表示错误: 
*/ 
DWORD FindFreeCluster() 
{ 
BYTE *buffer=wriBuf; 
WORD *buf_fat16; 
DWORD *buf_fat32; 
DWORD cluster=0; 
DWORD tempsector=FirstFATSector; 
WORD i=0; 
if(FAT32_Enable==0) 
	{ 
		for(;tempsector<FATsectors+FirstFATSector;tempsector++) 
			{ 
				if(FAT_ReadSector(tempsector,buffer)) 
				{return 1;} 
				buf_fat16=(WORD *)buffer; 
				for(i=0;i<256;i++) 
				   {	 
						if(*(buf_fat16+i)==0) 
							return cluster; 
						cluster++; 
 
			       } 
			} 
		return 1; 
 
	} 
else  
	{ 
 
		for(;tempsector<FATsectors+FirstFATSector;tempsector++) 
			{ 
				if(FAT_ReadSector(tempsector,buffer)){return 1;} 
				buf_fat32=(DWORD *)buffer; 
				for(i=0;i<128;i++) 
				   {	 
						if(*(buf_fat32+i)==0) 
							return cluster; 
						cluster++; 
					} 
 
			}        
		return 1; 
	} 
 
 
} 
 
 
/* 
在特点的目录创建文件 
 
*/ 
BYTE CreatFile(BYTE *name,BYTE *dir) 
{ 
	DWORD cluster; 
 
	BYTE *buffer=0; 
	DWORD tempclust; 
	DWORD sector; 
	BYTE cnt; 
	WORD offset; 
	BYTE i; 
	struct FileInfoStruct FileInfo; 
	struct direntry *item = 0; 
	buffer=tmpBuf; 
	cluster = FAT_OpenDir(dir); 
	if (! FAT_FindTxt(dir,name, &FileInfo)) return 0; 
	if(cluster==1) return 1; 
	if((cluster==0) && (FAT32_Enable == 0))// root directory 
	{ 
		 
		if(buffer==0)return 1;//if failed 
		for(cnt=0;cnt<RootDirSectors;cnt++) 
		{ 
			if(FAT_ReadSector(FirstDirSector+cnt,buffer)){return 1;} 
			for(offset=0;offset<512;offset+=32) 
			{ 
				item=(struct direntry *)(&buffer[offset]); 
				if(item->deName[0]== 0x00) 
				{ 
				//	wri_secter=FirstDirSector+cnt; 
				//	wri_offset=offset; 
					for(i=0;i<11;i++) 
					{ 
					  buffer[offset+i]=name[i]; 
					} 
					 cluster=FindFreeCluster(); 
					 if(cluster==1) 
						{ 
							return 1; 
						} 
 
					 if (SetFat(cluster,0x0fffffff)){return 1;} 
					 item->deStartCluster =(WORD)(cluster&0xffff); 
					 item->deHighClust=(cluster&0x0fff0000)>>16; 
					 
					 item->deFileSize=0; 
					 item->deAttributes=0x20; 
					 item->deAttributes=0x20; 
				     item->deLowerCase=0x18; 
					 item->deCHundredth=0x9d; 
					 item->deCTime=0x70dd; 
    				 item->deCDate=0x3AED; 
    				 item->deADate=0x3AED; 
					 //item->deHighClust=0; 
					 item->deMTime=0X70F4; 
					 item->deMDate=0x3AED; 
					 if(FAT_WriteSector(FirstDirSector+cnt,buffer)) 
						return 1; 
						return 0; 
					 
				} 
			} 
		} 
	 
	} 
	else 
	{ 
		tempclust=cluster; 
		while(1) 
		{   tempclust=cluster; 
			sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
			 
			if(buffer==0)return 1;//if failed 
			for(cnt=0;cnt<SectorsPerClust;cnt++) 
			{ 
				if(FAT_ReadSector(sector+cnt,buffer)){return 1;} 
				for(offset=0;offset<512;offset+=32) 
				{ 
					item=(struct direntry *)(&buffer[offset]); 
					if(item->deName[0]== 0x00) 
					{ 
					//	wri_secter=sector+cnt; 
					 //   wri_offset=offset; 
						for(i=0;i<11;i++) 
						{ 
						buffer[offset+i]=name[i]; 
						} 
				 
					  	cluster=FindFreeCluster(); 
	 
					 	if(cluster==1) 
						{    
							return 1; 
						} 
 
					 if (SetFat(cluster,0x0fffffff)){return 1;} 
					 item->deStartCluster =cluster&0xffff; 
					 item->deHighClust=(cluster&0xffff0000)>>16; 
					  
					 item->deFileSize=0; 
					 item->deAttributes=0x20; 
					 item->deAttributes=0x20; 
					 item->deLowerCase=0x18; 
					 item->deCHundredth=0x9d; 
				 	 item->deCTime=0x70dd; 
  					 item->deCDate=0x3AED; 
   					 item->deADate=0x3AED; 
					 //item->deHighClust=0; 
					 item->deMTime=0X70F4; 
					 item->deMDate=0x3AED; 
					 if(FAT_WriteSector(sector+cnt,buffer)) 
						return 1; 
					 else 
						return 0; 
					 
					} 
				} 
			} 
			 
			cluster=FAT_NextCluster(tempclust);//next cluster 
			if(cluster == 0x0fffffff || cluster == 0x0ffffff8 || (FAT32_Enable == 0 && cluster >= 0xfff8)) 
			 { 
				 
				cluster=FindFreeCluster(); 
				if (cluster==1) 
				{ 
					return 1; 
				} 
			   	if(SetFat(tempclust,cluster)) 
				{ 
					return 1; 
				} 
			   	if(SetFat(cluster,0x0fffffff)) 
				{ 
					return 1; 
				} 
 
				Clear_Cluster(cluster); 
 
			} 
		} 
	} 
	return 1; 
 
} 
 
/* 
 
搜索特定目录下的文件 
*/ 
unsigned char FAT_FindTxt(BYTE *dir,BYTE *name, struct FileInfoStruct *FileInfo) 
{ 
DWORD cluster; 
cluster = FAT_OpenDir(dir); 
if(cluster == 1) 
return 1; 
return FAT_FindItem(cluster,name,FileInfo); 
} 
 
/////////////// 
/////////////// 
///////////////// 
////////////////////// 
////////////////// 
int ReadTxt(struct FileInfoStruct *FileInfo) 
{   unsigned char ch; 
    unsigned char *buf=&tmpBuf[0]; 
	if(FileInfo->Offset==FileInfo->Size) 
	return 0xffff; 
	if(FileInfo->Offset==0) 
		{ 
		buf_index=0; 
		sector_count=0; 
		P=FileInfo->StartCluster; 
		FAT_LoadPartCluster(P,sector_count,buf); 
		FileInfo->Offset++; 
		buf_index++; 
		return tmpBuf[0]; 
		} 
	else 
		{ 
		 
 
		if(buf_index==512)//only for 512 bit per sector 
			{ 
				buf_index=0; 
				sector_count++; 
					if(sector_count==SectorsPerClust) 
						{sector_count=0; 
						P=FAT_NextCluster(P);//读下一簇数据	 
						if(P == 0x0fffffff || P == 0x0ffffff8 || (FAT32_Enable == 0 && P>= 0xfff8))//如果无后续簇则结束,	//no more cluster 
							{ 
		         
								return 0xffff; 
							} 
						} 
				FAT_LoadPartCluster(P,sector_count,buf); 
				FileInfo->Offset++; 
				ch=buf[buf_index]; 
		 		buf_index++; 
			} 
		else  
			{ 
			FileInfo->Offset++; 
			ch=buf[buf_index]; 
		 	buf_index++; 
			} 
		return ch; 
		} 
 
} 
/* 
 
写文件 
 
*/ 
BYTE WriteTxt(struct FileInfoStruct *FileInfo,BYTE ch) 
{ 
BYTE *buf=wriBuf; 
DWORD tempclust; 
 
	if (FileInfo->Offset==0) 
   { 
	 wri_cluster=FileInfo->StartCluster; 
     wribuf_index=0; 
     wrisector_count=0;	 
	 FileInfo->Size=0;		 
   } 
 
	buf[wribuf_index]=ch; 
	wribuf_index++; 
    FileInfo->Size++; 
	FileInfo->Offset++; 
	if(wribuf_index==BytesPerSector) 
	{   wribuf_index=0; 
		FAT_SetPartCluster(wri_cluster,wrisector_count,buf); 
		wrisector_count++; 
        if (wrisector_count==SectorsPerClust) 
		{ 
		   	tempclust=FAT_NextCluster(wri_cluster);//next cluster 
			if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust >= 0xfff8)) 
			  { 
				  
				tempclust=FindFreeCluster(); 
				if (tempclust==1) 
				{ 
					return 1; 
				} 
			   	if(SetFat(wri_cluster,tempclust)) 
				{ 
					return 1; 
				} 
			   	if(SetFat(tempclust,0x0fffffff)) 
				{ 
					return 1; 
				} 
				wrisector_count=0;	 
				wri_cluster=tempclust; 
			  }	 
			else 
			 { 
				wri_cluster=tempclust; 
 
			 } 
 
		} 
 
	} 
//FileInfo->WriSector=wrisector_count; 
//FileInfo->WriCluster=wri_cluster; 
 
 
return 0; 
 
} 
 
BYTE Flesh(struct FileInfoStruct *FileInfo) 
{ 
	BYTE *buf=wriBuf; 
	struct direntry *item = 0; 
	if(FAT_SetPartCluster(wri_cluster,wrisector_count,buf)) 
	{ 
	 
		return 1; 
 
	} 
	if(FAT_ReadSector(FileInfo->Sector,buf)) 
	{ 
		return 1; 
	} 
	 
	item=(struct direntry *)(&buf[FileInfo->Startindex]); 
 
					  
	item->deFileSize=FileInfo->Size; 
	item->deAttributes=0x20; 
	item->deLowerCase=0x18; 
	item->deCHundredth=0x9d; 
	item->deCTime=0x70dd; 
    item->deCDate=0x3AED; 
    item->deADate=0x3AED; 
//	item->deHighClust=0; 
	item->deMTime=0X70F4; 
	item->deMDate=0x3AED; 
	if(FAT_WriteSector(FileInfo->Sector,buf)) 
		return 1; 
	else 
 
	return 0; 
					 
} 
 
//把LONG型的数字转化成文件名 
//参数 (1)数字 ,(1)存储文件名的数组 
void GetFileName(DWORD nu,BYTE Name[12]) 
{ 
BYTE n; 
BYTE FirstFlag=0; 
WORD div=10000; 
WORD i=0,j=0; 
for(i=0;i<8;i++) 
{ 
Name[i]=' '; 
} 
for(i=0;i<5;i++) 
{ 
	n=nu/div; 
	if(n!=0) 
		{ 
		FirstFlag=1; 
	 
			Name[j]=n+48; 
		j++; 
		} 
	else 
 
    	{ 
    if(FirstFlag==1) 
	{ 
			Name[j]=n+48; 
		j++; 
	} 
 
 
	   }  
nu=nu%div; 
div=div/10; 
 
  } 
} 
 
BYTE CreatDictionary(BYTE *name,BYTE *dir) 
{ 
	DWORD cluster; 
	BYTE *buffer=0; 
	DWORD tempclust; 
	DWORD sector; 
	WORD cnt; 
	WORD offset; 
	WORD i,j; 
	struct FileInfoStruct FileInfo; 
	struct direntry *item = 0; 
	buffer=tmpBuf; 
	cluster = FAT_OpenDir(dir); 
	if (! FAT_FindTxt(dir,name, &FileInfo)) return 0; 
	if(cluster==1) return 1; 
	if((cluster==0) && (FAT32_Enable == 0))// root directory 
	{ 
		 
		if(buffer==0)return 1;//if failed 
		for(cnt=0;cnt<RootDirSectors;cnt++) 
		{ 
			if(FAT_ReadSector(FirstDirSector+cnt,buffer)){return 1;} 
			for(offset=0;offset<512;offset+=32) 
			{ 
				item=(struct direntry *)(&buffer[offset]); 
				if(item->deName[0]== 0x00) 
				{ 
				 
					//buffer[offset]=0xe5; 
					for(i=0;i<11;i++) 
					{ 
					  if(i<8) 
						buffer[offset+i]=name[i]; 
					  else buffer[offset+i]=0x20; 
					} 
					 cluster=FindFreeCluster(); 
					 if(cluster==1) 
						{ 
							return 1; 
						} 
 
					 if (SetFat(cluster,0x0fffffff)){return 1;} 
					 item->deStartCluster =(WORD)(cluster&0xffff); 
					 item->deHighClust=(cluster&0x0fff0000)>>16; 
					 item->deFileSize=0; 
					 item->deAttributes=0x10; 
				     item->deLowerCase=0x18; 
					 item->deCHundredth=0x9d; 
					 item->deCTime=0x70dd; 
    				 item->deCDate=0x3AED; 
    				 item->deADate=0x3AED; 
					 item->deMTime=0X70F4; 
					 item->deMDate=0x3AED; 
 
					 if(FAT_WriteSector(FirstDirSector+cnt,buffer)) 
						return 1; 
					offset=0; 
					for(j=0;j<512;j++)buffer[j]=0; 
					for(j=0;j<2;j++) 
					{ 
					 	item=(struct direntry *)(&buffer[offset]); 
					 	for(i=0;i<11;i++) 
						{ 
					  	 buffer[offset+i]=0x20; 
						} 
;						buffer[offset]='.'; 
						if (!j)  
						{	 
							buffer[offset+1]='.'; 
							item->deStartCluster =0; 
					 		item->deHighClust=0; 
						} 
						else 
						{ 
							item->deStartCluster =cluster&0xffff; 
					 		item->deHighClust=(cluster&0x0fff0000)>>16;	 
		 
						} 
					    
					 	item->deFileSize=0; 
					 	item->deAttributes=0x10; 
				     	item->deLowerCase=0x18; 
					 	item->deCHundredth=0x9d; 
						item->deCTime=0x70dd; 
    				 	item->deCDate=0x3AED; 
    				 	item->deADate=0x3AED; 
					 	item->deMTime=0X70F4; 
					 	item->deMDate=0x3AED; 
 
						offset=32; 
					}  
					sector=FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
					if(FAT_WriteSector(sector,buffer)) 
						return 1; 
						return 0; 
					 
				} 
			} 
		} 
	 
	} 
	else 
	{ 
 
		while(1) 
		{   tempclust=cluster; 
			sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
			 
			if(buffer==0)return 1;//if failed 
			for(cnt=0;cnt<SectorsPerClust;cnt++) 
			{ 
				if(FAT_ReadSector(sector+cnt,buffer)){return 1;} 
				for(offset=0;offset<512;offset+=32) 
				{ 
					item=(struct direntry *)(&buffer[offset]); 
					if(item->deName[0]== 0x00) 
					{ 
					 
						for(i=0;i<11;i++) 
						{if(i<8) 
						buffer[offset+i]=name[i]; 
						else buffer[offset+i]=0x20; 
						} 
				         
					  	cluster=FindFreeCluster(); 
	 
					 	if(cluster==1) 
						{    
							return 1; 
						} 
 
					 if (SetFat(cluster,0x0fffffff)){return 1;} 
					 item->deStartCluster =cluster&0xffff; 
					 item->deHighClust=(cluster&0xffff0000)>>16; 
					  
					 item->deFileSize=0; 
					 item->deAttributes=0x10; 
					 item->deLowerCase=0x18; 
					 item->deCHundredth=0x9d; 
				 	 item->deCTime=0x70dd; 
  					 item->deCDate=0x3AED; 
   					 item->deADate=0x3AED; 
					  
					 item->deMTime=0X70F4; 
					 item->deMDate=0x3AED; 
					 if(FAT_WriteSector(sector+cnt,buffer)) 
						return 1; 
						offset=0; 
					for(j=0;j<512;j++)buffer[j]=0; 
					for(j=0;j<2;j++) 
					{ 
					 	item=(struct direntry *)(&buffer[offset]); 
					 	 
;						for(i=0;i<11;i++) 
						{ 
					  	 buffer[offset+i]=0x20; 
						} 
;						buffer[offset]='.'; 
						if (!j)  
						{	 
							buffer[offset+1]='.'; 
							item->deStartCluster =0; 
					 		item->deHighClust=0; 
						} 
						else 
						{ 
							item->deStartCluster =cluster&0xffff; 
					 		item->deHighClust=(cluster&0x0fff0000)>>16;	 
		 
						} 
					    
					 	item->deFileSize=0; 
					 	item->deAttributes=0x10; 
				     	item->deLowerCase=0x18; 
					 	item->deCHundredth=0x9d; 
						item->deCTime=0x70dd; 
    				 	item->deCDate=0x3AED; 
    				 	item->deADate=0x3AED; 
					 	item->deMTime=0X70F4; 
					 	item->deMDate=0x3AED; 
						offset=32; 
					}  
					sector=FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
					if(FAT_WriteSector(sector,buffer)) 
						return 1; 
					 else 
						return 0; 
					 
					} 
				} 
			} 
			 
			cluster=FAT_NextCluster(tempclust);//next cluster 
			if(cluster == 0x0fffffff || cluster == 0x0ffffff8 || (FAT32_Enable == 0 && cluster >= 0xfff8)) 
			 { 
				 
				cluster=FindFreeCluster(); 
				if (cluster==1) 
				{ 
					return 1; 
				} 
 
			   	if(SetFat(tempclust,cluster)) 
				{ 
					return 1; 
				} 
			   	if(SetFat(cluster,0x0fffffff)) 
				{ 
					return 1; 
				} 
 
				Clear_Cluster(cluster); 
 
			} 
		} 
	} 
	return 1; 
 
} 
//将一簇写入0 
BYTE Clear_Cluster(DWORD cluster) 
	{ 
	BYTE *buffer=wriBuf; 
	WORD i=0; 
	WORD cnt; 
	DWORD sector=FirstDataSector+(DWORD)(cluster-2)*(DWORD)SectorsPerClust; 
	for(i=0;i<512;i++)buffer[i]=0; 
	for(cnt=0;cnt<SectorsPerClust;cnt++) 
		{ 
			if(FAT_WriteSector(sector+cnt,buffer)) 
			return 1; 
 
		} 
 
	return 0; 
 
	} 
//删除文件,以及文件夹 
//参数 1.文件名 2。文件所在的绝对路径 
//返回值 0.删除成功 1.删除失败 2.所要删除的文件不存在 
BYTE Delete_File(BYTE *name,BYTE *dir) 
{ 
	DWORD cluster,tmpclust; 
	struct FileInfoStruct fileInfo; 
 
	if (!FAT_FindTxt(dir,name, &fileInfo)) 
	{ 
		cluster=fileInfo.StartCluster; 
		if(Delete_Item(&fileInfo))return 1; 
		while(1) 
		{ 
			tmpclust=FAT_NextCluster(cluster);	 
			if(SetFat(cluster,0x00000000)) 
			{ 
				return 1; 
			} 
			cluster=tmpclust; 
			if(cluster== 0x0fffffff || cluster == 0x0ffffff8 || (FAT32_Enable == 0 && cluster== 0xffff)) 
        	{ 
				return 0; 
			} 
		} 
 
	} 
    else 
	{ 
		return 2; 
	} 
} 
 
 
BYTE Delete_Item(struct FileInfoStruct *FileInfo) 
{ 
	BYTE *buffer=tmpBuf; 
	DWORD tempclust; 
	DWORD sector=FileInfo->Sector;				 
	WORD offset=FileInfo->Startindex; 
	BYTE i; 
	if(FAT_ReadSector(sector,buffer)) 
			return 1; 
	for(i=0;i<32;i++) 
		{ 
		  buffer[offset+i]=0; 
		} 
	if(FAT_WriteSector(sector,buffer)) 
		return 1; 
	else 
	    return 0; 
} 
BYTE Rename(BYTE *name,BYTE *dir,BYTE *newname) 
{ 
	BYTE *buffer=tmpBuf; 
	DWORD tempclust; 
	DWORD sector;				 
	WORD offset; 
	BYTE i; 
	struct FileInfoStruct FileInfo; 
	if(FAT_FindTxt(dir,name,&FileInfo)) return 1; 
 
	sector=FileInfo.Sector;				 
	offset=FileInfo.Startindex; 
	 
	if(FAT_ReadSector(sector,buffer)) 
			return 1; 
	for(i=0;i<11;i++) 
		{ 
		  buffer[offset+i]=newname[i]; 
		} 
	if(FAT_WriteSector(sector,buffer)) 
		return 1; 
	else 
	    return 0; 
 
} 
//BYTE filenameofdir(BYTE *name,BYTE *dir,BYTE *newname,unsigned int fcount) 
//功能:返回特定文件夹下 第n个文件名 
//参数: 1.PATH 目录路径 2.name 存放文件名的指针,3.fcount第fcount个文件 
// 
BYTE filenameofdir(BYTE *PATH,BYTE *name,WORD fcount) 
{ 
DWORD cluster; 
BYTE *buffer; 
DWORD tempclust; 
DWORD sector; 
BYTE cnt; 
WORD offset,fn=0; 
BYTE i; 
struct direntry *item = 0; 
cluster = FAT_OpenDir(PATH); 
if(cluster == 1) 
return 1; 
 
 
     
	if((cluster==0) && (FAT32_Enable == 0))// root directory 
	{ 
		buffer=tmpBuf;//apply memory 
		if(buffer==0)return 1;//if failed 
		for(cnt=0;cnt<RootDirSectors;cnt++) 
		{ 
			if(FAT_ReadSector(FirstDirSector+cnt,buffer)){/*free(buffer);*/return 1;} 
			for(offset=0;offset<512;offset+=32) 
			{ 
				item=(struct direntry *)(&buffer[offset]); 
				if((item->deName[0] != '.')&&(item->deName[0] != 0x00) && (item->deName[0] != 0xe5) && (item->deAttributes != 0x0f)) 
				{ 
 
					if(((item->deAttributes&0x10)!=0x10)&&buffer[offset+8]=='T'&&buffer[offset+9]=='X'&&buffer[offset+10]=='T') 
						{ 
							fn++; 
							if(fn==fcount) 
								{ 
										for(i=0;i<8;i++) 
										{ 
											name[i]=buffer[offset+i]; 
										} 
										name[8]='\0'; 
										return 0; 
								} 
						} 
				 
 
				} 
			} 
		} 
 
	} 
	else//other folders 
	{ 
		tempclust=cluster; 
		while(1) 
		{ 
			sector=FirstDataSector+(DWORD)(tempclust-2)*(DWORD)SectorsPerClust;//calculate the actual sector number 
			buffer=tmpBuf;//apply memory 
			if(buffer==0){name[0]=0;return 1;};//if failed 
			for(cnt=0;cnt<SectorsPerClust;cnt++) 
			{ 
				if(FAT_ReadSector(sector+cnt,buffer)){name[0]=0;return 1;} 
				for(offset=0;offset<512;offset+=32) 
				{ 
					item=(struct direntry *)(&buffer[offset]); 
					if((item->deName[0] != '.')&&(item->deName[0] != 0x00) && (item->deName[0] != 0xe5) && (item->deAttributes != 0x0f)) 
					{ 
						if(((item->deAttributes&0x10)!=0x10)&&buffer[offset+8]=='T'&&buffer[offset+9]=='X'&&buffer[offset+10]=='T') 
						{ 
							fn++; 
							if(fn==fcount) 
								{ 
										for(i=0;i<8;i++) 
										{ 
											name[i]=buffer[offset+i]; 
										} 
										name[8]='\0'; 
										return 0; 
			 
								}       
						} 
					} 
				} 
			} 
			/*free(buffer);*///release 
			tempclust=FAT_NextCluster(tempclust);//next cluster 
			if(tempclust == 0x0fffffff || tempclust == 0x0ffffff8 || (FAT32_Enable == 0 && tempclust == 0xffff))break; 
		} 
	} 
	name[0]=0; 
	return 1; 
 
}