comparison fteeprom/ftee-fix-cksum.c @ 66:85256d5aa559

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