ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ota_backend_esp_idf.h
Go to the documentation of this file.
1#pragma once
2#ifdef USE_ESP32
3#include "ota_backend.h"
4
7
8#include <esp_ota_ops.h>
9
10namespace esphome::ota {
11
12#ifdef USE_OTA_PARTITIONS
13// Staging buffer holds the entire partition table for verification before any flash op.
14static constexpr size_t PARTITION_TABLE_BUFFER_SIZE = ESP_PARTITION_TABLE_MAX_LEN; // 0xC00
15
16void get_running_app_position(uint32_t &offset, size_t &size);
17#endif
18
19class IDFOTABackend final {
20 public:
21 OTAResponseTypes begin(size_t image_size, ota::OTAType ota_type = ota::OTA_TYPE_UPDATE_APP);
22 void set_update_md5(const char *md5);
23 OTAResponseTypes write(uint8_t *data, size_t len);
25 void abort();
26 bool supports_compression() { return false; }
27
28 protected:
29#ifdef USE_OTA_PARTITIONS
30 // copy_dest_part non-null means the running app must be copied INTO this slot of the current
31 // table before the new partition table is committed. The destination is in the current table
32 // because that's where esp_partition_copy can write; once the new table replaces it, the same
33 // flash region becomes target_app_index in the new table.
36 const esp_partition_t *copy_dest_part{nullptr};
37 };
38
39 OTAResponseTypes validate_new_partition_table_(uint32_t running_app_offset, size_t running_app_size,
40 PartitionTablePlan &plan);
43 // Defined in ota_bootloader_esp_idf.cpp:
47 OTAResponseTypes finalize_bootloader_update_(esp_err_t ota_end_err);
48
49 // The OTA types that flow through esp_ota_begin/write/end. Partition-table updates take a
50 // separate code path that buffers the table in RAM and never touches the OTA handle.
52 return this->ota_type_ == ota::OTA_TYPE_UPDATE_APP || this->ota_type_ == ota::OTA_TYPE_UPDATE_BOOTLOADER;
53 }
54#endif
55
56 private:
57 // Keep md5_ first since its digest_ is alignas(32) on DMA-SHA variants; md5_set_ stays last so buf_ packs tightly.
58 md5::MD5Digest md5_{};
59 esp_ota_handle_t update_handle_{0};
60 const esp_partition_t *partition_;
61 char expected_bin_md5_[32];
62 bool md5_set_{false};
63#ifdef USE_OTA_PARTITIONS
64 // Buffer first so it packs tightly after the preceding `bool md5_set_` with no alignment
65 // padding. Only resident during an active OTA: the backend is constructed per connection and
66 // destroyed on cleanup_connection_().
67 uint8_t buf_[PARTITION_TABLE_BUFFER_SIZE];
68 size_t buf_written_{0};
69 size_t image_size_{0};
70 const esp_partition_t *partition_table_part_{nullptr};
71 const esp_partition_t *bootloader_part_{nullptr};
73#endif
74};
75
76std::unique_ptr<IDFOTABackend> make_ota_backend();
77
78} // namespace esphome::ota
79#endif // USE_ESP32
void set_update_md5(const char *md5)
OTAResponseTypes finalize_bootloader_update_(esp_err_t ota_end_err)
OTAResponseTypes begin(size_t image_size, ota::OTAType ota_type=ota::OTA_TYPE_UPDATE_APP)
OTAResponseTypes prepare_bootloader_update_(size_t image_size)
OTAResponseTypes register_and_validate_bootloader_part_()
OTAResponseTypes write(uint8_t *data, size_t len)
OTAResponseTypes validate_new_partition_table_(uint32_t running_app_offset, size_t running_app_size, PartitionTablePlan &plan)
OTAResponseTypes register_and_validate_partition_table_part_()
@ OTA_TYPE_UPDATE_BOOTLOADER
Definition ota_backend.h:63
void get_running_app_position(uint32_t &offset, size_t &size)
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
const void size_t len
Definition hal.h:64
uint16_t size
Definition helpers.cpp:25
static void uint32_t