From a5574bbabe27fc086bf95b1a45365801566f8360 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 26 Dec 2025 21:59:47 -1000 Subject: [PATCH] dry --- .../components/ota/ota_backend_esp8266.cpp | 59 +++++++++---------- esphome/components/ota/ota_backend_esp8266.h | 6 ++ 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/esphome/components/ota/ota_backend_esp8266.cpp b/esphome/components/ota/ota_backend_esp8266.cpp index b849c55e71..38e9d214b2 100644 --- a/esphome/components/ota/ota_backend_esp8266.cpp +++ b/esphome/components/ota/ota_backend_esp8266.cpp @@ -151,19 +151,36 @@ OTAResponseTypes ESP8266OTABackend::write(uint8_t *data, size_t len) { return OTA_RESPONSE_OK; } +bool ESP8266OTABackend::erase_sector_if_needed_() { + if ((this->current_address_ % FLASH_SECTOR_SIZE) != 0) { + return true; // Not at sector boundary + } + + App.feed_wdt(); + if (spi_flash_erase_sector(this->current_address_ / FLASH_SECTOR_SIZE) != SPI_FLASH_RESULT_OK) { + ESP_LOGE(TAG, "Flash erase failed at 0x%08" PRIX32, this->current_address_); + return false; + } + return true; +} + +bool ESP8266OTABackend::flash_write_() { + App.feed_wdt(); + if (spi_flash_write(this->current_address_, reinterpret_cast(this->buffer_.get()), this->buffer_len_) != + SPI_FLASH_RESULT_OK) { + ESP_LOGE(TAG, "Flash write failed at 0x%08" PRIX32, this->current_address_); + return false; + } + return true; +} + bool ESP8266OTABackend::write_buffer_() { if (this->buffer_len_ == 0) { return true; } - // Erase sector if we're at a sector boundary - if ((this->current_address_ % FLASH_SECTOR_SIZE) == 0) { - App.feed_wdt(); - SpiFlashOpResult erase_result = spi_flash_erase_sector(this->current_address_ / FLASH_SECTOR_SIZE); - if (erase_result != SPI_FLASH_RESULT_OK) { - ESP_LOGE(TAG, "Flash erase failed at 0x%08" PRIX32, this->current_address_); - return false; - } + if (!this->erase_sector_if_needed_()) { + return false; } // Patch flash mode in first sector if needed @@ -185,13 +202,7 @@ bool ESP8266OTABackend::write_buffer_() { } } - // Write to flash (must be 4-byte aligned) - App.feed_wdt(); - SpiFlashOpResult write_result = - spi_flash_write(this->current_address_, reinterpret_cast(this->buffer_.get()), this->buffer_len_); - - if (write_result != SPI_FLASH_RESULT_OK) { - ESP_LOGE(TAG, "Flash write failed at 0x%08" PRIX32, this->current_address_); + if (!this->flash_write_()) { return false; } @@ -215,23 +226,11 @@ bool ESP8266OTABackend::write_buffer_final_() { return true; } - // Erase sector if we're at a sector boundary - if ((this->current_address_ % FLASH_SECTOR_SIZE) == 0) { - App.feed_wdt(); - SpiFlashOpResult erase_result = spi_flash_erase_sector(this->current_address_ / FLASH_SECTOR_SIZE); - if (erase_result != SPI_FLASH_RESULT_OK) { - ESP_LOGE(TAG, "Flash erase failed at 0x%08" PRIX32, this->current_address_); - return false; - } + if (!this->erase_sector_if_needed_()) { + return false; } - // Write to flash (must be 4-byte aligned) - App.feed_wdt(); - SpiFlashOpResult write_result = - spi_flash_write(this->current_address_, reinterpret_cast(this->buffer_.get()), this->buffer_len_); - - if (write_result != SPI_FLASH_RESULT_OK) { - ESP_LOGE(TAG, "Flash write failed at 0x%08" PRIX32, this->current_address_); + if (!this->flash_write_()) { return false; } diff --git a/esphome/components/ota/ota_backend_esp8266.h b/esphome/components/ota/ota_backend_esp8266.h index d901dd9127..d3668d4d10 100644 --- a/esphome/components/ota/ota_backend_esp8266.h +++ b/esphome/components/ota/ota_backend_esp8266.h @@ -22,6 +22,12 @@ class ESP8266OTABackend : public OTABackend { bool supports_compression() override { return true; } protected: + /// Erase flash sector if current address is at sector boundary + bool erase_sector_if_needed_(); + + /// Write buffer to flash (does not update address or clear buffer) + bool flash_write_(); + /// Write buffered data to flash and update MD5 bool write_buffer_();