2014-08-15 17:53:43 +00:00
|
|
|
/* Copyright (c) 2011,2012 Simon Schubert <2@0x2c.org>.
|
2015-01-11 03:55:28 +00:00
|
|
|
* Modifications by Jacob Alexander 2014-2015 <haata@kiibohd.com>
|
2014-08-15 17:53:43 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-01-11 03:55:28 +00:00
|
|
|
// ----- Includes -----
|
2014-08-15 17:53:43 +00:00
|
|
|
|
2015-01-11 03:55:28 +00:00
|
|
|
// Local Includes
|
2014-08-15 17:53:43 +00:00
|
|
|
#include "mchck.h"
|
|
|
|
#include "dfu.desc.h"
|
|
|
|
|
2015-04-27 07:57:34 +00:00
|
|
|
#include "debug.h"
|
|
|
|
|
2014-08-15 17:53:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ----- Variables -----
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unfortunately we can't DMA directly to FlexRAM, so we'll have to stage here.
|
|
|
|
*/
|
2015-05-26 00:23:18 +00:00
|
|
|
static char staging[ USB_DFU_TRANSFER_SIZE ];
|
2014-08-15 17:53:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ----- Functions -----
|
|
|
|
|
2015-05-26 00:50:32 +00:00
|
|
|
int sector_print( void* buf, size_t sector, size_t chunks )
|
2015-04-27 07:57:34 +00:00
|
|
|
{
|
|
|
|
uint8_t* start = (uint8_t*)buf + sector * USB_DFU_TRANSFER_SIZE;
|
|
|
|
uint8_t* end = (uint8_t*)buf + (sector + 1) * USB_DFU_TRANSFER_SIZE;
|
|
|
|
uint8_t* pos = start;
|
|
|
|
|
2015-05-26 00:23:18 +00:00
|
|
|
// Verify if sector erased
|
|
|
|
FTFL.fccob.read_1s_section.fcmd = FTFL_FCMD_READ_1s_SECTION;
|
|
|
|
FTFL.fccob.read_1s_section.addr = (uintptr_t)start;
|
|
|
|
FTFL.fccob.read_1s_section.margin = FTFL_MARGIN_NORMAL;
|
|
|
|
FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits
|
|
|
|
int retval = ftfl_submit_cmd();
|
|
|
|
|
2015-05-30 07:02:22 +00:00
|
|
|
#ifdef FLASH_DEBUG
|
2015-04-27 07:57:34 +00:00
|
|
|
print( NL );
|
|
|
|
print("Block ");
|
|
|
|
printHex( sector );
|
|
|
|
print(" ");
|
|
|
|
printHex( (size_t)start );
|
|
|
|
print(" -> ");
|
|
|
|
printHex( (size_t)end );
|
2015-05-26 00:23:18 +00:00
|
|
|
print(" Erased: ");
|
|
|
|
printHex( retval );
|
2015-04-27 07:57:34 +00:00
|
|
|
print( NL );
|
2015-05-30 07:02:22 +00:00
|
|
|
#endif
|
2015-04-27 07:57:34 +00:00
|
|
|
|
|
|
|
// Display sector
|
|
|
|
for ( size_t line = 0; pos < end - 24; line++ )
|
|
|
|
{
|
|
|
|
// Each Line
|
|
|
|
printHex_op( (size_t)pos, 4 );
|
2015-05-26 00:23:18 +00:00
|
|
|
print(": ");
|
2015-04-27 07:57:34 +00:00
|
|
|
|
|
|
|
// Each 2 byte chunk
|
|
|
|
for ( size_t chunk = 0; chunk < chunks; chunk++ )
|
|
|
|
{
|
|
|
|
// Print out the two bytes (second one first)
|
|
|
|
printHex_op( *(pos + 1), 2 );
|
|
|
|
printHex_op( *pos, 2 );
|
|
|
|
print(" ");
|
|
|
|
pos += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
print( NL );
|
|
|
|
}
|
2015-05-26 00:50:32 +00:00
|
|
|
|
|
|
|
return retval;
|
2015-04-27 07:57:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum dfu_status setup_read( size_t off, size_t *len, void **buf )
|
|
|
|
{
|
|
|
|
// Calculate starting address from offset
|
|
|
|
*buf = (void*)&_app_rom + (USB_DFU_TRANSFER_SIZE / 4) * off;
|
|
|
|
|
|
|
|
// Calculate length of transfer
|
2015-05-26 00:23:18 +00:00
|
|
|
/*
|
2015-04-27 07:57:34 +00:00
|
|
|
*len = *buf > (void*)(&_app_rom_end) - USB_DFU_TRANSFER_SIZE
|
|
|
|
? 0 : USB_DFU_TRANSFER_SIZE;
|
2015-05-26 00:23:18 +00:00
|
|
|
*/
|
2015-04-27 07:57:34 +00:00
|
|
|
|
|
|
|
// Check for error
|
2015-05-26 00:23:18 +00:00
|
|
|
/*
|
2015-04-27 07:57:34 +00:00
|
|
|
if ( *buf > (void*)&_app_rom_end )
|
|
|
|
return (DFU_STATUS_errADDRESS);
|
2015-05-26 00:23:18 +00:00
|
|
|
*/
|
2015-04-27 07:57:34 +00:00
|
|
|
|
|
|
|
return (DFU_STATUS_OK);
|
|
|
|
}
|
|
|
|
|
2015-04-05 06:21:11 +00:00
|
|
|
static enum dfu_status setup_write( size_t off, size_t len, void **buf )
|
2014-08-15 17:53:43 +00:00
|
|
|
{
|
2015-03-09 01:40:01 +00:00
|
|
|
static int last = 0;
|
|
|
|
|
2015-05-30 07:02:22 +00:00
|
|
|
#ifdef FLASH_DEBUG
|
|
|
|
// Debug
|
|
|
|
print("Setup Write: offset(");
|
|
|
|
printHex( off );
|
|
|
|
print(") len(");
|
|
|
|
printHex( len );
|
|
|
|
print(") last(");
|
|
|
|
printHex( last );
|
|
|
|
printNL(")");
|
|
|
|
#endif
|
|
|
|
|
2015-04-05 06:21:11 +00:00
|
|
|
if ( len > sizeof(staging) )
|
2015-03-09 01:40:01 +00:00
|
|
|
return (DFU_STATUS_errADDRESS);
|
|
|
|
|
|
|
|
// We only allow the last write to be less than one sector size.
|
2015-04-05 06:21:11 +00:00
|
|
|
if ( off == 0 )
|
2015-03-09 01:40:01 +00:00
|
|
|
last = 0;
|
2015-04-05 06:21:11 +00:00
|
|
|
if ( last && len != 0 )
|
2015-03-09 01:40:01 +00:00
|
|
|
return (DFU_STATUS_errADDRESS);
|
2015-05-30 07:02:22 +00:00
|
|
|
if ( len != USB_DFU_TRANSFER_SIZE )
|
2015-04-05 06:21:11 +00:00
|
|
|
{
|
2015-03-09 01:40:01 +00:00
|
|
|
last = 1;
|
2015-04-05 06:21:11 +00:00
|
|
|
memset( staging, 0xff, sizeof(staging) );
|
2015-03-09 01:40:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*buf = staging;
|
|
|
|
return (DFU_STATUS_OK);
|
2014-08-15 17:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static enum dfu_status finish_write( void *buf, size_t off, size_t len )
|
|
|
|
{
|
2015-03-09 01:40:01 +00:00
|
|
|
void *target;
|
2015-04-05 06:21:11 +00:00
|
|
|
if ( len == 0 )
|
2015-03-09 01:40:01 +00:00
|
|
|
return (DFU_STATUS_OK);
|
|
|
|
|
2015-05-30 07:02:22 +00:00
|
|
|
target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE );
|
2015-04-05 06:21:11 +00:00
|
|
|
if ( !target )
|
2015-03-09 01:40:01 +00:00
|
|
|
return (DFU_STATUS_errADDRESS);
|
2015-04-05 06:21:11 +00:00
|
|
|
memcpy( target, buf, len );
|
|
|
|
|
|
|
|
// Depending on the error return a different status
|
2015-05-30 07:02:22 +00:00
|
|
|
switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) )
|
2015-04-05 06:21:11 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
|
|
|
|
case FTFL_FSTAT_ACCERR: // Flash Access Error
|
|
|
|
case FTFL_FSTAT_FPVIOL: // Flash Protection Violation Error
|
2015-03-09 01:40:01 +00:00
|
|
|
return (DFU_STATUS_errADDRESS);
|
2015-04-05 06:21:11 +00:00
|
|
|
case FTFL_FSTAT_MGSTAT0: // Memory Controller Command Completion Error
|
|
|
|
return (DFU_STATUS_errADDRESS);
|
|
|
|
*/
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
default: // No error
|
|
|
|
return (DFU_STATUS_OK);
|
|
|
|
}
|
2014-08-15 17:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static struct dfu_ctx dfu_ctx;
|
|
|
|
|
|
|
|
void init_usb_bootloader( int config )
|
|
|
|
{
|
2015-04-27 07:57:34 +00:00
|
|
|
dfu_init( setup_read, setup_write, finish_write, &dfu_ctx );
|
2014-08-15 17:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
2015-01-11 03:55:28 +00:00
|
|
|
#if defined(_mk20dx128vlf5_) // Kiibohd-dfu / Infinity
|
|
|
|
// XXX McHCK uses B16 instead of A19
|
|
|
|
|
2014-08-15 17:53:43 +00:00
|
|
|
// Enabling LED to indicate we are in the bootloader
|
|
|
|
GPIOA_PDDR |= (1<<19);
|
|
|
|
// Setup pin - A19 - See Lib/pin_map.mchck for more details on pins
|
|
|
|
PORTA_PCR19 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
|
|
|
|
GPIOA_PSOR |= (1<<19);
|
|
|
|
|
2015-01-11 03:55:28 +00:00
|
|
|
#elif defined(_mk20dx256vlh7_) // Kiibohd-dfu
|
|
|
|
// Enabling LED to indicate we are in the bootloader
|
|
|
|
GPIOA_PDDR |= (1<<5);
|
|
|
|
// Setup pin - A5 - See Lib/pin_map.mchck for more details on pins
|
2015-04-05 06:21:11 +00:00
|
|
|
PORTA_PCR5 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
|
2015-01-11 03:55:28 +00:00
|
|
|
GPIOA_PSOR |= (1<<5);
|
2015-08-02 01:26:04 +00:00
|
|
|
|
|
|
|
// TODO Add CMake configuration for disabling
|
|
|
|
// Set LCD backlight on ICED to Red
|
|
|
|
GPIOC_PDDR |= (1<<1);
|
|
|
|
PORTC_PCR1 = PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1);
|
|
|
|
GPIOC_PCOR |= (1<<1);
|
2015-04-05 06:21:11 +00:00
|
|
|
#else
|
|
|
|
#error "Incompatible chip for bootloader"
|
2015-01-11 03:55:28 +00:00
|
|
|
#endif
|
|
|
|
|
2015-04-27 07:57:34 +00:00
|
|
|
uart_serial_setup();
|
|
|
|
printNL( NL "Bootloader DFU-Mode" );
|
|
|
|
|
2015-05-30 07:02:22 +00:00
|
|
|
// Bootloader Enter Reasons
|
|
|
|
print(" RCM_SRS0 - ");
|
|
|
|
printHex( RCM_SRS0 & 0x60 );
|
|
|
|
print( NL " RCM_SRS1 - ");
|
|
|
|
printHex( RCM_SRS1 & 0x02 );
|
|
|
|
print( NL " _app_rom - ");
|
|
|
|
printHex( (uint32_t)_app_rom );
|
|
|
|
print( NL " Soft Rst - " );
|
|
|
|
printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 );
|
|
|
|
print( NL );
|
|
|
|
|
|
|
|
#ifdef FLASH_DEBUG
|
|
|
|
for ( uint8_t sector = 0; sector < 3; sector++ )
|
2015-05-26 00:23:18 +00:00
|
|
|
sector_print( &_app_rom, sector, 16 );
|
|
|
|
print( NL );
|
2015-05-30 07:02:22 +00:00
|
|
|
#endif
|
2015-04-14 07:40:48 +00:00
|
|
|
|
2015-03-09 01:40:01 +00:00
|
|
|
flash_prepare_flashing();
|
|
|
|
usb_init( &dfu_device );
|
|
|
|
for (;;)
|
2014-08-15 17:53:43 +00:00
|
|
|
{
|
2015-03-09 01:40:01 +00:00
|
|
|
usb_poll();
|
|
|
|
}
|
2014-08-15 17:53:43 +00:00
|
|
|
}
|
|
|
|
|