blob: 8fe0b44c5f9c51c843889f1cbfe6029eeb37eb64 [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
34MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
35
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;
131 uint8_t write_sz;
132 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
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300155 if (bootutil_buffer_is_erased(fap, &status, 1)) {
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
200boot_status_internal_off(const struct boot_status *bs, int elem_sz)
201{
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) {
231 BOOT_LOG_WRN("Upgrade disabled for image %d", BOOT_CURR_IMG(state));
232 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
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
276int
277swap_status_source(struct boot_loader_state *state)
278{
279 struct boot_swap_state state_primary_slot;
Fabio Utzigce115972020-10-28 09:05:20 -0300280 struct boot_swap_state state_secondary_slot;
Fabio Utzig74aef312019-11-28 11:05:34 -0300281 int rc;
282 uint8_t source;
283 uint8_t image_index;
284
285#if (BOOT_IMAGE_NUMBER == 1)
286 (void)state;
287#endif
288
289 image_index = BOOT_CURR_IMG(state);
290
291 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
292 &state_primary_slot);
293 assert(rc == 0);
294
295 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
296
Fabio Utzigce115972020-10-28 09:05:20 -0300297 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
298 &state_secondary_slot);
299 assert(rc == 0);
300
301 BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot);
302
Fabio Utzig74aef312019-11-28 11:05:34 -0300303 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
Fabio Utzigce115972020-10-28 09:05:20 -0300304 state_primary_slot.copy_done == BOOT_FLAG_UNSET &&
305 state_secondary_slot.magic != BOOT_MAGIC_GOOD) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300306
307 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
308
309 BOOT_LOG_INF("Boot source: primary slot");
310 return source;
311 }
312
313 BOOT_LOG_INF("Boot source: none");
314 return BOOT_STATUS_SOURCE_NONE;
315}
316
317/*
318 * "Moves" the sector located at idx - 1 to idx.
319 */
320static void
321boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
322 struct boot_status *bs, const struct flash_area *fap_pri,
323 const struct flash_area *fap_sec)
324{
325 uint32_t new_off;
326 uint32_t old_off;
327 int rc;
328
329 /*
330 * FIXME: assuming sectors of size == sz, a single off variable
331 * would be enough
332 */
333
334 /* Calculate offset from start of image area. */
335 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
336 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
337
338 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300339 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
340 rc = swap_erase_trailer_sectors(state, fap_pri);
341 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300342
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300343 rc = swap_status_init(state, fap_pri, bs);
344 assert(rc == 0);
345 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300346
347 rc = swap_erase_trailer_sectors(state, fap_sec);
348 assert(rc == 0);
349 }
350
351 rc = boot_erase_region(fap_pri, new_off, sz);
352 assert(rc == 0);
353
354 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
355 assert(rc == 0);
356
357 rc = boot_write_status(state, bs);
358
359 bs->idx++;
360 BOOT_STATUS_ASSERT(rc == 0);
361}
362
363static void
364boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
365 struct boot_status *bs, const struct flash_area *fap_pri,
366 const struct flash_area *fap_sec)
367{
368 uint32_t pri_off;
369 uint32_t pri_up_off;
370 uint32_t sec_off;
371 int rc;
372
373 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
374 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
375 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
376
377 if (bs->state == BOOT_STATUS_STATE_0) {
378 rc = boot_erase_region(fap_pri, pri_off, sz);
379 assert(rc == 0);
380
381 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
382 assert(rc == 0);
383
384 rc = boot_write_status(state, bs);
385 bs->state = BOOT_STATUS_STATE_1;
386 BOOT_STATUS_ASSERT(rc == 0);
387 }
388
389 if (bs->state == BOOT_STATUS_STATE_1) {
390 rc = boot_erase_region(fap_sec, sec_off, sz);
391 assert(rc == 0);
392
393 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
394 assert(rc == 0);
395
396 rc = boot_write_status(state, bs);
397 bs->idx++;
398 bs->state = BOOT_STATUS_STATE_0;
399 BOOT_STATUS_ASSERT(rc == 0);
400 }
401}
402
403/*
404 * When starting a revert the swap status exists in the primary slot, and
405 * the status in the secondary slot is erased. To start the swap, the status
406 * area in the primary slot must be re-initialized; if during the small
407 * window of time between re-initializing it and writing the first metadata
408 * a reset happens, the swap process is broken and cannot be resumed.
409 *
410 * This function handles the issue by making the revert look like a permanent
411 * upgrade (by initializing the secondary slot).
412 */
413void
414fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
415 const struct flash_area *fap_sec, uint8_t sec_id)
416{
417 struct boot_swap_state swap_state;
418 int rc;
419
420#if (BOOT_IMAGE_NUMBER == 1)
421 (void)state;
422#endif
423
424 /* No fixup required */
425 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
426 bs->op != BOOT_STATUS_OP_MOVE ||
427 bs->idx != BOOT_STATUS_IDX_0) {
428 return;
429 }
430
431 rc = boot_read_swap_state_by_id(sec_id, &swap_state);
432 assert(rc == 0);
433
434 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
435
436 if (swap_state.magic == BOOT_MAGIC_UNSET) {
437 rc = swap_erase_trailer_sectors(state, fap_sec);
438 assert(rc == 0);
439
440 rc = boot_write_image_ok(fap_sec);
441 assert(rc == 0);
442
443 rc = boot_write_swap_size(fap_sec, bs->swap_size);
444 assert(rc == 0);
445
Fabio Utzig74aef312019-11-28 11:05:34 -0300446 rc = boot_write_magic(fap_sec);
447 assert(rc == 0);
448 }
449}
450
451void
452swap_run(struct boot_loader_state *state, struct boot_status *bs,
453 uint32_t copy_size)
454{
455 uint32_t sz;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300456 uint32_t sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300457 uint32_t idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300458 uint8_t image_index;
459 const struct flash_area *fap_pri;
460 const struct flash_area *fap_sec;
461 int rc;
462
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300463 sz = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300464 g_last_idx = 0;
465
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300466 sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300467 while (1) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300468 sz += sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300469 /* Skip to next sector because all sectors will be moved up. */
470 g_last_idx++;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300471 if (sz >= copy_size) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300472 break;
473 }
474 }
475
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200476#ifndef MCUBOOT_SWAP_USING_STATUS
477 uint32_t trailer_sz;
478 uint32_t first_trailer_idx;
479
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300480 /*
481 * When starting a new swap upgrade, check that there is enough space.
482 */
483 if (boot_status_is_reset(bs)) {
484 sz = 0;
485 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
486 first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
487
488 while (1) {
489 sz += sector_sz;
490 if (sz >= trailer_sz) {
491 break;
492 }
493 first_trailer_idx--;
494 }
495
496 if (g_last_idx >= first_trailer_idx) {
497 BOOT_LOG_WRN("Not enough free space to run swap upgrade");
498 bs->swap_type = BOOT_SWAP_TYPE_NONE;
499 return;
500 }
501 }
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200502#endif
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300503
Fabio Utzig74aef312019-11-28 11:05:34 -0300504 image_index = BOOT_CURR_IMG(state);
505
506 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
507 assert (rc == 0);
508
509 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
510 assert (rc == 0);
511
512 fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index));
513
Fabio Utzig74aef312019-11-28 11:05:34 -0300514 if (bs->op == BOOT_STATUS_OP_MOVE) {
515 idx = g_last_idx;
516 while (idx > 0) {
517 if (idx <= (g_last_idx - bs->idx + 1)) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300518 boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300519 }
520 idx--;
521 }
522 bs->idx = BOOT_STATUS_IDX_0;
523 }
524
525 bs->op = BOOT_STATUS_OP_SWAP;
526
527 idx = 1;
528 while (idx <= g_last_idx) {
529 if (idx >= bs->idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300530 boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300531 }
532 idx++;
533 }
534
535 flash_area_close(fap_pri);
536 flash_area_close(fap_sec);
537}
538
539#endif