1#if defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32P4)
7#include <esp_image_format.h>
8#include <esp_app_desc.h>
12#include <esp_hosted_ota.h>
17static const char *
const TAG =
"esp32_hosted.update";
27 esp_hosted_connect_to_slave();
31 esp_hosted_coprocessor_fwver_t ver_info;
32 if (esp_hosted_get_coprocessor_fwversion(&ver_info) == ESP_OK) {
40 const int app_desc_offset =
sizeof(esp_image_header_t) +
sizeof(esp_image_segment_header_t);
41 if (this->
firmware_size_ >= app_desc_offset +
sizeof(esp_app_desc_t)) {
42 esp_app_desc_t *app_desc = (esp_app_desc_t *) (this->
firmware_data_ + app_desc_offset);
43 if (app_desc->magic_word == ESP_APP_DESC_MAGIC_WORD) {
44 ESP_LOGD(TAG,
"Firmware version: %s", app_desc->version);
45 ESP_LOGD(TAG,
"Project name: %s", app_desc->project_name);
46 ESP_LOGD(TAG,
"Build date: %s", app_desc->date);
47 ESP_LOGD(TAG,
"Build time: %s", app_desc->time);
48 ESP_LOGD(TAG,
"IDF version: %s", app_desc->idf_ver);
56 ESP_LOGW(TAG,
"Invalid app description magic word: 0x%08x (expected 0x%08x)", app_desc->magic_word,
57 ESP_APP_DESC_MAGIC_WORD);
61 ESP_LOGW(TAG,
"Firmware too small to contain app description");
72 "ESP32 Hosted Update:\n"
73 " Current Version: %s\n"
74 " Latest Version: %s\n"
75 " Latest Size: %zu bytes",
77 this->firmware_size_);
82 ESP_LOGW(TAG,
"Update not available");
87 ESP_LOGE(TAG,
"No firmware data available");
95 if (!hasher.
equals_bytes(this->firmware_sha256_.data())) {
101 ESP_LOGI(TAG,
"Starting OTA update (%zu bytes)", this->
firmware_size_);
109 esp_err_t err = esp_hosted_slave_ota_begin();
111 ESP_LOGE(TAG,
"Failed to begin OTA: %s", esp_err_to_name(err));
112 this->
state_ = prev_state;
121 while (remaining > 0) {
122 size_t chunk_size = std::min(remaining,
static_cast<size_t>(
CHUNK_SIZE));
123 memcpy(chunk, data_ptr, chunk_size);
124 err = esp_hosted_slave_ota_write(chunk, chunk_size);
126 ESP_LOGE(TAG,
"Failed to write OTA data: %s", esp_err_to_name(err));
127 esp_hosted_slave_ota_end();
128 this->
state_ = prev_state;
133 data_ptr += chunk_size;
134 remaining -= chunk_size;
138 err = esp_hosted_slave_ota_end();
140 ESP_LOGE(TAG,
"Failed to end OTA: %s", esp_err_to_name(err));
141 this->
state_ = prev_state;
148 err = esp_hosted_slave_ota_activate();
150 ESP_LOGE(TAG,
"Failed to activate OTA: %s", esp_err_to_name(err));
151 this->
state_ = prev_state;
158 ESP_LOGI(TAG,
"OTA update successful");
164 ESP_LOGI(TAG,
"Restarting in 1 second");
void feed_wdt(uint32_t time=0)
void status_clear_error()
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
bool equals_bytes(const uint8_t *expected)
Compare the hash against a provided byte-encoded hash.
void dump_config() override
const uint8_t * firmware_data_
void calculate() override
void add(const uint8_t *data, size_t len) override
constexpr size_t CHUNK_SIZE
@ UPDATE_STATE_INSTALLING
std::string str_sprintf(const char *fmt,...)
Application App
Global storage of Application pointer - only one Application can exist.
std::string current_version
std::string latest_version