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

📄 main.c

📁 详细介绍了关于CF卡的存储结构
💻 C
📖 第 1 页 / 共 5 页
字号:
			if(ReadOneSec(fat_sector+1)==0)			//read FAT table
			{
				return 0xffff;
			}
			current_clustor=(next_clustor-2)*bootinfo.sec_per_clust+(bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512+1);
			next_clustor=data_buf[in_sector_pos+1]*256+data_buf[in_sector_pos];
		}
		if(out_count>=20)
		{
			return 0xffff;
		}
		if(next_clustor==0xffff)
		{
			for(i=0;i<bootinfo.sec_per_clust;i++)		//read one clustor
			{
				if(ReadOneSec(i+current_clustor)==0)		//read one sector
				{
					return 0xffff;
				}
				for(k=0;data_buf[32*k]!=0 && k<16;k++)
				{
#ifdef DEBUG
					printf("\nk=%x\n",k);
#endif
					memcpy(&sub_dir,data_buf+k*32,32);
					if((sub_dir.file_attribute==0x20 || sub_dir.file_attribute==0x10)			
						&& (unsigned char)sub_dir.file_name[0]!=0xe5)	
					{
						if(strncmp(file_name,(unsigned char*)(&sub_dir),11)==0x00)
						{
#ifdef DEBUG
							printf("succ\n");
#endif
							*file_record_sec=i+dir_clustor;
							*file_record_pos=k;
							return sub_dir.first_clust[1]*256+sub_dir.first_clust[0];								
						}
					}
				}
				if(data_buf[32*k]==0)
					return 0xffff;
			}
		}
	}
}


// format cf card 
// success return 1,error return -1
char FormatCF()
{
	xdata struct sBoot boot_sector;
	unsigned long total_sec,temp;
	if(ReadCIS()==0)
		return -1;	
	memcpy((unsigned char*)(&boot_sector),BPB,512);
	boot_sector.bsJump[0]=0xeb;
	boot_sector.bsJump[1]=0x3e;
	boot_sector.bsJump[2]=0x90;
	boot_sector.OemName[0]='S';
	boot_sector.OemName[1]='u';
	boot_sector.OemName[2]='p';
	boot_sector.OemName[3]='c';
	boot_sector.OemName[4]='o';
	boot_sector.OemName[5]='n';
	boot_sector.OemName[6]='y';
	boot_sector.OemName[7]='b';
	boot_sector.bsBytesPerSec[0]=((struct sCIS *)data_buf)->uf_bytes_per_sector[0];
	boot_sector.bsBytesPerSec[1]=((struct sCIS *)data_buf)->uf_bytes_per_sector[1];
	boot_sector.bsSecPerClust=4;
	boot_sector.bsResSectors[0]=1;
	boot_sector.bsResSectors[1]=0;
	boot_sector.bsFATs=2;
	boot_sector.bsRootDirEnts[0]=0;
	boot_sector.bsRootDirEnts[1]=2;
	total_sec=((struct sCIS *)data_buf)->sector_per_card[1]*16777216
		+((struct sCIS *)data_buf)->sector_per_card[0]*65536
		+((struct sCIS *)data_buf)->sector_per_card[3]*256
		+((struct sCIS *)data_buf)->sector_per_card[2];
	if(total_sec<=65536)
	{
		boot_sector.bsSectors[0]=((struct sCIS *)data_buf)->sector_per_card[2];
		boot_sector.bsSectors[1]=((struct sCIS *)data_buf)->sector_per_card[3];
		boot_sector.bsHugeSectors[0]=0;
		boot_sector.bsHugeSectors[1]=0;
		boot_sector.bsHugeSectors[2]=0;
		boot_sector.bsHugeSectors[3]=0;
	}
	else
	{
		boot_sector.bsSectors[0]=0;
		boot_sector.bsSectors[1]=0;
		boot_sector.bsHugeSectors[0]=((struct sCIS *)data_buf)->sector_per_card[2];
		boot_sector.bsHugeSectors[1]=((struct sCIS *)data_buf)->sector_per_card[3];
		boot_sector.bsHugeSectors[2]=((struct sCIS *)data_buf)->sector_per_card[0];
		boot_sector.bsHugeSectors[3]=((struct sCIS *)data_buf)->sector_per_card[1];
	}
	boot_sector.bsMedia=0xf8;
	temp=(total_sec-31+2*boot_sector.bsSecPerClust)/(256*boot_sector.bsSecPerClust+2);
	boot_sector.bsFATsecs[0]=temp%256;
	boot_sector.bsFATsecs[1]=temp/256;
	boot_sector.bsSecPerTrack[0]=((struct sCIS*)data_buf)->cur_sector_per_track[0];
	boot_sector.bsSecPerTrack[1]=((struct sCIS*)data_buf)->cur_sector_per_track[1];
	boot_sector.bsHeads[0]=((struct sCIS*)data_buf)->cur_head_num[0];
	boot_sector.bsHeads[1]=((struct sCIS*)data_buf)->cur_head_num[1];
	boot_sector.bsHiddenSecs[0]=0;
	boot_sector.bsHiddenSecs[1]=0;
	boot_sector.bsBootCode[0]=0; 
	boot_sector.bsBootCode[1]=0; 
	boot_sector.bsDriveNumber=0x80;			//harddisk use 0x8x
	boot_sector.bsReserved1=0;
	boot_sector.bsBootSignature=0x29; 
	boot_sector.bsVolumeID[0]=0x11; 
	boot_sector.bsVolumeID[1]=0x11; 
	boot_sector.bsVolumeID[2]=0x11; 
	boot_sector.bsVolumeID[3]=0x11;
	boot_sector.bsVolumeLabel[0]='c'; 
	boot_sector.bsVolumeLabel[1]='f';
	boot_sector.bsVolumeLabel[2]=' ';
	boot_sector.bsVolumeLabel[3]='c';
	boot_sector.bsVolumeLabel[4]='a';
	boot_sector.bsVolumeLabel[5]='r';
	boot_sector.bsVolumeLabel[6]='d';
	boot_sector.bsVolumeLabel[7]=' ';
	boot_sector.bsFileSysType[0]='F'; 
	boot_sector.bsFileSysType[1]='A'; 
	boot_sector.bsFileSysType[2]='T'; 
	boot_sector.bsFileSysType[3]='1'; 
	boot_sector.bsFileSysType[4]='6';
	boot_sector.bsFileSysType[5]=' ';
	boot_sector.bsFileSysType[6]=' ';
	boot_sector.bsFileSysType[7]=' ';
	boot_sector.bsSign[0]=0x55;
	boot_sector.bsSign[1]=0xaa;
	if(WriteOneSec((unsigned char*)(&boot_sector),0)==0)
		return -1;

	if(EraseSec(1,256)==0)
//		printf("sss1\n");
	if(EraseSec(256,256)==0)
//		printf("sss2\n");
	if(EraseSec(512,256)==0)
//		printf("sss3\n");
	if(EraseSec(768,256)==0)
//		printf("sss4\n");
	if(EraseSec(1024,256)==0)
//		printf("sss5\n");
	for(temp=0;temp<512;temp++)
	{
		data_buf[temp]=0;
	}
#ifdef CLEAR_FAT
	for(temp=1;temp<500;temp++)
	{
		zxPutChar(0x2e);
		if(WriteOneSec(data_buf,temp)==0)
			return -1;
	}
#endif
	data_buf[0]=0xf8;
	data_buf[1]=0xff;
	data_buf[2]=0xff;
	data_buf[3]=0xff;
	if(WriteOneSec(data_buf,1)==0)		//init Fat1
		return -1;
	if(WriteOneSec(data_buf,(boot_sector.bsFATsecs[1]*256+boot_sector.bsFATsecs[0]+1))==0)	//init Fat2
		return -1;
	for(temp=0;temp<4;temp++)
	{
		data_buf[temp]=0;
	}
	if(WriteOneSec(data_buf,(2*(boot_sector.bsFATsecs[1]*256+boot_sector.bsFATsecs[0])+1))==0)
		return -1;
	return 1;
}

//FDT_pos存放返回的目录登记表的扇区号,record_pos存放在扇区中的目录登记项号
//返回值:1成功,0失败
bit SerchFreeFDT(unsigned int *FDT_pos,unsigned int *record_pos,unsigned int start_sec)
{
	bit return_value=0;
	unsigned int i,j,k;
	if(start_sec==0x0)		//serch in root 
	{
		for(j=bootinfo.fat_size*2+1;j<=bootinfo.fat_size*2+1+bootinfo.root_dir_size*32/512;j++)
		{
			ReadOneSec(j);
			for(i=0; i<=bootinfo.root_dir_size*32/512; i++)
			{
				if(data_buf[32*i]==0x00 || data_buf[32*i]==0xe5)
				{
					*FDT_pos=j;
					*record_pos=i;
					return_value=1;
					break;
				}
			}
			if(return_value==1)
			{
				break;
			}
		}
	}
	else					//serch in sub 
	{
		for(k=0;k<bootinfo.sec_per_clust;k++)
		{
			if(ReadOneSec(start_sec+k)==0)
			{
				return 0;
			}
			for(j=0;j<32;j++)
			{
				for(i=0;i<32;i++)
				{
					if(data_buf[32*i]==0x00 || data_buf[32*i]==0xe5)
					{
						*FDT_pos=start_sec+k;
						*record_pos=i;
						return_value=1;
						return 1;
					}
				}
			}
		}
	}
	return return_value;
}

//找FAT表中的空登记项
//返回值:1 成功(有空闲磁盘空间);2 失败(磁盘满)
//clustor_chain:
/*
	FAT					explanation

	0x0000				referenced clustor is currently unused
	0xfff0-0xfff6		reserved cluster
	0xfff7				bad track
	0xffff				last clustor of a file
	0xnnnn				relative number of next clustor for a file
*/
bit SerchFreeFAT(unsigned int *clustor_chain,unsigned int data_len,unsigned int start_sector)
{
	bit return_value=0;
	unsigned int i,j,clustor_pos=0;
	unsigned int fat16_value=0;
	clustor_pos=0;
	for(j=0; j<bootinfo.fat_size+1-start_sector; j++)
	{
		ReadOneSec(j+start_sector);
		for(i=0;i<512;i=i+2)
		{
//			fat16_value=data_buf[i]*256+data_buf[i+1];
			fat16_value=data_buf[i+1]*256+data_buf[i];
			if(fat16_value==0x0000)
			{
//				clustor_chain[clustor_pos]=i+j*512;
				clustor_chain[clustor_pos]=(i+j*512)/2;
				clustor_pos++;
				if(clustor_pos>=data_len)
				{
					return 1;
				}
			}
		}
	}
	return 0;
}

//	function: like dos command "md"
//  input:directory name is 8 bytes
//	return: 1 success;-1 FAT table full;-2 FDT table full;-3 write FDT table Error	  
char CFMakeDirectory(char * name,unsigned int start_clustor)
{
	xdata struct sRootDir dir;
	bit success=0;
	static unsigned int sector;
	unsigned int i;
	static unsigned int fdt_pos,record_pos;
	for(i=1; i<=bootinfo.fat_size; i++)
	{
		success=SerchFreeFAT(&sector,1,1);		//find free fat table
		if(success==1)
		{
#ifdef DEBUG
			printf("\ni=%x,sector=%x\n",i,sector);	
#endif
			break;
		}
	}
	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
	if(start_clustor!=0)
	{
		fdt_pos=start_clustor;
		record_pos=0;	
	}
#ifdef DEBUG
	printf("fdt pos=%x,record pos=%x\n",fdt_pos,record_pos);
#endif
	if(ReadOneSec(fdt_pos)==0)					
		return -3;
	for(i=0;i<32;i++)
	{
		((unsigned char *)(&dir))[i]=0;
	}
	for(i=0;i<8;i++)
	{
		dir.file_name[i]=name[i];
	}
	for(i=0;i<3;i++)
	{
		dir.exten_name[i]=' ';
	}
	dir.file_attribute=0x10;
	dir.first_clust[0]=(sector)%256;
	dir.first_clust[1]=(sector)/256;
	for(i=0;i<32;i++)
	{
		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<32;i++)
	{
		((unsigned char *)(&dir))[i]=0;
	}
	for(i=0;i<11;i++)
	{
		((unsigned char*)(&dir))[i]=' ';
	}
	dir.file_name[0]='.';
	dir.file_attribute=0x10;
	dir.first_clust[0]=(sector)%256;
	dir.first_clust[1]=(sector)/256;
	for(i=0;i<512;i++)
	{
		data_buf[i]=0;
	}
	for(i=0;i<32;i++)
	{
		data_buf[i]=((unsigned char*)(&dir))[i];
	}
	dir.file_name[1]='.';
	dir.first_clust[0]=0;
	dir.first_clust[1]=0;
	for(i=0;i<32;i++)
	{
		data_buf[32+i]=((unsigned char*)(&dir))[i];
	}
	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;
}


char CFMakeSubDirectory(char *sub_dir_name,char *parent_dir_name)
{
	static xdata unsigned int parent_dir_sector;
	static xdata unsigned int fdt_pos,record_pos;
	static xdata unsigned int sector;
	xdata unsigned int temp_sec;
	xdata unsigned int i;
	xdata struct sRootDir dir;
	xdata unsigned int un_use;
	if(SerchFreeFAT(&sector,1,1)==0)	//find free clustor
	{
		return -1;						//disk full
	}
#ifdef DEBUG
	printf("\n free sec=%x\n",sector);
#endif
	parent_dir_sector=CFFindFileInRoot(parent_dir_name,&un_use,&un_use);
	if(parent_dir_sector==0xffff)		//not find that root directory
	{
		return -1;
	}
	if(SerchFreeFDT(&fdt_pos,&record_pos,parent_dir_sector)==0)		//find free record space in directory		
	{
		return -1;
	}
#ifdef DEBUG
	printf("\nmd sub: record_pos=%x,fdt_pos=%x,p_dir_sector=%x\n",record_pos,fdt_pos,parent_dir_sector);
#endif
	if(ReadOneSec(fdt_pos)==0)
	{
		return -1;
	}
	for(i=0;i<13;i++)
	{
		((unsigned char*)(&dir))[i]=sub_dir_name[i];
	}
	dir.file_attribute = DIR;
	dir.file_size[0] = 0;
	dir.file_size[1] = 0;
	dir.file_size[2] = 0;
	dir.file_size[3] = 0;
	dir.first_clust[0]=(sector)%256;
	dir.first_clust[1]=(sector)/256;
	for(i=0;i<32;i++)
	{
		data_buf[record_pos*32+i]=((unsigned char*)(&dir))[i];
	}
	if(WriteOneSec(data_buf,fdt_pos)==0)
	{
		return -1;
	}
	if(ReadOneSec((sector*2)/512+1)==0)		//read one sector of Fat table
	{
		return -1;
	}
	for(i=(sector*2)%512;i<(sector*2)%512+2;i++)
	{
#ifdef DEBUG
		printf("\ni=%x\n",i);
#endif
		data_buf[i]=0xff;
	}
	if(WriteOneSec(data_buf,(sector*2)/512+1)==0)
	{
		return -1;		//write fat table error
	}
//---------------write in sub directory------------------------------	
	if(ReadOneSec((sector*2)/512+1)==0)		//read one sector of Fat table
	{
		return -1;
	}
	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 -1;		//write fdt error
	}
	for(i=0;i<32;i++)
	{
		((unsigned char *)(&dir))[i]=0;
	}
	for(i=0;i<11;i++)
	{
		((unsigned char*)(&dir))[i]=' ';
	}
	dir.file_name[0]='.';				//current dir
	dir.file_attribute=0x10;
	dir.first_clust[0]=(sector)%256;
	dir.first_clust[1]=(sector)/256;
	for(i=0;i<512;i++)
	{
		data_buf[i]=0;
	}
	for(i=0;i<32;i++)
	{
		data_buf[i]=((unsigned char*)(&dir))[i];
	}
	temp_sec=(parent_dir_sector
			-(1+bootinfo.fat_size*bootinfo.fat_num+bootinfo.root_dir_size*32/512))
			/bootinfo.sec_per_clust+2; 
	dir.file_name[1]='.';				//parent dir
	dir.first_clust[0]=temp_sec%256;
	dir.first_clust[1]=temp_sec/256;
	for(i=0;i<32;i++)
	{
		data_buf[32+i]=((unsigned char*)(&dir))[i];
	}

⌨️ 快捷键说明

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