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

📄 main.c

📁 详细介绍了关于CF卡的存储结构
💻 C
📖 第 1 页 / 共 5 页
字号:
	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 -1;
	}
	return 1;
}

//char PutData(unsigned char* file_data,unsigned int data_len)
char CFWriteFile(unsigned char* file_data,unsigned int data_len)
{
	xdata unsigned int i;
	xdata int un_write_len=0;
//	xdata unsigned int current_file_pt;
	static xdata unsigned int next_clustor;
//    xdata unsigned int sector;
#ifdef DEBUG
	printf("\nstart clustor=%x\n",s_fileinfo.start_clustor);
	printf("current sec=%d\n",(int)s_fileinfo.current_sector);
	printf("in sec pos=%x\n",s_fileinfo.current_in_sector_pos);
	printf("size=%x,%u,%l\n",s_fileinfo.size,s_fileinfo.size,s_fileinfo.size);
	printf("buf pos=%d,%u,%d\n",(int)buf_pos,(long)buf_pos,buf_pos);
#endif
	if(data_len<=512)
	{
#ifdef DEBUG
		printf("buf_pos=%d\n",(int)buf_pos);
#endif
		un_write_len=data_len;
		for(i=buf_pos;(i<512)&&(un_write_len>0);i++)
		{
			write_buf[i]=file_data[i-buf_pos];
			un_write_len--;
		}
		s_fileinfo.size=s_fileinfo.size+i-buf_pos;
		buf_pos=i;
#ifdef DEBUG
		printf("buf_pos=%d\n",(int)buf_pos);
#endif
		if(buf_pos>=512)
		{
#ifdef DEBUG
			printf("start w\n");
#endif
			if(WriteOneSec(write_buf,(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)==0)
			{
#ifdef DEBUG
				printf("www\n");
#endif
				return -1;
			}
//			s_fileinfo.size=s_fileinfo.size+buf_pos;
			buf_pos=0;
			s_fileinfo.current_sector++;
#ifdef DEBUG
		printf("_in_buf_current_sector=%d\n",(int)s_fileinfo.current_sector);
#endif
#ifdef DEBUG
		printf("_in_buf_pos=%d\n",(int)buf_pos);
#endif
		}
		if(un_write_len>0)
		{
#ifdef DEBUG
			printf("a\n");
#endif
			for(i=0;i<un_write_len;i++)
			{
				write_buf[i]=file_data[i+(data_len-un_write_len)];
				buf_pos++;
			}

#ifdef DEBUG
		printf("un buf_pos=%d\n",(int)buf_pos);
#endif
			s_fileinfo.size=s_fileinfo.size+i;  
		}
		if(s_fileinfo.current_sector>=bootinfo.sec_per_clust)		//means one clustor written
		{
#ifdef DEBUG
			printf("b\n");
#endif
			s_fileinfo.current_sector=0;
//			if(SerchFreeFAT(&next_clustor,1,s_fileinfo.in_fat_sec)==0)		//search Fat table from last sector
			if(SerchFreeFAT(&next_clustor,1,1)==0)		//search Fat table from last sector
			{
				putchar('n');
#ifdef DEBUG
				printf("no free fat\n");
#endif
				return -1;
			}
#ifdef DEBUG
				printf("current clustor=%x\n",s_fileinfo.start_clustor);
				printf("next clustor=%x\n",next_clustor);
#endif
			if(ReadOneSec((s_fileinfo.start_clustor*2)/512+1)==0)
			{
				return -1;
			}
			data_buf[(s_fileinfo.start_clustor*2)%512]=next_clustor%256;
			data_buf[(s_fileinfo.start_clustor*2)%512+1]=next_clustor/256;
			if(WriteOneSec(data_buf,(s_fileinfo.start_clustor*2)/512+1)==0)
			{
				return -1;		//write fdt error
			}

			s_fileinfo.start_clustor=next_clustor; 
			s_fileinfo.in_fat_sec=(next_clustor*2)/512+1;

			if(ReadOneSec((s_fileinfo.start_clustor*2)/512+1)==0)		//read one sector in Fat table area
			{
				return -1;
			}
			for(i=(s_fileinfo.start_clustor*2)%512;i<(s_fileinfo.start_clustor*2)%512+2;i++)	//wrire fat table
			{
				data_buf[i]=0xff;
			}
			if(WriteOneSec(data_buf,(s_fileinfo.start_clustor*2)/512+1)==0)
			{
				return -1;		//write fdt error
			}
//			for(buf_pos=0;buf_pos<unuse_len;buf_pos++)
//			{
//				write_buf[buf_pos]=file_data[data_len-unuse_len+buf_pos];
//			}
//			s_fileinfo.size=s_fileinfo.size+buf_pos;
		}
		return 1;
	}
	else
	{
		return -1;		//data large then 512 bytes
	}
	//the data is more then one sector
/*
	if(data_len>512)
	{
		printf("c\n");
		if(s_fileinfo.current_in_sector_pos!=0)		//the target sector is used
		{
			if(ReadOneSec(((s_fileinfo.start_clustor+s_fileinfo.current_sector)/2-2)*bootinfo.sec_per_clust
				+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
			{
				return -1;
			}
			for(i=0;i<512-s_fileinfo.current_in_sector_pos;i++)
			{
				data_buf[s_fileinfo.current_in_sector_pos+i]=file_data[i];
			}
			if(WriteOneSec(data_buf,((s_fileinfo.start_clustor+s_fileinfo.current_sector)/2-2)*bootinfo.sec_per_clust
				+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
			{
				return -1;
			}
			s_fileinfo.current_sector++;
			current_file_pt=512-s_fileinfo.current_in_sector_pos;
			for(i=0;i<(data_len-current_file_pt)/512;i++)
			{
				if(WriteOneSec(file_data+current_file_pt+i*512,
						((s_fileinfo.start_clustor+s_fileinfo.current_sector+i+1)/2-2)
						*bootinfo.sec_per_clust
						+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
				{
					return -1;
				}
				s_fileinfo.current_sector++; 
//				if(s_fileinfo.current_sector%4==0)
//				{
//					s_fileinfo.current_in_sector_pos=s
			}

		}
		if(data_len-s_fileinfo.current_in_sector_pos>512*bootinfo.sec_per_clust)
		{
			if(WriteOneSec(file_data,(s_fileinfo.current_sector/2-2)*bootinfo.sec_per_clust
					+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
			{
				return -1;			//Write file error
			}
			if(ReadOneSec(s_fileinfo.current_sector/512+1)==0)		//read one sector in Fat table area
			{
				return 0xffff;
			}
			for(i=1;i<bootinfo.fat_size;i++)
			{
				SerchFreeFAT(&next_clustor,1,i);
			}
			for(i=s_fileinfo.current_sector%512;i<s_fileinfo.current_sector%512+2;i++)	//wrire fat table
			{
				data_buf[i]=0xff;
			}
			if(WriteOneSec(data_buf,s_fileinfo.current_in_sector_pos/512+1)==0)
			{
				return 0xffff;		//write fdt error
			}
			for(i=0;i<(data_len-512)/512;i++)
			{
//				data_buf[i]=
			}
		}
//		s_fileinfo.
//		s_fileinfo.current_sector
	}
	else		//the data to be written is less then one clustor
	{
		current_file_pt=0;
		if(s_fileinfo.current_in_sector_pos>0)		//the target sector is used
		{
			if(ReadOneSec(((s_fileinfo.start_clustor+s_fileinfo.current_sector)/2-2)*bootinfo.sec_per_clust
				+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
			{
				return -1;
			}
			for(i=0;i<512-s_fileinfo.current_in_sector_pos;i++)
			{
				data_buf[s_fileinfo.current_in_sector_pos+i]=file_data[i];
			}
			if(WriteOneSec(data_buf,((s_fileinfo.start_clustor+s_fileinfo.current_sector)/2-2)*bootinfo.sec_per_clust
				+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
			{
				return -1;
			}
			s_fileinfo.current_sector++;
			current_file_pt=512-s_fileinfo.current_in_sector_pos;
			for(i=0;i<data_len-current_file_pt;i++)
			{
				data_buf[i]=file_data[current_file_pt+i];
			}
			if(WriteOneSec(data_buf,((s_fileinfo.start_clustor+s_fileinfo.current_sector+i+1)/2-2)
					*bootinfo.sec_per_clust
					+(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))==0)
			{
				return -1;
			}
			s_fileinfo.current_sector++;
			if((s_fileinfo.current_sector+1)%4==0)
			{
				if(ReadOneSec((s_fileinfo.start_clustor+s_fileinfo.current_sector/4)/512+1)==0)		//read one sector of Fat table
					return -3;
				for(i=sector%512;i<sector%512+2;i++)
				{
					data_buf[i]=0xff;
				}
				if(WriteOneSec(data_buf,sector/512+1)==0)
				{
					return -3;		//write fdt error
				}

			}

		}
	}
*/
}

//Function: use it after delete a file,free used Fat record
//input:	first clustor number of the file
//return:	-1 if false; 1 if success
char DelFatRecord(unsigned int first_clustor)
{
	xdata unsigned int i;//,j;
	xdata unsigned int count=0;
	static xdata unsigned int last_fat_sector=0xffff;
	xdata unsigned int next_clustor;
	xdata unsigned int current_clustor;
	bit finish=0;
	last_fat_sector=0xffff;
	current_clustor=first_clustor;
	for(count=0;(count<bootinfo.fat_size*256)&&(finish==0);count++)
	{
		if(current_clustor*2/512!=last_fat_sector)
		{
			last_fat_sector=current_clustor*2/512;
			if(ReadOneSec(current_clustor*2/512+1)==0)
			{
				return -1;
			}
			next_clustor=data_buf[(current_clustor*2)%512]+data_buf[(current_clustor*2)%512+1]*256;
			for(i=current_clustor*2%512;i<current_clustor*2%512+2;i++)
			{
				data_buf[i]=0x00;
			}
			if(WriteOneSec(data_buf,current_clustor*2/512+1)==0)
			{
				return -1;
			}
			if(next_clustor==0xffff)
			{
//				if(WriteOneSec(data_buf,current_clustor*2/512+1)==0)
//				{
//					return -1;
//				}
				finish=1;
				return 1;
			}
			else
			{
				current_clustor=next_clustor;
			}
		}
		else
		{
			next_clustor=data_buf[(current_clustor*2)%512]+data_buf[(current_clustor*2)%512+1]*256;
			for(i=current_clustor*2%512;i<current_clustor*2%512+2;i++)
			{
				data_buf[i]=0x00;
			}
			if(next_clustor==0xffff)
			{
				if(WriteOneSec(data_buf,current_clustor*2/512+1)==0)
				{
					return -1;
				}
				finish=1;
				return 1;
			}
			if(next_clustor*2/512!=last_fat_sector)
			{
				if(WriteOneSec(data_buf,current_clustor*2/512+1)==0)
				{
					return -1;
				}
			}
#ifdef DEBUG
			printf("n c=%x\n",next_clustor);
#endif
			current_clustor=next_clustor;
		}
	}
}

//Function: search the last clustor position in Fat table
//input:	first clustor
//return:	0xffff false; else return the end clustor 
unsigned int FatLinkEnd(unsigned int start_clustor,unsigned int clus_num)
{
	xdata unsigned int count=0;
	xdata unsigned int last_fat_sector=0xffff;
	xdata unsigned int next_clustor;
	xdata unsigned int current_clustor;
	bit finish=0;
//	last_fat_sector=0xffff;
	current_clustor=(unsigned int)start_clustor;
#ifdef DEBUG_LINK
	printf("0 start clustor=%x",(unsigned int)start_clustor);
	printf("0 current clustor=%x",(unsigned int)current_clustor);
#endif
	for(count=0;(count<bootinfo.fat_size*256)&&(finish==0);count++)
	{
		if(current_clustor*2/512!=last_fat_sector)
		{
			last_fat_sector=current_clustor*2/512;
			if(ReadOneSec((current_clustor*2)/512+1)==0)
			{
				return 0xffff;
			}
			next_clustor=(unsigned int)data_buf[(current_clustor*2)%512+1]*256;
			next_clustor=(unsigned int)(next_clustor+(unsigned int)data_buf[(current_clustor*2)%512]);
#ifdef DEBUG_LINK
			printf("1 next=%x\n",next_clustor);
#endif
			if(next_clustor==0xffff)
			{
				finish=1;
				return current_clustor;
			}
			else
			{
				if(count==clus_num)
				{
					return current_clustor;
				}
				current_clustor=next_clustor;
			}
		}
		else
		{
			next_clustor=data_buf[(current_clustor*2)%512]+data_buf[(current_clustor*2)%512+1]*256;
#ifdef DEBUG_LINK
			printf("2 next=%x\n",next_clustor);
#endif
			if(next_clustor==0xffff)
			{
				finish=1;
				return current_clustor;
			}
			if(count==clus_num)
			{
				return current_clustor;
			}
			current_clustor=next_clustor;
		}
	}
	return 0xffff;				

}
//delete file
//input:
//mode:  1 in root; 2 in subdirectory
//parent_sector:
//file_name:use 8.3 format
//return: success 1; false -1
char CFDelete(char *file_name,unsigned int parent_dir_sector,unsigned char mode)
{
	xdata unsigned int file_first_pos;
	static xdata unsigned int file_record_sec;
	static xdata unsigned int file_record_pos;
	xdata unsigned int temp_sec;
//	xdata unsigned int i;
	if(mode==1)
	{
#ifdef DEBUG
		printf("mode 1\n");
#endif
		file_first_pos=CFFindFileInRoot(file_name,&file_record_sec,&file_record_pos);
		if(file_first_pos==0xffff)
		{
#ifdef DEBUG
			printf("file not find\n");
#endif
			return -1;
		}
		if(file_record_pos>=16)
		{
#ifdef DEBUG
			printf("file record pos too large\n");
#endif
			return -1;
		}
		temp_sec=(file_first_pos
				-(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))
				/bootinfo.sec_per_clust+2; 
		if(DelFatRecord(temp_sec)==-1)
		{
#ifdef DEBUG
			printf("del fat table error\n");
#endif
			return -1;
		}
		if(ReadOneSec(file_record_sec)==0)
		{
			return -1;
		}
		data_buf[file_record_pos*32]=0xE5;
		if(WriteOneSec(data_buf,file_record_sec)==0)
		{
			return -1;
		}
	}
	if(mode==2)
	{
#ifdef DEBUG
		printf("mode 2\n");
#endif
		file_first_pos=CFFindFileInDir(file_name,parent_dir_sector,&file_record_sec,&file_record_pos);
#ifdef DEBUG
		printf("file record sec=%x\n",(int)file_record_sec);
#endif
		if(file_first_pos==0xffff)
		{
			return -1;
		}
#ifdef DEBUG
		printf("1\n");
#endif
		if(file_record_pos>=16)
		{
#ifdef DEBUG
			printf("file record pos too large\n");
#endif
			return -1;
		}
#ifdef DEBUG
		printf("file first pos=%x\n",(int)file_first_pos);
#endif
		if(DelFatRecord(file_first_pos)==-1)
		{
#ifdef DEBUG
			printf("del fat table error\n");
#endif

⌨️ 快捷键说明

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