Skip to content

Commit a3094d1

Browse files
committed
mgmt: mcumgr: grp: os: Add active b0 slot bootloader info query
Allows querying the active b0 slot via MCUmgr so that the correct update image can be loaded to a device Signed-off-by: Jamie McCrae <[email protected]>
1 parent aca0c1c commit a3094d1

File tree

9 files changed

+140
-0
lines changed

9 files changed

+140
-0
lines changed

subsys/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,4 @@ add_subdirectory(suit)
6767
add_subdirectory_ifdef(CONFIG_MGMT_SUITFU mgmt/suitfu)
6868
add_subdirectory_ifdef(CONFIG_DULT dult)
6969
add_subdirectory_ifdef(CONFIG_NRF_COMPRESS nrf_compress)
70+
add_subdirectory(mgmt/mcumgr)

subsys/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rsource "nrf_rpc/Kconfig"
2828
rsource "zigbee/Kconfig"
2929
rsource "mgmt/fmfu/Kconfig"
3030
rsource "mgmt/suitfu/Kconfig"
31+
rsource "mgmt/mcumgr/Kconfig"
3132
rsource "caf/Kconfig"
3233
rsource "ieee802154/Kconfig"
3334
rsource "dm/Kconfig"

subsys/mgmt/mcumgr/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
add_subdirectory(grp)

subsys/mgmt/mcumgr/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
menu "Additional MCUmgr configuration"
8+
9+
rsource "grp/Kconfig"
10+
11+
endmenu
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
add_subdirectory_ifdef(CONFIG_MCUMGR_GRP_OS os_mgmt)

subsys/mgmt/mcumgr/grp/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
menu "Additional MCUmgr group configuration"
8+
9+
rsource "os_mgmt/Kconfig"
10+
11+
endmenu
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
if(CONFIG_MCUMGR_GRP_OS_BOOTLOADER_INFO_B0_ACTIVE_SLOT)
8+
zephyr_library_amend()
9+
zephyr_library_sources(src/os_mgmt_b0_active_slot.c)
10+
endif()
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
menu "Additional MCUmgr OS group management functionality"
8+
9+
config MCUMGR_GRP_OS_BOOTLOADER_INFO_B0_ACTIVE_SLOT
10+
bool "Bootloader info query for active b0 slot"
11+
depends on MCUMGR_GRP_OS_BOOTLOADER_INFO
12+
depends on BOOTLOADER_MCUBOOT
13+
depends on MCUBOOT_MCUBOOT_IMAGE_NUMBER != -1
14+
depends on FW_INFO
15+
depends on FW_INFO_API
16+
depends on MCUMGR_MGMT_NOTIFICATION_HOOKS
17+
depends on MCUMGR_GRP_OS_BOOTLOADER_INFO_HOOK
18+
help
19+
Enables a bootloader info command for `active_b0_slot` which will return the active b0
20+
image slot number, and can be used to determine which update image should be loaded.
21+
22+
endmenu
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (c) 2024 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
*/
6+
7+
#include <zcbor_common.h>
8+
#include <zcbor_encode.h>
9+
#include <pm_config.h>
10+
#include <fw_info.h>
11+
#include <zephyr/mgmt/mcumgr/mgmt/callbacks.h>
12+
#include <zephyr/mgmt/mcumgr/grp/os_mgmt/os_mgmt.h>
13+
14+
static enum mgmt_cb_return bootloader_info_hook(uint32_t event, enum mgmt_cb_return prev_status,
15+
int32_t *rc, uint16_t *group, bool *abort_more,
16+
void *data, size_t data_size)
17+
{
18+
struct os_mgmt_bootloader_info_data *bootloader_info_data =
19+
(struct os_mgmt_bootloader_info_data *)data;
20+
21+
if (event != MGMT_EVT_OP_OS_MGMT_BOOTLOADER_INFO || data_size !=
22+
sizeof(*bootloader_info_data)) {
23+
*rc = MGMT_ERR_EUNKNOWN;
24+
return MGMT_CB_ERROR_RC;
25+
}
26+
27+
if (*(bootloader_info_data->decoded) >= 1 && (sizeof("active_b0_slot") - 1) ==
28+
bootloader_info_data->query->len && memcmp("active_b0_slot",
29+
bootloader_info_data->query->value,
30+
bootloader_info_data->query->len) == 0) {
31+
const struct fw_info *s0_info = fw_info_find(PM_S0_ADDRESS);
32+
const struct fw_info *s1_info = fw_info_find(PM_S1_ADDRESS);
33+
34+
if (s0_info || s1_info) {
35+
uint32_t active_slot;
36+
bool ok;
37+
38+
if (!s1_info || (s0_info && s0_info->version >= s1_info->version)) {
39+
active_slot = 0;
40+
} else if (!s0_info || (s1_info && s1_info->version > s0_info->version)) {
41+
active_slot = 1;
42+
}
43+
44+
ok = zcbor_tstr_put_lit(bootloader_info_data->zse, "active") &&
45+
zcbor_uint32_put(bootloader_info_data->zse, active_slot);
46+
*rc = (ok ? MGMT_ERR_EOK : MGMT_ERR_EMSGSIZE);
47+
return MGMT_CB_ERROR_RC;
48+
}
49+
50+
/* Return response not valid error when active slot cannot be determined */
51+
*group = MGMT_GROUP_ID_OS;
52+
*rc = OS_MGMT_ERR_QUERY_RESPONSE_VALUE_NOT_VALID;
53+
return MGMT_CB_ERROR_ERR;
54+
}
55+
56+
return MGMT_CB_OK;
57+
}
58+
59+
static struct mgmt_callback cmd_bootloader_info_cb = {
60+
.callback = bootloader_info_hook,
61+
.event_id = MGMT_EVT_OP_OS_MGMT_BOOTLOADER_INFO,
62+
};
63+
64+
static int os_mgmt_register_bootloader_info_hook_b0_active_slot(void)
65+
{
66+
mgmt_callback_register(&cmd_bootloader_info_cb);
67+
return 0;
68+
}
69+
70+
SYS_INIT(os_mgmt_register_bootloader_info_hook_b0_active_slot, APPLICATION, 0);

0 commit comments

Comments
 (0)