update_task_worker_backup.c 6.7 KB

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