8#include <esp_ota_ops.h>
9#include <esp_task_wdt.h>
10#include <spi_flash_mmap.h>
14static const char *
const TAG =
"ota.idf";
16std::unique_ptr<IDFOTABackend>
make_ota_backend() {
return make_unique<IDFOTABackend>(); }
19#ifdef USE_OTA_ROLLBACK
23 esp_ota_mark_app_valid_cancel_rollback();
26 this->partition_ = esp_ota_get_next_update_partition(
nullptr);
27 if (this->partition_ ==
nullptr) {
31#if CONFIG_ESP_TASK_WDT_TIMEOUT_S < 15
33 esp_task_wdt_config_t wdtc;
34 wdtc.idle_core_mask = 0;
35#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
36 wdtc.idle_core_mask |= (1 << 0);
38#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
39 wdtc.idle_core_mask |= (1 << 1);
41 wdtc.timeout_ms = 15000;
42 wdtc.trigger_panic =
false;
43 esp_task_wdt_reconfigure(&wdtc);
46 esp_err_t err = esp_ota_begin(this->partition_, image_size, &this->update_handle_);
48#if CONFIG_ESP_TASK_WDT_TIMEOUT_S < 15
50 wdtc.timeout_ms = CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000;
51 esp_task_wdt_reconfigure(&wdtc);
55 esp_ota_abort(this->update_handle_);
56 this->update_handle_ = 0;
57 if (err == ESP_ERR_INVALID_SIZE) {
59 }
else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
69 memcpy(this->expected_bin_md5_, expected_md5, 32);
70 this->md5_set_ =
true;
74 esp_err_t err = esp_ota_write(this->update_handle_, data,
len);
77 if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
79 }
else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
90 if (!this->md5_.
equals_hex(this->expected_bin_md5_)) {
95 esp_err_t err = esp_ota_end(this->update_handle_);
96 this->update_handle_ = 0;
98 err = esp_ota_set_boot_partition(this->partition_);
103 if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
104#ifdef USE_OTA_SIGNED_VERIFICATION
105 ESP_LOGE(TAG,
"OTA validation failed (err=0x%X) - possible signature verification failure", err);
111 if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
118 esp_ota_abort(this->update_handle_);
119 this->update_handle_ = 0;
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash.
void calculate() override
Compute the digest, based on the provided data.
void add(const uint8_t *data, size_t len) override
Add bytes of data for the digest.
void init() override
Initialize a new MD5 digest computation.
void set_update_md5(const char *md5)
OTAResponseTypes write(uint8_t *data, size_t len)
OTAResponseTypes begin(size_t image_size)
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
@ OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE
@ OTA_RESPONSE_ERROR_WRITING_FLASH
@ OTA_RESPONSE_ERROR_UPDATE_END
@ OTA_RESPONSE_ERROR_SIGNATURE_INVALID
@ OTA_RESPONSE_ERROR_UNKNOWN
@ OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION
@ OTA_RESPONSE_ERROR_MAGIC
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()