2019-03-05 04:02:02 +00:00
|
|
|
/*
|
|
|
|
* Fadecandy DFU Bootloader
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Micah Elizabeth Scott
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
* this software and associated documentation files (the "Software"), to deal in
|
|
|
|
* the Software without restriction, including without limitation the rights to
|
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
* subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <toboot-api.h>
|
|
|
|
#include <toboot-internal.h>
|
|
|
|
#include <dfu.h>
|
|
|
|
|
2019-04-05 07:09:52 +00:00
|
|
|
#define ERASE_SIZE 65536 // Erase block size (in bytes)
|
|
|
|
#define WRITE_SIZE 256 // Number of bytes we can write
|
2019-03-25 09:39:06 +00:00
|
|
|
|
|
|
|
#include <spi.h>
|
|
|
|
extern struct ff_spi *spi; // Defined in main.c
|
|
|
|
|
2019-03-05 04:02:02 +00:00
|
|
|
// Internal flash-programming state machine
|
|
|
|
static unsigned fl_current_addr = 0;
|
|
|
|
static enum {
|
|
|
|
flsIDLE = 0,
|
|
|
|
flsERASING,
|
|
|
|
flsPROGRAMMING
|
|
|
|
} fl_state;
|
|
|
|
|
|
|
|
static dfu_state_t dfu_state = dfuIDLE;
|
|
|
|
static dfu_status_t dfu_status = OK;
|
2019-04-03 14:11:31 +00:00
|
|
|
static unsigned dfu_poll_timeout_ms = 1;
|
2019-03-05 04:02:02 +00:00
|
|
|
|
|
|
|
static uint32_t dfu_buffer[DFU_TRANSFER_SIZE/4];
|
|
|
|
static uint32_t dfu_buffer_offset;
|
2019-03-25 09:39:06 +00:00
|
|
|
static uint32_t dfu_bytes_remaining;
|
2019-03-05 04:02:02 +00:00
|
|
|
|
|
|
|
// Memory offset we're uploading to.
|
|
|
|
static uint32_t dfu_target_address;
|
|
|
|
|
|
|
|
static void set_state(dfu_state_t new_state, dfu_status_t new_status) {
|
|
|
|
dfu_state = new_state;
|
|
|
|
dfu_status = new_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ftfl_busy()
|
|
|
|
{
|
|
|
|
// Is the flash memory controller busy?
|
2019-03-25 09:39:06 +00:00
|
|
|
return spiIsBusy(spi);
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ftfl_busy_wait()
|
|
|
|
{
|
|
|
|
// Wait for the flash memory controller to finish any pending operation.
|
|
|
|
while (ftfl_busy())
|
|
|
|
;//watchdog_refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ftfl_begin_erase_sector(uint32_t address)
|
|
|
|
{
|
2019-03-25 09:39:06 +00:00
|
|
|
ftfl_busy_wait();
|
2019-03-26 01:39:55 +00:00
|
|
|
// Only erase if it's on the page boundry.
|
2019-04-05 07:09:52 +00:00
|
|
|
if ((address & ~(ERASE_SIZE - 1) ) == address)
|
|
|
|
spiBeginErase64(spi, address);
|
2019-03-26 01:39:55 +00:00
|
|
|
fl_state = flsERASING;
|
2019-03-25 09:39:06 +00:00
|
|
|
}
|
2019-03-05 04:02:02 +00:00
|
|
|
|
2019-03-25 09:39:06 +00:00
|
|
|
static void ftfl_write_more_bytes(void)
|
|
|
|
{
|
2019-04-05 07:09:52 +00:00
|
|
|
uint32_t bytes_to_write = WRITE_SIZE;
|
2019-03-25 09:39:06 +00:00
|
|
|
if (dfu_bytes_remaining < bytes_to_write)
|
|
|
|
bytes_to_write = dfu_bytes_remaining;
|
2019-03-05 04:02:02 +00:00
|
|
|
ftfl_busy_wait();
|
2019-03-25 09:39:06 +00:00
|
|
|
spiBeginWrite(spi, dfu_target_address, &dfu_buffer[dfu_buffer_offset], bytes_to_write);
|
|
|
|
|
|
|
|
dfu_bytes_remaining -= bytes_to_write;
|
|
|
|
dfu_target_address += bytes_to_write;
|
|
|
|
dfu_buffer_offset += bytes_to_write;
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ftfl_begin_program_section(uint32_t address)
|
|
|
|
{
|
|
|
|
// Write the buffer word to the currently selected address.
|
|
|
|
// Note that after this is done, the address is incremented by 4.
|
|
|
|
dfu_buffer_offset = 0;
|
|
|
|
dfu_target_address = address;
|
2019-03-25 09:39:06 +00:00
|
|
|
ftfl_write_more_bytes();
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t address_for_block(unsigned blockNum)
|
|
|
|
{
|
2019-03-25 09:39:06 +00:00
|
|
|
static const uint32_t starting_offset = 262144;
|
2019-04-05 07:09:52 +00:00
|
|
|
return starting_offset + (blockNum * WRITE_SIZE);
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void dfu_init(void)
|
|
|
|
{
|
2019-04-05 07:09:52 +00:00
|
|
|
return;
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t dfu_getstate(void)
|
|
|
|
{
|
|
|
|
return dfu_state;
|
|
|
|
}
|
|
|
|
|
2019-03-28 03:13:25 +00:00
|
|
|
unsigned last_blockNum;
|
|
|
|
unsigned last_blockLength;
|
|
|
|
unsigned last_packetOffset;
|
|
|
|
unsigned last_packetLength;
|
2019-03-05 04:02:02 +00:00
|
|
|
bool dfu_download(unsigned blockNum, unsigned blockLength,
|
2019-03-26 01:39:55 +00:00
|
|
|
unsigned packetOffset, unsigned packetLength, const uint8_t *data)
|
2019-03-05 04:02:02 +00:00
|
|
|
{
|
2019-03-26 01:39:55 +00:00
|
|
|
// uint32_t i;
|
2019-03-28 03:13:25 +00:00
|
|
|
last_packetLength = packetLength;
|
|
|
|
last_packetOffset = packetOffset;
|
|
|
|
last_blockLength = blockLength;
|
|
|
|
last_blockNum = blockNum;
|
2019-03-05 04:02:02 +00:00
|
|
|
|
|
|
|
if (packetOffset + packetLength > DFU_TRANSFER_SIZE ||
|
|
|
|
packetOffset + packetLength > blockLength) {
|
|
|
|
|
|
|
|
// Overflow!
|
|
|
|
set_state(dfuERROR, errADDRESS);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store more data...
|
|
|
|
memcpy(((uint8_t *)dfu_buffer) + packetOffset, data, packetLength);
|
|
|
|
|
|
|
|
if (packetOffset + packetLength != blockLength) {
|
|
|
|
// Still waiting for more data.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dfu_state != dfuIDLE && dfu_state != dfuDNLOAD_IDLE) {
|
|
|
|
// Wrong state! Oops.
|
|
|
|
set_state(dfuERROR, errSTALLEDPKT);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
if (ftfl_busy() || (fl_state != flsIDLE)) {
|
2019-03-05 04:02:02 +00:00
|
|
|
// Flash controller shouldn't be busy now!
|
2019-04-05 07:09:52 +00:00
|
|
|
set_state(dfuERROR, errWRITE);
|
|
|
|
return false;
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!blockLength) {
|
|
|
|
// End of download
|
|
|
|
set_state(dfuMANIFEST_SYNC, OK);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start programming a block by erasing the corresponding flash sector
|
|
|
|
fl_state = flsERASING;
|
|
|
|
fl_current_addr = address_for_block(blockNum);
|
2019-03-25 09:39:06 +00:00
|
|
|
dfu_bytes_remaining = blockLength;
|
2019-03-05 04:02:02 +00:00
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
#if 0
|
2019-03-05 04:02:02 +00:00
|
|
|
// If it's the first block, figure out what we need to do in terms of erasing
|
|
|
|
// data and programming the new file.
|
|
|
|
if (blockNum == 0) {
|
|
|
|
const struct toboot_configuration *old_config = tb_get_config();
|
|
|
|
|
|
|
|
// Don't allow overwriting Toboot itself.
|
|
|
|
if (fl_current_addr < tb_first_free_address()) {
|
|
|
|
set_state(dfuERROR, errADDRESS);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate generation number and hash
|
|
|
|
if (tb_state.version == 2) {
|
|
|
|
struct toboot_configuration *new_config = (struct toboot_configuration *)&dfu_buffer[0x94 / 4];
|
|
|
|
|
|
|
|
// Update generation number
|
|
|
|
new_config->reserved_gen = old_config->reserved_gen + 1;
|
|
|
|
|
|
|
|
// Ensure we know this header is not fake
|
|
|
|
new_config->config &= ~TOBOOT_CONFIG_FAKE;
|
|
|
|
|
|
|
|
// Generate a valid signature
|
|
|
|
tb_sign_config(new_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the old configuration requires that certain blocks be erased, do that.
|
|
|
|
tb_state.clear_hi = old_config->erase_mask_hi;
|
|
|
|
tb_state.clear_lo = old_config->erase_mask_lo;
|
|
|
|
tb_state.clear_current = 0;
|
|
|
|
|
2019-03-25 09:39:06 +00:00
|
|
|
// Ensure we don't erase Foboot itself
|
2019-03-05 04:02:02 +00:00
|
|
|
for (i = 0; i < tb_first_free_sector(); i++) {
|
|
|
|
if (i < 32)
|
|
|
|
tb_state.clear_lo &= ~(1 << i);
|
|
|
|
else
|
|
|
|
tb_state.clear_hi &= ~(1 << i);
|
|
|
|
}
|
|
|
|
|
2019-03-25 09:39:06 +00:00
|
|
|
// If the newly-loaded program does not conform to Foboot V2.0, then look
|
2019-03-05 04:02:02 +00:00
|
|
|
// for any existing programs on the flash and delete those sectors.
|
|
|
|
// Because of boot priority, we need to ensure that no V2.0 applications
|
|
|
|
// exist on flash.
|
|
|
|
if (tb_state.version < 2) {
|
|
|
|
for (i = tb_first_free_sector(); i < 64; i++) {
|
|
|
|
if (tb_valid_signature_at_page(i) < 0)
|
|
|
|
continue;
|
|
|
|
if (i < 32)
|
|
|
|
tb_state.clear_lo |= (1 << i);
|
|
|
|
else
|
|
|
|
tb_state.clear_hi |= (1 << (i - 32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we still have sectors to clear, do that. Otherwise,
|
|
|
|
// go straight into loading the program.
|
|
|
|
if (tb_state.clear_lo || tb_state.clear_hi) {
|
|
|
|
tb_state.state = tbsCLEARING;
|
|
|
|
tb_state.next_addr = fl_current_addr;
|
|
|
|
pre_clear_next_block();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tb_state.state = tbsLOADING;
|
|
|
|
ftfl_begin_erase_sector(fl_current_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2019-03-26 01:39:55 +00:00
|
|
|
#endif
|
|
|
|
ftfl_begin_erase_sector(fl_current_addr);
|
2019-03-05 04:02:02 +00:00
|
|
|
|
|
|
|
set_state(dfuDNLOAD_SYNC, OK);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fl_state_poll(void)
|
|
|
|
{
|
|
|
|
// Try to advance the state of our own flash programming state machine.
|
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
if (spiIsBusy(spi))
|
|
|
|
return;
|
2019-03-05 04:02:02 +00:00
|
|
|
|
|
|
|
switch (fl_state) {
|
|
|
|
|
2019-03-26 01:39:55 +00:00
|
|
|
case flsIDLE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case flsERASING:
|
|
|
|
// // If we're still pre-clearing, continue with that.
|
|
|
|
// if (tb_state.state == tbsCLEARING) {
|
|
|
|
// pre_clear_next_block();
|
|
|
|
// }
|
|
|
|
// // Done! Move on to programming the sector.
|
|
|
|
// else {
|
|
|
|
fl_state = flsPROGRAMMING;
|
|
|
|
ftfl_begin_program_section(fl_current_addr);
|
|
|
|
// }
|
|
|
|
break;
|
|
|
|
|
|
|
|
case flsPROGRAMMING:
|
|
|
|
// Program more blocks, if applicable
|
|
|
|
if (dfu_bytes_remaining)
|
|
|
|
ftfl_write_more_bytes();
|
|
|
|
else
|
|
|
|
fl_state = flsIDLE;
|
|
|
|
break;
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-05 07:09:52 +00:00
|
|
|
void dfu_poll(void)
|
|
|
|
{
|
|
|
|
if ((dfu_state == dfuDNLOAD_SYNC) || (dfu_state == dfuDNBUSY))
|
|
|
|
fl_state_poll();
|
|
|
|
}
|
|
|
|
|
2019-03-05 04:02:02 +00:00
|
|
|
bool dfu_getstatus(uint8_t status[8])
|
|
|
|
{
|
|
|
|
switch (dfu_state) {
|
|
|
|
|
|
|
|
case dfuDNLOAD_SYNC:
|
|
|
|
case dfuDNBUSY:
|
|
|
|
// Programming operation in progress. Advance our private flash state machine.
|
2019-04-05 07:09:52 +00:00
|
|
|
// fl_state_poll();
|
2019-03-05 04:02:02 +00:00
|
|
|
|
|
|
|
if (dfu_state == dfuERROR) {
|
|
|
|
// An error occurred inside fl_state_poll();
|
|
|
|
} else if (fl_state == flsIDLE) {
|
2019-03-26 01:39:55 +00:00
|
|
|
set_state(dfuDNLOAD_IDLE, OK);
|
2019-03-05 04:02:02 +00:00
|
|
|
} else {
|
2019-03-26 01:39:55 +00:00
|
|
|
set_state(dfuDNBUSY, OK);
|
2019-03-05 04:02:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case dfuMANIFEST_SYNC:
|
|
|
|
// Ready to reboot. The main thread will take care of this. Also let the DFU tool
|
|
|
|
// know to leave us alone until this happens.
|
|
|
|
dfu_state = dfuMANIFEST;
|
2019-03-28 03:13:25 +00:00
|
|
|
dfu_poll_timeout_ms = 10;
|
2019-03-05 04:02:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case dfuMANIFEST:
|
|
|
|
// Perform the reboot
|
|
|
|
dfu_state = dfuMANIFEST_WAIT_RESET;
|
2019-03-28 03:13:25 +00:00
|
|
|
dfu_poll_timeout_ms = 1000;
|
2019-03-05 04:02:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
status[0] = dfu_status;
|
2019-03-28 03:13:25 +00:00
|
|
|
status[1] = dfu_poll_timeout_ms;
|
|
|
|
status[2] = dfu_poll_timeout_ms >> 8;
|
|
|
|
status[3] = dfu_poll_timeout_ms >> 16;
|
2019-03-05 04:02:02 +00:00
|
|
|
status[4] = dfu_state;
|
|
|
|
status[5] = 0; // iString
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dfu_clrstatus(void)
|
|
|
|
{
|
|
|
|
switch (dfu_state) {
|
|
|
|
|
|
|
|
case dfuERROR:
|
|
|
|
// Clear an error
|
|
|
|
set_state(dfuIDLE, OK);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Unexpected request
|
|
|
|
set_state(dfuERROR, errSTALLEDPKT);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dfu_abort(void)
|
|
|
|
{
|
|
|
|
set_state(dfuIDLE, OK);
|
|
|
|
return true;
|
2019-03-25 09:39:06 +00:00
|
|
|
}
|