esp_flasher_worker.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "esp_flasher_worker.h"
  2. FuriStreamBuffer* flash_rx_stream; // TODO make safe
  3. EspFlasherApp* global_app; // TODO make safe
  4. FuriTimer* timer; // TODO make
  5. static uint32_t _remaining_time = 0;
  6. static void _timer_callback(void* context) {
  7. UNUSED(context);
  8. if(_remaining_time > 0) {
  9. _remaining_time--;
  10. }
  11. }
  12. static esp_loader_error_t _flash_file(EspFlasherApp* app, char* filepath, uint32_t addr) {
  13. // TODO cleanup
  14. esp_loader_error_t err;
  15. static uint8_t payload[1024];
  16. File* bin_file = storage_file_alloc(app->storage);
  17. char user_msg[256];
  18. // open file
  19. if(!storage_file_open(bin_file, filepath, FSAM_READ, FSOM_OPEN_EXISTING)) {
  20. storage_file_close(bin_file);
  21. storage_file_free(bin_file);
  22. dialog_message_show_storage_error(app->dialogs, "Cannot open file");
  23. return ESP_LOADER_ERROR_FAIL;
  24. }
  25. uint64_t size = storage_file_size(bin_file);
  26. loader_port_debug_print("Erasing flash...this may take a while\n");
  27. err = esp_loader_flash_start(addr, size, sizeof(payload));
  28. if(err != ESP_LOADER_SUCCESS) {
  29. storage_file_close(bin_file);
  30. storage_file_free(bin_file);
  31. snprintf(user_msg, sizeof(user_msg), "Erasing flash failed with error %d\n", err);
  32. loader_port_debug_print(user_msg);
  33. return err;
  34. }
  35. loader_port_debug_print("Start programming\n");
  36. uint64_t last_updated = size;
  37. while(size > 0) {
  38. if((last_updated - size) > 50000) {
  39. // inform user every 50k bytes
  40. // TODO: draw a progress bar next update
  41. snprintf(user_msg, sizeof(user_msg), "%llu bytes left.\n", size);
  42. loader_port_debug_print(user_msg);
  43. last_updated = size;
  44. }
  45. size_t to_read = MIN(size, sizeof(payload));
  46. uint16_t num_bytes = storage_file_read(bin_file, payload, to_read);
  47. err = esp_loader_flash_write(payload, num_bytes);
  48. if(err != ESP_LOADER_SUCCESS) {
  49. snprintf(user_msg, sizeof(user_msg), "Packet could not be written! Error: %u\n", err);
  50. storage_file_close(bin_file);
  51. storage_file_free(bin_file);
  52. loader_port_debug_print(user_msg);
  53. return err;
  54. }
  55. size -= num_bytes;
  56. }
  57. loader_port_debug_print("Finished programming\n");
  58. // TODO verify
  59. storage_file_close(bin_file);
  60. storage_file_free(bin_file);
  61. return ESP_LOADER_SUCCESS;
  62. }
  63. typedef struct {
  64. SelectedFlashOptions selected;
  65. const char* description;
  66. char* path;
  67. uint32_t addr;
  68. } FlashItem;
  69. static void _flash_all_files(EspFlasherApp* app) {
  70. esp_loader_error_t err;
  71. const int num_steps = app->num_selected_flash_options;
  72. #define NUM_FLASH_ITEMS 6
  73. FlashItem items[NUM_FLASH_ITEMS] = {
  74. {SelectedFlashBoot,
  75. "bootloader",
  76. app->bin_file_path_boot,
  77. app->selected_flash_options[SelectedFlashS3Mode] ? ESP_ADDR_BOOT_S3 : ESP_ADDR_BOOT},
  78. {SelectedFlashPart, "partition table", app->bin_file_path_part, ESP_ADDR_PART},
  79. {SelectedFlashNvs, "NVS", app->bin_file_path_nvs, ESP_ADDR_NVS},
  80. {SelectedFlashBootApp0, "boot_app0", app->bin_file_path_boot_app0, ESP_ADDR_BOOT_APP0},
  81. {SelectedFlashApp, "firmware", app->bin_file_path_app, ESP_ADDR_APP},
  82. {SelectedFlashCustom, "custom data", app->bin_file_path_custom, 0x0},
  83. /* if you add more entries, update NUM_FLASH_ITEMS above! */
  84. };
  85. char user_msg[256];
  86. int current_step = 1;
  87. for(FlashItem* item = &items[0]; item < &items[NUM_FLASH_ITEMS]; ++item) {
  88. if(app->selected_flash_options[item->selected]) {
  89. snprintf(
  90. user_msg,
  91. sizeof(user_msg),
  92. "Flashing %s (%d/%d) to address 0x%lx\n",
  93. item->description,
  94. current_step++,
  95. num_steps,
  96. item->addr);
  97. loader_port_debug_print(user_msg);
  98. err = _flash_file(app, item->path, item->addr);
  99. if(err) {
  100. break;
  101. }
  102. }
  103. }
  104. }
  105. static int32_t esp_flasher_flash_bin(void* context) {
  106. EspFlasherApp* app = (void*)context;
  107. esp_loader_error_t err;
  108. app->flash_worker_busy = true;
  109. // alloc global objects
  110. flash_rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  111. timer = furi_timer_alloc(_timer_callback, FuriTimerTypePeriodic, app);
  112. loader_port_debug_print("Connecting\n");
  113. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  114. err = esp_loader_connect(&connect_config);
  115. if(err != ESP_LOADER_SUCCESS) {
  116. char err_msg[256];
  117. snprintf(
  118. err_msg,
  119. sizeof(err_msg),
  120. "Cannot connect to target. Error: %u\nMake sure the device is in bootloader/reflash mode, then try again.\n",
  121. err);
  122. loader_port_debug_print(err_msg);
  123. }
  124. #if 0 // still getting packet drops with this
  125. // higher BR
  126. if(!err) {
  127. loader_port_debug_print("Increasing speed for faster flash\n");
  128. err = esp_loader_change_transmission_rate(230400);
  129. if (err != ESP_LOADER_SUCCESS) {
  130. char err_msg[256];
  131. snprintf(
  132. err_msg,
  133. sizeof(err_msg),
  134. "Cannot change transmission rate. Error: %u\n",
  135. err);
  136. loader_port_debug_print(err_msg);
  137. }
  138. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  139. }
  140. #endif
  141. if(!err) {
  142. loader_port_debug_print("Connected\n");
  143. _flash_all_files(app);
  144. #if 0
  145. loader_port_debug_print("Restoring transmission rate\n");
  146. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  147. #endif
  148. loader_port_debug_print("Done flashing. Please reset the board manually.\n");
  149. }
  150. // done
  151. app->flash_worker_busy = false;
  152. // cleanup
  153. furi_stream_buffer_free(flash_rx_stream);
  154. flash_rx_stream = NULL;
  155. furi_timer_free(timer);
  156. return 0;
  157. }
  158. void esp_flasher_worker_start_thread(EspFlasherApp* app) {
  159. global_app = app;
  160. app->flash_worker = furi_thread_alloc();
  161. furi_thread_set_name(app->flash_worker, "EspFlasherFlashWorker");
  162. furi_thread_set_stack_size(app->flash_worker, 2048);
  163. furi_thread_set_context(app->flash_worker, app);
  164. furi_thread_set_callback(app->flash_worker, esp_flasher_flash_bin);
  165. furi_thread_start(app->flash_worker);
  166. }
  167. void esp_flasher_worker_stop_thread(EspFlasherApp* app) {
  168. furi_thread_join(app->flash_worker);
  169. furi_thread_free(app->flash_worker);
  170. }
  171. esp_loader_error_t loader_port_read(uint8_t* data, uint16_t size, uint32_t timeout) {
  172. size_t read = furi_stream_buffer_receive(flash_rx_stream, data, size, pdMS_TO_TICKS(timeout));
  173. if(read < size) {
  174. return ESP_LOADER_ERROR_TIMEOUT;
  175. } else {
  176. return ESP_LOADER_SUCCESS;
  177. }
  178. }
  179. esp_loader_error_t loader_port_write(const uint8_t* data, uint16_t size, uint32_t timeout) {
  180. UNUSED(timeout);
  181. esp_flasher_uart_tx((uint8_t*)data, size);
  182. return ESP_LOADER_SUCCESS;
  183. }
  184. void loader_port_enter_bootloader(void) {
  185. // unimplemented
  186. }
  187. void loader_port_delay_ms(uint32_t ms) {
  188. furi_delay_ms(ms);
  189. }
  190. void loader_port_start_timer(uint32_t ms) {
  191. _remaining_time = ms;
  192. furi_timer_start(timer, pdMS_TO_TICKS(1));
  193. }
  194. uint32_t loader_port_remaining_time(void) {
  195. return _remaining_time;
  196. }
  197. extern void esp_flasher_console_output_handle_rx_data_cb(
  198. uint8_t* buf,
  199. size_t len,
  200. void* context); // TODO cleanup
  201. void loader_port_debug_print(const char* str) {
  202. if(global_app)
  203. esp_flasher_console_output_handle_rx_data_cb((uint8_t*)str, strlen(str), global_app);
  204. }
  205. void loader_port_spi_set_cs(uint32_t level) {
  206. UNUSED(level);
  207. // unimplemented
  208. }
  209. void esp_flasher_worker_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
  210. UNUSED(context);
  211. if(flash_rx_stream) {
  212. furi_stream_buffer_send(flash_rx_stream, buf, len, 0);
  213. } else {
  214. // done flashing
  215. if(global_app) esp_flasher_console_output_handle_rx_data_cb(buf, len, global_app);
  216. }
  217. }