unleashed-firmware/firmware/targets/f7/Src/update.c
hedger e02040107b
[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>
2022-04-13 23:50:25 +03:00

196 lines
5.1 KiB
C

#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);
}