2 * Copyright © 2001 Stephen Williams (steve@icarus.com)
3 * Copyright © 2001-2002 David Brownell (dbrownell@users.sourceforge.net)
4 * Copyright © 2008 Roger Williams (rawqux@users.sourceforge.net)
5 * Copyright © 2012 Pete Batard (pete@akeo.ie)
6 * Copyright © 2013 Federico Manzan (f.manzan@gmail.com)
8 * This source code is free software; you can redistribute it
9 * and/or modify it in source code form under the terms of the GNU
10 * General Public License as published by the Free Software
11 * Foundation; either version 2 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
31 #include <sys/types.h>
37 #if !defined(_WIN32) || defined(__CYGWIN__)
39 static bool dosyslog = false;
41 #define _stricmp strcasecmp
44 #ifndef FXLOAD_VERSION
45 #define FXLOAD_VERSION (__DATE__ " (libusb)")
49 #define ARRAYSIZE(A) (sizeof(A)/sizeof((A)[0]))
52 void logerror(const char *format, ...)
57 #if !defined(_WIN32) || defined(__CYGWIN__)
59 vsyslog(LOG_ERR, format, ap);
62 vfprintf(stderr, format, ap);
66 static int print_usage(int error_code) {
67 fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-p bus,addr] [-s loader] -i firmware\n");
68 fprintf(stderr, " -i <path> -- Firmware to upload\n");
69 fprintf(stderr, " -s <path> -- Second stage loader\n");
70 fprintf(stderr, " -t <type> -- Target type: an21, fx, fx2, fx2lp, fx3\n");
71 fprintf(stderr, " -d <vid:pid> -- Target device, as an USB VID:PID\n");
72 fprintf(stderr, " -p <bus,addr> -- Target device, as a libusb bus number and device address path\n");
73 fprintf(stderr, " -v -- Increase verbosity\n");
74 fprintf(stderr, " -q -- Decrease verbosity (silent mode)\n");
75 fprintf(stderr, " -V -- Print program version\n");
81 int main(int argc, char*argv[])
83 fx_known_device known_device[] = FX_KNOWN_DEVICES;
84 const char *path[] = { NULL, NULL };
85 const char *device_id = NULL;
86 const char *device_path = getenv("DEVICE");
87 const char *type = NULL;
88 const char *fx_name[FX_TYPE_MAX] = FX_TYPE_NAMES;
89 const char *ext, *img_name[] = IMG_TYPE_NAMES;
90 int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)];
93 unsigned vid = 0, pid = 0;
94 unsigned busnum = 0, devaddr = 0, _busnum, _devaddr;
95 libusb_device *dev, **devs;
96 libusb_device_handle *device = NULL;
97 struct libusb_device_descriptor desc;
99 while ((opt = getopt(argc, argv, "qvV?hd:p:i:I:s:S:t:")) != EOF)
104 if (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 ) {
105 fputs ("please specify VID & PID as \"vid:pid\" in hexadecimal format\n", stderr);
111 device_path = optarg;
112 if (sscanf(device_path, "%u,%u", &busnum, &devaddr) != 2 ) {
113 fputs ("please specify bus number & device number as \"bus,dev\" in decimal format\n", stderr);
120 path[FIRMWARE] = optarg;
125 path[LOADER] = optarg;
129 puts(FXLOAD_VERSION);
147 return print_usage(-1);
151 if (path[FIRMWARE] == NULL) {
152 logerror("no firmware specified!\n");
153 return print_usage(-1);
155 if ((device_id != NULL) && (device_path != NULL)) {
156 logerror("only one of -d or -p can be specified\n");
157 return print_usage(-1);
160 /* determine the target type */
162 for (i=0; i<FX_TYPE_MAX; i++) {
163 if (strcmp(type, fx_name[i]) == 0) {
168 if (i >= FX_TYPE_MAX) {
169 logerror("illegal microcontroller type: %s\n", type);
170 return print_usage(-1);
174 /* open the device using libusb */
175 status = libusb_init(NULL);
177 logerror("libusb_init() failed: %s\n", libusb_error_name(status));
180 libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, verbose);
182 /* try to pick up missing parameters from known devices */
183 if ((type == NULL) || (device_id == NULL) || (device_path != NULL)) {
184 if (libusb_get_device_list(NULL, &devs) < 0) {
185 logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status));
188 for (i=0; (dev=devs[i]) != NULL; i++) {
189 _busnum = libusb_get_bus_number(dev);
190 _devaddr = libusb_get_device_address(dev);
191 if ((type != NULL) && (device_path != NULL)) {
192 // if both a type and bus,addr were specified, we just need to find our match
193 if ((libusb_get_bus_number(dev) == busnum) && (libusb_get_device_address(dev) == devaddr))
196 status = libusb_get_device_descriptor(dev, &desc);
199 logerror("examining %04x:%04x (%d,%d)\n",
200 desc.idVendor, desc.idProduct, _busnum, _devaddr);
202 for (j=0; j<ARRAYSIZE(known_device); j++) {
203 if ((desc.idVendor == known_device[j].vid)
204 && (desc.idProduct == known_device[j].pid)) {
205 if (// nothing was specified
206 ((type == NULL) && (device_id == NULL) && (device_path == NULL)) ||
207 // vid:pid was specified and we have a match
208 ((type == NULL) && (device_id != NULL) && (vid == desc.idVendor) && (pid == desc.idProduct)) ||
209 // bus,addr was specified and we have a match
210 ((type == NULL) && (device_path != NULL) && (busnum == _busnum) && (devaddr == _devaddr)) ||
211 // type was specified and we have a match
212 ((type != NULL) && (device_id == NULL) && (device_path == NULL) && (fx_type == known_device[j].type)) ) {
213 fx_type = known_device[j].type;
215 pid = desc.idProduct;
222 if (j < ARRAYSIZE(known_device)) {
224 logerror("found device '%s' [%04x:%04x] (%d,%d)\n",
225 known_device[j].designation, vid, pid, busnum, devaddr);
232 libusb_free_device_list(devs, 1);
234 logerror("could not find a known device - please specify type and/or vid:pid and/or bus,dev\n");
235 return print_usage(-1);
237 status = libusb_open(dev, &device);
238 libusb_free_device_list(devs, 1);
240 logerror("libusb_open() failed: %s\n", libusb_error_name(status));
243 } else if (device_id != NULL) {
244 device = libusb_open_device_with_vid_pid(NULL, (uint16_t)vid, (uint16_t)pid);
245 if (device == NULL) {
246 logerror("libusb_open() failed\n");
251 /* We need to claim the first interface */
252 libusb_set_auto_detach_kernel_driver(device, 1);
253 status = libusb_claim_interface(device, 0);
254 if (status != LIBUSB_SUCCESS) {
255 libusb_close(device);
256 logerror("libusb_claim_interface failed: %s\n", libusb_error_name(status));
261 logerror("microcontroller type: %s\n", fx_name[fx_type]);
263 for (i=0; i<ARRAYSIZE(path); i++) {
264 if (path[i] != NULL) {
265 ext = path[i] + strlen(path[i]) - 4;
266 if ((_stricmp(ext, ".hex") == 0) || (strcmp(ext, ".ihx") == 0))
267 img_type[i] = IMG_TYPE_HEX;
268 else if (_stricmp(ext, ".iic") == 0)
269 img_type[i] = IMG_TYPE_IIC;
270 else if (_stricmp(ext, ".bix") == 0)
271 img_type[i] = IMG_TYPE_BIX;
272 else if (_stricmp(ext, ".img") == 0)
273 img_type[i] = IMG_TYPE_IMG;
275 logerror("%s is not a recognized image type\n", path[i]);
279 if (verbose && path[i] != NULL)
280 logerror("%s: type %s\n", path[i], img_name[img_type[i]]);
283 if (path[LOADER] == NULL) {
284 /* single stage, put into internal memory */
286 logerror("single stage: load on-chip memory\n");
287 status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 0);
289 /* two-stage, put loader into internal memory */
291 logerror("1st stage: load 2nd stage loader\n");
292 status = ezusb_load_ram(device, path[LOADER], fx_type, img_type[LOADER], 0);
294 /* two-stage, put firmware into internal memory */
296 logerror("2nd state: load on-chip memory\n");
297 status = ezusb_load_ram(device, path[FIRMWARE], fx_type, img_type[FIRMWARE], 1);
301 libusb_release_interface(device, 0);
302 libusb_close(device);