update_task_worker_backup.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_set_progress(update_task, UpdateTaskStageLfsBackup, 0);
  25. /* to avoid bootloops */
  26. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  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. TarArchive* archive = tar_archive_alloc(update_task->storage);
  53. do {
  54. path_concat(
  55. string_get_cstr(update_task->update_path), LFS_BACKUP_DEFAULT_FILENAME, file_path);
  56. update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
  57. update_operation_disarm();
  58. CHECK_RESULT(lfs_backup_unpack(update_task->storage, string_get_cstr(file_path)));
  59. if(update_task->state.groups & UpdateTaskStageGroupResources) {
  60. TarUnpackProgress progress = {
  61. .update_task = update_task,
  62. .total_files = 0,
  63. .processed_files = 0,
  64. };
  65. update_task_set_progress(update_task, UpdateTaskStageResourcesUpdate, 0);
  66. path_concat(
  67. string_get_cstr(update_task->update_path),
  68. string_get_cstr(update_task->manifest->resource_bundle),
  69. file_path);
  70. tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
  71. CHECK_RESULT(
  72. tar_archive_open(archive, string_get_cstr(file_path), TAR_OPEN_MODE_READ));
  73. progress.total_files = tar_archive_get_entries_count(archive);
  74. if(progress.total_files > 0) {
  75. CHECK_RESULT(tar_archive_unpack_to(archive, EXT_PATH));
  76. }
  77. }
  78. if(update_task->state.groups & UpdateTaskStageGroupSplashscreen) {
  79. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 0);
  80. string_t tmp_path;
  81. string_init_set(tmp_path, update_task->update_path);
  82. path_append(tmp_path, string_get_cstr(update_task->manifest->splash_file));
  83. if(storage_common_copy(
  84. update_task->storage, string_get_cstr(tmp_path), "/int/slideshow") != FSE_OK) {
  85. // actually, not critical
  86. }
  87. string_clear(tmp_path);
  88. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 100);
  89. }
  90. success = true;
  91. } while(false);
  92. tar_archive_free(archive);
  93. string_clear(file_path);
  94. return success;
  95. }
  96. int32_t update_task_worker_backup_restore(void* context) {
  97. furi_assert(context);
  98. UpdateTask* update_task = context;
  99. bool success = false;
  100. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  101. if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
  102. /* no idea how we got here. Clear to normal boot */
  103. update_operation_disarm();
  104. return UPDATE_TASK_NOERR;
  105. }
  106. if(!update_task_parse_manifest(update_task)) {
  107. return UPDATE_TASK_FAILED;
  108. }
  109. /* Waiting for BT service to 'start', so we don't race for boot mode flag */
  110. furi_record_open("bt");
  111. furi_record_close("bt");
  112. if(boot_mode == FuriHalRtcBootModePreUpdate) {
  113. success = update_task_pre_update(update_task);
  114. } else if(boot_mode == FuriHalRtcBootModePostUpdate) {
  115. success = update_task_post_update(update_task);
  116. }
  117. if(!success) {
  118. update_task_set_progress(update_task, UpdateTaskStageError, 0);
  119. return UPDATE_TASK_FAILED;
  120. }
  121. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  122. return UPDATE_TASK_NOERR;
  123. }