ESPHome 2025.12.0-dev
Loading...
Searching...
No Matches
esp32_hosted_update.cpp
Go to the documentation of this file.
1#if defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32P4)
6#include "esphome/core/log.h"
7#include <esp_image_format.h>
8#include <esp_app_desc.h>
9#include <esp_hosted.h>
10
11extern "C" {
12#include <esp_hosted_ota.h>
13}
14
16
17static const char *const TAG = "esp32_hosted.update";
18
19// older coprocessor firmware versions have a 1500-byte limit per RPC call
20constexpr size_t CHUNK_SIZE = 1500;
21
23 this->update_info_.title = "ESP32 Hosted Coprocessor";
24
25 // get coprocessor version
26 esp_hosted_coprocessor_fwver_t ver_info;
27 if (esp_hosted_get_coprocessor_fwversion(&ver_info) == ESP_OK) {
28 this->update_info_.current_version = str_sprintf("%d.%d.%d", ver_info.major1, ver_info.minor1, ver_info.patch1);
29 } else {
30 this->update_info_.current_version = "unknown";
31 }
32 ESP_LOGD(TAG, "Coprocessor version: %s", this->update_info_.current_version.c_str());
33
34 // get image version
35 const int app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
36 if (this->firmware_size_ >= app_desc_offset + sizeof(esp_app_desc_t)) {
37 esp_app_desc_t *app_desc = (esp_app_desc_t *) (this->firmware_data_ + app_desc_offset);
38 if (app_desc->magic_word == ESP_APP_DESC_MAGIC_WORD) {
39 ESP_LOGD(TAG, "Firmware version: %s", app_desc->version);
40 ESP_LOGD(TAG, "Project name: %s", app_desc->project_name);
41 ESP_LOGD(TAG, "Build date: %s", app_desc->date);
42 ESP_LOGD(TAG, "Build time: %s", app_desc->time);
43 ESP_LOGD(TAG, "IDF version: %s", app_desc->idf_ver);
44 this->update_info_.latest_version = app_desc->version;
45 if (this->update_info_.latest_version != this->update_info_.current_version) {
47 } else {
49 }
50 } else {
51 ESP_LOGW(TAG, "Invalid app description magic word: 0x%08x (expected 0x%08x)", app_desc->magic_word,
52 ESP_APP_DESC_MAGIC_WORD);
54 }
55 } else {
56 ESP_LOGW(TAG, "Firmware too small to contain app description");
58 }
59
60 // publish state
61 this->status_clear_error();
62 this->publish_state();
63}
64
66 ESP_LOGCONFIG(TAG,
67 "ESP32 Hosted Update:\n"
68 " Current Version: %s\n"
69 " Latest Version: %s\n"
70 " Latest Size: %zu bytes",
71 this->update_info_.current_version.c_str(), this->update_info_.latest_version.c_str(),
72 this->firmware_size_);
73}
74
76 if (this->state_ != update::UPDATE_STATE_AVAILABLE && !force) {
77 ESP_LOGW(TAG, "Update not available");
78 return;
79 }
80
81 if (this->firmware_data_ == nullptr || this->firmware_size_ == 0) {
82 ESP_LOGE(TAG, "No firmware data available");
83 return;
84 }
85
86 sha256::SHA256 hasher;
87 hasher.init();
88 hasher.add(this->firmware_data_, this->firmware_size_);
89 hasher.calculate();
90 if (!hasher.equals_bytes(this->firmware_sha256_.data())) {
91 this->status_set_error("SHA256 verification failed");
92 this->publish_state();
93 return;
94 }
95
96 ESP_LOGI(TAG, "Starting OTA update (%zu bytes)", this->firmware_size_);
97
98 watchdog::WatchdogManager watchdog(20000);
99 update::UpdateState prev_state = this->state_;
101 this->update_info_.has_progress = false;
102 this->publish_state();
103
104 esp_err_t err = esp_hosted_slave_ota_begin(); // NOLINT
105 if (err != ESP_OK) {
106 ESP_LOGE(TAG, "Failed to begin OTA: %s", esp_err_to_name(err));
107 this->state_ = prev_state;
108 this->status_set_error("Failed to begin OTA");
109 this->publish_state();
110 return;
111 }
112
113 uint8_t chunk[CHUNK_SIZE];
114 const uint8_t *data_ptr = this->firmware_data_;
115 size_t remaining = this->firmware_size_;
116 while (remaining > 0) {
117 size_t chunk_size = std::min(remaining, static_cast<size_t>(CHUNK_SIZE));
118 memcpy(chunk, data_ptr, chunk_size);
119 err = esp_hosted_slave_ota_write(chunk, chunk_size); // NOLINT
120 if (err != ESP_OK) {
121 ESP_LOGE(TAG, "Failed to write OTA data: %s", esp_err_to_name(err));
122 esp_hosted_slave_ota_end(); // NOLINT
123 this->state_ = prev_state;
124 this->status_set_error("Failed to write OTA data");
125 this->publish_state();
126 return;
127 }
128 data_ptr += chunk_size;
129 remaining -= chunk_size;
130 App.feed_wdt();
131 }
132
133 err = esp_hosted_slave_ota_end(); // NOLINT
134 if (err != ESP_OK) {
135 ESP_LOGE(TAG, "Failed to end OTA: %s", esp_err_to_name(err));
136 this->state_ = prev_state;
137 this->status_set_error("Failed to end OTA");
138 this->publish_state();
139 return;
140 }
141
142 // activate new firmware
143 err = esp_hosted_slave_ota_activate(); // NOLINT
144 if (err != ESP_OK) {
145 ESP_LOGE(TAG, "Failed to activate OTA: %s", esp_err_to_name(err));
146 this->state_ = prev_state;
147 this->status_set_error("Failed to activate OTA");
148 this->publish_state();
149 return;
150 }
151
152 // update state
153 ESP_LOGI(TAG, "OTA update successful");
155 this->status_clear_error();
156 this->publish_state();
157
158 // schedule a restart to ensure everything is in sync
159 ESP_LOGI(TAG, "Restarting in 1 second");
160 this->set_timeout(1000, []() { App.safe_reboot(); });
161}
162
163} // namespace esphome::esp32_hosted
164#endif
void feed_wdt(uint32_t time=0)
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
void status_set_error(const char *message=nullptr)
bool equals_bytes(const uint8_t *expected)
Compare the hash against a provided byte-encoded hash.
Definition hash_base.h:38
void calculate() override
Definition sha256.cpp:55
void add(const uint8_t *data, size_t len) override
Definition sha256.cpp:53
void init() override
Definition sha256.cpp:48
std::string str_sprintf(const char *fmt,...)
Definition helpers.cpp:222
Application App
Global storage of Application pointer - only one Application can exist.