2022-04-13 20:50:25 +00:00
|
|
|
#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",
|
2022-05-06 16:26:25 +00:00
|
|
|
[UpdateTaskStageFlashValidate] = "Validating flash",
|
|
|
|
[UpdateTaskStageRadioImageValidate] = "Checking radio FW",
|
|
|
|
[UpdateTaskStageRadioErase] = "Uninstalling radio FW",
|
|
|
|
[UpdateTaskStageRadioWrite] = "Writing radio FW",
|
|
|
|
[UpdateTaskStageRadioInstall] = "Installing radio FW",
|
|
|
|
[UpdateTaskStageRadioBusy] = "Radio is updating",
|
2022-04-27 15:53:48 +00:00
|
|
|
[UpdateTaskStageOBValidation] = "Validating opt. bytes",
|
2022-04-13 20:50:25 +00:00
|
|
|
[UpdateTaskStageLfsBackup] = "Backing up LFS",
|
|
|
|
[UpdateTaskStageLfsRestore] = "Restoring LFS",
|
2022-04-19 19:02:37 +00:00
|
|
|
[UpdateTaskStageResourcesUpdate] = "Updating resources",
|
2022-06-21 14:11:34 +00:00
|
|
|
[UpdateTaskStageSplashscreenInstall] = "Installing splashscreen",
|
2022-05-06 16:26:25 +00:00
|
|
|
[UpdateTaskStageCompleted] = "Restarting...",
|
2022-04-13 20:50:25 +00:00
|
|
|
[UpdateTaskStageError] = "Error",
|
2022-06-09 09:07:42 +00:00
|
|
|
[UpdateTaskStageOBError] = "OB, report",
|
2022-04-13 20:50:25 +00:00
|
|
|
};
|
|
|
|
|
2022-05-06 16:26:25 +00:00
|
|
|
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),
|
2022-06-21 14:11:34 +00:00
|
|
|
[UpdateTaskStageLfsBackup] = STAGE_DEF(UpdateTaskStageGroupPreUpdate, 15),
|
2022-05-06 16:26:25 +00:00
|
|
|
|
2022-07-04 13:36:12 +00:00
|
|
|
[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),
|
2022-05-06 16:26:25 +00:00
|
|
|
|
|
|
|
[UpdateTaskStageOBValidation] = STAGE_DEF(UpdateTaskStageGroupOptionBytes, 10),
|
|
|
|
|
2022-07-04 13:36:12 +00:00
|
|
|
[UpdateTaskStageValidateDFUImage] = STAGE_DEF(UpdateTaskStageGroupFirmware, 50),
|
2022-05-06 16:26:25 +00:00
|
|
|
[UpdateTaskStageFlashWrite] = STAGE_DEF(UpdateTaskStageGroupFirmware, 200),
|
2022-07-04 13:36:12 +00:00
|
|
|
[UpdateTaskStageFlashValidate] = STAGE_DEF(UpdateTaskStageGroupFirmware, 30),
|
2022-05-06 16:26:25 +00:00
|
|
|
|
|
|
|
[UpdateTaskStageLfsRestore] = STAGE_DEF(UpdateTaskStageGroupPostUpdate, 30),
|
|
|
|
|
|
|
|
[UpdateTaskStageResourcesUpdate] = STAGE_DEF(UpdateTaskStageGroupResources, 255),
|
2022-06-21 14:11:34 +00:00
|
|
|
[UpdateTaskStageSplashscreenInstall] = STAGE_DEF(UpdateTaskStageGroupSplashscreen, 5),
|
2022-05-06 16:26:25 +00:00
|
|
|
|
|
|
|
[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;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!furi_string_empty(manifest->radio_image)) {
|
2022-05-06 16:26:25 +00:00
|
|
|
ret |= UpdateTaskStageGroupRadio;
|
|
|
|
}
|
|
|
|
if(update_manifest_has_obdata(manifest)) {
|
|
|
|
ret |= UpdateTaskStageGroupOptionBytes;
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!furi_string_empty(manifest->firmware_dfu_image)) {
|
2022-05-06 16:26:25 +00:00
|
|
|
ret |= UpdateTaskStageGroupFirmware;
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!furi_string_empty(manifest->resource_bundle)) {
|
2022-05-06 16:26:25 +00:00
|
|
|
ret |= UpdateTaskStageGroupResources;
|
|
|
|
}
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!furi_string_empty(manifest->splash_file)) {
|
2022-06-21 14:11:34 +00:00
|
|
|
ret |= UpdateTaskStageGroupSplashscreen;
|
|
|
|
}
|
2022-05-06 16:26:25 +00:00
|
|
|
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;
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
2022-05-06 16:26:25 +00:00
|
|
|
completed_stages_points += grp_descr->weight;
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
2022-05-06 16:26:25 +00:00
|
|
|
update_task->state.completed_stages_points = completed_stages_points;
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_task_set_progress(UpdateTask* update_task, UpdateTaskStage stage, uint8_t progress) {
|
|
|
|
if(stage != UpdateTaskStageProgress) {
|
2022-05-06 16:26:25 +00:00
|
|
|
/* 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) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_printf(
|
2022-05-06 16:26:25 +00:00
|
|
|
update_task->state.status,
|
|
|
|
"%s #[%d-%d]",
|
|
|
|
update_task_stage_descr[stage],
|
|
|
|
update_task->state.stage,
|
|
|
|
update_task->state.stage_progress);
|
|
|
|
} else {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(update_task->state.status, update_task_stage_descr[stage]);
|
2022-05-06 16:26:25 +00:00
|
|
|
}
|
|
|
|
/* 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);
|
2022-04-27 15:53:48 +00:00
|
|
|
}
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 16:26:25 +00:00
|
|
|
/* 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;
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-06 16:26:25 +00:00
|
|
|
/* 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;
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
if(update_task->status_change_cb) {
|
|
|
|
(update_task->status_change_cb)(
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_get_cstr(update_task->state.status),
|
2022-05-06 16:26:25 +00:00
|
|
|
adapted_progress,
|
|
|
|
update_stage_is_error(update_task->state.stage),
|
2022-04-13 20:50:25 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static bool update_task_check_file_exists(UpdateTask* update_task, FuriString* filename) {
|
2022-04-13 20:50:25 +00:00
|
|
|
furi_assert(update_task);
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* tmp_path;
|
|
|
|
tmp_path = furi_string_alloc_set(update_task->update_path);
|
|
|
|
path_append(tmp_path, furi_string_get_cstr(filename));
|
|
|
|
bool exists = storage_file_exists(update_task->storage, furi_string_get_cstr(tmp_path));
|
|
|
|
furi_string_free(tmp_path);
|
2022-04-13 20:50:25 +00:00
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
bool update_task_open_file(UpdateTask* update_task, FuriString* filename) {
|
2022-04-13 20:50:25 +00:00
|
|
|
furi_assert(update_task);
|
|
|
|
update_task_close_file(update_task);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* tmp_path;
|
|
|
|
tmp_path = furi_string_alloc_set(update_task->update_path);
|
|
|
|
path_append(tmp_path, furi_string_get_cstr(filename));
|
2022-04-13 20:50:25 +00:00
|
|
|
bool open_success = storage_file_open(
|
2022-10-05 15:15:23 +00:00
|
|
|
update_task->file, furi_string_get_cstr(tmp_path), FSAM_READ, FSOM_OPEN_EXISTING);
|
|
|
|
furi_string_free(tmp_path);
|
2022-04-13 20:50:25 +00:00
|
|
|
return open_success;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void update_task_worker_thread_cb(FuriThreadState state, void* context) {
|
|
|
|
UpdateTask* update_task = context;
|
|
|
|
|
|
|
|
if(state != FuriThreadStateStopped) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:26:25 +00:00
|
|
|
if(furi_thread_get_return_code(update_task->thread) == UPDATE_TASK_NOERR) {
|
2022-07-20 10:56:33 +00:00
|
|
|
furi_delay_ms(UPDATE_DELAY_OPERATION_OK);
|
2022-04-13 20:50:25 +00:00
|
|
|
furi_hal_power_reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateTask* update_task_alloc() {
|
|
|
|
UpdateTask* update_task = malloc(sizeof(UpdateTask));
|
|
|
|
|
|
|
|
update_task->state.stage = UpdateTaskStageProgress;
|
2022-05-06 16:26:25 +00:00
|
|
|
update_task->state.stage_progress = 0;
|
|
|
|
update_task->state.overall_progress = 0;
|
2022-10-05 15:15:23 +00:00
|
|
|
update_task->state.status = furi_string_alloc();
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
update_task->manifest = update_manifest_alloc();
|
2022-07-26 12:21:51 +00:00
|
|
|
update_task->storage = furi_record_open(RECORD_STORAGE);
|
2022-04-13 20:50:25 +00:00
|
|
|
update_task->file = storage_file_alloc(update_task->storage);
|
|
|
|
update_task->status_change_cb = NULL;
|
2022-07-04 13:36:12 +00:00
|
|
|
update_task->boot_mode = furi_hal_rtc_get_boot_mode();
|
2022-10-05 15:15:23 +00:00
|
|
|
update_task->update_path = furi_string_alloc();
|
2022-04-13 20:50:25 +00:00
|
|
|
|
2022-11-23 12:49:17 +00:00
|
|
|
FuriThread* thread = update_task->thread =
|
|
|
|
furi_thread_alloc_ex("UpdateWorker", 5120, NULL, update_task);
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2022-07-26 12:21:51 +00:00
|
|
|
furi_record_close(RECORD_STORAGE);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(update_task->update_path);
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
free(update_task);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool update_task_parse_manifest(UpdateTask* update_task) {
|
|
|
|
furi_assert(update_task);
|
2022-05-06 16:26:25 +00:00
|
|
|
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;
|
|
|
|
|
2022-04-13 20:50:25 +00:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageReadManifest, 0);
|
|
|
|
bool result = false;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* manifest_path;
|
|
|
|
manifest_path = furi_string_alloc();
|
2022-04-13 20:50:25 +00:00
|
|
|
|
|
|
|
do {
|
2022-05-11 09:45:01 +00:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageProgress, 13);
|
|
|
|
if(!furi_hal_version_do_i_belong_here()) {
|
2022-04-13 20:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageProgress, 20);
|
|
|
|
if(!update_operation_get_current_package_manifest_path(
|
|
|
|
update_task->storage, manifest_path)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
path_extract_dirname(furi_string_get_cstr(manifest_path), update_task->update_path);
|
2022-04-13 20:50:25 +00:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageProgress, 30);
|
2022-05-06 16:26:25 +00:00
|
|
|
|
|
|
|
UpdateManifest* manifest = update_task->manifest;
|
2022-10-05 15:15:23 +00:00
|
|
|
if(!update_manifest_init(manifest, furi_string_get_cstr(manifest_path))) {
|
2022-04-13 20:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
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);
|
2023-02-07 16:33:05 +00:00
|
|
|
/* Check target only if it's set - skipped for pre-production samples */
|
|
|
|
if(furi_hal_version_get_hw_target() &&
|
|
|
|
(manifest->target != furi_hal_version_get_hw_target())) {
|
2022-05-11 09:45:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-06 16:26:25 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageProgress, 60);
|
2022-05-06 16:26:25 +00:00
|
|
|
if((update_task->state.groups & UpdateTaskStageGroupFirmware) &&
|
|
|
|
!update_task_check_file_exists(update_task, manifest->firmware_dfu_image)) {
|
2022-04-13 20:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-05-11 09:45:01 +00:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageProgress, 80);
|
2022-05-06 16:26:25 +00:00
|
|
|
if((update_task->state.groups & UpdateTaskStageGroupRadio) &&
|
|
|
|
(!update_task_check_file_exists(update_task, manifest->radio_image) ||
|
|
|
|
(manifest->radio_version.version.type == 0))) {
|
2022-04-13 20:50:25 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
update_task_set_progress(update_task, UpdateTaskStageProgress, 100);
|
|
|
|
result = true;
|
|
|
|
} while(false);
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(manifest_path);
|
2022-04-13 20:50:25 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-06-20 14:54:48 +00:00
|
|
|
void update_task_start(UpdateTask* update_task) {
|
2022-04-13 20:50:25 +00:00
|
|
|
furi_assert(update_task);
|
2022-06-20 14:54:48 +00:00
|
|
|
furi_thread_start(update_task->thread);
|
2022-04-13 20:50:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2022-05-06 13:37:10 +00:00
|
|
|
}
|