blob: 3266dc4ef0283e30c11483cea1fee54b222443b0 [file] [log] [blame]
Fabio Utzig12d59162019-11-28 10:01:59 -03001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
Fabio Utzig12d59162019-11-28 10:01:59 -03004 * 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
19#include <assert.h>
20#include <stddef.h>
21#include <stdbool.h>
22#include <inttypes.h>
23#include <stdlib.h>
24#include <string.h>
25#include "bootutil/bootutil.h"
26#include "bootutil_priv.h"
27#include "swap_priv.h"
28#include "bootutil/bootutil_log.h"
29
30#include "mcuboot_config/mcuboot_config.h"
31
32MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
33
Fabio Utzig74aef312019-11-28 11:05:34 -030034#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzig12d59162019-11-28 10:01:59 -030035
36#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
37/*
38 * FIXME: this might have to be updated for threaded sim
39 */
40int boot_status_fails = 0;
41#define BOOT_STATUS_ASSERT(x) \
42 do { \
43 if (!(x)) { \
44 boot_status_fails++; \
45 } \
46 } while (0)
47#else
48#define BOOT_STATUS_ASSERT(x) ASSERT(x)
49#endif
50
51int
52boot_read_image_header(struct boot_loader_state *state, int slot,
53 struct image_header *out_hdr, struct boot_status *bs)
54{
55 const struct flash_area *fap;
56 int area_id;
57 int rc;
58
59 (void)bs;
60
61#if (BOOT_IMAGE_NUMBER == 1)
62 (void)state;
63#endif
64
65 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
66 rc = flash_area_open(area_id, &fap);
67 if (rc != 0) {
68 rc = BOOT_EFLASH;
69 goto done;
70 }
71
72 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
73 if (rc != 0) {
74 rc = BOOT_EFLASH;
75 goto done;
76 }
77
78 rc = 0;
79
80done:
81 flash_area_close(fap);
82 return rc;
83}
84
85/**
86 * Reads the status of a partially-completed swap, if any. This is necessary
87 * to recover in case the boot lodaer was reset in the middle of a swap
88 * operation.
89 */
90int
91swap_read_status_bytes(const struct flash_area *fap,
92 struct boot_loader_state *state, struct boot_status *bs)
93{
94 uint32_t off;
95 uint8_t status;
96 int max_entries;
97 int found;
98 int found_idx;
99 int invalid;
100 int rc;
101 int i;
102
103 off = boot_status_off(fap);
104 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
105 if (max_entries < 0) {
106 return BOOT_EBADARGS;
107 }
108
109 found = 0;
110 found_idx = 0;
111 invalid = 0;
112 for (i = 0; i < max_entries; i++) {
113 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
114 &status, 1);
115 if (rc < 0) {
116 return BOOT_EFLASH;
117 }
118
119 if (rc == 1) {
120 if (found && !found_idx) {
121 found_idx = i;
122 }
123 } else if (!found) {
124 found = 1;
125 } else if (found_idx) {
126 invalid = 1;
127 break;
128 }
129 }
130
131 if (invalid) {
132 /* This means there was an error writing status on the last
133 * swap. Tell user and move on to validation!
134 */
David Brown098de832019-12-10 11:58:01 -0700135#if !defined(__BOOTSIM__)
Fabio Utzig12d59162019-11-28 10:01:59 -0300136 BOOT_LOG_ERR("Detected inconsistent status!");
David Brown098de832019-12-10 11:58:01 -0700137#endif
Fabio Utzig12d59162019-11-28 10:01:59 -0300138
139#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
140 /* With validation of the primary slot disabled, there is no way
141 * to be sure the swapped primary slot is OK, so abort!
142 */
143 assert(0);
144#endif
145 }
146
147 if (found) {
148 if (!found_idx) {
149 found_idx = i;
150 }
151 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
152 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
153 }
154
155 return 0;
156}
157
158uint32_t
159boot_status_internal_off(const struct boot_status *bs, int elem_sz)
160{
161 int idx_sz;
162
163 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
164
165 return (bs->idx - BOOT_STATUS_IDX_0) * idx_sz +
166 (bs->state - BOOT_STATUS_STATE_0) * elem_sz;
167}
168
169/*
170 * Slots are compatible when all sectors that store up to to size of the image
171 * round up to sector size, in both slot's are able to fit in the scratch
172 * area, and have sizes that are a multiple of each other (powers of two
173 * presumably!).
174 */
175int
176boot_slots_compatible(struct boot_loader_state *state)
177{
178 size_t num_sectors_primary;
179 size_t num_sectors_secondary;
180 size_t sz0, sz1;
181 size_t primary_slot_sz, secondary_slot_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300182#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig12d59162019-11-28 10:01:59 -0300183 size_t scratch_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300184#endif
Fabio Utzig12d59162019-11-28 10:01:59 -0300185 size_t i, j;
186 int8_t smaller;
187
188 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
189 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
190 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
191 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
192 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
193 return 0;
194 }
195
Fabio Utzig74aef312019-11-28 11:05:34 -0300196#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig12d59162019-11-28 10:01:59 -0300197 scratch_sz = boot_scratch_area_size(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300198#endif
Fabio Utzig12d59162019-11-28 10:01:59 -0300199
200 /*
201 * The following loop scans all sectors in a linear fashion, assuring that
202 * for each possible sector in each slot, it is able to fit in the other
203 * slot's sector or sectors. Slot's should be compatible as long as any
204 * number of a slot's sectors are able to fit into another, which only
205 * excludes cases where sector sizes are not a multiple of each other.
206 */
207 i = sz0 = primary_slot_sz = 0;
208 j = sz1 = secondary_slot_sz = 0;
209 smaller = 0;
210 while (i < num_sectors_primary || j < num_sectors_secondary) {
211 if (sz0 == sz1) {
212 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
213 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
214 i++;
215 j++;
216 } else if (sz0 < sz1) {
217 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
218 /* Guarantee that multiple sectors of the secondary slot
219 * fit into the primary slot.
220 */
221 if (smaller == 2) {
222 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
223 return 0;
224 }
225 smaller = 1;
226 i++;
227 } else {
228 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
229 /* Guarantee that multiple sectors of the primary slot
230 * fit into the secondary slot.
231 */
232 if (smaller == 1) {
233 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
234 return 0;
235 }
236 smaller = 2;
237 j++;
238 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300239#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig12d59162019-11-28 10:01:59 -0300240 if (sz0 == sz1) {
241 primary_slot_sz += sz0;
242 secondary_slot_sz += sz1;
243 /* Scratch has to fit each swap operation to the size of the larger
244 * sector among the primary slot and the secondary slot.
245 */
246 if (sz0 > scratch_sz || sz1 > scratch_sz) {
247 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
248 return 0;
249 }
250 smaller = sz0 = sz1 = 0;
251 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300252#endif
Fabio Utzig12d59162019-11-28 10:01:59 -0300253 }
254
255 if ((i != num_sectors_primary) ||
256 (j != num_sectors_secondary) ||
257 (primary_slot_sz != secondary_slot_sz)) {
258 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
259 return 0;
260 }
261
262 return 1;
263}
264
265#define BOOT_LOG_SWAP_STATE(area, state) \
266 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
267 "image_ok=0x%x", \
268 (area), \
269 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
270 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
271 "bad"), \
272 (state)->swap_type, \
273 (state)->copy_done, \
274 (state)->image_ok)
275
276struct boot_status_table {
277 uint8_t bst_magic_primary_slot;
278 uint8_t bst_magic_scratch;
279 uint8_t bst_copy_done_primary_slot;
280 uint8_t bst_status_source;
281};
282
283/**
284 * This set of tables maps swap state contents to boot status location.
285 * When searching for a match, these tables must be iterated in order.
286 */
287static const struct boot_status_table boot_status_tables[] = {
288 {
289 /* | primary slot | scratch |
290 * ----------+--------------+--------------|
291 * magic | Good | Any |
292 * copy-done | Set | N/A |
293 * ----------+--------------+--------------'
294 * source: none |
295 * ----------------------------------------'
296 */
297 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
298 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
299 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
300 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
301 },
302
303 {
304 /* | primary slot | scratch |
305 * ----------+--------------+--------------|
306 * magic | Good | Any |
307 * copy-done | Unset | N/A |
308 * ----------+--------------+--------------'
309 * source: primary slot |
310 * ----------------------------------------'
311 */
312 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
313 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
314 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
315 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
316 },
317
318 {
319 /* | primary slot | scratch |
320 * ----------+--------------+--------------|
321 * magic | Any | Good |
322 * copy-done | Any | N/A |
323 * ----------+--------------+--------------'
324 * source: scratch |
325 * ----------------------------------------'
326 */
327 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
328 .bst_magic_scratch = BOOT_MAGIC_GOOD,
329 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
330 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
331 },
332 {
333 /* | primary slot | scratch |
334 * ----------+--------------+--------------|
335 * magic | Unset | Any |
336 * copy-done | Unset | N/A |
337 * ----------+--------------+--------------|
338 * source: varies |
339 * ----------------------------------------+--------------------------+
340 * This represents one of two cases: |
341 * o No swaps ever (no status to read, so no harm in checking). |
342 * o Mid-revert; status in primary slot. |
343 * -------------------------------------------------------------------'
344 */
345 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
346 .bst_magic_scratch = BOOT_MAGIC_ANY,
347 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
348 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
349 },
350};
351
352#define BOOT_STATUS_TABLES_COUNT \
353 (sizeof boot_status_tables / sizeof boot_status_tables[0])
354
355/**
356 * Determines where in flash the most recent boot status is stored. The boot
357 * status is necessary for completing a swap that was interrupted by a boot
358 * loader reset.
359 *
360 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
361 * be read from.
362 */
363int
364swap_status_source(struct boot_loader_state *state)
365{
366 const struct boot_status_table *table;
367 struct boot_swap_state state_scratch;
368 struct boot_swap_state state_primary_slot;
369 int rc;
370 size_t i;
371 uint8_t source;
372 uint8_t image_index;
373
374#if (BOOT_IMAGE_NUMBER == 1)
375 (void)state;
376#endif
377
378 image_index = BOOT_CURR_IMG(state);
379 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
380 &state_primary_slot);
381 assert(rc == 0);
382
383 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
384 assert(rc == 0);
385
386 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
387 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
388
389 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
390 table = &boot_status_tables[i];
391
392 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
393 state_primary_slot.magic) &&
394 boot_magic_compatible_check(table->bst_magic_scratch,
395 state_scratch.magic) &&
396 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
397 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
398 {
399 source = table->bst_status_source;
400
401#if (BOOT_IMAGE_NUMBER > 1)
402 /* In case of multi-image boot it can happen that if boot status
403 * info is found on scratch area then it does not belong to the
404 * currently examined image.
405 */
406 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
407 state_scratch.image_num != BOOT_CURR_IMG(state)) {
408 source = BOOT_STATUS_SOURCE_NONE;
409 }
410#endif
411
412 BOOT_LOG_INF("Boot source: %s",
413 source == BOOT_STATUS_SOURCE_NONE ? "none" :
414 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
415 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
416 "primary slot" : "BUG; can't happen");
417 return source;
418 }
419 }
420
421 BOOT_LOG_INF("Boot source: none");
422 return BOOT_STATUS_SOURCE_NONE;
423}
424
Fabio Utzig74aef312019-11-28 11:05:34 -0300425#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig12d59162019-11-28 10:01:59 -0300426/**
427 * Calculates the number of sectors the scratch area can contain. A "last"
428 * source sector is specified because images are copied backwards in flash
429 * (final index to index number 0).
430 *
431 * @param last_sector_idx The index of the last source sector
432 * (inclusive).
433 * @param out_first_sector_idx The index of the first source sector
434 * (inclusive) gets written here.
435 *
436 * @return The number of bytes comprised by the
437 * [first-sector, last-sector] range.
438 */
439static uint32_t
440boot_copy_sz(const struct boot_loader_state *state, int last_sector_idx,
441 int *out_first_sector_idx)
442{
443 size_t scratch_sz;
444 uint32_t new_sz;
445 uint32_t sz;
446 int i;
447
448 sz = 0;
449
450 scratch_sz = boot_scratch_area_size(state);
451 for (i = last_sector_idx; i >= 0; i--) {
452 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
453 /*
454 * The secondary slot is not being checked here, because
455 * `boot_slots_compatible` already provides assurance that the copy size
456 * will be compatible with the primary slot and scratch.
457 */
458 if (new_sz > scratch_sz) {
459 break;
460 }
461 sz = new_sz;
462 }
463
464 /* i currently refers to a sector that doesn't fit or it is -1 because all
465 * sectors have been processed. In both cases, exclude sector i.
466 */
467 *out_first_sector_idx = i + 1;
468 return sz;
469}
470
471/**
472 * Swaps the contents of two flash regions within the two image slots.
473 *
474 * @param idx The index of the first sector in the range of
475 * sectors being swapped.
476 * @param sz The number of bytes to swap.
477 * @param bs The current boot status. This struct gets
478 * updated according to the outcome.
479 *
480 * @return 0 on success; nonzero on failure.
481 */
482static void
483boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
484 struct boot_status *bs)
485{
486 const struct flash_area *fap_primary_slot;
487 const struct flash_area *fap_secondary_slot;
488 const struct flash_area *fap_scratch;
489 uint32_t copy_sz;
490 uint32_t trailer_sz;
491 uint32_t img_off;
492 uint32_t scratch_trailer_off;
493 struct boot_swap_state swap_state;
494 size_t last_sector;
495 bool erase_scratch;
496 uint8_t image_index;
497 int rc;
498
499 /* Calculate offset from start of image area. */
500 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
501
502 copy_sz = sz;
503 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
504
505 /* sz in this function is always sized on a multiple of the sector size.
506 * The check against the start offset of the last sector
507 * is to determine if we're swapping the last sector. The last sector
508 * needs special handling because it's where the trailer lives. If we're
509 * copying it, we need to use scratch to write the trailer temporarily.
510 *
511 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
512 * controls if special handling is needed (swapping last sector).
513 */
514 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
515 if ((img_off + sz) >
516 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
517 copy_sz -= trailer_sz;
518 }
519
520 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
521
522 image_index = BOOT_CURR_IMG(state);
523
524 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
525 &fap_primary_slot);
526 assert (rc == 0);
527
528 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
529 &fap_secondary_slot);
530 assert (rc == 0);
531
532 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
533 assert (rc == 0);
534
535 if (bs->state == BOOT_STATUS_STATE_0) {
536 BOOT_LOG_DBG("erasing scratch area");
537 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
538 assert(rc == 0);
539
540 if (bs->idx == BOOT_STATUS_IDX_0) {
541 /* Write a trailer to the scratch area, even if we don't need the
542 * scratch area for status. We need a temporary place to store the
543 * `swap-type` while we erase the primary trailer.
544 */
545 rc = swap_status_init(state, fap_scratch, bs);
546 assert(rc == 0);
547
548 if (!bs->use_scratch) {
549 /* Prepare the primary status area... here it is known that the
550 * last sector is not being used by the image data so it's safe
551 * to erase.
552 */
553 rc = swap_erase_trailer_sectors(state, fap_primary_slot);
554 assert(rc == 0);
555
556 rc = swap_status_init(state, fap_primary_slot, bs);
557 assert(rc == 0);
558
559 /* Erase the temporary trailer from the scratch area. */
560 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
561 assert(rc == 0);
562 }
563 }
564
565 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
566 img_off, 0, copy_sz);
567 assert(rc == 0);
568
569 rc = boot_write_status(state, bs);
570 bs->state = BOOT_STATUS_STATE_1;
571 BOOT_STATUS_ASSERT(rc == 0);
572 }
573
574 if (bs->state == BOOT_STATUS_STATE_1) {
575 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
576 assert(rc == 0);
577
578 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
579 img_off, img_off, copy_sz);
580 assert(rc == 0);
581
582 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
583 /* If not all sectors of the slot are being swapped,
584 * guarantee here that only the primary slot will have the state.
585 */
586 rc = swap_erase_trailer_sectors(state, fap_secondary_slot);
587 assert(rc == 0);
588 }
589
590 rc = boot_write_status(state, bs);
591 bs->state = BOOT_STATUS_STATE_2;
592 BOOT_STATUS_ASSERT(rc == 0);
593 }
594
595 if (bs->state == BOOT_STATUS_STATE_2) {
596 rc = boot_erase_region(fap_primary_slot, img_off, sz);
597 assert(rc == 0);
598
599 /* NOTE: If this is the final sector, we exclude the image trailer from
600 * this copy (copy_sz was truncated earlier).
601 */
602 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
603 0, img_off, copy_sz);
604 assert(rc == 0);
605
606 if (bs->use_scratch) {
607 scratch_trailer_off = boot_status_off(fap_scratch);
608
609 /* copy current status that is being maintained in scratch */
610 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
611 scratch_trailer_off, img_off + copy_sz,
612 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
613 BOOT_STATUS_ASSERT(rc == 0);
614
615 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
616 &swap_state);
617 assert(rc == 0);
618
619 if (swap_state.image_ok == BOOT_FLAG_SET) {
620 rc = boot_write_image_ok(fap_primary_slot);
621 assert(rc == 0);
622 }
623
624 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
625 rc = boot_write_swap_info(fap_primary_slot,
626 swap_state.swap_type, image_index);
627 assert(rc == 0);
628 }
629
630 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
631 assert(rc == 0);
632
633#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -0300634 rc = boot_write_enc_key(fap_primary_slot, 0, bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300635 assert(rc == 0);
636
Fabio Utzig4741c452019-12-19 15:32:41 -0300637 rc = boot_write_enc_key(fap_primary_slot, 1, bs);
Fabio Utzig12d59162019-11-28 10:01:59 -0300638 assert(rc == 0);
639#endif
640 rc = boot_write_magic(fap_primary_slot);
641 assert(rc == 0);
642 }
643
644 /* If we wrote a trailer to the scratch area, erase it after we persist
645 * a trailer to the primary slot. We do this to prevent mcuboot from
646 * reading a stale status from the scratch area in case of immediate
647 * reset.
648 */
649 erase_scratch = bs->use_scratch;
650 bs->use_scratch = 0;
651
652 rc = boot_write_status(state, bs);
653 bs->idx++;
654 bs->state = BOOT_STATUS_STATE_0;
655 BOOT_STATUS_ASSERT(rc == 0);
656
657 if (erase_scratch) {
658 rc = boot_erase_region(fap_scratch, 0, sz);
659 assert(rc == 0);
660 }
661 }
662
663 flash_area_close(fap_primary_slot);
664 flash_area_close(fap_secondary_slot);
665 flash_area_close(fap_scratch);
666}
667
668void
669swap_run(struct boot_loader_state *state, struct boot_status *bs,
670 uint32_t copy_size)
671{
672 uint32_t sz;
673 int first_sector_idx;
674 int last_sector_idx;
675 uint32_t swap_idx;
676 int last_idx_secondary_slot;
677 uint32_t primary_slot_size;
678 uint32_t secondary_slot_size;
679 primary_slot_size = 0;
680 secondary_slot_size = 0;
681 last_sector_idx = 0;
682 last_idx_secondary_slot = 0;
683
684 /*
685 * Knowing the size of the largest image between both slots, here we
686 * find what is the last sector in the primary slot that needs swapping.
687 * Since we already know that both slots are compatible, the secondary
688 * slot's last sector is not really required after this check is finished.
689 */
690 while (1) {
691 if ((primary_slot_size < copy_size) ||
692 (primary_slot_size < secondary_slot_size)) {
693 primary_slot_size += boot_img_sector_size(state,
694 BOOT_PRIMARY_SLOT,
695 last_sector_idx);
696 }
697 if ((secondary_slot_size < copy_size) ||
698 (secondary_slot_size < primary_slot_size)) {
699 secondary_slot_size += boot_img_sector_size(state,
700 BOOT_SECONDARY_SLOT,
701 last_idx_secondary_slot);
702 }
703 if (primary_slot_size >= copy_size &&
704 secondary_slot_size >= copy_size &&
705 primary_slot_size == secondary_slot_size) {
706 break;
707 }
708 last_sector_idx++;
709 last_idx_secondary_slot++;
710 }
711
712 swap_idx = 0;
713 while (last_sector_idx >= 0) {
714 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
715 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
716 boot_swap_sectors(first_sector_idx, sz, state, bs);
717 }
718
719 last_sector_idx = first_sector_idx - 1;
720 swap_idx++;
721 }
722
723}
Fabio Utzig74aef312019-11-28 11:05:34 -0300724#endif
Fabio Utzig12d59162019-11-28 10:01:59 -0300725
726#endif