ESPHome 2025.8.2
Loading...
Searching...
No Matches
ota_esphome.cpp
Go to the documentation of this file.
1#include "ota_esphome.h"
2#ifdef USE_OTA
12#include "esphome/core/hal.h"
13#include "esphome/core/log.h"
14#include "esphome/core/util.h"
15
16#include <cerrno>
17#include <cstdio>
18
19namespace esphome {
20
21static const char *const TAG = "esphome.ota";
22static constexpr uint16_t OTA_BLOCK_SIZE = 8192;
23static constexpr uint32_t OTA_SOCKET_TIMEOUT_HANDSHAKE = 10000; // milliseconds for initial handshake
24static constexpr uint32_t OTA_SOCKET_TIMEOUT_DATA = 90000; // milliseconds for data transfer
25
27#ifdef USE_OTA_STATE_CALLBACK
29#endif
30
31 this->server_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0); // monitored for incoming connections
32 if (this->server_ == nullptr) {
33 this->log_socket_error_("creation");
34 this->mark_failed();
35 return;
36 }
37 int enable = 1;
38 int err = this->server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
39 if (err != 0) {
40 this->log_socket_error_("reuseaddr");
41 // we can still continue
42 }
43 err = this->server_->setblocking(false);
44 if (err != 0) {
45 this->log_socket_error_("non-blocking");
46 this->mark_failed();
47 return;
48 }
49
50 struct sockaddr_storage server;
51
52 socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_);
53 if (sl == 0) {
54 this->log_socket_error_("set sockaddr");
55 this->mark_failed();
56 return;
57 }
58
59 err = this->server_->bind((struct sockaddr *) &server, sizeof(server));
60 if (err != 0) {
61 this->log_socket_error_("bind");
62 this->mark_failed();
63 return;
64 }
65
66 err = this->server_->listen(4);
67 if (err != 0) {
68 this->log_socket_error_("listen");
69 this->mark_failed();
70 return;
71 }
72}
73
75 ESP_LOGCONFIG(TAG,
76 "Over-The-Air updates:\n"
77 " Address: %s:%u\n"
78 " Version: %d",
79 network::get_use_address().c_str(), this->port_, USE_OTA_VERSION);
80#ifdef USE_OTA_PASSWORD
81 if (!this->password_.empty()) {
82 ESP_LOGCONFIG(TAG, " Password configured");
83 }
84#endif
85}
86
88 // Skip handle_handshake_() call if no client connected and no incoming connections
89 // This optimization reduces idle loop overhead when OTA is not active
90 // Note: No need to check server_ for null as the component is marked failed in setup()
91 // if server_ creation fails
92 if (this->client_ != nullptr || this->server_->ready()) {
93 this->handle_handshake_();
94 }
95}
96
97static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01;
98
105
106 if (this->client_ == nullptr) {
107 // We already checked server_->ready() in loop(), so we can accept directly
108 struct sockaddr_storage source_addr;
109 socklen_t addr_len = sizeof(source_addr);
110 int enable = 1;
111
112 this->client_ = this->server_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
113 if (this->client_ == nullptr)
114 return;
115 int err = this->client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
116 if (err != 0) {
117 this->log_socket_error_("nodelay");
118 this->cleanup_connection_();
119 return;
120 }
121 err = this->client_->setblocking(false);
122 if (err != 0) {
123 this->log_socket_error_("non-blocking");
124 this->cleanup_connection_();
125 return;
126 }
127 this->log_start_("handshake");
129 this->magic_buf_pos_ = 0; // Reset magic buffer position
130 }
131
132 // Check for handshake timeout
133 uint32_t now = App.get_loop_component_start_time();
134 if (now - this->client_connect_time_ > OTA_SOCKET_TIMEOUT_HANDSHAKE) {
135 ESP_LOGW(TAG, "Handshake timeout");
136 this->cleanup_connection_();
137 return;
138 }
139
140 // Try to read remaining magic bytes
141 if (this->magic_buf_pos_ < 5) {
142 // Read as many bytes as available
143 uint8_t bytes_to_read = 5 - this->magic_buf_pos_;
144 ssize_t read = this->client_->read(this->magic_buf_ + this->magic_buf_pos_, bytes_to_read);
145
146 if (read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
147 return; // No data yet, try again next loop
148 }
149
150 if (read <= 0) {
151 // Error or connection closed
152 if (read == -1) {
153 this->log_socket_error_("reading magic bytes");
154 } else {
155 ESP_LOGW(TAG, "Remote closed during handshake");
156 }
157 this->cleanup_connection_();
158 return;
159 }
160
161 this->magic_buf_pos_ += read;
162 }
163
164 // Check if we have all 5 magic bytes
165 if (this->magic_buf_pos_ == 5) {
166 // Validate magic bytes
167 static const uint8_t MAGIC_BYTES[5] = {0x6C, 0x26, 0xF7, 0x5C, 0x45};
168 if (memcmp(this->magic_buf_, MAGIC_BYTES, 5) != 0) {
169 ESP_LOGW(TAG, "Magic bytes mismatch! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", this->magic_buf_[0],
170 this->magic_buf_[1], this->magic_buf_[2], this->magic_buf_[3], this->magic_buf_[4]);
171 // Send error response (non-blocking, best effort)
172 uint8_t error = static_cast<uint8_t>(ota::OTA_RESPONSE_ERROR_MAGIC);
173 this->client_->write(&error, 1);
174 this->cleanup_connection_();
175 return;
176 }
177
178 // All 5 magic bytes are valid, continue with data handling
179 this->handle_data_();
180 }
181}
182
190 bool update_started = false;
191 size_t total = 0;
192 uint32_t last_progress = 0;
193 uint8_t buf[1024];
194 char *sbuf = reinterpret_cast<char *>(buf);
195 size_t ota_size;
196 uint8_t ota_features;
197 std::unique_ptr<ota::OTABackend> backend;
198 (void) ota_features;
199#if USE_OTA_VERSION == 2
200 size_t size_acknowledged = 0;
201#endif
202
203 // Send OK and version - 2 bytes
204 buf[0] = ota::OTA_RESPONSE_OK;
205 buf[1] = USE_OTA_VERSION;
206 this->writeall_(buf, 2);
207
208 backend = ota::make_ota_backend();
209
210 // Read features - 1 byte
211 if (!this->readall_(buf, 1)) {
212 this->log_read_error_("features");
213 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
214 }
215 ota_features = buf[0]; // NOLINT
216 ESP_LOGV(TAG, "Features: 0x%02X", ota_features);
217
218 // Acknowledge header - 1 byte
220 if ((ota_features & FEATURE_SUPPORTS_COMPRESSION) != 0 && backend->supports_compression()) {
222 }
223
224 this->writeall_(buf, 1);
225
226#ifdef USE_OTA_PASSWORD
227 if (!this->password_.empty()) {
229 this->writeall_(buf, 1);
230 md5::MD5Digest md5{};
231 md5.init();
232 sprintf(sbuf, "%08" PRIx32, random_uint32());
233 md5.add(sbuf, 8);
234 md5.calculate();
235 md5.get_hex(sbuf);
236 ESP_LOGV(TAG, "Auth: Nonce is %s", sbuf);
237
238 // Send nonce, 32 bytes hex MD5
239 if (!this->writeall_(reinterpret_cast<uint8_t *>(sbuf), 32)) {
240 ESP_LOGW(TAG, "Auth: Writing nonce failed");
241 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
242 }
243
244 // prepare challenge
245 md5.init();
246 md5.add(this->password_.c_str(), this->password_.length());
247 // add nonce
248 md5.add(sbuf, 32);
249
250 // Receive cnonce, 32 bytes hex MD5
251 if (!this->readall_(buf, 32)) {
252 ESP_LOGW(TAG, "Auth: Reading cnonce failed");
253 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
254 }
255 sbuf[32] = '\0';
256 ESP_LOGV(TAG, "Auth: CNonce is %s", sbuf);
257 // add cnonce
258 md5.add(sbuf, 32);
259
260 // calculate result
261 md5.calculate();
262 md5.get_hex(sbuf);
263 ESP_LOGV(TAG, "Auth: Result is %s", sbuf);
264
265 // Receive result, 32 bytes hex MD5
266 if (!this->readall_(buf + 64, 32)) {
267 ESP_LOGW(TAG, "Auth: Reading response failed");
268 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
269 }
270 sbuf[64 + 32] = '\0';
271 ESP_LOGV(TAG, "Auth: Response is %s", sbuf + 64);
272
273 bool matches = true;
274 for (uint8_t i = 0; i < 32; i++)
275 matches = matches && buf[i] == buf[64 + i];
276
277 if (!matches) {
278 ESP_LOGW(TAG, "Auth failed! Passwords do not match");
280 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
281 }
282 }
283#endif // USE_OTA_PASSWORD
284
285 // Acknowledge auth OK - 1 byte
287 this->writeall_(buf, 1);
288
289 // Read size, 4 bytes MSB first
290 if (!this->readall_(buf, 4)) {
291 this->log_read_error_("size");
292 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
293 }
294 ota_size = 0;
295 for (uint8_t i = 0; i < 4; i++) {
296 ota_size <<= 8;
297 ota_size |= buf[i];
298 }
299 ESP_LOGV(TAG, "Size is %u bytes", ota_size);
300
301 // Now that we've passed authentication and are actually
302 // starting the update, set the warning status and notify
303 // listeners. This ensures that port scanners do not
304 // accidentally trigger the update process.
305 this->log_start_("update");
306 this->status_set_warning();
307#ifdef USE_OTA_STATE_CALLBACK
308 this->state_callback_.call(ota::OTA_STARTED, 0.0f, 0);
309#endif
310
311 // This will block for a few seconds as it locks flash
312 error_code = backend->begin(ota_size);
313 if (error_code != ota::OTA_RESPONSE_OK)
314 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
315 update_started = true;
316
317 // Acknowledge prepare OK - 1 byte
319 this->writeall_(buf, 1);
320
321 // Read binary MD5, 32 bytes
322 if (!this->readall_(buf, 32)) {
323 this->log_read_error_("MD5 checksum");
324 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
325 }
326 sbuf[32] = '\0';
327 ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf);
328 backend->set_update_md5(sbuf);
329
330 // Acknowledge MD5 OK - 1 byte
332 this->writeall_(buf, 1);
333
334 while (total < ota_size) {
335 // TODO: timeout check
336 size_t requested = std::min(sizeof(buf), ota_size - total);
337 ssize_t read = this->client_->read(buf, requested);
338 if (read == -1) {
339 if (errno == EAGAIN || errno == EWOULDBLOCK) {
341 continue;
342 }
343 ESP_LOGW(TAG, "Read error, errno %d", errno);
344 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
345 } else if (read == 0) {
346 // $ man recv
347 // "When a stream socket peer has performed an orderly shutdown, the return value will
348 // be 0 (the traditional "end-of-file" return)."
349 ESP_LOGW(TAG, "Remote closed connection");
350 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
351 }
352
353 error_code = backend->write(buf, read);
354 if (error_code != ota::OTA_RESPONSE_OK) {
355 ESP_LOGW(TAG, "Flash write error, code: %d", error_code);
356 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
357 }
358 total += read;
359#if USE_OTA_VERSION == 2
360 while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) {
362 this->writeall_(buf, 1);
363 size_acknowledged += OTA_BLOCK_SIZE;
364 }
365#endif
366
367 uint32_t now = millis();
368 if (now - last_progress > 1000) {
369 last_progress = now;
370 float percentage = (total * 100.0f) / ota_size;
371 ESP_LOGD(TAG, "Progress: %0.1f%%", percentage);
372#ifdef USE_OTA_STATE_CALLBACK
373 this->state_callback_.call(ota::OTA_IN_PROGRESS, percentage, 0);
374#endif
375 // feed watchdog and give other tasks a chance to run
377 }
378 }
379
380 // Acknowledge receive OK - 1 byte
382 this->writeall_(buf, 1);
383
384 error_code = backend->end();
385 if (error_code != ota::OTA_RESPONSE_OK) {
386 ESP_LOGW(TAG, "Error ending update! code: %d", error_code);
387 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
388 }
389
390 // Acknowledge Update end OK - 1 byte
392 this->writeall_(buf, 1);
393
394 // Read ACK
395 if (!this->readall_(buf, 1) || buf[0] != ota::OTA_RESPONSE_OK) {
396 this->log_read_error_("ack");
397 // do not go to error, this is not fatal
398 }
399
400 this->cleanup_connection_();
401 delay(10);
402 ESP_LOGI(TAG, "Update complete");
403 this->status_clear_warning();
404#ifdef USE_OTA_STATE_CALLBACK
405 this->state_callback_.call(ota::OTA_COMPLETED, 100.0f, 0);
406#endif
407 delay(100); // NOLINT
409
410error:
411 buf[0] = static_cast<uint8_t>(error_code);
412 this->writeall_(buf, 1);
413 this->cleanup_connection_();
414
415 if (backend != nullptr && update_started) {
416 backend->abort();
417 }
418
419 this->status_momentary_error("onerror", 5000);
420#ifdef USE_OTA_STATE_CALLBACK
421 this->state_callback_.call(ota::OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
422#endif
423}
424
425bool ESPHomeOTAComponent::readall_(uint8_t *buf, size_t len) {
426 uint32_t start = millis();
427 uint32_t at = 0;
428 while (len - at > 0) {
429 uint32_t now = millis();
430 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
431 ESP_LOGW(TAG, "Timeout reading %d bytes", len);
432 return false;
433 }
434
435 ssize_t read = this->client_->read(buf + at, len - at);
436 if (read == -1) {
437 if (errno != EAGAIN && errno != EWOULDBLOCK) {
438 ESP_LOGW(TAG, "Error reading %d bytes, errno %d", len, errno);
439 return false;
440 }
441 } else if (read == 0) {
442 ESP_LOGW(TAG, "Remote closed connection");
443 return false;
444 } else {
445 at += read;
446 }
448 }
449
450 return true;
451}
452bool ESPHomeOTAComponent::writeall_(const uint8_t *buf, size_t len) {
453 uint32_t start = millis();
454 uint32_t at = 0;
455 while (len - at > 0) {
456 uint32_t now = millis();
457 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
458 ESP_LOGW(TAG, "Timeout writing %d bytes", len);
459 return false;
460 }
461
462 ssize_t written = this->client_->write(buf + at, len - at);
463 if (written == -1) {
464 if (errno != EAGAIN && errno != EWOULDBLOCK) {
465 ESP_LOGW(TAG, "Error writing %d bytes, errno %d", len, errno);
466 return false;
467 }
468 } else {
469 at += written;
470 }
472 }
473 return true;
474}
475
477uint16_t ESPHomeOTAComponent::get_port() const { return this->port_; }
478void ESPHomeOTAComponent::set_port(uint16_t port) { this->port_ = port; }
479
480void ESPHomeOTAComponent::log_socket_error_(const char *msg) { ESP_LOGW(TAG, "Socket %s: errno %d", msg, errno); }
481
482void ESPHomeOTAComponent::log_read_error_(const char *what) { ESP_LOGW(TAG, "Read %s failed", what); }
483
484void ESPHomeOTAComponent::log_start_(const char *phase) {
485 ESP_LOGD(TAG, "Starting %s from %s", phase, this->client_->getpeername().c_str());
486}
487
489 this->client_->close();
490 this->client_ = nullptr;
491 this->client_connect_time_ = 0;
492 this->magic_buf_pos_ = 0;
493}
494
499
500} // namespace esphome
501#endif
void feed_wdt(uint32_t time=0)
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
virtual void mark_failed()
Mark this component as failed.
void status_set_warning(const char *message=nullptr)
void status_momentary_error(const std::string &name, uint32_t length=5000)
void status_clear_warning()
bool writeall_(const uint8_t *buf, size_t len)
void log_start_(const char *phase)
void log_socket_error_(const char *msg)
float get_setup_priority() const override
bool readall_(uint8_t *buf, size_t len)
void set_port(uint16_t port)
Manually set the port OTA should listen on.
std::unique_ptr< socket::Socket > server_
Definition ota_esphome.h:44
void log_read_error_(const char *what)
std::unique_ptr< socket::Socket > client_
Definition ota_esphome.h:45
void init()
Initialize a new MD5 digest computation.
Definition md5.cpp:11
StateCallbackManager state_callback_
Definition ota_backend.h:91
uint32_t socklen_t
Definition headers.h:97
__int64 ssize_t
Definition httplib.h:178
std::string get_use_address()
Get the active network hostname.
Definition util.cpp:88
void register_ota_platform(OTAComponent *ota_caller)
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_UPDATE_PREPARE_OK
Definition ota_backend.h:20
@ OTA_RESPONSE_SUPPORTS_COMPRESSION
Definition ota_backend.h:24
@ OTA_RESPONSE_BIN_MD5_OK
Definition ota_backend.h:21
@ OTA_RESPONSE_UPDATE_END_OK
Definition ota_backend.h:23
@ OTA_RESPONSE_RECEIVE_OK
Definition ota_backend.h:22
@ OTA_RESPONSE_CHUNK_OK
Definition ota_backend.h:25
@ OTA_RESPONSE_ERROR_AUTH_INVALID
Definition ota_backend.h:29
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:40
@ OTA_RESPONSE_ERROR_MAGIC
Definition ota_backend.h:27
@ OTA_RESPONSE_HEADER_OK
Definition ota_backend.h:18
@ OTA_RESPONSE_REQUEST_AUTH
Definition ota_backend.h:16
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.cpp:57
std::unique_ptr< Socket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.cpp:44
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
Definition socket.cpp:82
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:279
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:17
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28
Application App
Global storage of Application pointer - only one Application can exist.