Kiibohd Controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

dfu.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Copyright (c) 2011,2012 Simon Schubert <[email protected]>.
  2. * Modifications by Jacob Alexander 2014-2015 <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. // ----- Local Includes -----
  18. #include "usb.h"
  19. #include "dfu.h"
  20. #include "debug.h"
  21. // ----- Functions -----
  22. void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
  23. {
  24. ctx->status = err;
  25. if (ctx->status == DFU_STATUS_OK) {
  26. switch (ctx->state) {
  27. case DFU_STATE_dfuDNBUSY:
  28. ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
  29. break;
  30. default:
  31. break;
  32. }
  33. } else {
  34. ctx->state = DFU_STATE_dfuERROR;
  35. }
  36. }
  37. static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
  38. {
  39. struct dfu_ctx *ctx = cbdata;
  40. if (len > 0)
  41. ctx->state = DFU_STATE_dfuDNBUSY;
  42. else
  43. ctx->state = DFU_STATE_dfuMANIFEST;
  44. ctx->status = ctx->finish_write(buf, ctx->off, len);
  45. ctx->off += len;
  46. ctx->len = len;
  47. if (ctx->status != DFU_STATUS_async)
  48. dfu_write_done(ctx->status, ctx);
  49. usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
  50. }
  51. static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
  52. {
  53. SOFTWARE_RESET();
  54. }
  55. static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
  56. {
  57. struct dfu_ctx *ctx = data;
  58. int fail = 1;
  59. switch ((enum dfu_ctrl_req_code)req->bRequest)
  60. {
  61. case USB_CTRL_REQ_DFU_DNLOAD: {
  62. void *buf;
  63. switch (ctx->state) {
  64. case DFU_STATE_dfuIDLE:
  65. ctx->off = 0;
  66. break;
  67. case DFU_STATE_dfuDNLOAD_IDLE:
  68. break;
  69. default:
  70. goto err;
  71. }
  72. /**
  73. * XXX we are not allowed to STALL here, and we need to eat all transferred data.
  74. * better not allow setup_write to break the protocol.
  75. */
  76. ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
  77. if (ctx->status != DFU_STATUS_OK) {
  78. ctx->state = DFU_STATE_dfuERROR;
  79. goto err_have_status;
  80. }
  81. if (req->wLength > 0)
  82. usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
  83. else
  84. dfu_dnload_complete(NULL, 0, ctx);
  85. goto out_no_status;
  86. }
  87. case USB_CTRL_REQ_DFU_UPLOAD: {
  88. void *buf;
  89. size_t len = 0;
  90. switch ( ctx->state )
  91. {
  92. case DFU_STATE_dfuIDLE:
  93. break;
  94. case DFU_STATE_dfuUPLOAD_IDLE:
  95. break;
  96. default:
  97. goto err;
  98. }
  99. // Find which sector to read
  100. ctx->status = ctx->setup_read(ctx->off, &len, &buf);
  101. print("UPLOAD off:");
  102. printHex( ctx->off );
  103. print(" len:");
  104. printHex( len );
  105. print(" addr:");
  106. printHex( (uint32_t)buf );
  107. print( NL );
  108. if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
  109. {
  110. ctx->state = DFU_STATE_dfuERROR;
  111. goto err_have_status;
  112. }
  113. // Send bytes to Host
  114. if ( len > 0 )
  115. {
  116. usb_ep0_rx( buf, len, NULL, NULL );
  117. }
  118. else
  119. {
  120. ctx->state = DFU_STATE_dfuIDLE;
  121. }
  122. goto out_no_status;
  123. }
  124. case USB_CTRL_REQ_DFU_GETSTATUS: {
  125. struct dfu_status_t st;
  126. st.bState = ctx->state;
  127. st.bStatus = ctx->status;
  128. st.bwPollTimeout = 1000; /* XXX */
  129. // XXX FAKE WRITE
  130. if ( ctx->state == DFU_STATE_dfuMANIFEST )
  131. {
  132. uint8_t data[] = { 0x10, 0x20, 0x30, 0x40 };
  133. flash_program_longword((uintptr_t)&_app_rom, data);
  134. }
  135. /*
  136. uint32_t *position = &_app_rom + 0x100;
  137. for ( ; position < &_app_rom + 0x200; position++ )
  138. //for ( ; position < &_app_rom + 0x800; position++ )
  139. {
  140. if ( *position != 0xFFFFFFFF )
  141. {
  142. while( 1 )
  143. {
  144. GPIOA_PTOR |= (1<<5);
  145. for (uint32_t d = 0; d < 7200000; d++ );
  146. }
  147. }
  148. }*/
  149. // Check to see if vector table was flashed correctly
  150. // Return a flash error if it was not
  151. if (_app_rom == 0xffffffff && ctx->state == DFU_STATE_dfuMANIFEST)
  152. st.bStatus = DFU_STATUS_errPROG;
  153. //}
  154. /*
  155. if (ctx->state == DFU_STATE_dfuMANIFEST)
  156. {
  157. uint8_t *addr = (uint8_t*)_app_rom;
  158. //while (*(addr++) != 0x80);
  159. //st.bStatus = DFU_STATUS_errPROG;
  160. st.bStatus = (uint8_t)((uint32_t)(&_app_rom) >> 16);
  161. }
  162. */
  163. /**
  164. * If we're in DFU_STATE_dfuMANIFEST, we just finished
  165. * the download, and we're just about to send our last
  166. * status report. Once the report has been sent, go
  167. * and reset the system to put the new firmware into
  168. * effect.
  169. */
  170. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  171. if (ctx->state == DFU_STATE_dfuMANIFEST) {
  172. usb_handle_control_status_cb(dfu_reset_system);
  173. goto out_no_status;
  174. }
  175. break;
  176. }
  177. case USB_CTRL_REQ_DFU_CLRSTATUS:
  178. ctx->state = DFU_STATE_dfuIDLE;
  179. ctx->status = DFU_STATUS_OK;
  180. break;
  181. case USB_CTRL_REQ_DFU_GETSTATE: {
  182. uint8_t st = ctx->state;
  183. usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
  184. break;
  185. }
  186. case USB_CTRL_REQ_DFU_ABORT:
  187. switch (ctx->state) {
  188. case DFU_STATE_dfuIDLE:
  189. case DFU_STATE_dfuDNLOAD_IDLE:
  190. case DFU_STATE_dfuUPLOAD_IDLE:
  191. ctx->state = DFU_STATE_dfuIDLE;
  192. break;
  193. default:
  194. goto err;
  195. }
  196. break;
  197. default:
  198. return (0);
  199. }
  200. fail = 0;
  201. goto out;
  202. err:
  203. ctx->status = DFU_STATUS_errSTALLEDPKT;
  204. err_have_status:
  205. ctx->state = DFU_STATE_dfuERROR;
  206. out:
  207. usb_handle_control_status(fail);
  208. out_no_status:
  209. return (1);
  210. }
  211. 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 )
  212. {
  213. ctx->state = DFU_STATE_dfuIDLE;
  214. ctx->setup_read = setup_read;
  215. ctx->setup_write = setup_write;
  216. ctx->finish_write = finish_write;
  217. usb_attach_function(&dfu_function, &ctx->header);
  218. }
  219. const struct usbd_function dfu_function = {
  220. .control = dfu_handle_control,
  221. .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
  222. };