From 8aa50632adb1dff6eacf5a78b998b8def29b4c38 Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Sat, 16 Mar 2013 23:36:47 +0000 Subject: Samples: More fxload improvements and cleanup * Type detection when bus,addr is specified * Improved check for FX3 image header * Switch back to using -p for bus,addr and use a comma to match the output of listdevs --- examples/ezusb.c | 21 ++++++---- examples/fxload.c | 114 ++++++++++++++++++++++++++++---------------------- 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 -- Firmware to upload\n"); + fprintf(stderr, " -t -- Target type: an21, fx, fx2, fx2lp, fx3\n"); + fprintf(stderr, " -d -- Target device, as an USB VID:PID\n"); + fprintf(stderr, " -p -- 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 -- Firmware to upload\n"); - fprintf(stderr, " -t -- Target type: an21, fx, fx2, fx2lp, fx3\n"); - fprintf(stderr, " -d -- Target device, as an USB VID:PID\n"); - fprintf(stderr, " -a -- 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= 0) { + if (verbose >= 2) { + logerror("trying to match against %04x:%04x (%d,%d)\n", + desc.idVendor, desc.idProduct, _busnum, _devaddr); + } + for (j=0; j