diff options
-rw-r--r-- | examples/ezusb.c | 21 | ||||
-rw-r--r-- | examples/fxload.c | 114 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
3 files changed, 77 insertions, 60 deletions
diff --git a/examples/ezusb.c b/examples/ezusb.c index 1551187..5086da1 100644 --- a/examples/ezusb.c +++ b/examples/ezusb.c @@ -533,25 +533,30 @@ static int ram_poke(void *context, uint32_t addr, bool external, static int fx3_load_ram(libusb_device_handle *device, const char *path) { uint32_t dCheckSum, dExpectedCheckSum, dAddress, i, dLen, dLength; - uint16_t wSignature; uint32_t* dImageBuf; - unsigned char *bBuf, rBuf[4096]; + unsigned char *bBuf, hBuf[4], rBuf[4096]; FILE *image; image = fopen(path, "rb"); if (image == NULL) { - logerror("%s: unable to open for input.\n", path); + logerror("unable to open '%s' for input\n", path); return -2; } else if (verbose) logerror("open firmware image %s for RAM upload\n", path); - if ((fread(&wSignature, sizeof(uint16_t), 1, image) != 1) || - (wSignature != 0x5943)) { // check "CY" signature byte - logerror("invalid image (signature error)"); + // Read header + if (fread(hBuf, sizeof(char), sizeof(hBuf), image) != sizeof(hBuf)) { + logerror("could not read image header"); + return -3; + } + + // check "CY" signature byte and format + if ((hBuf[0] != 'C') || (hBuf[1] != 'Y')) { + logerror("image doesn't have a CYpress signature\n"); return -3; } - if (fread(&i, 1, 2, image) != 2) { // skip 2 dummy bytes - logerror("could not read image"); + if (hBuf[3] != 0xB0) { + logerror("invalid file format 0x%02X, expected 0xB0\n", hBuf[3]); return -3; } diff --git a/examples/fxload.c b/examples/fxload.c index d69eea4..8537ca2 100644 --- a/examples/fxload.c +++ b/examples/fxload.c @@ -64,6 +64,17 @@ void logerror(const char *format, ...) va_end(ap); } +static int print_usage(int errcode) { + fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-p bus,addr] -i firmware\n"); + fprintf(stderr, " -i <path> -- Firmware to upload\n"); + fprintf(stderr, " -t <type> -- Target type: an21, fx, fx2, fx2lp, fx3\n"); + fprintf(stderr, " -d <vid:pid> -- Target device, as an USB VID:PID\n"); + fprintf(stderr, " -p <bus,addr> -- Target device, as a libusbx bus number and device address path\n"); + fprintf(stderr, " -v -- Increase verbosity\n"); + fprintf(stderr, " -V -- Print program version\n"); + return errcode; +} + #define FIRMWARE 0 #define LOADER 1 int main(int argc, char*argv[]) @@ -78,26 +89,26 @@ int main(int argc, char*argv[]) int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)]; int i, j, opt, status; unsigned vid = 0, pid = 0; - unsigned busnum = 0, devaddr = 0; + unsigned busnum = 0, devaddr = 0, _busnum, _devaddr; libusb_device *dev, **devs; libusb_device_handle *device = NULL; struct libusb_device_descriptor desc; - while ((opt = getopt(argc, argv, "vV?hd:a:i:I:t:")) != EOF) + while ((opt = getopt(argc, argv, "vV?hd:p:i:I:t:")) != EOF) switch (opt) { case 'd': device_id = optarg; if (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 ) { - fputs ("please specify VID & PID as \"vid:pid\", in hexadecimal format\n", stderr); + fputs ("please specify VID & PID as \"vid:pid\" in hexadecimal format\n", stderr); return -1; } break; - case 'a': + case 'p': device_path = optarg; - if (sscanf(device_path, "%u:%u", &busnum, &devaddr) != 2 ) { - fputs ("please specify bus number & device address as \"bus:addr\", in decimal format\n", stderr); + if (sscanf(device_path, "%u,%u", &busnum, &devaddr) != 2 ) { + fputs ("please specify bus number & device number as \"bus,dev\" in decimal format\n", stderr); return -1; } break; @@ -122,21 +133,17 @@ int main(int argc, char*argv[]) case '?': case 'h': default: - goto usage; + return print_usage(-1); } if (path[FIRMWARE] == NULL) { logerror("no firmware specified!\n"); -usage: - fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-a bus:addr] -i firmware\n"); - fprintf(stderr, " -i <path> -- Firmware to upload\n"); - fprintf(stderr, " -t <type> -- Target type: an21, fx, fx2, fx2lp, fx3\n"); - fprintf(stderr, " -d <vid:pid> -- Target device, as an USB VID:PID\n"); - fprintf(stderr, " -a <bus:addr> -- Target device, as a libusbx bus and address\n"); - fprintf(stderr, " -v -- Increase verbosity\n"); - fprintf(stderr, " -V -- Print program version\n"); - return -1; + return print_usage(-1); + } + if ((device_id != NULL) && (device_path != NULL)) { + logerror("only one of -d or -a can be specified\n"); + return print_usage(-1); } /* determine the target type */ @@ -149,7 +156,7 @@ usage: } if (i >= FX_TYPE_MAX) { logerror("illegal microcontroller type: %s\n", type); - goto usage; + return print_usage(-1); } } @@ -162,53 +169,58 @@ usage: libusb_set_debug(NULL, verbose); /* try to pick up missing parameters from known devices */ - if (device_path || (type == NULL) || (device_id == NULL)) { + if ((type == NULL) || (device_id == NULL) || (device_path != NULL)) { if (libusb_get_device_list(NULL, &devs) < 0) { logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status)); goto err; } for (i=0; (dev=devs[i]) != NULL; i++) { - if (device_path) { + _busnum = libusb_get_bus_number(dev); + _devaddr = libusb_get_device_address(dev); + if ((type != NULL) && (device_path != NULL)) { + // if both a type and bus,addr were specified, we just need to find our match if ((libusb_get_bus_number(dev) == busnum) && (libusb_get_device_address(dev) == devaddr)) break; - continue; - } - status = libusb_get_device_descriptor(dev, &desc); - if (status >= 0) { - if (verbose >= 2) - logerror("trying to match against %04x:%04x\n", desc.idVendor, desc.idProduct); - for (j=0; j<ARRAYSIZE(known_device); j++) { - if ((desc.idVendor == known_device[j].vid) - && (desc.idProduct == known_device[j].pid)) { - if ((type == NULL) && (device_id == NULL)) { - fx_type = known_device[j].type; - vid = desc.idVendor; - pid = desc.idProduct; - break; - } else if ((type == NULL) && (vid == desc.idVendor) - && (pid == desc.idProduct)) { - fx_type = known_device[j].type; - break; - } else if ((device_id == NULL) - && (fx_type == known_device[j].type)) { - vid = desc.idVendor; - pid = desc.idProduct; - break; + } else { + status = libusb_get_device_descriptor(dev, &desc); + if (status >= 0) { + if (verbose >= 2) { + logerror("trying to match against %04x:%04x (%d,%d)\n", + desc.idVendor, desc.idProduct, _busnum, _devaddr); + } + for (j=0; j<ARRAYSIZE(known_device); j++) { + if ((desc.idVendor == known_device[j].vid) + && (desc.idProduct == known_device[j].pid)) { + if (// nothing was specified + ((type == NULL) && (device_id == NULL) && (device_path == NULL)) || + // vid:pid was specified and we have a match + ((type == NULL) && (device_id != NULL) && (vid == desc.idVendor) && (pid == desc.idProduct)) || + // bus,addr was specified and we have a match + ((type == NULL) && (device_path != NULL) && (busnum == _busnum) && (devaddr == _devaddr)) || + // type was specified and we have a match + ((type != NULL) && (device_id == NULL) && (device_path == NULL) && (fx_type == known_device[j].type)) ) { + fx_type = known_device[j].type; + vid = desc.idVendor; + pid = desc.idProduct; + busnum = _busnum; + devaddr = _devaddr; + break; + } } } - } - if (j < ARRAYSIZE(known_device)) { - if (verbose) - logerror("found device '%s' [%04x:%04x]\n", - known_device[j].designation, vid, pid); - break; + if (j < ARRAYSIZE(known_device)) { + if (verbose) + logerror("found device '%s' [%04x:%04x] (%d,%d)\n", + known_device[j].designation, vid, pid, busnum, devaddr); + break; + } } } } if (dev == NULL) { libusb_free_device_list(devs, 1); - logerror("could not find a known device - please specify type and/or vid:pid or bus:addr\n"); - goto usage; + logerror("could not find a known device - please specify type and/or vid:pid and/or bus,dev\n"); + return print_usage(-1); } status = libusb_open(dev, &device); if (status < 0) { @@ -216,7 +228,7 @@ usage: goto err; } libusb_free_device_list(devs, 1); - } else { + } else if (device_id != NULL) { device = libusb_open_device_with_vid_pid(NULL, (uint16_t)vid, (uint16_t)pid); if (device == NULL) { logerror("libusb_open() failed\n"); diff --git a/libusb/version_nano.h b/libusb/version_nano.h index 22e03e4..909ed7a 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10631 +#define LIBUSB_NANO 10632 |