📄 fat_misc.c
字号:
err1 =FS__lb_read(FS__pDevInfo[0].harddisk_driver,0,fatsec,(void *)pBuffer);
if (err1!=SD_NO_ERR) {
err1 =FS__lb_read(FS__pDevInfo[0].harddisk_driver,0,(fatsec+FS__pDiskInfo[0].FATSecCnt),(void *)pBuffer);
if (err1!=SD_NO_ERR) {
return FS_ERR;
}
/* Try to repair original FAT sector with contents of copy */
FS__lb_write(FS__pDevInfo[0].harddisk_driver,0,fatsec,(void *)pBuffer);
}
*pLastSector = fatsec;
}
//填入FAT表里的簇号
a = Cluster & 0xff;
b = (Cluster / 0x100L) & 0xff;
if(FAT_type==FAT16) { /* FAT16 */
pBuffer[fatoffs] = a;
pBuffer[fatoffs + 1] = b;
err1=FS__lb_write(FS__pDevInfo[0].harddisk_driver,0,fatsec,(void *)pBuffer);
err2=FS__lb_write(FS__pDevInfo[0].harddisk_driver,0,(fatsec+FS__pDiskInfo[0].FATSecCnt),(void *)pBuffer);
if((err1!=SD_NO_ERR)||(err2!=SD_NO_ERR))
{
return FS_ERR;
}
}
return FS_NO_ERR;
}
S32 FS__fat_checkunit(void)
{
U16 err;
if(FS__pDiskInfo[0].Drive_unit==0)
{
err=_FS_ReadBPB();
if(err!=FS_NO_ERR)
{
return err;
}
if(FS__pDiskInfo[0].NumFATs!=2)
{
return BPB_ERR;
}
}
else if(FS__pDiskInfo[0].NumFATs!=2)
{
err=_FS_ReadBPB();
if(err!=FS_NO_ERR)
{
return err;
}
if(FS__pDiskInfo[0].NumFATs!=2)
{
return BPB_ERR;
}
}
return FS_NO_ERR;
}
/*********************************************************************
*
* FS__fat_FAT_find_eof
*
Description:
FS internal function. Find the next EOF mark in the FAT.
Parameters:
StrtClst - Starting cluster in FAT.
pClstCnt - If not zero, this is a pointer to an FS_u32, which
is used to return the number of clusters found
between StrtClst and the next EOF mark.
Return value:
>=0 - Cluster, which contains the EOF mark.
<0 - An error has occured.
*/
S32 FS__fat_FAT_find_eof( U32 StrtClst, U32 *pClstCnt) {
U32 clstcount;
U32 fatsize;
U32 maxclst;
U32 fatindex;
U32 fatsec;
U32 fatoffs;
S32 lastsec;
U32 curclst;
U32 bytespersec;
U32 eofclst;
U8 fattype;
U16 err;
U8 *buffer;
unsigned char a;
unsigned char b;
fattype = FS__fat_which_type();
if(fattype==FAT16) {
maxclst = 65525UL; /* FAT16 */
}
buffer = FS__fat_malloc(FAT_SEC_SIZE);
if ((S32)BUFFER_ERR==(S32)buffer) {
return BUFFER_ERR;
}
fatsize = FS__pDiskInfo[0].FATSecCnt;
bytespersec = FS__pDiskInfo[0].BytsPerSec;
curclst = StrtClst;
lastsec = -1;
clstcount = 0;
while (clstcount < maxclst) {
eofclst = curclst;
clstcount++;
if(fattype==FAT16){
fatindex = curclst * 2; /* FAT16 */
}
fatsec = FS__pDiskInfo[0].FATStartSec + (fatindex / bytespersec);
fatoffs = fatindex % bytespersec;
if (fatsec != lastsec) {
err = FS__lb_read(FS__pDevInfo[0].harddisk_driver, 0, fatsec, (void*)buffer);
if (err!=SD_NO_ERR) {
err =FS__lb_read(FS__pDevInfo[0].harddisk_driver, 0, fatsec+fatsize, (void*)buffer);
if (err !=SD_NO_ERR) {
FS__fat_free(buffer);
return FS_ERR;
}
/* Try to repair original FAT sector with contents of copy */
FS__lb_write(FS__pDevInfo[0].harddisk_driver, 0, fatsec, (void*)buffer);
}
lastsec = fatsec;
}
if(fattype==FAT16) {
a = buffer[fatoffs];
b = buffer[fatoffs + 1];
curclst = a + 256 * b;
curclst &= 0xffffL;
if (curclst >= (S32)0xfff8L) {
/* EOF found */
FS__fat_free(buffer);
if (pClstCnt) {
*pClstCnt = clstcount;
}
return eofclst;
}
}
} /* while (clstcount<maxclst) */
FS__fat_free(buffer);
return FS_ERR;
}
/*********************************************************************
*
* FS__fat_FAT_alloc
*
Description:
FS internal function. Allocate a new cluster in the FAT and link it
to LastClust. Assign an EOF mark to the new allocated cluster.
The function has grown a lot, since it supports all FAT types (FAT12,
FAT16 & FAT32). There is also room for performance improvement, when
makeing the new FAT entry and the old entry is within the same FAT
sector.
Parameters:
LastClust - Number of cluster, to which the new allocated cluster
is linked to. If this is negative, the new cluster is
not linked to anything and only the EOF mark is set.
Return value:
>=0 - Number of new allocated cluster, which contains the
EOF mark.
<0 - An error has occured.
*/
S32 FS__fat_FAT_alloc( S32 LastClust) {
U32 fatsize;
U32 fatoffs;
U32 bytespersec;
S32 curclst;
U32 fatsec;
S32 lastsec;
U8 *buffer;
U16 fattype;
S32 err;
S32 err2;
S32 lexp;
buffer = (U8*)FS__fat_malloc(FAT_SEC_SIZE);
if ((S32)buffer==(S32)BUFFER_ERR) {
return BUFFER_ERR;
}
fattype = FS__fat_which_type();
fatsize = FS__pDiskInfo[0].FATSecCnt;
bytespersec = FS__pDiskInfo[0].BytsPerSec;
/* Find a free cluster in the FAT */
curclst = _FS__fat_FindFreeCluster( &fatsec, &lastsec, &fatoffs, LastClust, buffer, fattype, fatsize, bytespersec);
if (curclst <0) {
FS__fat_free(buffer); /* No free cluster found. */
return FS_ERR;
}
/* Make an EOF entry for the new cluster */
err = _FS__fat_SetEOFMark(fatoffs, buffer,fattype);
if (err < 0) {
FS__fat_free(buffer);
return FS_ERR;
}
/* Link the new cluster to the cluster list */
if (LastClust < 0) {
err = FS__lb_write(FS__pDevInfo[0].harddisk_driver, 0, lastsec, (void*)buffer);
err2 = FS__lb_write(FS__pDevInfo[0].harddisk_driver, 0, lastsec+fatsize, (void*)buffer);
lexp = (err < 0);
lexp = lexp || (err2 < 0);
if (lexp) {
FS__fat_free(buffer);
return FS_ERR;
}
}
else {
err = _FS__fat_LinkCluster(&lastsec, curclst, LastClust, buffer, fattype, fatsize, bytespersec);
if (err < 0) {
FS__fat_free(buffer);
return FS_ERR;
}
}
FS__fat_free(buffer);
return curclst;
}
/*********************************************************************
*
* FS__fat_diskclust
*
Description:
FS internal function. Walk through the FAT starting at StrtClst for
ClstNum times. Return the found cluster number of the media. This is
very similar to FS__fat_FAT_find_eof.
Parameters:
Idx - Index of device in the device information table
referred by FS__pDevInfo.
Unit - Unit number.
StrtClst - Starting point for FAT walk.
ClstNum - Number of steps.
Return value:
> 0 - Number of cluster found after ClstNum steps.
==0 - An error has occured.
*/
//返回ClstNum步后寻找到的簇号,若ClstNum步前出现EOF返回错误
U32 FS__fat_diskclust( S32 StrtClst, S32 ClstNum) {
U32 fatsize;
S32 fatindex;
S32 fatsec;
S32 fatoffs;
S32 lastsec;
S32 curclst;
S32 todo;
S32 bytespersec;
S32 err;
U8 fattype;
U8 *buffer;
unsigned char a;
unsigned char b;
fattype = FS__fat_which_type();
buffer = FS__fat_malloc(FAT_SEC_SIZE);
if (BUFFER_ERR==(S32)buffer) {
return 0;
}
fatsize = FS__pDiskInfo[0].NumFATs;
bytespersec = FS__pDiskInfo[0].BytsPerSec;
todo = ClstNum;
curclst = StrtClst;
lastsec = -1;
while (todo) {
if(fattype==FAT16){
fatindex = curclst * 2; /* FAT16 */
}
fatsec = FS__pDiskInfo[0].FATStartSec + (fatindex / bytespersec);
fatoffs = fatindex % bytespersec;
if (fatsec != lastsec) {
err =FS__lb_read(FS__pDevInfo[0].harddisk_driver, 0, fatsec, (void*)buffer);
if (err!=SD_NO_ERR) {
err = FS__lb_read(FS__pDevInfo[0].harddisk_driver, 0, fatsize+fatsec, (void*)buffer);
if (err!=SD_NO_ERR) {
FS__fat_free(buffer);
return 0;
}
/* Try to repair original FAT sector with contents of copy */
FS__lb_write(FS__pDevInfo[0].harddisk_driver, 0, fatsec, (void*)buffer);
}
lastsec = fatsec;
}
if(fattype==FAT16){
a = buffer[fatoffs];
b = buffer[fatoffs + 1];
curclst = a + 256 * b;
curclst &= 0xffffL;
if (curclst >= (S32)0xfff8L) {
FS__fat_free(buffer);
return 0;
}
}
todo--;
}
FS__fat_free(buffer);
return curclst;
}
/*********************************************************************
*
* Global Variables
*
**********************************************************************
*/
const FS__fsl_type FS__fat_functable = {
"FAT16",
FS__fat_fopen,
FS__fat_fclose,
FS__fat_fread,
FS__fat_fwrite,
0,
0,
0,
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -