Skip to content

Commit

Permalink
samples: nrf_compress: Add mcuboot_update sample
Browse files Browse the repository at this point in the history
Adds a sample which shows usage of compressed image updates

Signed-off-by: Jamie McCrae <[email protected]>
  • Loading branch information
nordicjm committed Oct 4, 2024
1 parent 843ffe6 commit 02ff743
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
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)
83 changes: 83 additions & 0 deletions samples/nrf_compress/mcuboot_update/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 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_IMAGE_SLOT_STATE_HOOK=y
CONFIG_MCUMGR_GRP_IMG_IMAGE_SLOT_STATE_STATES=20
53 changes: 53 additions & 0 deletions samples/nrf_compress/mcuboot_update/src/hook.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <zephyr/mgmt/mcumgr/mgmt/mgmt.h>
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
#include <zcbor_common.h>
#include <zcbor_encode.h>
#include <bootutil/image.h>

#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 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));
}

return MGMT_CB_OK;
}

static int setup_slot_hook(void)
{
/* Setup hook for when img mgmt image slot state is used */
image_slot_callback.callback = image_slot_state_cb;
image_slot_callback.event_id = MGMT_EVT_OP_IMG_MGMT_IMAGE_SLOT_STATE;
mgmt_callback_register(&image_slot_callback);

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
1 change: 1 addition & 0 deletions samples/nrf_compress/mcuboot_update/sysbuild/mcuboot.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CONFIG_BOOT_MAX_IMG_SECTORS=512
32 changes: 32 additions & 0 deletions samples/nrf_compress/mcuboot_update/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
common:
sysbuild: true
tags: sysbuild
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
harness: console
harness_config:
type: multi_line
ordered: true
regex:
- "Starting bootloader"
- "Image index: 0, Swap type: none"
- "Bootloader chainload address offset:"
- "Jumping to the first image slot"
- "Booting nRF Connect SDK"
- "Starting bootloader"
- "Image index: 0, Swap type: perm"
- "Image 0 upgrade secondary slot -> primary slot"
- "Erasing the primary slot"
- "Image 0 copying the secondary slot to the primary slot"
- "Bootloader chainload address offset:"
- "Jumping to the first image slot"
- "Booting nRF Connect SDK"
- "Compressed image running"

0 comments on commit 02ff743

Please sign in to comment.