From 43e95b51d13c20b4eea1db0a631d6890cd60a3b2 Mon Sep 17 00:00:00 2001 From: JP Hutchins Date: Fri, 29 May 2026 15:54:38 -0700 Subject: [PATCH] boot: boot_serial: add optional mcumgr parameters command Add support for the mcumgr OS management group "MCUmgr parameters" command (group 0, command ID 6), gated behind the new CONFIG_BOOT_MGMT_MCUMGR_PARAMS option. The read-only command reports the SMP transport buffer parameters as a CBOR map, mirroring the command provided by Zephyr's SMP server so that existing SMP clients can query them unchanged. The serial recovery reassembles one command at a time into a single buffer, so the response reports a buf_size of CONFIG_BOOT_SERIAL_MAX_RECEIVE_SIZE and a buf_count of 1. The parameters do not carry the transport line length, which SMP serial clients assume to be 128 bytes, so the option depends on CONFIG_BOOT_MAX_LINE_INPUT_LEN remaining at its default of 128. Signed-off-by: JP Hutchins Assisted-By: Claude:opus-4.8 --- boot/boot_serial/src/boot_serial.c | 33 +++++++++++++++++++ boot/boot_serial/src/boot_serial_priv.h | 1 + boot/zephyr/Kconfig.serial_recovery | 13 ++++++++ .../include/mcuboot_config/mcuboot_config.h | 4 +++ boot/zephyr/sample.yaml | 1 + .../serial-recovery-mcumgr-params.md | 7 ++++ docs/serial_recovery.md | 1 + 7 files changed, 60 insertions(+) create mode 100644 docs/release-notes.d/serial-recovery-mcumgr-params.md diff --git a/boot/boot_serial/src/boot_serial.c b/boot/boot_serial/src/boot_serial.c index f99841981c..e26c76e7f8 100644 --- a/boot/boot_serial/src/boot_serial.c +++ b/boot/boot_serial/src/boot_serial.c @@ -1261,6 +1261,34 @@ bs_echo(char *buf, int len) } #endif +#ifdef MCUBOOT_BOOT_MGMT_MCUMGR_PARAMS +/* + * Reports the SMP transport buffer parameters so that clients can negotiate + * optimal serial fragmentation, mirroring the mcumgr OS group "MCUmgr + * parameters" command provided by Zephyr's SMP server. The serial recovery + * reassembles one command at a time into a single buffer, hence a buffer + * count of 1. The line length is not reported; enabling this command requires + * BOOT_MAX_LINE_INPUT_LEN to remain at the standard 128-byte fragment size. + */ +static void +bs_mcumgr_params(char *buf, int len) +{ + if ( + zcbor_map_start_encode(cbor_state, 10) && + zcbor_tstr_put_lit_cast(cbor_state, "buf_size") && + zcbor_uint32_put(cbor_state, MCUBOOT_SERIAL_MAX_RECEIVE_SIZE) && + zcbor_tstr_put_lit_cast(cbor_state, "buf_count") && + zcbor_uint32_put(cbor_state, 1) && + zcbor_map_end_encode(cbor_state, 10) + ) { + boot_serial_output(); + } else { + reset_cbor_state(); + bs_rc_rsp(MGMT_ERR_ENOMEM); + } +} +#endif + /* * Reset, and (presumably) boot to newly uploaded image. Flush console * before restarting. @@ -1354,6 +1382,11 @@ boot_serial_input(char *buf, int len) case NMGR_ID_RESET: bs_reset(buf, len); break; +#ifdef MCUBOOT_BOOT_MGMT_MCUMGR_PARAMS + case NMGR_ID_MCUMGR_PARAMS: + bs_mcumgr_params(buf, len); + break; +#endif default: bs_rc_rsp(MGMT_ERR_ENOTSUP); break; diff --git a/boot/boot_serial/src/boot_serial_priv.h b/boot/boot_serial/src/boot_serial_priv.h index 1801da1098..09a10701a0 100644 --- a/boot/boot_serial/src/boot_serial_priv.h +++ b/boot/boot_serial/src/boot_serial_priv.h @@ -54,6 +54,7 @@ extern "C" { #define NMGR_ID_ECHO 0 #define NMGR_ID_CONS_ECHO_CTRL 1 #define NMGR_ID_RESET 5 +#define NMGR_ID_MCUMGR_PARAMS 6 #ifndef __packed #define __packed __attribute__((__packed__)) diff --git a/boot/zephyr/Kconfig.serial_recovery b/boot/zephyr/Kconfig.serial_recovery index 780ba775d5..5be5c99309 100644 --- a/boot/zephyr/Kconfig.serial_recovery +++ b/boot/zephyr/Kconfig.serial_recovery @@ -111,6 +111,19 @@ config BOOT_MGMT_ECHO help if enabled, support for the mcumgr echo command is being added. +config BOOT_MGMT_MCUMGR_PARAMS + bool "SMP server buffer size" + depends on BOOT_MAX_LINE_INPUT_LEN = 128 + help + If enabled, support for the mcumgr OS group "MCUmgr parameters" + command (command ID 6) is added, mirroring the command provided by + Zephyr's SMP server. It reports the SMP transport buffer size + (BOOT_SERIAL_MAX_RECEIVE_SIZE) and buffer count so that SMP clients + can negotiate optimal serial fragmentation. The parameters do not + carry the transport line length, so this option requires + BOOT_MAX_LINE_INPUT_LEN to remain at the standard 128-byte fragment + size. buf_count will always be 1. + menuconfig ENABLE_MGMT_PERUSER bool "System specific mcumgr commands" help diff --git a/boot/zephyr/include/mcuboot_config/mcuboot_config.h b/boot/zephyr/include/mcuboot_config/mcuboot_config.h index 766565f3ca..d95c058b01 100644 --- a/boot/zephyr/include/mcuboot_config/mcuboot_config.h +++ b/boot/zephyr/include/mcuboot_config/mcuboot_config.h @@ -296,6 +296,10 @@ #define MCUBOOT_BOOT_MGMT_ECHO #endif +#ifdef CONFIG_BOOT_MGMT_MCUMGR_PARAMS +#define MCUBOOT_BOOT_MGMT_MCUMGR_PARAMS +#endif + #ifdef CONFIG_BOOT_IMAGE_ACCESS_HOOKS #define MCUBOOT_IMAGE_ACCESS_HOOKS #endif diff --git a/boot/zephyr/sample.yaml b/boot/zephyr/sample.yaml index 5e50a44d8f..d2735b2566 100644 --- a/boot/zephyr/sample.yaml +++ b/boot/zephyr/sample.yaml @@ -54,6 +54,7 @@ tests: - CONFIG_ENABLE_MGMT_PERUSER=y - CONFIG_BOOT_MGMT_CUSTOM_STORAGE_ERASE=y - CONFIG_BOOT_MGMT_ECHO=y + - CONFIG_BOOT_MGMT_MCUMGR_PARAMS=y - CONFIG_BOOT_SERIAL_IMG_GRP_IMAGE_STATE=y - CONFIG_BOOT_SERIAL_IMG_GRP_SLOT_INFO=y platform_allow: nrf52840dk/nrf52840 diff --git a/docs/release-notes.d/serial-recovery-mcumgr-params.md b/docs/release-notes.d/serial-recovery-mcumgr-params.md new file mode 100644 index 0000000000..5d446702b6 --- /dev/null +++ b/docs/release-notes.d/serial-recovery-mcumgr-params.md @@ -0,0 +1,7 @@ +- Added an optional serial recovery ``MCUmgr parameters`` command (mcumgr OS + management group, command ID 6), enabled with + ``CONFIG_BOOT_MGMT_MCUMGR_PARAMS``. The read-only command reports the SMP + transport ``buf_size`` and ``buf_count`` so that SMP clients can negotiate + optimal serial fragmentation, matching the command provided by Zephyr's SMP + server. It requires ``CONFIG_BOOT_MAX_LINE_INPUT_LEN`` to remain at its + default of 128. diff --git a/docs/serial_recovery.md b/docs/serial_recovery.md index ade2419dff..75094f771b 100644 --- a/docs/serial_recovery.md +++ b/docs/serial_recovery.md @@ -32,6 +32,7 @@ See the Zephyr [Device Management](https://docs.zephyrproject.org/latest/service MCUboot supports the following subset of the MCUmgr commands: * echo (OS group) * reset (OS group) +* MCUmgr parameters SMP buffer size (OS group), when ``MCUBOOT_BOOT_MGMT_MCUMGR_PARAMS`` is enabled * image list (IMG group) * image upload (IMG group)