From 23562806dbaa6f8b24b7f5424b77a3c18e2e948e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20Ba=C5=82ys?= Date: Wed, 6 Sep 2023 13:37:06 +0200 Subject: [PATCH] [nrfconnect][ota] Change image checking condition for OTA. (#28983) If we want to confirm the new firmware update in the AppTask Init method we cannot rely on the imageProcessor. Instead, for the nRF53 target we should check if the image has been confirmed already, and if not, confirm it, for targets other than nRF53 we should use the mcuboot_swap_type function to verify whether the swapping type is equal to REVERT before verifying the image. --- .../nrfconnect/main/AppTask.cpp | 6 +----- .../nrfconnect/main/AppTask.cpp | 6 +----- .../nrfconnect/main/AppTask.cpp | 6 +----- .../lighting-app/nrfconnect/main/AppTask.cpp | 14 +++++-------- examples/lock-app/nrfconnect/main/AppTask.cpp | 6 +----- examples/platform/nrfconnect/util/OTAUtil.cpp | 20 ++++++++++++++----- .../nrfconnect/util/include/OTAUtil.h | 2 +- examples/pump-app/nrfconnect/main/AppTask.cpp | 6 +----- .../nrfconnect/main/AppTask.cpp | 6 +----- .../window-app/nrfconnect/main/AppTask.cpp | 6 +----- 10 files changed, 28 insertions(+), 50 deletions(-) diff --git a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp index 7f9b4f87b74eea..217748c218519f 100644 --- a/examples/all-clusters-app/nrfconnect/main/AppTask.cpp +++ b/examples/all-clusters-app/nrfconnect/main/AppTask.cpp @@ -180,11 +180,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp b/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp index 783096c52d32b5..6ec81dfd5c363b 100644 --- a/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp +++ b/examples/all-clusters-minimal-app/nrfconnect/main/AppTask.cpp @@ -139,11 +139,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/light-switch-app/nrfconnect/main/AppTask.cpp b/examples/light-switch-app/nrfconnect/main/AppTask.cpp index 6bc4138a9d635a..b64f70472558c4 100644 --- a/examples/light-switch-app/nrfconnect/main/AppTask.cpp +++ b/examples/light-switch-app/nrfconnect/main/AppTask.cpp @@ -181,11 +181,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize Timers diff --git a/examples/lighting-app/nrfconnect/main/AppTask.cpp b/examples/lighting-app/nrfconnect/main/AppTask.cpp index a2ad1d6e5996e5..8bbddd6b47774f 100644 --- a/examples/lighting-app/nrfconnect/main/AppTask.cpp +++ b/examples/lighting-app/nrfconnect/main/AppTask.cpp @@ -203,6 +203,11 @@ CHIP_ERROR AppTask::Init() GetDFUOverSMP().ConfirmNewImage(); #endif +#ifdef CONFIG_CHIP_OTA_REQUESTOR + /* OTA image confirmation must be done before the factory data init. */ + OtaConfirmNewImage(); +#endif + // Initialize lighting device (PWM) uint8_t minLightLevel = kDefaultMinLevel; Clusters::LevelControl::Attributes::MinLevel::Get(kLightEndpointId, &minLightLevel); @@ -217,15 +222,6 @@ CHIP_ERROR AppTask::Init() } mPWMDevice.SetCallbacks(ActionInitiated, ActionCompleted); -#ifdef CONFIG_CHIP_OTA_REQUESTOR - /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } -#endif - // Initialize CHIP server #if CONFIG_CHIP_FACTORY_DATA ReturnErrorOnFailure(mFactoryDataProvider.Init()); diff --git a/examples/lock-app/nrfconnect/main/AppTask.cpp b/examples/lock-app/nrfconnect/main/AppTask.cpp index ab5df56d36d9d4..17fa9e31412123 100644 --- a/examples/lock-app/nrfconnect/main/AppTask.cpp +++ b/examples/lock-app/nrfconnect/main/AppTask.cpp @@ -189,11 +189,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/platform/nrfconnect/util/OTAUtil.cpp b/examples/platform/nrfconnect/util/OTAUtil.cpp index f1c1c916aa0aa7..c642a9001f56ef 100644 --- a/examples/platform/nrfconnect/util/OTAUtil.cpp +++ b/examples/platform/nrfconnect/util/OTAUtil.cpp @@ -66,20 +66,30 @@ void InitBasicOTARequestor() imageProcessor.TriggerFlashAction(ExternalFlashManager::Action::SLEEP); } -CHIP_ERROR OtaConfirmNewImage() +void OtaConfirmNewImage() { - CHIP_ERROR err = CHIP_NO_ERROR; +#ifndef CONFIG_SOC_SERIES_NRF53X + /* Check if the image is run in the REVERT mode and eventually + confirm it to prevent reverting on the next boot. + On nRF53 target there is not way to verify current swap type + because we use permanent swap so we can skip it. */ + VerifyOrReturn(mcuboot_swap_type() == BOOT_SWAP_TYPE_REVERT); +#endif + OTAImageProcessorImpl & imageProcessor = GetOTAImageProcessor(); - if (imageProcessor.IsFirstImageRun()) + if (!boot_is_img_confirmed()) { CHIP_ERROR err = System::MapErrorZephyr(boot_write_img_confirmed()); if (CHIP_NO_ERROR == err) { imageProcessor.SetImageConfirmed(); + ChipLogProgress(SoftwareUpdate, "New firmware image confirmed"); + } + else + { + ChipLogError(SoftwareUpdate, "Failed to confirm firmware image, it will be reverted on the next boot"); } } - ChipLogError(SoftwareUpdate, "Failed to confirm firmware image, it will be reverted on the next boot"); - return err; } #endif diff --git a/examples/platform/nrfconnect/util/include/OTAUtil.h b/examples/platform/nrfconnect/util/include/OTAUtil.h index 2e120f6b7db096..9c4c6d8d410bc5 100644 --- a/examples/platform/nrfconnect/util/include/OTAUtil.h +++ b/examples/platform/nrfconnect/util/include/OTAUtil.h @@ -54,7 +54,7 @@ void InitBasicOTARequestor(); * boot after the OTA update. * Other CHIP_ERROR codes if the image could not be confirmed. */ -CHIP_ERROR OtaConfirmNewImage(); +void OtaConfirmNewImage(); #endif // CONFIG_CHIP_OTA_REQUESTOR diff --git a/examples/pump-app/nrfconnect/main/AppTask.cpp b/examples/pump-app/nrfconnect/main/AppTask.cpp index d21908bfa165b4..b2406ed56cf29d 100644 --- a/examples/pump-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-app/nrfconnect/main/AppTask.cpp @@ -162,11 +162,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp index f1b0fb19a3a19b..9b00a541cada3b 100644 --- a/examples/pump-controller-app/nrfconnect/main/AppTask.cpp +++ b/examples/pump-controller-app/nrfconnect/main/AppTask.cpp @@ -160,11 +160,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server diff --git a/examples/window-app/nrfconnect/main/AppTask.cpp b/examples/window-app/nrfconnect/main/AppTask.cpp index 5b80507a375c71..ed16dc1311dff8 100644 --- a/examples/window-app/nrfconnect/main/AppTask.cpp +++ b/examples/window-app/nrfconnect/main/AppTask.cpp @@ -167,11 +167,7 @@ CHIP_ERROR AppTask::Init() #ifdef CONFIG_CHIP_OTA_REQUESTOR /* OTA image confirmation must be done before the factory data init. */ - err = OtaConfirmNewImage(); - if (err != CHIP_NO_ERROR) - { - return err; - } + OtaConfirmNewImage(); #endif // Initialize CHIP server