update_task.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. static const char* update_task_stage_descr[] = {
  11. [UpdateTaskStageProgress] = "...",
  12. [UpdateTaskStageReadManifest] = "Loading update manifest",
  13. [UpdateTaskStageValidateDFUImage] = "Checking DFU file",
  14. [UpdateTaskStageFlashWrite] = "Writing flash",
  15. [UpdateTaskStageFlashValidate] = "Validating flash",
  16. [UpdateTaskStageRadioImageValidate] = "Checking radio FW",
  17. [UpdateTaskStageRadioErase] = "Uninstalling radio FW",
  18. [UpdateTaskStageRadioWrite] = "Writing radio FW",
  19. [UpdateTaskStageRadioInstall] = "Installing radio FW",
  20. [UpdateTaskStageRadioBusy] = "Radio is updating",
  21. [UpdateTaskStageOBValidation] = "Validating opt. bytes",
  22. [UpdateTaskStageLfsBackup] = "Backing up LFS",
  23. [UpdateTaskStageLfsRestore] = "Restoring LFS",
  24. [UpdateTaskStageResourcesUpdate] = "Updating resources",
  25. [UpdateTaskStageSplashscreenInstall] = "Installing splashscreen",
  26. [UpdateTaskStageCompleted] = "Restarting...",
  27. [UpdateTaskStageError] = "Error",
  28. [UpdateTaskStageOBError] = "OB, report",
  29. };
  30. typedef struct {
  31. UpdateTaskStageGroup group;
  32. uint8_t weight;
  33. } UpdateTaskStageGroupMap;
  34. #define STAGE_DEF(GROUP, WEIGHT) \
  35. { .group = (GROUP), .weight = (WEIGHT), }
  36. static const UpdateTaskStageGroupMap update_task_stage_progress[] = {
  37. [UpdateTaskStageProgress] = STAGE_DEF(UpdateTaskStageGroupMisc, 0),
  38. [UpdateTaskStageReadManifest] = STAGE_DEF(UpdateTaskStageGroupPreUpdate, 5),
  39. [UpdateTaskStageLfsBackup] = STAGE_DEF(UpdateTaskStageGroupPreUpdate, 15),
  40. [UpdateTaskStageRadioImageValidate] = STAGE_DEF(UpdateTaskStageGroupRadio, 10),
  41. [UpdateTaskStageRadioErase] = STAGE_DEF(UpdateTaskStageGroupRadio, 50),
  42. [UpdateTaskStageRadioWrite] = STAGE_DEF(UpdateTaskStageGroupRadio, 90),
  43. [UpdateTaskStageRadioInstall] = STAGE_DEF(UpdateTaskStageGroupRadio, 15),
  44. [UpdateTaskStageRadioBusy] = STAGE_DEF(UpdateTaskStageGroupRadio, 60),
  45. [UpdateTaskStageOBValidation] = STAGE_DEF(UpdateTaskStageGroupOptionBytes, 10),
  46. [UpdateTaskStageValidateDFUImage] = STAGE_DEF(UpdateTaskStageGroupFirmware, 100),
  47. [UpdateTaskStageFlashWrite] = STAGE_DEF(UpdateTaskStageGroupFirmware, 200),
  48. [UpdateTaskStageFlashValidate] = STAGE_DEF(UpdateTaskStageGroupFirmware, 50),
  49. [UpdateTaskStageLfsRestore] = STAGE_DEF(UpdateTaskStageGroupPostUpdate, 30),
  50. [UpdateTaskStageResourcesUpdate] = STAGE_DEF(UpdateTaskStageGroupResources, 255),
  51. [UpdateTaskStageSplashscreenInstall] = STAGE_DEF(UpdateTaskStageGroupSplashscreen, 5),
  52. [UpdateTaskStageCompleted] = STAGE_DEF(UpdateTaskStageGroupMisc, 1),
  53. [UpdateTaskStageError] = STAGE_DEF(UpdateTaskStageGroupMisc, 1),
  54. [UpdateTaskStageOBError] = STAGE_DEF(UpdateTaskStageGroupMisc, 1),
  55. };
  56. static UpdateTaskStageGroup update_task_get_task_groups(UpdateTask* update_task) {
  57. UpdateTaskStageGroup ret = UpdateTaskStageGroupPreUpdate | UpdateTaskStageGroupPostUpdate;
  58. UpdateManifest* manifest = update_task->manifest;
  59. if(!string_empty_p(manifest->radio_image)) {
  60. ret |= UpdateTaskStageGroupRadio;
  61. }
  62. if(update_manifest_has_obdata(manifest)) {
  63. ret |= UpdateTaskStageGroupOptionBytes;
  64. }
  65. if(!string_empty_p(manifest->firmware_dfu_image)) {
  66. ret |= UpdateTaskStageGroupFirmware;
  67. }
  68. if(!string_empty_p(manifest->resource_bundle)) {
  69. ret |= UpdateTaskStageGroupResources;
  70. }
  71. if(!string_empty_p(manifest->splash_file)) {
  72. ret |= UpdateTaskStageGroupSplashscreen;
  73. }
  74. return ret;
  75. }
  76. static void update_task_calc_completed_stages(UpdateTask* update_task) {
  77. uint32_t completed_stages_points = 0;
  78. for(UpdateTaskStage past_stage = UpdateTaskStageProgress;
  79. past_stage < update_task->state.stage;
  80. ++past_stage) {
  81. const UpdateTaskStageGroupMap* grp_descr = &update_task_stage_progress[past_stage];
  82. if((grp_descr->group & update_task->state.groups) == 0) {
  83. continue;
  84. }
  85. completed_stages_points += grp_descr->weight;
  86. }
  87. update_task->state.completed_stages_points = completed_stages_points;
  88. }
  89. void update_task_set_progress(UpdateTask* update_task, UpdateTaskStage stage, uint8_t progress) {
  90. if(stage != UpdateTaskStageProgress) {
  91. /* do not override more specific error states */
  92. if((stage >= UpdateTaskStageError) && (update_task->state.stage >= UpdateTaskStageError)) {
  93. return;
  94. }
  95. /* Build error message with code "[stage_idx-stage_percent]" */
  96. if(stage >= UpdateTaskStageError) {
  97. string_printf(
  98. update_task->state.status,
  99. "%s #[%d-%d]",
  100. update_task_stage_descr[stage],
  101. update_task->state.stage,
  102. update_task->state.stage_progress);
  103. } else {
  104. string_set_str(update_task->state.status, update_task_stage_descr[stage]);
  105. }
  106. /* Store stage update */
  107. update_task->state.stage = stage;
  108. /* If we are still alive, sum completed stages weights */
  109. if((stage > UpdateTaskStageProgress) && (stage < UpdateTaskStageCompleted)) {
  110. update_task_calc_completed_stages(update_task);
  111. }
  112. }
  113. /* Store stage progress for all non-error updates - to provide details on error state */
  114. if(!update_stage_is_error(stage)) {
  115. update_task->state.stage_progress = progress;
  116. }
  117. /* Calculate "overall" progress, based on stage weights */
  118. uint32_t adapted_progress = 1;
  119. if(update_task->state.total_progress_points != 0) {
  120. if(stage < UpdateTaskStageCompleted) {
  121. adapted_progress = MIN(
  122. (update_task->state.completed_stages_points +
  123. (update_task_stage_progress[update_task->state.stage].weight * progress / 100)) *
  124. 100 / (update_task->state.total_progress_points),
  125. 100u);
  126. } else {
  127. adapted_progress = update_task->state.overall_progress;
  128. }
  129. }
  130. update_task->state.overall_progress = adapted_progress;
  131. if(update_task->status_change_cb) {
  132. (update_task->status_change_cb)(
  133. string_get_cstr(update_task->state.status),
  134. adapted_progress,
  135. update_stage_is_error(update_task->state.stage),
  136. update_task->status_change_cb_state);
  137. }
  138. }
  139. static void update_task_close_file(UpdateTask* update_task) {
  140. furi_assert(update_task);
  141. if(!storage_file_is_open(update_task->file)) {
  142. return;
  143. }
  144. storage_file_close(update_task->file);
  145. }
  146. static bool update_task_check_file_exists(UpdateTask* update_task, string_t filename) {
  147. furi_assert(update_task);
  148. string_t tmp_path;
  149. string_init_set(tmp_path, update_task->update_path);
  150. path_append(tmp_path, string_get_cstr(filename));
  151. bool exists =
  152. (storage_common_stat(update_task->storage, string_get_cstr(tmp_path), NULL) == FSE_OK);
  153. string_clear(tmp_path);
  154. return exists;
  155. }
  156. bool update_task_open_file(UpdateTask* update_task, string_t filename) {
  157. furi_assert(update_task);
  158. update_task_close_file(update_task);
  159. string_t tmp_path;
  160. string_init_set(tmp_path, update_task->update_path);
  161. path_append(tmp_path, string_get_cstr(filename));
  162. bool open_success = storage_file_open(
  163. update_task->file, string_get_cstr(tmp_path), FSAM_READ, FSOM_OPEN_EXISTING);
  164. string_clear(tmp_path);
  165. return open_success;
  166. }
  167. static void update_task_worker_thread_cb(FuriThreadState state, void* context) {
  168. UpdateTask* update_task = context;
  169. if(state != FuriThreadStateStopped) {
  170. return;
  171. }
  172. if(furi_thread_get_return_code(update_task->thread) == UPDATE_TASK_NOERR) {
  173. osDelay(UPDATE_DELAY_OPERATION_OK);
  174. furi_hal_power_reset();
  175. }
  176. }
  177. UpdateTask* update_task_alloc() {
  178. UpdateTask* update_task = malloc(sizeof(UpdateTask));
  179. update_task->state.stage = UpdateTaskStageProgress;
  180. update_task->state.stage_progress = 0;
  181. update_task->state.overall_progress = 0;
  182. string_init(update_task->state.status);
  183. update_task->manifest = update_manifest_alloc();
  184. update_task->storage = furi_record_open("storage");
  185. update_task->file = storage_file_alloc(update_task->storage);
  186. update_task->status_change_cb = NULL;
  187. string_init(update_task->update_path);
  188. FuriThread* thread = update_task->thread = furi_thread_alloc();
  189. furi_thread_set_name(thread, "UpdateWorker");
  190. furi_thread_set_stack_size(thread, 5120);
  191. furi_thread_set_context(thread, update_task);
  192. furi_thread_set_state_callback(thread, update_task_worker_thread_cb);
  193. furi_thread_set_state_context(thread, update_task);
  194. #ifdef FURI_RAM_EXEC
  195. UNUSED(update_task_worker_backup_restore);
  196. furi_thread_set_callback(thread, update_task_worker_flash_writer);
  197. #else
  198. UNUSED(update_task_worker_flash_writer);
  199. furi_thread_set_callback(thread, update_task_worker_backup_restore);
  200. #endif
  201. return update_task;
  202. }
  203. void update_task_free(UpdateTask* update_task) {
  204. furi_assert(update_task);
  205. furi_thread_join(update_task->thread);
  206. furi_thread_free(update_task->thread);
  207. update_task_close_file(update_task);
  208. storage_file_free(update_task->file);
  209. update_manifest_free(update_task->manifest);
  210. furi_record_close("storage");
  211. string_clear(update_task->update_path);
  212. free(update_task);
  213. }
  214. bool update_task_parse_manifest(UpdateTask* update_task) {
  215. furi_assert(update_task);
  216. update_task->state.stage_progress = 0;
  217. update_task->state.overall_progress = 0;
  218. update_task->state.total_progress_points = 0;
  219. update_task->state.completed_stages_points = 0;
  220. update_task->state.groups = 0;
  221. update_task_set_progress(update_task, UpdateTaskStageReadManifest, 0);
  222. bool result = false;
  223. string_t manifest_path;
  224. string_init(manifest_path);
  225. do {
  226. update_task_set_progress(update_task, UpdateTaskStageProgress, 13);
  227. if(!furi_hal_version_do_i_belong_here()) {
  228. break;
  229. }
  230. update_task_set_progress(update_task, UpdateTaskStageProgress, 20);
  231. if(!update_operation_get_current_package_manifest_path(
  232. update_task->storage, manifest_path)) {
  233. break;
  234. }
  235. path_extract_dirname(string_get_cstr(manifest_path), update_task->update_path);
  236. update_task_set_progress(update_task, UpdateTaskStageProgress, 30);
  237. UpdateManifest* manifest = update_task->manifest;
  238. if(!update_manifest_init(manifest, string_get_cstr(manifest_path))) {
  239. break;
  240. }
  241. update_task_set_progress(update_task, UpdateTaskStageProgress, 40);
  242. if(manifest->manifest_version < UPDATE_OPERATION_MIN_MANIFEST_VERSION) {
  243. break;
  244. }
  245. update_task_set_progress(update_task, UpdateTaskStageProgress, 50);
  246. if(manifest->target != furi_hal_version_get_hw_target()) {
  247. break;
  248. }
  249. update_task->state.groups = update_task_get_task_groups(update_task);
  250. for(size_t stage_counter = 0; stage_counter < COUNT_OF(update_task_stage_progress);
  251. ++stage_counter) {
  252. const UpdateTaskStageGroupMap* grp_descr = &update_task_stage_progress[stage_counter];
  253. if((grp_descr->group & update_task->state.groups) != 0) {
  254. update_task->state.total_progress_points += grp_descr->weight;
  255. }
  256. }
  257. update_task_set_progress(update_task, UpdateTaskStageProgress, 60);
  258. if((update_task->state.groups & UpdateTaskStageGroupFirmware) &&
  259. !update_task_check_file_exists(update_task, manifest->firmware_dfu_image)) {
  260. break;
  261. }
  262. update_task_set_progress(update_task, UpdateTaskStageProgress, 80);
  263. if((update_task->state.groups & UpdateTaskStageGroupRadio) &&
  264. (!update_task_check_file_exists(update_task, manifest->radio_image) ||
  265. (manifest->radio_version.version.type == 0))) {
  266. break;
  267. }
  268. update_task_set_progress(update_task, UpdateTaskStageProgress, 100);
  269. result = true;
  270. } while(false);
  271. string_clear(manifest_path);
  272. return result;
  273. }
  274. void update_task_set_progress_cb(UpdateTask* update_task, updateProgressCb cb, void* state) {
  275. update_task->status_change_cb = cb;
  276. update_task->status_change_cb_state = state;
  277. }
  278. void update_task_start(UpdateTask* update_task) {
  279. furi_assert(update_task);
  280. furi_thread_start(update_task->thread);
  281. }
  282. bool update_task_is_running(UpdateTask* update_task) {
  283. furi_assert(update_task);
  284. return furi_thread_get_state(update_task->thread) == FuriThreadStateRunning;
  285. }
  286. UpdateTaskState const* update_task_get_state(UpdateTask* update_task) {
  287. furi_assert(update_task);
  288. return &update_task->state;
  289. }
  290. UpdateManifest const* update_task_get_manifest(UpdateTask* update_task) {
  291. furi_assert(update_task);
  292. return update_task->manifest;
  293. }