From 536aa1fb62bf8eddb14b151a05ecebfb8e0c33d6 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Fri, 15 Dec 2023 11:32:46 +0000 Subject: [PATCH] Fix post-OTA status save --- mongoose.c | 8 ++++++-- src/ota_flash.c | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/mongoose.c b/mongoose.c index 56c83e09..d247b33a 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5767,10 +5767,14 @@ MG_IRAM void mg_ota_boot(void) { struct mg_otadata prev = mg_otadata(MG_FIRMWARE_PREVIOUS); if (curr.status == MG_OTA_FIRST_BOOT) { + if (prev.status == MG_OTA_UNAVAILABLE) { + MG_INFO(("Setting previous firmware state to committed")); + prev.status = MG_OTA_COMMITTED; + mg_flash_save(NULL, mg_fwkey(MG_FIRMWARE_PREVIOUS), &prev, sizeof(prev)); + } curr.status = MG_OTA_UNCOMMITTED; MG_INFO(("First boot, setting status to UNCOMMITTED")); - mg_flash_save(NULL, MG_OTADATA_KEY + MG_FIRMWARE_CURRENT, &curr, - sizeof(curr)); + mg_flash_save(NULL, mg_fwkey(MG_FIRMWARE_CURRENT), &curr, sizeof(curr)); } else if (prev.status == MG_OTA_FIRST_BOOT && mg_flash_bank() == 0) { // Swap paritions. Pray power does not disappear size_t fs = mg_flash_size(), ss = mg_flash_sector_size(); diff --git a/src/ota_flash.c b/src/ota_flash.c index d5fe87b0..d52d90c3 100644 --- a/src/ota_flash.c +++ b/src/ota_flash.c @@ -147,10 +147,14 @@ MG_IRAM void mg_ota_boot(void) { struct mg_otadata prev = mg_otadata(MG_FIRMWARE_PREVIOUS); if (curr.status == MG_OTA_FIRST_BOOT) { + if (prev.status == MG_OTA_UNAVAILABLE) { + MG_INFO(("Setting previous firmware state to committed")); + prev.status = MG_OTA_COMMITTED; + mg_flash_save(NULL, mg_fwkey(MG_FIRMWARE_PREVIOUS), &prev, sizeof(prev)); + } curr.status = MG_OTA_UNCOMMITTED; MG_INFO(("First boot, setting status to UNCOMMITTED")); - mg_flash_save(NULL, MG_OTADATA_KEY + MG_FIRMWARE_CURRENT, &curr, - sizeof(curr)); + mg_flash_save(NULL, mg_fwkey(MG_FIRMWARE_CURRENT), &curr, sizeof(curr)); } else if (prev.status == MG_OTA_FIRST_BOOT && mg_flash_bank() == 0) { // Swap paritions. Pray power does not disappear size_t fs = mg_flash_size(), ss = mg_flash_sector_size();