update_task_workers.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 <toolbox/path.h>
  7. #include <update_util/dfu_file.h>
  8. #include <update_util/lfs_backup.h>
  9. #include <update_util/update_operation.h>
  10. #include <toolbox/tar/tar_archive.h>
  11. #define CHECK_RESULT(x) \
  12. if(!(x)) { \
  13. break; \
  14. }
  15. #define STM_DFU_VENDOR_ID 0x0483
  16. #define STM_DFU_PRODUCT_ID 0xDF11
  17. /* Written into DFU file by build pipeline */
  18. #define FLIPPER_ZERO_DFU_DEVICE_CODE 0xFFFF
  19. #define EXT_PATH "/ext"
  20. static const DfuValidationParams flipper_dfu_params = {
  21. .device = FLIPPER_ZERO_DFU_DEVICE_CODE,
  22. .product = STM_DFU_PRODUCT_ID,
  23. .vendor = STM_DFU_VENDOR_ID,
  24. };
  25. static void update_task_dfu_progress(const uint8_t progress, void* context) {
  26. UpdateTask* update_task = context;
  27. update_task_set_progress(update_task, UpdateTaskStageProgress, progress);
  28. }
  29. static bool page_task_compare_flash(
  30. const uint8_t i_page,
  31. const uint8_t* update_block,
  32. uint16_t update_block_len) {
  33. const size_t page_addr = furi_hal_flash_get_base() + furi_hal_flash_get_page_size() * i_page;
  34. return (memcmp(update_block, (void*)page_addr, update_block_len) == 0);
  35. }
  36. /* Verifies a flash operation address for fitting into writable memory
  37. */
  38. static bool check_address_boundaries(const size_t address) {
  39. const size_t min_allowed_address = furi_hal_flash_get_base();
  40. const size_t max_allowed_address = (size_t)furi_hal_flash_get_free_end_address();
  41. return ((address >= min_allowed_address) && (address < max_allowed_address));
  42. }
  43. int32_t update_task_worker_flash_writer(void* context) {
  44. furi_assert(context);
  45. UpdateTask* update_task = context;
  46. bool success = false;
  47. DfuUpdateTask page_task = {
  48. .address_cb = &check_address_boundaries,
  49. .progress_cb = &update_task_dfu_progress,
  50. .task_cb = &furi_hal_flash_program_page,
  51. .context = update_task,
  52. };
  53. update_task->state.current_stage_idx = 0;
  54. update_task->state.total_stages = 4;
  55. do {
  56. CHECK_RESULT(update_task_parse_manifest(update_task));
  57. if(!string_empty_p(update_task->manifest->firmware_dfu_image)) {
  58. update_task_set_progress(update_task, UpdateTaskStageValidateDFUImage, 0);
  59. CHECK_RESULT(
  60. update_task_open_file(update_task, update_task->manifest->firmware_dfu_image));
  61. CHECK_RESULT(
  62. dfu_file_validate_crc(update_task->file, &update_task_dfu_progress, update_task));
  63. const uint8_t valid_targets =
  64. dfu_file_validate_headers(update_task->file, &flipper_dfu_params);
  65. if(valid_targets == 0) {
  66. break;
  67. }
  68. update_task_set_progress(update_task, UpdateTaskStageFlashWrite, 0);
  69. CHECK_RESULT(dfu_file_process_targets(&page_task, update_task->file, valid_targets));
  70. page_task.task_cb = &page_task_compare_flash;
  71. update_task_set_progress(update_task, UpdateTaskStageFlashValidate, 0);
  72. CHECK_RESULT(dfu_file_process_targets(&page_task, update_task->file, valid_targets));
  73. }
  74. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  75. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePostUpdate);
  76. success = true;
  77. } while(false);
  78. if(!success) {
  79. update_task_set_progress(update_task, UpdateTaskStageError, update_task->state.progress);
  80. }
  81. return success ? UPDATE_TASK_NOERR : UPDATE_TASK_FAILED;
  82. }
  83. static bool update_task_pre_update(UpdateTask* update_task) {
  84. bool success = false;
  85. string_t backup_file_path;
  86. string_init(backup_file_path);
  87. path_concat(
  88. string_get_cstr(update_task->update_path), LFS_BACKUP_DEFAULT_FILENAME, backup_file_path);
  89. update_task->state.total_stages = 1;
  90. update_task_set_progress(update_task, UpdateTaskStageLfsBackup, 0);
  91. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal); // to avoid bootloops
  92. if((success = lfs_backup_create(update_task->storage, string_get_cstr(backup_file_path)))) {
  93. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeUpdate);
  94. }
  95. string_clear(backup_file_path);
  96. return success;
  97. }
  98. typedef struct {
  99. UpdateTask* update_task;
  100. int32_t total_files, processed_files;
  101. } TarUnpackProgress;
  102. static bool update_task_resource_unpack_cb(const char* name, bool is_directory, void* context) {
  103. UNUSED(name);
  104. UNUSED(is_directory);
  105. TarUnpackProgress* unpack_progress = context;
  106. unpack_progress->processed_files++;
  107. update_task_set_progress(
  108. unpack_progress->update_task,
  109. UpdateTaskStageProgress,
  110. unpack_progress->processed_files * 100 / (unpack_progress->total_files + 1));
  111. return true;
  112. }
  113. static bool update_task_post_update(UpdateTask* update_task) {
  114. bool success = false;
  115. string_t file_path;
  116. string_init(file_path);
  117. update_task->state.total_stages = 2;
  118. do {
  119. CHECK_RESULT(update_task_parse_manifest(update_task));
  120. path_concat(
  121. string_get_cstr(update_task->update_path), LFS_BACKUP_DEFAULT_FILENAME, file_path);
  122. bool unpack_resources = !string_empty_p(update_task->manifest->resource_bundle);
  123. if(unpack_resources) {
  124. update_task->state.total_stages++;
  125. }
  126. update_task_set_progress(update_task, UpdateTaskStageLfsRestore, 0);
  127. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  128. CHECK_RESULT(lfs_backup_unpack(update_task->storage, string_get_cstr(file_path)));
  129. if(unpack_resources) {
  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. string_get_cstr(update_task->update_path),
  138. string_get_cstr(update_task->manifest->resource_bundle),
  139. file_path);
  140. TarArchive* archive = tar_archive_alloc(update_task->storage);
  141. tar_archive_set_file_callback(archive, update_task_resource_unpack_cb, &progress);
  142. success = tar_archive_open(archive, string_get_cstr(file_path), TAR_OPEN_MODE_READ);
  143. if(success) {
  144. progress.total_files = tar_archive_get_entries_count(archive);
  145. if(progress.total_files > 0) {
  146. tar_archive_unpack_to(archive, EXT_PATH);
  147. }
  148. }
  149. tar_archive_free(archive);
  150. }
  151. } while(false);
  152. string_clear(file_path);
  153. return success;
  154. }
  155. int32_t update_task_worker_backup_restore(void* context) {
  156. furi_assert(context);
  157. UpdateTask* update_task = context;
  158. bool success = false;
  159. FuriHalRtcBootMode boot_mode = furi_hal_rtc_get_boot_mode();
  160. if((boot_mode != FuriHalRtcBootModePreUpdate) && (boot_mode != FuriHalRtcBootModePostUpdate)) {
  161. // no idea how we got here. Clear to normal boot
  162. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModeNormal);
  163. return UPDATE_TASK_NOERR;
  164. }
  165. update_task->state.current_stage_idx = 0;
  166. if(!update_operation_get_current_package_path(update_task->storage, update_task->update_path)) {
  167. return UPDATE_TASK_FAILED;
  168. }
  169. if(boot_mode == FuriHalRtcBootModePreUpdate) {
  170. success = update_task_pre_update(update_task);
  171. } else if(boot_mode == FuriHalRtcBootModePostUpdate) {
  172. success = update_task_post_update(update_task);
  173. }
  174. if(success) {
  175. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  176. } else {
  177. update_task_set_progress(update_task, UpdateTaskStageError, update_task->state.progress);
  178. }
  179. return success ? UPDATE_TASK_NOERR : UPDATE_TASK_FAILED;
  180. }