update_task_worker_backup.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 enum {
  38. UpdateTaskResourcesWeightsFileCleanup = 20,
  39. UpdateTaskResourcesWeightsDirCleanup = 20,
  40. UpdateTaskResourcesWeightsFileUnpack = 60,
  41. } UpdateTaskResourcesWeights;
  42. #define UPDATE_TASK_RESOURCES_FILE_TO_TOTAL_PERCENT 90
  43. typedef struct {
  44. UpdateTask* update_task;
  45. int32_t total_files, processed_files;
  46. } TarUnpackProgress;
  47. static bool update_task_resource_unpack_cb(const char* name, bool is_directory, void* context) {
  48. UNUSED(name);
  49. UNUSED(is_directory);
  50. TarUnpackProgress* unpack_progress = context;
  51. unpack_progress->processed_files++;
  52. update_task_set_progress(
  53. unpack_progress->update_task,
  54. UpdateTaskStageProgress,
  55. /* For this stage, last progress segment = extraction */
  56. (UpdateTaskResourcesWeightsFileCleanup + UpdateTaskResourcesWeightsDirCleanup) +
  57. (unpack_progress->processed_files * UpdateTaskResourcesWeightsFileUnpack) /
  58. (unpack_progress->total_files + 1));
  59. return true;
  60. }
  61. static void update_task_cleanup_resources(UpdateTask* update_task, const uint32_t n_tar_entries) {
  62. ResourceManifestReader* manifest_reader = resource_manifest_reader_alloc(update_task->storage);
  63. do {
  64. FURI_LOG_D(TAG, "Cleaning up old manifest");
  65. if(!resource_manifest_reader_open(manifest_reader, EXT_PATH("Manifest"))) {
  66. FURI_LOG_W(TAG, "No existing manifest");
  67. break;
  68. }
  69. const uint32_t n_approx_file_entries =
  70. n_tar_entries * UPDATE_TASK_RESOURCES_FILE_TO_TOTAL_PERCENT / 100 + 1;
  71. uint32_t n_dir_entries = 1;
  72. ResourceManifestEntry* entry_ptr = NULL;
  73. uint32_t n_processed_entries = 0;
  74. while((entry_ptr = resource_manifest_reader_next(manifest_reader))) {
  75. if(entry_ptr->type == ResourceManifestEntryTypeFile) {
  76. update_task_set_progress(
  77. update_task,
  78. UpdateTaskStageProgress,
  79. /* For this stage, first pass = old manifest's file cleanup */
  80. (n_processed_entries++ * UpdateTaskResourcesWeightsFileCleanup) /
  81. n_approx_file_entries);
  82. FuriString* file_path = furi_string_alloc();
  83. path_concat(
  84. STORAGE_EXT_PATH_PREFIX, furi_string_get_cstr(entry_ptr->name), file_path);
  85. FURI_LOG_D(TAG, "Removing %s", furi_string_get_cstr(file_path));
  86. storage_simply_remove(update_task->storage, furi_string_get_cstr(file_path));
  87. furi_string_free(file_path);
  88. } else if(entry_ptr->type == ResourceManifestEntryTypeDirectory) {
  89. n_dir_entries++;
  90. }
  91. }
  92. n_processed_entries = 0;
  93. while((entry_ptr = resource_manifest_reader_previous(manifest_reader))) {
  94. if(entry_ptr->type == ResourceManifestEntryTypeDirectory) {
  95. update_task_set_progress(
  96. update_task,
  97. UpdateTaskStageProgress,
  98. /* For this stage, second 10% of progress = cleanup directories */
  99. UpdateTaskResourcesWeightsFileCleanup +
  100. (n_processed_entries++ * UpdateTaskResourcesWeightsDirCleanup) /
  101. n_dir_entries);
  102. FuriString* folder_path = furi_string_alloc();
  103. File* folder_file = storage_file_alloc(update_task->storage);
  104. do {
  105. path_concat(
  106. STORAGE_EXT_PATH_PREFIX,
  107. furi_string_get_cstr(entry_ptr->name),
  108. folder_path);
  109. FURI_LOG_D(TAG, "Removing folder %s", furi_string_get_cstr(folder_path));
  110. if(!storage_dir_open(folder_file, furi_string_get_cstr(folder_path))) {
  111. FURI_LOG_W(
  112. TAG,
  113. "%s can't be opened, skipping",
  114. furi_string_get_cstr(folder_path));
  115. break;
  116. }
  117. if(storage_dir_read(folder_file, NULL, NULL, 0)) {
  118. FURI_LOG_I(
  119. TAG, "%s is not empty, skipping", furi_string_get_cstr(folder_path));
  120. break;
  121. }
  122. storage_simply_remove(update_task->storage, furi_string_get_cstr(folder_path));
  123. } while(false);
  124. storage_file_free(folder_file);
  125. furi_string_free(folder_path);
  126. }
  127. }
  128. } while(false);
  129. resource_manifest_reader_free(manifest_reader);
  130. }
  131. static bool update_task_post_update(UpdateTask* update_task) {
  132. bool success = false;
  133. FuriString* file_path;
  134. file_path = furi_string_alloc();
  135. TarArchive* archive = tar_archive_alloc(update_task->storage);
  136. do {
  137. path_concat(
  138. furi_string_get_cstr(update_task->update_path),
  139. LFS_BACKUP_DEFAULT_FILENAME,
  140. file_path);
  141. update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
  142. CHECK_RESULT(lfs_backup_unpack(update_task->storage, furi_string_get_cstr(file_path)));
  143. if(update_task->state.groups & UpdateTaskStageGroupResources) {
  144. TarUnpackProgress progress = {
  145. .update_task = update_task,
  146. .total_files = 0,
  147. .processed_files = 0,
  148. };
  149. update_task_set_progress(update_task, UpdateTaskStageResourcesUpdate, 0);
  150. path_concat(
  151. furi_string_get_cstr(update_task->update_path),
  152. furi_string_get_cstr(update_task->manifest->resource_bundle),
  153. file_path);
  154. tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
  155. CHECK_RESULT(
  156. tar_archive_open(archive, furi_string_get_cstr(file_path), TAR_OPEN_MODE_READ));
  157. progress.total_files = tar_archive_get_entries_count(archive);
  158. if(progress.total_files > 0) {
  159. update_task_cleanup_resources(update_task, progress.total_files);
  160. CHECK_RESULT(tar_archive_unpack_to(archive, STORAGE_EXT_PATH_PREFIX, NULL));
  161. }
  162. }
  163. if(update_task->state.groups & UpdateTaskStageGroupSplashscreen) {
  164. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 0);
  165. FuriString* tmp_path;
  166. tmp_path = furi_string_alloc_set(update_task->update_path);
  167. path_append(tmp_path, furi_string_get_cstr(update_task->manifest->splash_file));
  168. if(storage_common_copy(
  169. update_task->storage,
  170. furi_string_get_cstr(tmp_path),
  171. INT_PATH(SLIDESHOW_FILE_NAME)) != FSE_OK) {
  172. // actually, not critical
  173. }
  174. furi_string_free(tmp_path);
  175. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 100);
  176. }
  177. success = true;
  178. } while(false);
  179. tar_archive_free(archive);
  180. furi_string_free(file_path);
  181. return success;
  182. }
  183. int32_t update_task_worker_backup_restore(void* context) {
  184. furi_assert(context);
  185. UpdateTask* update_task = context;
  186. FuriHalRtcBootMode boot_mode = update_task->boot_mode;
  187. if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
  188. /* no idea how we got here. Do nothing */
  189. return UPDATE_TASK_NOERR;
  190. }
  191. bool success = false;
  192. do {
  193. if(!update_task_parse_manifest(update_task)) {
  194. break;
  195. }
  196. if(boot_mode == FuriHalRtcBootModePreUpdate) {
  197. success = update_task_pre_update(update_task);
  198. } else if(boot_mode == FuriHalRtcBootModePostUpdate) { //-V547
  199. success = update_task_post_update(update_task);
  200. if(success) {
  201. update_operation_disarm();
  202. }
  203. }
  204. } while(false);
  205. if(!success) {
  206. update_task_set_progress(update_task, UpdateTaskStageError, 0);
  207. return UPDATE_TASK_FAILED;
  208. }
  209. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  210. return UPDATE_TASK_NOERR;
  211. }