]> git.zerfleddert.de Git - fnordlicht-mini/blob - firmware/tools/patcheeprom.c
fix config.mk file header comment
[fnordlicht-mini] / firmware / tools / patcheeprom.c
1 #include <stdio.h>
2 #include <stdlib.h>
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
27 /* oh, this is soooo ugly... :-( */
28 int main(int argc, char ** argv)
29 {
30 int i, c, id;
31 uint16_t checksum = 0;
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++) {
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 }
63 /* print out checksum, and we're done */
64 putchar(checksum & 0xff);
65 putchar(checksum >> 8);
66
67 return 0;
68 }
69
Impressum, Datenschutz