磁盘格式化:
格式化的核心工作是对主要的功能部分进行构造,构造的核心是在于DBR(BPB)。
DBR中的BPB记录了FAT32的重要参数,主要有三项:有效总扇区数,每簇扇区数,FAT表扇区数。
#ifdef ZNFAT_MAKE_FS
UINT8 znFAT_Make_FS(UINT32 tt_sec,UINT16 clu_sz) //¸ñʽ»¯ tt_sec ×ÜÉÈÇøÊý clu_sz Ö¸¶¨µÄ´Ø´óС
{ //ÈôΪ0Ôò°´´ÅÅÌ´óСѡ¶¨Ä¬ÈÏÖµ ¸ñʽ»¯²ßÂÔ²ÉÓñȽϼòµ¥µÄSFD²ßÂÔ
//ÎÞMBR£¬²»Ö§³Ö·ÖÇø£¬Ö±½Ó´ÓDBR¿ªÊ¼
struct DBR *pdbr;
struct FSInfo *pfsinfo;
UINT32 temp=0,temp1=0,temp2=0;
tt_sec/=(UINT32)(NSECPERCYLINDER);
tt_sec*=(UINT32)(NSECPERCYLINDER);//ÉáÈ¥¡°Ê£ÓàÉÈÇø¡±£¬Ê£ÓàÉÈÇøÊÇÖ¸²»×ãÒ»¸öÖùÃæµÄÉÈÇøÊý
//=================ºÏ³ÉDBRÉÈÇøÊý¾Ý=============================================================
PGM2RAM(znFAT_Buffer,_dbr,512); //´ÓÄ£°æÊý×éÖаÑÊý¾Ý°áµ½ÄÚ²¿»º³åÇø
pdbr=(struct DBR *)znFAT_Buffer;
pdbr->BPB_SecPerClus=(UINT8)(clu_sz/512); //ÿ´ØÉÈÇøÊý
if(0==pdbr->BPB_SecPerClus) pdbr->BPB_SecPerClus=Get_Recmd_szClu(tt_sec);
if(0==pdbr->BPB_SecPerClus) return ERR_FMT_TOO_LOW_VOLUME; //ÈÝÁ¿Ì«Ð¡£¬²»ÄÜÓÃFAT32½øÐиñʽ»¯
temp1=pdbr->BPB_SecPerClus;
pdbr->BPB_TotSec32[0]=(UINT8)((tt_sec) &0X000000FF);
pdbr->BPB_TotSec32[1]=(UINT8)((tt_sec>>8) &0X000000FF);
pdbr->BPB_TotSec32[2]=(UINT8)((tt_sec>>16)&0X000000FF);
pdbr->BPB_TotSec32[3]=(UINT8)((tt_sec>>24)&0X000000FF); //¸Ã·ÖÇøµÄ×ÜÉÈÇøÊý
temp=(tt_sec-32)/(((UINT32)NITEMSINFATSEC)*((UINT32)(pdbr->BPB_SecPerClus)));
if((tt_sec-32)%((UINT32)NITEMSINFATSEC)*((UINT32)pdbr->BPB_SecPerClus)) temp++; //((tt_sec-32)-2*FATsz)/(SecPerClus*128)=FATsz ½âÕâ¸öµÈʽ
temp2=temp;
pdbr->BPB_FATSz32[0]=(UINT8)((temp) &0X000000FF);
pdbr->BPB_FATSz32[1]=(UINT8)((temp>>8) &0X000000FF);
pdbr->BPB_FATSz32[2]=(UINT8)((temp>>16)&0X000000FF);
pdbr->BPB_FATSz32[3]=(UINT8)((temp>>24)&0X000000FF); //FAT±íµÄÉÈÇøÊý
znFAT_Device_Write_Sector(0,znFAT_Buffer); //½«ºÏ³ÉºÃµÄDBRÊý¾ÝдÈëµ½0ÉÈÇøÖÐÈ¥
//===============================ÒÔÉÏ´úÂëÍê³É¶ÔDBRÉÈÇøÊý¾ÝµÄºÏ³É===============================
//===================================ÒÔÏ´úÂëÍê³É¶ÔFSINFOÉÈÇøÊý¾ÝµÄºÏ³É========================
Memory_Set(znFAT_Buffer,ZNFAT_BUF_SIZE,0); //½«ÄÚ²¿»º³åÇøÇå0
PGM2RAM(znFAT_Buffer,_fsinfo_1,4); //½«FSINFOÄ£°åÊý¾ÝµÄµÚÒ»²¿·Ö°á¹ýÀ´
PGM2RAM(znFAT_Buffer+484,_fsinfo_2,28); //½«FSINFOÄ£°åÊý¾ÝµÄµÚ¶þ²¿·Ö°á¹ýÀ´
//×¢£ºFSINFOÄ£°åÊý¾Ý·ÖΪÁ½²¿·Ö£¬Ö÷ÒªÊÇÒòΪÆäÖÐÓоø´ó
//²¿·ÖÊÇ0£¬ÎªÁ˽ÚÊ¡¹Ì»¯Êý¾ÝÁ¿£¬¼õÉÙFLASHROM¿Õ¼äµÄʹÓÃÁ¿
pfsinfo=(struct FSInfo *)znFAT_Buffer;
temp=(tt_sec-32-2*temp)/((UINT32)(temp1))-1; //×Ü´ØÊý-1£¬ÒòΪµÚ2´ØΪÊ×Ŀ¼£¬ÒѾ-±»¾í±êÕ¼ÓÃ
pfsinfo->Free_Cluster[0]=(UINT8)((temp) &0X000000FF);
pfsinfo->Free_Cluster[1]=(UINT8)((temp>>8) &0X000000FF);
pfsinfo->Free_Cluster[2]=(UINT8)((temp>>16)&0X000000FF);
pfsinfo->Free_Cluster[3]=(UINT8)((temp>>24)&0X000000FF); //Ê£Óà¿Õ´ØÊý
znFAT_Device_Write_Sector(1,znFAT_Buffer); //½«ºÏ³ÉºÃµÄDBRÊý¾ÝдÈëµ½64ÉÈÇøÖÐÈ¥£¬¼´DBRÉÈÇøµÄºóÒ»¸öÉÈÇø
//=====================================ÒÔÉÏ´úÂëÍê³É¶ÔFSINFOÉÈÇøÊý¾ÝµÄºÏ³É=====================
//=====================================ÒÔÏ´úÂëÍê³É¶ÔFAT±íµÄ³õʼ»¯============================
znFAT_Device_Clear_nSector(temp1,32+2*temp2);
znFAT_Device_Clear_nSector(temp2-1,33);
znFAT_Device_Clear_nSector(temp2-1,33+temp2);
PGM2RAM(znFAT_Buffer,_fatsec,12); //½«FAT±íÄ£°æÊý¾Ý°áµ½ÄÚ²¿»º³åÇø
znFAT_Device_Write_Sector(32,znFAT_Buffer); //ÏòFAT±í1ÖÐдÈë0
znFAT_Device_Write_Sector(32+temp2,znFAT_Buffer); //ÏòFAT±í2ÖÐдÈë0
//=====================================ÒÔÉÏ´úÂëÍê³É¶ÔFAT±íµÄ³õʼ»¯============================
//=====================================ÒÔÏ´úÂë¶ÔÊý¾ÝÇøÊ×ÉÈÇø½øÐгõʼ»¯£¬Ð´Èë¾í±ê=============
PGM2RAM(znFAT_Buffer,_1stsec,26);
znFAT_Device_Write_Sector(32+2*temp2,znFAT_Buffer); //ÏòÊý¾ÝÇøµÚÒ»¸öÉÈÇøдÈëÊý¾Ý
//=====================================ÒÔÉÏ.....==============================================
return ERR_SUCC;
}
#endif
试的时候,感觉格式化时间比较长。
|