Allow reboot to smaller OTA partition or factory partition

This commit is contained in:
MichaelDvP
2023-01-08 14:12:12 +01:00
parent 80ea1e95ee
commit f133fdfb1c
2 changed files with 27 additions and 4 deletions

View File

@@ -17,11 +17,24 @@ void RestartService::restart(AsyncWebServerRequest * request) {
void RestartService::partition(AsyncWebServerRequest * request) { void RestartService::partition(AsyncWebServerRequest * request) {
const esp_partition_t * factory_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL); const esp_partition_t * factory_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
if (!factory_partition) { if (factory_partition) {
esp_ota_set_boot_partition(factory_partition);
request->onDisconnect(RestartService::restartNow);
request->send(200);
return;
}
const esp_partition_t * ota_partition = esp_ota_get_next_update_partition(NULL);
if (!ota_partition) {
request->send(400); // bad request request->send(400); // bad request
return; return;
} }
esp_ota_set_boot_partition(factory_partition); uint64_t buffer;
esp_partition_read(ota_partition, 0, &buffer, 8);
if (buffer == 0xFFFFFFFFFFFFFFFF) { // partition empty
request->send(400); // bad request
return;
}
esp_ota_set_boot_partition(ota_partition);
request->onDisconnect(RestartService::restartNow); request->onDisconnect(RestartService::restartNow);
request->send(200); request->send(200);
} }

View File

@@ -32,8 +32,18 @@ void SystemStatus::systemStatus(AsyncWebServerRequest * request) {
root["psram_size"] = emsesp::EMSESP::system_.PSram(); root["psram_size"] = emsesp::EMSESP::system_.PSram();
root["free_psram"] = ESP.getFreePsram() / 1024; root["free_psram"] = ESP.getFreePsram() / 1024;
} }
const esp_partition_t * factory_partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL); const esp_partition_t * partition = esp_partition_find_first(ESP_PARTITION_TYPE_APP, ESP_PARTITION_SUBTYPE_APP_FACTORY, NULL);
root["has_loader"] = factory_partition != NULL; if (partition != NULL) { // factory partition found
root["has_loader"] = true;
} else { // check for not empty, smaller OTA partition
partition = esp_ota_get_next_update_partition(NULL);
if (partition) {
uint64_t buffer;
esp_partition_read(partition, 0, &buffer, 8);
const esp_partition_t * running = esp_ota_get_running_partition();
root["has_loader"] = (buffer != 0xFFFFFFFFFFFFFFFF && running->size != partition->size);
}
}
response->setLength(); response->setLength();
request->send(response); request->send(response);