From 06025a2b33c0f44ca88c913d821a900744c60430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sat, 23 Jul 2022 13:02:21 +0200 Subject: [PATCH] dfu: download writes partition data --- examples/device/dfu_freertos/src/main.c | 52 +++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/examples/device/dfu_freertos/src/main.c b/examples/device/dfu_freertos/src/main.c index 206f30dd4..2711ee106 100644 --- a/examples/device/dfu_freertos/src/main.c +++ b/examples/device/dfu_freertos/src/main.c @@ -210,6 +210,34 @@ void tud_dfu_download_cb(uint8_t alt, uint16_t block_num, uint8_t const* data, u return; } + // get handle for OTA update + if (0 == ota_handle) { + const esp_partition_t *ota_part = esp_ota_get_next_update_partition(NULL); + if (NULL == ota_part) { + ESP_LOGE(TAG, "OTA not found"); + tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG); + return; + } + rc = esp_ota_begin(ota_part, OTA_SIZE_UNKNOWN, &ota_handle); + if (ESP_OK != rc) { + ESP_LOGE(TAG, "init OTA failed"); + esp_ota_abort(ota_handle); + ota_handle = 0; + tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG); + return; + } + } + + // write data to partition + rc = esp_ota_write_with_offset(ota_handle, data, length, block_num * CFG_TUD_DFU_XFER_BUFSIZE); + if (ESP_OK != rc) { + ESP_LOGE(TAG, "writing OTA failed"); + esp_ota_abort(ota_handle); + ota_handle = 0; + tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG); + return; + } + // flashing op for download complete without error tud_dfu_finish_flashing(DFU_STATUS_OK); } @@ -223,6 +251,30 @@ void tud_dfu_manifest_cb(uint8_t alt) esp_err_t rc; ESP_LOGI(TAG, "download completed, enter manifestation"); + // finish flashing + if (ota_handle) { + rc = esp_ota_end(ota_handle); + ota_handle = 0; + if (ESP_OK != rc) { + ESP_LOGE(TAG, "close OTA failed"); + tud_dfu_finish_flashing(DFU_STATUS_ERR_PROG); + return; + } + } + + // switch to OTA app + const esp_partition_t *ota = esp_ota_get_next_update_partition(NULL); + esp_app_desc_t ota_desc; + esp_ota_get_partition_description(ota, &ota_desc); + if (ESP_APP_DESC_MAGIC_WORD == ota_desc.magic_word) { + ESP_LOGI(TAG, "boot set to valid app"); + esp_ota_set_boot_partition(ota); + } else { + ESP_LOGI(TAG, "no valid app"); + tud_dfu_finish_flashing(DFU_STATUS_ERR_VERIFY); + return; + } + // flashing op for manifest is complete without error // Application can perform checksum, should it fail, use appropriate status such as errVERIFY. tud_dfu_finish_flashing(DFU_STATUS_OK);