-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
333 lines (281 loc) · 9.88 KB
/
main.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
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (c) 2007 Claudio Favi ([email protected])
* Copyright (c) 2001 Stephen Williams ([email protected])
* Copyright (c) 2001-2002 David Brownell ([email protected])
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <getopt.h>
#include "libusb.h"
#include "ezusb.h"
#ifndef FXLOAD_VERSION
# define FXLOAD_VERSION (__DATE__ " (modified version at http://github.com/tai)")
#endif
struct device_spec { int index, vid, pid, bus, port; };
void logerror(const char *format, ...)
__attribute__ ((format (__printf__, 1, 2)));
void logerror(const char *format, ...)
{
va_list ap;
va_start(ap, format);
vfprintf(stderr, format, ap);
va_end(ap);
}
void usage(const char *argv0) {
char *p = strrchr(argv0, '/');
char *q = strrchr(argv0, '\\');
// basename of argv0
p = (p > q) ? p+1 : q+1;
fprintf(stderr, "Usage: %s [options] -I file [-c 0xC[02] -s loader]\n", p);
fprintf(stderr,
"Options:\n"
" -D <dev> : select device by vid:pid or bus.port\n"
" -t <type> : select type from (an21|fx|fx2|fx2lp)\n"
" -I <file> : program hex file\n"
" -s <loader>: program stage1 loader to write a file into EEPROM\n"
" -c <byte> : program first byte of EEPROM with either 0xC0 or 0xC2\n"
" -V : show version\n"
" -v : show verbose message\n");
fprintf(stderr,
"Examples:\n"
" // program fw.hex to the FIRST device with given vid\n"
" $ %s -d 04b4:@0 -I fw.hex\n"
"\n"
" // program fw.hex to the SECOND device at given bus\n"
" $ %s -d 004.@1 -I fw.hex\n"
"\n"
" // program vid:pid info to EEPROM\n"
" $ %s -I vidpid.hex -c 0xC0 -s Vend_Ax.hex\n"
"\n"
" // program whole firmware to EEPROM\n"
" $ %s -I fwfile.hex -c 0xC2 -s Vend_Ax.hex\n", p, p, p, p);
exit(1);
}
libusb_device_handle *get_usb_device(struct device_spec *wanted) {
libusb_device **list;
libusb_device_handle *dev_h = NULL;
libusb_init(NULL);
libusb_set_debug(NULL, 0);
libusb_device *found = NULL;
int nr_found = 0;
int interactive = (wanted->vid == 0 && wanted->bus == 0);
int i, nr = libusb_get_device_list(NULL, &list);
for (i = 0; i < nr; i++) {
libusb_device *dev = list[i];
struct libusb_device_descriptor desc;
libusb_get_device_descriptor(dev, &desc);
uint16_t vid = desc.idVendor;
uint16_t pid = desc.idProduct;
uint8_t bus = libusb_get_bus_number(dev);
uint8_t port = libusb_get_port_number(dev);
if ((bus == wanted->bus && (port == wanted->port || wanted->port == 0)) ||
(vid == wanted->vid && ( pid == wanted->pid || wanted->pid == 0))) {
if (nr_found++ == wanted->index) {
found = dev;
break;
}
}
if (interactive) {
unsigned char mfg[80] = {0}, prd[80] = {0}, ser[80] = {0};
libusb_device_handle *dh;
if (libusb_open(dev, &dh) == LIBUSB_SUCCESS) {
libusb_get_string_descriptor_ascii(dh, desc.iManufacturer, mfg, sizeof(mfg));
libusb_get_string_descriptor_ascii(dh, desc.iProduct , prd, sizeof(prd));
libusb_get_string_descriptor_ascii(dh, desc.iSerialNumber, ser, sizeof(ser));
libusb_close(dh);
}
printf("%d: Bus %03d Device %03d: ID %04X:%04X %s %s %s\n", i, bus, port, vid, pid, mfg, prd, ser);
}
}
if (interactive) {
char sbuf[32];
printf("Please select device to configure [0-%d]: ", nr - 1);
fflush(NULL);
fgets(sbuf, 32, stdin);
int sel = atoi(sbuf);
if( sel < 0 || sel >= nr) {
logerror("device selection out of bound: %d\n", sel);
return NULL;
}
found = list[sel];
}
if (! found) {
logerror("device not selected\n");
return NULL;
}
libusb_open(found, &dev_h);
libusb_free_device_list(list, 1);
return dev_h;
}
void
parse_device_path(const char *device_path, struct device_spec *spec) {
char *sub;
if ((sub = strchr(device_path, ':')) != NULL) {
spec->vid = strtol(device_path, NULL, 16);
spec->pid = strtol( sub + 1, NULL, 16);
}
else if ((sub = strchr(device_path, '.')) != NULL) {
spec->bus = atoi(device_path);
spec->port = atoi(sub + 1);
}
if ((sub = strchr(device_path, '@')) != NULL) {
spec->index = atoi(sub + 1);
}
}
int main(int argc, char*argv[])
{
struct device_spec spec = {0};
ezusb_chip_t type = NONE;
const char *ihex_path = 0;
const char *stage1 = 0;
int opt;
int config = -1;
while ((opt = getopt (argc, argv, "h?VvI:D:c:s:t:")) != EOF) {
switch (opt) {
case 'I':
ihex_path = optarg;
break;
case 'V':
puts(FXLOAD_VERSION);
return 0;
case 'D':
parse_device_path(optarg, &spec);
break;
case 'c':
config = strtoul (optarg, 0, 0);
if (config < 0 || config > 255) {
logerror("illegal config byte: %s\n", optarg);
usage(argv[0]);
}
break;
case 's':
stage1 = optarg;
break;
case 't':
type = ((strcmp(optarg, "an21") == 0) ? AN21 :
(strcmp(optarg, "fx") == 0) ? FX :
(strcmp(optarg, "fx2") == 0) ? FX2 :
(strcmp(optarg, "fx2lp") == 0) ? FX2LP : 0);
if (type == 0) {
logerror("illegal microcontroller type: %s\n", optarg);
usage(argv[0]);
}
break;
case 'v':
verbose++;
break;
default:
usage(argv[0]);
}
}
if (config >= 0) {
if (type == 0) {
logerror("must specify microcontroller type %s",
"to write EEPROM!\n");
usage(argv[0]);
}
if (!stage1 || !ihex_path) {
logerror("need 2nd stage loader and firmware %s",
"to write EEPROM!\n");
usage(argv[0]);
}
}
if (ihex_path) {
libusb_device_handle *device;
int status;
device = get_usb_device(&spec);
if (device == NULL) {
logerror("No device to configure\n");
return -1;
}
if (type == NONE) {
type = FX; /* an21-compatible for most purposes */
}
if (stage1) {
/* first stage: put loader into internal memory */
if (verbose)
logerror("1st stage: load 2nd stage loader\n");
status = ezusb_load_ram (device, stage1, type, 0);
if (status != 0)
return status;
/* second stage ... write either EEPROM, or RAM. */
if (config >= 0)
status = ezusb_load_eeprom (device, ihex_path, type, config);
else
status = ezusb_load_ram (device, ihex_path, type, 1);
if (status != 0)
return status;
} else {
/* single stage, put into internal memory */
if (verbose)
logerror("single stage: load on-chip memory\n");
status = ezusb_load_ram (device, ihex_path, type, 0);
if (status != 0)
return status;
}
libusb_close(device);
}
if (!ihex_path) {
logerror("missing hex file\n");
return -1;
}
exit(0);
}
/*
* $Log: main.c,v $
* Revision 1.2 2007/03/20 14:31:50 cfavi
* *** empty log message ***
*
* Revision 1.1 2007/03/19 20:46:30 cfavi
* fxload ported to use libusb
*
* Revision 1.8 2005/01/11 03:58:02 dbrownell
* From Dirk Jagdmann <[email protected]>: optionally output messages to
* syslog instead of stderr.
*
* Revision 1.7 2002/04/12 00:28:22 dbrownell
* support "-t an21" to program EEPROMs for those microcontrollers
*
* Revision 1.6 2002/04/02 05:26:15 dbrownell
* version display now noiseless (-V);
* '-?' (usage info) convention now explicit
*
* Revision 1.5 2002/02/26 20:10:28 dbrownell
* - "-s loader" option for 2nd stage loader
* - "-c byte" option to write EEPROM with 2nd stage
* - "-V" option to dump version code
*
* Revision 1.4 2002/01/17 14:19:28 dbrownell
* fix warnings
*
* Revision 1.3 2001/12/27 17:54:04 dbrownell
* forgot an important character :)
*
* Revision 1.2 2001/12/27 17:43:29 dbrownell
* fail on firmware download errors; add "-v" flag
*
* Revision 1.1 2001/06/12 00:00:50 stevewilliams
* Added the fxload program.
* Rework root makefile and hotplug.spec to install in prefix
* location without need of spec file for install.
*
*/