ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ota_backend_arduino_libretiny.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
3#include "ota_backend.h"
4
7#include "esphome/core/log.h"
8
9#include <algorithm>
10
11#include <Update.h>
12
13namespace esphome::ota {
14
15ESPHOME_LOG_TAG(TAG, "ota");
16
17std::unique_ptr<ArduinoLibreTinyOTABackend> make_ota_backend() { return make_unique<ArduinoLibreTinyOTABackend>(); }
18
20 if (!this->lock_.try_acquire())
22 if (ota_type != OTA_TYPE_UPDATE_APP) {
24 }
25 // Handle UPDATE_SIZE_UNKNOWN (0) which is used by web server OTA
26 // where the exact firmware size is unknown due to multipart encoding
27 if (image_size == 0) {
28 image_size = UPDATE_SIZE_UNKNOWN;
29 }
30 bool ret = Update.begin(image_size, U_FLASH);
31 if (ret) {
32 this->md5_.init();
33 return OTA_RESPONSE_OK;
34 }
35
36 uint8_t error = Update.getError();
37 if (error == UPDATE_ERROR_SIZE)
39
40 ESP_LOGE(TAG, "Begin error: %d", error);
41
43}
44
46 // Checked in end(); a checksum that does not parse refuses the update
47 this->md5_set_ = true;
48 this->md5_parsed_ = parse_hex(md5, this->expected_md5_, 16);
49}
50
51bool ArduinoLibreTinyOTABackend::feed_(const uint8_t *data, size_t len) {
52 while (len != 0) {
53 const size_t chunk = std::min(len, MAX_FEED);
54 if (Update.write(data, chunk) != chunk) {
55 ESP_LOGE(TAG, "Write error: %d", Update.getError());
56 return false;
57 }
58 this->wrote_ = true;
59 data += chunk;
60 len -= chunk;
61 }
62 return true;
63}
64
66 if (len == 0)
67 return OTA_RESPONSE_OK;
68 this->md5_.add(data, len);
69 // Keep the last byte back so the image stays incomplete until end()
70 if ((this->has_pending_ && !this->feed_(&this->pending_, 1)) || !this->feed_(data, len - 1))
72 this->pending_ = data[len - 1];
73 this->has_pending_ = true;
74 return OTA_RESPONSE_OK;
75}
76
78 if (this->md5_set_) {
79 this->md5_.calculate();
80 if (!this->md5_parsed_ || !this->md5_.equals_bytes(this->expected_md5_)) {
81 ESP_LOGE(TAG, "MD5 mismatch");
83 }
84 }
85 if (this->has_pending_ && !this->feed_(&this->pending_, 1))
87 // Use strict validation (false) when MD5 is set, lenient validation (true) when no MD5
88 // This matches the behavior of the old web_server OTA implementation
89 if (Update.end(!this->md5_set_)) {
90 this->lock_.hold_until_reboot();
91 return OTA_RESPONSE_OK;
92 }
93
94 uint8_t error = Update.getError();
95#ifdef USE_BK72XX
96 // The image was written to its own layout's download partition, not this build's
97 if (error == UPDATE_ERROR_ACTIVATE) {
98 ESP_LOGE(TAG, "End error: %d, image is for another flash layout; check that board: matches this module", error);
100 }
101#endif
102 ESP_LOGE(TAG, "End error: %d", error);
103
105}
106
108 if (!this->lock_.is_held()) // refused in begin(), or end() succeeded
109 return;
110 Update.abort();
111 // Bootloaders take a written image LibreTiny never marked: RTL8720C boots the
112 // valid one with the higher serial number, BK72xx and LN882H install a
113 // complete download. On LN882H the device image ends before the flasher
114 // blocks of a full UF2, so the held-back byte does not keep it incomplete.
115 if (!this->wrote_)
116 return;
117#ifdef USE_LN882X
118 // lt_ota_switch() ignores revert here; erasing the header voids the download
119 const bool reverted = lt_flash_erase_block(FLASH_OTA_OFFSET);
120#else
121 // On BK72xx this erases the download header
122 const bool reverted = lt_ota_switch(/* revert= */ true);
123#endif
124 if (!reverted) {
125 ESP_LOGE(TAG, "Revert failed");
126 }
127}
128
129} // namespace esphome::ota
130#endif // USE_LIBRETINY
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
OTAResponseTypes begin(size_t image_size, OTAType ota_type=OTA_TYPE_UPDATE_APP)
OTAResponseTypes write(uint8_t *data, size_t len)
void hold_until_reboot()
Keep the lock set until reboot; is_held() becomes false.
bool try_acquire()
Returns false if held elsewhere.
int ret
ESPHOME_LOG_TAG(TAG, "ota")
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
Definition ota_backend.h:44
@ OTA_RESPONSE_ERROR_ESP32_NOT_ENOUGH_SPACE
Definition ota_backend.h:42
@ OTA_RESPONSE_ERROR_WRITING_FLASH
Definition ota_backend.h:36
@ OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE
Definition ota_backend.h:47
@ OTA_RESPONSE_ERROR_UPDATE_END
Definition ota_backend.h:37
@ OTA_RESPONSE_ERROR_UPDATE_BUSY
Definition ota_backend.h:54
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:55
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
const void size_t len
Definition hal.h:68
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:370