forked from leaflabs/maple-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dfu.c
409 lines (350 loc) · 14.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
/* *****************************************************************************
* The MIT License
*
* Copyright (c) 2010 LeafLabs LLC.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* ****************************************************************************/
/**
* @file dfu.c
*
* @brief The principle dfu state machine as well as the data
* transfer callbacks accessed by the usb library
*
*
*/
#include "dfu.h"
#include "usb.h"
/* DFU globals */
u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */
u32 userAppEnd = RAM_END;
DFUStatus dfuAppStatus; /* includes state */
bool userFlash = FALSE;
bool dfuBusy = FALSE;
u8 recvBuffer[wTransferSize];
u32 userFirmwareLen = 0;
u16 thisBlockLen = 0;
u16 uploadBlockLen = 0;
PLOT code_copy_lock;
/* todo: force dfu globals to be singleton to avoid re-inits? */
void dfuInit(void) {
dfuAppStatus.bStatus = OK;
dfuAppStatus.bwPollTimeout0 = 0x00;
dfuAppStatus.bwPollTimeout1 = 0x00;
dfuAppStatus.bwPollTimeout2 = 0x00;
dfuAppStatus.bState = dfuIDLE;
dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */
userFirmwareLen = 0;
thisBlockLen = 0;;
userAppAddr = USER_CODE_RAM; /* default RAM user code location */
userAppEnd = RAM_END;
userFlash = FALSE;
code_copy_lock = WAIT;
dfuBusy = FALSE;
}
bool dfuUpdateByRequest(void) {
/* were using the global pInformation struct from usb_lib here,
see comment in maple_dfu.h around DFUEvent struct */
dfuBusy = TRUE;
u8 startState = dfuAppStatus.bState;
dfuAppStatus.bStatus = OK;
/* often leaner to nest if's then embed a switch/case */
if (startState == dfuIDLE) {
/* device running inside DFU mode */
dfuBusy = TRUE; // signals the main loop to defer to the dfu write-loop
if (pInformation->USBbRequest == DFU_DNLOAD) {
if (pInformation->USBwLengths.w > 0) {
userFirmwareLen = 0;
dfuAppStatus.bState = dfuDNLOAD_SYNC;
if (pInformation->Current_AlternateSetting == 1) {
userAppAddr = USER_CODE_FLASH;
userFlash = TRUE;
/* make sure the flash is setup properly, unlock it */
setupFLASH();
flashUnlock();
} else {
userAppAddr = USER_CODE_RAM;
userFlash = FALSE;
}
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errNOTDONE;
}
} else if (pInformation->USBbRequest == DFU_UPLOAD) {
dfuAppStatus.bState = dfuUPLOAD_IDLE;
/* record length of first block for calculating target
address from wValue in consecutive blocks */
uploadBlockLen = pInformation->USBwLengths.w;
thisBlockLen = uploadBlockLen; /* for this first block as well */
/* calculate where the data should be copied from */
userFirmwareLen = uploadBlockLen * pInformation->USBwValue;
if (pInformation->Current_AlternateSetting == 1) {
userAppAddr = USER_CODE_FLASH;
userAppEnd = FLASH_END;
} else {
userAppAddr = USER_CODE_RAM;
userAppEnd = RAM_END;
}
} else if (pInformation->USBbRequest == DFU_ABORT) {
dfuAppStatus.bState = dfuIDLE;
dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */
} else if (pInformation->USBbRequest == DFU_GETSTATUS) {
dfuAppStatus.bState = dfuIDLE;
} else if (pInformation->USBbRequest == DFU_GETSTATE) {
dfuAppStatus.bState = dfuIDLE;
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errSTALLEDPKT;
}
} else if (startState == dfuDNLOAD_SYNC) {
/* device received block, waiting for DFU_GETSTATUS request */
if (pInformation->USBbRequest == DFU_GETSTATUS) {
/* todo, add routine to wait for last block write to finish */
if (userFlash) {
if (code_copy_lock == WAIT) {
code_copy_lock = BEGINNING;
dfuAppStatus.bwPollTimeout0 = 0x20; /* 32 ms */
dfuAppStatus.bwPollTimeout1 = 0x00;
dfuAppStatus.bState = dfuDNBUSY;
} else if (code_copy_lock == BEGINNING) {
dfuAppStatus.bState = dfuDNLOAD_SYNC;
} else if (code_copy_lock == MIDDLE) {
dfuAppStatus.bState = dfuDNLOAD_SYNC;
} else if (code_copy_lock == END) {
dfuAppStatus.bwPollTimeout0 = 0x00;
code_copy_lock = WAIT;
dfuAppStatus.bState = dfuDNLOAD_IDLE;
}
} else {
dfuAppStatus.bState = dfuDNLOAD_IDLE;
dfuCopyBufferToExec();
}
} else if (pInformation->USBbRequest == DFU_GETSTATE) {
dfuAppStatus.bState = dfuDNLOAD_SYNC;
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errSTALLEDPKT;
}
} else if (startState == dfuDNBUSY) {
/* if were actually done writing, goto sync, else stay busy */
if (code_copy_lock == END) {
dfuAppStatus.bwPollTimeout0 = 0x00;
code_copy_lock = WAIT;
dfuAppStatus.bState = dfuDNLOAD_IDLE;
} else {
dfuAppStatus.bState = dfuDNBUSY;
}
} else if (startState == dfuDNLOAD_IDLE) {
/* device is expecting dfu_dnload requests */
if (pInformation->USBbRequest == DFU_DNLOAD) {
if (pInformation->USBwLengths.w > 0) {
dfuAppStatus.bState = dfuDNLOAD_SYNC;
} else {
/* todo, support "disagreement" if device expects more data than this */
dfuAppStatus.bState = dfuMANIFEST_SYNC;
/* relock the flash */
flashLock();
}
} else if (pInformation->USBbRequest == DFU_ABORT) {
dfuAppStatus.bState = dfuIDLE;
} else if (pInformation->USBbRequest == DFU_GETSTATUS) {
dfuAppStatus.bState = dfuIDLE;
} else if (pInformation->USBbRequest == DFU_GETSTATE) {
dfuAppStatus.bState = dfuIDLE;
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errSTALLEDPKT;
}
} else if (startState == dfuMANIFEST_SYNC) {
/* device has received last block, waiting DFU_GETSTATUS request */
if (pInformation->USBbRequest == DFU_GETSTATUS) {
dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET;
dfuAppStatus.bStatus = OK;
} else if (pInformation->USBbRequest == DFU_GETSTATE) {
dfuAppStatus.bState = dfuMANIFEST_SYNC;
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errSTALLEDPKT;
}
} else if (startState == dfuMANIFEST) {
/* device is in manifestation phase */
/* should never receive request while in manifest! */
dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET;
dfuAppStatus.bStatus = OK;
} else if (startState == dfuMANIFEST_WAIT_RESET) {
/* device has programmed new firmware but needs external
usb reset or power on reset to run the new code */
/* consider timing out and self-resetting */
dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET;
} else if (startState == dfuUPLOAD_IDLE) {
/* device expecting further dfu_upload requests */
if (pInformation->USBbRequest == DFU_UPLOAD) {
if (pInformation->USBwLengths.w > 0) {
/* check that this is not the last possible block */
userFirmwareLen = uploadBlockLen * pInformation->USBwValue;
if (userAppAddr + userFirmwareLen + uploadBlockLen <= userAppEnd) {
thisBlockLen = uploadBlockLen;
dfuAppStatus.bState = dfuUPLOAD_IDLE;
} else {
/* if above comparison was just equal, thisBlockLen becomes zero
next time when USBWValue has been increased by one */
thisBlockLen = userAppEnd - userAppAddr - userFirmwareLen;
/* check for overflow due to USBwValue out of range */
if (thisBlockLen >= pInformation->USBwLengths.w) {
thisBlockLen = 0;
}
dfuAppStatus.bState = dfuIDLE;
}
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errNOTDONE;
}
} else if (pInformation->USBbRequest == DFU_ABORT) {
dfuAppStatus.bState = dfuIDLE;
} else if (pInformation->USBbRequest == DFU_GETSTATUS) {
dfuAppStatus.bState = dfuUPLOAD_IDLE;
} else if (pInformation->USBbRequest == DFU_GETSTATE) {
dfuAppStatus.bState = dfuUPLOAD_IDLE;
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errSTALLEDPKT;
}
} else if (startState == dfuERROR) {
/* status is in error, awaiting DFU_CLRSTATUS request */
if (pInformation->USBbRequest == DFU_GETSTATUS) {
/* todo, add routine to wait for last block write to finish */
dfuAppStatus.bState = dfuERROR;
} else if (pInformation->USBbRequest == DFU_GETSTATE) {
dfuAppStatus.bState = dfuERROR;
} else if (pInformation->USBbRequest == DFU_CLRSTATUS) {
/* todo handle any cleanup we need here */
dfuAppStatus.bState = dfuIDLE;
dfuAppStatus.bStatus = OK;
} else {
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errSTALLEDPKT;
}
} else {
/* some kind of error... */
dfuAppStatus.bState = dfuERROR;
dfuAppStatus.bStatus = errSTALLEDPKT;
}
if (dfuAppStatus.bStatus == OK) {
return TRUE;
} else {
return FALSE;
}
}
void dfuUpdateByReset(void) {
u8 startState = dfuAppStatus.bState;
userFirmwareLen = 0;
if (startState == appDETACH) {
dfuAppStatus.bState = dfuIDLE;
dfuAppStatus.bStatus = OK;
nvicDisableInterrupts();
usbEnbISR();
} else if (startState == appIDLE || startState == dfuIDLE) {
/* do nothing...might be normal usb bus activity */
} else {
/* we reset from the dfu, reset everything and startover,
which is the correct operation if this is an erroneous
event or properly following a MANIFEST */
dfuAppStatus.bState = dfuIDLE;
dfuAppStatus.bStatus = OK;
systemHardReset();
}
}
void dfuUpdateByTimeout(void) {
}
u8 *dfuCopyState(u16 length) {
if (length == 0) {
pInformation->Ctrl_Info.Usb_wLength = 1;
return NULL;
} else {
return (&(dfuAppStatus.bState));
}
}
u8 *dfuCopyStatus(u16 length) {
if (length == 0) {
pInformation->Ctrl_Info.Usb_wLength = 6;
return NULL;
} else {
return(&dfuAppStatus);
}
}
u8 *dfuCopyDNLOAD(u16 length) {
if (length == 0) {
pInformation->Ctrl_Info.Usb_wLength = pInformation->USBwLengths.w - pInformation->Ctrl_Info.Usb_wOffset;
thisBlockLen = pInformation->USBwLengths.w;
return NULL;
} else {
return ((u8 *)recvBuffer + pInformation->Ctrl_Info.Usb_wOffset);
}
}
u8 *dfuCopyUPLOAD(u16 length) {
if (length == 0) {
pInformation->Ctrl_Info.Usb_wLength = thisBlockLen - pInformation->Ctrl_Info.Usb_wOffset;
return NULL;
} else {
return((u8*) userAppAddr + userFirmwareLen + pInformation->Ctrl_Info.Usb_wOffset);
}
}
void dfuCopyBufferToExec() {
int i;
u32 *userSpace;
if (!userFlash) {
userSpace = (u32 *)(USER_CODE_RAM + userFirmwareLen);
/* we dont need to handle when thisBlock len is not divisible by 4,
since the linker will align everything to 4B anyway */
for (i = 0; i < thisBlockLen; i = i + 4) {
*userSpace++ = *(u32 *)(recvBuffer + i);
}
} else {
userSpace = (u32 *)(USER_CODE_FLASH + userFirmwareLen);
flashErasePage((u32)(userSpace));
for (i = 0; i < thisBlockLen; i = i + 4) {
flashWriteWord(userSpace++, *(u32 *)(recvBuffer + i));
}
}
userFirmwareLen += thisBlockLen;
thisBlockLen = 0;
}
u8 dfuGetState(void) {
return dfuAppStatus.bState;
}
void dfuSetState(u8 newState) {
dfuAppStatus.bState = newState;
}
bool dfuUploadStarted() {
return dfuBusy;
}
void dfuFinishUpload() {
while (1) {
if (userFlash) {
if (code_copy_lock == BEGINNING) {
code_copy_lock = MIDDLE;
strobePin(LED_BANK, LED, 2, 0x1000);
dfuCopyBufferToExec();
strobePin(LED_BANK, LED, 2, 0x500);
code_copy_lock = END;
}
}
/* otherwise do nothing, dfu state machine resets itself */
}
}