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