Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 1 | /* |
David Brown | aac7111 | 2020-02-03 16:13:42 -0700 | [diff] [blame] | 2 | * SPDX-License-Identifier: Apache-2.0 |
| 3 | * |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 4 | * Copyright (c) 2019 JUUL Labs |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 19 | #include <stddef.h> |
| 20 | #include <stdbool.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include "bootutil/bootutil.h" |
| 25 | #include "bootutil_priv.h" |
| 26 | #include "swap_priv.h" |
| 27 | #include "bootutil/bootutil_log.h" |
| 28 | |
| 29 | #include "mcuboot_config/mcuboot_config.h" |
| 30 | |
Carlos Falgueras GarcĂa | a4b4b0f | 2021-06-22 10:00:22 +0200 | [diff] [blame] | 31 | BOOT_LOG_MODULE_DECLARE(mcuboot); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 32 | |
| 33 | #ifdef MCUBOOT_SWAP_USING_MOVE |
| 34 | |
| 35 | #if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) |
| 36 | /* |
| 37 | * FIXME: this might have to be updated for threaded sim |
| 38 | */ |
| 39 | int boot_status_fails = 0; |
| 40 | #define BOOT_STATUS_ASSERT(x) \ |
| 41 | do { \ |
| 42 | if (!(x)) { \ |
| 43 | boot_status_fails++; \ |
| 44 | } \ |
| 45 | } while (0) |
| 46 | #else |
| 47 | #define BOOT_STATUS_ASSERT(x) ASSERT(x) |
| 48 | #endif |
| 49 | |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 50 | uint32_t |
| 51 | find_last_idx(struct boot_loader_state *state, uint32_t swap_size) |
| 52 | { |
| 53 | uint32_t sector_sz; |
| 54 | uint32_t sz; |
| 55 | uint32_t last_idx; |
| 56 | |
| 57 | sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); |
| 58 | sz = 0; |
| 59 | last_idx = 0; |
| 60 | while (1) { |
| 61 | sz += sector_sz; |
| 62 | last_idx++; |
| 63 | if (sz >= swap_size) { |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | return last_idx; |
| 69 | } |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 70 | |
| 71 | int |
| 72 | boot_read_image_header(struct boot_loader_state *state, int slot, |
| 73 | struct image_header *out_hdr, struct boot_status *bs) |
| 74 | { |
| 75 | const struct flash_area *fap; |
| 76 | uint32_t off; |
| 77 | uint32_t sz; |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 78 | uint32_t last_idx; |
| 79 | uint32_t swap_size; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 80 | int area_id; |
| 81 | int rc; |
| 82 | |
| 83 | #if (BOOT_IMAGE_NUMBER == 1) |
| 84 | (void)state; |
| 85 | #endif |
| 86 | |
| 87 | off = 0; |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 88 | if (bs && !boot_status_is_reset(bs)) { |
Dominik Ermel | 472d4c7 | 2023-02-10 13:29:58 +0000 | [diff] [blame] | 89 | boot_find_status(BOOT_CURR_IMG(state), &fap); |
| 90 | if (fap == NULL || boot_read_swap_size(fap, &swap_size)) { |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 91 | rc = BOOT_EFLASH; |
| 92 | goto done; |
| 93 | } |
Dominik Ermel | 472d4c7 | 2023-02-10 13:29:58 +0000 | [diff] [blame] | 94 | flash_area_close(fap); |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 95 | |
| 96 | last_idx = find_last_idx(state, swap_size); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 97 | sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Find the correct offset or slot where the image header is expected to |
| 101 | * be found for the steps where it is moved or swapped. |
| 102 | */ |
| 103 | if (bs->op == BOOT_STATUS_OP_MOVE && slot == 0 && bs->idx > last_idx) { |
| 104 | off = sz; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 105 | } else if (bs->op == BOOT_STATUS_OP_SWAP) { |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 106 | if (bs->idx > 1 && bs->idx <= last_idx) { |
| 107 | slot = (slot == 0) ? 1 : 0; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 108 | } else if (bs->idx == 1) { |
| 109 | if (slot == 0) { |
| 110 | off = sz; |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 111 | } else if (slot == 1 && bs->state == 2) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 112 | slot = 0; |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
| 119 | rc = flash_area_open(area_id, &fap); |
| 120 | if (rc != 0) { |
| 121 | rc = BOOT_EFLASH; |
| 122 | goto done; |
| 123 | } |
| 124 | |
| 125 | rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr); |
| 126 | if (rc != 0) { |
| 127 | rc = BOOT_EFLASH; |
| 128 | goto done; |
| 129 | } |
| 130 | |
| 131 | /* We only know where the headers are located when bs is valid */ |
| 132 | if (bs != NULL && out_hdr->ih_magic != IMAGE_MAGIC) { |
| 133 | rc = -1; |
| 134 | goto done; |
| 135 | } |
| 136 | |
| 137 | rc = 0; |
| 138 | |
| 139 | done: |
| 140 | flash_area_close(fap); |
| 141 | return rc; |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | swap_read_status_bytes(const struct flash_area *fap, |
| 146 | struct boot_loader_state *state, struct boot_status *bs) |
| 147 | { |
| 148 | uint32_t off; |
| 149 | uint8_t status; |
| 150 | int max_entries; |
| 151 | int found_idx; |
| 152 | uint8_t write_sz; |
| 153 | int move_entries; |
| 154 | int rc; |
| 155 | int last_rc; |
| 156 | int erased_sections; |
| 157 | int i; |
| 158 | |
| 159 | max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap); |
| 160 | if (max_entries < 0) { |
| 161 | return BOOT_EBADARGS; |
| 162 | } |
| 163 | |
| 164 | erased_sections = 0; |
| 165 | found_idx = -1; |
| 166 | /* skip erased sectors at the end */ |
| 167 | last_rc = 1; |
| 168 | write_sz = BOOT_WRITE_SZ(state); |
| 169 | off = boot_status_off(fap); |
| 170 | for (i = max_entries; i > 0; i--) { |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 171 | rc = flash_area_read(fap, off + (i - 1) * write_sz, &status, 1); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 172 | if (rc < 0) { |
| 173 | return BOOT_EFLASH; |
| 174 | } |
| 175 | |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 176 | if (bootutil_buffer_is_erased(fap, &status, 1)) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 177 | if (rc != last_rc) { |
| 178 | erased_sections++; |
| 179 | } |
| 180 | } else { |
| 181 | if (found_idx == -1) { |
| 182 | found_idx = i; |
| 183 | } |
| 184 | } |
| 185 | last_rc = rc; |
| 186 | } |
| 187 | |
| 188 | if (erased_sections > 1) { |
| 189 | /* This means there was an error writing status on the last |
| 190 | * swap. Tell user and move on to validation! |
| 191 | */ |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 192 | #if !defined(__BOOTSIM__) |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 193 | BOOT_LOG_ERR("Detected inconsistent status!"); |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 194 | #endif |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 195 | |
| 196 | #if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) |
| 197 | /* With validation of the primary slot disabled, there is no way |
| 198 | * to be sure the swapped primary slot is OK, so abort! |
| 199 | */ |
| 200 | assert(0); |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT; |
| 205 | if (found_idx == -1) { |
| 206 | /* no swap status found; nothing to do */ |
| 207 | } else if (found_idx < move_entries) { |
| 208 | bs->op = BOOT_STATUS_OP_MOVE; |
| 209 | bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0; |
| 210 | bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;; |
| 211 | } else { |
| 212 | bs->op = BOOT_STATUS_OP_SWAP; |
| 213 | bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0; |
| 214 | bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0; |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | uint32_t |
| 221 | boot_status_internal_off(const struct boot_status *bs, int elem_sz) |
| 222 | { |
| 223 | uint32_t off; |
| 224 | int idx_sz; |
| 225 | |
| 226 | idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ? |
| 227 | BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT); |
| 228 | |
| 229 | off = ((bs->op == BOOT_STATUS_OP_MOVE) ? |
| 230 | 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) + |
| 231 | (bs->idx - BOOT_STATUS_IDX_0) * idx_sz + |
| 232 | (bs->state - BOOT_STATUS_STATE_0) * elem_sz; |
| 233 | |
| 234 | return off; |
| 235 | } |
| 236 | |
Jamie McCrae | 4d75fc8 | 2024-01-03 07:39:46 +0000 | [diff] [blame^] | 237 | static int app_max_sectors(struct boot_loader_state *state) |
| 238 | { |
| 239 | uint32_t sz = 0; |
| 240 | uint32_t sector_sz; |
| 241 | uint32_t trailer_sz; |
| 242 | uint32_t first_trailer_idx; |
| 243 | |
| 244 | sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); |
| 245 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); |
| 246 | first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1; |
| 247 | |
| 248 | while (1) { |
| 249 | sz += sector_sz; |
| 250 | if (sz >= trailer_sz) { |
| 251 | break; |
| 252 | } |
| 253 | first_trailer_idx--; |
| 254 | } |
| 255 | |
| 256 | return first_trailer_idx; |
| 257 | } |
| 258 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 259 | int |
| 260 | boot_slots_compatible(struct boot_loader_state *state) |
| 261 | { |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 262 | size_t num_sectors_pri; |
| 263 | size_t num_sectors_sec; |
| 264 | size_t sector_sz_pri = 0; |
| 265 | size_t sector_sz_sec = 0; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 266 | size_t i; |
Jamie McCrae | 4d75fc8 | 2024-01-03 07:39:46 +0000 | [diff] [blame^] | 267 | size_t num_usable_sectors_pri; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 268 | |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 269 | num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT); |
| 270 | num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT); |
Jamie McCrae | 4d75fc8 | 2024-01-03 07:39:46 +0000 | [diff] [blame^] | 271 | num_usable_sectors_pri = app_max_sectors(state); |
| 272 | |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 273 | if ((num_sectors_pri != num_sectors_sec) && |
Jamie McCrae | 4d75fc8 | 2024-01-03 07:39:46 +0000 | [diff] [blame^] | 274 | (num_sectors_pri != (num_sectors_sec + 1)) && |
| 275 | (num_usable_sectors_pri != (num_sectors_sec + 1))) { |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 276 | BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors"); |
Jamie McCrae | 4d75fc8 | 2024-01-03 07:39:46 +0000 | [diff] [blame^] | 277 | BOOT_LOG_DBG("slot0 sectors: %d, slot1 sectors: %d, usable slot0 sectors: %d", |
| 278 | (int)num_sectors_pri, (int)num_sectors_sec, |
| 279 | (int)(num_usable_sectors_pri - 1)); |
| 280 | return 0; |
| 281 | } else if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) { |
| 282 | BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed"); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
Jamie McCrae | 4d75fc8 | 2024-01-03 07:39:46 +0000 | [diff] [blame^] | 286 | if (num_usable_sectors_pri != (num_sectors_sec + 1)) { |
| 287 | BOOT_LOG_DBG("Non-optimal sector distribution, slot0 has %d usable sectors (%d assigned) " |
| 288 | "but slot1 has %d assigned", (int)(num_usable_sectors_pri - 1), |
| 289 | (int)num_sectors_pri, (int)num_sectors_sec); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 290 | } |
| 291 | |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 292 | for (i = 0; i < num_sectors_sec; i++) { |
| 293 | sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i); |
| 294 | sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i); |
| 295 | if (sector_sz_pri != sector_sz_sec) { |
| 296 | BOOT_LOG_WRN("Cannot upgrade: not same sector layout"); |
| 297 | return 0; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | if (num_sectors_pri > num_sectors_sec) { |
| 302 | if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 303 | BOOT_LOG_WRN("Cannot upgrade: not same sector layout"); |
| 304 | return 0; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
| 312 | BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \ |
| 313 | "image_ok=0x%x", \ |
| 314 | (area), \ |
| 315 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 316 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 317 | "bad"), \ |
| 318 | (state)->swap_type, \ |
| 319 | (state)->copy_done, \ |
| 320 | (state)->image_ok) |
| 321 | |
| 322 | int |
| 323 | swap_status_source(struct boot_loader_state *state) |
| 324 | { |
| 325 | struct boot_swap_state state_primary_slot; |
Fabio Utzig | ce11597 | 2020-10-28 09:05:20 -0300 | [diff] [blame] | 326 | struct boot_swap_state state_secondary_slot; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 327 | int rc; |
| 328 | uint8_t source; |
| 329 | uint8_t image_index; |
| 330 | |
| 331 | #if (BOOT_IMAGE_NUMBER == 1) |
| 332 | (void)state; |
| 333 | #endif |
| 334 | |
| 335 | image_index = BOOT_CURR_IMG(state); |
| 336 | |
| 337 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 338 | &state_primary_slot); |
| 339 | assert(rc == 0); |
| 340 | |
| 341 | BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); |
| 342 | |
Fabio Utzig | ce11597 | 2020-10-28 09:05:20 -0300 | [diff] [blame] | 343 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 344 | &state_secondary_slot); |
| 345 | assert(rc == 0); |
| 346 | |
| 347 | BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot); |
| 348 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 349 | if (state_primary_slot.magic == BOOT_MAGIC_GOOD && |
Fabio Utzig | ce11597 | 2020-10-28 09:05:20 -0300 | [diff] [blame] | 350 | state_primary_slot.copy_done == BOOT_FLAG_UNSET && |
| 351 | state_secondary_slot.magic != BOOT_MAGIC_GOOD) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 352 | |
| 353 | source = BOOT_STATUS_SOURCE_PRIMARY_SLOT; |
| 354 | |
| 355 | BOOT_LOG_INF("Boot source: primary slot"); |
| 356 | return source; |
| 357 | } |
| 358 | |
| 359 | BOOT_LOG_INF("Boot source: none"); |
| 360 | return BOOT_STATUS_SOURCE_NONE; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * "Moves" the sector located at idx - 1 to idx. |
| 365 | */ |
| 366 | static void |
| 367 | boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state, |
| 368 | struct boot_status *bs, const struct flash_area *fap_pri, |
| 369 | const struct flash_area *fap_sec) |
| 370 | { |
| 371 | uint32_t new_off; |
| 372 | uint32_t old_off; |
| 373 | int rc; |
| 374 | |
| 375 | /* |
| 376 | * FIXME: assuming sectors of size == sz, a single off variable |
| 377 | * would be enough |
| 378 | */ |
| 379 | |
| 380 | /* Calculate offset from start of image area. */ |
| 381 | new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx); |
| 382 | old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1); |
| 383 | |
| 384 | if (bs->idx == BOOT_STATUS_IDX_0) { |
Fabio Utzig | 7fd42d5 | 2020-10-28 09:04:41 -0300 | [diff] [blame] | 385 | if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) { |
| 386 | rc = swap_erase_trailer_sectors(state, fap_pri); |
| 387 | assert(rc == 0); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 388 | |
Fabio Utzig | 7fd42d5 | 2020-10-28 09:04:41 -0300 | [diff] [blame] | 389 | rc = swap_status_init(state, fap_pri, bs); |
| 390 | assert(rc == 0); |
| 391 | } |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 392 | |
| 393 | rc = swap_erase_trailer_sectors(state, fap_sec); |
| 394 | assert(rc == 0); |
| 395 | } |
| 396 | |
| 397 | rc = boot_erase_region(fap_pri, new_off, sz); |
| 398 | assert(rc == 0); |
| 399 | |
| 400 | rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz); |
| 401 | assert(rc == 0); |
| 402 | |
| 403 | rc = boot_write_status(state, bs); |
| 404 | |
| 405 | bs->idx++; |
| 406 | BOOT_STATUS_ASSERT(rc == 0); |
| 407 | } |
| 408 | |
| 409 | static void |
| 410 | boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state, |
| 411 | struct boot_status *bs, const struct flash_area *fap_pri, |
| 412 | const struct flash_area *fap_sec) |
| 413 | { |
| 414 | uint32_t pri_off; |
| 415 | uint32_t pri_up_off; |
| 416 | uint32_t sec_off; |
| 417 | int rc; |
| 418 | |
| 419 | pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx); |
| 420 | pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1); |
| 421 | sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1); |
| 422 | |
| 423 | if (bs->state == BOOT_STATUS_STATE_0) { |
| 424 | rc = boot_erase_region(fap_pri, pri_off, sz); |
| 425 | assert(rc == 0); |
| 426 | |
| 427 | rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz); |
| 428 | assert(rc == 0); |
| 429 | |
| 430 | rc = boot_write_status(state, bs); |
| 431 | bs->state = BOOT_STATUS_STATE_1; |
| 432 | BOOT_STATUS_ASSERT(rc == 0); |
| 433 | } |
| 434 | |
| 435 | if (bs->state == BOOT_STATUS_STATE_1) { |
| 436 | rc = boot_erase_region(fap_sec, sec_off, sz); |
| 437 | assert(rc == 0); |
| 438 | |
| 439 | rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz); |
| 440 | assert(rc == 0); |
| 441 | |
| 442 | rc = boot_write_status(state, bs); |
| 443 | bs->idx++; |
| 444 | bs->state = BOOT_STATUS_STATE_0; |
| 445 | BOOT_STATUS_ASSERT(rc == 0); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * When starting a revert the swap status exists in the primary slot, and |
| 451 | * the status in the secondary slot is erased. To start the swap, the status |
| 452 | * area in the primary slot must be re-initialized; if during the small |
| 453 | * window of time between re-initializing it and writing the first metadata |
| 454 | * a reset happens, the swap process is broken and cannot be resumed. |
| 455 | * |
| 456 | * This function handles the issue by making the revert look like a permanent |
| 457 | * upgrade (by initializing the secondary slot). |
| 458 | */ |
| 459 | void |
| 460 | fixup_revert(const struct boot_loader_state *state, struct boot_status *bs, |
Dominik Ermel | 0ab87b6 | 2021-05-25 11:50:04 +0000 | [diff] [blame] | 461 | const struct flash_area *fap_sec) |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 462 | { |
| 463 | struct boot_swap_state swap_state; |
| 464 | int rc; |
| 465 | |
| 466 | #if (BOOT_IMAGE_NUMBER == 1) |
| 467 | (void)state; |
| 468 | #endif |
| 469 | |
| 470 | /* No fixup required */ |
| 471 | if (bs->swap_type != BOOT_SWAP_TYPE_REVERT || |
| 472 | bs->op != BOOT_STATUS_OP_MOVE || |
| 473 | bs->idx != BOOT_STATUS_IDX_0) { |
| 474 | return; |
| 475 | } |
| 476 | |
Dominik Ermel | 0ab87b6 | 2021-05-25 11:50:04 +0000 | [diff] [blame] | 477 | rc = boot_read_swap_state(fap_sec, &swap_state); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 478 | assert(rc == 0); |
| 479 | |
| 480 | BOOT_LOG_SWAP_STATE("Secondary image", &swap_state); |
| 481 | |
| 482 | if (swap_state.magic == BOOT_MAGIC_UNSET) { |
| 483 | rc = swap_erase_trailer_sectors(state, fap_sec); |
| 484 | assert(rc == 0); |
| 485 | |
| 486 | rc = boot_write_image_ok(fap_sec); |
| 487 | assert(rc == 0); |
| 488 | |
| 489 | rc = boot_write_swap_size(fap_sec, bs->swap_size); |
| 490 | assert(rc == 0); |
| 491 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 492 | rc = boot_write_magic(fap_sec); |
| 493 | assert(rc == 0); |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | void |
| 498 | swap_run(struct boot_loader_state *state, struct boot_status *bs, |
| 499 | uint32_t copy_size) |
| 500 | { |
| 501 | uint32_t sz; |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 502 | uint32_t sector_sz; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 503 | uint32_t idx; |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 504 | uint32_t trailer_sz; |
| 505 | uint32_t first_trailer_idx; |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 506 | uint32_t last_idx; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 507 | uint8_t image_index; |
| 508 | const struct flash_area *fap_pri; |
| 509 | const struct flash_area *fap_sec; |
| 510 | int rc; |
| 511 | |
Andrzej Puzdrowski | f9dbf68 | 2021-12-23 12:32:28 +0100 | [diff] [blame] | 512 | BOOT_LOG_INF("Starting swap using move algorithm."); |
| 513 | |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 514 | last_idx = find_last_idx(state, copy_size); |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 515 | sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 516 | |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 517 | /* |
| 518 | * When starting a new swap upgrade, check that there is enough space. |
| 519 | */ |
| 520 | if (boot_status_is_reset(bs)) { |
| 521 | sz = 0; |
| 522 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); |
| 523 | first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1; |
| 524 | |
| 525 | while (1) { |
| 526 | sz += sector_sz; |
| 527 | if (sz >= trailer_sz) { |
| 528 | break; |
| 529 | } |
| 530 | first_trailer_idx--; |
| 531 | } |
| 532 | |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 533 | if (last_idx >= first_trailer_idx) { |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 534 | BOOT_LOG_WRN("Not enough free space to run swap upgrade"); |
Andrzej Puzdrowski | f9dbf68 | 2021-12-23 12:32:28 +0100 | [diff] [blame] | 535 | BOOT_LOG_WRN("required %d bytes but only %d are available", |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 536 | (last_idx + 1) * sector_sz, |
Andrzej Puzdrowski | f9dbf68 | 2021-12-23 12:32:28 +0100 | [diff] [blame] | 537 | first_trailer_idx * sector_sz); |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 538 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
| 539 | return; |
| 540 | } |
| 541 | } |
| 542 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 543 | image_index = BOOT_CURR_IMG(state); |
| 544 | |
| 545 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri); |
| 546 | assert (rc == 0); |
| 547 | |
| 548 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec); |
| 549 | assert (rc == 0); |
| 550 | |
Dominik Ermel | 0ab87b6 | 2021-05-25 11:50:04 +0000 | [diff] [blame] | 551 | fixup_revert(state, bs, fap_sec); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 552 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 553 | if (bs->op == BOOT_STATUS_OP_MOVE) { |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 554 | idx = last_idx; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 555 | while (idx > 0) { |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 556 | if (idx <= (last_idx - bs->idx + 1)) { |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 557 | boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 558 | } |
| 559 | idx--; |
| 560 | } |
| 561 | bs->idx = BOOT_STATUS_IDX_0; |
| 562 | } |
| 563 | |
| 564 | bs->op = BOOT_STATUS_OP_SWAP; |
| 565 | |
| 566 | idx = 1; |
Fabio Utzig | 7453075 | 2023-02-07 20:09:37 -0300 | [diff] [blame] | 567 | while (idx <= last_idx) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 568 | if (idx >= bs->idx) { |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 569 | boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 570 | } |
| 571 | idx++; |
| 572 | } |
| 573 | |
| 574 | flash_area_close(fap_pri); |
| 575 | flash_area_close(fap_sec); |
| 576 | } |
| 577 | |
Jamie McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 578 | int app_max_size(struct boot_loader_state *state) |
| 579 | { |
Jamie McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 580 | uint32_t sector_sz; |
Jamie McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 581 | |
| 582 | sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); |
Jamie McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 583 | |
Jamie McCrae | 4d75fc8 | 2024-01-03 07:39:46 +0000 | [diff] [blame^] | 584 | return (app_max_sectors(state) * sector_sz); |
Jamie McCrae | 3016d00 | 2023-03-14 12:35:51 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 587 | #endif |