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";
26 esp_hosted_coprocessor_fwver_t ver_info;
27 if (esp_hosted_get_coprocessor_fwversion(&ver_info) == ESP_OK) {
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);
51 ESP_LOGW(TAG,
"Invalid app description magic word: 0x%08x (expected 0x%08x)", app_desc->magic_word,
52 ESP_APP_DESC_MAGIC_WORD);
56 ESP_LOGW(TAG,
"Firmware too small to contain app description");
67 "ESP32 Hosted Update:\n"
68 " Current Version: %s\n"
69 " Latest Version: %s\n"
70 " Latest Size: %zu bytes",
72 this->firmware_size_);
77 ESP_LOGW(TAG,
"Update not available");
82 ESP_LOGE(TAG,
"No firmware data available");
90 if (!hasher.
equals_bytes(this->firmware_sha256_.data())) {
96 ESP_LOGI(TAG,
"Starting OTA update (%zu bytes)", this->
firmware_size_);
104 esp_err_t err = esp_hosted_slave_ota_begin();
106 ESP_LOGE(TAG,
"Failed to begin OTA: %s", esp_err_to_name(err));
107 this->
state_ = prev_state;
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);
121 ESP_LOGE(TAG,
"Failed to write OTA data: %s", esp_err_to_name(err));
122 esp_hosted_slave_ota_end();
123 this->
state_ = prev_state;
128 data_ptr += chunk_size;
129 remaining -= chunk_size;
133 err = esp_hosted_slave_ota_end();
135 ESP_LOGE(TAG,
"Failed to end OTA: %s", esp_err_to_name(err));
136 this->
state_ = prev_state;
143 err = esp_hosted_slave_ota_activate();
145 ESP_LOGE(TAG,
"Failed to activate OTA: %s", esp_err_to_name(err));
146 this->
state_ = prev_state;
153 ESP_LOGI(TAG,
"OTA update successful");
159 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.
void status_set_error(const char *message=nullptr)
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