update_task_worker_backup.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <desktop/helpers/slideshow_filename.h>
  7. #include <toolbox/path.h>
  8. #include <update_util/dfu_file.h>
  9. #include <update_util/lfs_backup.h>
  10. #include <update_util/update_operation.h>
  11. #include <update_util/resources/manifest.h>
  12. #include <toolbox/tar/tar_archive.h>
  13. #include <toolbox/crc32_calc.h>
  14. #define TAG "UpdWorkerBackup"
  15. #define CHECK_RESULT(x) \
  16. if(!(x)) { \
  17. break; \
  18. }
  19. static bool update_task_pre_update(UpdateTask* update_task) {
  20. bool success = false;
  21. FuriString* backup_file_path;
  22. backup_file_path = furi_string_alloc();
  23. path_concat(
  24. furi_string_get_cstr(update_task->update_path),
  25. LFS_BACKUP_DEFAULT_FILENAME,
  26. backup_file_path);
  27. update_task_set_progress(update_task, UpdateTaskStageLfsBackup, 0);
  28. /* to avoid bootloops */
  29. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  30. if((success =
  31. lfs_backup_create(update_task->storage, furi_string_get_cstr(backup_file_path)))) {
  32. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeUpdate);
  33. }
  34. furi_string_free(backup_file_path);
  35. return success;
  36. }
  37. typedef struct {
  38. UpdateTask* update_task;
  39. int32_t total_files, processed_files;
  40. } TarUnpackProgress;
  41. static bool update_task_resource_unpack_cb(const char* name, bool is_directory, void* context) {
  42. UNUSED(name);
  43. UNUSED(is_directory);
  44. TarUnpackProgress* unpack_progress = context;
  45. unpack_progress->processed_files++;
  46. update_task_set_progress(
  47. unpack_progress->update_task,
  48. UpdateTaskStageProgress,
  49. /* For this stage, last 70% of progress = extraction */
  50. 30 + (unpack_progress->processed_files * 70) / (unpack_progress->total_files + 1));
  51. return true;
  52. }
  53. static void
  54. update_task_cleanup_resources(UpdateTask* update_task, uint32_t n_approx_file_entries) {
  55. ResourceManifestReader* manifest_reader = resource_manifest_reader_alloc(update_task->storage);
  56. do {
  57. FURI_LOG_I(TAG, "Cleaning up old manifest");
  58. if(!resource_manifest_reader_open(manifest_reader, EXT_PATH("Manifest"))) {
  59. FURI_LOG_W(TAG, "No existing manifest");
  60. break;
  61. }
  62. /* We got # of entries in TAR file. Approx 1/4th is dir entries, we skip them */
  63. n_approx_file_entries = n_approx_file_entries * 3 / 4 + 1;
  64. uint32_t n_processed_files = 0;
  65. ResourceManifestEntry* entry_ptr = NULL;
  66. while((entry_ptr = resource_manifest_reader_next(manifest_reader))) {
  67. if(entry_ptr->type == ResourceManifestEntryTypeFile) {
  68. update_task_set_progress(
  69. update_task,
  70. UpdateTaskStageProgress,
  71. /* For this stage, first 30% of progress = cleanup */
  72. (n_processed_files++ * 30) / (n_approx_file_entries + 1));
  73. FuriString* file_path = furi_string_alloc();
  74. path_concat(
  75. STORAGE_EXT_PATH_PREFIX, furi_string_get_cstr(entry_ptr->name), file_path);
  76. FURI_LOG_D(TAG, "Removing %s", furi_string_get_cstr(file_path));
  77. storage_simply_remove(update_task->storage, furi_string_get_cstr(file_path));
  78. furi_string_free(file_path);
  79. }
  80. }
  81. } while(false);
  82. resource_manifest_reader_free(manifest_reader);
  83. }
  84. static bool update_task_post_update(UpdateTask* update_task) {
  85. bool success = false;
  86. FuriString* file_path;
  87. file_path = furi_string_alloc();
  88. TarArchive* archive = tar_archive_alloc(update_task->storage);
  89. do {
  90. path_concat(
  91. furi_string_get_cstr(update_task->update_path),
  92. LFS_BACKUP_DEFAULT_FILENAME,
  93. file_path);
  94. update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
  95. CHECK_RESULT(lfs_backup_unpack(update_task->storage, furi_string_get_cstr(file_path)));
  96. if(update_task->state.groups & UpdateTaskStageGroupResources) {
  97. TarUnpackProgress progress = {
  98. .update_task = update_task,
  99. .total_files = 0,
  100. .processed_files = 0,
  101. };
  102. update_task_set_progress(update_task, UpdateTaskStageResourcesUpdate, 0);
  103. path_concat(
  104. furi_string_get_cstr(update_task->update_path),
  105. furi_string_get_cstr(update_task->manifest->resource_bundle),
  106. file_path);
  107. tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
  108. CHECK_RESULT(
  109. tar_archive_open(archive, furi_string_get_cstr(file_path), TAR_OPEN_MODE_READ));
  110. progress.total_files = tar_archive_get_entries_count(archive);
  111. if(progress.total_files > 0) {
  112. update_task_cleanup_resources(update_task, progress.total_files);
  113. CHECK_RESULT(tar_archive_unpack_to(archive, STORAGE_EXT_PATH_PREFIX, NULL));
  114. }
  115. }
  116. if(update_task->state.groups & UpdateTaskStageGroupSplashscreen) {
  117. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 0);
  118. FuriString* tmp_path;
  119. tmp_path = furi_string_alloc_set(update_task->update_path);
  120. path_append(tmp_path, furi_string_get_cstr(update_task->manifest->splash_file));
  121. if(storage_common_copy(
  122. update_task->storage,
  123. furi_string_get_cstr(tmp_path),
  124. INT_PATH(SLIDESHOW_FILE_NAME)) != FSE_OK) {
  125. // actually, not critical
  126. }
  127. furi_string_free(tmp_path);
  128. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 100);
  129. }
  130. success = true;
  131. } while(false);
  132. tar_archive_free(archive);
  133. furi_string_free(file_path);
  134. return success;
  135. }
  136. int32_t update_task_worker_backup_restore(void* context) {
  137. furi_assert(context);
  138. UpdateTask* update_task = context;
  139. FuriHalRtcBootMode boot_mode = update_task->boot_mode;
  140. if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
  141. /* no idea how we got here. Do nothing */
  142. return UPDATE_TASK_NOERR;
  143. }
  144. bool success = false;
  145. do {
  146. if(!update_task_parse_manifest(update_task)) {
  147. break;
  148. }
  149. if(boot_mode == FuriHalRtcBootModePreUpdate) {
  150. success = update_task_pre_update(update_task);
  151. } else if(boot_mode == FuriHalRtcBootModePostUpdate) {
  152. success = update_task_post_update(update_task);
  153. if(success) {
  154. update_operation_disarm();
  155. }
  156. }
  157. } while(false);
  158. if(!success) {
  159. update_task_set_progress(update_task, UpdateTaskStageError, 0);
  160. return UPDATE_TASK_FAILED;
  161. }
  162. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  163. return UPDATE_TASK_NOERR;
  164. }