Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
| 4 | * Copyright (c) 2017-2019 Linaro LTD |
| 5 | * Copyright (c) 2016-2019 JUUL Labs |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 6 | * Copyright (c) 2019-2021 Arm Limited |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 7 | * Copyright (c) 2020 Nordic Semiconductor ASA |
| 8 | * |
| 9 | * Original license: |
| 10 | * |
| 11 | * Licensed to the Apache Software Foundation (ASF) under one |
| 12 | * or more contributor license agreements. See the NOTICE file |
| 13 | * distributed with this work for additional information |
| 14 | * regarding copyright ownership. The ASF licenses this file |
| 15 | * to you under the Apache License, Version 2.0 (the |
| 16 | * "License"); you may not use this file except in compliance |
| 17 | * with the License. You may obtain a copy of the License at |
| 18 | * |
| 19 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | * |
| 21 | * Unless required by applicable law or agreed to in writing, |
| 22 | * software distributed under the License is distributed on an |
| 23 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 24 | * KIND, either express or implied. See the License for the |
| 25 | * specific language governing permissions and limitations |
| 26 | * under the License. |
| 27 | */ |
| 28 | |
Andrzej Puzdrowski | 14ef576 | 2020-12-11 17:17:59 +0100 | [diff] [blame] | 29 | /** |
| 30 | * @file |
| 31 | * @brief Public MCUBoot interface API implementation |
| 32 | * |
| 33 | * This file contains API implementation which can be combined with |
| 34 | * the application in order to interact with the MCUBoot bootloader. |
| 35 | * This file contains shared code-base betwen MCUBoot and the application |
| 36 | * which controls DFU process. |
| 37 | */ |
| 38 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 39 | #include <string.h> |
| 40 | #include <inttypes.h> |
| 41 | #include <stddef.h> |
| 42 | |
| 43 | #include "sysflash/sysflash.h" |
| 44 | #include "flash_map_backend/flash_map_backend.h" |
| 45 | |
| 46 | #include "bootutil/image.h" |
| 47 | #include "bootutil/bootutil_public.h" |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 48 | #include "bootutil/bootutil_log.h" |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 49 | #include "swap_status.h" |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 50 | #ifdef MCUBOOT_ENC_IMAGES |
Andrzej Puzdrowski | 360763d | 2021-02-04 18:01:01 +0100 | [diff] [blame] | 51 | #include "bootutil/enc_key_public.h" |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 52 | #endif |
| 53 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 54 | #include "bootutil/boot_public_hooks.h" |
| 55 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_MCUBOOT |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 57 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 58 | #else |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 59 | BOOT_LOG_MODULE_REGISTER(mcuboot_util); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 60 | #endif |
| 61 | |
| 62 | const uint32_t boot_img_magic[] = { |
| 63 | 0xf395c277, |
| 64 | 0x7fefd260, |
| 65 | 0x0f505235, |
| 66 | 0x8079b62c, |
| 67 | }; |
| 68 | |
| 69 | #define BOOT_MAGIC_ARR_SZ \ |
| 70 | (sizeof boot_img_magic / sizeof boot_img_magic[0]) |
| 71 | |
| 72 | struct boot_swap_table { |
| 73 | uint8_t magic_primary_slot; |
| 74 | uint8_t magic_secondary_slot; |
| 75 | uint8_t image_ok_primary_slot; |
| 76 | uint8_t image_ok_secondary_slot; |
| 77 | uint8_t copy_done_primary_slot; |
| 78 | |
| 79 | uint8_t swap_type; |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * This set of tables maps image trailer contents to swap operation type. |
| 84 | * When searching for a match, these tables must be iterated sequentially. |
| 85 | * |
| 86 | * NOTE: the table order is very important. The settings in the secondary |
| 87 | * slot always are priority to the primary slot and should be located |
| 88 | * earlier in the table. |
| 89 | * |
| 90 | * The table lists only states where there is action needs to be taken by |
| 91 | * the bootloader, as in starting/finishing a swap operation. |
| 92 | */ |
| 93 | static const struct boot_swap_table boot_swap_tables[] = { |
| 94 | { |
| 95 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 96 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 97 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 98 | .image_ok_secondary_slot = BOOT_FLAG_UNSET, |
| 99 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 100 | .swap_type = BOOT_SWAP_TYPE_TEST, |
| 101 | }, |
| 102 | { |
| 103 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 104 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 105 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 106 | .image_ok_secondary_slot = BOOT_FLAG_SET, |
| 107 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 108 | .swap_type = BOOT_SWAP_TYPE_PERM, |
| 109 | }, |
| 110 | { |
| 111 | .magic_primary_slot = BOOT_MAGIC_GOOD, |
| 112 | .magic_secondary_slot = BOOT_MAGIC_UNSET, |
| 113 | .image_ok_primary_slot = BOOT_FLAG_UNSET, |
| 114 | .image_ok_secondary_slot = BOOT_FLAG_ANY, |
| 115 | .copy_done_primary_slot = BOOT_FLAG_SET, |
| 116 | .swap_type = BOOT_SWAP_TYPE_REVERT, |
| 117 | }, |
| 118 | }; |
| 119 | |
| 120 | #define BOOT_SWAP_TABLES_COUNT \ |
| 121 | (sizeof boot_swap_tables / sizeof boot_swap_tables[0]) |
| 122 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 123 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 124 | static int |
| 125 | boot_magic_decode(const uint32_t *magic) |
| 126 | { |
| 127 | if (memcmp(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
| 128 | return BOOT_MAGIC_GOOD; |
| 129 | } |
| 130 | return BOOT_MAGIC_BAD; |
| 131 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 132 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 133 | |
| 134 | static int |
| 135 | boot_flag_decode(uint8_t flag) |
| 136 | { |
| 137 | if (flag != BOOT_FLAG_SET) { |
| 138 | return BOOT_FLAG_BAD; |
| 139 | } |
| 140 | return BOOT_FLAG_SET; |
| 141 | } |
| 142 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 143 | #ifndef MCUBOOT_SWAP_USING_STATUS |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 144 | static inline uint32_t |
| 145 | boot_magic_off(const struct flash_area *fap) |
| 146 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 147 | return flash_area_get_size(fap) - BOOT_MAGIC_SZ; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | static inline uint32_t |
| 151 | boot_image_ok_off(const struct flash_area *fap) |
| 152 | { |
| 153 | return boot_magic_off(fap) - BOOT_MAX_ALIGN; |
| 154 | } |
| 155 | |
| 156 | static inline uint32_t |
| 157 | boot_copy_done_off(const struct flash_area *fap) |
| 158 | { |
| 159 | return boot_image_ok_off(fap) - BOOT_MAX_ALIGN; |
| 160 | } |
| 161 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 162 | uint32_t |
| 163 | boot_swap_info_off(const struct flash_area *fap) |
| 164 | { |
| 165 | return boot_copy_done_off(fap) - BOOT_MAX_ALIGN; |
| 166 | } |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 167 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * Determines if a status source table is satisfied by the specified magic |
| 171 | * code. |
| 172 | * |
| 173 | * @param tbl_val A magic field from a status source table. |
| 174 | * @param val The magic value in a trailer, encoded as a |
| 175 | * BOOT_MAGIC_[...]. |
| 176 | * |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 177 | * @return true - if the two values are compatible; |
| 178 | * false - otherwise. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 179 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 180 | bool boot_magic_compatible_check(uint8_t tbl_val, uint8_t val) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 181 | { |
| 182 | switch (tbl_val) { |
| 183 | case BOOT_MAGIC_ANY: |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 184 | return true; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 185 | |
| 186 | case BOOT_MAGIC_NOTGOOD: |
| 187 | return val != BOOT_MAGIC_GOOD; |
| 188 | |
| 189 | default: |
| 190 | return tbl_val == val; |
| 191 | } |
| 192 | } |
| 193 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 194 | bool bootutil_buffer_is_filled(const void *buffer, uint8_t fill, size_t len) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 195 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 196 | uint8_t *p; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 197 | |
| 198 | if (buffer == NULL || len == 0) { |
| 199 | return false; |
| 200 | } |
| 201 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 202 | for (p = (uint8_t *)buffer; len-- > 0; p++) { |
| 203 | if (*p != fill) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return true; |
| 209 | } |
| 210 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 211 | bool bootutil_buffer_is_erased(const struct flash_area *area, |
| 212 | const void *buffer, size_t len) |
| 213 | { |
| 214 | uint8_t erased_val; |
| 215 | |
| 216 | if (area == NULL) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | erased_val = flash_area_erased_val(area); |
| 221 | |
| 222 | return bootutil_buffer_is_filled(buffer, erased_val, len); |
| 223 | } |
| 224 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 225 | static int |
| 226 | boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off) |
| 227 | { |
| 228 | int rc; |
| 229 | |
| 230 | rc = flash_area_read(fap, off, flag, sizeof *flag); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 231 | if (rc != 0) { |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 232 | return BOOT_EFLASH; |
| 233 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 234 | if (*flag == flash_area_erased_val(fap)) { |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 235 | *flag = BOOT_FLAG_UNSET; |
| 236 | } else { |
| 237 | *flag = boot_flag_decode(*flag); |
| 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 243 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 244 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 245 | static inline int |
| 246 | boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done) |
| 247 | { |
| 248 | return boot_read_flag(fap, copy_done, boot_copy_done_off(fap)); |
| 249 | } |
| 250 | |
| 251 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 252 | int |
| 253 | boot_read_swap_state(const struct flash_area *fap, |
| 254 | struct boot_swap_state *state) |
| 255 | { |
| 256 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
| 257 | uint32_t off; |
| 258 | uint8_t swap_info; |
| 259 | int rc; |
| 260 | |
| 261 | off = boot_magic_off(fap); |
| 262 | rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 263 | if (rc != 0) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 264 | return BOOT_EFLASH; |
| 265 | } |
| 266 | if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) { |
| 267 | state->magic = BOOT_MAGIC_UNSET; |
| 268 | } else { |
| 269 | state->magic = boot_magic_decode(magic); |
| 270 | } |
| 271 | |
| 272 | off = boot_swap_info_off(fap); |
| 273 | rc = flash_area_read(fap, off, &swap_info, sizeof swap_info); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 274 | if (rc != 0) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 275 | return BOOT_EFLASH; |
| 276 | } |
| 277 | |
| 278 | /* Extract the swap type and image number */ |
| 279 | state->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
| 280 | state->image_num = BOOT_GET_IMAGE_NUM(swap_info); |
| 281 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 282 | if (swap_info == flash_area_erased_val(fap) || |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 283 | state->swap_type > BOOT_SWAP_TYPE_REVERT) { |
| 284 | state->swap_type = BOOT_SWAP_TYPE_NONE; |
| 285 | state->image_num = 0; |
| 286 | } |
| 287 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 288 | rc = boot_read_copy_done(fap, &state->copy_done); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 289 | if (rc != 0) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 290 | return BOOT_EFLASH; |
| 291 | } |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 292 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 293 | return boot_read_image_ok(fap, &state->image_ok); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 296 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
| 297 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 298 | int |
| 299 | boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state) |
| 300 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 301 | const struct flash_area *fap = NULL; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 302 | int rc; |
| 303 | |
| 304 | rc = flash_area_open(flash_area_id, &fap); |
| 305 | if (rc != 0) { |
| 306 | return BOOT_EFLASH; |
| 307 | } |
| 308 | |
| 309 | rc = boot_read_swap_state(fap, state); |
| 310 | flash_area_close(fap); |
| 311 | return rc; |
| 312 | } |
| 313 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 314 | #ifndef MCUBOOT_SWAP_USING_STATUS |
| 315 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 316 | int |
| 317 | boot_write_magic(const struct flash_area *fap) |
| 318 | { |
| 319 | uint32_t off; |
| 320 | int rc; |
| 321 | |
| 322 | off = boot_magic_off(fap); |
| 323 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 324 | BOOT_LOG_DBG("writing magic; fa_id=%u off=0x%" PRIx32 |
| 325 | " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap), |
| 326 | off, flash_area_get_off(fap) + off); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 327 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
| 328 | if (rc != 0) { |
| 329 | return BOOT_EFLASH; |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Write trailer data; status bytes, swap_size, etc |
| 337 | * |
| 338 | * @returns 0 on success, != 0 on error. |
| 339 | */ |
Dominik Ermel | a7f9e9f | 2021-03-24 17:31:57 +0000 | [diff] [blame] | 340 | int |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 341 | boot_write_trailer(const struct flash_area *fap, uint32_t off, |
| 342 | const uint8_t *inbuf, uint8_t inlen) |
| 343 | { |
| 344 | uint8_t buf[BOOT_MAX_ALIGN]; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 345 | size_t align; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 346 | uint8_t erased_val; |
| 347 | int rc; |
| 348 | |
| 349 | align = flash_area_align(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 350 | if (align == 0u) { |
| 351 | return BOOT_EFLASH; |
| 352 | } |
Dominik Ermel | 4828162 | 2021-03-24 18:04:54 +0000 | [diff] [blame] | 353 | align = (inlen + align - 1) & ~(align - 1); |
| 354 | if (align > BOOT_MAX_ALIGN) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 355 | return -1; |
| 356 | } |
| 357 | erased_val = flash_area_erased_val(fap); |
Dominik Ermel | 4828162 | 2021-03-24 18:04:54 +0000 | [diff] [blame] | 358 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 359 | memcpy(buf, inbuf, inlen); |
| 360 | memset(&buf[inlen], erased_val, align - inlen); |
| 361 | |
| 362 | rc = flash_area_write(fap, off, buf, align); |
| 363 | if (rc != 0) { |
| 364 | return BOOT_EFLASH; |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 370 | #endif /* !MCUBOOT_SWAP_USING_STATUS */ |
| 371 | |
Dominik Ermel | a7f9e9f | 2021-03-24 17:31:57 +0000 | [diff] [blame] | 372 | int |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 373 | boot_write_trailer_flag(const struct flash_area *fap, uint32_t off, |
| 374 | uint8_t flag_val) |
| 375 | { |
| 376 | const uint8_t buf[1] = { flag_val }; |
| 377 | return boot_write_trailer(fap, off, buf, 1); |
| 378 | } |
| 379 | |
| 380 | int |
| 381 | boot_write_image_ok(const struct flash_area *fap) |
| 382 | { |
| 383 | uint32_t off; |
| 384 | |
| 385 | off = boot_image_ok_off(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 386 | BOOT_LOG_DBG("writing image_ok; fa_id=%u off=0x%" PRIx32 |
| 387 | " (0x%" PRIx32 ")", (unsigned)flash_area_get_id(fap), |
| 388 | off, flash_area_get_off(fap) + off); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 389 | return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); |
| 390 | } |
| 391 | |
Andrzej Puzdrowski | 4700b80 | 2020-12-09 14:55:36 +0100 | [diff] [blame] | 392 | int |
| 393 | boot_read_image_ok(const struct flash_area *fap, uint8_t *image_ok) |
| 394 | { |
| 395 | return boot_read_flag(fap, image_ok, boot_image_ok_off(fap)); |
| 396 | } |
| 397 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 398 | /** |
| 399 | * Writes the specified value to the `swap-type` field of an image trailer. |
| 400 | * This value is persisted so that the boot loader knows what swap operation to |
| 401 | * resume in case of an unexpected reset. |
| 402 | */ |
| 403 | int |
| 404 | boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 405 | uint8_t image_num) |
| 406 | { |
| 407 | uint32_t off; |
| 408 | uint8_t swap_info; |
| 409 | |
| 410 | BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type); |
| 411 | off = boot_swap_info_off(fap); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 412 | BOOT_LOG_DBG("writing swap_info; fa_id=%u off=0x%" PRIx32 |
| 413 | " (0x%" PRIx32 "), swap_type=0x%x image_num=0x%x", |
| 414 | (unsigned)flash_area_get_id(fap), off, |
| 415 | flash_area_get_off(fap) + off, |
| 416 | (unsigned)swap_type, (unsigned)image_num); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 417 | return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1); |
| 418 | } |
| 419 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 420 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
| 421 | BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, image_ok=0x%x", \ |
| 422 | (area), \ |
| 423 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 424 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 425 | "bad"), \ |
| 426 | (unsigned)(state)->swap_type, \ |
| 427 | (unsigned)(state)->copy_done, \ |
| 428 | (unsigned)(state)->image_ok) |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 429 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 430 | int |
| 431 | boot_swap_type_multi(int image_index) |
| 432 | { |
Dovhal Artem (CSUKR CSS ICW SW FW 1) | f7a3d1b | 2022-04-01 15:07:37 +0000 | [diff] [blame] | 433 | const struct boot_swap_table *table = NULL; |
| 434 | struct boot_swap_state primary_slot = {0}; |
| 435 | struct boot_swap_state secondary_slot = {0}; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 436 | int rc; |
| 437 | size_t i; |
| 438 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 439 | rc = BOOT_HOOK_CALL(boot_read_swap_state_primary_slot_hook, |
| 440 | BOOT_HOOK_REGULAR, image_index, &primary_slot); |
| 441 | if (rc == BOOT_HOOK_REGULAR) |
| 442 | { |
| 443 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 444 | &primary_slot); |
| 445 | } |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 446 | if (rc) { |
| 447 | return BOOT_SWAP_TYPE_PANIC; |
| 448 | } |
| 449 | |
| 450 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 451 | &secondary_slot); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 452 | if (rc == BOOT_EFLASH) { |
| 453 | BOOT_LOG_INF("Secondary image of image pair (%d.) " |
| 454 | "is unreachable. Treat it as empty", image_index); |
| 455 | secondary_slot.magic = BOOT_MAGIC_UNSET; |
| 456 | secondary_slot.swap_type = BOOT_SWAP_TYPE_NONE; |
| 457 | secondary_slot.copy_done = BOOT_FLAG_UNSET; |
| 458 | secondary_slot.image_ok = BOOT_FLAG_UNSET; |
| 459 | secondary_slot.image_num = 0; |
| 460 | } else if (rc) { |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 461 | return BOOT_SWAP_TYPE_PANIC; |
| 462 | } |
| 463 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 464 | BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Primary image", &primary_slot); |
| 465 | BOOT_LOG_SWAP_STATE("boot_swap_type_multi: Secondary image", &secondary_slot); |
| 466 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 467 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 468 | table = boot_swap_tables + i; |
| 469 | |
| 470 | if (boot_magic_compatible_check(table->magic_primary_slot, |
| 471 | primary_slot.magic) && |
| 472 | boot_magic_compatible_check(table->magic_secondary_slot, |
| 473 | secondary_slot.magic) && |
| 474 | (table->image_ok_primary_slot == BOOT_FLAG_ANY || |
| 475 | table->image_ok_primary_slot == primary_slot.image_ok) && |
| 476 | (table->image_ok_secondary_slot == BOOT_FLAG_ANY || |
| 477 | table->image_ok_secondary_slot == secondary_slot.image_ok) && |
| 478 | (table->copy_done_primary_slot == BOOT_FLAG_ANY || |
| 479 | table->copy_done_primary_slot == primary_slot.copy_done)) { |
| 480 | BOOT_LOG_INF("Swap type: %s", |
| 481 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 482 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 483 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 484 | "BUG; can't happen"); |
| 485 | if (table->swap_type != BOOT_SWAP_TYPE_TEST && |
| 486 | table->swap_type != BOOT_SWAP_TYPE_PERM && |
| 487 | table->swap_type != BOOT_SWAP_TYPE_REVERT) { |
| 488 | return BOOT_SWAP_TYPE_PANIC; |
| 489 | } |
| 490 | return table->swap_type; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | BOOT_LOG_INF("Swap type: none"); |
| 495 | return BOOT_SWAP_TYPE_NONE; |
| 496 | } |
| 497 | |
| 498 | /* |
| 499 | * This function is not used by the bootloader itself, but its required API |
| 500 | * by external tooling like mcumgr. |
| 501 | */ |
| 502 | int |
| 503 | boot_swap_type(void) |
| 504 | { |
| 505 | return boot_swap_type_multi(0); |
| 506 | } |
| 507 | |
| 508 | /** |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 509 | * Marks the image with the given index in the secondary slot as pending. On the |
| 510 | * next reboot, the system will perform a one-time boot of the the secondary |
| 511 | * slot image. |
| 512 | * |
| 513 | * @param image_index Image pair index. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 514 | * |
| 515 | * @param permanent Whether the image should be used permanently or |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 516 | * only tested once: |
| 517 | * 0=run image once, then confirm or revert. |
| 518 | * 1=run image forever. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 519 | * |
| 520 | * @return 0 on success; nonzero on failure. |
| 521 | */ |
| 522 | int |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 523 | boot_set_pending_multi(int image_index, int permanent) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 524 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 525 | const struct flash_area *fap = NULL; |
| 526 | struct boot_swap_state state_secondary_slot = {0}; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 527 | uint8_t swap_type; |
| 528 | int rc; |
| 529 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 530 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 531 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 532 | return BOOT_EFLASH; |
| 533 | } |
| 534 | |
| 535 | rc = boot_read_swap_state(fap, &state_secondary_slot); |
| 536 | if (rc != 0) { |
| 537 | goto done; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | switch (state_secondary_slot.magic) { |
| 541 | case BOOT_MAGIC_GOOD: |
| 542 | /* Swap already scheduled. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 543 | break; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 544 | |
| 545 | case BOOT_MAGIC_UNSET: |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 546 | rc = boot_write_magic(fap); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 547 | |
| 548 | if (rc == 0 && permanent) { |
| 549 | rc = boot_write_image_ok(fap); |
| 550 | } |
| 551 | |
| 552 | if (rc == 0) { |
| 553 | if (permanent) { |
| 554 | swap_type = BOOT_SWAP_TYPE_PERM; |
| 555 | } else { |
| 556 | swap_type = BOOT_SWAP_TYPE_TEST; |
| 557 | } |
| 558 | rc = boot_write_swap_info(fap, swap_type, 0); |
| 559 | } |
| 560 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 561 | break; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 562 | |
| 563 | case BOOT_MAGIC_BAD: |
| 564 | /* The image slot is corrupt. There is no way to recover, so erase the |
| 565 | * slot to allow future upgrades. |
| 566 | */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 567 | flash_area_erase(fap, 0, flash_area_get_size(fap)); |
| 568 | rc = BOOT_EBADIMAGE; |
| 569 | break; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 570 | |
| 571 | default: |
| 572 | assert(0); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 573 | rc = BOOT_EBADIMAGE; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 574 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 575 | |
| 576 | done: |
| 577 | flash_area_close(fap); |
| 578 | return rc; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /** |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 582 | * Marks the image with index 0 in the secondary slot as pending. On the next |
| 583 | * reboot, the system will perform a one-time boot of the the secondary slot |
| 584 | * image. Note that this API is kept for compatibility. The |
| 585 | * boot_set_pending_multi() API is recommended. |
| 586 | * |
| 587 | * @param permanent Whether the image should be used permanently or |
| 588 | * only tested once: |
| 589 | * 0=run image once, then confirm or revert. |
| 590 | * 1=run image forever. |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 591 | * |
| 592 | * @return 0 on success; nonzero on failure. |
| 593 | */ |
| 594 | int |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 595 | boot_set_pending(int permanent) |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 596 | { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 597 | return boot_set_pending_multi(0, permanent); |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Marks the image with the given index in the primary slot as confirmed. The |
| 602 | * system will continue booting into the image in the primary slot until told to |
| 603 | * boot from a different slot. |
| 604 | * |
| 605 | * @param image_index Image pair index. |
| 606 | * |
| 607 | * @return 0 on success; nonzero on failure. |
| 608 | */ |
| 609 | int |
| 610 | boot_set_confirmed_multi(int image_index) |
| 611 | { |
| 612 | const struct flash_area *fap = NULL; |
| 613 | struct boot_swap_state state_primary_slot = {0}; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 614 | int rc; |
| 615 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 616 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 617 | if (rc != 0) { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 618 | return BOOT_EFLASH; |
| 619 | } |
| 620 | |
| 621 | rc = boot_read_swap_state(fap, &state_primary_slot); |
| 622 | if (rc != 0) { |
| 623 | goto done; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | switch (state_primary_slot.magic) { |
| 627 | case BOOT_MAGIC_GOOD: |
| 628 | /* Confirm needed; proceed. */ |
| 629 | break; |
| 630 | |
| 631 | case BOOT_MAGIC_UNSET: |
| 632 | /* Already confirmed. */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 633 | goto done; |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 634 | |
| 635 | case BOOT_MAGIC_BAD: |
| 636 | /* Unexpected state. */ |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 637 | rc = BOOT_EBADVECT; |
| 638 | goto done; |
| 639 | } |
| 640 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 641 | /* Intentionally do not check copy_done flag |
| 642 | * so can confirm a padded image which was programed using a programing |
| 643 | * interface. |
| 644 | */ |
| 645 | |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 646 | if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) { |
| 647 | /* Already confirmed. */ |
| 648 | goto done; |
| 649 | } |
| 650 | |
| 651 | rc = boot_write_image_ok(fap); |
| 652 | |
| 653 | done: |
| 654 | flash_area_close(fap); |
| 655 | return rc; |
| 656 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 657 | |
| 658 | /** |
| 659 | * Marks the image with index 0 in the primary slot as confirmed. The system |
| 660 | * will continue booting into the image in the primary slot until told to boot |
| 661 | * from a different slot. Note that this API is kept for compatibility. The |
| 662 | * boot_set_confirmed_multi() API is recommended. |
| 663 | * |
| 664 | * @return 0 on success; nonzero on failure. |
| 665 | */ |
| 666 | int |
| 667 | boot_set_confirmed(void) |
| 668 | { |
| 669 | return boot_set_confirmed_multi(0); |
| 670 | } |