Austin Schuh | 41baf20 | 2022-01-01 14:33:40 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * The MIT License (MIT) |
| 3 | * |
| 4 | * Copyright (c) 2021 XMOS LIMITED |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | * |
| 24 | * This file is part of the TinyUSB stack. |
| 25 | */ |
| 26 | |
| 27 | #include "tusb_option.h" |
| 28 | |
| 29 | #if (TUSB_OPT_DEVICE_ENABLED && CFG_TUD_DFU) |
| 30 | |
| 31 | #include "device/usbd.h" |
| 32 | #include "device/usbd_pvt.h" |
| 33 | |
| 34 | #include "dfu_device.h" |
| 35 | |
| 36 | //--------------------------------------------------------------------+ |
| 37 | // MACRO CONSTANT TYPEDEF |
| 38 | //--------------------------------------------------------------------+ |
| 39 | |
| 40 | //--------------------------------------------------------------------+ |
| 41 | // INTERNAL OBJECT & FUNCTION DECLARATION |
| 42 | //--------------------------------------------------------------------+ |
| 43 | typedef struct |
| 44 | { |
| 45 | uint8_t attrs; |
| 46 | uint8_t alt; |
| 47 | |
| 48 | dfu_state_t state; |
| 49 | dfu_status_t status; |
| 50 | |
| 51 | bool flashing_in_progress; |
| 52 | uint16_t block; |
| 53 | uint16_t length; |
| 54 | |
| 55 | CFG_TUSB_MEM_ALIGN uint8_t transfer_buf[CFG_TUD_DFU_XFER_BUFSIZE]; |
| 56 | } dfu_state_ctx_t; |
| 57 | |
| 58 | // Only a single dfu state is allowed |
| 59 | CFG_TUSB_MEM_SECTION static dfu_state_ctx_t _dfu_ctx; |
| 60 | |
| 61 | static void reset_state(void) |
| 62 | { |
| 63 | _dfu_ctx.state = DFU_IDLE; |
| 64 | _dfu_ctx.status = DFU_STATUS_OK; |
| 65 | _dfu_ctx.flashing_in_progress = false; |
| 66 | } |
| 67 | |
| 68 | static bool reply_getstatus(uint8_t rhport, tusb_control_request_t const * request, dfu_state_t state, dfu_status_t status, uint32_t timeout); |
| 69 | static bool process_download_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); |
| 70 | static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); |
| 71 | |
| 72 | //--------------------------------------------------------------------+ |
| 73 | // Debug |
| 74 | //--------------------------------------------------------------------+ |
| 75 | #if CFG_TUSB_DEBUG >= 2 |
| 76 | |
| 77 | static tu_lookup_entry_t const _dfu_request_lookup[] = |
| 78 | { |
| 79 | { .key = DFU_REQUEST_DETACH , .data = "DETACH" }, |
| 80 | { .key = DFU_REQUEST_DNLOAD , .data = "DNLOAD" }, |
| 81 | { .key = DFU_REQUEST_UPLOAD , .data = "UPLOAD" }, |
| 82 | { .key = DFU_REQUEST_GETSTATUS , .data = "GETSTATUS" }, |
| 83 | { .key = DFU_REQUEST_CLRSTATUS , .data = "CLRSTATUS" }, |
| 84 | { .key = DFU_REQUEST_GETSTATE , .data = "GETSTATE" }, |
| 85 | { .key = DFU_REQUEST_ABORT , .data = "ABORT" }, |
| 86 | }; |
| 87 | |
| 88 | static tu_lookup_table_t const _dfu_request_table = |
| 89 | { |
| 90 | .count = TU_ARRAY_SIZE(_dfu_request_lookup), |
| 91 | .items = _dfu_request_lookup |
| 92 | }; |
| 93 | |
| 94 | static tu_lookup_entry_t const _dfu_state_lookup[] = |
| 95 | { |
| 96 | { .key = APP_IDLE , .data = "APP_IDLE" }, |
| 97 | { .key = APP_DETACH , .data = "APP_DETACH" }, |
| 98 | { .key = DFU_IDLE , .data = "IDLE" }, |
| 99 | { .key = DFU_DNLOAD_SYNC , .data = "DNLOAD_SYNC" }, |
| 100 | { .key = DFU_DNBUSY , .data = "DNBUSY" }, |
| 101 | { .key = DFU_DNLOAD_IDLE , .data = "DNLOAD_IDLE" }, |
| 102 | { .key = DFU_MANIFEST_SYNC , .data = "MANIFEST_SYNC" }, |
| 103 | { .key = DFU_MANIFEST , .data = "MANIFEST" }, |
| 104 | { .key = DFU_MANIFEST_WAIT_RESET , .data = "MANIFEST_WAIT_RESET" }, |
| 105 | { .key = DFU_UPLOAD_IDLE , .data = "UPLOAD_IDLE" }, |
| 106 | { .key = DFU_ERROR , .data = "ERROR" }, |
| 107 | }; |
| 108 | |
| 109 | static tu_lookup_table_t const _dfu_state_table = |
| 110 | { |
| 111 | .count = TU_ARRAY_SIZE(_dfu_state_lookup), |
| 112 | .items = _dfu_state_lookup |
| 113 | }; |
| 114 | |
| 115 | static tu_lookup_entry_t const _dfu_status_lookup[] = |
| 116 | { |
| 117 | { .key = DFU_STATUS_OK , .data = "OK" }, |
| 118 | { .key = DFU_STATUS_ERR_TARGET , .data = "errTARGET" }, |
| 119 | { .key = DFU_STATUS_ERR_FILE , .data = "errFILE" }, |
| 120 | { .key = DFU_STATUS_ERR_WRITE , .data = "errWRITE" }, |
| 121 | { .key = DFU_STATUS_ERR_ERASE , .data = "errERASE" }, |
| 122 | { .key = DFU_STATUS_ERR_CHECK_ERASED , .data = "errCHECK_ERASED" }, |
| 123 | { .key = DFU_STATUS_ERR_PROG , .data = "errPROG" }, |
| 124 | { .key = DFU_STATUS_ERR_VERIFY , .data = "errVERIFY" }, |
| 125 | { .key = DFU_STATUS_ERR_ADDRESS , .data = "errADDRESS" }, |
| 126 | { .key = DFU_STATUS_ERR_NOTDONE , .data = "errNOTDONE" }, |
| 127 | { .key = DFU_STATUS_ERR_FIRMWARE , .data = "errFIRMWARE" }, |
| 128 | { .key = DFU_STATUS_ERR_VENDOR , .data = "errVENDOR" }, |
| 129 | { .key = DFU_STATUS_ERR_USBR , .data = "errUSBR" }, |
| 130 | { .key = DFU_STATUS_ERR_POR , .data = "errPOR" }, |
| 131 | { .key = DFU_STATUS_ERR_UNKNOWN , .data = "errUNKNOWN" }, |
| 132 | { .key = DFU_STATUS_ERR_STALLEDPKT , .data = "errSTALLEDPKT" }, |
| 133 | }; |
| 134 | |
| 135 | static tu_lookup_table_t const _dfu_status_table = |
| 136 | { |
| 137 | .count = TU_ARRAY_SIZE(_dfu_status_lookup), |
| 138 | .items = _dfu_status_lookup |
| 139 | }; |
| 140 | |
| 141 | #endif |
| 142 | |
| 143 | //--------------------------------------------------------------------+ |
| 144 | // USBD Driver API |
| 145 | //--------------------------------------------------------------------+ |
| 146 | void dfu_moded_reset(uint8_t rhport) |
| 147 | { |
| 148 | (void) rhport; |
| 149 | |
| 150 | _dfu_ctx.attrs = 0; |
| 151 | _dfu_ctx.alt = 0; |
| 152 | |
| 153 | reset_state(); |
| 154 | } |
| 155 | |
| 156 | void dfu_moded_init(void) |
| 157 | { |
| 158 | dfu_moded_reset(0); |
| 159 | } |
| 160 | |
| 161 | uint16_t dfu_moded_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len) |
| 162 | { |
| 163 | (void) rhport; |
| 164 | |
| 165 | //------------- Interface (with Alt) descriptor -------------// |
| 166 | uint8_t const itf_num = itf_desc->bInterfaceNumber; |
| 167 | uint8_t alt_count = 0; |
| 168 | |
| 169 | uint16_t drv_len = 0; |
| 170 | while(itf_desc->bInterfaceSubClass == TUD_DFU_APP_SUBCLASS && itf_desc->bInterfaceProtocol == DFU_PROTOCOL_DFU) |
| 171 | { |
| 172 | TU_ASSERT(max_len > drv_len, 0); |
| 173 | |
| 174 | // Alternate must have the same interface number |
| 175 | TU_ASSERT(itf_desc->bInterfaceNumber == itf_num, 0); |
| 176 | |
| 177 | // Alt should increase by one every time |
| 178 | TU_ASSERT(itf_desc->bAlternateSetting == alt_count, 0); |
| 179 | alt_count++; |
| 180 | |
| 181 | drv_len += tu_desc_len(itf_desc); |
| 182 | itf_desc = (tusb_desc_interface_t const *) tu_desc_next(itf_desc); |
| 183 | } |
| 184 | |
| 185 | //------------- DFU Functional descriptor -------------// |
| 186 | tusb_desc_dfu_functional_t const *func_desc = (tusb_desc_dfu_functional_t const *) itf_desc; |
| 187 | TU_ASSERT(tu_desc_type(func_desc) == TUSB_DESC_FUNCTIONAL, 0); |
| 188 | drv_len += sizeof(tusb_desc_dfu_functional_t); |
| 189 | |
| 190 | _dfu_ctx.attrs = func_desc->bAttributes; |
| 191 | |
| 192 | // CFG_TUD_DFU_XFER_BUFSIZE has to be set to the buffer size used in TUD_DFU_DESCRIPTOR |
| 193 | uint16_t const transfer_size = tu_le16toh( tu_unaligned_read16((uint8_t const*) func_desc + offsetof(tusb_desc_dfu_functional_t, wTransferSize)) ); |
| 194 | TU_ASSERT(transfer_size <= CFG_TUD_DFU_XFER_BUFSIZE, drv_len); |
| 195 | |
| 196 | return drv_len; |
| 197 | } |
| 198 | |
| 199 | // Invoked when a control transfer occurred on an interface of this class |
| 200 | // Driver response accordingly to the request and the transfer stage (setup/data/ack) |
| 201 | // return false to stall control endpoint (e.g unsupported request) |
| 202 | bool dfu_moded_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) |
| 203 | { |
| 204 | TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE); |
| 205 | |
| 206 | TU_LOG2(" DFU State : %s, Status: %s\r\n", tu_lookup_find(&_dfu_state_table, _dfu_ctx.state), tu_lookup_find(&_dfu_status_table, _dfu_ctx.status)); |
| 207 | |
| 208 | if ( request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD ) |
| 209 | { |
| 210 | // Standard request include GET/SET_INTERFACE |
| 211 | switch ( request->bRequest ) |
| 212 | { |
| 213 | case TUSB_REQ_SET_INTERFACE: |
| 214 | if ( stage == CONTROL_STAGE_SETUP ) |
| 215 | { |
| 216 | // Switch Alt interface and reset state machine |
| 217 | _dfu_ctx.alt = (uint8_t) request->wValue; |
| 218 | reset_state(); |
| 219 | return tud_control_status(rhport, request); |
| 220 | } |
| 221 | break; |
| 222 | |
| 223 | case TUSB_REQ_GET_INTERFACE: |
| 224 | if(stage == CONTROL_STAGE_SETUP) |
| 225 | { |
| 226 | return tud_control_xfer(rhport, request, &_dfu_ctx.alt, 1); |
| 227 | } |
| 228 | break; |
| 229 | |
| 230 | // unsupported request |
| 231 | default: return false; |
| 232 | } |
| 233 | } |
| 234 | else if ( request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS ) |
| 235 | { |
| 236 | TU_LOG2(" DFU Request: %s\r\n", tu_lookup_find(&_dfu_request_table, request->bRequest)); |
| 237 | |
| 238 | // Class request |
| 239 | switch ( request->bRequest ) |
| 240 | { |
| 241 | case DFU_REQUEST_DETACH: |
| 242 | if ( stage == CONTROL_STAGE_SETUP ) |
| 243 | { |
| 244 | tud_control_status(rhport, request); |
| 245 | } |
| 246 | else if ( stage == CONTROL_STAGE_ACK ) |
| 247 | { |
| 248 | if ( tud_dfu_detach_cb ) tud_dfu_detach_cb(); |
| 249 | } |
| 250 | break; |
| 251 | |
| 252 | case DFU_REQUEST_CLRSTATUS: |
| 253 | if ( stage == CONTROL_STAGE_SETUP ) |
| 254 | { |
| 255 | reset_state(); |
| 256 | tud_control_status(rhport, request); |
| 257 | } |
| 258 | break; |
| 259 | |
| 260 | case DFU_REQUEST_GETSTATE: |
| 261 | if ( stage == CONTROL_STAGE_SETUP ) |
| 262 | { |
| 263 | tud_control_xfer(rhport, request, &_dfu_ctx.state, 1); |
| 264 | } |
| 265 | break; |
| 266 | |
| 267 | case DFU_REQUEST_ABORT: |
| 268 | if ( stage == CONTROL_STAGE_SETUP ) |
| 269 | { |
| 270 | reset_state(); |
| 271 | tud_control_status(rhport, request); |
| 272 | } |
| 273 | else if ( stage == CONTROL_STAGE_ACK ) |
| 274 | { |
| 275 | if ( tud_dfu_abort_cb ) tud_dfu_abort_cb(_dfu_ctx.alt); |
| 276 | } |
| 277 | break; |
| 278 | |
| 279 | case DFU_REQUEST_UPLOAD: |
| 280 | if ( stage == CONTROL_STAGE_SETUP ) |
| 281 | { |
| 282 | TU_VERIFY(_dfu_ctx.attrs & DFU_ATTR_CAN_UPLOAD); |
| 283 | TU_VERIFY(tud_dfu_upload_cb); |
| 284 | TU_VERIFY(request->wLength <= CFG_TUD_DFU_XFER_BUFSIZE); |
| 285 | |
| 286 | uint16_t const xfer_len = tud_dfu_upload_cb(_dfu_ctx.alt, request->wValue, _dfu_ctx.transfer_buf, request->wLength); |
| 287 | |
| 288 | return tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, xfer_len); |
| 289 | } |
| 290 | break; |
| 291 | |
| 292 | case DFU_REQUEST_DNLOAD: |
| 293 | if ( stage == CONTROL_STAGE_SETUP ) |
| 294 | { |
| 295 | TU_VERIFY(_dfu_ctx.attrs & DFU_ATTR_CAN_DOWNLOAD); |
| 296 | TU_VERIFY(_dfu_ctx.state == DFU_IDLE || _dfu_ctx.state == DFU_DNLOAD_IDLE); |
| 297 | TU_VERIFY(request->wLength <= CFG_TUD_DFU_XFER_BUFSIZE); |
| 298 | |
| 299 | // set to true for both download and manifest |
| 300 | _dfu_ctx.flashing_in_progress = true; |
| 301 | |
| 302 | // save block and length for flashing |
| 303 | _dfu_ctx.block = request->wValue; |
| 304 | _dfu_ctx.length = request->wLength; |
| 305 | |
| 306 | if ( request->wLength ) |
| 307 | { |
| 308 | // Download with payload -> transition to DOWNLOAD SYNC |
| 309 | _dfu_ctx.state = DFU_DNLOAD_SYNC; |
| 310 | return tud_control_xfer(rhport, request, _dfu_ctx.transfer_buf, request->wLength); |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | // Download is complete -> transition to MANIFEST SYNC |
| 315 | _dfu_ctx.state = DFU_MANIFEST_SYNC; |
| 316 | return tud_control_status(rhport, request); |
| 317 | } |
| 318 | } |
| 319 | break; |
| 320 | |
| 321 | case DFU_REQUEST_GETSTATUS: |
| 322 | switch ( _dfu_ctx.state ) |
| 323 | { |
| 324 | case DFU_DNLOAD_SYNC: |
| 325 | return process_download_get_status(rhport, stage, request); |
| 326 | break; |
| 327 | |
| 328 | case DFU_MANIFEST_SYNC: |
| 329 | return process_manifest_get_status(rhport, stage, request); |
| 330 | break; |
| 331 | |
| 332 | default: |
| 333 | if ( stage == CONTROL_STAGE_SETUP ) return reply_getstatus(rhport, request, _dfu_ctx.state, _dfu_ctx.status, 0); |
| 334 | break; |
| 335 | } |
| 336 | break; |
| 337 | |
| 338 | default: return false; // stall unsupported request |
| 339 | } |
| 340 | }else |
| 341 | { |
| 342 | return false; // unsupported request |
| 343 | } |
| 344 | |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | void tud_dfu_finish_flashing(uint8_t status) |
| 349 | { |
| 350 | _dfu_ctx.flashing_in_progress = false; |
| 351 | |
| 352 | if ( status == DFU_STATUS_OK ) |
| 353 | { |
| 354 | if (_dfu_ctx.state == DFU_DNBUSY) |
| 355 | { |
| 356 | _dfu_ctx.state = DFU_DNLOAD_SYNC; |
| 357 | } |
| 358 | else if (_dfu_ctx.state == DFU_MANIFEST) |
| 359 | { |
| 360 | _dfu_ctx.state = (_dfu_ctx.attrs & DFU_ATTR_MANIFESTATION_TOLERANT) |
| 361 | ? DFU_MANIFEST_SYNC : DFU_MANIFEST_WAIT_RESET; |
| 362 | } |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | // failed while flashing, move to dfuError |
| 367 | _dfu_ctx.state = DFU_ERROR; |
| 368 | _dfu_ctx.status = (dfu_status_t)status; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | static bool process_download_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) |
| 373 | { |
| 374 | if ( stage == CONTROL_STAGE_SETUP ) |
| 375 | { |
| 376 | // only transition to next state on CONTROL_STAGE_ACK |
| 377 | dfu_state_t next_state; |
| 378 | uint32_t timeout; |
| 379 | |
| 380 | if ( _dfu_ctx.flashing_in_progress ) |
| 381 | { |
| 382 | next_state = DFU_DNBUSY; |
| 383 | timeout = tud_dfu_get_timeout_cb(_dfu_ctx.alt, (uint8_t) next_state); |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | next_state = DFU_DNLOAD_IDLE; |
| 388 | timeout = 0; |
| 389 | } |
| 390 | |
| 391 | return reply_getstatus(rhport, request, next_state, _dfu_ctx.status, timeout); |
| 392 | } |
| 393 | else if ( stage == CONTROL_STAGE_ACK ) |
| 394 | { |
| 395 | if ( _dfu_ctx.flashing_in_progress ) |
| 396 | { |
| 397 | _dfu_ctx.state = DFU_DNBUSY; |
| 398 | tud_dfu_download_cb(_dfu_ctx.alt, _dfu_ctx.block, _dfu_ctx.transfer_buf, _dfu_ctx.length); |
| 399 | }else |
| 400 | { |
| 401 | _dfu_ctx.state = DFU_DNLOAD_IDLE; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | static bool process_manifest_get_status(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) |
| 409 | { |
| 410 | if ( stage == CONTROL_STAGE_SETUP ) |
| 411 | { |
| 412 | // only transition to next state on CONTROL_STAGE_ACK |
| 413 | dfu_state_t next_state; |
| 414 | uint32_t timeout; |
| 415 | |
| 416 | if ( _dfu_ctx.flashing_in_progress ) |
| 417 | { |
| 418 | next_state = DFU_MANIFEST; |
| 419 | timeout = tud_dfu_get_timeout_cb(_dfu_ctx.alt, next_state); |
| 420 | } |
| 421 | else |
| 422 | { |
| 423 | next_state = DFU_IDLE; |
| 424 | timeout = 0; |
| 425 | } |
| 426 | |
| 427 | return reply_getstatus(rhport, request, next_state, _dfu_ctx.status, timeout); |
| 428 | } |
| 429 | else if ( stage == CONTROL_STAGE_ACK ) |
| 430 | { |
| 431 | if ( _dfu_ctx.flashing_in_progress ) |
| 432 | { |
| 433 | _dfu_ctx.state = DFU_MANIFEST; |
| 434 | tud_dfu_manifest_cb(_dfu_ctx.alt); |
| 435 | } |
| 436 | else |
| 437 | { |
| 438 | _dfu_ctx.state = DFU_IDLE; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | return true; |
| 443 | } |
| 444 | |
| 445 | static bool reply_getstatus(uint8_t rhport, tusb_control_request_t const * request, dfu_state_t state, dfu_status_t status, uint32_t timeout) |
| 446 | { |
| 447 | dfu_status_response_t resp; |
| 448 | resp.bStatus = (uint8_t) status; |
| 449 | resp.bwPollTimeout[0] = TU_U32_BYTE0(timeout); |
| 450 | resp.bwPollTimeout[1] = TU_U32_BYTE1(timeout); |
| 451 | resp.bwPollTimeout[2] = TU_U32_BYTE2(timeout); |
| 452 | resp.bState = (uint8_t) state; |
| 453 | resp.iString = 0; |
| 454 | |
| 455 | return tud_control_xfer(rhport, request, &resp, sizeof(dfu_status_response_t)); |
| 456 | } |
| 457 | |
| 458 | #endif |