📄 viewbin.c
字号:
return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL WriteSRERecord (const RECORD_DATA *rec){
DWORD dwRecAddr, dwRecLen;
DWORD dwRead, dwReadReq;
DWORD dwWrite;
DWORD chksum = 0;
BOOL fRet;
char szAscii[256] = {0};
DWORD i, j;
if (hSRE == INVALID_HANDLE_VALUE)
return FALSE;
dwRecLen = rec->dwLength;
dwRecAddr = rec->dwStartAddress;
// set file pointer to record location
SetFilePointer (hFile, rec->dwFilePointer, NULL, FILE_BEGIN);
for(i = dwRecLen; i > 0; ){
dwReadReq = (i > SRE_DATA_SIZE ? SRE_DATA_SIZE : i);
i -= dwReadReq;
chksum = dwReadReq + 5 + ADDR_SUM (dwRecAddr); // +5 for address and checksum
sprintf (szAscii, "S3%02X%08X", (unsigned char)(dwReadReq + 5), dwRecAddr); // +5 for address and checksum
dwRecAddr += dwReadReq;
fRet = ReadFile (hFile, pBuffer, dwReadReq, &dwRead, NULL);
if (!fRet || dwRead != dwReadReq)
break;
for (j = 0; j < dwReadReq; j++){
chksum += pBuffer[j];
sprintf (szAscii + strlen(szAscii), "%02X", pBuffer[j]);
}
sprintf (szAscii + strlen(szAscii), "%02X\n", CHKSUM (chksum));
fRet = WriteFile(hSRE, szAscii, strlen (szAscii), &dwWrite, NULL);
if (!fRet || dwWrite != strlen (szAscii)) {
printf ("Error writing SRE file (%d)\n", __LINE__);
CloseHandle (hSRE);
hSRE = INVALID_HANDLE_VALUE;
return FALSE;
}
}
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL WriteSREFooter (){
BOOL fRet;
DWORD dwWrite;
char szAscii[256] = {0};
DWORD dwStartOfExecution = g_dwROMOffset + g_dwStartAddr;
if (hSRE == INVALID_HANDLE_VALUE)
return FALSE;
sprintf (szAscii, "S705%08X%02X\n", dwStartOfExecution, CHKSUM (ADDR_SUM (dwStartOfExecution) + 5));
fRet = WriteFile (hSRE, szAscii, strlen(szAscii), &dwWrite, NULL);
if(!fRet || dwWrite != 15){
printf ("Error writing SRE file (%d)\n", __LINE__);
}
printf("SRE file completed!\n");
CloseHandle (hSRE);
hSRE = INVALID_HANDLE_VALUE;
return TRUE;
}
#define NATIVE_SIZE 32 // bit'ness of platform
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL CloseRawFiles(){
DWORD i;
// close all handles
for(i = 0; i < sizeof(hRaw) / sizeof(HANDLE); i++){
CloseHandle(hRaw[i]);
hRaw[i] = INVALID_HANDLE_VALUE;
}
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL OpenRawFiles(DWORD parts){
static int suffix = 0;
DWORD i;
CloseRawFiles();
// open new handles
for(i = 0; i < parts; i++){
char temp[MAX_PATH];
sprintf(temp, "%s.nb%d", BaseName(g_szFilename), suffix++);
DeleteFile(temp);
hRaw[i] = CreateFile (temp, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, 0, 0);
if (hRaw[i] == INVALID_HANDLE_VALUE) {
printf ("Error opening %s\n", temp);
CloseRawFiles();
return FALSE;
}
}
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL WriteROMs(){
DWORD dwRead;
DWORD dwWrite;
BOOL fRet;
DWORD i, j, k;
BYTE *pData, *ptr;
DWORD dwRomCount;
DWORD dwRomParts;
DWORD dwRomSize;
DWORD dwWriteSize;
// compute roms needed
dwRomCount = g_dwImageLength / g_iRomLength;
if(g_dwImageLength % g_iRomLength)
dwRomCount++;
// compute how many files to span a native machine size value ie. 16 bit rom and 32 bit platform == 2 roms wide
dwRomParts = NATIVE_SIZE / g_iRomWidth;
dwWriteSize = NATIVE_SIZE / dwRomParts / 8; // bytes to write at one time
dwRomSize = dwRomCount * dwRomParts * g_iRomLength;
if(g_iRomLength * dwRomParts < g_dwImageLength)
{
printf("Error: Image length (0x%08x) is less than rom length (0x%08x) * width (%d bits)\n", g_dwImageLength, g_iRomLength, g_iRomWidth);
return FALSE;
}
pData = (BYTE *) malloc(dwRomSize);
if(!pData){
printf("Error: Could not allocate memory for raw data\n");
return FALSE;
}
memset(pData, 0, dwRomSize);
for(i = 0; i < g_dwNumRecords; i++){
if(!g_Records[i].dwStartAddress && !g_Records[i].dwChecksum) // ignore the jump address
continue;
printf("start %08x length %08x\n", g_Records[i].dwStartAddress, g_Records[i].dwLength);
if(g_Records[i].dwStartAddress - g_iRomStartAddr > dwRomSize){
printf("Warning: Record outside of ROM range, record skipped\n");
continue;
}
SetFilePointer (hFile, g_Records[i].dwFilePointer, NULL, FILE_BEGIN);
fRet = ReadFile (hFile,
pData + g_Records[i].dwStartAddress - g_iRomStartAddr, // g_dwImageStart,
g_Records[i].dwLength,
&dwRead,
NULL);
if (!fRet || dwRead != g_Records[i].dwLength){
printf("Error: Could not read record %d\n", i);
}
}
ptr = pData;
printf("Progress...\n");
for(i = 0; i < dwRomCount; i+=dwRomParts){
OpenRawFiles(dwRomParts);
// opitmization for native machine sized roms
if(dwRomParts == 1)
dwWriteSize = dwRomSize;
for(j = 0; j < g_iRomLength / dwWriteSize; j++){
static DWORD percent = -1;
if(percent != (ptr - pData) * 100 / dwRomSize){
percent = (ptr - pData) * 100 / dwRomSize;
printf("\r%d%%", percent);
fflush(stdout);
}
for(k = 0; k < dwRomParts; k++, ptr += dwWriteSize){
WriteFile(hRaw[k], ptr, dwWriteSize, &dwWrite, NULL);
if (!fRet || dwWrite != dwWriteSize) {
printf ("Error writing Raw file (%s)\n", GetLastErrorString());
break;
}
}
}
CloseRawFiles();
}
free(pData);
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int main(int argc, char *argv[])
{
DWORD i, j, t, dwGap;
DWORD dwRecAddr, dwRecLen, dwRecChk;
DWORD dwRead, dwReadReq;
BOOL fRet;
LPTSTR pszCmdLine;
char szAscii[17];
pszCmdLine = GetCommandLine();
if(getenv("d_break"))
DebugBreak();
// Parse command line parameters (updates the ui variables).
if (!parseCmdLine(pszCmdLine))
return 0;
printf("ViewBin... %s\n", g_szFilename);
hFile = CreateFile(g_szFilename, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE) {
printf ("Error opening %s\n", g_szFilename);
return 0;
}
fRet = ReadFile(hFile, pBuffer, 7, &dwRead, NULL);
if (!fRet || dwRead != 7) {
printf("Error reading %s (%d)\n", g_szFilename, __LINE__);
CloseHandle(hFile);
return 0;
}
if (memcmp( pBuffer, "B000FF\x0A", 7 )) {
printf("Missing initial signature (BOOOFF\x0A). Not a BIN file\n");
CloseHandle(hFile);
return 0;
}
//
// Read the image header
//
fRet = ReadFile(hFile, &g_dwImageStart, sizeof(DWORD), &dwRead, NULL);
if (!fRet || dwRead != sizeof(DWORD)) {
CloseHandle(hFile);
return 0;
}
fRet = ReadFile(hFile, &g_dwImageLength, sizeof(DWORD), &dwRead, NULL);
if (!fRet || dwRead != sizeof(DWORD)) {
CloseHandle(hFile);
return 0;
}
printf("Image Start = 0x%08X, length = 0x%08X\n", g_dwImageStart, g_dwImageLength);
szAscii[16] = '\0';
//
// Now read the records.
//
while (1) {
//
// Record address
//
fRet = ReadFile(hFile, &dwRecAddr, sizeof(DWORD), &dwRead, NULL);
if (!fRet || dwRead != sizeof(DWORD)) {
break;
}
//
// Record length
//
fRet = ReadFile(hFile, &dwRecLen, sizeof(DWORD), &dwRead, NULL);
if (!fRet || dwRead != sizeof(DWORD)) {
break;
}
//
// Record checksum
//
fRet = ReadFile(hFile, &dwRecChk, sizeof(DWORD), &dwRead, NULL);
if (!fRet || dwRead != sizeof(DWORD)) {
break;
}
g_Records[g_dwNumRecords].dwStartAddress = dwRecAddr;
g_Records[g_dwNumRecords].dwLength = dwRecLen;
g_Records[g_dwNumRecords].dwChecksum = dwRecChk;
g_Records[g_dwNumRecords].dwFilePointer = SetFilePointer(hFile, 0, NULL, FILE_CURRENT);
if (g_fPrintRecords || g_fPrintData) {
printf("Record [%3d] : Start = 0x%08X, Length = 0x%08X, Chksum = 0x%08X\n", g_dwNumRecords, dwRecAddr, dwRecLen, dwRecChk);
}
g_dwNumRecords++;
if (dwRecAddr == 0 && dwRecChk == 0) {
g_dwStartAddr = dwRecLen;
printf ("\t\tStart address = 0x%08X\n", g_dwStartAddr);
break;
}
//
// Read or skip over the data
//
if (g_fPrintData) {
DWORD chksum = 0;
//
// Print all the data
//
for (i = dwRecLen, t=0; i > 0; ) {
dwReadReq = (i > BUFFER_SIZE ? BUFFER_SIZE : i);
fRet = ReadFile(hFile, pBuffer, dwReadReq, &dwRead, NULL);
if (!fRet || dwRead != dwReadReq) {
break;
}
i -= dwReadReq;
for (j = 0; j < dwReadReq; j++, t++) {
if (t % 16 == 0) {
printf (" 0x%08X :", t + dwRecAddr);
}
if (t % 4 == 0) {
printf (" ");
}
printf ("%02X", pBuffer[j]);
chksum += pBuffer[j];
if (pBuffer[j] < 0x20 || pBuffer[j] > 0x7E) {
szAscii[j%16] = '.';
} else {
szAscii[j%16] = pBuffer[j];
}
if ((j+1) % 16 == 0) {
printf (" %s \n", szAscii);
}
}
}
//
// Correct the last line (if partially printed a line)
//
dwGap = 16 - (j % 16);
if (dwGap != 16) {
for (; (j % 16); j++, t++) {
if (t % 4 == 0) {
printf (" ");
}
printf (" ");
szAscii[j%16] = ' ';
}
printf (" %s \n", szAscii);
}
printf ("\n");
if(chksum != dwRecChk)
printf(" Chksum error found in this record\n");
else
printf(" Chksum valid\n");
} else {
SetFilePointer(hFile, dwRecLen, NULL, FILE_CURRENT);
}
}
while(ComputeRomOffset())
if (g_fPrintTOC || g_fPrintOBJ)
PrintTOC();
if (g_fGenerateROM)
WriteROMs();
if (g_fGenerateSRE) {
WriteSREHeader();
for(i = 0; i < g_dwNumRecords; i++)
WriteSRERecord(&g_Records[i]);
WriteSREFooter();
}
CloseHandle(hFile);
printf("Done.\n");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -