1 /* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
2 * Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
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.
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.
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/>.
18 // ----- Includes -----
28 // ----- Variables -----
31 * Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
33 static char staging[ FLASH_SECTOR_SIZE ];
37 // ----- Functions -----
39 void sector_print( void* buf, size_t sector, size_t chunks )
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;
49 printHex( (size_t)start );
51 printHex( (size_t)end );
55 for ( size_t line = 0; pos < end - 24; line++ )
58 printHex_op( (size_t)pos, 4 );
62 for ( size_t chunk = 0; chunk < chunks; chunk++ )
64 // Print out the two bytes (second one first)
65 printHex_op( *(pos + 1), 2 );
66 printHex_op( *pos, 2 );
75 static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
77 // Calculate starting address from offset
78 *buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
80 // Calculate length of transfer
81 *len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
82 ? 0 : USB_DFU_TRANSFER_SIZE;
85 if ( *buf > (void*)&_app_rom_end )
86 return (DFU_STATUS_errADDRESS);
88 return (DFU_STATUS_OK);
91 static enum dfu_status setup_write( size_t off, size_t len, void **buf )
95 if ( len > sizeof(staging) )
96 return (DFU_STATUS_errADDRESS);
98 // We only allow the last write to be less than one sector size.
101 if ( last && len != 0 )
102 return (DFU_STATUS_errADDRESS);
103 if ( len != FLASH_SECTOR_SIZE )
106 memset( staging, 0xff, sizeof(staging) );
110 return (DFU_STATUS_OK);
113 static enum dfu_status finish_write( void *buf, size_t off, size_t len )
117 return (DFU_STATUS_OK);
119 target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
121 return (DFU_STATUS_errADDRESS);
122 memcpy( target, buf, len );
125 sector_print( target, 0, 16 );
127 // Depending on the error return a different status
128 switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
131 case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
132 case FTFL_FSTAT_ACCERR: // Flash Access Error
133 case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
134 return (DFU_STATUS_errADDRESS);
135 case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
136 return (DFU_STATUS_errADDRESS);
141 return (DFU_STATUS_OK);
146 static struct dfu_ctx dfu_ctx;
148 void init_usb_bootloader( int config )
150 dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
155 #if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
156 // XXX McHCK uses B16 instead of A19
158 // Enabling LED to indicate we are in the bootloader
159 GPIOA_PDDR |= (1<<19);
160 // Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
161 PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
162 GPIOA_PSOR |= (1<<19);
164 #elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
165 // Enabling LED to indicate we are in the bootloader
166 GPIOA_PDDR |= (1<<5);
167 // Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
168 PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
169 GPIOA_PSOR |= (1<<5);
171 #error "Incompatible chip for bootloader"
175 printNL( NL "Bootloader DFU-Mode" );
178 for ( uint8_t sector = 0; sector < 3; sector++ )
179 sector_print( &_app_rom, sector, 16 );
184 GPIOB_PDDR |= (1<<16);
185 PORTB_PCR16 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
186 GPIOB_PSOR |= (1<<16);
189 GPIOC_PDDR |= (1<<8);
190 PORTC_PCR8 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
191 GPIOC_PSOR |= (1<<8);
194 GPIOC_PDDR |= (1<<4);
195 PORTC_PCR4 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
196 GPIOC_PCOR |= (1<<4);
199 GPIOC_PDDR |= (1<<1);
200 PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
201 GPIOC_PCOR |= (1<<1);
202 GPIOC_PDDR |= (1<<2);
203 PORTC_PCR2 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
204 GPIOC_PCOR |= (1<<2);
205 GPIOC_PDDR |= (1<<3);
206 PORTC_PCR3 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
207 GPIOC_PCOR |= (1<<3);
209 flash_prepare_flashing();
211 //uint32_t *position = &_app_rom;
212 usb_init( &dfu_device );