-
Notifications
You must be signed in to change notification settings - Fork 18
/
mec_csum_outer.c
54 lines (47 loc) · 1.27 KB
/
mec_csum_outer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
* 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 short csum = 0;
unsigned short 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-2; p+=2)
csum += ntohs(*(unsigned short *)p);
csum = -csum;
csum = htons(csum);
stored = *(unsigned short *)p;
if (fix)
*(unsigned short *)p = csum;
printf("%04x %04x %s\n", stored, csum, (stored!=csum)?(fix?"FIXED":"FAIL"):"OK");
return (stored!=csum)&&!fix;
}