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