update_task_worker_flasher.c 14 KB

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