Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Nordic Semiconductor ASA |
| 3 | * |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include <zephyr.h> |
| 8 | #include <drivers/flash.h> |
| 9 | #include <mgmt/mcumgr/zephyr_groups.h> |
| 10 | |
| 11 | #include <flash_map_backend/flash_map_backend.h> |
| 12 | #include <sysflash/sysflash.h> |
| 13 | |
| 14 | #include "bootutil/bootutil_log.h" |
| 15 | #include "../boot_serial/src/boot_serial_priv.h" |
| 16 | #include "../boot_serial/src/cbor_encode.h" |
| 17 | |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 18 | #include "bootutil/image.h" |
| 19 | |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 20 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 21 | |
Dominik Ermel | 0435d5d | 2021-08-16 15:58:15 +0000 | [diff] [blame^] | 22 | #ifdef CONFIG_BOOT_MGMT_CUSTOM_STORAGE_ERASE |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 23 | static int bs_custom_storage_erase(cbor_state_t *cs) |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 24 | { |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 25 | int rc; |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 26 | |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 27 | const struct flash_area *fa; |
Dominik Ermel | 97b4c79 | 2021-06-25 17:32:38 +0000 | [diff] [blame] | 28 | |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 29 | rc = flash_area_open(FLASH_AREA_ID(storage), &fa); |
Dominik Ermel | 97b4c79 | 2021-06-25 17:32:38 +0000 | [diff] [blame] | 30 | |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 31 | if (rc < 0) { |
| 32 | LOG_ERR("failed to open flash area"); |
| 33 | } else { |
| 34 | rc = flash_area_erase(fa, 0, FLASH_AREA_SIZE(storage)); |
Dominik Ermel | 97b4c79 | 2021-06-25 17:32:38 +0000 | [diff] [blame] | 35 | if (rc < 0) { |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 36 | LOG_ERR("failed to erase flash area"); |
Dominik Ermel | 97b4c79 | 2021-06-25 17:32:38 +0000 | [diff] [blame] | 37 | } |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 38 | flash_area_close(fa); |
| 39 | } |
| 40 | if (rc == 0) { |
| 41 | rc = MGMT_ERR_OK; |
| 42 | } else { |
| 43 | rc = MGMT_ERR_EUNKNOWN; |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | map_start_encode(cs, 10); |
| 47 | tstrx_put(cs, "rc"); |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 48 | uintx32_put(cs, rc); |
Dominik Ermel | 3d51e43 | 2021-06-25 17:29:50 +0000 | [diff] [blame] | 49 | map_end_encode(cs, 10); |
| 50 | |
| 51 | return rc; |
| 52 | } |
Dominik Ermel | 0435d5d | 2021-08-16 15:58:15 +0000 | [diff] [blame^] | 53 | #endif |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 54 | |
Andrzej Puzdrowski | 420ad9a | 2021-07-29 16:22:52 +0200 | [diff] [blame] | 55 | #ifdef MCUBOOT_MGMT_CUSTOM_IMG_LIST |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 56 | static int custom_img_status(int image_index, uint32_t slot,char *buffer, |
| 57 | ssize_t len) |
| 58 | { |
| 59 | uint32_t area_id; |
| 60 | struct flash_area const *fap; |
| 61 | struct image_header hdr; |
| 62 | int rc; |
| 63 | int img_install_stat = 0; |
| 64 | |
| 65 | area_id = flash_area_id_from_multi_image_slot(image_index, slot); |
| 66 | |
| 67 | rc = flash_area_open(area_id, &fap); |
| 68 | if (rc) { |
| 69 | return rc; |
| 70 | } |
| 71 | |
| 72 | rc = flash_area_read(fap, 0, &hdr, sizeof(hdr)); |
| 73 | if (rc) { |
| 74 | goto func_end; |
| 75 | } |
| 76 | |
| 77 | if (hdr.ih_magic == IMAGE_MAGIC) { |
| 78 | snprintf(buffer, len, "ver=%d.%d.%d.%d,install_stat=%d", |
| 79 | hdr.ih_ver.iv_major, |
| 80 | hdr.ih_ver.iv_minor, |
| 81 | hdr.ih_ver.iv_revision, |
| 82 | hdr.ih_ver.iv_build_num, |
| 83 | img_install_stat); |
| 84 | } else { |
| 85 | rc = 1; |
| 86 | } |
| 87 | |
| 88 | func_end: |
| 89 | flash_area_close(fap); |
| 90 | return rc; |
| 91 | } |
| 92 | |
| 93 | static int bs_custom_img_list(cbor_state_t *cs) |
| 94 | { |
| 95 | int rc = 0; |
| 96 | char tmpbuf[64]; /* Buffer should fit version and flags */ |
| 97 | |
| 98 | map_start_encode(cs, 10); |
| 99 | |
| 100 | for (int img = 0; img < MCUBOOT_IMAGE_NUMBER; img++) { |
| 101 | for (int slot = 0; slot < 2; slot++) { |
| 102 | rc = custom_img_status(img, slot, tmpbuf, sizeof(tmpbuf)); |
| 103 | |
| 104 | intx32_put(cs, img * 2 + slot + 1); |
| 105 | if (rc == 0) { |
| 106 | tstrx_put_term(cs, tmpbuf); |
| 107 | } else { |
| 108 | tstrx_put_term(cs, ""); |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | tstrx_put(cs, "rc"); |
| 114 | uintx32_put(cs, MGMT_ERR_OK); |
| 115 | map_end_encode(cs, 10); |
| 116 | |
| 117 | return rc; |
| 118 | } |
| 119 | |
| 120 | #ifndef ZEPHYR_MGMT_GRP_BASIC_CMD_IMAGE_LIST |
| 121 | #define ZEPHYR_MGMT_GRP_BASIC_CMD_IMAGE_LIST 1 |
| 122 | #endif |
Andrzej Puzdrowski | 420ad9a | 2021-07-29 16:22:52 +0200 | [diff] [blame] | 123 | #endif /*MCUBOOT_MGMT_CUSTOM_IMG_LIST*/ |
| 124 | |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 125 | int bs_peruser_system_specific(const struct nmgr_hdr *hdr, const char *buffer, |
| 126 | int len, cbor_state_t *cs) |
| 127 | { |
| 128 | int mgmt_rc = MGMT_ERR_ENOTSUP; |
| 129 | |
| 130 | if (hdr->nh_group == ZEPHYR_MGMT_GRP_BASE) { |
| 131 | if (hdr->nh_op == NMGR_OP_WRITE) { |
Dominik Ermel | 0435d5d | 2021-08-16 15:58:15 +0000 | [diff] [blame^] | 132 | #ifdef CONFIG_BOOT_MGMT_CUSTOM_STORAGE_ERASE |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 133 | if (hdr->nh_id == ZEPHYR_MGMT_GRP_BASIC_CMD_ERASE_STORAGE) { |
| 134 | mgmt_rc = bs_custom_storage_erase(cs); |
| 135 | } |
Dominik Ermel | 0435d5d | 2021-08-16 15:58:15 +0000 | [diff] [blame^] | 136 | #endif |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 137 | } else if (hdr->nh_op == NMGR_OP_READ) { |
Andrzej Puzdrowski | 420ad9a | 2021-07-29 16:22:52 +0200 | [diff] [blame] | 138 | #ifdef MCUBOOT_MGMT_CUSTOM_IMG_LIST |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 139 | if (hdr->nh_id == ZEPHYR_MGMT_GRP_BASIC_CMD_IMAGE_LIST) { |
| 140 | mgmt_rc = bs_custom_img_list(cs); |
| 141 | } |
Andrzej Puzdrowski | 420ad9a | 2021-07-29 16:22:52 +0200 | [diff] [blame] | 142 | #endif |
Andrzej Puzdrowski | 1b62cf2 | 2021-07-28 17:21:19 +0200 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
| 146 | if (mgmt_rc == MGMT_ERR_ENOTSUP) { |
| 147 | map_start_encode(cs, 10); |
| 148 | tstrx_put(cs, "rc"); |
| 149 | uintx32_put(cs, mgmt_rc); |
| 150 | map_end_encode(cs, 10); |
| 151 | } |
| 152 | |
| 153 | return MGMT_ERR_OK; |
| 154 | } |