?? zlg_ffs.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: ZLG_FFS.c
** Last modified Date: 2005-10-15
** Last Version: 1.0
** Descriptions: ZLG/FFS V1.0 (flash files system for NAND flash)
**
**------------------------------------------------------------------------------------------------------
** Created by: Chenmingji
** Created date: 2005-10-15
** Version: 1.0
** Descriptions: The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#define IN_ZLG_FFS
#include "config.h"
//#include <stdlib.h>
#define BAD_BLOCK 0
#define FREE_BLOCK 1
#define USR_BLOCK 2
#define UN_FINISH_BLOCK 4
#define DELET_BLOCK 8
#define USR_BLOCK_ADD 0x20
/*********************************************************************************************************
** Function name: FFSSetBadBlock
** Descriptions: 把指定塊標記為壞塊 set bad block
** Input:Index: 底層驅動信息 low driver info
** BlockIndex: 塊編號 block index
** Output: TRUE: 成功 OK
** FALSE: 失敗 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 FFSSetBadBlock(FFSDisk *Index, uint16 BlockIndex)
{
uint8 *bufa;
const FlashDriver *Drive;
unsigned int BytsPerSec;
if (Index != NULL)
{
Drive = Index->Drive;
bufa = (uint8 *)(Drive->BufA);
BytsPerSec = Drive->BytsPerSec;
if (Drive->SectorRead(Drive->Index, bufa, BlockIndex / (BytsPerSec * 8)) == TRUE)
{
bufa[(BlockIndex / 8) % BytsPerSec] &= ~(1 << (BlockIndex % 8));
Index->BlockState[BlockIndex] = BAD_BLOCK;
Index->BlockEaseSum[BlockIndex] = ~0;
return Drive->SectorWrite(Drive->Index, bufa, BlockIndex / (BytsPerSec * 8));
}
}
return FALSE;
}
/*********************************************************************************************************
** Function name: FFSBlockErase
** Descriptions: 擦除指定塊塊 erase block
** Input:Index: 底層驅動信息 low driver info
** BlockIndex: 塊編號 block index
** Output: TRUE: 成功 OK
** FALSE: 失敗 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
/* 扇區擴展數據結構: */
/* buf[0..3]: 塊索引,0x00000000和0xffffffff為空閑 */
/* buf[4..4]: 有效標志:'Z' */
/* buf[5..5]: 使用標志:0:已經使用 */
/* buf[6..7]: 保留原始狀態 */
/* buf[8..11]: 擦除次數,低字節在前 */
/* buf[12..15]:保留 */
static uint8 FFSBlockErase(FFSDisk *Index, uint16 BlockIndex)
{
const FlashDriver *Drive;
uint32 temp1, temp2;
uint8 *BufA;
Drive = Index->Drive;
BufA = Drive->BufA;
if (Drive->ExSectorRead(Drive->Index, BufA, BlockIndex * (Drive->SecsPreBlock)) == FALSE)
{
goto err;
}
BufA[0] = 0xff;
BufA[1] = 0xff;
BufA[2] = 0xff;
BufA[3] = 0xff;
temp1 = (BufA[11] << 8) | BufA[10];
temp1 = temp1 << 16;
temp1 |= ((BufA[9] << 8) | BufA[8]);
if (BufA[4] == 'Z')
{
temp1++;
}
else
{
temp1 = 0;
}
Drive->BufA[4] = 'Z';
Drive->BufA[5] = 0xff;
BufA[8] = temp1;
BufA[9] = temp1 >> 8;
BufA[10] = temp1 >> 16;
BufA[11] = temp1 >> 24;
Index->BlockEaseSum[BlockIndex] = temp1;
Index->BlockState[BlockIndex] = FREE_BLOCK;
if (Drive->BlockErase(Drive->Index, BlockIndex * Drive->SecsPreBlock) == FALSE)
{
goto err;
}
temp1 = Drive->SecsPreBlock;
temp2 = BlockIndex * temp1;
do
{
if (Drive->ExSectorWrite(Drive->Index, BufA, temp2) == FALSE)
{
goto err;
}
if (Drive->ExSectorCheck(Drive->Index, BufA, temp2++) == FALSE)
{
goto err;
}
} while (--temp1 != 0);
return TRUE;
err:
return FFSSetBadBlock(Index, BlockIndex);
}
/*********************************************************************************************************
** Function name: FFSSetBadBlockI
** Descriptions: 把指定塊標記為壞塊 set bad block
** Input:Index: 底層驅動信息 low driver info
** BlockIndex: 塊編號 block index
** Output: TRUE: 成功 OK
** FALSE: 失敗 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 FFSSetBadBlockI(FFSDisk *Index, uint16 BlockIndex)
{
uint8 *bufa;
const FlashDriver *Drive;
unsigned int BytsPerSec;
if (Index != NULL)
{
Drive = Index->Drive;
bufa = (uint8 *)(Drive->BufA);
BytsPerSec = Drive->BytsPerSec;
if (Drive->SectorRead(Drive->Index, bufa, BlockIndex / (BytsPerSec * 8)) == TRUE)
{
bufa[(BlockIndex / 8) % BytsPerSec] &= ~(1 << (BlockIndex % 8));
return Drive->SectorWrite(Drive->Index, bufa, BlockIndex / (BytsPerSec * 8));
}
}
return FALSE;
}
/*********************************************************************************************************
** Function name: FFSBlockEraseI
** Descriptions: 擦除指定塊塊 erase block
** Input:Index: 底層驅動信息 low driver info
** BlockIndex: 塊編號 block index
** Output: TRUE: 成功 OK
** FALSE: 失敗 NOT OK
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
static uint8 FFSBlockEraseI(FFSDisk *Index, uint16 BlockIndex)
{
const FlashDriver *Drive;
uint32 temp1, temp2;
uint8 *BufA;
Drive = Index->Drive;
BufA = Drive->BufA;
if (Drive->ExSectorRead(Drive->Index, BufA, BlockIndex * (Drive->SecsPreBlock)) == FALSE)
{
goto err;
}
BufA[0] = 0xff;
BufA[1] = 0xff;
BufA[2] = 0xff;
BufA[3] = 0xff;
Drive->BufA[4] = 'Z';
Drive->BufA[5] = 0xff;
BufA[8] = 0;
BufA[9] = 0;
BufA[10] = 0;
BufA[11] = 0;
if (Drive->BlockErase(Drive->Index, BlockIndex * Drive->SecsPreBlock) == FALSE)
{
goto err;
}
temp1 = Drive->SecsPreBlock;
temp2 = BlockIndex * temp1;
do
{
if (Drive->ExSectorWrite(Drive->Index, BufA, temp2) == FALSE)
{
goto err;
}
if (Drive->ExSectorCheck(Drive->Index, BufA, temp2++) == FALSE)
{
goto err;
}
} while (--temp1 != 0);
return TRUE;
err:
return FFSSetBadBlockI(Index, BlockIndex);
}
/*********************************************************************************************************
** Function name: NFFormatB
** Descriptions: 格式化ZLG/FFS格式的FLASH Format flash that is ZLG/FFS format
** 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 NFFormatB(FFSDisk *Index)
{
uint32 i, BlockPreDisk;
unsigned int BytsPerSec;
unsigned int k;
const FlashDriver *Drive;
uint8 *bufa;
if (Index == NULL)
{
return FALSE;
}
Drive = Index->Drive;
bufa = Drive->BufA;
BytsPerSec = Drive->BytsPerSec;
BlockPreDisk = Drive->BlockPreDisk;
/* 初始化扇區 */
k = 0;
Drive->SectorRead(Drive->Index, bufa, 0);
for (i = 1; i < BlockPreDisk; i++)
{
if ((i % (BytsPerSec * 8)) == 0)
{
k++;
Drive->SectorRead(Drive->Index, bufa, k);
}
if ((bufa[(i / 8) % BytsPerSec] & (1 << (i % 8))) != 0)
{
FFSBlockErase(Index, i);
Drive->SectorRead(Drive->Index, bufa, k);
}
}
/* 初始化第0扇區 */
k = 0;
bufa = Drive->BufA;
Drive->SectorRead(Drive->Index, bufa, 0);
for (i = 1; i < Drive->BlockPreDisk; i++)
{
if ((i % (Drive->BytsPerSec * 8)) == 0)
{
k++;
Drive->SectorRead(Drive->Index, bufa, k);
}
if ((bufa[(i / 8) % (Drive->BytsPerSec)] & (1 << (i % 8))) != 0)
{
i = (Drive->SecsPreBlock) * i;
Drive->ExSectorRead(Drive->Index, bufa, i);
bufa[0] = 0x01;
bufa[1] = 0x00;
bufa[2] = 0x00;
bufa[3] = 0x00;
if (Drive->ExSectorWrite(Drive->Index, bufa, i) == FALSE)
{
FFSBlockErase(Index, i / Drive->SecsPreBlock);
break;
}
if (Drive->ExSectorCheck(Drive->Index, bufa, i) == FALSE)
{
FFSBlockEraseI(Index, i / Drive->SecsPreBlock );
break;
}
break;
}
}
return TRUE;
}
/*********************************************************************************************************
** Function name: NFFormatA
** Descriptions: 將非ZLG/FFS格式FLASH格式化成ZLG/FFS格式
The flash that is not ZLG/FFS format becomes the format of ZLG/FFS
** Input:Index: 底層驅動信息 low driver info
**
** Output: TRUE: 成功 OK
** FALSE: 失敗 NOT OK
**
** Created by: chenmingji
** Created Date: 2005-10-15
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
const static uint8 logo[16] = {'Z', 'L', 'G', '\\', 'F', 'F', 'S', 0,
'V', 0, 1, 0, 1, 0, 0, 0 };
const static uint8 logo1[16] = {0xb3, 0xc2, 0xc3, 0xf7, 0xbc, 0xc6, 0, 0,
'2', '0', '0', '4', '1', '1', 0, 0 };
static uint8 NFFormatA(FFSDisk *Index)
{
uint32 i, temp;
unsigned int j;
const FlashDriver *Drive;
uint8 *bufa;
Drive = Index->Drive;
temp = Drive->BlockPreDisk;
/* 設置壞塊表 */
Drive->BlockErase(Drive->Index, 0);
bufa = Drive->BufA;
j = Drive->BytsPerSec;
do
{
*bufa++ = 0xff;
} while ( --j != 0);
bufa = Drive->BufA;
for (i = 1; i < Drive->BlockPreDisk; i++)
{
if ((Drive->BlockCheck(Drive->Index, i)) != TRUE)
{
bufa[(i / 8) % (Drive->BytsPerSec)] &= ~(1 << (i % 8));
}
if ((i % (Drive->BytsPerSec * 8)) == 0)
{
bufa = Drive->BufA;
Drive->SectorWrite(Drive->Index, bufa, i / (Drive->BytsPerSec * 8));
j = Drive->BytsPerSec;
do
{
*bufa++ = 0xff;
} while (--j != 0);
bufa = Drive->BufA;
}
}
Drive->SectorWrite(Drive->Index, bufa, i / (Drive->BytsPerSec * 8));
/* 設置logo */
Drive->ExSectorWrite(Drive->Index, (uint8 *)logo, 0);
Drive->ExSectorWrite(Drive->Index, (uint8 *)logo, Drive->SecsPreBlock - 1);
return NFFormatB(Index);
}
/*********************************************************************************************************
** Function name: NFFormat
** Descriptions: 格式化FLASH format flash
** Input:Index: 底層驅動信息 low driver info
**
** Output: TRUE: 成功 OK
** FALSE: 失敗 NOT OK
**
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -