📄 复件 zlg_ffs.c
字号:
buf = Index->Drive->BufA;
if (Index != NULL)
if (Index->Drive->ExSectorRead(Index->Drive->Index, buf, 0) == TRUE)
{
if (buf[0] != 'Z' ||
buf[1] != 'L' ||
buf[2] != 'G' ||
buf[3] != '\\' ||
buf[4] != 'F' ||
buf[5] != 'F' ||
buf[6] != 'S' ||
buf[7] != 0)
{
return NFFormatA(Index);
}
else
{
return NFFormatB(Index);
}
}
return FALSE;
}
/*********************************************************************************************************
** Function name: FFSDiskInit
** Descriptions: 驱动程序初始化 driver init
** Input:Index: 底层驱动信息 low driver info
**
** Output: TRUE: 成功 OK
** FALSE: 失败 NOT OK
**
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 FFSDiskInit(FFSDisk *Index)
{
uint32 i, temp, temp1, temp2, temp3;
uint32 j, k, m, n, BytsPerSec, WriteFlag;
const FlashDriver *Drive;
uint8 *bufa, *bufb;
uint32 *VBlockInfo;
/* 检查参数有效性 */
if (Index == NULL)
{
return DISK_INIT_NOT_OK;
}
Drive = Index->Drive;
if (Drive == NULL ||
Drive->SectorRead == NULL ||
Drive->ExSectorRead == NULL ||
Drive->SectorWrite == NULL ||
Drive->ExSectorWrite == NULL ||
Drive->BlockErase == NULL ||
//Drive->SecCopy == NULL ||
Drive->SectorCheck == NULL ||
Drive->ExSectorCheck == NULL
)
{
return DISK_INIT_NOT_OK;
}
bufa = Drive->BufA;
if (Drive->ExSectorRead(Drive->Index, bufa, 0) == FALSE)
{
return DISK_INIT_NOT_OK;
}
/* 没有低级格式化就低级格式化 */
if (bufa[0] != 'Z' ||
bufa[1] != 'L' ||
bufa[2] != 'G' ||
bufa[3] != '\\' ||
bufa[4] != 'F' ||
bufa[5] != 'F' ||
bufa[6] != 'S' ||
bufa[7] != 0)
{
NFFormatA(Index);
}
/* 获取必须的内存空间 */
i = Drive->BlockPreDisk;
bufa = malloc(i * sizeof(uint8));
if (bufa == NULL)
{
return DISK_INIT_NOT_OK;
}
Index->BlockState = bufa;
/* 初始化内存空间 */
do
{
*bufa++ = BAD_BLOCK;
} while (--i != 0);
/* 获取必须的内存空间 */
i = Drive->BlockPreDisk;
VBlockInfo = malloc(i * sizeof(uint32));
if (VBlockInfo == NULL)
{
free(Index->BlockState);
Index->BlockState = NULL;
return DISK_INIT_NOT_OK;
}
Index->BlockEaseSum = VBlockInfo;
/* 初始化内存空间 */
do
{
*VBlockInfo++ = ~0;
} while (--i != 0);
/* 获得有效块数 */
bufa = Drive->BufA;
j = Drive->BlockPreDisk;
i = 0;
k = 0;
temp = 0;
BytsPerSec = Drive->BytsPerSec;
Drive->SectorRead(Drive->Index, bufa, 0);
do
{
if ((bufa[(i / 8) % BytsPerSec] & (1 << (i % 8))) != 0)
{
temp++;
}
i++;
if ((i % (BytsPerSec * 8)) == 0)
{
k++;
Drive->ExSectorRead(Drive->Index, bufa, k);
}
} while (--j != 0) ;
if (temp <= 1)
{
return DISK_INIT_NOT_OK;
}
/* 获取必须的内存空间 */
temp--; /* 1个保留块 */
Index->BlockSum = temp;
VBlockInfo = malloc(temp * sizeof(uint32));
if (VBlockInfo == NULL)
{
free(Index->BlockState);
Index->BlockState = NULL;
free(Index->BlockEaseSum);
Index->BlockEaseSum = NULL;
return DISK_INIT_NOT_OK;
}
Index->VBlockInfo = VBlockInfo;
/* 初始化内存空间 */
do
{
*VBlockInfo++ = 0;
} while (--temp != 0);
/* 获取虚拟块与物理块的映射表、物理块擦除计数表和物理块状态表 */
VBlockInfo = (Index->VBlockInfo) - 1;
bufa = Drive->BufA;
k = 0;
j = Index->BlockSum;
Drive->SectorRead(Drive->Index, bufa, 0);
for (i = 1; i < j; i++)
{
if ((i % (BytsPerSec * 8)) == 0)
{
k++;
Drive->ExSectorRead(Drive->Index, bufa, k);
}
if ((bufa[(i / 8) % BytsPerSec] & (1 << (i % 8))) != 0)
{
WriteFlag = USR_BLOCK;
Index->BlockState[i] = FREE_BLOCK;
m = Drive->SecsPreBlock;
n = m * i;
temp2 = 0;
temp1 = 0;
temp = ~0;
do
{
Drive->ExSectorRead(Drive->Index, bufa, n++);
if (bufa[5] == 0xff)
{
continue;
}
if (bufa[5] == 0x55)
{
WriteFlag = UN_FINISH_BLOCK;
continue;
}
temp = bufa[2] | bufa[3] << 8;
temp = temp << 16;
temp |= (bufa[0] | bufa[1] << 8);
temp3 = bufa[10] | bufa[11] << 8;
temp3 = temp3 << 16;
temp3 |= (bufa[8] | bufa[9] << 8);
if (temp3 != ~0)
{
temp1 = temp3;
}
} while (--m != 0);
if (temp < j && temp != 0)
{
if (WriteFlag == UN_FINISH_BLOCK)
{
if (VBlockInfo[temp] == 0)
{
VBlockInfo[temp] = i;
Index->BlockState[i] = UN_FINISH_BLOCK;
temp2 = USR_BLOCK_ADD;
}
else
{
Index->BlockState[i] = DELET_BLOCK;
}
}
else
{
m = VBlockInfo[temp];
if (m == 0)
{
VBlockInfo[temp] = i;
Index->BlockState[i] = USR_BLOCK;
temp2 = USR_BLOCK_ADD;
}
else
{
if (Index->BlockState[m] == UN_FINISH_BLOCK)
{
Index->BlockState[m] = DELET_BLOCK;
VBlockInfo[temp] = i;
Index->BlockState[i] = USR_BLOCK;
temp2 = USR_BLOCK_ADD;
}
else
{
Index->BlockState[i] = DELET_BLOCK;
}
}
}
}
else
{
if (WriteFlag == UN_FINISH_BLOCK)
{
Index->BlockState[i] = DELET_BLOCK;
}
}
Index->BlockEaseSum[i] = temp1 + temp2;
Drive->SectorRead(Drive->Index, bufa, k);
}
}
j = Drive->BlockPreDisk;
bufb =Index->BlockState;
for (i = 1; i < j; i++)
{
bufb++;
if (*bufb == UN_FINISH_BLOCK)
{
*bufb = USR_BLOCK;
bufa = Drive->BufA;
m = Drive->SecsPreBlock;
n = m * i - 1;
do
{
n++;
Drive->ExSectorRead(Drive->Index, bufa, n);
if (bufa[5] != 0x55)
{
continue;
}
bufa[5] = 0x00;
Drive->ExSectorWrite(Drive->Index, bufa, n);
} while (--m != 0);
}
if (*bufb == DELET_BLOCK)
{
FFSBlockErase(Index, i);
}
}
return DISK_INIT_OK;
}
/*********************************************************************************************************
** Function name: FFSDiskClose
** Descriptions: 关闭驱动程序 driver close
** Input:Index: 底层驱动信息 low driver info
**
** Output: TRUE: 成功 OK
** FALSE: 失败 NOT OK
**
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 FFSDiskClose(FFSDisk *Index)
{
if (Index != NULL)
{
if (Index->BlockState != NULL)
{
free(Index->BlockState);
}
Index->BlockState = NULL;
if (Index->BlockEaseSum != NULL)
{
free(Index->BlockEaseSum);
}
Index->BlockEaseSum = NULL;
if (Index->VBlockInfo != NULL)
{
free(Index->VBlockInfo);
}
Index->VBlockInfo = NULL;
Index->BlockSum = 0;
}
return DISK_RETURN_OK;
}
/*********************************************************************************************************
** Function name: FFSFreeSector
** Descriptions: 释放扇区 free sector
** Input:Index: 底层驱动信息 low driver info
** SecIndex:扇区索引 sector index
** Output: TRUE: 成功 OK
** FALSE: 失败 NOT OK
**
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 FFSFreeSector(FFSDisk *Index, uint32 SecIndex)
{
uint32 VBlockIndex, BlockIndex;
unsigned int i;
const FlashDriver *Drive;
uint8 *Buf;
Drive = Index->Drive;
VBlockIndex = SecIndex / Drive->SecsPreBlock;
Buf = Drive->BufA;
if (Index != NULL)
if (Drive != NULL)
if (VBlockIndex < Index->BlockSum)
{
BlockIndex = Index->VBlockInfo[VBlockIndex]; /* 获得物理块号 */
if (BlockIndex < Index->BlockSum)
if (BlockIndex != 0)
{
SecIndex = SecIndex % Drive->SecsPreBlock; /* 获得块内偏移 */
SecIndex = SecIndex + BlockIndex * Drive->SecsPreBlock; /* 获得物理扇区号 */
if (Drive->ExSectorRead(Drive->Index, Buf, SecIndex) == TRUE)
{
if (Buf[0] != 0xff ||
Buf[1] != 0xff ||
Buf[2] != 0xff ||
Buf[3] != 0xff)
{
Buf[0] = 0x00;
Buf[1] = 0x00;
Buf[2] = 0x00;
Buf[3] = 0x00;
if (Drive->ExSectorWrite(Drive->Index, Buf, SecIndex) != TRUE)
{
goto err;
}
if (Drive->ExSectorCheck(Drive->Index, Buf, SecIndex) != TRUE)
{
goto err;
}
}
SecIndex = BlockIndex * Drive->SecsPreBlock;
i = Drive->SecsPreBlock;
do
{
if (Drive->ExSectorRead(Drive->Index, Buf, SecIndex) == FALSE)
{
goto err;
}
if (Buf[0] == 0x00 &&
Buf[1] == 0x00 &&
Buf[2] == 0x00 &&
Buf[3] == 0x00)
{
SecIndex++;
continue;
}
if (Buf[0] == 0xff &&
Buf[1] == 0xff &&
Buf[2] == 0xff &&
Buf[3] == 0xff)
{
SecIndex++;
continue;
}
break;
} while (--i != 0);
if (i == 0)
{
Index->VBlockInfo[VBlockIndex] = 0x00000000;
Index->BlockState[BlockIndex] = FREE_BLOCK;
if (FFSBlockErase(Index, BlockIndex) != TRUE)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -