Skip to content

Commit

Permalink
[nrf fromtree] mgmt/MCUmgr/grp/os: Add booloader info support
Browse files Browse the repository at this point in the history
Adds command allowing to query information on bootloader.
In this case support is provided to query MCUboot information.

(cherry picked from commit e483544)
Signed-off-by: Dominik Ermel <[email protected]>
  • Loading branch information
de-nordic committed Oct 6, 2023
1 parent 58706ac commit cbe3d5f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions include/zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern "C" {
#define OS_MGMT_ID_RESET 5
#define OS_MGMT_ID_MCUMGR_PARAMS 6
#define OS_MGMT_ID_INFO 7
#define OS_MGMT_ID_BOOTLOADER_INFO 8

/**
* Command result codes for OS management group.
Expand All @@ -37,6 +38,9 @@ enum os_mgmt_err_code_t {

/** The provided format value is not valid. */
OS_MGMT_ERR_INVALID_FORMAT,

/** Query was not recognized. */
OS_MGMT_ERR_QUERY_YIELDS_NO_ANSWER,
};

/* Bitmask values used by the os info command handler. Note that the width of this variable is
Expand Down
8 changes: 7 additions & 1 deletion subsys/mgmt/mcumgr/grp/os_mgmt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Copyright (c) 2018-2021 mcumgr authors
# Copyright (c) 2022 Nordic Semiconductor ASA
# Copyright (c) 2022-2023 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: Apache-2.0
#
Expand All @@ -12,6 +12,12 @@ zephyr_library_sources(src/os_mgmt.c)

zephyr_library_include_directories(include)

if (CONFIG_MCUMGR_GRP_OS_BOOTLOADER_INFO)
zephyr_include_directories(
${ZEPHYR_MCUBOOT_MODULE_DIR}/boot/bootutil/include
)
endif()

if(DEFINED CONFIG_MCUMGR_GRP_OS_INFO_BUILD_DATE_TIME)
set(MCUMGR_GRP_OS_INFO_BUILD_DATE_TIME_DIR ${PROJECT_BINARY_DIR}/os_mgmt_auto)
file(MAKE_DIRECTORY ${MCUMGR_GRP_OS_INFO_BUILD_DATE_TIME_DIR})
Expand Down
10 changes: 10 additions & 0 deletions subsys/mgmt/mcumgr/grp/os_mgmt/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@ config MCUMGR_GRP_OS_INFO_BUILD_DATE_TIME

endif

if BOOTLOADER_MCUBOOT

config MCUMGR_GRP_OS_BOOTLOADER_INFO
bool "Bootloader information"
help
Allows to query MCUmgr about bootloader used by device and various bootloader
parameters.

endif # BOOTLOADER_MCUBOOT

module = MCUMGR_GRP_OS
module-str = mcumgr_grp_os
source "subsys/logging/Kconfig.template.log_config"
Expand Down
70 changes: 69 additions & 1 deletion subsys/mgmt/mcumgr/grp/os_mgmt/src/os_mgmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
#endif

#ifdef CONFIG_MCUMGR_GRP_OS_INFO
#if defined(CONFIG_MCUMGR_GRP_OS_INFO) || defined(CONFIG_MCUMGR_GRP_OS_BOOTLOADER_INFO)
#include <stdio.h>
#include <version.h>
#if defined(CONFIG_MCUMGR_GRP_OS_INFO)
#include <os_mgmt_processor.h>
#endif
#if defined(CONFIG_MCUMGR_GRP_OS_BOOTLOADER_INFO)
#include <bootutil/boot_status.h>
#endif
#include <mgmt/mcumgr/util/zcbor_bulk.h>
#if defined(CONFIG_NET_HOSTNAME_ENABLE)
#include <zephyr/net/hostname.h>
Expand Down Expand Up @@ -370,6 +375,64 @@ os_mgmt_mcumgr_params(struct smp_streamer *ctxt)
}
#endif

#if defined(CONFIG_MCUMGR_GRP_OS_BOOTLOADER_INFO)

#if IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_SINGLE_APP)
#define BOOTLOADER_MODE MCUBOOT_MODE_SINGLE_SLOT
#elif IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_SWAP_SCRATCH)
#define BOOTLOADER_MODE MCUBOOT_MODE_SWAP_USING_SCRATCH
#elif IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_OVERWRITE_ONLY)
#define BOOTLOADER_MODE MCUBOOT_MODE_UPGRADE_ONLY
#elif IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_SWAP_WITHOUT_SCRATCH)
#define BOOTLOADER_MODE MCUBOOT_MODE_SWAP_USING_MOVE
#elif IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP)
#define BOOTLOADER_MODE MCUBOOT_MODE_DIRECT_XIP
#elif IS_ENABLED(CONFIG_MCUBOOT_BOOTLOADER_MODE_DIRECT_XIP_WITH_REVERT)
#define BOOTLOADER_MODE MCUBOOT_MODE_DIRECT_XIP_WITH_REVERT
#else
#define BOOTLOADER_MODE -1
#endif

static int
os_mgmt_bootloader_info(struct smp_streamer *ctxt)
{
zcbor_state_t *zse = ctxt->writer->zs;
zcbor_state_t *zsd = ctxt->reader->zs;
struct zcbor_string query = { 0 };
size_t decoded;
bool ok;

struct zcbor_map_decode_key_val bootloader_info[] = {
ZCBOR_MAP_DECODE_KEY_DECODER("query", zcbor_tstr_decode, &query),
};

if (zcbor_map_decode_bulk(zsd, bootloader_info, ARRAY_SIZE(bootloader_info), &decoded)) {
return MGMT_ERR_EINVAL;
}

/* If no parameter is recognized then just introduce the bootloader. */
if (decoded == 0) {
ok = zcbor_tstr_put_lit(zse, "bootloader") &&
zcbor_tstr_put_lit(zse, "MCUboot");
} else if (zcbor_map_decode_bulk_key_found(bootloader_info, ARRAY_SIZE(bootloader_info),
"query") &&
(sizeof("mode") - 1) == query.len &&
memcmp("mode", query.value, query.len) == 0) {

ok = zcbor_tstr_put_lit(zse, "mode") &&
zcbor_int32_put(zse, BOOTLOADER_MODE);
#if IS_ENABLED(MCUBOOT_BOOTLOADER_NO_DOWNGRADE)
ok = zcbor_tstr_put_lit(zse, "no-downgrade") &&
zcbor_bool_encode(zse, true);
#endif
} else {
return OS_MGMT_ERR_QUERY_YIELDS_NO_ANSWER;
}

return ok ? MGMT_ERR_EOK : MGMT_ERR_EMSGSIZE;
}
#endif

#ifdef CONFIG_MCUMGR_GRP_OS_INFO
/**
* Command handler: os info
Expand Down Expand Up @@ -733,6 +796,11 @@ static const struct mgmt_handler os_mgmt_group_handlers[] = {
os_mgmt_info, NULL
},
#endif
#ifdef CONFIG_MCUMGR_GRP_OS_BOOTLOADER_INFO
[OS_MGMT_ID_BOOTLOADER_INFO] = {
os_mgmt_bootloader_info, NULL
},
#endif
};

#define OS_MGMT_GROUP_SZ ARRAY_SIZE(os_mgmt_group_handlers)
Expand Down

0 comments on commit cbe3d5f

Please sign in to comment.