ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ota_backend_arduino_rp2.cpp
Go to the documentation of this file.
1#ifdef USE_ARDUINO
2#ifdef USE_RP2
4#include "ota_backend.h"
5
8#include "esphome/core/log.h"
9
10#include <Updater.h>
11
12// LittleFS region bounds, provided by the arduino-pico linker script.
13// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
14extern "C" uint8_t _FS_start;
15// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
16extern "C" uint8_t _FS_end;
17
18namespace esphome::ota {
19
20ESPHOME_LOG_TAG(TAG, "ota");
21
22std::unique_ptr<ArduinoRP2OTABackend> make_ota_backend() { return make_unique<ArduinoRP2OTABackend>(); }
23
25 if (!this->lock_.try_acquire())
27 if (ota_type != OTA_TYPE_UPDATE_APP) {
29 }
30 // 0 means the size is unknown (web_server upload). The updater only checks the size
31 // against the LittleFS region it stages the image in, so ask for all of it.
32 if (image_size == 0) {
33 image_size = reinterpret_cast<uintptr_t>(&_FS_end) - reinterpret_cast<uintptr_t>(&_FS_start);
34 }
35 bool ret = Update.begin(image_size, U_FLASH);
36 if (ret) {
38 return OTA_RESPONSE_OK;
39 }
40
41 uint8_t error = Update.getError();
42 if (error == UPDATE_ERROR_BOOTSTRAP)
44 if (error == UPDATE_ERROR_NEW_FLASH_CONFIG)
46 if (error == UPDATE_ERROR_FLASH_CONFIG)
48 if (error == UPDATE_ERROR_SPACE || error == UPDATE_ERROR_SIZE)
50
51 ESP_LOGE(TAG, "Begin error: %d", error);
52
54}
55
57 Update.setMD5(md5);
58 this->md5_set_ = true;
59}
60
62 size_t written = Update.write(data, len);
63 if (written == len) {
64 return OTA_RESPONSE_OK;
65 }
66
67 uint8_t error = Update.getError();
68 ESP_LOGE(TAG, "Write error: %d", error);
69
71}
72
74 // Use strict validation (false) when MD5 is set, lenient validation (true) when no MD5
75 // This matches the behavior of the old web_server OTA implementation
76 if (Update.end(!this->md5_set_)) {
77 this->lock_.hold_until_reboot();
78 return OTA_RESPONSE_OK;
79 }
80
81 uint8_t error = Update.getError();
82 ESP_LOGE(TAG, "End error: %d", error);
83
85}
86
88 if (!this->lock_.is_held()) // refused in begin()
89 return;
90 Update.end();
92}
93
94} // namespace esphome::ota
95#endif // USE_RP2
96#endif // USE_ARDUINO
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_WRONG_NEW_FLASH_CONFIG
Definition ota_backend.h:40
@ OTA_RESPONSE_ERROR_WRONG_CURRENT_FLASH_CONFIG
Definition ota_backend.h:39
@ 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_RP2040_NOT_ENOUGH_SPACE
Definition ota_backend.h:45
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:55
@ OTA_RESPONSE_ERROR_INVALID_BOOTSTRAPPING
Definition ota_backend.h:38
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
void preferences_prevent_write(bool prevent)
const void size_t len
Definition hal.h:68
int written
Definition helpers.h:1340
uint8_t _FS_end
uint8_t _FS_start