Skip to content

Instantly share code, notes, and snippets.

@swiftgeek
Created September 6, 2020 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swiftgeek/96a40f8be2159f579dd7443197ed330c to your computer and use it in GitHub Desktop.
Save swiftgeek/96a40f8be2159f579dd7443197ed330c to your computer and use it in GitHub Desktop.
patched for 8bit sum - T20 and older
/*
* mec_csum_outer.c: check/fix outer checksum
* Should be run on the encrypted image
*/
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include "mmapfile.h"
int main(int argc, char *argv[])
{
size_t length;
unsigned char *base;
unsigned char *p, *end;
unsigned char csum = 0;
unsigned char stored;
int fix;
if ((argc >= 3) && (strcmp(argv[1], "-f") == 0))
fix = 1;
else if ((argc >= 3) && (strcmp(argv[1], "-c") == 0))
fix = 0;
else
{
fprintf(stderr, "usage: %s {-f|-c} file\n", argv[0]);
return 1;
}
base = (unsigned char *)mmapfile(argv[2], fix?(PROT_READ|PROT_WRITE):PROT_READ, MAP_SHARED, &length);
if (base == MAP_FAILED)
{
perror("mmapfile");
return 1;
}
if (*(unsigned int *)base == 0x0f802020)
{
fprintf(stderr, "you should run this on the encrypted image\n");
return 1;
}
end = base + length;
for (p = base; p < end-1; p+=1)
csum += *(unsigned char *)p;
csum = -csum;
stored = *(unsigned char *)p;
if (fix)
*(unsigned char *)p = csum;
printf("%04x %04x %s\n", stored, csum, (stored!=csum)?(fix?"FIXED":"FAIL"):"OK");
return (stored!=csum)&&!fix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment