blob: c98b841807b27620add24dbc0890a4186e43c071 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2016-2020 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
6 * Copyright (c) 2019-2020 Arm Limited
7 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28/**
29 * This file provides an interface to the boot loader. Functions defined in
30 * this file should only be called while the boot loader is running.
31 */
32
33#include <assert.h>
34#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060035#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include <inttypes.h>
37#include <stdlib.h>
38#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include <os/os_malloc.h>
40#include "bootutil/bootutil.h"
41#include "bootutil/image.h"
42#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030043#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050044#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010045#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010046#include "bootutil/boot_record.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050047
Fabio Utzigba829042018-09-18 08:29:34 -030048#ifdef MCUBOOT_ENC_IMAGES
49#include "bootutil/enc_key.h"
50#endif
51
Fabio Utzigba1fbe62017-07-21 14:01:20 -030052#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030053
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010054MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
55
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040056static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080057
Fabio Utzigabec0732019-07-31 08:40:22 -030058#if (BOOT_IMAGE_NUMBER > 1)
59#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
60#else
61#define IMAGES_ITER(x)
62#endif
63
Fabio Utzig10ee6482019-08-01 12:04:52 -030064/*
65 * This macro allows some control on the allocation of local variables.
66 * When running natively on a target, we don't want to allocated huge
67 * variables on the stack, so make them global instead. For the simulator
68 * we want to run as many threads as there are tests, and it's safer
69 * to just make those variables stack allocated.
70 */
71#if !defined(__BOOTSIM__)
72#define TARGET_STATIC static
73#else
74#define TARGET_STATIC
75#endif
76
David Vinczee574f2d2020-07-10 11:42:03 +020077static int
78boot_read_image_headers(struct boot_loader_state *state, bool require_all,
79 struct boot_status *bs)
80{
81 int rc;
82 int i;
83
84 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
85 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
86 if (rc != 0) {
87 /* If `require_all` is set, fail on any single fail, otherwise
88 * if at least the first slot's header was read successfully,
89 * then the boot loader can attempt a boot.
90 *
91 * Failure to read any headers is a fatal error.
92 */
93 if (i > 0 && !require_all) {
94 return 0;
95 } else {
96 return rc;
97 }
98 }
99 }
100
101 return 0;
102}
103
104#ifndef MCUBOOT_DIRECT_XIP
David Brownf5b33d82017-09-01 10:58:27 -0600105/*
106 * Compute the total size of the given image. Includes the size of
107 * the TLVs.
108 */
Fabio Utzig13d9e352017-10-05 20:32:31 -0300109#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600110static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300111boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600112{
113 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300114 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300115 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300116 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600117 int area_id;
118 int rc;
119
Fabio Utzig10ee6482019-08-01 12:04:52 -0300120#if (BOOT_IMAGE_NUMBER == 1)
121 (void)state;
122#endif
123
124 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600125 rc = flash_area_open(area_id, &fap);
126 if (rc != 0) {
127 rc = BOOT_EFLASH;
128 goto done;
129 }
130
Fabio Utzig61fd8882019-09-14 20:00:20 -0300131 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
132
133 if (flash_area_read(fap, off, &info, sizeof(info))) {
134 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600135 goto done;
136 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300137
Fabio Utzige52c08e2019-09-11 19:32:00 -0300138 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
139 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
140 if (protect_tlv_size != info.it_tlv_tot) {
141 rc = BOOT_EBADIMAGE;
142 goto done;
143 }
144
145 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
146 rc = BOOT_EFLASH;
147 goto done;
148 }
149 } else if (protect_tlv_size != 0) {
150 rc = BOOT_EBADIMAGE;
151 goto done;
152 }
153
Fabio Utzig61fd8882019-09-14 20:00:20 -0300154 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
155 rc = BOOT_EBADIMAGE;
156 goto done;
157 }
158
Fabio Utzige52c08e2019-09-11 19:32:00 -0300159 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600160 rc = 0;
161
162done:
163 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300164 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600165}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300166#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600167
David Brownab449182019-11-15 09:32:52 -0700168static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300169boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170{
David Brownab449182019-11-15 09:32:52 -0700171 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300172#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700173 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300174#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175
176 /* Figure out what size to write update status update as. The size depends
177 * on what the minimum write size is for scratch area, active image slot.
178 * We need to use the bigger of those 2 values.
179 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300180 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300181#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300182 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183 if (align > elem_sz) {
184 elem_sz = align;
185 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300186#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187
188 return elem_sz;
189}
190
Fabio Utzig10ee6482019-08-01 12:04:52 -0300191#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
192static int
193boot_initialize_area(struct boot_loader_state *state, int flash_area)
194{
195 int num_sectors = BOOT_MAX_IMG_SECTORS;
196 int rc;
197
198 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
199 rc = flash_area_to_sectors(flash_area, &num_sectors,
200 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
201 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
202
203 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
204 rc = flash_area_to_sectors(flash_area, &num_sectors,
205 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
206 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
207
Fabio Utzig12d59162019-11-28 10:01:59 -0300208#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300209 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
210 rc = flash_area_to_sectors(flash_area, &num_sectors,
211 state->scratch.sectors);
212 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300213#endif
214
Fabio Utzig10ee6482019-08-01 12:04:52 -0300215 } else {
216 return BOOT_EFLASH;
217 }
218
219 return rc;
220}
221#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
222static int
223boot_initialize_area(struct boot_loader_state *state, int flash_area)
224{
225 uint32_t num_sectors;
226 struct flash_sector *out_sectors;
227 size_t *out_num_sectors;
228 int rc;
229
230 num_sectors = BOOT_MAX_IMG_SECTORS;
231
232 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
233 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
234 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
235 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
236 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
237 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300238#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300239 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
240 out_sectors = state->scratch.sectors;
241 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300242#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300243 } else {
244 return BOOT_EFLASH;
245 }
246
247 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
248 if (rc != 0) {
249 return rc;
250 }
251 *out_num_sectors = num_sectors;
252 return 0;
253}
254#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
255
Christopher Collins92ea77f2016-12-12 15:59:26 -0800256/**
257 * Determines the sector layout of both image slots and the scratch area.
258 * This information is necessary for calculating the number of bytes to erase
259 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300260 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261 */
262static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300263boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300265 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267
Fabio Utzig10ee6482019-08-01 12:04:52 -0300268 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300269
270 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800271 if (rc != 0) {
272 return BOOT_EFLASH;
273 }
274
Fabio Utzig10ee6482019-08-01 12:04:52 -0300275 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800276 if (rc != 0) {
277 return BOOT_EFLASH;
278 }
279
Fabio Utzig12d59162019-11-28 10:01:59 -0300280#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300281 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200282 if (rc != 0) {
283 return BOOT_EFLASH;
284 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300285#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200286
Fabio Utzig10ee6482019-08-01 12:04:52 -0300287 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288
289 return 0;
290}
291
Fabio Utzig12d59162019-11-28 10:01:59 -0300292void
293boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294{
Fabio Utzig4741c452019-12-19 15:32:41 -0300295#ifdef MCUBOOT_ENC_IMAGES
296 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
297#if MCUBOOT_SWAP_SAVE_ENCTLV
298 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
299#endif
300#endif /* MCUBOOT_ENC_IMAGES */
301
302 bs->use_scratch = 0;
303 bs->swap_size = 0;
304 bs->source = 0;
305
Fabio Utzig74aef312019-11-28 11:05:34 -0300306 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300307 bs->idx = BOOT_STATUS_IDX_0;
308 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700309 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300310}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311
Fabio Utzig12d59162019-11-28 10:01:59 -0300312bool
313boot_status_is_reset(const struct boot_status *bs)
314{
Fabio Utzig74aef312019-11-28 11:05:34 -0300315 return (bs->op == BOOT_STATUS_OP_MOVE &&
316 bs->idx == BOOT_STATUS_IDX_0 &&
317 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800318}
319
320/**
321 * Writes the supplied boot status to the flash file system. The boot status
322 * contains the current state of an in-progress image copy operation.
323 *
324 * @param bs The boot status to write.
325 *
326 * @return 0 on success; nonzero on failure.
327 */
328int
Fabio Utzig12d59162019-11-28 10:01:59 -0300329boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800330{
331 const struct flash_area *fap;
332 uint32_t off;
333 int area_id;
334 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300335 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700336 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300337 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300339 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100340 * the trailer. Since in the last step the primary slot is erased, the
341 * first two status writes go to the scratch which will be copied to
342 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300343 */
344
Fabio Utzig12d59162019-11-28 10:01:59 -0300345#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300346 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347 /* Write to scratch. */
348 area_id = FLASH_AREA_IMAGE_SCRATCH;
349 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300350#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100351 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300353#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300355#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800356
357 rc = flash_area_open(area_id, &fap);
358 if (rc != 0) {
359 rc = BOOT_EFLASH;
360 goto done;
361 }
362
363 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300364 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200365 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300366 erased_val = flash_area_erased_val(fap);
367 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700368 buf[0] = bs->state;
369
370 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 if (rc != 0) {
372 rc = BOOT_EFLASH;
373 goto done;
374 }
375
376 rc = 0;
377
378done:
379 flash_area_close(fap);
380 return rc;
381}
David Vinczee574f2d2020-07-10 11:42:03 +0200382#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383
384/*
David Vinczec3084132020-02-18 14:50:47 +0100385 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386 */
387static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300388boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
389 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300391 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300392 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300393 int rc;
394
Fabio Utzig10ee6482019-08-01 12:04:52 -0300395#if (BOOT_IMAGE_NUMBER == 1)
396 (void)state;
397#endif
398
Fabio Utzigba829042018-09-18 08:29:34 -0300399 (void)bs;
400 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300401
402 image_index = BOOT_CURR_IMG(state);
403
404#ifdef MCUBOOT_ENC_IMAGES
405 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300406 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300407 if (rc < 0) {
408 return BOOT_EBADIMAGE;
409 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300410 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300411 return BOOT_EBADIMAGE;
412 }
413 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300414#endif
415
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300416 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300417 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800418 return BOOT_EBADIMAGE;
419 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300420
Christopher Collins92ea77f2016-12-12 15:59:26 -0800421 return 0;
422}
423
David Vinczee574f2d2020-07-10 11:42:03 +0200424#ifndef MCUBOOT_DIRECT_XIP
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425static int
426split_image_check(struct image_header *app_hdr,
427 const struct flash_area *app_fap,
428 struct image_header *loader_hdr,
429 const struct flash_area *loader_fap)
430{
431 static void *tmpbuf;
432 uint8_t loader_hash[32];
433
434 if (!tmpbuf) {
435 tmpbuf = malloc(BOOT_TMPBUF_SZ);
436 if (!tmpbuf) {
437 return BOOT_ENOMEM;
438 }
439 }
440
Fabio Utzig10ee6482019-08-01 12:04:52 -0300441 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
442 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443 return BOOT_EBADIMAGE;
444 }
445
Fabio Utzig10ee6482019-08-01 12:04:52 -0300446 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
447 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448 return BOOT_EBADIMAGE;
449 }
450
451 return 0;
452}
David Vinczee574f2d2020-07-10 11:42:03 +0200453#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454
Fabio Utzig338a19f2018-12-03 08:37:08 -0200455/*
David Brown9bf95af2019-10-10 15:36:36 -0600456 * Check that this is a valid header. Valid means that the magic is
457 * correct, and that the sizes/offsets are "sane". Sane means that
458 * there is no overflow on the arithmetic, and that the result fits
459 * within the flash area we are in.
460 */
461static bool
462boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
463{
464 uint32_t size;
465
466 if (hdr->ih_magic != IMAGE_MAGIC) {
467 return false;
468 }
469
470 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
471 return false;
472 }
473
474 if (size >= fap->fa_size) {
475 return false;
476 }
477
478 return true;
479}
480
481/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200482 * Check that a memory area consists of a given value.
483 */
484static inline bool
485boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300486{
487 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200488 uint8_t *p = (uint8_t *)data;
489 for (i = 0; i < len; i++) {
490 if (val != p[i]) {
491 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300492 }
493 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200494 return true;
495}
496
497static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300498boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200499{
500 const struct flash_area *fap;
501 struct image_header *hdr;
502 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300503 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200504 int rc;
505
Fabio Utzig10ee6482019-08-01 12:04:52 -0300506 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300507 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200508 if (rc != 0) {
509 return -1;
510 }
511
512 erased_val = flash_area_erased_val(fap);
513 flash_area_close(fap);
514
Fabio Utzig10ee6482019-08-01 12:04:52 -0300515 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200516 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
517 return -1;
518 }
519
520 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300521}
522
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000523#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200524 defined(MCUBOOT_DIRECT_XIP) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000525 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
526/**
David Vincze8b0b6372020-05-20 19:54:44 +0200527 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000528 *
David Vincze8b0b6372020-05-20 19:54:44 +0200529 * @param ver1 Pointer to the first image version to compare.
530 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000531 *
David Vincze8b0b6372020-05-20 19:54:44 +0200532 * @retval -1 If ver1 is strictly less than ver2.
533 * @retval 0 If the image version numbers are equal,
534 * (not including the build number).
535 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000536 */
537static int
David Vincze8b0b6372020-05-20 19:54:44 +0200538boot_version_cmp(const struct image_version *ver1,
539 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000540{
David Vincze8b0b6372020-05-20 19:54:44 +0200541 if (ver1->iv_major > ver2->iv_major) {
542 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000543 }
David Vincze8b0b6372020-05-20 19:54:44 +0200544 if (ver1->iv_major < ver2->iv_major) {
545 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000546 }
David Vincze8b0b6372020-05-20 19:54:44 +0200547 /* The major version numbers are equal, continue comparison. */
548 if (ver1->iv_minor > ver2->iv_minor) {
549 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000550 }
David Vincze8b0b6372020-05-20 19:54:44 +0200551 if (ver1->iv_minor < ver2->iv_minor) {
552 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000553 }
David Vincze8b0b6372020-05-20 19:54:44 +0200554 /* The minor version numbers are equal, continue comparison. */
555 if (ver1->iv_revision > ver2->iv_revision) {
556 return 1;
557 }
558 if (ver1->iv_revision < ver2->iv_revision) {
559 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000560 }
561
562 return 0;
563}
564#endif
565
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300566/*
567 * Check that there is a valid image in a slot
568 *
569 * @returns
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300570 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300571 * 1 if no bootloable image was found
572 * -1 on any errors
573 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300575boot_validate_slot(struct boot_loader_state *state, int slot,
576 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800577{
578 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400579 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300580 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300582
Fabio Utzig10ee6482019-08-01 12:04:52 -0300583 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300584 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300586 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 }
588
Fabio Utzig10ee6482019-08-01 12:04:52 -0300589 hdr = boot_img_hdr(state, slot);
590 if (boot_check_header_erased(state, slot) == 0 ||
591 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300592
593#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
594 /*
595 * This fixes an issue where an image might be erased, but a trailer
596 * be left behind. It can happen if the image is in the secondary slot
597 * and did not pass validation, in which case the whole slot is erased.
598 * If during the erase operation, a reset occurs, parts of the slot
599 * might have been erased while some did not. The concerning part is
600 * the trailer because it might disable a new image from being loaded
601 * through mcumgr; so we just get rid of the trailer here, if the header
602 * is erased.
603 */
604 if (slot != BOOT_PRIMARY_SLOT) {
605 swap_erase_trailer_sectors(state, fap);
606 }
607#endif
608
David Vincze2d736ad2019-02-18 11:50:22 +0100609 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300610 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200611 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300612 }
613
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000614#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
615 if (slot != BOOT_PRIMARY_SLOT) {
616 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200617 rc = boot_version_cmp(
618 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
619 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
620 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000621 BOOT_LOG_ERR("insufficient version in secondary slot");
622 flash_area_erase(fap, 0, fap->fa_size);
623 /* Image in the secondary slot does not satisfy version requirement.
624 * Erase the image and continue booting from the primary slot.
625 */
626 rc = 1;
627 goto out;
628 }
629 }
630#endif
631
David Brown9bf95af2019-10-10 15:36:36 -0600632 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
David Vinczee574f2d2020-07-10 11:42:03 +0200633 if ((slot != BOOT_PRIMARY_SLOT) || IS_IN_XIP_MODE()) {
David Brownb38e0442017-02-24 13:57:12 -0700634 flash_area_erase(fap, 0, fap->fa_size);
David Vinczee574f2d2020-07-10 11:42:03 +0200635 /* Image is invalid, erase it to prevent further unnecessary
636 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700637 */
638 }
David Brown098de832019-12-10 11:58:01 -0700639#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100640 BOOT_LOG_ERR("Image in the %s slot is not valid!",
641 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700642#endif
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000643 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200644 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800645 }
646
David Vincze2d736ad2019-02-18 11:50:22 +0100647 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200648 rc = 0;
649
650out:
651 flash_area_close(fap);
652 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653}
654
David Vinczec3084132020-02-18 14:50:47 +0100655#ifdef MCUBOOT_HW_ROLLBACK_PROT
656/**
657 * Updates the stored security counter value with the image's security counter
658 * value which resides in the given slot, only if it's greater than the stored
659 * value.
660 *
661 * @param image_index Index of the image to determine which security
662 * counter to update.
663 * @param slot Slot number of the image.
664 * @param hdr Pointer to the image header structure of the image
665 * that is currently stored in the given slot.
666 *
667 * @return 0 on success; nonzero on failure.
668 */
669static int
670boot_update_security_counter(uint8_t image_index, int slot,
671 struct image_header *hdr)
672{
673 const struct flash_area *fap = NULL;
674 uint32_t img_security_cnt;
675 int rc;
676
677 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
678 &fap);
679 if (rc != 0) {
680 rc = BOOT_EFLASH;
681 goto done;
682 }
683
684 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
685 if (rc != 0) {
686 goto done;
687 }
688
689 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
690 if (rc != 0) {
691 goto done;
692 }
693
694done:
695 flash_area_close(fap);
696 return rc;
697}
698#endif /* MCUBOOT_HW_ROLLBACK_PROT */
699
David Vinczee574f2d2020-07-10 11:42:03 +0200700#ifndef MCUBOOT_DIRECT_XIP
701/**
702 * Determines which swap operation to perform, if any. If it is determined
703 * that a swap operation is required, the image in the secondary slot is checked
704 * for validity. If the image in the secondary slot is invalid, it is erased,
705 * and a swap type of "none" is indicated.
706 *
707 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
708 */
709static int
710boot_validated_swap_type(struct boot_loader_state *state,
711 struct boot_status *bs)
712{
713 int swap_type;
714 int rc;
715
716 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
717 if (BOOT_IS_UPGRADE(swap_type)) {
718 /* Boot loader wants to switch to the secondary slot.
719 * Ensure image is valid.
720 */
721 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
722 if (rc == 1) {
723 swap_type = BOOT_SWAP_TYPE_NONE;
724 } else if (rc != 0) {
725 swap_type = BOOT_SWAP_TYPE_FAIL;
726 }
727 }
728
729 return swap_type;
730}
731
Christopher Collins92ea77f2016-12-12 15:59:26 -0800732/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733 * Erases a region of flash.
734 *
Fabio Utzigba829042018-09-18 08:29:34 -0300735 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800736 * @param off The offset within the flash area to start the
737 * erase.
738 * @param sz The number of bytes to erase.
739 *
740 * @return 0 on success; nonzero on failure.
741 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300742int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300743boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800744{
Fabio Utzigba829042018-09-18 08:29:34 -0300745 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800746}
747
748/**
749 * Copies the contents of one flash region to another. You must erase the
750 * destination region prior to calling this function.
751 *
752 * @param flash_area_id_src The ID of the source flash area.
753 * @param flash_area_id_dst The ID of the destination flash area.
754 * @param off_src The offset within the source flash area to
755 * copy from.
756 * @param off_dst The offset within the destination flash area to
757 * copy to.
758 * @param sz The number of bytes to copy.
759 *
760 * @return 0 on success; nonzero on failure.
761 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300762int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300763boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300764 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300765 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800766 uint32_t off_src, uint32_t off_dst, uint32_t sz)
767{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800768 uint32_t bytes_copied;
769 int chunk_sz;
770 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300771#ifdef MCUBOOT_ENC_IMAGES
772 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300773 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300774 size_t blk_off;
775 struct image_header *hdr;
776 uint16_t idx;
777 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300778 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300779#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800780
Fabio Utzig10ee6482019-08-01 12:04:52 -0300781 TARGET_STATIC uint8_t buf[1024];
782
783#if !defined(MCUBOOT_ENC_IMAGES)
784 (void)state;
785#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800786
Christopher Collins92ea77f2016-12-12 15:59:26 -0800787 bytes_copied = 0;
788 while (bytes_copied < sz) {
789 if (sz - bytes_copied > sizeof buf) {
790 chunk_sz = sizeof buf;
791 } else {
792 chunk_sz = sz - bytes_copied;
793 }
794
795 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
796 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300797 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800798 }
799
Fabio Utzigba829042018-09-18 08:29:34 -0300800#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300801 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300802 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
803 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
804 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
805 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100806 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300807 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300808#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300809 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300810 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100811 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300812 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300813 off = off_dst;
814 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300815#else
816 off = off_dst;
817 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
818 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
819 }
820#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200821 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300822 blk_sz = chunk_sz;
823 idx = 0;
824 if (off + bytes_copied < hdr->ih_hdr_size) {
825 /* do not decrypt header */
826 blk_off = 0;
827 blk_sz = chunk_sz - hdr->ih_hdr_size;
828 idx = hdr->ih_hdr_size;
829 } else {
830 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
831 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300832 tlv_off = BOOT_TLV_OFF(hdr);
833 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300834 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300835 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300836 blk_sz = 0;
837 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300838 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300839 }
840 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300841 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300842 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
843 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300844 }
845 }
846#endif
847
Christopher Collins92ea77f2016-12-12 15:59:26 -0800848 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
849 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300850 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800851 }
852
853 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300854
855 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800856 }
857
Fabio Utzigba829042018-09-18 08:29:34 -0300858 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859}
860
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861/**
David Vincze2d736ad2019-02-18 11:50:22 +0100862 * Overwrite primary slot with the image contained in the secondary slot.
863 * If a prior copy operation was interrupted by a system reset, this function
864 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800865 *
866 * @param bs The current boot status. This function reads
867 * this struct to determine if it is resuming
868 * an interrupted swap operation. This
869 * function writes the updated status to this
870 * function on return.
871 *
872 * @return 0 on success; nonzero on failure.
873 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200874#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600875static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300876boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600877{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400878 size_t sect_count;
879 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600880 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300881 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400882 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300883 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100884 const struct flash_area *fap_primary_slot;
885 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300886 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100887
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200888 (void)bs;
889
Fabio Utzig13d9e352017-10-05 20:32:31 -0300890#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
891 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300892 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300893 assert(rc == 0);
894#endif
David Brown17609d82017-05-05 09:41:34 -0600895
David Vincze2d736ad2019-02-18 11:50:22 +0100896 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
897 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600898
Fabio Utzigb0f04732019-07-31 09:49:19 -0300899 image_index = BOOT_CURR_IMG(state);
900
901 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
902 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300903 assert (rc == 0);
904
Fabio Utzigb0f04732019-07-31 09:49:19 -0300905 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
906 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300907 assert (rc == 0);
908
Fabio Utzig10ee6482019-08-01 12:04:52 -0300909 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300910 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300911 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300912 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600913 assert(rc == 0);
914
915 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300916
917#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
918 if (size >= src_size) {
919 break;
920 }
921#endif
David Brown17609d82017-05-05 09:41:34 -0600922 }
923
Fabio Utzigba829042018-09-18 08:29:34 -0300924#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300925 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300926 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300927 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300928 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100929
Fabio Utzigba829042018-09-18 08:29:34 -0300930 if (rc < 0) {
931 return BOOT_EBADIMAGE;
932 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300933 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300934 return BOOT_EBADIMAGE;
935 }
936 }
937#endif
938
David Vincze2d736ad2019-02-18 11:50:22 +0100939 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
940 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300941 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600942
David Vinczec3084132020-02-18 14:50:47 +0100943#ifdef MCUBOOT_HW_ROLLBACK_PROT
944 /* Update the stored security counter with the new image's security counter
945 * value. Both slots hold the new image at this point, but the secondary
946 * slot's image header must be passed since the image headers in the
947 * boot_data structure have not been updated yet.
948 */
949 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
950 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
951 if (rc != 0) {
952 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
953 return rc;
954 }
955#endif /* MCUBOOT_HW_ROLLBACK_PROT */
956
Fabio Utzig13d9e352017-10-05 20:32:31 -0300957 /*
958 * Erases header and trailer. The trailer is erased because when a new
959 * image is written without a trailer as is the case when using newt, the
960 * trailer that was left might trigger a new upgrade.
961 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700962 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300963 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300964 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
965 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600966 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300967 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700968 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300969 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300970 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
971 last_sector),
972 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
973 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300974 assert(rc == 0);
975
David Vincze2d736ad2019-02-18 11:50:22 +0100976 flash_area_close(fap_primary_slot);
977 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300978
David Vincze2d736ad2019-02-18 11:50:22 +0100979 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600980
981 return 0;
982}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200983#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300984
Christopher Collinsa1c12042019-05-23 14:00:28 -0700985#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300986/**
987 * Swaps the two images in flash. If a prior copy operation was interrupted
988 * by a system reset, this function completes that operation.
989 *
990 * @param bs The current boot status. This function reads
991 * this struct to determine if it is resuming
992 * an interrupted swap operation. This
993 * function writes the updated status to this
994 * function on return.
995 *
996 * @return 0 on success; nonzero on failure.
997 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800998static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300999boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001000{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001001 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001002#ifdef MCUBOOT_ENC_IMAGES
1003 const struct flash_area *fap;
1004 uint8_t slot;
1005 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001006#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001007 uint32_t size;
1008 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001009 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001010 int rc;
1011
1012 /* FIXME: just do this if asked by user? */
1013
1014 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001015 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001016
Fabio Utzig12d59162019-11-28 10:01:59 -03001017 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001018 /*
1019 * No swap ever happened, so need to find the largest image which
1020 * will be used to determine the amount of sectors to swap.
1021 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001022 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001023 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001024 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001025 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001026 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001027
Fabio Utzigba829042018-09-18 08:29:34 -03001028#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001029 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001030 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001031 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001032 assert(rc >= 0);
1033
1034 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001035 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001036 assert(rc == 0);
1037 } else {
1038 rc = 0;
1039 }
1040 } else {
1041 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1042 }
1043#endif
1044
Fabio Utzig10ee6482019-08-01 12:04:52 -03001045 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001046 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001047 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001048 assert(rc == 0);
1049 }
1050
Fabio Utzigba829042018-09-18 08:29:34 -03001051#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001052 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001053 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001054 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001055 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001056 assert(rc >= 0);
1057
1058 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001059 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001060 assert(rc == 0);
1061 } else {
1062 rc = 0;
1063 }
1064 } else {
1065 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1066 }
1067#endif
1068
Fabio Utzig46490722017-09-04 15:34:32 -03001069 if (size > copy_size) {
1070 copy_size = size;
1071 }
1072
1073 bs->swap_size = copy_size;
1074 } else {
1075 /*
1076 * If a swap was under way, the swap_size should already be present
1077 * in the trailer...
1078 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001079 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001080 assert(rc == 0);
1081
1082 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001083
1084#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001085 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1086 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001087 assert(rc == 0);
1088
1089 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001090 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001091 break;
1092 }
1093 }
1094
1095 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001096 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001097 }
1098 }
1099#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001100 }
1101
Fabio Utzig12d59162019-11-28 10:01:59 -03001102 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001103
David Vincze2d736ad2019-02-18 11:50:22 +01001104#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001105 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001106 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001107 BOOT_LOG_WRN("%d status write fails performing the swap",
1108 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001109 }
1110#endif
1111
Christopher Collins92ea77f2016-12-12 15:59:26 -08001112 return 0;
1113}
David Brown17609d82017-05-05 09:41:34 -06001114#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001115
David Vinczee32483f2019-06-13 10:46:24 +02001116#if (BOOT_IMAGE_NUMBER > 1)
1117/**
1118 * Check the image dependency whether it is satisfied and modify
1119 * the swap type if necessary.
1120 *
1121 * @param dep Image dependency which has to be verified.
1122 *
1123 * @return 0 on success; nonzero on failure.
1124 */
1125static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001126boot_verify_slot_dependency(struct boot_loader_state *state,
1127 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001128{
1129 struct image_version *dep_version;
1130 size_t dep_slot;
1131 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001132 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001133
1134 /* Determine the source of the image which is the subject of
1135 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001136 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001137 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1138 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001139 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001140
David Vincze8b0b6372020-05-20 19:54:44 +02001141 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1142 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001143 /* Dependency not satisfied.
1144 * Modify the swap type to decrease the version number of the image
1145 * (which will be located in the primary slot after the boot process),
1146 * consequently the number of unsatisfied dependencies will be
1147 * decreased or remain the same.
1148 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001149 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001150 case BOOT_SWAP_TYPE_TEST:
1151 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001152 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001153 break;
1154 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001155 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001156 break;
1157 default:
1158 break;
1159 }
David Vincze8b0b6372020-05-20 19:54:44 +02001160 } else {
1161 /* Dependency satisfied. */
1162 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001163 }
1164
1165 return rc;
1166}
1167
1168/**
1169 * Read all dependency TLVs of an image from the flash and verify
1170 * one after another to see if they are all satisfied.
1171 *
1172 * @param slot Image slot number.
1173 *
1174 * @return 0 on success; nonzero on failure.
1175 */
1176static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001177boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001178{
1179 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001180 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001181 struct image_dependency dep;
1182 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001183 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001184 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001185 int rc;
1186
Fabio Utzig10ee6482019-08-01 12:04:52 -03001187 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001188 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001189 if (rc != 0) {
1190 rc = BOOT_EFLASH;
1191 goto done;
1192 }
1193
Fabio Utzig61fd8882019-09-14 20:00:20 -03001194 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1195 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001196 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001197 goto done;
1198 }
1199
Fabio Utzig61fd8882019-09-14 20:00:20 -03001200 while (true) {
1201 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1202 if (rc < 0) {
1203 return -1;
1204 } else if (rc > 0) {
1205 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001206 break;
1207 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001208
1209 if (len != sizeof(dep)) {
1210 rc = BOOT_EBADIMAGE;
1211 goto done;
1212 }
1213
1214 rc = flash_area_read(fap, off, &dep, len);
1215 if (rc != 0) {
1216 rc = BOOT_EFLASH;
1217 goto done;
1218 }
1219
1220 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1221 rc = BOOT_EBADARGS;
1222 goto done;
1223 }
1224
1225 /* Verify dependency and modify the swap type if not satisfied. */
1226 rc = boot_verify_slot_dependency(state, &dep);
1227 if (rc != 0) {
1228 /* Dependency not satisfied. */
1229 goto done;
1230 }
David Vinczee32483f2019-06-13 10:46:24 +02001231 }
1232
1233done:
1234 flash_area_close(fap);
1235 return rc;
1236}
1237
1238/**
David Vinczee32483f2019-06-13 10:46:24 +02001239 * Iterate over all the images and verify whether the image dependencies in the
1240 * TLV area are all satisfied and update the related swap type if necessary.
1241 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001242static int
1243boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001244{
Erik Johnson49063752020-02-06 09:59:31 -06001245 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001246 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001247
Fabio Utzig10ee6482019-08-01 12:04:52 -03001248 BOOT_CURR_IMG(state) = 0;
1249 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001250 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1251 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1252 slot = BOOT_SECONDARY_SLOT;
1253 } else {
1254 slot = BOOT_PRIMARY_SLOT;
1255 }
1256
1257 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001258 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001259 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001260 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001261 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001262 /* Cannot upgrade due to non-met dependencies, so disable all
1263 * image upgrades.
1264 */
1265 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1266 BOOT_CURR_IMG(state) = idx;
1267 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1268 }
1269 break;
David Vinczee32483f2019-06-13 10:46:24 +02001270 }
1271 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001272 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001273}
1274#endif /* (BOOT_IMAGE_NUMBER > 1) */
1275
Christopher Collins92ea77f2016-12-12 15:59:26 -08001276/**
David Vinczeba3bd602019-06-17 16:01:43 +02001277 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001278 *
David Vinczeba3bd602019-06-17 16:01:43 +02001279 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001280 *
1281 * @return 0 on success; nonzero on failure.
1282 */
1283static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001284boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001285{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001286 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001287#ifndef MCUBOOT_OVERWRITE_ONLY
1288 uint8_t swap_type;
1289#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001290
David Vinczeba3bd602019-06-17 16:01:43 +02001291 /* At this point there are no aborted swaps. */
1292#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001293 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001294#elif defined(MCUBOOT_BOOTSTRAP)
1295 /* Check if the image update was triggered by a bad image in the
1296 * primary slot (the validity of the image in the secondary slot had
1297 * already been checked).
1298 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001299 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1300 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1301 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001302 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001303 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001304 }
1305#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001306 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001307#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001308 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001309
1310#ifndef MCUBOOT_OVERWRITE_ONLY
1311 /* The following state needs image_ok be explicitly set after the
1312 * swap was finished to avoid a new revert.
1313 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001314 swap_type = BOOT_SWAP_TYPE(state);
1315 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1316 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001317 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001318 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001319 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001320 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001321 }
1322
David Vinczec3084132020-02-18 14:50:47 +01001323#ifdef MCUBOOT_HW_ROLLBACK_PROT
1324 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1325 /* Update the stored security counter with the new image's security
1326 * counter value. The primary slot holds the new image at this point,
1327 * but the secondary slot's image header must be passed since image
1328 * headers in the boot_data structure have not been updated yet.
1329 *
1330 * In case of a permanent image swap mcuboot will never attempt to
1331 * revert the images on the next reboot. Therefore, the security
1332 * counter must be increased right after the image upgrade.
1333 */
1334 rc = boot_update_security_counter(
1335 BOOT_CURR_IMG(state),
1336 BOOT_PRIMARY_SLOT,
1337 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1338 if (rc != 0) {
1339 BOOT_LOG_ERR("Security counter update failed after "
1340 "image upgrade.");
1341 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1342 }
1343 }
1344#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1345
Fabio Utzige575b0b2019-09-11 12:34:23 -03001346 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001347 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001348 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001349 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001350 }
David Vinczeba3bd602019-06-17 16:01:43 +02001351 }
1352#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001353
David Vinczeba3bd602019-06-17 16:01:43 +02001354 return rc;
1355}
1356
1357/**
1358 * Completes a previously aborted image swap.
1359 *
1360 * @param bs The current boot status.
1361 *
1362 * @return 0 on success; nonzero on failure.
1363 */
1364#if !defined(MCUBOOT_OVERWRITE_ONLY)
1365static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001366boot_complete_partial_swap(struct boot_loader_state *state,
1367 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001368{
1369 int rc;
1370
1371 /* Determine the type of swap operation being resumed from the
1372 * `swap-type` trailer field.
1373 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001374 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001375 assert(rc == 0);
1376
Fabio Utzig10ee6482019-08-01 12:04:52 -03001377 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001378
1379 /* The following states need image_ok be explicitly set after the
1380 * swap was finished to avoid a new revert.
1381 */
1382 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1383 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001384 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001385 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001386 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001387 }
1388 }
1389
Fabio Utzige575b0b2019-09-11 12:34:23 -03001390 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001391 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001392 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001393 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001394 }
1395 }
1396
Fabio Utzig10ee6482019-08-01 12:04:52 -03001397 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001398 BOOT_LOG_ERR("panic!");
1399 assert(0);
1400
1401 /* Loop forever... */
1402 while (1) {}
1403 }
1404
1405 return rc;
1406}
1407#endif /* !MCUBOOT_OVERWRITE_ONLY */
1408
1409#if (BOOT_IMAGE_NUMBER > 1)
1410/**
1411 * Review the validity of previously determined swap types of other images.
1412 *
1413 * @param aborted_swap The current image upgrade is a
1414 * partial/aborted swap.
1415 */
1416static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001417boot_review_image_swap_types(struct boot_loader_state *state,
1418 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001419{
1420 /* In that case if we rebooted in the middle of an image upgrade process, we
1421 * must review the validity of swap types, that were previously determined
1422 * for other images. The image_ok flag had not been set before the reboot
1423 * for any of the updated images (only the copy_done flag) and thus falsely
1424 * the REVERT swap type has been determined for the previous images that had
1425 * been updated before the reboot.
1426 *
1427 * There are two separate scenarios that we have to deal with:
1428 *
1429 * 1. The reboot has happened during swapping an image:
1430 * The current image upgrade has been determined as a
1431 * partial/aborted swap.
1432 * 2. The reboot has happened between two separate image upgrades:
1433 * In this scenario we must check the swap type of the current image.
1434 * In those cases if it is NONE or REVERT we cannot certainly determine
1435 * the fact of a reboot. In a consistent state images must move in the
1436 * same direction or stay in place, e.g. in practice REVERT and TEST
1437 * swap types cannot be present at the same time. If the swap type of
1438 * the current image is either TEST, PERM or FAIL we must review the
1439 * already determined swap types of other images and set each false
1440 * REVERT swap types to NONE (these images had been successfully
1441 * updated before the system rebooted between two separate image
1442 * upgrades).
1443 */
1444
Fabio Utzig10ee6482019-08-01 12:04:52 -03001445 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001446 /* Nothing to do */
1447 return;
1448 }
1449
1450 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001451 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1452 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001453 /* Nothing to do */
1454 return;
1455 }
1456 }
1457
Fabio Utzig10ee6482019-08-01 12:04:52 -03001458 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1459 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1460 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001461 }
1462 }
1463}
1464#endif
1465
1466/**
1467 * Prepare image to be updated if required.
1468 *
1469 * Prepare image to be updated if required with completing an image swap
1470 * operation if one was aborted and/or determining the type of the
1471 * swap operation. In case of any error set the swap type to NONE.
1472 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001473 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001474 * @param bs Pointer where the read and possibly updated
1475 * boot status can be written to.
1476 */
1477static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001478boot_prepare_image_for_update(struct boot_loader_state *state,
1479 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001480{
1481 int rc;
1482
1483 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001484 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001485 if (rc != 0) {
1486 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1487 " - too small?", BOOT_MAX_IMG_SECTORS);
1488 /* Unable to determine sector layout, continue with next image
1489 * if there is one.
1490 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001491 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001492 return;
1493 }
1494
1495 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001496 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001497 if (rc != 0) {
1498 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001499 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001500 BOOT_CURR_IMG(state));
1501 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001502 return;
1503 }
1504
1505 /* If the current image's slots aren't compatible, no swap is possible.
1506 * Just boot into primary slot.
1507 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001508 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001509 boot_status_reset(bs);
1510
1511#ifndef MCUBOOT_OVERWRITE_ONLY
1512 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001513 if (rc != 0) {
1514 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001515 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001516 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001517 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001518 return;
1519 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001520#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001521
Fabio Utzig74aef312019-11-28 11:05:34 -03001522#ifdef MCUBOOT_SWAP_USING_MOVE
1523 /*
1524 * Must re-read image headers because the boot status might
1525 * have been updated in the previous function call.
1526 */
1527 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1528 if (rc != 0) {
1529 /* Continue with next image if there is one. */
1530 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1531 BOOT_CURR_IMG(state));
1532 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1533 return;
1534 }
1535#endif
1536
David Vinczeba3bd602019-06-17 16:01:43 +02001537 /* Determine if we rebooted in the middle of an image swap
1538 * operation. If a partial swap was detected, complete it.
1539 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001540 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001541
1542#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001544#endif
1545
1546#ifdef MCUBOOT_OVERWRITE_ONLY
1547 /* Should never arrive here, overwrite-only mode has
1548 * no swap state.
1549 */
1550 assert(0);
1551#else
1552 /* Determine the type of swap operation being resumed from the
1553 * `swap-type` trailer field.
1554 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001555 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001556 assert(rc == 0);
1557#endif
1558 /* Attempt to read an image header from each slot. Ensure that
1559 * image headers in slots are aligned with headers in boot_data.
1560 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001561 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001562 assert(rc == 0);
1563
1564 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001565 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001566 } else {
1567 /* There was no partial swap, determine swap type. */
1568 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001569 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1570 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1571 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001572 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001573 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001574 }
1575
1576#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001577 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001578#endif
1579
1580#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001581 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001582 /* Header checks are done first because they are
1583 * inexpensive. Since overwrite-only copies starting from
1584 * offset 0, if interrupted, it might leave a valid header
1585 * magic, so also run validation on the primary slot to be
1586 * sure it's not OK.
1587 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001588 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1589 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1590 if (boot_img_hdr(state,
1591 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1592 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001593 /* Set swap type to REVERT to overwrite the primary
1594 * slot with the image contained in secondary slot
1595 * and to trigger the explicit setting of the
1596 * image_ok flag.
1597 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001598 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001599 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001600 }
1601 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001602#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001603 }
David Vinczeba3bd602019-06-17 16:01:43 +02001604 } else {
1605 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001606 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001607 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001608}
1609
Christopher Collins92ea77f2016-12-12 15:59:26 -08001610int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001611context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001612{
Marti Bolivar84898652017-06-13 17:20:22 -04001613 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001614 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001615 int rc = -1;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001616 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001617 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001618 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001619
1620 /* The array of slot sectors are defined here (as opposed to file scope) so
1621 * that they don't get allocated for non-boot-loader apps. This is
1622 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001623 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001624 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001625 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1626 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001627#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001628 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001629#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001630
Fabio Utzig10ee6482019-08-01 12:04:52 -03001631 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001632 has_upgrade = false;
1633
1634#if (BOOT_IMAGE_NUMBER == 1)
1635 (void)has_upgrade;
1636#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001637
David Vinczeba3bd602019-06-17 16:01:43 +02001638 /* Iterate over all the images. By the end of the loop the swap type has
1639 * to be determined for each image and all aborted swaps have to be
1640 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001641 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001642 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001643
David Vinczeba3bd602019-06-17 16:01:43 +02001644#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1645 /* The keys used for encryption may no longer be valid (could belong to
1646 * another images). Therefore, mark them as invalid to force their reload
1647 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001648 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001649 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001650#endif
1651
Fabio Utzig10ee6482019-08-01 12:04:52 -03001652 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001653
Fabio Utzig10ee6482019-08-01 12:04:52 -03001654 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001655 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001656 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001657 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001658#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001659 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001660#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001661
1662 /* Open primary and secondary image areas for the duration
1663 * of this call.
1664 */
1665 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001666 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001667 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001668 assert(rc == 0);
1669 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001670#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001671 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001672 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001673 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001674#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001675
1676 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001677 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001678
Fabio Utzige575b0b2019-09-11 12:34:23 -03001679 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001680 has_upgrade = true;
1681 }
David Vinczeba3bd602019-06-17 16:01:43 +02001682 }
1683
David Vinczee32483f2019-06-13 10:46:24 +02001684#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001685 if (has_upgrade) {
1686 /* Iterate over all the images and verify whether the image dependencies
1687 * are all satisfied and update swap type if necessary.
1688 */
1689 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001690 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001691 /*
1692 * It was impossible to upgrade because the expected dependency version
1693 * was not available. Here we already changed the swap_type so that
1694 * instead of asserting the bootloader, we continue and no upgrade is
1695 * performed.
1696 */
1697 rc = 0;
1698 }
1699 }
David Vinczee32483f2019-06-13 10:46:24 +02001700#endif
1701
David Vinczeba3bd602019-06-17 16:01:43 +02001702 /* Iterate over all the images. At this point there are no aborted swaps
1703 * and the swap types are determined for each image. By the end of the loop
1704 * all required update operations will have been finished.
1705 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001706 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001707
1708#if (BOOT_IMAGE_NUMBER > 1)
1709#ifdef MCUBOOT_ENC_IMAGES
1710 /* The keys used for encryption may no longer be valid (could belong to
1711 * another images). Therefore, mark them as invalid to force their reload
1712 * by boot_enc_load().
1713 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001714 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001715#endif /* MCUBOOT_ENC_IMAGES */
1716
1717 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001718 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001719#endif /* (BOOT_IMAGE_NUMBER > 1) */
1720
1721 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001722 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001723
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001725 case BOOT_SWAP_TYPE_NONE:
1726 break;
1727
1728 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1729 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1730 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001731 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001732 assert(rc == 0);
1733 break;
1734
1735 case BOOT_SWAP_TYPE_FAIL:
1736 /* The image in secondary slot was invalid and is now erased. Ensure
1737 * we don't try to boot into it again on the next reboot. Do this by
1738 * pretending we just reverted back to primary slot.
1739 */
1740#ifndef MCUBOOT_OVERWRITE_ONLY
1741 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001742 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001743 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001744 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001745 }
1746#endif /* !MCUBOOT_OVERWRITE_ONLY */
1747 break;
1748
1749 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001750 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001751 }
1752
Fabio Utzig10ee6482019-08-01 12:04:52 -03001753 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001754 BOOT_LOG_ERR("panic!");
1755 assert(0);
1756
1757 /* Loop forever... */
1758 while (1) {}
1759 }
1760 }
1761
1762 /* Iterate over all the images. At this point all required update operations
1763 * have finished. By the end of the loop each image in the primary slot will
1764 * have been re-validated.
1765 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001766 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1767 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001768 /* Attempt to read an image header from each slot. Ensure that image
1769 * headers in slots are aligned with headers in boot_data.
1770 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001771 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001772 if (rc != 0) {
1773 goto out;
1774 }
1775 /* Since headers were reloaded, it can be assumed we just performed
1776 * a swap or overwrite. Now the header info that should be used to
1777 * provide the data for the bootstrap, which previously was at
1778 * secondary slot, was updated to primary slot.
1779 */
1780 }
1781
1782#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001783 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001784 if (rc != 0) {
1785 rc = BOOT_EBADIMAGE;
1786 goto out;
1787 }
1788#else
1789 /* Even if we're not re-validating the primary slot, we could be booting
1790 * onto an empty flash chip. At least do a basic sanity check that
1791 * the magic number on the image is OK.
1792 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001793 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001794 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001795 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1796 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001797 rc = BOOT_EBADIMAGE;
1798 goto out;
1799 }
David Vinczec3084132020-02-18 14:50:47 +01001800#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1801
1802#ifdef MCUBOOT_HW_ROLLBACK_PROT
1803 /* Update the stored security counter with the active image's security
1804 * counter value. It will only be updated if the new security counter is
1805 * greater than the stored value.
1806 *
1807 * In case of a successful image swapping when the swap type is TEST the
1808 * security counter can be increased only after a reset, when the swap
1809 * type is NONE and the image has marked itself "OK" (the image_ok flag
1810 * has been set). This way a "revert" can be performed when it's
1811 * necessary.
1812 */
1813 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1814 rc = boot_update_security_counter(
1815 BOOT_CURR_IMG(state),
1816 BOOT_PRIMARY_SLOT,
1817 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1818 if (rc != 0) {
1819 BOOT_LOG_ERR("Security counter update failed after image "
1820 "validation.");
1821 goto out;
1822 }
1823 }
1824#endif /* MCUBOOT_HW_ROLLBACK_PROT */
David Vincze1cf11b52020-03-24 07:51:09 +01001825
1826#ifdef MCUBOOT_MEASURED_BOOT
1827 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
1828 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1829 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1830 if (rc != 0) {
1831 BOOT_LOG_ERR("Failed to add Image %u data to shared memory area",
1832 BOOT_CURR_IMG(state));
1833 }
1834#endif /* MCUBOOT_MEASURED_BOOT */
1835
1836#ifdef MCUBOOT_DATA_SHARING
1837 rc = boot_save_shared_data(boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1838 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1839 if (rc != 0) {
1840 BOOT_LOG_ERR("Failed to add data to shared memory area.");
1841 }
1842#endif /* MCUBOOT_DATA_SHARING */
David Vinczeba3bd602019-06-17 16:01:43 +02001843 }
1844
Fabio Utzigb0f04732019-07-31 09:49:19 -03001845#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001846 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001847 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001848#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001849
Fabio Utzigf616c542019-12-19 15:23:32 -03001850 /*
1851 * Since the boot_status struct stores plaintext encryption keys, reset
1852 * them here to avoid the possibility of jumping into an image that could
1853 * easily recover them.
1854 */
1855 memset(&bs, 0, sizeof(struct boot_status));
1856
Fabio Utzig10ee6482019-08-01 12:04:52 -03001857 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1858 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1859 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001860
Fabio Utzig298913b2019-08-28 11:22:45 -03001861out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001862 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001863#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001864 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001865#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001866 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001867 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001868 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001869 }
1870 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001871}
1872
1873int
1874split_go(int loader_slot, int split_slot, void **entry)
1875{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001876 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001877 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001878 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001879 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001880 int rc;
1881
Christopher Collins92ea77f2016-12-12 15:59:26 -08001882 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1883 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001884 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001885 }
David Vinczeba3bd602019-06-17 16:01:43 +02001886 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1887 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001888
1889 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1890 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001891 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001892 assert(rc == 0);
1893 split_flash_id = flash_area_id_from_image_slot(split_slot);
1894 rc = flash_area_open(split_flash_id,
1895 &BOOT_IMG_AREA(&boot_data, split_slot));
1896 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001897
1898 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001899 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001900 if (rc != 0) {
1901 rc = SPLIT_GO_ERR;
1902 goto done;
1903 }
1904
Fabio Utzig12d59162019-11-28 10:01:59 -03001905 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001906 if (rc != 0) {
1907 goto done;
1908 }
1909
Christopher Collins92ea77f2016-12-12 15:59:26 -08001910 /* Don't check the bootable image flag because we could really call a
1911 * bootable or non-bootable image. Just validate that the image check
1912 * passes which is distinct from the normal check.
1913 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001914 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001915 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001916 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001917 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001918 if (rc != 0) {
1919 rc = SPLIT_GO_NON_MATCHING;
1920 goto done;
1921 }
1922
Marti Bolivarea088872017-06-12 17:10:49 -04001923 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001924 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001925 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001926 rc = SPLIT_GO_OK;
1927
1928done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001929 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1930 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001931 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001932 return rc;
1933}
David Vinczee574f2d2020-07-10 11:42:03 +02001934
1935#else /* MCUBOOT_DIRECT_XIP */
1936
1937/**
1938 * Iterates over all slots and determines which contain a firmware image.
1939 *
1940 * @param state Boot loader status information.
1941 * @param slot_usage Pointer to an array, which aim is to carry information
1942 * about slots that contain an image. After return the
1943 * corresponding array elements are set to a non-zero
1944 * value if the given slots are in use (contain a firmware
1945 * image), otherwise they are set to zero.
1946 * @param slot_cnt The number of slots, which can contain firmware images.
1947 * (Equal to or smaller than the size of the
1948 * slot_usage array.)
1949 *
1950 * @return The number of found images.
1951 */
1952static uint32_t
1953boot_get_slot_usage(struct boot_loader_state *state, uint8_t slot_usage[],
1954 uint32_t slot_cnt)
1955{
1956 struct image_header *hdr = NULL;
1957 uint32_t image_cnt = 0;
1958 uint32_t slot;
1959
1960 memset(slot_usage, 0, slot_cnt);
1961
1962 for (slot = 0; slot < slot_cnt; slot++) {
1963 hdr = boot_img_hdr(state, slot);
1964
1965 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
1966 slot_usage[slot] = 1;
1967 image_cnt++;
1968 BOOT_LOG_IMAGE_INFO(slot, hdr);
1969 } else {
1970 BOOT_LOG_INF("%s slot: Image not found", (slot == BOOT_PRIMARY_SLOT)
1971 ? "Primary" : "Secondary");
1972 }
1973 }
1974
1975 return image_cnt;
1976}
1977
1978int
1979context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
1980{
1981 struct image_header *hdr = NULL;
1982 struct image_header *selected_image_header = NULL;
1983 uint8_t slot_usage[BOOT_NUM_SLOTS];
1984 uint32_t selected_slot;
1985 uint32_t slot;
1986 uint32_t img_cnt;
1987 uint32_t i;
1988 int fa_id;
1989 int rc;
1990
1991 memset(state, 0, sizeof(struct boot_loader_state));
1992
1993 /* Open primary and secondary image areas for the duration
1994 * of this call.
1995 */
1996 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1997 fa_id = flash_area_id_from_image_slot(slot);
1998 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
1999 assert(rc == 0);
2000 }
2001
2002 /* Attempt to read an image header from each slot. */
2003 rc = boot_read_image_headers(state, false, NULL);
2004 if (rc != 0) {
2005 BOOT_LOG_WRN("Failed reading image headers.");
2006 goto out;
2007 }
2008
2009 img_cnt = boot_get_slot_usage(state, slot_usage,
2010 sizeof(slot_usage)/sizeof(slot_usage[0]));
2011
2012 if (img_cnt) {
2013 /* Select the newest and valid image. */
2014 for (i = 0; i < img_cnt; i++) {
2015 selected_slot = 0;
2016 selected_image_header = NULL;
2017
2018 /* Iterate over all the slots that are in use (contain an image)
2019 * and select the one that holds the newest image.
2020 */
2021 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2022 if (slot_usage[slot]) {
2023 hdr = boot_img_hdr(state, slot);
2024 if (selected_image_header != NULL) {
2025 rc = boot_version_cmp(&hdr->ih_ver,
2026 &selected_image_header->ih_ver);
2027 if (rc < 1) {
2028 /* The version of the image being examined wasn't
2029 * greater than the currently selected image's
2030 * version.
2031 */
2032 continue;
2033 }
2034 }
2035 selected_slot = slot;
2036 selected_image_header = hdr;
2037 }
2038 }
2039
2040 rc = boot_validate_slot(state, selected_slot, NULL);
2041 if (rc == 0) {
2042 /* If a valid image is found then there is no reason to check
2043 * the rest of the images, as each of them has a smaller version
2044 * number.
2045 */
2046 break;
2047 }
2048 /* The selected image is invalid, mark its slot as "unused"
2049 * and start over.
2050 */
2051 slot_usage[selected_slot] = 0;
2052 }
2053
2054 if (rc || (selected_image_header == NULL)) {
2055 /* If there was no valid image at all */
2056 rc = BOOT_EBADIMAGE;
2057 goto out;
2058 }
2059
2060#ifdef MCUBOOT_HW_ROLLBACK_PROT
2061 /* Update the stored security counter with the newer (active) image's
2062 * security counter value.
2063 */
2064 rc = boot_update_security_counter(0, selected_slot,
2065 selected_image_header);
2066 if (rc != 0) {
2067 BOOT_LOG_ERR("Security counter update failed after image "
2068 "validation.");
2069 goto out;
2070 }
2071#endif /* MCUBOOT_HW_ROLLBACK_PROT */
2072
2073#ifdef MCUBOOT_MEASURED_BOOT
2074 rc = boot_save_boot_status(0, selected_image_header,
2075 BOOT_IMG_AREA(state, selected_slot));
2076 if (rc != 0) {
2077 BOOT_LOG_ERR("Failed to add image data to shared area");
2078 }
2079#endif /* MCUBOOT_MEASURED_BOOT */
2080
2081#ifdef MCUBOOT_DATA_SHARING
2082 rc = boot_save_shared_data(selected_image_header,
2083 BOOT_IMG_AREA(state, selected_slot));
2084 if (rc != 0) {
2085 BOOT_LOG_ERR("Failed to add data to shared memory area.");
2086 }
2087#endif /* MCUBOOT_DATA_SHARING */
2088
2089 BOOT_LOG_INF("Booting image from the %s slot",
2090 (selected_slot == BOOT_PRIMARY_SLOT) ?
2091 "primary" : "secondary");
2092
2093 rsp->br_flash_dev_id =
2094 BOOT_IMG_AREA(state, selected_slot)->fa_device_id;
2095 rsp->br_image_off = boot_img_slot_off(state, selected_slot);
2096 rsp->br_hdr = selected_image_header;
2097 } else {
2098 /* No candidate image available */
2099 rc = BOOT_EBADIMAGE;
2100 goto out;
2101 }
2102
2103out:
2104 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2105 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
2106 }
2107 return rc;
2108}
2109#endif /* MCUBOOT_DIRECT_XIP */
2110
2111/**
2112 * Prepares the booting process. This function moves images around in flash as
2113 * appropriate, and tells you what address to boot from.
2114 *
2115 * @param rsp On success, indicates how booting should occur.
2116 *
2117 * @return 0 on success; nonzero on failure.
2118 */
2119int
2120boot_go(struct boot_rsp *rsp)
2121{
2122 return context_boot_go(&boot_data, rsp);
2123}