📄 fmd.cpp
字号:
return (PVOID)pNFCONF;
ErrExit:
FMD_Deinit((PVOID)pNFCONF);
return 0;
}
// FMD_Deinit
//
// De-initialize the flash chip
//
BOOL FMD_Deinit(PVOID hFMD)
{
if((DWORD)hFMD != (DWORD)pNFCONF)
{
return FALSE;
}
// destroy the mutex
if (g_hMutex)
{
#ifdef NOSYSCALL
#ifndef BOOT_LOADER
SC_CloseHandle(g_hMutex);
#endif
#else
CloseHandle(g_hMutex);
#endif
}
#ifndef NOSYSCALL
#ifdef NOBINFS
// We don't have to free pNFReg and clock register.
#ifdef CEDAR_ONLY
// ++ CE 3.0 Specific Code. Not needed for 4.x +
if (g_tblFastCall.UnRegisterBlockDrv) {
g_tblFastCall.UnRegisterBlockDrv(g_tblFastCall.pContext, PowerOnCallback);
}
// -- CE 3.0 Specific Code. Not needed for 4.x +
#endif // CEDAR_ONLY
// Close the handle
if (g_hUTLObject) {
CloseHandle(g_hUTLObject);
}
#else
if(pNFReg) {
VirtualFree(pNFReg, 0, MEM_RELEASE);
}
if(v_s2410CLKPWR) {
VirtualFree((LPVOID)v_s2410CLKPWR, 0, MEM_RELEASE);
}
#endif // NOBINFS
#endif
return TRUE;
}
// FMD_GetInfo
//
// Return the Flash information
//
BOOL FMD_GetInfo(PFlashInfo pFlashInfo)
{
pFlashInfo->flashType = NAND;
// OK, instead of reading it from the chip, we use the hardcoded
// numbers here.
pFlashInfo->dwNumBlocks = NUM_BLOCKS;
pFlashInfo->wSectorsPerBlock = PAGES_PER_BLOCK;
pFlashInfo->wDataBytesPerSector = SECTOR_SIZE;
return TRUE;
}
// FMD_ReadSector
//
// Read the content of the sector.
//
// startSectorAddr: Starting page address
// pSectorBuff : Buffer for the data portion
// pSectorInfoBuff: Buffer for Sector Info structure
// dwNumSectors : Number of sectors
//
typedef union _ECCRegVal
{
DWORD dwECCVal;
BYTE bECCBuf[4];
} ECCRegVal;
BOOL FMD_ReadSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff,
PSectorInfo pSectorInfoBuff, DWORD dwNumSectors)
{
DWORD i;
BYTE eccBuf[8];
ECCRegVal eccRegVal;
// RETAILMSG(1, (TEXT("FMD_ReadSector!\n")));
//RETAILMSG(1, (TEXT("startSectorAddr = %x, pSectorBuff = %x, pSectorInfoBuff = %x, dwNumSectors = %x\n"), startSectorAddr, pSectorBuff, pSectorInfoBuff, dwNumSectors));
// Sanity check
if (!pSectorBuff && !pSectorInfoBuff || dwNumSectors > 1) {
#ifdef BOOT_LOADER
EdbgOutputDebugString("Invalid parameters!\r\n");
#else
RETAILMSG(1, (TEXT("Invalid parameters!\n")));
#endif
#ifndef NOSYSCALL
SetLastError(ERROR_INVALID_PARAMETER);
#endif
return FALSE;
}
NF_Reset();
if(!pSectorBuff) {
// We are reading spare only
NAND_ReadSectorInfo(startSectorAddr, pSectorInfoBuff);
// There is no ECC for the sector info, so the read always succeed.
return TRUE;
}
GRABMUTEX();
// Initialize ECC register
NF_RSTECC();
// Enable the chip
NF_CE_L();
// Issue command
NF_CMD(CMD_READ);
// Set up address
NF_ADDR(0x00);
NF_ADDR((startSectorAddr) & 0xff);
NF_ADDR((startSectorAddr >> 8) & 0xff);
if (NEED_EXT_ADDR) {
NF_ADDR((startSectorAddr >> 16) & 0xff);
}
NF_WAITRB();
// BUGBUG, because Media Player for Pocket PC sometimes pass us un-aligned buffer
// we have to waste cycle here to work around this problem
if( ((DWORD) pSectorBuff) & 0x3) {
for(i=0; i<SECTOR_SIZE; i++) {
pSectorBuff[i] = (BYTE) NF_DATA_R();
}
}
else {
// The right way.
ReadPage512(pSectorBuff, pNFDATA);
}
// Do the ECC thing here
// We read the ECC value from the ECC register pFNECC
eccRegVal.dwECCVal = NF_ECC();
// Read the SectorInfo data
if(pSectorInfoBuff) {
// Read the SectorInfo data (we only need to read first 8 bytes)
pSectorInfoBuff->dwReserved1 = (DWORD) ((BYTE) NF_DATA_R()) << 24;
pSectorInfoBuff->dwReserved1 |= (DWORD) ((BYTE) NF_DATA_R()) << 16;
pSectorInfoBuff->dwReserved1 |= (DWORD) ((BYTE) NF_DATA_R()) << 8;
pSectorInfoBuff->dwReserved1 |= (DWORD) ((BYTE) NF_DATA_R());
// OEM byte
pSectorInfoBuff->bOEMReserved = (BYTE) NF_DATA_R();
// Read the bad block mark
pSectorInfoBuff->bBadBlock = (BYTE) NF_DATA_R();
// Second reserved field (WORD)
pSectorInfoBuff->wReserved2 = ((BYTE) NF_DATA_R() << 8);
pSectorInfoBuff->wReserved2 |= ((BYTE) NF_DATA_R());
}
else {
// Advance the read pointer
for(i=0; i<sizeof(SectorInfo); i++) {
eccBuf[i] = (BYTE) NF_DATA_R();
}
}
// Verify the ECC values
//
// Read the ECC buffer
for(i=0; i<3; i++) {
eccBuf[i] = (BYTE) NF_DATA_R();
}
NF_CE_H();
// Copmare with the ECC generated from the HW
if(eccBuf[0] != eccRegVal.bECCBuf[0] ||
eccBuf[1] != eccRegVal.bECCBuf[1] ||
eccBuf[2] != eccRegVal.bECCBuf[2] ) {
#ifdef BOOT_LOADER
EdbgOutputDebugString("FMD: ECC ERROR - Page #: %d\r\n", startSectorAddr);
#else
RETAILMSG(1, (TEXT("FMD: ECC ERROR - Page #: %d\r\n"), startSectorAddr));
#endif
// Now try to correct them
if(!ECC_CorrectData(pSectorBuff, eccBuf, eccRegVal.bECCBuf)) {
RETAILMSG(1, (TEXT("FMD: Unable to correct the ECC error - Page #: %d\r\n"),
startSectorAddr));
RELEASEMUTEX();
return FALSE;
}
}
RELEASEMUTEX();
return TRUE;
}
//
// IsBlockBad
//
// Check to see if the given block is bad. A block is bad if the 517th byte on
// the first or second page is not 0xff.
//
// blockID: The block address. We need to convert this to page address
//
//
BOOL IsBlockBad(BLOCK_ID blockID)
{
DWORD dwPageID = blockID << 5;
BOOL bRet = FALSE;
BYTE wFlag;
GRABMUTEX();
// Enable the chip
NF_CE_L();
// Issue the command
NF_CMD(CMD_READ2);
// Set up address
NF_ADDR(VALIDADDR);
NF_ADDR((dwPageID) & 0xff);
NF_ADDR((dwPageID >> 8) & 0xff);
if (NEED_EXT_ADDR) {
NF_ADDR((dwPageID >> 16) & 0xff);
}
// Wait for Ready bit
NF_WAITRB();
// Now get the byte we want
wFlag = (BYTE) NF_DATA_R();
if(wFlag != 0xff) {
bRet = TRUE;
}
// Disable the chip
NF_CE_H();
RELEASEMUTEX();
return bRet;
}
//
// FMD_GetBlockStatus
//
// Returns the status of a block. The status information is stored in the spare area of the first sector for
// the respective block.
//
// A block is BAD if the bBadBlock byte on the first page is not equal to 0xff.
//
DWORD FMD_GetBlockStatus(BLOCK_ID blockID)
{
SECTOR_ADDR sectorAddr = blockID << LOG_2_PAGES_PER_BLOCK;
SectorInfo SI;
DWORD dwResult = 0;
if(!FMD_ReadSector(sectorAddr, NULL, &SI, 1))
{
return BLOCK_STATUS_UNKNOWN;
}
if(!(SI.bOEMReserved & OEM_BLOCK_READONLY))
{
dwResult |= BLOCK_STATUS_READONLY;
}
if(SI.bBadBlock != 0xFF)
{
dwResult |= BLOCK_STATUS_BAD;
}
return dwResult;
}
// FMD_EraseBlock
//
// Erase the given block
//
BOOL FMD_EraseBlock(BLOCK_ID blockID)
{
BOOL bRet = TRUE;
DWORD dwPageID = blockID << 5;
// RETAILMSG(1, (TEXT("***** Erasing block %d!\n"), blockID));
GRABMUTEX();
#ifndef NOSYSCALL
if(blockID < IMAGE_START_BLOCK)
{
bRet = FALSE;
RELEASEMUTEX();
return bRet;
}
#endif
// Enable the chip
NF_CE_L();
// Issue command
NF_CMD(CMD_ERASE);
// Set up address
NF_ADDR((dwPageID) & 0xff);
NF_ADDR((dwPageID >> 8) & 0xff);
if (NEED_EXT_ADDR) {
NF_ADDR((dwPageID >> 16) & 0xff);
}
// Complete erase operation
NF_CMD(CMD_ERASE2);
// Wait for ready bit
NF_WAITRB();
// Check the status
NF_CMD(CMD_STATUS);
if(NF_DATA_R() & STATUS_ERROR) {
RETAILMSG(1, (TEXT("######## Error Erasing block %d!\n"), blockID));
bRet = FALSE;
}
NF_CE_H();
RELEASEMUTEX();
#if 0
{
DWORD i;
SectorInfo si;
si.bOEMReserved = 0;
si.bBadBlock = 0xFF;
si.dwReserved1 = 0;
si.wReserved2 = 0;
for ( i = 0; i < 32; i++ ) FMD_WriteSector(blockID*32 + i, NULL, &si, 1);
for ( i = 0; i < 32; i++ )
{
FMD_ReadSector(blockID*32+i, NULL, &si, 1);
RETAILMSG(1, (TEXT("FlashDrv!FMD!NAND_ReadSectorInfo(0x%x : 0x%x, 0x%x, 0x%x, 0x%x))\r\n"), blockID*32+i, si.dwReserved1, si.bOEMReserved, si.bBadBlock, si.wReserved2));
}
}
#endif
return bRet;
}
// FMD_WriteSector
//
// Write dwNumPages pages to the startSectorAddr
//
BOOL FMD_WriteSector(SECTOR_ADDR startSectorAddr, LPBYTE pSectorBuff, PSectorInfo pSectorInfoBuff,
DWORD dwNumSectors)
{
DWORD i;
BOOL bRet = TRUE;
DWORD dwECCVal;
BYTE eccBuf[4];
// RETAILMSG(1, (TEXT("FMD_WriteSector:startSectorAddr = %x, dwNumSectors = %x \r\n"), startSectorAddr, dwNumSectors));
// Sanity check
// BUGBUGBUG: I need to come back to support dwNumSectors > 1
//
if((!pSectorBuff && !pSectorInfoBuff) || dwNumSectors != 1) {
#ifdef BOOT_LOADER
EdbgOutputDebugString("Invalid parameters!\r\n");
#else
RETAILMSG(1, (TEXT("Invalid parameters!\n")));
#endif
#ifndef NOSYSCALL
SetLastError(ERROR_INVALID_PARAMETER);
#endif
return FALSE;
}
NF_Reset();
if(!pSectorBuff) {
// RETAILMSG(1, (TEXT("pSectorBuff = %x \r\n"), pSectorBuff));
// If we are asked just to write the SectorInfo, we will do that separately
bRet = NAND_WriteSectorInfo(startSectorAddr, pSectorInfoBuff);
return bRet; // Do not write the actual sector information...
}
// RETAILMSG(1, (TEXT("write sector start \r\n")));
GRABMUTEX();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -