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

📄 fsys.c

📁 USB驱动程序,和NANDflash的驱动, 包括文件系统,cpu是arm
💻 C
📖 第 1 页 / 共 2 页
字号:
				handles.SectorOffset = 0;
			}
			if(read_bytes == bytes)
			{
				return bytes;
			}
		}
	}
	while(Cluster != 0xffff)
	{
		for(i=0; i< Bpb.SecPerClus; i++)
		{
			if(RBC_Read(handles.CurrentSectorNum,1,(unsigned char *)Cache)==FALSE)return 0;
			pCache=Cache;
			pCache += handles.SectorOffset;
			max_copy_bytes_in_sector = (Bpb.BytsPerSec - handles.SectorOffset) > (bytes - read_bytes) ? (bytes - read_bytes) : (Bpb.BytsPerSec - handles.SectorOffset);
			memcpy(buffer, pCache, max_copy_bytes_in_sector);
			read_bytes += max_copy_bytes_in_sector;
			handles.SectorOffset += max_copy_bytes_in_sector;
			handles.offset += max_copy_bytes_in_sector;
			buffer = (char*)buffer + max_copy_bytes_in_sector;

			if(handles.SectorOffset == Bpb.BytsPerSec)
			{				
				if(i == Bpb.SecPerClus -1)
				{
					Cluster = GetNextClusterNum(Cluster);
					if(Cluster != 0xffff)
						handles.CurrentSectorNum = ClusterNum2SectorNum(Cluster);
				}
				else
					handles.CurrentSectorNum ++;

				handles.SectorOffset = 0;
			}

			if(read_bytes == bytes)
			{
				return bytes;
			}
		}		
	}
	return 0;
}

WORD AllocCluster(WORD PrevClusterNum)
{
	static WORD LastAllocClusterNum=0;
	WORD i;

	if(LastAllocClusterNum == 0)
		LastAllocClusterNum = PrevClusterNum;

	for(i = LastAllocClusterNum; i < Bpb.BytsPerSec * Bpb.FATSz / sizeof(WORD); i++)
	{
		if(FatCache[i] == 0)
		{
			FatCache[i] = 0xffff;	// flag with 0xffff, this is the last cluster.
			LastAllocClusterNum = i;
			//chain this cluster to prev one.
			if(PrevClusterNum != 0)
				FatCache[PrevClusterNum] = LastAllocClusterNum;
			return LastAllocClusterNum;
		}
	}
	for(i = 2; i < Bpb.BytsPerSec * Bpb.FATSz / sizeof(WORD); i++)
	{
		if(FatCache[i] == 0)
		{
			FatCache[i] = 0xffff;	// flag with 0xffff, this is the last cluster.
			
			LastAllocClusterNum = i;
			//chain this cluster to prev one.	
			if(PrevClusterNum != 0)
				FatCache[PrevClusterNum] = LastAllocClusterNum;
			return LastAllocClusterNum;
		}
	}
	
	return 0xffff;
}

unsigned char  AllocDir(BYTE* new_dir, struct _FILE * fp, WORD namelong)
{

	BYTE Cache[512];
	struct _DIR *dir;
	INT32 i,j,k,n;
	WORD PrevCluster;
	WORD Cluster;
	INT32 DirSectorNum = FirstRootDirSecNum;

	i=0,j=0,k=0;
	n=sizeof(struct _DIR);
	n*=Bpb.RootEntCnt;
	n/=Bpb.BytsPerSec;
	for(; i<Bpb.RootEntCnt * sizeof(struct _DIR) / Bpb.BytsPerSec; i++)
	{
		if(RBC_Read(DirSectorNum,1,(unsigned char *)Cache)==FALSE)return FALSE;
		for(dir = (struct _DIR *)Cache,j=0; (BYTE*)dir < Cache + Bpb.BytsPerSec; j+=32,dir++)
		{
			if(dir->Name[0] == '\0')
			{
				if(512-j<namelong+32)
				{
					if(i+1 >= Bpb.RootEntCnt * sizeof(struct _DIR) / Bpb.BytsPerSec)return FALSE;
					memcpy(dir, new_dir, 512-j);	
					if(!RBC_Write(DirSectorNum,1,Cache,TRUE))
						if(!RBC_Write(DirSectorNum,1,Cache,TRUE))
							return FALSE;	
					DirSectorNum++;
					if(RBC_Read(DirSectorNum,1,(unsigned char *)Cache)==FALSE)return FALSE;
					dir = (struct _DIR *)Cache;
					k=512-j;
					namelong=namelong+j-512;
				}
				memcpy(dir, new_dir+k, namelong+32);	
				if(fp)
				{
					fp->DirSectorNum = DirSectorNum;
					fp->DirIndex =namelong/32 + ((BYTE*)dir - Cache) / sizeof(struct _DIR);
					memcpy(&fp->dir, new_dir+k+namelong, sizeof(struct _DIR));
				}
				if(!RBC_Write(DirSectorNum,1,Cache,TRUE))
					if(!RBC_Write(DirSectorNum,1,Cache,TRUE))
						return FALSE;	
				return TRUE;							
			}
		}
		DirSectorNum++;
	}
	return FALSE; 

}


unsigned char ChangeLongFile(BYTE *filename,BYTE *Cache,BYTE sum)
{
	int i,j,n,m,namelong;
	namelong=strlen(filename);
	i=namelong/13;
	i+=namelong%13?1:0;
	memset(Cache,0,512);
	Cache[0]=0x40;
	while(i--)
	{
		j=namelong%13;
		if(j==0)j=13;
		namelong -= j;
		*(Cache++) |= i+1;
		
		for(n=0,m=0;n<13;n++)
		{
			if(strlen(filename)>namelong+n)
			{
				*(Cache++)=filename[namelong+n];
				*(Cache++)=0;
			}
			else if(strlen(filename)==namelong+n)
			{
				*(Cache++)=0;
				*(Cache++)=0;
			}
			else 
			{
				*(Cache++)=0xFF;
				*(Cache++)=0xFF;
			}
			if(n==4)
			{
				*(Cache++)=0x0F;
				*(Cache++)=0;
				*(Cache++)=sum;
			}
			else if(n==10)
			{
				*(Cache++)=0;
				*(Cache++)=0;
			}
		}
	}
	return TRUE;
}
unsigned char fat_creat(const char* filename, BYTE attribute)
{
	struct _DIR dir;
	char name[13];
	struct _FILE file;
	struct _DIR *pdir;
	WORD NewCluster;
	BYTE Cache[512];
	BYTE sum;
	WORD namelong;
	int i;
	static char static_path[256];
	char * p=static_path;
	if(Locate(filename, NULL) != 0xffffffff)
		return FALSE;
	
	if(strlen(filename)<13)
	{
		if(FALSE == get_valid_format(filename,p))return FALSE;
		strncpy(name, p, 11);
		namelong=0;
	}
	else
	{
		memset(name,0x20,11);
		for(i=0;i<strlen(filename);i++)
		{
			if(filename[i]=='.')break;
		}
		if(i>5)memcpy(name,filename,5);
		else memcpy(name,filename,i);
		name[6]='~';
		name[7]='1';
		name[8]='.';
		if(strlen(filename)-i-1>3)memcpy(name+9,filename+i+1,3);
		else memcpy(name+9,filename+i+1,strlen(filename)-i-1);
		name[12]='\0';
		while(Locate(name, NULL) != 0xffffffff)
		{
			name[7]+=1;
			continue;
		}
		if(FALSE == get_valid_format(name,p))return FALSE;
		strncpy(name, p, 11);
		name[11]=0;
		for(sum=i=0;i<11;i++)
		{
			sum = ((sum&1)<<7 | ((sum&0xFE)>>1)) + name[i];
		}
		namelong=strlen(filename)/13;
		namelong+=strlen(filename)%13 ?  1: 0;
		namelong*=32;
	}
	
	ChangeLongFile((BYTE *)filename,(BYTE*)Cache,sum);
	memset(&dir, 0, sizeof(dir));
	memcpy(dir.Name, name, 11);
	dir.Attr = attribute;
	dir.CrtDate = 00;
	dir.CrtTime = 00;
	dir.CrtTimeTenth = 0;
	dir.FileSize = 0;
	memcpy(Cache+namelong,&dir,32);
	
	if(AllocDir(Cache, &file,namelong) != TRUE)
		return FALSE;

	NewCluster = AllocCluster(0);
	if(NewCluster == 0xffff)
		return FALSE;

	if(RBC_Read(file.DirSectorNum,1,(unsigned char *)Cache)==FALSE)return FALSE;

	pdir = (struct _DIR *)Cache;
	pdir += file.DirIndex;

	pdir->FstClusLO = NewCluster;
	if(!RBC_Write(file.DirSectorNum,1,Cache,TRUE))
		if(!RBC_Write(file.DirSectorNum,1,Cache,TRUE))
		return FALSE;
	return fat_open(filename);
}

unsigned char fat_write(const char* buffer, unsigned int bytes)
{
	BYTE *pCache,Cache[512*16];
	unsigned int write_bytes =0;
	unsigned int max_write_bytes_in_sector;
	WORD Cluster;
	WORD PrevCluster;
	int i;
	

	Cluster = SectorNum2ClusterNum(handles.CurrentSectorNum);
	PrevCluster = Cluster;
	for(;;)
	{
		pCache=Cache;
		if(handles.SectorOffset==Bpb.BytsPerSec*Bpb.SecPerClus)
		{
			handles.SectorOffset=0;
			Cluster = GetNextClusterNum(Cluster);
			if(Cluster != 0xffff)
				handles.CurrentSectorNum = ClusterNum2SectorNum(Cluster);
			else
			{
				Cluster = AllocCluster(PrevCluster);
				FatCache[PrevCluster]=Cluster;
				if(Cluster == 0xffff)
				{
					return 0;
				}
				handles.CurrentSectorNum = ClusterNum2SectorNum(Cluster);
			}		
		}
		
		pCache += handles.SectorOffset;
		if(handles.SectorOffset!=0)
		{
			if(RBC_Read(handles.CurrentSectorNum,Bpb.SecPerClus,(unsigned char *)Cache)==FALSE)return FALSE;
			pCache=Cache;
			pCache += handles.SectorOffset;
		}
		if((Bpb.BytsPerSec*Bpb.SecPerClus - handles.SectorOffset) > (bytes - write_bytes))
			max_write_bytes_in_sector =  (bytes - write_bytes);
		else
			max_write_bytes_in_sector =  (Bpb.BytsPerSec*Bpb.SecPerClus - handles.SectorOffset);
		memcpy(pCache, buffer, max_write_bytes_in_sector);
		
		if(!RBC_Write(handles.CurrentSectorNum,Bpb.SecPerClus,(unsigned char *)Cache,TRUE))
			if(!RBC_Write(handles.CurrentSectorNum,Bpb.SecPerClus,(unsigned char *)Cache,TRUE))
				return FALSE;
		write_bytes += max_write_bytes_in_sector;
		handles.SectorOffset += max_write_bytes_in_sector;
		handles.offset += max_write_bytes_in_sector;
		buffer = (char*)buffer + max_write_bytes_in_sector;
		handles.dir.FileSize +=  max_write_bytes_in_sector;
		PrevCluster = Cluster;
		if(write_bytes >= bytes)
		{
			return write_bytes;
		}
	}
	// we can not reach here.
	return 0;
}
unsigned char fSysInitialize()
{
	BYTE Cache[512];
	INT32 DataSec;
	INT32 CountofClusters;	
	char filename[256];
	char buffer[4096];
	struct _STAT stat;
	_FatDate *pdate;
	_FatTime *ptime;
	int i,j,maxflong;
	INT16 year,month,day,hour,minitor,second,year1,month1,day1,hour1,minitor1,second1;
	
	if(EnumUsbDev(1)==FALSE)return FALSE;
	Delay(3000);
	if(	EnumMassDev()==FALSE)return FALSE;
	if(RBC_Read(0,1,(unsigned char *)Cache)==FALSE)	return FALSE;
	if(Cache[0x1BE]==0 || Cache[0x1BE]==0x80)
	{
		if(Cache[0x1C2]==6||Cache[0x1C2]==4)
		{
			m_Offset=GETINT32((Cache+0x1C6));
			///**/
		}
	}
	if(Cache[0x00]==0xEB || Cache[0x02]==0x90)
	{
		m_Offset=0;
	}
	if(RBC_Read(0+m_Offset,1,(unsigned char *)Cache)==FALSE)return FALSE;
	Bpb.BytsPerSec =GETINT16((&Cache[11]));// *((WORD*)&Cache[11]);
	Bpb.SecPerClus = Cache[13];
	Bpb.RsvdSecCnt =GETINT16((&Cache[14]));// *((WORD*)&Cache[14]);
	Bpb.NumFATs = Cache[16];
	Bpb.RootEntCnt = GETINT16((&Cache[17]));//*((WORD*)&Cache[17]);
	if((Bpb.TotSec = GETINT16((&Cache[19]))) == 0)//
		Bpb.TotSec = GETINT32((&Cache[32]));//*((INT32*)&Cache[32]);
	if((Bpb.FATSz = GETINT16((&Cache[22]))) == 0)//*((WORD*)&Cache[22])
		Bpb.FATSz = GETINT32((&Cache[36]));//*((INT32*)&Cache[36]);
		
	RootDirSectors = ((Bpb.RootEntCnt * 32) + (Bpb.BytsPerSec - 1)) / Bpb.BytsPerSec;
	DataSec = Bpb.TotSec - (Bpb.RsvdSecCnt + (Bpb.NumFATs * Bpb.FATSz) + RootDirSectors);
	CountofClusters = DataSec / Bpb.SecPerClus;
	if(CountofClusters < 4085 || CountofClusters >= 65525)
		return FALSE;
	FirstFatSecNum = Bpb.RsvdSecCnt+m_Offset;
	FirstRootDirSecNum = Bpb.RsvdSecCnt + (Bpb.NumFATs * Bpb.FATSz)+m_Offset;
	FirstDataSector = Bpb.RsvdSecCnt + (Bpb.NumFATs * Bpb.FATSz) + RootDirSectors+m_Offset;
	
	for(i=0; i <= Bpb.FATSz;i++)
	{
		if(RBC_Read(i+FirstFatSecNum,1,(unsigned char *)Cache)==FALSE)return FALSE;
		memcpy((char*)FatCache+i*512, Cache, Bpb.BytsPerSec);
	}
	
	handles.valid=FALSE;
	
/*	if(GetFirstFile((char*)filename)!=FALSE)
	{
	if(fat_open(filename)==FALSE)return FALSE;
	GetFileStat( filename, &stat);
	maxflong=0;
	while(1)
	{
		i=fat_read(buffer,500);
		maxflong+=i;
		if(i==0)
			break;
	}
	fat_close();
	}
	while(GetnextFile((char*)filename)==TRUE){
	GetFileStat( filename, &stat);
	year=getyear(stat.CrtDate);
	year+=1980;
	month=getmonth(stat.CrtDate);
	day=getday(stat.CrtDate);
	hour=gethour(stat.CrtTime);
	minitor=getminitor(stat.CrtTime);
	second=getsecond(stat.CrtTime)*2;
	year1=getyear(stat.WrtDate);//+1980;
	month1=getmonth(stat.WrtDate);
	day1=getday(stat.WrtDate);
	hour1=gethour(stat.WrtTime);
	minitor1=getminitor(stat.WrtTime);
	second1=getsecond(stat.WrtTime)*2;
	if(fat_open(filename)==FALSE)return FALSE;
	maxflong=0;
	while(1)
	{
		i=fat_read(buffer,500);
		maxflong+=i;
		if(i==0)
			break;
	}
	fat_close();
	continue;}*/
	fat_creat("cabcdefghijklmnopqrstuvwxyz1234567890.txt", 0x20);
//	if(fat_open("woaini.c")==FALSE)return FALSE;
	if(!RBC_Read(0x1fd,1,Cache))
		if(!RBC_Read(0x1fd,1,Cache))
			return FALSE;
	for(i=0;i<10;i++)
	{
		memset(buffer,0,4096);
		buffer[0]=i/100+'0';
		buffer[1]=0;

		buffer[2]=(i%100)/10+'0';
		buffer[3]=0;
		
		buffer[4]=i%10+'0';
		buffer[5]=0;
		for(j=0;j<100;j++)strcpy(buffer+5+strlen("中华民族万岁!")*j,"中华民族万岁!");
		strcpy(buffer+5+strlen("中华民族万岁!")*j,"\r\n");
		fat_write(buffer, 7+strlen("中华民族万岁!")*j);
	}
	fat_close();
	for(i=0; i <= Bpb.FATSz;i++)
	{
		memcpy( Cache,(char*)FatCache+i*512, Bpb.BytsPerSec);
		if(!RBC_Write(i+FirstFatSecNum,1,(unsigned char *)Cache,TRUE))
			if(!RBC_Write(i+FirstFatSecNum,1,(unsigned char *)Cache,TRUE))
				return FALSE;
	}
	return TRUE;
}

⌨️ 快捷键说明

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