blob: 90bf9d1fd522103b334d364142012e6bdf9d9e28 [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{
Fabio Utzigad055d12020-06-29 20:19:26 -0300215 size_t num_sectors_pri;
216 size_t num_sectors_sec;
217 size_t sector_sz_pri = 0;
218 size_t sector_sz_sec = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300219 size_t i;
220
Fabio Utzigad055d12020-06-29 20:19:26 -0300221 num_sectors_pri = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
222 num_sectors_sec = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
223 if ((num_sectors_pri != num_sectors_sec) &&
224 (num_sectors_pri != (num_sectors_sec + 1))) {
225 BOOT_LOG_WRN("Cannot upgrade: not a compatible amount of sectors");
Fabio Utzig74aef312019-11-28 11:05:34 -0300226 return 0;
227 }
228
Fabio Utzigad055d12020-06-29 20:19:26 -0300229 if (num_sectors_pri > BOOT_MAX_IMG_SECTORS) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300230 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
231 return 0;
232 }
233
Fabio Utzigad055d12020-06-29 20:19:26 -0300234 for (i = 0; i < num_sectors_sec; i++) {
235 sector_sz_pri = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
236 sector_sz_sec = boot_img_sector_size(state, BOOT_SECONDARY_SLOT, i);
237 if (sector_sz_pri != sector_sz_sec) {
238 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
239 return 0;
240 }
241 }
242
243 if (num_sectors_pri > num_sectors_sec) {
244 if (sector_sz_pri != boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300245 BOOT_LOG_WRN("Cannot upgrade: not same sector layout");
246 return 0;
247 }
248 }
249
250 return 1;
251}
252
253#define BOOT_LOG_SWAP_STATE(area, state) \
254 BOOT_LOG_INF("%s: magic=%s, swap_type=0x%x, copy_done=0x%x, " \
255 "image_ok=0x%x", \
256 (area), \
257 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
258 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
259 "bad"), \
260 (state)->swap_type, \
261 (state)->copy_done, \
262 (state)->image_ok)
263
264int
265swap_status_source(struct boot_loader_state *state)
266{
267 struct boot_swap_state state_primary_slot;
268 int rc;
269 uint8_t source;
270 uint8_t image_index;
271
272#if (BOOT_IMAGE_NUMBER == 1)
273 (void)state;
274#endif
275
276 image_index = BOOT_CURR_IMG(state);
277
278 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
279 &state_primary_slot);
280 assert(rc == 0);
281
282 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
283
284 if (state_primary_slot.magic == BOOT_MAGIC_GOOD &&
285 state_primary_slot.copy_done == BOOT_FLAG_UNSET) {
286
287 source = BOOT_STATUS_SOURCE_PRIMARY_SLOT;
288
289 BOOT_LOG_INF("Boot source: primary slot");
290 return source;
291 }
292
293 BOOT_LOG_INF("Boot source: none");
294 return BOOT_STATUS_SOURCE_NONE;
295}
296
297/*
298 * "Moves" the sector located at idx - 1 to idx.
299 */
300static void
301boot_move_sector_up(int idx, uint32_t sz, struct boot_loader_state *state,
302 struct boot_status *bs, const struct flash_area *fap_pri,
303 const struct flash_area *fap_sec)
304{
305 uint32_t new_off;
306 uint32_t old_off;
307 int rc;
308
309 /*
310 * FIXME: assuming sectors of size == sz, a single off variable
311 * would be enough
312 */
313
314 /* Calculate offset from start of image area. */
315 new_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
316 old_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
317
318 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzigb86e6882019-12-10 09:51:17 -0300319 if (bs->source != BOOT_STATUS_SOURCE_PRIMARY_SLOT) {
320 rc = swap_erase_trailer_sectors(state, fap_pri);
321 assert(rc == 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300322
Fabio Utzigb86e6882019-12-10 09:51:17 -0300323 rc = swap_status_init(state, fap_pri, bs);
324 assert(rc == 0);
325 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300326
327 rc = swap_erase_trailer_sectors(state, fap_sec);
328 assert(rc == 0);
329 }
330
331 rc = boot_erase_region(fap_pri, new_off, sz);
332 assert(rc == 0);
333
334 rc = boot_copy_region(state, fap_pri, fap_pri, old_off, new_off, sz);
335 assert(rc == 0);
336
337 rc = boot_write_status(state, bs);
338
339 bs->idx++;
340 BOOT_STATUS_ASSERT(rc == 0);
341}
342
343static void
344boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
345 struct boot_status *bs, const struct flash_area *fap_pri,
346 const struct flash_area *fap_sec)
347{
348 uint32_t pri_off;
349 uint32_t pri_up_off;
350 uint32_t sec_off;
351 int rc;
352
353 pri_up_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
354 pri_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx - 1);
355 sec_off = boot_img_sector_off(state, BOOT_SECONDARY_SLOT, idx - 1);
356
357 if (bs->state == BOOT_STATUS_STATE_0) {
358 rc = boot_erase_region(fap_pri, pri_off, sz);
359 assert(rc == 0);
360
361 rc = boot_copy_region(state, fap_sec, fap_pri, sec_off, pri_off, sz);
362 assert(rc == 0);
363
364 rc = boot_write_status(state, bs);
365 bs->state = BOOT_STATUS_STATE_1;
366 BOOT_STATUS_ASSERT(rc == 0);
367 }
368
369 if (bs->state == BOOT_STATUS_STATE_1) {
370 rc = boot_erase_region(fap_sec, sec_off, sz);
371 assert(rc == 0);
372
373 rc = boot_copy_region(state, fap_pri, fap_sec, pri_up_off, sec_off, sz);
374 assert(rc == 0);
375
376 rc = boot_write_status(state, bs);
377 bs->idx++;
378 bs->state = BOOT_STATUS_STATE_0;
379 BOOT_STATUS_ASSERT(rc == 0);
380 }
381}
382
383/*
384 * When starting a revert the swap status exists in the primary slot, and
385 * the status in the secondary slot is erased. To start the swap, the status
386 * area in the primary slot must be re-initialized; if during the small
387 * window of time between re-initializing it and writing the first metadata
388 * a reset happens, the swap process is broken and cannot be resumed.
389 *
390 * This function handles the issue by making the revert look like a permanent
391 * upgrade (by initializing the secondary slot).
392 */
393void
394fixup_revert(const struct boot_loader_state *state, struct boot_status *bs,
395 const struct flash_area *fap_sec, uint8_t sec_id)
396{
397 struct boot_swap_state swap_state;
398 int rc;
399
400#if (BOOT_IMAGE_NUMBER == 1)
401 (void)state;
402#endif
403
404 /* No fixup required */
405 if (bs->swap_type != BOOT_SWAP_TYPE_REVERT ||
406 bs->op != BOOT_STATUS_OP_MOVE ||
407 bs->idx != BOOT_STATUS_IDX_0) {
408 return;
409 }
410
411 rc = boot_read_swap_state_by_id(sec_id, &swap_state);
412 assert(rc == 0);
413
414 BOOT_LOG_SWAP_STATE("Secondary image", &swap_state);
415
416 if (swap_state.magic == BOOT_MAGIC_UNSET) {
417 rc = swap_erase_trailer_sectors(state, fap_sec);
418 assert(rc == 0);
419
420 rc = boot_write_image_ok(fap_sec);
421 assert(rc == 0);
422
423 rc = boot_write_swap_size(fap_sec, bs->swap_size);
424 assert(rc == 0);
425
Fabio Utzig74aef312019-11-28 11:05:34 -0300426 rc = boot_write_magic(fap_sec);
427 assert(rc == 0);
428 }
429}
430
431void
432swap_run(struct boot_loader_state *state, struct boot_status *bs,
433 uint32_t copy_size)
434{
435 uint32_t sz;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300436 uint32_t sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300437 uint32_t idx;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300438 uint32_t trailer_sz;
439 uint32_t first_trailer_idx;
Fabio Utzig74aef312019-11-28 11:05:34 -0300440 uint8_t image_index;
441 const struct flash_area *fap_pri;
442 const struct flash_area *fap_sec;
443 int rc;
444
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300445 sz = 0;
Fabio Utzig74aef312019-11-28 11:05:34 -0300446 g_last_idx = 0;
447
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300448 sector_sz = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, 0);
Fabio Utzig74aef312019-11-28 11:05:34 -0300449 while (1) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300450 sz += sector_sz;
Fabio Utzig74aef312019-11-28 11:05:34 -0300451 /* Skip to next sector because all sectors will be moved up. */
452 g_last_idx++;
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300453 if (sz >= copy_size) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300454 break;
455 }
456 }
457
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300458 /*
459 * When starting a new swap upgrade, check that there is enough space.
460 */
461 if (boot_status_is_reset(bs)) {
462 sz = 0;
463 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
464 first_trailer_idx = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
465
466 while (1) {
467 sz += sector_sz;
468 if (sz >= trailer_sz) {
469 break;
470 }
471 first_trailer_idx--;
472 }
473
474 if (g_last_idx >= first_trailer_idx) {
475 BOOT_LOG_WRN("Not enough free space to run swap upgrade");
476 bs->swap_type = BOOT_SWAP_TYPE_NONE;
477 return;
478 }
479 }
480
Fabio Utzig74aef312019-11-28 11:05:34 -0300481 image_index = BOOT_CURR_IMG(state);
482
483 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index), &fap_pri);
484 assert (rc == 0);
485
486 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index), &fap_sec);
487 assert (rc == 0);
488
489 fixup_revert(state, bs, fap_sec, FLASH_AREA_IMAGE_SECONDARY(image_index));
490
Fabio Utzig74aef312019-11-28 11:05:34 -0300491 if (bs->op == BOOT_STATUS_OP_MOVE) {
492 idx = g_last_idx;
493 while (idx > 0) {
494 if (idx <= (g_last_idx - bs->idx + 1)) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300495 boot_move_sector_up(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300496 }
497 idx--;
498 }
499 bs->idx = BOOT_STATUS_IDX_0;
500 }
501
502 bs->op = BOOT_STATUS_OP_SWAP;
503
504 idx = 1;
505 while (idx <= g_last_idx) {
506 if (idx >= bs->idx) {
Fabio Utzig9e1db9a2020-01-02 21:14:22 -0300507 boot_swap_sectors(idx, sector_sz, state, bs, fap_pri, fap_sec);
Fabio Utzig74aef312019-11-28 11:05:34 -0300508 }
509 idx++;
510 }
511
512 flash_area_close(fap_pri);
513 flash_area_close(fap_sec);
514}
515
516#endif