update_task_worker_flasher.c 14 KB

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