forked from xerub/xpwn
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdfu.c
420 lines (367 loc) · 12.1 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
/*
* dfu-programmer
*
* $Id: dfu.c,v 1.3 2006/06/20 06:28:04 schmidtw Exp $
*
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <usb.h>
#include "dfu.h"
/* DFU commands */
#define DFU_DETACH 0
#define DFU_DNLOAD 1
#define DFU_UPLOAD 2
#define DFU_GETSTATUS 3
#define DFU_CLRSTATUS 4
#define DFU_GETSTATE 5
#define DFU_ABORT 6
#define INVALID_DFU_TIMEOUT -1
static int dfu_timeout = INVALID_DFU_TIMEOUT;
static unsigned short transaction = 0;
static int dfu_debug_level = 0;
void dfu_init( const int timeout )
{
if( timeout > 0 ) {
dfu_timeout = timeout;
} else {
if( 0 != dfu_debug_level )
fprintf( stderr, "dfu_init: Invalid timeout value.\n" );
}
}
static int dfu_verify_init( const char *function )
{
if( INVALID_DFU_TIMEOUT == dfu_timeout ) {
if( 0 != dfu_debug_level )
fprintf( stderr,
"%s: dfu system not property initialized.\n",
function );
return -1;
}
return 0;
}
void dfu_debug( const int level )
{
dfu_debug_level = level;
}
/*
* DFU_DETACH Request (DFU Spec 1.0, Section 5.1)
*
* device - the usb_dev_handle to communicate with
* interface - the interface to communicate with
* timeout - the timeout in ms the USB device should wait for a pending
* USB reset before giving up and terminating the operation
*
* returns 0 or < 0 on error
*/
int dfu_detach( struct usb_dev_handle *device,
const unsigned short interface,
const unsigned short timeout )
{
if( 0 != dfu_verify_init(__FUNCTION__) )
return -1;
return usb_control_msg( device,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
/* bRequest */ DFU_DETACH,
/* wValue */ timeout,
/* wIndex */ interface,
/* Data */ NULL,
/* wLength */ 0,
dfu_timeout );
}
/*
* DFU_DNLOAD Request (DFU Spec 1.0, Section 6.1.1)
*
* device - the usb_dev_handle to communicate with
* interface - the interface to communicate with
* length - the total number of bytes to transfer to the USB
* device - must be less than wTransferSize
* data - the data to transfer
*
* returns the number of bytes written or < 0 on error
*/
int dfu_download( struct usb_dev_handle *device,
const unsigned short interface,
const unsigned short length,
char* data )
{
int status;
if( 0 != dfu_verify_init(__FUNCTION__) )
return -1;
/* Sanity checks */
if( (0 != length) && (NULL == data) ) {
if( 0 != dfu_debug_level )
fprintf( stderr,
"%s: data was NULL, but length != 0\n",
__FUNCTION__ );
return -1;
}
if( (0 == length) && (NULL != data) ) {
if( 0 != dfu_debug_level )
fprintf( stderr,
"%s: data was not NULL, but length == 0\n",
__FUNCTION__ );
return -2;
}
status = usb_control_msg( device,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
/* bRequest */ DFU_DNLOAD,
/* wValue */ transaction++,
/* wIndex */ interface,
/* Data */ data,
/* wLength */ length,
dfu_timeout );
if( status < 0 ) {
fprintf( stderr, "%s error %d\n", __FUNCTION__, status );
}
return status;
}
/*
* DFU_UPLOAD Request (DFU Spec 1.0, Section 6.2)
*
* device - the usb_dev_handle to communicate with
* interface - the interface to communicate with
* length - the maximum number of bytes to receive from the USB
* device - must be less than wTransferSize
* data - the buffer to put the received data in
*
* returns the number of bytes received or < 0 on error
*/
int dfu_upload( struct usb_dev_handle *device,
const unsigned short interface,
const unsigned short length,
char* data )
{
int status;
if( 0 != dfu_verify_init(__FUNCTION__) )
return -1;
/* Sanity checks */
if( (0 == length) || (NULL == data) ) {
if( 0 != dfu_debug_level )
fprintf( stderr,
"%s: data was NULL, or length is 0\n",
__FUNCTION__ );
return -1;
}
status = usb_control_msg( device,
/* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
/* bRequest */ DFU_UPLOAD,
/* wValue */ transaction++,
/* wIndex */ interface,
/* Data */ data,
/* wLength */ length,
dfu_timeout );
if( status < 0 ) {
fprintf( stderr, "%s error %d\n", __FUNCTION__, status );
}
return status;
}
/*
* DFU_GETSTATUS Request (DFU Spec 1.0, Section 6.1.2)
*
* device - the usb_dev_handle to communicate with
* interface - the interface to communicate with
* status - the data structure to be populated with the results
*
* return the number of bytes read in or < 0 on an error
*/
int dfu_get_status( struct usb_dev_handle *device,
const unsigned short interface,
struct dfu_status *status )
{
char buffer[6];
int result;
if( 0 != dfu_verify_init(__FUNCTION__) )
return -1;
/* Initialize the status data structure */
status->bStatus = DFU_STATUS_ERROR_UNKNOWN;
status->bwPollTimeout = 0;
status->bState = STATE_DFU_ERROR;
status->iString = 0;
result = usb_control_msg( device,
/* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
/* bRequest */ DFU_GETSTATUS,
/* wValue */ 0,
/* wIndex */ interface,
/* Data */ buffer,
/* wLength */ 6,
dfu_timeout );
if( 6 == result ) {
status->bStatus = buffer[0];
status->bwPollTimeout = ((0xff & buffer[3]) << 16) |
((0xff & buffer[2]) << 8) |
(0xff & buffer[1]);
status->bState = buffer[4];
status->iString = buffer[5];
}
return result;
}
/*
* DFU_CLRSTATUS Request (DFU Spec 1.0, Section 6.1.3)
*
* device - the usb_dev_handle to communicate with
* interface - the interface to communicate with
*
* return 0 or < 0 on an error
*/
int dfu_clear_status( struct usb_dev_handle *device,
const unsigned short interface )
{
if( 0 != dfu_verify_init(__FUNCTION__) )
return -1;
return usb_control_msg( device,
/* bmRequestType */ USB_ENDPOINT_OUT| USB_TYPE_CLASS | USB_RECIP_INTERFACE,
/* bRequest */ DFU_CLRSTATUS,
/* wValue */ 0,
/* wIndex */ interface,
/* Data */ NULL,
/* wLength */ 0,
dfu_timeout );
}
/*
* DFU_GETSTATE Request (DFU Spec 1.0, Section 6.1.5)
*
* device - the usb_dev_handle to communicate with
* interface - the interface to communicate with
* length - the maximum number of bytes to receive from the USB
* device - must be less than wTransferSize
* data - the buffer to put the received data in
*
* returns the state or < 0 on error
*/
int dfu_get_state( struct usb_dev_handle *device,
const unsigned short interface )
{
int result;
char buffer[1];
if( 0 != dfu_verify_init(__FUNCTION__) )
return -1;
result = usb_control_msg( device,
/* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
/* bRequest */ DFU_GETSTATE,
/* wValue */ 0,
/* wIndex */ interface,
/* Data */ buffer,
/* wLength */ 1,
dfu_timeout );
/* Return the error if there is one. */
if( result < 1 ) {
return result;
}
/* Return the state. */
return buffer[0];
}
/*
* DFU_ABORT Request (DFU Spec 1.0, Section 6.1.4)
*
* device - the usb_dev_handle to communicate with
* interface - the interface to communicate with
*
* returns 0 or < 0 on an error
*/
int dfu_abort( struct usb_dev_handle *device,
const unsigned short interface )
{
if( 0 != dfu_verify_init(__FUNCTION__) )
return -1;
return usb_control_msg( device,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
/* bRequest */ DFU_ABORT,
/* wValue */ 0,
/* wIndex */ interface,
/* Data */ NULL,
/* wLength */ 0,
dfu_timeout );
}
char* dfu_state_to_string( int state )
{
char *message = NULL;
switch( state ) {
case STATE_APP_IDLE:
message = "appIDLE";
break;
case STATE_APP_DETACH:
message = "appDETACH";
break;
case STATE_DFU_IDLE:
message = "dfuIDLE";
break;
case STATE_DFU_DOWNLOAD_SYNC:
message = "dfuDNLOAD-SYNC";
break;
case STATE_DFU_DOWNLOAD_BUSY:
message = "dfuDNBUSY";
break;
case STATE_DFU_DOWNLOAD_IDLE:
message = "dfuDNLOAD-IDLE";
break;
case STATE_DFU_MANIFEST_SYNC:
message = "dfuMANIFEST-SYNC";
break;
case STATE_DFU_MANIFEST:
message = "dfuMANIFEST";
break;
case STATE_DFU_MANIFEST_WAIT_RESET:
message = "dfuMANIFEST-WAIT-RESET";
break;
case STATE_DFU_UPLOAD_IDLE:
message = "dfuUPLOAD-IDLE";
break;
case STATE_DFU_ERROR:
message = "dfuERROR";
break;
}
return message;
}
/* Chapter 6.1.2 */
static const char *dfu_status_names[] = {
[DFU_STATUS_OK] = "No error condition is present",
[DFU_STATUS_errTARGET] =
"File is not targeted for use by this device",
[DFU_STATUS_errFILE] =
"File is for this device but fails some vendor-specific test",
[DFU_STATUS_errWRITE] =
"Device is unable to write memory",
[DFU_STATUS_errERASE] =
"Memory erase function failed",
[DFU_STATUS_errCHECK_ERASED] =
"Memory erase check failed",
[DFU_STATUS_errPROG] =
"Program memory function failed",
[DFU_STATUS_errVERIFY] =
"Programmed emmory failed verification",
[DFU_STATUS_errADDRESS] =
"Cannot program memory due to received address that is out of range",
[DFU_STATUS_errNOTDONE] =
"Received DFU_DNLOAD with wLength = 0, but device does not think that it has all data yet",
[DFU_STATUS_errFIRMWARE] =
"Device's firmware is corrupt. It cannot return to run-time (non-DFU) operations",
[DFU_STATUS_errVENDOR] =
"iString indicates a vendor specific error",
[DFU_STATUS_errUSBR] =
"Device detected unexpected USB reset signalling",
[DFU_STATUS_errPOR] =
"Device detected unexpected power on reset",
[DFU_STATUS_errUNKNOWN] =
"Something went wrong, but the device does not know what it was",
[DFU_STATUS_errSTALLEDPKT] =
"Device stalled an unexpected request",
};
const char *dfu_status_to_string(int status)
{
if (status > DFU_STATUS_errSTALLEDPKT)
return "INVALID";
return dfu_status_names[status];
}