forked from kiibohd/controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfu.c
300 lines (265 loc) · 6.49 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
/* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
* Modifications by Jacob Alexander 2014-2018 <[email protected]>
*
* 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 3 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/>.
*/
// ----- Local Includes -----
#include "usb.h"
#include "dfu.h"
#include "debug.h"
#include "weak.h"
// ----- Functions -----
void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
{
ctx->status = err;
if ( ctx->status == DFU_STATUS_OK )
{
switch ( ctx->state )
{
case DFU_STATE_dfuDNBUSY:
ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
break;
default:
break;
}
} else
{
ctx->state = DFU_STATE_dfuERROR;
}
}
static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
{
struct dfu_ctx *ctx = cbdata;
if ( len > 0 )
{
ctx->state = DFU_STATE_dfuDNBUSY;
}
else
{
ctx->state = DFU_STATE_dfuMANIFEST;
}
ctx->status = ctx->finish_write( buf, ctx->off, len );
// If this is the first block (and was used for key validation), don't increment offset
switch ( ctx->verified )
{
case DFU_VALIDATION_PENDING:
ctx->verified = DFU_VALIDATION_OK;
break;
case DFU_VALIDATION_OK:
ctx->off += len;
break;
default:
break;
}
ctx->len = len;
if ( ctx->status != DFU_STATUS_async )
{
dfu_write_done( ctx->status, ctx );
}
usb_handle_control_status( ctx->state == DFU_STATE_dfuERROR );
// If we failed validation, reset
if ( ctx->verified == DFU_VALIDATION_FAILED )
{
SOFTWARE_RESET();
}
}
static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
{
SOFTWARE_RESET();
}
int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
{
struct dfu_ctx *ctx = data;
int fail = 1;
if ( req->bRequest == MS_VENDOR_CODE) {
/* Microsoft extensions */
switch (req->wIndex) {
case USB_CTRL_REQ_MSFT_COMPAT_ID:
usb_ep0_tx_cp(&msft_extended_compat_desc, msft_extended_compat_desc.dwLength, req->wLength, NULL, NULL);
}
goto out_no_status;
}
switch ( (enum dfu_ctrl_req_code)req->bRequest )
{
// On Detach, just reset MCU and (attempt to) boot to firmware
case USB_CTRL_REQ_DFU_DETACH:
ctx->state = DFU_STATE_dfuIDLE;
usb_handle_control_status_cb(dfu_reset_system);
goto out_no_status;
case USB_CTRL_REQ_DFU_DNLOAD:
{
void *buf;
switch ( ctx->state )
{
case DFU_STATE_dfuIDLE:
ctx->off = 0;
break;
case DFU_STATE_dfuDNLOAD_IDLE:
break;
default:
goto err;
}
/**
* XXX we are not allowed to STALL here, and we need to eat all transferred data.
* better not allow setup_write to break the protocol.
*/
ctx->status = ctx->setup_write( ctx->off, req->wLength, &buf );
if ( ctx->status != DFU_STATUS_OK )
{
ctx->state = DFU_STATE_dfuERROR;
goto err_have_status;
}
if ( req->wLength > 0 )
{
usb_ep0_rx( buf, req->wLength, dfu_dnload_complete, ctx );
}
else
{
dfu_dnload_complete( NULL, 0, ctx );
}
goto out_no_status;
}
case USB_CTRL_REQ_DFU_UPLOAD:
{
void *buf;
size_t len = 0;
switch ( ctx->state )
{
case DFU_STATE_dfuIDLE:
break;
case DFU_STATE_dfuUPLOAD_IDLE:
break;
default:
goto err;
}
// Compute the requested offset
ctx->off = USB_DFU_TRANSFER_SIZE * req->wValue;
// Find which sector to read
ctx->status = ctx->setup_read( ctx->off, &len, &buf );
#ifdef FLASH_DEBUG
print("UPLOAD req:");
printHex( req->wValue );
print(" off:");
printHex( ctx->off );
print(" len:");
printHex( len );
print(" reqlen: ");
printHex( req->wLength );
print(" addr:");
printHex( (uint32_t)buf );
#endif
if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
{
ctx->state = DFU_STATE_dfuERROR;
goto err_have_status;
}
// Send bytes to Host
// Successfully transferred data to USB
if ( usb_ep0_tx( buf, len, len, NULL, NULL ) != -1 )
{
ctx->state = len < req->wLength
? DFU_STATE_dfuIDLE
: DFU_STATE_dfuUPLOAD_IDLE;
fail = 0;
}
// Problem transferring via USB
else
{
ctx->state = DFU_STATE_dfuERROR;
}
#ifdef FLASH_DEBUG
print(" state:");
printHex( ctx->state );
print( NL );
#endif
goto out;
}
case USB_CTRL_REQ_DFU_GETSTATUS:
{
struct dfu_status_t st;
st.bState = ctx->state;
st.bStatus = ctx->status;
st.bwPollTimeout = 1000; /* XXX */
/**
* If we're in DFU_STATE_dfuMANIFEST, we just finished
* the download, and we're just about to send our last
* status report. Once the report has been sent, go
* and reset the system to put the new firmware into
* effect.
*/
usb_ep0_tx_cp( &st, sizeof(st), req->wLength, NULL, NULL );
switch ( ctx->state )
{
case DFU_STATE_dfuMANIFEST:
ctx->state = DFU_STATE_dfuMANIFEST_WAIT_RESET;
// Download finished, just waiting for reset now
Chip_download_complete();
break;
case DFU_STATE_dfuMANIFEST_WAIT_RESET:
ctx->state = DFU_STATE_dfuIDLE;
usb_handle_control_status_cb(dfu_reset_system);
goto out_no_status;
default:
break;
}
break;
}
case USB_CTRL_REQ_DFU_CLRSTATUS:
ctx->state = DFU_STATE_dfuIDLE;
ctx->status = DFU_STATUS_OK;
break;
case USB_CTRL_REQ_DFU_GETSTATE:
{
uint8_t st = ctx->state;
usb_ep0_tx_cp( &st, sizeof(st), req->wLength, NULL, NULL );
break;
}
case USB_CTRL_REQ_DFU_ABORT:
switch ( ctx->state )
{
case DFU_STATE_dfuIDLE:
case DFU_STATE_dfuDNLOAD_IDLE:
case DFU_STATE_dfuUPLOAD_IDLE:
ctx->state = DFU_STATE_dfuIDLE;
break;
default:
goto err;
}
break;
default:
return 0;
}
fail = 0;
goto out;
err:
ctx->status = DFU_STATUS_errSTALLEDPKT;
err_have_status:
ctx->state = DFU_STATE_dfuERROR;
out:
usb_handle_control_status( fail );
out_no_status:
return 1;
}
void dfu_init( dfu_setup_read_t setup_read, dfu_setup_write_t setup_write, dfu_finish_write_t finish_write, struct dfu_ctx *ctx )
{
ctx->state = DFU_STATE_dfuIDLE;
ctx->setup_read = setup_read;
ctx->setup_write = setup_write;
ctx->finish_write = finish_write;
usb_attach_function(&dfu_function, &ctx->header);
}
const struct usbd_function dfu_function = {
.control = dfu_handle_control,
.interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
};