annotate fteeprom/ftee-fix-cksum.c @ 67:742c41f44658

doc/FTDI-chip-ID: new article
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 13 Sep 2023 00:43:14 +0000
parents 85256d5aa559
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
2 * Sometimes it is useful to be able to edit FTDI EEPROM hex images
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
3 * by hand, for special situations and experiments that are too unique
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
4 * to automate. However, the need for passing checksum gets in the way.
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
5 * This program reads an FTDI EEPROM image from a file (or from stdin),
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
6 * recomputes a new checksum and re-emits the checksum-fixed EEPROM hex
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
7 * image on stdout.
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 */
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <sys/types.h>
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <stdio.h>
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include <stdlib.h>
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 extern unsigned eeprom_size;
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern u_short eeprom[256];
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
19 static unsigned chksum_size;
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
21 static void
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
22 do_checksum()
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 u_short chksum = 0xAAAA;
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 unsigned n;
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
27 for (n = 0; n < chksum_size - 1; n++) {
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 chksum ^= eeprom[n];
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 chksum = (chksum << 1) | (chksum >> 15);
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
31 eeprom[n] = chksum;
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 }
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 static void
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
35 emit_output()
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 {
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
37 unsigned n, col;
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
39 for (n = 0; n < eeprom_size; n++) {
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
40 col = n & 7;
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
41 if (col == 0)
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
42 printf("%02X:", n * 2);
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
43 printf(" %04X", eeprom[n]);
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
44 if (col == 7)
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
45 putchar('\n');
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 }
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 main(argc, argv)
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 char **argv;
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 {
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (argc != 2) {
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 fprintf(stderr, "usage: %s eeprom-image-file\n", argv[0]);
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 exit(1);
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (strcmp(argv[1], "-"))
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 read_eeprom_from_file(argv[1]);
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 else
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 read_eeprom_from_stdin();
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 switch (eeprom_size) {
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 case 64:
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
62 chksum_size = 64;
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 break;
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 case 128:
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 case 256:
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
66 chksum_size = 128;
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 break;
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 default:
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 fprintf(stderr,
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 "BUG: invalid EEPROM size not caught earlier\n");
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 exit(1);
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
66
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
73 do_checksum();
85256d5aa559 ftee-fix-cksum program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents: 65
diff changeset
74 emit_output();
65
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 exit(0);
225dc1d9f2f1 ftee-decode program written, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 }