update_task_worker_backup.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "update_task.h"
  2. #include "update_task_i.h"
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <storage/storage.h>
  6. #include <toolbox/path.h>
  7. #include <update_util/dfu_file.h>
  8. #include <update_util/lfs_backup.h>
  9. #include <update_util/update_operation.h>
  10. #include <toolbox/tar/tar_archive.h>
  11. #include <toolbox/crc32_calc.h>
  12. #define TAG "UpdWorkerBackup"
  13. #define CHECK_RESULT(x) \
  14. if(!(x)) { \
  15. break; \
  16. }
  17. #define EXT_PATH "/ext"
  18. static bool update_task_pre_update(UpdateTask* update_task) {
  19. bool success = false;
  20. string_t backup_file_path;
  21. string_init(backup_file_path);
  22. path_concat(
  23. string_get_cstr(update_task->update_path), LFS_BACKUP_DEFAULT_FILENAME, backup_file_path);
  24. update_task->state.total_stages = 1;
  25. update_task_set_progress(update_task, UpdateTaskStageLfsBackup, 0);
  26. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal); // to avoid bootloops
  27. if((success = lfs_backup_create(update_task->storage, string_get_cstr(backup_file_path)))) {
  28. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeUpdate);
  29. }
  30. string_clear(backup_file_path);
  31. return success;
  32. }
  33. typedef struct {
  34. UpdateTask* update_task;
  35. int32_t total_files, processed_files;
  36. } TarUnpackProgress;
  37. static bool update_task_resource_unpack_cb(const char* name, bool is_directory, void* context) {
  38. UNUSED(name);
  39. UNUSED(is_directory);
  40. TarUnpackProgress* unpack_progress = context;
  41. unpack_progress->processed_files++;
  42. update_task_set_progress(
  43. unpack_progress->update_task,
  44. UpdateTaskStageProgress,
  45. unpack_progress->processed_files * 100 / (unpack_progress->total_files + 1));
  46. return true;
  47. }
  48. static bool update_task_post_update(UpdateTask* update_task) {
  49. bool success = false;
  50. string_t file_path;
  51. string_init(file_path);
  52. // status text is too long, too few stages to bother with a counter
  53. update_task->state.total_stages = 0;
  54. do {
  55. CHECK_RESULT(update_task_parse_manifest(update_task));
  56. path_concat(
  57. string_get_cstr(update_task->update_path), LFS_BACKUP_DEFAULT_FILENAME, file_path);
  58. bool unpack_resources = !string_empty_p(update_task->manifest->resource_bundle);
  59. if(unpack_resources) {
  60. update_task->state.total_stages++;
  61. }
  62. update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
  63. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  64. CHECK_RESULT(lfs_backup_unpack(update_task->storage, string_get_cstr(file_path)));
  65. if(unpack_resources) {
  66. TarUnpackProgress progress = {
  67. .update_task = update_task,
  68. .total_files = 0,
  69. .processed_files = 0,
  70. };
  71. update_task_set_progress(update_task, UpdateTaskStageResourcesUpdate, 0);
  72. path_concat(
  73. string_get_cstr(update_task->update_path),
  74. string_get_cstr(update_task->manifest->resource_bundle),
  75. file_path);
  76. TarArchive* archive = tar_archive_alloc(update_task->storage);
  77. tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
  78. success = tar_archive_open(archive, string_get_cstr(file_path), TAR_OPEN_MODE_READ);
  79. if(success) {
  80. progress.total_files = tar_archive_get_entries_count(archive);
  81. if(progress.total_files > 0) {
  82. tar_archive_unpack_to(archive, EXT_PATH);
  83. }
  84. }
  85. tar_archive_free(archive);
  86. }
  87. success = true;
  88. } while(false);
  89. string_clear(file_path);
  90. return success;
  91. }
  92. int32_t update_task_worker_backup_restore(void* context) {
  93. furi_assert(context);
  94. UpdateTask* update_task = context;
  95. bool success = false;
  96. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  97. if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
  98. // no idea how we got here. Clear to normal boot
  99. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  100. return UPDATE_TASK_NOERR;
  101. }
  102. update_task->state.current_stage_idx = 0;
  103. if(!update_operation_get_current_package_path(update_task->storage, update_task->update_path)) {
  104. return UPDATE_TASK_FAILED;
  105. }
  106. if(boot_mode == FuriHalRtcBootModePreUpdate) {
  107. success = update_task_pre_update(update_task);
  108. } else if(boot_mode == FuriHalRtcBootModePostUpdate) {
  109. success = update_task_post_update(update_task);
  110. }
  111. if(success) {
  112. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  113. } else {
  114. update_task_set_progress(update_task, UpdateTaskStageError, update_task->state.progress);
  115. }
  116. return success ? UPDATE_TASK_NOERR : UPDATE_TASK_FAILED;
  117. }