kfsw-services/src/golden/golden.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_BOOT | ||
| 2 | |||
| 3 | #include <errno.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #include <psa/crypto.h> | ||
| 7 | #include <zephyr/devicetree.h> | ||
| 8 | #include <zephyr/kernel.h> | ||
| 9 | #include <bootutil/image.h> | ||
| 10 | #include <zephyr/storage/flash_map.h> | ||
| 11 | #include <zephyr/sys/byteorder.h> | ||
| 12 | #include <zephyr/sys/crc.h> | ||
| 13 | |||
| 14 | #include <kfsw/services/boot.h> | ||
| 15 | #if CONFIG_KFSW_BOOT_BUDGET | ||
| 16 | #include <kfsw/services/boot_budget.h> | ||
| 17 | #endif | ||
| 18 | #include <kfsw/services/fwu.h> | ||
| 19 | #include <kfsw/services/golden.h> | ||
| 20 | #include <kfsw/services/log.h> | ||
| 21 | |||
| 22 | #if CONFIG_KFSW_CSP_HMAC_REQUIRE | ||
| 23 | #warning "A recovery image that requires authentication cannot be reached without the key" | ||
| 24 | #endif | ||
| 25 | |||
| 26 | #define GOLDEN_NODE DT_CHOSEN(kfsw_golden_partition) | ||
| 27 | #define GOLDEN_PRESENT (DT_NODE_EXISTS(GOLDEN_NODE) && !IS_ENABLED(CONFIG_KFSW_GOLDEN_IMAGE)) | ||
| 28 | |||
| 29 | #if GOLDEN_PRESENT | ||
| 30 | #define GOLDEN_ID DT_FIXED_PARTITION_ID(GOLDEN_NODE) | ||
| 31 | #define GOLDEN_SIZE DT_REG_SIZE(GOLDEN_NODE) | ||
| 32 | |||
| 33 | /* Read in pieces: the partition is larger than anything worth putting on a stack. */ | ||
| 34 | #define GOLDEN_CHUNK 256U | ||
| 35 | #define GOLDEN_SHA256_SIZE 32U | ||
| 36 | |||
| 37 | static K_MUTEX_DEFINE(install_lock); | ||
| 38 | static struct kfsw_fwu_handle pending_cleanup; | ||
| 39 | #endif | ||
| 40 | |||
| 41 | 62 | bool kfsw_golden_present(void) | |
| 42 | { | ||
| 43 | #if GOLDEN_PRESENT | ||
| 44 | const struct flash_area *area; | ||
| 45 | uint8_t header[4]; | ||
| 46 | 62 | bool found = false; | |
| 47 | |||
| 48 | 62 | if (flash_area_open(GOLDEN_ID, &area) != 0) { | |
| 49 | ✗ | return false; | |
| 50 | } | ||
| 51 | 62 | if (flash_area_read(area, 0U, header, sizeof(header)) == 0) { | |
| 52 | 62 | found = sys_get_le32(header) == IMAGE_MAGIC; | |
| 53 | } | ||
| 54 | 62 | flash_area_close(area); | |
| 55 | 62 | return found; | |
| 56 | #else | ||
| 57 | return false; | ||
| 58 | #endif | ||
| 59 | } | ||
| 60 | |||
| 61 | #if GOLDEN_PRESENT | ||
| 62 | 14 | static int abort_install(const struct kfsw_fwu_handle *handle, struct kfsw_fwu_status *status) | |
| 63 | { | ||
| 64 | 14 | int result = kfsw_fwu_session_abort(handle, status); | |
| 65 | |||
| 66 | 14 | if (result == 0 || result == -ESTALE || result == -EACCES) { | |
| 67 | 6 | if (pending_cleanup.generation == handle->generation) { | |
| 68 | 6 | pending_cleanup = (struct kfsw_fwu_handle){0}; | |
| 69 | } | ||
| 70 | 6 | return 0; | |
| 71 | } | ||
| 72 | 8 | pending_cleanup = *handle; | |
| 73 | 8 | kfsw_log_error("Recovery image cleanup failed (%d)", result); | |
| 74 | 8 | return result; | |
| 75 | } | ||
| 76 | |||
| 77 | /* Where the TLV area starts, after the header, the body and the protected TLVs | ||
| 78 | that the image's SHA-256 covers. */ | ||
| 79 | 34 | static int hashed_size(const struct flash_area *area, uint32_t *size) | |
| 80 | { | ||
| 81 | struct image_header header; | ||
| 82 | uint32_t total; | ||
| 83 | 34 | int result = flash_area_read(area, 0U, &header, sizeof(header)); | |
| 84 | |||
| 85 | 34 | if (result != 0) { | |
| 86 | ✗ | return result; | |
| 87 | } | ||
| 88 | 34 | total = (uint32_t)sys_le16_to_cpu(header.ih_hdr_size) + | |
| 89 | 34 | sys_le16_to_cpu(header.ih_protect_tlv_size); | |
| 90 | 34 | if ((sys_le32_to_cpu(header.ih_magic) != IMAGE_MAGIC) || (total > GOLDEN_SIZE) || | |
| 91 | 32 | (sys_le32_to_cpu(header.ih_img_size) > GOLDEN_SIZE - total)) { | |
| 92 | 4 | return -EBADMSG; | |
| 93 | } | ||
| 94 | 30 | *size = total + sys_le32_to_cpu(header.ih_img_size); | |
| 95 | 30 | return 0; | |
| 96 | } | ||
| 97 | |||
| 98 | 30 | static int expected_digest(const struct flash_area *area, uint32_t offset, uint8_t *digest) | |
| 99 | { | ||
| 100 | struct image_tlv_info info; | ||
| 101 | struct image_tlv tlv; | ||
| 102 | uint32_t end; | ||
| 103 | int result; | ||
| 104 | |||
| 105 | 30 | if (GOLDEN_SIZE - offset < sizeof(info)) { | |
| 106 | 2 | return -EBADMSG; | |
| 107 | } | ||
| 108 | 28 | result = flash_area_read(area, offset, &info, sizeof(info)); | |
| 109 | 28 | if (result != 0) { | |
| 110 | ✗ | return result; | |
| 111 | } | ||
| 112 | 28 | if ((sys_le16_to_cpu(info.it_magic) != IMAGE_TLV_INFO_MAGIC) || | |
| 113 | 26 | (sys_le16_to_cpu(info.it_tlv_tot) < sizeof(info)) || | |
| 114 | 26 | (sys_le16_to_cpu(info.it_tlv_tot) > GOLDEN_SIZE - offset)) { | |
| 115 | 4 | return -EBADMSG; | |
| 116 | } | ||
| 117 | 24 | end = offset + sys_le16_to_cpu(info.it_tlv_tot); | |
| 118 | 24 | offset += sizeof(info); | |
| 119 | 28 | while (end - offset >= sizeof(tlv)) { | |
| 120 | 24 | result = flash_area_read(area, offset, &tlv, sizeof(tlv)); | |
| 121 | 24 | if (result != 0) { | |
| 122 | ✗ | return result; | |
| 123 | } | ||
| 124 | 24 | offset += sizeof(tlv); | |
| 125 | 24 | if (sys_le16_to_cpu(tlv.it_len) > end - offset) { | |
| 126 | 2 | return -EBADMSG; | |
| 127 | } | ||
| 128 | 22 | if ((sys_le16_to_cpu(tlv.it_type) == IMAGE_TLV_SHA256) && | |
| 129 | 20 | (sys_le16_to_cpu(tlv.it_len) == GOLDEN_SHA256_SIZE)) { | |
| 130 | 18 | return flash_area_read(area, offset, digest, GOLDEN_SHA256_SIZE); | |
| 131 | } | ||
| 132 | 4 | offset += sys_le16_to_cpu(tlv.it_len); | |
| 133 | } | ||
| 134 | 4 | return -EBADMSG; | |
| 135 | } | ||
| 136 | |||
| 137 | /* The bootloader checks the signature only after the secondary slot has been | ||
| 138 | erased and rewritten. A damaged recovery image found here costs nothing; one | ||
| 139 | found by the bootloader costs a slot erase and a restart on every attempt. */ | ||
| 140 | 34 | static int check_image(const struct flash_area *area, uint8_t *chunk) | |
| 141 | { | ||
| 142 | 34 | psa_hash_operation_t hash = PSA_HASH_OPERATION_INIT; | |
| 143 | uint8_t expected[GOLDEN_SHA256_SIZE]; | ||
| 144 | uint8_t actual[GOLDEN_SHA256_SIZE]; | ||
| 145 | size_t actual_size; | ||
| 146 | uint32_t covered; | ||
| 147 | uint32_t offset; | ||
| 148 | 34 | int result = hashed_size(area, &covered); | |
| 149 | |||
| 150 | 34 | if (result == 0) { | |
| 151 | 30 | result = expected_digest(area, covered, expected); | |
| 152 | } | ||
| 153 | 34 | if (result != 0) { | |
| 154 | 16 | return result; | |
| 155 | } | ||
| 156 | 36 | if ((psa_crypto_init() != PSA_SUCCESS) || | |
| 157 | 18 | (psa_hash_setup(&hash, PSA_ALG_SHA_256) != PSA_SUCCESS)) { | |
| 158 | ✗ | return -EIO; | |
| 159 | } | ||
| 160 | 306 | for (offset = 0U; offset < covered; offset += GOLDEN_CHUNK) { | |
| 161 | 288 | size_t size = MIN(GOLDEN_CHUNK, covered - offset); | |
| 162 | |||
| 163 | 288 | result = flash_area_read(area, offset, chunk, size); | |
| 164 | 288 | if (result == 0 && psa_hash_update(&hash, chunk, size) != PSA_SUCCESS) { | |
| 165 | ✗ | result = -EIO; | |
| 166 | } | ||
| 167 | 288 | if (result != 0) { | |
| 168 | ✗ | (void)psa_hash_abort(&hash); | |
| 169 | ✗ | return result; | |
| 170 | } | ||
| 171 | } | ||
| 172 | 18 | if (psa_hash_finish(&hash, actual, sizeof(actual), &actual_size) != PSA_SUCCESS) { | |
| 173 | ✗ | (void)psa_hash_abort(&hash); | |
| 174 | ✗ | return -EIO; | |
| 175 | } | ||
| 176 | 18 | if ((actual_size != sizeof(actual)) || (memcmp(actual, expected, sizeof(actual)) != 0)) { | |
| 177 | 2 | return -EBADMSG; | |
| 178 | } | ||
| 179 | 16 | return 0; | |
| 180 | } | ||
| 181 | |||
| 182 | 36 | static int install_image(void) | |
| 183 | { | ||
| 184 | const struct flash_area *area; | ||
| 185 | uint8_t chunk[GOLDEN_CHUNK]; | ||
| 186 | 36 | const struct kfsw_fwu_owner owner = {.kind = KFSW_FWU_OWNER_LOCAL, .peer = 1U}; | |
| 187 | 36 | struct kfsw_fwu_image image = {.size = GOLDEN_SIZE}; | |
| 188 | 36 | struct kfsw_fwu_handle handle = {0}; | |
| 189 | struct kfsw_fwu_status status; | ||
| 190 | 36 | uint32_t crc = 0U; | |
| 191 | uint32_t offset; | ||
| 192 | int result; | ||
| 193 | |||
| 194 | 36 | if (!kfsw_golden_present()) { | |
| 195 | 2 | return -ENOENT; | |
| 196 | } | ||
| 197 | |||
| 198 | 34 | result = flash_area_open(GOLDEN_ID, &area); | |
| 199 | 34 | if (result != 0) { | |
| 200 | ✗ | return result; | |
| 201 | } | ||
| 202 | 34 | result = check_image(area, chunk); | |
| 203 | 34 | if (result != 0) { | |
| 204 | 18 | flash_area_close(area); | |
| 205 | 18 | kfsw_log_error("Recovery image failed its own SHA-256 (%d); not installed", result); | |
| 206 | 18 | return result; | |
| 207 | } | ||
| 208 | |||
| 209 | /* From the same bytes that will be written, so the update service checks | ||
| 210 | the copy and not the intention. */ | ||
| 211 | 272 | for (offset = 0U; offset < GOLDEN_SIZE; offset += GOLDEN_CHUNK) { | |
| 212 | 256 | result = flash_area_read(area, offset, chunk, GOLDEN_CHUNK); | |
| 213 | 256 | if (result != 0) { | |
| 214 | ✗ | flash_area_close(area); | |
| 215 | ✗ | return result; | |
| 216 | } | ||
| 217 | 256 | crc = crc32_ieee_update(crc, chunk, GOLDEN_CHUNK); | |
| 218 | } | ||
| 219 | |||
| 220 | 16 | image.crc32 = crc; | |
| 221 | 16 | result = kfsw_fwu_session_begin(&owner, &image, 0U, &handle, &status); | |
| 222 | 16 | if (result != 0) { | |
| 223 | 8 | flash_area_close(area); | |
| 224 | 8 | if (handle.generation != 0U) { | |
| 225 | 4 | (void)abort_install(&handle, &status); | |
| 226 | } | ||
| 227 | 8 | return result; | |
| 228 | } | ||
| 229 | |||
| 230 | 106 | for (offset = 0U; offset < GOLDEN_SIZE; offset += GOLDEN_CHUNK) { | |
| 231 | 100 | result = flash_area_read(area, offset, chunk, GOLDEN_CHUNK); | |
| 232 | 100 | if (result == 0) { | |
| 233 | 100 | result = kfsw_fwu_session_write(&handle, offset, chunk, GOLDEN_CHUNK, | |
| 234 | &status); | ||
| 235 | } | ||
| 236 | 100 | if (result != 0) { | |
| 237 | 2 | flash_area_close(area); | |
| 238 | 2 | (void)abort_install(&handle, &status); | |
| 239 | 2 | return result; | |
| 240 | } | ||
| 241 | } | ||
| 242 | 6 | flash_area_close(area); | |
| 243 | |||
| 244 | 6 | result = kfsw_fwu_session_verify(&handle, &status); | |
| 245 | 6 | if (result == 0) { | |
| 246 | 6 | result = kfsw_fwu_session_activate(&handle, &status); | |
| 247 | } | ||
| 248 | 6 | if (result != 0) { | |
| 249 | ✗ | (void)abort_install(&handle, &status); | |
| 250 | ✗ | return result; | |
| 251 | } | ||
| 252 | |||
| 253 | 6 | kfsw_log_warning("Recovery image installed; it boots at the next restart"); | |
| 254 | 6 | return 0; | |
| 255 | } | ||
| 256 | #endif | ||
| 257 | |||
| 258 | 38 | int kfsw_golden_install(void) | |
| 259 | { | ||
| 260 | #if GOLDEN_PRESENT && !CONFIG_KFSW_GOLDEN_IMAGE | ||
| 261 | struct kfsw_fwu_status status; | ||
| 262 | struct kfsw_fwu_handle handle; | ||
| 263 | 38 | int result = 0; | |
| 264 | |||
| 265 | /* Serialize the copy and retain failed cleanup across explicit install attempts. */ | ||
| 266 | 38 | k_mutex_lock(&install_lock, K_FOREVER); | |
| 267 | 38 | handle = pending_cleanup; | |
| 268 | 38 | if (handle.generation != 0U) { | |
| 269 | 8 | result = abort_install(&handle, &status); | |
| 270 | } | ||
| 271 | 38 | if (result == 0) { | |
| 272 | 36 | result = install_image(); | |
| 273 | } | ||
| 274 | 38 | k_mutex_unlock(&install_lock); | |
| 275 | 38 | return result; | |
| 276 | #else | ||
| 277 | return -ENODEV; | ||
| 278 | #endif | ||
| 279 | } | ||
| 280 | |||
| 281 | 6 | int kfsw_golden_check(enum kfsw_golden_outcome *outcome) | |
| 282 | { | ||
| 283 | 6 | if (outcome == NULL) { | |
| 284 | ✗ | return -EINVAL; | |
| 285 | } | ||
| 286 | 6 | *outcome = KFSW_GOLDEN_NOT_NEEDED; | |
| 287 | #if CONFIG_KFSW_BOOT_BUDGET && !CONFIG_KFSW_GOLDEN_IMAGE | ||
| 288 | uint8_t left; | ||
| 289 | uint8_t maximum; | ||
| 290 | int result; | ||
| 291 | |||
| 292 | 6 | kfsw_boot_budget_get(&left, &maximum); | |
| 293 | 6 | if (left == UINT8_MAX) { | |
| 294 | ✗ | return -ENODATA; | |
| 295 | } | ||
| 296 | 6 | if (left != 0U) { | |
| 297 | ✗ | return 0; | |
| 298 | } | ||
| 299 | 6 | if (!kfsw_golden_present()) { | |
| 300 | 2 | return -ENOENT; | |
| 301 | } | ||
| 302 | 4 | result = kfsw_golden_install(); | |
| 303 | 4 | if (result != 0) { | |
| 304 | 2 | return result; | |
| 305 | } | ||
| 306 | 2 | *outcome = KFSW_GOLDEN_INSTALLED; | |
| 307 | #endif | ||
| 308 | 2 | return 0; | |
| 309 | } | ||
| 310 |