[FL-2627] Flipper applications: SDK, build and debug system (#1387)
* Added support for running applications from SD card (FAPs - Flipper Application Packages) * Added plugin_dist target for fbt to build FAPs * All apps of type FlipperAppType.EXTERNAL and FlipperAppType.PLUGIN are built as FAPs by default * Updated VSCode configuration for new fbt features - re-deploy stock configuration to use them * Added debugging support for FAPs with fbt debug & VSCode * Added public firmware API with automated versioning Co-authored-by: hedger <hedger@users.noreply.github.com> Co-authored-by: SG <who.just.the.doctor@gmail.com> Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
39
applications/system/updater/application.fam
Normal file
39
applications/system/updater/application.fam
Normal file
@@ -0,0 +1,39 @@
|
||||
App(
|
||||
appid="updater",
|
||||
name="UpdaterSrv",
|
||||
apptype=FlipperAppType.SERVICE,
|
||||
cdefines=["SRV_UPDATER"],
|
||||
requires=[
|
||||
"gui",
|
||||
"storage",
|
||||
],
|
||||
conflicts=["desktop"],
|
||||
entry_point="updater_srv",
|
||||
stack_size=2 * 1024,
|
||||
order=130,
|
||||
)
|
||||
|
||||
App(
|
||||
appid="updater_app",
|
||||
name="UpdaterApp",
|
||||
apptype=FlipperAppType.SYSTEM,
|
||||
cdefines=["APP_UPDATER"],
|
||||
requires=[
|
||||
"gui",
|
||||
"storage",
|
||||
"bt",
|
||||
],
|
||||
conflicts=["updater"],
|
||||
provides=["updater_start"],
|
||||
entry_point="updater_srv",
|
||||
stack_size=2 * 1024,
|
||||
order=10,
|
||||
)
|
||||
|
||||
App(
|
||||
appid="updater_start",
|
||||
apptype=FlipperAppType.STARTUP,
|
||||
entry_point="updater_on_system_start",
|
||||
requires=["updater_app"],
|
||||
order=110,
|
||||
)
|
137
applications/system/updater/cli/updater_cli.c
Normal file
137
applications/system/updater/cli/updater_cli.c
Normal file
@@ -0,0 +1,137 @@
|
||||
|
||||
#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");
|
||||
furi_delay_ms(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(RECORD_STORAGE);
|
||||
bool success = lfs_backup_create(storage, string_get_cstr(args));
|
||||
furi_record_close(RECORD_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(RECORD_STORAGE);
|
||||
bool success = lfs_backup_unpack(storage, string_get_cstr(args));
|
||||
furi_record_close(RECORD_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/path/to/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) {
|
||||
UNUSED(cli);
|
||||
UNUSED(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) {
|
||||
UNUSED(arg);
|
||||
Loader* loader = furi_record_open(RECORD_LOADER);
|
||||
loader_start(loader, "UpdaterApp", NULL);
|
||||
furi_record_close(RECORD_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(RECORD_CLI);
|
||||
cli_add_command(cli, "update", CliCommandFlagDefault, updater_cli_ep, NULL);
|
||||
furi_record_close(RECORD_CLI);
|
||||
#else
|
||||
UNUSED(updater_cli_ep);
|
||||
#endif
|
||||
#ifndef FURI_RAM_EXEC
|
||||
updater_start_app();
|
||||
#else
|
||||
UNUSED(updater_start_app);
|
||||
#endif
|
||||
}
|
30
applications/system/updater/scenes/updater_scene.c
Normal file
30
applications/system/updater/scenes/updater_scene.c
Normal file
@@ -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,
|
||||
};
|
29
applications/system/updater/scenes/updater_scene.h
Normal file
29
applications/system/updater/scenes/updater_scene.h
Normal file
@@ -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
|
65
applications/system/updater/scenes/updater_scene_error.c
Normal file
65
applications/system/updater/scenes/updater_scene_error.c
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "../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);
|
||||
}
|
107
applications/system/updater/scenes/updater_scene_loadcfg.c
Normal file
107
applications/system/updater/scenes/updater_scene_loadcfg.c
Normal file
@@ -0,0 +1,107 @@
|
||||
#include "../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_text_box_element(
|
||||
updater->widget,
|
||||
5,
|
||||
20,
|
||||
118,
|
||||
32,
|
||||
AlignCenter,
|
||||
AlignCenter,
|
||||
string_get_cstr(pending_upd->manifest->version),
|
||||
true);
|
||||
|
||||
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);
|
||||
}
|
101
applications/system/updater/scenes/updater_scene_main.c
Normal file
101
applications/system/updater/scenes/updater_scene_main.c
Normal file
@@ -0,0 +1,101 @@
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <applications.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#include "../updater_i.h"
|
||||
#include "../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;
|
||||
notification_message(updater->notification, &sequence_display_backlight_enforce_on);
|
||||
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_cancel_update() {
|
||||
update_operation_disarm();
|
||||
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_cancel_update();
|
||||
}
|
||||
} else {
|
||||
updater->idle_ticks = 0;
|
||||
}
|
||||
} else if(event.type == SceneManagerEventTypeCustom) {
|
||||
switch(event.event) {
|
||||
case UpdaterCustomEventStartUpdate:
|
||||
case UpdaterCustomEventRetryUpdate:
|
||||
if(!update_task_is_running(updater->update_task) &&
|
||||
(update_task_get_state(updater->update_task)->stage != UpdateTaskStageCompleted))
|
||||
update_task_start(updater->update_task);
|
||||
consumed = true;
|
||||
break;
|
||||
|
||||
case UpdaterCustomEventCancelUpdate:
|
||||
if(!update_task_is_running(updater->update_task)) {
|
||||
updater_scene_cancel_update();
|
||||
}
|
||||
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;
|
||||
|
||||
notification_message(updater->notification, &sequence_display_backlight_enforce_auto);
|
||||
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);
|
||||
}
|
129
applications/system/updater/updater.c
Normal file
129
applications/system/updater/updater.c
Normal file
@@ -0,0 +1,129 @@
|
||||
#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, bool failed, void* context) {
|
||||
UpdaterMainView* main_view = context;
|
||||
updater_main_model_set_state(main_view, message, progress, failed);
|
||||
}
|
||||
|
||||
Updater* updater_alloc(const char* arg) {
|
||||
Updater* updater = malloc(sizeof(Updater));
|
||||
if(arg && strlen(arg)) {
|
||||
string_init_set_str(updater->startup_arg, arg);
|
||||
string_replace_str(updater->startup_arg, ANY_PATH(""), EXT_PATH(""));
|
||||
} else {
|
||||
string_init(updater->startup_arg);
|
||||
}
|
||||
|
||||
updater->storage = furi_record_open(RECORD_STORAGE);
|
||||
updater->notification = furi_record_open(RECORD_NOTIFICATION);
|
||||
|
||||
updater->gui = furi_record_open(RECORD_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, ViewDispatcherTypeFullscreen);
|
||||
|
||||
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
|
||||
FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
|
||||
if(!arg && ((boot_mode == FuriHalRtcBootModePreUpdate) ||
|
||||
(boot_mode == FuriHalRtcBootModePostUpdate))) {
|
||||
#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(RECORD_GUI);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
furi_record_close(RECORD_NOTIFICATION);
|
||||
|
||||
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;
|
||||
}
|
67
applications/system/updater/updater_i.h
Normal file
67
applications/system/updater/updater_i.h
Normal file
@@ -0,0 +1,67 @@
|
||||
#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 <notification/notification_app.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;
|
||||
NotificationApp* notification;
|
||||
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
|
350
applications/system/updater/util/update_task.c
Normal file
350
applications/system/updater/util/update_task.c
Normal file
@@ -0,0 +1,350 @@
|
||||
#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 flash",
|
||||
[UpdateTaskStageRadioImageValidate] = "Checking radio FW",
|
||||
[UpdateTaskStageRadioErase] = "Uninstalling radio FW",
|
||||
[UpdateTaskStageRadioWrite] = "Writing radio FW",
|
||||
[UpdateTaskStageRadioInstall] = "Installing radio FW",
|
||||
[UpdateTaskStageRadioBusy] = "Radio is updating",
|
||||
[UpdateTaskStageOBValidation] = "Validating opt. bytes",
|
||||
[UpdateTaskStageLfsBackup] = "Backing up LFS",
|
||||
[UpdateTaskStageLfsRestore] = "Restoring LFS",
|
||||
[UpdateTaskStageResourcesUpdate] = "Updating resources",
|
||||
[UpdateTaskStageSplashscreenInstall] = "Installing splashscreen",
|
||||
[UpdateTaskStageCompleted] = "Restarting...",
|
||||
[UpdateTaskStageError] = "Error",
|
||||
[UpdateTaskStageOBError] = "OB, report",
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
UpdateTaskStageGroup group;
|
||||
uint8_t weight;
|
||||
} UpdateTaskStageGroupMap;
|
||||
|
||||
#define STAGE_DEF(GROUP, WEIGHT) \
|
||||
{ .group = (GROUP), .weight = (WEIGHT), }
|
||||
|
||||
static const UpdateTaskStageGroupMap update_task_stage_progress[] = {
|
||||
[UpdateTaskStageProgress] = STAGE_DEF(UpdateTaskStageGroupMisc, 0),
|
||||
|
||||
[UpdateTaskStageReadManifest] = STAGE_DEF(UpdateTaskStageGroupPreUpdate, 5),
|
||||
[UpdateTaskStageLfsBackup] = STAGE_DEF(UpdateTaskStageGroupPreUpdate, 15),
|
||||
|
||||
[UpdateTaskStageRadioImageValidate] = STAGE_DEF(UpdateTaskStageGroupRadio, 15),
|
||||
[UpdateTaskStageRadioErase] = STAGE_DEF(UpdateTaskStageGroupRadio, 60),
|
||||
[UpdateTaskStageRadioWrite] = STAGE_DEF(UpdateTaskStageGroupRadio, 80),
|
||||
[UpdateTaskStageRadioInstall] = STAGE_DEF(UpdateTaskStageGroupRadio, 60),
|
||||
[UpdateTaskStageRadioBusy] = STAGE_DEF(UpdateTaskStageGroupRadio, 80),
|
||||
|
||||
[UpdateTaskStageOBValidation] = STAGE_DEF(UpdateTaskStageGroupOptionBytes, 10),
|
||||
|
||||
[UpdateTaskStageValidateDFUImage] = STAGE_DEF(UpdateTaskStageGroupFirmware, 50),
|
||||
[UpdateTaskStageFlashWrite] = STAGE_DEF(UpdateTaskStageGroupFirmware, 200),
|
||||
[UpdateTaskStageFlashValidate] = STAGE_DEF(UpdateTaskStageGroupFirmware, 30),
|
||||
|
||||
[UpdateTaskStageLfsRestore] = STAGE_DEF(UpdateTaskStageGroupPostUpdate, 30),
|
||||
|
||||
[UpdateTaskStageResourcesUpdate] = STAGE_DEF(UpdateTaskStageGroupResources, 255),
|
||||
[UpdateTaskStageSplashscreenInstall] = STAGE_DEF(UpdateTaskStageGroupSplashscreen, 5),
|
||||
|
||||
[UpdateTaskStageCompleted] = STAGE_DEF(UpdateTaskStageGroupMisc, 1),
|
||||
[UpdateTaskStageError] = STAGE_DEF(UpdateTaskStageGroupMisc, 1),
|
||||
[UpdateTaskStageOBError] = STAGE_DEF(UpdateTaskStageGroupMisc, 1),
|
||||
};
|
||||
|
||||
static UpdateTaskStageGroup update_task_get_task_groups(UpdateTask* update_task) {
|
||||
UpdateTaskStageGroup ret = UpdateTaskStageGroupPreUpdate | UpdateTaskStageGroupPostUpdate;
|
||||
UpdateManifest* manifest = update_task->manifest;
|
||||
if(!string_empty_p(manifest->radio_image)) {
|
||||
ret |= UpdateTaskStageGroupRadio;
|
||||
}
|
||||
if(update_manifest_has_obdata(manifest)) {
|
||||
ret |= UpdateTaskStageGroupOptionBytes;
|
||||
}
|
||||
if(!string_empty_p(manifest->firmware_dfu_image)) {
|
||||
ret |= UpdateTaskStageGroupFirmware;
|
||||
}
|
||||
if(!string_empty_p(manifest->resource_bundle)) {
|
||||
ret |= UpdateTaskStageGroupResources;
|
||||
}
|
||||
if(!string_empty_p(manifest->splash_file)) {
|
||||
ret |= UpdateTaskStageGroupSplashscreen;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void update_task_calc_completed_stages(UpdateTask* update_task) {
|
||||
uint32_t completed_stages_points = 0;
|
||||
for(UpdateTaskStage past_stage = UpdateTaskStageProgress;
|
||||
past_stage < update_task->state.stage;
|
||||
++past_stage) {
|
||||
const UpdateTaskStageGroupMap* grp_descr = &update_task_stage_progress[past_stage];
|
||||
if((grp_descr->group & update_task->state.groups) == 0) {
|
||||
continue;
|
||||
}
|
||||
completed_stages_points += grp_descr->weight;
|
||||
}
|
||||
update_task->state.completed_stages_points = completed_stages_points;
|
||||
}
|
||||
|
||||
void update_task_set_progress(UpdateTask* update_task, UpdateTaskStage stage, uint8_t progress) {
|
||||
if(stage != UpdateTaskStageProgress) {
|
||||
/* do not override more specific error states */
|
||||
if((stage >= UpdateTaskStageError) && (update_task->state.stage >= UpdateTaskStageError)) {
|
||||
return;
|
||||
}
|
||||
/* Build error message with code "[stage_idx-stage_percent]" */
|
||||
if(stage >= UpdateTaskStageError) {
|
||||
string_printf(
|
||||
update_task->state.status,
|
||||
"%s #[%d-%d]",
|
||||
update_task_stage_descr[stage],
|
||||
update_task->state.stage,
|
||||
update_task->state.stage_progress);
|
||||
} else {
|
||||
string_set_str(update_task->state.status, update_task_stage_descr[stage]);
|
||||
}
|
||||
/* Store stage update */
|
||||
update_task->state.stage = stage;
|
||||
/* If we are still alive, sum completed stages weights */
|
||||
if((stage > UpdateTaskStageProgress) && (stage < UpdateTaskStageCompleted)) {
|
||||
update_task_calc_completed_stages(update_task);
|
||||
}
|
||||
}
|
||||
|
||||
/* Store stage progress for all non-error updates - to provide details on error state */
|
||||
if(!update_stage_is_error(stage)) {
|
||||
update_task->state.stage_progress = progress;
|
||||
}
|
||||
|
||||
/* Calculate "overall" progress, based on stage weights */
|
||||
uint32_t adapted_progress = 1;
|
||||
if(update_task->state.total_progress_points != 0) {
|
||||
if(stage < UpdateTaskStageCompleted) {
|
||||
adapted_progress = MIN(
|
||||
(update_task->state.completed_stages_points +
|
||||
(update_task_stage_progress[update_task->state.stage].weight * progress / 100)) *
|
||||
100 / (update_task->state.total_progress_points),
|
||||
100u);
|
||||
|
||||
} else {
|
||||
adapted_progress = update_task->state.overall_progress;
|
||||
}
|
||||
}
|
||||
update_task->state.overall_progress = adapted_progress;
|
||||
|
||||
if(update_task->status_change_cb) {
|
||||
(update_task->status_change_cb)(
|
||||
string_get_cstr(update_task->state.status),
|
||||
adapted_progress,
|
||||
update_stage_is_error(update_task->state.stage),
|
||||
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_file_exists(update_task->storage, string_get_cstr(tmp_path));
|
||||
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;
|
||||
}
|
||||
|
||||
if(furi_thread_get_return_code(update_task->thread) == UPDATE_TASK_NOERR) {
|
||||
furi_delay_ms(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.stage_progress = 0;
|
||||
update_task->state.overall_progress = 0;
|
||||
string_init(update_task->state.status);
|
||||
|
||||
update_task->manifest = update_manifest_alloc();
|
||||
update_task->storage = furi_record_open(RECORD_STORAGE);
|
||||
update_task->file = storage_file_alloc(update_task->storage);
|
||||
update_task->status_change_cb = NULL;
|
||||
update_task->boot_mode = furi_hal_rtc_get_boot_mode();
|
||||
string_init(update_task->update_path);
|
||||
|
||||
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(RECORD_STORAGE);
|
||||
string_clear(update_task->update_path);
|
||||
|
||||
free(update_task);
|
||||
}
|
||||
|
||||
bool update_task_parse_manifest(UpdateTask* update_task) {
|
||||
furi_assert(update_task);
|
||||
update_task->state.stage_progress = 0;
|
||||
update_task->state.overall_progress = 0;
|
||||
update_task->state.total_progress_points = 0;
|
||||
update_task->state.completed_stages_points = 0;
|
||||
update_task->state.groups = 0;
|
||||
|
||||
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, 13);
|
||||
if(!furi_hal_version_do_i_belong_here()) {
|
||||
break;
|
||||
}
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageProgress, 20);
|
||||
if(!update_operation_get_current_package_manifest_path(
|
||||
update_task->storage, manifest_path)) {
|
||||
break;
|
||||
}
|
||||
|
||||
path_extract_dirname(string_get_cstr(manifest_path), update_task->update_path);
|
||||
update_task_set_progress(update_task, UpdateTaskStageProgress, 30);
|
||||
|
||||
UpdateManifest* manifest = update_task->manifest;
|
||||
if(!update_manifest_init(manifest, string_get_cstr(manifest_path))) {
|
||||
break;
|
||||
}
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageProgress, 40);
|
||||
if(manifest->manifest_version < UPDATE_OPERATION_MIN_MANIFEST_VERSION) {
|
||||
break;
|
||||
}
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageProgress, 50);
|
||||
if(manifest->target != furi_hal_version_get_hw_target()) {
|
||||
break;
|
||||
}
|
||||
|
||||
update_task->state.groups = update_task_get_task_groups(update_task);
|
||||
for(size_t stage_counter = 0; stage_counter < COUNT_OF(update_task_stage_progress);
|
||||
++stage_counter) {
|
||||
const UpdateTaskStageGroupMap* grp_descr = &update_task_stage_progress[stage_counter];
|
||||
if((grp_descr->group & update_task->state.groups) != 0) {
|
||||
update_task->state.total_progress_points += grp_descr->weight;
|
||||
}
|
||||
}
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageProgress, 60);
|
||||
if((update_task->state.groups & UpdateTaskStageGroupFirmware) &&
|
||||
!update_task_check_file_exists(update_task, manifest->firmware_dfu_image)) {
|
||||
break;
|
||||
}
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageProgress, 80);
|
||||
if((update_task->state.groups & UpdateTaskStageGroupRadio) &&
|
||||
(!update_task_check_file_exists(update_task, manifest->radio_image) ||
|
||||
(manifest->radio_version.version.type == 0))) {
|
||||
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;
|
||||
}
|
||||
|
||||
void update_task_start(UpdateTask* update_task) {
|
||||
furi_assert(update_task);
|
||||
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;
|
||||
}
|
89
applications/system/updater/util/update_task.h
Normal file
89
applications/system/updater/util/update_task.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#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 300
|
||||
#define UPDATE_DELAY_OPERATION_ERROR INT_MAX
|
||||
|
||||
typedef enum {
|
||||
UpdateTaskStageProgress = 0,
|
||||
|
||||
UpdateTaskStageReadManifest,
|
||||
UpdateTaskStageLfsBackup,
|
||||
|
||||
UpdateTaskStageRadioImageValidate,
|
||||
UpdateTaskStageRadioErase,
|
||||
UpdateTaskStageRadioWrite,
|
||||
UpdateTaskStageRadioInstall,
|
||||
UpdateTaskStageRadioBusy,
|
||||
|
||||
UpdateTaskStageOBValidation,
|
||||
|
||||
UpdateTaskStageValidateDFUImage,
|
||||
UpdateTaskStageFlashWrite,
|
||||
UpdateTaskStageFlashValidate,
|
||||
|
||||
UpdateTaskStageLfsRestore,
|
||||
UpdateTaskStageResourcesUpdate,
|
||||
UpdateTaskStageSplashscreenInstall,
|
||||
|
||||
UpdateTaskStageCompleted,
|
||||
UpdateTaskStageError,
|
||||
UpdateTaskStageOBError,
|
||||
UpdateTaskStageMAX
|
||||
} UpdateTaskStage;
|
||||
|
||||
inline bool update_stage_is_error(const UpdateTaskStage stage) {
|
||||
return stage >= UpdateTaskStageError;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
UpdateTaskStageGroupMisc = 0,
|
||||
UpdateTaskStageGroupPreUpdate = 1 << 1,
|
||||
UpdateTaskStageGroupFirmware = 1 << 2,
|
||||
UpdateTaskStageGroupOptionBytes = 1 << 3,
|
||||
UpdateTaskStageGroupRadio = 1 << 4,
|
||||
UpdateTaskStageGroupPostUpdate = 1 << 5,
|
||||
UpdateTaskStageGroupResources = 1 << 6,
|
||||
UpdateTaskStageGroupSplashscreen = 1 << 7,
|
||||
} UpdateTaskStageGroup;
|
||||
|
||||
typedef struct {
|
||||
UpdateTaskStage stage;
|
||||
uint8_t overall_progress, stage_progress;
|
||||
string_t status;
|
||||
UpdateTaskStageGroup groups;
|
||||
uint32_t total_progress_points;
|
||||
uint32_t completed_stages_points;
|
||||
} UpdateTaskState;
|
||||
|
||||
typedef struct UpdateTask UpdateTask;
|
||||
|
||||
typedef void (
|
||||
*updateProgressCb)(const char* status, const uint8_t stage_pct, bool failed, void* state);
|
||||
|
||||
UpdateTask* update_task_alloc();
|
||||
|
||||
void update_task_free(UpdateTask* update_task);
|
||||
|
||||
void update_task_set_progress_cb(UpdateTask* update_task, updateProgressCb cb, void* state);
|
||||
|
||||
void 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
|
26
applications/system/updater/util/update_task_i.h
Normal file
26
applications/system/updater/util/update_task_i.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <storage/storage.h>
|
||||
#include <furi_hal.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;
|
||||
FuriHalRtcBootMode boot_mode;
|
||||
} 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);
|
150
applications/system/updater/util/update_task_worker_backup.c
Normal file
150
applications/system/updater/util/update_task_worker_backup.c
Normal file
@@ -0,0 +1,150 @@
|
||||
#include "update_task.h"
|
||||
#include "update_task_i.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <storage/storage.h>
|
||||
#include <desktop/helpers/slideshow_filename.h>
|
||||
#include <toolbox/path.h>
|
||||
#include <update_util/dfu_file.h>
|
||||
#include <update_util/lfs_backup.h>
|
||||
#include <update_util/update_operation.h>
|
||||
#include <toolbox/tar/tar_archive.h>
|
||||
#include <toolbox/crc32_calc.h>
|
||||
|
||||
#define TAG "UpdWorkerBackup"
|
||||
|
||||
#define CHECK_RESULT(x) \
|
||||
if(!(x)) { \
|
||||
break; \
|
||||
}
|
||||
|
||||
static bool update_task_pre_update(UpdateTask* update_task) {
|
||||
bool success = false;
|
||||
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);
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageLfsBackup, 0);
|
||||
/* to avoid bootloops */
|
||||
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
|
||||
if((success = lfs_backup_create(update_task->storage, string_get_cstr(backup_file_path)))) {
|
||||
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeUpdate);
|
||||
}
|
||||
|
||||
string_clear(backup_file_path);
|
||||
return success;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
UpdateTask* update_task;
|
||||
int32_t total_files, processed_files;
|
||||
} TarUnpackProgress;
|
||||
|
||||
static bool update_task_resource_unpack_cb(const char* name, bool is_directory, void* context) {
|
||||
UNUSED(name);
|
||||
UNUSED(is_directory);
|
||||
TarUnpackProgress* unpack_progress = context;
|
||||
unpack_progress->processed_files++;
|
||||
update_task_set_progress(
|
||||
unpack_progress->update_task,
|
||||
UpdateTaskStageProgress,
|
||||
unpack_progress->processed_files * 100 / (unpack_progress->total_files + 1));
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool update_task_post_update(UpdateTask* update_task) {
|
||||
bool success = false;
|
||||
|
||||
string_t file_path;
|
||||
string_init(file_path);
|
||||
|
||||
TarArchive* archive = tar_archive_alloc(update_task->storage);
|
||||
do {
|
||||
path_concat(
|
||||
string_get_cstr(update_task->update_path), LFS_BACKUP_DEFAULT_FILENAME, file_path);
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
|
||||
|
||||
CHECK_RESULT(lfs_backup_unpack(update_task->storage, string_get_cstr(file_path)));
|
||||
|
||||
if(update_task->state.groups & UpdateTaskStageGroupResources) {
|
||||
TarUnpackProgress progress = {
|
||||
.update_task = update_task,
|
||||
.total_files = 0,
|
||||
.processed_files = 0,
|
||||
};
|
||||
update_task_set_progress(update_task, UpdateTaskStageResourcesUpdate, 0);
|
||||
|
||||
path_concat(
|
||||
string_get_cstr(update_task->update_path),
|
||||
string_get_cstr(update_task->manifest->resource_bundle),
|
||||
file_path);
|
||||
|
||||
tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
|
||||
CHECK_RESULT(
|
||||
tar_archive_open(archive, string_get_cstr(file_path), TAR_OPEN_MODE_READ));
|
||||
|
||||
progress.total_files = tar_archive_get_entries_count(archive);
|
||||
if(progress.total_files > 0) {
|
||||
CHECK_RESULT(tar_archive_unpack_to(archive, STORAGE_EXT_PATH_PREFIX, NULL));
|
||||
}
|
||||
}
|
||||
|
||||
if(update_task->state.groups & UpdateTaskStageGroupSplashscreen) {
|
||||
update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 0);
|
||||
string_t tmp_path;
|
||||
string_init_set(tmp_path, update_task->update_path);
|
||||
path_append(tmp_path, string_get_cstr(update_task->manifest->splash_file));
|
||||
if(storage_common_copy(
|
||||
update_task->storage,
|
||||
string_get_cstr(tmp_path),
|
||||
INT_PATH(SLIDESHOW_FILE_NAME)) != FSE_OK) {
|
||||
// actually, not critical
|
||||
}
|
||||
string_clear(tmp_path);
|
||||
update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 100);
|
||||
}
|
||||
success = true;
|
||||
} while(false);
|
||||
|
||||
tar_archive_free(archive);
|
||||
string_clear(file_path);
|
||||
return success;
|
||||
}
|
||||
|
||||
int32_t update_task_worker_backup_restore(void* context) {
|
||||
furi_assert(context);
|
||||
UpdateTask* update_task = context;
|
||||
|
||||
FuriHalRtcBootMode boot_mode = update_task->boot_mode;
|
||||
if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
|
||||
/* no idea how we got here. Do nothing */
|
||||
return UPDATE_TASK_NOERR;
|
||||
}
|
||||
|
||||
bool success = false;
|
||||
do {
|
||||
if(!update_task_parse_manifest(update_task)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(boot_mode == FuriHalRtcBootModePreUpdate) {
|
||||
success = update_task_pre_update(update_task);
|
||||
} else if(boot_mode == FuriHalRtcBootModePostUpdate) {
|
||||
success = update_task_post_update(update_task);
|
||||
if(success) {
|
||||
update_operation_disarm();
|
||||
}
|
||||
}
|
||||
} while(false);
|
||||
|
||||
if(!success) {
|
||||
update_task_set_progress(update_task, UpdateTaskStageError, 0);
|
||||
return UPDATE_TASK_FAILED;
|
||||
}
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
|
||||
return UPDATE_TASK_NOERR;
|
||||
}
|
356
applications/system/updater/util/update_task_worker_flasher.c
Normal file
356
applications/system/updater/util/update_task_worker_flasher.c
Normal file
@@ -0,0 +1,356 @@
|
||||
#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>
|
||||
#include <toolbox/tar/tar_archive.h>
|
||||
#include <toolbox/crc32_calc.h>
|
||||
|
||||
#define TAG "UpdWorkerRAM"
|
||||
|
||||
#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
|
||||
/* Time, in ms, to wait for system restart by C2 before crashing */
|
||||
#define C2_MODE_SWITCH_TIMEOUT 10000
|
||||
|
||||
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_file_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));
|
||||
}
|
||||
|
||||
static bool update_task_write_dfu(UpdateTask* update_task) {
|
||||
DfuUpdateTask page_task = {
|
||||
.address_cb = &check_address_boundaries,
|
||||
.progress_cb = &update_task_file_progress,
|
||||
.task_cb = &furi_hal_flash_program_page,
|
||||
.context = update_task,
|
||||
};
|
||||
|
||||
bool success = false;
|
||||
do {
|
||||
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_file_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));
|
||||
success = true;
|
||||
} while(false);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool update_task_write_stack_data(UpdateTask* update_task) {
|
||||
furi_check(storage_file_is_open(update_task->file));
|
||||
const size_t FLASH_PAGE_SIZE = furi_hal_flash_get_page_size();
|
||||
|
||||
uint32_t stack_size = storage_file_size(update_task->file);
|
||||
storage_file_seek(update_task->file, 0, true);
|
||||
|
||||
if(!check_address_boundaries(update_task->manifest->radio_address) ||
|
||||
!check_address_boundaries(update_task->manifest->radio_address + stack_size)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioWrite, 0);
|
||||
uint8_t* fw_block = malloc(FLASH_PAGE_SIZE);
|
||||
uint16_t bytes_read = 0;
|
||||
uint32_t element_offs = 0;
|
||||
|
||||
while(element_offs < stack_size) {
|
||||
uint32_t n_bytes_to_read = FLASH_PAGE_SIZE;
|
||||
if((element_offs + n_bytes_to_read) > stack_size) {
|
||||
n_bytes_to_read = stack_size - element_offs;
|
||||
}
|
||||
|
||||
bytes_read = storage_file_read(update_task->file, fw_block, n_bytes_to_read);
|
||||
CHECK_RESULT(bytes_read != 0);
|
||||
|
||||
int16_t i_page =
|
||||
furi_hal_flash_get_page_number(update_task->manifest->radio_address + element_offs);
|
||||
CHECK_RESULT(i_page >= 0);
|
||||
|
||||
CHECK_RESULT(furi_hal_flash_program_page(i_page, fw_block, bytes_read));
|
||||
|
||||
element_offs += bytes_read;
|
||||
update_task_set_progress(
|
||||
update_task, UpdateTaskStageProgress, element_offs * 100 / stack_size);
|
||||
}
|
||||
|
||||
free(fw_block);
|
||||
return element_offs == stack_size;
|
||||
}
|
||||
|
||||
static void update_task_wait_for_restart(UpdateTask* update_task) {
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 10);
|
||||
furi_delay_ms(C2_MODE_SWITCH_TIMEOUT);
|
||||
furi_crash("C2 timeout");
|
||||
}
|
||||
|
||||
static bool update_task_write_stack(UpdateTask* update_task) {
|
||||
bool success = false;
|
||||
UpdateManifest* manifest = update_task->manifest;
|
||||
do {
|
||||
FURI_LOG_W(TAG, "Writing stack");
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioImageValidate, 0);
|
||||
CHECK_RESULT(update_task_open_file(update_task, manifest->radio_image));
|
||||
CHECK_RESULT(
|
||||
crc32_calc_file(update_task->file, &update_task_file_progress, update_task) ==
|
||||
manifest->radio_crc);
|
||||
|
||||
CHECK_RESULT(update_task_write_stack_data(update_task));
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 0);
|
||||
CHECK_RESULT(
|
||||
ble_glue_fus_stack_install(manifest->radio_address, 0) != BleGlueCommandResultError);
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 80);
|
||||
CHECK_RESULT(ble_glue_fus_wait_operation() == BleGlueCommandResultOK);
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 100);
|
||||
/* ...system will restart here. */
|
||||
update_task_wait_for_restart(update_task);
|
||||
success = true;
|
||||
} while(false);
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool update_task_remove_stack(UpdateTask* update_task) {
|
||||
bool success = false;
|
||||
do {
|
||||
FURI_LOG_W(TAG, "Removing stack");
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioErase, 30);
|
||||
CHECK_RESULT(ble_glue_fus_stack_delete() != BleGlueCommandResultError);
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioErase, 80);
|
||||
CHECK_RESULT(ble_glue_fus_wait_operation() == BleGlueCommandResultOK);
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioErase, 100);
|
||||
/* ...system will restart here. */
|
||||
update_task_wait_for_restart(update_task);
|
||||
success = true;
|
||||
} while(false);
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool update_task_manage_radiostack(UpdateTask* update_task) {
|
||||
bool success = false;
|
||||
do {
|
||||
CHECK_RESULT(ble_glue_wait_for_c2_start(FURI_HAL_BT_C2_START_TIMEOUT));
|
||||
|
||||
const BleGlueC2Info* c2_state = ble_glue_get_c2_info();
|
||||
|
||||
const UpdateManifestRadioVersion* radio_ver = &update_task->manifest->radio_version;
|
||||
bool stack_version_match = (c2_state->VersionMajor == radio_ver->version.major) &&
|
||||
(c2_state->VersionMinor == radio_ver->version.minor) &&
|
||||
(c2_state->VersionSub == radio_ver->version.sub) &&
|
||||
(c2_state->VersionBranch == radio_ver->version.branch) &&
|
||||
(c2_state->VersionReleaseType == radio_ver->version.release);
|
||||
bool stack_missing = (c2_state->VersionMajor == 0) && (c2_state->VersionMinor == 0);
|
||||
|
||||
if(c2_state->mode == BleGlueC2ModeStack) {
|
||||
/* Stack type is not available when we have FUS running. */
|
||||
bool total_stack_match = stack_version_match &&
|
||||
(c2_state->StackType == radio_ver->version.type);
|
||||
if(total_stack_match) {
|
||||
/* Nothing to do. */
|
||||
FURI_LOG_W(TAG, "Stack version is up2date");
|
||||
furi_hal_rtc_reset_flag(FuriHalRtcFlagC2Update);
|
||||
success = true;
|
||||
break;
|
||||
} else {
|
||||
/* Version or type mismatch. Let's boot to FUS and start updating. */
|
||||
FURI_LOG_W(TAG, "Restarting to FUS");
|
||||
furi_hal_rtc_set_flag(FuriHalRtcFlagC2Update);
|
||||
CHECK_RESULT(furi_hal_bt_ensure_c2_mode(BleGlueC2ModeFUS));
|
||||
/* ...system will restart here. */
|
||||
update_task_wait_for_restart(update_task);
|
||||
}
|
||||
} else if(c2_state->mode == BleGlueC2ModeFUS) {
|
||||
/* OK, we're in FUS mode. */
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 10);
|
||||
FURI_LOG_W(TAG, "Waiting for FUS to settle");
|
||||
ble_glue_fus_wait_operation();
|
||||
if(stack_version_match) {
|
||||
/* We can't check StackType with FUS, but partial version matches */
|
||||
if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagC2Update)) {
|
||||
/* This flag was set when full version was checked.
|
||||
* And something in versions of the stack didn't match.
|
||||
* So, clear the flag and drop the stack. */
|
||||
furi_hal_rtc_reset_flag(FuriHalRtcFlagC2Update);
|
||||
FURI_LOG_W(TAG, "Forcing stack removal (match)");
|
||||
CHECK_RESULT(update_task_remove_stack(update_task));
|
||||
} else {
|
||||
/* We might just had the stack installed.
|
||||
* Let's start it up to check its version */
|
||||
FURI_LOG_W(TAG, "Starting stack to check full version");
|
||||
update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 40);
|
||||
CHECK_RESULT(furi_hal_bt_ensure_c2_mode(BleGlueC2ModeStack));
|
||||
/* ...system will restart here. */
|
||||
update_task_wait_for_restart(update_task);
|
||||
}
|
||||
} else {
|
||||
if(stack_missing) {
|
||||
/* Install stack. */
|
||||
CHECK_RESULT(update_task_write_stack(update_task));
|
||||
} else {
|
||||
CHECK_RESULT(update_task_remove_stack(update_task));
|
||||
}
|
||||
}
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
bool update_task_validate_optionbytes(UpdateTask* update_task) {
|
||||
update_task_set_progress(update_task, UpdateTaskStageOBValidation, 0);
|
||||
|
||||
bool match = true;
|
||||
bool ob_dirty = false;
|
||||
const UpdateManifest* manifest = update_task->manifest;
|
||||
const FuriHalFlashRawOptionByteData* device_data = furi_hal_flash_ob_get_raw_ptr();
|
||||
for(size_t idx = 0; idx < FURI_HAL_FLASH_OB_TOTAL_VALUES; ++idx) {
|
||||
update_task_set_progress(
|
||||
update_task, UpdateTaskStageProgress, idx * 100 / FURI_HAL_FLASH_OB_TOTAL_VALUES);
|
||||
const uint32_t ref_value = manifest->ob_reference.obs[idx].values.base;
|
||||
const uint32_t device_ob_value = device_data->obs[idx].values.base;
|
||||
const uint32_t device_ob_value_masked = device_ob_value &
|
||||
manifest->ob_compare_mask.obs[idx].values.base;
|
||||
if(ref_value != device_ob_value_masked) {
|
||||
match = false;
|
||||
FURI_LOG_E(
|
||||
TAG,
|
||||
"OB MISMATCH: #%d: real %08X != %08X (exp.), full %08X",
|
||||
idx,
|
||||
device_ob_value_masked,
|
||||
ref_value,
|
||||
device_ob_value);
|
||||
|
||||
/* any bits we are allowed to write?.. */
|
||||
bool can_patch = ((device_ob_value_masked ^ ref_value) &
|
||||
manifest->ob_write_mask.obs[idx].values.base) != 0;
|
||||
|
||||
if(can_patch) {
|
||||
const uint32_t patched_value =
|
||||
/* take all non-writable bits from real value */
|
||||
(device_ob_value & ~(manifest->ob_write_mask.obs[idx].values.base)) |
|
||||
/* take all writable bits from reference value */
|
||||
(manifest->ob_reference.obs[idx].values.base &
|
||||
manifest->ob_write_mask.obs[idx].values.base);
|
||||
|
||||
FURI_LOG_W(TAG, "Fixing up OB byte #%d to %08X", idx, patched_value);
|
||||
ob_dirty = true;
|
||||
|
||||
bool is_fixed = furi_hal_flash_ob_set_word(idx, patched_value) &&
|
||||
((device_data->obs[idx].values.base &
|
||||
manifest->ob_compare_mask.obs[idx].values.base) == ref_value);
|
||||
|
||||
if(!is_fixed) {
|
||||
/* Things are so bad that fixing what we are allowed to still doesn't match
|
||||
* reference value */
|
||||
FURI_LOG_W(
|
||||
TAG,
|
||||
"OB #%d is FUBAR (fixed&masked %08X, not %08X)",
|
||||
idx,
|
||||
patched_value,
|
||||
ref_value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
FURI_LOG_I(
|
||||
TAG,
|
||||
"OB MATCH: #%d: real %08X == %08X (exp.)",
|
||||
idx,
|
||||
device_ob_value_masked,
|
||||
ref_value);
|
||||
}
|
||||
}
|
||||
if(!match) {
|
||||
update_task_set_progress(update_task, UpdateTaskStageOBError, 0);
|
||||
}
|
||||
|
||||
if(ob_dirty) {
|
||||
FURI_LOG_W(TAG, "OBs were changed, applying");
|
||||
furi_hal_flash_ob_apply();
|
||||
}
|
||||
return match;
|
||||
}
|
||||
|
||||
int32_t update_task_worker_flash_writer(void* context) {
|
||||
furi_assert(context);
|
||||
UpdateTask* update_task = context;
|
||||
bool success = false;
|
||||
|
||||
do {
|
||||
CHECK_RESULT(update_task_parse_manifest(update_task));
|
||||
|
||||
if(update_task->state.groups & UpdateTaskStageGroupRadio) {
|
||||
CHECK_RESULT(update_task_manage_radiostack(update_task));
|
||||
}
|
||||
|
||||
if(update_task->state.groups & UpdateTaskStageGroupOptionBytes) {
|
||||
CHECK_RESULT(update_task_validate_optionbytes(update_task));
|
||||
}
|
||||
|
||||
if(update_task->state.groups & UpdateTaskStageGroupFirmware) {
|
||||
CHECK_RESULT(update_task_write_dfu(update_task));
|
||||
}
|
||||
|
||||
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePostUpdate);
|
||||
// Format LFS before restoring backup on next boot
|
||||
furi_hal_rtc_set_flag(FuriHalRtcFlagFactoryReset);
|
||||
|
||||
update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
|
||||
success = true;
|
||||
} while(false);
|
||||
|
||||
if(!success) {
|
||||
update_task_set_progress(update_task, UpdateTaskStageError, 0);
|
||||
return UPDATE_TASK_FAILED;
|
||||
}
|
||||
|
||||
return UPDATE_TASK_NOERR;
|
||||
}
|
140
applications/system/updater/views/updater_main.c
Normal file
140
applications/system/updater/views/updater_main.c
Normal file
@@ -0,0 +1,140 @@
|
||||
#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 = 1; /* percent, to limit rendering rate */
|
||||
|
||||
typedef struct {
|
||||
string_t status;
|
||||
uint8_t progress, rendered_progress;
|
||||
bool failed;
|
||||
} UpdaterProgressModel;
|
||||
|
||||
void updater_main_model_set_state(
|
||||
UpdaterMainView* main_view,
|
||||
const char* message,
|
||||
uint8_t progress,
|
||||
bool failed) {
|
||||
with_view_model(
|
||||
main_view->view, (UpdaterProgressModel * model) {
|
||||
model->failed = failed;
|
||||
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) && (event->key == InputKeyOk)) {
|
||||
view_dispatcher_send_custom_event(
|
||||
main_view->view_dispatcher, UpdaterCustomEventRetryUpdate);
|
||||
} else if((event->type == InputTypeLong) && (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);
|
||||
|
||||
if(model->failed) {
|
||||
canvas_draw_str_aligned(canvas, 42, 16, AlignLeft, AlignTop, "Update Failed!");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str_aligned(
|
||||
canvas, 42, 32, AlignLeft, AlignTop, string_get_cstr(model->status));
|
||||
|
||||
canvas_draw_icon(canvas, 7, 16, &I_Warning_30x23);
|
||||
canvas_draw_str_aligned(
|
||||
canvas, 18, 51, AlignLeft, AlignTop, "to retry, hold to abort");
|
||||
canvas_draw_icon(canvas, 7, 50, &I_Ok_btn_9x9);
|
||||
canvas_draw_icon(canvas, 75, 51, &I_Pin_back_arrow_10x8);
|
||||
} else {
|
||||
canvas_draw_str_aligned(canvas, 55, 14, AlignLeft, AlignTop, "UPDATING");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str_aligned(
|
||||
canvas, 64, 51, AlignCenter, AlignTop, string_get_cstr(model->status));
|
||||
canvas_draw_icon(canvas, 4, 5, &I_Updating_32x40);
|
||||
elements_progress_bar(canvas, 42, 29, 80, (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 SD card");
|
||||
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;
|
||||
}
|
26
applications/system/updater/views/updater_main.h
Normal file
26
applications/system/updater/views/updater_main.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#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,
|
||||
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);
|
Reference in New Issue
Block a user