From f50429bcf289beaa539d6644a30c438037129651 Mon Sep 17 00:00:00 2001 From: Tomasz Duda Date: Tue, 7 May 2024 19:43:01 +0200 Subject: [PATCH] rollback ota --- esphome/components/ota/__init__.py | 68 +-- .../{ota_network => ota}/ota_backend.h | 4 +- .../ota_backend_arduino_esp32.cpp | 4 +- .../ota_backend_arduino_esp32.h | 4 +- .../ota_backend_arduino_esp8266.cpp | 4 +- .../ota_backend_arduino_esp8266.h | 4 +- .../ota_backend_arduino_libretiny.cpp | 4 +- .../ota_backend_arduino_libretiny.h | 4 +- .../ota_backend_arduino_rp2040.cpp | 4 +- .../ota_backend_arduino_rp2040.h | 4 +- .../ota_backend_esp_idf.cpp | 4 +- .../ota_backend_esp_idf.h | 4 +- esphome/components/ota/ota_component.cpp | 520 +++++++++++++++++ esphome/components/ota/ota_component.h | 89 ++- esphome/components/ota_network/__init__.py | 3 - .../components/ota_network/ota_component.cpp | 527 ------------------ .../components/ota_network/ota_component.h | 98 ---- 17 files changed, 638 insertions(+), 711 deletions(-) rename esphome/components/{ota_network => ota}/ota_backend.h (89%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_esp32.cpp (95%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_esp32.h (91%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_esp8266.cpp (96%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_esp8266.h (93%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_libretiny.cpp (95%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_libretiny.h (91%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_rp2040.cpp (96%) rename esphome/components/{ota_network => ota}/ota_backend_arduino_rp2040.h (92%) rename esphome/components/{ota_network => ota}/ota_backend_esp_idf.cpp (98%) rename esphome/components/{ota_network => ota}/ota_backend_esp_idf.h (93%) delete mode 100644 esphome/components/ota_network/__init__.py delete mode 100644 esphome/components/ota_network/ota_component.cpp delete mode 100644 esphome/components/ota_network/ota_component.h diff --git a/esphome/components/ota/__init__.py b/esphome/components/ota/__init__.py index 53439d9ac0..3c845490dc 100644 --- a/esphome/components/ota/__init__.py +++ b/esphome/components/ota/__init__.py @@ -15,17 +15,10 @@ from esphome.const import ( CONF_VERSION, ) from esphome.core import CORE, coroutine_with_priority -import esphome.final_validate as fv -from esphome.components.zephyr.const import BOOTLOADER_MCUBOOT CODEOWNERS = ["@esphome/core"] - - -def AUTO_LOAD(): - if CORE.using_zephyr: - return ["zephyr_ota_mcumgr"] - return ["ota_network"] - +DEPENDENCIES = ["network"] +AUTO_LOAD = ["socket", "md5"] CONF_ON_STATE_CHANGE = "on_state_change" CONF_ON_BEGIN = "on_begin" @@ -35,14 +28,7 @@ CONF_ON_ERROR = "on_error" ota_ns = cg.esphome_ns.namespace("ota") OTAState = ota_ns.enum("OTAState") -if CORE.using_zephyr: - OTAComponent = cg.esphome_ns.namespace("zephyr_ota_mcumgr").class_( - "OTAComponent", cg.Component - ) -else: - OTAComponent = cg.esphome_ns.namespace("ota_network").class_( - "OTAComponent", cg.Component - ) +OTAComponent = ota_ns.class_("OTAComponent", cg.Component) OTAStateChangeTrigger = ota_ns.class_( "OTAStateChangeTrigger", automation.Trigger.template() ) @@ -52,25 +38,11 @@ OTAEndTrigger = ota_ns.class_("OTAEndTrigger", automation.Trigger.template()) OTAErrorTrigger = ota_ns.class_("OTAErrorTrigger", automation.Trigger.template()) -def _not_supported_by_zephyr(value): - if CORE.using_zephyr: - raise cv.Invalid(f"Not supported by zephyr framework({value})") - return value - - -def _default_ota_version(): - if CORE.using_zephyr: - return cv.UNDEFINED - return 2 - - CONFIG_SCHEMA = cv.Schema( { cv.GenerateID(): cv.declare_id(OTAComponent), cv.Optional(CONF_SAFE_MODE, default=True): cv.boolean, - cv.Optional(CONF_VERSION, default=_default_ota_version()): cv.All( - cv.one_of(1, 2, int=True), _not_supported_by_zephyr - ), + cv.Optional(CONF_VERSION, default=2): cv.one_of(1, 2, int=True), cv.SplitDefault( CONF_PORT, esp8266=8266, @@ -78,11 +50,8 @@ CONFIG_SCHEMA = cv.Schema( rp2040=2040, bk72xx=8892, rtl87xx=8892, - ): cv.All( - cv.port, - _not_supported_by_zephyr, - ), - cv.Optional(CONF_PASSWORD): cv.All(cv.string, _not_supported_by_zephyr), + ): cv.port, + cv.Optional(CONF_PASSWORD): cv.string, cv.Optional( CONF_REBOOT_TIMEOUT, default="5min" ): cv.positive_time_period_milliseconds, @@ -116,32 +85,17 @@ CONFIG_SCHEMA = cv.Schema( ).extend(cv.COMPONENT_SCHEMA) -def _validate_mcumgr(config): - if CORE.using_zephyr: - fconf = fv.full_config.get() - try: - bootloader = fconf.get_config_for_path(["nrf52", "bootloader"]) - if bootloader != BOOTLOADER_MCUBOOT: - raise cv.Invalid(f"'{bootloader}' bootloader does not support OTA") - except KeyError: - pass - - -FINAL_VALIDATE_SCHEMA = _validate_mcumgr - - @coroutine_with_priority(50.0) async def to_code(config): CORE.data[CONF_OTA] = {} var = cg.new_Pvariable(config[CONF_ID]) + cg.add(var.set_port(config[CONF_PORT])) cg.add_define("USE_OTA") - if not CORE.using_zephyr: - cg.add(var.set_port(config[CONF_PORT])) - if CONF_PASSWORD in config: - cg.add(var.set_auth_password(config[CONF_PASSWORD])) - cg.add_define("USE_OTA_PASSWORD") - cg.add_define("USE_OTA_VERSION", config[CONF_VERSION]) + if CONF_PASSWORD in config: + cg.add(var.set_auth_password(config[CONF_PASSWORD])) + cg.add_define("USE_OTA_PASSWORD") + cg.add_define("USE_OTA_VERSION", config[CONF_VERSION]) await cg.register_component(var, config) diff --git a/esphome/components/ota_network/ota_backend.h b/esphome/components/ota/ota_backend.h similarity index 89% rename from esphome/components/ota_network/ota_backend.h rename to esphome/components/ota/ota_backend.h index dbabf704fc..5c5b61a278 100644 --- a/esphome/components/ota_network/ota_backend.h +++ b/esphome/components/ota/ota_backend.h @@ -2,7 +2,7 @@ #include "ota_component.h" namespace esphome { -namespace ota_network { +namespace ota { class OTABackend { public: @@ -15,5 +15,5 @@ class OTABackend { virtual bool supports_compression() = 0; }; -} // namespace ota_network +} // namespace ota } // namespace esphome diff --git a/esphome/components/ota_network/ota_backend_arduino_esp32.cpp b/esphome/components/ota/ota_backend_arduino_esp32.cpp similarity index 95% rename from esphome/components/ota_network/ota_backend_arduino_esp32.cpp rename to esphome/components/ota/ota_backend_arduino_esp32.cpp index e269c0b6c1..4759737dbd 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp32.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp32.cpp @@ -8,7 +8,7 @@ #include namespace esphome { -namespace ota_network { +namespace ota { OTAResponseTypes ArduinoESP32OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); @@ -40,7 +40,7 @@ OTAResponseTypes ArduinoESP32OTABackend::end() { void ArduinoESP32OTABackend::abort() { Update.abort(); } -} // namespace ota_network +} // namespace ota } // namespace esphome #endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ota_network/ota_backend_arduino_esp32.h b/esphome/components/ota/ota_backend_arduino_esp32.h similarity index 91% rename from esphome/components/ota_network/ota_backend_arduino_esp32.h rename to esphome/components/ota/ota_backend_arduino_esp32.h index 609891370b..f86a70d678 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp32.h +++ b/esphome/components/ota/ota_backend_arduino_esp32.h @@ -6,7 +6,7 @@ #include "ota_backend.h" namespace esphome { -namespace ota_network { +namespace ota { class ArduinoESP32OTABackend : public OTABackend { public: @@ -18,7 +18,7 @@ class ArduinoESP32OTABackend : public OTABackend { bool supports_compression() override { return false; } }; -} // namespace ota_network +} // namespace ota } // namespace esphome #endif // USE_ESP32_FRAMEWORK_ARDUINO diff --git a/esphome/components/ota_network/ota_backend_arduino_esp8266.cpp b/esphome/components/ota/ota_backend_arduino_esp8266.cpp similarity index 96% rename from esphome/components/ota_network/ota_backend_arduino_esp8266.cpp rename to esphome/components/ota/ota_backend_arduino_esp8266.cpp index 048cc71da1..23dc0d4e21 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp8266.cpp +++ b/esphome/components/ota/ota_backend_arduino_esp8266.cpp @@ -10,7 +10,7 @@ #include namespace esphome { -namespace ota_network { +namespace ota { OTAResponseTypes ArduinoESP8266OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); @@ -52,7 +52,7 @@ void ArduinoESP8266OTABackend::abort() { esp8266::preferences_prevent_write(false); } -} // namespace ota_network +} // namespace ota } // namespace esphome #endif diff --git a/esphome/components/ota_network/ota_backend_arduino_esp8266.h b/esphome/components/ota/ota_backend_arduino_esp8266.h similarity index 93% rename from esphome/components/ota_network/ota_backend_arduino_esp8266.h rename to esphome/components/ota/ota_backend_arduino_esp8266.h index 512e2a4de6..7937c665b0 100644 --- a/esphome/components/ota_network/ota_backend_arduino_esp8266.h +++ b/esphome/components/ota/ota_backend_arduino_esp8266.h @@ -8,7 +8,7 @@ #include "esphome/core/macros.h" namespace esphome { -namespace ota_network { +namespace ota { class ArduinoESP8266OTABackend : public OTABackend { public: @@ -24,7 +24,7 @@ class ArduinoESP8266OTABackend : public OTABackend { #endif }; -} // namespace ota_network +} // namespace ota } // namespace esphome #endif diff --git a/esphome/components/ota_network/ota_backend_arduino_libretiny.cpp b/esphome/components/ota/ota_backend_arduino_libretiny.cpp similarity index 95% rename from esphome/components/ota_network/ota_backend_arduino_libretiny.cpp rename to esphome/components/ota/ota_backend_arduino_libretiny.cpp index 1fd534d814..dbf6c97988 100644 --- a/esphome/components/ota_network/ota_backend_arduino_libretiny.cpp +++ b/esphome/components/ota/ota_backend_arduino_libretiny.cpp @@ -8,7 +8,7 @@ #include namespace esphome { -namespace ota_network { +namespace ota { OTAResponseTypes ArduinoLibreTinyOTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); @@ -40,7 +40,7 @@ OTAResponseTypes ArduinoLibreTinyOTABackend::end() { void ArduinoLibreTinyOTABackend::abort() { Update.abort(); } -} // namespace ota_network +} // namespace ota } // namespace esphome #endif // USE_LIBRETINY diff --git a/esphome/components/ota_network/ota_backend_arduino_libretiny.h b/esphome/components/ota/ota_backend_arduino_libretiny.h similarity index 91% rename from esphome/components/ota_network/ota_backend_arduino_libretiny.h rename to esphome/components/ota/ota_backend_arduino_libretiny.h index 71e63020ef..79656bb353 100644 --- a/esphome/components/ota_network/ota_backend_arduino_libretiny.h +++ b/esphome/components/ota/ota_backend_arduino_libretiny.h @@ -6,7 +6,7 @@ #include "ota_backend.h" namespace esphome { -namespace ota_network { +namespace ota { class ArduinoLibreTinyOTABackend : public OTABackend { public: @@ -18,7 +18,7 @@ class ArduinoLibreTinyOTABackend : public OTABackend { bool supports_compression() override { return false; } }; -} // namespace ota_network +} // namespace ota } // namespace esphome #endif // USE_LIBRETINY diff --git a/esphome/components/ota_network/ota_backend_arduino_rp2040.cpp b/esphome/components/ota/ota_backend_arduino_rp2040.cpp similarity index 96% rename from esphome/components/ota_network/ota_backend_arduino_rp2040.cpp rename to esphome/components/ota/ota_backend_arduino_rp2040.cpp index 217069a9e5..260387cec1 100644 --- a/esphome/components/ota_network/ota_backend_arduino_rp2040.cpp +++ b/esphome/components/ota/ota_backend_arduino_rp2040.cpp @@ -10,7 +10,7 @@ #include namespace esphome { -namespace ota_network { +namespace ota { OTAResponseTypes ArduinoRP2040OTABackend::begin(size_t image_size) { bool ret = Update.begin(image_size, U_FLASH); @@ -52,7 +52,7 @@ void ArduinoRP2040OTABackend::abort() { rp2040::preferences_prevent_write(false); } -} // namespace ota_network +} // namespace ota } // namespace esphome #endif // USE_RP2040 diff --git a/esphome/components/ota_network/ota_backend_arduino_rp2040.h b/esphome/components/ota/ota_backend_arduino_rp2040.h similarity index 92% rename from esphome/components/ota_network/ota_backend_arduino_rp2040.h rename to esphome/components/ota/ota_backend_arduino_rp2040.h index 4842e96400..5aa2ec9435 100644 --- a/esphome/components/ota_network/ota_backend_arduino_rp2040.h +++ b/esphome/components/ota/ota_backend_arduino_rp2040.h @@ -8,7 +8,7 @@ #include "ota_component.h" namespace esphome { -namespace ota_network { +namespace ota { class ArduinoRP2040OTABackend : public OTABackend { public: @@ -20,7 +20,7 @@ class ArduinoRP2040OTABackend : public OTABackend { bool supports_compression() override { return false; } }; -} // namespace ota_network +} // namespace ota } // namespace esphome #endif // USE_RP2040 diff --git a/esphome/components/ota_network/ota_backend_esp_idf.cpp b/esphome/components/ota/ota_backend_esp_idf.cpp similarity index 98% rename from esphome/components/ota_network/ota_backend_esp_idf.cpp rename to esphome/components/ota/ota_backend_esp_idf.cpp index 41969aba85..319a1482f1 100644 --- a/esphome/components/ota_network/ota_backend_esp_idf.cpp +++ b/esphome/components/ota/ota_backend_esp_idf.cpp @@ -13,7 +13,7 @@ #endif namespace esphome { -namespace ota_network { +namespace ota { OTAResponseTypes IDFOTABackend::begin(size_t image_size) { this->partition_ = esp_ota_get_next_update_partition(nullptr); @@ -110,6 +110,6 @@ void IDFOTABackend::abort() { this->update_handle_ = 0; } -} // namespace ota_network +} // namespace ota } // namespace esphome #endif diff --git a/esphome/components/ota_network/ota_backend_esp_idf.h b/esphome/components/ota/ota_backend_esp_idf.h similarity index 93% rename from esphome/components/ota_network/ota_backend_esp_idf.h rename to esphome/components/ota/ota_backend_esp_idf.h index bf83199326..af09d0d693 100644 --- a/esphome/components/ota_network/ota_backend_esp_idf.h +++ b/esphome/components/ota/ota_backend_esp_idf.h @@ -8,7 +8,7 @@ #include "esphome/components/md5/md5.h" namespace esphome { -namespace ota_network { +namespace ota { class IDFOTABackend : public OTABackend { public: @@ -26,6 +26,6 @@ class IDFOTABackend : public OTABackend { char expected_bin_md5_[32]; }; -} // namespace ota_network +} // namespace ota } // namespace esphome #endif diff --git a/esphome/components/ota/ota_component.cpp b/esphome/components/ota/ota_component.cpp index c785c4bc8e..15af14ff1a 100644 --- a/esphome/components/ota/ota_component.cpp +++ b/esphome/components/ota/ota_component.cpp @@ -1,10 +1,530 @@ #include "ota_component.h" +#include "ota_backend.h" +#include "ota_backend_arduino_esp32.h" +#include "ota_backend_arduino_esp8266.h" +#include "ota_backend_arduino_rp2040.h" +#include "ota_backend_arduino_libretiny.h" +#include "ota_backend_esp_idf.h" + +#include "esphome/core/log.h" +#include "esphome/core/application.h" +#include "esphome/core/hal.h" +#include "esphome/core/util.h" +#include "esphome/components/md5/md5.h" +#include "esphome/components/network/util.h" + +#include +#include namespace esphome { namespace ota { +static const char *const TAG = "ota"; +static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; + OTAComponent *global_ota_component = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables) +std::unique_ptr make_ota_backend() { +#ifdef USE_ARDUINO +#ifdef USE_ESP8266 + return make_unique(); +#endif // USE_ESP8266 +#ifdef USE_ESP32 + return make_unique(); +#endif // USE_ESP32 +#endif // USE_ARDUINO +#ifdef USE_ESP_IDF + return make_unique(); +#endif // USE_ESP_IDF +#ifdef USE_RP2040 + return make_unique(); +#endif // USE_RP2040 +#ifdef USE_LIBRETINY + return make_unique(); +#endif +} + +OTAComponent::OTAComponent() { global_ota_component = this; } + +void OTAComponent::setup() { + server_ = socket::socket_ip(SOCK_STREAM, 0); + if (server_ == nullptr) { + ESP_LOGW(TAG, "Could not create socket."); + this->mark_failed(); + return; + } + int enable = 1; + int err = server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err); + // we can still continue + } + err = server_->setblocking(false); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err); + this->mark_failed(); + return; + } + + struct sockaddr_storage server; + + socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_); + if (sl == 0) { + ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno); + this->mark_failed(); + return; + } + + err = server_->bind((struct sockaddr *) &server, sizeof(server)); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno); + this->mark_failed(); + return; + } + + err = server_->listen(4); + if (err != 0) { + ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno); + this->mark_failed(); + return; + } + + this->dump_config(); +} + +void OTAComponent::dump_config() { + ESP_LOGCONFIG(TAG, "Over-The-Air Updates:"); + ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_); +#ifdef USE_OTA_PASSWORD + if (!this->password_.empty()) { + ESP_LOGCONFIG(TAG, " Using Password."); + } +#endif + ESP_LOGCONFIG(TAG, " OTA version: %d.", USE_OTA_VERSION); + if (this->has_safe_mode_ && this->safe_mode_rtc_value_ > 1 && + this->safe_mode_rtc_value_ != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + ESP_LOGW(TAG, "Last Boot was an unhandled reset, will proceed to safe mode in %" PRIu32 " restarts", + this->safe_mode_num_attempts_ - this->safe_mode_rtc_value_); + } +} + +void OTAComponent::loop() { + this->handle_(); + + if (this->has_safe_mode_ && (millis() - this->safe_mode_start_time_) > this->safe_mode_enable_time_) { + this->has_safe_mode_ = false; + // successful boot, reset counter + ESP_LOGI(TAG, "Boot seems successful, resetting boot loop counter."); + this->clean_rtc(); + } +} + +static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01; + +void OTAComponent::handle_() { + OTAResponseTypes error_code = OTA_RESPONSE_ERROR_UNKNOWN; + bool update_started = false; + size_t total = 0; + uint32_t last_progress = 0; + uint8_t buf[1024]; + char *sbuf = reinterpret_cast(buf); + size_t ota_size; + uint8_t ota_features; + std::unique_ptr backend; + (void) ota_features; +#if USE_OTA_VERSION == 2 + size_t size_acknowledged = 0; +#endif + + if (client_ == nullptr) { + struct sockaddr_storage source_addr; + socklen_t addr_len = sizeof(source_addr); + client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len); + } + if (client_ == nullptr) + return; + + int enable = 1; + int err = client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int)); + if (err != 0) { + ESP_LOGW(TAG, "Socket could not enable tcp nodelay, errno: %d", errno); + return; + } + + ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str()); + this->status_set_warning(); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_STARTED, 0.0f, 0); +#endif + + if (!this->readall_(buf, 5)) { + ESP_LOGW(TAG, "Reading magic bytes failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + // 0x6C, 0x26, 0xF7, 0x5C, 0x45 + if (buf[0] != 0x6C || buf[1] != 0x26 || buf[2] != 0xF7 || buf[3] != 0x5C || buf[4] != 0x45) { + ESP_LOGW(TAG, "Magic bytes do not match! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", buf[0], buf[1], buf[2], buf[3], + buf[4]); + error_code = OTA_RESPONSE_ERROR_MAGIC; + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + // Send OK and version - 2 bytes + buf[0] = OTA_RESPONSE_OK; + buf[1] = USE_OTA_VERSION; + this->writeall_(buf, 2); + + backend = make_ota_backend(); + + // Read features - 1 byte + if (!this->readall_(buf, 1)) { + ESP_LOGW(TAG, "Reading features failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + ota_features = buf[0]; // NOLINT + ESP_LOGV(TAG, "OTA features is 0x%02X", ota_features); + + // Acknowledge header - 1 byte + buf[0] = OTA_RESPONSE_HEADER_OK; + if ((ota_features & FEATURE_SUPPORTS_COMPRESSION) != 0 && backend->supports_compression()) { + buf[0] = OTA_RESPONSE_SUPPORTS_COMPRESSION; + } + + this->writeall_(buf, 1); + +#ifdef USE_OTA_PASSWORD + if (!this->password_.empty()) { + buf[0] = OTA_RESPONSE_REQUEST_AUTH; + this->writeall_(buf, 1); + md5::MD5Digest md5{}; + md5.init(); + sprintf(sbuf, "%08" PRIx32, random_uint32()); + md5.add(sbuf, 8); + md5.calculate(); + md5.get_hex(sbuf); + ESP_LOGV(TAG, "Auth: Nonce is %s", sbuf); + + // Send nonce, 32 bytes hex MD5 + if (!this->writeall_(reinterpret_cast(sbuf), 32)) { + ESP_LOGW(TAG, "Auth: Writing nonce failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + // prepare challenge + md5.init(); + md5.add(this->password_.c_str(), this->password_.length()); + // add nonce + md5.add(sbuf, 32); + + // Receive cnonce, 32 bytes hex MD5 + if (!this->readall_(buf, 32)) { + ESP_LOGW(TAG, "Auth: Reading cnonce failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + sbuf[32] = '\0'; + ESP_LOGV(TAG, "Auth: CNonce is %s", sbuf); + // add cnonce + md5.add(sbuf, 32); + + // calculate result + md5.calculate(); + md5.get_hex(sbuf); + ESP_LOGV(TAG, "Auth: Result is %s", sbuf); + + // Receive result, 32 bytes hex MD5 + if (!this->readall_(buf + 64, 32)) { + ESP_LOGW(TAG, "Auth: Reading response failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + sbuf[64 + 32] = '\0'; + ESP_LOGV(TAG, "Auth: Response is %s", sbuf + 64); + + bool matches = true; + for (uint8_t i = 0; i < 32; i++) + matches = matches && buf[i] == buf[64 + i]; + + if (!matches) { + ESP_LOGW(TAG, "Auth failed! Passwords do not match!"); + error_code = OTA_RESPONSE_ERROR_AUTH_INVALID; + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + } +#endif // USE_OTA_PASSWORD + + // Acknowledge auth OK - 1 byte + buf[0] = OTA_RESPONSE_AUTH_OK; + this->writeall_(buf, 1); + + // Read size, 4 bytes MSB first + if (!this->readall_(buf, 4)) { + ESP_LOGW(TAG, "Reading size failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + ota_size = 0; + for (uint8_t i = 0; i < 4; i++) { + ota_size <<= 8; + ota_size |= buf[i]; + } + ESP_LOGV(TAG, "OTA size is %u bytes", ota_size); + + error_code = backend->begin(ota_size); + if (error_code != OTA_RESPONSE_OK) + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + update_started = true; + + // Acknowledge prepare OK - 1 byte + buf[0] = OTA_RESPONSE_UPDATE_PREPARE_OK; + this->writeall_(buf, 1); + + // Read binary MD5, 32 bytes + if (!this->readall_(buf, 32)) { + ESP_LOGW(TAG, "Reading binary MD5 checksum failed!"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + sbuf[32] = '\0'; + ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf); + backend->set_update_md5(sbuf); + + // Acknowledge MD5 OK - 1 byte + buf[0] = OTA_RESPONSE_BIN_MD5_OK; + this->writeall_(buf, 1); + + while (total < ota_size) { + // TODO: timeout check + size_t requested = std::min(sizeof(buf), ota_size - total); + ssize_t read = this->client_->read(buf, requested); + if (read == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + App.feed_wdt(); + delay(1); + continue; + } + ESP_LOGW(TAG, "Error receiving data for update, errno: %d", errno); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } else if (read == 0) { + // $ man recv + // "When a stream socket peer has performed an orderly shutdown, the return value will + // be 0 (the traditional "end-of-file" return)." + ESP_LOGW(TAG, "Remote end closed connection"); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + error_code = backend->write(buf, read); + if (error_code != OTA_RESPONSE_OK) { + ESP_LOGW(TAG, "Error writing binary data to flash!, error_code: %d", error_code); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + total += read; +#if USE_OTA_VERSION == 2 + while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) { + buf[0] = OTA_RESPONSE_CHUNK_OK; + this->writeall_(buf, 1); + size_acknowledged += OTA_BLOCK_SIZE; + } +#endif + + uint32_t now = millis(); + if (now - last_progress > 1000) { + last_progress = now; + float percentage = (total * 100.0f) / ota_size; + ESP_LOGD(TAG, "OTA in progress: %0.1f%%", percentage); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_IN_PROGRESS, percentage, 0); +#endif + // feed watchdog and give other tasks a chance to run + App.feed_wdt(); + yield(); + } + } + + // Acknowledge receive OK - 1 byte + buf[0] = OTA_RESPONSE_RECEIVE_OK; + this->writeall_(buf, 1); + + error_code = backend->end(); + if (error_code != OTA_RESPONSE_OK) { + ESP_LOGW(TAG, "Error ending OTA!, error_code: %d", error_code); + goto error; // NOLINT(cppcoreguidelines-avoid-goto) + } + + // Acknowledge Update end OK - 1 byte + buf[0] = OTA_RESPONSE_UPDATE_END_OK; + this->writeall_(buf, 1); + + // Read ACK + if (!this->readall_(buf, 1) || buf[0] != OTA_RESPONSE_OK) { + ESP_LOGW(TAG, "Reading back acknowledgement failed!"); + // do not go to error, this is not fatal + } + + this->client_->close(); + this->client_ = nullptr; + delay(10); + ESP_LOGI(TAG, "OTA update finished!"); + this->status_clear_warning(); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_COMPLETED, 100.0f, 0); +#endif + delay(100); // NOLINT + App.safe_reboot(); + +error: + buf[0] = static_cast(error_code); + this->writeall_(buf, 1); + this->client_->close(); + this->client_ = nullptr; + + if (backend != nullptr && update_started) { + backend->abort(); + } + + this->status_momentary_error("onerror", 5000); +#ifdef USE_OTA_STATE_CALLBACK + this->state_callback_.call(OTA_ERROR, 0.0f, static_cast(error_code)); +#endif +} + +bool OTAComponent::readall_(uint8_t *buf, size_t len) { + uint32_t start = millis(); + uint32_t at = 0; + while (len - at > 0) { + uint32_t now = millis(); + if (now - start > 1000) { + ESP_LOGW(TAG, "Timed out reading %d bytes of data", len); + return false; + } + + ssize_t read = this->client_->read(buf + at, len - at); + if (read == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + App.feed_wdt(); + delay(1); + continue; + } + ESP_LOGW(TAG, "Failed to read %d bytes of data, errno: %d", len, errno); + return false; + } else if (read == 0) { + ESP_LOGW(TAG, "Remote closed connection"); + return false; + } else { + at += read; + } + App.feed_wdt(); + delay(1); + } + + return true; +} +bool OTAComponent::writeall_(const uint8_t *buf, size_t len) { + uint32_t start = millis(); + uint32_t at = 0; + while (len - at > 0) { + uint32_t now = millis(); + if (now - start > 1000) { + ESP_LOGW(TAG, "Timed out writing %d bytes of data", len); + return false; + } + + ssize_t written = this->client_->write(buf + at, len - at); + if (written == -1) { + if (errno == EAGAIN || errno == EWOULDBLOCK) { + App.feed_wdt(); + delay(1); + continue; + } + ESP_LOGW(TAG, "Failed to write %d bytes of data, errno: %d", len, errno); + return false; + } else { + at += written; + } + App.feed_wdt(); + delay(1); + } + return true; +} + +float OTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; } +uint16_t OTAComponent::get_port() const { return this->port_; } +void OTAComponent::set_port(uint16_t port) { this->port_ = port; } + +void OTAComponent::set_safe_mode_pending(const bool &pending) { + if (!this->has_safe_mode_) + return; + + uint32_t current_rtc = this->read_rtc_(); + + if (pending && current_rtc != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + ESP_LOGI(TAG, "Device will enter safe mode on next boot."); + this->write_rtc_(esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC); + } + + if (!pending && current_rtc == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) { + ESP_LOGI(TAG, "Safe mode pending has been cleared"); + this->clean_rtc(); + } +} +bool OTAComponent::get_safe_mode_pending() { + return this->has_safe_mode_ && this->read_rtc_() == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC; +} + +bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) { + this->has_safe_mode_ = true; + this->safe_mode_start_time_ = millis(); + this->safe_mode_enable_time_ = enable_time; + this->safe_mode_num_attempts_ = num_attempts; + this->rtc_ = global_preferences->make_preference(233825507UL, false); + this->safe_mode_rtc_value_ = this->read_rtc_(); + + bool is_manual_safe_mode = this->safe_mode_rtc_value_ == esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC; + + if (is_manual_safe_mode) { + ESP_LOGI(TAG, "Safe mode has been entered manually"); + } else { + ESP_LOGCONFIG(TAG, "There have been %" PRIu32 " suspected unsuccessful boot attempts.", this->safe_mode_rtc_value_); + } + + if (this->safe_mode_rtc_value_ >= num_attempts || is_manual_safe_mode) { + this->clean_rtc(); + + if (!is_manual_safe_mode) { + ESP_LOGE(TAG, "Boot loop detected. Proceeding to safe mode."); + } + + this->status_set_error(); + this->set_timeout(enable_time, []() { + ESP_LOGE(TAG, "No OTA attempt made, restarting."); + App.reboot(); + }); + + // Delay here to allow power to stabilise before Wi-Fi/Ethernet is initialised. + delay(300); // NOLINT + App.setup(); + + ESP_LOGI(TAG, "Waiting for OTA attempt."); + + return true; + } else { + // increment counter + this->write_rtc_(this->safe_mode_rtc_value_ + 1); + return false; + } +} +void OTAComponent::write_rtc_(uint32_t val) { + this->rtc_.save(&val); + global_preferences->sync(); +} +uint32_t OTAComponent::read_rtc_() { + uint32_t val; + if (!this->rtc_.load(&val)) + return 0; + return val; +} +void OTAComponent::clean_rtc() { this->write_rtc_(0); } +void OTAComponent::on_safe_shutdown() { + if (this->has_safe_mode_ && this->read_rtc_() != esphome::ota::OTAComponent::ENTER_SAFE_MODE_MAGIC) + this->clean_rtc(); +} + #ifdef USE_OTA_STATE_CALLBACK void OTAComponent::add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); diff --git a/esphome/components/ota/ota_component.h b/esphome/components/ota/ota_component.h index 461a43717d..c20f4f0709 100644 --- a/esphome/components/ota/ota_component.h +++ b/esphome/components/ota/ota_component.h @@ -1,25 +1,106 @@ #pragma once -#include "esphome/core/defines.h" +#include "esphome/components/socket/socket.h" #include "esphome/core/component.h" +#include "esphome/core/preferences.h" #include "esphome/core/helpers.h" +#include "esphome/core/defines.h" namespace esphome { namespace ota { +enum OTAResponseTypes { + OTA_RESPONSE_OK = 0x00, + OTA_RESPONSE_REQUEST_AUTH = 0x01, + + OTA_RESPONSE_HEADER_OK = 0x40, + OTA_RESPONSE_AUTH_OK = 0x41, + OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, + OTA_RESPONSE_BIN_MD5_OK = 0x43, + OTA_RESPONSE_RECEIVE_OK = 0x44, + OTA_RESPONSE_UPDATE_END_OK = 0x45, + OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, + OTA_RESPONSE_CHUNK_OK = 0x47, + + OTA_RESPONSE_ERROR_MAGIC = 0x80, + OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, + OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, + OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, + OTA_RESPONSE_ERROR_UPDATE_END = 0x84, + OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, + OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, + OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, + OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, + OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, + OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, + OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, + OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, + OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, +}; + enum OTAState { OTA_COMPLETED = 0, OTA_STARTED, OTA_IN_PROGRESS, OTA_ERROR }; +/// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA. class OTAComponent : public Component { public: - virtual bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) { return false; } + OTAComponent(); +#ifdef USE_OTA_PASSWORD + void set_auth_password(const std::string &password) { password_ = password; } +#endif // USE_OTA_PASSWORD + + /// Manually set the port OTA should listen on. + void set_port(uint16_t port); + + bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time); + /// Set to true if the next startup will enter safe mode - virtual void set_safe_mode_pending(const bool &pending) {} - virtual bool get_safe_mode_pending() { return false; } + void set_safe_mode_pending(const bool &pending); + bool get_safe_mode_pending(); #ifdef USE_OTA_STATE_CALLBACK void add_on_state_callback(std::function &&callback); #endif + + // ========== INTERNAL METHODS ========== + // (In most use cases you won't need these) + void setup() override; + void dump_config() override; + float get_setup_priority() const override; + void loop() override; + + uint16_t get_port() const; + + void clean_rtc(); + + void on_safe_shutdown() override; + protected: + void write_rtc_(uint32_t val); + uint32_t read_rtc_(); + + void handle_(); + bool readall_(uint8_t *buf, size_t len); + bool writeall_(const uint8_t *buf, size_t len); + +#ifdef USE_OTA_PASSWORD + std::string password_; +#endif // USE_OTA_PASSWORD + + uint16_t port_; + + std::unique_ptr server_; + std::unique_ptr client_; + + bool has_safe_mode_{false}; ///< stores whether safe mode can be enabled. + uint32_t safe_mode_start_time_; ///< stores when safe mode was enabled. + uint32_t safe_mode_enable_time_{60000}; ///< The time safe mode should be on for. + uint32_t safe_mode_rtc_value_; + uint8_t safe_mode_num_attempts_; + ESPPreferenceObject rtc_; + + static const uint32_t ENTER_SAFE_MODE_MAGIC = + 0x5afe5afe; ///< a magic number to indicate that safe mode should be entered on next boot + #ifdef USE_OTA_STATE_CALLBACK CallbackManager state_callback_{}; #endif diff --git a/esphome/components/ota_network/__init__.py b/esphome/components/ota_network/__init__.py deleted file mode 100644 index 0b3d6f29be..0000000000 --- a/esphome/components/ota_network/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -CODEOWNERS = ["@esphome/core"] -DEPENDENCIES = ["network"] -AUTO_LOAD = ["socket", "md5"] diff --git a/esphome/components/ota_network/ota_component.cpp b/esphome/components/ota_network/ota_component.cpp deleted file mode 100644 index 4e7f0880d7..0000000000 --- a/esphome/components/ota_network/ota_component.cpp +++ /dev/null @@ -1,527 +0,0 @@ -#include "ota_component.h" -#include "ota_backend.h" -#include "ota_backend_arduino_esp32.h" -#include "ota_backend_arduino_esp8266.h" -#include "ota_backend_arduino_rp2040.h" -#include "ota_backend_arduino_libretiny.h" -#include "ota_backend_esp_idf.h" - -#include "esphome/core/log.h" -#include "esphome/core/application.h" -#include "esphome/core/hal.h" -#include "esphome/core/util.h" -#include "esphome/components/md5/md5.h" -#include "esphome/components/network/util.h" - -#include -#include - -namespace esphome { -namespace ota_network { - -static const char *const TAG = "ota"; -static constexpr u_int16_t OTA_BLOCK_SIZE = 8192; - -std::unique_ptr make_ota_backend() { -#ifdef USE_ARDUINO -#ifdef USE_ESP8266 - return make_unique(); -#endif // USE_ESP8266 -#ifdef USE_ESP32 - return make_unique(); -#endif // USE_ESP32 -#endif // USE_ARDUINO -#ifdef USE_ESP_IDF - return make_unique(); -#endif // USE_ESP_IDF -#ifdef USE_RP2040 - return make_unique(); -#endif // USE_RP2040 -#ifdef USE_LIBRETINY - return make_unique(); -#endif -} - -OTAComponent::OTAComponent() { ota::global_ota_component = this; } - -void OTAComponent::setup() { - server_ = socket::socket_ip(SOCK_STREAM, 0); - if (server_ == nullptr) { - ESP_LOGW(TAG, "Could not create socket."); - this->mark_failed(); - return; - } - int enable = 1; - int err = server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err); - // we can still continue - } - err = server_->setblocking(false); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err); - this->mark_failed(); - return; - } - - struct sockaddr_storage server; - - socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_); - if (sl == 0) { - ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno); - this->mark_failed(); - return; - } - - err = server_->bind((struct sockaddr *) &server, sizeof(server)); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno); - this->mark_failed(); - return; - } - - err = server_->listen(4); - if (err != 0) { - ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno); - this->mark_failed(); - return; - } - - this->dump_config(); -} - -void OTAComponent::dump_config() { - ESP_LOGCONFIG(TAG, "Over-The-Air Updates:"); - ESP_LOGCONFIG(TAG, " Address: %s:%u", network::get_use_address().c_str(), this->port_); -#ifdef USE_OTA_PASSWORD - if (!this->password_.empty()) { - ESP_LOGCONFIG(TAG, " Using Password."); - } -#endif - ESP_LOGCONFIG(TAG, " OTA version: %d.", USE_OTA_VERSION); - if (this->has_safe_mode_ && this->safe_mode_rtc_value_ > 1 && - this->safe_mode_rtc_value_ != esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC) { - ESP_LOGW(TAG, "Last Boot was an unhandled reset, will proceed to safe mode in %" PRIu32 " restarts", - this->safe_mode_num_attempts_ - this->safe_mode_rtc_value_); - } -} - -void OTAComponent::loop() { - this->handle_(); - - if (this->has_safe_mode_ && (millis() - this->safe_mode_start_time_) > this->safe_mode_enable_time_) { - this->has_safe_mode_ = false; - // successful boot, reset counter - ESP_LOGI(TAG, "Boot seems successful, resetting boot loop counter."); - this->clean_rtc(); - } -} - -static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01; - -void OTAComponent::handle_() { - OTAResponseTypes error_code = OTA_RESPONSE_ERROR_UNKNOWN; - bool update_started = false; - size_t total = 0; - uint32_t last_progress = 0; - uint8_t buf[1024]; - char *sbuf = reinterpret_cast(buf); - size_t ota_size; - uint8_t ota_features; - std::unique_ptr backend; - (void) ota_features; -#if USE_OTA_VERSION == 2 - size_t size_acknowledged = 0; -#endif - - if (client_ == nullptr) { - struct sockaddr_storage source_addr; - socklen_t addr_len = sizeof(source_addr); - client_ = server_->accept((struct sockaddr *) &source_addr, &addr_len); - } - if (client_ == nullptr) - return; - - int enable = 1; - int err = client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int)); - if (err != 0) { - ESP_LOGW(TAG, "Socket could not enable tcp nodelay, errno: %d", errno); - return; - } - - ESP_LOGD(TAG, "Starting OTA Update from %s...", this->client_->getpeername().c_str()); - this->status_set_warning(); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(ota::OTA_STARTED, 0.0f, 0); -#endif - - if (!this->readall_(buf, 5)) { - ESP_LOGW(TAG, "Reading magic bytes failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - // 0x6C, 0x26, 0xF7, 0x5C, 0x45 - if (buf[0] != 0x6C || buf[1] != 0x26 || buf[2] != 0xF7 || buf[3] != 0x5C || buf[4] != 0x45) { - ESP_LOGW(TAG, "Magic bytes do not match! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", buf[0], buf[1], buf[2], buf[3], - buf[4]); - error_code = OTA_RESPONSE_ERROR_MAGIC; - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - // Send OK and version - 2 bytes - buf[0] = OTA_RESPONSE_OK; - buf[1] = USE_OTA_VERSION; - this->writeall_(buf, 2); - - backend = make_ota_backend(); - - // Read features - 1 byte - if (!this->readall_(buf, 1)) { - ESP_LOGW(TAG, "Reading features failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - ota_features = buf[0]; // NOLINT - ESP_LOGV(TAG, "OTA features is 0x%02X", ota_features); - - // Acknowledge header - 1 byte - buf[0] = OTA_RESPONSE_HEADER_OK; - if ((ota_features & FEATURE_SUPPORTS_COMPRESSION) != 0 && backend->supports_compression()) { - buf[0] = OTA_RESPONSE_SUPPORTS_COMPRESSION; - } - - this->writeall_(buf, 1); - -#ifdef USE_OTA_PASSWORD - if (!this->password_.empty()) { - buf[0] = OTA_RESPONSE_REQUEST_AUTH; - this->writeall_(buf, 1); - md5::MD5Digest md5{}; - md5.init(); - sprintf(sbuf, "%08" PRIx32, random_uint32()); - md5.add(sbuf, 8); - md5.calculate(); - md5.get_hex(sbuf); - ESP_LOGV(TAG, "Auth: Nonce is %s", sbuf); - - // Send nonce, 32 bytes hex MD5 - if (!this->writeall_(reinterpret_cast(sbuf), 32)) { - ESP_LOGW(TAG, "Auth: Writing nonce failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - // prepare challenge - md5.init(); - md5.add(this->password_.c_str(), this->password_.length()); - // add nonce - md5.add(sbuf, 32); - - // Receive cnonce, 32 bytes hex MD5 - if (!this->readall_(buf, 32)) { - ESP_LOGW(TAG, "Auth: Reading cnonce failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - sbuf[32] = '\0'; - ESP_LOGV(TAG, "Auth: CNonce is %s", sbuf); - // add cnonce - md5.add(sbuf, 32); - - // calculate result - md5.calculate(); - md5.get_hex(sbuf); - ESP_LOGV(TAG, "Auth: Result is %s", sbuf); - - // Receive result, 32 bytes hex MD5 - if (!this->readall_(buf + 64, 32)) { - ESP_LOGW(TAG, "Auth: Reading response failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - sbuf[64 + 32] = '\0'; - ESP_LOGV(TAG, "Auth: Response is %s", sbuf + 64); - - bool matches = true; - for (uint8_t i = 0; i < 32; i++) - matches = matches && buf[i] == buf[64 + i]; - - if (!matches) { - ESP_LOGW(TAG, "Auth failed! Passwords do not match!"); - error_code = OTA_RESPONSE_ERROR_AUTH_INVALID; - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - } -#endif // USE_OTA_PASSWORD - - // Acknowledge auth OK - 1 byte - buf[0] = OTA_RESPONSE_AUTH_OK; - this->writeall_(buf, 1); - - // Read size, 4 bytes MSB first - if (!this->readall_(buf, 4)) { - ESP_LOGW(TAG, "Reading size failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - ota_size = 0; - for (uint8_t i = 0; i < 4; i++) { - ota_size <<= 8; - ota_size |= buf[i]; - } - ESP_LOGV(TAG, "OTA size is %u bytes", ota_size); - - error_code = backend->begin(ota_size); - if (error_code != OTA_RESPONSE_OK) - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - update_started = true; - - // Acknowledge prepare OK - 1 byte - buf[0] = OTA_RESPONSE_UPDATE_PREPARE_OK; - this->writeall_(buf, 1); - - // Read binary MD5, 32 bytes - if (!this->readall_(buf, 32)) { - ESP_LOGW(TAG, "Reading binary MD5 checksum failed!"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - sbuf[32] = '\0'; - ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf); - backend->set_update_md5(sbuf); - - // Acknowledge MD5 OK - 1 byte - buf[0] = OTA_RESPONSE_BIN_MD5_OK; - this->writeall_(buf, 1); - - while (total < ota_size) { - // TODO: timeout check - size_t requested = std::min(sizeof(buf), ota_size - total); - ssize_t read = this->client_->read(buf, requested); - if (read == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - App.feed_wdt(); - delay(1); - continue; - } - ESP_LOGW(TAG, "Error receiving data for update, errno: %d", errno); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } else if (read == 0) { - // $ man recv - // "When a stream socket peer has performed an orderly shutdown, the return value will - // be 0 (the traditional "end-of-file" return)." - ESP_LOGW(TAG, "Remote end closed connection"); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - error_code = backend->write(buf, read); - if (error_code != OTA_RESPONSE_OK) { - ESP_LOGW(TAG, "Error writing binary data to flash!, error_code: %d", error_code); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - total += read; -#if USE_OTA_VERSION == 2 - while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) { - buf[0] = OTA_RESPONSE_CHUNK_OK; - this->writeall_(buf, 1); - size_acknowledged += OTA_BLOCK_SIZE; - } -#endif - - uint32_t now = millis(); - if (now - last_progress > 1000) { - last_progress = now; - float percentage = (total * 100.0f) / ota_size; - ESP_LOGD(TAG, "OTA in progress: %0.1f%%", percentage); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(ota::OTA_IN_PROGRESS, percentage, 0); -#endif - // feed watchdog and give other tasks a chance to run - App.feed_wdt(); - yield(); - } - } - - // Acknowledge receive OK - 1 byte - buf[0] = OTA_RESPONSE_RECEIVE_OK; - this->writeall_(buf, 1); - - error_code = backend->end(); - if (error_code != OTA_RESPONSE_OK) { - ESP_LOGW(TAG, "Error ending OTA!, error_code: %d", error_code); - goto error; // NOLINT(cppcoreguidelines-avoid-goto) - } - - // Acknowledge Update end OK - 1 byte - buf[0] = OTA_RESPONSE_UPDATE_END_OK; - this->writeall_(buf, 1); - - // Read ACK - if (!this->readall_(buf, 1) || buf[0] != OTA_RESPONSE_OK) { - ESP_LOGW(TAG, "Reading back acknowledgement failed!"); - // do not go to error, this is not fatal - } - - this->client_->close(); - this->client_ = nullptr; - delay(10); - ESP_LOGI(TAG, "OTA update finished!"); - this->status_clear_warning(); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(ota::OTA_COMPLETED, 100.0f, 0); -#endif - delay(100); // NOLINT - App.safe_reboot(); - -error: - buf[0] = static_cast(error_code); - this->writeall_(buf, 1); - this->client_->close(); - this->client_ = nullptr; - - if (backend != nullptr && update_started) { - backend->abort(); - } - - this->status_momentary_error("onerror", 5000); -#ifdef USE_OTA_STATE_CALLBACK - this->state_callback_.call(ota::OTA_ERROR, 0.0f, static_cast(error_code)); -#endif -} - -bool OTAComponent::readall_(uint8_t *buf, size_t len) { - uint32_t start = millis(); - uint32_t at = 0; - while (len - at > 0) { - uint32_t now = millis(); - if (now - start > 1000) { - ESP_LOGW(TAG, "Timed out reading %d bytes of data", len); - return false; - } - - ssize_t read = this->client_->read(buf + at, len - at); - if (read == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - App.feed_wdt(); - delay(1); - continue; - } - ESP_LOGW(TAG, "Failed to read %d bytes of data, errno: %d", len, errno); - return false; - } else if (read == 0) { - ESP_LOGW(TAG, "Remote closed connection"); - return false; - } else { - at += read; - } - App.feed_wdt(); - delay(1); - } - - return true; -} -bool OTAComponent::writeall_(const uint8_t *buf, size_t len) { - uint32_t start = millis(); - uint32_t at = 0; - while (len - at > 0) { - uint32_t now = millis(); - if (now - start > 1000) { - ESP_LOGW(TAG, "Timed out writing %d bytes of data", len); - return false; - } - - ssize_t written = this->client_->write(buf + at, len - at); - if (written == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) { - App.feed_wdt(); - delay(1); - continue; - } - ESP_LOGW(TAG, "Failed to write %d bytes of data, errno: %d", len, errno); - return false; - } else { - at += written; - } - App.feed_wdt(); - delay(1); - } - return true; -} - -float OTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; } -uint16_t OTAComponent::get_port() const { return this->port_; } -void OTAComponent::set_port(uint16_t port) { this->port_ = port; } - -void OTAComponent::set_safe_mode_pending(const bool &pending) { - if (!this->has_safe_mode_) - return; - - uint32_t current_rtc = this->read_rtc_(); - - if (pending && current_rtc != esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC) { - ESP_LOGI(TAG, "Device will enter safe mode on next boot."); - this->write_rtc_(esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC); - } - - if (!pending && current_rtc == esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC) { - ESP_LOGI(TAG, "Safe mode pending has been cleared"); - this->clean_rtc(); - } -} -bool OTAComponent::get_safe_mode_pending() { - return this->has_safe_mode_ && this->read_rtc_() == esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC; -} - -bool OTAComponent::should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) { - this->has_safe_mode_ = true; - this->safe_mode_start_time_ = millis(); - this->safe_mode_enable_time_ = enable_time; - this->safe_mode_num_attempts_ = num_attempts; - this->rtc_ = global_preferences->make_preference(233825507UL, false); - this->safe_mode_rtc_value_ = this->read_rtc_(); - - bool is_manual_safe_mode = this->safe_mode_rtc_value_ == esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC; - - if (is_manual_safe_mode) { - ESP_LOGI(TAG, "Safe mode has been entered manually"); - } else { - ESP_LOGCONFIG(TAG, "There have been %" PRIu32 " suspected unsuccessful boot attempts.", this->safe_mode_rtc_value_); - } - - if (this->safe_mode_rtc_value_ >= num_attempts || is_manual_safe_mode) { - this->clean_rtc(); - - if (!is_manual_safe_mode) { - ESP_LOGE(TAG, "Boot loop detected. Proceeding to safe mode."); - } - - this->status_set_error(); - this->set_timeout(enable_time, []() { - ESP_LOGE(TAG, "No OTA attempt made, restarting."); - App.reboot(); - }); - - // Delay here to allow power to stabilise before Wi-Fi/Ethernet is initialised. - delay(300); // NOLINT - App.setup(); - - ESP_LOGI(TAG, "Waiting for OTA attempt."); - - return true; - } else { - // increment counter - this->write_rtc_(this->safe_mode_rtc_value_ + 1); - return false; - } -} -void OTAComponent::write_rtc_(uint32_t val) { - this->rtc_.save(&val); - global_preferences->sync(); -} -uint32_t OTAComponent::read_rtc_() { - uint32_t val; - if (!this->rtc_.load(&val)) - return 0; - return val; -} -void OTAComponent::clean_rtc() { this->write_rtc_(0); } -void OTAComponent::on_safe_shutdown() { - if (this->has_safe_mode_ && this->read_rtc_() != esphome::ota_network::OTAComponent::ENTER_SAFE_MODE_MAGIC) - this->clean_rtc(); -} - -} // namespace ota_network -} // namespace esphome diff --git a/esphome/components/ota_network/ota_component.h b/esphome/components/ota_network/ota_component.h deleted file mode 100644 index 468637177b..0000000000 --- a/esphome/components/ota_network/ota_component.h +++ /dev/null @@ -1,98 +0,0 @@ -#pragma once - -#include "esphome/components/socket/socket.h" -#include "esphome/core/preferences.h" -#include "esphome/components/ota/ota_component.h" - -namespace esphome { -namespace ota_network { - -enum OTAResponseTypes { - OTA_RESPONSE_OK = 0x00, - OTA_RESPONSE_REQUEST_AUTH = 0x01, - - OTA_RESPONSE_HEADER_OK = 0x40, - OTA_RESPONSE_AUTH_OK = 0x41, - OTA_RESPONSE_UPDATE_PREPARE_OK = 0x42, - OTA_RESPONSE_BIN_MD5_OK = 0x43, - OTA_RESPONSE_RECEIVE_OK = 0x44, - OTA_RESPONSE_UPDATE_END_OK = 0x45, - OTA_RESPONSE_SUPPORTS_COMPRESSION = 0x46, - OTA_RESPONSE_CHUNK_OK = 0x47, - - OTA_RESPONSE_ERROR_MAGIC = 0x80, - OTA_RESPONSE_ERROR_UPDATE_PREPARE = 0x81, - OTA_RESPONSE_ERROR_AUTH_INVALID = 0x82, - OTA_RESPONSE_ERROR_WRITING_FLASH = 0x83, - OTA_RESPONSE_ERROR_UPDATE_END = 0x84, - OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING = 0x85, - OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG = 0x86, - OTA_RESPONSE_ERROR_WRONG_NEW_FLASH_CONFIG = 0x87, - OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE = 0x88, - OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE = 0x89, - OTA_RESPONSE_ERROR_NO_UPDATE_PARTITION = 0x8A, - OTA_RESPONSE_ERROR_MD5_MISMATCH = 0x8B, - OTA_RESPONSE_ERROR_RP2040_NOT_ENOUGH_SPACE = 0x8C, - OTA_RESPONSE_ERROR_UNKNOWN = 0xFF, -}; - -/// OTAComponent provides a simple way to integrate Over-the-Air updates into your app using ArduinoOTA. -class OTAComponent : public ota::OTAComponent { - public: - OTAComponent(); -#ifdef USE_OTA_PASSWORD - void set_auth_password(const std::string &password) { password_ = password; } -#endif // USE_OTA_PASSWORD - - /// Manually set the port OTA should listen on. - void set_port(uint16_t port); - - bool should_enter_safe_mode(uint8_t num_attempts, uint32_t enable_time) override; - - /// Set to true if the next startup will enter safe mode - void set_safe_mode_pending(const bool &pending) override; - bool get_safe_mode_pending() override; - - // ========== INTERNAL METHODS ========== - // (In most use cases you won't need these) - void setup() override; - void dump_config() override; - float get_setup_priority() const override; - void loop() override; - - uint16_t get_port() const; - - void clean_rtc(); - - void on_safe_shutdown() override; - - protected: - void write_rtc_(uint32_t val); - uint32_t read_rtc_(); - - void handle_(); - bool readall_(uint8_t *buf, size_t len); - bool writeall_(const uint8_t *buf, size_t len); - -#ifdef USE_OTA_PASSWORD - std::string password_; -#endif // USE_OTA_PASSWORD - - uint16_t port_; - - std::unique_ptr server_; - std::unique_ptr client_; - - bool has_safe_mode_{false}; ///< stores whether safe mode can be enabled. - uint32_t safe_mode_start_time_; ///< stores when safe mode was enabled. - uint32_t safe_mode_enable_time_{60000}; ///< The time safe mode should be on for. - uint32_t safe_mode_rtc_value_; - uint8_t safe_mode_num_attempts_; - ESPPreferenceObject rtc_; - - static const uint32_t ENTER_SAFE_MODE_MAGIC = - 0x5afe5afe; ///< a magic number to indicate that safe mode should be entered on next boot -}; - -} // namespace ota_network -} // namespace esphome