blob: af54b8766c68701ce6e71a1e7620f364c8a80fe2 [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
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010043MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
44
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040045static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080046
Fabio Utzigf70e3022018-01-10 15:00:42 -020047#if defined(MCUBOOT_VALIDATE_SLOT0) && !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020048static int boot_status_fails = 0;
49#define BOOT_STATUS_ASSERT(x) \
50 do { \
Johann Fischered8461b2018-02-15 16:50:31 +010051 if (!(x)) { \
Fabio Utziga0e1cce2017-11-23 20:04:01 -020052 boot_status_fails++; \
53 } \
54 } while (0)
55#else
Fabio Utzig57c40f72017-12-12 21:48:30 -020056#define BOOT_STATUS_ASSERT(x) ASSERT(x)
Fabio Utziga0e1cce2017-11-23 20:04:01 -020057#endif
58
Christopher Collins92ea77f2016-12-12 15:59:26 -080059struct boot_status_table {
Christopher Collins92ea77f2016-12-12 15:59:26 -080060 uint8_t bst_magic_slot0;
61 uint8_t bst_magic_scratch;
62 uint8_t bst_copy_done_slot0;
63 uint8_t bst_status_source;
64};
65
66/**
67 * This set of tables maps swap state contents to boot status location.
68 * When searching for a match, these tables must be iterated in order.
69 */
70static const struct boot_status_table boot_status_tables[] = {
71 {
72 /* | slot-0 | scratch |
73 * ----------+------------+------------|
74 * magic | Good | Any |
Fabio Utzig39000012018-07-30 12:40:20 -030075 * copy-done | Set | N/A |
Christopher Collins92ea77f2016-12-12 15:59:26 -080076 * ----------+------------+------------'
77 * source: none |
78 * ------------------------------------'
79 */
80 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030081 .bst_magic_scratch = BOOT_MAGIC_ANY,
82 .bst_copy_done_slot0 = BOOT_FLAG_SET,
Christopher Collins92ea77f2016-12-12 15:59:26 -080083 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
84 },
85
86 {
87 /* | slot-0 | scratch |
88 * ----------+------------+------------|
89 * magic | Good | Any |
Fabio Utzig39000012018-07-30 12:40:20 -030090 * copy-done | Unset | N/A |
Christopher Collins92ea77f2016-12-12 15:59:26 -080091 * ----------+------------+------------'
92 * source: slot 0 |
93 * ------------------------------------'
94 */
95 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -030096 .bst_magic_scratch = BOOT_MAGIC_ANY,
97 .bst_copy_done_slot0 = BOOT_FLAG_UNSET,
Christopher Collins92ea77f2016-12-12 15:59:26 -080098 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
99 },
100
101 {
102 /* | slot-0 | scratch |
103 * ----------+------------+------------|
104 * magic | Any | Good |
105 * copy-done | Any | N/A |
106 * ----------+------------+------------'
107 * source: scratch |
108 * ------------------------------------'
109 */
Fabio Utzig39000012018-07-30 12:40:20 -0300110 .bst_magic_slot0 = BOOT_MAGIC_ANY,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800111 .bst_magic_scratch = BOOT_MAGIC_GOOD,
Fabio Utzig39000012018-07-30 12:40:20 -0300112 .bst_copy_done_slot0 = BOOT_FLAG_ANY,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800113 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
114 },
Christopher Collins92ea77f2016-12-12 15:59:26 -0800115 {
116 /* | slot-0 | scratch |
117 * ----------+------------+------------|
118 * magic | Unset | Any |
Fabio Utzig39000012018-07-30 12:40:20 -0300119 * copy-done | Unset | N/A |
Christopher Collins92ea77f2016-12-12 15:59:26 -0800120 * ----------+------------+------------|
121 * source: varies |
122 * ------------------------------------+------------------------------+
123 * This represents one of two cases: |
124 * o No swaps ever (no status to read, so no harm in checking). |
125 * o Mid-revert; status in slot 0. |
126 * -------------------------------------------------------------------'
127 */
128 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
Fabio Utzig39000012018-07-30 12:40:20 -0300129 .bst_magic_scratch = BOOT_MAGIC_ANY,
130 .bst_copy_done_slot0 = BOOT_FLAG_UNSET,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800131 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
132 },
133};
134
135#define BOOT_STATUS_TABLES_COUNT \
136 (sizeof boot_status_tables / sizeof boot_status_tables[0])
137
Marti Bolivarfd20c762017-02-07 16:52:50 -0500138#define BOOT_LOG_SWAP_STATE(area, state) \
139 BOOT_LOG_INF("%s: magic=%s, copy_done=0x%x, image_ok=0x%x", \
140 (area), \
141 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
142 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
143 "bad"), \
144 (state)->copy_done, \
145 (state)->image_ok)
146
Christopher Collins92ea77f2016-12-12 15:59:26 -0800147/**
148 * Determines where in flash the most recent boot status is stored. The boot
149 * status is necessary for completing a swap that was interrupted by a boot
150 * loader reset.
151 *
152 * @return A BOOT_STATUS_SOURCE_[...] code indicating where * status should be read from.
153 */
154static int
155boot_status_source(void)
156{
157 const struct boot_status_table *table;
158 struct boot_swap_state state_scratch;
159 struct boot_swap_state state_slot0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800160 int rc;
Fabio Utzigcd5774b2017-11-29 10:18:26 -0200161 size_t i;
Marti Bolivarfd20c762017-02-07 16:52:50 -0500162 uint8_t source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163
Fabio Utzig2473ac02017-05-02 12:45:02 -0300164 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165 assert(rc == 0);
166
Fabio Utzig2473ac02017-05-02 12:45:02 -0300167 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168 assert(rc == 0);
169
Marti Bolivarfd20c762017-02-07 16:52:50 -0500170 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
Marti Bolivarfd20c762017-02-07 16:52:50 -0500171 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
172
Christopher Collins92ea77f2016-12-12 15:59:26 -0800173 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300174 table = &boot_status_tables[i];
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175
Fabio Utzig39000012018-07-30 12:40:20 -0300176 if ((table->bst_magic_slot0 == BOOT_MAGIC_ANY ||
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 table->bst_magic_slot0 == state_slot0.magic) &&
Fabio Utzig39000012018-07-30 12:40:20 -0300178 (table->bst_magic_scratch == BOOT_MAGIC_ANY ||
Christopher Collins92ea77f2016-12-12 15:59:26 -0800179 table->bst_magic_scratch == state_scratch.magic) &&
Fabio Utzig39000012018-07-30 12:40:20 -0300180 (table->bst_copy_done_slot0 == BOOT_FLAG_ANY ||
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
Marti Bolivarfd20c762017-02-07 16:52:50 -0500182 source = table->bst_status_source;
183 BOOT_LOG_INF("Boot source: %s",
184 source == BOOT_STATUS_SOURCE_NONE ? "none" :
185 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
186 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
187 "BUG; can't happen");
188 return source;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800189 }
190 }
191
Marti Bolivarfd20c762017-02-07 16:52:50 -0500192 BOOT_LOG_INF("Boot source: none");
Christopher Collins92ea77f2016-12-12 15:59:26 -0800193 return BOOT_STATUS_SOURCE_NONE;
194}
195
196/**
197 * Calculates the type of swap that just completed.
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300198 *
199 * This is used when a swap is interrupted by an external event. After
200 * finishing the swap operation determines what the initial request was.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800201 */
202static int
203boot_previous_swap_type(void)
204{
205 int post_swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800206
207 post_swap_type = boot_swap_type();
208
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300209 switch (post_swap_type) {
210 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
211 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
212 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800213 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300214
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300215 return BOOT_SWAP_TYPE_FAIL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800216}
217
David Brownf5b33d82017-09-01 10:58:27 -0600218/*
219 * Compute the total size of the given image. Includes the size of
220 * the TLVs.
221 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300222#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600223static int
224boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
225{
226 const struct flash_area *fap;
227 struct image_tlv_info info;
228 int area_id;
229 int rc;
230
231 area_id = flash_area_id_from_image_slot(slot);
232 rc = flash_area_open(area_id, &fap);
233 if (rc != 0) {
234 rc = BOOT_EFLASH;
235 goto done;
236 }
237
238 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
239 &info, sizeof(info));
240 if (rc != 0) {
241 rc = BOOT_EFLASH;
242 goto done;
243 }
244 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
245 rc = BOOT_EBADIMAGE;
246 goto done;
247 }
248 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
249 rc = 0;
250
251done:
252 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300253 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600254}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300255#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600256
Fabio Utzigc08ed212017-06-20 19:28:36 -0300257static int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800258boot_read_image_header(int slot, struct image_header *out_hdr)
259{
260 const struct flash_area *fap;
261 int area_id;
262 int rc;
263
264 area_id = flash_area_id_from_image_slot(slot);
265 rc = flash_area_open(area_id, &fap);
266 if (rc != 0) {
267 rc = BOOT_EFLASH;
268 goto done;
269 }
270
271 rc = flash_area_read(fap, 0, out_hdr, sizeof *out_hdr);
272 if (rc != 0) {
273 rc = BOOT_EFLASH;
274 goto done;
275 }
276
277 rc = 0;
278
279done:
280 flash_area_close(fap);
281 return rc;
282}
283
284static int
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200285boot_read_image_headers(bool require_all)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800286{
287 int rc;
288 int i;
289
290 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Marti Bolivarf804f622017-06-12 15:41:48 -0400291 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200293 /* If `require_all` is set, fail on any single fail, otherwise
294 * if at least the first slot's header was read successfully,
295 * then the boot loader can attempt a boot.
296 *
297 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800298 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200299 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800300 return 0;
301 } else {
302 return rc;
303 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304 }
305 }
306
307 return 0;
308}
309
310static uint8_t
311boot_write_sz(void)
312{
313 uint8_t elem_sz;
314 uint8_t align;
315
316 /* Figure out what size to write update status update as. The size depends
317 * on what the minimum write size is for scratch area, active image slot.
318 * We need to use the bigger of those 2 values.
319 */
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200320 elem_sz = flash_area_align(boot_data.imgs[0].area);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200321 align = flash_area_align(boot_data.scratch.area);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800322 if (align > elem_sz) {
323 elem_sz = align;
324 }
325
326 return elem_sz;
327}
328
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200329/*
330 * Slots are compatible when all sectors that store upto to size of the image
331 * round up to sector size, in both slot's are able to fit in the scratch
332 * area, and have sizes that are a multiple of each other (powers of two
333 * presumably!).
334 */
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800335static int
336boot_slots_compatible(void)
337{
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200338 size_t num_sectors_0;
339 size_t num_sectors_1;
340 size_t sz0, sz1;
341 size_t slot0_sz, slot1_sz;
342 size_t scratch_sz;
343 size_t i, j;
344 int8_t smaller;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800345
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200346 num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
347 num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
Fabio Utziga1fae672018-03-30 10:52:38 -0300348 if (num_sectors_0 > BOOT_MAX_IMG_SECTORS || num_sectors_1 > BOOT_MAX_IMG_SECTORS) {
349 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800350 return 0;
351 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300352
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200353 scratch_sz = boot_scratch_area_size(&boot_data);
354
355 /*
356 * The following loop scans all sectors in a linear fashion, assuring that
357 * for each possible sector in each slot, it is able to fit in the other
358 * slot's sector or sectors. Slot's should be compatible as long as any
359 * number of a slot's sectors are able to fit into another, which only
360 * excludes cases where sector sizes are not a multiple of each other.
361 */
362 i = sz0 = slot0_sz = 0;
363 j = sz1 = slot1_sz = 0;
364 smaller = 0;
365 while (i < num_sectors_0 || j < num_sectors_1) {
366 if (sz0 == sz1) {
367 sz0 += boot_img_sector_size(&boot_data, 0, i);
368 sz1 += boot_img_sector_size(&boot_data, 1, j);
369 i++;
370 j++;
371 } else if (sz0 < sz1) {
372 sz0 += boot_img_sector_size(&boot_data, 0, i);
373 /* guarantee that multiple sectors of slot1 fit into slot0 */
374 if (smaller == 2) {
375 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
376 return 0;
377 }
378 smaller = 1;
379 i++;
380 } else {
381 sz1 += boot_img_sector_size(&boot_data, 1, j);
382 /* guarantee that multiple sectors of slot0 fit into slot1 */
383 if (smaller == 1) {
384 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible sectors");
385 return 0;
386 }
387 smaller = 2;
388 j++;
389 }
390 if (sz0 == sz1) {
391 slot0_sz += sz0;
392 slot1_sz += sz1;
393 /* scratch has to fit each swap operation to the size of the larger
394 * sector among slot0 and slot1
395 */
396 if (sz0 > scratch_sz || sz1 > scratch_sz) {
397 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside scratch");
398 return 0;
399 }
400 smaller = sz0 = sz1 = 0;
401 }
Fabio Utziga1fae672018-03-30 10:52:38 -0300402 }
403
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200404 if (i != num_sectors_0 || j != num_sectors_1 || slot0_sz != slot1_sz) {
405 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
406 return 0;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800407 }
408
409 return 1;
410}
411
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412/**
413 * Determines the sector layout of both image slots and the scratch area.
414 * This information is necessary for calculating the number of bytes to erase
415 * and copy during an image swap. The information collected during this
416 * function is used to populate the boot_data global.
417 */
418static int
419boot_read_sectors(void)
420{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422
Marti Bolivarcca28a92017-06-12 16:52:22 -0400423 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424 if (rc != 0) {
425 return BOOT_EFLASH;
426 }
427
Marti Bolivarcca28a92017-06-12 16:52:22 -0400428 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429 if (rc != 0) {
430 return BOOT_EFLASH;
431 }
432
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200433 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
434 if (rc != 0) {
435 return BOOT_EFLASH;
436 }
437
Marti Bolivare10a7392017-06-14 16:20:07 -0400438 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439
440 return 0;
441}
442
443static uint32_t
444boot_status_internal_off(int idx, int state, int elem_sz)
445{
446 int idx_sz;
447
448 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
449
Fabio Utzig39000012018-07-30 12:40:20 -0300450 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
451 (state - BOOT_STATUS_STATE_0) * elem_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452}
453
454/**
455 * Reads the status of a partially-completed swap, if any. This is necessary
456 * to recover in case the boot lodaer was reset in the middle of a swap
457 * operation.
458 */
459static int
460boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
461{
462 uint32_t off;
463 uint8_t status;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300464 int max_entries;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465 int found;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200466 int found_idx;
467 int invalid;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 int rc;
469 int i;
470
471 off = boot_status_off(fap);
Fabio Utzig4cee4f72017-05-22 10:59:57 -0400472 max_entries = boot_status_entries(fap);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300473
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 found = 0;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200475 found_idx = 0;
476 invalid = 0;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300477 for (i = 0; i < max_entries; i++) {
Fabio Utzig178be542018-09-19 08:12:56 -0300478 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
479 &status, 1);
480 if (rc < 0) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 return BOOT_EFLASH;
482 }
483
Fabio Utzig178be542018-09-19 08:12:56 -0300484 if (rc == 1) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200485 if (found && !found_idx) {
486 found_idx = i;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800487 }
488 } else if (!found) {
489 found = 1;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200490 } else if (found_idx) {
491 invalid = 1;
492 break;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800493 }
494 }
495
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200496 if (invalid) {
497 /* This means there was an error writing status on the last
498 * swap. Tell user and move on to validation!
499 */
500 BOOT_LOG_ERR("Detected inconsistent status!");
501
502#if !defined(MCUBOOT_VALIDATE_SLOT0)
503 /* With validation of slot0 disabled, there is no way to be sure the
504 * swapped slot0 is OK, so abort!
505 */
506 assert(0);
507#endif
508 }
509
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510 if (found) {
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200511 if (!found_idx) {
512 found_idx = i;
513 }
514 found_idx--;
Fabio Utzig39000012018-07-30 12:40:20 -0300515 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
516 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800517 }
518
519 return 0;
520}
521
522/**
523 * Reads the boot status from the flash. The boot status contains
524 * the current state of an interrupted image copy operation. If the boot
525 * status is not present, or it indicates that previous copy finished,
526 * there is no operation in progress.
527 */
528static int
529boot_read_status(struct boot_status *bs)
530{
531 const struct flash_area *fap;
532 int status_loc;
533 int area_id;
534 int rc;
535
536 memset(bs, 0, sizeof *bs);
Fabio Utzig39000012018-07-30 12:40:20 -0300537 bs->idx = BOOT_STATUS_IDX_0;
538 bs->state = BOOT_STATUS_STATE_0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700540#ifdef MCUBOOT_OVERWRITE_ONLY
541 /* Overwrite-only doesn't make use of the swap status area. */
542 return 0;
543#endif
544
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545 status_loc = boot_status_source();
546 switch (status_loc) {
547 case BOOT_STATUS_SOURCE_NONE:
548 return 0;
549
550 case BOOT_STATUS_SOURCE_SCRATCH:
551 area_id = FLASH_AREA_IMAGE_SCRATCH;
552 break;
553
554 case BOOT_STATUS_SOURCE_SLOT0:
555 area_id = FLASH_AREA_IMAGE_0;
556 break;
557
558 default:
559 assert(0);
560 return BOOT_EBADARGS;
561 }
562
563 rc = flash_area_open(area_id, &fap);
564 if (rc != 0) {
565 return BOOT_EFLASH;
566 }
567
Fabio Utzig46490722017-09-04 15:34:32 -0300568 rc = boot_read_status_bytes(fap, bs);
569
570 flash_area_close(fap);
Fabio Utzig03dc9a02018-06-11 12:24:07 -0700571
Fabio Utzig46490722017-09-04 15:34:32 -0300572 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573}
574
575/**
576 * Writes the supplied boot status to the flash file system. The boot status
577 * contains the current state of an in-progress image copy operation.
578 *
579 * @param bs The boot status to write.
580 *
581 * @return 0 on success; nonzero on failure.
582 */
583int
584boot_write_status(struct boot_status *bs)
585{
586 const struct flash_area *fap;
587 uint32_t off;
588 int area_id;
589 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300590 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700591 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300592 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800593
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300594 /* NOTE: The first sector copied (that is the last sector on slot) contains
595 * the trailer. Since in the last step SLOT 0 is erased, the first
596 * two status writes go to the scratch which will be copied to SLOT 0!
597 */
598
Fabio Utzig2473ac02017-05-02 12:45:02 -0300599 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600 /* Write to scratch. */
601 area_id = FLASH_AREA_IMAGE_SCRATCH;
602 } else {
603 /* Write to slot 0. */
604 area_id = FLASH_AREA_IMAGE_0;
605 }
606
607 rc = flash_area_open(area_id, &fap);
608 if (rc != 0) {
609 rc = BOOT_EFLASH;
610 goto done;
611 }
612
613 off = boot_status_off(fap) +
Marti Bolivare10a7392017-06-14 16:20:07 -0400614 boot_status_internal_off(bs->idx, bs->state,
615 BOOT_WRITE_SZ(&boot_data));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200616 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300617 erased_val = flash_area_erased_val(fap);
618 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700619 buf[0] = bs->state;
620
621 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 if (rc != 0) {
623 rc = BOOT_EFLASH;
624 goto done;
625 }
626
627 rc = 0;
628
629done:
630 flash_area_close(fap);
631 return rc;
632}
633
634/*
635 * Validate image hash/signature in a slot.
636 */
637static int
Fabio Utzigba829042018-09-18 08:29:34 -0300638boot_image_check(struct image_header *hdr, const struct flash_area *fap,
639 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640{
David Browndb1d9d32017-01-06 11:07:54 -0700641 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigba829042018-09-18 08:29:34 -0300642 int rc;
643
644#ifndef MCUBOOT_ENC_IMAGES
645 (void)bs;
646 (void)rc;
647#else
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200648 if (fap->fa_id == FLASH_AREA_IMAGE_1 && IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300649 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
650 if (rc < 0) {
651 return BOOT_EBADIMAGE;
652 }
653 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
654 return BOOT_EBADIMAGE;
655 }
656 }
657#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658
Christopher Collins92ea77f2016-12-12 15:59:26 -0800659 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
660 NULL, 0, NULL)) {
661 return BOOT_EBADIMAGE;
662 }
663 return 0;
664}
665
666static int
667split_image_check(struct image_header *app_hdr,
668 const struct flash_area *app_fap,
669 struct image_header *loader_hdr,
670 const struct flash_area *loader_fap)
671{
672 static void *tmpbuf;
673 uint8_t loader_hash[32];
674
675 if (!tmpbuf) {
676 tmpbuf = malloc(BOOT_TMPBUF_SZ);
677 if (!tmpbuf) {
678 return BOOT_ENOMEM;
679 }
680 }
681
682 if (bootutil_img_validate(loader_hdr, loader_fap, tmpbuf, BOOT_TMPBUF_SZ,
683 NULL, 0, loader_hash)) {
684 return BOOT_EBADIMAGE;
685 }
686
687 if (bootutil_img_validate(app_hdr, app_fap, tmpbuf, BOOT_TMPBUF_SZ,
688 loader_hash, 32, NULL)) {
689 return BOOT_EBADIMAGE;
690 }
691
692 return 0;
693}
694
Fabio Utzig338a19f2018-12-03 08:37:08 -0200695/*
696 * Check that a memory area consists of a given value.
697 */
698static inline bool
699boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300700{
701 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200702 uint8_t *p = (uint8_t *)data;
703 for (i = 0; i < len; i++) {
704 if (val != p[i]) {
705 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300706 }
707 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200708 return true;
709}
710
711static int
712boot_check_header_erased(int slot)
713{
714 const struct flash_area *fap;
715 struct image_header *hdr;
716 uint8_t erased_val;
717 int rc;
718
719 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
720 if (rc != 0) {
721 return -1;
722 }
723
724 erased_val = flash_area_erased_val(fap);
725 flash_area_close(fap);
726
727 hdr = boot_img_hdr(&boot_data, slot);
728 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
729 return -1;
730 }
731
732 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300733}
734
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735static int
Fabio Utzigba829042018-09-18 08:29:34 -0300736boot_validate_slot(int slot, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800737{
738 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400739 struct image_header *hdr;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800740 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300741
David Brownd930ec62016-12-14 07:59:48 -0700742 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800743 if (rc != 0) {
744 return BOOT_EFLASH;
745 }
746
Fabio Utzig39000012018-07-30 12:40:20 -0300747 hdr = boot_img_hdr(&boot_data, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200748 if (boot_check_header_erased(slot) == 0 || (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig39000012018-07-30 12:40:20 -0300749 /* No bootable image in slot; continue booting from slot 0. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200750 rc = -1;
751 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300752 }
753
Fabio Utzigba829042018-09-18 08:29:34 -0300754 if ((hdr->ih_magic != IMAGE_MAGIC || boot_image_check(hdr, fap, bs) != 0)) {
David Brownb38e0442017-02-24 13:57:12 -0700755 if (slot != 0) {
756 flash_area_erase(fap, 0, fap->fa_size);
757 /* Image in slot 1 is invalid. Erase the image and
758 * continue booting from slot 0.
759 */
760 }
Fabio Utzigb6297af2017-10-05 18:26:36 -0300761 BOOT_LOG_ERR("Image in slot %d is not valid!", slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200762 rc = -1;
763 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800764 }
765
Christopher Collins92ea77f2016-12-12 15:59:26 -0800766 /* Image in slot 1 is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200767 rc = 0;
768
769out:
770 flash_area_close(fap);
771 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800772}
773
774/**
775 * Determines which swap operation to perform, if any. If it is determined
776 * that a swap operation is required, the image in the second slot is checked
777 * for validity. If the image in the second slot is invalid, it is erased, and
778 * a swap type of "none" is indicated.
779 *
780 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
781 */
782static int
Fabio Utzigba829042018-09-18 08:29:34 -0300783boot_validated_swap_type(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800784{
785 int swap_type;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800786
787 swap_type = boot_swap_type();
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300788 switch (swap_type) {
789 case BOOT_SWAP_TYPE_TEST:
790 case BOOT_SWAP_TYPE_PERM:
791 case BOOT_SWAP_TYPE_REVERT:
792 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
Fabio Utzigba829042018-09-18 08:29:34 -0300793 if (boot_validate_slot(1, bs) != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300794 swap_type = BOOT_SWAP_TYPE_FAIL;
795 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800796 }
797
798 return swap_type;
799}
800
801/**
802 * Calculates the number of sectors the scratch area can contain. A "last"
803 * source sector is specified because images are copied backwards in flash
804 * (final index to index number 0).
805 *
806 * @param last_sector_idx The index of the last source sector
807 * (inclusive).
808 * @param out_first_sector_idx The index of the first source sector
809 * (inclusive) gets written here.
810 *
811 * @return The number of bytes comprised by the
812 * [first-sector, last-sector] range.
813 */
Fabio Utzig3488eef2017-06-12 10:25:43 -0300814#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -0800815static uint32_t
816boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
817{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400818 size_t scratch_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800819 uint32_t new_sz;
820 uint32_t sz;
821 int i;
822
823 sz = 0;
824
Marti Bolivard3269fd2017-06-12 16:31:12 -0400825 scratch_sz = boot_scratch_area_size(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826 for (i = last_sector_idx; i >= 0; i--) {
Marti Bolivard3269fd2017-06-12 16:31:12 -0400827 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200828 /*
829 * slot1 is not being checked here, because `boot_slots_compatible`
830 * already provides assurance that the copy size will be compatible
831 * with slot0 and scratch.
832 */
Marti Bolivard3269fd2017-06-12 16:31:12 -0400833 if (new_sz > scratch_sz) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800834 break;
835 }
836 sz = new_sz;
837 }
838
839 /* i currently refers to a sector that doesn't fit or it is -1 because all
840 * sectors have been processed. In both cases, exclude sector i.
841 */
842 *out_first_sector_idx = i + 1;
843 return sz;
844}
Fabio Utzig3488eef2017-06-12 10:25:43 -0300845#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800846
847/**
848 * Erases a region of flash.
849 *
Fabio Utzigba829042018-09-18 08:29:34 -0300850 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800851 * @param off The offset within the flash area to start the
852 * erase.
853 * @param sz The number of bytes to erase.
854 *
855 * @return 0 on success; nonzero on failure.
856 */
Fabio Utzigba829042018-09-18 08:29:34 -0300857static inline int
858boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859{
Fabio Utzigba829042018-09-18 08:29:34 -0300860 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861}
862
863/**
864 * Copies the contents of one flash region to another. You must erase the
865 * destination region prior to calling this function.
866 *
867 * @param flash_area_id_src The ID of the source flash area.
868 * @param flash_area_id_dst The ID of the destination flash area.
869 * @param off_src The offset within the source flash area to
870 * copy from.
871 * @param off_dst The offset within the destination flash area to
872 * copy to.
873 * @param sz The number of bytes to copy.
874 *
875 * @return 0 on success; nonzero on failure.
876 */
877static int
Fabio Utzigba829042018-09-18 08:29:34 -0300878boot_copy_sector(const struct flash_area *fap_src,
879 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800880 uint32_t off_src, uint32_t off_dst, uint32_t sz)
881{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800882 uint32_t bytes_copied;
883 int chunk_sz;
884 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300885#ifdef MCUBOOT_ENC_IMAGES
886 uint32_t off;
887 size_t blk_off;
888 struct image_header *hdr;
889 uint16_t idx;
890 uint32_t blk_sz;
891#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800892
893 static uint8_t buf[1024];
894
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 bytes_copied = 0;
896 while (bytes_copied < sz) {
897 if (sz - bytes_copied > sizeof buf) {
898 chunk_sz = sizeof buf;
899 } else {
900 chunk_sz = sz - bytes_copied;
901 }
902
903 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
904 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300905 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800906 }
907
Fabio Utzigba829042018-09-18 08:29:34 -0300908#ifdef MCUBOOT_ENC_IMAGES
909 if (fap_src->fa_id == FLASH_AREA_IMAGE_1 ||
910 fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
911 /* assume slot1 as src, needs decryption */
912 hdr = boot_img_hdr(&boot_data, 1);
913 off = off_src;
914 if (fap_dst->fa_id == FLASH_AREA_IMAGE_1) {
915 /* might need encryption (metadata from slot0) */
916 hdr = boot_img_hdr(&boot_data, 0);
917 off = off_dst;
918 }
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200919 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300920 blk_sz = chunk_sz;
921 idx = 0;
922 if (off + bytes_copied < hdr->ih_hdr_size) {
923 /* do not decrypt header */
924 blk_off = 0;
925 blk_sz = chunk_sz - hdr->ih_hdr_size;
926 idx = hdr->ih_hdr_size;
927 } else {
928 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
929 }
930 if (off + bytes_copied + chunk_sz > hdr->ih_hdr_size + hdr->ih_img_size) {
931 /* do not decrypt TLVs */
932 if (off + bytes_copied >= hdr->ih_hdr_size + hdr->ih_img_size) {
933 blk_sz = 0;
934 } else {
935 blk_sz = (hdr->ih_hdr_size + hdr->ih_img_size) - (off + bytes_copied);
936 }
937 }
938 boot_encrypt(fap_src, (off + bytes_copied + idx) - hdr->ih_hdr_size,
939 blk_sz, blk_off, &buf[idx]);
940 }
941 }
942#endif
943
Christopher Collins92ea77f2016-12-12 15:59:26 -0800944 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
945 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300946 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800947 }
948
949 bytes_copied += chunk_sz;
950 }
951
Fabio Utzigba829042018-09-18 08:29:34 -0300952 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800953}
954
David Brown6b1b3b92017-09-19 08:59:10 -0600955#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300956static inline int
Fabio Utzigba829042018-09-18 08:29:34 -0300957boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300958{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300959 struct boot_swap_state swap_state;
960 int rc;
961
Fabio Utzig2473ac02017-05-02 12:45:02 -0300962 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
963 assert(rc == 0);
964
Fabio Utzigde8a38a2017-05-23 11:15:01 -0400965 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300966 rc = boot_write_image_ok(fap);
967 assert(rc == 0);
968 }
969
Fabio Utzig46490722017-09-04 15:34:32 -0300970 rc = boot_write_swap_size(fap, bs->swap_size);
971 assert(rc == 0);
972
Fabio Utzigba829042018-09-18 08:29:34 -0300973#ifdef MCUBOOT_ENC_IMAGES
974 rc = boot_write_enc_key(fap, 0, bs->enckey[0]);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300975 assert(rc == 0);
976
Fabio Utzigba829042018-09-18 08:29:34 -0300977 rc = boot_write_enc_key(fap, 1, bs->enckey[1]);
978 assert(rc == 0);
979#endif
980
981 rc = boot_write_magic(fap);
982 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300983
984 return 0;
985}
David Brown6b1b3b92017-09-19 08:59:10 -0600986#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300987
Fabio Utzig358c9352017-07-25 22:10:45 -0300988#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig2473ac02017-05-02 12:45:02 -0300989static int
Fabio Utziged0ca432019-01-23 14:50:11 -0200990boot_erase_trailer_sectors(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300991{
992 uint8_t slot;
Fabio Utziged0ca432019-01-23 14:50:11 -0200993 uint32_t sector;
994 uint32_t trailer_sz;
995 uint32_t total_sz;
996 uint32_t off;
997 uint32_t sz;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300998 int rc;
999
Fabio Utzigba829042018-09-18 08:29:34 -03001000 switch (fap->fa_id) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001001 case FLASH_AREA_IMAGE_0:
1002 slot = 0;
1003 break;
1004 case FLASH_AREA_IMAGE_1:
1005 slot = 1;
1006 break;
1007 default:
1008 return BOOT_EFLASH;
1009 }
1010
Fabio Utziged0ca432019-01-23 14:50:11 -02001011 /* delete starting from last sector and moving to beginning */
1012 sector = boot_img_num_sectors(&boot_data, slot) - 1;
1013 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
1014 total_sz = 0;
1015 do {
1016 sz = boot_img_sector_size(&boot_data, slot, sector);
1017 off = boot_img_sector_off(&boot_data, slot, sector);
1018 rc = boot_erase_sector(fap, off, sz);
1019 assert(rc == 0);
1020
1021 sector--;
1022 total_sz += sz;
1023 } while (total_sz < trailer_sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001024
1025 return rc;
1026}
Fabio Utzig358c9352017-07-25 22:10:45 -03001027#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001028
Christopher Collins92ea77f2016-12-12 15:59:26 -08001029/**
1030 * Swaps the contents of two flash regions within the two image slots.
1031 *
1032 * @param idx The index of the first sector in the range of
1033 * sectors being swapped.
1034 * @param sz The number of bytes to swap.
1035 * @param bs The current boot status. This struct gets
1036 * updated according to the outcome.
1037 *
1038 * @return 0 on success; nonzero on failure.
1039 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001040#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001041static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001042boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1043{
Fabio Utzigba829042018-09-18 08:29:34 -03001044 const struct flash_area *fap_slot0;
1045 const struct flash_area *fap_slot1;
1046 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001047 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001048 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001050 uint32_t scratch_trailer_off;
1051 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001052 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001053 int rc;
1054
1055 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -04001056 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001057
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001058 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -04001059 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001060
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001061 /* sz in this function is always sized on a multiple of the sector size.
1062 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001063 * is to determine if we're swapping the last sector. The last sector
1064 * needs special handling because it's where the trailer lives. If we're
1065 * copying it, we need to use scratch to write the trailer temporarily.
1066 *
1067 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1068 * controls if special handling is needed (swapping last sector).
1069 */
Marti Bolivard3269fd2017-06-12 16:31:12 -04001070 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -04001071 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001072 copy_sz -= trailer_sz;
1073 }
1074
Fabio Utzig39000012018-07-30 12:40:20 -03001075 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001076
Fabio Utzigba829042018-09-18 08:29:34 -03001077 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1078 assert (rc == 0);
1079
1080 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1081 assert (rc == 0);
1082
1083 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1084 assert (rc == 0);
1085
Fabio Utzig39000012018-07-30 12:40:20 -03001086 if (bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001087 rc = boot_erase_sector(fap_scratch, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001088 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001089
Fabio Utzigba829042018-09-18 08:29:34 -03001090 rc = boot_copy_sector(fap_slot1, fap_scratch, img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001091 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001092
Fabio Utzig39000012018-07-30 12:40:20 -03001093 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001094 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001095 boot_status_init(fap_scratch, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001096 } else {
1097 /* Prepare the status area... here it is known that the
1098 * last sector is not being used by the image data so it's
1099 * safe to erase.
1100 */
Fabio Utziged0ca432019-01-23 14:50:11 -02001101 rc = boot_erase_trailer_sectors(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001102 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001103
Fabio Utzigba829042018-09-18 08:29:34 -03001104 boot_status_init(fap_slot0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001105 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001106 }
1107
Fabio Utzig39000012018-07-30 12:40:20 -03001108 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001109 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001110 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001111 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001112
Fabio Utzig39000012018-07-30 12:40:20 -03001113 if (bs->state == BOOT_STATUS_STATE_1) {
Fabio Utzigba829042018-09-18 08:29:34 -03001114 rc = boot_erase_sector(fap_slot1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001115 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001116
Fabio Utzigba829042018-09-18 08:29:34 -03001117 rc = boot_copy_sector(fap_slot0, fap_slot1, img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001118 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001119
Fabio Utzig39000012018-07-30 12:40:20 -03001120 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001121 /* If not all sectors of the slot are being swapped,
1122 * guarantee here that only slot0 will have the state.
1123 */
Fabio Utziged0ca432019-01-23 14:50:11 -02001124 rc = boot_erase_trailer_sectors(fap_slot1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001125 assert(rc == 0);
1126 }
1127
Fabio Utzig39000012018-07-30 12:40:20 -03001128 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001129 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001130 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001131 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001132
Fabio Utzig39000012018-07-30 12:40:20 -03001133 if (bs->state == BOOT_STATUS_STATE_2) {
Fabio Utzigba829042018-09-18 08:29:34 -03001134 rc = boot_erase_sector(fap_slot0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001135 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001136
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001137 /* NOTE: also copy trailer from scratch (has status info) */
Fabio Utzigba829042018-09-18 08:29:34 -03001138 rc = boot_copy_sector(fap_scratch, fap_slot0, 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001139 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001140
Fabio Utzig94d998c2017-05-22 11:02:41 -04001141 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001142 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001143
1144 /* copy current status that is being maintained in scratch */
Fabio Utzigba829042018-09-18 08:29:34 -03001145 rc = boot_copy_sector(fap_scratch, fap_slot0, scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001146 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001147 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001148 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001149
Fabio Utzig2473ac02017-05-02 12:45:02 -03001150 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1151 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001152 assert(rc == 0);
1153
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001154 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzigba829042018-09-18 08:29:34 -03001155 rc = boot_write_image_ok(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001156 assert(rc == 0);
1157 }
1158
Fabio Utzigba829042018-09-18 08:29:34 -03001159 rc = boot_write_swap_size(fap_slot0, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001160 assert(rc == 0);
1161
Fabio Utzigba829042018-09-18 08:29:34 -03001162#ifdef MCUBOOT_ENC_IMAGES
1163 rc = boot_write_enc_key(fap_slot0, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001164 assert(rc == 0);
1165
Fabio Utzigba829042018-09-18 08:29:34 -03001166 rc = boot_write_enc_key(fap_slot0, 1, bs->enckey[1]);
1167 assert(rc == 0);
1168#endif
1169
1170 rc = boot_write_magic(fap_slot0);
1171 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001172 }
1173
Christopher Collins92ea77f2016-12-12 15:59:26 -08001174 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001175 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001176 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001177 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001178 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001179 }
Fabio Utzigba829042018-09-18 08:29:34 -03001180
1181 flash_area_close(fap_slot0);
1182 flash_area_close(fap_slot1);
1183 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001184}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001185#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001186
1187/**
Fabio Utzigba829042018-09-18 08:29:34 -03001188 * Overwrite slot 0 with the image contained in slot 1. If a prior copy
1189 * operation was interrupted by a system reset, this function redos the
1190 * copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001191 *
1192 * @param bs The current boot status. This function reads
1193 * this struct to determine if it is resuming
1194 * an interrupted swap operation. This
1195 * function writes the updated status to this
1196 * function on return.
1197 *
1198 * @return 0 on success; nonzero on failure.
1199 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001200#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001201static int
1202boot_copy_image(struct boot_status *bs)
1203{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001204 size_t sect_count;
1205 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001206 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001207 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001208 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001209 size_t last_sector;
Fabio Utzigba829042018-09-18 08:29:34 -03001210 const struct flash_area *fap_slot0;
1211 const struct flash_area *fap_slot1;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001212
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001213 (void)bs;
1214
Fabio Utzig13d9e352017-10-05 20:32:31 -03001215#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1216 uint32_t src_size = 0;
1217 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1218 assert(rc == 0);
1219#endif
David Brown17609d82017-05-05 09:41:34 -06001220
1221 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1222 BOOT_LOG_INF("Erasing slot0");
1223
Fabio Utzigba829042018-09-18 08:29:34 -03001224 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1225 assert (rc == 0);
1226
1227 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1228 assert (rc == 0);
1229
Marti Bolivard3269fd2017-06-12 16:31:12 -04001230 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001231 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001232 this_size = boot_img_sector_size(&boot_data, 0, sect);
Fabio Utzigba829042018-09-18 08:29:34 -03001233 rc = boot_erase_sector(fap_slot0, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001234 assert(rc == 0);
1235
1236 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001237
1238#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1239 if (size >= src_size) {
1240 break;
1241 }
1242#endif
David Brown17609d82017-05-05 09:41:34 -06001243 }
1244
Fabio Utzigba829042018-09-18 08:29:34 -03001245#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001246 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, 1))) {
Fabio Utzige641ea52018-12-03 10:37:53 -02001247 rc = boot_enc_load(boot_img_hdr(&boot_data, 1), fap_slot1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001248 if (rc < 0) {
1249 return BOOT_EBADIMAGE;
1250 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001251 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001252 return BOOT_EBADIMAGE;
1253 }
1254 }
1255#endif
1256
Fabio Utzig338a19f2018-12-03 08:37:08 -02001257 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%zx bytes", size);
Fabio Utzigba829042018-09-18 08:29:34 -03001258 rc = boot_copy_sector(fap_slot1, fap_slot0, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001259
Fabio Utzig13d9e352017-10-05 20:32:31 -03001260 /*
1261 * Erases header and trailer. The trailer is erased because when a new
1262 * image is written without a trailer as is the case when using newt, the
1263 * trailer that was left might trigger a new upgrade.
1264 */
Fabio Utzigba829042018-09-18 08:29:34 -03001265 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001266 boot_img_sector_off(&boot_data, 1, 0),
1267 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001268 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001269 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -03001270 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001271 boot_img_sector_off(&boot_data, 1, last_sector),
1272 boot_img_sector_size(&boot_data, 1, last_sector));
1273 assert(rc == 0);
1274
Fabio Utzigba829042018-09-18 08:29:34 -03001275 flash_area_close(fap_slot0);
1276 flash_area_close(fap_slot1);
1277
Fabio Utzig13d9e352017-10-05 20:32:31 -03001278 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001279
1280 return 0;
1281}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001282#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001283
1284/**
1285 * Swaps the two images in flash. If a prior copy operation was interrupted
1286 * by a system reset, this function completes that operation.
1287 *
1288 * @param bs The current boot status. This function reads
1289 * this struct to determine if it is resuming
1290 * an interrupted swap operation. This
1291 * function writes the updated status to this
1292 * function on return.
1293 *
1294 * @return 0 on success; nonzero on failure.
1295 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001296#if !defined(MCUBOOT_OVERWRITE_ONLY)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001297static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001298boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001299{
1300 uint32_t sz;
1301 int first_sector_idx;
1302 int last_sector_idx;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001303 int last_idx_slot1;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001304 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001305 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001306#ifdef MCUBOOT_ENC_IMAGES
1307 const struct flash_area *fap;
1308 uint8_t slot;
1309 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001310#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001311 uint32_t size;
1312 uint32_t copy_size;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001313 uint32_t slot0_size;
1314 uint32_t slot1_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001315 int rc;
1316
1317 /* FIXME: just do this if asked by user? */
1318
1319 size = copy_size = 0;
1320
Fabio Utzig39000012018-07-30 12:40:20 -03001321 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001322 /*
1323 * No swap ever happened, so need to find the largest image which
1324 * will be used to determine the amount of sectors to swap.
1325 */
1326 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001327 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001328 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001329 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001330 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001331
Fabio Utzigba829042018-09-18 08:29:34 -03001332#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001333 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001334 fap = BOOT_IMG_AREA(&boot_data, 0);
1335 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1336 assert(rc >= 0);
1337
1338 if (rc == 0) {
1339 rc = boot_enc_set_key(0, bs->enckey[0]);
1340 assert(rc == 0);
1341 } else {
1342 rc = 0;
1343 }
1344 } else {
1345 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1346 }
1347#endif
1348
Fabio Utzig46490722017-09-04 15:34:32 -03001349 hdr = boot_img_hdr(&boot_data, 1);
1350 if (hdr->ih_magic == IMAGE_MAGIC) {
1351 rc = boot_read_image_size(1, hdr, &size);
1352 assert(rc == 0);
1353 }
1354
Fabio Utzigba829042018-09-18 08:29:34 -03001355#ifdef MCUBOOT_ENC_IMAGES
1356 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001357 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001358 fap = BOOT_IMG_AREA(&boot_data, 1);
1359 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1360 assert(rc >= 0);
1361
1362 if (rc == 0) {
1363 rc = boot_enc_set_key(1, bs->enckey[1]);
1364 assert(rc == 0);
1365 } else {
1366 rc = 0;
1367 }
1368 } else {
1369 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1370 }
1371#endif
1372
Fabio Utzig46490722017-09-04 15:34:32 -03001373 if (size > copy_size) {
1374 copy_size = size;
1375 }
1376
1377 bs->swap_size = copy_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001378
Fabio Utzig46490722017-09-04 15:34:32 -03001379 } else {
1380 /*
1381 * If a swap was under way, the swap_size should already be present
1382 * in the trailer...
1383 */
1384 rc = boot_read_swap_size(&bs->swap_size);
1385 assert(rc == 0);
1386
1387 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001388
1389#ifdef MCUBOOT_ENC_IMAGES
1390 for (slot = 0; slot <= 1; slot++) {
1391 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1392 assert(rc == 0);
1393
1394 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001395 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001396 break;
1397 }
1398 }
1399
1400 if (i != BOOT_ENC_KEY_SIZE) {
1401 boot_enc_set_key(slot, bs->enckey[slot]);
1402 }
1403 }
1404#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001405 }
1406
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001407 slot0_size = 0;
1408 slot1_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001409 last_sector_idx = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001410 last_idx_slot1 = 0;
1411
1412 /*
1413 * Knowing the size of the largest image between both slots, here we
1414 * find what is the last sector in slot0 that needs swapping. Since we
1415 * already know that both slots are compatible, slot1's last sector is
1416 * not really required after this check is finished.
1417 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001418 while (1) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001419 if (slot0_size < copy_size || slot0_size < slot1_size) {
1420 slot0_size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
1421 }
1422 if (slot1_size < copy_size || slot1_size < slot0_size) {
1423 slot1_size += boot_img_sector_size(&boot_data, 1, last_idx_slot1);
1424 }
1425 if (slot0_size >= copy_size &&
1426 slot1_size >= copy_size &&
1427 slot0_size == slot1_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001428 break;
1429 }
1430 last_sector_idx++;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001431 last_idx_slot1++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001432 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001433
1434 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001435 while (last_sector_idx >= 0) {
1436 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001437 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001438 boot_swap_sectors(first_sector_idx, sz, bs);
1439 }
1440
1441 last_sector_idx = first_sector_idx - 1;
1442 swap_idx++;
1443 }
1444
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001445#ifdef MCUBOOT_VALIDATE_SLOT0
1446 if (boot_status_fails > 0) {
1447 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1448 }
1449#endif
1450
Christopher Collins92ea77f2016-12-12 15:59:26 -08001451 return 0;
1452}
David Brown17609d82017-05-05 09:41:34 -06001453#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001454
1455/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001456 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001457 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001458#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001459static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001460boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001461{
1462 const struct flash_area *fap;
1463 int rc;
1464
1465 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1466 if (rc != 0) {
1467 return BOOT_EFLASH;
1468 }
1469
1470 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001471 flash_area_close(fap);
1472 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001473}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001474#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001475
1476/**
1477 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1478 * the status bytes from the image revert operation don't get processed on a
1479 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001480 *
1481 * NOTE: image_ok is tested before writing because if there's a valid permanent
1482 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1483 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001484 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001485#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001486static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001487boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001488{
1489 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001490 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001491 int rc;
1492
1493 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1494 if (rc != 0) {
1495 return BOOT_EFLASH;
1496 }
1497
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001498 rc = boot_read_swap_state(fap, &state);
1499 if (rc != 0) {
1500 rc = BOOT_EFLASH;
1501 goto out;
1502 }
1503
1504 if (state.image_ok == BOOT_FLAG_UNSET) {
1505 rc = boot_write_image_ok(fap);
1506 }
1507
1508out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001509 flash_area_close(fap);
1510 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001511}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001512#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001513
1514/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001515 * Performs an image swap if one is required.
1516 *
1517 * @param out_swap_type On success, the type of swap performed gets
1518 * written here.
1519 *
1520 * @return 0 on success; nonzero on failure.
1521 */
1522static int
1523boot_swap_if_needed(int *out_swap_type)
1524{
1525 struct boot_status bs;
1526 int swap_type;
1527 int rc;
1528
1529 /* Determine if we rebooted in the middle of an image swap
1530 * operation.
1531 */
1532 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001533 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001534 if (rc != 0) {
1535 return rc;
1536 }
1537
1538 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001539 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001540#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001541 /* Should never arrive here, overwrite-only mode has no swap state. */
1542 assert(0);
1543#else
1544 rc = boot_swap_image(&bs);
1545#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001546 assert(rc == 0);
1547
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001548 /* NOTE: here we have finished a swap resume. The initial request
1549 * was either a TEST or PERM swap, which now after the completed
1550 * swap will be determined to be respectively REVERT (was TEST)
1551 * or NONE (was PERM).
1552 */
1553
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001554 /* Extrapolate the type of the partial swap. We need this
1555 * information to know how to mark the swap complete in flash.
1556 */
1557 swap_type = boot_previous_swap_type();
1558 } else {
Fabio Utzigba829042018-09-18 08:29:34 -03001559 swap_type = boot_validated_swap_type(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001560 switch (swap_type) {
1561 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001562 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001563 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001564#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001565 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001566#else
1567 rc = boot_swap_image(&bs);
1568#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001569 assert(rc == 0);
1570 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001571#ifdef MCUBOOT_BOOTSTRAP
1572 case BOOT_SWAP_TYPE_NONE:
1573 /*
1574 * Header checks are done first because they are inexpensive.
1575 * Since overwrite-only copies starting from offset 0, if
1576 * interrupted, it might leave a valid header magic, so also
1577 * run validation on slot0 to be sure it's not OK.
1578 */
1579 if (boot_check_header_erased(0) == 0 ||
1580 boot_validate_slot(0, &bs) != 0) {
1581 if (boot_img_hdr(&boot_data, 1)->ih_magic == IMAGE_MAGIC &&
1582 boot_validate_slot(1, &bs) == 0) {
1583 rc = boot_copy_image(&bs);
1584 assert(rc == 0);
1585
1586 /* Returns fail here to trigger a re-read of the headers. */
1587 swap_type = BOOT_SWAP_TYPE_FAIL;
1588 }
1589 }
1590 break;
1591#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001592 }
1593 }
1594
1595 *out_swap_type = swap_type;
1596 return 0;
1597}
1598
1599/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001600 * Prepares the booting process. This function moves images around in flash as
1601 * appropriate, and tells you what address to boot from.
1602 *
1603 * @param rsp On success, indicates how booting should occur.
1604 *
1605 * @return 0 on success; nonzero on failure.
1606 */
1607int
1608boot_go(struct boot_rsp *rsp)
1609{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001610 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001611 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001612 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001613 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001614 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001615
1616 /* The array of slot sectors are defined here (as opposed to file scope) so
1617 * that they don't get allocated for non-boot-loader apps. This is
1618 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001619 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001620 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001621 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1622 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001623 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001624 boot_data.imgs[0].sectors = slot0_sectors;
1625 boot_data.imgs[1].sectors = slot1_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001626 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001627
Fabio Utzigba829042018-09-18 08:29:34 -03001628#ifdef MCUBOOT_ENC_IMAGES
1629 /* FIXME: remove this after RAM is cleared by sim */
1630 boot_enc_zeroize();
1631#endif
1632
Marti Bolivarc0b47912017-06-13 17:18:09 -04001633 /* Open boot_data image areas for the duration of this call. */
1634 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1635 fa_id = flash_area_id_from_image_slot(slot);
1636 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1637 assert(rc == 0);
1638 }
1639 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1640 &BOOT_SCRATCH_AREA(&boot_data));
1641 assert(rc == 0);
1642
Christopher Collins92ea77f2016-12-12 15:59:26 -08001643 /* Determine the sector layout of the image slots and scratch area. */
1644 rc = boot_read_sectors();
1645 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001646 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1647 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001648 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001649 }
1650
1651 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001652 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001653 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001654 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001655 }
1656
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001657 /* If the image slots aren't compatible, no swap is possible. Just boot
1658 * into slot 0.
1659 */
1660 if (boot_slots_compatible()) {
1661 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001662 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001663 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001664 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001665 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001666
1667 /*
1668 * The following states need image_ok be explicitly set after the
1669 * swap was finished to avoid a new revert.
1670 */
1671 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001672#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001673 rc = boot_set_image_ok();
1674 if (rc != 0) {
1675 swap_type = BOOT_SWAP_TYPE_PANIC;
1676 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001677#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001678 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001679 } else {
1680 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001681 }
1682
1683 switch (swap_type) {
1684 case BOOT_SWAP_TYPE_NONE:
1685 slot = 0;
1686 break;
1687
Fabio Utzig695d5642017-07-20 09:47:16 -03001688 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1689 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001690 case BOOT_SWAP_TYPE_REVERT:
1691 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001692 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001693#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001694 rc = boot_set_copy_done();
1695 if (rc != 0) {
1696 swap_type = BOOT_SWAP_TYPE_PANIC;
1697 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001698#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001699 break;
1700
1701 case BOOT_SWAP_TYPE_FAIL:
1702 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1703 * try to boot into it again on the next reboot. Do this by pretending
1704 * we just reverted back to slot 0.
1705 */
1706 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001707 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001708 break;
1709
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001710 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001711 swap_type = BOOT_SWAP_TYPE_PANIC;
1712 }
1713
1714 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1715 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001716 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001717
1718 /* Loop forever... */
1719 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001720 }
1721
David Brown52eee562017-07-05 11:25:09 -06001722 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001723 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001724 if (rc != 0) {
1725 goto out;
1726 }
1727 /* Since headers were reloaded, it can be assumed we just performed a
1728 * swap or overwrite. Now the header info that should be used to
1729 * provide the data for the bootstrap, which previously was at Slot 1,
1730 * was updated to Slot 0.
1731 */
1732 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001733 }
1734
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001735#ifdef MCUBOOT_VALIDATE_SLOT0
Fabio Utzigba829042018-09-18 08:29:34 -03001736 rc = boot_validate_slot(0, NULL);
David Brown554c52e2017-06-30 16:01:07 -06001737 if (rc != 0) {
1738 rc = BOOT_EBADIMAGE;
1739 goto out;
1740 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001741#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001742 /* Even if we're not re-validating slot 0, we could be booting
1743 * onto an empty flash chip. At least do a basic sanity check that
1744 * the magic number on the image is OK.
1745 */
1746 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
Fabio Utzig67716012018-02-26 10:36:15 -03001747 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001748 rc = BOOT_EBADIMAGE;
1749 goto out;
1750 }
David Brown554c52e2017-06-30 16:01:07 -06001751#endif
1752
Christopher Collins92ea77f2016-12-12 15:59:26 -08001753 /* Always boot from the primary slot. */
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +02001754 rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id;
Marti Bolivar88f48d92017-05-01 22:30:02 -04001755 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001756 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001757
Marti Bolivarc0b47912017-06-13 17:18:09 -04001758 out:
1759 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1760 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1761 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1762 }
1763 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001764}
1765
1766int
1767split_go(int loader_slot, int split_slot, void **entry)
1768{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001769 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001770 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001771 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001772 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001773 int rc;
1774
Christopher Collins92ea77f2016-12-12 15:59:26 -08001775 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1776 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001777 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001778 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001779 boot_data.imgs[loader_slot].sectors = sectors + 0;
1780 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1781
1782 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1783 rc = flash_area_open(loader_flash_id,
1784 &BOOT_IMG_AREA(&boot_data, split_slot));
1785 assert(rc == 0);
1786 split_flash_id = flash_area_id_from_image_slot(split_slot);
1787 rc = flash_area_open(split_flash_id,
1788 &BOOT_IMG_AREA(&boot_data, split_slot));
1789 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001790
1791 /* Determine the sector layout of the image slots and scratch area. */
1792 rc = boot_read_sectors();
1793 if (rc != 0) {
1794 rc = SPLIT_GO_ERR;
1795 goto done;
1796 }
1797
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001798 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001799 if (rc != 0) {
1800 goto done;
1801 }
1802
Christopher Collins92ea77f2016-12-12 15:59:26 -08001803 /* Don't check the bootable image flag because we could really call a
1804 * bootable or non-bootable image. Just validate that the image check
1805 * passes which is distinct from the normal check.
1806 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001807 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001808 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001809 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001810 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001811 if (rc != 0) {
1812 rc = SPLIT_GO_NON_MATCHING;
1813 goto done;
1814 }
1815
Marti Bolivarea088872017-06-12 17:10:49 -04001816 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001817 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001818 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001819 rc = SPLIT_GO_OK;
1820
1821done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001822 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1823 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001824 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001825 return rc;
1826}