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

📄 main.c

📁 详细介绍了关于CF卡的存储结构
💻 C
📖 第 1 页 / 共 5 页
字号:
			return -1;
		}
		if(ReadOneSec(file_record_sec)==0)
		{
#ifdef DEBUG
			printf("read error\n");
#endif
			return -1;
		}
		data_buf[file_record_pos*32]=0xE5;
#ifdef DEBUG
		printf("file record sec=%x\n",(int)file_record_sec);
#endif
		if(WriteOneSec(data_buf,file_record_sec)==0)
		{
#ifdef DEBUG
			printf("write error\n");
#endif
			return -1;
		}
	}
	return 1;
}


// current version write one file to root directory
//name is 13 bytes use 8.3 format,unused bytes input 0x20
#ifdef ROOT_FILE
char CFWriteFileInRoot(char *name,unsigned char *file_data)
{
	static xdata struct sRootDir dir;
	bit success=0;
	static unsigned int sector;
	unsigned int i;
	static xdata unsigned int fdt_pos,record_pos;
	success=SerchFreeFAT(&sector,1,1);		//find free fat table
	if(success==1)
	{
		printf("\ni=%x,sector=%x\n",i,sector);
	}
	if(success==0)
	{
		return -1;	//Fat table full
	}
	if(SerchFreeFDT(&fdt_pos,&record_pos,0)==0)	//find free fdt table	
		return -2;	//FDT table full
	printf("fdt pos=%x,record pos=%x\n",fdt_pos,record_pos);
	if(ReadOneSec(fdt_pos)==0)					
		return -3;
	for(i=0;i<32;i++)							//empty Fdt record struct
	{
		((unsigned char *)(&dir))[i]=0;
	}
	for(i=0;i<13;i++)							//fill file name to Fdt record
	{
		dir.file_name[i]=name[i];
	}
	dir.file_attribute=0x20;
	dir.file_size[0]=0xf0;
	dir.first_clust[0]=(sector)%256;
	dir.first_clust[1]=(sector)/256;
	for(i=0;i<32;i++)							//copy one Fdt Record(32 bytes) to sector
	{
		data_buf[record_pos*32+i]=((unsigned char*)(&dir))[i];
	}
	if(WriteOneSec(data_buf,fdt_pos)==0)
	{
		return -3;		//write fdt error
	}
	if(ReadOneSec((sector*2)/512+1)==0)		//read one sector of Fat table
		return -3;
	for(i=(sector*2)%512;i<(sector*2)%512+2;i++)
	{
		data_buf[i]=0xff;
	}
	if(WriteOneSec(data_buf,(sector*2)/512+1)==0)
	{
		return -3;		//write fdt error
	}

	for(i=0;i<512;i++)
	{
		data_buf[i]=0;
	}
	for(i=0;i<240;i++)
	{
		data_buf[i]=*file_data;
	}
	if(WriteOneSec(data_buf,(sector-2)*bootinfo.sec_per_clust
				+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
	{
		return -3;
	}
	return 1;
}
#endif

//function:Create a file 
//input: 
//	file name: with 8.3 format,use 0x20 to fill empty space;
//	mode: use to set file mode 
//			1 creat new file,
//			2 open a exist file with add mode;
//	dir_name: with 8.3 format,use 0x20 to fill empty space,if dir_name=='8',means in root directory 
//return: file's handle,return 0xffff if operation falled
unsigned int CFOpenFile(unsigned int dir_start_sec,char* file_name, unsigned char mode)
{
	unsigned int temp;
	static xdata struct sRootDir file;
	bit success=0;
	static xdata unsigned int sector;
	unsigned int i;
	static xdata unsigned int fdt_pos,record_pos;
	static xdata unsigned int next_clustor,next_sec,current_sec;
	xdata unsigned int current_sec_fat;
	xdata unsigned char out_count;
	for(i=0;i<sizeof(s_fileinfo);i++)
	{
		((unsigned char*)(&s_fileinfo))[i]=0;
	}
	if(mode==2)
	{
		sector=CFFindFileInDir(file_name,dir_start_sec,&fdt_pos,&record_pos);
#ifdef DEBUG
		printf("mode 2 sec=%x\n",sector);
#endif
		if(sector==0xffff)
		{
			return 0xffff;
		}
		s_fileinfo.in_fat_sec=(sector*2)/512+1; 
		s_fileinfo.file_record_sector=fdt_pos;
		s_fileinfo.file_pos_in_sector=record_pos;
#ifdef DEBUG
		printf("fdt pos=%x\n",fdt_pos);
		printf("record pos=%x\n",record_pos);
#endif
		if(ReadOneSec(fdt_pos)==0)
		{
			return 0xffff;
		}
		memcpy(&file,data_buf+record_pos*32,32);
		s_fileinfo.size=file.file_size[3]*16777216+file.file_size[2]*65536+file.file_size[1]*256+file.file_size[0];
#ifdef DEBUG
		printf("size=%x,%u,%l\n",s_fileinfo.size,s_fileinfo.size,s_fileinfo.size);
		printf("sector=%x\n",sector);
#endif
		s_fileinfo.start_clustor=FatLinkEnd((unsigned int)sector,0xffff);
#ifdef DEBUG
		printf("finish link s\n");
#endif
		if(s_fileinfo.start_clustor==0xffff)
		{
			return 0xffff;
		}
		s_fileinfo.current_sector=((s_fileinfo.size)%(bootinfo.sec_per_clust*512))/512;
		s_fileinfo.current_in_sector_pos=(s_fileinfo.size%(bootinfo.sec_per_clust*512))%512;
		if(s_fileinfo.current_sector==0 && s_fileinfo.current_in_sector_pos==0 && s_fileinfo.size>0)
		{
#ifdef DEBUG
			printf("in1\n");
#endif
			success=SerchFreeFAT(&sector,1,1);		//find free fat table
			if(success==1)
			{
//				s_fileinfo.in_fat_sec=(sector*2)/512+1;
#ifdef DEBUG
				printf("\nsector=%x\n",sector); 
#endif
			}
			if(success==0)
			{
				return 0xffff;	//Fat table full
			}
			if(ReadOneSec(s_fileinfo.start_clustor*2/512+1)==0)
			{
				return 0xffff;
			}
			data_buf[s_fileinfo.start_clustor*2%512]=sector%256;
			data_buf[s_fileinfo.start_clustor*2%512+1]=sector/256;
			if(WriteOneSec(data_buf,s_fileinfo.start_clustor*2/512+1)==0)
			{
				return 0xffff;
			}
			s_fileinfo.start_clustor=sector;
			if(ReadOneSec((sector*2)/512+1)==0)		//read one sector in Fat table area
			{
				return 0xffff;
			}
			for(i=(sector*2)%512;i<(sector*2)%512+2;i++)	//wrire fat table
			{
				data_buf[i]=0xff;
			}
			if(WriteOneSec(data_buf,(sector*2)/512+1)==0)
			{
				return 0xffff;		//write fdt error
			}
			buf_pos=0;
			return file.first_clust[1]*256+file.first_clust[0];  
		}
		if(s_fileinfo.current_in_sector_pos!=0)
		{
#ifdef DEBUG
			printf("in2\n");
#endif
			temp=(s_fileinfo.start_clustor-2)*bootinfo.sec_per_clust
						+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512)
						+s_fileinfo.current_sector;
#ifdef DEBUG
			printf("temp=%x\n",temp);
#endif
			if(ReadOneSec((s_fileinfo.start_clustor-2)*bootinfo.sec_per_clust
						+(bootinfo.fat_num*bootinfo.fat_size+bootinfo.root_dir_size*32/512+1)
						+s_fileinfo.current_sector)==0)
			{
				return 0xffff;
			}
			for(i=0;i<512;i++)
			{
				write_buf[i]=data_buf[i];
			}
//			memcpy(write_buf,data_buf,512);
			buf_pos=s_fileinfo.current_in_sector_pos;
			return file.first_clust[1]*256+file.first_clust[0];

		}
		if(file.file_size==0)
		{
#ifdef DEBUG
			printf("in3\n");
#endif
			buf_pos=0;
			return file.first_clust[1]*256+file.first_clust[0];  
		}
		buf_pos=0;
		return file.first_clust[1]*256+file.first_clust[0];  
	}
	if(mode==1)									//greate new file
	{
		current_sec=dir_start_sec;
		current_sec_fat=current_sec-(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512);
		current_sec_fat=current_sec_fat/bootinfo.sec_per_clust+2; 
		for(out_count=0;(out_count<20)&&(SerchFreeFDT(&fdt_pos,&record_pos,current_sec)==0);out_count++)
		{
				if(ReadOneSec((current_sec_fat*2)/512+1)==0)
				{
#ifdef DEBUG
					printf("1\n");
#endif
					return 0xffff;
				}
				next_clustor=data_buf[(current_sec_fat*2)%512+1]*256+data_buf[(current_sec_fat*2)%512];
				if(next_clustor==0xffff)
				{
					if(SerchFreeFAT(&next_sec,1,1)==0)
					{
#ifdef DEBUG
						printf("2\n");
#endif
						return 0xffff;					//no free fat record
					}
					data_buf[(current_sec_fat*2)%512+1]=next_sec/256;
					data_buf[(current_sec_fat*2)%512]=next_sec%256;
					if(WriteOneSec(data_buf,(current_sec_fat*2)/512+1)==0)
					{
#ifdef DEBUG
						printf("3\n");
#endif
						return 0xffff;
					}
					if(ReadOneSec((next_sec*2)/512+1)==0)
					{
#ifdef DEBUG
						printf("4\n");
#endif
						return 0xffff;
					}
					data_buf[(next_sec*2)%512+1]=0xff;
					data_buf[(next_sec*2)%512]=0xff;
					if(WriteOneSec(data_buf,(next_sec*2)/512+1)==0)
					{
#ifdef DEBUG
						printf("5\n");
#endif
						return 0xffff;
					}
					if(EmptyDirFDT((next_sec-2)*bootinfo.sec_per_clust
						+(bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512+1))==-1)
					{
#ifdef DEBUG
						printf("6\n");
#endif
						return 0xffff;
					}
					current_sec_fat=next_sec;
					current_sec=(next_sec-2)*bootinfo.sec_per_clust
						+(bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512+1);   
/*
					if(SerchFreeFDT(&fdt_pos,&record_pos,(next_sec-2)*bootinfo.sec_per_clust
						+(bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512+1))==0)
					{
						return 0xffff;
					}
					else
					{
						out_count=30;
#ifdef DEBUG
						printf("find\n");
#endif
					}
*/
				}
				else
				{
#ifdef DEBUG
					printf("7\n");
#endif
					current_sec_fat=next_clustor;
					current_sec=(next_clustor-2)*bootinfo.sec_per_clust
						+(bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512+1);   
				}
		}
		if(out_count==20)			//means no free fdt record position
		{
#ifdef DEBUG
			printf("8\n");
#endif
			return 0xffff;	
		}

		success=SerchFreeFAT(&sector,1,1);		//find free fat table
		if(success==1)
		{
			s_fileinfo.in_fat_sec=(sector*2)/512+1;
#ifdef DEBUG
			printf("\nsector=%x\n",sector); 
#endif
		}
		if(success==0)
		{
			return 0xffff;	//Fat table full
		}

#ifdef DEBUG
		printf("fdt pos=%x,record pos=%x\n",fdt_pos,record_pos);
#endif
		if(ReadOneSec(fdt_pos)==0)					//read current Fdt Table,and save to buffer
			return 0xffff;
		s_fileinfo.file_record_sector=fdt_pos;		//save opened file infomation
		s_fileinfo.file_pos_in_sector=record_pos;
		s_fileinfo.start_clustor=sector;
//		s_fileinfo.start_clustor=fdt_pos;
		for(i=0;i<32;i++)
		{
			((unsigned char *)(&file))[i]=0;
		}
		for(i=0;i<11;i++)
		{
			((unsigned char*)(&file))[i]=file_name[i];	//put file name
		}
//		file.file_attribute=ARCHIVE|READONLY;
		file.file_attribute=ARCHIVE;
		file.first_clust[0]=(sector)%256;
		file.first_clust[1]=(sector)/256;
		file.file_size[0]=0x0;
		file.file_size[1]=0x0;
		file.file_size[2]=0;
		file.file_size[3]=0;
		for(i=0;i<32;i++)			//save file record to FDT table,file size field do not write 
		{
			data_buf[record_pos*32+i]=((unsigned char*)(&file))[i];
		}
		if(WriteOneSec(data_buf,fdt_pos)==0)
		{
			return 0xffff;		//write fdt error
		}
//#ifdef WRITE_IN_OPEN
		if(ReadOneSec((sector*2)/512+1)==0)		//read one sector in Fat table area
		{
			return 0xffff;
		}
		for(i=(sector*2)%512;i<(sector*2)%512+2;i++)	//wrire fat table
		{
			data_buf[i]=0xff;
		}
		if(WriteOneSec(data_buf,(sector*2)/512+1)==0)
		{
			return 0xffff;		//write fdt error
		}
//#endif
		s_fileinfo.current_sector=0;
		s_fileinfo.current_in_sector_pos=0;
		s_fileinfo.pos_sector_in_clustor=0; 
		s_fileinfo.size=0;
		buf_pos=0;
#ifdef WRITE_IN_OPEN
		for(i=0;i<512;i++)
		{
			data_buf[i]=0;
		}
		for(i=0;i<240;i++)
		{
			data_buf[i]='A';
		}
		if(WriteOneSec(data_buf,(sector-2)*bootinfo.sec_per_clust
					+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
		{
			return 0xffff;
		}
#endif
	}
	return (sector-2)*bootinfo.sec_per_clust
		   +(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512);
}

//Function:		read data from a opened file
char CFReadFile(unsigned char* file_data,unsigned int len,unsigned long start_pos,unsigned int* read)
{
	xdata struct sSubDir s_file;
	xdata unsigned int clustor_num;
	xdata unsigned int in_clus_sec;
	xdata unsigned int in_sec_pos;
//	xdata unsigned int next_clus;
	xdata unsigned int current_clus;
	xdata unsigned int i,j;
	xdata unsigned int read_sec_count=0;
	xdata unsigned int read_clus_count=0;
	xdata unsigned int temp;
	xdata unsigned long start_clus=0;
	xdata long lenth;
//	xdata unsigned int read_count;
	if(ReadOneSec(s_fileinfo.file_record_sector)==0)
	{
		return -1;
	}
	memcpy((unsigned char*)(&s_file),data_buf+s_fileinfo.file_pos_in_sector*32,32);
	current_clus=s_file.first_clust[1]*256+s_file.first_clust[0];
	lenth=len;
	start_clus=start_pos;
	j=0;
	*read=lenth;
	if(start_pos+len>s_fileinfo.size)
	{
		lenth=s_fileinfo.size-start_pos;
		*read=lenth;
		if(lenth<=0)
		{
			return -2;
		}
	}
	while(lenth>0)
	{
		clustor_num=(start_clus/512)/bootinfo.sec_per_clust;
		in_sec_pos=start_clus%512;
		in_clus_sec=start_clus%(512*bootinfo.sec_per_clust)/512;
		temp=FatLinkEnd(current_clus,clustor_num);
		if(temp==0xffff)
		{
			return -1;
		}
#ifdef DEBUG_READ
		printf("\ncurrent clus=%d,start clus=%d\n",(int)current_clus,(int)start_clus);
		printf("temp=%d,in clus sec=%x,in sec pos=%x\n",(int)temp,in_clus_sec,in_sec_pos);
#endif
//		current_clus=temp;
//		if(ReadOneSec((current_clus-2)*bootinfo.sec_per_clust
//			+(bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512+1)+in_clus_sec)==0)
		if(ReadOneSec((temp-2)*bootinfo.sec_per_clust
			+(bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512+1)+in_clus_sec)==0)
		{
			return -1;
		}
#ifdef DEBUG_READ
		printf("j=%d,len=%d\n",(int)j,(int)lenth);
#endif
		for(i=0;(i<512-in_sec_pos)&&lenth>0;i++)
		{
			file_data[i+j]=data_buf[i+in_sec_pos];
			lenth--;
//			putchar((char)data_buf[i]);
		}
		j=j+i;
		if(lenth<=0)
		{

⌨️ 快捷键说明

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