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

📄 cffmt.c

📁 嵌入式系统中文件系统源代码
💻 C
字号:
//#include "typedefine.h"
#include <sys\syscall.h>
#include "cf_card.h"               
#include "../include/ata_pcc.h"

extern struct FAT_C_H_S FATDiskInfo;


unsigned char CF_BOOT_OTHER_DATA[] =
{
	0x33 ,0xC9 ,0x8E ,0xD1 ,0xBC ,0xF0 ,0x7B ,0x8E ,0xD9 ,0xB8 ,0x00 ,0x20 ,0x8E ,0xC0 ,0xFC ,0xBD ,0x00 ,0x7C
		,0x38 ,0x4E ,0x24 ,0x7D ,0x24 ,0x8B ,0xC1 ,0x99 ,0xE8 ,0x3C ,0x01 ,0x72 ,0x1C ,0x83 ,0xEB ,0x3A ,0x66 ,0xA1 ,0x1C ,0x7C
		,0x26 ,0x66 ,0x3B ,0x07 ,0x26 ,0x8A ,0x57 ,0xFC ,0x75 ,0x06 ,0x80 ,0xCA ,0x02 ,0x88 ,0x56 ,0x02 ,0x80 ,0xC3 ,0x10 ,0x73
		,0xEB ,0x33 ,0xC9 ,0x8A ,0x46 ,0x10 ,0x98 ,0xF7 ,0x66 ,0x16 ,0x03 ,0x46 ,0x1C ,0x13 ,0x56 ,0x1E ,0x03 ,0x46 ,0x0E ,0x13
		,0xD1 ,0x8B ,0x76 ,0x11 ,0x60 ,0x89 ,0x46 ,0xFC ,0x89 ,0x56 ,0xFE ,0xB8 ,0x20 ,0x00 ,0xF7 ,0xE6 ,0x8B ,0x5E ,0x0B ,0x03
		,0xC3 ,0x48 ,0xF7 ,0xF3 ,0x01 ,0x46 ,0xFC ,0x11 ,0x4E ,0xFE ,0x61 ,0xBF ,0x00 ,0x00 ,0xE8 ,0xE6 ,0x00 ,0x72 ,0x39 ,0x26
		,0x38 ,0x2D ,0x74 ,0x17 ,0x60 ,0xB1 ,0x0B ,0xBE ,0xA1 ,0x7D ,0xF3 ,0xA6 ,0x61 ,0x74 ,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
	sysTime now;

	sc_getTime(&now);
	//high-word
	itemp=(now.month)*256+now.day+now.second*256;
	//low-word
	iID=now.year+now.hour*256+now.minute;
	iID+=itemp*256*256;
	//#endif 
	return iID;
}

static _CF_WriteSector(int logicalNo, unsigned char *buffer)
{        
	int ret;
                   
	ret = ATA_WRITE_SECTOR(0, 1, logicalNo, (unsigned short *)buffer);
	
	return ret;

}

extern unsigned short SwapWord(unsigned short sdata);
extern unsigned int SwapDWord(unsigned int ldata);
//0--quickly format  1--standard format!
int CF_Format(void)
{
	int i,j,ret;
	unsigned short ishort, sdata;
	unsigned int  ilong, ldata;
	unsigned char buff[512];
	int temp,iHandle;
	TBootInfo bootRec;//62 byte
	int isectors,isectrack;
	int perclus,ihead;
	unsigned short istemp;//2 bytes
	unsigned int   iltemp;//4 bytes
	               
/*
	ret = CF_Init();
	if(ret!=STATUS_OK)
		return STATUS_ERR;	
*/	
	memset(buff,0,sizeof(buff));

	isectrack = FATDiskInfo.Sector;  // count per track 
	ihead = FATDiskInfo.Head; //CF_GetHeadTotal();          
	isectors = ihead*isectrack*FATDiskInfo.Cylinder;
	
	perclus=isectors/65536;
	perclus += (isectors%65536?1:0);
	
	if(perclus>32)
		perclus=64;
	else if(perclus>16)
		perclus=32;
	else if(perclus>8)
		perclus=16;
	else if(perclus>4)
		perclus=8;
	else if(perclus>2)
		perclus=4;
	else if(perclus>1)
		perclus=2;
	else 
		perclus=1;
	
	//1.JMP to boot code
	bootRec.bootCode[0]=0xEB; //EB 3C 90
	bootRec.bootCode[1]=0x3C;
	bootRec.bootCode[2]=0x90;
	//2.oem name and version
	strcpy((char*)bootRec.oemName,"MSDOS5.0");
	
	//3.bytes per sector
	sdata=512;

#ifdef __WIN32__
	istemp = sdata;
#else
	istemp = SwapWord(sdata);
#endif
	//istemp = 512;
	memcpy(bootRec.sectionBytes,&istemp,sizeof(istemp));
	
	//4.sectors per cluster(must be a power of 2)
	bootRec.clusterSections=perclus;
	//5.reserved sectors(for dir,fat,etc.)
	
	sdata = 1;

#ifdef __WIN32__
	istemp = sdata;
#else
	istemp = SwapWord(sdata);
#endif
	//istemp = SwapWord(sdata);

	//istemp = 1;
	memcpy(bootRec.reserveSection,&istemp,sizeof(istemp));
	//6.number of copies of FAT
	bootRec.fatNumber=2;
	//7.maximum number of root directory entries
	
	sdata = 512;//rootentries=32*16

#ifdef __WIN32__
	istemp = sdata;
#else
	istemp = SwapWord(sdata);
#endif
	//istemp = SwapWord(sdata);
	//istemp = 512;
	memcpy(bootRec.dirSections,&istemp,sizeof(istemp));
	//8.total number of sectord in logical image
	//14.number of sectors if the size of the drive is larger than 32MB
	if(isectors>64*1024)
	{
		bootRec.totalSections[0]=0;
		bootRec.totalSections[1]=0;
		
		ldata=isectors;
#ifdef __WIN32__
		iltemp = ldata;
#else
		iltemp = SwapDWord(ldata);
#endif	
		//iltemp = SwapDWord(ldata);

		//iltemp = isectors;
		memcpy(bootRec.sectNumber,&iltemp,sizeof(iltemp));
	}
	else
	{
		memset(bootRec.sectNumber,0,4);
		sdata = isectors;
		
#ifdef __WIN32__
		istemp = sdata;
#else
		istemp = SwapWord(sdata);
#endif	
		//istemp = SwapWord(sdata);

		//istemp = isectors;
		memcpy(bootRec.totalSections,&istemp,sizeof(istemp));
	} 
	
	//9.media descriptor byte
	bootRec.mediaDesc=0xF8;
	
	//10.number of sectors in FAT
	iltemp = ((isectors-32)/perclus)*2;          // 32: dir sector
	//sdata = iltemp/512+(iltemp%512?1:0);
	sdata = iltemp/512;
     
	iltemp = ((isectors-32-sdata)/perclus)*2;
	sdata = iltemp/512;

#ifdef __WIN32__
		istemp = sdata;
#else
		istemp = SwapWord(sdata);
#endif		
	//istemp = SwapWord(sdata);
	memcpy(bootRec.fatSections,&istemp,sizeof(istemp));
	
	//11.number of sectors per track
	sdata=isectrack;
#ifdef __WIN32__
		istemp = sdata;
#else
		istemp = SwapWord(sdata);
#endif	
	//istemp = SwapWord(sdata);
	//istemp = isectrack;
	memcpy(bootRec.trackSections,&istemp,sizeof(istemp));
	//12.number of heads
	
	sdata=ihead;
#ifdef __WIN32__
		istemp = sdata;
#else
		istemp = SwapWord(sdata);
#endif	
	//istemp = SwapWord(sdata);
	//istemp = ihead;
	memcpy(bootRec.headNumber,&istemp,sizeof(istemp));
	
	//13.number of hidden sectors
	bootRec.hidSections[0]=0;
	bootRec.hidSections[1]=0;
	bootRec.hidSections[2]=0;
	bootRec.hidSections[3]=0;
	//15.drive number used internally dos
	bootRec.diverNum = 0x00; //130;//E:
	//16.reserved
	bootRec.reserved=0;
	//17.boot signature
	bootRec.bootFlag=0x29;
	//18.volumn id number
	ldata=_CF_GetVolumID(); 

#ifdef __WIN32__
		iltemp = ldata;
#else
		iltemp = SwapDWord(ldata);
#endif	
	iltemp = SwapDWord(ldata);

	//iltemp = ldata;
	memcpy(bootRec.volumnID,&iltemp,sizeof(iltemp));
	//19.volumn label
	memcpy(bootRec.volumnLab,"NO NAME  ",11);
	//20.file-system type
	strcpy((char*)&bootRec.fileSysType,"FAT16   ");	
	
	//return 0;
	
	//setBootInfo(&bootRec,iHandle==NF_HNDL?NF_DISK:CF_DISK);
	
	memcpy(buff,&bootRec,sizeof(TBootInfo));//62
	memcpy(&buff[62],CF_BOOT_OTHER_DATA,450);//+450
	//format the 0_sector
	                             
	//_WriteDisk(diskType, 0, (char*)buff,512, WITH_ERASE);
	_CF_WriteSector(0, buff);
	
	//format fat1
	memcpy(&sdata, bootRec.fatSections, 2);
#ifdef __WIN32__
		ishort = sdata;
#else
		ishort = SwapWord(sdata);
#endif	
	//ishort = SwapWord(sdata);
	//ishort = sdata;
	
	memset(buff,0,sizeof(buff));
	buff[0]=0xf8;
	buff[1]=0xff;
	buff[2]=0xff;
	buff[3]=0xff;
	
	// wirte flag of fat1 
	//_WriteDisk(diskType, 512, (char*)buff, 512, WITH_ERASE);
	_CF_WriteSector(1, buff);
                                    
	memset(buff,0,sizeof(buff));
	for(i=2;i<ishort+1;i++)
	{
		//memset(buff,0,sizeof(buff));
		_CF_WriteSector(i, buff);
	}
		

	memset(buff,0,sizeof(buff));
	buff[0]=0xf8;
	buff[1]=0xff;
	buff[2]=0xff;
	buff[3]=0xff;
	_CF_WriteSector(ishort+1, buff);
                                
	memset(buff,0,sizeof(buff));
	//format fat2
	for(i=ishort+2;i<2*(ishort)+1;i++)
	{
		_CF_WriteSector(i, buff);		
		
	}
	
	//format root
	j=ishort;
	memset(buff,0,sizeof(buff));
	for(i=2*j+1;i<32+2*j+1;i++)
	{
		//_WriteDisk(diskType, i*512, (char*)buff, 512, WITH_ERASE);
		_CF_WriteSector(i, buff);		
	}
				
        
/*
	memset(buff,0xF6,sizeof(buff));
	if(flag>0)
	{
		for(i=2*j+32+1;i<(isectors)/(bootRec.clusterSections);i++)
		{
			//_WriteDisk(diskType, i*512, (char*)buff, 512, WITH_ERASE);
			_CF_WriteSector(i, buff);
		}
	}
*/	                       

	//CloseDisk(iHandle);
//	CloseDisk(diskType);
	return 0;
}

⌨️ 快捷键说明

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