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