📄 stampbin.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
//------------------------------------------------------------------------------
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
//------------------------------------------------------------------------------
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <pehdr.h>
#include <romldr.h>
#define BUFFER_SIZE 64*1024
BYTE pBuffer[BUFFER_SIZE];
HANDLE hFile;
DWORD g_pTOC = 0;
DWORD g_dwImageStart;
DWORD g_dwImageLength;
DWORD g_dwNumRecords;
DWORD g_dwROMOffset;
typedef struct _RECORD_DATA {
DWORD dwStartAddress;
DWORD dwLength;
DWORD dwChecksum;
DWORD dwFilePointer;
} RECORD_DATA, *PRECORD_DATA;
#define MAX_RECORDS 2048
RECORD_DATA g_Records[MAX_RECORDS];
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL
ReWriteCheckSum(DWORD dwAddress)
{
DWORD dwBytes;
DWORD i, j;
UCHAR c;
DWORD dwCheckSum = 0;
for (i = 0; i < g_dwNumRecords; i++) {
if (g_Records[i].dwStartAddress <= dwAddress &&
g_Records[i].dwStartAddress + g_Records[i].dwLength > dwAddress) {
printf("Original record checksum: 0x%x\n", g_Records[i].dwChecksum);
// point to record
SetFilePointer(hFile, g_Records[i].dwFilePointer, NULL, FILE_BEGIN);
// calculate checksum
for (j = 0; j < g_Records[i].dwLength; j++) {
if (!ReadFile(hFile, &c, 1, &dwBytes, NULL) || (dwBytes != 1)) return FALSE;
dwCheckSum += c;
}
printf("New record checksum: 0x%x\n", dwCheckSum);
// Now write the checksum
SetFilePointer(hFile, g_Records[i].dwFilePointer - 4, NULL, FILE_BEGIN);
if (!WriteFile(hFile, &dwCheckSum, 4, &dwBytes, NULL) || (dwBytes != 4)) return FALSE;
return TRUE;
}
}
return FALSE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL
AccessBinFile(
PBYTE pBuffer,
DWORD dwAddress,
DWORD dwLength,
DWORD dwROMOffsetRead,
BOOL bWrite
)
{
DWORD dwBytes;
BOOL fRet;
DWORD i;
DWORD dwDiff;
FlushFileBuffers(hFile);
//
// Adjust if needed
//
dwAddress += dwROMOffsetRead;
for (i = 0; i < g_dwNumRecords; i++) {
if (g_Records[i].dwStartAddress <= dwAddress &&
g_Records[i].dwStartAddress + g_Records[i].dwLength >= (dwAddress + dwLength)) {
//
// Offset into the record.
//
dwDiff = dwAddress - g_Records[i].dwStartAddress;
SetFilePointer(hFile, g_Records[i].dwFilePointer + dwDiff, NULL, FILE_BEGIN);
if (bWrite) {
if (dwLength) {
fRet = WriteFile(hFile, pBuffer, dwLength, &dwBytes, NULL);
if (!fRet || dwBytes != dwLength) {
return FALSE;
}
} else {
do {
fRet = WriteFile(hFile, pBuffer, 1, &dwBytes, NULL);
} while (*pBuffer++);
}
if(!ReWriteCheckSum(dwAddress))
return FALSE;
} else {
if (dwLength) {
fRet = ReadFile(hFile, pBuffer, dwLength, &dwBytes, NULL);
if (!fRet || dwBytes != dwLength) {
return FALSE;
}
} else {
do {
fRet = ReadFile(hFile, pBuffer, 1, &dwBytes, NULL);
} while (*pBuffer++);
}
}
return TRUE;
}
}
return FALSE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL
ComputeRomOffset(DWORD pTOC)
{
DWORD i;
BOOL fFoundIt = FALSE;
BOOL fRet;
DWORD dwROMOffsetRead;
for (i = 0; i < g_dwNumRecords; i++) {
//
// Check for potential TOC records
//
if (g_Records[i].dwLength == sizeof(ROMHDR)) {
//
// If this _IS_ the TOC record, compute the ROM Offset.
//
dwROMOffsetRead = g_Records[i].dwStartAddress - pTOC;
//
// Read out the record to verify. (unadjusted)
//
fRet = AccessBinFile(pBuffer, g_Records[i].dwStartAddress, sizeof(ROMHDR), 0, FALSE);
if (fRet) {
ROMHDR *pTOC = (ROMHDR *)pBuffer;
if ((pTOC->physfirst == (g_dwImageStart - dwROMOffsetRead)) &&
(pTOC->physlast == (g_dwImageStart - dwROMOffsetRead) + g_dwImageLength)) {
//
// Extra sanity check...
//
if (pTOC->dllfirst <= pTOC->dlllast && pTOC->dlllast == 0x02000000) {
fFoundIt = TRUE;
break;
} else {
printf ("NOTICE! Record %d looked like a TOC except DLL first = 0x%08X, and DLL last = 0x%08X\r\n", pTOC->dllfirst, pTOC->dlllast);
}
}
}
}
}
if (fFoundIt) {
g_dwROMOffset = dwROMOffsetRead;
} else {
g_dwROMOffset = 0;
}
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
BOOL
WritePID(ROMPID *pPID)
{
int i = 0;
printf("Writing pid ");
for(i = 0; i < sizeof(ROMHDR)/4 - 1; i++)
printf("%08x ", ((PDWORD)pPID)[i]);
printf("\n");
if (AccessBinFile(pBuffer, g_pTOC, sizeof(ROMHDR), g_dwROMOffset, FALSE)) {
ROMHDR* pTOC = (ROMHDR *)pBuffer;
if (pTOC->pExtensions) {
if (!AccessBinFile((PBYTE)pPID, (DWORD)pTOC->pExtensions, sizeof(ROMPID) - 4, g_dwROMOffset, TRUE)) {
printf("Couldn't write TOC Extensions\n");
return FALSE;
}
}
} else {
printf("Couldn't read TOC data\n");
return FALSE;
}
return TRUE;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
int OpenBin(LPCTSTR lpFileName){
DWORD dwRecAddr, dwRecLen, dwRecChk;
DWORD dwRead;
BOOL fRet;
hFile = CreateFile(lpFileName, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE) {
printf ("Error opening %s\n", lpFileName);
return FALSE;
}
fRet = ReadFile(hFile, pBuffer, 7, &dwRead, NULL);
if (!fRet || dwRead != 7) {
printf("ERROR: Reading %s (%d)\n", lpFileName, __LINE__);
goto Error;
}
if (memcmp( pBuffer, "B000FF\x0A", 7 )) {
printf("Missing initial signature (BOOOFF\x0A). Not a BIN file\n");
goto Error;
}
//
// Read the image header
//
fRet = ReadFile(hFile, &g_dwImageStart, sizeof(DWORD), &dwRead, NULL);
if (!fRet || dwRead != sizeof(DWORD)) {
printf("ERROR: Can't find Image start\n");
goto Error;
}
fRet = ReadFile(hFile, &g_dwImageLength, sizeof(DWORD), &dwRead, NULL);
if (!fRet || dwRead != sizeof(DWORD)) {
printf("ERROR: Can't find Image length\n");
goto Error;
}
//
// Now read the records.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -