update_task_worker_flasher.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include <toolbox/crc32_calc.h>
  12. #define TAG "UpdWorkerRAM"
  13. #define CHECK_RESULT(x) \
  14. if(!(x)) { \
  15. break; \
  16. }
  17. #define STM_DFU_VENDOR_ID 0x0483
  18. #define STM_DFU_PRODUCT_ID 0xDF11
  19. /* Written into DFU file by build pipeline */
  20. #define FLIPPER_ZERO_DFU_DEVICE_CODE 0xFFFF
  21. /* Time, in ms, to wait for system restart by C2 before crashing */
  22. #define C2_MODE_SWITCH_TIMEOUT 10000
  23. static const DfuValidationParams flipper_dfu_params = {
  24. .device = FLIPPER_ZERO_DFU_DEVICE_CODE,
  25. .product = STM_DFU_PRODUCT_ID,
  26. .vendor = STM_DFU_VENDOR_ID,
  27. };
  28. static void update_task_file_progress(const uint8_t progress, void* context) {
  29. UpdateTask* update_task = context;
  30. update_task_set_progress(update_task, UpdateTaskStageProgress, progress);
  31. }
  32. static bool page_task_compare_flash(
  33. const uint8_t i_page,
  34. const uint8_t* update_block,
  35. uint16_t update_block_len) {
  36. const size_t page_addr = furi_hal_flash_get_base() + furi_hal_flash_get_page_size() * i_page;
  37. return (memcmp(update_block, (void*)page_addr, update_block_len) == 0);
  38. }
  39. /* Verifies a flash operation address for fitting into writable memory
  40. */
  41. static bool check_address_boundaries(const size_t address) {
  42. const size_t min_allowed_address = furi_hal_flash_get_base();
  43. const size_t max_allowed_address = (size_t)furi_hal_flash_get_free_end_address();
  44. return ((address >= min_allowed_address) && (address < max_allowed_address));
  45. }
  46. static bool update_task_write_dfu(UpdateTask* update_task) {
  47. DfuUpdateTask page_task = {
  48. .address_cb = &check_address_boundaries,
  49. .progress_cb = &update_task_file_progress,
  50. .task_cb = &furi_hal_flash_program_page,
  51. .context = update_task,
  52. };
  53. bool success = false;
  54. do {
  55. update_task_set_progress(update_task, UpdateTaskStageValidateDFUImage, 0);
  56. CHECK_RESULT(
  57. update_task_open_file(update_task, update_task->manifest->firmware_dfu_image));
  58. CHECK_RESULT(
  59. dfu_file_validate_crc(update_task->file, &update_task_file_progress, update_task));
  60. const uint8_t valid_targets =
  61. dfu_file_validate_headers(update_task->file, &flipper_dfu_params);
  62. if(valid_targets == 0) {
  63. break;
  64. }
  65. update_task_set_progress(update_task, UpdateTaskStageFlashWrite, 0);
  66. CHECK_RESULT(dfu_file_process_targets(&page_task, update_task->file, valid_targets));
  67. page_task.task_cb = &page_task_compare_flash;
  68. update_task_set_progress(update_task, UpdateTaskStageFlashValidate, 0);
  69. CHECK_RESULT(dfu_file_process_targets(&page_task, update_task->file, valid_targets));
  70. success = true;
  71. } while(false);
  72. return success;
  73. }
  74. static bool update_task_write_stack_data(UpdateTask* update_task) {
  75. furi_check(storage_file_is_open(update_task->file));
  76. const size_t FLASH_PAGE_SIZE = furi_hal_flash_get_page_size();
  77. uint32_t stack_size = storage_file_size(update_task->file);
  78. storage_file_seek(update_task->file, 0, true);
  79. if(!check_address_boundaries(update_task->manifest->radio_address) ||
  80. !check_address_boundaries(update_task->manifest->radio_address + stack_size)) {
  81. return false;
  82. }
  83. update_task_set_progress(update_task, UpdateTaskStageRadioWrite, 0);
  84. uint8_t* fw_block = malloc(FLASH_PAGE_SIZE);
  85. uint16_t bytes_read = 0;
  86. uint32_t element_offs = 0;
  87. while(element_offs < stack_size) {
  88. uint32_t n_bytes_to_read = FLASH_PAGE_SIZE;
  89. if((element_offs + n_bytes_to_read) > stack_size) {
  90. n_bytes_to_read = stack_size - element_offs;
  91. }
  92. bytes_read = storage_file_read(update_task->file, fw_block, n_bytes_to_read);
  93. if(bytes_read == 0) {
  94. break;
  95. }
  96. int16_t i_page =
  97. furi_hal_flash_get_page_number(update_task->manifest->radio_address + element_offs);
  98. if(i_page < 0) {
  99. break;
  100. }
  101. if(!furi_hal_flash_program_page(i_page, fw_block, bytes_read)) {
  102. break;
  103. }
  104. element_offs += bytes_read;
  105. update_task_set_progress(
  106. update_task, UpdateTaskStageProgress, element_offs * 100 / stack_size);
  107. }
  108. free(fw_block);
  109. return element_offs == stack_size;
  110. }
  111. static void update_task_wait_for_restart(UpdateTask* update_task) {
  112. update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 10);
  113. osDelay(C2_MODE_SWITCH_TIMEOUT);
  114. furi_crash("C2 timeout");
  115. }
  116. static bool update_task_write_stack(UpdateTask* update_task) {
  117. bool success = false;
  118. do {
  119. FURI_LOG_W(TAG, "Writing stack");
  120. update_task_set_progress(update_task, UpdateTaskStageRadioImageValidate, 0);
  121. CHECK_RESULT(update_task_open_file(update_task, update_task->manifest->radio_image));
  122. CHECK_RESULT(
  123. crc32_calc_file(update_task->file, &update_task_file_progress, update_task) ==
  124. update_task->manifest->radio_crc);
  125. CHECK_RESULT(update_task_write_stack_data(update_task));
  126. update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 0);
  127. CHECK_RESULT(
  128. ble_glue_fus_stack_install(update_task->manifest->radio_address, 0) !=
  129. BleGlueCommandResultError);
  130. update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 80);
  131. CHECK_RESULT(ble_glue_fus_wait_operation() == BleGlueCommandResultOK);
  132. update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 100);
  133. /* ...system will restart here. */
  134. update_task_wait_for_restart(update_task);
  135. success = true;
  136. } while(false);
  137. return success;
  138. }
  139. static bool update_task_remove_stack(UpdateTask* update_task) {
  140. bool success = false;
  141. do {
  142. FURI_LOG_W(TAG, "Removing stack");
  143. update_task_set_progress(update_task, UpdateTaskStageRadioErase, 30);
  144. CHECK_RESULT(ble_glue_fus_stack_delete() != BleGlueCommandResultError);
  145. update_task_set_progress(update_task, UpdateTaskStageRadioErase, 80);
  146. CHECK_RESULT(ble_glue_fus_wait_operation() == BleGlueCommandResultOK);
  147. update_task_set_progress(update_task, UpdateTaskStageRadioErase, 100);
  148. /* ...system will restart here. */
  149. update_task_wait_for_restart(update_task);
  150. success = true;
  151. } while(false);
  152. return success;
  153. }
  154. static bool update_task_manage_radiostack(UpdateTask* update_task) {
  155. bool success = false;
  156. do {
  157. CHECK_RESULT(ble_glue_wait_for_c2_start(FURI_HAL_BT_C2_START_TIMEOUT));
  158. const BleGlueC2Info* c2_state = ble_glue_get_c2_info();
  159. const UpdateManifestRadioVersion* radio_ver = &update_task->manifest->radio_version;
  160. bool stack_version_match = (c2_state->VersionMajor == radio_ver->version.major) &&
  161. (c2_state->VersionMinor == radio_ver->version.minor) &&
  162. (c2_state->VersionSub == radio_ver->version.sub) &&
  163. (c2_state->VersionBranch == radio_ver->version.branch) &&
  164. (c2_state->VersionReleaseType == radio_ver->version.release);
  165. bool stack_missing = (c2_state->VersionMajor == 0) && (c2_state->VersionMinor == 0);
  166. if(c2_state->mode == BleGlueC2ModeStack) {
  167. /* Stack type is not available when we have FUS running. */
  168. bool total_stack_match = stack_version_match &&
  169. (c2_state->StackType == radio_ver->version.type);
  170. if(total_stack_match) {
  171. /* Nothing to do. */
  172. FURI_LOG_W(TAG, "Stack version is up2date");
  173. furi_hal_rtc_reset_flag(FuriHalRtcFlagC2Update);
  174. success = true;
  175. break;
  176. } else {
  177. /* Version or type mismatch. Let's boot to FUS and start updating. */
  178. FURI_LOG_W(TAG, "Restarting to FUS");
  179. furi_hal_rtc_set_flag(FuriHalRtcFlagC2Update);
  180. CHECK_RESULT(furi_hal_bt_ensure_c2_mode(BleGlueC2ModeFUS));
  181. /* ...system will restart here. */
  182. update_task_wait_for_restart(update_task);
  183. }
  184. } else if(c2_state->mode == BleGlueC2ModeFUS) {
  185. /* OK, we're in FUS mode. */
  186. update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 10);
  187. FURI_LOG_W(TAG, "Waiting for FUS to settle");
  188. ble_glue_fus_wait_operation();
  189. if(stack_version_match) {
  190. /* We can't check StackType with FUS, but partial version matches */
  191. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagC2Update)) {
  192. /* This flag was set when full version was checked.
  193. * And something in versions of the stack didn't match.
  194. * So, clear the flag and drop the stack. */
  195. furi_hal_rtc_reset_flag(FuriHalRtcFlagC2Update);
  196. FURI_LOG_W(TAG, "Forcing stack removal (match)");
  197. CHECK_RESULT(update_task_remove_stack(update_task));
  198. } else {
  199. /* We might just had the stack installed.
  200. * Let's start it up to check its version */
  201. FURI_LOG_W(TAG, "Starting stack to check full version");
  202. update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 40);
  203. CHECK_RESULT(furi_hal_bt_ensure_c2_mode(BleGlueC2ModeStack));
  204. /* ...system will restart here. */
  205. update_task_wait_for_restart(update_task);
  206. }
  207. } else {
  208. if(stack_missing) {
  209. /* Install stack. */
  210. CHECK_RESULT(update_task_write_stack(update_task));
  211. } else {
  212. CHECK_RESULT(update_task_remove_stack(update_task));
  213. }
  214. }
  215. }
  216. } while(false);
  217. return success;
  218. }
  219. bool update_task_validate_optionbytes(UpdateTask* update_task) {
  220. update_task_set_progress(update_task, UpdateTaskStageOBValidation, 0);
  221. bool match = true;
  222. bool ob_dirty = false;
  223. const UpdateManifest* manifest = update_task->manifest;
  224. const FuriHalFlashRawOptionByteData* device_data = furi_hal_flash_ob_get_raw_ptr();
  225. for(size_t idx = 0; idx < FURI_HAL_FLASH_OB_TOTAL_VALUES; ++idx) {
  226. update_task_set_progress(
  227. update_task, UpdateTaskStageProgress, idx * 100 / FURI_HAL_FLASH_OB_TOTAL_VALUES);
  228. const uint32_t ref_value = manifest->ob_reference.obs[idx].values.base;
  229. const uint32_t device_ob_value = device_data->obs[idx].values.base;
  230. const uint32_t device_ob_value_masked = device_ob_value &
  231. manifest->ob_compare_mask.obs[idx].values.base;
  232. if(ref_value != device_ob_value_masked) {
  233. match = false;
  234. FURI_LOG_E(
  235. TAG,
  236. "OB MISMATCH: #%d: real %08X != %08X (exp.), full %08X",
  237. idx,
  238. device_ob_value_masked,
  239. ref_value,
  240. device_ob_value);
  241. /* any bits we are allowed to write?.. */
  242. bool can_patch = ((device_ob_value_masked ^ ref_value) &
  243. manifest->ob_write_mask.obs[idx].values.base) != 0;
  244. if(can_patch) {
  245. /* patch & restart loop */
  246. const uint32_t patched_value =
  247. /* take all non-writable bits from real value */
  248. (device_ob_value & ~(manifest->ob_write_mask.obs[idx].values.base)) |
  249. /* take all writable bits from reference value */
  250. (manifest->ob_reference.obs[idx].values.base &
  251. manifest->ob_write_mask.obs[idx].values.base);
  252. FURI_LOG_W(TAG, "Fixing up OB byte #%d to %08X", idx, patched_value);
  253. ob_dirty = true;
  254. bool is_fixed = furi_hal_flash_ob_set_word(idx, patched_value) &&
  255. ((device_data->obs[idx].values.base &
  256. manifest->ob_compare_mask.obs[idx].values.base) == ref_value);
  257. if(!is_fixed) {
  258. /* Things are so bad that fixing what we are allowed to still doesn't match
  259. * reference value
  260. */
  261. FURI_LOG_W(
  262. TAG,
  263. "OB #%d is FUBAR (fixed&masked %08X, not %08X)",
  264. idx,
  265. patched_value,
  266. ref_value);
  267. }
  268. }
  269. } else {
  270. FURI_LOG_I(
  271. TAG,
  272. "OB MATCH: #%d: real %08X == %08X (exp.)",
  273. idx,
  274. device_ob_value_masked,
  275. ref_value);
  276. }
  277. }
  278. if(!match) {
  279. update_task_set_progress(update_task, UpdateTaskStageOBError, 95);
  280. }
  281. if(ob_dirty) {
  282. FURI_LOG_W(TAG, "OB were changed, applying");
  283. furi_hal_flash_ob_apply();
  284. }
  285. return match;
  286. }
  287. int32_t update_task_worker_flash_writer(void* context) {
  288. furi_assert(context);
  289. UpdateTask* update_task = context;
  290. bool success = false;
  291. update_task->state.current_stage_idx = 0;
  292. update_task->state.total_stages = 0;
  293. do {
  294. CHECK_RESULT(update_task_parse_manifest(update_task));
  295. if(!string_empty_p(update_task->manifest->radio_image)) {
  296. CHECK_RESULT(update_task_manage_radiostack(update_task));
  297. }
  298. bool check_ob = update_manifest_has_obdata(update_task->manifest);
  299. if(check_ob) {
  300. update_task->state.total_stages++;
  301. CHECK_RESULT(update_task_validate_optionbytes(update_task));
  302. }
  303. if(!string_empty_p(update_task->manifest->firmware_dfu_image)) {
  304. update_task->state.total_stages += 4;
  305. CHECK_RESULT(update_task_write_dfu(update_task));
  306. }
  307. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePostUpdate);
  308. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  309. success = true;
  310. } while(false);
  311. return success ? UPDATE_TASK_NOERR : UPDATE_TASK_FAILED;
  312. }