Skip to content
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
27 changes: 21 additions & 6 deletions cmake/sysbuild/image_signing.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ function(zephyr_mcuboot_tasks)
# MCUboot.
#
# Therefore, go with an explicitly installed imgtool first, falling
# back on mcuboot/scripts/imgtool.py.
if(IMGTOOL)
# back on mcuboot/scripts/imgtool.py. We exclude the system imgtool when
# compressed image support is enabled due to needing a version of imgtool
# that has features not in the most recent public release.
if(IMGTOOL AND NOT CONFIG_MCUBOOT_COMPRESSED_IMAGE_SUPPORT_ENABLED)
set(imgtool_path "${IMGTOOL}")
elseif(DEFINED ZEPHYR_MCUBOOT_MODULE_DIR)
set(IMGTOOL_PY "${ZEPHYR_MCUBOOT_MODULE_DIR}/scripts/imgtool.py")
Expand Down Expand Up @@ -103,6 +105,19 @@ function(zephyr_mcuboot_tasks)
set(imgtool_extra)
endif()

if(CONFIG_MCUBOOT_COMPRESSED_IMAGE_SUPPORT_ENABLED)
set(imgtool_bin_extra --compression lzma2armthumb)
else()
set(imgtool_bin_extra)
endif()

# Apply compression to hex file if this is a test
if(ncs_compress_test_compress_hex)
set(imgtool_hex_extra ${imgtool_bin_extra})
else()
set(imgtool_hex_extra)
endif()

if(CONFIG_MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION)
set(imgtool_extra --security-counter ${CONFIG_MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_VALUE} ${imgtool_extra})
endif()
Expand Down Expand Up @@ -153,7 +168,7 @@ function(zephyr_mcuboot_tasks)
# calls to the "extra_post_build_commands" property ensures they run
# after the commands which generate the unsigned versions.
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
${imgtool_sign} ${imgtool_args} ${unconfirmed_args})
${imgtool_sign} ${imgtool_args} ${imgtool_bin_extra} ${unconfirmed_args})

if(NOT "${keyfile_enc}" STREQUAL "")
if(CONFIG_BUILD_WITH_TFM)
Expand All @@ -169,7 +184,7 @@ function(zephyr_mcuboot_tasks)
)

set_property(GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
${imgtool_sign} ${imgtool_args} --encrypt "${keyfile_enc}" ${unconfirmed_args})
${imgtool_sign} ${imgtool_args} --encrypt "${keyfile_enc}" ${imgtool_bin_extra} ${unconfirmed_args})
endif()
endif()

Expand All @@ -196,7 +211,7 @@ function(zephyr_mcuboot_tasks)
# calls to the "extra_post_build_commands" property ensures they run
# after the commands which generate the unsigned versions.
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
${imgtool_sign} ${imgtool_args} ${imgtool_directxip_hex_command} ${unconfirmed_args})
${imgtool_sign} ${imgtool_args} ${imgtool_directxip_hex_command} ${imgtool_hex_extra} ${unconfirmed_args})

if(NOT "${keyfile_enc}" STREQUAL "")
set(unconfirmed_args ${input}.hex ${output}.encrypted.hex)
Expand All @@ -206,7 +221,7 @@ function(zephyr_mcuboot_tasks)
)

set_property(GLOBAL APPEND PROPERTY extra_post_build_commands COMMAND
${imgtool_sign} ${imgtool_args} --encrypt "${keyfile_enc}" ${unconfirmed_args})
${imgtool_sign} ${imgtool_args} --encrypt "${keyfile_enc}" ${imgtool_hex_extra} ${unconfirmed_args})
endif()
endif()

Expand Down
15 changes: 15 additions & 0 deletions samples/nrf_compress/mcuboot_update/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mcuboot_update)

# This project uses orginal sdk-zephyr C source code
target_sources(app PRIVATE src/hook.c ${ZEPHYR_BASE}/samples/subsys/mgmt/mcumgr/smp_svr/src/main.c)
target_sources_ifdef(CONFIG_MCUMGR_TRANSPORT_BT app PRIVATE ${ZEPHYR_BASE}/samples/subsys/mgmt/mcumgr/smp_svr/src/bluetooth.c)
zephyr_link_libraries(MCUBOOT_BOOTUTIL)
84 changes: 84 additions & 0 deletions samples/nrf_compress/mcuboot_update/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Enable MCUmgr and dependencies.
CONFIG_NET_BUF=y
CONFIG_ZCBOR=y
CONFIG_CRC=y
CONFIG_MCUMGR=y
CONFIG_STREAM_FLASH=y
CONFIG_FLASH_MAP=y

# Some command handlers require a large stack.
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2304
CONFIG_MAIN_STACK_SIZE=2048

# Ensure an MCUboot-compatible binary is generated.
CONFIG_BOOTLOADER_MCUBOOT=y

# Enable flash operations.
CONFIG_FLASH=y

# Required by the `taskstat` command.
CONFIG_THREAD_MONITOR=y

# Support for taskstat command
CONFIG_MCUMGR_GRP_OS_TASKSTAT=y

# Enable statistics and statistic names.
CONFIG_STATS=y
CONFIG_STATS_NAMES=y

# Enable most core commands.
CONFIG_FLASH=y
CONFIG_IMG_MANAGER=y
CONFIG_MCUMGR_GRP_IMG=y
CONFIG_MCUMGR_GRP_OS=y
CONFIG_MCUMGR_GRP_STAT=y

# Enable logging
CONFIG_LOG=y
CONFIG_MCUBOOT_UTIL_LOG_LEVEL_WRN=y

# Disable debug logging
CONFIG_LOG_MAX_LEVEL=3

CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y

# Allow for large Bluetooth data packets.
CONFIG_BT_L2CAP_TX_MTU=498
CONFIG_BT_BUF_ACL_RX_SIZE=502
CONFIG_BT_BUF_ACL_TX_SIZE=502
CONFIG_BT_CTLR_DATA_LENGTH_MAX=251

# Enable the Bluetooth mcumgr transport (unauthenticated).
CONFIG_MCUMGR_TRANSPORT_BT=y
CONFIG_MCUMGR_TRANSPORT_BT_AUTHEN=n
CONFIG_MCUMGR_TRANSPORT_BT_CONN_PARAM_CONTROL=y

# Enable the Shell mcumgr transport.
CONFIG_BASE64=y
CONFIG_SHELL=y
CONFIG_SHELL_BACKEND_SERIAL=y
CONFIG_MCUMGR_TRANSPORT_SHELL=y
CONFIG_MCUMGR_TRANSPORT_SHELL_RX_BUF_COUNT=8

# Enable the mcumgr Packet Reassembly feature over Bluetooth and its configuration dependencies.
# MCUmgr buffer size is optimized to fit one SMP packet divided into five Bluetooth Write Commands,
# transmitted with the maximum possible MTU value: 498 bytes.
CONFIG_MCUMGR_TRANSPORT_BT_REASSEMBLY=y
CONFIG_MCUMGR_TRANSPORT_NETBUF_SIZE=2475
CONFIG_MCUMGR_GRP_OS_MCUMGR_PARAMS=y
CONFIG_MCUMGR_TRANSPORT_WORKQUEUE_STACK_SIZE=4608

# Disable Bluetooth ping support
CONFIG_BT_CTLR_LE_PING=n

# Disable shell commands that are not needed
CONFIG_CLOCK_CONTROL_NRF_SHELL=n
CONFIG_DEVICE_SHELL=n
CONFIG_DEVMEM_SHELL=n
CONFIG_FLASH_SHELL=n

CONFIG_MCUMGR_MGMT_NOTIFICATION_HOOKS=y
CONFIG_MCUMGR_GRP_IMG_STATUS_HOOKS=y
CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_HOOK=y
CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_STATES=20
88 changes: 88 additions & 0 deletions samples/nrf_compress/mcuboot_update/src/hook.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/storage/flash_map.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
#include <zephyr/mgmt/mcumgr/grp/img_mgmt/img_mgmt.h>
#include <zephyr/logging/log.h>
#include <zcbor_common.h>
#include <zcbor_encode.h>
#include <bootutil/image.h>

LOG_MODULE_REGISTER(mcuboot_update_sample, LOG_LEVEL_DBG);

#define SECONDARY_SLOT 1

#ifndef CONFIG_MCUMGR_GRP_IMG_FRUGAL_LIST
#define ZCBOR_ENCODE_FLAG(zse, label, value) \
(zcbor_tstr_put_lit(zse, label) && zcbor_bool_put(zse, value))
#else
/* In "frugal" lists flags are added to response only when they evaluate to true */
/* Note that value is evaluated twice! */
#define ZCBOR_ENCODE_FLAG(zse, label, value) (!(value) || \
(zcbor_tstr_put_lit(zse, label) && zcbor_bool_put(zse, (value))))
#endif

static struct mgmt_callback image_slot_callback;
static struct k_work slot_output_work;

static void slot_output_handler(struct k_work *work)
{
uint32_t slot_flags;
int rc;

rc = img_mgmt_read_info(SECONDARY_SLOT, NULL, NULL, &slot_flags);

if (!rc) {
slot_flags &= IMAGE_F_COMPRESSED_LZMA2 && IMAGE_F_COMPRESSED_ARM_THUMB_FLT;

if (slot_flags == (IMAGE_F_COMPRESSED_LZMA2 && IMAGE_F_COMPRESSED_ARM_THUMB_FLT)) {
LOG_INF(
"Secondary slot image is LZMA2 compressed with ARM thumb filter applied");
} else if (slot_flags & IMAGE_F_COMPRESSED_LZMA2) {
LOG_INF("Secondary slot image is LZMA2 compressed");
} else {
LOG_INF("Secondary slot image is uncompressed");
}
} else {
LOG_ERR("Failed to read slot info: %d", rc);
}
}

static enum mgmt_cb_return image_slot_state_cb(uint32_t event, enum mgmt_cb_return prev_status,
int32_t *rc, uint16_t *group, bool *abort_more,
void *data, size_t data_size)
{
if (event == MGMT_EVT_OP_IMG_MGMT_IMAGE_SLOT_STATE) {
/* Append a field indicating if the image in the slot is compressed */
struct img_mgmt_state_slot_encode *slot_data =
(struct img_mgmt_state_slot_encode *)data;

*slot_data->ok = ZCBOR_ENCODE_FLAG(slot_data->zse, "compressed",
(slot_data->flags & IMAGE_F_COMPRESSED_LZMA2));
} else if (event == MGMT_EVT_OP_IMG_MGMT_DFU_PENDING) {
(void)k_work_submit(&slot_output_work);
}

return MGMT_CB_OK;
}

static int setup_slot_hook(void)
{
/* Setup hook for when img mgmt image slot state is used and DFU pending callback */
image_slot_callback.callback = image_slot_state_cb;
image_slot_callback.event_id = MGMT_EVT_OP_IMG_MGMT_IMAGE_SLOT_STATE |
MGMT_EVT_OP_IMG_MGMT_DFU_PENDING;
mgmt_callback_register(&image_slot_callback);
k_work_init(&slot_output_work, slot_output_handler);

return 0;
}

SYS_INIT(setup_slot_hook, APPLICATION, 0);
12 changes: 12 additions & 0 deletions samples/nrf_compress/mcuboot_update/sysbuild.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

SB_CONFIG_BOOTLOADER_MCUBOOT=y
SB_CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256=y
SB_CONFIG_MCUBOOT_MODE_OVERWRITE_ONLY=y
SB_CONFIG_MCUBOOT_UPDATEABLE_IMAGES=1
SB_CONFIG_MCUBOOT_COMPRESSED_IMAGE_SUPPORT=y
SB_CONFIG_NETCORE_HCI_IPC=y
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_BOOT_MAX_IMG_SECTORS=512
14 changes: 14 additions & 0 deletions samples/nrf_compress/mcuboot_update/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
common:
sysbuild: true
tags: sysbuild
build_only: true
tests:
nrf_compress.mcuboot_update:
platform_allow:
- nrf52840dk/nrf52840
- nrf5340dk/nrf5340/cpuapp
- nrf54l15pdk/nrf54l15/cpuapp
integration_platforms:
- nrf52840dk/nrf52840
- nrf5340dk/nrf5340/cpuapp
- nrf54l15pdk/nrf54l15/cpuapp
5 changes: 5 additions & 0 deletions subsys/bootloader/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,9 @@ rsource "bl_crypto/Kconfig"
rsource "bl_validation/Kconfig"
rsource "bl_storage/Kconfig"

config MCUBOOT_COMPRESSED_IMAGE_SUPPORT_ENABLED
bool "MCUboot compressed image support"
help
This is a Kconfig which is informative only, the value should not be changed.

endmenu
6 changes: 6 additions & 0 deletions subsys/partition_manager/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,10 @@ config PM_SRAM_SIZE
default 0x40000 if SOC_NRF54L15_CPUFLPR # Because the cpuapp_rram DT node is deleted in nrf54l15_cpuflpr.dtsi
default $(dt_node_reg_size_hex,/soc/memory@20000000)

config PM_USE_CONFIG_SRAM_SIZE
bool
help
Hidden option that, if set, will use the Kconfig value of PM_SRAM_SIZE for the SRAM size
value instead of the partition-manager project generated output value

endmenu # Partition Manager
15 changes: 15 additions & 0 deletions sysbuild/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,21 @@ function(${SYSBUILD_CURRENT_MODULE_NAME}_pre_cmake)
set_config_bool(mcuboot CONFIG_USE_NRF53_MULTI_IMAGE_WITHOUT_UPGRADE_ONLY y)
endif()
endif()

# Apply image compression support options
if(SB_CONFIG_MCUBOOT_COMPRESSED_IMAGE_SUPPORT)
set_config_bool(mcuboot CONFIG_NRF_COMPRESS y)
set_config_bool(mcuboot CONFIG_NRF_COMPRESS_DECOMPRESSION y)
set_config_bool(mcuboot CONFIG_NRF_COMPRESS_LZMA y)
set_config_bool(mcuboot CONFIG_NRF_COMPRESS_LZMA_VERSION_LZMA2 y)
set_config_bool(mcuboot CONFIG_NRF_COMPRESS_ARM_THUMB y)
set_config_bool(mcuboot CONFIG_NRF_COMPRESS_CLEANUP y)
set_config_bool(mcuboot CONFIG_BOOT_DECOMPRESSION y)
set_config_bool(${DEFAULT_IMAGE} CONFIG_MCUBOOT_COMPRESSED_IMAGE_SUPPORT_ENABLED y)
else()
set_config_bool(mcuboot CONFIG_BOOT_DECOMPRESSION n)
set_config_bool(${DEFAULT_IMAGE} CONFIG_MCUBOOT_COMPRESSED_IMAGE_SUPPORT_ENABLED n)
endif()
endif()

if(SB_CONFIG_SECURE_BOOT_APPCORE)
Expand Down
9 changes: 9 additions & 0 deletions sysbuild/Kconfig.mcuboot
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ config MCUBOOT_HW_DOWNGRADE_PREVENTION_COUNTER_VALUE

endif # MCUBOOT_HARDWARE_DOWNGRADE_PREVENTION

config MCUBOOT_COMPRESSED_IMAGE_SUPPORT
bool "Compressed image support"
depends on MCUBOOT_MODE_OVERWRITE_ONLY
depends on MCUBOOT_UPDATEABLE_IMAGES = "1"
depends on !BOOT_ENCRYPTION
help
When enabled, supports loading compressed images using LZMA2 to the secondary slot which
will then be decompressed and loaded to the primary slot.

config MCUBOOT_MAX_UPDATEABLE_IMAGES
int
default 4
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mcuboot_update)

target_sources(app PRIVATE src/main.c)

zephyr_link_libraries(MCUBOOT_BOOTUTIL)
14 changes: 14 additions & 0 deletions tests/subsys/nrf_compress/decompression/mcuboot_update/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

config UPDATE_SLOT_NUMBER
int "Image update number"
range 0 3
default 0
help
Should be set to the image containing the firmware to mark for upgrade

source "Kconfig.zephyr"
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/ {
chosen {
nordic,pm-ext-flash = &mx25r64;
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(smp_svr)

target_sources(app PRIVATE src/main.c)
Loading
Loading