#include "ota.h" #include "web.h" #include #include #include "esp_log.h" #include "esp_ota_ops.h" #include "esp_app_format.h" #include "esp_system.h" #include #include "esp_timer.h" static const char *TAG = "ota"; #define CHUNK 4096 #define CONFIRM_AFTER_US (10 * 1000000LL) static bool s_busy; static void reboot_cb(void *arg) { esp_restart(); } static void confirm_cb(void *arg) { esp_err_t err = esp_ota_mark_app_valid_cancel_rollback(); if (err == ESP_OK) { ESP_LOGI(TAG, "running image confirmed"); } } void ota_confirm_later(void) { esp_ota_img_states_t state; const esp_partition_t *run = esp_ota_get_running_partition(); if (esp_ota_get_state_partition(run, &state) != ESP_OK || state != ESP_OTA_IMG_PENDING_VERIFY) { return; } static esp_timer_handle_t t; const esp_timer_create_args_t args = { .callback = confirm_cb, .name = "ota_confirm" }; if (esp_timer_create(&args, &t) == ESP_OK) { esp_timer_start_once(t, CONFIRM_AFTER_US); } } static esp_err_t fail(httpd_req_t *req, const char *status, const char *msg) { ESP_LOGE(TAG, "%s", msg); httpd_resp_set_status(req, status); httpd_resp_set_type(req, "application/json"); web_allow_lan_origin(req); char buf[160]; snprintf(buf, sizeof(buf), "{\"error\":\"%s\"}", msg); return httpd_resp_sendstr(req, buf); } static esp_err_t ota_post_handler(httpd_req_t *req) { if (req->content_len == 0) { return fail(req, "400 Bad Request", "empty body"); } const esp_partition_t *part = esp_ota_get_next_update_partition(NULL); if (!part) { return fail(req, "500 Internal Server Error", "no update partition"); } if (req->content_len > part->size) { return fail(req, "413 Payload Too Large", "image larger than the app slot"); } if (s_busy) { return fail(req, "409 Conflict", "update already running"); } s_busy = true; char *buf = malloc(CHUNK); if (!buf) { s_busy = false; return fail(req, "500 Internal Server Error", "out of memory"); } esp_ota_handle_t handle = 0; esp_err_t err = esp_ota_begin(part, req->content_len, &handle); if (err != ESP_OK) { free(buf); s_busy = false; return fail(req, "500 Internal Server Error", "ota begin failed"); } ESP_LOGI(TAG, "receiving %u bytes into %s", (unsigned)req->content_len, part->label); size_t got = 0; const char *bad = NULL; while (got < req->content_len) { int want = req->content_len - got; int r = httpd_req_recv(req, buf, want > CHUNK ? CHUNK : want); if (r == HTTPD_SOCK_ERR_TIMEOUT) { continue; } if (r <= 0) { bad = "receive failed"; break; } if (got == 0 && (r < (int)(sizeof(esp_image_header_t)) || (uint8_t)buf[0] != ESP_IMAGE_HEADER_MAGIC)) { bad = "not an ESP32 app image"; break; } if (esp_ota_write(handle, buf, r) != ESP_OK) { bad = "flash write failed"; break; } got += r; } free(buf); if (bad) { esp_ota_abort(handle); s_busy = false; return fail(req, "400 Bad Request", bad); } // esp_ota_end checks the image checksum and hash if (esp_ota_end(handle) != ESP_OK) { s_busy = false; return fail(req, "400 Bad Request", "image failed validation"); } if (esp_ota_set_boot_partition(part) != ESP_OK) { s_busy = false; return fail(req, "500 Internal Server Error", "set boot partition failed"); } ESP_LOGI(TAG, "update ok, rebooting"); httpd_resp_set_type(req, "application/json"); web_allow_lan_origin(req); httpd_resp_sendstr(req, "{\"message\":\"update written, rebooting\"}"); static esp_timer_handle_t t; const esp_timer_create_args_t args = { .callback = reboot_cb, .name = "ota_reboot" }; if (esp_timer_create(&args, &t) == ESP_OK) { esp_timer_start_once(t, 500 * 1000); } else { esp_restart(); } return ESP_OK; } static esp_err_t ota_get_handler(httpd_req_t *req) { const esp_partition_t *run = esp_ota_get_running_partition(); const esp_app_desc_t *app = esp_app_get_description(); // The build date is only recompiled with its own file, so the ELF hash is what tells two builds apart char sha[17]; for (int i = 0; i < 8; i++) { snprintf(sha + 2 * i, 3, "%02x", app->app_elf_sha256[i]); } char buf[288]; snprintf(buf, sizeof(buf), "{\"partition\":\"%s\",\"version\":\"%s\",\"built\":\"%s %s\",\"idf\":\"%s\",\"sha\":\"%s\"}", run ? run->label : "?", app->version, app->date, app->time, app->idf_ver, sha); httpd_resp_set_type(req, "application/json"); web_allow_lan_origin(req); return httpd_resp_sendstr(req, buf); } static esp_err_t reboot_post_handler(httpd_req_t *req) { ESP_LOGI(TAG, "reboot requested"); httpd_resp_set_type(req, "application/json"); web_allow_lan_origin(req); httpd_resp_sendstr(req, "{\"message\":\"rebooting\"}"); static esp_timer_handle_t t; const esp_timer_create_args_t args = { .callback = reboot_cb, .name = "reboot" }; if (esp_timer_create(&args, &t) == ESP_OK) { esp_timer_start_once(t, 500 * 1000); } else { esp_restart(); } return ESP_OK; } // Browser preflight for a firmware upload from another device's page (octet-stream is not a simple request) static esp_err_t ota_preflight_handler(httpd_req_t *req) { web_allow_lan_origin(req); httpd_resp_set_hdr(req, "Access-Control-Allow-Methods", "POST"); httpd_resp_set_hdr(req, "Access-Control-Allow-Headers", "Content-Type"); httpd_resp_set_hdr(req, "Access-Control-Max-Age", "600"); httpd_resp_set_status(req, "204 No Content"); return httpd_resp_send(req, NULL, 0); } esp_err_t ota_register(httpd_handle_t server) { const httpd_uri_t post = { .uri = "/ota", .method = HTTP_POST, .handler = ota_post_handler }; const httpd_uri_t get = { .uri = "/ota", .method = HTTP_GET, .handler = ota_get_handler }; const httpd_uri_t preflight = { .uri = "/ota", .method = HTTP_OPTIONS, .handler = ota_preflight_handler }; esp_err_t err = httpd_register_uri_handler(server, &post); if (err == ESP_OK) { err = httpd_register_uri_handler(server, &get); } if (err == ESP_OK) { err = httpd_register_uri_handler(server, &preflight); } const httpd_uri_t reboot = { .uri = "/reboot", .method = HTTP_POST, .handler = reboot_post_handler }; if (err == ESP_OK) { err = httpd_register_uri_handler(server, &reboot); } return err; }