blob: fccd83d4c8c3923cf714034e51647ea7d3e485bb [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
David Vinczee2453472019-06-17 12:31:59 +020020/*
David Vinczec3084132020-02-18 14:50:47 +010021 * Modifications are Copyright (c) 2019-2020 Arm Limited.
David Vinczee2453472019-06-17 12:31:59 +020022 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030039#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050040#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010041#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010042#include "bootutil/boot_record.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050043
Fabio Utzigba829042018-09-18 08:29:34 -030044#ifdef MCUBOOT_ENC_IMAGES
45#include "bootutil/enc_key.h"
46#endif
47
Fabio Utzigba1fbe62017-07-21 14:01:20 -030048#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030049
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010050MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
51
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040052static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080053
Fabio Utzigabec0732019-07-31 08:40:22 -030054#if (BOOT_IMAGE_NUMBER > 1)
55#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
56#else
57#define IMAGES_ITER(x)
58#endif
59
Fabio Utzig10ee6482019-08-01 12:04:52 -030060/*
61 * This macro allows some control on the allocation of local variables.
62 * When running natively on a target, we don't want to allocated huge
63 * variables on the stack, so make them global instead. For the simulator
64 * we want to run as many threads as there are tests, and it's safer
65 * to just make those variables stack allocated.
66 */
67#if !defined(__BOOTSIM__)
68#define TARGET_STATIC static
69#else
70#define TARGET_STATIC
71#endif
72
David Brownf5b33d82017-09-01 10:58:27 -060073/*
74 * Compute the total size of the given image. Includes the size of
75 * the TLVs.
76 */
Fabio Utzig13d9e352017-10-05 20:32:31 -030077#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -060078static int
Fabio Utzigd638b172019-08-09 10:38:05 -030079boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -060080{
81 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -030082 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -030083 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030084 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -060085 int area_id;
86 int rc;
87
Fabio Utzig10ee6482019-08-01 12:04:52 -030088#if (BOOT_IMAGE_NUMBER == 1)
89 (void)state;
90#endif
91
92 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -060093 rc = flash_area_open(area_id, &fap);
94 if (rc != 0) {
95 rc = BOOT_EFLASH;
96 goto done;
97 }
98
Fabio Utzig61fd8882019-09-14 20:00:20 -030099 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
100
101 if (flash_area_read(fap, off, &info, sizeof(info))) {
102 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600103 goto done;
104 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300105
Fabio Utzige52c08e2019-09-11 19:32:00 -0300106 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
107 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
108 if (protect_tlv_size != info.it_tlv_tot) {
109 rc = BOOT_EBADIMAGE;
110 goto done;
111 }
112
113 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
114 rc = BOOT_EFLASH;
115 goto done;
116 }
117 } else if (protect_tlv_size != 0) {
118 rc = BOOT_EBADIMAGE;
119 goto done;
120 }
121
Fabio Utzig61fd8882019-09-14 20:00:20 -0300122 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
123 rc = BOOT_EBADIMAGE;
124 goto done;
125 }
126
Fabio Utzige52c08e2019-09-11 19:32:00 -0300127 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600128 rc = 0;
129
130done:
131 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300132 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600133}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300134#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600135
Fabio Utzigc08ed212017-06-20 19:28:36 -0300136static int
Fabio Utzig12d59162019-11-28 10:01:59 -0300137boot_read_image_headers(struct boot_loader_state *state, bool require_all,
138 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800139{
140 int rc;
141 int i;
142
143 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig12d59162019-11-28 10:01:59 -0300144 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800145 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200146 /* If `require_all` is set, fail on any single fail, otherwise
147 * if at least the first slot's header was read successfully,
148 * then the boot loader can attempt a boot.
149 *
150 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800151 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200152 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800153 return 0;
154 } else {
155 return rc;
156 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800157 }
158 }
159
160 return 0;
161}
162
David Brownab449182019-11-15 09:32:52 -0700163static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300164boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800165{
David Brownab449182019-11-15 09:32:52 -0700166 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300167#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700168 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300169#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170
171 /* Figure out what size to write update status update as. The size depends
172 * on what the minimum write size is for scratch area, active image slot.
173 * We need to use the bigger of those 2 values.
174 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300175 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300176#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300177 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800178 if (align > elem_sz) {
179 elem_sz = align;
180 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300181#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182
183 return elem_sz;
184}
185
Fabio Utzig10ee6482019-08-01 12:04:52 -0300186#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
187static int
188boot_initialize_area(struct boot_loader_state *state, int flash_area)
189{
190 int num_sectors = BOOT_MAX_IMG_SECTORS;
191 int rc;
192
193 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
194 rc = flash_area_to_sectors(flash_area, &num_sectors,
195 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
196 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
197
198 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
199 rc = flash_area_to_sectors(flash_area, &num_sectors,
200 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
201 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
202
Fabio Utzig12d59162019-11-28 10:01:59 -0300203#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300204 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
205 rc = flash_area_to_sectors(flash_area, &num_sectors,
206 state->scratch.sectors);
207 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300208#endif
209
Fabio Utzig10ee6482019-08-01 12:04:52 -0300210 } else {
211 return BOOT_EFLASH;
212 }
213
214 return rc;
215}
216#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
217static int
218boot_initialize_area(struct boot_loader_state *state, int flash_area)
219{
220 uint32_t num_sectors;
221 struct flash_sector *out_sectors;
222 size_t *out_num_sectors;
223 int rc;
224
225 num_sectors = BOOT_MAX_IMG_SECTORS;
226
227 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
228 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
229 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
230 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
231 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
232 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300233#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300234 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
235 out_sectors = state->scratch.sectors;
236 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300237#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300238 } else {
239 return BOOT_EFLASH;
240 }
241
242 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
243 if (rc != 0) {
244 return rc;
245 }
246 *out_num_sectors = num_sectors;
247 return 0;
248}
249#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
250
Christopher Collins92ea77f2016-12-12 15:59:26 -0800251/**
252 * Determines the sector layout of both image slots and the scratch area.
253 * This information is necessary for calculating the number of bytes to erase
254 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300255 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800256 */
257static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300258boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300260 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262
Fabio Utzig10ee6482019-08-01 12:04:52 -0300263 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300264
265 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266 if (rc != 0) {
267 return BOOT_EFLASH;
268 }
269
Fabio Utzig10ee6482019-08-01 12:04:52 -0300270 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800271 if (rc != 0) {
272 return BOOT_EFLASH;
273 }
274
Fabio Utzig12d59162019-11-28 10:01:59 -0300275#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300276 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200277 if (rc != 0) {
278 return BOOT_EFLASH;
279 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300280#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200281
Fabio Utzig10ee6482019-08-01 12:04:52 -0300282 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800283
284 return 0;
285}
286
Fabio Utzig12d59162019-11-28 10:01:59 -0300287void
288boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289{
Fabio Utzig4741c452019-12-19 15:32:41 -0300290#ifdef MCUBOOT_ENC_IMAGES
291 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
292#if MCUBOOT_SWAP_SAVE_ENCTLV
293 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
294#endif
295#endif /* MCUBOOT_ENC_IMAGES */
296
297 bs->use_scratch = 0;
298 bs->swap_size = 0;
299 bs->source = 0;
300
Fabio Utzig74aef312019-11-28 11:05:34 -0300301 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300302 bs->idx = BOOT_STATUS_IDX_0;
303 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700304 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300305}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306
Fabio Utzig12d59162019-11-28 10:01:59 -0300307bool
308boot_status_is_reset(const struct boot_status *bs)
309{
Fabio Utzig74aef312019-11-28 11:05:34 -0300310 return (bs->op == BOOT_STATUS_OP_MOVE &&
311 bs->idx == BOOT_STATUS_IDX_0 &&
312 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313}
314
315/**
316 * Writes the supplied boot status to the flash file system. The boot status
317 * contains the current state of an in-progress image copy operation.
318 *
319 * @param bs The boot status to write.
320 *
321 * @return 0 on success; nonzero on failure.
322 */
323int
Fabio Utzig12d59162019-11-28 10:01:59 -0300324boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800325{
326 const struct flash_area *fap;
327 uint32_t off;
328 int area_id;
329 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300330 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700331 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300332 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800333
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300334 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100335 * the trailer. Since in the last step the primary slot is erased, the
336 * first two status writes go to the scratch which will be copied to
337 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300338 */
339
Fabio Utzig12d59162019-11-28 10:01:59 -0300340#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300341 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800342 /* Write to scratch. */
343 area_id = FLASH_AREA_IMAGE_SCRATCH;
344 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300345#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100346 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300347 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300348#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300350#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351
352 rc = flash_area_open(area_id, &fap);
353 if (rc != 0) {
354 rc = BOOT_EFLASH;
355 goto done;
356 }
357
358 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300359 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200360 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300361 erased_val = flash_area_erased_val(fap);
362 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700363 buf[0] = bs->state;
364
365 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 if (rc != 0) {
367 rc = BOOT_EFLASH;
368 goto done;
369 }
370
371 rc = 0;
372
373done:
374 flash_area_close(fap);
375 return rc;
376}
377
378/*
David Vinczec3084132020-02-18 14:50:47 +0100379 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 */
381static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300382boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
383 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300385 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300386 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300387 int rc;
388
Fabio Utzig10ee6482019-08-01 12:04:52 -0300389#if (BOOT_IMAGE_NUMBER == 1)
390 (void)state;
391#endif
392
Fabio Utzigba829042018-09-18 08:29:34 -0300393 (void)bs;
394 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300395
396 image_index = BOOT_CURR_IMG(state);
397
398#ifdef MCUBOOT_ENC_IMAGES
399 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300400 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300401 if (rc < 0) {
402 return BOOT_EBADIMAGE;
403 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300404 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300405 return BOOT_EBADIMAGE;
406 }
407 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300408#endif
409
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300410 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300411 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412 return BOOT_EBADIMAGE;
413 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300414
Christopher Collins92ea77f2016-12-12 15:59:26 -0800415 return 0;
416}
417
418static int
419split_image_check(struct image_header *app_hdr,
420 const struct flash_area *app_fap,
421 struct image_header *loader_hdr,
422 const struct flash_area *loader_fap)
423{
424 static void *tmpbuf;
425 uint8_t loader_hash[32];
426
427 if (!tmpbuf) {
428 tmpbuf = malloc(BOOT_TMPBUF_SZ);
429 if (!tmpbuf) {
430 return BOOT_ENOMEM;
431 }
432 }
433
Fabio Utzig10ee6482019-08-01 12:04:52 -0300434 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
435 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436 return BOOT_EBADIMAGE;
437 }
438
Fabio Utzig10ee6482019-08-01 12:04:52 -0300439 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
440 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441 return BOOT_EBADIMAGE;
442 }
443
444 return 0;
445}
446
Fabio Utzig338a19f2018-12-03 08:37:08 -0200447/*
David Brown9bf95af2019-10-10 15:36:36 -0600448 * Check that this is a valid header. Valid means that the magic is
449 * correct, and that the sizes/offsets are "sane". Sane means that
450 * there is no overflow on the arithmetic, and that the result fits
451 * within the flash area we are in.
452 */
453static bool
454boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
455{
456 uint32_t size;
457
458 if (hdr->ih_magic != IMAGE_MAGIC) {
459 return false;
460 }
461
462 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
463 return false;
464 }
465
466 if (size >= fap->fa_size) {
467 return false;
468 }
469
470 return true;
471}
472
473/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200474 * Check that a memory area consists of a given value.
475 */
476static inline bool
477boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300478{
479 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200480 uint8_t *p = (uint8_t *)data;
481 for (i = 0; i < len; i++) {
482 if (val != p[i]) {
483 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300484 }
485 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200486 return true;
487}
488
489static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300490boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200491{
492 const struct flash_area *fap;
493 struct image_header *hdr;
494 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300495 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200496 int rc;
497
Fabio Utzig10ee6482019-08-01 12:04:52 -0300498 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300499 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200500 if (rc != 0) {
501 return -1;
502 }
503
504 erased_val = flash_area_erased_val(fap);
505 flash_area_close(fap);
506
Fabio Utzig10ee6482019-08-01 12:04:52 -0300507 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200508 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
509 return -1;
510 }
511
512 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300513}
514
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000515#if (BOOT_IMAGE_NUMBER > 1) || \
516 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
517/**
518 * Check if the version of the image is not older than required.
519 *
520 * @param req Required minimal image version.
521 * @param ver Version of the image to be checked.
522 *
523 * @return 0 if the version is sufficient, nonzero otherwise.
524 */
525static int
526boot_is_version_sufficient(struct image_version *req,
527 struct image_version *ver)
528{
529 if (ver->iv_major > req->iv_major) {
530 return 0;
531 }
532 if (ver->iv_major < req->iv_major) {
533 return BOOT_EBADVERSION;
534 }
535 /* The major version numbers are equal. */
536 if (ver->iv_minor > req->iv_minor) {
537 return 0;
538 }
539 if (ver->iv_minor < req->iv_minor) {
540 return BOOT_EBADVERSION;
541 }
542 /* The minor version numbers are equal. */
543 if (ver->iv_revision < req->iv_revision) {
544 return BOOT_EBADVERSION;
545 }
546
547 return 0;
548}
549#endif
550
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300551/*
552 * Check that there is a valid image in a slot
553 *
554 * @returns
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300555 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300556 * 1 if no bootloable image was found
557 * -1 on any errors
558 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800559static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300560boot_validate_slot(struct boot_loader_state *state, int slot,
561 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800562{
563 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400564 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300565 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300567
Fabio Utzig10ee6482019-08-01 12:04:52 -0300568 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300569 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800570 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300571 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800572 }
573
Fabio Utzig10ee6482019-08-01 12:04:52 -0300574 hdr = boot_img_hdr(state, slot);
575 if (boot_check_header_erased(state, slot) == 0 ||
576 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100577 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300578 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200579 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300580 }
581
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000582#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
583 if (slot != BOOT_PRIMARY_SLOT) {
584 /* Check if version of secondary slot is sufficient */
585 rc = boot_is_version_sufficient(
586 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver,
587 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver);
588 if (rc != 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
589 BOOT_LOG_ERR("insufficient version in secondary slot");
590 flash_area_erase(fap, 0, fap->fa_size);
591 /* Image in the secondary slot does not satisfy version requirement.
592 * Erase the image and continue booting from the primary slot.
593 */
594 rc = 1;
595 goto out;
596 }
597 }
598#endif
599
David Brown9bf95af2019-10-10 15:36:36 -0600600 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100601 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700602 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100603 /* Image in the secondary slot is invalid. Erase the image and
604 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700605 */
606 }
David Brown098de832019-12-10 11:58:01 -0700607#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100608 BOOT_LOG_ERR("Image in the %s slot is not valid!",
609 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700610#endif
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000611 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200612 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800613 }
614
David Vincze2d736ad2019-02-18 11:50:22 +0100615 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200616 rc = 0;
617
618out:
619 flash_area_close(fap);
620 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800621}
622
623/**
624 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100625 * that a swap operation is required, the image in the secondary slot is checked
626 * for validity. If the image in the secondary slot is invalid, it is erased,
627 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800628 *
629 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
630 */
631static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300632boot_validated_swap_type(struct boot_loader_state *state,
633 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634{
635 int swap_type;
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300636 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800637
Fabio Utzigb0f04732019-07-31 09:49:19 -0300638 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzige575b0b2019-09-11 12:34:23 -0300639 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100640 /* Boot loader wants to switch to the secondary slot.
641 * Ensure image is valid.
642 */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300643 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
644 if (rc == 1) {
645 swap_type = BOOT_SWAP_TYPE_NONE;
646 } else if (rc != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300647 swap_type = BOOT_SWAP_TYPE_FAIL;
648 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800649 }
650
651 return swap_type;
652}
653
David Vinczec3084132020-02-18 14:50:47 +0100654#ifdef MCUBOOT_HW_ROLLBACK_PROT
655/**
656 * Updates the stored security counter value with the image's security counter
657 * value which resides in the given slot, only if it's greater than the stored
658 * value.
659 *
660 * @param image_index Index of the image to determine which security
661 * counter to update.
662 * @param slot Slot number of the image.
663 * @param hdr Pointer to the image header structure of the image
664 * that is currently stored in the given slot.
665 *
666 * @return 0 on success; nonzero on failure.
667 */
668static int
669boot_update_security_counter(uint8_t image_index, int slot,
670 struct image_header *hdr)
671{
672 const struct flash_area *fap = NULL;
673 uint32_t img_security_cnt;
674 int rc;
675
676 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
677 &fap);
678 if (rc != 0) {
679 rc = BOOT_EFLASH;
680 goto done;
681 }
682
683 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
684 if (rc != 0) {
685 goto done;
686 }
687
688 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
689 if (rc != 0) {
690 goto done;
691 }
692
693done:
694 flash_area_close(fap);
695 return rc;
696}
697#endif /* MCUBOOT_HW_ROLLBACK_PROT */
698
Christopher Collins92ea77f2016-12-12 15:59:26 -0800699/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800700 * Erases a region of flash.
701 *
Fabio Utzigba829042018-09-18 08:29:34 -0300702 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800703 * @param off The offset within the flash area to start the
704 * erase.
705 * @param sz The number of bytes to erase.
706 *
707 * @return 0 on success; nonzero on failure.
708 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300709int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300710boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711{
Fabio Utzigba829042018-09-18 08:29:34 -0300712 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713}
714
715/**
716 * Copies the contents of one flash region to another. You must erase the
717 * destination region prior to calling this function.
718 *
719 * @param flash_area_id_src The ID of the source flash area.
720 * @param flash_area_id_dst The ID of the destination flash area.
721 * @param off_src The offset within the source flash area to
722 * copy from.
723 * @param off_dst The offset within the destination flash area to
724 * copy to.
725 * @param sz The number of bytes to copy.
726 *
727 * @return 0 on success; nonzero on failure.
728 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300729int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300730boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300731 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300732 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733 uint32_t off_src, uint32_t off_dst, uint32_t sz)
734{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735 uint32_t bytes_copied;
736 int chunk_sz;
737 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300738#ifdef MCUBOOT_ENC_IMAGES
739 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300740 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300741 size_t blk_off;
742 struct image_header *hdr;
743 uint16_t idx;
744 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300745 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300746#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747
Fabio Utzig10ee6482019-08-01 12:04:52 -0300748 TARGET_STATIC uint8_t buf[1024];
749
750#if !defined(MCUBOOT_ENC_IMAGES)
751 (void)state;
752#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800753
Christopher Collins92ea77f2016-12-12 15:59:26 -0800754 bytes_copied = 0;
755 while (bytes_copied < sz) {
756 if (sz - bytes_copied > sizeof buf) {
757 chunk_sz = sizeof buf;
758 } else {
759 chunk_sz = sz - bytes_copied;
760 }
761
762 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
763 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300764 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800765 }
766
Fabio Utzigba829042018-09-18 08:29:34 -0300767#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300768 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300769 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
770 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
771 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
772 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100773 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300774 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300775#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300776 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300777 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100778 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300779 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300780 off = off_dst;
781 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300782#else
783 off = off_dst;
784 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
785 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
786 }
787#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200788 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300789 blk_sz = chunk_sz;
790 idx = 0;
791 if (off + bytes_copied < hdr->ih_hdr_size) {
792 /* do not decrypt header */
793 blk_off = 0;
794 blk_sz = chunk_sz - hdr->ih_hdr_size;
795 idx = hdr->ih_hdr_size;
796 } else {
797 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
798 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300799 tlv_off = BOOT_TLV_OFF(hdr);
800 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300801 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300802 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300803 blk_sz = 0;
804 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300805 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300806 }
807 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300808 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300809 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
810 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300811 }
812 }
813#endif
814
Christopher Collins92ea77f2016-12-12 15:59:26 -0800815 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
816 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300817 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800818 }
819
820 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300821
822 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800823 }
824
Fabio Utzigba829042018-09-18 08:29:34 -0300825 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800826}
827
Christopher Collins92ea77f2016-12-12 15:59:26 -0800828/**
David Vincze2d736ad2019-02-18 11:50:22 +0100829 * Overwrite primary slot with the image contained in the secondary slot.
830 * If a prior copy operation was interrupted by a system reset, this function
831 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800832 *
833 * @param bs The current boot status. This function reads
834 * this struct to determine if it is resuming
835 * an interrupted swap operation. This
836 * function writes the updated status to this
837 * function on return.
838 *
839 * @return 0 on success; nonzero on failure.
840 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200841#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600842static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300843boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600844{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400845 size_t sect_count;
846 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600847 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300848 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400849 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300850 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100851 const struct flash_area *fap_primary_slot;
852 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300853 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100854
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200855 (void)bs;
856
Fabio Utzig13d9e352017-10-05 20:32:31 -0300857#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
858 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300859 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300860 assert(rc == 0);
861#endif
David Brown17609d82017-05-05 09:41:34 -0600862
David Vincze2d736ad2019-02-18 11:50:22 +0100863 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
864 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600865
Fabio Utzigb0f04732019-07-31 09:49:19 -0300866 image_index = BOOT_CURR_IMG(state);
867
868 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
869 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300870 assert (rc == 0);
871
Fabio Utzigb0f04732019-07-31 09:49:19 -0300872 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
873 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300874 assert (rc == 0);
875
Fabio Utzig10ee6482019-08-01 12:04:52 -0300876 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300877 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300878 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300879 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600880 assert(rc == 0);
881
882 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300883
884#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
885 if (size >= src_size) {
886 break;
887 }
888#endif
David Brown17609d82017-05-05 09:41:34 -0600889 }
890
Fabio Utzigba829042018-09-18 08:29:34 -0300891#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300892 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300893 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300894 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300895 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100896
Fabio Utzigba829042018-09-18 08:29:34 -0300897 if (rc < 0) {
898 return BOOT_EBADIMAGE;
899 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300900 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300901 return BOOT_EBADIMAGE;
902 }
903 }
904#endif
905
David Vincze2d736ad2019-02-18 11:50:22 +0100906 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
907 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300908 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600909
David Vinczec3084132020-02-18 14:50:47 +0100910#ifdef MCUBOOT_HW_ROLLBACK_PROT
911 /* Update the stored security counter with the new image's security counter
912 * value. Both slots hold the new image at this point, but the secondary
913 * slot's image header must be passed since the image headers in the
914 * boot_data structure have not been updated yet.
915 */
916 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
917 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
918 if (rc != 0) {
919 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
920 return rc;
921 }
922#endif /* MCUBOOT_HW_ROLLBACK_PROT */
923
Fabio Utzig13d9e352017-10-05 20:32:31 -0300924 /*
925 * Erases header and trailer. The trailer is erased because when a new
926 * image is written without a trailer as is the case when using newt, the
927 * trailer that was left might trigger a new upgrade.
928 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700929 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300930 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300931 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
932 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600933 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300934 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700935 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300936 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300937 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
938 last_sector),
939 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
940 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300941 assert(rc == 0);
942
David Vincze2d736ad2019-02-18 11:50:22 +0100943 flash_area_close(fap_primary_slot);
944 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300945
David Vincze2d736ad2019-02-18 11:50:22 +0100946 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600947
948 return 0;
949}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200950#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300951
Christopher Collinsa1c12042019-05-23 14:00:28 -0700952#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300953/**
954 * Swaps the two images in flash. If a prior copy operation was interrupted
955 * by a system reset, this function completes that operation.
956 *
957 * @param bs The current boot status. This function reads
958 * this struct to determine if it is resuming
959 * an interrupted swap operation. This
960 * function writes the updated status to this
961 * function on return.
962 *
963 * @return 0 on success; nonzero on failure.
964 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300966boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800967{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300968 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -0300969#ifdef MCUBOOT_ENC_IMAGES
970 const struct flash_area *fap;
971 uint8_t slot;
972 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -0300973#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300974 uint32_t size;
975 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300976 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300977 int rc;
978
979 /* FIXME: just do this if asked by user? */
980
981 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300982 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300983
Fabio Utzig12d59162019-11-28 10:01:59 -0300984 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -0300985 /*
986 * No swap ever happened, so need to find the largest image which
987 * will be used to determine the amount of sectors to swap.
988 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300989 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300990 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -0300991 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -0600992 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300993 }
Fabio Utzig2473ac02017-05-02 12:45:02 -0300994
Fabio Utzigba829042018-09-18 08:29:34 -0300995#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200996 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300997 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -0300998 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300999 assert(rc >= 0);
1000
1001 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001002 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001003 assert(rc == 0);
1004 } else {
1005 rc = 0;
1006 }
1007 } else {
1008 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1009 }
1010#endif
1011
Fabio Utzig10ee6482019-08-01 12:04:52 -03001012 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001013 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001014 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001015 assert(rc == 0);
1016 }
1017
Fabio Utzigba829042018-09-18 08:29:34 -03001018#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001019 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001020 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001021 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001022 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001023 assert(rc >= 0);
1024
1025 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001026 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001027 assert(rc == 0);
1028 } else {
1029 rc = 0;
1030 }
1031 } else {
1032 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1033 }
1034#endif
1035
Fabio Utzig46490722017-09-04 15:34:32 -03001036 if (size > copy_size) {
1037 copy_size = size;
1038 }
1039
1040 bs->swap_size = copy_size;
1041 } else {
1042 /*
1043 * If a swap was under way, the swap_size should already be present
1044 * in the trailer...
1045 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001046 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001047 assert(rc == 0);
1048
1049 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001050
1051#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001052 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1053 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001054 assert(rc == 0);
1055
1056 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001057 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001058 break;
1059 }
1060 }
1061
1062 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001063 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001064 }
1065 }
1066#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001067 }
1068
Fabio Utzig12d59162019-11-28 10:01:59 -03001069 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001070
David Vincze2d736ad2019-02-18 11:50:22 +01001071#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001072 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001073 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001074 BOOT_LOG_WRN("%d status write fails performing the swap",
1075 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001076 }
1077#endif
1078
Christopher Collins92ea77f2016-12-12 15:59:26 -08001079 return 0;
1080}
David Brown17609d82017-05-05 09:41:34 -06001081#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001082
David Vinczee32483f2019-06-13 10:46:24 +02001083#if (BOOT_IMAGE_NUMBER > 1)
1084/**
1085 * Check the image dependency whether it is satisfied and modify
1086 * the swap type if necessary.
1087 *
1088 * @param dep Image dependency which has to be verified.
1089 *
1090 * @return 0 on success; nonzero on failure.
1091 */
1092static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001093boot_verify_slot_dependency(struct boot_loader_state *state,
1094 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001095{
1096 struct image_version *dep_version;
1097 size_t dep_slot;
1098 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001099 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001100
1101 /* Determine the source of the image which is the subject of
1102 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001103 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001104 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1105 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001106 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001107
1108 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1109 if (rc != 0) {
1110 /* Dependency not satisfied.
1111 * Modify the swap type to decrease the version number of the image
1112 * (which will be located in the primary slot after the boot process),
1113 * consequently the number of unsatisfied dependencies will be
1114 * decreased or remain the same.
1115 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001116 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001117 case BOOT_SWAP_TYPE_TEST:
1118 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001119 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001120 break;
1121 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001122 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001123 break;
1124 default:
1125 break;
1126 }
1127 }
1128
1129 return rc;
1130}
1131
1132/**
1133 * Read all dependency TLVs of an image from the flash and verify
1134 * one after another to see if they are all satisfied.
1135 *
1136 * @param slot Image slot number.
1137 *
1138 * @return 0 on success; nonzero on failure.
1139 */
1140static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001141boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001142{
1143 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001144 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001145 struct image_dependency dep;
1146 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001147 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001148 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001149 int rc;
1150
Fabio Utzig10ee6482019-08-01 12:04:52 -03001151 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001152 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001153 if (rc != 0) {
1154 rc = BOOT_EFLASH;
1155 goto done;
1156 }
1157
Fabio Utzig61fd8882019-09-14 20:00:20 -03001158 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1159 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001160 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001161 goto done;
1162 }
1163
Fabio Utzig61fd8882019-09-14 20:00:20 -03001164 while (true) {
1165 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1166 if (rc < 0) {
1167 return -1;
1168 } else if (rc > 0) {
1169 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001170 break;
1171 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001172
1173 if (len != sizeof(dep)) {
1174 rc = BOOT_EBADIMAGE;
1175 goto done;
1176 }
1177
1178 rc = flash_area_read(fap, off, &dep, len);
1179 if (rc != 0) {
1180 rc = BOOT_EFLASH;
1181 goto done;
1182 }
1183
1184 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1185 rc = BOOT_EBADARGS;
1186 goto done;
1187 }
1188
1189 /* Verify dependency and modify the swap type if not satisfied. */
1190 rc = boot_verify_slot_dependency(state, &dep);
1191 if (rc != 0) {
1192 /* Dependency not satisfied. */
1193 goto done;
1194 }
David Vinczee32483f2019-06-13 10:46:24 +02001195 }
1196
1197done:
1198 flash_area_close(fap);
1199 return rc;
1200}
1201
1202/**
David Vinczee32483f2019-06-13 10:46:24 +02001203 * Iterate over all the images and verify whether the image dependencies in the
1204 * TLV area are all satisfied and update the related swap type if necessary.
1205 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001206static int
1207boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001208{
Erik Johnson49063752020-02-06 09:59:31 -06001209 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001210 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001211
Fabio Utzig10ee6482019-08-01 12:04:52 -03001212 BOOT_CURR_IMG(state) = 0;
1213 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001214 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1215 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1216 slot = BOOT_SECONDARY_SLOT;
1217 } else {
1218 slot = BOOT_PRIMARY_SLOT;
1219 }
1220
1221 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001222 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001223 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001224 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001225 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001226 /* Cannot upgrade due to non-met dependencies, so disable all
1227 * image upgrades.
1228 */
1229 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1230 BOOT_CURR_IMG(state) = idx;
1231 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1232 }
1233 break;
David Vinczee32483f2019-06-13 10:46:24 +02001234 } else {
1235 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03001236 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001237 }
1238 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001239 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001240}
1241#endif /* (BOOT_IMAGE_NUMBER > 1) */
1242
Christopher Collins92ea77f2016-12-12 15:59:26 -08001243/**
David Vinczeba3bd602019-06-17 16:01:43 +02001244 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001245 *
David Vinczeba3bd602019-06-17 16:01:43 +02001246 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001247 *
1248 * @return 0 on success; nonzero on failure.
1249 */
1250static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001251boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001252{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001253 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001254#ifndef MCUBOOT_OVERWRITE_ONLY
1255 uint8_t swap_type;
1256#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001257
David Vinczeba3bd602019-06-17 16:01:43 +02001258 /* At this point there are no aborted swaps. */
1259#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001260 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001261#elif defined(MCUBOOT_BOOTSTRAP)
1262 /* Check if the image update was triggered by a bad image in the
1263 * primary slot (the validity of the image in the secondary slot had
1264 * already been checked).
1265 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001266 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1267 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1268 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001269 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001270 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001271 }
1272#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001273 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001274#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001275 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001276
1277#ifndef MCUBOOT_OVERWRITE_ONLY
1278 /* The following state needs image_ok be explicitly set after the
1279 * swap was finished to avoid a new revert.
1280 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001281 swap_type = BOOT_SWAP_TYPE(state);
1282 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1283 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001284 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001285 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001286 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001287 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001288 }
1289
David Vinczec3084132020-02-18 14:50:47 +01001290#ifdef MCUBOOT_HW_ROLLBACK_PROT
1291 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1292 /* Update the stored security counter with the new image's security
1293 * counter value. The primary slot holds the new image at this point,
1294 * but the secondary slot's image header must be passed since image
1295 * headers in the boot_data structure have not been updated yet.
1296 *
1297 * In case of a permanent image swap mcuboot will never attempt to
1298 * revert the images on the next reboot. Therefore, the security
1299 * counter must be increased right after the image upgrade.
1300 */
1301 rc = boot_update_security_counter(
1302 BOOT_CURR_IMG(state),
1303 BOOT_PRIMARY_SLOT,
1304 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1305 if (rc != 0) {
1306 BOOT_LOG_ERR("Security counter update failed after "
1307 "image upgrade.");
1308 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1309 }
1310 }
1311#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1312
Fabio Utzige575b0b2019-09-11 12:34:23 -03001313 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001314 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001315 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001316 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001317 }
David Vinczeba3bd602019-06-17 16:01:43 +02001318 }
1319#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001320
David Vinczeba3bd602019-06-17 16:01:43 +02001321 return rc;
1322}
1323
1324/**
1325 * Completes a previously aborted image swap.
1326 *
1327 * @param bs The current boot status.
1328 *
1329 * @return 0 on success; nonzero on failure.
1330 */
1331#if !defined(MCUBOOT_OVERWRITE_ONLY)
1332static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001333boot_complete_partial_swap(struct boot_loader_state *state,
1334 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001335{
1336 int rc;
1337
1338 /* Determine the type of swap operation being resumed from the
1339 * `swap-type` trailer field.
1340 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001341 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001342 assert(rc == 0);
1343
Fabio Utzig10ee6482019-08-01 12:04:52 -03001344 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001345
1346 /* The following states need image_ok be explicitly set after the
1347 * swap was finished to avoid a new revert.
1348 */
1349 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1350 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001351 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001352 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001353 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001354 }
1355 }
1356
Fabio Utzige575b0b2019-09-11 12:34:23 -03001357 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001358 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001359 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001360 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001361 }
1362 }
1363
Fabio Utzig10ee6482019-08-01 12:04:52 -03001364 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001365 BOOT_LOG_ERR("panic!");
1366 assert(0);
1367
1368 /* Loop forever... */
1369 while (1) {}
1370 }
1371
1372 return rc;
1373}
1374#endif /* !MCUBOOT_OVERWRITE_ONLY */
1375
1376#if (BOOT_IMAGE_NUMBER > 1)
1377/**
1378 * Review the validity of previously determined swap types of other images.
1379 *
1380 * @param aborted_swap The current image upgrade is a
1381 * partial/aborted swap.
1382 */
1383static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001384boot_review_image_swap_types(struct boot_loader_state *state,
1385 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001386{
1387 /* In that case if we rebooted in the middle of an image upgrade process, we
1388 * must review the validity of swap types, that were previously determined
1389 * for other images. The image_ok flag had not been set before the reboot
1390 * for any of the updated images (only the copy_done flag) and thus falsely
1391 * the REVERT swap type has been determined for the previous images that had
1392 * been updated before the reboot.
1393 *
1394 * There are two separate scenarios that we have to deal with:
1395 *
1396 * 1. The reboot has happened during swapping an image:
1397 * The current image upgrade has been determined as a
1398 * partial/aborted swap.
1399 * 2. The reboot has happened between two separate image upgrades:
1400 * In this scenario we must check the swap type of the current image.
1401 * In those cases if it is NONE or REVERT we cannot certainly determine
1402 * the fact of a reboot. In a consistent state images must move in the
1403 * same direction or stay in place, e.g. in practice REVERT and TEST
1404 * swap types cannot be present at the same time. If the swap type of
1405 * the current image is either TEST, PERM or FAIL we must review the
1406 * already determined swap types of other images and set each false
1407 * REVERT swap types to NONE (these images had been successfully
1408 * updated before the system rebooted between two separate image
1409 * upgrades).
1410 */
1411
Fabio Utzig10ee6482019-08-01 12:04:52 -03001412 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001413 /* Nothing to do */
1414 return;
1415 }
1416
1417 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001418 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1419 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001420 /* Nothing to do */
1421 return;
1422 }
1423 }
1424
Fabio Utzig10ee6482019-08-01 12:04:52 -03001425 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1426 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1427 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001428 }
1429 }
1430}
1431#endif
1432
1433/**
1434 * Prepare image to be updated if required.
1435 *
1436 * Prepare image to be updated if required with completing an image swap
1437 * operation if one was aborted and/or determining the type of the
1438 * swap operation. In case of any error set the swap type to NONE.
1439 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001440 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001441 * @param bs Pointer where the read and possibly updated
1442 * boot status can be written to.
1443 */
1444static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001445boot_prepare_image_for_update(struct boot_loader_state *state,
1446 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001447{
1448 int rc;
1449
1450 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001451 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001452 if (rc != 0) {
1453 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1454 " - too small?", BOOT_MAX_IMG_SECTORS);
1455 /* Unable to determine sector layout, continue with next image
1456 * if there is one.
1457 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001458 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001459 return;
1460 }
1461
1462 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001463 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001464 if (rc != 0) {
1465 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001466 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001467 BOOT_CURR_IMG(state));
1468 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001469 return;
1470 }
1471
1472 /* If the current image's slots aren't compatible, no swap is possible.
1473 * Just boot into primary slot.
1474 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001475 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001476 boot_status_reset(bs);
1477
1478#ifndef MCUBOOT_OVERWRITE_ONLY
1479 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001480 if (rc != 0) {
1481 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001482 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001483 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001484 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001485 return;
1486 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001487#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001488
Fabio Utzig74aef312019-11-28 11:05:34 -03001489#ifdef MCUBOOT_SWAP_USING_MOVE
1490 /*
1491 * Must re-read image headers because the boot status might
1492 * have been updated in the previous function call.
1493 */
1494 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1495 if (rc != 0) {
1496 /* Continue with next image if there is one. */
1497 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1498 BOOT_CURR_IMG(state));
1499 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1500 return;
1501 }
1502#endif
1503
David Vinczeba3bd602019-06-17 16:01:43 +02001504 /* Determine if we rebooted in the middle of an image swap
1505 * operation. If a partial swap was detected, complete it.
1506 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001507 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001508
1509#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001510 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001511#endif
1512
1513#ifdef MCUBOOT_OVERWRITE_ONLY
1514 /* Should never arrive here, overwrite-only mode has
1515 * no swap state.
1516 */
1517 assert(0);
1518#else
1519 /* Determine the type of swap operation being resumed from the
1520 * `swap-type` trailer field.
1521 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001522 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001523 assert(rc == 0);
1524#endif
1525 /* Attempt to read an image header from each slot. Ensure that
1526 * image headers in slots are aligned with headers in boot_data.
1527 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001528 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001529 assert(rc == 0);
1530
1531 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001532 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001533 } else {
1534 /* There was no partial swap, determine swap type. */
1535 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001536 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1537 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1538 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001539 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001540 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001541 }
1542
1543#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001544 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001545#endif
1546
1547#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001548 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001549 /* Header checks are done first because they are
1550 * inexpensive. Since overwrite-only copies starting from
1551 * offset 0, if interrupted, it might leave a valid header
1552 * magic, so also run validation on the primary slot to be
1553 * sure it's not OK.
1554 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001555 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1556 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1557 if (boot_img_hdr(state,
1558 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1559 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001560 /* Set swap type to REVERT to overwrite the primary
1561 * slot with the image contained in secondary slot
1562 * and to trigger the explicit setting of the
1563 * image_ok flag.
1564 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001565 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001566 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001567 }
1568 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001569#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001570 }
David Vinczeba3bd602019-06-17 16:01:43 +02001571 } else {
1572 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001573 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001574 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001575}
1576
Christopher Collins92ea77f2016-12-12 15:59:26 -08001577int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001578context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001579{
Marti Bolivar84898652017-06-13 17:20:22 -04001580 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001581 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001582 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001583 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001584 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001585 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001586
1587 /* The array of slot sectors are defined here (as opposed to file scope) so
1588 * that they don't get allocated for non-boot-loader apps. This is
1589 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001590 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001591 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001592 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1593 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001594#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001595 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001596#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001597
Fabio Utzig10ee6482019-08-01 12:04:52 -03001598 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001599 has_upgrade = false;
1600
1601#if (BOOT_IMAGE_NUMBER == 1)
1602 (void)has_upgrade;
1603#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001604
David Vinczeba3bd602019-06-17 16:01:43 +02001605 /* Iterate over all the images. By the end of the loop the swap type has
1606 * to be determined for each image and all aborted swaps have to be
1607 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001608 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001609 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001610
David Vinczeba3bd602019-06-17 16:01:43 +02001611#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1612 /* The keys used for encryption may no longer be valid (could belong to
1613 * another images). Therefore, mark them as invalid to force their reload
1614 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001615 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001616 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001617#endif
1618
Fabio Utzig10ee6482019-08-01 12:04:52 -03001619 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001620
Fabio Utzig10ee6482019-08-01 12:04:52 -03001621 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001622 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001623 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001624 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001625#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001626 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001627#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001628
1629 /* Open primary and secondary image areas for the duration
1630 * of this call.
1631 */
1632 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001633 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001634 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001635 assert(rc == 0);
1636 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001637#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001638 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001639 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001640 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001641#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001642
1643 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001644 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001645
Fabio Utzige575b0b2019-09-11 12:34:23 -03001646 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001647 has_upgrade = true;
1648 }
David Vinczeba3bd602019-06-17 16:01:43 +02001649 }
1650
David Vinczee32483f2019-06-13 10:46:24 +02001651#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001652 if (has_upgrade) {
1653 /* Iterate over all the images and verify whether the image dependencies
1654 * are all satisfied and update swap type if necessary.
1655 */
1656 rc = boot_verify_dependencies(state);
1657 if (rc == BOOT_EBADVERSION) {
1658 /*
1659 * It was impossible to upgrade because the expected dependency version
1660 * was not available. Here we already changed the swap_type so that
1661 * instead of asserting the bootloader, we continue and no upgrade is
1662 * performed.
1663 */
1664 rc = 0;
1665 }
1666 }
David Vinczee32483f2019-06-13 10:46:24 +02001667#endif
1668
David Vinczeba3bd602019-06-17 16:01:43 +02001669 /* Iterate over all the images. At this point there are no aborted swaps
1670 * and the swap types are determined for each image. By the end of the loop
1671 * all required update operations will have been finished.
1672 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001673 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001674
1675#if (BOOT_IMAGE_NUMBER > 1)
1676#ifdef MCUBOOT_ENC_IMAGES
1677 /* The keys used for encryption may no longer be valid (could belong to
1678 * another images). Therefore, mark them as invalid to force their reload
1679 * by boot_enc_load().
1680 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001681 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001682#endif /* MCUBOOT_ENC_IMAGES */
1683
1684 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001685 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001686#endif /* (BOOT_IMAGE_NUMBER > 1) */
1687
1688 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001689 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001690
Fabio Utzig10ee6482019-08-01 12:04:52 -03001691 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001692 case BOOT_SWAP_TYPE_NONE:
1693 break;
1694
1695 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1696 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1697 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001698 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001699 assert(rc == 0);
1700 break;
1701
1702 case BOOT_SWAP_TYPE_FAIL:
1703 /* The image in secondary slot was invalid and is now erased. Ensure
1704 * we don't try to boot into it again on the next reboot. Do this by
1705 * pretending we just reverted back to primary slot.
1706 */
1707#ifndef MCUBOOT_OVERWRITE_ONLY
1708 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001709 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001710 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001711 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001712 }
1713#endif /* !MCUBOOT_OVERWRITE_ONLY */
1714 break;
1715
1716 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001717 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001718 }
1719
Fabio Utzig10ee6482019-08-01 12:04:52 -03001720 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001721 BOOT_LOG_ERR("panic!");
1722 assert(0);
1723
1724 /* Loop forever... */
1725 while (1) {}
1726 }
1727 }
1728
1729 /* Iterate over all the images. At this point all required update operations
1730 * have finished. By the end of the loop each image in the primary slot will
1731 * have been re-validated.
1732 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001733 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1734 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001735 /* Attempt to read an image header from each slot. Ensure that image
1736 * headers in slots are aligned with headers in boot_data.
1737 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001738 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001739 if (rc != 0) {
1740 goto out;
1741 }
1742 /* Since headers were reloaded, it can be assumed we just performed
1743 * a swap or overwrite. Now the header info that should be used to
1744 * provide the data for the bootstrap, which previously was at
1745 * secondary slot, was updated to primary slot.
1746 */
1747 }
1748
1749#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001750 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001751 if (rc != 0) {
1752 rc = BOOT_EBADIMAGE;
1753 goto out;
1754 }
1755#else
1756 /* Even if we're not re-validating the primary slot, we could be booting
1757 * onto an empty flash chip. At least do a basic sanity check that
1758 * the magic number on the image is OK.
1759 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001760 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001761 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001762 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1763 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001764 rc = BOOT_EBADIMAGE;
1765 goto out;
1766 }
David Vinczec3084132020-02-18 14:50:47 +01001767#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1768
1769#ifdef MCUBOOT_HW_ROLLBACK_PROT
1770 /* Update the stored security counter with the active image's security
1771 * counter value. It will only be updated if the new security counter is
1772 * greater than the stored value.
1773 *
1774 * In case of a successful image swapping when the swap type is TEST the
1775 * security counter can be increased only after a reset, when the swap
1776 * type is NONE and the image has marked itself "OK" (the image_ok flag
1777 * has been set). This way a "revert" can be performed when it's
1778 * necessary.
1779 */
1780 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1781 rc = boot_update_security_counter(
1782 BOOT_CURR_IMG(state),
1783 BOOT_PRIMARY_SLOT,
1784 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1785 if (rc != 0) {
1786 BOOT_LOG_ERR("Security counter update failed after image "
1787 "validation.");
1788 goto out;
1789 }
1790 }
1791#endif /* MCUBOOT_HW_ROLLBACK_PROT */
David Vincze1cf11b52020-03-24 07:51:09 +01001792
1793#ifdef MCUBOOT_MEASURED_BOOT
1794 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
1795 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1796 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1797 if (rc != 0) {
1798 BOOT_LOG_ERR("Failed to add Image %u data to shared memory area",
1799 BOOT_CURR_IMG(state));
1800 }
1801#endif /* MCUBOOT_MEASURED_BOOT */
1802
1803#ifdef MCUBOOT_DATA_SHARING
1804 rc = boot_save_shared_data(boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1805 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1806 if (rc != 0) {
1807 BOOT_LOG_ERR("Failed to add data to shared memory area.");
1808 }
1809#endif /* MCUBOOT_DATA_SHARING */
David Vinczeba3bd602019-06-17 16:01:43 +02001810 }
1811
Fabio Utzigb0f04732019-07-31 09:49:19 -03001812#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001813 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001814 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001815#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001816
Fabio Utzigf616c542019-12-19 15:23:32 -03001817 /*
1818 * Since the boot_status struct stores plaintext encryption keys, reset
1819 * them here to avoid the possibility of jumping into an image that could
1820 * easily recover them.
1821 */
1822 memset(&bs, 0, sizeof(struct boot_status));
1823
Fabio Utzig10ee6482019-08-01 12:04:52 -03001824 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1825 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1826 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001827
Fabio Utzig298913b2019-08-28 11:22:45 -03001828out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001829 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001830#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001831 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001832#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001833 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001834 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001835 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001836 }
1837 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001838}
1839
Fabio Utzig10ee6482019-08-01 12:04:52 -03001840/**
1841 * Prepares the booting process. This function moves images around in flash as
1842 * appropriate, and tells you what address to boot from.
1843 *
1844 * @param rsp On success, indicates how booting should occur.
1845 *
1846 * @return 0 on success; nonzero on failure.
1847 */
1848int
1849boot_go(struct boot_rsp *rsp)
1850{
1851 return context_boot_go(&boot_data, rsp);
1852}
1853
Christopher Collins92ea77f2016-12-12 15:59:26 -08001854int
1855split_go(int loader_slot, int split_slot, void **entry)
1856{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001857 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001858 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001859 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001860 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001861 int rc;
1862
Christopher Collins92ea77f2016-12-12 15:59:26 -08001863 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1864 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001865 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001866 }
David Vinczeba3bd602019-06-17 16:01:43 +02001867 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1868 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001869
1870 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1871 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001872 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001873 assert(rc == 0);
1874 split_flash_id = flash_area_id_from_image_slot(split_slot);
1875 rc = flash_area_open(split_flash_id,
1876 &BOOT_IMG_AREA(&boot_data, split_slot));
1877 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001878
1879 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001880 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001881 if (rc != 0) {
1882 rc = SPLIT_GO_ERR;
1883 goto done;
1884 }
1885
Fabio Utzig12d59162019-11-28 10:01:59 -03001886 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001887 if (rc != 0) {
1888 goto done;
1889 }
1890
Christopher Collins92ea77f2016-12-12 15:59:26 -08001891 /* Don't check the bootable image flag because we could really call a
1892 * bootable or non-bootable image. Just validate that the image check
1893 * passes which is distinct from the normal check.
1894 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001895 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001896 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001897 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001898 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001899 if (rc != 0) {
1900 rc = SPLIT_GO_NON_MATCHING;
1901 goto done;
1902 }
1903
Marti Bolivarea088872017-06-12 17:10:49 -04001904 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001905 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001906 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001907 rc = SPLIT_GO_OK;
1908
1909done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001910 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1911 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001912 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001913 return rc;
1914}