blob: 1711ae954af8e8e21b3d2c7a2f1b262c33ec4e8f [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 Utzig338a19f2018-12-03 08:37:08 -0200693/*
694 * Check that a memory area consists of a given value.
695 */
696static inline bool
697boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300698{
699 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200700 uint8_t *p = (uint8_t *)data;
701 for (i = 0; i < len; i++) {
702 if (val != p[i]) {
703 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300704 }
705 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200706 return true;
707}
708
709static int
710boot_check_header_erased(int slot)
711{
712 const struct flash_area *fap;
713 struct image_header *hdr;
714 uint8_t erased_val;
715 int rc;
716
717 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
718 if (rc != 0) {
719 return -1;
720 }
721
722 erased_val = flash_area_erased_val(fap);
723 flash_area_close(fap);
724
725 hdr = boot_img_hdr(&boot_data, slot);
726 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
727 return -1;
728 }
729
730 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300731}
732
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733static int
Fabio Utzigba829042018-09-18 08:29:34 -0300734boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735{
736 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400737 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800738 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300739
David Brownd930ec62016-12-14 07:59:48 -0700740 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741 if (rc != 0) {
742 return BOOT_EFLASH;
743 }
744
Fabio Utzig39000012018-07-30 12:40:20 -0300745 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200746 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig39000012018-07-30 12:40:20 -0300747 /* No bootable image in slot; continue booting from slot 0. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200748 rc = -1;
749 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300750 }
751
Fabio Utzigba829042018-09-18 08:29:34 -0300752 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700753 if (slot != 0) {
754 flash_area_erase(fap, 0, fap->fa_size);
755 /* Image in slot 1 is invalid. Erase the image and
756 * continue booting from slot 0.
757 */
758 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300759 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200760 rc = -1;
761 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800762 }
763
Christopher Collins92ea77f2016-12-12 15:59:26 -0800764 /* Image in slot 1 is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200765 rc = 0;
766
767out:
768 flash_area_close(fap);
769 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800770}
771
772/**
773 * Determines which swap operation to perform, if any. If it is determined
774 * that a swap operation is required, the image in the second slot is checked
775 * for validity. If the image in the second slot is invalid, it is erased, and
776 * a swap type of "none" is indicated.
777 *
778 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
779 */
780static int
Fabio Utzigba829042018-09-18 08:29:34 -0300781boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800782{
783 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800784
785 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300786 switch (swap_type) {
787 case BOOT_SWAP_TYPE_TEST:
788 case BOOT_SWAP_TYPE_PERM:
789 case BOOT_SWAP_TYPE_REVERT:
790 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
Fabio Utzigba829042018-09-18 08:29:34 -0300791 if (boot_validate_slot(1, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300792 swap_type = BOOT_SWAP_TYPE_FAIL;
793 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800794 }
795
796 return swap_type;
797}
798
799/**
800 * Calculates the number of sectors the scratch area can contain. A "last"
801 * source sector is specified because images are copied backwards in flash
802 * (final index to index number 0).
803 *
804 * @param last_sector_idx The index of the last source sector
805 * (inclusive).
806 * @param out_first_sector_idx The index of the first source sector
807 * (inclusive) gets written here.
808 *
809 * @return The number of bytes comprised by the
810 * [first-sector, last-sector] range.
811 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300812#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800813static uint32_t
814boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
815{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400816 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817 uint32_t new_sz;
818 uint32_t sz;
819 int i;
820
821 sz = 0;
822
Marti Bolivard3269fd2017-06-12 16:31:12 -0400823 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800824 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400825 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200826 /*
827 * slot1 is not being checked here, because `boot_slots_compatible`
828 * already provides assurance that the copy size will be compatible
829 * with slot0 and scratch.
830 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400831 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800832 break;
833 }
834 sz = new_sz;
835 }
836
837 /* i currently refers to a sector that doesn't fit or it is -1 because all
838 * sectors have been processed. In both cases, exclude sector i.
839 */
840 *out_first_sector_idx = i + 1;
841 return sz;
842}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300843#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800844
845/**
846 * Erases a region of flash.
847 *
Fabio Utzigba829042018-09-18 08:29:34 -0300848 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849 * @param off The offset within the flash area to start the
850 * erase.
851 * @param sz The number of bytes to erase.
852 *
853 * @return 0 on success; nonzero on failure.
854 */
Fabio Utzigba829042018-09-18 08:29:34 -0300855static inline int
856boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800857{
Fabio Utzigba829042018-09-18 08:29:34 -0300858 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859}
860
861/**
862 * Copies the contents of one flash region to another. You must erase the
863 * destination region prior to calling this function.
864 *
865 * @param flash_area_id_src The ID of the source flash area.
866 * @param flash_area_id_dst The ID of the destination flash area.
867 * @param off_src The offset within the source flash area to
868 * copy from.
869 * @param off_dst The offset within the destination flash area to
870 * copy to.
871 * @param sz The number of bytes to copy.
872 *
873 * @return 0 on success; nonzero on failure.
874 */
875static int
Fabio Utzigba829042018-09-18 08:29:34 -0300876boot_copy_sector(const struct flash_area *fap_src,
877 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878 uint32_t off_src, uint32_t off_dst, uint32_t sz)
879{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800880 uint32_t bytes_copied;
881 int chunk_sz;
882 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300883#ifdef MCUBOOT_ENC_IMAGES
884 uint32_t off;
885 size_t blk_off;
886 struct image_header *hdr;
887 uint16_t idx;
888 uint32_t blk_sz;
889#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800890
891 static uint8_t buf[1024];
892
Christopher Collins92ea77f2016-12-12 15:59:26 -0800893 bytes_copied = 0;
894 while (bytes_copied < sz) {
895 if (sz - bytes_copied > sizeof buf) {
896 chunk_sz = sizeof buf;
897 } else {
898 chunk_sz = sz - bytes_copied;
899 }
900
901 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
902 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300903 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904 }
905
Fabio Utzigba829042018-09-18 08:29:34 -0300906#ifdef MCUBOOT_ENC_IMAGES
907 if (fap_src->fa_id == FLASH_AREA_IMAGE_1 ||
908 fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
909 /* assume slot1 as src, needs decryption */
910 hdr = boot_img_hdr(&boot_data, 1);
911 off = off_src;
912 if (fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
913 /* might need encryption (metadata from slot0) */
914 hdr = boot_img_hdr(&boot_data, 0);
915 off = off_dst;
916 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200917 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300918 blk_sz = chunk_sz;
919 idx = 0;
920 if (off + bytes_copied < hdr->ih_hdr_size) {
921 /* do not decrypt header */
922 blk_off = 0;
923 blk_sz = chunk_sz - hdr->ih_hdr_size;
924 idx = hdr->ih_hdr_size;
925 } else {
926 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
927 }
928 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
929 /* do not decrypt TLVs */
930 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
931 blk_sz = 0;
932 } else {
933 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
934 }
935 }
936 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
937 blk_sz, blk_off, &buf[idx]);
938 }
939 }
940#endif
941
Christopher Collins92ea77f2016-12-12 15:59:26 -0800942 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
943 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300944 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800945 }
946
947 bytes_copied += chunk_sz;
948 }
949
Fabio Utzigba829042018-09-18 08:29:34 -0300950 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800951}
952
David Brown6b1b3b92017-09-19 08:59:10 -0600953#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300954static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300955boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300956{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300957 struct boot_swap_state swap_state;
958 int rc;
959
Fabio Utzig2473ac02017-05-02 12:45:02 -0300960 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
961 assert(rc == 0);
962
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400963 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300964 rc = boot_write_image_ok(fap);
965 assert(rc == 0);
966 }
967
Fabio Utzig46490722017-09-04 15:34:32 -0300968 rc = boot_write_swap_size(fap, bs->swap_size);
969 assert(rc == 0);
970
Fabio Utzigba829042018-09-18 08:29:34 -0300971#ifdef MCUBOOT_ENC_IMAGES
972 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300973 assert(rc == 0);
974
Fabio Utzigba829042018-09-18 08:29:34 -0300975 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
976 assert(rc == 0);
977#endif
978
979 rc = boot_write_magic(fap);
980 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300981
982 return 0;
983}
David Brown6b1b3b92017-09-19 08:59:10 -0600984#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300985
Fabio Utzig358c9352017-07-25 22:10:45 -0300986#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300987static int
Fabio Utzigba829042018-09-18 08:29:34 -0300988boot_erase_last_sector(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300989{
990 uint8_t slot;
991 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300992 int rc;
993
Fabio Utzigba829042018-09-18 08:29:34 -0300994 switch (fap->fa_id) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300995 case FLASH_AREA_IMAGE_0:
996 slot = 0;
997 break;
998 case FLASH_AREA_IMAGE_1:
999 slot = 1;
1000 break;
1001 default:
1002 return BOOT_EFLASH;
1003 }
1004
Marti Bolivard3269fd2017-06-12 16:31:12 -04001005 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -03001006 rc = boot_erase_sector(fap,
Marti Bolivarea088872017-06-12 17:10:49 -04001007 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -04001008 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -03001009 assert(rc == 0);
1010
1011 return rc;
1012}
Fabio Utzig358c9352017-07-25 22:10:45 -03001013#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001014
Christopher Collins92ea77f2016-12-12 15:59:26 -08001015/**
1016 * Swaps the contents of two flash regions within the two image slots.
1017 *
1018 * @param idx The index of the first sector in the range of
1019 * sectors being swapped.
1020 * @param sz The number of bytes to swap.
1021 * @param bs The current boot status. This struct gets
1022 * updated according to the outcome.
1023 *
1024 * @return 0 on success; nonzero on failure.
1025 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001026#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001027static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001028boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1029{
Fabio Utzigba829042018-09-18 08:29:34 -03001030 const struct flash_area *fap_slot0;
1031 const struct flash_area *fap_slot1;
1032 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001034 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001036 uint32_t scratch_trailer_off;
1037 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001038 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001039 int rc;
1040
1041 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -04001042 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001043
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001044 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -04001045 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001046
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001047 /* sz in this function is always sized on a multiple of the sector size.
1048 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001049 * is to determine if we're swapping the last sector. The last sector
1050 * needs special handling because it's where the trailer lives. If we're
1051 * copying it, we need to use scratch to write the trailer temporarily.
1052 *
1053 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1054 * controls if special handling is needed (swapping last sector).
1055 */
Marti Bolivard3269fd2017-06-12 16:31:12 -04001056 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -04001057 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001058 copy_sz -= trailer_sz;
1059 }
1060
Fabio Utzig39000012018-07-30 12:40:20 -03001061 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001062
Fabio Utzigba829042018-09-18 08:29:34 -03001063 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1064 assert (rc == 0);
1065
1066 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1067 assert (rc == 0);
1068
1069 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1070 assert (rc == 0);
1071
Fabio Utzig39000012018-07-30 12:40:20 -03001072 if (bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001073 rc = boot_erase_sector(fap_scratch, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001074 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001075
Fabio Utzigba829042018-09-18 08:29:34 -03001076 rc = boot_copy_sector(fap_slot1, fap_scratch, img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001077 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078
Fabio Utzig39000012018-07-30 12:40:20 -03001079 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001080 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001081 boot_status_init(fap_scratch, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001082 } else {
1083 /* Prepare the status area... here it is known that the
1084 * last sector is not being used by the image data so it's
1085 * safe to erase.
1086 */
Fabio Utzigba829042018-09-18 08:29:34 -03001087 rc = boot_erase_last_sector(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001088 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001089
Fabio Utzigba829042018-09-18 08:29:34 -03001090 boot_status_init(fap_slot0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001091 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001092 }
1093
Fabio Utzig39000012018-07-30 12:40:20 -03001094 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001095 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001096 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001097 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001098
Fabio Utzig39000012018-07-30 12:40:20 -03001099 if (bs->state == BOOT_STATUS_STATE_1) {
Fabio Utzigba829042018-09-18 08:29:34 -03001100 rc = boot_erase_sector(fap_slot1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001101 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001102
Fabio Utzigba829042018-09-18 08:29:34 -03001103 rc = boot_copy_sector(fap_slot0, fap_slot1, img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001104 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001105
Fabio Utzig39000012018-07-30 12:40:20 -03001106 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001107 /* If not all sectors of the slot are being swapped,
1108 * guarantee here that only slot0 will have the state.
1109 */
Fabio Utzigba829042018-09-18 08:29:34 -03001110 rc = boot_erase_last_sector(fap_slot1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001111 assert(rc == 0);
1112 }
1113
Fabio Utzig39000012018-07-30 12:40:20 -03001114 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001115 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001116 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001117 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001118
Fabio Utzig39000012018-07-30 12:40:20 -03001119 if (bs->state == BOOT_STATUS_STATE_2) {
Fabio Utzigba829042018-09-18 08:29:34 -03001120 rc = boot_erase_sector(fap_slot0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001121 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001122
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001123 /* NOTE: also copy trailer from scratch (has status info) */
Fabio Utzigba829042018-09-18 08:29:34 -03001124 rc = boot_copy_sector(fap_scratch, fap_slot0, 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001125 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001126
Fabio Utzig94d998c2017-05-22 11:02:41 -04001127 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001128 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001129
1130 /* copy current status that is being maintained in scratch */
Fabio Utzigba829042018-09-18 08:29:34 -03001131 rc = boot_copy_sector(fap_scratch, fap_slot0, scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001132 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001133 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001134 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001135
Fabio Utzig2473ac02017-05-02 12:45:02 -03001136 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1137 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001138 assert(rc == 0);
1139
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001140 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzigba829042018-09-18 08:29:34 -03001141 rc = boot_write_image_ok(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001142 assert(rc == 0);
1143 }
1144
Fabio Utzigba829042018-09-18 08:29:34 -03001145 rc = boot_write_swap_size(fap_slot0, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001146 assert(rc == 0);
1147
Fabio Utzigba829042018-09-18 08:29:34 -03001148#ifdef MCUBOOT_ENC_IMAGES
1149 rc = boot_write_enc_key(fap_slot0, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001150 assert(rc == 0);
1151
Fabio Utzigba829042018-09-18 08:29:34 -03001152 rc = boot_write_enc_key(fap_slot0, 1, bs->enckey[1]);
1153 assert(rc == 0);
1154#endif
1155
1156 rc = boot_write_magic(fap_slot0);
1157 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001158 }
1159
Christopher Collins92ea77f2016-12-12 15:59:26 -08001160 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001161 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001162 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001163 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001164 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001165 }
Fabio Utzigba829042018-09-18 08:29:34 -03001166
1167 flash_area_close(fap_slot0);
1168 flash_area_close(fap_slot1);
1169 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001170}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001171#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001172
1173/**
Fabio Utzigba829042018-09-18 08:29:34 -03001174 * Overwrite slot 0 with the image contained in slot 1. If a prior copy
1175 * operation was interrupted by a system reset, this function redos the
1176 * copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001177 *
1178 * @param bs The current boot status. This function reads
1179 * this struct to determine if it is resuming
1180 * an interrupted swap operation. This
1181 * function writes the updated status to this
1182 * function on return.
1183 *
1184 * @return 0 on success; nonzero on failure.
1185 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001186#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001187static int
1188boot_copy_image(struct boot_status *bs)
1189{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001190 size_t sect_count;
1191 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001192 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001193 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001194 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001195 size_t last_sector;
Fabio Utzigba829042018-09-18 08:29:34 -03001196 const struct flash_area *fap_slot0;
1197 const struct flash_area *fap_slot1;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001198
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001199 (void)bs;
1200
Fabio Utzig13d9e352017-10-05 20:32:31 -03001201#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1202 uint32_t src_size = 0;
1203 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1204 assert(rc == 0);
1205#endif
David Brown17609d82017-05-05 09:41:34 -06001206
1207 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1208 BOOT_LOG_INF("Erasing slot0");
1209
Fabio Utzigba829042018-09-18 08:29:34 -03001210 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1211 assert (rc == 0);
1212
1213 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1214 assert (rc == 0);
1215
Marti Bolivard3269fd2017-06-12 16:31:12 -04001216 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001217 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001218 this_size = boot_img_sector_size(&boot_data, 0, sect);
Fabio Utzigba829042018-09-18 08:29:34 -03001219 rc = boot_erase_sector(fap_slot0, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001220 assert(rc == 0);
1221
1222 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001223
1224#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1225 if (size >= src_size) {
1226 break;
1227 }
1228#endif
David Brown17609d82017-05-05 09:41:34 -06001229 }
1230
Fabio Utzigba829042018-09-18 08:29:34 -03001231#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001232 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, 1))) {
Fabio Utzige641ea52018-12-03 10:37:53 -02001233 rc = boot_enc_load(boot_img_hdr(&boot_data, 1), fap_slot1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001234 if (rc < 0) {
1235 return BOOT_EBADIMAGE;
1236 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001237 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001238 return BOOT_EBADIMAGE;
1239 }
1240 }
1241#endif
1242
Fabio Utzig338a19f2018-12-03 08:37:08 -02001243 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%zx bytes", size);
Fabio Utzigba829042018-09-18 08:29:34 -03001244 rc = boot_copy_sector(fap_slot1, fap_slot0, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001245
Fabio Utzig13d9e352017-10-05 20:32:31 -03001246 /*
1247 * Erases header and trailer. The trailer is erased because when a new
1248 * image is written without a trailer as is the case when using newt, the
1249 * trailer that was left might trigger a new upgrade.
1250 */
Fabio Utzigba829042018-09-18 08:29:34 -03001251 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001252 boot_img_sector_off(&boot_data, 1, 0),
1253 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001254 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001255 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -03001256 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001257 boot_img_sector_off(&boot_data, 1, last_sector),
1258 boot_img_sector_size(&boot_data, 1, last_sector));
1259 assert(rc == 0);
1260
Fabio Utzigba829042018-09-18 08:29:34 -03001261 flash_area_close(fap_slot0);
1262 flash_area_close(fap_slot1);
1263
Fabio Utzig13d9e352017-10-05 20:32:31 -03001264 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001265
1266 return 0;
1267}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001268#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001269
1270/**
1271 * Swaps the two images in flash. If a prior copy operation was interrupted
1272 * by a system reset, this function completes that operation.
1273 *
1274 * @param bs The current boot status. This function reads
1275 * this struct to determine if it is resuming
1276 * an interrupted swap operation. This
1277 * function writes the updated status to this
1278 * function on return.
1279 *
1280 * @return 0 on success; nonzero on failure.
1281 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001282#if !defined(MCUBOOT_OVERWRITE_ONLY)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001283static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001284boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001285{
1286 uint32_t sz;
1287 int first_sector_idx;
1288 int last_sector_idx;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001289 int last_idx_slot1;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001290 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001291 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001292#ifdef MCUBOOT_ENC_IMAGES
1293 const struct flash_area *fap;
1294 uint8_t slot;
1295 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001296#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001297 uint32_t size;
1298 uint32_t copy_size;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001299 uint32_t slot0_size;
1300 uint32_t slot1_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001301 int rc;
1302
1303 /* FIXME: just do this if asked by user? */
1304
1305 size = copy_size = 0;
1306
Fabio Utzig39000012018-07-30 12:40:20 -03001307 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001308 /*
1309 * No swap ever happened, so need to find the largest image which
1310 * will be used to determine the amount of sectors to swap.
1311 */
1312 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001313 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001314 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001315 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001316 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001317
Fabio Utzigba829042018-09-18 08:29:34 -03001318#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001319 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001320 fap = BOOT_IMG_AREA(&boot_data, 0);
1321 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1322 assert(rc >= 0);
1323
1324 if (rc == 0) {
1325 rc = boot_enc_set_key(0, bs->enckey[0]);
1326 assert(rc == 0);
1327 } else {
1328 rc = 0;
1329 }
1330 } else {
1331 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1332 }
1333#endif
1334
Fabio Utzig46490722017-09-04 15:34:32 -03001335 hdr = boot_img_hdr(&boot_data, 1);
1336 if (hdr->ih_magic == IMAGE_MAGIC) {
1337 rc = boot_read_image_size(1, hdr, &size);
1338 assert(rc == 0);
1339 }
1340
Fabio Utzigba829042018-09-18 08:29:34 -03001341#ifdef MCUBOOT_ENC_IMAGES
1342 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001343 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001344 fap = BOOT_IMG_AREA(&boot_data, 1);
1345 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1346 assert(rc >= 0);
1347
1348 if (rc == 0) {
1349 rc = boot_enc_set_key(1, bs->enckey[1]);
1350 assert(rc == 0);
1351 } else {
1352 rc = 0;
1353 }
1354 } else {
1355 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1356 }
1357#endif
1358
Fabio Utzig46490722017-09-04 15:34:32 -03001359 if (size > copy_size) {
1360 copy_size = size;
1361 }
1362
1363 bs->swap_size = copy_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001364
Fabio Utzig46490722017-09-04 15:34:32 -03001365 } else {
1366 /*
1367 * If a swap was under way, the swap_size should already be present
1368 * in the trailer...
1369 */
1370 rc = boot_read_swap_size(&bs->swap_size);
1371 assert(rc == 0);
1372
1373 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001374
1375#ifdef MCUBOOT_ENC_IMAGES
1376 for (slot = 0; slot <= 1; slot++) {
1377 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1378 assert(rc == 0);
1379
1380 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001381 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001382 break;
1383 }
1384 }
1385
1386 if (i != BOOT_ENC_KEY_SIZE) {
1387 boot_enc_set_key(slot, bs->enckey[slot]);
1388 }
1389 }
1390#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001391 }
1392
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001393 slot0_size = 0;
1394 slot1_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001395 last_sector_idx = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001396 last_idx_slot1 = 0;
1397
1398 /*
1399 * Knowing the size of the largest image between both slots, here we
1400 * find what is the last sector in slot0 that needs swapping. Since we
1401 * already know that both slots are compatible, slot1's last sector is
1402 * not really required after this check is finished.
1403 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001404 while (1) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001405 if (slot0_size < copy_size || slot0_size < slot1_size) {
1406 slot0_size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
1407 }
1408 if (slot1_size < copy_size || slot1_size < slot0_size) {
1409 slot1_size += boot_img_sector_size(&boot_data, 1, last_idx_slot1);
1410 }
1411 if (slot0_size >= copy_size &&
1412 slot1_size >= copy_size &&
1413 slot0_size == slot1_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001414 break;
1415 }
1416 last_sector_idx++;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001417 last_idx_slot1++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001418 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001419
1420 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001421 while (last_sector_idx >= 0) {
1422 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001423 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001424 boot_swap_sectors(first_sector_idx, sz, bs);
1425 }
1426
1427 last_sector_idx = first_sector_idx - 1;
1428 swap_idx++;
1429 }
1430
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001431#ifdef MCUBOOT_VALIDATE_SLOT0
1432 if (boot_status_fails > 0) {
1433 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1434 }
1435#endif
1436
Christopher Collins92ea77f2016-12-12 15:59:26 -08001437 return 0;
1438}
David Brown17609d82017-05-05 09:41:34 -06001439#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001440
1441/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001442 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001443 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001444#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001445static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001446boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001447{
1448 const struct flash_area *fap;
1449 int rc;
1450
1451 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1452 if (rc != 0) {
1453 return BOOT_EFLASH;
1454 }
1455
1456 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001457 flash_area_close(fap);
1458 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001459}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001460#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001461
1462/**
1463 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1464 * the status bytes from the image revert operation don't get processed on a
1465 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001466 *
1467 * NOTE: image_ok is tested before writing because if there's a valid permanent
1468 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1469 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001470 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001471#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001472static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001473boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001474{
1475 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001476 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001477 int rc;
1478
1479 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1480 if (rc != 0) {
1481 return BOOT_EFLASH;
1482 }
1483
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001484 rc = boot_read_swap_state(fap, &state);
1485 if (rc != 0) {
1486 rc = BOOT_EFLASH;
1487 goto out;
1488 }
1489
1490 if (state.image_ok == BOOT_FLAG_UNSET) {
1491 rc = boot_write_image_ok(fap);
1492 }
1493
1494out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001495 flash_area_close(fap);
1496 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001497}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001498#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001499
1500/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001501 * Performs an image swap if one is required.
1502 *
1503 * @param out_swap_type On success, the type of swap performed gets
1504 * written here.
1505 *
1506 * @return 0 on success; nonzero on failure.
1507 */
1508static int
1509boot_swap_if_needed(int *out_swap_type)
1510{
1511 struct boot_status bs;
1512 int swap_type;
1513 int rc;
1514
1515 /* Determine if we rebooted in the middle of an image swap
1516 * operation.
1517 */
1518 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001519 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001520 if (rc != 0) {
1521 return rc;
1522 }
1523
1524 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001525 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utzig338a19f2018-12-03 08:37:08 -02001526#if MCUBOOT_OVERWRITE_ONLY
1527 /* Should never arrive here, overwrite-only mode has no swap state. */
1528 assert(0);
1529#else
1530 rc = boot_swap_image(&bs);
1531#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001532 assert(rc == 0);
1533
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001534 /* NOTE: here we have finished a swap resume. The initial request
1535 * was either a TEST or PERM swap, which now after the completed
1536 * swap will be determined to be respectively REVERT (was TEST)
1537 * or NONE (was PERM).
1538 */
1539
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001540 /* Extrapolate the type of the partial swap. We need this
1541 * information to know how to mark the swap complete in flash.
1542 */
1543 swap_type = boot_previous_swap_type();
1544 } else {
Fabio Utzigba829042018-09-18 08:29:34 -03001545 swap_type = boot_validated_swap_type(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001546 switch (swap_type) {
1547 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001548 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001549 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig338a19f2018-12-03 08:37:08 -02001550#if MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001551 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001552#else
1553 rc = boot_swap_image(&bs);
1554#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001555 assert(rc == 0);
1556 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001557#ifdef MCUBOOT_BOOTSTRAP
1558 case BOOT_SWAP_TYPE_NONE:
1559 /*
1560 * Header checks are done first because they are inexpensive.
1561 * Since overwrite-only copies starting from offset 0, if
1562 * interrupted, it might leave a valid header magic, so also
1563 * run validation on slot0 to be sure it's not OK.
1564 */
1565 if (boot_check_header_erased(0) == 0 ||
1566 boot_validate_slot(0, &bs) != 0) {
1567 if (boot_img_hdr(&boot_data, 1)->ih_magic == IMAGE_MAGIC &&
1568 boot_validate_slot(1, &bs) == 0) {
1569 rc = boot_copy_image(&bs);
1570 assert(rc == 0);
1571
1572 /* Returns fail here to trigger a re-read of the headers. */
1573 swap_type = BOOT_SWAP_TYPE_FAIL;
1574 }
1575 }
1576 break;
1577#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001578 }
1579 }
1580
1581 *out_swap_type = swap_type;
1582 return 0;
1583}
1584
1585/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001586 * Prepares the booting process. This function moves images around in flash as
1587 * appropriate, and tells you what address to boot from.
1588 *
1589 * @param rsp On success, indicates how booting should occur.
1590 *
1591 * @return 0 on success; nonzero on failure.
1592 */
1593int
1594boot_go(struct boot_rsp *rsp)
1595{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001596 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001597 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001598 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001599 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001600 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001601
1602 /* The array of slot sectors are defined here (as opposed to file scope) so
1603 * that they don't get allocated for non-boot-loader apps. This is
1604 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001605 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001606 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001607 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1608 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001609 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001610 boot_data.imgs[0].sectors = slot0_sectors;
1611 boot_data.imgs[1].sectors = slot1_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001612 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001613
Fabio Utzigba829042018-09-18 08:29:34 -03001614#ifdef MCUBOOT_ENC_IMAGES
1615 /* FIXME: remove this after RAM is cleared by sim */
1616 boot_enc_zeroize();
1617#endif
1618
Marti Bolivarc0b47912017-06-13 17:18:09 -04001619 /* Open boot_data image areas for the duration of this call. */
1620 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1621 fa_id = flash_area_id_from_image_slot(slot);
1622 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1623 assert(rc == 0);
1624 }
1625 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1626 &BOOT_SCRATCH_AREA(&boot_data));
1627 assert(rc == 0);
1628
Christopher Collins92ea77f2016-12-12 15:59:26 -08001629 /* Determine the sector layout of the image slots and scratch area. */
1630 rc = boot_read_sectors();
1631 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001632 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1633 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001634 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001635 }
1636
1637 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001638 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001639 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001640 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001641 }
1642
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001643 /* If the image slots aren't compatible, no swap is possible. Just boot
1644 * into slot 0.
1645 */
1646 if (boot_slots_compatible()) {
1647 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001648 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001649 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001650 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001651 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001652
1653 /*
1654 * The following states need image_ok be explicitly set after the
1655 * swap was finished to avoid a new revert.
1656 */
1657 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001658#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001659 rc = boot_set_image_ok();
1660 if (rc != 0) {
1661 swap_type = BOOT_SWAP_TYPE_PANIC;
1662 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001663#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001664 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001665 } else {
1666 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001667 }
1668
1669 switch (swap_type) {
1670 case BOOT_SWAP_TYPE_NONE:
1671 slot = 0;
1672 break;
1673
Fabio Utzig695d5642017-07-20 09:47:16 -03001674 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1675 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001676 case BOOT_SWAP_TYPE_REVERT:
1677 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001678 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001679#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001680 rc = boot_set_copy_done();
1681 if (rc != 0) {
1682 swap_type = BOOT_SWAP_TYPE_PANIC;
1683 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001684#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001685 break;
1686
1687 case BOOT_SWAP_TYPE_FAIL:
1688 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1689 * try to boot into it again on the next reboot. Do this by pretending
1690 * we just reverted back to slot 0.
1691 */
1692 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001693 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001694 break;
1695
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001696 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001697 swap_type = BOOT_SWAP_TYPE_PANIC;
1698 }
1699
1700 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1701 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001702 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001703
1704 /* Loop forever... */
1705 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001706 }
1707
David Brown52eee562017-07-05 11:25:09 -06001708 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001709 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001710 if (rc != 0) {
1711 goto out;
1712 }
1713 /* Since headers were reloaded, it can be assumed we just performed a
1714 * swap or overwrite. Now the header info that should be used to
1715 * provide the data for the bootstrap, which previously was at Slot 1,
1716 * was updated to Slot 0.
1717 */
1718 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001719 }
1720
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001721#ifdef MCUBOOT_VALIDATE_SLOT0
Fabio Utzigba829042018-09-18 08:29:34 -03001722 rc = boot_validate_slot(0, NULL);
Fabio Utzig57c40f72017-12-12 21:48:30 -02001723 ASSERT(rc == 0);
David Brown554c52e2017-06-30 16:01:07 -06001724 if (rc != 0) {
1725 rc = BOOT_EBADIMAGE;
1726 goto out;
1727 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001728#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001729 /* Even if we're not re-validating slot 0, we could be booting
1730 * onto an empty flash chip. At least do a basic sanity check that
1731 * the magic number on the image is OK.
1732 */
1733 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
Fabio Utzig67716012018-02-26 10:36:15 -03001734 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001735 rc = BOOT_EBADIMAGE;
1736 goto out;
1737 }
David Brown554c52e2017-06-30 16:01:07 -06001738#endif
1739
Christopher Collins92ea77f2016-12-12 15:59:26 -08001740 /* Always boot from the primary slot. */
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +02001741 rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id;
Marti Bolivar88f48d92017-05-01 22:30:02 -04001742 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001743 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001744
Marti Bolivarc0b47912017-06-13 17:18:09 -04001745 out:
1746 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1747 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1748 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1749 }
1750 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001751}
1752
1753int
1754split_go(int loader_slot, int split_slot, void **entry)
1755{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001756 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001757 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001758 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001759 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001760 int rc;
1761
Christopher Collins92ea77f2016-12-12 15:59:26 -08001762 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1763 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001764 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001765 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001766 boot_data.imgs[loader_slot].sectors = sectors + 0;
1767 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1768
1769 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1770 rc = flash_area_open(loader_flash_id,
1771 &BOOT_IMG_AREA(&boot_data, split_slot));
1772 assert(rc == 0);
1773 split_flash_id = flash_area_id_from_image_slot(split_slot);
1774 rc = flash_area_open(split_flash_id,
1775 &BOOT_IMG_AREA(&boot_data, split_slot));
1776 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001777
1778 /* Determine the sector layout of the image slots and scratch area. */
1779 rc = boot_read_sectors();
1780 if (rc != 0) {
1781 rc = SPLIT_GO_ERR;
1782 goto done;
1783 }
1784
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001785 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001786 if (rc != 0) {
1787 goto done;
1788 }
1789
Christopher Collins92ea77f2016-12-12 15:59:26 -08001790 /* Don't check the bootable image flag because we could really call a
1791 * bootable or non-bootable image. Just validate that the image check
1792 * passes which is distinct from the normal check.
1793 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001794 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001795 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001796 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001797 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001798 if (rc != 0) {
1799 rc = SPLIT_GO_NON_MATCHING;
1800 goto done;
1801 }
1802
Marti Bolivarea088872017-06-12 17:10:49 -04001803 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001804 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001805 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001806 rc = SPLIT_GO_OK;
1807
1808done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001809 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1810 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001811 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001812 return rc;
1813}