feat: enhance OTA upload handling with magic byte checks for image types
This commit is contained in:
parent
8b2a537b72
commit
c857e16de2
35
src/ota.cpp
35
src/ota.cpp
@ -4,8 +4,14 @@
|
|||||||
#include <SPIFFS.h>
|
#include <SPIFFS.h>
|
||||||
#include "commonFS.h"
|
#include "commonFS.h"
|
||||||
|
|
||||||
|
// Magic byte patterns für verschiedene Image-Typen
|
||||||
|
const uint8_t FIRMWARE_MAGIC = 0xE9;
|
||||||
|
const uint8_t ESP_MAGIC = 0xE9;
|
||||||
|
|
||||||
void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
|
void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
|
||||||
static size_t contentLength = 0;
|
static size_t contentLength = 0;
|
||||||
|
static bool isFullImage = false;
|
||||||
|
static uint32_t currentOffset = 0;
|
||||||
|
|
||||||
if (!index) {
|
if (!index) {
|
||||||
contentLength = request->contentLength();
|
contentLength = request->contentLength();
|
||||||
@ -16,29 +22,26 @@ void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t ind
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Determine if this is a full image (firmware + SPIFFS) or just firmware
|
// Prüfe Magic Byte für Image-Typ
|
||||||
bool isFullImage = (contentLength > 0x3D0000); // SPIFFS starts at 0x3D0000
|
if (data[0] == ESP_MAGIC) {
|
||||||
|
// Normales Firmware Image oder full.bin
|
||||||
|
isFullImage = (contentLength > 0x3D0000);
|
||||||
|
|
||||||
if (isFullImage) {
|
if (!Update.begin(contentLength)) {
|
||||||
// For full images, we need to make sure we have enough space and properly partition it
|
|
||||||
if (!Update.begin(ESP.getFreeSketchSpace(), U_FLASH)) {
|
|
||||||
Serial.printf("Not enough space for full image: %u bytes required\n", contentLength);
|
|
||||||
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Full image updates are not supported via OTA. Please use USB update for full images.\"}");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// For firmware-only updates
|
|
||||||
if (!Update.begin(contentLength, U_FLASH)) {
|
|
||||||
Serial.printf("Not enough space: %u required\n", contentLength);
|
Serial.printf("Not enough space: %u required\n", contentLength);
|
||||||
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Not enough space available for firmware update\"}");
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Not enough space available\"}");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
Serial.println(isFullImage ? "Full image update started" : "Firmware update started");
|
Serial.println(isFullImage ? "Full image update started" : "Firmware update started");
|
||||||
|
} else {
|
||||||
|
request->send(400, "application/json", "{\"status\":\"error\",\"message\":\"Invalid image format\"}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
currentOffset = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write chunk to flash
|
// Schreibe Daten
|
||||||
if (Update.write(data, len) != len) {
|
if (Update.write(data, len) != len) {
|
||||||
Update.printError(Serial);
|
Update.printError(Serial);
|
||||||
String errorMsg = Update.errorString();
|
String errorMsg = Update.errorString();
|
||||||
@ -46,6 +49,8 @@ void handleOTAUpload(AsyncWebServerRequest *request, String filename, size_t ind
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentOffset += len;
|
||||||
|
|
||||||
if (final) {
|
if (final) {
|
||||||
if (Update.end(true)) {
|
if (Update.end(true)) {
|
||||||
Serial.println("Update complete");
|
Serial.println("Update complete");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user