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 | |
| 31 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 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 | |
| 50 | static uint32_t g_last_idx = UINT32_MAX; |
| 51 | |
| 52 | int |
| 53 | boot_read_image_header(struct boot_loader_state *state, int slot, |
| 54 | struct image_header *out_hdr, struct boot_status *bs) |
| 55 | { |
| 56 | const struct flash_area *fap; |
| 57 | uint32_t off; |
| 58 | uint32_t sz; |
| 59 | int area_id; |
| 60 | int rc; |
| 61 | |
| 62 | #if (BOOT_IMAGE_NUMBER == 1) |
| 63 | (void)state; |
| 64 | #endif |
| 65 | |
| 66 | off = 0; |
| 67 | if (bs) { |
| 68 | sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); |
| 69 | if (bs->op == BOOT_STATUS_OP_MOVE) { |
| 70 | if (slot == 0 && bs->idx > g_last_idx) { |
| 71 | /* second sector */ |
| 72 | off = sz; |
| 73 | } |
| 74 | } else if (bs->op == BOOT_STATUS_OP_SWAP) { |
| 75 | if (bs->idx > 1 && bs->idx <= g_last_idx) { |
| 76 | if (slot == 0) { |
| 77 | slot = 1; |
| 78 | } else { |
| 79 | slot = 0; |
| 80 | } |
| 81 | } else if (bs->idx == 1) { |
| 82 | if (slot == 0) { |
| 83 | off = sz; |
| 84 | } |
| 85 | if (slot == 1 && bs->state == 2) { |
| 86 | slot = 0; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot); |
| 93 | rc = flash_area_open(area_id, &fap); |
| 94 | if (rc != 0) { |
| 95 | rc = BOOT_EFLASH; |
| 96 | goto done; |
| 97 | } |
| 98 | |
| 99 | rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr); |
| 100 | if (rc != 0) { |
| 101 | rc = BOOT_EFLASH; |
| 102 | goto done; |
| 103 | } |
| 104 | |
| 105 | /* We only know where the headers are located when bs is valid */ |
| 106 | if (bs != NULL && out_hdr->ih_magic != IMAGE_MAGIC) { |
| 107 | rc = -1; |
| 108 | goto done; |
| 109 | } |
| 110 | |
| 111 | rc = 0; |
| 112 | |
| 113 | done: |
| 114 | flash_area_close(fap); |
| 115 | return rc; |
| 116 | } |
| 117 | |
| 118 | int |
| 119 | swap_read_status_bytes(const struct flash_area *fap, |
| 120 | struct boot_loader_state *state, struct boot_status *bs) |
| 121 | { |
| 122 | uint32_t off; |
| 123 | uint8_t status; |
| 124 | int max_entries; |
| 125 | int found_idx; |
| 126 | uint8_t write_sz; |
| 127 | int move_entries; |
| 128 | int rc; |
| 129 | int last_rc; |
| 130 | int erased_sections; |
| 131 | int i; |
| 132 | |
| 133 | max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap); |
| 134 | if (max_entries < 0) { |
| 135 | return BOOT_EBADARGS; |
| 136 | } |
| 137 | |
| 138 | erased_sections = 0; |
| 139 | found_idx = -1; |
| 140 | /* skip erased sectors at the end */ |
| 141 | last_rc = 1; |
| 142 | write_sz = BOOT_WRITE_SZ(state); |
| 143 | off = boot_status_off(fap); |
| 144 | for (i = max_entries; i > 0; i--) { |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 145 | rc = flash_area_read(fap, off + (i - 1) * write_sz, &status, 1); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 146 | if (rc < 0) { |
| 147 | return BOOT_EFLASH; |
| 148 | } |
| 149 | |
Fabio Utzig | 4b2e55f | 2020-09-24 11:49:20 -0300 | [diff] [blame] | 150 | if (bootutil_buffer_is_erased(fap, &status, 1)) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 151 | if (rc != last_rc) { |
| 152 | erased_sections++; |
| 153 | } |
| 154 | } else { |
| 155 | if (found_idx == -1) { |
| 156 | found_idx = i; |
| 157 | } |
| 158 | } |
| 159 | last_rc = rc; |
| 160 | } |
| 161 | |
| 162 | if (erased_sections > 1) { |
| 163 | /* This means there was an error writing status on the last |
| 164 | * swap. Tell user and move on to validation! |
| 165 | */ |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 166 | #if !defined(__BOOTSIM__) |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 167 | BOOT_LOG_ERR("Detected inconsistent status!"); |
David Brown | 098de83 | 2019-12-10 11:58:01 -0700 | [diff] [blame] | 168 | #endif |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 169 | |
| 170 | #if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) |
| 171 | /* With validation of the primary slot disabled, there is no way |
| 172 | * to be sure the swapped primary slot is OK, so abort! |
| 173 | */ |
| 174 | assert(0); |
| 175 | #endif |
| 176 | } |
| 177 | |
| 178 | move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT; |
| 179 | if (found_idx == -1) { |
| 180 | /* no swap status found; nothing to do */ |
| 181 | } else if (found_idx < move_entries) { |
| 182 | bs->op = BOOT_STATUS_OP_MOVE; |
| 183 | bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0; |
| 184 | bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;; |
| 185 | } else { |
| 186 | bs->op = BOOT_STATUS_OP_SWAP; |
| 187 | bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0; |
| 188 | bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0; |
| 189 | } |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | uint32_t |
| 195 | boot_status_internal_off(const struct boot_status *bs, int elem_sz) |
| 196 | { |
| 197 | uint32_t off; |
| 198 | int idx_sz; |
| 199 | |
| 200 | idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ? |
| 201 | BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT); |
| 202 | |
| 203 | off = ((bs->op == BOOT_STATUS_OP_MOVE) ? |
| 204 | 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) + |
| 205 | (bs->idx - BOOT_STATUS_IDX_0) * idx_sz + |
| 206 | (bs->state - BOOT_STATUS_STATE_0) * elem_sz; |
| 207 | |
| 208 | return off; |
| 209 | } |
| 210 | |
| 211 | int |
| 212 | boot_slots_compatible(struct boot_loader_state *state) |
| 213 | { |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 214 | size_t num_sectors_pri; |
| 215 | size_t num_sectors_sec; |
| 216 | size_t sector_sz_pri = 0; |
| 217 | size_t sector_sz_sec = 0; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 218 | size_t i; |
| 219 | |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 220 | num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT); |
| 221 | num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT); |
| 222 | if ((num_sectors_pri != num_sectors_sec) && |
| 223 | (num_sectors_pri != (num_sectors_sec + 1))) { |
| 224 | BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors"); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 228 | if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 229 | BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed"); |
| 230 | return 0; |
| 231 | } |
| 232 | |
Fabio Utzig | ad055d1 | 2020-06-29 20:19:26 -0300 | [diff] [blame] | 233 | for (i = 0; i < num_sectors_sec; i++) { |
| 234 | sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i); |
| 235 | sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i); |
| 236 | if (sector_sz_pri != sector_sz_sec) { |
| 237 | BOOT_LOG_WRN("Cannot upgrade: not same sector layout"); |
| 238 | return 0; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | if (num_sectors_pri > num_sectors_sec) { |
| 243 | 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] | 244 | BOOT_LOG_WRN("Cannot upgrade: not same sector layout"); |
| 245 | return 0; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | return 1; |
| 250 | } |
| 251 | |
| 252 | #define BOOT_LOG_SWAP_STATE(area, state) \ |
| 253 | BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \ |
| 254 | "image_ok=0x%x", \ |
| 255 | (area), \ |
| 256 | ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \ |
| 257 | (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \ |
| 258 | "bad"), \ |
| 259 | (state)->swap_type, \ |
| 260 | (state)->copy_done, \ |
| 261 | (state)->image_ok) |
| 262 | |
| 263 | int |
| 264 | swap_status_source(struct boot_loader_state *state) |
| 265 | { |
| 266 | struct boot_swap_state state_primary_slot; |
Fabio Utzig | ce11597 | 2020-10-28 09:05:20 -0300 | [diff] [blame] | 267 | struct boot_swap_state state_secondary_slot; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 268 | int rc; |
| 269 | uint8_t source; |
| 270 | uint8_t image_index; |
| 271 | |
| 272 | #if (BOOT_IMAGE_NUMBER == 1) |
| 273 | (void)state; |
| 274 | #endif |
| 275 | |
| 276 | image_index = BOOT_CURR_IMG(state); |
| 277 | |
| 278 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index), |
| 279 | &state_primary_slot); |
| 280 | assert(rc == 0); |
| 281 | |
| 282 | BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot); |
| 283 | |
Fabio Utzig | ce11597 | 2020-10-28 09:05:20 -0300 | [diff] [blame] | 284 | rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index), |
| 285 | &state_secondary_slot); |
| 286 | assert(rc == 0); |
| 287 | |
| 288 | BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot); |
| 289 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 290 | if (state_primary_slot.magic == BOOT_MAGIC_GOOD && |
Fabio Utzig | ce11597 | 2020-10-28 09:05:20 -0300 | [diff] [blame] | 291 | state_primary_slot.copy_done == BOOT_FLAG_UNSET && |
| 292 | state_secondary_slot.magic != BOOT_MAGIC_GOOD) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 293 | |
| 294 | source = BOOT_STATUS_SOURCE_PRIMARY_SLOT; |
| 295 | |
| 296 | BOOT_LOG_INF("Boot source: primary slot"); |
| 297 | return source; |
| 298 | } |
| 299 | |
| 300 | BOOT_LOG_INF("Boot source: none"); |
| 301 | return BOOT_STATUS_SOURCE_NONE; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * "Moves" the sector located at idx - 1 to idx. |
| 306 | */ |
| 307 | static void |
| 308 | boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state, |
| 309 | struct boot_status *bs, const struct flash_area *fap_pri, |
| 310 | const struct flash_area *fap_sec) |
| 311 | { |
| 312 | uint32_t new_off; |
| 313 | uint32_t old_off; |
| 314 | int rc; |
| 315 | |
| 316 | /* |
| 317 | * FIXME: assuming sectors of size == sz, a single off variable |
| 318 | * would be enough |
| 319 | */ |
| 320 | |
| 321 | /* Calculate offset from start of image area. */ |
| 322 | new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx); |
| 323 | old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1); |
| 324 | |
| 325 | if (bs->idx == BOOT_STATUS_IDX_0) { |
Fabio Utzig | 7fd42d5 | 2020-10-28 09:04:41 -0300 | [diff] [blame] | 326 | if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) { |
| 327 | rc = swap_erase_trailer_sectors(state, fap_pri); |
| 328 | assert(rc == 0); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 329 | |
Fabio Utzig | 7fd42d5 | 2020-10-28 09:04:41 -0300 | [diff] [blame] | 330 | rc = swap_status_init(state, fap_pri, bs); |
| 331 | assert(rc == 0); |
| 332 | } |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 333 | |
| 334 | rc = swap_erase_trailer_sectors(state, fap_sec); |
| 335 | assert(rc == 0); |
| 336 | } |
| 337 | |
| 338 | rc = boot_erase_region(fap_pri, new_off, sz); |
| 339 | assert(rc == 0); |
| 340 | |
| 341 | rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz); |
| 342 | assert(rc == 0); |
| 343 | |
| 344 | rc = boot_write_status(state, bs); |
| 345 | |
| 346 | bs->idx++; |
| 347 | BOOT_STATUS_ASSERT(rc == 0); |
| 348 | } |
| 349 | |
| 350 | static void |
| 351 | boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state, |
| 352 | struct boot_status *bs, const struct flash_area *fap_pri, |
| 353 | const struct flash_area *fap_sec) |
| 354 | { |
| 355 | uint32_t pri_off; |
| 356 | uint32_t pri_up_off; |
| 357 | uint32_t sec_off; |
| 358 | int rc; |
| 359 | |
| 360 | pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx); |
| 361 | pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1); |
| 362 | sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1); |
| 363 | |
| 364 | if (bs->state == BOOT_STATUS_STATE_0) { |
| 365 | rc = boot_erase_region(fap_pri, pri_off, sz); |
| 366 | assert(rc == 0); |
| 367 | |
| 368 | rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz); |
| 369 | assert(rc == 0); |
| 370 | |
| 371 | rc = boot_write_status(state, bs); |
| 372 | bs->state = BOOT_STATUS_STATE_1; |
| 373 | BOOT_STATUS_ASSERT(rc == 0); |
| 374 | } |
| 375 | |
| 376 | if (bs->state == BOOT_STATUS_STATE_1) { |
| 377 | rc = boot_erase_region(fap_sec, sec_off, sz); |
| 378 | assert(rc == 0); |
| 379 | |
| 380 | rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz); |
| 381 | assert(rc == 0); |
| 382 | |
| 383 | rc = boot_write_status(state, bs); |
| 384 | bs->idx++; |
| 385 | bs->state = BOOT_STATUS_STATE_0; |
| 386 | BOOT_STATUS_ASSERT(rc == 0); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * When starting a revert the swap status exists in the primary slot, and |
| 392 | * the status in the secondary slot is erased. To start the swap, the status |
| 393 | * area in the primary slot must be re-initialized; if during the small |
| 394 | * window of time between re-initializing it and writing the first metadata |
| 395 | * a reset happens, the swap process is broken and cannot be resumed. |
| 396 | * |
| 397 | * This function handles the issue by making the revert look like a permanent |
| 398 | * upgrade (by initializing the secondary slot). |
| 399 | */ |
| 400 | void |
| 401 | fixup_revert(const struct boot_loader_state *state, struct boot_status *bs, |
| 402 | const struct flash_area *fap_sec, uint8_t sec_id) |
| 403 | { |
| 404 | struct boot_swap_state swap_state; |
| 405 | int rc; |
| 406 | |
| 407 | #if (BOOT_IMAGE_NUMBER == 1) |
| 408 | (void)state; |
| 409 | #endif |
| 410 | |
| 411 | /* No fixup required */ |
| 412 | if (bs->swap_type != BOOT_SWAP_TYPE_REVERT || |
| 413 | bs->op != BOOT_STATUS_OP_MOVE || |
| 414 | bs->idx != BOOT_STATUS_IDX_0) { |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | rc = boot_read_swap_state_by_id(sec_id, &swap_state); |
| 419 | assert(rc == 0); |
| 420 | |
| 421 | BOOT_LOG_SWAP_STATE("Secondary image", &swap_state); |
| 422 | |
| 423 | if (swap_state.magic == BOOT_MAGIC_UNSET) { |
| 424 | rc = swap_erase_trailer_sectors(state, fap_sec); |
| 425 | assert(rc == 0); |
| 426 | |
| 427 | rc = boot_write_image_ok(fap_sec); |
| 428 | assert(rc == 0); |
| 429 | |
| 430 | rc = boot_write_swap_size(fap_sec, bs->swap_size); |
| 431 | assert(rc == 0); |
| 432 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 433 | rc = boot_write_magic(fap_sec); |
| 434 | assert(rc == 0); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void |
| 439 | swap_run(struct boot_loader_state *state, struct boot_status *bs, |
| 440 | uint32_t copy_size) |
| 441 | { |
| 442 | uint32_t sz; |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 443 | uint32_t sector_sz; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 444 | uint32_t idx; |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 445 | uint32_t trailer_sz; |
| 446 | uint32_t first_trailer_idx; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 447 | uint8_t image_index; |
| 448 | const struct flash_area *fap_pri; |
| 449 | const struct flash_area *fap_sec; |
| 450 | int rc; |
| 451 | |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 452 | sz = 0; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 453 | g_last_idx = 0; |
| 454 | |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 455 | sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 456 | while (1) { |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 457 | sz += sector_sz; |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 458 | /* Skip to next sector because all sectors will be moved up. */ |
| 459 | g_last_idx++; |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 460 | if (sz >= copy_size) { |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 461 | break; |
| 462 | } |
| 463 | } |
| 464 | |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 465 | /* |
| 466 | * When starting a new swap upgrade, check that there is enough space. |
| 467 | */ |
| 468 | if (boot_status_is_reset(bs)) { |
| 469 | sz = 0; |
| 470 | trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state)); |
| 471 | first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1; |
| 472 | |
| 473 | while (1) { |
| 474 | sz += sector_sz; |
| 475 | if (sz >= trailer_sz) { |
| 476 | break; |
| 477 | } |
| 478 | first_trailer_idx--; |
| 479 | } |
| 480 | |
| 481 | if (g_last_idx >= first_trailer_idx) { |
| 482 | BOOT_LOG_WRN("Not enough free space to run swap upgrade"); |
| 483 | bs->swap_type = BOOT_SWAP_TYPE_NONE; |
| 484 | return; |
| 485 | } |
| 486 | } |
| 487 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 488 | image_index = BOOT_CURR_IMG(state); |
| 489 | |
| 490 | rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri); |
| 491 | assert (rc == 0); |
| 492 | |
| 493 | rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec); |
| 494 | assert (rc == 0); |
| 495 | |
| 496 | fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index)); |
| 497 | |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 498 | if (bs->op == BOOT_STATUS_OP_MOVE) { |
| 499 | idx = g_last_idx; |
| 500 | while (idx > 0) { |
| 501 | if (idx <= (g_last_idx - bs->idx + 1)) { |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 502 | 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] | 503 | } |
| 504 | idx--; |
| 505 | } |
| 506 | bs->idx = BOOT_STATUS_IDX_0; |
| 507 | } |
| 508 | |
| 509 | bs->op = BOOT_STATUS_OP_SWAP; |
| 510 | |
| 511 | idx = 1; |
| 512 | while (idx <= g_last_idx) { |
| 513 | if (idx >= bs->idx) { |
Fabio Utzig | 9e1db9a | 2020-01-02 21:14:22 -0300 | [diff] [blame] | 514 | boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec); |
Fabio Utzig | 74aef31 | 2019-11-28 11:05:34 -0300 | [diff] [blame] | 515 | } |
| 516 | idx++; |
| 517 | } |
| 518 | |
| 519 | flash_area_close(fap_pri); |
| 520 | flash_area_close(fap_sec); |
| 521 | } |
| 522 | |
| 523 | #endif |