[FL-2263] Flasher service & RAM exec (#1006)
* WIP on stripping fw * Compact FW build - use RAM_EXEC=1 COMPACT=1 DEBUG=0 * Fixed uninitialized storage struct; small fixes to compact fw * Flasher srv w/mocked flash ops * Fixed typos & accomodated FFF changes * Alternative fw startup branch * Working load & jmp to RAM fw * +manifest processing for stage loader; + crc verification for stage payload * Fixed questionable code & potential leaks * Lowered screen update rate; added radio stack update stubs; working dfu write * Console EP with manifest & stage validation * Added microtar lib; minor ui fixes for updater * Removed microtar * Removed mtar #2 * Added a better version of microtar * TAR archive api; LFS backup & restore core * Recursive backup/restore * LFS worker thread * Added system apps to loader - not visible in UI; full update process with restarts * Typo fix * Dropped BL & f6; tooling for updater WIP * Minor py fixes * Minor fixes to make it build after merge * Ported flash workaround from BL + fixed visuals * Minor cleanup * Chmod + loader app search fix * Python linter fix * Removed usb stuff & float read support for staged loader == -10% of binary size * Added backup/restore & update pb requests * Added stub impl to RPC for backup/restore/update commands * Reworked TAR to use borrowed Storage api; slightly reduced build size by removing `static string`; hidden update-related RPC behind defines * Moved backup&restore to storage * Fixed new message types * Backup/restore/update RPC impl * Moved furi_hal_crc to LL; minor fixes * CRC HAL rework to LL * Purging STM HAL * Brought back minimal DFU boot mode (no gui); additional crc state checks * Added splash screen, BROKEN usb function * Clock init rework WIP * Stripped graphics from DFU mode * Temp fix for unused static fun * WIP update picker - broken! * Fixed UI * Bumping version * Fixed RTC setup * Backup to update folder instead of ext root * Removed unused scenes & more usb remnants from staged loader * CI updates * Fixed update bundle name * Temporary restored USB handler * Attempt to prevent .text corruption * Comments on how I spent this Saturday * Added update file icon * Documentation updates * Moved common code to lib folder * Storage: more unit tests * Storage: blocking dir open, differentiate file and dir when freed. * Major refactoring; added input processing to updater to allow retrying on failures (not very useful prob). Added API for extraction of thread return value * Removed re-init check for manifest * Changed low-level path manipulation to toolbox/path.h; makefile cleanup; tiny fix in lint.py * Increased update worker stack size * Text fixes in backup CLI * Displaying number of update stages to run; removed timeout in handling errors * Bumping version * Added thread cleanup for spawner thread * Updated build targets to exclude firmware bundle from 'ALL' * Fixed makefile for update_package; skipping VCP init for update mode (ugly) * Switched github build from ALL to update_package * Added +x for dist_update.sh * Cli: add total heap size to "free" command * Moved (RAM) suffix to build version instead of git commit no. * DFU comment * Some fixes suggested by clang-tidy * Fixed recursive PREFIX macro * Makefile: gather all new rules in updater namespace. FuriHal: rename bootloader to boot, isr safe delays * Github: correct build target name in firmware build * FuriHal: move target switch to boot * Makefile: fix firmware flash * Furi, FuriHal: move kernel start to furi, early init * Drop bootloader related stuff * Drop cube. Drop bootloader linker script. * Renamed update_hl, moved constants to #defines * Moved update-related boot mode to separate bitfield * Reworked updater cli to single entry point; fixed crash on tar cleanup * Added Python replacement for dist shell scripts * Linter fixes for dist.py +x * Fixes for environment suffix * Dropped bash scripts * Added dirty build flag to version structure & interfaces * Version string escapes * Fixed flag logic in dist.py; added support for App instances being imported and not terminating the whole program * Fixed fw address in ReadMe.md * Rpc: fix crash on double screen start * Return back original boot behavior and fix jump to system bootloader * Cleanup code, add error sequence for RTC * Update firmware readme * FuriHal: drop boot, restructure RTC registers usage and add header register check * Furi goes first * Toolchain: add ccache support * Renamed update bundle dir Co-authored-by: DrZlo13 <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
41
firmware/targets/f7/Src/dfu.c
Normal file
41
firmware/targets/f7/Src/dfu.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <flipper.h>
|
||||
#include <alt_boot.h>
|
||||
#include <u8g2_glue.h>
|
||||
#include <assets_icons.h>
|
||||
|
||||
void flipper_boot_dfu_show_splash() {
|
||||
// Initialize
|
||||
furi_hal_compress_icon_init();
|
||||
|
||||
u8g2_t* fb = malloc(sizeof(u8g2_t));
|
||||
memset(fb, 0, sizeof(u8g2_t));
|
||||
u8g2_Setup_st756x_flipper(fb, U8G2_R0, u8x8_hw_spi_stm32, u8g2_gpio_and_delay_stm32);
|
||||
u8g2_InitDisplay(fb);
|
||||
u8g2_SetDrawColor(fb, 0x01);
|
||||
uint8_t* splash_data = NULL;
|
||||
furi_hal_compress_icon_decode(icon_get_data(&I_DFU_128x50), &splash_data);
|
||||
u8g2_DrawXBM(fb, 0, 64 - 50, 128, 50, splash_data);
|
||||
u8g2_SetFont(fb, u8g2_font_helvB08_tr);
|
||||
u8g2_DrawStr(fb, 2, 8, "Update & Recovery Mode");
|
||||
u8g2_DrawStr(fb, 2, 21, "DFU started");
|
||||
u8g2_SetPowerSave(fb, 0);
|
||||
u8g2_SendBuffer(fb);
|
||||
}
|
||||
|
||||
void flipper_boot_dfu_exec() {
|
||||
// Show DFU splashscreen
|
||||
flipper_boot_dfu_show_splash();
|
||||
|
||||
// Errata 2.2.9, Flash OPTVERR flag is always set after system reset
|
||||
WRITE_REG(FLASH->SR, FLASH_SR_OPTVERR);
|
||||
|
||||
// Cleanup before jumping to DFU
|
||||
furi_hal_deinit_early();
|
||||
|
||||
// Remap memory to system bootloader
|
||||
LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SYSTEMFLASH);
|
||||
// Jump
|
||||
furi_hal_switch(0x0);
|
||||
}
|
@@ -1,29 +1,85 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <flipper.h>
|
||||
#include <alt_boot.h>
|
||||
#include <semphr.h>
|
||||
|
||||
#define TAG "Main"
|
||||
|
||||
int main(void) {
|
||||
// Flipper critical FURI HAL
|
||||
furi_hal_init_critical();
|
||||
|
||||
#ifdef FURI_RAM_EXEC
|
||||
int main() {
|
||||
// Initialize FURI layer
|
||||
furi_init();
|
||||
|
||||
// Flipper critical FURI HAL
|
||||
furi_hal_init_early();
|
||||
|
||||
// Flipper FURI HAL
|
||||
furi_hal_init();
|
||||
|
||||
// CMSIS initialization
|
||||
osKernelInitialize();
|
||||
FURI_LOG_I(TAG, "KERNEL OK");
|
||||
|
||||
// Init flipper
|
||||
flipper_init();
|
||||
|
||||
// Start kernel
|
||||
osKernelStart();
|
||||
furi_run();
|
||||
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
#else
|
||||
int main() {
|
||||
// Initialize FURI layer
|
||||
furi_init();
|
||||
|
||||
// Flipper critical FURI HAL
|
||||
furi_hal_init_early();
|
||||
furi_hal_light_sequence("RGB");
|
||||
|
||||
// Delay is for button sampling
|
||||
furi_hal_delay_ms(100);
|
||||
|
||||
FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
|
||||
if(boot_mode == FuriHalRtcBootModeDfu || !furi_hal_gpio_read(&gpio_button_left)) {
|
||||
furi_hal_light_sequence("rgb WB");
|
||||
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
|
||||
flipper_boot_dfu_exec();
|
||||
furi_hal_power_reset();
|
||||
} else if(boot_mode == FuriHalRtcBootModeUpdate) {
|
||||
furi_hal_light_sequence("rgb BR");
|
||||
flipper_boot_update_exec();
|
||||
// if things go nice, we shouldn't reach this point.
|
||||
// But if we do, abandon to avoid bootloops
|
||||
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
|
||||
furi_hal_power_reset();
|
||||
} else {
|
||||
furi_hal_light_sequence("rgb G");
|
||||
|
||||
// Flipper FURI HAL
|
||||
furi_hal_init();
|
||||
|
||||
// Init flipper
|
||||
flipper_init();
|
||||
|
||||
furi_run();
|
||||
}
|
||||
|
||||
while(1) {
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void Error_Handler(void) {
|
||||
furi_crash("ErrorHandler");
|
||||
}
|
||||
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief Reports the name of the source file and the source line number
|
||||
* where the assert_param error has occurred.
|
||||
* @param file: pointer to the source file name
|
||||
* @param line: assert_param error line source number
|
||||
* @retval None
|
||||
*/
|
||||
void assert_failed(uint8_t* file, uint32_t line) {
|
||||
furi_crash("HAL assert failed");
|
||||
}
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
|
@@ -1,15 +1,15 @@
|
||||
#include "stm32wbxx.h"
|
||||
|
||||
/*!< Uncomment the following line if you need to relocate your vector Table in
|
||||
Internal SRAM. */
|
||||
/*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */
|
||||
/* #define VECT_TAB_SRAM */
|
||||
|
||||
#ifndef VECT_TAB_OFFSET
|
||||
#define VECT_TAB_OFFSET \
|
||||
OS_OFFSET /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
0x0 /*!< Vector Table base offset field. This value must be a multiple of 0x200. */
|
||||
#endif
|
||||
|
||||
#define VECT_TAB_BASE_ADDRESS \
|
||||
SRAM1_BASE /*!< Vector Table base offset field.
|
||||
This value must be a multiple of 0x200. */
|
||||
SRAM1_BASE /*!< Vector Table base offset field. This value must be a multiple of 0x200. */
|
||||
|
||||
/* The SystemCoreClock variable is updated in three ways:
|
||||
1) by calling CMSIS function SystemCoreClockUpdate()
|
||||
|
196
firmware/targets/f7/Src/update.c
Normal file
196
firmware/targets/f7/Src/update.c
Normal file
@@ -0,0 +1,196 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <flipper.h>
|
||||
#include <alt_boot.h>
|
||||
|
||||
#include <fatfs.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
|
||||
#include <update_util/update_manifest.h>
|
||||
#include <lib/toolbox/path.h>
|
||||
|
||||
static FATFS* pfs = NULL;
|
||||
|
||||
static const char FS_ROOT_PATH[] = "/";
|
||||
|
||||
#define CHECK_FRESULT(result) \
|
||||
{ \
|
||||
if((result) != FR_OK) { \
|
||||
return false; \
|
||||
} \
|
||||
}
|
||||
|
||||
static bool flipper_update_init() {
|
||||
furi_hal_clock_init();
|
||||
furi_hal_rtc_init();
|
||||
furi_hal_interrupt_init();
|
||||
furi_hal_delay_init();
|
||||
|
||||
furi_hal_spi_init();
|
||||
furi_hal_crc_init(false);
|
||||
|
||||
MX_FATFS_Init();
|
||||
if(!hal_sd_detect()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(BSP_SD_Init(true)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
pfs = malloc(sizeof(FATFS));
|
||||
CHECK_FRESULT(f_mount(pfs, FS_ROOT_PATH, 1));
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool flipper_update_load_stage(const string_t work_dir, UpdateManifest* manifest) {
|
||||
FIL file;
|
||||
FILINFO stat;
|
||||
|
||||
string_t loader_img_path;
|
||||
string_init_set(loader_img_path, work_dir);
|
||||
path_append(loader_img_path, string_get_cstr(manifest->staged_loader_file));
|
||||
|
||||
if((f_stat(string_get_cstr(loader_img_path), &stat) != FR_OK) ||
|
||||
(f_open(&file, string_get_cstr(loader_img_path), FA_OPEN_EXISTING | FA_READ) != FR_OK)) {
|
||||
string_clear(loader_img_path);
|
||||
return false;
|
||||
}
|
||||
string_clear(loader_img_path);
|
||||
|
||||
void* img = malloc(stat.fsize);
|
||||
uint32_t bytes_read = 0;
|
||||
const uint16_t MAX_READ = 0xFFFF;
|
||||
|
||||
furi_hal_crc_reset();
|
||||
uint32_t crc = 0;
|
||||
do {
|
||||
uint16_t size_read = 0;
|
||||
if(f_read(&file, img + bytes_read, MAX_READ, &size_read) != FR_OK) {
|
||||
break;
|
||||
}
|
||||
crc = furi_hal_crc_feed(img + bytes_read, size_read);
|
||||
bytes_read += size_read;
|
||||
} while(bytes_read == MAX_READ);
|
||||
furi_hal_crc_reset();
|
||||
|
||||
do {
|
||||
if((bytes_read != stat.fsize) || (crc != manifest->staged_loader_crc)) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Point of no return. Literally
|
||||
*
|
||||
* NB: we MUST disable IRQ, otherwise handlers from flash
|
||||
* will change global variables (like tick count)
|
||||
* that are located in .data. And we move staged loader
|
||||
* to the same memory region. So, IRQ handlers will mess up
|
||||
* memmove'd .text section and ruin your day.
|
||||
* We don't want that to happen.
|
||||
*/
|
||||
__disable_irq();
|
||||
|
||||
memmove((void*)(SRAM1_BASE), img, stat.fsize);
|
||||
LL_SYSCFG_SetRemapMemory(LL_SYSCFG_REMAP_SRAM);
|
||||
furi_hal_switch((void*)SRAM1_BASE);
|
||||
return true;
|
||||
|
||||
} while(false);
|
||||
|
||||
free(img);
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool flipper_update_get_work_directory(string_t out_dir) {
|
||||
const uint32_t update_index = furi_hal_rtc_get_register(FuriHalRtcRegisterUpdateFolderFSIndex);
|
||||
if(update_index == 0) {
|
||||
string_set(out_dir, UPDATE_DIR_DEFAULT_REL_PATH);
|
||||
return true;
|
||||
}
|
||||
|
||||
DIR dir;
|
||||
UINT entry_idx = 0;
|
||||
FILINFO fno;
|
||||
CHECK_FRESULT(f_opendir(&dir, UPDATE_DIR_DEFAULT_REL_PATH));
|
||||
string_set(out_dir, UPDATE_DIR_DEFAULT_REL_PATH);
|
||||
|
||||
while(f_readdir(&dir, &fno) == FR_OK) {
|
||||
entry_idx++;
|
||||
if(fno.fname[0] == '\0') {
|
||||
return false;
|
||||
}
|
||||
if(entry_idx == update_index) {
|
||||
path_append(out_dir, fno.fname);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
string_reset(out_dir);
|
||||
return false;
|
||||
}
|
||||
|
||||
static UpdateManifest* flipper_update_process_manifest(const string_t work_dir) {
|
||||
FIL file;
|
||||
FILINFO stat;
|
||||
|
||||
string_t manifest_path;
|
||||
string_init_set(manifest_path, work_dir);
|
||||
path_append(manifest_path, UPDATE_MANIFEST_DEFAULT_NAME);
|
||||
|
||||
CHECK_FRESULT(f_stat(string_get_cstr(manifest_path), &stat));
|
||||
CHECK_FRESULT(f_open(&file, string_get_cstr(manifest_path), FA_OPEN_EXISTING | FA_READ));
|
||||
|
||||
uint8_t* manifest_data = malloc(stat.fsize);
|
||||
uint32_t bytes_read = 0;
|
||||
const uint16_t MAX_READ = 0xFFFF;
|
||||
|
||||
do {
|
||||
uint16_t size_read = 0;
|
||||
if(f_read(&file, manifest_data + bytes_read, MAX_READ, &size_read) != FR_OK) {
|
||||
break;
|
||||
}
|
||||
bytes_read += size_read;
|
||||
} while(bytes_read == MAX_READ);
|
||||
|
||||
UpdateManifest* manifest = NULL;
|
||||
do {
|
||||
if(bytes_read != stat.fsize) {
|
||||
break;
|
||||
}
|
||||
|
||||
manifest = update_manifest_alloc();
|
||||
if(!update_manifest_init_mem(manifest, manifest_data, bytes_read)) {
|
||||
update_manifest_free(manifest);
|
||||
manifest = NULL;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
string_clear(manifest_path);
|
||||
free(manifest_data);
|
||||
return manifest;
|
||||
}
|
||||
|
||||
void flipper_boot_update_exec() {
|
||||
if(!flipper_update_init()) {
|
||||
return;
|
||||
}
|
||||
|
||||
string_t work_dir;
|
||||
string_init(work_dir);
|
||||
do {
|
||||
if(!flipper_update_get_work_directory(work_dir)) {
|
||||
break;
|
||||
}
|
||||
|
||||
UpdateManifest* manifest = flipper_update_process_manifest(work_dir);
|
||||
if(!manifest) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(!flipper_update_load_stage(work_dir, manifest)) {
|
||||
update_manifest_free(manifest);
|
||||
}
|
||||
} while(false);
|
||||
string_clear(work_dir);
|
||||
free(pfs);
|
||||
}
|
Reference in New Issue
Block a user