读取DBAU1200开发板的BOOT FLASH参考代码

//========================================================================
//TITLE:
// 读取DBAU1200开发板的BOOT FLASH参考代码
//AUTHOR:
// norains
//DATE:
// Saturday 31-May-2008
//Environment:
// VS2005 + DBAU1200 BOARD + MIPSII SDK
//========================================================================

该段代码用来读取DBAU1200开发板的BOOT FLASH,并保存为SREC格式。

CFlashAmd主要是用来读取BOOT FLASH,该类可在此获取:http://blog.csdn.net/norains/archive/2008/05/31/2498178.aspx

需要注意的是,本文所列的代码为参考代码,还有很多不完善的地方,比如读取的FLASH大小必须为16的整数倍等等,使用的时候需要小心。


#include "stdafx.h"
#include "flashamd.h"
#include <vector>
#include <string>
#include <algorithm>

//DBAU1200开发板的FLASH起始地址
#define DEFAULT_DBAU1200_FLASH_ADDRESS_BASE 0x9C000000
//读取的偏移地址
#define DEFAULT_DBAU1200_FLASH_OFFSET_READ 0x3C00000
//读取的FLASH大小
#define MAX_SIZE_READ (1024 * 1024) // 1Mbyte

int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
CFlashAmd flash;
flash.SetFlashInfo(Flash::FLASH_INFO_AM29LV256M);
flash.SetFlashAddressBase(DEFAULT_DBAU1200_FLASH_ADDRESS_BASE);

std::vector<BYTE> vtBuf(MAX_SIZE_READ,0);
flash.ReadFlash(DEFAULT_DBAU1200_FLASH_OFFSET_READ,&vtBuf[0],MAX_SIZE_READ);

//Change it to S3 type
const DWORD dwDataPerLine = 16;
DWORD dwStoreCount = 0;
std::string strStore;
DWORD dwChkSum = 0;
DWORD dwAddress = DEFAULT_DBAU1200_FLASH_ADDRESS_BASE + DEFAULT_DBAU1200_FLASH_OFFSET_READ;
while(true)
{
if(dwStoreCount + dwDataPerLine > vtBuf.size())
{
break;
}

//Clear the check sum.
dwChkSum = 0;

//The type and length
std::string strLine;
strLine += "S315";
dwChkSum += 0x15;

//The buffer for storing the convert string
std::vector<char> vtCnvt(9,0);

//The address
_ultoa(dwAddress,&vtCnvt[0],16);
strLine += reinterpret_cast<char *>(&vtCnvt[0]);
dwChkSum += dwAddress >> 24;
dwChkSum += (dwAddress >> 16) & 0xFF;
dwChkSum += (dwAddress >> 8) & 0xFF;
dwChkSum += dwAddress & 0xFF;
dwAddress += dwDataPerLine;



//The data
for(int i = 0; i < dwDataPerLine; i ++)
{
_ultoa(vtBuf[dwStoreCount + i],&vtCnvt[0],16);
if(vtBuf[dwStoreCount + i] > 0x0F)
{
strLine += reinterpret_cast<char *>(&vtCnvt[0]);
}
else
{
strLine = strLine + "0" + reinterpret_cast<char *>(&vtCnvt[0]);
}
dwChkSum += vtBuf[dwStoreCount + i];
}
dwStoreCount += dwDataPerLine;

//The check sum
dwChkSum = 0xFF - (dwChkSum & 0xFF);
_ultoa(dwChkSum,&vtCnvt[0],16);
if(dwChkSum > 0x0F)
{
strLine += reinterpret_cast<char *>(&vtCnvt[0]);
}
else
{
strLine = strLine + "0" + reinterpret_cast<char *>(&vtCnvt[0]);
}
strLine += "/r/n";

//Store to the buffer
strStore += strLine;
}

//to upper
transform(strStore.begin(),strStore.end(),strStore.begin(),toupper);


//Save the data to file
HANDLE hFile = CreateFile(TEXT("bootsave.rec"),GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
DWORD dwWrite = 0;
WriteFile(hFile,&strStore[0],strStore.size(),&dwWrite,NULL);
CloseHandle(hFile);
return 0;
}

阅读更多

没有更多推荐了,返回首页