| 1 |
/* |
| 2 |
* Low-level DFU communication routines, originally taken from |
| 3 |
* $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $ |
| 4 |
* (part of dfu-programmer). |
| 5 |
* |
| 6 |
* Copyright 2005-2006 Weston Schmidt <[email protected]> |
| 7 |
* Copyright 2011-2014 Tormod Volden <[email protected]> |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify |
| 10 |
* it under the terms of the GNU General Public License as published by |
| 11 |
* the Free Software Foundation; either version 2 of the License, or |
| 12 |
* (at your option) any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
* GNU General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License |
| 20 |
* along with this program; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
# include "config.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <libusb.h> |
| 31 |
|
| 32 |
#include "portable.h" |
| 33 |
#include "dfu.h" |
| 34 |
#include "quirks.h" |
| 35 |
|
| 36 |
static int dfu_timeout = 5000; /* 5 seconds - default */ |
| 37 |
|
| 38 |
/* |
| 39 |
* DFU_DETACH Request (DFU Spec 1.0, Section 5.1) |
| 40 |
* |
| 41 |
* device - the usb_dev_handle to communicate with |
| 42 |
* interface - the interface to communicate with |
| 43 |
* timeout - the timeout in ms the USB device should wait for a pending |
| 44 |
* USB reset before giving up and terminating the operation |
| 45 |
* |
| 46 |
* returns 0 or < 0 on error |
| 47 |
*/ |
| 48 |
int dfu_detach( libusb_device_handle *device, |
| 49 |
const unsigned short interface, |
| 50 |
const unsigned short timeout ) |
| 51 |
{ |
| 52 |
return libusb_control_transfer( device, |
| 53 |
/* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 54 |
/* bRequest */ DFU_DETACH, |
| 55 |
/* wValue */ timeout, |
| 56 |
/* wIndex */ interface, |
| 57 |
/* Data */ NULL, |
| 58 |
/* wLength */ 0, |
| 59 |
dfu_timeout ); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/* |
| 64 |
* DFU_DNLOAD Request (DFU Spec 1.0, Section 6.1.1) |
| 65 |
* |
| 66 |
* device - the usb_dev_handle to communicate with |
| 67 |
* interface - the interface to communicate with |
| 68 |
* length - the total number of bytes to transfer to the USB |
| 69 |
* device - must be less than wTransferSize |
| 70 |
* data - the data to transfer |
| 71 |
* |
| 72 |
* returns the number of bytes written or < 0 on error |
| 73 |
*/ |
| 74 |
int dfu_download( libusb_device_handle *device, |
| 75 |
const unsigned short interface, |
| 76 |
const unsigned short length, |
| 77 |
const unsigned short transaction, |
| 78 |
unsigned char* data ) |
| 79 |
{ |
| 80 |
int status; |
| 81 |
|
| 82 |
status = libusb_control_transfer( device, |
| 83 |
/* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 84 |
/* bRequest */ DFU_DNLOAD, |
| 85 |
/* wValue */ transaction, |
| 86 |
/* wIndex */ interface, |
| 87 |
/* Data */ data, |
| 88 |
/* wLength */ length, |
| 89 |
dfu_timeout ); |
| 90 |
return status; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/* |
| 95 |
* DFU_UPLOAD Request (DFU Spec 1.0, Section 6.2) |
| 96 |
* |
| 97 |
* device - the usb_dev_handle to communicate with |
| 98 |
* interface - the interface to communicate with |
| 99 |
* length - the maximum number of bytes to receive from the USB |
| 100 |
* device - must be less than wTransferSize |
| 101 |
* data - the buffer to put the received data in |
| 102 |
* |
| 103 |
* returns the number of bytes received or < 0 on error |
| 104 |
*/ |
| 105 |
int dfu_upload( libusb_device_handle *device, |
| 106 |
const unsigned short interface, |
| 107 |
const unsigned short length, |
| 108 |
const unsigned short transaction, |
| 109 |
unsigned char* data ) |
| 110 |
{ |
| 111 |
int status; |
| 112 |
|
| 113 |
status = libusb_control_transfer( device, |
| 114 |
/* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 115 |
/* bRequest */ DFU_UPLOAD, |
| 116 |
/* wValue */ transaction, |
| 117 |
/* wIndex */ interface, |
| 118 |
/* Data */ data, |
| 119 |
/* wLength */ length, |
| 120 |
dfu_timeout ); |
| 121 |
return status; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/* |
| 126 |
* DFU_GETSTATUS Request (DFU Spec 1.0, Section 6.1.2) |
| 127 |
* |
| 128 |
* device - the usb_dev_handle to communicate with |
| 129 |
* interface - the interface to communicate with |
| 130 |
* status - the data structure to be populated with the results |
| 131 |
* |
| 132 |
* return the number of bytes read in or < 0 on an error |
| 133 |
*/ |
| 134 |
int dfu_get_status( struct dfu_if *dif, struct dfu_status *status ) |
| 135 |
{ |
| 136 |
unsigned char buffer[6]; |
| 137 |
int result; |
| 138 |
|
| 139 |
/* Initialize the status data structure */ |
| 140 |
status->bStatus = DFU_STATUS_ERROR_UNKNOWN; |
| 141 |
status->bwPollTimeout = 0; |
| 142 |
status->bState = STATE_DFU_ERROR; |
| 143 |
status->iString = 0; |
| 144 |
|
| 145 |
result = libusb_control_transfer( dif->dev_handle, |
| 146 |
/* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 147 |
/* bRequest */ DFU_GETSTATUS, |
| 148 |
/* wValue */ 0, |
| 149 |
/* wIndex */ dif->interface, |
| 150 |
/* Data */ buffer, |
| 151 |
/* wLength */ 6, |
| 152 |
dfu_timeout ); |
| 153 |
|
| 154 |
if( 6 == result ) { |
| 155 |
status->bStatus = buffer[0]; |
| 156 |
if (dif->quirks & QUIRK_POLLTIMEOUT) |
| 157 |
status->bwPollTimeout = DEFAULT_POLLTIMEOUT; |
| 158 |
else |
| 159 |
status->bwPollTimeout = ((0xff & buffer[3]) << 16) | |
| 160 |
((0xff & buffer[2]) << 8) | |
| 161 |
(0xff & buffer[1]); |
| 162 |
status->bState = buffer[4]; |
| 163 |
status->iString = buffer[5]; |
| 164 |
} |
| 165 |
|
| 166 |
return result; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
/* |
| 171 |
* DFU_CLRSTATUS Request (DFU Spec 1.0, Section 6.1.3) |
| 172 |
* |
| 173 |
* device - the usb_dev_handle to communicate with |
| 174 |
* interface - the interface to communicate with |
| 175 |
* |
| 176 |
* return 0 or < 0 on an error |
| 177 |
*/ |
| 178 |
int dfu_clear_status( libusb_device_handle *device, |
| 179 |
const unsigned short interface ) |
| 180 |
{ |
| 181 |
return libusb_control_transfer( device, |
| 182 |
/* bmRequestType */ LIBUSB_ENDPOINT_OUT| LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 183 |
/* bRequest */ DFU_CLRSTATUS, |
| 184 |
/* wValue */ 0, |
| 185 |
/* wIndex */ interface, |
| 186 |
/* Data */ NULL, |
| 187 |
/* wLength */ 0, |
| 188 |
dfu_timeout ); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
/* |
| 193 |
* DFU_GETSTATE Request (DFU Spec 1.0, Section 6.1.5) |
| 194 |
* |
| 195 |
* device - the usb_dev_handle to communicate with |
| 196 |
* interface - the interface to communicate with |
| 197 |
* length - the maximum number of bytes to receive from the USB |
| 198 |
* device - must be less than wTransferSize |
| 199 |
* data - the buffer to put the received data in |
| 200 |
* |
| 201 |
* returns the state or < 0 on error |
| 202 |
*/ |
| 203 |
int dfu_get_state( libusb_device_handle *device, |
| 204 |
const unsigned short interface ) |
| 205 |
{ |
| 206 |
int result; |
| 207 |
unsigned char buffer[1]; |
| 208 |
|
| 209 |
result = libusb_control_transfer( device, |
| 210 |
/* bmRequestType */ LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 211 |
/* bRequest */ DFU_GETSTATE, |
| 212 |
/* wValue */ 0, |
| 213 |
/* wIndex */ interface, |
| 214 |
/* Data */ buffer, |
| 215 |
/* wLength */ 1, |
| 216 |
dfu_timeout ); |
| 217 |
|
| 218 |
/* Return the error if there is one. */ |
| 219 |
if (result < 1) |
| 220 |
return -1; |
| 221 |
|
| 222 |
/* Return the state. */ |
| 223 |
return buffer[0]; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
/* |
| 228 |
* DFU_ABORT Request (DFU Spec 1.0, Section 6.1.4) |
| 229 |
* |
| 230 |
* device - the usb_dev_handle to communicate with |
| 231 |
* interface - the interface to communicate with |
| 232 |
* |
| 233 |
* returns 0 or < 0 on an error |
| 234 |
*/ |
| 235 |
int dfu_abort( libusb_device_handle *device, |
| 236 |
const unsigned short interface ) |
| 237 |
{ |
| 238 |
return libusb_control_transfer( device, |
| 239 |
/* bmRequestType */ LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE, |
| 240 |
/* bRequest */ DFU_ABORT, |
| 241 |
/* wValue */ 0, |
| 242 |
/* wIndex */ interface, |
| 243 |
/* Data */ NULL, |
| 244 |
/* wLength */ 0, |
| 245 |
dfu_timeout ); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
const char* dfu_state_to_string( int state ) |
| 250 |
{ |
| 251 |
const char *message; |
| 252 |
|
| 253 |
switch (state) { |
| 254 |
case STATE_APP_IDLE: |
| 255 |
message = "appIDLE"; |
| 256 |
break; |
| 257 |
case STATE_APP_DETACH: |
| 258 |
message = "appDETACH"; |
| 259 |
break; |
| 260 |
case STATE_DFU_IDLE: |
| 261 |
message = "dfuIDLE"; |
| 262 |
break; |
| 263 |
case STATE_DFU_DOWNLOAD_SYNC: |
| 264 |
message = "dfuDNLOAD-SYNC"; |
| 265 |
break; |
| 266 |
case STATE_DFU_DOWNLOAD_BUSY: |
| 267 |
message = "dfuDNBUSY"; |
| 268 |
break; |
| 269 |
case STATE_DFU_DOWNLOAD_IDLE: |
| 270 |
message = "dfuDNLOAD-IDLE"; |
| 271 |
break; |
| 272 |
case STATE_DFU_MANIFEST_SYNC: |
| 273 |
message = "dfuMANIFEST-SYNC"; |
| 274 |
break; |
| 275 |
case STATE_DFU_MANIFEST: |
| 276 |
message = "dfuMANIFEST"; |
| 277 |
break; |
| 278 |
case STATE_DFU_MANIFEST_WAIT_RESET: |
| 279 |
message = "dfuMANIFEST-WAIT-RESET"; |
| 280 |
break; |
| 281 |
case STATE_DFU_UPLOAD_IDLE: |
| 282 |
message = "dfuUPLOAD-IDLE"; |
| 283 |
break; |
| 284 |
case STATE_DFU_ERROR: |
| 285 |
message = "dfuERROR"; |
| 286 |
break; |
| 287 |
default: |
| 288 |
message = NULL; |
| 289 |
break; |
| 290 |
} |
| 291 |
|
| 292 |
return message; |
| 293 |
} |
| 294 |
|
| 295 |
/* Chapter 6.1.2 */ |
| 296 |
static const char *dfu_status_names[] = { |
| 297 |
/* DFU_STATUS_OK */ |
| 298 |
"No error condition is present", |
| 299 |
/* DFU_STATUS_errTARGET */ |
| 300 |
"File is not targeted for use by this device", |
| 301 |
/* DFU_STATUS_errFILE */ |
| 302 |
"File is for this device but fails some vendor-specific test", |
| 303 |
/* DFU_STATUS_errWRITE */ |
| 304 |
"Device is unable to write memory", |
| 305 |
/* DFU_STATUS_errERASE */ |
| 306 |
"Memory erase function failed", |
| 307 |
/* DFU_STATUS_errCHECK_ERASED */ |
| 308 |
"Memory erase check failed", |
| 309 |
/* DFU_STATUS_errPROG */ |
| 310 |
"Program memory function failed", |
| 311 |
/* DFU_STATUS_errVERIFY */ |
| 312 |
"Programmed memory failed verification", |
| 313 |
/* DFU_STATUS_errADDRESS */ |
| 314 |
"Cannot program memory due to received address that is out of range", |
| 315 |
/* DFU_STATUS_errNOTDONE */ |
| 316 |
"Received DFU_DNLOAD with wLength = 0, but device does not think that it has all data yet", |
| 317 |
/* DFU_STATUS_errFIRMWARE */ |
| 318 |
"Device's firmware is corrupt. It cannot return to run-time (non-DFU) operations", |
| 319 |
/* DFU_STATUS_errVENDOR */ |
| 320 |
"iString indicates a vendor specific error", |
| 321 |
/* DFU_STATUS_errUSBR */ |
| 322 |
"Device detected unexpected USB reset signalling", |
| 323 |
/* DFU_STATUS_errPOR */ |
| 324 |
"Device detected unexpected power on reset", |
| 325 |
/* DFU_STATUS_errUNKNOWN */ |
| 326 |
"Something went wrong, but the device does not know what it was", |
| 327 |
/* DFU_STATUS_errSTALLEDPKT */ |
| 328 |
"Device stalled an unexpected request" |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
const char *dfu_status_to_string(int status) |
| 333 |
{ |
| 334 |
if (status > DFU_STATUS_errSTALLEDPKT) |
| 335 |
return "INVALID"; |
| 336 |
return dfu_status_names[status]; |
| 337 |
} |
| 338 |
|
| 339 |
int dfu_abort_to_idle(struct dfu_if *dif) |
| 340 |
{ |
| 341 |
int ret; |
| 342 |
struct dfu_status dst; |
| 343 |
|
| 344 |
ret = dfu_abort(dif->dev_handle, dif->interface); |
| 345 |
if (ret < 0) { |
| 346 |
errx(EX_IOERR, "Error sending dfu abort request"); |
| 347 |
exit(1); |
| 348 |
} |
| 349 |
ret = dfu_get_status(dif, &dst); |
| 350 |
if (ret < 0) { |
| 351 |
errx(EX_IOERR, "Error during abort get_status"); |
| 352 |
exit(1); |
| 353 |
} |
| 354 |
if (dst.bState != DFU_STATE_dfuIDLE) { |
| 355 |
errx(EX_IOERR, "Failed to enter idle state on abort"); |
| 356 |
exit(1); |
| 357 |
} |
| 358 |
milli_sleep(dst.bwPollTimeout); |
| 359 |
return ret; |
| 360 |
} |