Skip to content

Commit

Permalink
[nrf toup][nrfconnect] Added support for Matter OTA using SUIT
Browse files Browse the repository at this point in the history
Modified the OTAImageProcessorImpl to use dfu target multi image
only in case of using the mcuboot bootloader and to use dfu
target in case of using suit.

Signed-off-by: Kamil Kasperczyk <[email protected]>
  • Loading branch information
kkasperczyk-no committed Jul 15, 2024
1 parent 4d8e75c commit ebf22d4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions config/nrfconnect/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ config BOOT_IMAGE_ACCESS_HOOKS
config UPDATEABLE_IMAGE_NUMBER
default 3 if NRF_WIFI_PATCHES_EXT_FLASH_STORE
default 2 if SOC_SERIES_NRF53X
default 1 if SUIT # SUIT does not support the multi-image dfu

config DFU_MULTI_IMAGE_MAX_IMAGE_COUNT
default 3 if NRF_WIFI_PATCHES_EXT_FLASH_STORE
Expand Down
49 changes: 44 additions & 5 deletions src/platform/nrfconnect/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,16 @@

#include "DFUSync.h"

#include <dfu/dfu_multi_image.h>
#include <dfu/dfu_target.h>

#ifdef CONFIG_SUIT
#include <dfu/dfu_target_suit.h>
#else
#include <dfu/dfu_multi_image.h>
#include <dfu/dfu_target_mcuboot.h>
#include <zephyr/dfu/mcuboot.h>
#endif

#include <zephyr/logging/log.h>
#include <zephyr/pm/device.h>

Expand Down Expand Up @@ -90,6 +96,7 @@ CHIP_ERROR OTAImageProcessorImpl::PrepareDownloadImpl()
{
mHeaderParser.Init();
mParams = {};
#ifndef CONFIG_SUIT
ReturnErrorOnFailure(System::MapErrorZephyr(dfu_target_mcuboot_set_buf(mBuffer, sizeof(mBuffer))));
ReturnErrorOnFailure(System::MapErrorZephyr(dfu_multi_image_init(mBuffer, sizeof(mBuffer))));

Expand All @@ -103,6 +110,7 @@ CHIP_ERROR OTAImageProcessorImpl::PrepareDownloadImpl()

ReturnErrorOnFailure(System::MapErrorZephyr(dfu_multi_image_register_writer(&writer)));
};
#endif

#ifdef CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE
dfu_image_writer cdWriter;
Expand All @@ -128,12 +136,25 @@ CHIP_ERROR OTAImageProcessorImpl::Finalize()
{
PostOTAStateChangeEvent(DeviceLayer::kOtaDownloadComplete);
DFUSync::GetInstance().Free(mDfuSyncMutexId);

#ifdef CONFIG_SUIT
mDfuTargetSuitInitialized = false;
return System::MapErrorZephyr(dfu_target_done(true));
#else
return System::MapErrorZephyr(dfu_multi_image_done(true));
#endif
}

CHIP_ERROR OTAImageProcessorImpl::Abort()
{
CHIP_ERROR error = System::MapErrorZephyr(dfu_multi_image_done(false));
CHIP_ERROR error;

#ifdef CONFIG_SUIT
error = System::MapErrorZephyr(dfu_target_reset());
mDfuTargetSuitInitialized = false;
#else
error = System::MapErrorZephyr(dfu_multi_image_done(false));
#endif

DFUSync::GetInstance().Free(mDfuSyncMutexId);
TriggerFlashAction(ExternalFlashManager::Action::SLEEP);
Expand All @@ -145,6 +166,11 @@ CHIP_ERROR OTAImageProcessorImpl::Abort()
CHIP_ERROR OTAImageProcessorImpl::Apply()
{
PostOTAStateChangeEvent(DeviceLayer::kOtaApplyInProgress);

#ifdef CONFIG_SUIT
mDfuTargetSuitInitialized = false;
#endif

// Schedule update of all images
int err = dfu_target_schedule_update(-1);

Expand Down Expand Up @@ -178,6 +204,16 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & aBlock)

CHIP_ERROR error = ProcessHeader(aBlock);

#ifdef CONFIG_SUIT
if (!mDfuTargetSuitInitialized && error == CHIP_NO_ERROR)
{
ReturnErrorOnFailure(System::MapErrorZephyr(dfu_target_suit_set_buf(mBuffer, sizeof(mBuffer))));
ReturnErrorOnFailure(System::MapErrorZephyr(
dfu_target_init(DFU_TARGET_IMAGE_TYPE_SUIT, 0, static_cast<size_t>(mParams.totalFileBytes), nullptr)));
mDfuTargetSuitInitialized = true;
}
#endif

if (error == CHIP_NO_ERROR)
{
// DFU target library buffers data internally, so do not clone the block data.
Expand All @@ -187,9 +223,12 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & aBlock)
}
else
{
error = System::MapErrorZephyr(
dfu_multi_image_write(static_cast<size_t>(mParams.downloadedBytes), aBlock.data(), aBlock.size()));
mParams.downloadedBytes += aBlock.size();
#ifdef CONFIG_SUIT
int err = dfu_target_write(aBlock.data(), aBlock.size());
#else
int err = dfu_multi_image_write(static_cast<size_t>(mParams.downloadedBytes), aBlock.data(), aBlock.size());
#endif
error = System::MapErrorZephyr(err);
}
}

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 @@ -58,6 +58,10 @@ class OTAImageProcessorImpl : public OTAImageProcessorInterface
private:
bool mImageConfirmed = false;
uint32_t mDfuSyncMutexId;

#ifdef CONFIG_SUIT
bool mDfuTargetSuitInitialized = false;
#endif
};

} // namespace DeviceLayer
Expand Down

0 comments on commit ebf22d4

Please sign in to comment.