Skip to content

Commit

Permalink
[nrf toup] [nrfconnect] Add support for SUIT over Matter OTA
Browse files Browse the repository at this point in the history
Added support for multi-image dfu target while using SUIT.

Signed-off-by: Arkadiusz Balys <[email protected]>
  • Loading branch information
ArekBalysNordic committed Oct 23, 2024
1 parent 5148b72 commit 7486fd3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
6 changes: 4 additions & 2 deletions config/nrfconnect/chip-module/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ config CHIP_DFU_LIBRARY_MCUMGR

config CHIP_DFU_LIBRARY_DFU_TARGET
bool "Use dfu target library for Matter DFU purposes"
# MCUBOOT
imply DFU_MULTI_IMAGE if CHIP_BOOTLOADER_MCUBOOT
imply DFU_MULTI_IMAGE
# SUIT
imply DFU_TARGET_SUIT if CHIP_BOOTLOADER_SUIT
# Initialize SUIT dfu by dfu target if not using mcumgr.
# If using mcumgr, SUIT dfu is initialized by mcumgr.
imply DFU_TARGET_SUIT_INITIALIZE_SUIT if (!CHIP_DFU_LIBRARY_MCUMGR && CHIP_BOOTLOADER_SUIT)
# COMMON
imply DFU_TARGET
imply STREAM_FLASH
Expand Down
4 changes: 2 additions & 2 deletions config/nrfconnect/chip-module/Kconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,11 @@ 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
default 2 if SOC_SERIES_NRF53X || CHIP_BOOTLOADER_SUIT

config DFU_MULTI_IMAGE_MAX_IMAGE_COUNT
default 3 if NRF_WIFI_PATCHES_EXT_FLASH_STORE
default 2 if CHIP_BOOTLOADER_SUIT

config NRF_WIFI_FW_PATCH_DFU
default y if NRF_WIFI_PATCHES_EXT_FLASH_STORE
Expand Down
55 changes: 21 additions & 34 deletions src/platform/nrfconnect/OTAImageProcessorImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@

#include <dfu/dfu_target.h>

#ifdef CONFIG_SUIT
#ifdef CONFIG_DFU_TARGET_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 <dfu/dfu_multi_image.h>

#include <zephyr/logging/log.h>
#include <zephyr/pm/device.h>
Expand Down Expand Up @@ -96,21 +96,32 @@ CHIP_ERROR OTAImageProcessorImpl::PrepareDownloadImpl()
{
mHeaderParser.Init();
mParams = {};
#ifndef CONFIG_SUIT
#ifdef CONFIG_DFU_TARGET_SUIT
ReturnErrorOnFailure(System::MapErrorZephyr(dfu_target_suit_set_buf(mBuffer, sizeof(mBuffer))));
#else
ReturnErrorOnFailure(System::MapErrorZephyr(dfu_target_mcuboot_set_buf(mBuffer, sizeof(mBuffer))));
#endif // CONFIG_DFU_TARGET_SUIT
ReturnErrorOnFailure(System::MapErrorZephyr(dfu_multi_image_init(mBuffer, sizeof(mBuffer))));

for (int image_id = 0; image_id < CONFIG_UPDATEABLE_IMAGE_NUMBER; ++image_id)
{
dfu_image_writer writer;

#ifdef CONFIG_DFU_TARGET_SUIT
// The first image is the SUIT manifest and must be placed in id=0, while all other images must be placed in id + 1,
// because id=1 is dedicated for internal DFU purposes when the SUIT manifest contains the firmware.
// In our case, we use cache processing, so we need to put firmware images starting from id=2.
writer.image_id = image_id == 0 ? image_id : image_id + 1;
writer.open = [](int id, size_t size) { return dfu_target_init(DFU_TARGET_IMAGE_TYPE_SUIT, id, size, nullptr); };
#else
writer.image_id = image_id;
writer.open = [](int id, size_t size) { return dfu_target_init(DFU_TARGET_IMAGE_TYPE_MCUBOOT, id, size, nullptr); };
writer.write = [](const uint8_t * chunk, size_t chunk_size) { return dfu_target_write(chunk, chunk_size); };
writer.close = [](bool success) { return success ? dfu_target_done(success) : dfu_target_reset(); };
#endif
writer.write = [](const uint8_t * chunk, size_t chunk_size) { return dfu_target_write(chunk, chunk_size); };
writer.close = [](bool success) { return success ? dfu_target_done(success) : dfu_target_reset(); };

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

#ifdef CONFIG_CHIP_CERTIFICATION_DECLARATION_STORAGE
dfu_image_writer cdWriter;
Expand All @@ -137,24 +148,14 @@ 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;

#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 @@ -167,10 +168,6 @@ 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 All @@ -184,7 +181,11 @@ CHIP_ERROR OTAImageProcessorImpl::Apply()
[](System::Layer *, void * /* context */) {
PlatformMgr().HandleServerShuttingDown();
k_msleep(CHIP_DEVICE_CONFIG_SERVER_SHUTDOWN_ACTIONS_SLEEP_MS);
#ifdef CONFIG_DFU_TARGET_SUIT
dfu_target_suit_reboot();
#else
Reboot(SoftwareRebootReason::kSoftwareUpdate);
#endif
},
nullptr /* context */);
}
Expand All @@ -204,16 +205,6 @@ 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 @@ -223,12 +214,8 @@ CHIP_ERROR OTAImageProcessorImpl::ProcessBlock(ByteSpan & aBlock)
}
else
{
#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());
mParams.downloadedBytes += aBlock.size();
#endif
error = System::MapErrorZephyr(err);
}
}
Expand Down

0 comments on commit 7486fd3

Please sign in to comment.