update_task_worker_flasher.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. bool success = false;
  119. UpdateManifest* manifest = update_task->manifest;
  120. do {
  121. FURI_LOG_W(TAG, "Writing stack");
  122. update_task_set_progress(update_task, UpdateTaskStageRadioImageValidate, 0);
  123. CHECK_RESULT(update_task_open_file(update_task, manifest->radio_image));
  124. CHECK_RESULT(
  125. crc32_calc_file(update_task->file, &update_task_file_progress, update_task) ==
  126. manifest->radio_crc);
  127. CHECK_RESULT(update_task_write_stack_data(update_task));
  128. update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 0);
  129. CHECK_RESULT(
  130. ble_glue_fus_stack_install(manifest->radio_address, 0) != BleGlueCommandResultError);
  131. update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 80);
  132. CHECK_RESULT(ble_glue_fus_wait_operation() == BleGlueCommandResultOK);
  133. update_task_set_progress(update_task, UpdateTaskStageRadioInstall, 100);
  134. /* ...system will restart here. */
  135. update_task_wait_for_restart(update_task);
  136. success = true;
  137. } while(false);
  138. return success;
  139. }
  140. static bool update_task_remove_stack(UpdateTask* update_task) {
  141. bool success = false;
  142. do {
  143. FURI_LOG_W(TAG, "Removing stack");
  144. update_task_set_progress(update_task, UpdateTaskStageRadioErase, 30);
  145. CHECK_RESULT(ble_glue_fus_stack_delete() != BleGlueCommandResultError);
  146. update_task_set_progress(update_task, UpdateTaskStageRadioErase, 80);
  147. CHECK_RESULT(ble_glue_fus_wait_operation() == BleGlueCommandResultOK);
  148. update_task_set_progress(update_task, UpdateTaskStageRadioErase, 100);
  149. /* ...system will restart here. */
  150. update_task_wait_for_restart(update_task);
  151. success = true;
  152. } while(false);
  153. return success;
  154. }
  155. static bool update_task_manage_radiostack(UpdateTask* update_task) {
  156. bool success = false;
  157. do {
  158. CHECK_RESULT(ble_glue_wait_for_c2_start(FURI_HAL_BT_C2_START_TIMEOUT));
  159. const BleGlueC2Info* c2_state = ble_glue_get_c2_info();
  160. const UpdateManifestRadioVersion* radio_ver = &update_task->manifest->radio_version;
  161. bool stack_version_match = (c2_state->VersionMajor == radio_ver->version.major) &&
  162. (c2_state->VersionMinor == radio_ver->version.minor) &&
  163. (c2_state->VersionSub == radio_ver->version.sub) &&
  164. (c2_state->VersionBranch == radio_ver->version.branch) &&
  165. (c2_state->VersionReleaseType == radio_ver->version.release);
  166. bool stack_missing = (c2_state->VersionMajor == 0) && (c2_state->VersionMinor == 0);
  167. if(c2_state->mode == BleGlueC2ModeStack) {
  168. /* Stack type is not available when we have FUS running. */
  169. bool total_stack_match = stack_version_match &&
  170. (c2_state->StackType == radio_ver->version.type);
  171. if(total_stack_match) {
  172. /* Nothing to do. */
  173. FURI_LOG_W(TAG, "Stack version is up2date");
  174. furi_hal_rtc_reset_flag(FuriHalRtcFlagC2Update);
  175. success = true;
  176. break;
  177. } else {
  178. /* Version or type mismatch. Let's boot to FUS and start updating. */
  179. FURI_LOG_W(TAG, "Restarting to FUS");
  180. furi_hal_rtc_set_flag(FuriHalRtcFlagC2Update);
  181. CHECK_RESULT(furi_hal_bt_ensure_c2_mode(BleGlueC2ModeFUS));
  182. /* ...system will restart here. */
  183. update_task_wait_for_restart(update_task);
  184. }
  185. } else if(c2_state->mode == BleGlueC2ModeFUS) {
  186. /* OK, we're in FUS mode. */
  187. update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 10);
  188. FURI_LOG_W(TAG, "Waiting for FUS to settle");
  189. ble_glue_fus_wait_operation();
  190. if(stack_version_match) {
  191. /* We can't check StackType with FUS, but partial version matches */
  192. if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagC2Update)) {
  193. /* This flag was set when full version was checked.
  194. * And something in versions of the stack didn't match.
  195. * So, clear the flag and drop the stack. */
  196. furi_hal_rtc_reset_flag(FuriHalRtcFlagC2Update);
  197. FURI_LOG_W(TAG, "Forcing stack removal (match)");
  198. CHECK_RESULT(update_task_remove_stack(update_task));
  199. } else {
  200. /* We might just had the stack installed.
  201. * Let's start it up to check its version */
  202. FURI_LOG_W(TAG, "Starting stack to check full version");
  203. update_task_set_progress(update_task, UpdateTaskStageRadioBusy, 40);
  204. CHECK_RESULT(furi_hal_bt_ensure_c2_mode(BleGlueC2ModeStack));
  205. /* ...system will restart here. */
  206. update_task_wait_for_restart(update_task);
  207. }
  208. } else {
  209. if(stack_missing) {
  210. /* Install stack. */
  211. CHECK_RESULT(update_task_write_stack(update_task));
  212. } else {
  213. CHECK_RESULT(update_task_remove_stack(update_task));
  214. }
  215. }
  216. }
  217. } while(false);
  218. return success;
  219. }
  220. bool update_task_validate_optionbytes(UpdateTask* update_task) {
  221. update_task_set_progress(update_task, UpdateTaskStageOBValidation, 0);
  222. bool match = true;
  223. bool ob_dirty = false;
  224. const UpdateManifest* manifest = update_task->manifest;
  225. const FuriHalFlashRawOptionByteData* device_data = furi_hal_flash_ob_get_raw_ptr();
  226. for(size_t idx = 0; idx < FURI_HAL_FLASH_OB_TOTAL_VALUES; ++idx) {
  227. update_task_set_progress(
  228. update_task, UpdateTaskStageProgress, idx * 100 / FURI_HAL_FLASH_OB_TOTAL_VALUES);
  229. const uint32_t ref_value = manifest->ob_reference.obs[idx].values.base;
  230. const uint32_t device_ob_value = device_data->obs[idx].values.base;
  231. const uint32_t device_ob_value_masked = device_ob_value &
  232. manifest->ob_compare_mask.obs[idx].values.base;
  233. if(ref_value != device_ob_value_masked) {
  234. match = false;
  235. FURI_LOG_E(
  236. TAG,
  237. "OB MISMATCH: #%d: real %08lX != %08lX (exp.), full %08lX",
  238. idx,
  239. device_ob_value_masked,
  240. ref_value,
  241. device_ob_value);
  242. /* any bits we are allowed to write?.. */
  243. bool can_patch = ((device_ob_value_masked ^ ref_value) &
  244. manifest->ob_write_mask.obs[idx].values.base) != 0;
  245. if(can_patch) {
  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 %08lX", 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. FURI_LOG_W(
  261. TAG,
  262. "OB #%d is FUBAR (fixed&masked %08lX, not %08lX)",
  263. idx,
  264. patched_value,
  265. ref_value);
  266. }
  267. }
  268. } else {
  269. FURI_LOG_D(
  270. TAG,
  271. "OB MATCH: #%d: real %08lX == %08lX (exp.)",
  272. idx,
  273. device_ob_value_masked,
  274. ref_value);
  275. }
  276. }
  277. if(!match) {
  278. update_task_set_progress(update_task, UpdateTaskStageOBError, 0);
  279. }
  280. if(ob_dirty) {
  281. FURI_LOG_W(TAG, "OBs were changed, applying");
  282. furi_hal_flash_ob_apply();
  283. }
  284. return match;
  285. }
  286. int32_t update_task_worker_flash_writer(void* context) {
  287. furi_assert(context);
  288. UpdateTask* update_task = context;
  289. bool success = false;
  290. do {
  291. CHECK_RESULT(update_task_parse_manifest(update_task));
  292. if(update_task->state.groups & UpdateTaskStageGroupRadio) {
  293. CHECK_RESULT(update_task_manage_radiostack(update_task));
  294. }
  295. if(update_task->state.groups & UpdateTaskStageGroupOptionBytes) {
  296. CHECK_RESULT(update_task_validate_optionbytes(update_task));
  297. }
  298. if(update_task->state.groups & UpdateTaskStageGroupFirmware) {
  299. CHECK_RESULT(update_task_write_dfu(update_task));
  300. }
  301. furi_hal_rtc_set_boot_mode(FuriHalRtcBootModePostUpdate);
  302. // Format LFS before restoring backup on next boot
  303. furi_hal_rtc_set_flag(FuriHalRtcFlagFactoryReset);
  304. update_task_set_progress(update_task, UpdateTaskStageCompleted, 100);
  305. success = true;
  306. } while(false);
  307. if(!success) {
  308. update_task_set_progress(update_task, UpdateTaskStageError, 0);
  309. return UPDATE_TASK_FAILED;
  310. }
  311. return UPDATE_TASK_NOERR;
  312. }