blob: a2b237914799b9f671c5dc6767bd2b2aa9f7b4a3 [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 Utzigba829042018-09-18 08:29:34 -0300990boot_erase_last_sector(const struct flash_area *fap)
Fabio Utzig2473ac02017-05-02 12:45:02 -0300991{
992 uint8_t slot;
993 uint32_t last_sector;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300994 int rc;
995
Fabio Utzigba829042018-09-18 08:29:34 -0300996 switch (fap->fa_id) {
Fabio Utzig2473ac02017-05-02 12:45:02 -0300997 case FLASH_AREA_IMAGE_0:
998 slot = 0;
999 break;
1000 case FLASH_AREA_IMAGE_1:
1001 slot = 1;
1002 break;
1003 default:
1004 return BOOT_EFLASH;
1005 }
1006
Marti Bolivard3269fd2017-06-12 16:31:12 -04001007 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -03001008 rc = boot_erase_sector(fap,
Marti Bolivarea088872017-06-12 17:10:49 -04001009 boot_img_sector_off(&boot_data, slot, last_sector),
Marti Bolivard3269fd2017-06-12 16:31:12 -04001010 boot_img_sector_size(&boot_data, slot, last_sector));
Fabio Utzig2473ac02017-05-02 12:45:02 -03001011 assert(rc == 0);
1012
1013 return rc;
1014}
Fabio Utzig358c9352017-07-25 22:10:45 -03001015#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001016
Christopher Collins92ea77f2016-12-12 15:59:26 -08001017/**
1018 * Swaps the contents of two flash regions within the two image slots.
1019 *
1020 * @param idx The index of the first sector in the range of
1021 * sectors being swapped.
1022 * @param sz The number of bytes to swap.
1023 * @param bs The current boot status. This struct gets
1024 * updated according to the outcome.
1025 *
1026 * @return 0 on success; nonzero on failure.
1027 */
Fabio Utzig3488eef2017-06-12 10:25:43 -03001028#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins4772ac42017-02-27 20:08:01 -08001029static void
Christopher Collins92ea77f2016-12-12 15:59:26 -08001030boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1031{
Fabio Utzigba829042018-09-18 08:29:34 -03001032 const struct flash_area *fap_slot0;
1033 const struct flash_area *fap_slot1;
1034 const struct flash_area *fap_scratch;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035 uint32_t copy_sz;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001036 uint32_t trailer_sz;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001037 uint32_t img_off;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001038 uint32_t scratch_trailer_off;
1039 struct boot_swap_state swap_state;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001040 size_t last_sector;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001041 int rc;
1042
1043 /* Calculate offset from start of image area. */
Marti Bolivarea088872017-06-12 17:10:49 -04001044 img_off = boot_img_sector_off(&boot_data, 0, idx);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001045
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001046 copy_sz = sz;
Marti Bolivare10a7392017-06-14 16:20:07 -04001047 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Fabio Utzig9678c972017-05-23 11:28:56 -04001048
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001049 /* sz in this function is always sized on a multiple of the sector size.
1050 * The check against the start offset of the last sector
Fabio Utzig9678c972017-05-23 11:28:56 -04001051 * is to determine if we're swapping the last sector. The last sector
1052 * needs special handling because it's where the trailer lives. If we're
1053 * copying it, we need to use scratch to write the trailer temporarily.
1054 *
1055 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1056 * controls if special handling is needed (swapping last sector).
1057 */
Marti Bolivard3269fd2017-06-12 16:31:12 -04001058 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
Marti Bolivarea088872017-06-12 17:10:49 -04001059 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001060 copy_sz -= trailer_sz;
1061 }
1062
Fabio Utzig39000012018-07-30 12:40:20 -03001063 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001064
Fabio Utzigba829042018-09-18 08:29:34 -03001065 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1066 assert (rc == 0);
1067
1068 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1069 assert (rc == 0);
1070
1071 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1072 assert (rc == 0);
1073
Fabio Utzig39000012018-07-30 12:40:20 -03001074 if (bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001075 rc = boot_erase_sector(fap_scratch, 0, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001076 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001077
Fabio Utzigba829042018-09-18 08:29:34 -03001078 rc = boot_copy_sector(fap_slot1, fap_scratch, img_off, 0, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001079 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001080
Fabio Utzig39000012018-07-30 12:40:20 -03001081 if (bs->idx == BOOT_STATUS_IDX_0) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001082 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001083 boot_status_init(fap_scratch, bs);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001084 } else {
1085 /* Prepare the status area... here it is known that the
1086 * last sector is not being used by the image data so it's
1087 * safe to erase.
1088 */
Fabio Utzigba829042018-09-18 08:29:34 -03001089 rc = boot_erase_last_sector(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001090 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001091
Fabio Utzigba829042018-09-18 08:29:34 -03001092 boot_status_init(fap_slot0, bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001093 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001094 }
1095
Fabio Utzig39000012018-07-30 12:40:20 -03001096 bs->state = BOOT_STATUS_STATE_1;
Christopher Collins4772ac42017-02-27 20:08:01 -08001097 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001098 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001099 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001100
Fabio Utzig39000012018-07-30 12:40:20 -03001101 if (bs->state == BOOT_STATUS_STATE_1) {
Fabio Utzigba829042018-09-18 08:29:34 -03001102 rc = boot_erase_sector(fap_slot1, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001103 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001104
Fabio Utzigba829042018-09-18 08:29:34 -03001105 rc = boot_copy_sector(fap_slot0, fap_slot1, img_off, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001106 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001107
Fabio Utzig39000012018-07-30 12:40:20 -03001108 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001109 /* If not all sectors of the slot are being swapped,
1110 * guarantee here that only slot0 will have the state.
1111 */
Fabio Utzigba829042018-09-18 08:29:34 -03001112 rc = boot_erase_last_sector(fap_slot1);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001113 assert(rc == 0);
1114 }
1115
Fabio Utzig39000012018-07-30 12:40:20 -03001116 bs->state = BOOT_STATUS_STATE_2;
Christopher Collins4772ac42017-02-27 20:08:01 -08001117 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001118 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001119 }
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001120
Fabio Utzig39000012018-07-30 12:40:20 -03001121 if (bs->state == BOOT_STATUS_STATE_2) {
Fabio Utzigba829042018-09-18 08:29:34 -03001122 rc = boot_erase_sector(fap_slot0, img_off, sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001123 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001124
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001125 /* NOTE: also copy trailer from scratch (has status info) */
Fabio Utzigba829042018-09-18 08:29:34 -03001126 rc = boot_copy_sector(fap_scratch, fap_slot0, 0, img_off, copy_sz);
Christopher Collins4772ac42017-02-27 20:08:01 -08001127 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001128
Fabio Utzig94d998c2017-05-22 11:02:41 -04001129 if (bs->use_scratch) {
Fabio Utzigba829042018-09-18 08:29:34 -03001130 scratch_trailer_off = boot_status_off(fap_scratch);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001131
1132 /* copy current status that is being maintained in scratch */
Fabio Utzigba829042018-09-18 08:29:34 -03001133 rc = boot_copy_sector(fap_scratch, fap_slot0, scratch_trailer_off,
Fabio Utziga0bc9b52017-06-28 09:19:55 -03001134 img_off + copy_sz,
Marti Bolivare10a7392017-06-14 16:20:07 -04001135 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001136 BOOT_STATUS_ASSERT(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001137
Fabio Utzig2473ac02017-05-02 12:45:02 -03001138 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1139 &swap_state);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001140 assert(rc == 0);
1141
Fabio Utzigde8a38a2017-05-23 11:15:01 -04001142 if (swap_state.image_ok == BOOT_FLAG_SET) {
Fabio Utzigba829042018-09-18 08:29:34 -03001143 rc = boot_write_image_ok(fap_slot0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001144 assert(rc == 0);
1145 }
1146
Fabio Utzigba829042018-09-18 08:29:34 -03001147 rc = boot_write_swap_size(fap_slot0, bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001148 assert(rc == 0);
1149
Fabio Utzigba829042018-09-18 08:29:34 -03001150#ifdef MCUBOOT_ENC_IMAGES
1151 rc = boot_write_enc_key(fap_slot0, 0, bs->enckey[0]);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001152 assert(rc == 0);
1153
Fabio Utzigba829042018-09-18 08:29:34 -03001154 rc = boot_write_enc_key(fap_slot0, 1, bs->enckey[1]);
1155 assert(rc == 0);
1156#endif
1157
1158 rc = boot_write_magic(fap_slot0);
1159 assert(rc == 0);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001160 }
1161
Christopher Collins92ea77f2016-12-12 15:59:26 -08001162 bs->idx++;
Fabio Utzig39000012018-07-30 12:40:20 -03001163 bs->state = BOOT_STATUS_STATE_0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001164 bs->use_scratch = 0;
Christopher Collins4772ac42017-02-27 20:08:01 -08001165 rc = boot_write_status(bs);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001166 BOOT_STATUS_ASSERT(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001167 }
Fabio Utzigba829042018-09-18 08:29:34 -03001168
1169 flash_area_close(fap_slot0);
1170 flash_area_close(fap_slot1);
1171 flash_area_close(fap_scratch);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001172}
Fabio Utzig3488eef2017-06-12 10:25:43 -03001173#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001174
1175/**
Fabio Utzigba829042018-09-18 08:29:34 -03001176 * Overwrite slot 0 with the image contained in slot 1. If a prior copy
1177 * operation was interrupted by a system reset, this function redos the
1178 * copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001179 *
1180 * @param bs The current boot status. This function reads
1181 * this struct to determine if it is resuming
1182 * an interrupted swap operation. This
1183 * function writes the updated status to this
1184 * function on return.
1185 *
1186 * @return 0 on success; nonzero on failure.
1187 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001188#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001189static int
1190boot_copy_image(struct boot_status *bs)
1191{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001192 size_t sect_count;
1193 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001194 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001195 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001196 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001197 size_t last_sector;
Fabio Utzigba829042018-09-18 08:29:34 -03001198 const struct flash_area *fap_slot0;
1199 const struct flash_area *fap_slot1;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001200
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001201 (void)bs;
1202
Fabio Utzig13d9e352017-10-05 20:32:31 -03001203#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1204 uint32_t src_size = 0;
1205 rc = boot_read_image_size(1, boot_img_hdr(&boot_data, 1), &src_size);
1206 assert(rc == 0);
1207#endif
David Brown17609d82017-05-05 09:41:34 -06001208
1209 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1210 BOOT_LOG_INF("Erasing slot0");
1211
Fabio Utzigba829042018-09-18 08:29:34 -03001212 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap_slot0);
1213 assert (rc == 0);
1214
1215 rc = flash_area_open(FLASH_AREA_IMAGE_1, &fap_slot1);
1216 assert (rc == 0);
1217
Marti Bolivard3269fd2017-06-12 16:31:12 -04001218 sect_count = boot_img_num_sectors(&boot_data, 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001219 for (sect = 0, size = 0; sect < sect_count; sect++) {
Marti Bolivard3269fd2017-06-12 16:31:12 -04001220 this_size = boot_img_sector_size(&boot_data, 0, sect);
Fabio Utzigba829042018-09-18 08:29:34 -03001221 rc = boot_erase_sector(fap_slot0, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001222 assert(rc == 0);
1223
1224 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001225
1226#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1227 if (size >= src_size) {
1228 break;
1229 }
1230#endif
David Brown17609d82017-05-05 09:41:34 -06001231 }
1232
Fabio Utzigba829042018-09-18 08:29:34 -03001233#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001234 if (IS_ENCRYPTED(boot_img_hdr(&boot_data, 1))) {
Fabio Utzige641ea52018-12-03 10:37:53 -02001235 rc = boot_enc_load(boot_img_hdr(&boot_data, 1), fap_slot1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -03001236 if (rc < 0) {
1237 return BOOT_EBADIMAGE;
1238 }
Fabio Utzige641ea52018-12-03 10:37:53 -02001239 if (rc == 0 && boot_enc_set_key(1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -03001240 return BOOT_EBADIMAGE;
1241 }
1242 }
1243#endif
1244
Fabio Utzig338a19f2018-12-03 08:37:08 -02001245 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%zx bytes", size);
Fabio Utzigba829042018-09-18 08:29:34 -03001246 rc = boot_copy_sector(fap_slot1, fap_slot0, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -06001247
Fabio Utzig13d9e352017-10-05 20:32:31 -03001248 /*
1249 * Erases header and trailer. The trailer is erased because when a new
1250 * image is written without a trailer as is the case when using newt, the
1251 * trailer that was left might trigger a new upgrade.
1252 */
Fabio Utzigba829042018-09-18 08:29:34 -03001253 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001254 boot_img_sector_off(&boot_data, 1, 0),
1255 boot_img_sector_size(&boot_data, 1, 0));
David Brown17609d82017-05-05 09:41:34 -06001256 assert(rc == 0);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001257 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
Fabio Utzigba829042018-09-18 08:29:34 -03001258 rc = boot_erase_sector(fap_slot1,
Fabio Utzig13d9e352017-10-05 20:32:31 -03001259 boot_img_sector_off(&boot_data, 1, last_sector),
1260 boot_img_sector_size(&boot_data, 1, last_sector));
1261 assert(rc == 0);
1262
Fabio Utzigba829042018-09-18 08:29:34 -03001263 flash_area_close(fap_slot0);
1264 flash_area_close(fap_slot1);
1265
Fabio Utzig13d9e352017-10-05 20:32:31 -03001266 /* TODO: Perhaps verify slot 0's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001267
1268 return 0;
1269}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001270#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001271
1272/**
1273 * Swaps the two images in flash. If a prior copy operation was interrupted
1274 * by a system reset, this function completes that operation.
1275 *
1276 * @param bs The current boot status. This function reads
1277 * this struct to determine if it is resuming
1278 * an interrupted swap operation. This
1279 * function writes the updated status to this
1280 * function on return.
1281 *
1282 * @return 0 on success; nonzero on failure.
1283 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001284#if !defined(MCUBOOT_OVERWRITE_ONLY)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001285static int
Fabio Utzig338a19f2018-12-03 08:37:08 -02001286boot_swap_image(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001287{
1288 uint32_t sz;
1289 int first_sector_idx;
1290 int last_sector_idx;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001291 int last_idx_slot1;
Fabio Utzigcd5774b2017-11-29 10:18:26 -02001292 uint32_t swap_idx;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001293 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001294#ifdef MCUBOOT_ENC_IMAGES
1295 const struct flash_area *fap;
1296 uint8_t slot;
1297 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001298#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001299 uint32_t size;
1300 uint32_t copy_size;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001301 uint32_t slot0_size;
1302 uint32_t slot1_size;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001303 int rc;
1304
1305 /* FIXME: just do this if asked by user? */
1306
1307 size = copy_size = 0;
1308
Fabio Utzig39000012018-07-30 12:40:20 -03001309 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Fabio Utzig46490722017-09-04 15:34:32 -03001310 /*
1311 * No swap ever happened, so need to find the largest image which
1312 * will be used to determine the amount of sectors to swap.
1313 */
1314 hdr = boot_img_hdr(&boot_data, 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001315 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzig46490722017-09-04 15:34:32 -03001316 rc = boot_read_image_size(0, hdr, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001317 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001318 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001319
Fabio Utzigba829042018-09-18 08:29:34 -03001320#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001321 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001322 fap = BOOT_IMG_AREA(&boot_data, 0);
1323 rc = boot_enc_load(hdr, fap, bs->enckey[0]);
1324 assert(rc >= 0);
1325
1326 if (rc == 0) {
1327 rc = boot_enc_set_key(0, bs->enckey[0]);
1328 assert(rc == 0);
1329 } else {
1330 rc = 0;
1331 }
1332 } else {
1333 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1334 }
1335#endif
1336
Fabio Utzig46490722017-09-04 15:34:32 -03001337 hdr = boot_img_hdr(&boot_data, 1);
1338 if (hdr->ih_magic == IMAGE_MAGIC) {
1339 rc = boot_read_image_size(1, hdr, &size);
1340 assert(rc == 0);
1341 }
1342
Fabio Utzigba829042018-09-18 08:29:34 -03001343#ifdef MCUBOOT_ENC_IMAGES
1344 hdr = boot_img_hdr(&boot_data, 1);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001345 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001346 fap = BOOT_IMG_AREA(&boot_data, 1);
1347 rc = boot_enc_load(hdr, fap, bs->enckey[1]);
1348 assert(rc >= 0);
1349
1350 if (rc == 0) {
1351 rc = boot_enc_set_key(1, bs->enckey[1]);
1352 assert(rc == 0);
1353 } else {
1354 rc = 0;
1355 }
1356 } else {
1357 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1358 }
1359#endif
1360
Fabio Utzig46490722017-09-04 15:34:32 -03001361 if (size > copy_size) {
1362 copy_size = size;
1363 }
1364
1365 bs->swap_size = copy_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001366
Fabio Utzig46490722017-09-04 15:34:32 -03001367 } else {
1368 /*
1369 * If a swap was under way, the swap_size should already be present
1370 * in the trailer...
1371 */
1372 rc = boot_read_swap_size(&bs->swap_size);
1373 assert(rc == 0);
1374
1375 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001376
1377#ifdef MCUBOOT_ENC_IMAGES
1378 for (slot = 0; slot <= 1; slot++) {
1379 rc = boot_read_enc_key(slot, bs->enckey[slot]);
1380 assert(rc == 0);
1381
1382 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001383 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001384 break;
1385 }
1386 }
1387
1388 if (i != BOOT_ENC_KEY_SIZE) {
1389 boot_enc_set_key(slot, bs->enckey[slot]);
1390 }
1391 }
1392#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001393 }
1394
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001395 slot0_size = 0;
1396 slot1_size = 0;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001397 last_sector_idx = 0;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001398 last_idx_slot1 = 0;
1399
1400 /*
1401 * Knowing the size of the largest image between both slots, here we
1402 * find what is the last sector in slot0 that needs swapping. Since we
1403 * already know that both slots are compatible, slot1's last sector is
1404 * not really required after this check is finished.
1405 */
Fabio Utzig2473ac02017-05-02 12:45:02 -03001406 while (1) {
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001407 if (slot0_size < copy_size || slot0_size < slot1_size) {
1408 slot0_size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
1409 }
1410 if (slot1_size < copy_size || slot1_size < slot0_size) {
1411 slot1_size += boot_img_sector_size(&boot_data, 1, last_idx_slot1);
1412 }
1413 if (slot0_size >= copy_size &&
1414 slot1_size >= copy_size &&
1415 slot0_size == slot1_size) {
Fabio Utzig2473ac02017-05-02 12:45:02 -03001416 break;
1417 }
1418 last_sector_idx++;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001419 last_idx_slot1++;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001420 }
Christopher Collins92ea77f2016-12-12 15:59:26 -08001421
1422 swap_idx = 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001423 while (last_sector_idx >= 0) {
1424 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
Fabio Utzig39000012018-07-30 12:40:20 -03001425 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001426 boot_swap_sectors(first_sector_idx, sz, bs);
1427 }
1428
1429 last_sector_idx = first_sector_idx - 1;
1430 swap_idx++;
1431 }
1432
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001433#ifdef MCUBOOT_VALIDATE_SLOT0
1434 if (boot_status_fails > 0) {
1435 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1436 }
1437#endif
1438
Christopher Collins92ea77f2016-12-12 15:59:26 -08001439 return 0;
1440}
David Brown17609d82017-05-05 09:41:34 -06001441#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001442
1443/**
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001444 * Marks the image in slot 0 as fully copied.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001445 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001446#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001447static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001448boot_set_copy_done(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001449{
1450 const struct flash_area *fap;
1451 int rc;
1452
1453 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1454 if (rc != 0) {
1455 return BOOT_EFLASH;
1456 }
1457
1458 rc = boot_write_copy_done(fap);
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001459 flash_area_close(fap);
1460 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001461}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001462#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001463
1464/**
1465 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1466 * the status bytes from the image revert operation don't get processed on a
1467 * subsequent boot.
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001468 *
1469 * NOTE: image_ok is tested before writing because if there's a valid permanent
1470 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1471 * image_ok would be overwritten.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001472 */
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001473#ifndef MCUBOOT_OVERWRITE_ONLY
Christopher Collins92ea77f2016-12-12 15:59:26 -08001474static int
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001475boot_set_image_ok(void)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001476{
1477 const struct flash_area *fap;
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001478 struct boot_swap_state state;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001479 int rc;
1480
1481 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1482 if (rc != 0) {
1483 return BOOT_EFLASH;
1484 }
1485
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001486 rc = boot_read_swap_state(fap, &state);
1487 if (rc != 0) {
1488 rc = BOOT_EFLASH;
1489 goto out;
1490 }
1491
1492 if (state.image_ok == BOOT_FLAG_UNSET) {
1493 rc = boot_write_image_ok(fap);
1494 }
1495
1496out:
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001497 flash_area_close(fap);
1498 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001499}
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001500#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001501
1502/**
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001503 * Performs an image swap if one is required.
1504 *
1505 * @param out_swap_type On success, the type of swap performed gets
1506 * written here.
1507 *
1508 * @return 0 on success; nonzero on failure.
1509 */
1510static int
1511boot_swap_if_needed(int *out_swap_type)
1512{
1513 struct boot_status bs;
1514 int swap_type;
1515 int rc;
1516
1517 /* Determine if we rebooted in the middle of an image swap
1518 * operation.
1519 */
1520 rc = boot_read_status(&bs);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001521 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001522 if (rc != 0) {
1523 return rc;
1524 }
1525
1526 /* If a partial swap was detected, complete it. */
Fabio Utzig39000012018-07-30 12:40:20 -03001527 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Fabio Utziga32f1af2019-01-07 06:50:58 -02001528#ifdef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig338a19f2018-12-03 08:37:08 -02001529 /* Should never arrive here, overwrite-only mode has no swap state. */
1530 assert(0);
1531#else
1532 rc = boot_swap_image(&bs);
1533#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001534 assert(rc == 0);
1535
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001536 /* NOTE: here we have finished a swap resume. The initial request
1537 * was either a TEST or PERM swap, which now after the completed
1538 * swap will be determined to be respectively REVERT (was TEST)
1539 * or NONE (was PERM).
1540 */
1541
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001542 /* Extrapolate the type of the partial swap. We need this
1543 * information to know how to mark the swap complete in flash.
1544 */
1545 swap_type = boot_previous_swap_type();
1546 } else {
Fabio Utzigba829042018-09-18 08:29:34 -03001547 swap_type = boot_validated_swap_type(&bs);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001548 switch (swap_type) {
1549 case BOOT_SWAP_TYPE_TEST:
Christopher Collinsfd7eb5c2016-12-21 13:46:08 -08001550 case BOOT_SWAP_TYPE_PERM:
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001551 case BOOT_SWAP_TYPE_REVERT:
Fabio Utziga32f1af2019-01-07 06:50:58 -02001552#ifdef MCUBOOT_OVERWRITE_ONLY
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001553 rc = boot_copy_image(&bs);
Fabio Utzig338a19f2018-12-03 08:37:08 -02001554#else
1555 rc = boot_swap_image(&bs);
1556#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001557 assert(rc == 0);
1558 break;
Fabio Utzig338a19f2018-12-03 08:37:08 -02001559#ifdef MCUBOOT_BOOTSTRAP
1560 case BOOT_SWAP_TYPE_NONE:
1561 /*
1562 * Header checks are done first because they are inexpensive.
1563 * Since overwrite-only copies starting from offset 0, if
1564 * interrupted, it might leave a valid header magic, so also
1565 * run validation on slot0 to be sure it's not OK.
1566 */
1567 if (boot_check_header_erased(0) == 0 ||
1568 boot_validate_slot(0, &bs) != 0) {
1569 if (boot_img_hdr(&boot_data, 1)->ih_magic == IMAGE_MAGIC &&
1570 boot_validate_slot(1, &bs) == 0) {
1571 rc = boot_copy_image(&bs);
1572 assert(rc == 0);
1573
1574 /* Returns fail here to trigger a re-read of the headers. */
1575 swap_type = BOOT_SWAP_TYPE_FAIL;
1576 }
1577 }
1578 break;
1579#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001580 }
1581 }
1582
1583 *out_swap_type = swap_type;
1584 return 0;
1585}
1586
1587/**
Christopher Collins92ea77f2016-12-12 15:59:26 -08001588 * Prepares the booting process. This function moves images around in flash as
1589 * appropriate, and tells you what address to boot from.
1590 *
1591 * @param rsp On success, indicates how booting should occur.
1592 *
1593 * @return 0 on success; nonzero on failure.
1594 */
1595int
1596boot_go(struct boot_rsp *rsp)
1597{
Christopher Collins92ea77f2016-12-12 15:59:26 -08001598 int swap_type;
Marti Bolivar84898652017-06-13 17:20:22 -04001599 size_t slot;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001600 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001601 int fa_id;
David Brown52eee562017-07-05 11:25:09 -06001602 bool reload_headers = false;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001603
1604 /* The array of slot sectors are defined here (as opposed to file scope) so
1605 * that they don't get allocated for non-boot-loader apps. This is
1606 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001607 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001608 */
Marti Bolivarc50926f2017-06-14 09:35:40 -04001609 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1610 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001611 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Christopher Collins92ea77f2016-12-12 15:59:26 -08001612 boot_data.imgs[0].sectors = slot0_sectors;
1613 boot_data.imgs[1].sectors = slot1_sectors;
Fabio Utzig2bd980a2018-11-26 10:38:17 -02001614 boot_data.scratch.sectors = scratch_sectors;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001615
Fabio Utzigba829042018-09-18 08:29:34 -03001616#ifdef MCUBOOT_ENC_IMAGES
1617 /* FIXME: remove this after RAM is cleared by sim */
1618 boot_enc_zeroize();
1619#endif
1620
Marti Bolivarc0b47912017-06-13 17:18:09 -04001621 /* Open boot_data image areas for the duration of this call. */
1622 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1623 fa_id = flash_area_id_from_image_slot(slot);
1624 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1625 assert(rc == 0);
1626 }
1627 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1628 &BOOT_SCRATCH_AREA(&boot_data));
1629 assert(rc == 0);
1630
Christopher Collins92ea77f2016-12-12 15:59:26 -08001631 /* Determine the sector layout of the image slots and scratch area. */
1632 rc = boot_read_sectors();
1633 if (rc != 0) {
Fabio Utziga1fae672018-03-30 10:52:38 -03001634 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1635 BOOT_MAX_IMG_SECTORS);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001636 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001637 }
1638
1639 /* Attempt to read an image header from each slot. */
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001640 rc = boot_read_image_headers(false);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001641 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001642 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001643 }
1644
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001645 /* If the image slots aren't compatible, no swap is possible. Just boot
1646 * into slot 0.
1647 */
1648 if (boot_slots_compatible()) {
1649 rc = boot_swap_if_needed(&swap_type);
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001650 assert(rc == 0);
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001651 if (rc != 0) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001652 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001653 }
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001654
1655 /*
1656 * The following states need image_ok be explicitly set after the
1657 * swap was finished to avoid a new revert.
1658 */
1659 if (swap_type == BOOT_SWAP_TYPE_REVERT || swap_type == BOOT_SWAP_TYPE_FAIL) {
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001660#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001661 rc = boot_set_image_ok();
1662 if (rc != 0) {
1663 swap_type = BOOT_SWAP_TYPE_PANIC;
1664 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001665#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001666 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001667 } else {
1668 swap_type = BOOT_SWAP_TYPE_NONE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001669 }
1670
1671 switch (swap_type) {
1672 case BOOT_SWAP_TYPE_NONE:
1673 slot = 0;
1674 break;
1675
Fabio Utzig695d5642017-07-20 09:47:16 -03001676 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1677 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001678 case BOOT_SWAP_TYPE_REVERT:
1679 slot = 1;
David Brown52eee562017-07-05 11:25:09 -06001680 reload_headers = true;
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001681#ifndef MCUBOOT_OVERWRITE_ONLY
Fabio Utzig695d5642017-07-20 09:47:16 -03001682 rc = boot_set_copy_done();
1683 if (rc != 0) {
1684 swap_type = BOOT_SWAP_TYPE_PANIC;
1685 }
Fabio Utzig8d0e5882017-09-13 17:32:44 -03001686#endif /* !MCUBOOT_OVERWRITE_ONLY */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001687 break;
1688
1689 case BOOT_SWAP_TYPE_FAIL:
1690 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1691 * try to boot into it again on the next reboot. Do this by pretending
1692 * we just reverted back to slot 0.
1693 */
1694 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001695 reload_headers = true;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001696 break;
1697
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001698 default:
Fabio Utzig695d5642017-07-20 09:47:16 -03001699 swap_type = BOOT_SWAP_TYPE_PANIC;
1700 }
1701
1702 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1703 BOOT_LOG_ERR("panic!");
Fabio Utzigb5b2f552017-06-30 10:03:47 -03001704 assert(0);
Fabio Utzig695d5642017-07-20 09:47:16 -03001705
1706 /* Loop forever... */
1707 while (1) {}
Christopher Collins92ea77f2016-12-12 15:59:26 -08001708 }
1709
David Brown52eee562017-07-05 11:25:09 -06001710 if (reload_headers) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001711 rc = boot_read_image_headers(false);
Fabio Utzigc6a7b0c2017-09-13 19:01:15 -03001712 if (rc != 0) {
1713 goto out;
1714 }
1715 /* Since headers were reloaded, it can be assumed we just performed a
1716 * swap or overwrite. Now the header info that should be used to
1717 * provide the data for the bootstrap, which previously was at Slot 1,
1718 * was updated to Slot 0.
1719 */
1720 slot = 0;
David Brown52eee562017-07-05 11:25:09 -06001721 }
1722
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001723#ifdef MCUBOOT_VALIDATE_SLOT0
Fabio Utzigba829042018-09-18 08:29:34 -03001724 rc = boot_validate_slot(0, NULL);
Fabio Utzig57c40f72017-12-12 21:48:30 -02001725 ASSERT(rc == 0);
David Brown554c52e2017-06-30 16:01:07 -06001726 if (rc != 0) {
1727 rc = BOOT_EBADIMAGE;
1728 goto out;
1729 }
Fabio Utzig1e56fcc2017-07-17 15:39:14 -03001730#else
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001731 /* Even if we're not re-validating slot 0, we could be booting
1732 * onto an empty flash chip. At least do a basic sanity check that
1733 * the magic number on the image is OK.
1734 */
1735 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
Fabio Utzig67716012018-02-26 10:36:15 -03001736 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
Marti Bolivarc1f939d2017-11-14 20:04:51 -05001737 rc = BOOT_EBADIMAGE;
1738 goto out;
1739 }
David Brown554c52e2017-06-30 16:01:07 -06001740#endif
1741
Christopher Collins92ea77f2016-12-12 15:59:26 -08001742 /* Always boot from the primary slot. */
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +02001743 rsp->br_flash_dev_id = boot_data.imgs[0].area->fa_device_id;
Marti Bolivar88f48d92017-05-01 22:30:02 -04001744 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
Marti Bolivarf804f622017-06-12 15:41:48 -04001745 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001746
Marti Bolivarc0b47912017-06-13 17:18:09 -04001747 out:
1748 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1749 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1750 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1751 }
1752 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001753}
1754
1755int
1756split_go(int loader_slot, int split_slot, void **entry)
1757{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001758 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001759 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001760 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001761 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001762 int rc;
1763
Christopher Collins92ea77f2016-12-12 15:59:26 -08001764 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1765 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001766 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001767 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001768 boot_data.imgs[loader_slot].sectors = sectors + 0;
1769 boot_data.imgs[split_slot].sectors = sectors + BOOT_MAX_IMG_SECTORS;
1770
1771 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1772 rc = flash_area_open(loader_flash_id,
1773 &BOOT_IMG_AREA(&boot_data, split_slot));
1774 assert(rc == 0);
1775 split_flash_id = flash_area_id_from_image_slot(split_slot);
1776 rc = flash_area_open(split_flash_id,
1777 &BOOT_IMG_AREA(&boot_data, split_slot));
1778 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001779
1780 /* Determine the sector layout of the image slots and scratch area. */
1781 rc = boot_read_sectors();
1782 if (rc != 0) {
1783 rc = SPLIT_GO_ERR;
1784 goto done;
1785 }
1786
Fabio Utzig9c25fa72017-12-12 14:57:20 -02001787 rc = boot_read_image_headers(true);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001788 if (rc != 0) {
1789 goto done;
1790 }
1791
Christopher Collins92ea77f2016-12-12 15:59:26 -08001792 /* Don't check the bootable image flag because we could really call a
1793 * bootable or non-bootable image. Just validate that the image check
1794 * passes which is distinct from the normal check.
1795 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001796 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001797 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001798 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001799 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001800 if (rc != 0) {
1801 rc = SPLIT_GO_NON_MATCHING;
1802 goto done;
1803 }
1804
Marti Bolivarea088872017-06-12 17:10:49 -04001805 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001806 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001807 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001808 rc = SPLIT_GO_OK;
1809
1810done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001811 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1812 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001813 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001814 return rc;
1815}