forked from command-tab/ch341eeprom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch341eeprom.c
322 lines (291 loc) · 12.8 KB
/
ch341eeprom.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// ch341eeprom programmer version 0.1 (Beta)
//
// Programming tool for the 24Cxx serial EEPROMs using the Winchiphead CH341A IC
//
// (c) December 2011 asbokid <ballymunboy@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <libusb-1.0/libusb.h>
#include <sys/errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
#include <getopt.h>
#include <limits.h>
#include <sys/mman.h>
#include "ch341eeprom.h"
FILE *debugout, *verbout;
uint8_t *readbuf = NULL;
int main(int argc, char **argv) {
int i, eepromsize = 0, bytesread = 0;
uint8_t debug = FALSE, verbose = FALSE, eeprom_bus_addr = EEPROM_I2C_BUS_ADDRESS;
struct libusb_device_handle *devHandle = NULL;
char *filename = NULL, eepromname[12], operation = 0;
uint32_t speed = CH341_I2C_STANDARD_SPEED;
uint8_t *verifybuf;
uint8_t verify_failed = FALSE;
FILE *fp;
struct EEPROM eeprom_info;
static char version_msg[] =
"ch341eeprom - an i2c EEPROM programming tool for the WCH CH341a IC\n" \
"Version " CH341TOOLVERSION " copyright (c) 2011 asbokid <ballymunboy@gmail.com>\n\n" \
"This program comes with absolutely no warranty; This is free software,\n" \
"and you are welcome to redistribute it under certain conditions:\n" \
"GNU GPL v3 License: http://www.gnu.org/licenses/gpl.html\n";
static char usage_msg[] =
"Usage:\n" \
" -h, --help display this text\n" \
" -v, --verbose verbose output\n" \
" -d, --debug debug output\n" \
" -s, --size size of EEPROM {24c01|24c02|24c04|24c08|24c16|24c32|24c64|24c128|24c256|24c512|24c1024}\n" \
" -e, --erase erase EEPROM (fill with 0xff)\n" \
" -p, --speed i2c speed (low|fast|high) if different than standard which is default\n" \
" -a, --addr i2c eeprom address (default 0x50)\n" \
" -w, --write <filename> write EEPROM with image from filename\n" \
" -r, --read <filename> read EEPROM and save image to filename\n" \
" -V, --verify <filename> verify EEPROM contents against image in filename\n\n" \
"Example: ch341eeprom -v -s 24c64 -w bootrom.bin\n";
static struct option longopts[] = {
{"help", no_argument, 0, 'h'},
{"verbose", no_argument, 0, 'v'},
{"debug", no_argument, 0, 'd'},
{"erase", no_argument, 0, 'e'},
{"size", required_argument, 0, 's'},
{"speed", required_argument, 0, 'p'},
{"addr", required_argument, 0, 'a'},
{"read", required_argument, 0, 'r'},
{"write", required_argument, 0, 'w'},
{"verify", required_argument, 0, 'V'},
{0, 0, 0, 0}
};
static int speed_table[] = {20, 100, 400, 750};
while (TRUE) {
int32_t optidx = 0;
int8_t c = getopt_long(argc,argv,"hvdes:p:a:w:r:V:", longopts, &optidx);
if (c == -1)
break;
switch (c) {
case 'h': fprintf(stdout, "%s\n%s", version_msg, usage_msg);
return 0;
case 'v': verbose = TRUE;
break;
case 'd': debug = TRUE;
break;
case 's': if((eepromsize = parseEEPsize(optarg, &eeprom_info)) > 0)
strncpy(eepromname, optarg, 10);
break;
case 'p': if(strstr(optarg, "low"))
speed = CH341_I2C_LOW_SPEED;
else if(strstr(optarg, "fast"))
speed = CH341_I2C_FAST_SPEED;
else if(strstr(optarg, "high"))
speed = CH341_I2C_HIGH_SPEED;
else
speed = CH341_I2C_STANDARD_SPEED;
break;
case 'a': eeprom_bus_addr = (uint8_t)strtol(optarg,NULL,0);
if(eeprom_bus_addr==0 || eeprom_bus_addr>127) {
fprintf(stderr, "Invalid i2c eeprom address\n");
goto shutdown;
}
break;
case 'e': if(!operation)
operation = 'e';
else {
fprintf(stderr, "Conflicting command line options\n");
goto shutdown;
}
break;
case 'r': if(!operation) {
operation = 'r';
filename = (char *) malloc(strlen(optarg)+1);
strcpy(filename, optarg);
} else {
fprintf(stderr, "Conflicting command line options\n");
goto shutdown;
}
break;
case 'w': if(!operation) {
operation = 'w';
filename = (char *) malloc(strlen(optarg)+1);
strcpy(filename, optarg);
} else {
fprintf(stderr, "Conflicting command line options\n");
goto shutdown;
}
break;
case 'V': if(!operation) {
operation = 'V';
filename = (char *) malloc(strlen(optarg)+1);
strcpy(filename, optarg);
} else {
fprintf(stderr, "Conflicting command line options\n");
goto shutdown;
}
break;
default :
case '?': fprintf(stdout, "%s", version_msg);
fprintf(stderr, "%s", usage_msg);
goto shutdown;
}
}
debugout = (debug == TRUE) ? stdout : fopen("/dev/null","w");
verbout = (verbose == TRUE) ? stdout : fopen("/dev/null","w");
fprintf(debugout, "Debug Enabled\n");
if(!operation) {
fprintf(stderr, "%s\n%s", version_msg, usage_msg);
goto shutdown;
}
if(eepromsize <= 0) {
fprintf(stderr, "Invalid EEPROM size\n");
goto shutdown;
}
if((eeprom_bus_addr&(~eeprom_info.i2c_addr_mask)) != eeprom_bus_addr) {
fprintf(stderr, "Invalid i2c eeprom address for type %s\n", eeprom_info.name);
goto shutdown;
}
readbuf = (uint8_t *) malloc(MAX_EEPROM_SIZE); // space to store loaded EEPROM
if(!readbuf) {
fprintf(stderr, "Couldnt malloc space needed for EEPROM image\n");
goto shutdown;
}
if(!(devHandle = ch341configure(USB_LOCK_VENDOR, USB_LOCK_PRODUCT))) {
fprintf(stderr, "Couldnt configure USB device with vendor ID: %04x product ID: %04x\n", USB_LOCK_VENDOR, USB_LOCK_PRODUCT);
goto shutdown;
}
fprintf(verbout, "Configured USB device with vendor ID: %04x product ID: %04x\n", USB_LOCK_VENDOR, USB_LOCK_PRODUCT);
if(ch341setstream(devHandle, speed) < 0) {
fprintf(stderr, "Couldnt set i2c bus speed\n");
goto shutdown;
}
fprintf(verbout, "Set i2c bus speed to [%dkHz]\n", speed_table[speed]);
switch(operation) {
case 'r': // read
memset(readbuf, 0xff, MAX_EEPROM_SIZE);
if(ch341readEEPROM(devHandle, readbuf, eepromsize, &eeprom_info, eeprom_bus_addr) < 0) {
fprintf(stderr, "Couldnt read [%d] bytes from [%s] EEPROM\n", eepromsize, eepromname);
goto shutdown;
}
fprintf(stdout, "Read [%d] bytes from [%s] EEPROM\n", eepromsize, eepromname);
for(i=0;i<eepromsize;i++) {
if(!(i%16))
fprintf(debugout, "\n%04x: ", i);
fprintf(debugout, "%02x ", readbuf[i]);
}
fprintf(debugout, "\n");
if(!(fp=fopen(filename, "wb"))) {
fprintf(stderr, "Couldnt open file [%s] for writing\n", filename);
goto shutdown;
}
fwrite(readbuf, 1, eepromsize, fp);
if(ferror(fp)) {
fprintf(stderr, "Error writing file [%s]\n", filename);
if(fp)
fclose(fp);
goto shutdown;
}
fclose(fp);
fprintf(stdout, "Wrote [%d] bytes to file [%s]\n", eepromsize, filename);
break;
case 'V': // verify
memset(readbuf, 0xff, MAX_EEPROM_SIZE);
if(ch341readEEPROM(devHandle, readbuf, eepromsize, &eeprom_info, eeprom_bus_addr) < 0) {
fprintf(stderr, "Couldnt read [%d] bytes from [%s] EEPROM\n", eepromsize, eepromname);
goto shutdown;
}
fprintf(stdout, "Read [%d] bytes from [%s] EEPROM\n", eepromsize, eepromname);
for(i=0;i<eepromsize;i++) {
if(!(i%16))
fprintf(debugout, "\n%04x: ", i);
fprintf(debugout, "%02x ", readbuf[i]);
}
fprintf(debugout, "\n");
if(!(fp=fopen(filename, "rb"))) {
fprintf(stderr, "Couldnt open file [%s] for reading\n", filename);
goto shutdown;
}
verifybuf = mmap(NULL, eepromsize, PROT_READ, MAP_PRIVATE, fileno(fp), 0);
if(!verifybuf) {
fprintf(stderr, "Error mapping file [%s]\n", filename);
if(fp)
fclose(fp);
goto shutdown;
}
for(i=0;i<eepromsize;i++) {
if(readbuf[i]!=verifybuf[i]) {
verify_failed = TRUE;
break;
}
}
if(verify_failed)
fprintf(stdout, "Verification against file [%s] failed at offset [%d], EEPROM: %02hhX, file: %02hhX\n", filename, i, readbuf[i], verifybuf[i]);
else
fprintf(stdout, "Verified [%d] bytes against file [%s]\n", eepromsize, filename);
munmap(verifybuf, eepromsize);
fclose(fp);
break;
case 'w': // write
if(!(fp=fopen(filename, "rb"))) {
fprintf(stderr, "Couldnt open file [%s] for reading\n", filename);
goto shutdown;
}
memset(readbuf, 0xff, MAX_EEPROM_SIZE);
bytesread = fread(readbuf, 1, MAX_EEPROM_SIZE, fp);
if(ferror(fp)) {
fprintf(stderr, "Error reading file [%s]\n", filename);
if(fp)
fclose(fp);
goto shutdown;
}
fclose(fp);
fprintf(stdout, "Read [%d] bytes from file [%s]\n", bytesread, filename);
if(bytesread < eepromsize)
fprintf(stdout, "Padded to [%d] bytes for [%s] EEPROM\n", eepromsize, eepromname);
if(bytesread > eepromsize)
fprintf(stdout, "Truncated to [%d] bytes for [%s] EEPROM\n", eepromsize, eepromname);
if(ch341writeEEPROM(devHandle, readbuf, eepromsize, &eeprom_info, eeprom_bus_addr) < 0) {
fprintf(stderr,"Failed to write [%d] bytes from [%s] to [%s] EEPROM\n", eepromsize, filename, eepromname);
goto shutdown;
}
fprintf(stdout, "Wrote [%d] bytes to [%s] EEPROM\n", eepromsize, eepromname);
break;
case 'e': // erase
memset(readbuf, 0xff, MAX_EEPROM_SIZE);
if(ch341writeEEPROM(devHandle, readbuf, eepromsize, &eeprom_info, eeprom_bus_addr) < 0) {
fprintf(stderr,"Failed to erase [%d] bytes of [%s] EEPROM\n", eepromsize, eepromname);
goto shutdown;
}
fprintf(stdout, "Erased [%d] bytes of [%s] EEPROM\n", eepromsize, eepromname);
break;
default:
fprintf(stderr, "Unknown option\n");
goto shutdown;
}
shutdown:
if(readbuf)
free(readbuf);
if(filename)
free(filename);
if(devHandle) {
libusb_release_interface(devHandle, DEFAULT_INTERFACE);
fprintf(debugout, "Released device interface [%d]\n", DEFAULT_INTERFACE);
libusb_close(devHandle);
fprintf(verbout, "Closed USB device\n");
libusb_exit(NULL);
}
return 0;
}