]> git.donarmstrong.com Git - kiibohd-controller.git/blob - Bootloader/main.c
bca3fdf8717484fb2f3ed4f9647ce797f29b238e
[kiibohd-controller.git] / Bootloader / main.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 // ----- Includes -----
19
20 // Local Includes
21 #include "mchck.h"
22 #include "dfu.desc.h"
23
24 #include "debug.h"
25
26
27
28 // ----- Variables -----
29
30 /**
31  * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
32  */
33 static char staging[ USB_DFU_TRANSFER_SIZE ];
34
35
36
37 // ----- Functions -----
38
39 int sector_print( void* buf, size_t sector, size_t chunks )
40 {
41         uint8_t* start = (uint8_t*)buf + sector * USB_DFU_TRANSFER_SIZE;
42         uint8_t* end = (uint8_t*)buf + (sector + 1) * USB_DFU_TRANSFER_SIZE;
43         uint8_t* pos = start;
44
45         // Verify if sector erased
46         FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
47         FTFL.fccob.read_1s_section.addr = (uintptr_t)start;
48         FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
49         FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits
50         int retval = ftfl_submit_cmd();
51
52 #ifdef FLASH_DEBUG
53         print( NL );
54         print("Block ");
55         printHex( sector );
56         print(" ");
57         printHex( (size_t)start );
58         print(" -> ");
59         printHex( (size_t)end );
60         print(" Erased: ");
61         printHex( retval );
62         print( NL );
63 #endif
64
65         // Display sector
66         for ( size_t line = 0; pos < end - 24; line++ )
67         {
68                 // Each Line
69                 printHex_op( (size_t)pos, 4 );
70                 print(": ");
71
72                 // Each 2 byte chunk
73                 for ( size_t chunk = 0; chunk < chunks; chunk++ )
74                 {
75                         // Print out the two bytes (second one first)
76                         printHex_op( *(pos + 1), 2 );
77                         printHex_op( *pos, 2 );
78                         print(" ");
79                         pos += 2;
80                 }
81
82                 print( NL );
83         }
84
85         return retval;
86 }
87
88 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
89 {
90         // Calculate starting address from offset
91         *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
92
93         // Calculate length of transfer
94         /*
95         *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
96                 ? 0 : USB_DFU_TRANSFER_SIZE;
97         */
98
99         // Check for error
100         /*
101         if ( *buf > (void*)&_app_rom_end )
102                 return (DFU_STATUS_errADDRESS);
103         */
104
105         return (DFU_STATUS_OK);
106 }
107
108 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
109 {
110         static int last = 0;
111
112 #ifdef FLASH_DEBUG
113         // Debug
114         print("Setup Write: offset(");
115         printHex( off );
116         print(") len(");
117         printHex( len );
118         print(") last(");
119         printHex( last );
120         printNL(")");
121 #endif
122
123         if ( len > sizeof(staging) )
124                 return (DFU_STATUS_errADDRESS);
125
126         // We only allow the last write to be less than one sector size.
127         if ( off == 0 )
128                 last = 0;
129         if ( last && len != 0 )
130                 return (DFU_STATUS_errADDRESS);
131         if ( len != USB_DFU_TRANSFER_SIZE )
132         {
133                 last = 1;
134                 memset( staging, 0xff, sizeof(staging) );
135         }
136
137         *buf = staging;
138         return (DFU_STATUS_OK);
139 }
140
141 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
142 {
143         void *target;
144         if ( len == 0 )
145                 return (DFU_STATUS_OK);
146
147         target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE );
148         if ( !target )
149                 return (DFU_STATUS_errADDRESS);
150         memcpy( target, buf, len );
151
152         // Depending on the error return a different status
153         switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) )
154         {
155         /*
156         case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
157         case FTFL_FSTAT_ACCERR:   // Flash Access Error
158         case FTFL_FSTAT_FPVIOL:   // Flash Protection Violation Error
159                 return (DFU_STATUS_errADDRESS);
160         case FTFL_FSTAT_MGSTAT0:  // Memory Controller Command Completion Error
161                 return (DFU_STATUS_errADDRESS);
162         */
163
164         case 0:
165         default: // No error
166                 return (DFU_STATUS_OK);
167         }
168 }
169
170
171 static struct dfu_ctx dfu_ctx;
172
173 void init_usb_bootloader( int config )
174 {
175         dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
176 }
177
178 void main()
179 {
180 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
181         // XXX McHCK uses B16 instead of A19
182
183         // Enabling LED to indicate we are in the bootloader
184         GPIOA_PDDR |= (1<<19);
185         // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
186         PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
187         GPIOA_PSOR |= (1<<19);
188
189 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
190         // Enabling LED to indicate we are in the bootloader
191         GPIOA_PDDR |= (1<<5);
192         // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
193         PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
194         GPIOA_PSOR |= (1<<5);
195 #else
196 #error "Incompatible chip for bootloader"
197 #endif
198
199         uart_serial_setup();
200         printNL( NL "Bootloader DFU-Mode" );
201
202         // Bootloader Enter Reasons
203         print(" RCM_SRS0 - ");
204         printHex( RCM_SRS0 & 0x60 );
205         print( NL " RCM_SRS1 - ");
206         printHex( RCM_SRS1 & 0x02 );
207         print( NL " _app_rom - ");
208         printHex( (uint32_t)_app_rom );
209         print( NL " Soft Rst - " );
210         printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 );
211         print( NL );
212
213 #ifdef FLASH_DEBUG
214         for ( uint8_t sector = 0; sector < 3; sector++ )
215                 sector_print( &_app_rom, sector, 16 );
216         print( NL );
217 #endif
218
219         flash_prepare_flashing();
220         usb_init( &dfu_device );
221         for (;;)
222         {
223                 usb_poll();
224         }
225 }
226