Horus bootloader (#5455)

Horus bootloader
This commit is contained in:
Raphael Coeffic
2017-12-19 12:13:59 +01:00
committed by Bertrand Songis
parent 908adb4d2b
commit 0d7832b208
54 changed files with 1772 additions and 1038 deletions
+25
View File
@@ -0,0 +1,25 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
__attribute__ ((section(".bootloader"), used))
const unsigned char BootCode[] = {
#include "bootloader.lbm"
};
@@ -36,6 +36,7 @@ set(FIRMWARE_TARGET_SRC
../common/arm/stm32/usbd_desc.c
../common/arm/stm32/usbd_usr.cpp
../common/arm/stm32/usb_driver.cpp
../common/arm/stm32/flash_driver.cpp
)
set(FIRMWARE_TARGET_SRC
${FIRMWARE_TARGET_SRC}
@@ -0,0 +1,130 @@
# TODO if the files order is different (these 2 SRC sections exchanged), the bootloader hangs for 20s in USB init. Why?
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
if(PCB STREQUAL X12S OR PCB STREQUAL X10)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../f4/system_stm32f4xx.c
../../../../../targets/${TARGET_DIR}/startup_stm32f42_43xxx.s
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spi.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_fmc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_ltdc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_dma.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_dma2d.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_sdio.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/misc.c
)
elseif(PCB STREQUAL X9E)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../f4/system_stm32f4xx.c
../../../../../${STM32LIB_DIR}/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc_ride7/startup_stm32f40_41xxx.s
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_spi.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_i2c.c
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/misc.c
)
else()
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../f2/system_stm32f2xx.c
../../../../../${STM32LIB_DIR}/CMSIS/Device/ST/STM32F2xx/Source/Templates/gcc_ride7/startup_stm32f2xx.s
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_rcc.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_gpio.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_spi.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/stm32f2xx_i2c.c
../../../../../${STM32LIB_DIR}/STM32F2xx_StdPeriph_Driver/src/misc.c
)
endif()
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../gui/${GUI_DIR}/lcd.cpp
../../../../../gui/${GUI_DIR}/fonts.cpp
../../../../../keys.cpp
../../../../../strhelpers.cpp
../../../../../stamp.cpp
../../../../../${STM32USB_DIR}/STM32_USB_OTG_Driver/src/usb_core.c
../../../../../${STM32USB_DIR}/STM32_USB_OTG_Driver/src/usb_dcd.c
../../../../../${STM32USB_DIR}/STM32_USB_OTG_Driver/src/usb_dcd_int.c
../../../../../${STM32USB_DIR}/STM32_USB_Device_Library/Core/src/usbd_core.c
../../../../../${STM32USB_DIR}/STM32_USB_Device_Library/Core/src/usbd_ioreq.c
../../../../../${STM32USB_DIR}/STM32_USB_Device_Library/Core/src/usbd_req.c
../../../../../${STM32USB_DIR}/STM32_USB_Device_Library/Class/msc/src/usbd_msc_data.c
../../../../../${STM32USB_DIR}/STM32_USB_Device_Library/Class/msc/src/usbd_msc_scsi.c
../../../../../${STM32USB_DIR}/STM32_USB_Device_Library/Class/msc/src/usbd_msc_bot.c
../../../../../${STM32USB_DIR}/STM32_USB_Device_Library/Class/msc/src/usbd_msc_core.c
../../../../../${FATFS_DIR}/ff.c
../../../../../${FATFS_DIR}/option/ccsbcs.c
../../../../../targets/${TARGET_DIR}/${LCD_DRIVER}
../../../../../targets/${TARGET_DIR}/backlight_driver.cpp
../../../../../targets/${TARGET_DIR}/keys_driver.cpp
../../../../../targets/${TARGET_DIR}/diskio.cpp
../../../../../targets/${TARGET_DIR}/pwr_driver.cpp
../../../../../targets/${TARGET_DIR}/bootloader/boot_menu.cpp
../usbd_usr.cpp
../usbd_storage_msd.cpp
../delays.cpp
../usbd_desc.c
../usb_bsp.c
../usb_driver.cpp
../flash_driver.cpp
init.c
boot.cpp
bin_files.cpp
)
if(NOT (PCB STREQUAL X10 OR PCB STREQUAL X12S))
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/${TARGET_DIR}/i2c_driver.cpp
)
remove_definitions(-DDEBUG)
else()
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../targets/${TARGET_DIR}/sdram_driver.c
../../../../../targets/${TARGET_DIR}/sdio_sd.c
../../../../../targets/${TARGET_DIR}/haptic_driver.cpp
../../../../../gui/${GUI_DIR}/bitmapbuffer.cpp
../../../../../syscalls.c
)
if(DEBUG)
set(BOOTLOADER_SRC
${BOOTLOADER_SRC}
../../../../../${STM32LIB_DIR}/STM32F4xx_StdPeriph_Driver/src/stm32f4xx_usart.c
../../../../../serial.cpp
../serial2_driver.cpp
)
endif()
endif()
remove_definitions(-DDISK_CACHE)
remove_definitions(-DLUA)
remove_definitions(-DCLI)
add_definitions(-DBOOT)
set(CMAKE_EXE_LINKER_FLAGS "-mcpu=${MCU} -mthumb -nostartfiles -lm -T${RADIO_SRC_DIRECTORY}/targets/${TARGET_DIR}/stm32_ramboot.ld -Wl,-Map=bootloader.map,--cref,--no-warn-mismatch,--gc-sections")
add_executable(bootloader ${BOOTLOADER_SRC})
add_dependencies(bootloader ${BITMAPS_TARGET} firmware_translations)
add_custom_command(
TARGET bootloader POST_BUILD
COMMAND arm-none-eabi-objcopy -O binary bootloader.elf bootloader.bin
)
if(PYTHONINTERP_FOUND)
add_custom_command(
TARGET bootloader POST_BUILD
COMMAND ${PYTHON_EXECUTABLE} ${RADIO_DIRECTORY}/util/bin2lbm.py bootloader.bin bootloader.lbm
)
endif()
PrintTargetReport("bootloader")
@@ -0,0 +1,145 @@
#include "opentx.h"
#include "boot.h"
#include "bin_files.h"
// 'private'
static DIR dir;
static FIL FlashFile;
// 'public' variables
BinFileInfo binFiles[MAX_BIN_FILES];
uint8_t Block_buffer[BLOCK_LEN];
UINT BlockCount;
FRESULT openBinDir(MemoryType mt)
{
FRESULT fr = f_chdir(getBinaryPath(mt));
if (fr != FR_OK) return fr;
return f_opendir(&dir, ".");
}
static FRESULT findNextBinFile(FILINFO* fno)
{
FRESULT fr;
do {
fr = f_readdir(&dir, fno);
if (fr != FR_OK || fno->fname[0] == 0)
break;
int32_t len = strlen(fno->fname) - 4;
if (len < 0)
continue;
if (fno->fname[len] != '.')
continue;
if ((fno->fname[len + 1] != 'b') && (fno->fname[len + 1] != 'B'))
continue;
if ((fno->fname[len + 2] != 'i') && (fno->fname[len + 2] != 'I'))
continue;
if ((fno->fname[len + 3] != 'n') && (fno->fname[len + 3] != 'N'))
continue;
// match!
break;
} while (1);
return fr;
}
unsigned int fetchBinFiles(unsigned int index)
{
FILINFO file_info;
// rewind
if (f_readdir(&dir, NULL) != FR_OK)
return 0;
// skip 'index' .bin files
for (unsigned int i = 0; i <= index; i++) {
if (findNextBinFile(&file_info) != FR_OK /*|| file_info.fname[0] == 0*/)
return 0;
}
strAppend(binFiles[0].name, file_info.fname);
binFiles[0].size = file_info.fsize;
unsigned int i = 1;
for (; i < MAX_NAMES_ON_SCREEN+1; i++) {
if (findNextBinFile(&file_info) != FR_OK || file_info.fname[0] == 0)
return i;
strAppend(binFiles[i].name, file_info.fname);
binFiles[i].size = file_info.fsize;
}
return i;
}
FRESULT openBinFile(MemoryType mt, unsigned int index)
{
TCHAR full_path[_MAX_LFN+1];
FRESULT fr;
// build full_path: [bin path]/[filename]
char* s = strAppend(full_path, getBinaryPath(mt));
s = strAppend(s, "/");
strAppend(s, binFiles[index].name);
BlockCount = 0;
// open the file
if ((fr = f_open(&FlashFile, full_path, FA_READ)) != FR_OK)
return fr;
// skip bootloader in firmware
if (mt == MEM_FLASH &&
((fr = f_lseek(&FlashFile, BOOTLOADER_SIZE)) != FR_OK))
return fr;
// ... and fetch BLOCK_LEN bytes
fr = f_read(&FlashFile, Block_buffer, BLOCK_LEN, &BlockCount);
if (BlockCount == BLOCK_LEN)
return fr;
return FR_INVALID_OBJECT;
}
void extractFirmwareVersion(VersionTag* tag)
{
const char* vers = getOtherVersion((char*)Block_buffer);
if (!vers || (vers[0] == 'n' && vers[1] == 'o')) { // "no version found"
memcpy(tag->flavour, "unknown", sizeof("unknown"));
tag->version = "unknown";
}
// skip "opentx-"
vers += sizeof("opentx-") - 1;
char* fl = tag->flavour;
while(*vers != '-')
*(fl++) = *(vers++);
tag->version = ++vers;
}
FRESULT readBinFile()
{
BlockCount = 0;
return f_read(&FlashFile, Block_buffer, sizeof(Block_buffer), &BlockCount);
}
FRESULT closeBinFile()
{
return f_close(&FlashFile);
}
@@ -0,0 +1,88 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#ifndef _bin_files_h_
#define _bin_files_h_
#include "opentx.h"
enum MemoryType {
MEM_FLASH,
MEM_EEPROM
};
#if defined(EEPROM)
#define getBinaryPath(mt) ((mt == MEM_FLASH) ? FIRMWARES_PATH : EEPROMS_PATH)
#else
#define getBinaryPath(mt) (FIRMWARES_PATH)
#endif
#define MAX_NAMES_ON_SCREEN 6
#define MAX_BIN_FILES (MAX_NAMES_ON_SCREEN+1)
// Size of the block read when checking / writing BIN files
#define BLOCK_LEN 4096
// File info struct while browsing files on SD card
struct BinFileInfo {
TCHAR name[_MAX_LFN + 1];
unsigned int size;
};
// File info storage while browsing files on SD card
extern BinFileInfo binFiles[MAX_BIN_FILES];
// Block buffer used when checking / writing BIN files
extern uint8_t Block_buffer[BLOCK_LEN];
// Bytes read into the Block_buffer
extern UINT BlockCount;
// Open directory for EEPROM / firmware files
FRESULT openBinDir(MemoryType mt);
// Fetch file names and sizes into binFiles,
// starting at the provided index.
// Only files ending with ".bin" (case-insensitive)
// will be considered.
unsigned int fetchBinFiles(unsigned int index);
// Open file indexed in binFiles and read the first BLOCK_LEN bytes
// Bootloader is skipped in firmware files
FRESULT openBinFile(MemoryType mt, unsigned int index);
struct VersionTag
{
char flavour[8];
const char* version;
};
// Can be called right after openBinFile() to extract the version information
// from a firmware file
void extractFirmwareVersion(VersionTag* tag);
// Read the next BLOCK_LEN bytes into 'Block_buffer'
// Check 'BlockCount' for # of bytes read
FRESULT readBinFile();
// Close the previously opened file
FRESULT closeBinFile();
#endif
@@ -0,0 +1,523 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include "opentx.h"
#include "stamp.h"
#include "boot.h"
#include "bin_files.h"
#define APP_START_ADDRESS (uint32_t)(FIRMWARE_ADDRESS + BOOTLOADER_SIZE)
#if defined(EEPROM)
#define MAIN_MENU_LEN 3
#else
#define MAIN_MENU_LEN 2
#endif
typedef void (*voidFunction)(void);
#define jumpTo(addr) { \
SCB->VTOR = addr; \
__set_MSP(*(__IO uint32_t*)addr); \
uint32_t jumpAddress = *(uint32_t*)(addr + 4); \
voidFunction jumpFn = (voidFunction)jumpAddress; \
jumpFn(); \
}
// Bootloader marker:
// -> used to detect valid bootloader files
const uint8_t bootloaderVersion[] __attribute__ ((section(".version"), used)) =
{ 'B', 'O', 'O', 'T', '1', '0' };
#if defined(ROTARY_ENCODER_NAVIGATION)
volatile rotenc_t rotencValue[1] = {0};
#endif
/*----------------------------------------------------------------------------
* Local variables
*----------------------------------------------------------------------------*/
uint32_t FirmwareSize;
uint32_t firmwareAddress = FIRMWARE_ADDRESS;
uint32_t firmwareWritten = 0;
#if defined(EEPROM)
uint32_t eepromAddress = 0;
uint32_t eepromWritten = 0;
#endif
//TCHAR backupFilename[_MAX_LFN+1];
//uint32_t Master_frequency;
volatile uint8_t Tenms = 1;
FlashCheckRes Valid;
MemoryType memoryType;
uint32_t unlocked = 0;
void interrupt10ms(void)
{
Tenms |= 1; // 10 mS has passed
uint8_t index = 0;
uint8_t in = readKeys();
for (uint8_t i = 1; i != uint8_t(1 << TRM_BASE); i <<= 1) {
uint8_t value = (in & i);
keys[index].input(value);
++index;
}
#if defined(ROTARY_ENCODER_NAVIGATION)
checkRotaryEncoder();
static rotenc_t rePreviousValue;
rotenc_t reNewValue = (rotencValue[0] / 2);
int8_t scrollRE = reNewValue - rePreviousValue;
if (scrollRE) {
rePreviousValue = reNewValue;
if (scrollRE < 0) {
putEvent(EVT_KEY_FIRST(KEY_UP)); //EVT_ROTARY_LEFT
}
else {
putEvent(EVT_KEY_FIRST(KEY_DOWN)); //EVT_ROTARY_RIGHT
}
}
#endif
}
void init10msTimer()
{
INTERRUPT_xMS_TIMER->ARR = 9999; // 10mS in uS
INTERRUPT_xMS_TIMER->PSC = (PERI1_FREQUENCY * TIMER_MULT_APB1) / 1000000 - 1; // 1uS
INTERRUPT_xMS_TIMER->CCER = 0;
INTERRUPT_xMS_TIMER->CCMR1 = 0;
INTERRUPT_xMS_TIMER->EGR = 0;
INTERRUPT_xMS_TIMER->CR1 = 5;
INTERRUPT_xMS_TIMER->DIER |= 1;
NVIC_EnableIRQ(INTERRUPT_xMS_IRQn);
}
extern "C" void INTERRUPT_xMS_IRQHandler()
{
INTERRUPT_xMS_TIMER->SR &= ~TIM_SR_UIF;
interrupt10ms();
}
uint32_t isValidBufferStart(const uint8_t * buffer)
{
#if defined(EEPROM)
if (memoryType == MEM_FLASH)
return isFirmwareStart(buffer);
else
return isEepromStart(buffer);
#else
return isFirmwareStart(buffer);
#endif
}
FlashCheckRes checkFlashFile(unsigned int index, FlashCheckRes res)
{
if (res != FC_UNCHECKED)
return res;
if (openBinFile(memoryType, index) != FR_OK)
return FC_ERROR;
if (closeBinFile() != FR_OK)
return FC_ERROR;
if (!isValidBufferStart(Block_buffer))
return FC_ERROR;
return FC_OK;
}
int menuFlashFile(uint32_t index, event_t event)
{
Valid = checkFlashFile(index, Valid);
if (Valid == FC_ERROR) {
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER))
return 0;
return -1;
}
if (event == EVT_KEY_LONG(KEY_ENTER)) {
return (openBinFile(memoryType, index) == FR_OK) && isValidBufferStart(Block_buffer);
}
else if (event == EVT_KEY_BREAK(KEY_EXIT))
return 0;
return -1;
}
extern Key keys[];
static uint32_t PowerUpDelay;
void flashWriteBlock()
{
uint32_t blockOffset = 0;
while (BlockCount) {
flashWrite((uint32_t *)firmwareAddress, (uint32_t *)&Block_buffer[blockOffset]);
blockOffset += FLASH_PAGESIZE;
firmwareAddress += FLASH_PAGESIZE;
if (BlockCount > FLASH_PAGESIZE) {
BlockCount -= FLASH_PAGESIZE;
}
else {
BlockCount = 0;
}
}
}
#if defined(EEPROM)
void writeEepromBlock()
{
eepromWriteBlock(Block_buffer, eepromAddress, BlockCount);
eepromAddress += BlockCount;
}
#endif
int main()
{
BootloaderState state = ST_START;
uint32_t vpos = 0;
uint8_t index = 0;
FRESULT fr;
uint32_t nameCount = 0;
wdt_reset();
RCC_AHB1PeriphClockCmd(PWR_RCC_AHB1Periph | KEYS_RCC_AHB1Periph |
LCD_RCC_AHB1Periph | BACKLIGHT_RCC_AHB1Periph |
SERIAL_RCC_AHB1Periph | I2C_RCC_AHB1Periph |
SD_RCC_AHB1Periph, ENABLE);
RCC_APB1PeriphClockCmd(LCD_RCC_APB1Periph | BACKLIGHT_RCC_APB1Periph |
INTERRUPT_xMS_RCC_APB1Periph | I2C_RCC_APB1Periph |
SERIAL_RCC_APB1Periph |
SD_RCC_APB1Periph, ENABLE);
RCC_APB2PeriphClockCmd(LCD_RCC_APB2Periph | BACKLIGHT_RCC_APB2Periph, ENABLE);
keysInit();
// wait for inputs to stabilize
for (uint32_t i = 0; i < 50000; i += 1) {
wdt_reset();
}
// LHR & RHL trims not pressed simultanously
if (readTrims() != 0x42) {
// Start main application
jumpTo(APP_START_ADDRESS);
}
pwrInit();
delaysInit(); // needed for lcdInit()
#if defined(DEBUG)
serial2Init(UART_MODE_DEBUG, 0); // default serial mode (None if DEBUG not defined)
#endif
__enable_irq();
TRACE("\nBootloader started :)");
lcdInit();
backlightInit();
#if defined(PCBTARANIS)
i2cInit();
#endif
init10msTimer();
// SD card detect pin
sdInit();
usbInit();
// init screen
bootloaderInitScreen();
#if defined(PWR_PRESS_BUTTON) or defined(PCBHORUS)
// wait until power button is released
while(pwrPressed()) {
wdt_reset();
}
#endif
for (;;) {
wdt_reset();
if (Tenms) {
Tenms = 0;
if (state != ST_USB) {
if (usbPlugged()) {
state = ST_USB;
if (!unlocked) {
unlocked = 1;
unlockFlash();
}
usbStart();
usbPluggedIn();
}
}
if (state == ST_USB) {
if (usbPlugged() == 0) {
vpos = 0;
usbStop();
if (unlocked) {
lockFlash();
unlocked = 0;
}
state = ST_START;
}
bootloaderDrawScreen(state, 0);
}
lcdRefreshWait();
event_t event = getEvent();
if (state == ST_START) {
bootloaderDrawScreen(state, vpos);
if (event == EVT_KEY_FIRST(KEY_DOWN)) {
vpos = (vpos + 1) % MAIN_MENU_LEN;
continue;
}
else if (event == EVT_KEY_FIRST(KEY_UP)) {
vpos = (vpos + MAIN_MENU_LEN - 1) % MAIN_MENU_LEN;
continue;
}
else if (event == EVT_KEY_BREAK(KEY_ENTER)) {
switch (vpos) {
case 0:
memoryType = MEM_FLASH;
state = ST_DIR_CHECK;
break;
#if defined(EEPROM)
case 1:
memoryType = MEM_EEPROM;
state = ST_DIR_CHECK;
break;
#endif
default:
state = ST_REBOOT;
break;
}
// next loop
continue;
}
}
else if (state == ST_DIR_CHECK) {
fr = openBinDir(memoryType);
if (fr == FR_OK) {
index = vpos = 0;
state = ST_FILE_LIST;
nameCount = fetchBinFiles(index);
continue;
}
else {
bootloaderDrawScreen(state, fr);
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
vpos = 0;
state = ST_START;
continue;
}
}
}
if (state == ST_FILE_LIST) {
uint32_t limit = MAX_NAMES_ON_SCREEN;
if (nameCount < limit) {
limit = nameCount;
}
if (event == EVT_KEY_REPT(KEY_DOWN) || event == EVT_KEY_FIRST(KEY_DOWN)) {
if (vpos < limit - 1) {
vpos += 1;
}
else {
if (nameCount > limit) {
index += 1;
nameCount = fetchBinFiles(index);
}
}
}
else if (event == EVT_KEY_REPT(KEY_UP) || event == EVT_KEY_FIRST(KEY_UP)) {
if (vpos > 0) {
vpos -= 1;
}
else {
if (index) {
index -= 1;
nameCount = fetchBinFiles(index);
}
}
}
bootloaderDrawScreen(state, 0);
for (uint32_t i=0; i<limit; i++) {
bootloaderDrawFilename(binFiles[i].name, i, (vpos == i));
}
if (event == EVT_KEY_BREAK(KEY_ENTER)) {
// Select file to flash
state = ST_FLASH_CHECK;
Valid = FC_UNCHECKED;
continue;
}
else if (event == EVT_KEY_BREAK(KEY_EXIT)) {
state = ST_START;
vpos = 0;
continue;
}
}
else if (state == ST_FLASH_CHECK) {
bootloaderDrawScreen(state, Valid, binFiles[vpos].name);
int result = menuFlashFile(vpos, event);
if (result == 0) {
// canceled
state = ST_FILE_LIST;
}
else if (result == 1) {
// confirmed
if (memoryType == MEM_FLASH) {
FirmwareSize = binFiles[vpos].size - BOOTLOADER_SIZE;
firmwareAddress = FIRMWARE_ADDRESS + BOOTLOADER_SIZE;
firmwareWritten = 0;
}
#if defined(EEPROM)
else {
eepromAddress = 0;
eepromWritten = 0;
}
#endif
state = ST_FLASHING;
}
}
else if (state == ST_FLASHING) {
// commit to flashing
if (!unlocked && (memoryType == MEM_FLASH)) {
unlocked = 1;
unlockFlash();
}
int progress = 0;
if (memoryType == MEM_FLASH) {
flashWriteBlock();
firmwareWritten += sizeof(Block_buffer);
progress = (100*firmwareWritten) / FirmwareSize;
}
#if defined(EEPROM)
else {
writeEepromBlock();
eepromWritten += sizeof(Block_buffer);
progress = (100*eepromWritten) / EEPROM_SIZE;
}
#endif
bootloaderDrawScreen(state, progress);
fr = readBinFile();
if (BlockCount == 0) {
state = ST_FLASH_DONE; // EOF
}
else if ((memoryType == MEM_FLASH) &&
(firmwareWritten >= FLASHSIZE - BOOTLOADER_SIZE)) {
state = ST_FLASH_DONE; // Backstop
}
#if defined(EEPROM)
else if ((memoryType == MEM_EEPROM) &&
(eepromWritten >= EEPROM_SIZE)) {
state = ST_FLASH_DONE; // Backstop
}
#endif
}
if (state == ST_FLASH_DONE) {
if (unlocked) {
lockFlash();
unlocked = 0;
}
if (event == EVT_KEY_BREAK(KEY_EXIT) || event == EVT_KEY_BREAK(KEY_ENTER)) {
state = ST_START;
vpos = 0;
}
bootloaderDrawScreen(state, 100);
}
if (event == EVT_KEY_LONG(KEY_EXIT)) {
// Start the main application
state = ST_REBOOT;
}
lcdRefresh();
if (PowerUpDelay < 20) { // 200 mS
PowerUpDelay += 1;
}
else {
sdPoll10ms();
}
}
if (state != ST_FLASHING && state != ST_USB) {
if (pwrOffPressed()) {
lcdClear();
lcdOff(); // this drains LCD caps
pwrOff();
for (;;) {
// Wait for power to go off
}
}
}
if (state == ST_REBOOT) {
// Jump to proper application address
lcdClear();
jumpTo(APP_START_ADDRESS);
}
}
return 0;
}
#if defined(PCBHORUS)
void *__dso_handle = 0;
#endif
@@ -0,0 +1,80 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#ifndef _boot_h_
#define _boot_h_
#include "stamp.h"
#define BOOTLOADER_TITLE " OTX Bootloader - " VERSION
#define DISPLAY_CHAR_WIDTH (LCD_COLS+4)
#if LCD_W >= 480
#define STR_INVALID_FIRMWARE "Not a valid firmware file"
#elif LCD_W >= 212
#define STR_OR_PLUGIN_USB_CABLE INDENT "Or plug in a USB cable for mass storage"
#define STR_HOLD_ENTER_TO_START "\012Hold [ENT] to start writing"
#define STR_INVALID_FIRMWARE "\011Not a valid firmware file! "
#define STR_INVALID_EEPROM "\011Not a valid EEPROM file! "
#else
#define STR_OR_PLUGIN_USB_CABLE INDENT "Or plug in a USB cable"
#define STR_HOLD_ENTER_TO_START "\006Hold [ENT] to start"
#define STR_INVALID_FIRMWARE "\004Not a valid firmware! "
#define STR_INVALID_EEPROM "\004Not a valid EEPROM! "
#endif
#define STR_USB_CONNECTED CENTER "\011USB Connected"
// Bootloader states
enum BootloaderState {
ST_START,
ST_FLASH_MENU,
ST_DIR_CHECK,
ST_OPEN_DIR,
ST_FILE_LIST,
ST_FLASH_CHECK,
ST_FLASHING,
ST_FLASH_DONE,
ST_RESTORE_MENU,
ST_USB,
ST_REBOOT,
};
enum FlashCheckRes {
FC_UNCHECKED=0,
FC_OK,
FC_ERROR
};
// Declarations of functions that need to be implemented
// for each target with a bootloader
// On bootloader start after lcdInit()
void bootloaderInitScreen();
// Depending on the state, up to two optional parameters are passed.
// See boot.cpp/main for more details
void bootloaderDrawScreen(BootloaderState st, int opt, const char* str = NULL);
// Once for each file in a filename list on screen
void bootloaderDrawFilename(const char* str, uint8_t line, bool selected);
#endif
@@ -0,0 +1,4 @@
void _init (void)
{
}
@@ -0,0 +1,201 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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.
*/
#include "opentx.h"
//After reset, write is not allowed in the Flash control register (FLASH_CR) to protect the
//Flash memory against possible unwanted operations due, for example, to electric
//disturbances. The following sequence is used to unlock this register:
//1. Write KEY1 = 0x45670123 in the Flash key register (FLASH_KEYR)
//2. Write KEY2 = 0xCDEF89AB in the Flash key register (FLASH_KEYR)
//Any wrong sequence will return a bus error and lock up the FLASH_CR register until the
//next reset.
//The FLASH_CR register can be locked again by software by setting the LOCK bit in the
//FLASH_CR register.
void unlockFlash()
{
FLASH->KEYR = 0x45670123;
FLASH->KEYR = 0xCDEF89AB;
}
void lockFlash()
{
while (FLASH->SR & FLASH_SR_BSY);
FLASH->CR |= FLASH_CR_LOCK;
}
void waitFlashIdle()
{
while (FLASH->SR & FLASH_FLAG_BSY) {
wdt_reset();
}
}
#define SECTOR_MASK ((uint32_t)0xFFFFFF07)
void eraseSector(uint32_t sector)
{
waitFlashIdle();
FLASH->CR &= CR_PSIZE_MASK;
FLASH->CR |= FLASH_PSIZE_WORD;
FLASH->CR &= SECTOR_MASK;
FLASH->CR |= FLASH_CR_SER | (sector << 3);
FLASH->CR |= FLASH_CR_STRT;
/* Wait for operation to be completed */
waitFlashIdle();
/* if the erase operation is completed, disable the SER Bit */
FLASH->CR &= (~FLASH_CR_SER);
FLASH->CR &= SECTOR_MASK;
}
void flashWrite(uint32_t * address, uint32_t * buffer) // page size is 256 bytes
{
#define SECTOR_ADDRESS (((uint32_t)address) & 0xFFFFF)
// Please note that there is an offset of 4 between
// sector 11 and 12
#define FLASH_BANK ((((uint32_t)address) & 0x100000) ? 16 : 0)
// test for possible flash sector boundary
if ((((uint32_t)address) & 0x1FFFF) == 0) {
// test first 16KB sectors
if (SECTOR_ADDRESS == 0x00000) {
eraseSector(0 + FLASH_BANK);
}
// test 128KB sectors
else if (SECTOR_ADDRESS == 0x20000) {
eraseSector(5 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0x40000) {
eraseSector(6 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0x60000) {
eraseSector(7 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0x80000) {
eraseSector(8 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0xA0000) {
eraseSector(9 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0xC0000) {
eraseSector(10 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0xE0000) {
eraseSector(11 + FLASH_BANK);
}
}
// test 64KB sector
else if (SECTOR_ADDRESS == 0x10000) {
eraseSector(4 + FLASH_BANK);
}
else if ((((uint32_t)address) & 0x3FFF) == 0) {
// test other 16KB sectors
if (SECTOR_ADDRESS == 0x04000) {
eraseSector(1 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0x08000) {
eraseSector(2 + FLASH_BANK);
}
else if (SECTOR_ADDRESS == 0x0C000) {
eraseSector(3 + FLASH_BANK);
}
}
#undef SECTOR_ADDRESS
#undef FLASH_BANK
for (uint32_t i=0; i<FLASH_PAGESIZE/4; i++) {
/* Device voltage range supposed to be [2.7V to 3.6V], the operation will
be done by word */
// Wait for last operation to be completed
waitFlashIdle();
FLASH->CR &= CR_PSIZE_MASK;
FLASH->CR |= FLASH_PSIZE_WORD;
FLASH->CR |= FLASH_CR_PG;
*address = *buffer;
/* Wait for operation to be completed */
waitFlashIdle();
FLASH->CR &= (~FLASH_CR_PG);
/* Check the written value */
if (*address != *buffer) {
/* Flash content doesn't match SRAM content */
return;
}
/* Increment FLASH destination address */
address += 1;
buffer += 1;
}
}
uint32_t isFirmwareStart(const uint8_t * buffer)
{
const uint32_t * block = (const uint32_t *)buffer;
#if defined(STM32F4)
// Stack pointer in CCM or RAM
if ((block[0] & 0xFFFC0000) != 0x10000000 && (block[0] & 0xFFFC0000) != 0x20000000) {
return 0;
}
// First ISR pointer in FLASH
if ((block[1] & 0xFFF00000) != 0x08000000) {
return 0;
}
// Second ISR pointer in FLASH
if ((block[2] & 0xFFF00000) != 0x08000000) {
return 0;
}
#else
// Stack pointer in RAM
if ((block[0] & 0xFFFC0000) != 0x20000000) {
return 0;
}
// First ISR pointer in FLASH
if ((block[1] & 0xFFF00000) != 0x08000000) {
return 0;
}
// Second ISR pointer in FLASH
if ((block[2] & 0xFFF00000) != 0x08000000) {
return 0;
}
#endif
return 1;
}
uint32_t isBootloaderStart(const uint8_t * buffer)
{
const uint32_t * block = (const uint32_t *)buffer;
for (int i=0; i<256; i++) {
if (block[i] == 0x544F4F42/*BOOT*/) {
return 1;
}
}
return 0;
}
@@ -408,7 +408,7 @@ const FATDirEntry_t g_DIRroot[] =
0xA302,
0x3D55,
0x0002,
sizeof(firmware_txt) + strlen(getOtherVersion())
sizeof(firmware_txt) + strlen(getOtherVersion(nullptr))
},
{
{ 'F', 'I', 'R', 'M', 'W', 'A', 'R', 'E'},
@@ -515,7 +515,7 @@ int32_t fat12Read(uint8_t * buffer, uint16_t sector, uint16_t count)
else if (sector == 4)
{
memcpy(buffer, firmware_txt, sizeof(firmware_txt));
memcpy(buffer + sizeof(firmware_txt), getOtherVersion(), strlen(getOtherVersion()));
memcpy(buffer + sizeof(firmware_txt), getOtherVersion(nullptr), strlen(getOtherVersion(nullptr)));
}
else if (sector < RESERVED_SECTORS)
{