GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Samples: fxload improvements and cleanup · Raghuvardhan-Reddy78/libusb@d345a1e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • examples
    • ezusb.c
    • ezusb.h
    • fxload.c
  • libusb
    • version_nano.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎examples/ezusb.c‎

Copy file name to clipboardExpand all lines: examples/ezusb.c
+44-20Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,9 @@ static int ram_poke(void *context, uint32_t addr, bool external,
532532
*/
533533
int fx3_load_ram(libusb_device_handle *device, const char *path)
534534
{
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;
538538
unsigned char *bBuf, rBuf[4096];
539539
FILE *image;
540540

@@ -545,22 +545,38 @@ int fx3_load_ram(libusb_device_handle *device, const char *path)
545545
} else if (verbose)
546546
logerror("open firmware image %s for RAM upload\n", path);
547547

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");
551555
return -3;
552556
}
553-
fread(&i, 2, 1, image); // skip 2 dummy bytes
554557

555558
dCheckSum = 0;
556559
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+
}
559565
if (dLength == 0)
560566
break; // done
561567

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+
562574
// 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+
}
564580
for (i = 0; i < dLength; i++)
565581
dCheckSum += dImageBuf[i];
566582
dLength <<= 2; // convert to Byte length
@@ -570,33 +586,41 @@ int fx3_load_ram(libusb_device_handle *device, const char *path)
570586
dLen = 4096; // 4K max
571587
if (dLen > dLength)
572588
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+
}
575595
// Verify data: rBuf with bBuf
576596
for (i = 0; i < dLen; i++) {
577597
if (rBuf[i] != bBuf[i]) {
578-
logerror("Fail to verify image");
579-
return -3;
598+
logerror("verify error");
599+
free(dImageBuf);
600+
return -6;
580601
}
581602
}
582603

583604
dLength -= dLen;
584605
bBuf += dLen;
585606
dAddress += dLen;
586607
}
608+
free(dImageBuf);
587609
}
588610

589611
// 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;
594616
}
595617

596618
// 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+
}
598623

599-
logerror("Done!\n");
600624
return 0;
601625
}
602626

Collapse file

‎examples/ezusb.h‎

Copy file name to clipboardExpand all lines: examples/ezusb.h
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
#define IMG_TYPE_MAX 4
5656
#define IMG_TYPE_NAMES { "Intel HEX", "Cypress 8051 IIC", "Cypress 8051 BIX", "Cypress IMG format" }
5757

58+
#ifdef __cplusplus
59+
extern "C" {
60+
#endif
61+
5862
/*
5963
* Automatically identified devices (VID, PID, type, designation).
6064
* TODO: Could use some validation. Also where's the FX2?
@@ -108,4 +112,9 @@ extern int ezusb_load_eeprom(libusb_device_handle *device,
108112

109113
/* boolean flag, says whether to write extra messages to stderr */
110114
extern int verbose;
115+
116+
#ifdef __cplusplus
117+
}
118+
#endif
119+
111120
#endif
Collapse file

‎examples/fxload.c‎

Copy file name to clipboardExpand all lines: examples/fxload.c
+35-22Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © 2001-2002 David Brownell ([email protected])
44
* Copyright © 2008 Roger Williams ([email protected])
55
* Copyright © 2012 Pete Batard ([email protected])
6+
* Copyright © 2013 Federico Manzan ([email protected])
67
*
78
* This source code is free software; you can redistribute it
89
* and/or modify it in source code form under the terms of the GNU
@@ -20,16 +21,6 @@
2021
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
2122
*/
2223

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-
*/
3324
#include <stdlib.h>
3425
#include <stdio.h>
3526
#include <string.h>
@@ -79,24 +70,39 @@ int main(int argc, char*argv[])
7970
{
8071
fx_known_device known_device[] = FX_KNOWN_DEVICES;
8172
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");
8375
const char *type = NULL;
8476
const char *fx_name[FX_TYPE_MAX] = FX_TYPE_NAMES;
8577
const char *ext, *img_name[] = IMG_TYPE_NAMES;
8678
int fx_type = FX_TYPE_UNDEFINED, img_type[ARRAYSIZE(path)];
8779
int i, j, opt, status;
8880
unsigned vid = 0, pid = 0;
81+
unsigned busnum = 0, devaddr = 0;
8982
libusb_device *dev, **devs;
9083
libusb_device_handle *device = NULL;
9184
struct libusb_device_descriptor desc;
9285

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)
9487
switch (opt) {
9588

96-
case 'D':
89+
case 'd':
9790
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+
}
9895
break;
9996

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':
100106
case 'I':
101107
path[FIRMWARE] = optarg;
102108
break;
@@ -114,6 +120,7 @@ int main(int argc, char*argv[])
114120
break;
115121

116122
case '?':
123+
case 'h':
117124
default:
118125
goto usage;
119126

@@ -122,13 +129,13 @@ int main(int argc, char*argv[])
122129
if (path[FIRMWARE] == NULL) {
123130
logerror("no firmware specified!\n");
124131
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");
132139
return -1;
133140
}
134141

@@ -155,12 +162,17 @@ int main(int argc, char*argv[])
155162
libusb_set_debug(NULL, verbose);
156163

157164
/* 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)) {
159166
if (libusb_get_device_list(NULL, &devs) < 0) {
160167
logerror("libusb_get_device_list() failed: %s\n", libusb_error_name(status));
161168
goto err;
162169
}
163170
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+
}
164176
status = libusb_get_device_descriptor(dev, &desc);
165177
if (status >= 0) {
166178
if (verbose >= 2)
@@ -195,7 +207,7 @@ int main(int argc, char*argv[])
195207
}
196208
if (dev == NULL) {
197209
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");
199211
goto usage;
200212
}
201213
status = libusb_open(dev, &device);
@@ -211,6 +223,7 @@ int main(int argc, char*argv[])
211223
goto err;
212224
}
213225
}
226+
214227
/* We need to claim the first interface */
215228
status = libusb_claim_interface(device, 0);
216229
#if defined(__linux__)
Collapse file

‎libusb/version_nano.h‎

Copy file name to clipboard
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
#define LIBUSB_NANO 10628
1+
#define LIBUSB_NANO 10629

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.