blob: e7c8d733a582f00a6c262e2b636cf9914ae1fb13 [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);
319 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
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800327static int
328boot_slots_compatible(void)
329{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400330 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
331 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
332 size_t size_0, size_1;
333 size_t i;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800334
Fabio Utziga1fae672018-03-30 10:52:38 -0300335 if (num_sectors_0 > BOOT_MAX_IMG_SECTORS || num_sectors_1 > BOOT_MAX_IMG_SECTORS) {
336 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800337 return 0;
338 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300339
340 /* Ensure both image slots have identical sector layouts. */
341 if (num_sectors_0 != num_sectors_1) {
342 BOOT_LOG_WRN("Cannot upgrade: number of sectors differ between slots");
343 return 0;
344 }
345
Marti Bolivard3269fd2017-06-12 16:31:12 -0400346 for (i = 0; i < num_sectors_0; i++) {
347 size_0 = boot_img_sector_size(&boot_data, 0, i);
348 size_1 = boot_img_sector_size(&boot_data, 1, i);
349 if (size_0 != size_1) {
Fabio Utziga1fae672018-03-30 10:52:38 -0300350 BOOT_LOG_WRN("Cannot upgrade: an incompatible sector was found");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800351 return 0;
352 }
353 }
354
355 return 1;
356}
357
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358/**
359 * Determines the sector layout of both image slots and the scratch area.
360 * This information is necessary for calculating the number of bytes to erase
361 * and copy during an image swap. The information collected during this
362 * function is used to populate the boot_data global.
363 */
364static int
365boot_read_sectors(void)
366{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800367 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368
Marti Bolivarcca28a92017-06-12 16:52:22 -0400369 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 if (rc != 0) {
371 return BOOT_EFLASH;
372 }
373
Marti Bolivarcca28a92017-06-12 16:52:22 -0400374 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375 if (rc != 0) {
376 return BOOT_EFLASH;
377 }
378
Marti Bolivare10a7392017-06-14 16:20:07 -0400379 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380
381 return 0;
382}
383
384static uint32_t
385boot_status_internal_off(int idx, int state, int elem_sz)
386{
387 int idx_sz;
388
389 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
390
Fabio Utzig39000012018-07-30 12:40:20 -0300391 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
392 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800393}
394
395/**
396 * Reads the status of a partially-completed swap, if any. This is necessary
397 * to recover in case the boot lodaer was reset in the middle of a swap
398 * operation.
399 */
400static int
401boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
402{
403 uint32_t off;
404 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300405 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800406 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200407 int found_idx;
408 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409 int rc;
410 int i;
411
412 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400413 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300414
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200416 found_idx = 0;
417 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300418 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300419 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
420 &status, 1);
421 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422 return BOOT_EFLASH;
423 }
424
Fabio Utzig178be542018-09-19 08:12:56 -0300425 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200426 if (found && !found_idx) {
427 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428 }
429 } else if (!found) {
430 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200431 } else if (found_idx) {
432 invalid = 1;
433 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434 }
435 }
436
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200437 if (invalid) {
438 /* This means there was an error writing status on the last
439 * swap. Tell user and move on to validation!
440 */
441 BOOT_LOG_ERR("Detected inconsistent status!");
442
443#if !defined(MCUBOOT_VALIDATE_SLOT0)
444 /* With validation of slot0 disabled, there is no way to be sure the
445 * swapped slot0 is OK, so abort!
446 */
447 assert(0);
448#endif
449 }
450
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200452 if (!found_idx) {
453 found_idx = i;
454 }
455 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300456 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
457 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 }
459
460 return 0;
461}
462
463/**
464 * Reads the boot status from the flash. The boot status contains
465 * the current state of an interrupted image copy operation. If the boot
466 * status is not present, or it indicates that previous copy finished,
467 * there is no operation in progress.
468 */
469static int
470boot_read_status(struct boot_status *bs)
471{
472 const struct flash_area *fap;
473 int status_loc;
474 int area_id;
475 int rc;
476
477 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300478 bs->idx = BOOT_STATUS_IDX_0;
479 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700481#ifdef MCUBOOT_OVERWRITE_ONLY
482 /* Overwrite-only doesn't make use of the swap status area. */
483 return 0;
484#endif
485
Christopher Collins92ea77f2016-12-12 15:59:26 -0800486 status_loc = boot_status_source();
487 switch (status_loc) {
488 case BOOT_STATUS_SOURCE_NONE:
489 return 0;
490
491 case BOOT_STATUS_SOURCE_SCRATCH:
492 area_id = FLASH_AREA_IMAGE_SCRATCH;
493 break;
494
495 case BOOT_STATUS_SOURCE_SLOT0:
496 area_id = FLASH_AREA_IMAGE_0;
497 break;
498
499 default:
500 assert(0);
501 return BOOT_EBADARGS;
502 }
503
504 rc = flash_area_open(area_id, &fap);
505 if (rc != 0) {
506 return BOOT_EFLASH;
507 }
508
Fabio Utzig46490722017-09-04 15:34:32 -0300509 rc = boot_read_status_bytes(fap, bs);
510
511 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700512
Fabio Utzig46490722017-09-04 15:34:32 -0300513 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514}
515
516/**
517 * Writes the supplied boot status to the flash file system. The boot status
518 * contains the current state of an in-progress image copy operation.
519 *
520 * @param bs The boot status to write.
521 *
522 * @return 0 on success; nonzero on failure.
523 */
524int
525boot_write_status(struct boot_status *bs)
526{
527 const struct flash_area *fap;
528 uint32_t off;
529 int area_id;
530 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300531 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700532 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300533 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800534
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300535 /* NOTE: The first sector copied (that is the last sector on slot) contains
536 * the trailer. Since in the last step SLOT 0 is erased, the first
537 * two status writes go to the scratch which will be copied to SLOT 0!
538 */
539
Fabio Utzig2473ac02017-05-02 12:45:02 -0300540 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541 /* Write to scratch. */
542 area_id = FLASH_AREA_IMAGE_SCRATCH;
543 } else {
544 /* Write to slot 0. */
545 area_id = FLASH_AREA_IMAGE_0;
546 }
547
548 rc = flash_area_open(area_id, &fap);
549 if (rc != 0) {
550 rc = BOOT_EFLASH;
551 goto done;
552 }
553
554 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400555 boot_status_internal_off(bs->idx, bs->state,
556 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200557 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300558 erased_val = flash_area_erased_val(fap);
559 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700560 buf[0] = bs->state;
561
562 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800563 if (rc != 0) {
564 rc = BOOT_EFLASH;
565 goto done;
566 }
567
568 rc = 0;
569
570done:
571 flash_area_close(fap);
572 return rc;
573}
574
575/*
576 * Validate image hash/signature in a slot.
577 */
578static int
Fabio Utzigba829042018-09-18 08:29:34 -0300579boot_image_check(struct image_header *hdr, const struct flash_area *fap,
580 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581{
David Browndb1d9d32017-01-06 11:07:54 -0700582 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300583 int rc;
584
585#ifndef MCUBOOT_ENC_IMAGES
586 (void)bs;
587 (void)rc;
588#else
589 if (fap->fa_id == FLASH_AREA_IMAGE_1 && hdr->ih_flags & IMAGE_F_ENCRYPTED) {
590 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
591 if (rc < 0) {
592 return BOOT_EBADIMAGE;
593 }
594 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
595 return BOOT_EBADIMAGE;
596 }
597 }
598#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800599
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
601 NULL, 0, NULL)) {
602 return BOOT_EBADIMAGE;
603 }
604 return 0;
605}
606
607static int
608split_image_check(struct image_header *app_hdr,
609 const struct flash_area *app_fap,
610 struct image_header *loader_hdr,
611 const struct flash_area *loader_fap)
612{
613 static void *tmpbuf;
614 uint8_t loader_hash[32];
615
616 if (!tmpbuf) {
617 tmpbuf = malloc(BOOT_TMPBUF_SZ);
618 if (!tmpbuf) {
619 return BOOT_ENOMEM;
620 }
621 }
622
623 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
624 NULL, 0, loader_hash)) {
625 return BOOT_EBADIMAGE;
626 }
627
628 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
629 loader_hash, 32, NULL)) {
630 return BOOT_EBADIMAGE;
631 }
632
633 return 0;
634}
635
Fabio Utzig39000012018-07-30 12:40:20 -0300636static inline int
637boot_magic_is_erased(uint8_t erased_val, uint32_t magic)
638{
639 uint8_t i;
640 for (i = 0; i < sizeof(magic); i++) {
641 if (erased_val != *(((uint8_t *)&magic) + i)) {
642 return 0;
643 }
644 }
645 return 1;
646}
647
Christopher Collins92ea77f2016-12-12 15:59:26 -0800648static int
Fabio Utzigba829042018-09-18 08:29:34 -0300649boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650{
651 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400652 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300654
David Brownd930ec62016-12-14 07:59:48 -0700655 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656 if (rc != 0) {
657 return BOOT_EFLASH;
658 }
659
Fabio Utzig39000012018-07-30 12:40:20 -0300660 hdr = boot_img_hdr(&boot_data, slot);
661 if (boot_magic_is_erased(flash_area_erased_val(fap), hdr->ih_magic) ||
662 hdr->ih_flags & IMAGE_F_NON_BOOTABLE) {
663 /* No bootable image in slot; continue booting from slot 0. */
664 return -1;
665 }
666
Fabio Utzigba829042018-09-18 08:29:34 -0300667 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700668 if (slot != 0) {
669 flash_area_erase(fap, 0, fap->fa_size);
670 /* Image in slot 1 is invalid. Erase the image and
671 * continue booting from slot 0.
672 */
673 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300674 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800675 return -1;
676 }
677
678 flash_area_close(fap);
679
680 /* Image in slot 1 is valid. */
681 return 0;
682}
683
684/**
685 * Determines which swap operation to perform, if any. If it is determined
686 * that a swap operation is required, the image in the second slot is checked
687 * for validity. If the image in the second slot is invalid, it is erased, and
688 * a swap type of "none" is indicated.
689 *
690 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
691 */
692static int
Fabio Utzigba829042018-09-18 08:29:34 -0300693boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800694{
695 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800696
697 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300698 switch (swap_type) {
699 case BOOT_SWAP_TYPE_TEST:
700 case BOOT_SWAP_TYPE_PERM:
701 case BOOT_SWAP_TYPE_REVERT:
702 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
Fabio Utzigba829042018-09-18 08:29:34 -0300703 if (boot_validate_slot(1, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300704 swap_type = BOOT_SWAP_TYPE_FAIL;
705 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706 }
707
708 return swap_type;
709}
710
711/**
712 * Calculates the number of sectors the scratch area can contain. A "last"
713 * source sector is specified because images are copied backwards in flash
714 * (final index to index number 0).
715 *
716 * @param last_sector_idx The index of the last source sector
717 * (inclusive).
718 * @param out_first_sector_idx The index of the first source sector
719 * (inclusive) gets written here.
720 *
721 * @return The number of bytes comprised by the
722 * [first-sector, last-sector] range.
723 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300724#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800725static uint32_t
726boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
727{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400728 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800729 uint32_t new_sz;
730 uint32_t sz;
731 int i;
732
733 sz = 0;
734
Marti Bolivard3269fd2017-06-12 16:31:12 -0400735 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800736 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400737 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
738 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739 break;
740 }
741 sz = new_sz;
742 }
743
744 /* i currently refers to a sector that doesn't fit or it is -1 because all
745 * sectors have been processed. In both cases, exclude sector i.
746 */
747 *out_first_sector_idx = i + 1;
748 return sz;
749}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300750#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800751
752/**
753 * Erases a region of flash.
754 *
Fabio Utzigba829042018-09-18 08:29:34 -0300755 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800756 * @param off The offset within the flash area to start the
757 * erase.
758 * @param sz The number of bytes to erase.
759 *
760 * @return 0 on success; nonzero on failure.
761 */
Fabio Utzigba829042018-09-18 08:29:34 -0300762static inline int
763boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800764{
Fabio Utzigba829042018-09-18 08:29:34 -0300765 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800766}
767
768/**
769 * Copies the contents of one flash region to another. You must erase the
770 * destination region prior to calling this function.
771 *
772 * @param flash_area_id_src The ID of the source flash area.
773 * @param flash_area_id_dst The ID of the destination flash area.
774 * @param off_src The offset within the source flash area to
775 * copy from.
776 * @param off_dst The offset within the destination flash area to
777 * copy to.
778 * @param sz The number of bytes to copy.
779 *
780 * @return 0 on success; nonzero on failure.
781 */
782static int
Fabio Utzigba829042018-09-18 08:29:34 -0300783boot_copy_sector(const struct flash_area *fap_src,
784 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800785 uint32_t off_src, uint32_t off_dst, uint32_t sz)
786{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800787 uint32_t bytes_copied;
788 int chunk_sz;
789 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300790#ifdef MCUBOOT_ENC_IMAGES
791 uint32_t off;
792 size_t blk_off;
793 struct image_header *hdr;
794 uint16_t idx;
795 uint32_t blk_sz;
796#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800797
798 static uint8_t buf[1024];
799
Christopher Collins92ea77f2016-12-12 15:59:26 -0800800 bytes_copied = 0;
801 while (bytes_copied < sz) {
802 if (sz - bytes_copied > sizeof buf) {
803 chunk_sz = sizeof buf;
804 } else {
805 chunk_sz = sz - bytes_copied;
806 }
807
808 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
809 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300810 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800811 }
812
Fabio Utzigba829042018-09-18 08:29:34 -0300813#ifdef MCUBOOT_ENC_IMAGES
814 if (fap_src->fa_id == FLASH_AREA_IMAGE_1 ||
815 fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
816 /* assume slot1 as src, needs decryption */
817 hdr = boot_img_hdr(&boot_data, 1);
818 off = off_src;
819 if (fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
820 /* might need encryption (metadata from slot0) */
821 hdr = boot_img_hdr(&boot_data, 0);
822 off = off_dst;
823 }
824 if (hdr->ih_flags & IMAGE_F_ENCRYPTED) {
825 blk_sz = chunk_sz;
826 idx = 0;
827 if (off + bytes_copied < hdr->ih_hdr_size) {
828 /* do not decrypt header */
829 blk_off = 0;
830 blk_sz = chunk_sz - hdr->ih_hdr_size;
831 idx = hdr->ih_hdr_size;
832 } else {
833 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
834 }
835 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
836 /* do not decrypt TLVs */
837 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
838 blk_sz = 0;
839 } else {
840 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
841 }
842 }
843 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
844 blk_sz, blk_off, &buf[idx]);
845 }
846 }
847#endif
848
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
850 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300851 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800852 }
853
854 bytes_copied += chunk_sz;
855 }
856
Fabio Utzigba829042018-09-18 08:29:34 -0300857 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800858}
859
David Brown6b1b3b92017-09-19 08:59:10 -0600860#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300861static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300862boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300863{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300864 struct boot_swap_state swap_state;
865 int rc;
866
Fabio Utzig2473ac02017-05-02 12:45:02 -0300867 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
868 assert(rc == 0);
869
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400870 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300871 rc = boot_write_image_ok(fap);
872 assert(rc == 0);
873 }
874
Fabio Utzig46490722017-09-04 15:34:32 -0300875 rc = boot_write_swap_size(fap, bs->swap_size);
876 assert(rc == 0);
877
Fabio Utzigba829042018-09-18 08:29:34 -0300878#ifdef MCUBOOT_ENC_IMAGES
879 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300880 assert(rc == 0);
881
Fabio Utzigba829042018-09-18 08:29:34 -0300882 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
883 assert(rc == 0);
884#endif
885
886 rc = boot_write_magic(fap);
887 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300888
889 return 0;
890}
David Brown6b1b3b92017-09-19 08:59:10 -0600891#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300892
Fabio Utzig358c9352017-07-25 22:10:45 -0300893#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300894static int
Fabio Utzigba829042018-09-18 08:29:34 -0300895boot_erase_last_sector(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300896{
897 uint8_t slot;
898 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300899 int rc;
900
Fabio Utzigba829042018-09-18 08:29:34 -0300901 switch (fap->fa_id) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300902 case FLASH_AREA_IMAGE_0:
903 slot = 0;
904 break;
905 case FLASH_AREA_IMAGE_1:
906 slot = 1;
907 break;
908 default:
909 return BOOT_EFLASH;
910 }
911
Marti Bolivard3269fd2017-06-12 16:31:12 -0400912 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -0300913 rc = boot_erase_sector(fap,
Marti Bolivarea088872017-06-12 17:10:49 -0400914 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -0400915 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -0300916 assert(rc == 0);
917
918 return rc;
919}
Fabio Utzig358c9352017-07-25 22:10:45 -0300920#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -0300921
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922/**
923 * Swaps the contents of two flash regions within the two image slots.
924 *
925 * @param idx The index of the first sector in the range of
926 * sectors being swapped.
927 * @param sz The number of bytes to swap.
928 * @param bs The current boot status. This struct gets
929 * updated according to the outcome.
930 *
931 * @return 0 on success; nonzero on failure.
932 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300933#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -0800934static void
Christopher Collins92ea77f2016-12-12 15:59:26 -0800935boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
936{
Fabio Utzigba829042018-09-18 08:29:34 -0300937 const struct flash_area *fap_slot0;
938 const struct flash_area *fap_slot1;
939 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800940 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300941 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800942 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300943 uint32_t scratch_trailer_off;
944 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400945 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800946 int rc;
947
948 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -0400949 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800950
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300951 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -0400952 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -0400953
954 /* sz in this function is always is always sized on a multiple of the
Marti Bolivarea088872017-06-12 17:10:49 -0400955 * sector size. The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -0400956 * is to determine if we're swapping the last sector. The last sector
957 * needs special handling because it's where the trailer lives. If we're
958 * copying it, we need to use scratch to write the trailer temporarily.
959 *
960 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
961 * controls if special handling is needed (swapping last sector).
962 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400963 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -0400964 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300965 copy_sz -= trailer_sz;
966 }
967
Fabio Utzig39000012018-07-30 12:40:20 -0300968 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300969
Fabio Utzigba829042018-09-18 08:29:34 -0300970 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
971 assert (rc == 0);
972
973 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
974 assert (rc == 0);
975
976 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
977 assert (rc == 0);
978
Fabio Utzig39000012018-07-30 12:40:20 -0300979 if (bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300980 rc = boot_erase_sector(fap_scratch, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800981 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982
Fabio Utzigba829042018-09-18 08:29:34 -0300983 rc = boot_copy_sector(fap_slot1, fap_scratch, img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -0800984 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985
Fabio Utzig39000012018-07-30 12:40:20 -0300986 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300987 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -0300988 boot_status_init(fap_scratch, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300989 } else {
990 /* Prepare the status area... here it is known that the
991 * last sector is not being used by the image data so it's
992 * safe to erase.
993 */
Fabio Utzigba829042018-09-18 08:29:34 -0300994 rc = boot_erase_last_sector(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300995 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300996
Fabio Utzigba829042018-09-18 08:29:34 -0300997 boot_status_init(fap_slot0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300998 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300999 }
1000
Fabio Utzig39000012018-07-30 12:40:20 -03001001 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001002 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001003 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001004 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001005
Fabio Utzig39000012018-07-30 12:40:20 -03001006 if (bs->state == BOOT_STATUS_STATE_1) {
Fabio Utzigba829042018-09-18 08:29:34 -03001007 rc = boot_erase_sector(fap_slot1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001008 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009
Fabio Utzigba829042018-09-18 08:29:34 -03001010 rc = boot_copy_sector(fap_slot0, fap_slot1, img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001011 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001012
Fabio Utzig39000012018-07-30 12:40:20 -03001013 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001014 /* If not all sectors of the slot are being swapped,
1015 * guarantee here that only slot0 will have the state.
1016 */
Fabio Utzigba829042018-09-18 08:29:34 -03001017 rc = boot_erase_last_sector(fap_slot1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001018 assert(rc == 0);
1019 }
1020
Fabio Utzig39000012018-07-30 12:40:20 -03001021 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001022 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001023 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001024 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001025
Fabio Utzig39000012018-07-30 12:40:20 -03001026 if (bs->state == BOOT_STATUS_STATE_2) {
Fabio Utzigba829042018-09-18 08:29:34 -03001027 rc = boot_erase_sector(fap_slot0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001028 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001029
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001030 /* NOTE: also copy trailer from scratch (has status info) */
Fabio Utzigba829042018-09-18 08:29:34 -03001031 rc = boot_copy_sector(fap_scratch, fap_slot0, 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001032 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033
Fabio Utzig94d998c2017-05-22 11:02:41 -04001034 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001035 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001036
1037 /* copy current status that is being maintained in scratch */
Fabio Utzigba829042018-09-18 08:29:34 -03001038 rc = boot_copy_sector(fap_scratch, fap_slot0, scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001039 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001040 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001041 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001042
Fabio Utzig2473ac02017-05-02 12:45:02 -03001043 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1044 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001045 assert(rc == 0);
1046
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001047 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzigba829042018-09-18 08:29:34 -03001048 rc = boot_write_image_ok(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001049 assert(rc == 0);
1050 }
1051
Fabio Utzigba829042018-09-18 08:29:34 -03001052 rc = boot_write_swap_size(fap_slot0, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001053 assert(rc == 0);
1054
Fabio Utzigba829042018-09-18 08:29:34 -03001055#ifdef MCUBOOT_ENC_IMAGES
1056 rc = boot_write_enc_key(fap_slot0, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001057 assert(rc == 0);
1058
Fabio Utzigba829042018-09-18 08:29:34 -03001059 rc = boot_write_enc_key(fap_slot0, 1, bs->enckey[1]);
1060 assert(rc == 0);
1061#endif
1062
1063 rc = boot_write_magic(fap_slot0);
1064 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001065 }
1066
Christopher Collins92ea77f2016-12-12 15:59:26 -08001067 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001068 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001069 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001070 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001071 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001072 }
Fabio Utzigba829042018-09-18 08:29:34 -03001073
1074 flash_area_close(fap_slot0);
1075 flash_area_close(fap_slot1);
1076 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001077}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001078#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001079
1080/**
Fabio Utzigba829042018-09-18 08:29:34 -03001081 * Overwrite slot 0 with the image contained in slot 1. If a prior copy
1082 * operation was interrupted by a system reset, this function redos the
1083 * copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001084 *
1085 * @param bs The current boot status. This function reads
1086 * this struct to determine if it is resuming
1087 * an interrupted swap operation. This
1088 * function writes the updated status to this
1089 * function on return.
1090 *
1091 * @return 0 on success; nonzero on failure.
1092 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001093#ifdef MCUBOOT_OVERWRITE_ONLY
David Brown17609d82017-05-05 09:41:34 -06001094static int
1095boot_copy_image(struct boot_status *bs)
1096{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001097 size_t sect_count;
1098 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001099 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001100 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001101 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001102 size_t last_sector;
Fabio Utzigba829042018-09-18 08:29:34 -03001103 const struct flash_area *fap_slot0;
1104 const struct flash_area *fap_slot1;
1105#ifdef MCUBOOT_ENC_IMAGES
1106 uint8_t enckey[BOOT_ENC_KEY_SIZE];
1107#endif
Fabio Utzig13d9e352017-10-05 20:32:31 -03001108
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001109 (void)bs;
1110
Fabio Utzig13d9e352017-10-05 20:32:31 -03001111#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1112 uint32_t src_size = 0;
1113 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1114 assert(rc == 0);
1115#endif
David Brown17609d82017-05-05 09:41:34 -06001116
1117 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1118 BOOT_LOG_INF("Erasing slot0");
1119
Fabio Utzigba829042018-09-18 08:29:34 -03001120 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1121 assert (rc == 0);
1122
1123 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1124 assert (rc == 0);
1125
Marti Bolivard3269fd2017-06-12 16:31:12 -04001126 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001127 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001128 this_size = boot_img_sector_size(&boot_data, 0, sect);
Fabio Utzigba829042018-09-18 08:29:34 -03001129 rc = boot_erase_sector(fap_slot0, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001130 assert(rc == 0);
1131
1132 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001133
1134#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1135 if (size >= src_size) {
1136 break;
1137 }
1138#endif
David Brown17609d82017-05-05 09:41:34 -06001139 }
1140
Fabio Utzigba829042018-09-18 08:29:34 -03001141#ifdef MCUBOOT_ENC_IMAGES
1142 if (boot_img_hdr(&boot_data, 1)->ih_flags & IMAGE_F_ENCRYPTED) {
1143 rc = boot_enc_load(boot_img_hdr(&boot_data, 1), fap_slot1, enckey);
1144 if (rc < 0) {
1145 return BOOT_EBADIMAGE;
1146 }
1147 if (rc == 0 && boot_enc_set_key(1, enckey)) {
1148 return BOOT_EBADIMAGE;
1149 }
1150 }
1151#endif
1152
Fabio Utzig6a537ee2017-09-13 17:31:44 -03001153 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%lx bytes", size);
Fabio Utzigba829042018-09-18 08:29:34 -03001154 rc = boot_copy_sector(fap_slot1, fap_slot0, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001155
Fabio Utzig13d9e352017-10-05 20:32:31 -03001156 /*
1157 * Erases header and trailer. The trailer is erased because when a new
1158 * image is written without a trailer as is the case when using newt, the
1159 * trailer that was left might trigger a new upgrade.
1160 */
Fabio Utzigba829042018-09-18 08:29:34 -03001161 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001162 boot_img_sector_off(&boot_data, 1, 0),
1163 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001164 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001165 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -03001166 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001167 boot_img_sector_off(&boot_data, 1, last_sector),
1168 boot_img_sector_size(&boot_data, 1, last_sector));
1169 assert(rc == 0);
1170
Fabio Utzigba829042018-09-18 08:29:34 -03001171 flash_area_close(fap_slot0);
1172 flash_area_close(fap_slot1);
1173
Fabio Utzig13d9e352017-10-05 20:32:31 -03001174 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001175
1176 return 0;
1177}
Fabio Utzigba829042018-09-18 08:29:34 -03001178
David Brown17609d82017-05-05 09:41:34 -06001179#else
Fabio Utzigba829042018-09-18 08:29:34 -03001180
1181/**
1182 * Swaps the two images in flash. If a prior copy operation was interrupted
1183 * by a system reset, this function completes that operation.
1184 *
1185 * @param bs The current boot status. This function reads
1186 * this struct to determine if it is resuming
1187 * an interrupted swap operation. This
1188 * function writes the updated status to this
1189 * function on return.
1190 *
1191 * @return 0 on success; nonzero on failure.
1192 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001193static int
1194boot_copy_image(struct boot_status *bs)
1195{
1196 uint32_t sz;
1197 int first_sector_idx;
1198 int last_sector_idx;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001199 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001200 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001201#ifdef MCUBOOT_ENC_IMAGES
1202 const struct flash_area *fap;
1203 uint8_t slot;
1204 uint8_t i;
1205 uint8_t enckey[BOOT_ENC_KEY_SIZE];
1206#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001207 uint32_t size;
1208 uint32_t copy_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001209 int rc;
1210
1211 /* FIXME: just do this if asked by user? */
1212
1213 size = copy_size = 0;
1214
Fabio Utzig39000012018-07-30 12:40:20 -03001215 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001216 /*
1217 * No swap ever happened, so need to find the largest image which
1218 * will be used to determine the amount of sectors to swap.
1219 */
1220 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001221 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001222 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001223 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001224 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001225
Fabio Utzigba829042018-09-18 08:29:34 -03001226#ifdef MCUBOOT_ENC_IMAGES
1227 if (hdr->ih_flags & IMAGE_F_ENCRYPTED) {
1228 fap = BOOT_IMG_AREA(&boot_data, 0);
1229 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1230 assert(rc >= 0);
1231
1232 if (rc == 0) {
1233 rc = boot_enc_set_key(0, bs->enckey[0]);
1234 assert(rc == 0);
1235 } else {
1236 rc = 0;
1237 }
1238 } else {
1239 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1240 }
1241#endif
1242
Fabio Utzig46490722017-09-04 15:34:32 -03001243 hdr = boot_img_hdr(&boot_data, 1);
1244 if (hdr->ih_magic == IMAGE_MAGIC) {
1245 rc = boot_read_image_size(1, hdr, &size);
1246 assert(rc == 0);
1247 }
1248
Fabio Utzigba829042018-09-18 08:29:34 -03001249#ifdef MCUBOOT_ENC_IMAGES
1250 hdr = boot_img_hdr(&boot_data, 1);
1251 if (hdr->ih_flags & IMAGE_F_ENCRYPTED) {
1252 fap = BOOT_IMG_AREA(&boot_data, 1);
1253 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1254 assert(rc >= 0);
1255
1256 if (rc == 0) {
1257 rc = boot_enc_set_key(1, bs->enckey[1]);
1258 assert(rc == 0);
1259 } else {
1260 rc = 0;
1261 }
1262 } else {
1263 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1264 }
1265#endif
1266
Fabio Utzig46490722017-09-04 15:34:32 -03001267 if (size > copy_size) {
1268 copy_size = size;
1269 }
1270
1271 bs->swap_size = copy_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001272
Fabio Utzig46490722017-09-04 15:34:32 -03001273 } else {
1274 /*
1275 * If a swap was under way, the swap_size should already be present
1276 * in the trailer...
1277 */
1278 rc = boot_read_swap_size(&bs->swap_size);
1279 assert(rc == 0);
1280
1281 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001282
1283#ifdef MCUBOOT_ENC_IMAGES
1284 for (slot = 0; slot <= 1; slot++) {
1285 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1286 assert(rc == 0);
1287
1288 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
1289 if (enckey[i] != 0xff) {
1290 break;
1291 }
1292 }
1293
1294 if (i != BOOT_ENC_KEY_SIZE) {
1295 boot_enc_set_key(slot, bs->enckey[slot]);
1296 }
1297 }
1298#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001299 }
1300
1301 size = 0;
1302 last_sector_idx = 0;
1303 while (1) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001304 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001305 if (size >= copy_size) {
1306 break;
1307 }
1308 last_sector_idx++;
1309 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001310
1311 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001312 while (last_sector_idx >= 0) {
1313 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001314 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001315 boot_swap_sectors(first_sector_idx, sz, bs);
1316 }
1317
1318 last_sector_idx = first_sector_idx - 1;
1319 swap_idx++;
1320 }
1321
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001322#ifdef MCUBOOT_VALIDATE_SLOT0
1323 if (boot_status_fails > 0) {
1324 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1325 }
1326#endif
1327
Christopher Collins92ea77f2016-12-12 15:59:26 -08001328 return 0;
1329}
David Brown17609d82017-05-05 09:41:34 -06001330#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001331
1332/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001333 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001334 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001335#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001336static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001337boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338{
1339 const struct flash_area *fap;
1340 int rc;
1341
1342 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1343 if (rc != 0) {
1344 return BOOT_EFLASH;
1345 }
1346
1347 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001348 flash_area_close(fap);
1349 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001350}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001351#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001352
1353/**
1354 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1355 * the status bytes from the image revert operation don't get processed on a
1356 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001357 *
1358 * NOTE: image_ok is tested before writing because if there's a valid permanent
1359 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1360 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001361 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001362#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001363static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001364boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001365{
1366 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001367 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001368 int rc;
1369
1370 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1371 if (rc != 0) {
1372 return BOOT_EFLASH;
1373 }
1374
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001375 rc = boot_read_swap_state(fap, &state);
1376 if (rc != 0) {
1377 rc = BOOT_EFLASH;
1378 goto out;
1379 }
1380
1381 if (state.image_ok == BOOT_FLAG_UNSET) {
1382 rc = boot_write_image_ok(fap);
1383 }
1384
1385out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001386 flash_area_close(fap);
1387 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001388}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001389#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001390
1391/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001392 * Performs an image swap if one is required.
1393 *
1394 * @param out_swap_type On success, the type of swap performed gets
1395 * written here.
1396 *
1397 * @return 0 on success; nonzero on failure.
1398 */
1399static int
1400boot_swap_if_needed(int *out_swap_type)
1401{
1402 struct boot_status bs;
1403 int swap_type;
1404 int rc;
1405
1406 /* Determine if we rebooted in the middle of an image swap
1407 * operation.
1408 */
1409 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001410 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001411 if (rc != 0) {
1412 return rc;
1413 }
1414
1415 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001416 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001417 rc = boot_copy_image(&bs);
1418 assert(rc == 0);
1419
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001420 /* NOTE: here we have finished a swap resume. The initial request
1421 * was either a TEST or PERM swap, which now after the completed
1422 * swap will be determined to be respectively REVERT (was TEST)
1423 * or NONE (was PERM).
1424 */
1425
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001426 /* Extrapolate the type of the partial swap. We need this
1427 * information to know how to mark the swap complete in flash.
1428 */
1429 swap_type = boot_previous_swap_type();
1430 } else {
Fabio Utzigba829042018-09-18 08:29:34 -03001431 swap_type = boot_validated_swap_type(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001432 switch (swap_type) {
1433 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001434 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001435 case BOOT_SWAP_TYPE_REVERT:
1436 rc = boot_copy_image(&bs);
1437 assert(rc == 0);
1438 break;
1439 }
1440 }
1441
1442 *out_swap_type = swap_type;
1443 return 0;
1444}
1445
1446/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001447 * Prepares the booting process. This function moves images around in flash as
1448 * appropriate, and tells you what address to boot from.
1449 *
1450 * @param rsp On success, indicates how booting should occur.
1451 *
1452 * @return 0 on success; nonzero on failure.
1453 */
1454int
1455boot_go(struct boot_rsp *rsp)
1456{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001457 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001458 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001459 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001460 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001461 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001462
1463 /* The array of slot sectors are defined here (as opposed to file scope) so
1464 * that they don't get allocated for non-boot-loader apps. This is
1465 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001466 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001467 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001468 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1469 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001470 boot_data.imgs[0].sectors = slot0_sectors;
1471 boot_data.imgs[1].sectors = slot1_sectors;
1472
Fabio Utzigba829042018-09-18 08:29:34 -03001473#ifdef MCUBOOT_ENC_IMAGES
1474 /* FIXME: remove this after RAM is cleared by sim */
1475 boot_enc_zeroize();
1476#endif
1477
Marti Bolivarc0b47912017-06-13 17:18:09 -04001478 /* Open boot_data image areas for the duration of this call. */
1479 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1480 fa_id = flash_area_id_from_image_slot(slot);
1481 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1482 assert(rc == 0);
1483 }
1484 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1485 &BOOT_SCRATCH_AREA(&boot_data));
1486 assert(rc == 0);
1487
Christopher Collins92ea77f2016-12-12 15:59:26 -08001488 /* Determine the sector layout of the image slots and scratch area. */
1489 rc = boot_read_sectors();
1490 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001491 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1492 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001493 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001494 }
1495
1496 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001497 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001498 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001499 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001500 }
1501
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001502 /* If the image slots aren't compatible, no swap is possible. Just boot
1503 * into slot 0.
1504 */
1505 if (boot_slots_compatible()) {
1506 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001507 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001508 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001509 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001510 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001511
1512 /*
1513 * The following states need image_ok be explicitly set after the
1514 * swap was finished to avoid a new revert.
1515 */
1516 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001517#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001518 rc = boot_set_image_ok();
1519 if (rc != 0) {
1520 swap_type = BOOT_SWAP_TYPE_PANIC;
1521 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001522#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001523 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001524 } else {
1525 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001526 }
1527
1528 switch (swap_type) {
1529 case BOOT_SWAP_TYPE_NONE:
1530 slot = 0;
1531 break;
1532
Fabio Utzig695d5642017-07-20 09:47:16 -03001533 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1534 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001535 case BOOT_SWAP_TYPE_REVERT:
1536 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001537 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001538#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001539 rc = boot_set_copy_done();
1540 if (rc != 0) {
1541 swap_type = BOOT_SWAP_TYPE_PANIC;
1542 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001543#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001544 break;
1545
1546 case BOOT_SWAP_TYPE_FAIL:
1547 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1548 * try to boot into it again on the next reboot. Do this by pretending
1549 * we just reverted back to slot 0.
1550 */
1551 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001552 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001553 break;
1554
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001555 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001556 swap_type = BOOT_SWAP_TYPE_PANIC;
1557 }
1558
1559 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1560 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001561 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001562
1563 /* Loop forever... */
1564 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001565 }
1566
David Brown52eee562017-07-05 11:25:09 -06001567 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001568 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001569 if (rc != 0) {
1570 goto out;
1571 }
1572 /* Since headers were reloaded, it can be assumed we just performed a
1573 * swap or overwrite. Now the header info that should be used to
1574 * provide the data for the bootstrap, which previously was at Slot 1,
1575 * was updated to Slot 0.
1576 */
1577 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001578 }
1579
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001580#ifdef MCUBOOT_VALIDATE_SLOT0
Fabio Utzigba829042018-09-18 08:29:34 -03001581 rc = boot_validate_slot(0, NULL);
Fabio Utzig57c40f72017-12-12 21:48:30 -02001582 ASSERT(rc == 0);
David Brown554c52e2017-06-30 16:01:07 -06001583 if (rc != 0) {
1584 rc = BOOT_EBADIMAGE;
1585 goto out;
1586 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001587#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001588 /* Even if we're not re-validating slot 0, we could be booting
1589 * onto an empty flash chip. At least do a basic sanity check that
1590 * the magic number on the image is OK.
1591 */
1592 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
Fabio Utzig67716012018-02-26 10:36:15 -03001593 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001594 rc = BOOT_EBADIMAGE;
1595 goto out;
1596 }
David Brown554c52e2017-06-30 16:01:07 -06001597#endif
1598
Christopher Collins92ea77f2016-12-12 15:59:26 -08001599 /* Always boot from the primary slot. */
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +02001600 rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id;
Marti Bolivar88f48d92017-05-01 22:30:02 -04001601 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001602 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001603
Marti Bolivarc0b47912017-06-13 17:18:09 -04001604 out:
1605 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1606 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1607 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1608 }
1609 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001610}
1611
1612int
1613split_go(int loader_slot, int split_slot, void **entry)
1614{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001615 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001616 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001617 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001618 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001619 int rc;
1620
Christopher Collins92ea77f2016-12-12 15:59:26 -08001621 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1622 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001623 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001624 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001625 boot_data.imgs[loader_slot].sectors = sectors + 0;
1626 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1627
1628 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1629 rc = flash_area_open(loader_flash_id,
1630 &BOOT_IMG_AREA(&boot_data, split_slot));
1631 assert(rc == 0);
1632 split_flash_id = flash_area_id_from_image_slot(split_slot);
1633 rc = flash_area_open(split_flash_id,
1634 &BOOT_IMG_AREA(&boot_data, split_slot));
1635 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001636
1637 /* Determine the sector layout of the image slots and scratch area. */
1638 rc = boot_read_sectors();
1639 if (rc != 0) {
1640 rc = SPLIT_GO_ERR;
1641 goto done;
1642 }
1643
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001644 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001645 if (rc != 0) {
1646 goto done;
1647 }
1648
Christopher Collins92ea77f2016-12-12 15:59:26 -08001649 /* Don't check the bootable image flag because we could really call a
1650 * bootable or non-bootable image. Just validate that the image check
1651 * passes which is distinct from the normal check.
1652 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001653 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001654 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001655 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001656 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001657 if (rc != 0) {
1658 rc = SPLIT_GO_NON_MATCHING;
1659 goto done;
1660 }
1661
Marti Bolivarea088872017-06-12 17:10:49 -04001662 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001663 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001664 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001665 rc = SPLIT_GO_OK;
1666
1667done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001668 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1669 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001670 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001671 return rc;
1672}