Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nrfconnect] [OTA] Confirm the new image in the app init. #28924

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/all-clusters-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ CHIP_ERROR AppTask::Init()
k_timer_init(&sFunctionTimer, &AppTask::FunctionTimerTimeoutCallback, nullptr);
k_timer_user_data_set(&sFunctionTimer, this);

#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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ CHIP_ERROR AppTask::Init()
k_timer_init(&sFunctionTimer, &AppTask::FunctionTimerTimeoutCallback, nullptr);
k_timer_user_data_set(&sFunctionTimer, this);

#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());
Expand Down
9 changes: 9 additions & 0 deletions examples/light-switch-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,15 @@ CHIP_ERROR AppTask::Init()
return System::MapErrorZephyr(ret);
}

#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 Timers
k_timer_init(&sFunctionTimer, AppTask::FunctionTimerTimeoutCallback, nullptr);
k_timer_init(&sDimmerPressKeyTimer, AppTask::FunctionTimerTimeoutCallback, nullptr);
Expand Down
9 changes: 9 additions & 0 deletions examples/lighting-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ 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());
Expand Down
9 changes: 9 additions & 0 deletions examples/lock-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,15 @@ CHIP_ERROR AppTask::Init()

BoltLockMgr().Init(LockStateChanged);

#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());
Expand Down
19 changes: 19 additions & 0 deletions examples/platform/nrfconnect/util/OTAUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
#include <app/clusters/ota-requestor/DefaultOTARequestorDriver.h>
#include <app/clusters/ota-requestor/DefaultOTARequestorStorage.h>
#include <app/server/Server.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/nrfconnect/OTAImageProcessorImpl.h>
#include <zephyr/dfu/mcuboot.h>
#endif

using namespace chip;
Expand Down Expand Up @@ -63,6 +65,23 @@ void InitBasicOTARequestor()
sOTARequestorDriver.Init(&sOTARequestor, &imageProcessor);
imageProcessor.TriggerFlashAction(ExternalFlashManager::Action::SLEEP);
}

CHIP_ERROR OtaConfirmNewImage()
{
CHIP_ERROR err = CHIP_NO_ERROR;
OTAImageProcessorImpl & imageProcessor = GetOTAImageProcessor();
if (imageProcessor.IsFirstImageRun())
{
CHIP_ERROR err = System::MapErrorZephyr(boot_write_img_confirmed());
if (CHIP_NO_ERROR == err)
{
imageProcessor.SetImageConfirmed();
}
}
ChipLogError(SoftwareUpdate, "Failed to confirm firmware image, it will be reverted on the next boot");
return err;
}

#endif

ExternalFlashManager & GetFlashHandler()
Expand Down
10 changes: 10 additions & 0 deletions examples/platform/nrfconnect/util/include/OTAUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ chip::DeviceLayer::OTAImageProcessorImpl & GetOTAImageProcessor();
*/
void InitBasicOTARequestor();

/**
* Check if the current image is the first boot the after OTA update and if so
* confirm it in MCUBoot.
*
* @return CHIP_NO_ERROR if the image has been confirmed, or it is not the first
* boot after the OTA update.
* Other CHIP_ERROR codes if the image could not be confirmed.
*/
CHIP_ERROR OtaConfirmNewImage();

#endif // CONFIG_CHIP_OTA_REQUESTOR

/**
Expand Down
9 changes: 9 additions & 0 deletions examples/pump-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ CHIP_ERROR AppTask::Init()
GetDFUOverSMP().ConfirmNewImage();
#endif

#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());
Expand Down
9 changes: 9 additions & 0 deletions examples/pump-controller-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ CHIP_ERROR AppTask::Init()
GetDFUOverSMP().ConfirmNewImage();
#endif

#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());
Expand Down
9 changes: 9 additions & 0 deletions examples/window-app/nrfconnect/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ CHIP_ERROR AppTask::Init()
GetDFUOverSMP().ConfirmNewImage();
#endif

#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());
Expand Down
2 changes: 1 addition & 1 deletion src/platform/nrfconnect/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ bool OTAImageProcessorImpl::IsFirstImageRun()
CHIP_ERROR OTAImageProcessorImpl::ConfirmCurrentImage()
{
PostOTAStateChangeEvent(DeviceLayer::kOtaApplyComplete);
return System::MapErrorZephyr(boot_write_img_confirmed());
return mImageConfirmed ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE;
}

CHIP_ERROR OTAImageProcessorImpl::ProcessHeader(ByteSpan & aBlock)
Expand Down
4 changes: 4 additions & 0 deletions src/platform/nrfconnect/OTAImageProcessorImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface
CHIP_ERROR ProcessBlock(ByteSpan & aBlock) override;
bool IsFirstImageRun() override;
CHIP_ERROR ConfirmCurrentImage() override;
void SetImageConfirmed() { mImageConfirmed = true; }

protected:
CHIP_ERROR PrepareDownloadImpl();
Expand All @@ -53,6 +54,9 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface
OTAImageHeaderParser mHeaderParser;
uint8_t mBuffer[kBufferSize];
ExternalFlashManager * mFlashHandler;

private:
bool mImageConfirmed = false;
};

} // namespace DeviceLayer
Expand Down