ESPHome 2026.1.4
Loading...
Searching...
No Matches
ota_backend_esp_idf.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
3
6
7#include <esp_ota_ops.h>
8#include <esp_task_wdt.h>
9#include <spi_flash_mmap.h>
10
11namespace esphome {
12namespace ota {
13
14std::unique_ptr<ota::OTABackend> make_ota_backend() { return make_unique<ota::IDFOTABackend>(); }
15
17#ifdef USE_OTA_ROLLBACK
18 // If we're starting an OTA, the current boot is good enough - mark it valid
19 // to prevent rollback and allow the OTA to proceed even if the safe mode
20 // timer hasn't expired yet.
21 esp_ota_mark_app_valid_cancel_rollback();
22#endif
23
24 this->partition_ = esp_ota_get_next_update_partition(nullptr);
25 if (this->partition_ == nullptr) {
27 }
28
29#if CONFIG_ESP_TASK_WDT_TIMEOUT_S < 15
30 // The following function takes longer than the 5 seconds timeout of WDT
31 esp_task_wdt_config_t wdtc;
32 wdtc.idle_core_mask = 0;
33#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0
34 wdtc.idle_core_mask |= (1 << 0);
35#endif
36#if CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1
37 wdtc.idle_core_mask |= (1 << 1);
38#endif
39 wdtc.timeout_ms = 15000;
40 wdtc.trigger_panic = false;
41 esp_task_wdt_reconfigure(&wdtc);
42#endif
43
44 esp_err_t err = esp_ota_begin(this->partition_, image_size, &this->update_handle_);
45
46#if CONFIG_ESP_TASK_WDT_TIMEOUT_S < 15
47 // Set the WDT back to the configured timeout
48 wdtc.timeout_ms = CONFIG_ESP_TASK_WDT_TIMEOUT_S * 1000;
49 esp_task_wdt_reconfigure(&wdtc);
50#endif
51
52 if (err != ESP_OK) {
53 esp_ota_abort(this->update_handle_);
54 this->update_handle_ = 0;
55 if (err == ESP_ERR_INVALID_SIZE) {
57 } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
59 }
61 }
62 this->md5_.init();
63 return OTA_RESPONSE_OK;
64}
65
66void IDFOTABackend::set_update_md5(const char *expected_md5) {
67 memcpy(this->expected_bin_md5_, expected_md5, 32);
68 this->md5_set_ = true;
69}
70
72 esp_err_t err = esp_ota_write(this->update_handle_, data, len);
73 this->md5_.add(data, len);
74 if (err != ESP_OK) {
75 if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
77 } else if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
79 }
81 }
82 return OTA_RESPONSE_OK;
83}
84
86 if (this->md5_set_) {
87 this->md5_.calculate();
88 if (!this->md5_.equals_hex(this->expected_bin_md5_)) {
89 this->abort();
91 }
92 }
93 esp_err_t err = esp_ota_end(this->update_handle_);
94 this->update_handle_ = 0;
95 if (err == ESP_OK) {
96 err = esp_ota_set_boot_partition(this->partition_);
97 if (err == ESP_OK) {
98 return OTA_RESPONSE_OK;
99 }
100 }
101 if (err == ESP_ERR_OTA_VALIDATE_FAILED) {
103 }
104 if (err == ESP_ERR_FLASH_OP_TIMEOUT || err == ESP_ERR_FLASH_OP_FAIL) {
106 }
108}
109
111 esp_ota_abort(this->update_handle_);
112 this->update_handle_ = 0;
113}
114
115} // namespace ota
116} // namespace esphome
117#endif // USE_ESP32
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash.
Definition hash_base.h:35
void calculate() override
Compute the digest, based on the provided data.
Definition md5.cpp:17
void add(const uint8_t *data, size_t len) override
Add bytes of data for the digest.
Definition md5.cpp:15
void init() override
Initialize a new MD5 digest computation.
Definition md5.cpp:10
OTAResponseTypes write(uint8_t *data, size_t len) override
void set_update_md5(const char *md5) override
OTAResponseTypes end() override
OTAResponseTypes begin(size_t image_size) override
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
Definition ota_backend.h:39
@ OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE
Definition ota_backend.h:37
@ OTA_RESPONSE_ERROR_WRITING_FLASH
Definition ota_backend.h:31
@ OTA_RESPONSE_ERROR_UPDATE_END
Definition ota_backend.h:32
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:41
@ OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION
Definition ota_backend.h:38
@ OTA_RESPONSE_ERROR_MAGIC
Definition ota_backend.h:28
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:595