annotate miscprog/pircksum2.c @ 251:6d9a6627b085

pirelli/fw-disasm: continuing Switch_ON() analysis
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 24 Dec 2017 18:03:25 +0000
parents d69f7512e3c1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
215
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program verifies the correctness of the understanding described in the
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * ../pirelli/flash2-chksum write-up.
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/file.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <sys/stat.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <sys/mman.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdio.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <stdint.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <endian.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <stdlib.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <unistd.h>
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 char *image_filename;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 u_char *image_map, *endrec;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned imgsize, max_imgsize;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 open_and_map_file()
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int fd;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 struct stat st;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 fd = open(image_filename, O_RDONLY);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 if (fd < 0) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 perror(image_filename);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 exit(1);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 fstat(fd, &st);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (!S_ISREG(st.st_mode)) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 fprintf(stderr, "error: %s is not a regular file\n",
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 image_filename);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 exit(1);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (st.st_size != 0x10000 && st.st_size != 0x360000) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 fprintf(stderr, "error: %s has an unexpected size\n",
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 image_filename);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 exit(1);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 max_imgsize = st.st_size - 12;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 image_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (image_map == MAP_FAILED) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 perror("mmap");
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 exit(1);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 close(fd);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 endrec = image_map + max_imgsize;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 return 0;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 verify_chksum1()
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 uint16_t sum, ref;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 unsigned offset;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 sum = 0;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 for (offset = 0; offset + 1 < imgsize; offset += 2)
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 sum += le16toh(*(uint16_t *)(image_map + offset));
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 if (imgsize & 1)
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 sum += image_map[offset];
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 ref = le16toh(*(uint16_t *)(endrec + 8));
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 if (sum == ref) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 printf("Checksum 1 matches (%04X)\n", sum);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 return 0;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 } else {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 printf("Checksum 1 FAILED: computed %04X, stored %04X\n",
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 sum, ref);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 return 1;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 verify_chksum2()
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 uint16_t sum, ref;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 unsigned offset;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 sum = 0;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 for (offset = 0; offset < 10; offset += 2)
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 sum += le16toh(*(uint16_t *)(endrec + offset));
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 ref = le16toh(*(uint16_t *)(endrec + 10));
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 if (sum == ref) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 printf("Checksum 2 matches (%04X)\n", sum);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 return 0;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 } else {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 printf("Checksum 2 FAILED: computed %04X, stored %04X\n",
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 sum, ref);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 return 1;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 main(argc, argv)
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 char **argv;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 int stat1, stat2;
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 if (argc != 2) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 fprintf(stderr, "usage: %s image-filename\n", argv[0]);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 exit(1);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 image_filename = argv[1];
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 open_and_map_file();
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 if (le32toh(*(uint32_t *)endrec) != 0x12345678) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 printf("Error: 0x12345678 signature missing\n");
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 exit(1);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 imgsize = le32toh(*(uint32_t *)(endrec + 4));
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 if (imgsize < 1 || imgsize > max_imgsize) {
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 printf("Error: bogus image size of 0x%x bytes\n", imgsize);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 exit(1);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 }
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 printf("Image size: 0x%x bytes\n", imgsize);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 stat1 = verify_chksum1();
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 stat2 = verify_chksum2();
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 exit(stat1 || stat2);
d69f7512e3c1 Pirelli: documented and verified the checksum scheme used for the factory block
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 }