Created
September 6, 2020 11:32
-
-
Save swiftgeek/96a40f8be2159f579dd7443197ed330c to your computer and use it in GitHub Desktop.
patched for 8bit sum - T20 and older
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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