[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:
hedger
2022-04-13 23:50:25 +03:00
committed by GitHub
parent a25552eb99
commit e02040107b
221 changed files with 4199 additions and 11704 deletions
+1
View File
@@ -34,6 +34,7 @@
- `system` - System settings, tools and API
- `tests` - Unit tests and etc
- `u2f` - U2F Application
- `updater` - Update service & application
- `application.c` - Firmware application list source
- `application.h` - Firmware application list header
+3 -33
View File
@@ -116,9 +116,10 @@ static DialogMessageButton fw_version_screen(DialogsApp* dialogs, DialogMessage*
} else {
string_cat_printf(
buffer,
"%s [%s]\n%s [%s]\n[%d] %s",
"%s [%s]\n%s%s [%s]\n[%d] %s",
version_get_version(ver),
version_get_builddate(ver),
version_get_dirty_flag(ver) ? "[!] " : "",
version_get_githash(ver),
version_get_gitbranchnum(ver),
version_get_target(ver),
@@ -135,36 +136,6 @@ static DialogMessageButton fw_version_screen(DialogsApp* dialogs, DialogMessage*
return result;
}
static DialogMessageButton bootloader_version_screen(DialogsApp* dialogs, DialogMessage* message) {
DialogMessageButton result;
string_t buffer;
string_init(buffer);
const Version* ver = furi_hal_version_get_bootloader_version();
if(!ver) {
string_cat_printf(buffer, "No info\n");
} else {
string_cat_printf(
buffer,
"%s [%s]\n%s [%s]\n[%d] %s",
version_get_version(ver),
version_get_builddate(ver),
version_get_githash(ver),
version_get_gitbranchnum(ver),
version_get_target(ver),
version_get_gitbranch(ver));
}
dialog_message_set_header(message, "Boot Version info:", 0, 0, AlignLeft, AlignTop);
dialog_message_set_text(message, string_get_cstr(buffer), 0, 13, AlignLeft, AlignTop);
result = dialog_message_show(dialogs, message);
dialog_message_set_text(message, NULL, 0, 0, AlignLeft, AlignTop);
dialog_message_set_header(message, NULL, 0, 0, AlignLeft, AlignTop);
string_clear(buffer);
return result;
}
const AboutDialogScreen about_screens[] = {
product_screen,
compliance_screen,
@@ -172,8 +143,7 @@ const AboutDialogScreen about_screens[] = {
icon1_screen,
icon2_screen,
hw_version_screen,
fw_version_screen,
bootloader_version_screen};
fw_version_screen};
const size_t about_screens_count = sizeof(about_screens) / sizeof(AboutDialogScreen);
+28 -1
View File
@@ -14,6 +14,7 @@ extern int32_t notification_srv(void* p);
extern int32_t power_srv(void* p);
extern int32_t storage_srv(void* p);
extern int32_t desktop_srv(void* p);
extern int32_t updater_srv(void* p);
// Apps
extern int32_t accessor_app(void* p);
@@ -58,6 +59,7 @@ extern void storage_on_system_start();
extern void subghz_on_system_start();
extern void power_on_system_start();
extern void unit_tests_on_system_start();
extern void updater_on_system_start();
// Settings
extern int32_t notification_settings_app(void* p);
@@ -91,6 +93,9 @@ const FlipperApplication FLIPPER_SERVICES[] = {
#endif
#ifdef SRV_DESKTOP
#ifdef SRV_UPDATER
#error SRV_UPDATER and SRV_DESKTOP are mutually exclusive!
#endif
{.app = desktop_srv, .name = "DesktopSrv", .stack_size = 2048, .icon = NULL},
#endif
@@ -117,10 +122,28 @@ const FlipperApplication FLIPPER_SERVICES[] = {
#ifdef SRV_STORAGE
{.app = storage_srv, .name = "StorageSrv", .stack_size = 3072, .icon = NULL},
#endif
#ifdef SRV_UPDATER
#ifdef SRV_DESKTOP
#error SRV_UPDATER and SRV_DESKTOP are mutually exclusive!
#endif
{.app = updater_srv, .name = "UpdaterSrv", .stack_size = 2048, .icon = NULL},
#endif
};
const size_t FLIPPER_SERVICES_COUNT = sizeof(FLIPPER_SERVICES) / sizeof(FlipperApplication);
const FlipperApplication FLIPPER_SYSTEM_APPS[] = {
#ifdef APP_UPDATER
#ifdef SRV_UPDATER
#error APP_UPDATER and SRV_UPDATER are mutually exclusive!
#endif
{.app = updater_srv, .name = "UpdaterApp", .stack_size = 2048, .icon = NULL},
#endif
};
const size_t FLIPPER_SYSTEM_APPS_COUNT = sizeof(FLIPPER_SERVICES) / sizeof(FlipperApplication);
// Main menu APP
const FlipperApplication FLIPPER_APPS[] = {
@@ -199,6 +222,10 @@ const FlipperOnStartHook FLIPPER_ON_SYSTEM_START[] = {
#ifdef APP_UNIT_TESTS
unit_tests_on_system_start,
#endif
#ifdef APP_UPDATER
updater_on_system_start,
#endif
};
const size_t FLIPPER_ON_SYSTEM_START_COUNT =
@@ -326,4 +353,4 @@ const FlipperApplication FLIPPER_SETTINGS_APPS[] = {
};
const size_t FLIPPER_SETTINGS_APPS_COUNT =
sizeof(FLIPPER_SETTINGS_APPS) / sizeof(FlipperApplication);
sizeof(FLIPPER_SETTINGS_APPS) / sizeof(FlipperApplication);
+7 -1
View File
@@ -42,6 +42,12 @@ extern const size_t FLIPPER_PLUGINS_COUNT;
extern const FlipperApplication FLIPPER_DEBUG_APPS[];
extern const size_t FLIPPER_DEBUG_APPS_COUNT;
/* System apps
* Can only be spawned by loader by name
*/
extern const FlipperApplication FLIPPER_SYSTEM_APPS[];
extern const size_t FLIPPER_SYSTEM_APPS_COUNT;
/* Seperate scene app holder
* Spawned by loader
*/
@@ -55,4 +61,4 @@ extern const FlipperApplication FLIPPER_ARCHIVE;
* Spawned by loader
*/
extern const FlipperApplication FLIPPER_SETTINGS_APPS[];
extern const size_t FLIPPER_SETTINGS_APPS_COUNT;
extern const size_t FLIPPER_SETTINGS_APPS_COUNT;
+29 -2
View File
@@ -5,6 +5,16 @@ CFLAGS += -I$(APP_DIR)
C_SOURCES += $(shell find $(APP_DIR) -name "*.c")
CPP_SOURCES += $(shell find $(APP_DIR) -name "*.cpp")
RAM_EXEC ?= 0
ifeq ($(RAM_EXEC), 1)
APP_RELEASE = 0
SRV_GUI = 1
SRV_INPUT = 1
SRV_NOTIFICATION = 1
SRV_STORAGE = 1
SRV_UPDATER = 1
APP_UPDATER = 0
endif
APP_RELEASE ?= 1
ifeq ($(APP_RELEASE), 1)
@@ -18,13 +28,13 @@ SRV_INPUT = 1
SRV_LOADER = 1
SRV_NOTIFICATION = 1
SRV_POWER = 1
SRV_RPC = 1
SRV_RPC = 1
SRV_STORAGE = 1
# Apps
SRV_DESKTOP = 1
APP_ARCHIVE = 1
APP_GPIO = 1
APP_GPIO = 1
APP_IBUTTON = 1
APP_INFRARED = 1
APP_LF_RFID = 1
@@ -32,6 +42,7 @@ APP_NFC = 1
APP_SUBGHZ = 1
APP_ABOUT = 1
APP_PASSPORT = 1
APP_UPDATER = 1
# Plugins
APP_MUSIC_PLAYER = 1
@@ -223,6 +234,14 @@ CFLAGS += -DAPP_IBUTTON
SRV_GUI = 1
endif
APP_UPDATER ?= 0
ifeq ($(APP_UPDATER), 1)
CFLAGS += -DAPP_UPDATER
SRV_GUI = 1
SRV_STORAGE = 1
SRV_NOTIFICATION = 1
SRV_INPUT = 1
endif
# Services
# that will start with OS
@@ -245,6 +264,14 @@ SRV_GUI = 1
endif
SRV_UPDATER ?= 0
ifeq ($(SRV_UPDATER), 1)
CFLAGS += -DSRV_UPDATER
SRV_STORAGE = 1
SRV_GUI = 1
endif
SRV_DOLPHIN ?= 0
ifeq ($(SRV_DOLPHIN), 1)
CFLAGS += -DSRV_DOLPHIN
@@ -25,6 +25,7 @@ static const char* known_ext[] = {
[ArchiveFileTypeInfrared] = ".ir",
[ArchiveFileTypeBadUsb] = ".txt",
[ArchiveFileTypeU2f] = "?",
[ArchiveFileTypeUpdateManifest] = ".fuf",
[ArchiveFileTypeFolder] = "?",
[ArchiveFileTypeUnknown] = "*",
};
@@ -10,6 +10,7 @@ typedef enum {
ArchiveFileTypeInfrared,
ArchiveFileTypeBadUsb,
ArchiveFileTypeU2f,
ArchiveFileTypeUpdateManifest,
ArchiveFileTypeFolder,
ArchiveFileTypeUnknown,
ArchiveFileTypeLoading,
@@ -15,6 +15,7 @@ static const char* flipper_app_name[] = {
[ArchiveFileTypeInfrared] = "Infrared",
[ArchiveFileTypeBadUsb] = "Bad USB",
[ArchiveFileTypeU2f] = "U2F",
[ArchiveFileTypeUpdateManifest] = "UpdaterApp",
};
static void archive_run_in_app(ArchiveBrowserView* browser, ArchiveFile_t* selected) {
@@ -12,7 +12,8 @@ static const char* ArchiveTabNames[] = {
[ArchiveTabInfrared] = "Infrared",
[ArchiveTabBadUsb] = "Bad USB",
[ArchiveTabU2f] = "U2F",
[ArchiveTabBrowser] = "Browser"};
[ArchiveTabBrowser] = "Browser",
};
static const Icon* ArchiveItemIcons[] = {
[ArchiveFileTypeIButton] = &I_ibutt_10px,
@@ -22,6 +23,7 @@ static const Icon* ArchiveItemIcons[] = {
[ArchiveFileTypeInfrared] = &I_ir_10px,
[ArchiveFileTypeBadUsb] = &I_badusb_10px,
[ArchiveFileTypeU2f] = &I_u2f_10px,
[ArchiveFileTypeUpdateManifest] = &I_update_10px,
[ArchiveFileTypeFolder] = &I_dir_10px,
[ArchiveFileTypeUnknown] = &I_unknown_10px,
[ArchiveFileTypeLoading] = &I_unknown_10px, // TODO loading icon
@@ -374,4 +376,4 @@ void browser_free(ArchiveBrowserView* browser) {
view_free(browser->view);
free(browser);
}
}
+3 -1
View File
@@ -244,5 +244,7 @@ void bt_on_system_start() {
cli_add_command(cli, "bt", CliCommandFlagDefault, bt_cli, NULL);
furi_record_close("bt");
furi_record_close("cli");
#else
UNUSED(bt_cli);
#endif
}
}
+2 -1
View File
@@ -95,10 +95,11 @@ void cli_motd() {
const Version* firmware_version = furi_hal_version_get_firmware_version();
if(firmware_version) {
printf(
"Firmware version: %s %s (%s built on %s)\r\n",
"Firmware version: %s %s (%s%s built on %s)\r\n",
version_get_gitbranch(firmware_version),
version_get_version(firmware_version),
version_get_githash(firmware_version),
version_get_dirty_flag(firmware_version) ? "-dirty" : "",
version_get_builddate(firmware_version));
}
}
+1
View File
@@ -327,6 +327,7 @@ void cli_command_ps(Cli* cli, string_t args, void* context) {
void cli_command_free(Cli* cli, string_t args, void* context) {
printf("Free heap size: %d\r\n", memmgr_get_free_heap());
printf("Total heap size: %d\r\n", memmgr_get_total_heap());
printf("Minimum heap size: %d\r\n", memmgr_get_minimum_free_heap());
printf("Maximum heap block: %d\r\n", memmgr_heap_get_max_free_block());
}
+3 -1
View File
@@ -276,7 +276,7 @@ void crypto_cli_store_key(Cli* cli, string_t args) {
string_clear(key_type);
}
void crypto_cli(Cli* cli, string_t args, void* context) {
static void crypto_cli(Cli* cli, string_t args, void* context) {
string_t cmd;
string_init(cmd);
@@ -317,5 +317,7 @@ void crypto_on_system_start() {
Cli* cli = furi_record_open("cli");
cli_add_command(cli, "crypto", CliCommandFlagDefault, crypto_cli, NULL);
furi_record_close("cli");
#else
UNUSED(crypto_cli);
#endif
}
@@ -23,7 +23,7 @@ void desktop_debug_render(Canvas* canvas, void* model) {
const Version* ver;
char buffer[64];
static const char* headers[] = {"FW Version info:", "Boot Version info:", "Dolphin info:"};
static const char* headers[] = {"FW Version info:", "Dolphin info:"};
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
@@ -44,8 +44,7 @@ void desktop_debug_render(Canvas* canvas, void* model) {
my_name ? my_name : "Unknown");
canvas_draw_str(canvas, 5, 19 + STATUS_BAR_Y_SHIFT, buffer);
ver = m->screen == DesktopViewStatsBoot ? furi_hal_version_get_bootloader_version() :
furi_hal_version_get_firmware_version();
ver = furi_hal_version_get_firmware_version();
if(!ver) {
canvas_draw_str(canvas, 5, 29 + STATUS_BAR_Y_SHIFT, "No info");
@@ -63,7 +62,8 @@ void desktop_debug_render(Canvas* canvas, void* model) {
snprintf(
buffer,
sizeof(buffer),
"%s [%s]",
"%s%s [%s]",
version_get_dirty_flag(ver) ? "[!] " : "",
version_get_githash(ver),
version_get_gitbranchnum(ver));
canvas_draw_str(canvas, 5, 39 + STATUS_BAR_Y_SHIFT, buffer);
@@ -11,7 +11,6 @@ typedef void (*DesktopDebugViewCallback)(DesktopEvent event, void* context);
// Debug info
typedef enum {
DesktopViewStatsFw,
DesktopViewStatsBoot,
DesktopViewStatsMeta,
DesktopViewStatsTotalCount,
} DesktopViewStatsScreens;
+1 -1
View File
@@ -739,4 +739,4 @@ void elements_text_box(
}
}
canvas_set_font(canvas, FontSecondary);
}
}
+8 -5
View File
@@ -6,8 +6,8 @@
#include <one_wire/ibutton/ibutton_worker.h>
#include <one_wire/one_wire_host.h>
void ibutton_cli(Cli* cli, string_t args, void* context);
void onewire_cli(Cli* cli, string_t args, void* context);
static void ibutton_cli(Cli* cli, string_t args, void* context);
static void onewire_cli(Cli* cli, string_t args, void* context);
// app cli function
void ibutton_on_system_start() {
@@ -16,6 +16,9 @@ void ibutton_on_system_start() {
cli_add_command(cli, "ikey", CliCommandFlagDefault, ibutton_cli, cli);
cli_add_command(cli, "onewire", CliCommandFlagDefault, onewire_cli, cli);
furi_record_close("cli");
#else
UNUSED(ibutton_cli);
UNUSED(onewire_cli);
#endif
}
@@ -236,7 +239,7 @@ void ibutton_cli_emulate(Cli* cli, string_t args) {
ibutton_key_free(key);
};
void ibutton_cli(Cli* cli, string_t args, void* context) {
static void ibutton_cli(Cli* cli, string_t args, void* context) {
string_t cmd;
string_init(cmd);
@@ -264,7 +267,7 @@ void onewire_cli_print_usage() {
printf("onewire search\r\n");
};
void onewire_cli_search(Cli* cli) {
static void onewire_cli_search(Cli* cli) {
OneWireHost* onewire = onewire_host_alloc();
uint8_t address[8];
bool done = false;
@@ -308,4 +311,4 @@ void onewire_cli(Cli* cli, string_t args, void* context) {
}
string_clear(cmd);
}
}
@@ -192,5 +192,7 @@ extern "C" void infrared_on_system_start() {
Cli* cli = (Cli*)furi_record_open("cli");
cli_add_command(cli, "ir", CliCommandFlagDefault, infrared_cli_start_ir, NULL);
furi_record_close("cli");
#else
UNUSED(infrared_cli_start_ir);
#endif
}
@@ -13,7 +13,7 @@
#include <storage/storage.h>
#include "infrared_app.h"
static const std::string default_remote_name = "remote";
static const char* default_remote_name = "remote";
std::string InfraredAppRemoteManager::make_full_name(
const std::string& path,
+1 -1
View File
@@ -50,7 +50,7 @@ static void input_cli_send_print_usage() {
printf("\t\t <type>\t - one of 'press', 'release', 'short', 'long'\r\n");
}
void input_cli_send(Cli* cli, string_t args, Input* input) {
static void input_cli_send(Cli* cli, string_t args, Input* input) {
InputEvent event;
string_t key_str;
string_init(key_str);
+8 -6
View File
@@ -7,7 +7,7 @@
#include "helpers/rfid_reader.h"
#include "helpers/rfid_timer_emulator.h"
void lfrfid_cli(Cli* cli, string_t args, void* context);
static void lfrfid_cli(Cli* cli, string_t args, void* context);
// app cli function
extern "C" void lfrfid_on_system_start() {
@@ -15,6 +15,8 @@ extern "C" void lfrfid_on_system_start() {
Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
cli_add_command(cli, "rfid", CliCommandFlagDefault, lfrfid_cli, NULL);
furi_record_close("cli");
#else
UNUSED(lfrfid_cli);
#endif
}
@@ -29,7 +31,7 @@ void lfrfid_cli_print_usage() {
printf("\t<key_data> are hex-formatted\r\n");
};
bool lfrfid_cli_get_key_type(string_t data, LfrfidKeyType* type) {
static bool lfrfid_cli_get_key_type(string_t data, LfrfidKeyType* type) {
bool result = false;
if(string_cmp_str(data, "EM4100") == 0 || string_cmp_str(data, "EM-Marin") == 0) {
@@ -46,7 +48,7 @@ bool lfrfid_cli_get_key_type(string_t data, LfrfidKeyType* type) {
return result;
}
void lfrfid_cli_read(Cli* cli, string_t args) {
static void lfrfid_cli_read(Cli* cli, string_t args) {
RfidReader reader;
string_t type_string;
string_init(type_string);
@@ -97,12 +99,12 @@ void lfrfid_cli_read(Cli* cli, string_t args) {
string_clear(type_string);
}
void lfrfid_cli_write(Cli* cli, string_t args) {
static void lfrfid_cli_write(Cli* cli, string_t args) {
// TODO implement rfid write
printf("Not implemented :(\r\n");
}
void lfrfid_cli_emulate(Cli* cli, string_t args) {
static void lfrfid_cli_emulate(Cli* cli, string_t args) {
string_t data;
string_init(data);
RfidTimerEmulator emulator;
@@ -144,7 +146,7 @@ void lfrfid_cli_emulate(Cli* cli, string_t args) {
string_clear(data);
}
void lfrfid_cli(Cli* cli, string_t args, void* context) {
static void lfrfid_cli(Cli* cli, string_t args, void* context) {
string_t cmd;
string_init(cmd);
+29 -24
View File
@@ -65,33 +65,36 @@ static void loader_cli_print_usage() {
printf("\topen <Application Name:string>\t - Open application by name\r\n");
}
static FlipperApplication const* loader_find_application_by_name_in_list(
const char* name,
const FlipperApplication* list,
const uint32_t n_apps) {
for(size_t i = 0; i < n_apps; i++) {
if(strcmp(name, list[i].name) == 0) {
return &list[i];
}
}
return NULL;
}
const FlipperApplication* loader_find_application_by_name(const char* name) {
const FlipperApplication* application = NULL;
for(size_t i = 0; i < FLIPPER_APPS_COUNT; i++) {
if(strcmp(name, FLIPPER_APPS[i].name) == 0) {
application = &FLIPPER_APPS[i];
}
application = loader_find_application_by_name_in_list(name, FLIPPER_APPS, FLIPPER_APPS_COUNT);
if(!application) {
application =
loader_find_application_by_name_in_list(name, FLIPPER_PLUGINS, FLIPPER_PLUGINS_COUNT);
}
for(size_t i = 0; i < FLIPPER_PLUGINS_COUNT; i++) {
if(strcmp(name, FLIPPER_PLUGINS[i].name) == 0) {
application = &FLIPPER_PLUGINS[i];
}
if(!application) {
application = loader_find_application_by_name_in_list(
name, FLIPPER_SETTINGS_APPS, FLIPPER_SETTINGS_APPS_COUNT);
}
for(size_t i = 0; i < FLIPPER_SETTINGS_APPS_COUNT; i++) {
if(strcmp(name, FLIPPER_SETTINGS_APPS[i].name) == 0) {
application = &FLIPPER_SETTINGS_APPS[i];
}
if(!application) {
application = loader_find_application_by_name_in_list(
name, FLIPPER_SYSTEM_APPS, FLIPPER_SYSTEM_APPS_COUNT);
}
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
for(size_t i = 0; i < FLIPPER_DEBUG_APPS_COUNT; i++) {
if(strcmp(name, FLIPPER_DEBUG_APPS[i].name) == 0) {
application = &FLIPPER_DEBUG_APPS[i];
}
}
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) && !application) {
application = loader_find_application_by_name_in_list(
name, FLIPPER_DEBUG_APPS, FLIPPER_DEBUG_APPS_COUNT);
}
return application;
@@ -149,7 +152,7 @@ void loader_cli_list(Cli* cli, string_t args, Loader* instance) {
}
}
void loader_cli(Cli* cli, string_t args, void* _ctx) {
static void loader_cli(Cli* cli, string_t args, void* _ctx) {
furi_assert(_ctx);
Loader* instance = _ctx;
@@ -290,6 +293,8 @@ static Loader* loader_alloc() {
#ifdef SRV_CLI
instance->cli = furi_record_open("cli");
cli_add_command(instance->cli, "loader", CliCommandFlagDefault, loader_cli, instance);
#else
UNUSED(loader_cli);
#endif
instance->loader_thread = osThreadGetId();
@@ -483,4 +488,4 @@ int32_t loader_srv(void* p) {
FuriPubSub* loader_get_pubsub(Loader* instance) {
return instance->pubsub;
}
}
+2
View File
@@ -136,5 +136,7 @@ void nfc_on_system_start() {
Cli* cli = furi_record_open("cli");
cli_add_command(cli, "nfc", CliCommandFlagDefault, nfc_cli, NULL);
furi_record_close("cli");
#else
UNUSED(nfc_cli);
#endif
}
+2
View File
@@ -128,5 +128,7 @@ void power_on_system_start() {
cli_add_command(cli, "power", CliCommandFlagParallelSafe, power_cli, NULL);
furi_record_close("cli");
#else
UNUSED(power_cli);
#endif
}
+3 -4
View File
@@ -1,8 +1,7 @@
#include "power_i.h"
#include <furi.h>
#include "furi_hal_power.h"
#include "furi_hal_bootloader.h"
#include <furi_hal.h>
void power_off(Power* power) {
furi_hal_power_off();
@@ -15,9 +14,9 @@ void power_off(Power* power) {
void power_reboot(PowerBootMode mode) {
if(mode == PowerBootModeNormal) {
furi_hal_bootloader_set_mode(FuriHalBootloaderModeNormal);
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
} else if(mode == PowerBootModeDfu) {
furi_hal_bootloader_set_mode(FuriHalBootloaderModeDFU);
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeDfu);
}
furi_hal_power_reset();
}
+26 -22
View File
@@ -69,30 +69,34 @@ static void rpc_system_gui_start_screen_stream_process(const PB_Main* request, v
RpcSession* session = rpc_gui->session;
furi_assert(session);
furi_assert(!rpc_gui->is_streaming);
rpc_send_and_release_empty(session, request->command_id, PB_CommandStatus_OK);
if(rpc_gui->is_streaming) {
rpc_send_and_release_empty(
session, request->command_id, PB_CommandStatus_ERROR_VIRTUAL_DISPLAY_ALREADY_STARTED);
} else {
rpc_send_and_release_empty(session, request->command_id, PB_CommandStatus_OK);
rpc_gui->is_streaming = true;
size_t framebuffer_size = gui_get_framebuffer_size(rpc_gui->gui);
// Reusable Frame
rpc_gui->transmit_frame = malloc(sizeof(PB_Main));
rpc_gui->transmit_frame->which_content = PB_Main_gui_screen_frame_tag;
rpc_gui->transmit_frame->command_status = PB_CommandStatus_OK;
rpc_gui->transmit_frame->content.gui_screen_frame.data =
malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(framebuffer_size));
rpc_gui->transmit_frame->content.gui_screen_frame.data->size = framebuffer_size;
// Transmission thread for async TX
rpc_gui->transmit_thread = furi_thread_alloc();
furi_thread_set_name(rpc_gui->transmit_thread, "GuiRpcWorker");
furi_thread_set_callback(
rpc_gui->transmit_thread, rpc_system_gui_screen_stream_frame_transmit_thread);
furi_thread_set_context(rpc_gui->transmit_thread, rpc_gui);
furi_thread_set_stack_size(rpc_gui->transmit_thread, 1024);
furi_thread_start(rpc_gui->transmit_thread);
// GUI framebuffer callback
gui_add_framebuffer_callback(
rpc_gui->gui, rpc_system_gui_screen_stream_frame_callback, context);
rpc_gui->is_streaming = true;
size_t framebuffer_size = gui_get_framebuffer_size(rpc_gui->gui);
// Reusable Frame
rpc_gui->transmit_frame = malloc(sizeof(PB_Main));
rpc_gui->transmit_frame->which_content = PB_Main_gui_screen_frame_tag;
rpc_gui->transmit_frame->command_status = PB_CommandStatus_OK;
rpc_gui->transmit_frame->content.gui_screen_frame.data =
malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(framebuffer_size));
rpc_gui->transmit_frame->content.gui_screen_frame.data->size = framebuffer_size;
// Transmission thread for async TX
rpc_gui->transmit_thread = furi_thread_alloc();
furi_thread_set_name(rpc_gui->transmit_thread, "GuiRpcWorker");
furi_thread_set_callback(
rpc_gui->transmit_thread, rpc_system_gui_screen_stream_frame_transmit_thread);
furi_thread_set_context(rpc_gui->transmit_thread, rpc_gui);
furi_thread_set_stack_size(rpc_gui->transmit_thread, 1024);
furi_thread_start(rpc_gui->transmit_thread);
// GUI framebuffer callback
gui_add_framebuffer_callback(
rpc_gui->gui, rpc_system_gui_screen_stream_frame_callback, context);
}
}
static void rpc_system_gui_stop_screen_stream_process(const PB_Main* request, void* context) {
+57 -2
View File
@@ -10,10 +10,12 @@
#include "storage/storage.h"
#include <stdint.h>
#include <lib/toolbox/md5.h>
#include <update_util/lfs_backup.h>
#define RPC_TAG "RPC_STORAGE"
#define MAX_NAME_LENGTH 255
#define MAX_DATA_SIZE 512
static const size_t MAX_DATA_SIZE = 512;
typedef enum {
RpcStorageStateIdle = 0,
@@ -185,7 +187,7 @@ static void rpc_system_storage_list_root(const PB_Main* request, void* context)
};
furi_assert(COUNT_OF(hard_coded_dirs) < COUNT_OF(response.content.storage_list_response.file));
for(int i = 0; i < COUNT_OF(hard_coded_dirs); ++i) {
for(uint32_t i = 0; i < COUNT_OF(hard_coded_dirs); ++i) {
++response.content.storage_list_response.file_count;
response.content.storage_list_response.file[i].data = NULL;
response.content.storage_list_response.file[i].size = 0;
@@ -538,6 +540,53 @@ static void rpc_system_storage_rename_process(const PB_Main* request, void* cont
rpc_send_and_release_empty(session, request->command_id, status);
}
static void rpc_system_storage_backup_create_process(const PB_Main* request, void* context) {
furi_assert(request);
furi_assert(request->which_content == PB_Main_storage_backup_create_request_tag);
RpcSession* session = (RpcSession*)context;
furi_assert(session);
PB_Main* response = malloc(sizeof(PB_Main));
response->command_id = request->command_id;
response->has_next = false;
Storage* fs_api = furi_record_open("storage");
bool backup_ok =
lfs_backup_create(fs_api, request->content.storage_backup_create_request.archive_path);
response->command_status = backup_ok ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
furi_record_close("storage");
rpc_send_and_release(session, response);
free(response);
}
static void rpc_system_storage_backup_restore_process(const PB_Main* request, void* context) {
furi_assert(request);
furi_assert(request->which_content == PB_Main_storage_backup_restore_request_tag);
RpcSession* session = (RpcSession*)context;
furi_assert(session);
PB_Main* response = malloc(sizeof(PB_Main));
response->command_id = request->command_id;
response->has_next = false;
response->command_status = PB_CommandStatus_OK;
Storage* fs_api = furi_record_open("storage");
bool backup_ok =
lfs_backup_unpack(fs_api, request->content.storage_backup_restore_request.archive_path);
response->command_status = backup_ok ? PB_CommandStatus_OK : PB_CommandStatus_ERROR;
furi_record_close("storage");
rpc_send_and_release(session, response);
free(response);
}
void* rpc_system_storage_alloc(RpcSession* session) {
furi_assert(session);
@@ -579,6 +628,12 @@ void* rpc_system_storage_alloc(RpcSession* session) {
rpc_handler.message_handler = rpc_system_storage_rename_process;
rpc_add_handler(session, PB_Main_storage_rename_request_tag, &rpc_handler);
rpc_handler.message_handler = rpc_system_storage_backup_create_process;
rpc_add_handler(session, PB_Main_storage_backup_create_request_tag, &rpc_handler);
rpc_handler.message_handler = rpc_system_storage_backup_restore_process;
rpc_add_handler(session, PB_Main_storage_backup_restore_request_tag, &rpc_handler);
return rpc_storage;
}
+29 -1
View File
@@ -3,6 +3,7 @@
#include <power/power_service/power.h>
#include <notification/notification_messages.h>
#include <protobuf_version.h>
#include <update_util/update_operation.h>
#include "rpc_i.h"
@@ -242,6 +243,28 @@ static void rpc_system_system_get_power_info_process(const PB_Main* request, voi
free(response);
}
#ifdef APP_UPDATER
static void rpc_system_system_update_request_process(const PB_Main* request, void* context) {
furi_assert(request);
furi_assert(request->which_content == PB_Main_system_update_request_tag);
RpcSession* session = (RpcSession*)context;
furi_assert(session);
bool update_prepare_result =
update_operation_prepare(request->content.system_update_request.update_folder) ==
UpdatePrepareResultOK;
PB_Main* response = malloc(sizeof(PB_Main));
response->command_id = request->command_id;
response->has_next = false;
response->command_status = update_prepare_result ? PB_CommandStatus_OK :
PB_CommandStatus_ERROR_INVALID_PARAMETERS;
rpc_send_and_release(session, response);
free(response);
}
#endif
void* rpc_system_system_alloc(RpcSession* session) {
RpcHandler rpc_handler = {
.message_handler = NULL,
@@ -276,5 +299,10 @@ void* rpc_system_system_alloc(RpcSession* session) {
rpc_handler.message_handler = rpc_system_system_get_power_info_process;
rpc_add_handler(session, PB_Main_system_power_info_request_tag, &rpc_handler);
#ifdef APP_UPDATER
rpc_handler.message_handler = rpc_system_system_update_request_process;
rpc_add_handler(session, PB_Main_system_update_request_tag, &rpc_handler);
#endif
return NULL;
}
}
+2
View File
@@ -40,7 +40,9 @@ Storage* storage_app_alloc() {
storage_data_init(&app->storage[i]);
}
#ifndef FURI_RAM_EXEC
storage_int_init(&app->storage[ST_INT]);
#endif
storage_ext_init(&app->storage[ST_EXT]);
// sd icon gui
+17 -1
View File
@@ -263,6 +263,22 @@ FS_Error storage_sd_info(Storage* api, SDInfo* info);
*/
FS_Error storage_sd_status(Storage* api);
/******************* Internal LFS Functions *******************/
/** Backs up internal storage to a tar archive
* @param api pointer to the api
* @param dstmane destination archive path
* @return FS_Error operation result
*/
FS_Error storage_int_backup(Storage* api, const char* dstname);
/** Restores internal storage from a tar archive
* @param api pointer to the api
* @param dstmane archive path
* @return FS_Error operation result
*/
FS_Error storage_int_restore(Storage* api, const char* dstname);
/***************** Simplified Functions ******************/
/**
@@ -309,4 +325,4 @@ void storage_get_next_filename(
#ifdef __cplusplus
}
#endif
#endif
+3 -1
View File
@@ -521,7 +521,7 @@ void storage_cli(Cli* cli, string_t args, void* context) {
string_clear(cmd);
}
void storage_cli_factory_reset(Cli* cli, string_t args, void* context) {
static void storage_cli_factory_reset(Cli* cli, string_t args, void* context) {
printf("All data will be lost. Are you sure (y/n)?\r\n");
char c = cli_getc(cli);
if(c == 'y' || c == 'Y') {
@@ -540,5 +540,7 @@ void storage_on_system_start() {
cli_add_command(
cli, "factory_reset", CliCommandFlagParallelSafe, storage_cli_factory_reset, NULL);
furi_record_close("cli");
#else
UNUSED(storage_cli_factory_reset);
#endif
}
@@ -0,0 +1,22 @@
#include <furi/record.h>
#include <m-string.h>
#include "storage.h"
#include <toolbox/tar/tar_archive.h>
#define INT_PATH "/int"
FS_Error storage_int_backup(Storage* api, const char* dstname) {
TarArchive* archive = tar_archive_alloc(api);
bool success = tar_archive_open(archive, dstname, TAR_OPEN_MODE_WRITE) &&
tar_archive_add_dir(archive, INT_PATH, "") && tar_archive_finalize(archive);
tar_archive_free(archive);
return success ? FSE_OK : FSE_INTERNAL;
}
FS_Error storage_int_restore(Storage* api, const char* srcname) {
TarArchive* archive = tar_archive_alloc(api);
bool success = tar_archive_open(archive, srcname, TAR_OPEN_MODE_READ) &&
tar_archive_unpack_to(archive, INT_PATH);
tar_archive_free(archive);
return success ? FSE_OK : FSE_INTERNAL;
}
@@ -20,7 +20,11 @@ static StorageData* storage_get_storage_by_type(Storage* app, StorageType type)
}
static bool storage_type_is_not_valid(StorageType type) {
#ifdef FURI_RAM_EXEC
return type != ST_EXT;
#else
return type >= ST_ERROR;
#endif
}
static StorageData* get_storage_by_file(File* file, StorageData* storages) {
+46 -2
View File
@@ -54,10 +54,12 @@ static bool sd_mount_card(StorageData* storage, bool notify) {
SDError status = f_mount(sd_data->fs, sd_data->path, 1);
if(status == FR_OK || status == FR_NO_FILESYSTEM) {
#ifndef FURI_RAM_EXEC
FATFS* fs;
uint32_t free_clusters;
status = f_getfree(sd_data->path, &free_clusters, &fs);
#endif
if(status == FR_OK || status == FR_NO_FILESYSTEM) {
result = true;
@@ -110,6 +112,9 @@ FS_Error sd_unmount_card(StorageData* storage) {
}
FS_Error sd_format_card(StorageData* storage) {
#ifdef FURI_RAM_EXEC
return FSE_NOT_READY;
#else
uint8_t* work_area;
SDData* sd_data = storage->data;
SDError error;
@@ -135,11 +140,14 @@ FS_Error sd_format_card(StorageData* storage) {
storage_data_unlock(storage);
return storage_ext_parse_error(error);
#endif
}
FS_Error sd_card_info(StorageData* storage, SDInfo* sd_info) {
#ifndef FURI_RAM_EXEC
uint32_t free_clusters, free_sectors, total_sectors;
FATFS* fs;
#endif
SDData* sd_data = storage->data;
SDError error;
@@ -150,20 +158,32 @@ FS_Error sd_card_info(StorageData* storage, SDInfo* sd_info) {
storage_data_lock(storage);
error = f_getlabel(sd_data->path, sd_info->label, NULL);
if(error == FR_OK) {
#ifndef FURI_RAM_EXEC
error = f_getfree(sd_data->path, &free_clusters, &fs);
#endif
}
storage_data_unlock(storage);
if(error == FR_OK) {
// calculate size
#ifndef FURI_RAM_EXEC
total_sectors = (fs->n_fatent - 2) * fs->csize;
free_sectors = free_clusters * fs->csize;
#endif
uint16_t sector_size = _MAX_SS;
#if _MAX_SS != _MIN_SS
sector_size = fs->ssize;
#endif
#ifdef FURI_RAM_EXEC
sd_info->fs_type = 0;
sd_info->kb_total = 0;
sd_info->kb_free = 0;
sd_info->cluster_size = 512;
sd_info->sector_size = sector_size;
#else
sd_info->fs_type = fs->fs_type;
switch(fs->fs_type) {
case FS_FAT12:
sd_info->fs_type = FST_FAT12;
@@ -177,7 +197,6 @@ FS_Error sd_card_info(StorageData* storage, SDInfo* sd_info) {
case FS_EXFAT:
sd_info->fs_type = FST_EXFAT;
break;
default:
sd_info->fs_type = FST_UNKNOWN;
break;
@@ -187,6 +206,7 @@ FS_Error sd_card_info(StorageData* storage, SDInfo* sd_info) {
sd_info->kb_free = free_sectors / 1024 * sector_size;
sd_info->cluster_size = fs->csize;
sd_info->sector_size = sector_size;
#endif
}
return storage_ext_parse_error(error);
@@ -328,12 +348,16 @@ static uint16_t
static uint16_t
storage_ext_file_write(void* ctx, File* file, const void* buff, uint16_t const bytes_to_write) {
#ifdef FURI_RAM_EXEC
return FSE_NOT_READY;
#else
StorageData* storage = ctx;
SDFile* file_data = storage_get_storage_file_data(file, storage);
uint16_t bytes_written = 0;
file->internal_error_id = f_write(file_data, buff, bytes_to_write, &bytes_written);
file->error_id = storage_ext_parse_error(file->internal_error_id);
return bytes_written;
#endif
}
static bool
@@ -364,21 +388,29 @@ static uint64_t storage_ext_file_tell(void* ctx, File* file) {
}
static bool storage_ext_file_truncate(void* ctx, File* file) {
#ifdef FURI_RAM_EXEC
return FSE_NOT_READY;
#else
StorageData* storage = ctx;
SDFile* file_data = storage_get_storage_file_data(file, storage);
file->internal_error_id = f_truncate(file_data);
file->error_id = storage_ext_parse_error(file->internal_error_id);
return (file->error_id == FSE_OK);
#endif
}
static bool storage_ext_file_sync(void* ctx, File* file) {
#ifdef FURI_RAM_EXEC
return FSE_NOT_READY;
#else
StorageData* storage = ctx;
SDFile* file_data = storage_get_storage_file_data(file, storage);
file->internal_error_id = f_sync(file_data);
file->error_id = storage_ext_parse_error(file->internal_error_id);
return (file->error_id == FSE_OK);
#endif
}
static uint64_t storage_ext_file_size(void* ctx, File* file) {
@@ -479,13 +511,21 @@ static FS_Error storage_ext_common_stat(void* ctx, const char* path, FileInfo* f
}
static FS_Error storage_ext_common_remove(void* ctx, const char* path) {
#ifdef FURI_RAM_EXEC
return FSE_NOT_READY;
#else
SDError result = f_unlink(path);
return storage_ext_parse_error(result);
#endif
}
static FS_Error storage_ext_common_mkdir(void* ctx, const char* path) {
#ifdef FURI_RAM_EXEC
return FSE_NOT_READY;
#else
SDError result = f_mkdir(path);
return storage_ext_parse_error(result);
#endif
}
static FS_Error storage_ext_common_fs_info(
@@ -493,6 +533,9 @@ static FS_Error storage_ext_common_fs_info(
const char* fs_path,
uint64_t* total_space,
uint64_t* free_space) {
#ifdef FURI_RAM_EXEC
return FSE_NOT_READY;
#else
StorageData* storage = ctx;
SDData* sd_data = storage->data;
@@ -519,6 +562,7 @@ static FS_Error storage_ext_common_fs_info(
}
return storage_ext_parse_error(fresult);
#endif
}
/******************* Init Storage *******************/
@@ -566,4 +610,4 @@ void storage_ext_init(StorageData* storage) {
// do not notify on first launch, notifications app is waiting for our thread to read settings
storage_ext_tick_internal(storage, false);
}
}
+3 -1
View File
@@ -571,7 +571,7 @@ static void subghz_cli_command_chat(Cli* cli, string_t args) {
printf("\r\nExit chat\r\n");
}
void subghz_cli_command(Cli* cli, string_t args, void* context) {
static void subghz_cli_command(Cli* cli, string_t args, void* context) {
string_t cmd;
string_init(cmd);
@@ -630,5 +630,7 @@ void subghz_on_system_start() {
cli_add_command(cli, "subghz", CliCommandFlagDefault, subghz_cli_command, NULL);
furi_record_close("cli");
#else
UNUSED(subghz_cli_command);
#endif
}
+134
View File
@@ -0,0 +1,134 @@
#include <furi.h>
#include <furi_hal.h>
#include <m-string.h>
#include <cli/cli.h>
#include <storage/storage.h>
#include <loader/loader.h>
#include <toolbox/path.h>
#include <toolbox/tar/tar_archive.h>
#include <toolbox/args.h>
#include <update_util/update_manifest.h>
#include <update_util/lfs_backup.h>
#include <update_util/update_operation.h>
typedef void (*cmd_handler)(string_t args);
typedef struct {
const char* command;
const cmd_handler handler;
} CliSubcommand;
static void updater_cli_install(string_t manifest_path) {
printf("Verifying update package at '%s'\r\n", string_get_cstr(manifest_path));
UpdatePrepareResult result = update_operation_prepare(string_get_cstr(manifest_path));
if(result != UpdatePrepareResultOK) {
printf(
"Error: %s. Stopping update.\r\n",
update_operation_describe_preparation_result(result));
return;
}
printf("OK.\r\nRestarting to apply update. BRB\r\n");
osDelay(100);
furi_hal_power_reset();
}
static void updater_cli_backup(string_t args) {
printf("Backup /int to '%s'\r\n", string_get_cstr(args));
Storage* storage = furi_record_open("storage");
bool success = lfs_backup_create(storage, string_get_cstr(args));
furi_record_close("storage");
printf("Result: %s\r\n", success ? "OK" : "FAIL");
}
static void updater_cli_restore(string_t args) {
printf("Restore /int from '%s'\r\n", string_get_cstr(args));
Storage* storage = furi_record_open("storage");
bool success = lfs_backup_unpack(storage, string_get_cstr(args));
furi_record_close("storage");
printf("Result: %s\r\n", success ? "OK" : "FAIL");
}
static void updater_cli_help(string_t args) {
UNUSED(args);
printf("Commands:\r\n"
"\tinstall /ext/update/PACKAGE/update.fuf - verify & apply update package\r\n"
"\tbackup /ext/path/to/backup.tar - create internal storage backup\r\n"
"\trestore /ext/path/to/backup.tar - restore internal storage backup\r\n");
}
static const CliSubcommand update_cli_subcommands[] = {
{.command = "install", .handler = updater_cli_install},
{.command = "backup", .handler = updater_cli_backup},
{.command = "restore", .handler = updater_cli_restore},
{.command = "help", .handler = updater_cli_help},
};
static void updater_cli_ep(Cli* cli, string_t args, void* context) {
string_t subcommand;
string_init(subcommand);
if(!args_read_string_and_trim(args, subcommand) || string_empty_p(args)) {
updater_cli_help(args);
string_clear(subcommand);
return;
}
for(size_t idx = 0; idx < COUNT_OF(update_cli_subcommands); ++idx) {
const CliSubcommand* subcmd_def = &update_cli_subcommands[idx];
if(string_cmp_str(subcommand, subcmd_def->command) == 0) {
string_clear(subcommand);
subcmd_def->handler(args);
return;
}
}
string_clear(subcommand);
updater_cli_help(args);
}
static int32_t updater_spawner_thread_worker(void* arg) {
Loader* loader = furi_record_open("loader");
loader_start(loader, "UpdaterApp", NULL);
furi_record_close("loader");
return 0;
}
static void updater_spawner_thread_cleanup(FuriThreadState state, void* context) {
FuriThread* thread = context;
if(state == FuriThreadStateStopped) {
furi_thread_free(thread);
}
}
static void updater_start_app() {
FuriHalRtcBootMode mode = furi_hal_rtc_get_boot_mode();
if((mode != FuriHalRtcBootModePreUpdate) && (mode != FuriHalRtcBootModePostUpdate)) {
return;
}
/* We need to spawn a separate thread, because these callbacks are executed
* inside loader process, at startup.
* So, accessing its record would cause a deadlock
*/
FuriThread* thread = furi_thread_alloc();
furi_thread_set_name(thread, "UpdateAppSpawner");
furi_thread_set_stack_size(thread, 768);
furi_thread_set_callback(thread, updater_spawner_thread_worker);
furi_thread_set_state_callback(thread, updater_spawner_thread_cleanup);
furi_thread_set_state_context(thread, thread);
furi_thread_start(thread);
}
void updater_on_system_start() {
#ifdef SRV_CLI
Cli* cli = (Cli*)furi_record_open("cli");
cli_add_command(cli, "update", CliCommandFlagDefault, updater_cli_ep, NULL);
furi_record_close("cli");
#else
UNUSED(updater_cli_ep);
#endif
#ifndef FURI_RAM_EXEC
updater_start_app();
#else
UNUSED(updater_start_app);
#endif
}
@@ -0,0 +1,30 @@
#include "updater_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const updater_on_enter_handlers[])(void*) = {
#include "updater_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const updater_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "updater_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const updater_on_exit_handlers[])(void* context) = {
#include "updater_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers updater_scene_handlers = {
.on_enter_handlers = updater_on_enter_handlers,
.on_event_handlers = updater_on_event_handlers,
.on_exit_handlers = updater_on_exit_handlers,
.scene_num = UpdaterSceneNum,
};
@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) UpdaterScene##id,
typedef enum {
#include "updater_scene_config.h"
UpdaterSceneNum,
} UpdaterScene;
#undef ADD_SCENE
extern const SceneManagerHandlers updater_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "updater_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "updater_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "updater_scene_config.h"
#undef ADD_SCENE
@@ -0,0 +1,5 @@
ADD_SCENE(updater, main, Main)
#ifndef FURI_RAM_EXEC
ADD_SCENE(updater, loadcfg, LoadCfg)
ADD_SCENE(updater, error, Error)
#endif
@@ -0,0 +1,65 @@
#include "updater/updater_i.h"
#include "updater_scene.h"
#include <update_util/update_operation.h>
void updater_scene_error_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context);
Updater* updater = context;
if(type != InputTypeShort) {
return;
}
if(result == GuiButtonTypeLeft) {
view_dispatcher_send_custom_event(
updater->view_dispatcher, UpdaterCustomEventCancelUpdate);
}
}
void updater_scene_error_on_enter(void* context) {
Updater* updater = (Updater*)context;
widget_add_button_element(
updater->widget, GuiButtonTypeLeft, "Exit", updater_scene_error_callback, updater);
widget_add_string_multiline_element(
updater->widget, 64, 13, AlignCenter, AlignCenter, FontPrimary, "Error");
widget_add_string_multiline_element(
updater->widget,
64,
33,
AlignCenter,
AlignCenter,
FontPrimary,
update_operation_describe_preparation_result(updater->preparation_result));
view_dispatcher_switch_to_view(updater->view_dispatcher, UpdaterViewWidget);
}
bool updater_scene_error_on_event(void* context, SceneManagerEvent event) {
Updater* updater = (Updater*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
view_dispatcher_stop(updater->view_dispatcher);
consumed = true;
} else if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case UpdaterCustomEventCancelUpdate:
view_dispatcher_stop(updater->view_dispatcher);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void updater_scene_error_on_exit(void* context) {
Updater* updater = (Updater*)context;
widget_reset(updater->widget);
free(updater->pending_update);
}
@@ -0,0 +1,105 @@
#include "updater/updater_i.h"
#include "updater_scene.h"
#include <update_util/update_operation.h>
#include <furi_hal.h>
void updater_scene_loadcfg_apply_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context);
Updater* updater = context;
if(type != InputTypeShort) {
return;
}
if(result == GuiButtonTypeRight) {
view_dispatcher_send_custom_event(updater->view_dispatcher, UpdaterCustomEventStartUpdate);
} else if(result == GuiButtonTypeLeft) {
view_dispatcher_send_custom_event(
updater->view_dispatcher, UpdaterCustomEventCancelUpdate);
}
}
void updater_scene_loadcfg_on_enter(void* context) {
Updater* updater = (Updater*)context;
UpdaterManifestProcessingState* pending_upd = updater->pending_update =
malloc(sizeof(UpdaterManifestProcessingState));
pending_upd->manifest = update_manifest_alloc();
if(update_manifest_init(pending_upd->manifest, string_get_cstr(updater->startup_arg))) {
widget_add_string_element(
updater->widget, 64, 12, AlignCenter, AlignCenter, FontPrimary, "Update");
widget_add_string_multiline_element(
updater->widget,
64,
32,
AlignCenter,
AlignCenter,
FontSecondary,
string_get_cstr(pending_upd->manifest->version));
widget_add_button_element(
updater->widget,
GuiButtonTypeRight,
"Install",
updater_scene_loadcfg_apply_callback,
updater);
} else {
widget_add_string_element(
updater->widget, 64, 24, AlignCenter, AlignCenter, FontPrimary, "Invalid manifest");
}
widget_add_button_element(
updater->widget,
GuiButtonTypeLeft,
"Cancel",
updater_scene_loadcfg_apply_callback,
updater);
view_dispatcher_switch_to_view(updater->view_dispatcher, UpdaterViewWidget);
}
bool updater_scene_loadcfg_on_event(void* context, SceneManagerEvent event) {
Updater* updater = (Updater*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
view_dispatcher_stop(updater->view_dispatcher);
consumed = true;
} else if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case UpdaterCustomEventStartUpdate:
updater->preparation_result =
update_operation_prepare(string_get_cstr(updater->startup_arg));
if(updater->preparation_result == UpdatePrepareResultOK) {
furi_hal_power_reset();
} else {
#ifndef FURI_RAM_EXEC
scene_manager_next_scene(updater->scene_manager, UpdaterSceneError);
#endif
}
consumed = true;
break;
case UpdaterCustomEventCancelUpdate:
view_dispatcher_stop(updater->view_dispatcher);
consumed = true;
break;
default:
break;
}
}
return consumed;
}
void updater_scene_loadcfg_on_exit(void* context) {
Updater* updater = (Updater*)context;
if(updater->pending_update) {
update_manifest_free(updater->pending_update->manifest);
string_clear(updater->pending_update->message);
}
widget_reset(updater->widget);
free(updater->pending_update);
}
@@ -0,0 +1,106 @@
#include <furi.h>
#include <furi_hal.h>
#include <applications.h>
#include <storage/storage.h>
#include "updater/updater_i.h"
#include "updater/views/updater_main.h"
#include "updater_scene.h"
static void sd_mount_callback(const void* message, void* context) {
Updater* updater = context;
const StorageEvent* new_event = message;
switch(new_event->type) {
case StorageEventTypeCardMount:
view_dispatcher_send_custom_event(updater->view_dispatcher, UpdaterCustomEventStartUpdate);
break;
case StorageEventTypeCardUnmount:
view_dispatcher_send_custom_event(updater->view_dispatcher, UpdaterCustomEventSdUnmounted);
break;
default:
break;
}
}
void updater_scene_main_on_enter(void* context) {
Updater* updater = (Updater*)context;
UpdaterMainView* main_view = updater->main_view;
FuriPubSubSubscription* sub =
furi_pubsub_subscribe(storage_get_pubsub(updater->storage), &sd_mount_callback, updater);
updater_main_set_storage_pubsub(main_view, sub);
/* FIXME: there's a misbehavior in storage subsystem. If we produce heavy load on it before it
* fires an SD card event, it'll never do that until the load is lifted. Meanwhile SD card icon
* will be missing from UI, however, /ext will be fully operational. So, until it's fixed, this
* should remain commented out. */
// If (somehow) we started after SD card is mounted, initiate update immediately
//if(storage_sd_status(updater->storage) == FSE_OK) {
// view_dispatcher_send_custom_event(updater->view_dispatcher, UpdaterCustomEventStartUpdate);
//}
updater_main_set_view_dispatcher(main_view, updater->view_dispatcher);
view_dispatcher_switch_to_view(updater->view_dispatcher, UpdaterViewMain);
}
static void updater_scene_restart_to_postupdate() {
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePostUpdate);
furi_hal_power_reset();
}
bool updater_scene_main_on_event(void* context, SceneManagerEvent event) {
Updater* updater = (Updater*)context;
bool consumed = false;
if(event.type == SceneManagerEventTypeTick) {
if(!update_task_is_running(updater->update_task)) {
if(updater->idle_ticks++ >= (UPDATE_DELAY_OPERATION_ERROR / UPDATER_APP_TICK)) {
updater_scene_restart_to_postupdate();
}
} else {
updater->idle_ticks = 0;
}
} else if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case UpdaterCustomEventStartUpdate:
if(!update_task_is_running(updater->update_task) &&
update_task_init(updater->update_task)) {
update_task_start(updater->update_task);
}
consumed = true;
break;
case UpdaterCustomEventRetryUpdate:
if(!update_task_is_running(updater->update_task) &&
(update_task_get_state(updater->update_task)->stage != UpdateTaskStageComplete))
update_task_start(updater->update_task);
consumed = true;
break;
case UpdaterCustomEventCancelUpdate:
if(!update_task_is_running(updater->update_task)) {
updater_scene_restart_to_postupdate();
}
consumed = true;
break;
case UpdaterCustomEventSdUnmounted:
// TODO: error out, stop worker (it's probably dead actually)
break;
default:
break;
}
}
return consumed;
}
void updater_scene_main_on_exit(void* context) {
Updater* updater = (Updater*)context;
furi_pubsub_unsubscribe(
storage_get_pubsub(updater->storage), updater_main_get_storage_pubsub(updater->main_view));
scene_manager_set_scene_state(updater->scene_manager, UpdaterSceneMain, 0);
}
+132
View File
@@ -0,0 +1,132 @@
#include "scenes/updater_scene.h"
#include "updater_i.h"
#include <storage/storage.h>
#include <gui/view_dispatcher.h>
#include <furi.h>
#include <furi_hal.h>
#include <portmacro.h>
#include <stdint.h>
static bool updater_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Updater* updater = (Updater*)context;
return scene_manager_handle_custom_event(updater->scene_manager, event);
}
static void updater_tick_event_callback(void* context) {
furi_assert(context);
Updater* app = context;
scene_manager_handle_tick_event(app->scene_manager);
}
static bool updater_back_event_callback(void* context) {
furi_assert(context);
Updater* updater = (Updater*)context;
return scene_manager_handle_back_event(updater->scene_manager);
}
static void status_update_cb(
const char* message,
const uint8_t progress,
const uint8_t idx_stage,
const uint8_t total_stages,
bool failed,
void* context) {
UpdaterMainView* main_view = context;
updater_main_model_set_state(main_view, message, progress, idx_stage, total_stages, failed);
}
Updater* updater_alloc(const char* arg) {
Updater* updater = malloc(sizeof(Updater));
if(arg) {
string_init_set_str(updater->startup_arg, arg);
string_replace_str(updater->startup_arg, "/any/", "/ext/");
} else {
string_init(updater->startup_arg);
}
updater->storage = furi_record_open("storage");
updater->gui = furi_record_open("gui");
updater->view_dispatcher = view_dispatcher_alloc();
updater->scene_manager = scene_manager_alloc(&updater_scene_handlers, updater);
view_dispatcher_enable_queue(updater->view_dispatcher);
view_dispatcher_set_event_callback_context(updater->view_dispatcher, updater);
view_dispatcher_set_custom_event_callback(
updater->view_dispatcher, updater_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
updater->view_dispatcher, updater_back_event_callback);
view_dispatcher_set_tick_event_callback(
updater->view_dispatcher, updater_tick_event_callback, UPDATER_APP_TICK);
view_dispatcher_attach_to_gui(
updater->view_dispatcher,
updater->gui,
arg ? ViewDispatcherTypeFullscreen : ViewDispatcherTypeWindow);
updater->main_view = updater_main_alloc();
view_dispatcher_add_view(
updater->view_dispatcher, UpdaterViewMain, updater_main_get_view(updater->main_view));
#ifndef FURI_RAM_EXEC
updater->widget = widget_alloc();
view_dispatcher_add_view(
updater->view_dispatcher, UpdaterViewWidget, widget_get_view(updater->widget));
#endif
#ifdef FURI_RAM_EXEC
if(true) {
#else
if(!arg) {
#endif
updater->update_task = update_task_alloc();
update_task_set_progress_cb(updater->update_task, status_update_cb, updater->main_view);
scene_manager_next_scene(updater->scene_manager, UpdaterSceneMain);
} else {
#ifndef FURI_RAM_EXEC
scene_manager_next_scene(updater->scene_manager, UpdaterSceneLoadCfg);
#endif
}
return updater;
}
void updater_free(Updater* updater) {
furi_assert(updater);
string_clear(updater->startup_arg);
if(updater->update_task) {
update_task_set_progress_cb(updater->update_task, NULL, NULL);
update_task_free(updater->update_task);
}
view_dispatcher_remove_view(updater->view_dispatcher, UpdaterViewMain);
updater_main_free(updater->main_view);
#ifndef FURI_RAM_EXEC
view_dispatcher_remove_view(updater->view_dispatcher, UpdaterViewWidget);
widget_free(updater->widget);
#endif
view_dispatcher_free(updater->view_dispatcher);
scene_manager_free(updater->scene_manager);
furi_record_close("gui");
furi_record_close("storage");
free(updater);
}
int32_t updater_srv(void* p) {
const char* cfgpath = p;
Updater* updater = updater_alloc(cfgpath);
view_dispatcher_run(updater->view_dispatcher);
updater_free(updater);
return 0;
}
+65
View File
@@ -0,0 +1,65 @@
#pragma once
#include "views/updater_main.h"
#include "util/update_task.h"
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_stack.h>
#include <gui/view_dispatcher.h>
#include <gui/modules/popup.h>
#include <gui/scene_manager.h>
#include <gui/modules/widget.h>
#include <storage/storage.h>
#include <update_util/update_operation.h>
#ifdef __cplusplus
extern "C" {
#endif
#define UPDATER_APP_TICK 500
typedef enum {
UpdaterViewMain,
UpdaterViewWidget,
} UpdaterViewEnum;
typedef enum {
UpdaterCustomEventUnknown,
UpdaterCustomEventStartUpdate,
UpdaterCustomEventRetryUpdate,
UpdaterCustomEventCancelUpdate,
UpdaterCustomEventSdUnmounted,
} UpdaterCustomEvent;
typedef struct UpdaterManifestProcessingState {
UpdateManifest* manifest;
string_t message;
bool ready_to_be_applied;
} UpdaterManifestProcessingState;
typedef struct {
// GUI
Gui* gui;
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;
Storage* storage;
UpdaterMainView* main_view;
UpdaterManifestProcessingState* pending_update;
UpdatePrepareResult preparation_result;
UpdateTask* update_task;
Widget* widget;
string_t startup_arg;
int32_t idle_ticks;
} Updater;
Updater* updater_alloc(const char* arg);
void updater_free(Updater* updater);
#ifdef __cplusplus
}
#endif
+226
View File
@@ -0,0 +1,226 @@
#include "update_task.h"
#include "update_task_i.h"
#include <furi.h>
#include <furi_hal.h>
#include <storage/storage.h>
#include <toolbox/path.h>
#include <update_util/dfu_file.h>
#include <update_util/lfs_backup.h>
#include <update_util/update_operation.h>
static const char* update_task_stage_descr[] = {
[UpdateTaskStageProgress] = "...",
[UpdateTaskStageReadManifest] = "Loading update manifest",
[UpdateTaskStageValidateDFUImage] = "Checking DFU file",
[UpdateTaskStageFlashWrite] = "Writing flash",
[UpdateTaskStageFlashValidate] = "Validating",
[UpdateTaskStageRadioWrite] = "Writing radio stack",
[UpdateTaskStageRadioCommit] = "Applying radio stack",
[UpdateTaskStageLfsBackup] = "Backing up LFS",
[UpdateTaskStageLfsRestore] = "Restoring LFS",
[UpdateTaskStageComplete] = "Complete",
[UpdateTaskStageError] = "Error",
};
static void update_task_set_status(UpdateTask* update_task, const char* status) {
if(!status) {
if(update_task->state.stage >= COUNT_OF(update_task_stage_descr)) {
status = "...";
} else {
status = update_task_stage_descr[update_task->state.stage];
}
}
string_set_str(update_task->state.status, status);
}
void update_task_set_progress(UpdateTask* update_task, UpdateTaskStage stage, uint8_t progress) {
if(stage != UpdateTaskStageProgress) {
update_task->state.stage = stage;
update_task->state.current_stage_idx++;
update_task_set_status(update_task, NULL);
}
if(progress > 100) {
progress = 100;
}
update_task->state.progress = progress;
if(update_task->status_change_cb) {
(update_task->status_change_cb)(
string_get_cstr(update_task->state.status),
progress,
update_task->state.current_stage_idx,
update_task->state.total_stages,
update_task->state.stage == UpdateTaskStageError,
update_task->status_change_cb_state);
}
}
static void update_task_close_file(UpdateTask* update_task) {
furi_assert(update_task);
if(!storage_file_is_open(update_task->file)) {
return;
}
storage_file_close(update_task->file);
}
static bool update_task_check_file_exists(UpdateTask* update_task, string_t filename) {
furi_assert(update_task);
string_t tmp_path;
string_init_set(tmp_path, update_task->update_path);
path_append(tmp_path, string_get_cstr(filename));
bool exists =
(storage_common_stat(update_task->storage, string_get_cstr(tmp_path), NULL) == FSE_OK);
string_clear(tmp_path);
return exists;
}
bool update_task_open_file(UpdateTask* update_task, string_t filename) {
furi_assert(update_task);
update_task_close_file(update_task);
string_t tmp_path;
string_init_set(tmp_path, update_task->update_path);
path_append(tmp_path, string_get_cstr(filename));
bool open_success = storage_file_open(
update_task->file, string_get_cstr(tmp_path), FSAM_READ, FSOM_OPEN_EXISTING);
string_clear(tmp_path);
return open_success;
}
static void update_task_worker_thread_cb(FuriThreadState state, void* context) {
UpdateTask* update_task = context;
if(state != FuriThreadStateStopped) {
return;
}
int32_t op_result = furi_thread_get_return_code(update_task->thread);
if(op_result == UPDATE_TASK_NOERR) {
osDelay(UPDATE_DELAY_OPERATION_OK);
furi_hal_power_reset();
}
}
UpdateTask* update_task_alloc() {
UpdateTask* update_task = malloc(sizeof(UpdateTask));
update_task->state.stage = UpdateTaskStageProgress;
update_task->state.progress = 0;
string_init(update_task->state.status);
update_task->manifest = update_manifest_alloc();
update_task->storage = furi_record_open("storage");
update_task->file = storage_file_alloc(update_task->storage);
update_task->status_change_cb = NULL;
FuriThread* thread = update_task->thread = furi_thread_alloc();
furi_thread_set_name(thread, "UpdateWorker");
furi_thread_set_stack_size(thread, 5120);
furi_thread_set_context(thread, update_task);
furi_thread_set_state_callback(thread, update_task_worker_thread_cb);
furi_thread_set_state_context(thread, update_task);
#ifdef FURI_RAM_EXEC
UNUSED(update_task_worker_backup_restore);
furi_thread_set_callback(thread, update_task_worker_flash_writer);
#else
UNUSED(update_task_worker_flash_writer);
furi_thread_set_callback(thread, update_task_worker_backup_restore);
#endif
return update_task;
}
void update_task_free(UpdateTask* update_task) {
furi_assert(update_task);
furi_thread_join(update_task->thread);
furi_thread_free(update_task->thread);
update_task_close_file(update_task);
storage_file_free(update_task->file);
update_manifest_free(update_task->manifest);
furi_record_close("storage");
string_clear(update_task->update_path);
free(update_task);
}
bool update_task_init(UpdateTask* update_task) {
furi_assert(update_task);
string_init(update_task->update_path);
return true;
}
bool update_task_parse_manifest(UpdateTask* update_task) {
furi_assert(update_task);
update_task_set_progress(update_task, UpdateTaskStageReadManifest, 0);
bool result = false;
string_t manifest_path;
string_init(manifest_path);
do {
update_task_set_progress(update_task, UpdateTaskStageProgress, 10);
if(!update_operation_get_current_package_path(
update_task->storage, update_task->update_path)) {
break;
}
path_concat(
string_get_cstr(update_task->update_path),
UPDATE_MANIFEST_DEFAULT_NAME,
manifest_path);
update_task_set_progress(update_task, UpdateTaskStageProgress, 30);
if(!update_manifest_init(update_task->manifest, string_get_cstr(manifest_path))) {
break;
}
update_task_set_progress(update_task, UpdateTaskStageProgress, 50);
if(!string_empty_p(update_task->manifest->firmware_dfu_image) &&
!update_task_check_file_exists(update_task, update_task->manifest->firmware_dfu_image)) {
break;
}
update_task_set_progress(update_task, UpdateTaskStageProgress, 70);
if(!string_empty_p(update_task->manifest->radio_image) &&
!update_task_check_file_exists(update_task, update_task->manifest->radio_image)) {
break;
}
update_task_set_progress(update_task, UpdateTaskStageProgress, 100);
result = true;
} while(false);
string_clear(manifest_path);
return result;
}
void update_task_set_progress_cb(UpdateTask* update_task, updateProgressCb cb, void* state) {
update_task->status_change_cb = cb;
update_task->status_change_cb_state = state;
}
bool update_task_start(UpdateTask* update_task) {
furi_assert(update_task);
return furi_thread_start(update_task->thread);
}
bool update_task_is_running(UpdateTask* update_task) {
furi_assert(update_task);
return furi_thread_get_state(update_task->thread) == FuriThreadStateRunning;
}
UpdateTaskState const* update_task_get_state(UpdateTask* update_task) {
furi_assert(update_task);
return &update_task->state;
}
UpdateManifest const* update_task_get_manifest(UpdateTask* update_task) {
furi_assert(update_task);
return update_task->manifest;
}
+66
View File
@@ -0,0 +1,66 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
#include <update_util/update_manifest.h>
#include <stdint.h>
#include <stdbool.h>
#include <m-string.h>
#define UPDATE_DELAY_OPERATION_OK 600
#define UPDATE_DELAY_OPERATION_ERROR INT_MAX
typedef enum {
UpdateTaskStageProgress,
UpdateTaskStageReadManifest,
UpdateTaskStageValidateDFUImage,
UpdateTaskStageFlashWrite,
UpdateTaskStageFlashValidate,
UpdateTaskStageRadioWrite,
UpdateTaskStageRadioCommit,
UpdateTaskStageLfsBackup,
UpdateTaskStageLfsRestore,
UpdateTaskStageComplete,
UpdateTaskStageError,
} UpdateTaskStage;
typedef struct {
UpdateTaskStage stage;
uint8_t progress;
uint8_t current_stage_idx;
uint8_t total_stages;
string_t status;
} UpdateTaskState;
typedef struct UpdateTask UpdateTask;
typedef void (*updateProgressCb)(
const char* status,
const uint8_t stage_pct,
const uint8_t idx_stage,
const uint8_t total_stages,
bool failed,
void* state);
UpdateTask* update_task_alloc();
void update_task_free(UpdateTask* update_task);
bool update_task_init(UpdateTask* update_task);
void update_task_set_progress_cb(UpdateTask* update_task, updateProgressCb cb, void* state);
bool update_task_start(UpdateTask* update_task);
bool update_task_is_running(UpdateTask* update_task);
UpdateTaskState const* update_task_get_state(UpdateTask* update_task);
UpdateManifest const* update_task_get_manifest(UpdateTask* update_task);
#ifdef __cplusplus
}
#endif
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include <storage/storage.h>
#define UPDATE_TASK_NOERR 0
#define UPDATE_TASK_FAILED -1
typedef struct UpdateTask {
UpdateTaskState state;
string_t update_path;
UpdateManifest* manifest;
FuriThread* thread;
Storage* storage;
File* file;
updateProgressCb status_change_cb;
void* status_change_cb_state;
} UpdateTask;
void update_task_set_progress(UpdateTask* update_task, UpdateTaskStage stage, uint8_t progress);
bool update_task_parse_manifest(UpdateTask* update_task);
bool update_task_open_file(UpdateTask* update_task, string_t filename);
int32_t update_task_worker_flash_writer(void* context);
int32_t update_task_worker_backup_restore(void* context);
@@ -0,0 +1,148 @@
#include "update_task.h"
#include "update_task_i.h"
#include <furi.h>
#include <furi_hal.h>
#include <storage/storage.h>
#include <toolbox/path.h>
#include <update_util/dfu_file.h>
#include <update_util/lfs_backup.h>
#include <update_util/update_operation.h>
#define CHECK_RESULT(x) \
if(!(x)) { \
break; \
}
#define STM_DFU_VENDOR_ID 0x0483
#define STM_DFU_PRODUCT_ID 0xDF11
/* Written into DFU file by build pipeline */
#define FLIPPER_ZERO_DFU_DEVICE_CODE 0xFFFF
static const DfuValidationParams flipper_dfu_params = {
.device = FLIPPER_ZERO_DFU_DEVICE_CODE,
.product = STM_DFU_PRODUCT_ID,
.vendor = STM_DFU_VENDOR_ID,
};
static void update_task_dfu_progress(const uint8_t progress, void* context) {
UpdateTask* update_task = context;
update_task_set_progress(update_task, UpdateTaskStageProgress, progress);
}
static bool page_task_compare_flash(
const uint8_t i_page,
const uint8_t* update_block,
uint16_t update_block_len) {
const size_t page_addr = furi_hal_flash_get_base() + furi_hal_flash_get_page_size() * i_page;
return (memcmp(update_block, (void*)page_addr, update_block_len) == 0);
}
/* Verifies a flash operation address for fitting into writable memory
*/
static bool check_address_boundaries(const size_t address) {
const size_t min_allowed_address = furi_hal_flash_get_base();
const size_t max_allowed_address = (size_t)furi_hal_flash_get_free_end_address();
return ((address >= min_allowed_address) && (address < max_allowed_address));
}
int32_t update_task_worker_flash_writer(void* context) {
furi_assert(context);
UpdateTask* update_task = context;
bool success = false;
DfuUpdateTask page_task = {
.address_cb = &check_address_boundaries,
.progress_cb = &update_task_dfu_progress,
.task_cb = &furi_hal_flash_program_page,
.context = update_task,
};
update_task->state.current_stage_idx = 0;
update_task->state.total_stages = 4;
do {
CHECK_RESULT(update_task_parse_manifest(update_task));
if(!string_empty_p(update_task->manifest->firmware_dfu_image)) {
update_task_set_progress(update_task, UpdateTaskStageValidateDFUImage, 0);
CHECK_RESULT(
update_task_open_file(update_task, update_task->manifest->firmware_dfu_image));
CHECK_RESULT(
dfu_file_validate_crc(update_task->file, &update_task_dfu_progress, update_task));
const uint8_t valid_targets =
dfu_file_validate_headers(update_task->file, &flipper_dfu_params);
if(valid_targets == 0) {
break;
}
update_task_set_progress(update_task, UpdateTaskStageFlashWrite, 0);
CHECK_RESULT(dfu_file_process_targets(&page_task, update_task->file, valid_targets));
page_task.task_cb = &page_task_compare_flash;
update_task_set_progress(update_task, UpdateTaskStageFlashValidate, 0);
CHECK_RESULT(dfu_file_process_targets(&page_task, update_task->file, valid_targets));
}
update_task_set_progress(update_task, UpdateTaskStageComplete, 100);
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePostUpdate);
success = true;
} while(false);
if(!success) {
update_task_set_progress(update_task, UpdateTaskStageError, update_task->state.progress);
}
return success ? UPDATE_TASK_NOERR : UPDATE_TASK_FAILED;
}
int32_t update_task_worker_backup_restore(void* context) {
furi_assert(context);
UpdateTask* update_task = context;
bool success = false;
FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
// no idea how we got here. Clear to normal boot
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
return UPDATE_TASK_NOERR;
}
update_task->state.current_stage_idx = 0;
update_task->state.total_stages = 1;
if(!update_operation_get_current_package_path(update_task->storage, update_task->update_path)) {
return UPDATE_TASK_FAILED;
}
string_t backup_file_path;
string_init(backup_file_path);
path_concat(
string_get_cstr(update_task->update_path), LFS_BACKUP_DEFAULT_FILENAME, backup_file_path);
if(boot_mode == FuriHalRtcBootModePreUpdate) {
update_task_set_progress(update_task, UpdateTaskStageLfsBackup, 0);
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal); // to avoid bootloops
if((success =
lfs_backup_create(update_task->storage, string_get_cstr(backup_file_path)))) {
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeUpdate);
}
} else if(boot_mode == FuriHalRtcBootModePostUpdate) {
update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
success = lfs_backup_unpack(update_task->storage, string_get_cstr(backup_file_path));
}
if(success) {
update_task_set_progress(update_task, UpdateTaskStageComplete, 100);
} else {
update_task_set_progress(update_task, UpdateTaskStageError, update_task->state.progress);
}
string_clear(backup_file_path);
return success ? UPDATE_TASK_NOERR : UPDATE_TASK_FAILED;
}
+151
View File
@@ -0,0 +1,151 @@
#include <gui/gui_i.h>
#include <gui/view.h>
#include <gui/elements.h>
#include <gui/canvas.h>
#include <furi.h>
#include <input/input.h>
#include "../updater_i.h"
#include "updater_main.h"
struct UpdaterMainView {
View* view;
ViewDispatcher* view_dispatcher;
FuriPubSubSubscription* subscription;
void* context;
};
static const uint8_t PROGRESS_RENDER_STEP = 3; /* percent, to limit rendering rate */
typedef struct {
string_t status;
uint8_t progress, rendered_progress;
uint8_t idx_stage, total_stages;
bool failed;
} UpdaterProgressModel;
void updater_main_model_set_state(
UpdaterMainView* main_view,
const char* message,
uint8_t progress,
uint8_t idx_stage,
uint8_t total_stages,
bool failed) {
with_view_model(
main_view->view, (UpdaterProgressModel * model) {
model->failed = failed;
model->idx_stage = idx_stage;
model->total_stages = total_stages;
model->progress = progress;
if(string_cmp_str(model->status, message)) {
string_set(model->status, message);
model->rendered_progress = progress;
return true;
}
if((model->rendered_progress > progress) ||
((progress - model->rendered_progress) > PROGRESS_RENDER_STEP)) {
model->rendered_progress = progress;
return true;
}
return false;
});
}
View* updater_main_get_view(UpdaterMainView* main_view) {
furi_assert(main_view);
return main_view->view;
}
bool updater_main_input(InputEvent* event, void* context) {
furi_assert(event);
furi_assert(context);
UpdaterMainView* main_view = context;
if(!main_view->view_dispatcher) {
return true;
}
if(event->type != InputTypeShort) {
return true;
}
if(event->key == InputKeyOk) {
view_dispatcher_send_custom_event(
main_view->view_dispatcher, UpdaterCustomEventRetryUpdate);
} else if(event->key == InputKeyBack) {
view_dispatcher_send_custom_event(
main_view->view_dispatcher, UpdaterCustomEventCancelUpdate);
}
return true;
}
static void updater_main_draw_callback(Canvas* canvas, void* _model) {
UpdaterProgressModel* model = _model;
canvas_set_font(canvas, FontPrimary);
uint16_t y_offset = model->failed ? 5 : 13;
string_t status_text;
if(!model->failed && (model->idx_stage != 0) && (model->idx_stage <= model->total_stages)) {
string_init_printf(
status_text,
"[%d/%d] %s",
model->idx_stage,
model->total_stages,
string_get_cstr(model->status));
} else {
string_init_set(status_text, model->status);
}
canvas_draw_str_aligned(
canvas, 128 / 2, y_offset, AlignCenter, AlignTop, string_get_cstr(status_text));
string_clear(status_text);
if(model->failed) {
canvas_set_font(canvas, FontSecondary);
canvas_draw_str_aligned(
canvas, 128 / 2, 20, AlignCenter, AlignTop, "[OK] to retry, [Back] to abort");
}
elements_progress_bar(canvas, 14, 35, 100, (float)model->progress / 100);
}
UpdaterMainView* updater_main_alloc() {
UpdaterMainView* main_view = malloc(sizeof(UpdaterMainView));
main_view->view = view_alloc();
view_allocate_model(main_view->view, ViewModelTypeLocking, sizeof(UpdaterProgressModel));
with_view_model(
main_view->view, (UpdaterProgressModel * model) {
string_init_set(model->status, "Waiting for storage");
return true;
});
view_set_context(main_view->view, main_view);
view_set_input_callback(main_view->view, updater_main_input);
view_set_draw_callback(main_view->view, updater_main_draw_callback);
return main_view;
}
void updater_main_free(UpdaterMainView* main_view) {
furi_assert(main_view);
with_view_model(
main_view->view, (UpdaterProgressModel * model) {
string_clear(model->status);
return false;
});
view_free(main_view->view);
free(main_view);
}
void updater_main_set_storage_pubsub(UpdaterMainView* main_view, FuriPubSubSubscription* sub) {
main_view->subscription = sub;
}
FuriPubSubSubscription* updater_main_get_storage_pubsub(UpdaterMainView* main_view) {
return main_view->subscription;
}
void updater_main_set_view_dispatcher(UpdaterMainView* main_view, ViewDispatcher* view_dispatcher) {
main_view->view_dispatcher = view_dispatcher;
}
+28
View File
@@ -0,0 +1,28 @@
#pragma once
#include <gui/view.h>
typedef struct UpdaterMainView UpdaterMainView;
typedef struct FuriPubSubSubscription FuriPubSubSubscription;
typedef struct ViewDispatcher ViewDispatcher;
typedef void (*UpdaterMainInputCallback)(InputType type, void* context);
View* updater_main_get_view(UpdaterMainView* main_view);
UpdaterMainView* updater_main_alloc();
void updater_main_free(UpdaterMainView* main_view);
void updater_main_model_set_state(
UpdaterMainView* main_view,
const char* message,
uint8_t progress,
uint8_t idx_stage,
uint8_t total_stages,
bool failed);
void updater_main_set_storage_pubsub(UpdaterMainView* main_view, FuriPubSubSubscription* sub);
FuriPubSubSubscription* updater_main_get_storage_pubsub(UpdaterMainView* main_view);
void updater_main_set_view_dispatcher(UpdaterMainView* main_view, ViewDispatcher* view_dispatcher);