📄 nor.c
字号:
ULONG j;
ULONG *p;
ULONG ulEndAddress;
ULONG ulFlashSectorAddr;
ULONG ulEraseBlockSize;
ULONG ulFlashSize;
BOOL bFail;
BOOL bRegionDone;
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORErase+ 0x%x 0x%x\r\n"), ulStartAddress, ulLen));
bIsPaired = g_FlashDesc.PairedFlash;
ulFlashBase = g_FlashDesc.FlashBase;
ulEndAddress = ulStartAddress + ulLen;
ulFlashSize = bIsPaired? 2 * (1 << g_FlashDesc.Geometry.DevSize) : (1 << g_FlashDesc.Geometry.DevSize);
if (ulStartAddress < ulFlashBase || ulEndAddress > (ulFlashBase + ulFlashSize) ) {
ERRORMSG(ZONE_ERROR, (TEXT("NORErase+: Invalid parameters! start 0x%x len 0x%x\r\n"),
ulStartAddress, ulLen));
DEBUGMSG(ZONE_INFO, (TEXT("NORErase+: Base 0x%x size 0x%x\r\n"),
ulFlashBase, ulFlashSize));
return 0;
}
ulFlashSectorAddr = ulFlashBase;
bFail = FALSE;
bRegionDone = FALSE;
ulLen = 0;
// Walk the entire device
for (i = 0; !bRegionDone && i < g_FlashDesc.Geometry.NumEraseBlockRegions
&& !bFail; i++) {
// Get erase block size
ulEraseBlockSize = g_FlashDesc.Geometry.EraseBlockInfo[i].EraseBlockSize * CFI_ERASE_BLOCK_SIZE_MULTP;
ulEraseBlockSize += bIsPaired? ulEraseBlockSize : 0;
for (j = 0;
!bRegionDone &&
j < (ULONG)(g_FlashDesc.Geometry.EraseBlockInfo[i].NumIdentEraseBlocks + 1) &&
!bFail;
j++) {
if ((ulFlashSectorAddr >= ulStartAddress) &&
((ulFlashSectorAddr + ulEraseBlockSize) > ulFlashSectorAddr) &&
ulFlashSectorAddr < ulEndAddress) {
DEBUGMSG(ZONE_FUNCTION, (TEXT("Erasing [0x%x]\r\n"), ulFlashSectorAddr));
if (!g_FlashDriver.EraseSector(&g_FlashDesc, ulFlashSectorAddr)) {
ERRORMSG(ZONE_ERROR, (TEXT("Erase ulFlashBlockAddr 0x%x failed\r\n"),
ulFlashSectorAddr));
bFail = TRUE;
break;
} else {
// Check array contents for erase value (all 1's)
for (p = (ULONG *) ulFlashSectorAddr;
p < (ULONG *) (ulFlashSectorAddr + ulEraseBlockSize);
p++) {
if (*p != 0xFFFFFFFF) {
bFail = TRUE;
break;
}
}
ulLen += ulEraseBlockSize;
PROGRESSMSG(g_bShowProgress, (TEXT("#")));
}
}
if (!bFail && ulFlashSectorAddr >= ulStartAddress &&
(ulFlashSectorAddr + ulEraseBlockSize) >= ulEndAddress)
bRegionDone = TRUE;
// Increment walk address
ulFlashSectorAddr += ulEraseBlockSize;
} // End of erase block
}
PROGRESSMSG(g_bShowProgress, (TEXT("\r\n")));
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORErase- ulLen 0x%x\r\n"), ulLen));
if (bFail)
return 0;
else
return ulLen;
}
//-----------------------------------------------------------------------------
//
// Function: NOREraseChip
//
// Erases the entire flash chip.
//
// Parameters:
// None
//
// Returns:
// FALSE on failure
//
//-----------------------------------------------------------------------------
BOOL NOREraseChip(void)
{
BOOL bSuccess;
DEBUGMSG(ZONE_FUNCTION, (TEXT("NOREraseChip+\r\n")));
if (!g_FlashDriver.EraseChip ||
!g_FlashDesc.SysInt.Typical.ChipEraseTO_ms) {
ULONG ulFlashSectorAddr;
ULONG i;
ULONG j;
ULONG ulEraseBlockSize;
BOOL bIsPaired;
DEBUGMSG(ZONE_FUNCTION,
(TEXT("NOREraseChip: Erase sector by sector\r\n")));
// Erase sector by sector
bIsPaired = g_FlashDesc.PairedFlash;
ulFlashSectorAddr = g_FlashDesc.FlashBase;
bSuccess = TRUE;
for (i = 0;
bSuccess && i < g_FlashDesc.Geometry.NumEraseBlockRegions;
i++) {
// Get erase block size
ulEraseBlockSize =
g_FlashDesc.Geometry.EraseBlockInfo[i].EraseBlockSize << 8;
ulEraseBlockSize += bIsPaired ? ulEraseBlockSize : 0;
for (j = 0;
bSuccess &&
j < (ULONG)(g_FlashDesc.Geometry.EraseBlockInfo[i].NumIdentEraseBlocks + 1);
j++) {
DEBUGMSG(ZONE_FUNCTION,
(TEXT("ulFlashSectorAddr 0x%x\r\n"), ulFlashSectorAddr));
if (!g_FlashDriver.EraseSector(&g_FlashDesc, ulFlashSectorAddr)) {
bSuccess = FALSE;
} else {
ulFlashSectorAddr += ulEraseBlockSize;
PROGRESSMSG(g_bShowProgress, (TEXT("#")));
}
}
}
PROGRESSMSG(g_bShowProgress, (TEXT("\r\n")));
}
else
bSuccess = g_FlashDriver.EraseChip(&g_FlashDesc);
DEBUGMSG(ZONE_FUNCTION, (TEXT("NOREraseChip-\r\n")));
return bSuccess;
}
//-----------------------------------------------------------------------------
//
// Function: NORLockSector
//
// Locks a sector that contains the selected address.
//
// Parameters:
// ulFlashAddress
// [in] address to lock
//
// Returns:
// FALSE if not supported or failure.
//
//-----------------------------------------------------------------------------
BOOL NORLockSector(ULONG ulFlashAddress)
{
ULONG ulFlashSectorAddr;
BOOL bStatus;
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORLockSector+ 0x%x\r\n"), ulFlashAddress));
bStatus = FALSE;
if (g_FlashDriver.LockSector) {
ulFlashSectorAddr = ulFlashAddress;
if (NORFindSector(&ulFlashSectorAddr, NULL)) {
bStatus = g_FlashDriver.LockSector(&g_FlashDesc, ulFlashSectorAddr);
} else {
ERRORMSG(ZONE_ERROR,
(TEXT("NORLockSector: invalid flash address! 0x%x!\r\n"),
ulFlashAddress));
}
} else {
DEBUGMSG(ZONE_INFO,
(TEXT("Lock sector not supported by driver.\r\n")));
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORLockSector-\r\n")));
return bStatus;
}
//-----------------------------------------------------------------------------
//
// Function: NORUnlockSector
//
// Unlocks a block that contains the selected address.
//
// Parameters:
// ulFlashAddress
// [in] address to unlock
//
// Returns:
// FALSE if not supported or failure.
//
//-----------------------------------------------------------------------------
BOOL NORUnlockSector(ULONG ulFlashAddress)
{
ULONG ulFlashSectorAddr;
BOOL bStatus;
DEBUGMSG(ZONE_FUNCTION,
(TEXT("NORUnlockSector+ 0x%x\r\n"), ulFlashAddress));
bStatus = FALSE;
if (g_FlashDriver.UnlockSector) {
ulFlashSectorAddr = ulFlashAddress;
if (NORFindSector(&ulFlashSectorAddr, NULL)) {
bStatus = g_FlashDriver.UnlockSector(&g_FlashDesc,
ulFlashSectorAddr);
} else {
ERRORMSG(ZONE_ERROR,
(TEXT("NORUnlockSector: invalid flash address! 0x%x!\r\n"),
ulFlashAddress));
}
} else {
DEBUGMSG(ZONE_INFO,
(TEXT("Unlock sector not supported by driver.\r\n")));
}
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORUnlockSector-\r\n")));
return bStatus;
}
//-----------------------------------------------------------------------------
//
// Function: NORIsAddrLocked
//
// Get lock status of a selected location
//
// Parameters:
// ulFlashAddress
// [in] address to check lock status
//
// Returns:
// FALSE on if block unlocked(default), TRUE if locked
//
//-----------------------------------------------------------------------------
BOOL NORIsAddrLocked(ULONG ulFlashAddress)
{
ULONG ulFlashSectorAddr;
BOOL bStatus;
DEBUGMSG(ZONE_FUNCTION,
(TEXT("NORIsSectorLocked+ 0x%x\r\n"), ulFlashAddress));
// TODO: assume all blocks are unlocked?
bStatus = FALSE;
if (g_FlashDriver.IsSectorLocked) {
ulFlashSectorAddr = ulFlashAddress;
if (NORFindSector(&ulFlashSectorAddr, NULL)) {
bStatus = g_FlashDriver.IsSectorLocked(&g_FlashDesc,
ulFlashSectorAddr);
} else {
ERRORMSG(ZONE_ERROR,
(TEXT("NORIsAddrLocked: invalid flash address! 0x%x!\r\n"),
ulFlashAddress));
}
}
else
DEBUGMSG(ZONE_INFO,
(TEXT("Get sector lock status not supported by driver.\r\n")));
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORIsSectorLocked-\r\n")));
return bStatus;
}
//-----------------------------------------------------------------------------
//
// Function: NORGetDeviceWidth
//
// Returns flash device and bus width information
//
// Parameters:
// bIsPaired
// [out] TRUE if flash is paired
//
// ucDeviceWidth
// [out] device width in bits
//
// Returns:
// FALSE if incorrect param
//
//-----------------------------------------------------------------------------
BOOL NORGetDeviceWidth(BOOL *pbIsPaired, ULONG *pulDeviceWidth)
{
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORGetDeviceWidth+\r\n")));
if (pbIsPaired == NULL || pulDeviceWidth == NULL) {
ERRORMSG(ZONE_ERROR, (TEXT("NORGetDeviceWidth: NULL parameter!\r\n")));
return FALSE;
}
*pbIsPaired = g_FlashDesc.PairedFlash;
*pulDeviceWidth = g_FlashDesc.DeviceWidth;
DEBUGMSG(ZONE_FUNCTION, (TEXT("NORGetDeviceWidth-\r\n")));
return TRUE;
}
//-----------------------------------------------------------------------------
//
// Function: NORGetGeometry
//
// Returns CFI flash geometry information.
//
// Parameters:
// pGeometry
// [out] pointer to CFI flash geometry struct
//
// Returns:
// FALSE if incorrect param
//
//-----------------------------------------------------------------------------
BOOL NORGetGeometry(CFI_FLASH_GEOMETRY_INFO *pGeometry)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -