annotate fteeprom/ftee-gen2232c.c @ 34:ae48b7d69744

ftee-gen2232c: added guard against strings overflowing EEPROM space
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Apr 2019 18:50:33 +0000
parents 16b625911e19
children ea0b5aed9a9c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 #include <sys/types.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 #include <ctype.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 #include <string.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 #include <strings.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <stdio.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <stdlib.h>
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 u_short vid = 0x0403, pid = 0x6010;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 char *manuf, *product;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 u_char byte00 = 0x08, byte01 = 0x08;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 u_char byte08 = 0x80, byte0A = 0x00;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 unsigned maxpower = 100;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 u_short usb_version = 0x0200;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 u_short eeprom[64];
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned eeprom_string_ptr = 0x0B;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 read_config_file(filename)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 char *filename;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 FILE *inf;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 char linebuf[1024];
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 int lineno;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 char *cp, *np;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 inf = fopen(filename, "r");
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 if (!inf) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 perror(filename);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 cp = index(linebuf, '\n');
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (!cp) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 fprintf(stderr,
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 "%s line %d: too long or unterminated\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 filename, lineno);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 *cp = '\0';
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 for (cp = linebuf; isspace(*cp); cp++)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 ;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 if (*cp == '\0' || *cp == '#')
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 continue;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 for (np = cp; *cp && !isspace(*cp); cp++)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 ;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (*cp)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 *cp++ = '\0';
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 while (isspace(*cp))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 cp++;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 if (*cp == '\0' || *cp == '#') {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 fprintf(stderr,
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 "%s line %d: \"%s\" setting without argument\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 filename, lineno, np);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 if (!strcmp(np, "vid"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 vid = strtoul(cp, 0, 16);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 else if (!strcmp(np, "pid"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 pid = strtoul(cp, 0, 16);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 else if (!strcmp(np, "manuf"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 manuf = strdup(cp);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 else if (!strcmp(np, "product"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 product = strdup(cp);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 else if (!strcmp(np, "byte00"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 byte00 = strtoul(cp, 0, 16);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 else if (!strcmp(np, "byte01"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 byte01 = strtoul(cp, 0, 16);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 else if (!strcmp(np, "byte08"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 byte08 = strtoul(cp, 0, 16);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 else if (!strcmp(np, "byte0A"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 byte0A = strtoul(cp, 0, 16);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 else if (!strcmp(np, "maxpower"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 maxpower = strtoul(cp, 0, 10);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 else if (!strcmp(np, "usbver"))
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 usb_version = strtoul(cp, 0, 16);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 else {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 fprintf(stderr, "%s line %d: unknown \"%s\" setting\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 filename, lineno, np);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 fclose(inf);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (!manuf) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 fprintf(stderr, "error: manuf not set in %s\n", filename);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 if (!product) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 fprintf(stderr, "error: product not set in %s\n", filename);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 write_string(str)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 char *str;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 unsigned longlen, startptr;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97
34
ae48b7d69744 ftee-gen2232c: added guard against strings overflowing EEPROM space
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
98 if (63 - eeprom_string_ptr < strlen(str) + 1) {
ae48b7d69744 ftee-gen2232c: added guard against strings overflowing EEPROM space
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
99 fprintf(stderr, "error: strings are too long\n");
ae48b7d69744 ftee-gen2232c: added guard against strings overflowing EEPROM space
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
100 exit(1);
ae48b7d69744 ftee-gen2232c: added guard against strings overflowing EEPROM space
Mychaela Falconia <falcon@freecalypso.org>
parents: 31
diff changeset
101 }
31
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 longlen = strlen(str) * 2 + 2;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 startptr = eeprom_string_ptr;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 eeprom[eeprom_string_ptr++] = 0x0300 | longlen;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 while (*str)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 eeprom[eeprom_string_ptr++] = *str++;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 return (longlen << 8) | 0x80 | (startptr << 1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 fill_eeprom(serial)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 char *serial;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 u_char byte09;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 if (serial)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 byte0A |= 0x08;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 else
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 byte0A &= 0xF7;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 byte09 = maxpower / 2;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 eeprom[0] = (byte01 << 8) | byte00;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 eeprom[1] = vid;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 eeprom[2] = pid;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 eeprom[3] = 0x0500;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 eeprom[4] = (byte09 << 8) | byte08;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 eeprom[5] = byte0A;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 eeprom[6] = usb_version;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 eeprom[7] = write_string(manuf);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 eeprom[8] = write_string(product);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 if (serial)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 eeprom[9] = write_string(serial);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 else
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 eeprom[9] = 0;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 eeprom[10] = 0x46;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 do_checksum()
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 u_short chksum = 0xAAAA;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 unsigned n;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 for (n = 0; n < 63; n++) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 chksum ^= eeprom[n];
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 chksum = (chksum << 1) | (chksum >> 15);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 eeprom[63] = chksum;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 emit_output()
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 unsigned n, col;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 for (n = 0; n < 64; n++) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 col = n & 7;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 if (col == 0)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 printf("%02X:", n * 2);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 printf(" %04X", eeprom[n]);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 if (col == 7)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 putchar('\n');
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 main(argc, argv)
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 char **argv;
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 if (argc < 2 || argc > 3) {
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 fprintf(stderr, "usage: %s config-file [serial-num]\n",
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 argv[0]);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 exit(1);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 }
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 read_config_file(argv[1]);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 fill_eeprom(argv[2]);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 do_checksum();
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 emit_output();
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 exit(0);
16b625911e19 fteeprom: generalization of previous ee2232 tools
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 }