update_task_worker_backup.c 8.6 KB

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