blob: a4ce84b4df2bc218c96e0d1a67feeb1e88ca47f9 [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"
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +020027#ifdef MCUBOOT_SWAP_USING_STATUS
28#include "swap_status.h"
29#endif
Fabio Utzig74aef312019-11-28 11:05:34 -030030#include "bootutil/bootutil_log.h"
31
32#include "mcuboot_config/mcuboot_config.h"
33
Roman Okhrimenko977b3752022-03-31 14:40:48 +030034BOOT_LOG_MODULE_DECLARE(mcuboot);
Fabio Utzig74aef312019-11-28 11:05:34 -030035
36#ifdef MCUBOOT_SWAP_USING_MOVE
37
38#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
39/*
40 * FIXME: this might have to be updated for threaded sim
41 */
42int boot_status_fails = 0;
43#define BOOT_STATUS_ASSERT(x) \
44 do { \
45 if (!(x)) { \
46 boot_status_fails++; \
47 } \
48 } while (0)
49#else
50#define BOOT_STATUS_ASSERT(x) ASSERT(x)
51#endif
52
53static uint32_t g_last_idx = UINT32_MAX;
54
55int
56boot_read_image_header(struct boot_loader_state *state, int slot,
57 struct image_header *out_hdr, struct boot_status *bs)
58{
59 const struct flash_area *fap;
60 uint32_t off;
61 uint32_t sz;
62 int area_id;
63 int rc;
64
65#if (BOOT_IMAGE_NUMBER == 1)
66 (void)state;
67#endif
68
69 off = 0;
70 if (bs) {
71 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
72 if (bs->op == BOOT_STATUS_OP_MOVE) {
73 if (slot == 0 && bs->idx > g_last_idx) {
74 /* second sector */
75 off = sz;
76 }
77 } else if (bs->op == BOOT_STATUS_OP_SWAP) {
78 if (bs->idx > 1 && bs->idx <= g_last_idx) {
79 if (slot == 0) {
80 slot = 1;
81 } else {
82 slot = 0;
83 }
84 } else if (bs->idx == 1) {
85 if (slot == 0) {
86 off = sz;
87 }
88 if (slot == 1 && bs->state == 2) {
89 slot = 0;
90 }
91 }
92 }
93 }
94
95 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
96 rc = flash_area_open(area_id, &fap);
97 if (rc != 0) {
98 rc = BOOT_EFLASH;
99 goto done;
100 }
101
102 rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr);
103 if (rc != 0) {
104 rc = BOOT_EFLASH;
105 goto done;
106 }
107
108 /* We only know where the headers are located when bs is valid */
109 if (bs != NULL && out_hdr->ih_magic != IMAGE_MAGIC) {
110 rc = -1;
111 goto done;
112 }
113
114 rc = 0;
115
116done:
117 flash_area_close(fap);
118 return rc;
119}
120
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200121#ifndef MCUBOOT_SWAP_USING_STATUS
122
Fabio Utzig74aef312019-11-28 11:05:34 -0300123int
124swap_read_status_bytes(const struct flash_area *fap,
125 struct boot_loader_state *state, struct boot_status *bs)
126{
127 uint32_t off;
128 uint8_t status;
129 int max_entries;
130 int found_idx;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300131 uint32_t write_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300132 int move_entries;
133 int rc;
134 int last_rc;
135 int erased_sections;
136 int i;
137
138 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
139 if (max_entries < 0) {
140 return BOOT_EBADARGS;
141 }
142
143 erased_sections = 0;
144 found_idx = -1;
145 /* skip erased sectors at the end */
146 last_rc = 1;
147 write_sz = BOOT_WRITE_SZ(state);
148 off = boot_status_off(fap);
149 for (i = max_entries; i > 0; i--) {
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300150 rc = flash_area_read(fap, off + (i - 1) * write_sz, &status, 1);
Fabio Utzig74aef312019-11-28 11:05:34 -0300151 if (rc < 0) {
152 return BOOT_EFLASH;
153 }
154
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300155 if (flash_area_erased_val(fap) == status) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300156 if (rc != last_rc) {
157 erased_sections++;
158 }
159 } else {
160 if (found_idx == -1) {
161 found_idx = i;
162 }
163 }
164 last_rc = rc;
165 }
166
167 if (erased_sections > 1) {
168 /* This means there was an error writing status on the last
169 * swap. Tell user and move on to validation!
170 */
David Brown098de832019-12-10 11:58:01 -0700171#if !defined(__BOOTSIM__)
Fabio Utzig74aef312019-11-28 11:05:34 -0300172 BOOT_LOG_ERR("Detected inconsistent status!");
David Brown098de832019-12-10 11:58:01 -0700173#endif
Fabio Utzig74aef312019-11-28 11:05:34 -0300174
175#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
176 /* With validation of the primary slot disabled, there is no way
177 * to be sure the swapped primary slot is OK, so abort!
178 */
179 assert(0);
180#endif
181 }
182
183 move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT;
184 if (found_idx == -1) {
185 /* no swap status found; nothing to do */
186 } else if (found_idx < move_entries) {
187 bs->op = BOOT_STATUS_OP_MOVE;
188 bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0;
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200189 bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300190 } else {
191 bs->op = BOOT_STATUS_OP_SWAP;
192 bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0;
193 bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0;
194 }
195
196 return 0;
197}
198
199uint32_t
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300200boot_status_internal_off(const struct boot_status *bs, uint32_t elem_sz)
Fabio Utzig74aef312019-11-28 11:05:34 -0300201{
202 uint32_t off;
203 int idx_sz;
204
205 idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ?
206 BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT);
207
208 off = ((bs->op == BOOT_STATUS_OP_MOVE) ?
209 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) +
210 (bs->idx - BOOT_STATUS_IDX_0) * idx_sz +
211 (bs->state - BOOT_STATUS_STATE_0) * elem_sz;
212
213 return off;
214}
215
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200216#endif /* !MCUBOOT_SWAP_USING_STATUS */
217
Fabio Utzig74aef312019-11-28 11:05:34 -0300218int
219boot_slots_compatible(struct boot_loader_state *state)
220{
Fabio Utzigad055d12020-06-29 20:19:26 -0300221 size_t num_sectors_pri;
222 size_t num_sectors_sec;
223 size_t sector_sz_pri = 0;
224 size_t sector_sz_sec = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300225 size_t i;
226
Fabio Utzigad055d12020-06-29 20:19:26 -0300227 num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
228 num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200229
230 if (num_sectors_sec == 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300231 BOOT_LOG_WRN("Upgrade disabled for image %u", (unsigned)BOOT_CURR_IMG(state));
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200232 return 0;
233 }
234
Fabio Utzigad055d12020-06-29 20:19:26 -0300235 if ((num_sectors_pri != num_sectors_sec) &&
236 (num_sectors_pri != (num_sectors_sec + 1))) {
237 BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors");
Fabio Utzig74aef312019-11-28 11:05:34 -0300238 return 0;
239 }
240
Fabio Utzigad055d12020-06-29 20:19:26 -0300241 if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300242 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
243 return 0;
244 }
245
Fabio Utzigad055d12020-06-29 20:19:26 -0300246 for (i = 0; i < num_sectors_sec; i++) {
247 sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
248 sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i);
249 if (sector_sz_pri != sector_sz_sec) {
250 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
251 return 0;
252 }
253 }
254
255 if (num_sectors_pri > num_sectors_sec) {
256 if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300257 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
258 return 0;
259 }
260 }
261
262 return 1;
263}
264
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300265#define BOOT_LOG_SWAP_STATE(area, state) \
266 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, image_ok=0x%x", \
267 (area), \
268 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
269 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
270 "bad"), \
271 (unsigned)(state)->swap_type, \
272 (unsigned)(state)->copy_done, \
273 (unsigned)(state)->image_ok)
Fabio Utzig74aef312019-11-28 11:05:34 -0300274
275int
276swap_status_source(struct boot_loader_state *state)
277{
278 struct boot_swap_state state_primary_slot;
Fabio Utzigce115972020-10-28 09:05:20 -0300279 struct boot_swap_state state_secondary_slot;
Fabio Utzig74aef312019-11-28 11:05:34 -0300280 int rc;
281 uint8_t source;
282 uint8_t image_index;
283
284#if (BOOT_IMAGE_NUMBER == 1)
285 (void)state;
286#endif
287
288 image_index = BOOT_CURR_IMG(state);
289
290 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
291 &state_primary_slot);
292 assert(rc == 0);
293
294 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
295
Fabio Utzigce115972020-10-28 09:05:20 -0300296 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
297 &state_secondary_slot);
298 assert(rc == 0);
299
300 BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot);
301
Fabio Utzig74aef312019-11-28 11:05:34 -0300302 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
Fabio Utzigce115972020-10-28 09:05:20 -0300303 state_primary_slot.copy_done == BOOT_FLAG_UNSET &&
304 state_secondary_slot.magic != BOOT_MAGIC_GOOD) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300305
306 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
307
308 BOOT_LOG_INF("Boot source: primary slot");
309 return source;
310 }
311
312 BOOT_LOG_INF("Boot source: none");
313 return BOOT_STATUS_SOURCE_NONE;
314}
315
316/*
317 * "Moves" the sector located at idx - 1 to idx.
318 */
319static void
320boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
321 struct boot_status *bs, const struct flash_area *fap_pri,
322 const struct flash_area *fap_sec)
323{
324 uint32_t new_off;
325 uint32_t old_off;
326 int rc;
327
328 /*
329 * FIXME: assuming sectors of size == sz, a single off variable
330 * would be enough
331 */
332
333 /* Calculate offset from start of image area. */
334 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
335 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
336
337 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300338 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
339 rc = swap_erase_trailer_sectors(state, fap_pri);
340 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300341
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300342 rc = swap_status_init(state, fap_pri, bs);
343 assert(rc == 0);
344 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300345
346 rc = swap_erase_trailer_sectors(state, fap_sec);
347 assert(rc == 0);
348 }
349
350 rc = boot_erase_region(fap_pri, new_off, sz);
351 assert(rc == 0);
352
353 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
354 assert(rc == 0);
355
356 rc = boot_write_status(state, bs);
357
358 bs->idx++;
359 BOOT_STATUS_ASSERT(rc == 0);
360}
361
362static void
363boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
364 struct boot_status *bs, const struct flash_area *fap_pri,
365 const struct flash_area *fap_sec)
366{
367 uint32_t pri_off;
368 uint32_t pri_up_off;
369 uint32_t sec_off;
370 int rc;
371
372 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
373 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
374 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
375
376 if (bs->state == BOOT_STATUS_STATE_0) {
377 rc = boot_erase_region(fap_pri, pri_off, sz);
378 assert(rc == 0);
379
380 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
381 assert(rc == 0);
382
383 rc = boot_write_status(state, bs);
384 bs->state = BOOT_STATUS_STATE_1;
385 BOOT_STATUS_ASSERT(rc == 0);
386 }
387
388 if (bs->state == BOOT_STATUS_STATE_1) {
389 rc = boot_erase_region(fap_sec, sec_off, sz);
390 assert(rc == 0);
391
392 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
393 assert(rc == 0);
394
395 rc = boot_write_status(state, bs);
396 bs->idx++;
397 bs->state = BOOT_STATUS_STATE_0;
398 BOOT_STATUS_ASSERT(rc == 0);
399 }
400}
401
402/*
403 * When starting a revert the swap status exists in the primary slot, and
404 * the status in the secondary slot is erased. To start the swap, the status
405 * area in the primary slot must be re-initialized; if during the small
406 * window of time between re-initializing it and writing the first metadata
407 * a reset happens, the swap process is broken and cannot be resumed.
408 *
409 * This function handles the issue by making the revert look like a permanent
410 * upgrade (by initializing the secondary slot).
411 */
412void
413fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300414 const struct flash_area *fap_sec)
Fabio Utzig74aef312019-11-28 11:05:34 -0300415{
416 struct boot_swap_state swap_state;
417 int rc;
418
419#if (BOOT_IMAGE_NUMBER == 1)
420 (void)state;
421#endif
422
423 /* No fixup required */
424 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
425 bs->op != BOOT_STATUS_OP_MOVE ||
426 bs->idx != BOOT_STATUS_IDX_0) {
427 return;
428 }
429
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300430 rc = boot_read_swap_state(fap_sec, &swap_state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300431 assert(rc == 0);
432
433 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
434
435 if (swap_state.magic == BOOT_MAGIC_UNSET) {
436 rc = swap_erase_trailer_sectors(state, fap_sec);
437 assert(rc == 0);
438
439 rc = boot_write_image_ok(fap_sec);
440 assert(rc == 0);
441
442 rc = boot_write_swap_size(fap_sec, bs->swap_size);
443 assert(rc == 0);
444
Fabio Utzig74aef312019-11-28 11:05:34 -0300445 rc = boot_write_magic(fap_sec);
446 assert(rc == 0);
447 }
448}
449
450void
451swap_run(struct boot_loader_state *state, struct boot_status *bs,
452 uint32_t copy_size)
453{
454 uint32_t sz;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300455 uint32_t sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300456 uint32_t idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300457 uint8_t image_index;
458 const struct flash_area *fap_pri;
459 const struct flash_area *fap_sec;
460 int rc;
461
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300462 BOOT_LOG_INF("Starting swap using move algorithm.");
463
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300464 sz = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300465 g_last_idx = 0;
466
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300467 sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300468 while (1) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300469 sz += sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300470 /* Skip to next sector because all sectors will be moved up. */
471 g_last_idx++;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300472 if (sz >= copy_size) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300473 break;
474 }
475 }
476
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200477#ifndef MCUBOOT_SWAP_USING_STATUS
478 uint32_t trailer_sz;
479 uint32_t first_trailer_idx;
480
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300481 /*
482 * When starting a new swap upgrade, check that there is enough space.
483 */
484 if (boot_status_is_reset(bs)) {
485 sz = 0;
486 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
487 first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
488
489 while (1) {
490 sz += sector_sz;
491 if (sz >= trailer_sz) {
492 break;
493 }
494 first_trailer_idx--;
495 }
496
497 if (g_last_idx >= first_trailer_idx) {
498 BOOT_LOG_WRN("Not enough free space to run swap upgrade");
Roman Okhrimenkodc0ca082023-06-21 20:49:51 +0300499 BOOT_LOG_WRN("required %d bytes but only %d are available",
500 (g_last_idx + 1) * sector_sz ,
501 first_trailer_idx * sector_sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300502 bs->swap_type = BOOT_SWAP_TYPE_NONE;
503 return;
504 }
505 }
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200506#endif
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300507
Fabio Utzig74aef312019-11-28 11:05:34 -0300508 image_index = BOOT_CURR_IMG(state);
509
510 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
511 assert (rc == 0);
512
513 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
514 assert (rc == 0);
515
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300516 fixup_revert(state, bs, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300517
Fabio Utzig74aef312019-11-28 11:05:34 -0300518 if (bs->op == BOOT_STATUS_OP_MOVE) {
519 idx = g_last_idx;
520 while (idx > 0) {
521 if (idx <= (g_last_idx - bs->idx + 1)) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300522 boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300523 }
524 idx--;
525 }
526 bs->idx = BOOT_STATUS_IDX_0;
527 }
528
529 bs->op = BOOT_STATUS_OP_SWAP;
530
531 idx = 1;
532 while (idx <= g_last_idx) {
533 if (idx >= bs->idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300534 boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300535 }
536 idx++;
537 }
538
539 flash_area_close(fap_pri);
540 flash_area_close(fap_sec);
541}
542
543#endif