192 lines
6.4 KiB
C++
192 lines
6.4 KiB
C++
#include "ota_mgr.h"
|
|
|
|
#include <esp_http_client.h>
|
|
#include <esp_ota_ops.h>
|
|
#include <esp_partition.h>
|
|
#include <mbedtls/sha256.h>
|
|
#include <strings.h> // strcasecmp
|
|
|
|
#include "app_config.h"
|
|
#include "power_mgr.h"
|
|
|
|
static bool s_pending_verify = false;
|
|
static OtaBatteryOkFn s_battery_fn = nullptr;
|
|
static uint8_t s_battery_min = 50;
|
|
|
|
void otaBegin() {
|
|
const esp_partition_t* running = esp_ota_get_running_partition();
|
|
esp_ota_img_states_t state;
|
|
|
|
if (esp_ota_get_state_partition(running, &state) == ESP_OK) {
|
|
s_pending_verify = (state == ESP_OTA_IMG_PENDING_VERIFY);
|
|
}
|
|
|
|
log_i("ota: running %s from %s%s", FW_VERSION, running->label,
|
|
s_pending_verify ? " [TRIAL BOOT - unconfirmed]" : "");
|
|
}
|
|
|
|
bool otaPendingVerify() { return s_pending_verify; }
|
|
|
|
void otaMarkValid() {
|
|
if (!s_pending_verify) return;
|
|
if (esp_ota_mark_app_valid_cancel_rollback() == ESP_OK) {
|
|
s_pending_verify = false;
|
|
log_i("ota: trial image confirmed good, rollback cancelled");
|
|
} else {
|
|
log_e("ota: failed to confirm trial image");
|
|
}
|
|
}
|
|
|
|
void otaRollback(const char* reason) {
|
|
log_e("ota: rolling back — %s", reason);
|
|
delay(200);
|
|
esp_ota_mark_app_invalid_rollback_and_reboot();
|
|
}
|
|
|
|
bool otaIsNewer(const OtaTarget& t) {
|
|
// Deliberately a string inequality, not a semver comparison: the endpoint
|
|
// decides which version a device should run (SPEC §3 `firmware.is_target`),
|
|
// so "different from target" is the correct trigger. That also makes
|
|
// deliberate downgrades work, which semver ordering would block.
|
|
return t.version[0] && strcmp(t.version, FW_VERSION) != 0;
|
|
}
|
|
|
|
void otaSetBatteryCheck(OtaBatteryOkFn fn, uint8_t min_pct) {
|
|
s_battery_fn = fn;
|
|
s_battery_min = min_pct;
|
|
}
|
|
|
|
static bool hexEq(const uint8_t* digest, const char* expect_hex) {
|
|
char hex[65];
|
|
for (int i = 0; i < 32; i++) snprintf(hex + i * 2, 3, "%02x", digest[i]);
|
|
hex[64] = '\0';
|
|
return strcasecmp(hex, expect_hex) == 0;
|
|
}
|
|
|
|
bool otaApply(const OtaTarget& t) {
|
|
if (!g_cfg.ota_enabled) {
|
|
log_w("ota: disabled in config, ignoring target %s", t.version);
|
|
return false;
|
|
}
|
|
|
|
// SPEC C6: a brownout mid-write bricks the device.
|
|
if (s_battery_fn && !s_battery_fn(s_battery_min)) {
|
|
log_w("ota: refused, battery below %u%%", s_battery_min);
|
|
return false;
|
|
}
|
|
|
|
if (strlen(t.sha256) != 64) {
|
|
log_e("ota: refused, target has no usable SHA-256");
|
|
return false;
|
|
}
|
|
|
|
// Never sleep while flashing.
|
|
SleepGuard guard(INHIBIT_OTA);
|
|
|
|
String url = (strncmp(t.url, "http", 4) == 0) ? String(t.url) : (apiBaseUrl() + t.url);
|
|
log_i("ota: %s -> %s (%u bytes) from %s", FW_VERSION, t.version, (unsigned)t.size, url.c_str());
|
|
|
|
esp_http_client_config_t cfg = {};
|
|
cfg.url = url.c_str();
|
|
cfg.method = HTTP_METHOD_GET;
|
|
cfg.timeout_ms = 30000;
|
|
|
|
esp_http_client_handle_t c = esp_http_client_init(&cfg);
|
|
char bearer[80];
|
|
snprintf(bearer, sizeof(bearer), "Bearer %s", g_cfg.device_token);
|
|
esp_http_client_set_header(c, "Authorization", bearer);
|
|
|
|
bool ok = false;
|
|
esp_ota_handle_t ota = 0;
|
|
const esp_partition_t* target_part = nullptr;
|
|
mbedtls_sha256_context sha;
|
|
mbedtls_sha256_init(&sha);
|
|
|
|
do {
|
|
if (esp_http_client_open(c, 0) != ESP_OK) { log_e("ota: connect failed"); break; }
|
|
|
|
int content_len = esp_http_client_fetch_headers(c);
|
|
int status = esp_http_client_get_status_code(c);
|
|
if (status != 200) { log_e("ota: HTTP %d", status); break; }
|
|
if (content_len <= 0) {
|
|
// Chunked responses are legal but we want the length to sanity
|
|
// check against the manifest before erasing a partition.
|
|
log_e("ota: server did not send Content-Length");
|
|
break;
|
|
}
|
|
if (t.size && (uint32_t)content_len != t.size) {
|
|
log_e("ota: size mismatch, manifest %u vs body %d", (unsigned)t.size, content_len);
|
|
break;
|
|
}
|
|
|
|
target_part = esp_ota_get_next_update_partition(nullptr);
|
|
if (!target_part) { log_e("ota: no free OTA slot"); break; }
|
|
if ((uint32_t)content_len > target_part->size) {
|
|
log_e("ota: image %d does not fit in %s (%u)", content_len,
|
|
target_part->label, (unsigned)target_part->size);
|
|
break;
|
|
}
|
|
|
|
log_i("ota: erasing %s", target_part->label);
|
|
if (esp_ota_begin(target_part, content_len, &ota) != ESP_OK) {
|
|
log_e("ota: esp_ota_begin failed");
|
|
ota = 0;
|
|
break;
|
|
}
|
|
|
|
mbedtls_sha256_starts(&sha, 0);
|
|
|
|
static uint8_t buf[4096];
|
|
int written = 0, last_pct = -10;
|
|
bool io_error = false;
|
|
|
|
while (written < content_len) {
|
|
int n = esp_http_client_read(c, (char*)buf, sizeof(buf));
|
|
if (n <= 0) { log_e("ota: download truncated at %d/%d", written, content_len); io_error = true; break; }
|
|
|
|
if (esp_ota_write(ota, buf, n) != ESP_OK) { log_e("ota: flash write failed"); io_error = true; break; }
|
|
mbedtls_sha256_update(&sha, buf, n);
|
|
written += n;
|
|
|
|
int pct = (int)((int64_t)written * 100 / content_len);
|
|
if (pct >= last_pct + 10) { log_i("ota: %d%%", pct); last_pct = pct; }
|
|
}
|
|
if (io_error) break;
|
|
|
|
uint8_t digest[32];
|
|
mbedtls_sha256_finish(&sha, digest);
|
|
if (!hexEq(digest, t.sha256)) {
|
|
log_e("ota: SHA-256 mismatch, discarding image");
|
|
break;
|
|
}
|
|
log_i("ota: SHA-256 verified");
|
|
|
|
if (esp_ota_end(ota) != ESP_OK) { log_e("ota: esp_ota_end failed"); ota = 0; break; }
|
|
ota = 0;
|
|
|
|
if (esp_ota_set_boot_partition(target_part) != ESP_OK) {
|
|
log_e("ota: set_boot_partition failed");
|
|
break;
|
|
}
|
|
ok = true;
|
|
} while (false);
|
|
|
|
if (ota) esp_ota_abort(ota);
|
|
mbedtls_sha256_free(&sha);
|
|
esp_http_client_close(c);
|
|
esp_http_client_cleanup(c);
|
|
|
|
if (ok) {
|
|
// The new image boots in PENDING_VERIFY. If it cannot reach the
|
|
// endpoint it never calls otaMarkValid(), and the next reset returns
|
|
// to this slot automatically.
|
|
log_i("ota: activated %s, restarting into trial boot", target_part->label);
|
|
delay(300);
|
|
esp_restart();
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
const char* otaRunningVersion() { return FW_VERSION; }
|
|
const char* otaRunningPartition() { return esp_ota_get_running_partition()->label; }
|