From 4bdbd186a5a0bf878b90befc27d2270fcca9b3a7 Mon Sep 17 00:00:00 2001 From: Jan Zyczkowski Date: Tue, 21 May 2024 11:07:47 +0200 Subject: [PATCH] applications: nrf_desktop: Add SMP SUIT DFU support for nrf54h20 Add SMP SUIT DFU support for nrf_desktop application. JIRA: NCSDK-26998 NCSDK-25024 Signed-off-by: Jan Zyczkowski --- applications/nrf_desktop/Kconfig.sysbuild | 3 + .../nrf54h20dk_nrf54h20_cpuapp/app.overlay | 15 +++++ .../nrf54h20dk_nrf54h20_cpuapp/prj.conf | 30 +++++++++ .../prj_release.conf | 28 +++++++++ .../images/ipc_radio/prj.conf | 5 ++ .../images/ipc_radio/prj_release.conf | 5 ++ .../src/modules/Kconfig.dfu_mcumgr | 43 ++++++++++--- .../nrf_desktop/src/modules/dfu_mcumgr.c | 61 ++++++++++++++++--- 8 files changed, 172 insertions(+), 18 deletions(-) diff --git a/applications/nrf_desktop/Kconfig.sysbuild b/applications/nrf_desktop/Kconfig.sysbuild index 3eed51bf7363..b0a210ef8624 100644 --- a/applications/nrf_desktop/Kconfig.sysbuild +++ b/applications/nrf_desktop/Kconfig.sysbuild @@ -29,4 +29,7 @@ config SECURE_BOOT_APPCORE config PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY default y if BOARD_NRF52840DK_NRF52840 +config SUIT_ENVELOPE + default y if BOARD_NRF54H20DK + source "${ZEPHYR_BASE}/share/sysbuild/Kconfig" diff --git a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/app.overlay b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/app.overlay index caa85784048f..16995191907a 100644 --- a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/app.overlay +++ b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/app.overlay @@ -52,3 +52,18 @@ }; }; }; + +&cpuapp_rw_partitions { + dfu_partition: partition@13c000 { + label = "dfu_partition"; + reg = <0x13c000 DT_SIZE_K(676)>; + }; +}; + +&cpusec_cpuapp_ipc { + status = "okay"; +}; + +&cpusec_bellboard { + status = "okay"; +}; diff --git a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj.conf b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj.conf index e17a7f182dc0..398907a0f379 100644 --- a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj.conf +++ b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj.conf @@ -30,6 +30,8 @@ CONFIG_CAF_CLICK_DETECTOR=y CONFIG_CAF_LEDS=y +CONFIG_CAF_INIT_LOG_BLE_SMP_TRANSFER_EVENTS=n + # The LLPM must be explicitly enabled, as the Bluetooth Controller # is not part of the main application. CONFIG_CAF_BLE_USE_LLPM=y @@ -107,3 +109,31 @@ CONFIG_LOG_MODE_DEFERRED=y CONFIG_LOG_PRINTK=y CONFIG_LOG_BUFFER_SIZE=4096 CONFIG_LOG_PROCESS_THREAD_STACK_SIZE=1024 + +# Enable MCUmgr Bluetooth transport +CONFIG_DESKTOP_DFU_MCUMGR_ENABLE=y +CONFIG_MCUMGR_TRANSPORT_BT=y +CONFIG_MCUMGR_TRANSPORT_BT_AUTHEN=n + +# 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=251 + +# 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_TRANSPORT_WORKQUEUE_STACK_SIZE=4096 + +# Allow a DFU host to retrieve information about the number and size of MCUmgr buffers. +# The nRF Connect Device Manager uses this information to speed up the DFU. +CONFIG_MCUMGR_GRP_OS=y +CONFIG_MCUMGR_GRP_OS_MCUMGR_PARAMS=y + +################################################################################ +# DFU configuration + +CONFIG_SUIT=y +CONFIG_SUIT_DFU_CANDIDATE_PROCESSING_MINIMAL=y diff --git a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj_release.conf b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj_release.conf index 72fadd81a00c..f8a9630ea74f 100644 --- a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj_release.conf +++ b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpuapp/prj_release.conf @@ -87,3 +87,31 @@ CONFIG_BT_CONN_TX_MAX=4 CONFIG_SERIAL=n CONFIG_CONSOLE=n CONFIG_UART_CONSOLE=n + +# Enable MCUmgr Bluetooth transport +CONFIG_DESKTOP_DFU_MCUMGR_ENABLE=y +CONFIG_MCUMGR_TRANSPORT_BT=y +CONFIG_MCUMGR_TRANSPORT_BT_AUTHEN=n + +# 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=251 + +# 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_TRANSPORT_WORKQUEUE_STACK_SIZE=4096 + +# Allow a DFU host to retrieve information about the number and size of MCUmgr buffers. +# The nRF Connect Device Manager uses this information to speed up the DFU. +CONFIG_MCUMGR_GRP_OS=y +CONFIG_MCUMGR_GRP_OS_MCUMGR_PARAMS=y + +################################################################################ +# DFU configuration + +CONFIG_SUIT=y +CONFIG_SUIT_DFU_CANDIDATE_PROCESSING_MINIMAL=y diff --git a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj.conf b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj.conf index 5e745d3ed66f..f99145ba2140 100644 --- a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj.conf +++ b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj.conf @@ -59,3 +59,8 @@ CONFIG_THREAD_NAME=y CONFIG_IPC_RADIO_BT=y CONFIG_IPC_RADIO_BT_HCI_IPC=y + +# Allow for large Bluetooth data packets. +CONFIG_BT_BUF_ACL_RX_SIZE=502 +CONFIG_BT_BUF_ACL_TX_SIZE=251 +CONFIG_BT_CTLR_DATA_LENGTH_MAX=251 diff --git a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj_release.conf b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj_release.conf index e8a968c83fcc..9782e493e7d1 100644 --- a/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj_release.conf +++ b/applications/nrf_desktop/configuration/nrf54h20dk_nrf54h20_cpurad/images/ipc_radio/prj_release.conf @@ -46,3 +46,8 @@ CONFIG_UART_CONSOLE=n CONFIG_IPC_RADIO_BT=y CONFIG_IPC_RADIO_BT_HCI_IPC=y + +# Allow for large Bluetooth data packets. +CONFIG_BT_BUF_ACL_RX_SIZE=502 +CONFIG_BT_BUF_ACL_TX_SIZE=251 +CONFIG_BT_CTLR_DATA_LENGTH_MAX=251 diff --git a/applications/nrf_desktop/src/modules/Kconfig.dfu_mcumgr b/applications/nrf_desktop/src/modules/Kconfig.dfu_mcumgr index 0acbd7314d51..8aaa9884fc41 100644 --- a/applications/nrf_desktop/src/modules/Kconfig.dfu_mcumgr +++ b/applications/nrf_desktop/src/modules/Kconfig.dfu_mcumgr @@ -9,20 +9,13 @@ menuconfig DESKTOP_DFU_MCUMGR_ENABLE select EXPERIMENTAL select CAF_BLE_SMP_TRANSFER_EVENTS if MCUMGR_TRANSPORT_BT select MCUMGR - select MCUMGR_GRP_IMG - select MCUMGR_GRP_OS select MCUMGR_MGMT_NOTIFICATION_HOOKS - select MCUMGR_GRP_IMG_MUTEX if DESKTOP_DFU_LOCK select MCUMGR_SMP_COMMAND_STATUS_HOOKS - select NET_BUF - select ZCBOR select CRC - select IMG_MANAGER - select MCUBOOT_BOOTUTIL_LIB - select STREAM_FLASH - select FLASH_MAP + select NET_BUF select FLASH - depends on BOOTLOADER_MCUBOOT + select FLASH_MAP + select ZCBOR help This option enables an alternative method of performing DFU using the MCUmgr module. @@ -43,6 +36,36 @@ config DESKTOP_DFU_MCUMGR_MCUBOOT_DIRECT_XIP boots the image with a higher version. The module does not confirm the newly updated image after a successful boot. +choice DESKTOP_DFU_MCUMGR_BACKEND + prompt "Choose DFU backend" + default DESKTOP_DFU_BACKEND_MCUBOOT if BOOTLOADER_MCUBOOT + default DESKTOP_DFU_BACKEND_SUIT if SUIT + +config DESKTOP_DFU_BACKEND_MCUBOOT + bool "Use MCUboot as DFU backend" + select MCUBOOT_BOOTUTIL_LIB + select IMG_MANAGER + select MCUMGR_GRP_IMG + select MCUMGR_GRP_IMG_MUTEX if DESKTOP_DFU_LOCK + select MCUMGR_GRP_OS + select STREAM_FLASH + depends on BOOTLOADER_MCUBOOT + help + This option enables the MCUboot bootloader as the backend for the MCUmgr DFU module. + +config DESKTOP_DFU_BACKEND_SUIT + bool "Use SUIT as DFU backend" + select MCUMGR_SMP_LEGACY_RC_BEHAVIOUR + select MGMT_SUITFU + select MGMT_SUITFU_GRP_SUIT + select SSF_SUIT_SERVICE_ENABLED + select ZCBOR_CANONICAL + depends on SUIT + help + This option enables the SUIT as the backend for the MCUmgr DFU module. + +endchoice + module = DESKTOP_DFU_MCUMGR module-str = DFU MCUmgr source "subsys/logging/Kconfig.template.log_config" diff --git a/applications/nrf_desktop/src/modules/dfu_mcumgr.c b/applications/nrf_desktop/src/modules/dfu_mcumgr.c index cf4dfd31893f..6b8f2df48022 100644 --- a/applications/nrf_desktop/src/modules/dfu_mcumgr.c +++ b/applications/nrf_desktop/src/modules/dfu_mcumgr.c @@ -4,8 +4,16 @@ * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ +#include + +#if defined CONFIG_DESKTOP_DFU_BACKEND_MCUBOOT #include #include +#elif defined CONFIG_DESKTOP_DFU_BACKEND_SUIT +#include +#include +#endif + #include #include #include @@ -37,7 +45,16 @@ static void dfu_lock_owner_changed(const struct dfu_lock_owner *new_owner) /* The function declaration is not included in MCUmgr's header file if the mutex locking * of the image management state object is disabled. */ +#if defined CONFIG_DESKTOP_DFU_BACKEND_MCUBOOT img_mgmt_reset_upload(); +#elif defined CONFIG_DESKTOP_DFU_BACKEND_SUIT + int err = suit_dfu_cleanup(); + + if (err) { + module_set_state(MODULE_STATE_ERROR); + LOG_ERR("Failed to cleanup SUIT DFU: %d", err); + } +#endif #endif /* CONFIG_DESKTOP_DFU_LOCK */ } @@ -110,6 +127,41 @@ static struct mgmt_callback cmd_recv_cb = { .event_id = MGMT_EVT_OP_CMD_RECV, }; +#if CONFIG_DESKTOP_DFU_BACKEND_MCUBOOT +static void dfu_backend_init(void) +{ + if (!IS_ENABLED(CONFIG_DESKTOP_DFU_MCUMGR_MCUBOOT_DIRECT_XIP)) { + int err = boot_write_img_confirmed(); + + if (err) { + LOG_ERR("Cannot confirm a running image: %d", err); + } + } + + LOG_INF("MCUboot image version: %s", CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION); +} +#elif CONFIG_DESKTOP_DFU_BACKEND_SUIT +static void dfu_backend_init(void) +{ + unsigned int seq_num = 0; + suit_ssf_manifest_class_info_t class_info; + + int err = suit_get_supported_manifest_info(SUIT_MANIFEST_APP_ROOT, &class_info); + + if (!err) { + err = suit_get_installed_manifest_info(&(class_info.class_id), + &seq_num, NULL, NULL, NULL, NULL); + } + if (!err) { + LOG_INF("SUIT sequence number: %d", seq_num); + } else { + LOG_ERR("suit retrive manifest seq num failed (err: %d)", err); + } +} +#else +#error "The DFU backend choice is not supported" +#endif /* CONFIG_DESKTOP_DFU_BACKEND_MCUBOOT */ + static bool app_event_handler(const struct app_event_header *aeh) { if (IS_ENABLED(CONFIG_MCUMGR_TRANSPORT_BT) && @@ -127,15 +179,8 @@ static bool app_event_handler(const struct app_event_header *aeh) cast_module_state_event(aeh); if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) { - if (!IS_ENABLED(CONFIG_DESKTOP_DFU_MCUMGR_MCUBOOT_DIRECT_XIP)) { - int err = boot_write_img_confirmed(); - - if (err) { - LOG_ERR("Cannot confirm a running image: %d", err); - } - } + dfu_backend_init(); - LOG_INF("MCUboot image version: %s", CONFIG_MCUBOOT_IMGTOOL_SIGN_VERSION); k_work_init_delayable(&dfu_timeout, dfu_timeout_handler); mgmt_callback_register(&cmd_recv_cb);