diff --git a/esphome/components/ota/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp index 9a97fd8d8b..3eff46dc0f 100644 --- a/esphome/components/ota/ota_backend_esp_idf.cpp +++ b/esphome/components/ota/ota_backend_esp_idf.cpp @@ -331,13 +331,12 @@ OTAResponseTypes IDFOTABackend::update_partition_table() { // underlying ESP-IDF calls take longer than expected on a given chip variant. watchdog::WatchdogManager watchdog(15000); - // Copy the running app partition to new position if needed + // Copy the running app partition to new position if needed. + // esp_ota_get_running_partition() is still valid here (we have not yet called + // esp_partition_unload_all()) and returns the same partition that find_app_partition_at would + // have located, without an extra iterator walk. if (new_app_part_index == -1) { - const esp_partition_t *running_app_part = find_app_partition_at(running_app_offset, running_app_size); - if (running_app_part == nullptr) { - ESP_LOGE(TAG, "Running app partition not found in current partition table"); - return OTA_RESPONSE_ERROR_PARTITION_TABLE_UPDATE; - } + const esp_partition_t *running_app_part = esp_ota_get_running_partition(); ESP_LOGD(TAG, "Copying running app from 0x%X to 0x%X (size: 0x%X)", running_app_part->address, app_copy_target_part->address, running_app_size); diff --git a/esphome/components/ota/ota_backend_esp_idf.h b/esphome/components/ota/ota_backend_esp_idf.h index d9de8a5eed..08d4ac2abe 100644 --- a/esphome/components/ota/ota_backend_esp_idf.h +++ b/esphome/components/ota/ota_backend_esp_idf.h @@ -45,11 +45,14 @@ class IDFOTABackend final { char expected_bin_md5_[32]; bool md5_set_{false}; #ifdef USE_OTA_PARTITIONS - ota::OTAType ota_type_{ota::OTA_TYPE_UPDATE_APP}; + // Place the byte buffer first so it sits immediately after the preceding `bool md5_set_`, + // eliminating the 3-byte alignment padding that an int-sized member would otherwise force. + // Remaining members are 4-byte-aligned and pack tightly after the buffer. uint8_t buf_[PARTITION_TABLE_BUFFER_SIZE]; size_t buf_written_{0}; size_t image_size_{0}; const esp_partition_t *partition_table_part_{nullptr}; + ota::OTAType ota_type_{ota::OTA_TYPE_UPDATE_APP}; #endif };