Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 20 | /* |
| 21 | * Original code taken from mcuboot project at: |
| 22 | * https://github.com/JuulLabs-OSS/mcuboot |
David Vincze | 61bd1e5 | 2019-10-24 16:47:31 +0200 | [diff] [blame^] | 23 | * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4 |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 24 | * Modifications are Copyright (c) 2019 Arm Limited. |
| 25 | */ |
| 26 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 27 | #include <assert.h> |
| 28 | #include <string.h> |
| 29 | #include <inttypes.h> |
| 30 | #include <stddef.h> |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 31 | #include <stdbool.h> |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 32 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 33 | #include "flash_map/flash_map.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 34 | #include "bootutil/image.h" |
| 35 | #include "bootutil/bootutil.h" |
| 36 | #include "bootutil_priv.h" |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 37 | #include "bootutil/bootutil_log.h" |
| 38 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 39 | /* Currently only used by imgmgr */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 40 | int boot_current_slot; |
| 41 | |
| 42 | const uint32_t boot_img_magic[] = { |
| 43 | 0xf395c277, |
| 44 | 0x7fefd260, |
| 45 | 0x0f505235, |
| 46 | 0x8079b62c, |
| 47 | }; |
| 48 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 49 | #define BOOT_MAGIC_ARR_SZ \ |
| 50 | (sizeof boot_img_magic / sizeof boot_img_magic[0]) |
| 51 | |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 52 | const uint32_t BOOT_MAGIC_SZ = sizeof(boot_img_magic); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 53 | const uint32_t BOOT_MAX_ALIGN = MAX_FLASH_ALIGN; |
| 54 | |
| 55 | struct boot_swap_table { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 56 | uint8_t magic_primary_slot; |
| 57 | uint8_t magic_secondary_slot; |
| 58 | uint8_t image_ok_primary_slot; |
| 59 | uint8_t image_ok_secondary_slot; |
| 60 | uint8_t copy_done_primary_slot; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 61 | |
| 62 | uint8_t swap_type; |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * This set of tables maps image trailer contents to swap operation type. |
| 67 | * When searching for a match, these tables must be iterated sequentially. |
| 68 | * |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 69 | * NOTE: the table order is very important. The settings in the secondary |
| 70 | * slot always are priority to the primary slot and should be located |
| 71 | * earlier in the table. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 72 | * |
| 73 | * The table lists only states where there is action needs to be taken by |
| 74 | * the bootloader, as in starting/finishing a swap operation. |
| 75 | */ |
| 76 | static const struct boot_swap_table boot_swap_tables[] = { |
| 77 | { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 78 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 79 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 80 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 81 | .image_ok_secondary_slot = BOOT_FLAG_UNSET, |
| 82 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 83 | .swap_type = BOOT_SWAP_TYPE_TEST, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 84 | }, |
| 85 | { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 86 | .magic_primary_slot = BOOT_MAGIC_ANY, |
| 87 | .magic_secondary_slot = BOOT_MAGIC_GOOD, |
| 88 | .image_ok_primary_slot = BOOT_FLAG_ANY, |
| 89 | .image_ok_secondary_slot = BOOT_FLAG_SET, |
| 90 | .copy_done_primary_slot = BOOT_FLAG_ANY, |
| 91 | .swap_type = BOOT_SWAP_TYPE_PERM, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 92 | }, |
| 93 | { |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 94 | .magic_primary_slot = BOOT_MAGIC_GOOD, |
| 95 | .magic_secondary_slot = BOOT_MAGIC_UNSET, |
| 96 | .image_ok_primary_slot = BOOT_FLAG_UNSET, |
| 97 | .image_ok_secondary_slot = BOOT_FLAG_ANY, |
| 98 | .copy_done_primary_slot = BOOT_FLAG_SET, |
| 99 | .swap_type = BOOT_SWAP_TYPE_REVERT, |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 100 | }, |
| 101 | }; |
| 102 | |
| 103 | #define BOOT_SWAP_TABLES_COUNT \ |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 104 | (sizeof(boot_swap_tables) / sizeof(boot_swap_tables[0])) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 105 | |
Raef Coles | 25b857a | 2019-09-05 13:59:55 +0100 | [diff] [blame] | 106 | /** |
| 107 | * @brief Determine if the data at two memory addresses is equal |
| 108 | * |
| 109 | * @param s1 The first memory region to compare. |
| 110 | * @param s2 The second memory region to compare. |
| 111 | * @param n The amount of bytes to compare. |
| 112 | * |
| 113 | * @note This function does not comply with the specification of memcmp, |
| 114 | * so should not be considered a drop-in replacement. |
| 115 | * |
| 116 | * @return 0 if memory regions are equal. |
| 117 | */ |
| 118 | uint32_t boot_secure_memequal(const void *s1, const void *s2, size_t n) |
| 119 | { |
| 120 | size_t i; |
| 121 | uint8_t *s1_p = (uint8_t*) s1; |
| 122 | uint8_t *s2_p = (uint8_t*) s2; |
| 123 | uint32_t ret = 0; |
| 124 | |
| 125 | for (i = 0; i < n; i++) { |
| 126 | ret |= (s1_p[i] ^ s2_p[i]); |
| 127 | } |
| 128 | |
| 129 | return ret; |
| 130 | } |
| 131 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 132 | static int |
| 133 | boot_magic_decode(const uint32_t *magic) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 134 | { |
Raef Coles | 25b857a | 2019-09-05 13:59:55 +0100 | [diff] [blame] | 135 | if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 136 | return BOOT_MAGIC_GOOD; |
| 137 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 138 | return BOOT_MAGIC_BAD; |
| 139 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 140 | |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 141 | static int |
| 142 | boot_flag_decode(uint8_t flag) |
| 143 | { |
| 144 | if (flag != BOOT_FLAG_SET) { |
| 145 | return BOOT_FLAG_BAD; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 146 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 147 | return BOOT_FLAG_SET; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 148 | } |
| 149 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 150 | /** |
| 151 | * Determines if a status source table is satisfied by the specified magic |
| 152 | * code. |
| 153 | * |
| 154 | * @param tbl_val A magic field from a status source table. |
| 155 | * @param val The magic value in a trailer, encoded as a |
| 156 | * BOOT_MAGIC_[...]. |
| 157 | * |
| 158 | * @return 1 if the two values are compatible; |
| 159 | * 0 otherwise. |
| 160 | */ |
| 161 | int |
| 162 | boot_magic_compatible_check(uint8_t tbl_val, uint8_t val) |
| 163 | { |
| 164 | switch (tbl_val) { |
| 165 | case BOOT_MAGIC_ANY: |
| 166 | return 1; |
| 167 | |
| 168 | case BOOT_MAGIC_NOTGOOD: |
| 169 | return val != BOOT_MAGIC_GOOD; |
| 170 | |
| 171 | default: |
| 172 | return tbl_val == val; |
| 173 | } |
| 174 | } |
| 175 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 176 | uint32_t |
Raef Coles | 204c5b4 | 2019-09-05 09:18:47 +0100 | [diff] [blame] | 177 | boot_trailer_sz(uint32_t min_write_sz) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 178 | { |
| 179 | return /* state for all sectors */ |
| 180 | BOOT_STATUS_MAX_ENTRIES * BOOT_STATUS_STATE_COUNT * min_write_sz + |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 181 | /* swap_type + copy_done + image_ok + swap_size */ |
| 182 | BOOT_MAX_ALIGN * 4 + |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 183 | BOOT_MAGIC_SZ; |
| 184 | } |
| 185 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 186 | int |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 187 | boot_status_entries(int image_index, const struct flash_area *fap) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 188 | { |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 189 | if (fap->fa_id == FLASH_AREA_IMAGE_SCRATCH) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 190 | return BOOT_STATUS_STATE_COUNT; |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 191 | } else if ((fap->fa_id == FLASH_AREA_IMAGE_PRIMARY(image_index)) || |
| 192 | (fap->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) { |
David Vincze | bb20798 | 2019-08-21 15:13:04 +0200 | [diff] [blame] | 193 | return BOOT_STATUS_STATE_COUNT * BOOT_STATUS_MAX_ENTRIES; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 194 | } |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 195 | return -1; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | uint32_t |
| 199 | boot_status_off(const struct flash_area *fap) |
| 200 | { |
| 201 | uint32_t off_from_end; |
Raef Coles | 204c5b4 | 2019-09-05 09:18:47 +0100 | [diff] [blame] | 202 | uint32_t elem_sz; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 203 | |
| 204 | elem_sz = flash_area_align(fap); |
| 205 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 206 | off_from_end = boot_trailer_sz(elem_sz); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 207 | |
| 208 | assert(off_from_end <= fap->fa_size); |
| 209 | return fap->fa_size - off_from_end; |
| 210 | } |
| 211 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 212 | static inline uint32_t |
| 213 | boot_magic_off(const struct flash_area *fap) |
| 214 | { |
| 215 | return fap->fa_size - BOOT_MAGIC_SZ; |
| 216 | } |
| 217 | |
| 218 | static inline uint32_t |
| 219 | boot_image_ok_off(const struct flash_area *fap) |
| 220 | { |
| 221 | return boot_magic_off(fap) - BOOT_MAX_ALIGN; |
| 222 | } |
| 223 | |
| 224 | static inline uint32_t |
| 225 | boot_copy_done_off(const struct flash_area *fap) |
| 226 | { |
| 227 | return boot_image_ok_off(fap) - BOOT_MAX_ALIGN; |
| 228 | } |
| 229 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 230 | uint32_t |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 231 | boot_swap_info_off(const struct flash_area *fap) |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 232 | { |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 233 | return boot_copy_done_off(fap) - BOOT_MAX_ALIGN; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 234 | } |
| 235 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 236 | static inline uint32_t |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 237 | boot_swap_size_off(const struct flash_area *fap) |
| 238 | { |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 239 | return boot_swap_info_off(fap) - BOOT_MAX_ALIGN; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | int |
| 243 | boot_read_swap_state(const struct flash_area *fap, |
| 244 | struct boot_swap_state *state) |
| 245 | { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 246 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 247 | uint32_t off; |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 248 | uint8_t swap_info; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 249 | int rc; |
| 250 | |
| 251 | off = boot_magic_off(fap); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 252 | rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ); |
| 253 | if (rc < 0) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 254 | return BOOT_EFLASH; |
| 255 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 256 | if (rc == 1) { |
| 257 | state->magic = BOOT_MAGIC_UNSET; |
| 258 | } else { |
| 259 | state->magic = boot_magic_decode(magic); |
| 260 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 261 | |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 262 | off = boot_swap_info_off(fap); |
| 263 | rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 264 | if (rc < 0) { |
| 265 | return BOOT_EFLASH; |
| 266 | } |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 267 | |
| 268 | /* Extract the swap type and image number */ |
| 269 | state->swap_type = BOOT_GET_SWAP_TYPE(swap_info); |
| 270 | state->image_num = BOOT_GET_IMAGE_NUM(swap_info); |
| 271 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 272 | if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) { |
| 273 | state->swap_type = BOOT_SWAP_TYPE_NONE; |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 274 | state->image_num = 0; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | off = boot_copy_done_off(fap); |
| 278 | rc = flash_area_read_is_empty(fap, off, &state->copy_done, |
| 279 | sizeof state->copy_done); |
| 280 | if (rc < 0) { |
| 281 | return BOOT_EFLASH; |
| 282 | } |
| 283 | if (rc == 1) { |
| 284 | state->copy_done = BOOT_FLAG_UNSET; |
| 285 | } else { |
| 286 | state->copy_done = boot_flag_decode(state->copy_done); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | off = boot_image_ok_off(fap); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 290 | rc = flash_area_read_is_empty(fap, off, &state->image_ok, |
| 291 | sizeof state->image_ok); |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 292 | if (rc < 0) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 293 | return BOOT_EFLASH; |
| 294 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 295 | if (rc == 1) { |
| 296 | state->image_ok = BOOT_FLAG_UNSET; |
| 297 | } else { |
| 298 | state->image_ok = boot_flag_decode(state->image_ok); |
| 299 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Reads the image trailer from the scratch area. |
| 306 | */ |
| 307 | int |
| 308 | boot_read_swap_state_by_id(int flash_area_id, struct boot_swap_state *state) |
| 309 | { |
| 310 | const struct flash_area *fap; |
| 311 | int rc; |
| 312 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 313 | rc = flash_area_open(flash_area_id, &fap); |
| 314 | if (rc != 0) { |
| 315 | return BOOT_EFLASH; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | rc = boot_read_swap_state(fap, state); |
| 319 | flash_area_close(fap); |
| 320 | return rc; |
| 321 | } |
| 322 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 323 | /** |
| 324 | * This functions tries to locate the status area after an aborted swap, |
| 325 | * by looking for the magic in the possible locations. |
| 326 | * |
| 327 | * If the magic is sucessfully found, a flash_area * is returned and it |
| 328 | * is the responsibility of the called to close it. |
| 329 | * |
| 330 | * @returns 0 on success, -1 on errors |
| 331 | */ |
| 332 | static int |
| 333 | boot_find_status(int image_index, const struct flash_area **fap) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 334 | { |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 335 | uint32_t magic[BOOT_MAGIC_ARR_SZ]; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 336 | uint32_t off; |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 337 | uint8_t areas[2] = { |
| 338 | FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 339 | FLASH_AREA_IMAGE_SCRATCH, |
| 340 | }; |
| 341 | unsigned int i; |
| 342 | int rc; |
| 343 | |
| 344 | /* |
| 345 | * In the middle a swap, tries to locate the area that is currently |
| 346 | * storing a valid magic, first on the primary slot, then on scratch. |
| 347 | * Both "slots" can end up being temporary storage for a swap and it |
| 348 | * is assumed that if magic is valid then other metadata is too, |
| 349 | * because magic is always written in the last step. |
| 350 | */ |
| 351 | |
| 352 | for (i = 0; i < sizeof(areas) / sizeof(areas[0]); i++) { |
| 353 | rc = flash_area_open(areas[i], fap); |
| 354 | if (rc != 0) { |
| 355 | return rc; |
| 356 | } |
| 357 | |
| 358 | off = boot_magic_off(*fap); |
| 359 | rc = flash_area_read(*fap, off, magic, BOOT_MAGIC_SZ); |
| 360 | if (rc != 0) { |
| 361 | flash_area_close(*fap); |
| 362 | return rc; |
| 363 | } |
| 364 | |
David Vincze | c256612 | 2019-10-25 13:18:54 +0200 | [diff] [blame] | 365 | if (boot_secure_memequal(magic, boot_img_magic, BOOT_MAGIC_SZ) == 0) { |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | flash_area_close(*fap); |
| 370 | } |
| 371 | |
| 372 | /* If we got here, no magic was found */ |
| 373 | return -1; |
| 374 | } |
| 375 | |
| 376 | int |
| 377 | boot_read_swap_size(int image_index, uint32_t *swap_size) |
| 378 | { |
| 379 | uint32_t off; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 380 | const struct flash_area *fap; |
| 381 | int rc; |
| 382 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 383 | rc = boot_find_status(image_index, &fap); |
| 384 | if (rc == 0) { |
| 385 | off = boot_swap_size_off(fap); |
| 386 | rc = flash_area_read(fap, off, swap_size, sizeof *swap_size); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 387 | flash_area_close(fap); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 390 | return rc; |
| 391 | } |
| 392 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 393 | int |
| 394 | boot_write_magic(const struct flash_area *fap) |
| 395 | { |
| 396 | uint32_t off; |
| 397 | int rc; |
| 398 | |
| 399 | off = boot_magic_off(fap); |
| 400 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 401 | BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)", |
| 402 | fap->fa_id, (unsigned long)off, |
| 403 | (unsigned long)(fap->fa_off + off)); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 404 | rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); |
| 405 | if (rc != 0) { |
| 406 | return BOOT_EFLASH; |
| 407 | } |
| 408 | |
| 409 | return 0; |
| 410 | } |
| 411 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 412 | /** |
| 413 | * Write trailer data; status bytes, swap_size, etc |
| 414 | * |
| 415 | * @returns 0 on success, != 0 on error. |
| 416 | */ |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 417 | static int |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 418 | boot_write_trailer(const struct flash_area *fap, uint32_t off, |
| 419 | const uint8_t *inbuf, uint8_t inlen) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 420 | { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 421 | uint8_t buf[BOOT_MAX_ALIGN]; |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 422 | uint8_t align; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 423 | uint8_t erased_val; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 424 | int rc; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 425 | |
Tamas Ban | c382885 | 2018-02-01 12:24:16 +0000 | [diff] [blame] | 426 | align = flash_area_align(fap); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 427 | if (inlen > BOOT_MAX_ALIGN || align > BOOT_MAX_ALIGN) { |
| 428 | return -1; |
| 429 | } |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 430 | erased_val = flash_area_erased_val(fap); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 431 | if (align < inlen) { |
| 432 | align = inlen; |
| 433 | } |
| 434 | memcpy(buf, inbuf, inlen); |
| 435 | memset(&buf[inlen], erased_val, align - inlen); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 436 | |
| 437 | rc = flash_area_write(fap, off, buf, align); |
| 438 | if (rc != 0) { |
| 439 | return BOOT_EFLASH; |
| 440 | } |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 445 | static int |
| 446 | boot_write_trailer_flag(const struct flash_area *fap, uint32_t off, |
| 447 | uint8_t flag_val) |
| 448 | { |
| 449 | const uint8_t buf[1] = { flag_val }; |
| 450 | return boot_write_trailer(fap, off, buf, 1); |
| 451 | } |
| 452 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 453 | int |
| 454 | boot_write_copy_done(const struct flash_area *fap) |
| 455 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 456 | uint32_t off; |
| 457 | |
| 458 | off = boot_copy_done_off(fap); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 459 | BOOT_LOG_DBG("writing copy_done; fa_id=%d off=0x%lx (0x%lx)", |
| 460 | fap->fa_id, (unsigned long)off, |
| 461 | (unsigned long)(fap->fa_off + off)); |
| 462 | return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | int |
| 466 | boot_write_image_ok(const struct flash_area *fap) |
| 467 | { |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 468 | uint32_t off; |
| 469 | |
| 470 | off = boot_image_ok_off(fap); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 471 | BOOT_LOG_DBG("writing image_ok; fa_id=%d off=0x%lx (0x%lx)", |
| 472 | fap->fa_id, (unsigned long)off, |
| 473 | (unsigned long)(fap->fa_off + off)); |
| 474 | return boot_write_trailer_flag(fap, off, BOOT_FLAG_SET); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Writes the specified value to the `swap-type` field of an image trailer. |
| 479 | * This value is persisted so that the boot loader knows what swap operation to |
| 480 | * resume in case of an unexpected reset. |
| 481 | */ |
| 482 | int |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 483 | boot_write_swap_info(const struct flash_area *fap, uint8_t swap_type, |
| 484 | uint8_t image_num) |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 485 | { |
| 486 | uint32_t off; |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 487 | uint8_t swap_info; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 488 | |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 489 | BOOT_SET_SWAP_INFO(swap_info, image_num, swap_type); |
| 490 | off = boot_swap_info_off(fap); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 491 | BOOT_LOG_DBG("writing swap_info; fa_id=%d off=0x%lx (0x%lx), swap_type=0x%x" |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 492 | " image_num=0x%x", |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 493 | fap->fa_id, (unsigned long)off, |
| 494 | (unsigned long)(fap->fa_off + off), swap_type, image_num); |
| 495 | return boot_write_trailer(fap, off, (const uint8_t *) &swap_info, 1); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | int |
| 499 | boot_write_swap_size(const struct flash_area *fap, uint32_t swap_size) |
| 500 | { |
| 501 | uint32_t off; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 502 | |
| 503 | off = boot_swap_size_off(fap); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 504 | BOOT_LOG_DBG("writing swap_size; fa_id=%d off=0x%lx (0x%lx)", |
| 505 | fap->fa_id, (unsigned long)off, |
| 506 | (unsigned long)fap->fa_off + off); |
| 507 | return boot_write_trailer(fap, off, (const uint8_t *) &swap_size, 4); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | int |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 511 | boot_swap_type_multi(int image_index) |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 512 | { |
| 513 | const struct boot_swap_table *table; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 514 | struct boot_swap_state primary_slot; |
| 515 | struct boot_swap_state secondary_slot; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 516 | int rc; |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 517 | size_t i; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 518 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 519 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 520 | &primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 521 | if (rc) { |
| 522 | return BOOT_SWAP_TYPE_PANIC; |
| 523 | } |
| 524 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 525 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 526 | &secondary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 527 | if (rc) { |
| 528 | return BOOT_SWAP_TYPE_PANIC; |
| 529 | } |
| 530 | |
| 531 | for (i = 0; i < BOOT_SWAP_TABLES_COUNT; i++) { |
| 532 | table = boot_swap_tables + i; |
| 533 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 534 | if (boot_magic_compatible_check(table->magic_primary_slot, |
| 535 | primary_slot.magic) && |
| 536 | boot_magic_compatible_check(table->magic_secondary_slot, |
| 537 | secondary_slot.magic) && |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 538 | (table->image_ok_primary_slot == BOOT_FLAG_ANY || |
| 539 | table->image_ok_primary_slot == primary_slot.image_ok) && |
| 540 | (table->image_ok_secondary_slot == BOOT_FLAG_ANY || |
| 541 | table->image_ok_secondary_slot == secondary_slot.image_ok) && |
| 542 | (table->copy_done_primary_slot == BOOT_FLAG_ANY || |
| 543 | table->copy_done_primary_slot == primary_slot.copy_done)) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 544 | BOOT_LOG_INF("Swap type: %s", |
| 545 | table->swap_type == BOOT_SWAP_TYPE_TEST ? "test" : |
| 546 | table->swap_type == BOOT_SWAP_TYPE_PERM ? "perm" : |
| 547 | table->swap_type == BOOT_SWAP_TYPE_REVERT ? "revert" : |
| 548 | "BUG; can't happen"); |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 549 | if (table->swap_type != BOOT_SWAP_TYPE_TEST && |
| 550 | table->swap_type != BOOT_SWAP_TYPE_PERM && |
| 551 | table->swap_type != BOOT_SWAP_TYPE_REVERT) { |
| 552 | return BOOT_SWAP_TYPE_PANIC; |
| 553 | } |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 554 | return table->swap_type; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | BOOT_LOG_INF("Swap type: none"); |
| 559 | return BOOT_SWAP_TYPE_NONE; |
| 560 | } |
| 561 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 562 | /* |
| 563 | * This function is not used by the bootloader itself, but its required API |
| 564 | * by external tooling like mcumgr. |
| 565 | */ |
| 566 | int |
| 567 | boot_swap_type(void) |
| 568 | { |
| 569 | return boot_swap_type_multi(0); |
| 570 | } |
| 571 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 572 | /** |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 573 | * Marks the image in the secondary slot as pending. On the next reboot, |
| 574 | * the system will perform a one-time boot of the the secondary slot image. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 575 | * |
| 576 | * @param permanent Whether the image should be used permanently or |
| 577 | * only tested once: |
| 578 | * 0=run image once, then confirm or revert. |
| 579 | * 1=run image forever. |
| 580 | * |
| 581 | * @return 0 on success; nonzero on failure. |
| 582 | */ |
| 583 | int |
| 584 | boot_set_pending(int permanent) |
| 585 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 586 | const struct flash_area *fap = NULL; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 587 | struct boot_swap_state state_secondary_slot; |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 588 | uint8_t swap_type; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 589 | int rc; |
| 590 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 591 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(0), |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 592 | &state_secondary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 593 | if (rc != 0) { |
| 594 | return rc; |
| 595 | } |
| 596 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 597 | switch (state_secondary_slot.magic) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 598 | case BOOT_MAGIC_GOOD: |
| 599 | /* Swap already scheduled. */ |
| 600 | return 0; |
| 601 | |
| 602 | case BOOT_MAGIC_UNSET: |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 603 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 604 | if (rc != 0) { |
| 605 | rc = BOOT_EFLASH; |
| 606 | } else { |
| 607 | rc = boot_write_magic(fap); |
| 608 | } |
| 609 | |
| 610 | if (rc == 0 && permanent) { |
| 611 | rc = boot_write_image_ok(fap); |
| 612 | } |
| 613 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 614 | if (rc == 0) { |
| 615 | if (permanent) { |
| 616 | swap_type = BOOT_SWAP_TYPE_PERM; |
| 617 | } else { |
| 618 | swap_type = BOOT_SWAP_TYPE_TEST; |
| 619 | } |
David Vincze | 91b71ef | 2019-06-24 13:06:47 +0200 | [diff] [blame] | 620 | rc = boot_write_swap_info(fap, swap_type, 0); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 621 | } |
| 622 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 623 | flash_area_close(fap); |
| 624 | return rc; |
| 625 | |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 626 | case BOOT_MAGIC_BAD: |
| 627 | /* The image slot is corrupt. There is no way to recover, so erase the |
| 628 | * slot to allow future upgrades. |
| 629 | */ |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 630 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(0), &fap); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 631 | if (rc != 0) { |
| 632 | return BOOT_EFLASH; |
| 633 | } |
| 634 | |
| 635 | flash_area_erase(fap, 0, fap->fa_size); |
| 636 | flash_area_close(fap); |
| 637 | return BOOT_EBADIMAGE; |
| 638 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 639 | default: |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 640 | assert(0); |
David Vincze | 401c742 | 2019-06-21 20:44:05 +0200 | [diff] [blame] | 641 | return BOOT_EBADIMAGE; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 642 | } |
| 643 | } |
| 644 | |
| 645 | /** |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 646 | * Marks the image in the primary slot as confirmed. The system will continue |
| 647 | * booting into the image in the primary slot until told to boot from a |
| 648 | * different slot. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 649 | * |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 650 | * @return 0 on success; non-zero on failure. |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 651 | */ |
| 652 | int |
| 653 | boot_set_confirmed(void) |
| 654 | { |
Tamas Ban | 581034a | 2017-12-19 19:54:37 +0000 | [diff] [blame] | 655 | const struct flash_area *fap = NULL; |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 656 | struct boot_swap_state state_primary_slot; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 657 | int rc; |
| 658 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 659 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(0), |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 660 | &state_primary_slot); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 661 | if (rc != 0) { |
| 662 | return rc; |
| 663 | } |
| 664 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 665 | switch (state_primary_slot.magic) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 666 | case BOOT_MAGIC_GOOD: |
| 667 | /* Confirm needed; proceed. */ |
| 668 | break; |
| 669 | |
| 670 | case BOOT_MAGIC_UNSET: |
| 671 | /* Already confirmed. */ |
| 672 | return 0; |
| 673 | |
| 674 | case BOOT_MAGIC_BAD: |
| 675 | /* Unexpected state. */ |
| 676 | return BOOT_EBADVECT; |
| 677 | } |
| 678 | |
David Vincze | cea8b59 | 2019-10-29 16:09:51 +0100 | [diff] [blame] | 679 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(0), &fap); |
| 680 | if (rc) { |
| 681 | rc = BOOT_EFLASH; |
| 682 | goto done; |
| 683 | } |
| 684 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 685 | if (state_primary_slot.copy_done == BOOT_FLAG_UNSET) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 686 | /* Swap never completed. This is unexpected. */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 687 | rc = BOOT_EBADVECT; |
| 688 | goto done; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 689 | } |
| 690 | |
David Vincze | 8bdfc2d | 2019-03-18 15:49:23 +0100 | [diff] [blame] | 691 | if (state_primary_slot.image_ok != BOOT_FLAG_UNSET) { |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 692 | /* Already confirmed. */ |
David Vincze | 39e7855 | 2018-10-10 17:10:01 +0200 | [diff] [blame] | 693 | goto done; |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 696 | rc = boot_write_image_ok(fap); |
Tamas Ban | f70ef8c | 2017-12-19 15:35:09 +0000 | [diff] [blame] | 697 | |
| 698 | done: |
| 699 | flash_area_close(fap); |
| 700 | return rc; |
| 701 | } |
David Vincze | bb6e3b6 | 2019-07-31 14:24:05 +0200 | [diff] [blame] | 702 | |
Tamas Ban | 056ed0b | 2019-09-16 12:48:32 +0100 | [diff] [blame] | 703 | /** |
| 704 | * Checks whether on overflow can happen during a summation operation |
| 705 | * |
| 706 | * @param a First operand of summation |
| 707 | * |
| 708 | * @param b Second operand of summation |
| 709 | * |
| 710 | * @return True in case of overflow, false otherwise |
| 711 | */ |
| 712 | bool |
| 713 | boot_add_uint32_overflow_check(uint32_t a, uint32_t b) |
| 714 | { |
| 715 | return (a > UINT32_MAX - b); |
| 716 | } |
| 717 | |
| 718 | /** |
| 719 | * Checks whether on overflow can happen during a summation operation |
| 720 | * |
| 721 | * @param a First operand of summation |
| 722 | * |
| 723 | * @param b Second operand of summation |
| 724 | * |
| 725 | * @return True in case of overflow, false otherwise |
| 726 | */ |
| 727 | bool |
| 728 | boot_add_uint16_overflow_check(uint16_t a, uint16_t b) |
| 729 | { |
| 730 | return (a > UINT16_MAX - b); |
| 731 | } |