firmware-ota (#2)

Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
2026-03-03 22:47:03 -05:00
parent 9bce74e027
commit 0ec7f7f08a
21 changed files with 1065 additions and 255 deletions

View File

@@ -0,0 +1,187 @@
// SDK
#include "cJSON.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "esp_partition.h"
#include "esp_system.h"
#include "esp_timer.h"
#include "nvs.h"
#include "nvs_flash.h"
#include <sys/param.h>
// Project
#include "appstate.hpp"
#include "types.hpp"
#define BUNDLE_SCRATCH_BUFSIZE 4096
typedef struct
{
char magic[4];
uint32_t fw_size;
uint32_t www_size;
} bundle_header_t;
internal void bundle_ota_restart_timer_callback(void *arg) { esp_restart(); }
internal esp_err_t api_ota_bundle_handler(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
if (req->content_len < sizeof(bundle_header_t))
{
ESP_LOGE("OTA_BUNDLE", "Request content too short");
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Content too short");
return ESP_FAIL;
}
char *buf = (char *)malloc(BUNDLE_SCRATCH_BUFSIZE);
if (!buf)
{
ESP_LOGE("OTA_BUNDLE", "Failed to allocate buffer");
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Out of memory");
return ESP_FAIL;
}
// 1. Read Header
bundle_header_t header;
int overhead = httpd_req_recv(req, (char *)&header, sizeof(bundle_header_t));
if (overhead <= 0)
{
free(buf);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"Header receive failed");
return ESP_FAIL;
}
if (memcmp(header.magic, "BNDL", 4) != 0)
{
free(buf);
ESP_LOGE("OTA_BUNDLE", "Invalid magic: %.4s", header.magic);
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid bundle magic");
return ESP_FAIL;
}
ESP_LOGI("OTA_BUNDLE",
"Starting Universal Update: FW %lu bytes, WWW %lu bytes",
header.fw_size, header.www_size);
// 2. Prepare Firmware Update
const esp_partition_t *fw_part = esp_ota_get_next_update_partition(NULL);
esp_ota_handle_t fw_handle = 0;
esp_err_t err = esp_ota_begin(fw_part, header.fw_size, &fw_handle);
if (err != ESP_OK)
{
free(buf);
ESP_LOGE("OTA_BUNDLE", "esp_ota_begin failed (%s)", esp_err_to_name(err));
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"OTA Begin failed");
return ESP_FAIL;
}
// 3. Stream Firmware
uint32_t fw_remaining = header.fw_size;
bool fw_first_chunk = true;
while (fw_remaining > 0)
{
int recv_len =
httpd_req_recv(req, buf, MIN(fw_remaining, BUNDLE_SCRATCH_BUFSIZE));
if (recv_len <= 0)
{
if (recv_len == HTTPD_SOCK_ERR_TIMEOUT)
continue;
esp_ota_abort(fw_handle);
free(buf);
return ESP_FAIL;
}
if (fw_first_chunk && recv_len > 0)
{
if ((uint8_t)buf[0] != 0xE9)
{
ESP_LOGE("OTA_BUNDLE", "Invalid FW magic in bundle: %02X",
(uint8_t)buf[0]);
esp_ota_abort(fw_handle);
free(buf);
httpd_resp_send_err(
req, HTTPD_400_BAD_REQUEST,
"Invalid Bundle: Firmware part is corrupted or invalid.");
return ESP_FAIL;
}
fw_first_chunk = false;
}
esp_ota_write(fw_handle, buf, recv_len);
fw_remaining -= recv_len;
}
esp_ota_end(fw_handle);
// 4. Prepare WWW Update
uint8_t target_www_slot = g_Active_WWW_Partition == 0 ? 1 : 0;
const char *www_label = target_www_slot == 0 ? "www_0" : "www_1";
const esp_partition_t *www_part = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_LITTLEFS, www_label);
esp_partition_erase_range(www_part, 0, www_part->size);
// 5. Stream WWW
uint32_t www_remaining = header.www_size;
uint32_t www_written = 0;
while (www_remaining > 0)
{
int recv_len =
httpd_req_recv(req, buf, MIN(www_remaining, BUNDLE_SCRATCH_BUFSIZE));
if (recv_len <= 0)
{
if (recv_len == HTTPD_SOCK_ERR_TIMEOUT)
continue;
free(buf);
return ESP_FAIL;
}
esp_partition_write(www_part, www_written, buf, recv_len);
www_written += recv_len;
www_remaining -= recv_len;
}
free(buf);
// 6. Commit Updates
esp_ota_set_boot_partition(fw_part);
nvs_handle_t nvs_h;
if (nvs_open("storage", NVS_READWRITE, &nvs_h) == ESP_OK)
{
nvs_set_u8(nvs_h, "www_part", target_www_slot);
nvs_commit(nvs_h);
nvs_close(nvs_h);
}
ESP_LOGI("OTA_BUNDLE", "Universal Update Complete! Rebooting...");
httpd_resp_set_type(req, "application/json");
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "status", "success");
cJSON_AddStringToObject(root, "message",
"Universal update successful, rebooting...");
const char *resp = cJSON_Print(root);
httpd_resp_sendstr(req, resp);
free((void *)resp);
cJSON_Delete(root);
// Reboot
esp_timer_create_args_t tmr_args = {};
tmr_args.callback = &bundle_ota_restart_timer_callback;
tmr_args.name = "bundle_reboot";
esp_timer_handle_t tmr;
esp_timer_create(&tmr_args, &tmr);
esp_timer_start_once(tmr, 1'000'000);
return ESP_OK;
}
internal const httpd_uri_t api_ota_bundle_uri = {.uri = "/api/ota/bundle",
.method = HTTP_POST,
.handler =
api_ota_bundle_handler,
.user_ctx = NULL};

View File

@@ -0,0 +1,164 @@
// SDK
#include "cJSON.h"
#include "esp_http_server.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "esp_system.h"
#include "esp_timer.h"
#include <sys/param.h>
// Project
#include "appstate.hpp"
#include "types.hpp"
#define OTA_FIRMWARE_SCRATCH_BUFSIZE 4096
internal void firmware_ota_restart_timer_callback(void *arg) { esp_restart(); }
internal esp_err_t api_ota_firmware_handler(httpd_req_t *req)
{
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
const esp_partition_t *update_partition =
esp_ota_get_next_update_partition(NULL);
if (update_partition == NULL)
{
ESP_LOGE("OTA_FW", "Passive OTA partition not found");
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"OTA partition not found");
return ESP_FAIL;
}
ESP_LOGI("OTA_FW", "Writing to partition subtype %d at offset 0x%lx",
update_partition->subtype, update_partition->address);
esp_ota_handle_t update_handle = 0;
esp_err_t err =
esp_ota_begin(update_partition, OTA_SIZE_UNKNOWN, &update_handle);
if (err != ESP_OK)
{
ESP_LOGE("OTA_FW", "esp_ota_begin failed (%s)", esp_err_to_name(err));
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"OTA begin failed");
return ESP_FAIL;
}
char *buf = (char *)malloc(OTA_FIRMWARE_SCRATCH_BUFSIZE);
if (!buf)
{
ESP_LOGE("OTA_FW", "Failed to allocate buffer");
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Out of memory");
return ESP_FAIL;
}
int binary_file_len = 0;
int remaining = req->content_len;
while (remaining > 0)
{
int recv_len =
httpd_req_recv(req, buf, MIN(remaining, OTA_FIRMWARE_SCRATCH_BUFSIZE));
if (recv_len <= 0)
{
if (recv_len == HTTPD_SOCK_ERR_TIMEOUT)
{
continue;
}
ESP_LOGE("OTA_FW", "Receive failed");
esp_ota_abort(update_handle);
free(buf);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"Receive failed");
return ESP_FAIL;
}
if (binary_file_len == 0 && recv_len > 0)
{
if ((uint8_t)buf[0] != 0xE9)
{
ESP_LOGE("OTA_FW", "Invalid magic: %02X. Expected 0xE9 for Firmware.",
(uint8_t)buf[0]);
esp_ota_abort(update_handle);
free(buf);
httpd_resp_send_err(
req, HTTPD_400_BAD_REQUEST,
"Invalid file: This does not look like an ESP32 firmware binary.");
return ESP_FAIL;
}
}
err = esp_ota_write(update_handle, (const void *)buf, recv_len);
if (err != ESP_OK)
{
ESP_LOGE("OTA_FW", "esp_ota_write failed (%s)", esp_err_to_name(err));
esp_ota_abort(update_handle);
free(buf);
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"Flash write failed");
return ESP_FAIL;
}
binary_file_len += recv_len;
remaining -= recv_len;
}
free(buf);
ESP_LOGI("OTA_FW", "Total binary data written: %d", binary_file_len);
err = esp_ota_end(update_handle);
if (err != ESP_OK)
{
if (err == ESP_ERR_OTA_VALIDATE_FAILED)
{
ESP_LOGE("OTA_FW", "Image validation failed, image is corrupted");
}
else
{
ESP_LOGE("OTA_FW", "esp_ota_end failed (%s)!", esp_err_to_name(err));
}
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"OTA validation/end failed");
return ESP_FAIL;
}
err = esp_ota_set_boot_partition(update_partition);
if (err != ESP_OK)
{
ESP_LOGE("OTA_FW", "esp_ota_set_boot_partition failed (%s)!",
esp_err_to_name(err));
httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR,
"Failed to set boot partition");
return ESP_FAIL;
}
ESP_LOGI("OTA_FW", "OTA successful, rebooting...");
httpd_resp_set_type(req, "application/json");
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "status", "success");
cJSON_AddStringToObject(root, "message",
"Firmware update successful, rebooting...");
const char *response_text = cJSON_Print(root);
httpd_resp_sendstr(req, response_text);
free((void *)response_text);
cJSON_Delete(root);
// Trigger reboot with 1s delay
const esp_timer_create_args_t restart_timer_args = {
.callback = &firmware_ota_restart_timer_callback,
.arg = (void *)0,
.dispatch_method = ESP_TIMER_TASK,
.name = "fw_ota_restart_timer",
.skip_unhandled_events = false};
esp_timer_handle_t restart_timer;
esp_timer_create(&restart_timer_args, &restart_timer);
esp_timer_start_once(restart_timer, 1'000'000);
return ESP_OK;
}
internal const httpd_uri_t api_ota_firmware_uri = {.uri = "/api/ota/firmware",
.method = HTTP_POST,
.handler =
api_ota_firmware_handler,
.user_ctx = NULL};

View File

@@ -8,7 +8,6 @@
#include "nvs.h"
#include "nvs_flash.h"
// Project
#include "appstate.hpp"
#include "types.hpp"
@@ -59,6 +58,7 @@ internal esp_err_t api_ota_frontend_handler(httpd_req_t *req)
int total_read = 0;
int remaining = req->content_len;
bool first_chunk = true;
while (remaining > 0)
{
@@ -77,6 +77,21 @@ internal esp_err_t api_ota_frontend_handler(httpd_req_t *req)
return ESP_FAIL;
}
if (first_chunk)
{
if ((uint8_t)buf[0] == 0xE9)
{
ESP_LOGE("OTA", "Magic 0xE9 detected. This looks like a FIRMWARE bin, "
"but you are uploading to FRONTEND slot!");
free(buf);
httpd_resp_send_err(
req, HTTPD_400_BAD_REQUEST,
"Invalid file: This is a Firmware binary, not a UI binary.");
return ESP_FAIL;
}
first_chunk = false;
}
err = esp_partition_write(partition, total_read, buf, recv_len);
if (err != ESP_OK)
{

View File

@@ -1,12 +1,20 @@
#include <cstddef>
// SDK
#include "cJSON.h"
#include "esp_http_server.h"
#include "esp_image_format.h"
#include "esp_littlefs.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "esp_partition.h"
#include "esp_vfs.h"
#include <string.h>
// Project
#include "appstate.hpp"
#include "types.hpp"
#include "utils.hpp"
internal esp_err_t api_ota_status_handler(httpd_req_t *req)
{
@@ -17,34 +25,58 @@ internal esp_err_t api_ota_status_handler(httpd_req_t *req)
cJSON_AddNumberToObject(root, "active_slot", g_Active_WWW_Partition);
const char *partitions[] = {"www_0", "www_1"};
cJSON *parts_arr = cJSON_AddArrayToObject(root, "partitions");
for (int i = 0; i < 2; i++)
esp_partition_iterator_t it = esp_partition_find(
ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL);
while (it != NULL)
{
const esp_partition_t *p = esp_partition_get(it);
cJSON *p_obj = cJSON_CreateObject();
cJSON_AddStringToObject(p_obj, "label", partitions[i]);
cJSON_AddStringToObject(p_obj, "label", p->label);
cJSON_AddNumberToObject(p_obj, "type", p->type);
cJSON_AddNumberToObject(p_obj, "subtype", p->subtype);
cJSON_AddNumberToObject(p_obj, "address", p->address);
cJSON_AddNumberToObject(p_obj, "size", p->size);
const esp_partition_t *p = esp_partition_find_first(
ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, partitions[i]);
if (p)
// Try to get LittleFS info if it's a data partition
if (p->type == ESP_PARTITION_TYPE_DATA)
{
cJSON_AddNumberToObject(p_obj, "size", p->size);
size_t total = 0, used = 0;
if (esp_littlefs_info(partitions[i], &total, &used) == ESP_OK)
if (esp_littlefs_info(p->label, &total, &used) == ESP_OK)
{
cJSON_AddNumberToObject(p_obj, "used", used);
cJSON_AddNumberToObject(p_obj, "free", total - used);
}
else
{
// Not mounted or not LFS
cJSON_AddNumberToObject(p_obj, "used", 0);
cJSON_AddNumberToObject(p_obj, "free", p->size);
// For other data partitions (nvs, phy_init), just show total as used
// for now
cJSON_AddNumberToObject(p_obj, "used", p->size);
cJSON_AddNumberToObject(p_obj, "free", 0);
}
}
// For app partitions, try to find the binary size
else if (p->type == ESP_PARTITION_TYPE_APP)
{
esp_app_desc_t app_desc;
if (esp_ota_get_partition_description(p, &app_desc) == ESP_OK)
{
cJSON_AddStringToObject(p_obj, "app_version", app_desc.version);
// Get the true binary size from image metadata
esp_image_metadata_t data;
const esp_partition_pos_t pos = {.offset = p->address, .size = p->size};
if (esp_image_get_metadata(&pos, &data) == ESP_OK)
{
cJSON_AddNumberToObject(p_obj, "used", data.image_len);
cJSON_AddNumberToObject(p_obj, "free", p->size - data.image_len);
}
}
}
cJSON_AddItemToArray(parts_arr, p_obj);
it = esp_partition_next(it);
}
cJSON_AddStringToObject(root, "active_partition",
@@ -52,6 +84,24 @@ internal esp_err_t api_ota_status_handler(httpd_req_t *req)
cJSON_AddStringToObject(root, "target_partition",
g_Active_WWW_Partition == 0 ? "www_1" : "www_0");
const esp_partition_t *running = esp_ota_get_running_partition();
if (running)
{
cJSON_AddStringToObject(root, "running_firmware_label", running->label);
if (running->subtype >= ESP_PARTITION_SUBTYPE_APP_OTA_MIN &&
running->subtype <= ESP_PARTITION_SUBTYPE_APP_OTA_MAX)
{
cJSON_AddNumberToObject(root, "running_firmware_slot",
running->subtype -
ESP_PARTITION_SUBTYPE_APP_OTA_MIN);
}
else
{
cJSON_AddNumberToObject(root, "running_firmware_slot",
-1); // Factory or other
}
}
const char *status_info = cJSON_Print(root);
httpd_resp_sendstr(req, status_info);