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

📄 filesys.c

📁 arm(s3c440b)读写u盘源程序
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "DataType.h"
#include "FAT.h"
#include "ufi.h"
#include "FileSys.h"
#include "Console.h"
#include "string.h"

extern Control		USB_Control;
extern BPB_INFO		DiskInfo;
extern FILE_INFO	ThisFile;
extern DIR_INFO		ThisDir;
extern WORD Fat16Cache[2][257];
extern DWORD Fat32Cache[2][129];
BYTE OpenFile( const char *filename)
{
	U16 i,sum;
	char bstop,sector,count,flag;
	BYTE BUF[512];
	U32 sectorToRead,dTempCluster,dirSectorPointer,dirClusterPointer;
	PDIR_STRUCT pDir;
	/////////////////////////////////////////////////////////////////
	ThisFile.bOpen=0;
	flag=0;
	sum=0;
	count=0;
	bstop=0;
	sector=0;
	dirClusterPointer=ThisDir.StartCluster;
	dirSectorPointer=ClusterToSec(ThisDir.StartCluster);
	for(;;)
	    {   
		/////////////////////////////////////////////////
		if(USB_Control.bIsFat32==0)	//Is fat16
		{
		
			if(ThisDir.bRootDir)	//Is Root directory
				{

					if(sector>DiskInfo.RootEntCnt>>4)
					{
						printf("openfile中的1错误\n");
						return FALSE;
					}
					sectorToRead=ThisDir.StartSector+sector++;
				}
			else					//Not Root directory
				{
				
				if(sector>DiskInfo.SecPerClus-1)
					{
					dirClusterPointer=GetNextCluster(dirClusterPointer);
					if(dirClusterPointer>0xfff8)
						{
						printf("openfile中的2错误\n");
						return FALSE;
						}
					dirSectorPointer=ClusterToSec(dirClusterPointer);
					sector=0;
					}
				sectorToRead=dirSectorPointer+sector++;
				}
		}
		else	//Is fat32
		{
			if(sector>DiskInfo.SecPerClus-1)
				{
					dirClusterPointer=GetNextCluster(dirClusterPointer);
					if(dirClusterPointer>0x0ffffff8)
						{
						printf("openfile中的3错误\n");
						return FALSE;
						}
					dirSectorPointer=ClusterToSec(dirClusterPointer);
					sector=0;
				}
			sectorToRead=dirSectorPointer+sector++;
		}
		//////////////////////////////////////////////////
		if(!Read(sectorToRead,1,(BYTE *)BUF))
			{
				printf("openfile中的4错误,%d\n",sectorToRead);
				return FALSE;	
			}
		pDir=(PDIR_STRUCT)BUF;
		///////////////////////////////////////////////////
		for(i=0;i<DiskInfo.BytesPerSec;pDir++,i=i+32)
			{
			//c=BUF[i];	
			if(BUF[i]==0x00)
				{
			//	printf("openfile中的1错误\n");
				return FALSE;				
				}
			///////////////////////////////////////////
			//	j=0;
			if(BUF[i]==0xE5||(BUF[i+11]==0x10))
				continue;
		
				if(memcmp(pDir->DIR_Name,filename,strlen(filename))==0)
				{
					ThisFile.bOpen=1;
					bstop=1;
					break;
				}
			
			
		}
		///////////////////////////////////////////////////////
		if(bstop==1)break;
		
	    }
	////////////////////////////////////////////
	if(USB_Control.bIsFat32==0)
		ThisFile.StartCluster=pDir->DIR_FstClusLO;
	else
		ThisFile.StartCluster=(pDir->DIR_FstClusHI<<16)|(pDir->DIR_FstClusLO);
	//////////////////////////////////////////////////////////////
	ThisFile.DirSector=sectorToRead;//指向目录扇区
	ThisFile.DirNumOffSet=(pDir-(PDIR_STRUCT)BUF);//指向该扇区中目录的具体位置
	//////////////////////////////////////////////////////////

	ThisFile.LengthInByte=pDir->DIR_FileSize;
	ThisFile.ClusterPointer=ThisFile.StartCluster;
	dTempCluster=ThisFile.StartCluster;
//	printf("%d\n\n\n",dTempCluster);
	////////////////////////////////////////////////////////
	//将指针移到文件末尾
	if(ThisFile.LengthInByte==0)
	{
		ThisFile.SectorPointer=ClusterToSec(ThisFile.StartCluster);
	}else{
		if(USB_Control.bIsFat32==0)
		{
			do
			{	
				ThisFile.SectorPointer=ClusterToSec(dTempCluster);
				dTempCluster=GetNextCluster(dTempCluster);
//				printf("%d\n",dTempCluster);
			}while(dTempCluster!=0xffff);

		}else{// isfat32
			do
			{
			ThisFile.SectorPointer=ClusterToSec(dTempCluster);
			dTempCluster=GetNextCluster(dTempCluster);
			}while(dTempCluster!=0x0fffffff);	

		}
	}//end if
	/////////////////////////////////////////////////////////
//	printf("开始簇号%dt%d%,%d/n",ThisFile.StartCluster,ThisFile.SectorPointer,dTempCluster);
	ThisFile.OffsetofSector=ThisFile.LengthInByte%(DiskInfo.BytesPerSec*DiskInfo.SecPerClus);
	ThisFile.Readpointer=ThisFile.LengthInByte;//指针移到最文件后;
	return TRUE;
}

BYTE CreateFile(const char *filename)
{
	U16 i;//j,
	char bstop,sector;
	BYTE BUF[512],flag,count;	
	U32 sectorToRead;
	DIR_STRUCT Dir;
	U32 dirSectorPointer,dirClusterPointer;
	ThisFile.bOpen=0;	
	memset(&Dir,0,sizeof(DIR_STRUCT));
	count=0;
	flag=0;
	bstop=0;	
	sector=0;
	dirClusterPointer=ThisDir.StartCluster;
	dirSectorPointer=ClusterToSec(ThisDir.StartCluster);
	while(1)
	    {   
		///////////////////////////////////////////////////
		if(USB_Control.bIsFat32==0)
		{
			if(ThisDir.bRootDir)	//Is Root directory
				{
				
				if(sector>DiskInfo.RootEntCnt>>4)
					{
					printf("CreateFile:错误1\n");
					return FALSE;
					}
				sectorToRead=ThisDir.StartSector+sector++;
				}
			else					//Not Root directory
				{
				
				
				if(sector>DiskInfo.SecPerClus-1)
					{
					dirClusterPointer=GetNextCluster(dirClusterPointer);
					if(dirClusterPointer>0xfff8)
						{
						// To do:
						printf("CreateFile:错误2\n");
						return FALSE;
						
						}
					dirSectorPointer=ClusterToSec(dirClusterPointer);
					sector=0;
					}
				sectorToRead=dirSectorPointer+sector++;
				}
		}
		else	//Is FAT32
		{
			if(sector>DiskInfo.SecPerClus-1)
				{
					dirClusterPointer=GetNextCluster(dirClusterPointer);
					if(dirClusterPointer>0x0ffffff8)
						{
						printf("CreateFile:错误3\n");
						return FALSE;
						}
					dirSectorPointer=ClusterToSec(dirClusterPointer);
					sector=0;
				}
			sectorToRead=dirSectorPointer+sector++;
		}
		//////////////////////////////////////////////////
		if(!Read(sectorToRead,1,BUF))
			{
				printf("CreateFile:错误4\n%d",sectorToRead);
				return FALSE;	
			
			}
		///////////////////////////////////////////////////
		for(i=0;i<DiskInfo.BytesPerSec;i=i+32)
			{
				if((flag!=0)||(BUF[i]==0x00)||(BUF[i]==0xE5))
					{
	
					if(strlen(filename)<12)
						{
							Dir.DIR_Attr=0x20;
							memcpy((char *)Dir.DIR_Name,filename,11);
							memcpy((char *)BUF+i,(char *)&Dir,32);
							if(!Write(sectorToRead,1,BUF))
		  	 					{
								printf("CreateFile:错误6\n%d",sectorToRead);
								return FALSE;	
		  	 					}
							bstop=1;
							break;
						}

					}

			}//end for
		///////////////////////////////////////////////////////
		if(bstop==1)break;
		
	    }//end while
	/////////////////////////////////////////////
	ThisFile.StartCluster=0x00000000;//SwapINT16(pDirInfo->startCluster);
	ThisFile.LengthInByte=0;
	ThisFile.ClusterPointer=0x00;//ThisFile.StartCluster;
	ThisFile.SectorPointer=0x00;//ClusterToSec(ThisFile.StartCluster);
	ThisFile.OffsetofSector=0;
//	ThisFile.bOpen=1;
	return TRUE;
}
BYTE Locate( BYTE *ShortName)
{
	U16 i;
	char bstop,sector;
	BYTE BUF[512];
	U32 sectorToRead;
	PDIR_STRUCT pDir;
	U32 dirSectorPointer,dirClusterPointer;
	/////////////////////////////////////////////////////////////////
	//转换为大写格式
	for(i=0;i<strlen((char *)ShortName);i++)
	{
		if((ShortName[i]>=97)&&(ShortName[i]<=122))
		{
			ShortName[i]=ShortName[i]-32;
		}
	}
	bstop=0;
	sector=0;
	dirClusterPointer=ThisDir.StartCluster;
	dirSectorPointer=ClusterToSec(ThisDir.StartCluster);
	for(;;)
	    {   
		/////////////////////////////////////////////////
		if(USB_Control.bIsFat32==0)	//Is fat16
		{
		
			if(ThisDir.bRootDir)	//Is Root directory
				{

					if(sector>DiskInfo.RootEntCnt>>4)
					{
						return FALSE;
					}
					sectorToRead=ThisDir.StartSector+sector++;
				}
			else					//Not Root directory
				{
				
				if(sector>DiskInfo.SecPerClus-1)
					{
					dirClusterPointer=GetNextCluster(dirClusterPointer);
					if(dirClusterPointer>0xfff8)
						{
						
						return FALSE;
						}
					dirSectorPointer=ClusterToSec(dirClusterPointer);
					sector=0;
					}
				sectorToRead=dirSectorPointer+sector++;
				}
		}
		else	//Is fat32
		{
			if(sector>DiskInfo.SecPerClus-1)
				{
					dirClusterPointer=GetNextCluster(dirClusterPointer);
					if(dirClusterPointer>0x0ffffff8)
						{
						return FALSE;
						}
					dirSectorPointer=ClusterToSec(dirClusterPointer);
					sector=0;
				}
			sectorToRead=dirSectorPointer+sector++;
		}
		//////////////////////////////////////////////////
		if(!Read(sectorToRead,1,BUF))
			{
			return FALSE;	
			}
		pDir=(PDIR_STRUCT)BUF;
		///////////////////////////////////////////////////
		for(i=0;i<DiskInfo.BytesPerSec;i=i+32,pDir++)
			{
				if(BUF[i]==0x00)
				{
				return FALSE;	
				}

			if((BUF[i+11]==0x0f)||BUF[i]==0xE5)
				continue;
			if(memcmp(ShortName,BUF+i,11)==0)
				{
				  bstop=1;
				  break;
				}
			}
		////////////////////////////////////////////////////
		if(bstop)
			break;
	}//end for
	return TRUE;
}
BYTE  GetShorFileForm(const char *fullpath,char static_path[256])
{
	char* p=static_path;
	char path[256];
	char* ppath = path;
	int dir_len_count; 
	int i;
	if(fullpath == NULL || strlen(fullpath) >12)
		return FALSE;
	dir_len_count=0;
	strcpy(path, fullpath);
	for(i=0;(*ppath!=NULL&&i<12);i++)
	{
		if(	*ppath > 96 && *ppath < 123)
		{
			*ppath -= 32;
		}
		ppath++;
	}
	memset(p, 0, 256);
	ppath = path;
	for(;;)
	{
		switch(*ppath)
		{
		case 0:
			if(dir_len_count>=11)	return TRUE;
			else 
			{
				dir_len_count ++;
				*p = 0x20;
				p++;
			}
			continue;
		case '.':
			{
				if(dir_len_count > 8 || dir_len_count ==0) 
					return FALSE;
				if(ppath[1] == '.')
					return FALSE;
//				if(dir_len_count > 8)
//				{
//					*p = *ppath;
//					p++;
//				}
//				else
//				{	
					for(i=0; i<(8 - dir_len_count); i++)
					{
						*p = 0x20;
						p++;
					}
//				}

				dir_len_count =8;
				ppath++;
				continue;
			}
			break;

		case 0x22:
		case 0x2A:
		case 0x2B:
		case 0x2C:
		case 0x2F:
		case 0x3A:
		case 0x3B:
		case 0x3C:
		case 0x3D:
		case 0x3E:
		case 0x3F:
		case 0x5B:
		case 0x5D:
		case 0x7C:
			return FALSE;

		default:
			{
				if(*ppath < 0x20)
					return FALSE;		
			}
			break;
		}
		*p = *ppath;
		dir_len_count ++;

		if(dir_len_count >= 11)
			return TRUE;
		p++;
		ppath++;
	}
}
BYTE GetShorDirForm(const char * DirName,char *p)
{
	BYTE i;
	BYTE flag='.';
	if(strlen(DirName)>8)
		return FALSE;

	memset(p,0x20,11);
	if(strchr(DirName,(int)flag))
	{
		printf("GetShortDirForm:错误1\n");
		return FALSE;
	}

⌨️ 快捷键说明

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