ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
ota_backend_esp8266.cpp
Go to the documentation of this file.
1#ifdef USE_ESP8266
3#include "ota_backend.h"
4
9#include "esphome/core/log.h"
10
11#include <Esp.h>
12#include <esp8266_peri.h>
13
14#include <cinttypes>
15
16extern "C" {
17#include <c_types.h>
18#include <eboot_command.h>
19#include <flash_hal.h>
20#include <spi_flash.h>
21#include <user_interface.h>
22}
23
24// Note: FLASH_SECTOR_SIZE (0x1000) is already defined in spi_flash_geometry.h
25
26// Flash header offsets
27static constexpr uint8_t FLASH_MODE_OFFSET = 2;
28
29// Firmware magic bytes
30static constexpr uint8_t FIRMWARE_MAGIC = 0xE9;
31static constexpr uint8_t GZIP_MAGIC_1 = 0x1F;
32static constexpr uint8_t GZIP_MAGIC_2 = 0x8B;
33
34// ESP8266 flash memory base address (memory-mapped flash starts here)
35static constexpr uint32_t FLASH_BASE_ADDRESS = 0x40200000;
36
37// Boot mode extraction from GPI register (bits 16-19 contain boot mode)
38static constexpr int BOOT_MODE_SHIFT = 16;
39static constexpr int BOOT_MODE_MASK = 0xf;
40
41// Boot mode indicating UART download mode (OTA not possible)
42static constexpr int BOOT_MODE_UART_DOWNLOAD = 1;
43
44// Minimum buffer size when memory is constrained
45static constexpr size_t MIN_BUFFER_SIZE = 256;
46
47namespace esphome::ota {
48
49static const char *const TAG = "ota.esp8266";
50
51std::unique_ptr<ESP8266OTABackend> make_ota_backend() { return make_unique<ESP8266OTABackend>(); }
52
54 if (ota_type != OTA_TYPE_UPDATE_APP) {
56 }
57 // Handle UPDATE_SIZE_UNKNOWN (0) by calculating available space
58 if (image_size == 0) {
59 // Round down to sector boundary: subtract one sector, then mask to sector alignment
60 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
61 image_size = (ESP.getFreeSketchSpace() - FLASH_SECTOR_SIZE) & ~(FLASH_SECTOR_SIZE - 1);
62 }
63
64 // Check boot mode - if boot mode is UART download mode,
65 // we will not be able to reset into normal mode once update is done
66 // NOLINTNEXTLINE(clang-analyzer-core.FixedAddressDereference) -- GPI is MMIO at a fixed address
67 int boot_mode = (GPI >> BOOT_MODE_SHIFT) & BOOT_MODE_MASK;
68 if (boot_mode == BOOT_MODE_UART_DOWNLOAD) {
70 }
71
72 // Check flash configuration - real size must be >= configured size
73 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
74 if (!ESP.checkFlashConfig(false)) {
76 }
77
78 // Get current sketch size
79 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
80 uint32_t sketch_size = ESP.getSketchSize();
81
82 // Size of current sketch rounded to sector boundary
83 uint32_t current_sketch_size = (sketch_size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
84
85 // Size of update rounded to sector boundary
86 uint32_t rounded_size = (image_size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
87
88 // End of available space for sketch and update (start of filesystem)
89 uint32_t update_end_address = FS_start - FLASH_BASE_ADDRESS;
90
91 // Calculate start address for the update (write from end backwards)
92 this->start_address_ = (update_end_address > rounded_size) ? (update_end_address - rounded_size) : 0;
93
94 // Check if there's enough space for both current sketch and update
95 if (this->start_address_ < current_sketch_size) {
97 }
98
99 // Allocate buffer for sector writes (use smaller buffer if memory constrained)
100 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
101 this->buffer_size_ = (ESP.getFreeHeap() > 2 * FLASH_SECTOR_SIZE) ? FLASH_SECTOR_SIZE : MIN_BUFFER_SIZE;
102
103 // ESP8266's umm_malloc guarantees 4-byte aligned allocations, which is required
104 // for spi_flash_write(). This is the same pattern used by Arduino's Updater class.
105 this->buffer_ = make_unique<uint8_t[]>(this->buffer_size_);
106 if (!this->buffer_) {
108 }
109
110 this->current_address_ = this->start_address_;
111 this->image_size_ = image_size;
112 this->bytes_received_ = 0;
113 this->buffer_len_ = 0;
114 this->md5_set_ = false;
115
116 // Disable WiFi sleep during update
117 wifi_set_sleep_type(NONE_SLEEP_T);
118
119 // Prevent preference writes during update
121
122 // Initialize MD5 computation
123 this->md5_.init();
124
125 ESP_LOGD(TAG, "OTA begin: start=0x%08" PRIX32 ", size=%zu", this->start_address_, image_size);
126
127 return OTA_RESPONSE_OK;
128}
129
131 // Parse hex string to bytes
132 if (parse_hex(md5, this->expected_md5_, 16)) {
133 this->md5_set_ = true;
134 }
135}
136
138 if (!this->buffer_) {
140 }
141
142 size_t written = 0;
143 while (written < len) {
144 // Calculate how much we can buffer
145 size_t to_buffer = std::min(len - written, this->buffer_size_ - this->buffer_len_);
146 memcpy(this->buffer_.get() + this->buffer_len_, data + written, to_buffer);
147 this->buffer_len_ += to_buffer;
148 this->bytes_received_ += to_buffer;
149 written += to_buffer;
150
151 // If buffer is full, write to flash
152 if (this->buffer_len_ == this->buffer_size_ && !this->write_buffer_()) {
154 }
155 }
156
157 return OTA_RESPONSE_OK;
158}
159
161 if ((this->current_address_ % FLASH_SECTOR_SIZE) != 0) {
162 return true; // Not at sector boundary
163 }
164
165 App.feed_wdt();
166 if (spi_flash_erase_sector(this->current_address_ / FLASH_SECTOR_SIZE) != SPI_FLASH_RESULT_OK) {
167 ESP_LOGE(TAG, "Flash erase failed at 0x%08" PRIX32, this->current_address_);
168 return false;
169 }
170 return true;
171}
172
174 App.feed_wdt();
175 if (spi_flash_write(this->current_address_, reinterpret_cast<uint32_t *>(this->buffer_.get()), this->buffer_len_) !=
176 SPI_FLASH_RESULT_OK) {
177 ESP_LOGE(TAG, "Flash write failed at 0x%08" PRIX32, this->current_address_);
178 return false;
179 }
180 return true;
181}
182
184 if (this->buffer_len_ == 0) {
185 return true;
186 }
187
188 if (!this->erase_sector_if_needed_()) {
189 return false;
190 }
191
192 // Patch flash mode in first sector if needed
193 // This is analogous to what esptool.py does when it receives a --flash_mode argument
194 bool is_first_sector = (this->current_address_ == this->start_address_);
195 uint8_t original_flash_mode = 0;
196 bool patched_flash_mode = false;
197
198 // Only patch if we have enough bytes to access flash mode offset and it's not GZIP
199 if (is_first_sector && this->buffer_len_ > FLASH_MODE_OFFSET && this->buffer_[0] != GZIP_MAGIC_1) {
200 // Not GZIP compressed - check and patch flash mode
201 uint8_t current_flash_mode = this->get_flash_chip_mode_();
202 uint8_t buffer_flash_mode = this->buffer_[FLASH_MODE_OFFSET];
203
204 if (buffer_flash_mode != current_flash_mode) {
205 original_flash_mode = buffer_flash_mode;
206 this->buffer_[FLASH_MODE_OFFSET] = current_flash_mode;
207 patched_flash_mode = true;
208 }
209 }
210
211 if (!this->flash_write_()) {
212 return false;
213 }
214
215 // Restore original flash mode for MD5 calculation
216 if (patched_flash_mode) {
217 this->buffer_[FLASH_MODE_OFFSET] = original_flash_mode;
218 }
219
220 // Update MD5 with original (unpatched) data
221 this->md5_.add(this->buffer_.get(), this->buffer_len_);
222
223 this->current_address_ += this->buffer_len_;
224 this->buffer_len_ = 0;
225
226 return true;
227}
228
230 // Similar to write_buffer_(), but without flash mode patching or MD5 update (for final padded write)
231 if (this->buffer_len_ == 0) {
232 return true;
233 }
234
235 if (!this->erase_sector_if_needed_() || !this->flash_write_()) {
236 return false;
237 }
238
239 this->current_address_ += this->buffer_len_;
240 this->buffer_len_ = 0;
241
242 return true;
243}
244
246 // Write any remaining buffered data
247 if (this->buffer_len_ > 0) {
248 // Add actual data to MD5 before padding
249 this->md5_.add(this->buffer_.get(), this->buffer_len_);
250
251 // Pad to 4-byte alignment for flash write
252 while (this->buffer_len_ % 4 != 0) {
253 this->buffer_[this->buffer_len_++] = 0xFF;
254 }
255 if (!this->write_buffer_final_()) {
256 this->abort();
258 }
259 }
260
261 // Calculate actual bytes written (exact uploaded size, excluding flash write padding)
262 size_t actual_size = this->bytes_received_;
263
264 // Check if any data was written
265 if (actual_size == 0) {
266 ESP_LOGE(TAG, "No data written");
267 this->abort();
269 }
270
271 // Verify MD5 if set (strict mode), otherwise use lenient mode
272 // In lenient mode (no MD5), we accept whatever was written
273 if (this->md5_set_) {
274 this->md5_.calculate();
275 if (!this->md5_.equals_bytes(this->expected_md5_)) {
276 ESP_LOGE(TAG, "MD5 mismatch");
277 this->abort();
279 }
280 } else {
281 // Lenient mode: adjust size to what was actually written
282 // This matches Arduino's Update.end(true) behavior
283 this->image_size_ = actual_size;
284 }
285
286 // Verify firmware header
287 if (!this->verify_end_()) {
288 this->abort();
290 }
291
292 // Write eboot command to copy firmware on next boot
293 eboot_command ebcmd;
294 ebcmd.action = ACTION_COPY_RAW;
295 ebcmd.args[0] = this->start_address_;
296 ebcmd.args[1] = 0x00000; // Destination: start of flash
297 ebcmd.args[2] = this->image_size_;
298 eboot_command_write(&ebcmd);
299
300 ESP_LOGI(TAG, "OTA update staged: 0x%08" PRIX32 " -> 0x00000, size=%zu", this->start_address_, this->image_size_);
301
302 // Clean up
303 this->buffer_.reset();
305
306 return OTA_RESPONSE_OK;
307}
308
310 this->buffer_.reset();
311 this->buffer_len_ = 0;
312 this->image_size_ = 0;
313 this->bytes_received_ = 0;
315}
316
318 uint32_t buf;
319 if (spi_flash_read(this->start_address_, &buf, 4) != SPI_FLASH_RESULT_OK) {
320 ESP_LOGE(TAG, "Failed to read firmware header");
321 return false;
322 }
323
324 uint8_t *bytes = reinterpret_cast<uint8_t *>(&buf);
325
326 // Check for GZIP (compressed firmware)
327 if (bytes[0] == GZIP_MAGIC_1 && bytes[1] == GZIP_MAGIC_2) {
328 // GZIP compressed - can't verify further
329 return true;
330 }
331
332 // Check firmware magic byte
333 if (bytes[0] != FIRMWARE_MAGIC) {
334 ESP_LOGE(TAG, "Invalid firmware magic: 0x%02X (expected 0x%02X)", bytes[0], FIRMWARE_MAGIC);
335 return false;
336 }
337
338#if !FLASH_MAP_SUPPORT
339 // Check if new firmware's flash size fits (only when auto-detection is disabled)
340 // With FLASH_MAP_SUPPORT (modern cores), flash size is auto-detected from chip
341 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
342 uint32_t bin_flash_size = ESP.magicFlashChipSize((bytes[3] & 0xf0) >> 4);
343 // NOLINTNEXTLINE(readability-static-accessed-through-instance)
344 if (bin_flash_size > ESP.getFlashChipRealSize()) {
345 ESP_LOGE(TAG, "Firmware flash size (%" PRIu32 ") exceeds chip size (%" PRIu32 ")", bin_flash_size,
346 ESP.getFlashChipRealSize());
347 return false;
348 }
349#endif
350
351 return true;
352}
353
355 uint32_t data;
356 if (spi_flash_read(0x0000, &data, 4) != SPI_FLASH_RESULT_OK) {
357 return 0; // Default to QIO
358 }
359 return (reinterpret_cast<uint8_t *>(&data))[FLASH_MODE_OFFSET];
360}
361
362} // namespace esphome::ota
363#endif // USE_ESP8266
void feed_wdt()
Feed the task watchdog.
bool equals_bytes(const uint8_t *expected)
Compare the hash against a provided byte-encoded hash.
Definition hash_base.h:32
void calculate() override
Compute the digest, based on the provided data.
Definition md5.cpp:16
void add(const uint8_t *data, size_t len) override
Add bytes of data for the digest.
Definition md5.cpp:14
void init() override
Initialize a new MD5 digest computation.
Definition md5.cpp:9
bool write_buffer_final_()
Write buffered data to flash without MD5 update (for final padded write)
OTAResponseTypes write(uint8_t *data, size_t len)
std::unique_ptr< uint8_t[]> buffer_
bool erase_sector_if_needed_()
Erase flash sector if current address is at sector boundary.
bool write_buffer_()
Write buffered data to flash and update MD5.
OTAResponseTypes begin(size_t image_size, OTAType ota_type=OTA_TYPE_UPDATE_APP)
bool verify_end_()
Verify the firmware header is valid.
bool flash_write_()
Write buffer to flash (does not update address or clear buffer)
uint8_t get_flash_chip_mode_()
Get current flash chip mode from flash header.
void preferences_prevent_write(bool prevent)
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
Definition ota_backend.h:41
@ OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG
Definition ota_backend.h:36
@ OTA_RESPONSE_ERROR_WRITING_FLASH
Definition ota_backend.h:33
@ OTA_RESPONSE_ERROR_ESP8266_NOT_ENOUGH_SPACE
Definition ota_backend.h:38
@ OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE
Definition ota_backend.h:44
@ OTA_RESPONSE_ERROR_UPDATE_END
Definition ota_backend.h:34
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:49
@ OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING
Definition ota_backend.h:35
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
std::string size_t len
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:275
int written
Definition helpers.h:1045
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t