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