-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathezusb.c
317 lines (257 loc) · 8.16 KB
/
ezusb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "ezusb.h"
#include <inttypes.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "libusb.h"
#include <stdio.h>
/**
* Read a 32 bits little endian unsigned integer out of memory.
* @param x a pointer to the input memory
* @return the corresponding unsigned integer
*/
#define RL32(x) \
(((unsigned)((const uint8_t *)(x))[3] << 24) | \
((unsigned)((const uint8_t *)(x))[2] << 16) | \
((unsigned)((const uint8_t *)(x))[1] << 8) | \
(unsigned)((const uint8_t *)(x))[0])
#undef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define FW_CHUNKSIZE (4 * 1024)
/**
* Retrieve the size of the open stream @a file.
*
* This function only works on seekable streams. However, the set of seekable
* streams is generally congruent with the set of streams that have a size.
* Code that needs to work with any type of stream (including pipes) should
* require neither seekability nor advance knowledge of the size.
* On failure, the return value is negative and errno is set.
*
* @param file An I/O stream opened in binary mode.
* @return The size of @a file in bytes, or a negative value on failure.
*
* @private
*/
int64_t file_get_size(FILE *file) {
off_t filepos, filesize;
/* ftello() and fseeko() are not standard C, but part of POSIX.1-2001.
* Thus, if these functions are available at all, they can reasonably
* be expected to also conform to POSIX semantics. In particular, this
* means that ftello() after fseeko(..., SEEK_END) has a defined result
* and can be used to get the size of a seekable stream.
* On Windows, the result is fully defined only for binary streams.
*/
filepos = ftello(file);
if (filepos < 0)
return -1;
if (fseeko(file, 0, SEEK_END) < 0)
return -1;
filesize = ftello(file);
if (filesize < 0)
return -1;
if (fseeko(file, filepos, SEEK_SET) < 0)
return -1;
return filesize;
}
int ezusb_install_firmware(libusb_device_handle *hdl, const char *filename) {
unsigned char *firmware;
void *buf;
size_t n_read, length, offset, chunksize;
int ret, result;
FILE *file = NULL;
int64_t filesize;
const size_t max_size = 0x86000;
file = fopen(filename, "rb");
if (file)
fprintf(stderr, "Opened '%s'.\n", filename);
else {
fprintf(stderr, "Attempt to open file failed\n");
free(file);
}
if (!file) {
fprintf(stderr, "Failed to locate '%s'.\n", filename);
return -1;
}
filesize = file_get_size(file);
if (filesize < 0) {
fprintf(stderr, "Failed to obtain size of '%s'\n", filename);
fclose(file);
return -1;
}
if (filesize > max_size) {
fprintf(stderr, "Size %" PRIu64 " of '%s' exceeds limit %zu.\n", filesize,
filename, max_size);
if (fclose(file) < 0)
fprintf(stderr, "Failed to close file\n");
return -1;
}
buf = malloc(filesize);
if (!buf) {
fprintf(stderr, "Failed to allocate buffer for '%s'.\n", filename);
return -1;
}
n_read = fread(buf, 1, filesize, file);
if (fclose(file) < 0) {
fprintf(stderr, "Failed to close file\n");
return -1;
}
if (n_read < 0 || (size_t)n_read != filesize) {
if (n_read >= 0)
fprintf(stderr, "Failed to read '%s': premature end of file.\n",
filename);
free(buf);
return -1;
}
firmware = buf;
length = filesize;
if (!firmware)
return -1;
fprintf(stderr, "Uploading firmware '%s'.\n", filename);
result = 0;
offset = 0;
if (length < 4 || firmware[0] != 'C' || firmware[1] != 'Y' ||
firmware[3] != 0xb0) {
fprintf(stderr, "Invalid signature on firmware\n");
free(firmware);
return -1;
}
offset = 4;
while (offset < length) {
size_t addr, sublength, suboffset;
if (offset + 4 == length) {
/* Skip checksum */
offset += 4;
break;
}
if (length < offset + 8) {
break;
}
sublength = RL32(firmware + offset) << 2;
offset += 4;
addr = RL32(firmware + offset);
offset += 4;
if (sublength > length - offset) {
break;
}
suboffset = 0;
do {
chunksize = MIN(sublength - suboffset, FW_CHUNKSIZE);
ret = libusb_control_transfer(
hdl, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, 0xa0,
(addr + suboffset) & 0xffff, (addr + suboffset) >> 16,
firmware + offset + suboffset, chunksize, 100);
if (ret < 0) {
fprintf(stderr, "Unable to send firmware to device: %s.\n",
libusb_error_name(ret));
free(firmware);
return -1;
}
fprintf(stderr, "Uploaded %zu bytes.\n", chunksize);
suboffset += chunksize;
} while (suboffset < sublength);
offset += sublength;
}
free(firmware);
if (offset < length) {
fprintf(stderr, "Firmware file is truncated.\n");
return -1;
}
fprintf(stderr, "Firmware upload done.\n");
return result;
}
int ezusb_reset(struct libusb_device_handle *hdl, int set_clear) {
int ret;
unsigned char buf[1];
fprintf(stderr, "setting CPU reset mode %s...\n", set_clear ? "on" : "off");
buf[0] = set_clear ? 1 : 0;
ret = libusb_control_transfer(hdl, LIBUSB_REQUEST_TYPE_VENDOR, 0xa0, 0xe600,
0x0000, buf, 1, 100);
if (ret < 0)
fprintf(stderr, "Unable to send control request: %s.\n",
libusb_error_name(ret));
return ret;
}
int ezusb_upload_firmware(libusb_device *dev, int configuration,
const char *filename) {
struct libusb_device_handle *hdl;
int ret;
fprintf(stderr, "Uploading firmware to device on %d.%d\n",
libusb_get_bus_number(dev), libusb_get_device_address(dev));
if ((ret = libusb_open(dev, &hdl)) < 0) {
fprintf(stderr, "failed to open device: %s.\n", libusb_error_name(ret));
return -1;
}
/*
* The libusb Darwin backend is broken: it can report a kernel driver being
* active, but detaching it always returns an error.
*/
#if !defined(__APPLE__)
if (libusb_kernel_driver_active(hdl, 0) == 1) {
if ((ret = libusb_detach_kernel_driver(hdl, 0)) < 0) {
fprintf(stderr, "failed to detach kernel driver: %s\n",
libusb_error_name(ret));
return -1;
}
}
#endif
if ((ret = libusb_set_configuration(hdl, configuration)) < 0) {
fprintf(stderr, "Unable to set configuration: %s\n",
libusb_error_name(ret));
return -1;
}
if (ezusb_install_firmware(hdl, filename) < 0)
return -1;
libusb_close(hdl);
return 0;
}
/**
* Check the USB configuration to determine if this device has a given
* manufacturer and product string.
*
* @return TRUE if the device's configuration profile strings
* configuration, FALSE otherwise.
*/
int usb_match_manuf_prod(libusb_device *dev, const char *manufacturer,
const char *product) {
struct libusb_device_descriptor des;
struct libusb_device_handle *hdl;
int ret;
unsigned char strdesc[64];
hdl = NULL;
ret = false;
while (!ret) {
/* Assume the FW has not been loaded, unless proven wrong. */
libusb_get_device_descriptor(dev, &des);
if (libusb_open(dev, &hdl) != 0)
break;
if (libusb_get_string_descriptor_ascii(hdl, des.iManufacturer, strdesc,
sizeof(strdesc)) < 0)
break;
if (strcmp((const char *)strdesc, manufacturer))
break;
if (libusb_get_string_descriptor_ascii(hdl, des.iProduct, strdesc,
sizeof(strdesc)) < 0)
break;
if (strcmp((const char *)strdesc, product))
break;
ret = true;
}
if (hdl)
libusb_close(hdl);
return ret;
}
int command_send(struct libusb_device_handle *dev_handle, enum FX3Command cmd,
uint32_t data) {
int ret;
/* Send the control message. */
ret = libusb_control_transfer(
dev_handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, cmd, 0, 0,
(unsigned char *)&data, sizeof(data), 0);
if (ret < 0) {
fprintf(stderr, "Could not send command: 0x%X with data: %d. Error : %s.\n",
cmd, data, libusb_error_name(ret));
return -1;
}
return 0;
}