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

📄 osfile.c

📁 ucos-II+移植到arm+s3c2410的全套代码
💻 C
📖 第 1 页 / 共 4 页
字号:

	ReadFAT2Mem();
	ReadFileRoot2Mem();
	return TRUE;
}

unsigned int NextCluster(unsigned int CurrentCluster)
{
	unsigned int i,j;
	//by threewater ---------------->
/*	if(FAT_TYPE==FAT12){
		ReadPage(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)/SecPerClus,(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)%SecPerClus,databuff);
		if(CurrentCluster%2==0) {
			j=databuff[(CurrentCluster*3/2)%BytesPerSec+1];
			i=databuff[(CurrentCluster*3/2)%BytesPerSec]|(j<<8);
			i&=0x0fff;
			}
		else {
			j=databuff[(CurrentCluster*3/2)%BytesPerSec+1];
			i=databuff[(CurrentCluster*3/2)%BytesPerSec]|(j<<8);
			i=(i>>4);
			}
		return (i);
		}
	else if(FAT_TYPE==FAT16){
		ReadPage(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*2)/BytesPerSec)/SecPerClus,(BootSector+RsdSector+(CurrentCluster*2)/BytesPerSec)%SecPerClus,databuff);
		i=databuff[(CurrentCluster*2)%BytesPerSec+1]*256+databuff[(CurrentCluster*2)%BytesPerSec];
		return (i);
		}
	else if(FAT_TYPE==FAT32){
		ReadPage(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*4)/BytesPerSec)/SecPerClus,(BootSector+RsdSector+(CurrentCluster*4)/BytesPerSec)%SecPerClus,databuff);
		i=databuff[(CurrentCluster*4)%BytesPerSec+3]*256*256*256+databuff[(CurrentCluster*4)%BytesPerSec+2]*256*256+databuff[(CurrentCluster*4)%BytesPerSec+1]*256+databuff[(CurrentCluster*4)%BytesPerSec];
		return (i);
		}*/
	if(CurrentCluster>=SectorofFatSize*BytesPerSec)
		return -1;

	if(FAT_TYPE==FAT12){
		if(CurrentCluster%2==0) {
			j=FileSystemFAT[(CurrentCluster*3/2)+1];
			i=FileSystemFAT[CurrentCluster*3/2]|(j<<8);
			i&=0x0fff;
		}
		else {
			j=FileSystemFAT[(CurrentCluster*3/2)+1];
			i=FileSystemFAT[CurrentCluster*3/2]|(j<<8);
			i=(i>>4);
		}
		return (i);
	}
	else if(FAT_TYPE==FAT16){
		i=FileSystemFAT[(CurrentCluster*2)+1]*256+FileSystemFAT[(CurrentCluster*2)];
		return (i);
	}
	else if(FAT_TYPE==FAT32){
		i=FileSystemFAT[(CurrentCluster*4)+3]*256*256*256+
			FileSystemFAT[(CurrentCluster*4)+2]*256*256+
			FileSystemFAT[(CurrentCluster*4)+1]*256+FileSystemFAT[(CurrentCluster*4)];
		return (i);
	}
	return -1;
	//<-------------------by threewater
}

unsigned int AllocateCluster(unsigned int CurrentCluster)
{
	unsigned int i=0,j=0,s=0;
	unsigned int FAT12_1,FAT12_2;
	switch(FAT_TYPE){
		
		case FAT12:
//				for(j=0;j<(512*SectorofFatSize-3);j+=3) {
				for(j=0;j<FAT_TABLE_SIZE;j+=3) {	//by threewater
					s=FileSystemFAT[j+1];FAT12_1=((s<<8)|FileSystemFAT[j]);FAT12_1&=0x0fff;
					s=FileSystemFAT[j+2];FAT12_2=(s<<8)|FileSystemFAT[j+1];FAT12_2=FAT12_2>>4;
//					printk("FAT12_1:%x,FAT12_2:%x",FAT12_1,FAT12_2);
					if(FAT12_1==0x00) {
						//或者接连两个0,则置第一个零为FF,第二个零的低位为F,且表项号为i*2/3,i为此字节的下标
						FileSystemFAT[j]=0xff;
						FileSystemFAT[j+1]|=0x0f;
//						printk("\nj1:%x,%x%x\n",j,databuff[j],databuff[j+1]);

//						memcpy(Buffer+(BootSector+RsdSector+i)*512,databuff,512);
//						memcpy(Buffer+(BootSector+RsdSector+i+SectorofFatSize)*512,databuff,512);
//						Write2Flash(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,(BootSector+RsdSector+i+SectorofFatSize)%PagePerClus,Buffer);

						FAT12_1=(j+i*512)*8/FAT_TYPE;
						if(CurrentCluster!=0) {//将原来的末簇指向新分配的簇
//							ReadPage(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)/PagePerClus,(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)%PagePerClus,databuff);
//							DPRINTK("\nCurrent:%x ",CurrentCluster);

							if(CurrentCluster%2==0) {
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec+1]&=0xf0;
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec+1]|=((FAT12_1&0x0f00)>>8);
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec]=FAT12_1&0x00ff;
								
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)*512,databuff,512);
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec+SectorofFatSize)*512,databuff,512);
//								Write2Flash(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)/PagePerClus,(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)%PagePerClus,(BootSector+RsdSector+i+SectorofFatSize)%PagePerClus,Buffer);


								}
							else {
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec+1]=(FAT12_1&0x0ff0)>>4;
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec]&=0x0f;
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec]|=((FAT12_1&0x000f)<<4);
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)*512,databuff,512);
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec+SectorofFatSize)*512,databuff,512);
//								Write2Flash(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)/PagePerClus,(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)%PagePerClus,(BootSector+RsdSector+i+SectorofFatSize)%PagePerClus,Buffer);
								}
//							printk("\n");for(s=0;s<64;s++) printk("%x ",databuff[s]);
							
						}
					
						return(FAT12_1);
						}
					else if((FAT12_1!=0x00)&&(FAT12_2==0x00)){
						//某个字节为0,且其前一字节高位为0时,此FAT表项为空,置前一字节高位为F,本字节为FF,且表项号为i*2/3,i为此字节的下标
						FileSystemFAT[j+2]=0xff;
						FileSystemFAT[j+1]|=0xf0;
//						printk("j2:%x,%x%x\n",j,databuff[j+2],databuff[j+1]);

//						memcpy(Buffer+(BootSector+RsdSector+i)*512,databuff,512);
//						memcpy(Buffer+(BootSector+RsdSector+i+SectorofFatSize)*512,databuff,512);
//						Write2Flash(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,(BootSector+RsdSector+i+SectorofFatSize)%PagePerClus,Buffer);

						FAT12_2=(j+i*512+2)*8/FAT_TYPE;
						if(CurrentCluster!=0) {//将原来的末簇指向新分配的簇
//							ReadPage(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)/PagePerClus,(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)%PagePerClus,databuff);
//							DPRINTK("Allocate:%x ",CurrentCluster);
							
							if(CurrentCluster%2==0) {
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec+1]&=0xf0;
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec+1]|=((FAT12_2&0x0f00)>>8);
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec]=FAT12_2&0x00ff;
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)*512,databuff,512);
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec+SectorofFatSize)*512,databuff,512);
//								Write2Flash(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)/PagePerClus,(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)%PagePerClus,(BootSector+RsdSector+i+SectorofFatSize)%PagePerClus,Buffer);
								}
							else {
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec+1]=(FAT12_2&0x0ff0)>>4;
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec]&=0x0f;
								FileSystemFAT[(CurrentCluster*3/2)%BytesPerSec]|=((FAT12_2&0x000f)<<4);
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)*512,databuff,512);
//								memcpy(Buffer+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec+SectorofFatSize)*512,databuff,512);
//								Write2Flash(Begin_Cluster+(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)/PagePerClus,(BootSector+RsdSector+(CurrentCluster*3/2)/BytesPerSec)%PagePerClus,(BootSector+RsdSector+i+SectorofFatSize)%PagePerClus,Buffer);
								}
//							printk("\n");for(s=0;s<64;s++) printk("%x ",databuff[s]);
							
						}
						
						return(FAT12_2);
					}
				}
			break;
		case FAT16:
			for(i=0;i<SectorofFatSize;i++)	{
				ReadPage(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,databuff);
		//		for(s=0;s<512;s++) printk("%x",databuff[0x80+s]);
				for(j=0;j<512/(FAT_TYPE/8);j++) {
					if((databuff[j*(FAT_TYPE/8)]==0x00)&&(databuff[j*(FAT_TYPE/8)+1]==0x00)) {
						databuff[j*(FAT_TYPE/8)]=0xff;
						databuff[j*(FAT_TYPE/8)+1]=0xff;

//						memcpy(Buffer+(BootSector+RsdSector+i)*512,databuff,512);
//						Write2Flash(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,(BootSector+RsdSector+i)%PagePerClus,Buffer);
						
						return(j+i*512/(FAT_TYPE/8));
						}
					}
				}
			break;
		case FAT32:
			for(i=0;i<SectorofFatSize;i++)	{
				ReadPage(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,databuff);
		//		for(s=0;s<512;s++) printk("%x",databuff[0x80+s]);
				for(j=0;j<512/(FAT_TYPE/8);j++) {
					if((databuff[j*(FAT_TYPE/8)]==0x00)&&(databuff[j*(FAT_TYPE/8)+1]==0x00)&&(databuff[j*(FAT_TYPE/8)+2]==0x00)&&(databuff[j*(FAT_TYPE/8)+3]==0x00)) {
						databuff[j*(FAT_TYPE/8)]=0xff;
						databuff[j*(FAT_TYPE/8)+1]=0xff;
						databuff[j*(FAT_TYPE/8)+2]=0xff;
						databuff[j*(FAT_TYPE/8)+3]=0xff;

//						memcpy(Buffer+(BootSector+RsdSector+i)*512,databuff,512);
//						Write2Flash(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,(BootSector+RsdSector+i)%PagePerClus,Buffer);
						
						return(j+i*512/(FAT_TYPE/8));
						}
					}
				}
			
			break;
	default:break;
	}
	return 0;
}

/*
unsigned int AllocateCluster(OSFILE *pfile)
{
	unsigned int i,j,s;
	unsigned int FAT12_1,FAT12_2;
	switch(FAT_TYPE){
		
		case FAT12:
				//by threewater--------------------->
				for(j=0;j<(512-3)*SectorofFatSize;j+=3) {
					s=FileSystemFAT[j+1];	FAT12_1=((s<<8)|FileSystemFAT[j]);	FAT12_1&=0x0fff;
					s=FileSystemFAT[j+2];	FAT12_2=(s<<8)|FileSystemFAT[j+1];	FAT12_2=FAT12_2>>4;
//					DPRINTK("FAT12_1:%x,FAT12_2:%x",FAT12_1,FAT12_2);
					if(FAT12_1==0x00) {
						//或者接连两个0,则置第一个零为FF,第二个零的低位为F,且表项号为i*2/3,i为此字节的下标
						FileSystemFAT[j]=0xff;
						FileSystemFAT[j+1]|=0xf;//by threewater
//						FileSystemFAT[j+1]=0x0f;
//						DPRINTK("\nj1:%x,%x%x\n",j,databuff[j],databuff[j+1]);
						return(j*8/FAT_TYPE);
					}
					else if((FAT12_1!=0x00)&&(FAT12_2==0x00)){
						//某个字节为0,且其前一字节高位为0时,此FAT表项为空,置前一字节高位为F,本字节为FF,且表项号为i*2/3,i为此字节的下标
						FileSystemFAT[j+2]=0xff;
						FileSystemFAT[j+1]|=0xf0;
						return((j+2)*8/FAT_TYPE);
					}
				}
				//<---------------------by threewater
			break;
		case FAT16:
			for(i=0;i<SectorofFatSize;i++)	{
				ReadPage(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,databuff);
		//		for(s=0;s<512;s++) DPRINTK("%x",databuff[0x80+s]);
				for(j=0;j<512/(FAT_TYPE/8);j++) {
					if((databuff[j*(FAT_TYPE/8)]==0x00)&&(databuff[j*(FAT_TYPE/8)+1]==0x00)) {
						databuff[j*(FAT_TYPE/8)]==0xff;
						databuff[j*(FAT_TYPE/8)+1]==0xff;

						memcpy(pfile->Buffer+(BootSector+RsdSector+i)*512,databuff,512);
						Write2Flash(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,(BootSector+RsdSector+i)%PagePerClus,pfile->Buffer);
						
						return(j+i*512/(FAT_TYPE/8));
						}
					}
				}
			break;
		case FAT32:
			for(i=0;i<SectorofFatSize;i++)	{
				ReadPage(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,databuff);
		//		for(s=0;s<512;s++) DPRINTK("%x",databuff[0x80+s]);
				for(j=0;j<512/(FAT_TYPE/8);j++) {
					if((databuff[j*(FAT_TYPE/8)]==0x00)&&(databuff[j*(FAT_TYPE/8)+1]==0x00)&&(databuff[j*(FAT_TYPE/8)+2]==0x00)&&(databuff[j*(FAT_TYPE/8)+3]==0x00)) {
						databuff[j*(FAT_TYPE/8)]==0xff;
						databuff[j*(FAT_TYPE/8)+1]==0xff;
						databuff[j*(FAT_TYPE/8)+2]==0xff;
						databuff[j*(FAT_TYPE/8)+3]==0xff;

						memcpy(pfile->Buffer+(BootSector+RsdSector+i)*512,databuff,512);
						Write2Flash(Begin_Cluster+(BootSector+RsdSector+i)/PagePerClus,(BootSector+RsdSector+i)%PagePerClus,(BootSector+RsdSector+i)%PagePerClus,pfile->Buffer);
						
						return(j+i*512/(FAT_TYPE/8));
						}
					}
				}
			
			break;
	default:break;
	}
	return 0;
}
*/

void TestFAT_READ(char filename[])
{
	OSFILE *file2;
	int i;
//	char LineString[100]={0,};

	file2=OpenOSFile(filename,FILEMODE_READ);
	if(file2!=NULL)
		for(i=0;i<16;i++) DPRINTK("%c",file2->Buffer[i]);
	/*
	j=LineReadOSFile(file2, LineString);
	DPRINTK("LINE1\n");
	for(i=0;i<j;i++) DPRINTK("%c",LineString[i]);
	j=LineReadOSFile(file2, LineString);
	DPRINTK("LINE2\n");
	for(i=0;i<j;i++) DPRINTK("%c",LineString[i]);
	j=LineReadOSFile(file2, LineString);
	DPRINTK("LINE3\n");
	for(i=0;i<j;i++) DPRINTK("%c",LineString[i]);

	SeekOSFile(file2,GetPosOSFile(file2));
	j=LineReadOSFile(file2, LineString);
	DPRINTK("LINE4\n");
	for(i=0;i<j;i++) DPRINTK("%c",LineString[i]);
	j=LineReadOSFile(file2, LineString);
	DPRINTK("LINE5\n");
	for(i=0;i<j;i++) DPRINTK("%c",LineString[i]);
	j=LineReadOSFile(file2, LineString);
	DPRINTK("LINE6\n");
	for(i=0;i<j;i++) DPRINTK("%c",LineString[i]);
	*/
}
void TestFAT_DELETE(char filename[])
{
	DeleteOSFile(filename);

}
void TestFAT_CREATE(char filename[])
{
	OSFILE *file2;
	int i;
	file2=OpenOSFile(filename,FILEMODE_WRITE);
	for(i=0;i<1024;i++) file2->Buffer[i]='A'+i/40;
	for(i=0;i<1024;i+=40) {file2->Buffer[i]=13;file2->Buffer[i+1]=10;}
	CloseOSFile(file2);

}
void TestFAT_COPY(char filename1[],char filename2[])
{
	OSFILE *file1,*file2;
	int i;
	file1=OpenOSFile(filename1,FILEMODE_READ);
	file2=OpenOSFile(filename2,FILEMODE_WRITE);
	for(i=0;i<file1->filesize;i++) file2->Buffer[i]=file1->Buffer[i];
	CloseOSFile(file1);
	CloseOSFile(file2);
}

//获得文件分区信息,可以为空指针,返回块大小
int GetFS_Info(int *TotalSec, int *unusedSec, int *badSec)
{
	int unused=0, bad=0,i;
	unsigned int FAT12_1, FAT12_2, s;
	if(TotalSec)
		*TotalSec=TotalSector;

	if(!unusedSec && !badSec)
		return BytesPerSec;

	//计算未使用的块和坏块
	for(i=0;i<(TotalSector*3/2/SecPerClus-3); i+=3){
		s=FileSystemFAT[i+1];
		FAT12_1=((s<<8)|FileSystemFAT[i]);
		FAT12_1&=0x0fff;
		s=FileSystemFAT[i+2];
		FAT12_2=(s<<8)|FileSystemFAT[i+1];
		FAT12_2=FAT12_2>>4;

		if(FAT12_1==0)	//unused
			unused++;
		if(FAT12_2==0)	//unused
			unused++;
	}

	if(unusedSec)
		*unusedSec=unused*SecPerClus;

	if(badSec)
		*badSec=bad;

	return BytesPerSec;
}

#else	//for yaffs file system

int initOSFile()
{
	ucos2_malloc_init();

	yaffs_StartUp();

	return TRUE;
}

#endif	//#if (USE_YAFFS==0)

⌨️ 快捷键说明

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