blob: 7332414ba1814715a7f8816ac74e54bab4a41dbc [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--) {
145 rc = flash_area_read_is_empty(fap, off + (i - 1) * write_sz, &status, 1);
146 if (rc < 0) {
147 return BOOT_EFLASH;
148 }
149
150 if (rc == 1) {
151 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 Utzig296949e2020-10-06 18:18:43 -0300318 rc = swap_erase_trailer_sectors(state, fap_pri);
319 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300320
Fabio Utzig296949e2020-10-06 18:18:43 -0300321 rc = swap_status_init(state, fap_pri, bs);
322 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300323
324 rc = swap_erase_trailer_sectors(state, fap_sec);
325 assert(rc == 0);
326 }
327
328 rc = boot_erase_region(fap_pri, new_off, sz);
329 assert(rc == 0);
330
331 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
332 assert(rc == 0);
333
334 rc = boot_write_status(state, bs);
335
336 bs->idx++;
337 BOOT_STATUS_ASSERT(rc == 0);
338}
339
340static void
341boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
342 struct boot_status *bs, const struct flash_area *fap_pri,
343 const struct flash_area *fap_sec)
344{
345 uint32_t pri_off;
346 uint32_t pri_up_off;
347 uint32_t sec_off;
348 int rc;
349
350 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
351 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
352 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
353
354 if (bs->state == BOOT_STATUS_STATE_0) {
355 rc = boot_erase_region(fap_pri, pri_off, sz);
356 assert(rc == 0);
357
358 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
359 assert(rc == 0);
360
361 rc = boot_write_status(state, bs);
362 bs->state = BOOT_STATUS_STATE_1;
363 BOOT_STATUS_ASSERT(rc == 0);
364 }
365
366 if (bs->state == BOOT_STATUS_STATE_1) {
367 rc = boot_erase_region(fap_sec, sec_off, sz);
368 assert(rc == 0);
369
370 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
371 assert(rc == 0);
372
373 rc = boot_write_status(state, bs);
374 bs->idx++;
375 bs->state = BOOT_STATUS_STATE_0;
376 BOOT_STATUS_ASSERT(rc == 0);
377 }
378}
379
380/*
381 * When starting a revert the swap status exists in the primary slot, and
382 * the status in the secondary slot is erased. To start the swap, the status
383 * area in the primary slot must be re-initialized; if during the small
384 * window of time between re-initializing it and writing the first metadata
385 * a reset happens, the swap process is broken and cannot be resumed.
386 *
387 * This function handles the issue by making the revert look like a permanent
388 * upgrade (by initializing the secondary slot).
389 */
390void
391fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
392 const struct flash_area *fap_sec, uint8_t sec_id)
393{
394 struct boot_swap_state swap_state;
395 int rc;
396
397#if (BOOT_IMAGE_NUMBER == 1)
398 (void)state;
399#endif
400
401 /* No fixup required */
402 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
403 bs->op != BOOT_STATUS_OP_MOVE ||
404 bs->idx != BOOT_STATUS_IDX_0) {
405 return;
406 }
407
408 rc = boot_read_swap_state_by_id(sec_id, &swap_state);
409 assert(rc == 0);
410
411 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
412
413 if (swap_state.magic == BOOT_MAGIC_UNSET) {
414 rc = swap_erase_trailer_sectors(state, fap_sec);
415 assert(rc == 0);
416
417 rc = boot_write_image_ok(fap_sec);
418 assert(rc == 0);
419
420 rc = boot_write_swap_size(fap_sec, bs->swap_size);
421 assert(rc == 0);
422
Fabio Utzig74aef312019-11-28 11:05:34 -0300423 rc = boot_write_magic(fap_sec);
424 assert(rc == 0);
425 }
426}
427
428void
429swap_run(struct boot_loader_state *state, struct boot_status *bs,
430 uint32_t copy_size)
431{
432 uint32_t sz;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300433 uint32_t sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300434 uint32_t idx;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300435 uint32_t trailer_sz;
436 uint32_t first_trailer_idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300437 uint8_t image_index;
438 const struct flash_area *fap_pri;
439 const struct flash_area *fap_sec;
440 int rc;
441
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300442 sz = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300443 g_last_idx = 0;
444
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300445 sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300446 while (1) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300447 sz += sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300448 /* Skip to next sector because all sectors will be moved up. */
449 g_last_idx++;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300450 if (sz >= copy_size) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300451 break;
452 }
453 }
454
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300455 /*
456 * When starting a new swap upgrade, check that there is enough space.
457 */
458 if (boot_status_is_reset(bs)) {
459 sz = 0;
460 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
461 first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
462
463 while (1) {
464 sz += sector_sz;
465 if (sz >= trailer_sz) {
466 break;
467 }
468 first_trailer_idx--;
469 }
470
471 if (g_last_idx >= first_trailer_idx) {
472 BOOT_LOG_WRN("Not enough free space to run swap upgrade");
473 bs->swap_type = BOOT_SWAP_TYPE_NONE;
474 return;
475 }
476 }
477
Fabio Utzig74aef312019-11-28 11:05:34 -0300478 image_index = BOOT_CURR_IMG(state);
479
480 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
481 assert (rc == 0);
482
483 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
484 assert (rc == 0);
485
486 fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index));
487
Fabio Utzig74aef312019-11-28 11:05:34 -0300488 if (bs->op == BOOT_STATUS_OP_MOVE) {
489 idx = g_last_idx;
490 while (idx > 0) {
491 if (idx <= (g_last_idx - bs->idx + 1)) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300492 boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300493 }
494 idx--;
495 }
496 bs->idx = BOOT_STATUS_IDX_0;
497 }
498
499 bs->op = BOOT_STATUS_OP_SWAP;
500
501 idx = 1;
502 while (idx <= g_last_idx) {
503 if (idx >= bs->idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300504 boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300505 }
506 idx++;
507 }
508
509 flash_area_close(fap_pri);
510 flash_area_close(fap_sec);
511}
512
513#endif