comparison fteeprom/ftee-gen2232h.c @ 40:d150d4704ff5

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