This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from avrdudes/avrdude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfu.c
516 lines (408 loc) · 14.5 KB
/
dfu.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2012 Kirill Levchenko
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Id$ */
#include "ac_cfg.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include "avrdude.h"
#include "libavrdude.h"
#include "dfu.h"
#include "usbdevs.h" /* for USB_VENDOR_ATMEL */
/* If we don't have LibUSB, define dummy functions that report an error. */
#ifndef HAVE_LIBUSB
struct dfu_dev *dfu_open(char *port_name) {
avrdude_message(MSG_INFO, "%s: Error: No USB support in this compile of avrdude\n",
progname);
return NULL;
}
int dfu_init(struct dfu_dev *dfu, unsigned short usb_vid, unsigned short usb_pid) {
return -1;
}
void dfu_close(struct dfu_dev *dfu) {
/* nothing */
}
int dfu_getstatus(struct dfu_dev *dfu, struct dfu_status *status)
{
return -1;
}
int dfu_clrstatus(struct dfu_dev *dfu) {
return -1;
}
int dfu_download(struct dfu_dev *dfu, void * ptr, int size) {
return -1;
}
int dfu_upload(struct dfu_dev *dfu, void * ptr, int size) {
return -1;
}
#else
/* If we DO have LibUSB, we can define the real functions. */
/* DFU data structures and constants.
*/
#define DFU_TIMEOUT 200 /* ms */
#define DFU_DNLOAD 1
#define DFU_UPLOAD 2
#define DFU_GETSTATUS 3
#define DFU_CLRSTATUS 4
#define DFU_GETSTATE 5 /* FLIPv1 only; not used */
#define DFU_ABORT 6 /* FLIPv1 only */
/* Block counter global variable. Incremented each time a DFU_DNLOAD command
* is sent to the device.
*/
static uint16_t wIndex = 0;
/* INTERNAL FUNCTION PROTOTYPES
*/
static char * get_usb_string(usb_dev_handle * dev_handle, int index);
/* EXPORTED FUNCTION DEFINITIONS
*/
struct dfu_dev * dfu_open(char *port_spec)
{
struct dfu_dev *dfu;
char *bus_name = NULL;
char *dev_name = NULL;
/* The following USB device spec parsing code was copied from usbtiny.c. The
* expected format is "usb:BUS:DEV" where BUS and DEV are the bus and device
* names. We stash these away in the dfu_dev structure for the dfu_init()
* function, where we actually open the device.
*/
if (strncmp(port_spec, "usb", 3) != 0) {
avrdude_message(MSG_INFO, "%s: Error: "
"Invalid port specification \"%s\" for USB device\n",
progname, port_spec);
return NULL;
}
if(':' == port_spec[3]) {
bus_name = strdup(port_spec + 3 + 1);
if (bus_name == NULL) {
avrdude_message(MSG_INFO, "%s: Out of memory in strdup\n", progname);
return NULL;
}
dev_name = strchr(bus_name, ':');
if(NULL != dev_name)
*dev_name++ = '\0';
}
/* Allocate the dfu_dev structure and save the bus_name and dev_name
* strings for use in dfu_initialize().
*/
dfu = calloc(1, sizeof(struct dfu_dev));
if (dfu == NULL)
{
avrdude_message(MSG_INFO, "%s: out of memory\n", progname);
free(bus_name);
return NULL;
}
dfu->bus_name = bus_name;
dfu->dev_name = dev_name;
dfu->timeout = DFU_TIMEOUT;
/* LibUSB initialization. */
usb_init();
usb_find_busses();
usb_find_devices();
return dfu;
}
int dfu_init(struct dfu_dev *dfu, unsigned short vid, unsigned short pid)
{
struct usb_device *found = NULL;
struct usb_device *dev;
struct usb_bus *bus;
/* At last, we reach out through the USB bus to the part. There are three
* ways to specify the part: by USB address, by USB vendor and product id,
* and by part name. To specify the part by USB address, the user specifies
* a port parameter in the form "usb:BUS:DEV" (see dfu_open()). To specify
* the part by vendor and product, the user must specify a usbvid and usbpid
* in the configuration file. Finally, if the user specifies the part only,
* we use the default vendor and product id.
*/
if (pid == 0 && dfu->dev_name == NULL) {
avrdude_message(MSG_INFO, "%s: Error: No DFU support for part; "
"specify PID in config or USB address (via -P) to override.\n",
progname);
return -1;
}
/* Scan through all the devices for the part. The matching rules are:
*
* 1. If the user specified a USB bus name, it must match.
* 2. If the user specified a USB device name, it must match.
* 3. If the user didn't specify a USB device name and specified a vendor
* id, the vendor id must match.
* 4. If the user didn't specify a USB device name and specified a product
* id, the product id must match.
*/
for (bus = usb_busses; !found && bus != NULL; bus = bus->next) {
for (dev = bus->devices; !found && dev != NULL; dev = dev->next) {
if (dfu->bus_name != NULL && strcmp(bus->dirname, dfu->bus_name))
continue;
if (dfu->dev_name != NULL) {
if (strcmp(dev->filename, dfu->dev_name))
continue;
} else if (vid != dev->descriptor.idVendor)
continue;
else if (pid != 0 && pid != dev->descriptor.idProduct)
continue;
found = dev;
}
}
if (found == NULL) {
/* We could try to be more informative here. For example, we could report
* why the match failed, and if we came across another DFU-capable part.
*/
avrdude_message(MSG_INFO, "%s: Error: No matching USB device found\n", progname);
return -1;
}
if(verbose)
avrdude_message(MSG_INFO, "%s: Found VID=0x%04x PID=0x%04x at %s:%s\n",
progname, found->descriptor.idVendor, found->descriptor.idProduct,
found->bus->dirname, found->filename);
dfu->dev_handle = usb_open(found);
if (dfu->dev_handle == NULL) {
avrdude_message(MSG_INFO, "%s: Error: USB device at %s:%s: %s\n",
progname, found->bus->dirname, found->filename, usb_strerror());
return -1;
}
/* Save device, configuration, interface and endpoint descriptors. */
memcpy(&dfu->dev_desc, &found->descriptor, sizeof(dfu->dev_desc));
memcpy(&dfu->conf_desc, found->config, sizeof(dfu->conf_desc));
dfu->conf_desc.interface = NULL;
memcpy(&dfu->intf_desc, found->config->interface->altsetting,
sizeof(dfu->intf_desc));
dfu->intf_desc.endpoint = &dfu->endp_desc;
if (found->config->interface->altsetting->endpoint != 0)
memcpy(&dfu->endp_desc, found->config->interface->altsetting->endpoint,
sizeof(dfu->endp_desc));
/* Get strings. */
dfu->manf_str = get_usb_string(dfu->dev_handle,
dfu->dev_desc.iManufacturer);
dfu->prod_str = get_usb_string(dfu->dev_handle,
dfu->dev_desc.iProduct);
dfu->serno_str = get_usb_string(dfu->dev_handle,
dfu->dev_desc.iSerialNumber);
return 0;
}
void dfu_close(struct dfu_dev *dfu)
{
if (dfu->dev_handle != NULL)
usb_close(dfu->dev_handle);
if (dfu->bus_name != NULL)
free(dfu->bus_name);
if (dfu->manf_str != NULL)
free(dfu->manf_str);
if (dfu->prod_str != NULL)
free(dfu->prod_str);
if (dfu->serno_str != NULL)
free(dfu->serno_str);
}
int dfu_getstatus(struct dfu_dev *dfu, struct dfu_status *status)
{
int result;
avrdude_message(MSG_TRACE, "%s: dfu_getstatus(): issuing control IN message\n",
progname);
result = usb_control_msg(dfu->dev_handle,
0x80 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_GETSTATUS, 0, 0,
(char*) status, sizeof(struct dfu_status), dfu->timeout);
if (result < 0) {
avrdude_message(MSG_INFO, "%s: Error: Failed to get DFU status: %s\n",
progname, usb_strerror());
return -1;
}
if (result < sizeof(struct dfu_status)) {
avrdude_message(MSG_INFO, "%s: Error: Failed to get DFU status: %s\n",
progname, "short read");
return -1;
}
if (result > sizeof(struct dfu_status)) {
avrdude_message(MSG_INFO, "%s: Error: Oversize read (should not happen); "
"exiting\n", progname);
exit(1);
}
avrdude_message(MSG_TRACE, "%s: dfu_getstatus(): bStatus 0x%02x, bwPollTimeout %d, bState 0x%02x, iString %d\n",
progname,
status->bStatus,
status->bwPollTimeout[0] | (status->bwPollTimeout[1] << 8) | (status->bwPollTimeout[2] << 16),
status->bState,
status->iString);
return 0;
}
int dfu_clrstatus(struct dfu_dev *dfu)
{
int result;
avrdude_message(MSG_TRACE, "%s: dfu_clrstatus(): issuing control OUT message\n",
progname);
result = usb_control_msg(dfu->dev_handle,
USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_CLRSTATUS, 0, 0,
NULL, 0, dfu->timeout);
if (result < 0) {
avrdude_message(MSG_INFO, "%s: Error: Failed to clear DFU status: %s\n",
progname, usb_strerror());
return -1;
}
return 0;
}
int dfu_abort(struct dfu_dev *dfu)
{
int result;
avrdude_message(MSG_TRACE, "%s: dfu_abort(): issuing control OUT message\n",
progname);
result = usb_control_msg(dfu->dev_handle,
USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_ABORT, 0, 0,
NULL, 0, dfu->timeout);
if (result < 0) {
avrdude_message(MSG_INFO, "%s: Error: Failed to reset DFU state: %s\n",
progname, usb_strerror());
return -1;
}
return 0;
}
int dfu_dnload(struct dfu_dev *dfu, void *ptr, int size)
{
int result;
avrdude_message(MSG_TRACE, "%s: dfu_dnload(): issuing control OUT message, wIndex = %d, ptr = %p, size = %d\n",
progname, wIndex, ptr, size);
result = usb_control_msg(dfu->dev_handle,
USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_DNLOAD, wIndex++, 0,
ptr, size, dfu->timeout);
if (result < 0) {
avrdude_message(MSG_INFO, "%s: Error: DFU_DNLOAD failed: %s\n",
progname, usb_strerror());
return -1;
}
if (result < size) {
avrdude_message(MSG_INFO, "%s: Error: DFU_DNLOAD failed: %s\n",
progname, "short write");
return -1;
}
if (result > size) {
avrdude_message(MSG_INFO, "%s: Error: Oversize write (should not happen); " \
"exiting\n", progname);
exit(1);
}
return 0;
}
int dfu_upload(struct dfu_dev *dfu, void *ptr, int size)
{
int result;
avrdude_message(MSG_TRACE, "%s: dfu_upload(): issuing control IN message, wIndex = %d, ptr = %p, size = %d\n",
progname, wIndex, ptr, size);
result = usb_control_msg(dfu->dev_handle,
0x80 | USB_TYPE_CLASS | USB_RECIP_INTERFACE, DFU_UPLOAD, wIndex++, 0,
ptr, size, dfu->timeout);
if (result < 0) {
avrdude_message(MSG_INFO, "%s: Error: DFU_UPLOAD failed: %s\n",
progname, usb_strerror());
return -1;
}
if (result < size) {
avrdude_message(MSG_INFO, "%s: Error: DFU_UPLOAD failed: %s\n",
progname, "short read");
return -1;
}
if (result > size) {
avrdude_message(MSG_INFO, "%s: Error: Oversize read (should not happen); "
"exiting\n", progname);
exit(1);
}
return 0;
}
void dfu_show_info(struct dfu_dev *dfu)
{
if (dfu->manf_str != NULL)
avrdude_message(MSG_INFO, " USB Vendor : %s (0x%04hX)\n",
dfu->manf_str, (unsigned short) dfu->dev_desc.idVendor);
else
avrdude_message(MSG_INFO, " USB Vendor : 0x%04hX\n",
(unsigned short) dfu->dev_desc.idVendor);
if (dfu->prod_str != NULL)
avrdude_message(MSG_INFO, " USB Product : %s (0x%04hX)\n",
dfu->prod_str, (unsigned short) dfu->dev_desc.idProduct);
else
avrdude_message(MSG_INFO, " USB Product : 0x%04hX\n",
(unsigned short) dfu->dev_desc.idProduct);
avrdude_message(MSG_INFO, " USB Release : %hu.%hu.%hu\n",
((unsigned short) dfu->dev_desc.bcdDevice >> 8) & 0xFF,
((unsigned short) dfu->dev_desc.bcdDevice >> 4) & 0xF,
((unsigned short) dfu->dev_desc.bcdDevice >> 0) & 0xF);
if (dfu->serno_str != NULL)
avrdude_message(MSG_INFO, " USB Serial No : %s\n", dfu->serno_str);
}
/* INTERNAL FUNCTION DEFINITIONS
*/
char * get_usb_string(usb_dev_handle * dev_handle, int index) {
char buffer[256];
char * str;
int result;
if (index == 0)
return NULL;
result = usb_get_string_simple(dev_handle, index, buffer, sizeof(buffer)-1);
if (result < 0) {
avrdude_message(MSG_INFO, "%s: Warning: Failed to read USB device string %d: %s\n",
progname, index, usb_strerror());
return NULL;
}
str = malloc(result+1);
if (str == NULL) {
avrdude_message(MSG_INFO, "%s: Out of memory allocating a string\n", progname);
return 0;
}
memcpy(str, buffer, result);
str[result] = '\0';
return str;
}
#endif /* defined(HAVE_LIBUSB) */
/* EXPORTED FUNCTIONS THAT DO NO REQUIRE LIBUSB
*/
const char * dfu_status_str(int bStatus)
{
switch (bStatus) {
case DFU_STATUS_OK: return "OK";
case DFU_STATUS_ERR_TARGET: return "ERR_TARGET";
case DFU_STATUS_ERR_FILE: return "ERR_FILE";
case DFU_STATUS_ERR_WRITE: return "ERR_WRITE";
case DFU_STATUS_ERR_ERASE: return "ERR_ERASE";
case DFU_STATUS_ERR_CHECK_ERASED: return "ERR_CHECK_ERASED";
case DFU_STATUS_ERR_PROG: return "ERR_PROG";
case DFU_STATUS_ERR_VERIFY: return "ERR_VERIFY";
case DFU_STATUS_ERR_ADDRESS: return "ERR_ADDRESS";
case DFU_STATUS_ERR_NOTDONE: return "ERR_NOTDONE";
case DFU_STATUS_ERR_FIRMWARE: return "ERR_FIRMWARE";
case DFU_STATUS_ERR_VENDOR: return "ERR_VENDOR";
case DFU_STATUS_ERR_USBR: return "ERR_USBR";
case DFU_STATUS_ERR_POR: return "ERR_POR";
case DFU_STATUS_ERR_UNKNOWN: return "ERR_UNKNOWN";
case DFU_STATUS_ERR_STALLEDPKT: return "ERR_STALLEDPKT";
default: return "Unknown";
}
}
const char * dfu_state_str(int bState)
{
switch (bState) {
case DFU_STATE_APP_IDLE: return "APP_IDLE";
case DFU_STATE_APP_DETACH: return "APP_DETACH";
case DFU_STATE_DFU_IDLE: return "DFU_IDLE";
case DFU_STATE_DFU_DLOAD_SYNC: return "DFU_DLOAD_SYNC";
case DFU_STATE_DFU_DNBUSY: return "DFU_DNBUSY";
case DFU_STATE_DFU_DNLOAD_IDLE: return "DFU_DNLOAD_IDLE";
case DFU_STATE_DFU_MANIFEST_SYNC: return "DFU_MANIFEST_SYNC";
case DFU_STATE_DFU_MANIFEST: return "DFU_MANIFEST";
case DFU_STATE_DFU_MANIFEST_WAIT_RESET: return "DFU_MANIFEST_WAIT_RESET";
case DFU_STATE_DFU_UPLOAD_IDLE: return "DFU_UPLOAD_IDLE";
case DFU_STATE_DFU_ERROR: return "DFU_ERROR";
default: return "Unknown";
}
}