Skip to content

Instantly share code, notes, and snippets.

@miek

miek/checksum.c Secret

Created July 29, 2020 09:15
Show Gist options
  • Save miek/b6248205b6cfd26c1c6c5d6ac0abdc3d to your computer and use it in GitHub Desktop.
Save miek/b6248205b6cfd26c1c6c5d6ac0abdc3d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdint.h>
int main(int argc, char **argv) {
FILE *input_file = fopen(argv[1], "rb");
// Checksum sample code
uint32_t dCheckSum, dExpectedCheckSum;
uint16_t wSignature, wLen;
uint32_t dAddress, i;
uint32_t dImageBuf[512*1024];
uint32_t dLength;
fread(&wSignature,1,2,input_file); // read signature bytes
if (wSignature != 0x5943)
// check ‘CY’ signature byte
{
printf("Invalid image (0x%X)\n", wSignature);
return 1;
}
fread(&i, 2, 1, input_file);
// skip 2 dummy bytes
dCheckSum = 0;
while (1)
{
fread(&dLength,4,1,input_file); // read dLength
fread(&dAddress,4,1,input_file); // read dAddress
if (dLength==0) break;
// done
// read sections
fread(dImageBuf, 4, dLength, input_file);
for (i=0; i<dLength; i++) dCheckSum += dImageBuf[i];
}
// read pre-computed checksum data
fread(&dExpectedCheckSum, 4, 1, input_file);
if (dCheckSum != dExpectedCheckSum)
{
printf("Fail to boot due to checksum error (calculated 0x%X != file 0x%X)\n", dCheckSum, dExpectedCheckSum);
return 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment