comparison fteeprom/ftee-gen232r.c @ 35:a79b0240534a

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