]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/dfu.c
Adding initial dfu-upload code and debugging for Bootloader.
[kiibohd-controller.git] / Bootloader / dfu.c
1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2  * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
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
18 // ----- Local Includes -----
19
20 #include "usb.h"
21 #include "dfu.h"
22 #include "debug.h"
23
24
25
26 // ----- Functions -----
27
28 void dfu_write_done( enum dfu_status err, struct dfu_ctx *ctx )
29 {
30         ctx->status = err;
31         if (ctx->status == DFU_STATUS_OK) {
32                 switch (ctx->state) {
33                 case DFU_STATE_dfuDNBUSY:
34                         ctx->state = DFU_STATE_dfuDNLOAD_IDLE;
35                         break;
36                 default:
37                         break;
38                 }
39         } else {
40                 ctx->state = DFU_STATE_dfuERROR;
41         }
42 }
43
44 static void dfu_dnload_complete( void *buf, ssize_t len, void *cbdata )
45 {
46         struct dfu_ctx *ctx = cbdata;
47
48         if (len > 0)
49                 ctx->state = DFU_STATE_dfuDNBUSY;
50         else
51                 ctx->state = DFU_STATE_dfuMANIFEST;
52         ctx->status = ctx->finish_write(buf, ctx->off, len);
53         ctx->off += len;
54         ctx->len = len;
55
56         if (ctx->status != DFU_STATUS_async)
57                 dfu_write_done(ctx->status, ctx);
58
59         usb_handle_control_status(ctx->state == DFU_STATE_dfuERROR);
60 }
61
62 static void dfu_reset_system( void *buf, ssize_t len, void *cbdata )
63 {
64         SOFTWARE_RESET();
65 }
66
67 static int dfu_handle_control( struct usb_ctrl_req_t *req, void *data )
68 {
69         struct dfu_ctx *ctx = data;
70         int fail = 1;
71
72         switch ((enum dfu_ctrl_req_code)req->bRequest)
73         {
74         case USB_CTRL_REQ_DFU_DNLOAD: {
75                 void *buf;
76
77                 switch (ctx->state) {
78                 case DFU_STATE_dfuIDLE:
79                         ctx->off = 0;
80                         break;
81                 case DFU_STATE_dfuDNLOAD_IDLE:
82                         break;
83                 default:
84                         goto err;
85                 }
86
87                 /**
88                  * XXX we are not allowed to STALL here, and we need to eat all transferred data.
89                  * better not allow setup_write to break the protocol.
90                  */
91                 ctx->status = ctx->setup_write(ctx->off, req->wLength, &buf);
92                 if (ctx->status != DFU_STATUS_OK) {
93                         ctx->state = DFU_STATE_dfuERROR;
94                         goto err_have_status;
95                 }
96
97                 if (req->wLength > 0)
98                         usb_ep0_rx(buf, req->wLength, dfu_dnload_complete, ctx);
99                 else
100                         dfu_dnload_complete(NULL, 0, ctx);
101                 goto out_no_status;
102         }
103         case USB_CTRL_REQ_DFU_UPLOAD: {
104                 void *buf;
105                 size_t len = 0;
106
107                 switch ( ctx->state )
108                 {
109                 case DFU_STATE_dfuIDLE:
110                         break;
111                 case DFU_STATE_dfuUPLOAD_IDLE:
112                         break;
113                 default:
114                         goto err;
115                 }
116
117                 // Find which sector to read
118                 ctx->status = ctx->setup_read(ctx->off, &len, &buf);
119                 print("UPLOAD off:");
120                 printHex( ctx->off );
121                 print(" len:");
122                 printHex( len );
123                 print(" addr:");
124                 printHex( (uint32_t)buf );
125                 print( NL );
126
127                 if ( ctx->status != DFU_STATUS_OK || len > req->wLength )
128                 {
129                         ctx->state = DFU_STATE_dfuERROR;
130                         goto err_have_status;
131                 }
132
133                 // Send bytes to Host
134                 if ( len > 0 )
135                 {
136                         usb_ep0_rx( buf, len, NULL, NULL );
137                 }
138                 else
139                 {
140                         ctx->state = DFU_STATE_dfuIDLE;
141                 }
142
143                 goto out_no_status;
144         }
145         case USB_CTRL_REQ_DFU_GETSTATUS: {
146                 struct dfu_status_t st;
147
148                 st.bState = ctx->state;
149                 st.bStatus = ctx->status;
150                 st.bwPollTimeout = 1000; /* XXX */
151
152                 // XXX FAKE WRITE
153                 if ( ctx->state == DFU_STATE_dfuMANIFEST )
154                 {
155                         uint8_t data[] = { 0x10, 0x20, 0x30, 0x40 };
156                         flash_program_longword((uintptr_t)&_app_rom, data);
157                 }
158                 /*
159
160                         uint32_t *position = &_app_rom + 0x100;
161                 for ( ; position < &_app_rom + 0x200; position++ )
162                 //for ( ; position < &_app_rom + 0x800; position++ )
163                 {
164                         if ( *position != 0xFFFFFFFF )
165                         {
166                         while( 1 )
167                         {
168                                 GPIOA_PTOR |= (1<<5);
169                                 for (uint32_t d = 0; d < 7200000; d++ );
170                         }
171                         }
172                 }*/
173
174                 // Check to see if vector table was flashed correctly
175                 // Return a flash error if it was not
176                 if (_app_rom == 0xffffffff && ctx->state == DFU_STATE_dfuMANIFEST)
177                         st.bStatus = DFU_STATUS_errPROG;
178                 //}
179                 /*
180                 if (ctx->state == DFU_STATE_dfuMANIFEST)
181                 {
182                         uint8_t *addr = (uint8_t*)_app_rom;
183                         //while (*(addr++) != 0x80);
184                         //st.bStatus = DFU_STATUS_errPROG;
185                         st.bStatus = (uint8_t)((uint32_t)(&_app_rom) >> 16);
186                 }
187                 */
188
189                 /**
190                  * If we're in DFU_STATE_dfuMANIFEST, we just finished
191                  * the download, and we're just about to send our last
192                  * status report.  Once the report has been sent, go
193                  * and reset the system to put the new firmware into
194                  * effect.
195                  */
196                 usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
197                 if (ctx->state == DFU_STATE_dfuMANIFEST) {
198                         usb_handle_control_status_cb(dfu_reset_system);
199                         goto out_no_status;
200                 }
201                 break;
202         }
203         case USB_CTRL_REQ_DFU_CLRSTATUS:
204                 ctx->state = DFU_STATE_dfuIDLE;
205                 ctx->status = DFU_STATUS_OK;
206                 break;
207         case USB_CTRL_REQ_DFU_GETSTATE: {
208                 uint8_t st = ctx->state;
209                 usb_ep0_tx_cp(&st, sizeof(st), req->wLength, NULL, NULL);
210                 break;
211         }
212         case USB_CTRL_REQ_DFU_ABORT:
213                 switch (ctx->state) {
214                 case DFU_STATE_dfuIDLE:
215                 case DFU_STATE_dfuDNLOAD_IDLE:
216                 case DFU_STATE_dfuUPLOAD_IDLE:
217                         ctx->state = DFU_STATE_dfuIDLE;
218                         break;
219                 default:
220                         goto err;
221                 }
222                 break;
223         default:
224                 return (0);
225         }
226
227         fail = 0;
228         goto out;
229
230 err:
231         ctx->status = DFU_STATUS_errSTALLEDPKT;
232 err_have_status:
233         ctx->state = DFU_STATE_dfuERROR;
234 out:
235         usb_handle_control_status(fail);
236 out_no_status:
237         return (1);
238 }
239
240 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 )
241 {
242         ctx->state = DFU_STATE_dfuIDLE;
243         ctx->setup_read = setup_read;
244         ctx->setup_write = setup_write;
245         ctx->finish_write = finish_write;
246         usb_attach_function(&dfu_function, &ctx->header);
247 }
248
249 const struct usbd_function dfu_function = {
250         .control = dfu_handle_control,
251         .interface_count = USB_FUNCTION_DFU_IFACE_COUNT,
252 };
253