29static const char *
const TAG =
"esphome.ota";
30static constexpr uint16_t OTA_BLOCK_SIZE = 8192;
31static constexpr size_t OTA_BUFFER_SIZE = 1024;
32static constexpr uint32_t OTA_SOCKET_TIMEOUT_HANDSHAKE = 20000;
33static constexpr uint32_t OTA_SOCKET_TIMEOUT_DATA = 90000;
35#ifdef USE_OTA_PASSWORD
37static constexpr size_t MD5_HEX_SIZE = 32;
40static constexpr size_t SHA256_HEX_SIZE = 64;
45#ifdef USE_OTA_STATE_CALLBACK
56 int err = this->
server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
61 err = this->
server_->setblocking(
false);
94 "Over-The-Air updates:\n"
98#ifdef USE_OTA_PASSWORD
100 ESP_LOGCONFIG(TAG,
" Password configured");
115static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01;
117static const uint8_t FEATURE_SUPPORTS_SHA256_AUTH = 0x02;
124#define ALLOW_OTA_DOWNGRADE_MD5
133 if (this->
client_ ==
nullptr) {
142 int err = this->
client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable,
sizeof(
int));
148 err = this->
client_->setblocking(
false);
163 ESP_LOGW(TAG,
"Handshake timeout");
171 if (!this->
try_read_(5, LOG_STR(
"read magic"))) {
176 static const uint8_t MAGIC_BYTES[5] = {0x6C, 0x26, 0xF7, 0x5C, 0x45};
178 ESP_LOGW(TAG,
"Magic bytes mismatch! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", this->
handshake_buf_[0],
193 if (!this->
try_write_(2, LOG_STR(
"ack magic"))) {
204 if (!this->
try_read_(1, LOG_STR(
"read feature"))) {
211 ((this->
ota_features_ & FEATURE_SUPPORTS_COMPRESSION) != 0 && this->
backend_->supports_compression())
219 if (!this->
try_write_(1, LOG_STR(
"ack feature"))) {
222#ifdef USE_OTA_PASSWORD
235#ifdef USE_OTA_PASSWORD
273 bool update_started =
false;
275 uint32_t last_progress = 0;
276 uint8_t buf[OTA_BUFFER_SIZE];
277 char *sbuf =
reinterpret_cast<char *
>(buf);
279#if USE_OTA_VERSION == 2
280 size_t size_acknowledged = 0;
291 ota_size = (
static_cast<size_t>(buf[0]) << 24) | (
static_cast<size_t>(buf[1]) << 16) |
292 (
static_cast<size_t>(buf[2]) << 8) | buf[3];
293 ESP_LOGV(TAG,
"Size is %u bytes", ota_size);
301#ifdef USE_OTA_STATE_CALLBACK
306 error_code = this->
backend_->begin(ota_size);
309 update_started =
true;
320 ESP_LOGV(TAG,
"Update: Binary MD5 is %s", sbuf);
321 this->
backend_->set_update_md5(sbuf);
326 while (total < ota_size) {
328 size_t remaining = ota_size - total;
329 size_t requested = remaining < OTA_BUFFER_SIZE ? remaining : OTA_BUFFER_SIZE;
336 ESP_LOGW(TAG,
"Read err %d", errno);
338 }
else if (read == 0) {
339 ESP_LOGW(TAG,
"Remote closed");
343 error_code = this->
backend_->write(buf, read);
345 ESP_LOGW(TAG,
"Flash write err %d", error_code);
349#if USE_OTA_VERSION == 2
350 while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) {
352 size_acknowledged += OTA_BLOCK_SIZE;
357 if (now - last_progress > 1000) {
359 float percentage = (total * 100.0f) / ota_size;
360 ESP_LOGD(TAG,
"Progress: %0.1f%%", percentage);
361#ifdef USE_OTA_STATE_CALLBACK
374 ESP_LOGW(TAG,
"End update err %d", error_code);
389 ESP_LOGI(TAG,
"Update complete");
391#ifdef USE_OTA_STATE_CALLBACK
398 this->
write_byte_(
static_cast<uint8_t
>(error_code));
401 if (this->
backend_ !=
nullptr && update_started) {
406#ifdef USE_OTA_STATE_CALLBACK
412 uint32_t start =
millis();
414 while (
len - at > 0) {
416 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
417 ESP_LOGW(TAG,
"Timeout reading %d bytes",
len);
424 ESP_LOGW(TAG,
"Read err %d bytes, errno %d",
len, errno);
427 }
else if (read == 0) {
428 ESP_LOGW(TAG,
"Remote closed");
439 uint32_t start =
millis();
441 while (
len - at > 0) {
443 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
444 ESP_LOGW(TAG,
"Timeout writing %d bytes",
len);
451 ESP_LOGW(TAG,
"Write err %d bytes, errno %d",
len, errno);
467 ESP_LOGW(TAG,
"Socket %s: errno %d", LOG_STR_ARG(msg), errno);
473 ESP_LOGD(TAG,
"Starting %s from %s", LOG_STR_ARG(phase), this->
client_->getpeername().c_str());
477 ESP_LOGW(TAG,
"Remote closed at %s", LOG_STR_ARG(during));
514 this->handshake_buf_pos_ += read;
516 return this->handshake_buf_pos_ >= to_read;
528 this->handshake_buf_pos_ += written;
530 return this->handshake_buf_pos_ >= to_write;
541#ifdef USE_OTA_PASSWORD
551#ifdef USE_OTA_PASSWORD
556 bool client_supports_sha256 = (this->
ota_features_ & FEATURE_SUPPORTS_SHA256_AUTH) != 0;
558#ifdef ALLOW_OTA_DOWNGRADE_MD5
560 if (client_supports_sha256) {
576 if (!client_supports_sha256) {
634 hasher = &sha_hasher;
639 hasher = &md5_hasher;
643 const size_t hex_size = hasher->
get_size() * 2;
644 const size_t nonce_len = hasher->
get_size() / 4;
645 const size_t auth_buf_size = 1 + 3 * hex_size;
646 this->
auth_buf_ = std::make_unique<uint8_t[]>(auth_buf_size);
649 char *buf =
reinterpret_cast<char *
>(this->
auth_buf_.get() + 1);
650 if (!
random_bytes(
reinterpret_cast<uint8_t *
>(buf), nonce_len)) {
657 hasher->
add(buf, nonce_len);
662#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
664 memcpy(log_buf, buf, hex_size);
665 log_buf[hex_size] =
'\0';
666 ESP_LOGV(TAG,
"Auth: Nonce is %s", log_buf);
672 const size_t to_write = 1 + hex_size;
680 this->auth_buf_pos_ += written;
683 if (this->auth_buf_pos_ < to_write) {
688 this->auth_buf_pos_ = 0;
694 const size_t to_read = hex_size * 2;
698 size_t cnonce_offset = 1 + hex_size;
706 this->auth_buf_pos_ += read;
709 if (this->auth_buf_pos_ < to_read) {
714 const char *nonce =
reinterpret_cast<char *
>(this->
auth_buf_.get() + 1);
715 const char *cnonce = nonce + hex_size;
716 const char *response = cnonce + hex_size;
733 hasher = &sha_hasher;
738 hasher = &md5_hasher;
743 hasher->
add(this->
password_.c_str(), this->password_.length());
744 hasher->
add(nonce, hex_size * 2);
747#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
750 memcpy(log_buf, cnonce, hex_size);
751 log_buf[hex_size] =
'\0';
752 ESP_LOGV(TAG,
"Auth: CNonce is %s", log_buf);
756 log_buf[hex_size] =
'\0';
757 ESP_LOGV(TAG,
"Auth: Result is %s", log_buf);
760 memcpy(log_buf, response, hex_size);
761 log_buf[hex_size] =
'\0';
762 ESP_LOGV(TAG,
"Auth: Response is %s", log_buf);
783 return SHA256_HEX_SIZE;
789#ifndef USE_OTA_SHA256
790#error "Either USE_OTA_MD5 or USE_OTA_SHA256 must be defined when USE_OTA_PASSWORD is enabled"
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 would_block_(int error_code) const
std::unique_ptr< ota::OTABackend > backend_
uint8_t handshake_buf_pos_
uint16_t get_port() const
void yield_and_feed_watchdog_()
bool writeall_(const uint8_t *buf, size_t len)
bool try_read_(size_t to_read, const LogString *desc)
bool try_write_(size_t to_write, const LogString *desc)
void dump_config() override
std::unique_ptr< uint8_t[]> auth_buf_
bool handle_write_error_(ssize_t written, const LogString *desc)
uint8_t handshake_buf_[5]
void log_auth_warning_(const LogString *msg)
uint32_t client_connect_time_
float get_setup_priority() const override
void send_error_and_cleanup_(ota::OTAResponseTypes error)
bool handle_read_error_(ssize_t read, const LogString *desc)
void log_read_error_(const LogString *what)
bool readall_(uint8_t *buf, size_t len)
void set_port(uint16_t port)
Manually set the port OTA should listen on.
bool write_byte_(uint8_t byte)
size_t get_auth_hex_size_() const
std::unique_ptr< socket::Socket > server_
void transition_ota_state_(OTAState next_state)
void cleanup_connection_()
void log_remote_closed_(const LogString *during)
std::unique_ptr< socket::Socket > client_
void log_start_(const LogString *phase)
void log_socket_error_(const LogString *msg)
Base class for hash algorithms.
void get_hex(char *output)
Retrieve the hash as hex characters.
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash.
virtual void calculate()=0
Compute the hash based on provided data.
virtual void init()=0
Initialize a new hash computation.
virtual size_t get_size() const =0
Get the size of the hash in bytes (16 for MD5, 32 for SHA256)
virtual void add(const uint8_t *data, size_t len)=0
Add bytes of data for the hash.
StateCallbackManager state_callback_
const char * get_use_address()
Get the active network hostname.
void register_ota_platform(OTAComponent *ota_caller)
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_UPDATE_PREPARE_OK
@ OTA_RESPONSE_SUPPORTS_COMPRESSION
@ OTA_RESPONSE_BIN_MD5_OK
@ OTA_RESPONSE_UPDATE_END_OK
@ OTA_RESPONSE_RECEIVE_OK
@ OTA_RESPONSE_ERROR_AUTH_INVALID
@ OTA_RESPONSE_ERROR_UNKNOWN
@ OTA_RESPONSE_REQUEST_SHA256_AUTH
@ OTA_RESPONSE_ERROR_MAGIC
@ OTA_RESPONSE_REQUEST_AUTH
const float AFTER_WIFI
For components that should be initialized after WiFi is connected.
std::unique_ptr< Socket > socket_ip_loop_monitored(int type, int protocol)
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().
Providing packet encoding functions for exchanging data with a remote host.
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.