|
| 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