From cb97c35d3637c83d1319360f1119ae46c2c730f3 Mon Sep 17 00:00:00 2001 From: Mat931 <49403702+Mat931@users.noreply.github.com> Date: Fri, 1 May 2026 10:29:41 +0200 Subject: [PATCH] Backport changes --- .../components/esphome/ota/ota_esphome.cpp | 35 ++++++++----------- esphome/components/esphome/ota/ota_esphome.h | 2 -- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/esphome/components/esphome/ota/ota_esphome.cpp b/esphome/components/esphome/ota/ota_esphome.cpp index 3ae7be7e8f..ca5a382875 100644 --- a/esphome/components/esphome/ota/ota_esphome.cpp +++ b/esphome/components/esphome/ota/ota_esphome.cpp @@ -237,19 +237,17 @@ void ESPHomeOTAComponent::handle_handshake_() { const bool supports_compression = (this->ota_features_ & CLIENT_FEATURE_SUPPORTS_COMPRESSION) != 0 && this->backend_->supports_compression(); - // Compose the feature-ack response. When USE_OTA_PARTITIONS is enabled and the client - // negotiates the extended protocol we emit a 2-byte response (marker + server feature flags); - // otherwise we emit the single-byte legacy response. The #ifdef wraps only the extended-proto - // branch so the legacy branch reads as unconditional code in either build configuration. -#ifdef USE_OTA_PARTITIONS + // Compose the feature-ack response. When the client negotiates the extended protocol we emit + // a 2-byte response (marker + server feature flags); otherwise we emit the single-byte + // legacy response. this->extended_proto_ = (this->ota_features_ & CLIENT_FEATURE_SUPPORTS_EXTENDED_PROTOCOL) != 0; if (this->extended_proto_) { this->handshake_buf_[0] = ota::OTA_RESPONSE_FEATURE_FLAGS; - this->handshake_buf_[1] = - SERVER_FEATURE_SUPPORTS_PARTITION_ACCESS | (supports_compression ? SERVER_FEATURE_SUPPORTS_COMPRESSION : 0); - } else + this->handshake_buf_[1] = (supports_compression ? SERVER_FEATURE_SUPPORTS_COMPRESSION : 0); +#ifdef USE_OTA_PARTITIONS + this->handshake_buf_[1] |= SERVER_FEATURE_SUPPORTS_PARTITION_ACCESS; #endif - { + } else { this->handshake_buf_[0] = supports_compression ? ota::OTA_RESPONSE_SUPPORTS_COMPRESSION : ota::OTA_RESPONSE_HEADER_OK; } @@ -257,15 +255,12 @@ void ESPHomeOTAComponent::handle_handshake_() { } case OTAState::FEATURE_ACK: { -#ifdef USE_OTA_PARTITIONS - const size_t ack_size = this->extended_proto_ ? 2 : 1; -#else - const size_t ack_size = 1; -#endif + static constexpr size_t STANDARD_PROTO_ACK_SIZE = 1; + static constexpr size_t EXTENDED_PROTO_ACK_SIZE = 2; + const size_t ack_size = this->extended_proto_ ? EXTENDED_PROTO_ACK_SIZE : STANDARD_PROTO_ACK_SIZE; if (!this->try_write_(ack_size, LOG_STR("ack feature"))) { return; } - #ifdef USE_OTA_PASSWORD // If password is set, move to auth phase if (!this->password_.empty()) { @@ -349,9 +344,7 @@ void ESPHomeOTAComponent::handle_data_() { uint8_t buf[OTA_BUFFER_SIZE]; char *sbuf = reinterpret_cast(buf); size_t ota_size; -#ifdef USE_OTA_PARTITIONS ota::OTAType ota_type = ota::OTA_TYPE_UPDATE_APP; -#endif #if USE_OTA_VERSION == 2 size_t size_acknowledged = 0; #endif @@ -367,7 +360,6 @@ void ESPHomeOTAComponent::handle_data_() { // Acknowledge auth OK - 1 byte this->write_byte_(ota::OTA_RESPONSE_AUTH_OK); -#ifdef USE_OTA_PARTITIONS if (this->extended_proto_) { // Read ota type, 1 byte if (!this->readall_(buf, 1)) { @@ -377,7 +369,6 @@ void ESPHomeOTAComponent::handle_data_() { ota_type = static_cast(buf[0]); } ESP_LOGV(TAG, "OTA type is 0x%02x", ota_type); -#endif // Read size, 4 bytes MSB first if (!this->readall_(buf, 4)) { @@ -398,10 +389,14 @@ void ESPHomeOTAComponent::handle_data_() { this->notify_state_(ota::OTA_STARTED, 0.0f, 0); #endif - // This will block for a few seconds as it locks flash #ifdef USE_OTA_PARTITIONS error_code = this->backend_->begin(ota_size, ota_type); #else + if (ota_type != ota::OTA_TYPE_UPDATE_APP) { + error_code = ota::OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE; + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + // This will block for a few seconds as it locks flash error_code = this->backend_->begin(ota_size); #endif if (error_code != ota::OTA_RESPONSE_OK) diff --git a/esphome/components/esphome/ota/ota_esphome.h b/esphome/components/esphome/ota/ota_esphome.h index 9bed9240aa..f612451ab0 100644 --- a/esphome/components/esphome/ota/ota_esphome.h +++ b/esphome/components/esphome/ota/ota_esphome.h @@ -91,9 +91,7 @@ class ESPHomeOTAComponent final : public ota::OTAComponent { std::string password_; std::unique_ptr auth_buf_; #endif // USE_OTA_PASSWORD -#ifdef USE_OTA_PARTITIONS bool extended_proto_{false}; -#endif socket::ListenSocket *server_{nullptr}; std::unique_ptr client_;