]>
Commit | Line | Data |
---|---|---|
5911f50b | 1 | #include <stdio.h> |
2acfa12f | 2 | #include <stdlib.h> |
5911f50b | 3 | #include <inttypes.h> |
4 | ||
5 | /* EEPROM data size, not including checksum */ | |
6 | #define EEP_DATA_SIZE 494 | |
7 | /* FIXME: actually we should either find a way to include the declaration of | |
8 | struct storage_t here or at least make this a command line parameter */ | |
9 | ||
10 | /* code from http://www.nongnu.org/avr-libc/user-manual/group__util__crc.html */ | |
11 | uint16_t crc16_update(uint16_t crc, uint8_t a) | |
12 | { | |
13 | int i; | |
14 | ||
15 | crc ^= a; | |
16 | for (i = 0; i < 8; ++i) | |
17 | { | |
18 | if (crc & 1) | |
19 | crc = (crc >> 1) ^ 0xA001; | |
20 | else | |
21 | crc = (crc >> 1); | |
22 | } | |
23 | ||
24 | return crc; | |
25 | } | |
26 | ||
2acfa12f | 27 | /* oh, this is soooo ugly... :-( */ |
5911f50b | 28 | int main(int argc, char ** argv) |
29 | { | |
2acfa12f | 30 | int i, c, id; |
5911f50b | 31 | uint16_t checksum = 0; |
2acfa12f | 32 | |
33 | if ((argc < 2) || ((id = atoi(argv[1])) > 15) || (id <= 0)) | |
34 | { | |
35 | fputs("usage: patcheeprom id < eeprom-template > eeprom-binary\n\n", stderr); | |
36 | return 127; | |
37 | } | |
38 | /* print out magic byte and add it to checksum */ | |
39 | putchar(0x23); | |
40 | checksum = crc16_update(checksum, 0x23); | |
41 | ||
42 | /* print out id and add it to checksum */ | |
43 | putchar(id); | |
44 | checksum = crc16_update(checksum, id); | |
45 | ||
46 | /* ignore first two bytes of input */ | |
47 | getchar(); | |
48 | getchar(); | |
49 | ||
50 | /* now copy template and adapt checksum */ | |
51 | for (i = 2; i < EEP_DATA_SIZE; i++) { | |
5911f50b | 52 | |
53 | if ((c = getchar()) == EOF) { | |
54 | return 1; | |
55 | } | |
56 | ||
57 | checksum = crc16_update(checksum, c); | |
58 | ||
59 | if (putchar(c) == EOF) { | |
60 | return 2; | |
61 | } | |
62 | } | |
2acfa12f | 63 | /* print out checksum, and we're done */ |
5911f50b | 64 | putchar(checksum & 0xff); |
2eadb723 | 65 | putchar(checksum >> 8); |
5911f50b | 66 | |
67 | return 0; | |
68 | } | |
69 |