FreeCalypso > hg > freecalypso-reveng
annotate miscprog/factdiff.c @ 160:db3b300da465
malware version of the C118 bootloader reversed
author | Michael Spacefalcon <msokolov@ivan.Harhan.ORG> |
---|---|
date | Thu, 15 May 2014 08:38:42 +0000 |
parents | 597143ba1c37 |
children |
rev | line source |
---|---|
52
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * The 64 KiB "factory block" at the end of the 2nd flash chip select on |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * Pirelli DP-L10 phones is believed to contain juicy info (IMEI and RF |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 * calibration data), but the format is yet to be cracked. |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 * |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 * This program compares Pirelli factory block images that have been read |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 * out of several phones, seeking to determine which bytes are always the |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 * same and which bytes change from specimen to specimen. |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 * |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 * Written by Spacefalcon the Outlaw. |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 */ |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 #include <sys/types.h> |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 #include <sys/file.h> |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 #include <stdio.h> |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 #include <stdlib.h> |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 u_char specimen0[65536]; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 char is_diff[65536]; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 read_specimen_file(filename, buf) |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 char *filename; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 u_char *buf; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 { |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 int fd, cc; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 fd = open(filename, O_RDONLY); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 if (fd < 0) { |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 perror(filename); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 exit(1); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 } |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 cc = read(fd, buf, 65536); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 close(fd); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 if (cc != 65536) { |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 fprintf(stderr, "%s: unable to read 64 KiB\n", filename); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 exit(1); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 } |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 } |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 process_comp_specimen(filename) |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 char *filename; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 { |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 u_char this_spec[65536]; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 int i; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 read_specimen_file(filename, this_spec); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 for (i = 0; i < 65536; i++) |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 if (this_spec[i] != specimen0[i]) |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 is_diff[i] = 1; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 } |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 output() |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 { |
53
00dedefbdfd1
factdiff: print the number of bytes in each varying chunk
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
52
diff
changeset
|
54 int off, state, cstart, num; |
52
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 for (off = 0; off < 65536; ) { |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 state = is_diff[off]; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 cstart = off; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 while (off < 65536 && is_diff[off] == state) |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 off++; |
53
00dedefbdfd1
factdiff: print the number of bytes in each varying chunk
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
52
diff
changeset
|
61 printf("%04X-%04X: %s", cstart, off-1, |
52
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 state ? "varying" : "constant"); |
53
00dedefbdfd1
factdiff: print the number of bytes in each varying chunk
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
52
diff
changeset
|
63 if (state) { |
00dedefbdfd1
factdiff: print the number of bytes in each varying chunk
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
52
diff
changeset
|
64 num = off - cstart; |
00dedefbdfd1
factdiff: print the number of bytes in each varying chunk
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
52
diff
changeset
|
65 printf(" (%d byte%s)", num, num != 1 ? "s" : ""); |
00dedefbdfd1
factdiff: print the number of bytes in each varying chunk
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
52
diff
changeset
|
66 } |
00dedefbdfd1
factdiff: print the number of bytes in each varying chunk
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
52
diff
changeset
|
67 putchar('\n'); |
52
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 } |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 } |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 main(argc, argv) |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 char **argv; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 { |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 char **ap; |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 if (argc < 3) { |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 fprintf(stderr, "usage: %s specimen0 specimen1 ...\n", argv[0]); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 exit(1); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 } |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 read_specimen_file(argv[1], specimen0); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 for (ap = argv + 2; *ap; ap++) |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 process_comp_specimen(*ap); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 output(); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 exit(0); |
103d996ed2d5
factdiff utility written
Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 } |