esp_flasher_worker.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, "bootloader", app->bin_file_path_boot, app->selected_flash_options[SelectedFlashS3Mode] ? ESP_ADDR_BOOT_S3 : ESP_ADDR_BOOT },
  75. { SelectedFlashPart, "partition table", app->bin_file_path_part, ESP_ADDR_PART },
  76. { SelectedFlashNvs, "NVS", app->bin_file_path_nvs, ESP_ADDR_NVS },
  77. { SelectedFlashBootApp0, "boot_app0", app->bin_file_path_boot_app0, ESP_ADDR_BOOT_APP0 },
  78. { SelectedFlashApp, "firmware", app->bin_file_path_app, ESP_ADDR_APP },
  79. { SelectedFlashCustom, "custom data", app->bin_file_path_custom, 0x0 },
  80. /* if you add more entries, update NUM_FLASH_ITEMS above! */
  81. };
  82. char user_msg[256];
  83. int current_step = 1;
  84. for (FlashItem* item = &items[0]; item < &items[NUM_FLASH_ITEMS]; ++item) {
  85. if(app->selected_flash_options[item->selected]) {
  86. snprintf(user_msg, sizeof(user_msg), "Flashing %s (%d/%d) to address 0x%lx\n", item->description, current_step++, num_steps, item->addr);
  87. loader_port_debug_print(user_msg);
  88. err = _flash_file(app, item->path, item->addr);
  89. if(err) {
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. static int32_t esp_flasher_flash_bin(void* context) {
  96. EspFlasherApp* app = (void*)context;
  97. esp_loader_error_t err;
  98. app->flash_worker_busy = true;
  99. // alloc global objects
  100. flash_rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  101. timer = furi_timer_alloc(_timer_callback, FuriTimerTypePeriodic, app);
  102. loader_port_debug_print("Connecting\n");
  103. esp_loader_connect_args_t connect_config = ESP_LOADER_CONNECT_DEFAULT();
  104. err = esp_loader_connect(&connect_config);
  105. if(err != ESP_LOADER_SUCCESS) {
  106. char err_msg[256];
  107. snprintf(err_msg, sizeof(err_msg), "Cannot connect to target. Error: %u\nMake sure the device is in bootloader/reflash mode, then try again.\n", err);
  108. loader_port_debug_print(err_msg);
  109. }
  110. #if 0 // still getting packet drops with this
  111. // higher BR
  112. if(!err) {
  113. loader_port_debug_print("Increasing speed for faster flash\n");
  114. err = esp_loader_change_transmission_rate(230400);
  115. if (err != ESP_LOADER_SUCCESS) {
  116. char err_msg[256];
  117. snprintf(
  118. err_msg,
  119. sizeof(err_msg),
  120. "Cannot change transmission rate. Error: %u\n",
  121. err);
  122. loader_port_debug_print(err_msg);
  123. }
  124. furi_hal_uart_set_br(FuriHalUartIdUSART1, 230400);
  125. }
  126. #endif
  127. if(!err) {
  128. loader_port_debug_print("Connected\n");
  129. _flash_all_files(app);
  130. #if 0
  131. loader_port_debug_print("Restoring transmission rate\n");
  132. furi_hal_uart_set_br(FuriHalUartIdUSART1, 115200);
  133. #endif
  134. loader_port_debug_print("Done flashing. Please reset the board manually.\n");
  135. }
  136. // done
  137. app->flash_worker_busy = false;
  138. // cleanup
  139. furi_stream_buffer_free(flash_rx_stream);
  140. flash_rx_stream = NULL;
  141. furi_timer_free(timer);
  142. return 0;
  143. }
  144. void esp_flasher_worker_start_thread(EspFlasherApp* app) {
  145. global_app = app;
  146. app->flash_worker = furi_thread_alloc();
  147. furi_thread_set_name(app->flash_worker, "EspFlasherFlashWorker");
  148. furi_thread_set_stack_size(app->flash_worker, 2048);
  149. furi_thread_set_context(app->flash_worker, app);
  150. furi_thread_set_callback(app->flash_worker, esp_flasher_flash_bin);
  151. furi_thread_start(app->flash_worker);
  152. }
  153. void esp_flasher_worker_stop_thread(EspFlasherApp* app) {
  154. furi_thread_join(app->flash_worker);
  155. furi_thread_free(app->flash_worker);
  156. }
  157. esp_loader_error_t loader_port_read(uint8_t* data, uint16_t size, uint32_t timeout) {
  158. size_t read = furi_stream_buffer_receive(flash_rx_stream, data, size, pdMS_TO_TICKS(timeout));
  159. if(read < size) {
  160. return ESP_LOADER_ERROR_TIMEOUT;
  161. } else {
  162. return ESP_LOADER_SUCCESS;
  163. }
  164. }
  165. esp_loader_error_t loader_port_write(const uint8_t* data, uint16_t size, uint32_t timeout) {
  166. UNUSED(timeout);
  167. esp_flasher_uart_tx((uint8_t*)data, size);
  168. return ESP_LOADER_SUCCESS;
  169. }
  170. void loader_port_enter_bootloader(void) {
  171. // unimplemented
  172. }
  173. void loader_port_delay_ms(uint32_t ms) {
  174. furi_delay_ms(ms);
  175. }
  176. void loader_port_start_timer(uint32_t ms) {
  177. _remaining_time = ms;
  178. furi_timer_start(timer, pdMS_TO_TICKS(1));
  179. }
  180. uint32_t loader_port_remaining_time(void) {
  181. return _remaining_time;
  182. }
  183. extern void esp_flasher_console_output_handle_rx_data_cb(
  184. uint8_t* buf,
  185. size_t len,
  186. void* context); // TODO cleanup
  187. void loader_port_debug_print(const char* str) {
  188. if(global_app)
  189. esp_flasher_console_output_handle_rx_data_cb((uint8_t*)str, strlen(str), global_app);
  190. }
  191. void loader_port_spi_set_cs(uint32_t level) {
  192. UNUSED(level);
  193. // unimplemented
  194. }
  195. void esp_flasher_worker_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
  196. UNUSED(context);
  197. if(flash_rx_stream) {
  198. furi_stream_buffer_send(flash_rx_stream, buf, len, 0);
  199. } else {
  200. // done flashing
  201. if(global_app) esp_flasher_console_output_handle_rx_data_cb(buf, len, global_app);
  202. }
  203. }