blob: d7b510801f07f8371757dd5b632ca97c544111b1 [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)) {
Dominik Ermel472d4c72023-02-10 13:29:58 +000089 boot_find_status(BOOT_CURR_IMG(state), &fap);
90 if (fap == NULL || boot_read_swap_size(fap, &swap_size)) {
Fabio Utzig74530752023-02-07 20:09:37 -030091 rc = BOOT_EFLASH;
92 goto done;
93 }
Dominik Ermel472d4c72023-02-10 13:29:58 +000094 flash_area_close(fap);
Fabio Utzig74530752023-02-07 20:09:37 -030095
96 last_idx = find_last_idx(state, swap_size);
Fabio Utzig74aef312019-11-28 11:05:34 -030097 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74530752023-02-07 20:09:37 -030098
99 /*
100 * Find the correct offset or slot where the image header is expected to
101 * be found for the steps where it is moved or swapped.
102 */
103 if (bs->op == BOOT_STATUS_OP_MOVE && slot == 0 && bs->idx > last_idx) {
104 off = sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300105 } else if (bs->op == BOOT_STATUS_OP_SWAP) {
Fabio Utzig74530752023-02-07 20:09:37 -0300106 if (bs->idx > 1 && bs->idx <= last_idx) {
107 slot = (slot == 0) ? 1 : 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300108 } else if (bs->idx == 1) {
109 if (slot == 0) {
110 off = sz;
Fabio Utzig74530752023-02-07 20:09:37 -0300111 } else if (slot == 1 && bs->state == 2) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300112 slot = 0;
113 }
114 }
115 }
116 }
117
118 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
119 rc = flash_area_open(area_id, &fap);
120 if (rc != 0) {
121 rc = BOOT_EFLASH;
122 goto done;
123 }
124
125 rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr);
126 if (rc != 0) {
127 rc = BOOT_EFLASH;
128 goto done;
129 }
130
131 /* We only know where the headers are located when bs is valid */
132 if (bs != NULL && out_hdr->ih_magic != IMAGE_MAGIC) {
133 rc = -1;
134 goto done;
135 }
136
137 rc = 0;
138
139done:
140 flash_area_close(fap);
141 return rc;
142}
143
144int
145swap_read_status_bytes(const struct flash_area *fap,
146 struct boot_loader_state *state, struct boot_status *bs)
147{
148 uint32_t off;
149 uint8_t status;
150 int max_entries;
151 int found_idx;
152 uint8_t write_sz;
153 int move_entries;
154 int rc;
155 int last_rc;
156 int erased_sections;
157 int i;
158
159 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
160 if (max_entries < 0) {
161 return BOOT_EBADARGS;
162 }
163
164 erased_sections = 0;
165 found_idx = -1;
166 /* skip erased sectors at the end */
167 last_rc = 1;
168 write_sz = BOOT_WRITE_SZ(state);
169 off = boot_status_off(fap);
170 for (i = max_entries; i > 0; i--) {
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300171 rc = flash_area_read(fap, off + (i - 1) * write_sz, &status, 1);
Fabio Utzig74aef312019-11-28 11:05:34 -0300172 if (rc < 0) {
173 return BOOT_EFLASH;
174 }
175
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300176 if (bootutil_buffer_is_erased(fap, &status, 1)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300177 if (rc != last_rc) {
178 erased_sections++;
179 }
180 } else {
181 if (found_idx == -1) {
182 found_idx = i;
183 }
184 }
185 last_rc = rc;
186 }
187
188 if (erased_sections > 1) {
189 /* This means there was an error writing status on the last
190 * swap. Tell user and move on to validation!
191 */
David Brown098de832019-12-10 11:58:01 -0700192#if !defined(__BOOTSIM__)
Fabio Utzig74aef312019-11-28 11:05:34 -0300193 BOOT_LOG_ERR("Detected inconsistent status!");
David Brown098de832019-12-10 11:58:01 -0700194#endif
Fabio Utzig74aef312019-11-28 11:05:34 -0300195
196#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
197 /* With validation of the primary slot disabled, there is no way
198 * to be sure the swapped primary slot is OK, so abort!
199 */
200 assert(0);
201#endif
202 }
203
204 move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT;
205 if (found_idx == -1) {
206 /* no swap status found; nothing to do */
207 } else if (found_idx < move_entries) {
208 bs->op = BOOT_STATUS_OP_MOVE;
209 bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0;
210 bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;;
211 } else {
212 bs->op = BOOT_STATUS_OP_SWAP;
213 bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0;
214 bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0;
215 }
216
217 return 0;
218}
219
220uint32_t
221boot_status_internal_off(const struct boot_status *bs, int elem_sz)
222{
223 uint32_t off;
224 int idx_sz;
225
226 idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ?
227 BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT);
228
229 off = ((bs->op == BOOT_STATUS_OP_MOVE) ?
230 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) +
231 (bs->idx - BOOT_STATUS_IDX_0) * idx_sz +
232 (bs->state - BOOT_STATUS_STATE_0) * elem_sz;
233
234 return off;
235}
236
237int
238boot_slots_compatible(struct boot_loader_state *state)
239{
Fabio Utzigad055d12020-06-29 20:19:26 -0300240 size_t num_sectors_pri;
241 size_t num_sectors_sec;
242 size_t sector_sz_pri = 0;
243 size_t sector_sz_sec = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300244 size_t i;
245
Fabio Utzigad055d12020-06-29 20:19:26 -0300246 num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
247 num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
248 if ((num_sectors_pri != num_sectors_sec) &&
249 (num_sectors_pri != (num_sectors_sec + 1))) {
250 BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors");
Fabio Utzig74aef312019-11-28 11:05:34 -0300251 return 0;
252 }
253
Fabio Utzigad055d12020-06-29 20:19:26 -0300254 if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300255 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
256 return 0;
257 }
258
Fabio Utzigad055d12020-06-29 20:19:26 -0300259 for (i = 0; i < num_sectors_sec; i++) {
260 sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
261 sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i);
262 if (sector_sz_pri != sector_sz_sec) {
263 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
264 return 0;
265 }
266 }
267
268 if (num_sectors_pri > num_sectors_sec) {
269 if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300270 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
271 return 0;
272 }
273 }
274
275 return 1;
276}
277
278#define BOOT_LOG_SWAP_STATE(area, state) \
279 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
280 "image_ok=0x%x", \
281 (area), \
282 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
283 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
284 "bad"), \
285 (state)->swap_type, \
286 (state)->copy_done, \
287 (state)->image_ok)
288
289int
290swap_status_source(struct boot_loader_state *state)
291{
292 struct boot_swap_state state_primary_slot;
Fabio Utzigce115972020-10-28 09:05:20 -0300293 struct boot_swap_state state_secondary_slot;
Fabio Utzig74aef312019-11-28 11:05:34 -0300294 int rc;
295 uint8_t source;
296 uint8_t image_index;
297
298#if (BOOT_IMAGE_NUMBER == 1)
299 (void)state;
300#endif
301
302 image_index = BOOT_CURR_IMG(state);
303
304 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
305 &state_primary_slot);
306 assert(rc == 0);
307
308 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
309
Fabio Utzigce115972020-10-28 09:05:20 -0300310 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
311 &state_secondary_slot);
312 assert(rc == 0);
313
314 BOOT_LOG_SWAP_STATE("Secondary image", &state_secondary_slot);
315
Fabio Utzig74aef312019-11-28 11:05:34 -0300316 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
Fabio Utzigce115972020-10-28 09:05:20 -0300317 state_primary_slot.copy_done == BOOT_FLAG_UNSET &&
318 state_secondary_slot.magic != BOOT_MAGIC_GOOD) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300319
320 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
321
322 BOOT_LOG_INF("Boot source: primary slot");
323 return source;
324 }
325
326 BOOT_LOG_INF("Boot source: none");
327 return BOOT_STATUS_SOURCE_NONE;
328}
329
330/*
331 * "Moves" the sector located at idx - 1 to idx.
332 */
333static void
334boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
335 struct boot_status *bs, const struct flash_area *fap_pri,
336 const struct flash_area *fap_sec)
337{
338 uint32_t new_off;
339 uint32_t old_off;
340 int rc;
341
342 /*
343 * FIXME: assuming sectors of size == sz, a single off variable
344 * would be enough
345 */
346
347 /* Calculate offset from start of image area. */
348 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
349 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
350
351 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300352 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
353 rc = swap_erase_trailer_sectors(state, fap_pri);
354 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300355
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300356 rc = swap_status_init(state, fap_pri, bs);
357 assert(rc == 0);
358 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300359
360 rc = swap_erase_trailer_sectors(state, fap_sec);
361 assert(rc == 0);
362 }
363
364 rc = boot_erase_region(fap_pri, new_off, sz);
365 assert(rc == 0);
366
367 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
368 assert(rc == 0);
369
370 rc = boot_write_status(state, bs);
371
372 bs->idx++;
373 BOOT_STATUS_ASSERT(rc == 0);
374}
375
376static void
377boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
378 struct boot_status *bs, const struct flash_area *fap_pri,
379 const struct flash_area *fap_sec)
380{
381 uint32_t pri_off;
382 uint32_t pri_up_off;
383 uint32_t sec_off;
384 int rc;
385
386 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
387 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
388 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
389
390 if (bs->state == BOOT_STATUS_STATE_0) {
391 rc = boot_erase_region(fap_pri, pri_off, sz);
392 assert(rc == 0);
393
394 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
395 assert(rc == 0);
396
397 rc = boot_write_status(state, bs);
398 bs->state = BOOT_STATUS_STATE_1;
399 BOOT_STATUS_ASSERT(rc == 0);
400 }
401
402 if (bs->state == BOOT_STATUS_STATE_1) {
403 rc = boot_erase_region(fap_sec, sec_off, sz);
404 assert(rc == 0);
405
406 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
407 assert(rc == 0);
408
409 rc = boot_write_status(state, bs);
410 bs->idx++;
411 bs->state = BOOT_STATUS_STATE_0;
412 BOOT_STATUS_ASSERT(rc == 0);
413 }
414}
415
416/*
417 * When starting a revert the swap status exists in the primary slot, and
418 * the status in the secondary slot is erased. To start the swap, the status
419 * area in the primary slot must be re-initialized; if during the small
420 * window of time between re-initializing it and writing the first metadata
421 * a reset happens, the swap process is broken and cannot be resumed.
422 *
423 * This function handles the issue by making the revert look like a permanent
424 * upgrade (by initializing the secondary slot).
425 */
426void
427fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
Dominik Ermel0ab87b62021-05-25 11:50:04 +0000428 const struct flash_area *fap_sec)
Fabio Utzig74aef312019-11-28 11:05:34 -0300429{
430 struct boot_swap_state swap_state;
431 int rc;
432
433#if (BOOT_IMAGE_NUMBER == 1)
434 (void)state;
435#endif
436
437 /* No fixup required */
438 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
439 bs->op != BOOT_STATUS_OP_MOVE ||
440 bs->idx != BOOT_STATUS_IDX_0) {
441 return;
442 }
443
Dominik Ermel0ab87b62021-05-25 11:50:04 +0000444 rc = boot_read_swap_state(fap_sec, &swap_state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300445 assert(rc == 0);
446
447 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
448
449 if (swap_state.magic == BOOT_MAGIC_UNSET) {
450 rc = swap_erase_trailer_sectors(state, fap_sec);
451 assert(rc == 0);
452
453 rc = boot_write_image_ok(fap_sec);
454 assert(rc == 0);
455
456 rc = boot_write_swap_size(fap_sec, bs->swap_size);
457 assert(rc == 0);
458
Fabio Utzig74aef312019-11-28 11:05:34 -0300459 rc = boot_write_magic(fap_sec);
460 assert(rc == 0);
461 }
462}
463
464void
465swap_run(struct boot_loader_state *state, struct boot_status *bs,
466 uint32_t copy_size)
467{
468 uint32_t sz;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300469 uint32_t sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300470 uint32_t idx;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300471 uint32_t trailer_sz;
472 uint32_t first_trailer_idx;
Fabio Utzig74530752023-02-07 20:09:37 -0300473 uint32_t last_idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300474 uint8_t image_index;
475 const struct flash_area *fap_pri;
476 const struct flash_area *fap_sec;
477 int rc;
478
Andrzej Puzdrowskif9dbf682021-12-23 12:32:28 +0100479 BOOT_LOG_INF("Starting swap using move algorithm.");
480
Fabio Utzig74530752023-02-07 20:09:37 -0300481 last_idx = find_last_idx(state, copy_size);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300482 sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300483
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300484 /*
485 * When starting a new swap upgrade, check that there is enough space.
486 */
487 if (boot_status_is_reset(bs)) {
488 sz = 0;
489 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
490 first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
491
492 while (1) {
493 sz += sector_sz;
494 if (sz >= trailer_sz) {
495 break;
496 }
497 first_trailer_idx--;
498 }
499
Fabio Utzig74530752023-02-07 20:09:37 -0300500 if (last_idx >= first_trailer_idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300501 BOOT_LOG_WRN("Not enough free space to run swap upgrade");
Andrzej Puzdrowskif9dbf682021-12-23 12:32:28 +0100502 BOOT_LOG_WRN("required %d bytes but only %d are available",
Fabio Utzig74530752023-02-07 20:09:37 -0300503 (last_idx + 1) * sector_sz,
Andrzej Puzdrowskif9dbf682021-12-23 12:32:28 +0100504 first_trailer_idx * sector_sz);
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300505 bs->swap_type = BOOT_SWAP_TYPE_NONE;
506 return;
507 }
508 }
509
Fabio Utzig74aef312019-11-28 11:05:34 -0300510 image_index = BOOT_CURR_IMG(state);
511
512 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
513 assert (rc == 0);
514
515 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
516 assert (rc == 0);
517
Dominik Ermel0ab87b62021-05-25 11:50:04 +0000518 fixup_revert(state, bs, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300519
Fabio Utzig74aef312019-11-28 11:05:34 -0300520 if (bs->op == BOOT_STATUS_OP_MOVE) {
Fabio Utzig74530752023-02-07 20:09:37 -0300521 idx = last_idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300522 while (idx > 0) {
Fabio Utzig74530752023-02-07 20:09:37 -0300523 if (idx <= (last_idx - bs->idx + 1)) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300524 boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300525 }
526 idx--;
527 }
528 bs->idx = BOOT_STATUS_IDX_0;
529 }
530
531 bs->op = BOOT_STATUS_OP_SWAP;
532
533 idx = 1;
Fabio Utzig74530752023-02-07 20:09:37 -0300534 while (idx <= last_idx) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300535 if (idx >= bs->idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300536 boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300537 }
538 idx++;
539 }
540
541 flash_area_close(fap_pri);
542 flash_area_close(fap_sec);
543}
544
545#endif