GeezerVison is showing github.com — Samples: fxload improvements and cleanup · Raghuvardhan-Reddy78/libusb@d345a1e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: examples/ezusb.c +44-20Lines changed: 44 additions & 20 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -532,9 +532,9 @@ static int ram_poke(void *context, uint32_t addr, bool external, | 532 | 532 | */ | 533 | 533 | int fx3_load_ram(libusb_device_handle *device, const char *path) | 534 | 534 | { | 535 | | - unsigned int dCheckSum, dExpectedCheckSum, dAddress, i, dLen, dLength; | 536 | | - unsigned short wSignature; | 537 | | - unsigned int dImageBuf[512 * 1024]; | | 535 | + uint32_t dCheckSum, dExpectedCheckSum, dAddress, i, dLen, dLength; | | 536 | + uint16_t wSignature; | | 537 | + uint32_t* dImageBuf; | 538 | 538 | unsigned char *bBuf, rBuf[4096]; | 539 | 539 | FILE *image; | 540 | 540 |
| @@ -545,22 +545,38 @@ int fx3_load_ram(libusb_device_handle *device, const char *path) | 545 | 545 | } else if (verbose) | 546 | 546 | logerror("open firmware image %s for RAM upload\n", path); | 547 | 547 |
| 548 | | - fread(&wSignature, 1, 2, image); // read signature bytes | 549 | | - if (wSignature != 0x5943) { // check CY signature byte | 550 | | - logerror("Invalid image"); | | 548 | + if ((fread(&wSignature, sizeof(uint16_t), 1, image) != 1) || | | 549 | + (wSignature != 0x5943)) { // check "CY" signature byte | | 550 | + logerror("invalid image (signature error)"); | | 551 | + return -3; | | 552 | + } | | 553 | + if (fread(&i, 1, 2, image) != 2) { // skip 2 dummy bytes | | 554 | + logerror("could not read image"); | 551 | 555 | return -3; | 552 | 556 | } | 553 | | - fread(&i, 2, 1, image); // skip 2 dummy bytes | 554 | 557 |
| 555 | 558 | dCheckSum = 0; | 556 | 559 | while (1) { | 557 | | - fread(&dLength, 4, 1, image); // read dLength | 558 | | - fread(&dAddress, 4, 1, image); // read dAddress | | 560 | + if ((fread(&dLength, sizeof(uint32_t), 1, image) != 1) || // read dLength | | 561 | + (fread(&dAddress, sizeof(uint32_t), 1, image) != 1)) { // read dAddress | | 562 | + logerror("could not read image"); | | 563 | + return -3; | | 564 | + } | 559 | 565 | if (dLength == 0) | 560 | 566 | break; // done | 561 | 567 |
| | 568 | + dImageBuf = calloc(dLength, sizeof(uint32_t)); | | 569 | + if (dImageBuf == NULL) { | | 570 | + logerror("could not allocate buffer for image chunk\n"); | | 571 | + return -4; | | 572 | + } | | 573 | + | 562 | 574 | // read sections | 563 | | - fread(dImageBuf, 4, dLength, image); | | 575 | + if (fread(dImageBuf, sizeof(uint32_t), dLength, image) != dLength) { | | 576 | + logerror("could not read image"); | | 577 | + free(dImageBuf); | | 578 | + return -3; | | 579 | + } | 564 | 580 | for (i = 0; i < dLength; i++) | 565 | 581 | dCheckSum += dImageBuf[i]; | 566 | 582 | dLength <<= 2; // convert to Byte length | @@ -570,33 +586,41 @@ int fx3_load_ram(libusb_device_handle *device, const char *path) | 570 | 586 | dLen = 4096; // 4K max | 571 | 587 | if (dLen > dLength) | 572 | 588 | dLen = dLength; | 573 | | - ezusb_write(device, "Write firmware", RW_INTERNAL, dAddress, bBuf, dLen); | 574 | | - ezusb_read(device, "Read firmware", RW_INTERNAL, dAddress, rBuf, dLen); | | 589 | + if ((ezusb_write(device, "write firmware", RW_INTERNAL, dAddress, bBuf, dLen) < 0) || | | 590 | + (ezusb_read(device, "read firmware", RW_INTERNAL, dAddress, rBuf, dLen) < 0)) { | | 591 | + logerror("R/W error\n"); | | 592 | + free(dImageBuf); | | 593 | + return -5; | | 594 | + } | 575 | 595 | // Verify data: rBuf with bBuf | 576 | 596 | for (i = 0; i < dLen; i++) { | 577 | 597 | if (rBuf[i] != bBuf[i]) { | 578 | | - logerror("Fail to verify image"); | 579 | | - return -3; | | 598 | + logerror("verify error"); | | 599 | + free(dImageBuf); | | 600 | + return -6; | 580 | 601 | } | 581 | 602 | } | 582 | 603 |
| 583 | 604 | dLength -= dLen; | 584 | 605 | bBuf += dLen; | 585 | 606 | dAddress += dLen; | 586 | 607 | } | | 608 | + free(dImageBuf); | 587 | 609 | } | 588 | 610 |
| 589 | 611 | // read pre-computed checksum data | 590 | | - fread(&dExpectedCheckSum, 4, 1, image); | 591 | | - if (dCheckSum != dExpectedCheckSum) { | 592 | | - logerror("Fail to boot due to checksum error\n"); | 593 | | - return -4; | | 612 | + if ((fread(&dExpectedCheckSum, sizeof(uint32_t), 1, image) != 1) || | | 613 | + (dCheckSum != dExpectedCheckSum)) { | | 614 | + logerror("checksum error\n"); | | 615 | + return -7; | 594 | 616 | } | 595 | 617 |
| 596 | 618 | // transfer execution to Program Entry | 597 | | - ezusb_write(device, "Jump command", RW_INTERNAL, dAddress, NULL, 0); | | 619 | + if (ezusb_write(device, "Jump command", RW_INTERNAL, dAddress, NULL, 0) < 0) { | | 620 | + logerror("failed to send jump command\n"); | | 621 | + return -6; | | 622 | + } | 598 | 623 |
| 599 | | - logerror("Done!\n"); | 600 | 624 | return 0; | 601 | 625 | } | 602 | 626 |
| |
Collapse file Copy file name to clipboardExpand all lines: examples/ezusb.h +9Lines changed: 9 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 55 | 55 | #define IMG_TYPE_MAX 4 | 56 | 56 | #define IMG_TYPE_NAMES { "Intel HEX", "Cypress 8051 IIC", "Cypress 8051 BIX", "Cypress IMG format" } | 57 | 57 |
| | 58 | +#ifdef __cplusplus | | 59 | +extern "C" { | | 60 | +#endif | | 61 | + | 58 | 62 | /* | 59 | 63 | * Automatically identified devices (VID, PID, type, designation). | 60 | 64 | * TODO: Could use some validation. Also where's the FX2? | @@ -108,4 +112,9 @@ extern int ezusb_load_eeprom(libusb_device_handle *device, | 108 | 112 |
| 109 | 113 | /* boolean flag, says whether to write extra messages to stderr */ | 110 | 114 | extern int verbose; | | 115 | + | | 116 | +#ifdef __cplusplus | | 117 | +} | | 118 | +#endif | | 119 | + | 111 | 120 | #endif |
Collapse file Copy file name to clipboardExpand all lines: examples/fxload.c +35-22Lines changed: 35 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | | 4 | 4 | | 5 | 5 | | | 6 | + | 6 | 7 | * | 7 | 8 | * This source code is free software; you can redistribute it | 8 | 9 | * and/or modify it in source code form under the terms of the GNU | | 20 | 21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | 21 | 22 | */ | 22 | 23 |
| 23 | | -/* | 24 | | - * This program supports uploading firmware into a target USB device. | 25 | | - * | 26 | | - * -I <path> -- Upload this firmware | 27 | | - * -t <type> -- uController type: an21, fx, fx2, fx2lp, fx3 | 28 | | - * | 29 | | - * -D <vid:pid> -- Use this device, instead of $DEVICE | 30 | | - * | 31 | | - * -V -- Print version ID for program | 32 | | - */ | 33 | 24 | #include <stdlib.h> | 34 | 25 | #include <stdio.h> | 35 | 26 | #include <string.h> | @@ -79,24 +70,39 @@ int main(int argc, char*argv[]) | 79 | 70 | { | 80 | 71 | fx_known_device known_device[] = FX_KNOWN_DEVICES; | 81 | 72 | const char *path[] = { NULL, NULL }; | 82 | | - const char *device_id = getenv("DEVICE"); | | 73 | + const char *device_id = NULL; | | 74 | + const char *device_path = getenv("DEVICE"); | 83 | 75 | const char *type = NULL; | 84 | 76 | const char *fx_name[FX_TYPE_MAX] = FX_TYPE_NAMES; | 85 | 77 | const char *ext, *img_name[] = IMG_TYPE_NAMES; | 86 | 78 | int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)]; | 87 | 79 | int i, j, opt, status; | 88 | 80 | unsigned vid = 0, pid = 0; | | 81 | + unsigned busnum = 0, devaddr = 0; | 89 | 82 | libusb_device *dev, **devs; | 90 | 83 | libusb_device_handle *device = NULL; | 91 | 84 | struct libusb_device_descriptor desc; | 92 | 85 |
| 93 | | - while ((opt = getopt(argc, argv, "vV?D:I:c:s:t:")) != EOF) | | 86 | + while ((opt = getopt(argc, argv, "vV?hd:a:i:I:t:")) != EOF) | 94 | 87 | switch (opt) { | 95 | 88 |
| 96 | | - case 'D': | | 89 | + case 'd': | 97 | 90 | device_id = optarg; | | 91 | + if (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 ) { | | 92 | + fputs ("please specify VID & PID as \"vid:pid\", in hexadecimal format\n", stderr); | | 93 | + return -1; | | 94 | + } | 98 | 95 | break; | 99 | 96 |
| | 97 | + case 'a': | | 98 | + device_path = optarg; | | 99 | + if (sscanf(device_path, "%u:%u", &busnum, &devaddr) != 2 ) { | | 100 | + fputs ("please specify bus number & device address as \"bus:addr\", in decimal format\n", stderr); | | 101 | + return -1; | | 102 | + } | | 103 | + break; | | 104 | + | | 105 | + case 'i': | 100 | 106 | case 'I': | 101 | 107 | path[FIRMWARE] = optarg; | 102 | 108 | break; | @@ -114,6 +120,7 @@ int main(int argc, char*argv[]) | 114 | 120 | break; | 115 | 121 |
| 116 | 122 | case '?': | | 123 | + case 'h': | 117 | 124 | default: | 118 | 125 | goto usage; | 119 | 126 |
| @@ -122,13 +129,13 @@ int main(int argc, char*argv[]) | 122 | 129 | if (path[FIRMWARE] == NULL) { | 123 | 130 | logerror("no firmware specified!\n"); | 124 | 131 | usage: | 125 | | - fprintf(stderr, "\nusage: %s [-vV] [-t type] [-D vid:pid] -I firmware\n", argv[0]); | 126 | | - fprintf(stderr, " type: one of an21, fx, fx2, fx2lp, fx3\n"); | 127 | | - return -1; | 128 | | - } | 129 | | - | 130 | | - if ((device_id != NULL) && (sscanf(device_id, "%x:%x" , &vid, &pid) != 2 )) { | 131 | | - fputs ("please specify VID & PID as \"vid:pid\" in hexadecimal format\n", stderr); | | 132 | + fprintf(stderr, "\nUsage: fxload [-v] [-V] [-t type] [-d vid:pid] [-a bus:addr] -i firmware\n"); | | 133 | + fprintf(stderr, " -i <path> -- Firmware to upload\n"); | | 134 | + fprintf(stderr, " -t <type> -- Target type: an21, fx, fx2, fx2lp, fx3\n"); | | 135 | + fprintf(stderr, " -d <vid:pid> -- Target device, as an USB VID:PID\n"); | | 136 | + fprintf(stderr, " -a <bus:addr> -- Target device, as a libusbx bus and address\n"); | | 137 | + fprintf(stderr, " -v -- Increase verbosity\n"); | | 138 | + fprintf(stderr, " -V -- Print program version\n"); | 132 | 139 | return -1; | 133 | 140 | } | 134 | 141 |
| @@ -155,12 +162,17 @@ int main(int argc, char*argv[]) | 155 | 162 | libusb_set_debug(NULL, verbose); | 156 | 163 |
| 157 | 164 | /* try to pick up missing parameters from known devices */ | 158 | | - if ((type == NULL) || (device_id == NULL)) { | | 165 | + if (device_path || (type == NULL) || (device_id == NULL)) { | 159 | 166 | if (libusb_get_device_list(NULL, &devs) < 0) { | 160 | 167 | logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status)); | 161 | 168 | goto err; | 162 | 169 | } | 163 | 170 | for (i=0; (dev=devs[i]) != NULL; i++) { | | 171 | + if (device_path) { | | 172 | + if ((libusb_get_bus_number(dev) == busnum) && (libusb_get_device_address(dev) == devaddr)) | | 173 | + break; | | 174 | + continue; | | 175 | + } | 164 | 176 | status = libusb_get_device_descriptor(dev, &desc); | 165 | 177 | if (status >= 0) { | 166 | 178 | if (verbose >= 2) | @@ -195,7 +207,7 @@ int main(int argc, char*argv[]) | 195 | 207 | } | 196 | 208 | if (dev == NULL) { | 197 | 209 | libusb_free_device_list(devs, 1); | 198 | | - logerror("could not find a known device - please specify type and/or vid:pid\n"); | | 210 | + logerror("could not find a known device - please specify type and/or vid:pid or bus:addr\n"); | 199 | 211 | goto usage; | 200 | 212 | } | 201 | 213 | status = libusb_open(dev, &device); | @@ -211,6 +223,7 @@ int main(int argc, char*argv[]) | 211 | 223 | goto err; | 212 | 224 | } | 213 | 225 | } | | 226 | + | 214 | 227 | /* We need to claim the first interface */ | 215 | 228 | status = libusb_claim_interface(device, 0); | 216 | 229 | #if defined(__linux__) | |
Collapse file Copy file name to clipboard +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -#define LIBUSB_NANO 10628 | | 1 | +#define LIBUSB_NANO 10629 |
|