blob: ffcea73e4daced8db81fd67fea8fdd1e1f8f354d [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20/**
21 * This file provides an interface to the boot loader. Functions defined in
22 * this file should only be called while the boot loader is running.
23 */
24
25#include <assert.h>
26#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060027#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080028#include <inttypes.h>
29#include <stdlib.h>
30#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080031#include <os/os_malloc.h>
32#include "bootutil/bootutil.h"
33#include "bootutil/image.h"
34#include "bootutil_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050035#include "bootutil/bootutil_log.h"
36
Fabio Utzigba829042018-09-18 08:29:34 -030037#ifdef MCUBOOT_ENC_IMAGES
38#include "bootutil/enc_key.h"
39#endif
40
Fabio Utzigba1fbe62017-07-21 14:01:20 -030041#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030042
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040043static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080044
Fabio Utzigf70e3022018-01-10 15:00:42 -020045#if defined(MCUBOOT_VALIDATE_SLOT0) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020046static int boot_status_fails = 0;
47#define BOOT_STATUS_ASSERT(x) \
48 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010049 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020050 boot_status_fails++; \
51 } \
52 } while (0)
53#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020054#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020055#endif
56
Christopher Collins92ea77f2016-12-12 15:59:26 -080057struct boot_status_table {
Christopher Collins92ea77f2016-12-12 15:59:26 -080058 uint8_t bst_magic_slot0;
59 uint8_t bst_magic_scratch;
60 uint8_t bst_copy_done_slot0;
61 uint8_t bst_status_source;
62};
63
64/**
65 * This set of tables maps swap state contents to boot status location.
66 * When searching for a match, these tables must be iterated in order.
67 */
68static const struct boot_status_table boot_status_tables[] = {
69 {
70 /* | slot-0 | scratch |
71 * ----------+------------+------------|
72 * magic | Good | Any |
Fabio Utzig39000012018-07-30 12:40:20 -030073 * copy-done | Set | N/A |
Christopher Collins92ea77f2016-12-12 15:59:26 -080074 * ----------+------------+------------'
75 * source: none |
76 * ------------------------------------'
77 */
78 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030079 .bst_magic_scratch = BOOT_MAGIC_ANY,
80 .bst_copy_done_slot0 = BOOT_FLAG_SET,
Christopher Collins92ea77f2016-12-12 15:59:26 -080081 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
82 },
83
84 {
85 /* | slot-0 | scratch |
86 * ----------+------------+------------|
87 * magic | Good | Any |
Fabio Utzig39000012018-07-30 12:40:20 -030088 * copy-done | Unset | N/A |
Christopher Collins92ea77f2016-12-12 15:59:26 -080089 * ----------+------------+------------'
90 * source: slot 0 |
91 * ------------------------------------'
92 */
93 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030094 .bst_magic_scratch = BOOT_MAGIC_ANY,
95 .bst_copy_done_slot0 = BOOT_FLAG_UNSET,
Christopher Collins92ea77f2016-12-12 15:59:26 -080096 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
97 },
98
99 {
100 /* | slot-0 | scratch |
101 * ----------+------------+------------|
102 * magic | Any | Good |
103 * copy-done | Any | N/A |
104 * ----------+------------+------------'
105 * source: scratch |
106 * ------------------------------------'
107 */
Fabio Utzig39000012018-07-30 12:40:20 -0300108 .bst_magic_slot0 = BOOT_MAGIC_ANY,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800109 .bst_magic_scratch = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -0300110 .bst_copy_done_slot0 = BOOT_FLAG_ANY,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
112 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 {
114 /* | slot-0 | scratch |
115 * ----------+------------+------------|
116 * magic | Unset | Any |
Fabio Utzig39000012018-07-30 12:40:20 -0300117 * copy-done | Unset | N/A |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800118 * ----------+------------+------------|
119 * source: varies |
120 * ------------------------------------+------------------------------+
121 * This represents one of two cases: |
122 * o No swaps ever (no status to read, so no harm in checking). |
123 * o Mid-revert; status in slot 0. |
124 * -------------------------------------------------------------------'
125 */
126 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
Fabio Utzig39000012018-07-30 12:40:20 -0300127 .bst_magic_scratch = BOOT_MAGIC_ANY,
128 .bst_copy_done_slot0 = BOOT_FLAG_UNSET,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800129 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
130 },
131};
132
133#define BOOT_STATUS_TABLES_COUNT \
134 (sizeof boot_status_tables / sizeof boot_status_tables[0])
135
Marti Bolivarfd20c762017-02-07 16:52:50 -0500136#define BOOT_LOG_SWAP_STATE(area, state) \
137 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
138 (area), \
139 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
140 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
141 "bad"), \
142 (state)->copy_done, \
143 (state)->image_ok)
144
Christopher Collins92ea77f2016-12-12 15:59:26 -0800145/**
146 * Determines where in flash the most recent boot status is stored. The boot
147 * status is necessary for completing a swap that was interrupted by a boot
148 * loader reset.
149 *
150 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
151 */
152static int
153boot_status_source(void)
154{
155 const struct boot_status_table *table;
156 struct boot_swap_state state_scratch;
157 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800158 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200159 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500160 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161
Fabio Utzig2473ac02017-05-02 12:45:02 -0300162 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163 assert(rc == 0);
164
Fabio Utzig2473ac02017-05-02 12:45:02 -0300165 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800166 assert(rc == 0);
167
Marti Bolivarfd20c762017-02-07 16:52:50 -0500168 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500169 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
170
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300172 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173
Fabio Utzig39000012018-07-30 12:40:20 -0300174 if ((table->bst_magic_slot0 == BOOT_MAGIC_ANY ||
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175 table->bst_magic_slot0 == state_slot0.magic) &&
Fabio Utzig39000012018-07-30 12:40:20 -0300176 (table->bst_magic_scratch == BOOT_MAGIC_ANY ||
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 table->bst_magic_scratch == state_scratch.magic) &&
Fabio Utzig39000012018-07-30 12:40:20 -0300178 (table->bst_copy_done_slot0 == BOOT_FLAG_ANY ||
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500180 source = table->bst_status_source;
181 BOOT_LOG_INF("Boot source: %s",
182 source == BOOT_STATUS_SOURCE_NONE ? "none" :
183 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
184 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
185 "BUG; can't happen");
186 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187 }
188 }
189
Marti Bolivarfd20c762017-02-07 16:52:50 -0500190 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800191 return BOOT_STATUS_SOURCE_NONE;
192}
193
194/**
195 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300196 *
197 * This is used when a swap is interrupted by an external event. After
198 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800199 */
200static int
201boot_previous_swap_type(void)
202{
203 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800204
205 post_swap_type = boot_swap_type();
206
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300207 switch (post_swap_type) {
208 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
209 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
210 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800211 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300212
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300213 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800214}
215
David Brownf5b33d82017-09-01 10:58:27 -0600216/*
217 * Compute the total size of the given image. Includes the size of
218 * the TLVs.
219 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300220#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600221static int
222boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
223{
224 const struct flash_area *fap;
225 struct image_tlv_info info;
226 int area_id;
227 int rc;
228
229 area_id = flash_area_id_from_image_slot(slot);
230 rc = flash_area_open(area_id, &fap);
231 if (rc != 0) {
232 rc = BOOT_EFLASH;
233 goto done;
234 }
235
236 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
237 &info, sizeof(info));
238 if (rc != 0) {
239 rc = BOOT_EFLASH;
240 goto done;
241 }
242 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
243 rc = BOOT_EBADIMAGE;
244 goto done;
245 }
246 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
247 rc = 0;
248
249done:
250 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300251 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600252}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300253#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600254
Fabio Utzigc08ed212017-06-20 19:28:36 -0300255static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800256boot_read_image_header(int slot, struct image_header *out_hdr)
257{
258 const struct flash_area *fap;
259 int area_id;
260 int rc;
261
262 area_id = flash_area_id_from_image_slot(slot);
263 rc = flash_area_open(area_id, &fap);
264 if (rc != 0) {
265 rc = BOOT_EFLASH;
266 goto done;
267 }
268
269 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
270 if (rc != 0) {
271 rc = BOOT_EFLASH;
272 goto done;
273 }
274
275 rc = 0;
276
277done:
278 flash_area_close(fap);
279 return rc;
280}
281
282static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200283boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284{
285 int rc;
286 int i;
287
288 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400289 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200291 /* If `require_all` is set, fail on any single fail, otherwise
292 * if at least the first slot's header was read successfully,
293 * then the boot loader can attempt a boot.
294 *
295 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800296 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200297 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800298 return 0;
299 } else {
300 return rc;
301 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 }
303 }
304
305 return 0;
306}
307
308static uint8_t
309boot_write_sz(void)
310{
311 uint8_t elem_sz;
312 uint8_t align;
313
314 /* Figure out what size to write update status update as. The size depends
315 * on what the minimum write size is for scratch area, active image slot.
316 * We need to use the bigger of those 2 values.
317 */
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200318 elem_sz = flash_area_align(boot_data.imgs[0].area);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200319 align = flash_area_align(boot_data.scratch.area);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800320 if (align > elem_sz) {
321 elem_sz = align;
322 }
323
324 return elem_sz;
325}
326
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200327/*
328 * Slots are compatible when all sectors that store upto to size of the image
329 * round up to sector size, in both slot's are able to fit in the scratch
330 * area, and have sizes that are a multiple of each other (powers of two
331 * presumably!).
332 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800333static int
334boot_slots_compatible(void)
335{
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200336 size_t num_sectors_0;
337 size_t num_sectors_1;
338 size_t sz0, sz1;
339 size_t slot0_sz, slot1_sz;
340 size_t scratch_sz;
341 size_t i, j;
342 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800343
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200344 num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
345 num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
Fabio Utziga1fae672018-03-30 10:52:38 -0300346 if (num_sectors_0 > BOOT_MAX_IMG_SECTORS || num_sectors_1 > BOOT_MAX_IMG_SECTORS) {
347 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800348 return 0;
349 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300350
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200351 scratch_sz = boot_scratch_area_size(&boot_data);
352
353 /*
354 * The following loop scans all sectors in a linear fashion, assuring that
355 * for each possible sector in each slot, it is able to fit in the other
356 * slot's sector or sectors. Slot's should be compatible as long as any
357 * number of a slot's sectors are able to fit into another, which only
358 * excludes cases where sector sizes are not a multiple of each other.
359 */
360 i = sz0 = slot0_sz = 0;
361 j = sz1 = slot1_sz = 0;
362 smaller = 0;
363 while (i < num_sectors_0 || j < num_sectors_1) {
364 if (sz0 == sz1) {
365 sz0 += boot_img_sector_size(&boot_data, 0, i);
366 sz1 += boot_img_sector_size(&boot_data, 1, j);
367 i++;
368 j++;
369 } else if (sz0 < sz1) {
370 sz0 += boot_img_sector_size(&boot_data, 0, i);
371 /* guarantee that multiple sectors of slot1 fit into slot0 */
372 if (smaller == 2) {
373 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
374 return 0;
375 }
376 smaller = 1;
377 i++;
378 } else {
379 sz1 += boot_img_sector_size(&boot_data, 1, j);
380 /* guarantee that multiple sectors of slot0 fit into slot1 */
381 if (smaller == 1) {
382 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
383 return 0;
384 }
385 smaller = 2;
386 j++;
387 }
388 if (sz0 == sz1) {
389 slot0_sz += sz0;
390 slot1_sz += sz1;
391 /* scratch has to fit each swap operation to the size of the larger
392 * sector among slot0 and slot1
393 */
394 if (sz0 > scratch_sz || sz1 > scratch_sz) {
395 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
396 return 0;
397 }
398 smaller = sz0 = sz1 = 0;
399 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300400 }
401
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200402 if (i != num_sectors_0 || j != num_sectors_1 || slot0_sz != slot1_sz) {
403 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
404 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800405 }
406
407 return 1;
408}
409
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410/**
411 * Determines the sector layout of both image slots and the scratch area.
412 * This information is necessary for calculating the number of bytes to erase
413 * and copy during an image swap. The information collected during this
414 * function is used to populate the boot_data global.
415 */
416static int
417boot_read_sectors(void)
418{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800420
Marti Bolivarcca28a92017-06-12 16:52:22 -0400421 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422 if (rc != 0) {
423 return BOOT_EFLASH;
424 }
425
Marti Bolivarcca28a92017-06-12 16:52:22 -0400426 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427 if (rc != 0) {
428 return BOOT_EFLASH;
429 }
430
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200431 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
432 if (rc != 0) {
433 return BOOT_EFLASH;
434 }
435
Marti Bolivare10a7392017-06-14 16:20:07 -0400436 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437
438 return 0;
439}
440
441static uint32_t
442boot_status_internal_off(int idx, int state, int elem_sz)
443{
444 int idx_sz;
445
446 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
447
Fabio Utzig39000012018-07-30 12:40:20 -0300448 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
449 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800450}
451
452/**
453 * Reads the status of a partially-completed swap, if any. This is necessary
454 * to recover in case the boot lodaer was reset in the middle of a swap
455 * operation.
456 */
457static int
458boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
459{
460 uint32_t off;
461 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300462 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200464 int found_idx;
465 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 int rc;
467 int i;
468
469 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400470 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300471
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200473 found_idx = 0;
474 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300475 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300476 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
477 &status, 1);
478 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 return BOOT_EFLASH;
480 }
481
Fabio Utzig178be542018-09-19 08:12:56 -0300482 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200483 if (found && !found_idx) {
484 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 }
486 } else if (!found) {
487 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200488 } else if (found_idx) {
489 invalid = 1;
490 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800491 }
492 }
493
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200494 if (invalid) {
495 /* This means there was an error writing status on the last
496 * swap. Tell user and move on to validation!
497 */
498 BOOT_LOG_ERR("Detected inconsistent status!");
499
500#if !defined(MCUBOOT_VALIDATE_SLOT0)
501 /* With validation of slot0 disabled, there is no way to be sure the
502 * swapped slot0 is OK, so abort!
503 */
504 assert(0);
505#endif
506 }
507
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200509 if (!found_idx) {
510 found_idx = i;
511 }
512 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300513 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
514 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800515 }
516
517 return 0;
518}
519
520/**
521 * Reads the boot status from the flash. The boot status contains
522 * the current state of an interrupted image copy operation. If the boot
523 * status is not present, or it indicates that previous copy finished,
524 * there is no operation in progress.
525 */
526static int
527boot_read_status(struct boot_status *bs)
528{
529 const struct flash_area *fap;
530 int status_loc;
531 int area_id;
532 int rc;
533
534 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300535 bs->idx = BOOT_STATUS_IDX_0;
536 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700538#ifdef MCUBOOT_OVERWRITE_ONLY
539 /* Overwrite-only doesn't make use of the swap status area. */
540 return 0;
541#endif
542
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543 status_loc = boot_status_source();
544 switch (status_loc) {
545 case BOOT_STATUS_SOURCE_NONE:
546 return 0;
547
548 case BOOT_STATUS_SOURCE_SCRATCH:
549 area_id = FLASH_AREA_IMAGE_SCRATCH;
550 break;
551
552 case BOOT_STATUS_SOURCE_SLOT0:
553 area_id = FLASH_AREA_IMAGE_0;
554 break;
555
556 default:
557 assert(0);
558 return BOOT_EBADARGS;
559 }
560
561 rc = flash_area_open(area_id, &fap);
562 if (rc != 0) {
563 return BOOT_EFLASH;
564 }
565
Fabio Utzig46490722017-09-04 15:34:32 -0300566 rc = boot_read_status_bytes(fap, bs);
567
568 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700569
Fabio Utzig46490722017-09-04 15:34:32 -0300570 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571}
572
573/**
574 * Writes the supplied boot status to the flash file system. The boot status
575 * contains the current state of an in-progress image copy operation.
576 *
577 * @param bs The boot status to write.
578 *
579 * @return 0 on success; nonzero on failure.
580 */
581int
582boot_write_status(struct boot_status *bs)
583{
584 const struct flash_area *fap;
585 uint32_t off;
586 int area_id;
587 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300588 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700589 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300590 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300592 /* NOTE: The first sector copied (that is the last sector on slot) contains
593 * the trailer. Since in the last step SLOT 0 is erased, the first
594 * two status writes go to the scratch which will be copied to SLOT 0!
595 */
596
Fabio Utzig2473ac02017-05-02 12:45:02 -0300597 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598 /* Write to scratch. */
599 area_id = FLASH_AREA_IMAGE_SCRATCH;
600 } else {
601 /* Write to slot 0. */
602 area_id = FLASH_AREA_IMAGE_0;
603 }
604
605 rc = flash_area_open(area_id, &fap);
606 if (rc != 0) {
607 rc = BOOT_EFLASH;
608 goto done;
609 }
610
611 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400612 boot_status_internal_off(bs->idx, bs->state,
613 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200614 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300615 erased_val = flash_area_erased_val(fap);
616 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700617 buf[0] = bs->state;
618
619 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 if (rc != 0) {
621 rc = BOOT_EFLASH;
622 goto done;
623 }
624
625 rc = 0;
626
627done:
628 flash_area_close(fap);
629 return rc;
630}
631
632/*
633 * Validate image hash/signature in a slot.
634 */
635static int
Fabio Utzigba829042018-09-18 08:29:34 -0300636boot_image_check(struct image_header *hdr, const struct flash_area *fap,
637 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800638{
David Browndb1d9d32017-01-06 11:07:54 -0700639 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300640 int rc;
641
642#ifndef MCUBOOT_ENC_IMAGES
643 (void)bs;
644 (void)rc;
645#else
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200646 if (fap->fa_id == FLASH_AREA_IMAGE_1 && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300647 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
648 if (rc < 0) {
649 return BOOT_EBADIMAGE;
650 }
651 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
652 return BOOT_EBADIMAGE;
653 }
654 }
655#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656
Christopher Collins92ea77f2016-12-12 15:59:26 -0800657 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
658 NULL, 0, NULL)) {
659 return BOOT_EBADIMAGE;
660 }
661 return 0;
662}
663
664static int
665split_image_check(struct image_header *app_hdr,
666 const struct flash_area *app_fap,
667 struct image_header *loader_hdr,
668 const struct flash_area *loader_fap)
669{
670 static void *tmpbuf;
671 uint8_t loader_hash[32];
672
673 if (!tmpbuf) {
674 tmpbuf = malloc(BOOT_TMPBUF_SZ);
675 if (!tmpbuf) {
676 return BOOT_ENOMEM;
677 }
678 }
679
680 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
681 NULL, 0, loader_hash)) {
682 return BOOT_EBADIMAGE;
683 }
684
685 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
686 loader_hash, 32, NULL)) {
687 return BOOT_EBADIMAGE;
688 }
689
690 return 0;
691}
692
Fabio Utzig39000012018-07-30 12:40:20 -0300693static inline int
694boot_magic_is_erased(uint8_t erased_val, uint32_t magic)
695{
696 uint8_t i;
697 for (i = 0; i < sizeof(magic); i++) {
698 if (erased_val != *(((uint8_t *)&magic) + i)) {
699 return 0;
700 }
701 }
702 return 1;
703}
704
Christopher Collins92ea77f2016-12-12 15:59:26 -0800705static int
Fabio Utzigba829042018-09-18 08:29:34 -0300706boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800707{
708 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400709 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800710 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300711
David Brownd930ec62016-12-14 07:59:48 -0700712 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713 if (rc != 0) {
714 return BOOT_EFLASH;
715 }
716
Fabio Utzig39000012018-07-30 12:40:20 -0300717 hdr = boot_img_hdr(&boot_data, slot);
718 if (boot_magic_is_erased(flash_area_erased_val(fap), hdr->ih_magic) ||
719 hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
720 /* No bootable image in slot; continue booting from slot 0. */
721 return -1;
722 }
723
Fabio Utzigba829042018-09-18 08:29:34 -0300724 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700725 if (slot != 0) {
726 flash_area_erase(fap, 0, fap->fa_size);
727 /* Image in slot 1 is invalid. Erase the image and
728 * continue booting from slot 0.
729 */
730 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300731 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800732 return -1;
733 }
734
735 flash_area_close(fap);
736
737 /* Image in slot 1 is valid. */
738 return 0;
739}
740
741/**
742 * Determines which swap operation to perform, if any. If it is determined
743 * that a swap operation is required, the image in the second slot is checked
744 * for validity. If the image in the second slot is invalid, it is erased, and
745 * a swap type of "none" is indicated.
746 *
747 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
748 */
749static int
Fabio Utzigba829042018-09-18 08:29:34 -0300750boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800751{
752 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800753
754 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300755 switch (swap_type) {
756 case BOOT_SWAP_TYPE_TEST:
757 case BOOT_SWAP_TYPE_PERM:
758 case BOOT_SWAP_TYPE_REVERT:
759 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
Fabio Utzigba829042018-09-18 08:29:34 -0300760 if (boot_validate_slot(1, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300761 swap_type = BOOT_SWAP_TYPE_FAIL;
762 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800763 }
764
765 return swap_type;
766}
767
768/**
769 * Calculates the number of sectors the scratch area can contain. A "last"
770 * source sector is specified because images are copied backwards in flash
771 * (final index to index number 0).
772 *
773 * @param last_sector_idx The index of the last source sector
774 * (inclusive).
775 * @param out_first_sector_idx The index of the first source sector
776 * (inclusive) gets written here.
777 *
778 * @return The number of bytes comprised by the
779 * [first-sector, last-sector] range.
780 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300781#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800782static uint32_t
783boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
784{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400785 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800786 uint32_t new_sz;
787 uint32_t sz;
788 int i;
789
790 sz = 0;
791
Marti Bolivard3269fd2017-06-12 16:31:12 -0400792 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400794 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200795 /*
796 * slot1 is not being checked here, because `boot_slots_compatible`
797 * already provides assurance that the copy size will be compatible
798 * with slot0 and scratch.
799 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400800 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800801 break;
802 }
803 sz = new_sz;
804 }
805
806 /* i currently refers to a sector that doesn't fit or it is -1 because all
807 * sectors have been processed. In both cases, exclude sector i.
808 */
809 *out_first_sector_idx = i + 1;
810 return sz;
811}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300812#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800813
814/**
815 * Erases a region of flash.
816 *
Fabio Utzigba829042018-09-18 08:29:34 -0300817 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800818 * @param off The offset within the flash area to start the
819 * erase.
820 * @param sz The number of bytes to erase.
821 *
822 * @return 0 on success; nonzero on failure.
823 */
Fabio Utzigba829042018-09-18 08:29:34 -0300824static inline int
825boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826{
Fabio Utzigba829042018-09-18 08:29:34 -0300827 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800828}
829
830/**
831 * Copies the contents of one flash region to another. You must erase the
832 * destination region prior to calling this function.
833 *
834 * @param flash_area_id_src The ID of the source flash area.
835 * @param flash_area_id_dst The ID of the destination flash area.
836 * @param off_src The offset within the source flash area to
837 * copy from.
838 * @param off_dst The offset within the destination flash area to
839 * copy to.
840 * @param sz The number of bytes to copy.
841 *
842 * @return 0 on success; nonzero on failure.
843 */
844static int
Fabio Utzigba829042018-09-18 08:29:34 -0300845boot_copy_sector(const struct flash_area *fap_src,
846 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800847 uint32_t off_src, uint32_t off_dst, uint32_t sz)
848{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849 uint32_t bytes_copied;
850 int chunk_sz;
851 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300852#ifdef MCUBOOT_ENC_IMAGES
853 uint32_t off;
854 size_t blk_off;
855 struct image_header *hdr;
856 uint16_t idx;
857 uint32_t blk_sz;
858#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859
860 static uint8_t buf[1024];
861
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862 bytes_copied = 0;
863 while (bytes_copied < sz) {
864 if (sz - bytes_copied > sizeof buf) {
865 chunk_sz = sizeof buf;
866 } else {
867 chunk_sz = sz - bytes_copied;
868 }
869
870 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
871 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300872 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873 }
874
Fabio Utzigba829042018-09-18 08:29:34 -0300875#ifdef MCUBOOT_ENC_IMAGES
876 if (fap_src->fa_id == FLASH_AREA_IMAGE_1 ||
877 fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
878 /* assume slot1 as src, needs decryption */
879 hdr = boot_img_hdr(&boot_data, 1);
880 off = off_src;
881 if (fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
882 /* might need encryption (metadata from slot0) */
883 hdr = boot_img_hdr(&boot_data, 0);
884 off = off_dst;
885 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200886 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300887 blk_sz = chunk_sz;
888 idx = 0;
889 if (off + bytes_copied < hdr->ih_hdr_size) {
890 /* do not decrypt header */
891 blk_off = 0;
892 blk_sz = chunk_sz - hdr->ih_hdr_size;
893 idx = hdr->ih_hdr_size;
894 } else {
895 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
896 }
897 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
898 /* do not decrypt TLVs */
899 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
900 blk_sz = 0;
901 } else {
902 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
903 }
904 }
905 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
906 blk_sz, blk_off, &buf[idx]);
907 }
908 }
909#endif
910
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
912 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300913 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800914 }
915
916 bytes_copied += chunk_sz;
917 }
918
Fabio Utzigba829042018-09-18 08:29:34 -0300919 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800920}
921
David Brown6b1b3b92017-09-19 08:59:10 -0600922#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300923static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300924boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300925{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300926 struct boot_swap_state swap_state;
927 int rc;
928
Fabio Utzig2473ac02017-05-02 12:45:02 -0300929 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
930 assert(rc == 0);
931
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400932 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300933 rc = boot_write_image_ok(fap);
934 assert(rc == 0);
935 }
936
Fabio Utzig46490722017-09-04 15:34:32 -0300937 rc = boot_write_swap_size(fap, bs->swap_size);
938 assert(rc == 0);
939
Fabio Utzigba829042018-09-18 08:29:34 -0300940#ifdef MCUBOOT_ENC_IMAGES
941 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300942 assert(rc == 0);
943
Fabio Utzigba829042018-09-18 08:29:34 -0300944 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
945 assert(rc == 0);
946#endif
947
948 rc = boot_write_magic(fap);
949 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300950
951 return 0;
952}
David Brown6b1b3b92017-09-19 08:59:10 -0600953#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300954
Fabio Utzig358c9352017-07-25 22:10:45 -0300955#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300956static int
Fabio Utzigba829042018-09-18 08:29:34 -0300957boot_erase_last_sector(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300958{
959 uint8_t slot;
960 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300961 int rc;
962
Fabio Utzigba829042018-09-18 08:29:34 -0300963 switch (fap->fa_id) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300964 case FLASH_AREA_IMAGE_0:
965 slot = 0;
966 break;
967 case FLASH_AREA_IMAGE_1:
968 slot = 1;
969 break;
970 default:
971 return BOOT_EFLASH;
972 }
973
Marti Bolivard3269fd2017-06-12 16:31:12 -0400974 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -0300975 rc = boot_erase_sector(fap,
Marti Bolivarea088872017-06-12 17:10:49 -0400976 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400977 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300978 assert(rc == 0);
979
980 return rc;
981}
Fabio Utzig358c9352017-07-25 22:10:45 -0300982#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300983
Christopher Collins92ea77f2016-12-12 15:59:26 -0800984/**
985 * Swaps the contents of two flash regions within the two image slots.
986 *
987 * @param idx The index of the first sector in the range of
988 * sectors being swapped.
989 * @param sz The number of bytes to swap.
990 * @param bs The current boot status. This struct gets
991 * updated according to the outcome.
992 *
993 * @return 0 on success; nonzero on failure.
994 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300995#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800996static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800997boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
998{
Fabio Utzigba829042018-09-18 08:29:34 -0300999 const struct flash_area *fap_slot0;
1000 const struct flash_area *fap_slot1;
1001 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001002 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001003 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001004 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001005 uint32_t scratch_trailer_off;
1006 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001007 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001008 int rc;
1009
1010 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -04001011 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001012
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001013 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -04001014 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001015
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001016 /* sz in this function is always sized on a multiple of the sector size.
1017 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001018 * is to determine if we're swapping the last sector. The last sector
1019 * needs special handling because it's where the trailer lives. If we're
1020 * copying it, we need to use scratch to write the trailer temporarily.
1021 *
1022 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1023 * controls if special handling is needed (swapping last sector).
1024 */
Marti Bolivard3269fd2017-06-12 16:31:12 -04001025 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -04001026 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001027 copy_sz -= trailer_sz;
1028 }
1029
Fabio Utzig39000012018-07-30 12:40:20 -03001030 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001031
Fabio Utzigba829042018-09-18 08:29:34 -03001032 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1033 assert (rc == 0);
1034
1035 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1036 assert (rc == 0);
1037
1038 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1039 assert (rc == 0);
1040
Fabio Utzig39000012018-07-30 12:40:20 -03001041 if (bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001042 rc = boot_erase_sector(fap_scratch, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001043 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001044
Fabio Utzigba829042018-09-18 08:29:34 -03001045 rc = boot_copy_sector(fap_slot1, fap_scratch, img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001046 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001047
Fabio Utzig39000012018-07-30 12:40:20 -03001048 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001049 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001050 boot_status_init(fap_scratch, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001051 } else {
1052 /* Prepare the status area... here it is known that the
1053 * last sector is not being used by the image data so it's
1054 * safe to erase.
1055 */
Fabio Utzigba829042018-09-18 08:29:34 -03001056 rc = boot_erase_last_sector(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001057 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001058
Fabio Utzigba829042018-09-18 08:29:34 -03001059 boot_status_init(fap_slot0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001060 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001061 }
1062
Fabio Utzig39000012018-07-30 12:40:20 -03001063 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001064 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001065 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001066 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001067
Fabio Utzig39000012018-07-30 12:40:20 -03001068 if (bs->state == BOOT_STATUS_STATE_1) {
Fabio Utzigba829042018-09-18 08:29:34 -03001069 rc = boot_erase_sector(fap_slot1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001070 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001071
Fabio Utzigba829042018-09-18 08:29:34 -03001072 rc = boot_copy_sector(fap_slot0, fap_slot1, img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001073 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001074
Fabio Utzig39000012018-07-30 12:40:20 -03001075 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001076 /* If not all sectors of the slot are being swapped,
1077 * guarantee here that only slot0 will have the state.
1078 */
Fabio Utzigba829042018-09-18 08:29:34 -03001079 rc = boot_erase_last_sector(fap_slot1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001080 assert(rc == 0);
1081 }
1082
Fabio Utzig39000012018-07-30 12:40:20 -03001083 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001084 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001085 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001086 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001087
Fabio Utzig39000012018-07-30 12:40:20 -03001088 if (bs->state == BOOT_STATUS_STATE_2) {
Fabio Utzigba829042018-09-18 08:29:34 -03001089 rc = boot_erase_sector(fap_slot0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001090 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001091
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001092 /* NOTE: also copy trailer from scratch (has status info) */
Fabio Utzigba829042018-09-18 08:29:34 -03001093 rc = boot_copy_sector(fap_scratch, fap_slot0, 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001094 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001095
Fabio Utzig94d998c2017-05-22 11:02:41 -04001096 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001097 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001098
1099 /* copy current status that is being maintained in scratch */
Fabio Utzigba829042018-09-18 08:29:34 -03001100 rc = boot_copy_sector(fap_scratch, fap_slot0, scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001101 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001102 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001103 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001104
Fabio Utzig2473ac02017-05-02 12:45:02 -03001105 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1106 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001107 assert(rc == 0);
1108
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001109 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzigba829042018-09-18 08:29:34 -03001110 rc = boot_write_image_ok(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001111 assert(rc == 0);
1112 }
1113
Fabio Utzigba829042018-09-18 08:29:34 -03001114 rc = boot_write_swap_size(fap_slot0, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001115 assert(rc == 0);
1116
Fabio Utzigba829042018-09-18 08:29:34 -03001117#ifdef MCUBOOT_ENC_IMAGES
1118 rc = boot_write_enc_key(fap_slot0, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001119 assert(rc == 0);
1120
Fabio Utzigba829042018-09-18 08:29:34 -03001121 rc = boot_write_enc_key(fap_slot0, 1, bs->enckey[1]);
1122 assert(rc == 0);
1123#endif
1124
1125 rc = boot_write_magic(fap_slot0);
1126 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001127 }
1128
Christopher Collins92ea77f2016-12-12 15:59:26 -08001129 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001130 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001131 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001132 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001133 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001134 }
Fabio Utzigba829042018-09-18 08:29:34 -03001135
1136 flash_area_close(fap_slot0);
1137 flash_area_close(fap_slot1);
1138 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001139}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001140#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001141
1142/**
Fabio Utzigba829042018-09-18 08:29:34 -03001143 * Overwrite slot 0 with the image contained in slot 1. If a prior copy
1144 * operation was interrupted by a system reset, this function redos the
1145 * copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001146 *
1147 * @param bs The current boot status. This function reads
1148 * this struct to determine if it is resuming
1149 * an interrupted swap operation. This
1150 * function writes the updated status to this
1151 * function on return.
1152 *
1153 * @return 0 on success; nonzero on failure.
1154 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001155#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -06001156static int
1157boot_copy_image(struct boot_status *bs)
1158{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001159 size_t sect_count;
1160 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001161 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001162 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001163 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001164 size_t last_sector;
Fabio Utzigba829042018-09-18 08:29:34 -03001165 const struct flash_area *fap_slot0;
1166 const struct flash_area *fap_slot1;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001167
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001168 (void)bs;
1169
Fabio Utzig13d9e352017-10-05 20:32:31 -03001170#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1171 uint32_t src_size = 0;
1172 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1173 assert(rc == 0);
1174#endif
David Brown17609d82017-05-05 09:41:34 -06001175
1176 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1177 BOOT_LOG_INF("Erasing slot0");
1178
Fabio Utzigba829042018-09-18 08:29:34 -03001179 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1180 assert (rc == 0);
1181
1182 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1183 assert (rc == 0);
1184
Marti Bolivard3269fd2017-06-12 16:31:12 -04001185 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001186 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001187 this_size = boot_img_sector_size(&boot_data, 0, sect);
Fabio Utzigba829042018-09-18 08:29:34 -03001188 rc = boot_erase_sector(fap_slot0, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001189 assert(rc == 0);
1190
1191 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001192
1193#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1194 if (size >= src_size) {
1195 break;
1196 }
1197#endif
David Brown17609d82017-05-05 09:41:34 -06001198 }
1199
Fabio Utzigba829042018-09-18 08:29:34 -03001200#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001201 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, 1))) {
Fabio Utzige641ea52018-12-03 10:37:53 -02001202 rc = boot_enc_load(boot_img_hdr(&boot_data, 1), fap_slot1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001203 if (rc < 0) {
1204 return BOOT_EBADIMAGE;
1205 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001206 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001207 return BOOT_EBADIMAGE;
1208 }
1209 }
1210#endif
1211
Fabio Utzig6a537ee2017-09-13 17:31:44 -03001212 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%lx bytes", size);
Fabio Utzigba829042018-09-18 08:29:34 -03001213 rc = boot_copy_sector(fap_slot1, fap_slot0, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001214
Fabio Utzig13d9e352017-10-05 20:32:31 -03001215 /*
1216 * Erases header and trailer. The trailer is erased because when a new
1217 * image is written without a trailer as is the case when using newt, the
1218 * trailer that was left might trigger a new upgrade.
1219 */
Fabio Utzigba829042018-09-18 08:29:34 -03001220 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001221 boot_img_sector_off(&boot_data, 1, 0),
1222 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001223 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001224 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -03001225 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001226 boot_img_sector_off(&boot_data, 1, last_sector),
1227 boot_img_sector_size(&boot_data, 1, last_sector));
1228 assert(rc == 0);
1229
Fabio Utzigba829042018-09-18 08:29:34 -03001230 flash_area_close(fap_slot0);
1231 flash_area_close(fap_slot1);
1232
Fabio Utzig13d9e352017-10-05 20:32:31 -03001233 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001234
1235 return 0;
1236}
Fabio Utzigba829042018-09-18 08:29:34 -03001237
David Brown17609d82017-05-05 09:41:34 -06001238#else
Fabio Utzigba829042018-09-18 08:29:34 -03001239
1240/**
1241 * Swaps the two images in flash. If a prior copy operation was interrupted
1242 * by a system reset, this function completes that operation.
1243 *
1244 * @param bs The current boot status. This function reads
1245 * this struct to determine if it is resuming
1246 * an interrupted swap operation. This
1247 * function writes the updated status to this
1248 * function on return.
1249 *
1250 * @return 0 on success; nonzero on failure.
1251 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001252static int
1253boot_copy_image(struct boot_status *bs)
1254{
1255 uint32_t sz;
1256 int first_sector_idx;
1257 int last_sector_idx;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001258 int last_idx_slot1;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001259 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001260 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001261#ifdef MCUBOOT_ENC_IMAGES
1262 const struct flash_area *fap;
1263 uint8_t slot;
1264 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001265#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001266 uint32_t size;
1267 uint32_t copy_size;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001268 uint32_t slot0_size;
1269 uint32_t slot1_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001270 int rc;
1271
1272 /* FIXME: just do this if asked by user? */
1273
1274 size = copy_size = 0;
1275
Fabio Utzig39000012018-07-30 12:40:20 -03001276 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001277 /*
1278 * No swap ever happened, so need to find the largest image which
1279 * will be used to determine the amount of sectors to swap.
1280 */
1281 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001282 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001283 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001284 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001285 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001286
Fabio Utzigba829042018-09-18 08:29:34 -03001287#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001288 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001289 fap = BOOT_IMG_AREA(&boot_data, 0);
1290 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1291 assert(rc >= 0);
1292
1293 if (rc == 0) {
1294 rc = boot_enc_set_key(0, bs->enckey[0]);
1295 assert(rc == 0);
1296 } else {
1297 rc = 0;
1298 }
1299 } else {
1300 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1301 }
1302#endif
1303
Fabio Utzig46490722017-09-04 15:34:32 -03001304 hdr = boot_img_hdr(&boot_data, 1);
1305 if (hdr->ih_magic == IMAGE_MAGIC) {
1306 rc = boot_read_image_size(1, hdr, &size);
1307 assert(rc == 0);
1308 }
1309
Fabio Utzigba829042018-09-18 08:29:34 -03001310#ifdef MCUBOOT_ENC_IMAGES
1311 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001312 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001313 fap = BOOT_IMG_AREA(&boot_data, 1);
1314 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1315 assert(rc >= 0);
1316
1317 if (rc == 0) {
1318 rc = boot_enc_set_key(1, bs->enckey[1]);
1319 assert(rc == 0);
1320 } else {
1321 rc = 0;
1322 }
1323 } else {
1324 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1325 }
1326#endif
1327
Fabio Utzig46490722017-09-04 15:34:32 -03001328 if (size > copy_size) {
1329 copy_size = size;
1330 }
1331
1332 bs->swap_size = copy_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001333
Fabio Utzig46490722017-09-04 15:34:32 -03001334 } else {
1335 /*
1336 * If a swap was under way, the swap_size should already be present
1337 * in the trailer...
1338 */
1339 rc = boot_read_swap_size(&bs->swap_size);
1340 assert(rc == 0);
1341
1342 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001343
1344#ifdef MCUBOOT_ENC_IMAGES
1345 for (slot = 0; slot <= 1; slot++) {
1346 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1347 assert(rc == 0);
1348
1349 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001350 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001351 break;
1352 }
1353 }
1354
1355 if (i != BOOT_ENC_KEY_SIZE) {
1356 boot_enc_set_key(slot, bs->enckey[slot]);
1357 }
1358 }
1359#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001360 }
1361
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001362 slot0_size = 0;
1363 slot1_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001364 last_sector_idx = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001365 last_idx_slot1 = 0;
1366
1367 /*
1368 * Knowing the size of the largest image between both slots, here we
1369 * find what is the last sector in slot0 that needs swapping. Since we
1370 * already know that both slots are compatible, slot1's last sector is
1371 * not really required after this check is finished.
1372 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001373 while (1) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001374 if (slot0_size < copy_size || slot0_size < slot1_size) {
1375 slot0_size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
1376 }
1377 if (slot1_size < copy_size || slot1_size < slot0_size) {
1378 slot1_size += boot_img_sector_size(&boot_data, 1, last_idx_slot1);
1379 }
1380 if (slot0_size >= copy_size &&
1381 slot1_size >= copy_size &&
1382 slot0_size == slot1_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001383 break;
1384 }
1385 last_sector_idx++;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001386 last_idx_slot1++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001387 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001388
1389 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001390 while (last_sector_idx >= 0) {
1391 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001392 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001393 boot_swap_sectors(first_sector_idx, sz, bs);
1394 }
1395
1396 last_sector_idx = first_sector_idx - 1;
1397 swap_idx++;
1398 }
1399
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001400#ifdef MCUBOOT_VALIDATE_SLOT0
1401 if (boot_status_fails > 0) {
1402 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1403 }
1404#endif
1405
Christopher Collins92ea77f2016-12-12 15:59:26 -08001406 return 0;
1407}
David Brown17609d82017-05-05 09:41:34 -06001408#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001409
1410/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001411 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001412 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001413#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001414static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001415boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001416{
1417 const struct flash_area *fap;
1418 int rc;
1419
1420 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1421 if (rc != 0) {
1422 return BOOT_EFLASH;
1423 }
1424
1425 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001426 flash_area_close(fap);
1427 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001428}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001429#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001430
1431/**
1432 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1433 * the status bytes from the image revert operation don't get processed on a
1434 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001435 *
1436 * NOTE: image_ok is tested before writing because if there's a valid permanent
1437 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1438 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001439 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001440#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001441static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001442boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001443{
1444 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001445 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001446 int rc;
1447
1448 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1449 if (rc != 0) {
1450 return BOOT_EFLASH;
1451 }
1452
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001453 rc = boot_read_swap_state(fap, &state);
1454 if (rc != 0) {
1455 rc = BOOT_EFLASH;
1456 goto out;
1457 }
1458
1459 if (state.image_ok == BOOT_FLAG_UNSET) {
1460 rc = boot_write_image_ok(fap);
1461 }
1462
1463out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001464 flash_area_close(fap);
1465 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001466}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001467#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001468
1469/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001470 * Performs an image swap if one is required.
1471 *
1472 * @param out_swap_type On success, the type of swap performed gets
1473 * written here.
1474 *
1475 * @return 0 on success; nonzero on failure.
1476 */
1477static int
1478boot_swap_if_needed(int *out_swap_type)
1479{
1480 struct boot_status bs;
1481 int swap_type;
1482 int rc;
1483
1484 /* Determine if we rebooted in the middle of an image swap
1485 * operation.
1486 */
1487 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001488 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001489 if (rc != 0) {
1490 return rc;
1491 }
1492
1493 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001494 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001495 rc = boot_copy_image(&bs);
1496 assert(rc == 0);
1497
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001498 /* NOTE: here we have finished a swap resume. The initial request
1499 * was either a TEST or PERM swap, which now after the completed
1500 * swap will be determined to be respectively REVERT (was TEST)
1501 * or NONE (was PERM).
1502 */
1503
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001504 /* Extrapolate the type of the partial swap. We need this
1505 * information to know how to mark the swap complete in flash.
1506 */
1507 swap_type = boot_previous_swap_type();
1508 } else {
Fabio Utzigba829042018-09-18 08:29:34 -03001509 swap_type = boot_validated_swap_type(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001510 switch (swap_type) {
1511 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001512 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001513 case BOOT_SWAP_TYPE_REVERT:
1514 rc = boot_copy_image(&bs);
1515 assert(rc == 0);
1516 break;
1517 }
1518 }
1519
1520 *out_swap_type = swap_type;
1521 return 0;
1522}
1523
1524/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001525 * Prepares the booting process. This function moves images around in flash as
1526 * appropriate, and tells you what address to boot from.
1527 *
1528 * @param rsp On success, indicates how booting should occur.
1529 *
1530 * @return 0 on success; nonzero on failure.
1531 */
1532int
1533boot_go(struct boot_rsp *rsp)
1534{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001535 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001536 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001537 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001538 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001539 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001540
1541 /* The array of slot sectors are defined here (as opposed to file scope) so
1542 * that they don't get allocated for non-boot-loader apps. This is
1543 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001544 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001545 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001546 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1547 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001548 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001549 boot_data.imgs[0].sectors = slot0_sectors;
1550 boot_data.imgs[1].sectors = slot1_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001551 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001552
Fabio Utzigba829042018-09-18 08:29:34 -03001553#ifdef MCUBOOT_ENC_IMAGES
1554 /* FIXME: remove this after RAM is cleared by sim */
1555 boot_enc_zeroize();
1556#endif
1557
Marti Bolivarc0b47912017-06-13 17:18:09 -04001558 /* Open boot_data image areas for the duration of this call. */
1559 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1560 fa_id = flash_area_id_from_image_slot(slot);
1561 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1562 assert(rc == 0);
1563 }
1564 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1565 &BOOT_SCRATCH_AREA(&boot_data));
1566 assert(rc == 0);
1567
Christopher Collins92ea77f2016-12-12 15:59:26 -08001568 /* Determine the sector layout of the image slots and scratch area. */
1569 rc = boot_read_sectors();
1570 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001571 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1572 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001573 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001574 }
1575
1576 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001577 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001578 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001579 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001580 }
1581
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001582 /* If the image slots aren't compatible, no swap is possible. Just boot
1583 * into slot 0.
1584 */
1585 if (boot_slots_compatible()) {
1586 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001587 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001588 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001589 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001590 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001591
1592 /*
1593 * The following states need image_ok be explicitly set after the
1594 * swap was finished to avoid a new revert.
1595 */
1596 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001597#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001598 rc = boot_set_image_ok();
1599 if (rc != 0) {
1600 swap_type = BOOT_SWAP_TYPE_PANIC;
1601 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001602#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001603 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001604 } else {
1605 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001606 }
1607
1608 switch (swap_type) {
1609 case BOOT_SWAP_TYPE_NONE:
1610 slot = 0;
1611 break;
1612
Fabio Utzig695d5642017-07-20 09:47:16 -03001613 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1614 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001615 case BOOT_SWAP_TYPE_REVERT:
1616 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001617 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001618#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001619 rc = boot_set_copy_done();
1620 if (rc != 0) {
1621 swap_type = BOOT_SWAP_TYPE_PANIC;
1622 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001623#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001624 break;
1625
1626 case BOOT_SWAP_TYPE_FAIL:
1627 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1628 * try to boot into it again on the next reboot. Do this by pretending
1629 * we just reverted back to slot 0.
1630 */
1631 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001632 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001633 break;
1634
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001635 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001636 swap_type = BOOT_SWAP_TYPE_PANIC;
1637 }
1638
1639 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1640 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001641 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001642
1643 /* Loop forever... */
1644 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001645 }
1646
David Brown52eee562017-07-05 11:25:09 -06001647 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001648 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001649 if (rc != 0) {
1650 goto out;
1651 }
1652 /* Since headers were reloaded, it can be assumed we just performed a
1653 * swap or overwrite. Now the header info that should be used to
1654 * provide the data for the bootstrap, which previously was at Slot 1,
1655 * was updated to Slot 0.
1656 */
1657 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001658 }
1659
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001660#ifdef MCUBOOT_VALIDATE_SLOT0
Fabio Utzigba829042018-09-18 08:29:34 -03001661 rc = boot_validate_slot(0, NULL);
Fabio Utzig57c40f72017-12-12 21:48:30 -02001662 ASSERT(rc == 0);
David Brown554c52e2017-06-30 16:01:07 -06001663 if (rc != 0) {
1664 rc = BOOT_EBADIMAGE;
1665 goto out;
1666 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001667#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001668 /* Even if we're not re-validating slot 0, we could be booting
1669 * onto an empty flash chip. At least do a basic sanity check that
1670 * the magic number on the image is OK.
1671 */
1672 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
Fabio Utzig67716012018-02-26 10:36:15 -03001673 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001674 rc = BOOT_EBADIMAGE;
1675 goto out;
1676 }
David Brown554c52e2017-06-30 16:01:07 -06001677#endif
1678
Christopher Collins92ea77f2016-12-12 15:59:26 -08001679 /* Always boot from the primary slot. */
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +02001680 rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id;
Marti Bolivar88f48d92017-05-01 22:30:02 -04001681 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001682 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001683
Marti Bolivarc0b47912017-06-13 17:18:09 -04001684 out:
1685 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1686 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1687 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1688 }
1689 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001690}
1691
1692int
1693split_go(int loader_slot, int split_slot, void **entry)
1694{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001695 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001696 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001697 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001698 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001699 int rc;
1700
Christopher Collins92ea77f2016-12-12 15:59:26 -08001701 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1702 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001703 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001704 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001705 boot_data.imgs[loader_slot].sectors = sectors + 0;
1706 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1707
1708 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1709 rc = flash_area_open(loader_flash_id,
1710 &BOOT_IMG_AREA(&boot_data, split_slot));
1711 assert(rc == 0);
1712 split_flash_id = flash_area_id_from_image_slot(split_slot);
1713 rc = flash_area_open(split_flash_id,
1714 &BOOT_IMG_AREA(&boot_data, split_slot));
1715 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001716
1717 /* Determine the sector layout of the image slots and scratch area. */
1718 rc = boot_read_sectors();
1719 if (rc != 0) {
1720 rc = SPLIT_GO_ERR;
1721 goto done;
1722 }
1723
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001724 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001725 if (rc != 0) {
1726 goto done;
1727 }
1728
Christopher Collins92ea77f2016-12-12 15:59:26 -08001729 /* Don't check the bootable image flag because we could really call a
1730 * bootable or non-bootable image. Just validate that the image check
1731 * passes which is distinct from the normal check.
1732 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001733 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001734 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001735 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001736 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001737 if (rc != 0) {
1738 rc = SPLIT_GO_NON_MATCHING;
1739 goto done;
1740 }
1741
Marti Bolivarea088872017-06-12 17:10:49 -04001742 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001743 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001744 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001745 rc = SPLIT_GO_OK;
1746
1747done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001748 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1749 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001750 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001751 return rc;
1752}