blob: d7c9ad457d14eb44fab2f97700389e8f9c134a55 [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
31MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
32
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
50static uint32_t g_last_idx = UINT32_MAX;
51
52int
53boot_read_image_header(struct boot_loader_state *state, int slot,
54 struct image_header *out_hdr, struct boot_status *bs)
55{
56 const struct flash_area *fap;
57 uint32_t off;
58 uint32_t sz;
59 int area_id;
60 int rc;
61
62#if (BOOT_IMAGE_NUMBER == 1)
63 (void)state;
64#endif
65
66 off = 0;
67 if (bs) {
68 sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
69 if (bs->op == BOOT_STATUS_OP_MOVE) {
70 if (slot == 0 && bs->idx > g_last_idx) {
71 /* second sector */
72 off = sz;
73 }
74 } else if (bs->op == BOOT_STATUS_OP_SWAP) {
75 if (bs->idx > 1 && bs->idx <= g_last_idx) {
76 if (slot == 0) {
77 slot = 1;
78 } else {
79 slot = 0;
80 }
81 } else if (bs->idx == 1) {
82 if (slot == 0) {
83 off = sz;
84 }
85 if (slot == 1 && bs->state == 2) {
86 slot = 0;
87 }
88 }
89 }
90 }
91
92 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
93 rc = flash_area_open(area_id, &fap);
94 if (rc != 0) {
95 rc = BOOT_EFLASH;
96 goto done;
97 }
98
99 rc = flash_area_read(fap, off, out_hdr, sizeof *out_hdr);
100 if (rc != 0) {
101 rc = BOOT_EFLASH;
102 goto done;
103 }
104
105 /* We only know where the headers are located when bs is valid */
106 if (bs != NULL && out_hdr->ih_magic != IMAGE_MAGIC) {
107 rc = -1;
108 goto done;
109 }
110
111 rc = 0;
112
113done:
114 flash_area_close(fap);
115 return rc;
116}
117
118int
119swap_read_status_bytes(const struct flash_area *fap,
120 struct boot_loader_state *state, struct boot_status *bs)
121{
122 uint32_t off;
123 uint8_t status;
124 int max_entries;
125 int found_idx;
126 uint8_t write_sz;
127 int move_entries;
128 int rc;
129 int last_rc;
130 int erased_sections;
131 int i;
132
133 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
134 if (max_entries < 0) {
135 return BOOT_EBADARGS;
136 }
137
138 erased_sections = 0;
139 found_idx = -1;
140 /* skip erased sectors at the end */
141 last_rc = 1;
142 write_sz = BOOT_WRITE_SZ(state);
143 off = boot_status_off(fap);
144 for (i = max_entries; i > 0; i--) {
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300145 rc = flash_area_read(fap, off + (i - 1) * write_sz, &status, 1);
Fabio Utzig74aef312019-11-28 11:05:34 -0300146 if (rc < 0) {
147 return BOOT_EFLASH;
148 }
149
Fabio Utzig4b2e55f2020-09-24 11:49:20 -0300150 if (bootutil_buffer_is_erased(fap, &status, 1)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300151 if (rc != last_rc) {
152 erased_sections++;
153 }
154 } else {
155 if (found_idx == -1) {
156 found_idx = i;
157 }
158 }
159 last_rc = rc;
160 }
161
162 if (erased_sections > 1) {
163 /* This means there was an error writing status on the last
164 * swap. Tell user and move on to validation!
165 */
David Brown098de832019-12-10 11:58:01 -0700166#if !defined(__BOOTSIM__)
Fabio Utzig74aef312019-11-28 11:05:34 -0300167 BOOT_LOG_ERR("Detected inconsistent status!");
David Brown098de832019-12-10 11:58:01 -0700168#endif
Fabio Utzig74aef312019-11-28 11:05:34 -0300169
170#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
171 /* With validation of the primary slot disabled, there is no way
172 * to be sure the swapped primary slot is OK, so abort!
173 */
174 assert(0);
175#endif
176 }
177
178 move_entries = BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT;
179 if (found_idx == -1) {
180 /* no swap status found; nothing to do */
181 } else if (found_idx < move_entries) {
182 bs->op = BOOT_STATUS_OP_MOVE;
183 bs->idx = (found_idx / BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_IDX_0;
184 bs->state = (found_idx % BOOT_STATUS_MOVE_STATE_COUNT) + BOOT_STATUS_STATE_0;;
185 } else {
186 bs->op = BOOT_STATUS_OP_SWAP;
187 bs->idx = ((found_idx - move_entries) / BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_IDX_0;
188 bs->state = ((found_idx - move_entries) % BOOT_STATUS_SWAP_STATE_COUNT) + BOOT_STATUS_STATE_0;
189 }
190
191 return 0;
192}
193
194uint32_t
195boot_status_internal_off(const struct boot_status *bs, int elem_sz)
196{
197 uint32_t off;
198 int idx_sz;
199
200 idx_sz = elem_sz * ((bs->op == BOOT_STATUS_OP_MOVE) ?
201 BOOT_STATUS_MOVE_STATE_COUNT : BOOT_STATUS_SWAP_STATE_COUNT);
202
203 off = ((bs->op == BOOT_STATUS_OP_MOVE) ?
204 0 : (BOOT_MAX_IMG_SECTORS * BOOT_STATUS_MOVE_STATE_COUNT * elem_sz)) +
205 (bs->idx - BOOT_STATUS_IDX_0) * idx_sz +
206 (bs->state - BOOT_STATUS_STATE_0) * elem_sz;
207
208 return off;
209}
210
211int
212boot_slots_compatible(struct boot_loader_state *state)
213{
Fabio Utzigad055d12020-06-29 20:19:26 -0300214 size_t num_sectors_pri;
215 size_t num_sectors_sec;
216 size_t sector_sz_pri = 0;
217 size_t sector_sz_sec = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300218 size_t i;
219
Fabio Utzigad055d12020-06-29 20:19:26 -0300220 num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
221 num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
222 if ((num_sectors_pri != num_sectors_sec) &&
223 (num_sectors_pri != (num_sectors_sec + 1))) {
224 BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors");
Fabio Utzig74aef312019-11-28 11:05:34 -0300225 return 0;
226 }
227
Fabio Utzigad055d12020-06-29 20:19:26 -0300228 if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300229 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
230 return 0;
231 }
232
Fabio Utzigad055d12020-06-29 20:19:26 -0300233 for (i = 0; i < num_sectors_sec; i++) {
234 sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
235 sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i);
236 if (sector_sz_pri != sector_sz_sec) {
237 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
238 return 0;
239 }
240 }
241
242 if (num_sectors_pri > num_sectors_sec) {
243 if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300244 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
245 return 0;
246 }
247 }
248
249 return 1;
250}
251
252#define BOOT_LOG_SWAP_STATE(area, state) \
253 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
254 "image_ok=0x%x", \
255 (area), \
256 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
257 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
258 "bad"), \
259 (state)->swap_type, \
260 (state)->copy_done, \
261 (state)->image_ok)
262
263int
264swap_status_source(struct boot_loader_state *state)
265{
266 struct boot_swap_state state_primary_slot;
267 int rc;
268 uint8_t source;
269 uint8_t image_index;
270
271#if (BOOT_IMAGE_NUMBER == 1)
272 (void)state;
273#endif
274
275 image_index = BOOT_CURR_IMG(state);
276
277 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
278 &state_primary_slot);
279 assert(rc == 0);
280
281 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
282
283 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
284 state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
285
286 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
287
288 BOOT_LOG_INF("Boot source: primary slot");
289 return source;
290 }
291
292 BOOT_LOG_INF("Boot source: none");
293 return BOOT_STATUS_SOURCE_NONE;
294}
295
296/*
297 * "Moves" the sector located at idx - 1 to idx.
298 */
299static void
300boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
301 struct boot_status *bs, const struct flash_area *fap_pri,
302 const struct flash_area *fap_sec)
303{
304 uint32_t new_off;
305 uint32_t old_off;
306 int rc;
307
308 /*
309 * FIXME: assuming sectors of size == sz, a single off variable
310 * would be enough
311 */
312
313 /* Calculate offset from start of image area. */
314 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
315 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
316
317 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300318 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
319 rc = swap_erase_trailer_sectors(state, fap_pri);
320 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300321
Fabio Utzig7fd42d52020-10-28 09:04:41 -0300322 rc = swap_status_init(state, fap_pri, bs);
323 assert(rc == 0);
324 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300325
326 rc = swap_erase_trailer_sectors(state, fap_sec);
327 assert(rc == 0);
328 }
329
330 rc = boot_erase_region(fap_pri, new_off, sz);
331 assert(rc == 0);
332
333 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
334 assert(rc == 0);
335
336 rc = boot_write_status(state, bs);
337
338 bs->idx++;
339 BOOT_STATUS_ASSERT(rc == 0);
340}
341
342static void
343boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
344 struct boot_status *bs, const struct flash_area *fap_pri,
345 const struct flash_area *fap_sec)
346{
347 uint32_t pri_off;
348 uint32_t pri_up_off;
349 uint32_t sec_off;
350 int rc;
351
352 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
353 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
354 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
355
356 if (bs->state == BOOT_STATUS_STATE_0) {
357 rc = boot_erase_region(fap_pri, pri_off, sz);
358 assert(rc == 0);
359
360 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
361 assert(rc == 0);
362
363 rc = boot_write_status(state, bs);
364 bs->state = BOOT_STATUS_STATE_1;
365 BOOT_STATUS_ASSERT(rc == 0);
366 }
367
368 if (bs->state == BOOT_STATUS_STATE_1) {
369 rc = boot_erase_region(fap_sec, sec_off, sz);
370 assert(rc == 0);
371
372 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
373 assert(rc == 0);
374
375 rc = boot_write_status(state, bs);
376 bs->idx++;
377 bs->state = BOOT_STATUS_STATE_0;
378 BOOT_STATUS_ASSERT(rc == 0);
379 }
380}
381
382/*
383 * When starting a revert the swap status exists in the primary slot, and
384 * the status in the secondary slot is erased. To start the swap, the status
385 * area in the primary slot must be re-initialized; if during the small
386 * window of time between re-initializing it and writing the first metadata
387 * a reset happens, the swap process is broken and cannot be resumed.
388 *
389 * This function handles the issue by making the revert look like a permanent
390 * upgrade (by initializing the secondary slot).
391 */
392void
393fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
394 const struct flash_area *fap_sec, uint8_t sec_id)
395{
396 struct boot_swap_state swap_state;
397 int rc;
398
399#if (BOOT_IMAGE_NUMBER == 1)
400 (void)state;
401#endif
402
403 /* No fixup required */
404 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
405 bs->op != BOOT_STATUS_OP_MOVE ||
406 bs->idx != BOOT_STATUS_IDX_0) {
407 return;
408 }
409
410 rc = boot_read_swap_state_by_id(sec_id, &swap_state);
411 assert(rc == 0);
412
413 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
414
415 if (swap_state.magic == BOOT_MAGIC_UNSET) {
416 rc = swap_erase_trailer_sectors(state, fap_sec);
417 assert(rc == 0);
418
419 rc = boot_write_image_ok(fap_sec);
420 assert(rc == 0);
421
422 rc = boot_write_swap_size(fap_sec, bs->swap_size);
423 assert(rc == 0);
424
Fabio Utzig74aef312019-11-28 11:05:34 -0300425 rc = boot_write_magic(fap_sec);
426 assert(rc == 0);
427 }
428}
429
430void
431swap_run(struct boot_loader_state *state, struct boot_status *bs,
432 uint32_t copy_size)
433{
434 uint32_t sz;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300435 uint32_t sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300436 uint32_t idx;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300437 uint32_t trailer_sz;
438 uint32_t first_trailer_idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300439 uint8_t image_index;
440 const struct flash_area *fap_pri;
441 const struct flash_area *fap_sec;
442 int rc;
443
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300444 sz = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300445 g_last_idx = 0;
446
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300447 sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300448 while (1) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300449 sz += sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300450 /* Skip to next sector because all sectors will be moved up. */
451 g_last_idx++;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300452 if (sz >= copy_size) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300453 break;
454 }
455 }
456
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300457 /*
458 * When starting a new swap upgrade, check that there is enough space.
459 */
460 if (boot_status_is_reset(bs)) {
461 sz = 0;
462 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
463 first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
464
465 while (1) {
466 sz += sector_sz;
467 if (sz >= trailer_sz) {
468 break;
469 }
470 first_trailer_idx--;
471 }
472
473 if (g_last_idx >= first_trailer_idx) {
474 BOOT_LOG_WRN("Not enough free space to run swap upgrade");
475 bs->swap_type = BOOT_SWAP_TYPE_NONE;
476 return;
477 }
478 }
479
Fabio Utzig74aef312019-11-28 11:05:34 -0300480 image_index = BOOT_CURR_IMG(state);
481
482 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
483 assert (rc == 0);
484
485 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
486 assert (rc == 0);
487
488 fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index));
489
Fabio Utzig74aef312019-11-28 11:05:34 -0300490 if (bs->op == BOOT_STATUS_OP_MOVE) {
491 idx = g_last_idx;
492 while (idx > 0) {
493 if (idx <= (g_last_idx - bs->idx + 1)) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300494 boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300495 }
496 idx--;
497 }
498 bs->idx = BOOT_STATUS_IDX_0;
499 }
500
501 bs->op = BOOT_STATUS_OP_SWAP;
502
503 idx = 1;
504 while (idx <= g_last_idx) {
505 if (idx >= bs->idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300506 boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300507 }
508 idx++;
509 }
510
511 flash_area_close(fap_pri);
512 flash_area_close(fap_sec);
513}
514
515#endif