blob: a063a0cd57b099c41f5329e5852428319e32e5ce [file] [log] [blame]
Fabio Utzig74aef312019-11-28 11:05:34 -03001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
Fabio Utzig74aef312019-11-28 11:05:34 -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
Fabio Utzig74aef312019-11-28 11:05:34 -030019#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Ă­aa4b4b0f2021-06-22 10:00:22 +020031BOOT_LOG_MODULE_DECLARE(mcuboot);
Fabio Utzig74aef312019-11-28 11:05:34 -030032
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 */
39int 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 Utzig74530752023-02-07 20:09:37 -030050uint32_t
51find_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 Utzig74aef312019-11-28 11:05:34 -030070
71int
72boot_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 Utzig74530752023-02-07 20:09:37 -030078 uint32_t last_idx;
79 uint32_t swap_size;
Fabio Utzig74aef312019-11-28 11:05:34 -030080 int area_id;
81 int rc;
82
83#if (BOOT_IMAGE_NUMBER == 1)
84 (void)state;
85#endif
86
87 off = 0;
Fabio Utzig74530752023-02-07 20:09:37 -030088 if (bs && !boot_status_is_reset(bs)) {
89 rc = boot_read_swap_size(BOOT_CURR_IMG(state), &swap_size);
90 if (rc) {
91 rc = BOOT_EFLASH;
92 goto done;
93 }
94
95 last_idx = find_last_idx(state, swap_size);
Fabio Utzig74aef312019-11-28 11:05:34 -030096 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74530752023-02-07 20:09:37 -030097
98 /*
99 * Find the correct offset or slot where the image header is expected to
100 * be found for the steps where it is moved or swapped.
101 */
102 if (bs->op == BOOT_STATUS_OP_MOVE && slot == 0 && bs->idx > last_idx) {
103 off = sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300104 } else if (bs->op == BOOT_STATUS_OP_SWAP) {
Fabio Utzig74530752023-02-07 20:09:37 -0300105 if (bs->idx > 1 && bs->idx <= last_idx) {
106 slot = (slot == 0) ? 1 : 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300107 } else if (bs->idx == 1) {
108 if (slot == 0) {
109 off = sz;
Fabio Utzig74530752023-02-07 20:09:37 -0300110 } else if (slot == 1 && bs->state == 2) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300111 slot = 0;
112 }
113 }
114 }
115 }
116
117 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
118 rc = flash_area_open(area_id, &fap);
119 if (rc != 0) {
120 rc = BOOT_EFLASH;
121 goto done;
122 }
123
124 rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr);
125 if (rc != 0) {
126 rc = BOOT_EFLASH;
127 goto done;
128 }
129
130 /* We only know where the headers are located when bs is valid */
131 if (bs != NULL && out_hdr->ih_magic != IMAGE_MAGIC) {
132 rc = -1;
133 goto done;
134 }
135
136 rc = 0;
137
138done:
139 flash_area_close(fap);
140 return rc;
141}
142
143int
144swap_read_status_bytes(const struct flash_area *fap,
145 struct boot_loader_state *state, struct boot_status *bs)
146{
147 uint32_t off;
148 uint8_t status;
149 int max_entries;
150 int found_idx;
151 uint8_t write_sz;
152 int move_entries;
153 int rc;
154 int last_rc;
155 int erased_sections;
156 int i;
157
158 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
159 if (max_entries < 0) {
160 return BOOT_EBADARGS;
161 }
162
163 erased_sections = 0;
164 found_idx = -1;
165 /* skip erased sectors at the end */
166 last_rc = 1;
167 write_sz = BOOT_WRITE_SZ(state);
168 off = boot_status_off(fap);
169 for (i = max_entries; i > 0; i--) {
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300170 rc = flash_area_read(fap, off + (i - 1) * write_sz, &status, 1);
Fabio Utzig74aef312019-11-28 11:05:34 -0300171 if (rc < 0) {
172 return BOOT_EFLASH;
173 }
174
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300175 if (bootutil_buffer_is_erased(fap, &status, 1)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300176 if (rc != last_rc) {
177 erased_sections++;
178 }
179 } else {
180 if (found_idx == -1) {
181 found_idx = i;
182 }
183 }
184 last_rc = rc;
185 }
186
187 if (erased_sections > 1) {
188 /* This means there was an error writing status on the last
189 * swap. Tell user and move on to validation!
190 */
David Brown098de832019-12-10 11:58:01 -0700191#if !defined(__BOOTSIM__)
Fabio Utzig74aef312019-11-28 11:05:34 -0300192 BOOT_LOG_ERR("Detected inconsistent status!");
David Brown098de832019-12-10 11:58:01 -0700193#endif
Fabio Utzig74aef312019-11-28 11:05:34 -0300194
195#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
196 /* With validation of the primary slot disabled, there is no way
197 * to be sure the swapped primary slot is OK, so abort!
198 */
199 assert(0);
200#endif
201 }
202
203 move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT;
204 if (found_idx == -1) {
205 /* no swap status found; nothing to do */
206 } else if (found_idx < move_entries) {
207 bs->op = BOOT_STATUS_OP_MOVE;
208 bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0;
209 bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;;
210 } else {
211 bs->op = BOOT_STATUS_OP_SWAP;
212 bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0;
213 bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0;
214 }
215
216 return 0;
217}
218
219uint32_t
220boot_status_internal_off(const struct boot_status *bs, int elem_sz)
221{
222 uint32_t off;
223 int idx_sz;
224
225 idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ?
226 BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT);
227
228 off = ((bs->op == BOOT_STATUS_OP_MOVE) ?
229 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) +
230 (bs->idx - BOOT_STATUS_IDX_0) * idx_sz +
231 (bs->state - BOOT_STATUS_STATE_0) * elem_sz;
232
233 return off;
234}
235
236int
237boot_slots_compatible(struct boot_loader_state *state)
238{
Fabio Utzigad055d12020-06-29 20:19:26 -0300239 size_t num_sectors_pri;
240 size_t num_sectors_sec;
241 size_t sector_sz_pri = 0;
242 size_t sector_sz_sec = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300243 size_t i;
244
Fabio Utzigad055d12020-06-29 20:19:26 -0300245 num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
246 num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
247 if ((num_sectors_pri != num_sectors_sec) &&
248 (num_sectors_pri != (num_sectors_sec + 1))) {
249 BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors");
Fabio Utzig74aef312019-11-28 11:05:34 -0300250 return 0;
251 }
252
Fabio Utzigad055d12020-06-29 20:19:26 -0300253 if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300254 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
255 return 0;
256 }
257
Fabio Utzigad055d12020-06-29 20:19:26 -0300258 for (i = 0; i < num_sectors_sec; i++) {
259 sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
260 sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i);
261 if (sector_sz_pri != sector_sz_sec) {
262 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
263 return 0;
264 }
265 }
266
267 if (num_sectors_pri > num_sectors_sec) {
268 if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300269 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
270 return 0;
271 }
272 }
273
274 return 1;
275}
276
277#define BOOT_LOG_SWAP_STATE(area, state) \
278 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
279 "image_ok=0x%x", \
280 (area), \
281 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
282 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
283 "bad"), \
284 (state)->swap_type, \
285 (state)->copy_done, \
286 (state)->image_ok)
287
288int
289swap_status_source(struct boot_loader_state *state)
290{
291 struct boot_swap_state state_primary_slot;
Fabio Utzigce115972020-10-28 09:05:20 -0300292 struct boot_swap_state state_secondary_slot;
Fabio Utzig74aef312019-11-28 11:05:34 -0300293 int rc;
294 uint8_t source;
295 uint8_t image_index;
296
297#if (BOOT_IMAGE_NUMBER == 1)
298 (void)state;
299#endif
300
301 image_index = BOOT_CURR_IMG(state);
302
303 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
304 &state_primary_slot);
305 assert(rc == 0);
306
307 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
308
Fabio Utzigce115972020-10-28 09:05:20 -0300309 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
310 &state_secondary_slot);
311 assert(rc == 0);
312
313 BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot);
314
Fabio Utzig74aef312019-11-28 11:05:34 -0300315 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
Fabio Utzigce115972020-10-28 09:05:20 -0300316 state_primary_slot.copy_done == BOOT_FLAG_UNSET &&
317 state_secondary_slot.magic != BOOT_MAGIC_GOOD) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300318
319 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
320
321 BOOT_LOG_INF("Boot source: primary slot");
322 return source;
323 }
324
325 BOOT_LOG_INF("Boot source: none");
326 return BOOT_STATUS_SOURCE_NONE;
327}
328
329/*
330 * "Moves" the sector located at idx - 1 to idx.
331 */
332static void
333boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
334 struct boot_status *bs, const struct flash_area *fap_pri,
335 const struct flash_area *fap_sec)
336{
337 uint32_t new_off;
338 uint32_t old_off;
339 int rc;
340
341 /*
342 * FIXME: assuming sectors of size == sz, a single off variable
343 * would be enough
344 */
345
346 /* Calculate offset from start of image area. */
347 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
348 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
349
350 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300351 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
352 rc = swap_erase_trailer_sectors(state, fap_pri);
353 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300354
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300355 rc = swap_status_init(state, fap_pri, bs);
356 assert(rc == 0);
357 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300358
359 rc = swap_erase_trailer_sectors(state, fap_sec);
360 assert(rc == 0);
361 }
362
363 rc = boot_erase_region(fap_pri, new_off, sz);
364 assert(rc == 0);
365
366 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
367 assert(rc == 0);
368
369 rc = boot_write_status(state, bs);
370
371 bs->idx++;
372 BOOT_STATUS_ASSERT(rc == 0);
373}
374
375static void
376boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
377 struct boot_status *bs, const struct flash_area *fap_pri,
378 const struct flash_area *fap_sec)
379{
380 uint32_t pri_off;
381 uint32_t pri_up_off;
382 uint32_t sec_off;
383 int rc;
384
385 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
386 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
387 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
388
389 if (bs->state == BOOT_STATUS_STATE_0) {
390 rc = boot_erase_region(fap_pri, pri_off, sz);
391 assert(rc == 0);
392
393 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
394 assert(rc == 0);
395
396 rc = boot_write_status(state, bs);
397 bs->state = BOOT_STATUS_STATE_1;
398 BOOT_STATUS_ASSERT(rc == 0);
399 }
400
401 if (bs->state == BOOT_STATUS_STATE_1) {
402 rc = boot_erase_region(fap_sec, sec_off, sz);
403 assert(rc == 0);
404
405 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
406 assert(rc == 0);
407
408 rc = boot_write_status(state, bs);
409 bs->idx++;
410 bs->state = BOOT_STATUS_STATE_0;
411 BOOT_STATUS_ASSERT(rc == 0);
412 }
413}
414
415/*
416 * When starting a revert the swap status exists in the primary slot, and
417 * the status in the secondary slot is erased. To start the swap, the status
418 * area in the primary slot must be re-initialized; if during the small
419 * window of time between re-initializing it and writing the first metadata
420 * a reset happens, the swap process is broken and cannot be resumed.
421 *
422 * This function handles the issue by making the revert look like a permanent
423 * upgrade (by initializing the secondary slot).
424 */
425void
426fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
Dominik Ermel0ab87b62021-05-25 11:50:04 +0000427 const struct flash_area *fap_sec)
Fabio Utzig74aef312019-11-28 11:05:34 -0300428{
429 struct boot_swap_state swap_state;
430 int rc;
431
432#if (BOOT_IMAGE_NUMBER == 1)
433 (void)state;
434#endif
435
436 /* No fixup required */
437 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
438 bs->op != BOOT_STATUS_OP_MOVE ||
439 bs->idx != BOOT_STATUS_IDX_0) {
440 return;
441 }
442
Dominik Ermel0ab87b62021-05-25 11:50:04 +0000443 rc = boot_read_swap_state(fap_sec, &swap_state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300444 assert(rc == 0);
445
446 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
447
448 if (swap_state.magic == BOOT_MAGIC_UNSET) {
449 rc = swap_erase_trailer_sectors(state, fap_sec);
450 assert(rc == 0);
451
452 rc = boot_write_image_ok(fap_sec);
453 assert(rc == 0);
454
455 rc = boot_write_swap_size(fap_sec, bs->swap_size);
456 assert(rc == 0);
457
Fabio Utzig74aef312019-11-28 11:05:34 -0300458 rc = boot_write_magic(fap_sec);
459 assert(rc == 0);
460 }
461}
462
463void
464swap_run(struct boot_loader_state *state, struct boot_status *bs,
465 uint32_t copy_size)
466{
467 uint32_t sz;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300468 uint32_t sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300469 uint32_t idx;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300470 uint32_t trailer_sz;
471 uint32_t first_trailer_idx;
Fabio Utzig74530752023-02-07 20:09:37 -0300472 uint32_t last_idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300473 uint8_t image_index;
474 const struct flash_area *fap_pri;
475 const struct flash_area *fap_sec;
476 int rc;
477
Andrzej Puzdrowskif9dbf682021-12-23 12:32:28 +0100478 BOOT_LOG_INF("Starting swap using move algorithm.");
479
Fabio Utzig74530752023-02-07 20:09:37 -0300480 last_idx = find_last_idx(state, copy_size);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300481 sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300482
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300483 /*
484 * When starting a new swap upgrade, check that there is enough space.
485 */
486 if (boot_status_is_reset(bs)) {
487 sz = 0;
488 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
489 first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
490
491 while (1) {
492 sz += sector_sz;
493 if (sz >= trailer_sz) {
494 break;
495 }
496 first_trailer_idx--;
497 }
498
Fabio Utzig74530752023-02-07 20:09:37 -0300499 if (last_idx >= first_trailer_idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300500 BOOT_LOG_WRN("Not enough free space to run swap upgrade");
Andrzej Puzdrowskif9dbf682021-12-23 12:32:28 +0100501 BOOT_LOG_WRN("required %d bytes but only %d are available",
Fabio Utzig74530752023-02-07 20:09:37 -0300502 (last_idx + 1) * sector_sz,
Andrzej Puzdrowskif9dbf682021-12-23 12:32:28 +0100503 first_trailer_idx * sector_sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300504 bs->swap_type = BOOT_SWAP_TYPE_NONE;
505 return;
506 }
507 }
508
Fabio Utzig74aef312019-11-28 11:05:34 -0300509 image_index = BOOT_CURR_IMG(state);
510
511 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
512 assert (rc == 0);
513
514 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
515 assert (rc == 0);
516
Dominik Ermel0ab87b62021-05-25 11:50:04 +0000517 fixup_revert(state, bs, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300518
Fabio Utzig74aef312019-11-28 11:05:34 -0300519 if (bs->op == BOOT_STATUS_OP_MOVE) {
Fabio Utzig74530752023-02-07 20:09:37 -0300520 idx = last_idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300521 while (idx > 0) {
Fabio Utzig74530752023-02-07 20:09:37 -0300522 if (idx <= (last_idx - bs->idx + 1)) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300523 boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300524 }
525 idx--;
526 }
527 bs->idx = BOOT_STATUS_IDX_0;
528 }
529
530 bs->op = BOOT_STATUS_OP_SWAP;
531
532 idx = 1;
Fabio Utzig74530752023-02-07 20:09:37 -0300533 while (idx <= last_idx) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300534 if (idx >= bs->idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300535 boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300536 }
537 idx++;
538 }
539
540 flash_area_close(fap_pri);
541 flash_area_close(fap_sec);
542}
543
544#endif