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