flashmenu.c
来自「mx27 f14v2 源代码。包括ADS板上诸多驱动的源码。」· C语言 代码 · 共 1,008 行 · 第 1/2 页
C
1,008 行
{
OALLog(L"\r\n Sector Number: ");
if (OALBLMenuReadLine(szInputLine, dimof(szInputLine)) == 0)
{
break;
}
// Get sector number
sector = OALStringToUINT32(szInputLine);
// Check sector number
if (sector > flashInfo.dwNumBlocks * flashInfo.wSectorsPerBlock)
{
OALLog(L" Oops, too big sector number\r\n");
continue;
}
if (!FMD_ReadSector(sector, buffer, §orInfo, 1))
{
OALLog(L" Oops, sector read failed\r\n");
continue;
}
OALLog(
L"\r\nSector %d (sector %d in block %d)\r\n", sector,
sector%flashInfo.wSectorsPerBlock, sector/flashInfo.wSectorsPerBlock
);
OALLog(
L"Reserved1: %08x OEMReserved: %02x Bad: %02x Reserved2: %04x\r\n",
sectorInfo.dwReserved1, sectorInfo.bOEMReserved,
sectorInfo.bBadBlock, sectorInfo.wReserved2
);
for (i = 0; i < flashInfo.wDataBytesPerSector; i += 16)
{
OALLog(L"%04x ", i);
for (j = i; j < i + 16 && j < flashInfo.wDataBytesPerSector; j++)
{
OALLog(L" %02x", buffer[j]);
}
OALLog(L" ");
for (j = i; j < i + 16 && j < flashInfo.wDataBytesPerSector; j++)
{
if (buffer[j] >= ' ' && buffer[j] < 127)
{
OALLog(L"%c", buffer[j]);
}
else
{
OALLog(L".");
}
}
OALLog(L"\r\n");
}
}
cleanUp:
if (hFMD != NULL)
{
FMD_Deinit(hFMD);
}
return;
}
//------------------------------------------------------------------------------
VOID SetBadBlock(void)
{
HANDLE hFMD = NULL;
FlashInfo flashInfo;
BLOCK_ID blockId;
WCHAR szInputLine[16];
// Open FMD
hFMD = FMD_Init(NULL, NULL, NULL);
if (hFMD == NULL)
{
OALLog(L" Oops, can't open FMD driver\r\n");
goto cleanUp;
}
if (!FMD_GetInfo(&flashInfo))
{
OALLog(L" Oops, can't get flash geometry info\r\n");
goto cleanUp;
}
OALLog(L"\r\n Block Number: ");
if (OALBLMenuReadLine(szInputLine, dimof(szInputLine)) == 0)
{
goto cleanUp;
}
// Get sector number
blockId = OALStringToUINT32(szInputLine);
// Check sector number
if (blockId >= flashInfo.dwNumBlocks)
{
OALLog(L" Oops, too big block number\r\n");
goto cleanUp;
}
FMD_SetBlockStatus(blockId, BLOCK_STATUS_BAD);
OALLog(L"\r\n Done\r\n");
cleanUp:
if (hFMD != NULL)
{
FMD_Deinit(hFMD);
}
return;
}
VOID ReservedBlocks(void)
{
BLOCK_ID firstblock=0;
int noOfBlocks;
WCHAR szInputLine[16];
OALLog(L"\r\n First Block Number: ");
if (OALBLMenuReadLine(szInputLine, dimof(szInputLine)) == 0)
{
goto cleanUp;
}
// Get block number
firstblock = OALStringToUINT32(szInputLine);
OALLog(L"\r\n No of Blocks to reserve: ");
if (OALBLMenuReadLine(szInputLine, dimof(szInputLine)) == 0)
{
goto cleanUp;
}
// Get block number
noOfBlocks = OALStringToUINT32(szInputLine);
ReservedSpareArea(firstblock, noOfBlocks); // Reserved blocks
return;
cleanUp:
OALLog(L"Set/Clear Reserved Blocks cancel\r\n");
return;
}
VOID DumpBlocks(void)
{
HANDLE hFMD = NULL;
FlashInfo flashInfo;
BLOCK_ID firstblock, lastblock=0;
WCHAR szInputLine[16];
SectorInfo sectorInfo;
SECTOR_ADDR start_sector;
SECTOR_ADDR end_sector;
SECTOR_ADDR sector;
UINT32 i, j;
UINT8 buffer[512];
OALLog(L"\r\n First Block Number: ");
if (OALBLMenuReadLine(szInputLine, dimof(szInputLine)) == 0)
{
goto cleanUp;
}
// Get block number
firstblock = OALStringToUINT32(szInputLine);
OALLog(L"\r\n Last Block Number: ");
if (OALBLMenuReadLine(szInputLine, dimof(szInputLine)) != 0)
{
// Get block number
lastblock = OALStringToUINT32(szInputLine);
}
if (lastblock < firstblock)
{
lastblock=firstblock;
}
// Open FMD
hFMD = FMD_Init(NULL, NULL, NULL);
if (hFMD == NULL)
{
OALLog(L" Oops, can't open FMD driver\r\n");
goto cleanUp;
}
if (!FMD_GetInfo(&flashInfo))
{
OALLog(L" Oops, can't get flash geometry info\r\n");
goto cleanUp;
}
if (lastblock >= flashInfo.dwNumBlocks)
{
OALLog(L" Oops, too big block number\r\n");
goto cleanUp;
}
start_sector = firstblock * flashInfo.wSectorsPerBlock;
end_sector = (lastblock+1) * flashInfo.wSectorsPerBlock;
for (sector = start_sector; sector < end_sector; sector++)
{
// Check sector number
if (sector > flashInfo.dwNumBlocks * flashInfo.wSectorsPerBlock)
{
OALLog(L" Oops, too big sector number\r\n");
continue;
}
if (!FMD_ReadSector(sector, buffer, §orInfo, 1))
{
OALLog(L" Oops, sector read failed\r\n");
continue;
}
OALLog(
L"\r\nSector %d (sector %d in block %d)\r\n", sector,
sector%flashInfo.wSectorsPerBlock, sector/flashInfo.wSectorsPerBlock
);
OALLog(
L"Reserved1: %08x OEMReserved: %02x Bad: %02x Reserved2: %04x\r\n",
sectorInfo.dwReserved1, sectorInfo.bOEMReserved,
sectorInfo.bBadBlock, sectorInfo.wReserved2
);
for (i = 0; i < flashInfo.wDataBytesPerSector; i += 16)
{
OALLog(L"%04x ", i);
for (j = i; j < i + 16 && j < flashInfo.wDataBytesPerSector; j++)
{
OALLog(L" %02x", buffer[j]);
}
OALLog(L" ");
for (j = i; j < i + 16 && j < flashInfo.wDataBytesPerSector; j++)
{
if (buffer[j] >= ' ' && buffer[j] < 127)
{
OALLog(L"%c", buffer[j]);
}
else
{
OALLog(L".");
}
}
OALLog(L"\r\n");
}
}
OALLog(L" Done\r\n");
cleanUp:
if (hFMD != NULL) FMD_Deinit(hFMD);
return;
}
//------------------------------------------------------------------------------
UINT32 OALStringToUINT32(LPCWSTR psz)
{
UINT32 i = 0;
// Replace the dots with NULL terminators
while (psz != NULL && *psz != L'\0')
{
if (*psz < L'0' || *psz > L'9')
{
break;
}
i = i * 10 + (*psz - L'0');
psz++;
}
return i;
}
#if 1
static BOOL EraseBlock(BLOCK_ID block)
{
BOOL rc;
UINT32 retry = 4;
// Erase block
do
{
rc = FMD_EraseBlock(block);
}
while (!rc && --retry > 0);
return rc;
}
//------------------------------------------------------------------------------
//
// Function: WriteBlock
//
static UINT8 *WriteBlock(BLOCK_ID block, UINT8 *pDataBuffer, FlashInfo * pFlashInfo, BOOL fSectorInfo)
{
UINT32 count;
UINT32 sectorSize, sectorsPerBlock;
SectorInfo sectorInfo;
SECTOR_ADDR sector;
UINT32 nextSectorSize;
// Get flash info
sectorSize = pFlashInfo->wDataBytesPerSector;
sectorsPerBlock = pFlashInfo->wSectorsPerBlock;
// Erase block
if (!EraseBlock(block))
{
// Erase failed, mark block as bad
// FMD_SetBlockStatus(block, BLOCK_STATUS_BAD);
EdbgOutputDebugString("ERROR: WriteBlock: Erase block %d failed!\r\n",
block);
// EdbgOutputDebugString("WARNING: Would just try to write the block directly\r\n");
return NULL;
}
// Read data from block
if (pDataBuffer != NULL)
{
sector = block * sectorsPerBlock;
for (count = 0; count < sectorsPerBlock; count++)
{
unsigned int i;
BOOL allones = TRUE;
//EdbgOutputDebugString("-");
// Prepare sector info
if (fSectorInfo)
{
memcpy(§orInfo, pDataBuffer+sectorSize,sizeof(SectorInfo));
// In case the BIN/DIO image accidentally sets bBadBlock to anything other than 0xFF...
// (this will turn a good block bad!)
if (sectorInfo.bBadBlock != 0xFF)
{
EdbgOutputDebugString("WARNING: Bad block byte is set (%B) on sector %d of block %d! Resetting to 0xFF.\r\n", sectorInfo.bBadBlock, count, block);
sectorInfo.bBadBlock = 0xFF;
}
nextSectorSize = sectorSize+sizeof(SectorInfo);
}
else
{
memset(§orInfo, 0xFF, sizeof(sectorInfo));
sectorInfo.bOEMReserved &= ~(OEM_BLOCK_READONLY|OEM_BLOCK_RESERVED);
//sectorInfo.bOEMReserved &= ~(OEM_BLOCK_READONLY);
sectorInfo.dwReserved1 = 0;
sectorInfo.wReserved2 = 0;
//RETAILMSG(1, (TEXT("Write sector with bOEMReserved = 0x%x\r\n"), sectorInfo.bOEMReserved));
nextSectorSize = sectorSize;
}
for (i=0;i<sectorSize;i++)
{
if ((*(pDataBuffer+i))!=0xff)
{
allones = FALSE;
break;
}
}
if (allones)
{
//EdbgOutputDebugString("INFO: WriteBlock: all-one write sector %d to flash\r\n",
// sector);
//dumpData((char *)(pDataBuffer+sectorSize), 8, 0);
// Do nothing here. Don't program this page. Just skip it.
}
else
{
if (!FMD_WriteSector(sector, pDataBuffer, §orInfo, 1))
{
// Write failed, mark block as bad
EdbgOutputDebugString("ERROR: WriteBlock: Failed write sector %d to flash\r\n",
sector);
//FMD_SetBlockStatus(block, BLOCK_STATUS_BAD);
return NULL;
}
}
//Move to next sector
sector++;
pDataBuffer += nextSectorSize;
}
}
else
return NULL;
return pDataBuffer;
}
//------------------------------------------------------------------------------
//
// Function: ReadBlock
//
static BOOL ReadBlock(BLOCK_ID block, UINT8 *pDataBuffer, FlashInfo * pFlashInfo)
{
BOOL rc = FALSE;
UINT32 count;
UINT32 sectorSize, sectorsPerBlock;
SectorInfo sectorInfo;
SECTOR_ADDR sector;
// Get flash info
sectorSize = pFlashInfo->wDataBytesPerSector;
sectorsPerBlock = pFlashInfo->wSectorsPerBlock;
// Read data from block
if (pDataBuffer != NULL)
{
sector = block * sectorsPerBlock;
for (count = 0; count < sectorsPerBlock; count++)
{
// EdbgOutputDebugString("FMD_ReadSector: sector=%d\r\n", sector);
if (!FMD_ReadSector(sector, pDataBuffer, §orInfo, 1))
{
// When block read fail, there isn't much we can do more
EdbgOutputDebugString("ERROR: ReadBlock: Failed read sector %d from flash\r\n",
sector);
return rc;
}
// Move to next sector
sector++;
pDataBuffer += sectorSize;
}
}
else
return rc;
return TRUE;
}
BOOL ReservedSpareArea(BLOCK_ID start, int NoOfBlocks)
{
PVOID hFMD = NULL;
FlashInfo flashInfo;
UINT32 blockSize;
BLOCK_ID block = start;
UINT16 count;
UINT8 *pNextData;
BLOCK_ID nextBlock;
DWORD oemReserved = (BLOCK_STATUS_READONLY|BLOCK_STATUS_RESERVED);
volatile UINT8 *pDataBuffer = (UINT8 *)OALPAtoUA(IMAGE_BOOT_NKIMAGE_RAM_PA_START);
//EdbgOutputDebugString("+ReservedSpareArea: startBlockID = %d NoOfBlocks = %d\r\n", start, NoOfBlocks);
hFMD = FMD_Init((_T("FMD")), NULL, NULL);
if (hFMD == NULL)
{
OALMSG(1, (_T("FlashInit Failed!\r\n")));
return FALSE;
}
// Get flash info
if (FMD_GetInfo(&flashInfo) == FALSE)
{
OALMSG(1,(_T("ERROR: LoadImage: FMD_GetInfo call failed!\r\n")));
return FALSE;
}
blockSize = flashInfo.dwBytesPerBlock;
nextBlock = start;
for (count=0; count < NoOfBlocks; count++)
{
if (!ReadBlock(block, (UINT8 *)pDataBuffer, &flashInfo))
{
EdbgOutputDebugString("Failure to read block %d\r\n", block);
return FALSE;
}
//EdbgOutputDebugString("Write block %d\r\n", block);
pNextData = WriteBlock(block, (UINT8 *)pDataBuffer, &flashInfo, FALSE);
if (pNextData == NULL)
{
EdbgOutputDebugString("Failure to write block %d\r\n", block);
return FALSE;
}
//pDataBuffer = pNextData;
nextBlock++;
}
return TRUE;
}
#endif
//------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?