update_task_worker_backup.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. FS_Error result =
  87. storage_common_remove(update_task->storage, furi_string_get_cstr(file_path));
  88. if(result != FSE_OK && result != FSE_EXIST) {
  89. FURI_LOG_E(
  90. TAG,
  91. "%s remove failed, cause %s",
  92. furi_string_get_cstr(file_path),
  93. storage_error_get_desc(result));
  94. }
  95. furi_string_free(file_path);
  96. } else if(entry_ptr->type == ResourceManifestEntryTypeDirectory) {
  97. n_dir_entries++;
  98. }
  99. }
  100. n_processed_entries = 0;
  101. while((entry_ptr = resource_manifest_reader_previous(manifest_reader))) {
  102. if(entry_ptr->type == ResourceManifestEntryTypeDirectory) {
  103. update_task_set_progress(
  104. update_task,
  105. UpdateTaskStageProgress,
  106. /* For this stage, second 10% of progress = cleanup directories */
  107. UpdateTaskResourcesWeightsFileCleanup +
  108. (n_processed_entries++ * UpdateTaskResourcesWeightsDirCleanup) /
  109. n_dir_entries);
  110. FuriString* folder_path = furi_string_alloc();
  111. do {
  112. path_concat(
  113. STORAGE_EXT_PATH_PREFIX,
  114. furi_string_get_cstr(entry_ptr->name),
  115. folder_path);
  116. FURI_LOG_D(TAG, "Removing folder %s", furi_string_get_cstr(folder_path));
  117. FS_Error result = storage_common_remove(
  118. update_task->storage, furi_string_get_cstr(folder_path));
  119. if(result != FSE_OK && result != FSE_EXIST) {
  120. FURI_LOG_E(
  121. TAG,
  122. "%s remove failed, cause %s",
  123. furi_string_get_cstr(folder_path),
  124. storage_error_get_desc(result));
  125. }
  126. } while(false);
  127. furi_string_free(folder_path);
  128. }
  129. }
  130. } while(false);
  131. resource_manifest_reader_free(manifest_reader);
  132. }
  133. static bool update_task_post_update(UpdateTask* update_task) {
  134. bool success = false;
  135. FuriString* file_path;
  136. file_path = furi_string_alloc();
  137. TarArchive* archive = tar_archive_alloc(update_task->storage);
  138. do {
  139. path_concat(
  140. furi_string_get_cstr(update_task->update_path),
  141. LFS_BACKUP_DEFAULT_FILENAME,
  142. file_path);
  143. update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
  144. CHECK_RESULT(lfs_backup_unpack(update_task->storage, furi_string_get_cstr(file_path)));
  145. if(update_task->state.groups & UpdateTaskStageGroupResources) {
  146. TarUnpackProgress progress = {
  147. .update_task = update_task,
  148. .total_files = 0,
  149. .processed_files = 0,
  150. };
  151. update_task_set_progress(update_task, UpdateTaskStageResourcesUpdate, 0);
  152. path_concat(
  153. furi_string_get_cstr(update_task->update_path),
  154. furi_string_get_cstr(update_task->manifest->resource_bundle),
  155. file_path);
  156. tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
  157. CHECK_RESULT(
  158. tar_archive_open(archive, furi_string_get_cstr(file_path), TAR_OPEN_MODE_READ));
  159. progress.total_files = tar_archive_get_entries_count(archive);
  160. if(progress.total_files > 0) {
  161. update_task_cleanup_resources(update_task, progress.total_files);
  162. CHECK_RESULT(tar_archive_unpack_to(archive, STORAGE_EXT_PATH_PREFIX, NULL));
  163. }
  164. }
  165. if(update_task->state.groups & UpdateTaskStageGroupSplashscreen) {
  166. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 0);
  167. FuriString* tmp_path;
  168. tmp_path = furi_string_alloc_set(update_task->update_path);
  169. path_append(tmp_path, furi_string_get_cstr(update_task->manifest->splash_file));
  170. if(storage_common_copy(
  171. update_task->storage,
  172. furi_string_get_cstr(tmp_path),
  173. INT_PATH(SLIDESHOW_FILE_NAME)) != FSE_OK) {
  174. // actually, not critical
  175. }
  176. furi_string_free(tmp_path);
  177. update_task_set_progress(update_task, UpdateTaskStageSplashscreenInstall, 100);
  178. }
  179. success = true;
  180. } while(false);
  181. tar_archive_free(archive);
  182. furi_string_free(file_path);
  183. return success;
  184. }
  185. int32_t update_task_worker_backup_restore(void* context) {
  186. furi_assert(context);
  187. UpdateTask* update_task = context;
  188. FuriHalRtcBootMode boot_mode = update_task->boot_mode;
  189. if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
  190. /* no idea how we got here. Do nothing */
  191. return UPDATE_TASK_NOERR;
  192. }
  193. bool success = false;
  194. do {
  195. if(!update_task_parse_manifest(update_task)) {
  196. break;
  197. }
  198. if(boot_mode == FuriHalRtcBootModePreUpdate) {
  199. success = update_task_pre_update(update_task);
  200. } else if(boot_mode == FuriHalRtcBootModePostUpdate) { //-V547
  201. success = update_task_post_update(update_task);
  202. if(success) {
  203. update_operation_disarm();
  204. }
  205. }
  206. } while(false);
  207. if(!success) {
  208. update_task_set_progress(update_task, UpdateTaskStageError, 0);
  209. return UPDATE_TASK_FAILED;
  210. }
  211. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  212. return UPDATE_TASK_NOERR;
  213. }