2022-04-13 23:50:25 +03:00
|
|
|
#include "update_task.h"
|
|
|
|
#include "update_task_i.h"
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
#include <furi_hal.h>
|
|
|
|
#include <storage/storage.h>
|
2022-07-26 15:21:51 +03:00
|
|
|
#include <desktop/helpers/slideshow_filename.h>
|
2022-04-13 23:50:25 +03:00
|
|
|
#include <toolbox/path.h>
|
|
|
|
#include <update_util/dfu_file.h>
|
2024-09-05 20:44:22 +03:00
|
|
|
#include <update_util/int_backup.h>
|
2022-04-13 23:50:25 +03:00
|
|
|
#include <update_util/update_operation.h>
|
2022-09-28 20:13:12 +03:00
|
|
|
#include <update_util/resources/manifest.h>
|
2022-04-19 11:03:28 +03:00
|
|
|
#include <toolbox/tar/tar_archive.h>
|
2022-04-27 18:53:48 +03:00
|
|
|
#include <toolbox/crc32_calc.h>
|
|
|
|
|
|
|
|
#define TAG "UpdWorkerBackup"
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-04-19 11:03:28 +03:00
|
|
|
static bool update_task_pre_update(UpdateTask* update_task) {
|
|
|
|
bool success = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* backup_file_path;
|
|
|
|
backup_file_path = furi_string_alloc();
|
2022-04-19 11:03:28 +03:00
|
|
|
path_concat(
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_get_cstr(update_task->update_path),
|
2024-09-05 20:44:22 +03:00
|
|
|
INT_BACKUP_DEFAULT_FILENAME,
|
2022-10-05 18:15:23 +03:00
|
|
|
backup_file_path);
|
2022-04-19 11:03:28 +03:00
|
|
|
|
2024-09-05 20:44:22 +03:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageIntBackup, 0);
|
2022-04-28 19:05:37 +03:00
|
|
|
/* to avoid bootloops */
|
|
|
|
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
|
2022-10-05 18:15:23 +03:00
|
|
|
if((success =
|
2024-09-05 20:44:22 +03:00
|
|
|
int_backup_create(update_task->storage, furi_string_get_cstr(backup_file_path)))) {
|
2022-04-19 11:03:28 +03:00
|
|
|
furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeUpdate);
|
|
|
|
}
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(backup_file_path);
|
2022-04-19 11:03:28 +03:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
typedef struct {
|
|
|
|
UpdateTask* update_task;
|
2024-06-30 13:38:48 +03:00
|
|
|
TarArchive* archive;
|
2022-04-19 11:03:28 +03:00
|
|
|
} 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;
|
2024-06-30 13:38:48 +03:00
|
|
|
int32_t progress = 0, total = 0;
|
|
|
|
tar_archive_get_read_progress(unpack_progress->archive, &progress, &total);
|
2022-04-19 11:03:28 +03:00
|
|
|
update_task_set_progress(
|
2024-06-30 13:38:48 +03:00
|
|
|
unpack_progress->update_task, UpdateTaskStageProgress, (progress * 100) / (total + 1));
|
2022-04-19 11:03:28 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
static void update_task_cleanup_resources(UpdateTask* update_task) {
|
2022-09-28 20:13:12 +03:00
|
|
|
ResourceManifestReader* manifest_reader = resource_manifest_reader_alloc(update_task->storage);
|
|
|
|
do {
|
2023-02-13 17:07:53 +03:00
|
|
|
FURI_LOG_D(TAG, "Cleaning up old manifest");
|
2022-09-28 20:13:12 +03:00
|
|
|
if(!resource_manifest_reader_open(manifest_reader, EXT_PATH("Manifest"))) {
|
|
|
|
FURI_LOG_W(TAG, "No existing manifest");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourceManifestEntry* entry_ptr = NULL;
|
2024-06-30 13:38:48 +03:00
|
|
|
/* Iterate over manifest and calculate entries count */
|
|
|
|
uint32_t n_file_entries = 1, n_dir_entries = 1;
|
|
|
|
while((entry_ptr = resource_manifest_reader_next(manifest_reader))) {
|
|
|
|
if(entry_ptr->type == ResourceManifestEntryTypeFile) {
|
|
|
|
n_file_entries++;
|
|
|
|
} else if(entry_ptr->type == ResourceManifestEntryTypeDirectory) {
|
|
|
|
n_dir_entries++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resource_manifest_rewind(manifest_reader);
|
|
|
|
|
|
|
|
update_task_set_progress(update_task, UpdateTaskStageResourcesFileCleanup, 0);
|
|
|
|
uint32_t n_processed_file_entries = 0;
|
2022-09-28 20:13:12 +03:00
|
|
|
while((entry_ptr = resource_manifest_reader_next(manifest_reader))) {
|
|
|
|
if(entry_ptr->type == ResourceManifestEntryTypeFile) {
|
|
|
|
update_task_set_progress(
|
|
|
|
update_task,
|
|
|
|
UpdateTaskStageProgress,
|
2024-06-30 13:38:48 +03:00
|
|
|
(n_processed_file_entries++ * 100) / n_file_entries);
|
2022-09-28 20:13:12 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* file_path = furi_string_alloc();
|
|
|
|
path_concat(
|
|
|
|
STORAGE_EXT_PATH_PREFIX, furi_string_get_cstr(entry_ptr->name), file_path);
|
|
|
|
FURI_LOG_D(TAG, "Removing %s", furi_string_get_cstr(file_path));
|
2023-02-26 18:15:26 +03:00
|
|
|
|
|
|
|
FS_Error result =
|
|
|
|
storage_common_remove(update_task->storage, furi_string_get_cstr(file_path));
|
|
|
|
if(result != FSE_OK && result != FSE_EXIST) {
|
|
|
|
FURI_LOG_E(
|
|
|
|
TAG,
|
|
|
|
"%s remove failed, cause %s",
|
|
|
|
furi_string_get_cstr(file_path),
|
|
|
|
storage_error_get_desc(result));
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(file_path);
|
2022-09-28 20:13:12 +03:00
|
|
|
}
|
|
|
|
}
|
2023-01-06 09:31:17 +03:00
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageResourcesDirCleanup, 0);
|
|
|
|
uint32_t n_processed_dir_entries = 0;
|
2023-01-06 09:31:17 +03:00
|
|
|
while((entry_ptr = resource_manifest_reader_previous(manifest_reader))) {
|
|
|
|
if(entry_ptr->type == ResourceManifestEntryTypeDirectory) {
|
|
|
|
update_task_set_progress(
|
|
|
|
update_task,
|
|
|
|
UpdateTaskStageProgress,
|
2024-06-30 13:38:48 +03:00
|
|
|
(n_processed_dir_entries++ * 100) / n_dir_entries);
|
2023-01-06 09:31:17 +03:00
|
|
|
|
|
|
|
FuriString* folder_path = furi_string_alloc();
|
|
|
|
|
|
|
|
do {
|
|
|
|
path_concat(
|
|
|
|
STORAGE_EXT_PATH_PREFIX,
|
|
|
|
furi_string_get_cstr(entry_ptr->name),
|
|
|
|
folder_path);
|
|
|
|
|
|
|
|
FURI_LOG_D(TAG, "Removing folder %s", furi_string_get_cstr(folder_path));
|
2023-02-26 18:15:26 +03:00
|
|
|
FS_Error result = storage_common_remove(
|
|
|
|
update_task->storage, furi_string_get_cstr(folder_path));
|
|
|
|
if(result != FSE_OK && result != FSE_EXIST) {
|
|
|
|
FURI_LOG_E(
|
2023-01-06 09:31:17 +03:00
|
|
|
TAG,
|
2023-02-26 18:15:26 +03:00
|
|
|
"%s remove failed, cause %s",
|
|
|
|
furi_string_get_cstr(folder_path),
|
|
|
|
storage_error_get_desc(result));
|
2023-01-06 09:31:17 +03:00
|
|
|
}
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
furi_string_free(folder_path);
|
|
|
|
}
|
|
|
|
}
|
2022-09-28 20:13:12 +03:00
|
|
|
} while(false);
|
|
|
|
resource_manifest_reader_free(manifest_reader);
|
|
|
|
}
|
|
|
|
|
2022-04-19 11:03:28 +03:00
|
|
|
static bool update_task_post_update(UpdateTask* update_task) {
|
|
|
|
bool success = false;
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* file_path;
|
|
|
|
file_path = furi_string_alloc();
|
2022-04-19 11:03:28 +03:00
|
|
|
|
2022-05-06 19:26:25 +03:00
|
|
|
TarArchive* archive = tar_archive_alloc(update_task->storage);
|
2022-04-19 11:03:28 +03:00
|
|
|
do {
|
|
|
|
path_concat(
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_get_cstr(update_task->update_path),
|
2024-09-05 20:44:22 +03:00
|
|
|
INT_BACKUP_DEFAULT_FILENAME,
|
2022-10-05 18:15:23 +03:00
|
|
|
file_path);
|
2022-04-19 11:03:28 +03:00
|
|
|
|
2024-09-05 20:44:22 +03:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageIntRestore, 0);
|
2022-04-19 11:03:28 +03:00
|
|
|
|
2024-09-05 20:44:22 +03:00
|
|
|
CHECK_RESULT(int_backup_unpack(update_task->storage, furi_string_get_cstr(file_path)));
|
2022-04-19 11:03:28 +03:00
|
|
|
|
2022-05-06 19:26:25 +03:00
|
|
|
if(update_task->state.groups & UpdateTaskStageGroupResources) {
|
2022-04-19 11:03:28 +03:00
|
|
|
TarUnpackProgress progress = {
|
|
|
|
.update_task = update_task,
|
2024-06-30 13:38:48 +03:00
|
|
|
.archive = archive,
|
2022-04-19 11:03:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
path_concat(
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_get_cstr(update_task->update_path),
|
|
|
|
furi_string_get_cstr(update_task->manifest->resource_bundle),
|
2022-04-19 11:03:28 +03:00
|
|
|
file_path);
|
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
CHECK_RESULT(tar_archive_open(
|
|
|
|
archive, furi_string_get_cstr(file_path), TarOpenModeReadHeatshrink));
|
2022-05-06 19:26:25 +03:00
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
update_task_cleanup_resources(update_task);
|
2022-09-28 20:13:12 +03:00
|
|
|
|
2024-06-30 13:38:48 +03:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageResourcesFileUnpack, 0);
|
|
|
|
tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
|
|
|
|
CHECK_RESULT(tar_archive_unpack_to(archive, STORAGE_EXT_PATH_PREFIX, NULL));
|
2022-04-19 11:03:28 +03:00
|
|
|
}
|
2022-06-21 17:11:34 +03:00
|
|
|
|
|
|
|
if(update_task->state.groups & UpdateTaskStageGroupSplashscreen) {
|
|
|
|
update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 0);
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* tmp_path;
|
|
|
|
tmp_path = furi_string_alloc_set(update_task->update_path);
|
|
|
|
path_append(tmp_path, furi_string_get_cstr(update_task->manifest->splash_file));
|
2022-06-21 17:11:34 +03:00
|
|
|
if(storage_common_copy(
|
2022-07-26 15:21:51 +03:00
|
|
|
update_task->storage,
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_get_cstr(tmp_path),
|
2022-07-26 15:21:51 +03:00
|
|
|
INT_PATH(SLIDESHOW_FILE_NAME)) != FSE_OK) {
|
2022-06-21 17:11:34 +03:00
|
|
|
// actually, not critical
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(tmp_path);
|
2022-06-21 17:11:34 +03:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 100);
|
|
|
|
}
|
2022-04-27 18:53:48 +03:00
|
|
|
success = true;
|
2022-04-19 11:03:28 +03:00
|
|
|
} while(false);
|
|
|
|
|
2022-05-06 19:26:25 +03:00
|
|
|
tar_archive_free(archive);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(file_path);
|
2022-04-19 11:03:28 +03:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2022-04-13 23:50:25 +03:00
|
|
|
int32_t update_task_worker_backup_restore(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
UpdateTask* update_task = context;
|
|
|
|
|
2022-07-04 16:36:12 +03:00
|
|
|
FuriHalRtcBootMode boot_mode = update_task->boot_mode;
|
2022-04-13 23:50:25 +03:00
|
|
|
if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
|
2022-07-04 16:36:12 +03:00
|
|
|
/* no idea how we got here. Do nothing */
|
2022-04-13 23:50:25 +03:00
|
|
|
return UPDATE_TASK_NOERR;
|
|
|
|
}
|
|
|
|
|
2022-07-04 16:36:12 +03:00
|
|
|
bool success = false;
|
|
|
|
do {
|
|
|
|
if(!update_task_parse_manifest(update_task)) {
|
|
|
|
break;
|
|
|
|
}
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-07-04 16:36:12 +03:00
|
|
|
if(boot_mode == FuriHalRtcBootModePreUpdate) {
|
|
|
|
success = update_task_pre_update(update_task);
|
2022-12-26 15:13:30 +03:00
|
|
|
} else if(boot_mode == FuriHalRtcBootModePostUpdate) { //-V547
|
2022-07-04 16:36:12 +03:00
|
|
|
success = update_task_post_update(update_task);
|
|
|
|
if(success) {
|
|
|
|
update_operation_disarm();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(false);
|
2022-04-13 23:50:25 +03:00
|
|
|
|
2022-05-06 19:26:25 +03:00
|
|
|
if(!success) {
|
|
|
|
update_task_set_progress(update_task, UpdateTaskStageError, 0);
|
|
|
|
return UPDATE_TASK_FAILED;
|
2022-04-13 23:50:25 +03:00
|
|
|
}
|
|
|
|
|
2022-05-06 19:26:25 +03:00
|
|
|
update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
|
|
|
|
return UPDATE_TASK_NOERR;
|
2022-04-28 19:05:37 +03:00
|
|
|
}
|