blob: e59fad78dbcefaffc978ac3d04a71ce1a4b124a3 [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
Mark Horvathccaf7f82021-01-04 18:16:42 +01006 * Copyright (c) 2019-2021 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
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
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060034#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "bootutil/bootutil.h"
Kristine Jassmann73c38c62021-02-03 16:56:14 +000039#include "bootutil/bootutil_public.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#include "bootutil/image.h"
41#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030042#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050043#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010044#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010045#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010046#include "bootutil/fault_injection_hardening.h"
Mark Horvathccaf7f82021-01-04 18:16:42 +010047#include "bootutil/ramload.h"
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020048#include "bootutil/boot_hooks.h"
Jamie McCrae56cb6102022-03-23 11:57:03 +000049#include "bootutil/mcuboot_status.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050050
Fabio Utzigba829042018-09-18 08:29:34 -030051#ifdef MCUBOOT_ENC_IMAGES
52#include "bootutil/enc_key.h"
53#endif
54
Carlos Falgueras García391b1972021-06-21 16:58:07 +020055#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
56#include <os/os_malloc.h>
57#endif
58
Fabio Utzigba1fbe62017-07-21 14:01:20 -030059#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030060
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020061BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010062
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040063static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080064
Fabio Utzigabec0732019-07-31 08:40:22 -030065#if (BOOT_IMAGE_NUMBER > 1)
66#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
67#else
68#define IMAGES_ITER(x)
69#endif
70
Fabio Utzig10ee6482019-08-01 12:04:52 -030071/*
72 * This macro allows some control on the allocation of local variables.
73 * When running natively on a target, we don't want to allocated huge
74 * variables on the stack, so make them global instead. For the simulator
75 * we want to run as many threads as there are tests, and it's safer
76 * to just make those variables stack allocated.
77 */
78#if !defined(__BOOTSIM__)
79#define TARGET_STATIC static
80#else
81#define TARGET_STATIC
82#endif
83
Kristine Jassmann73c38c62021-02-03 16:56:14 +000084#if BOOT_MAX_ALIGN > 1024
85#define BUF_SZ BOOT_MAX_ALIGN
86#else
87#define BUF_SZ 1024
88#endif
89
David Vinczee574f2d2020-07-10 11:42:03 +020090static int
91boot_read_image_headers(struct boot_loader_state *state, bool require_all,
92 struct boot_status *bs)
93{
94 int rc;
95 int i;
96
97 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020098 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
99 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
100 if (rc == BOOT_HOOK_REGULAR)
101 {
102 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
103 }
David Vinczee574f2d2020-07-10 11:42:03 +0200104 if (rc != 0) {
105 /* If `require_all` is set, fail on any single fail, otherwise
106 * if at least the first slot's header was read successfully,
107 * then the boot loader can attempt a boot.
108 *
109 * Failure to read any headers is a fatal error.
110 */
111 if (i > 0 && !require_all) {
112 return 0;
113 } else {
114 return rc;
115 }
116 }
117 }
118
119 return 0;
120}
121
Mark Horvathccaf7f82021-01-04 18:16:42 +0100122/**
123 * Saves boot status and shared data for current image.
124 *
125 * @param state Boot loader status information.
126 * @param active_slot Index of the slot will be loaded for current image.
127 *
128 * @return 0 on success; nonzero on failure.
129 */
130static int
131boot_add_shared_data(struct boot_loader_state *state,
132 uint32_t active_slot)
133{
134#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
135 int rc;
136
137#ifdef MCUBOOT_MEASURED_BOOT
138 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
139 boot_img_hdr(state, active_slot),
140 BOOT_IMG_AREA(state, active_slot));
141 if (rc != 0) {
142 BOOT_LOG_ERR("Failed to add image data to shared area");
143 return rc;
144 }
145#endif /* MCUBOOT_MEASURED_BOOT */
146
147#ifdef MCUBOOT_DATA_SHARING
148 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
149 BOOT_IMG_AREA(state, active_slot));
150 if (rc != 0) {
151 BOOT_LOG_ERR("Failed to add data to shared memory area.");
152 return rc;
153 }
154#endif /* MCUBOOT_DATA_SHARING */
155
156 return 0;
157
158#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
159 (void) (state);
160 (void) (active_slot);
161
162 return 0;
163#endif
164}
165
166/**
167 * Fills rsp to indicate how booting should occur.
168 *
169 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100170 * @param rsp boot_rsp struct to fill.
171 */
172static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100173fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100174{
175 uint32_t active_slot;
176
177#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100178 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100179 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100180 IMAGES_ITER(BOOT_CURR_IMG(state)) {
181 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
182 break;
183 }
184 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100185#endif
186
187#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100188 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100189#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100190 active_slot = BOOT_PRIMARY_SLOT;
191#endif
192
Dominik Ermel260ae092021-04-23 05:38:45 +0000193 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100194 rsp->br_image_off = boot_img_slot_off(state, active_slot);
195 rsp->br_hdr = boot_img_hdr(state, active_slot);
196}
197
198/**
199 * Closes all flash areas.
200 *
201 * @param state Boot loader status information.
202 */
203static void
204close_all_flash_areas(struct boot_loader_state *state)
205{
206 uint32_t slot;
207
208 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100209#if BOOT_IMAGE_NUMBER > 1
210 if (state->img_mask[BOOT_CURR_IMG(state)]) {
211 continue;
212 }
213#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100214#if MCUBOOT_SWAP_USING_SCRATCH
215 flash_area_close(BOOT_SCRATCH_AREA(state));
216#endif
217 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
218 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
219 }
220 }
221}
222
Tamas Banfe031092020-09-10 17:32:39 +0200223#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600224/*
225 * Compute the total size of the given image. Includes the size of
226 * the TLVs.
227 */
Tamas Banfe031092020-09-10 17:32:39 +0200228#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600229static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300230boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600231{
232 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300233 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300234 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300235 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600236 int area_id;
237 int rc;
238
Fabio Utzig10ee6482019-08-01 12:04:52 -0300239#if (BOOT_IMAGE_NUMBER == 1)
240 (void)state;
241#endif
242
243 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600244 rc = flash_area_open(area_id, &fap);
245 if (rc != 0) {
246 rc = BOOT_EFLASH;
247 goto done;
248 }
249
Fabio Utzig61fd8882019-09-14 20:00:20 -0300250 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
251
252 if (flash_area_read(fap, off, &info, sizeof(info))) {
253 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600254 goto done;
255 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300256
Fabio Utzige52c08e2019-09-11 19:32:00 -0300257 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
258 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
259 if (protect_tlv_size != info.it_tlv_tot) {
260 rc = BOOT_EBADIMAGE;
261 goto done;
262 }
263
264 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
265 rc = BOOT_EFLASH;
266 goto done;
267 }
268 } else if (protect_tlv_size != 0) {
269 rc = BOOT_EBADIMAGE;
270 goto done;
271 }
272
Fabio Utzig61fd8882019-09-14 20:00:20 -0300273 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
274 rc = BOOT_EBADIMAGE;
275 goto done;
276 }
277
Fabio Utzige52c08e2019-09-11 19:32:00 -0300278 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600279 rc = 0;
280
281done:
282 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300283 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600284}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300285#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600286
Tamas Banfe031092020-09-10 17:32:39 +0200287#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700288static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300289boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800290{
David Brownab449182019-11-15 09:32:52 -0700291 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300292#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700293 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300294#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295
296 /* Figure out what size to write update status update as. The size depends
297 * on what the minimum write size is for scratch area, active image slot.
298 * We need to use the bigger of those 2 values.
299 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300300 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300301#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300302 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303 if (align > elem_sz) {
304 elem_sz = align;
305 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300306#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307
308 return elem_sz;
309}
310
Fabio Utzig10ee6482019-08-01 12:04:52 -0300311static int
312boot_initialize_area(struct boot_loader_state *state, int flash_area)
313{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000314 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
315 boot_sector_t *out_sectors;
316 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300317 int rc;
318
319 num_sectors = BOOT_MAX_IMG_SECTORS;
320
321 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
322 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
323 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
324 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
325 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
326 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300327#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300328 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
329 out_sectors = state->scratch.sectors;
330 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300331#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300332 } else {
333 return BOOT_EFLASH;
334 }
335
Dominik Ermel51c8d762021-05-24 15:34:01 +0000336#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300337 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000338#else
339 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
340 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
341#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300342 if (rc != 0) {
343 return rc;
344 }
345 *out_num_sectors = num_sectors;
346 return 0;
347}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300348
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349/**
350 * Determines the sector layout of both image slots and the scratch area.
351 * This information is necessary for calculating the number of bytes to erase
352 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300353 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354 */
355static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300356boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800357{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300358 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360
Fabio Utzig10ee6482019-08-01 12:04:52 -0300361 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300362
363 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364 if (rc != 0) {
365 return BOOT_EFLASH;
366 }
367
Fabio Utzig10ee6482019-08-01 12:04:52 -0300368 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200370 /* We need to differentiate from the primary image issue */
371 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800372 }
373
Fabio Utzig12d59162019-11-28 10:01:59 -0300374#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300375 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200376 if (rc != 0) {
377 return BOOT_EFLASH;
378 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300379#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200380
Fabio Utzig10ee6482019-08-01 12:04:52 -0300381 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382
383 return 0;
384}
385
Fabio Utzig12d59162019-11-28 10:01:59 -0300386void
387boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388{
Fabio Utzig4741c452019-12-19 15:32:41 -0300389#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000390 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300391#if MCUBOOT_SWAP_SAVE_ENCTLV
392 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
393#endif
394#endif /* MCUBOOT_ENC_IMAGES */
395
396 bs->use_scratch = 0;
397 bs->swap_size = 0;
398 bs->source = 0;
399
Fabio Utzig74aef312019-11-28 11:05:34 -0300400 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300401 bs->idx = BOOT_STATUS_IDX_0;
402 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700403 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300404}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800405
Fabio Utzig12d59162019-11-28 10:01:59 -0300406bool
407boot_status_is_reset(const struct boot_status *bs)
408{
Fabio Utzig74aef312019-11-28 11:05:34 -0300409 return (bs->op == BOOT_STATUS_OP_MOVE &&
410 bs->idx == BOOT_STATUS_IDX_0 &&
411 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800412}
413
414/**
415 * Writes the supplied boot status to the flash file system. The boot status
416 * contains the current state of an in-progress image copy operation.
417 *
418 * @param bs The boot status to write.
419 *
420 * @return 0 on success; nonzero on failure.
421 */
422int
Fabio Utzig12d59162019-11-28 10:01:59 -0300423boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424{
425 const struct flash_area *fap;
426 uint32_t off;
427 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000428 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300429 uint8_t buf[BOOT_MAX_ALIGN];
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300430 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300431 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300433 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100434 * the trailer. Since in the last step the primary slot is erased, the
435 * first two status writes go to the scratch which will be copied to
436 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300437 */
438
Fabio Utzig12d59162019-11-28 10:01:59 -0300439#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300440 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441 /* Write to scratch. */
442 area_id = FLASH_AREA_IMAGE_SCRATCH;
443 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300444#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100445 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300446 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300447#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300449#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800450
451 rc = flash_area_open(area_id, &fap);
452 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000453 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454 }
455
456 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300457 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200458 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300459 erased_val = flash_area_erased_val(fap);
460 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700461 buf[0] = bs->state;
462
463 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800464 if (rc != 0) {
465 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 }
467
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000469
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 return rc;
471}
Tamas Banfe031092020-09-10 17:32:39 +0200472#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200473#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474
475/*
David Vinczec3084132020-02-18 14:50:47 +0100476 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100478static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300479boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
480 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300482 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300483 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300484 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100485 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300486
Fabio Utzig10ee6482019-08-01 12:04:52 -0300487#if (BOOT_IMAGE_NUMBER == 1)
488 (void)state;
489#endif
490
Fabio Utzigba829042018-09-18 08:29:34 -0300491 (void)bs;
492 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300493
494 image_index = BOOT_CURR_IMG(state);
495
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000496/* In the case of ram loading the image has already been decrypted as it is
497 * decrypted when copied in ram */
498#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300499 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300500 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300501 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100502 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300503 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300504 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100505 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300506 }
507 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300508#endif
509
Raef Colese8fe6cf2020-05-26 13:07:40 +0100510 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
511 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300512
Raef Colese8fe6cf2020-05-26 13:07:40 +0100513 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514}
515
Tamas Banfe031092020-09-10 17:32:39 +0200516#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100517static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518split_image_check(struct image_header *app_hdr,
519 const struct flash_area *app_fap,
520 struct image_header *loader_hdr,
521 const struct flash_area *loader_fap)
522{
523 static void *tmpbuf;
524 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100525 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526
527 if (!tmpbuf) {
528 tmpbuf = malloc(BOOT_TMPBUF_SZ);
529 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100530 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800531 }
532 }
533
Raef Colese8fe6cf2020-05-26 13:07:40 +0100534 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
535 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
536 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
537 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800538 }
539
Raef Colese8fe6cf2020-05-26 13:07:40 +0100540 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
541 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542
Raef Colese8fe6cf2020-05-26 13:07:40 +0100543out:
544 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545}
Tamas Banfe031092020-09-10 17:32:39 +0200546#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800547
Fabio Utzig338a19f2018-12-03 08:37:08 -0200548/*
David Brown9bf95af2019-10-10 15:36:36 -0600549 * Check that this is a valid header. Valid means that the magic is
550 * correct, and that the sizes/offsets are "sane". Sane means that
551 * there is no overflow on the arithmetic, and that the result fits
552 * within the flash area we are in.
553 */
554static bool
555boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
556{
557 uint32_t size;
558
559 if (hdr->ih_magic != IMAGE_MAGIC) {
560 return false;
561 }
562
563 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
564 return false;
565 }
566
Dominik Ermel260ae092021-04-23 05:38:45 +0000567 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600568 return false;
569 }
570
571 return true;
572}
573
574/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200575 * Check that a memory area consists of a given value.
576 */
577static inline bool
578boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300579{
580 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200581 uint8_t *p = (uint8_t *)data;
582 for (i = 0; i < len; i++) {
583 if (val != p[i]) {
584 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300585 }
586 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200587 return true;
588}
589
590static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300591boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200592{
593 const struct flash_area *fap;
594 struct image_header *hdr;
595 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300596 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200597 int rc;
598
Fabio Utzig10ee6482019-08-01 12:04:52 -0300599 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300600 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200601 if (rc != 0) {
602 return -1;
603 }
604
605 erased_val = flash_area_erased_val(fap);
606 flash_area_close(fap);
607
Fabio Utzig10ee6482019-08-01 12:04:52 -0300608 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200609 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
610 return -1;
611 }
612
613 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300614}
615
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000616#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200617 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200618 defined(MCUBOOT_RAM_LOAD) || \
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +0200619 defined(MCUBOOT_DOWNGRADE_PREVENTION)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000620/**
David Vincze8b0b6372020-05-20 19:54:44 +0200621 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000622 *
David Vincze8b0b6372020-05-20 19:54:44 +0200623 * @param ver1 Pointer to the first image version to compare.
624 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000625 *
David Vincze8b0b6372020-05-20 19:54:44 +0200626 * @retval -1 If ver1 is strictly less than ver2.
627 * @retval 0 If the image version numbers are equal,
628 * (not including the build number).
629 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000630 */
631static int
David Vincze8b0b6372020-05-20 19:54:44 +0200632boot_version_cmp(const struct image_version *ver1,
633 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000634{
David Vincze8b0b6372020-05-20 19:54:44 +0200635 if (ver1->iv_major > ver2->iv_major) {
636 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000637 }
David Vincze8b0b6372020-05-20 19:54:44 +0200638 if (ver1->iv_major < ver2->iv_major) {
639 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000640 }
David Vincze8b0b6372020-05-20 19:54:44 +0200641 /* The major version numbers are equal, continue comparison. */
642 if (ver1->iv_minor > ver2->iv_minor) {
643 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000644 }
David Vincze8b0b6372020-05-20 19:54:44 +0200645 if (ver1->iv_minor < ver2->iv_minor) {
646 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000647 }
David Vincze8b0b6372020-05-20 19:54:44 +0200648 /* The minor version numbers are equal, continue comparison. */
649 if (ver1->iv_revision > ver2->iv_revision) {
650 return 1;
651 }
652 if (ver1->iv_revision < ver2->iv_revision) {
653 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000654 }
655
656 return 0;
657}
658#endif
659
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000660#if defined(MCUBOOT_DIRECT_XIP)
661/**
662 * Check if image in slot has been set with specific ROM address to run from
663 * and whether the slot starts at that address.
664 *
665 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
666 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
667 * header matches the slot address;
668 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
669 * does not match the slot address.
670 */
671static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100672boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000673{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100674 uint32_t active_slot;
675 const struct image_header *hdr;
676 uint32_t f_off;
677
Raef Colesfe57e7d2021-10-15 11:07:09 +0100678 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100679 hdr = boot_img_hdr(state, active_slot);
680 f_off = boot_img_slot_off(state, active_slot);
681
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000682 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
683 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100684 ", skipping",
685 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000686 hdr->ih_load_addr);
687
688 /* If there is address mismatch, the image is not bootable from this
689 * slot.
690 */
691 return 1;
692 }
693 return 0;
694}
695#endif
696
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300697/*
698 * Check that there is a valid image in a slot
699 *
700 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100701 * FIH_SUCCESS if image was successfully validated
702 * 1 (or its fih_int encoded form) if no bootloable image was found
703 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300704 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100705static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300706boot_validate_slot(struct boot_loader_state *state, int slot,
707 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800708{
709 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400710 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300711 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100712 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300714
Fabio Utzig10ee6482019-08-01 12:04:52 -0300715 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300716 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800717 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100718 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800719 }
720
Fabio Utzig10ee6482019-08-01 12:04:52 -0300721 hdr = boot_img_hdr(state, slot);
722 if (boot_check_header_erased(state, slot) == 0 ||
723 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300724
725#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
726 /*
727 * This fixes an issue where an image might be erased, but a trailer
728 * be left behind. It can happen if the image is in the secondary slot
729 * and did not pass validation, in which case the whole slot is erased.
730 * If during the erase operation, a reset occurs, parts of the slot
731 * might have been erased while some did not. The concerning part is
732 * the trailer because it might disable a new image from being loaded
733 * through mcumgr; so we just get rid of the trailer here, if the header
734 * is erased.
735 */
736 if (slot != BOOT_PRIMARY_SLOT) {
737 swap_erase_trailer_sectors(state, fap);
738 }
739#endif
740
David Vincze2d736ad2019-02-18 11:50:22 +0100741 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100742 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200743 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300744 }
745
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000746#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
747 if (slot != BOOT_PRIMARY_SLOT) {
748 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200749 rc = boot_version_cmp(
750 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
751 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
752 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000753 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000754 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000755 /* Image in the secondary slot does not satisfy version requirement.
756 * Erase the image and continue booting from the primary slot.
757 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100758 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000759 goto out;
760 }
761 }
762#endif
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200763 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
764 fih_rc, BOOT_CURR_IMG(state), slot);
Andrzej Puzdrowski9d4d45c2021-09-14 17:25:58 +0200765 if (fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200766 {
767 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
768 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100769 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200770 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000771 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200772 /* Image is invalid, erase it to prevent further unnecessary
773 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700774 */
775 }
David Brown098de832019-12-10 11:58:01 -0700776#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100777 BOOT_LOG_ERR("Image in the %s slot is not valid!",
778 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700779#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100780 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200781 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800782 }
783
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000784#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
785 /* Verify that the image in the secondary slot has a reset address
786 * located in the primary slot. This is done to avoid users incorrectly
787 * overwriting an application written to the incorrect slot.
788 * This feature is only supported by ARM platforms.
789 */
790 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
791 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
792 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
793 uint32_t reset_value = 0;
794 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
795
796 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
797 if (rc != 0) {
798 fih_rc = fih_int_encode(1);
799 goto out;
800 }
801
802 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
803 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
804 BOOT_LOG_ERR("Erasing image from secondary slot");
805
806 /* The vector table in the image located in the secondary
807 * slot does not target the primary slot. This might
808 * indicate that the image was loaded to the wrong slot.
809 *
810 * Erase the image and continue booting from the primary slot.
811 */
812 flash_area_erase(fap, 0, fap->fa_size);
813 fih_rc = fih_int_encode(1);
814 goto out;
815 }
816 }
817#endif
818
Fabio Utzig338a19f2018-12-03 08:37:08 -0200819out:
820 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100821
822 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800823}
824
David Vinczec3084132020-02-18 14:50:47 +0100825#ifdef MCUBOOT_HW_ROLLBACK_PROT
826/**
827 * Updates the stored security counter value with the image's security counter
828 * value which resides in the given slot, only if it's greater than the stored
829 * value.
830 *
831 * @param image_index Index of the image to determine which security
832 * counter to update.
833 * @param slot Slot number of the image.
834 * @param hdr Pointer to the image header structure of the image
835 * that is currently stored in the given slot.
836 *
837 * @return 0 on success; nonzero on failure.
838 */
839static int
840boot_update_security_counter(uint8_t image_index, int slot,
841 struct image_header *hdr)
842{
843 const struct flash_area *fap = NULL;
844 uint32_t img_security_cnt;
845 int rc;
846
847 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
848 &fap);
849 if (rc != 0) {
850 rc = BOOT_EFLASH;
851 goto done;
852 }
853
854 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
855 if (rc != 0) {
856 goto done;
857 }
858
859 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
860 if (rc != 0) {
861 goto done;
862 }
863
864done:
865 flash_area_close(fap);
866 return rc;
867}
868#endif /* MCUBOOT_HW_ROLLBACK_PROT */
869
Tamas Banfe031092020-09-10 17:32:39 +0200870#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200871/**
872 * Determines which swap operation to perform, if any. If it is determined
873 * that a swap operation is required, the image in the secondary slot is checked
874 * for validity. If the image in the secondary slot is invalid, it is erased,
875 * and a swap type of "none" is indicated.
876 *
877 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
878 */
879static int
880boot_validated_swap_type(struct boot_loader_state *state,
881 struct boot_status *bs)
882{
883 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100884 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200885
886 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
887 if (BOOT_IS_UPGRADE(swap_type)) {
888 /* Boot loader wants to switch to the secondary slot.
889 * Ensure image is valid.
890 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100891 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
892 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
893 if (fih_eq(fih_rc, fih_int_encode(1))) {
894 swap_type = BOOT_SWAP_TYPE_NONE;
895 } else {
896 swap_type = BOOT_SWAP_TYPE_FAIL;
897 }
David Vinczee574f2d2020-07-10 11:42:03 +0200898 }
899 }
900
901 return swap_type;
902}
David Brown94ed12c2021-05-26 16:28:14 -0600903#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200904
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800906 * Erases a region of flash.
907 *
Fabio Utzigba829042018-09-18 08:29:34 -0300908 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909 * @param off The offset within the flash area to start the
910 * erase.
911 * @param sz The number of bytes to erase.
912 *
913 * @return 0 on success; nonzero on failure.
914 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300915int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300916boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800917{
Fabio Utzigba829042018-09-18 08:29:34 -0300918 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800919}
920
David Brown94ed12c2021-05-26 16:28:14 -0600921#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922/**
923 * Copies the contents of one flash region to another. You must erase the
924 * destination region prior to calling this function.
925 *
926 * @param flash_area_id_src The ID of the source flash area.
927 * @param flash_area_id_dst The ID of the destination flash area.
928 * @param off_src The offset within the source flash area to
929 * copy from.
930 * @param off_dst The offset within the destination flash area to
931 * copy to.
932 * @param sz The number of bytes to copy.
933 *
934 * @return 0 on success; nonzero on failure.
935 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300936int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300937boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300938 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300939 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800940 uint32_t off_src, uint32_t off_dst, uint32_t sz)
941{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800942 uint32_t bytes_copied;
943 int chunk_sz;
944 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300945#ifdef MCUBOOT_ENC_IMAGES
946 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300947 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300948 size_t blk_off;
949 struct image_header *hdr;
950 uint16_t idx;
951 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300952 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300953#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000955 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300956
957#if !defined(MCUBOOT_ENC_IMAGES)
958 (void)state;
959#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800960
Christopher Collins92ea77f2016-12-12 15:59:26 -0800961 bytes_copied = 0;
962 while (bytes_copied < sz) {
963 if (sz - bytes_copied > sizeof buf) {
964 chunk_sz = sizeof buf;
965 } else {
966 chunk_sz = sz - bytes_copied;
967 }
968
969 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
970 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300971 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800972 }
973
Fabio Utzigba829042018-09-18 08:29:34 -0300974#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300975 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000976 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
977 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
978 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
979 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100980 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300981 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300982#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300983 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000984 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100985 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300986 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300987 off = off_dst;
988 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300989#else
990 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000991 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300992 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
993 }
994#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200995 if (IS_ENCRYPTED(hdr)) {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +0100996 uint32_t abs_off = off + bytes_copied;
997 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -0300998 /* do not decrypt header */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +0100999 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001000 /* The lower part of the chunk contains header data */
1001 blk_off = 0;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001002 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1003 idx = hdr->ih_hdr_size - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001004 } else {
1005 /* The chunk contains exclusively header data */
1006 blk_sz = 0; /* nothing to decrypt */
1007 }
Fabio Utzigba829042018-09-18 08:29:34 -03001008 } else {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001009 idx = 0;
1010 blk_sz = chunk_sz;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001011 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001012 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001013
1014 if (blk_sz > 0)
1015 {
1016 tlv_off = BOOT_TLV_OFF(hdr);
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001017 if (abs_off + chunk_sz > tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001018 /* do not decrypt TLVs */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001019 if (abs_off >= tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001020 blk_sz = 0;
1021 } else {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001022 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001023 }
Fabio Utzigba829042018-09-18 08:29:34 -03001024 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001025 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001026 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001027 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001028 }
Fabio Utzigba829042018-09-18 08:29:34 -03001029 }
1030 }
1031#endif
1032
Christopher Collins92ea77f2016-12-12 15:59:26 -08001033 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1034 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001035 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001036 }
1037
1038 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001039
1040 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001041 }
1042
Fabio Utzigba829042018-09-18 08:29:34 -03001043 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001044}
1045
Christopher Collins92ea77f2016-12-12 15:59:26 -08001046/**
David Vincze2d736ad2019-02-18 11:50:22 +01001047 * Overwrite primary slot with the image contained in the secondary slot.
1048 * If a prior copy operation was interrupted by a system reset, this function
1049 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001050 *
1051 * @param bs The current boot status. This function reads
1052 * this struct to determine if it is resuming
1053 * an interrupted swap operation. This
1054 * function writes the updated status to this
1055 * function on return.
1056 *
1057 * @return 0 on success; nonzero on failure.
1058 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001059#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001060static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001061boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001062{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001063 size_t sect_count;
1064 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001065 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001066 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001067 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001068 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001069 const struct flash_area *fap_primary_slot;
1070 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001071 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001072
Fabio Utzigb4f88102020-10-04 10:16:24 -03001073#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1074 uint32_t sector;
1075 uint32_t trailer_sz;
1076 uint32_t off;
1077 uint32_t sz;
1078#endif
1079
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001080 (void)bs;
1081
Fabio Utzig13d9e352017-10-05 20:32:31 -03001082#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1083 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001084 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001085 assert(rc == 0);
1086#endif
David Brown17609d82017-05-05 09:41:34 -06001087
David Vincze2d736ad2019-02-18 11:50:22 +01001088 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1089 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001090
Fabio Utzigb0f04732019-07-31 09:49:19 -03001091 image_index = BOOT_CURR_IMG(state);
1092
1093 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1094 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001095 assert (rc == 0);
1096
Fabio Utzigb0f04732019-07-31 09:49:19 -03001097 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1098 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001099 assert (rc == 0);
1100
Fabio Utzig10ee6482019-08-01 12:04:52 -03001101 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001102 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001103 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001104 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001105 assert(rc == 0);
1106
Fabio Utzig13d9e352017-10-05 20:32:31 -03001107#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001108 if ((size + this_size) >= src_size) {
1109 size += src_size - size;
1110 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001111 break;
1112 }
1113#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001114
1115 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001116 }
1117
Fabio Utzigb4f88102020-10-04 10:16:24 -03001118#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1119 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1120 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1121 sz = 0;
1122 do {
1123 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1124 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1125 sector--;
1126 } while (sz < trailer_sz);
1127
1128 rc = boot_erase_region(fap_primary_slot, off, sz);
1129 assert(rc == 0);
1130#endif
1131
Fabio Utzigba829042018-09-18 08:29:34 -03001132#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001133 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001134 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001135 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001136 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001137
Fabio Utzigba829042018-09-18 08:29:34 -03001138 if (rc < 0) {
1139 return BOOT_EBADIMAGE;
1140 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001141 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001142 return BOOT_EBADIMAGE;
1143 }
1144 }
1145#endif
1146
David Vincze2d736ad2019-02-18 11:50:22 +01001147 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1148 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001149 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001150 if (rc != 0) {
1151 return rc;
1152 }
1153
1154#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1155 rc = boot_write_magic(fap_primary_slot);
1156 if (rc != 0) {
1157 return rc;
1158 }
1159#endif
David Brown17609d82017-05-05 09:41:34 -06001160
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001161 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1162 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1163 if (rc != 0) {
1164 return rc;
1165 }
1166
David Vinczec3084132020-02-18 14:50:47 +01001167#ifdef MCUBOOT_HW_ROLLBACK_PROT
1168 /* Update the stored security counter with the new image's security counter
1169 * value. Both slots hold the new image at this point, but the secondary
1170 * slot's image header must be passed since the image headers in the
1171 * boot_data structure have not been updated yet.
1172 */
1173 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1174 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1175 if (rc != 0) {
1176 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1177 return rc;
1178 }
1179#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1180
Fabio Utzig13d9e352017-10-05 20:32:31 -03001181 /*
1182 * Erases header and trailer. The trailer is erased because when a new
1183 * image is written without a trailer as is the case when using newt, the
1184 * trailer that was left might trigger a new upgrade.
1185 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001186 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001187 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001188 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1189 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001190 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001191 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001192 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001193 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001194 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1195 last_sector),
1196 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1197 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001198 assert(rc == 0);
1199
David Vincze2d736ad2019-02-18 11:50:22 +01001200 flash_area_close(fap_primary_slot);
1201 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001202
David Vincze2d736ad2019-02-18 11:50:22 +01001203 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001204
1205 return 0;
1206}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001207#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001208
Christopher Collinsa1c12042019-05-23 14:00:28 -07001209#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001210/**
1211 * Swaps the two images in flash. If a prior copy operation was interrupted
1212 * by a system reset, this function completes that operation.
1213 *
1214 * @param bs The current boot status. This function reads
1215 * this struct to determine if it is resuming
1216 * an interrupted swap operation. This
1217 * function writes the updated status to this
1218 * function on return.
1219 *
1220 * @return 0 on success; nonzero on failure.
1221 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001222static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001223boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001224{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001225 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001226#ifdef MCUBOOT_ENC_IMAGES
1227 const struct flash_area *fap;
1228 uint8_t slot;
1229 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001230#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001231 uint32_t size;
1232 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001233 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001234 int rc;
1235
1236 /* FIXME: just do this if asked by user? */
1237
1238 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001239 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001240
Fabio Utzig12d59162019-11-28 10:01:59 -03001241 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001242 /*
1243 * No swap ever happened, so need to find the largest image which
1244 * will be used to determine the amount of sectors to swap.
1245 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001246 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001247 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001248 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001249 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001250 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001251
Fabio Utzigba829042018-09-18 08:29:34 -03001252#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001253 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001254 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001255 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001256 assert(rc >= 0);
1257
1258 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001259 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001260 assert(rc == 0);
1261 } else {
1262 rc = 0;
1263 }
1264 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001265 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001266 }
1267#endif
1268
Fabio Utzig10ee6482019-08-01 12:04:52 -03001269 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001270 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001271 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001272 assert(rc == 0);
1273 }
1274
Fabio Utzigba829042018-09-18 08:29:34 -03001275#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001276 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001277 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001278 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001279 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001280 assert(rc >= 0);
1281
1282 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001283 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001284 assert(rc == 0);
1285 } else {
1286 rc = 0;
1287 }
1288 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001289 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001290 }
1291#endif
1292
Fabio Utzig46490722017-09-04 15:34:32 -03001293 if (size > copy_size) {
1294 copy_size = size;
1295 }
1296
1297 bs->swap_size = copy_size;
1298 } else {
1299 /*
1300 * If a swap was under way, the swap_size should already be present
1301 * in the trailer...
1302 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001303 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001304 assert(rc == 0);
1305
1306 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001307
1308#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001309 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1310 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001311 assert(rc == 0);
1312
1313 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001314 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001315 break;
1316 }
1317 }
1318
1319 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001320 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001321 }
1322 }
1323#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001324 }
1325
Fabio Utzig12d59162019-11-28 10:01:59 -03001326 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001327
David Vincze2d736ad2019-02-18 11:50:22 +01001328#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001329 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001330 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001331 BOOT_LOG_WRN("%d status write fails performing the swap",
1332 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001333 }
1334#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001335 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1336 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001337
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338 return 0;
1339}
David Brown17609d82017-05-05 09:41:34 -06001340#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001341
David Vinczee32483f2019-06-13 10:46:24 +02001342#if (BOOT_IMAGE_NUMBER > 1)
1343/**
1344 * Check the image dependency whether it is satisfied and modify
1345 * the swap type if necessary.
1346 *
1347 * @param dep Image dependency which has to be verified.
1348 *
1349 * @return 0 on success; nonzero on failure.
1350 */
1351static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001352boot_verify_slot_dependency(struct boot_loader_state *state,
1353 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001354{
1355 struct image_version *dep_version;
1356 size_t dep_slot;
1357 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001358 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001359
1360 /* Determine the source of the image which is the subject of
1361 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001362 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001363 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1364 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001365 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001366
David Vincze8b0b6372020-05-20 19:54:44 +02001367 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1368 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001369 /* Dependency not satisfied.
1370 * Modify the swap type to decrease the version number of the image
1371 * (which will be located in the primary slot after the boot process),
1372 * consequently the number of unsatisfied dependencies will be
1373 * decreased or remain the same.
1374 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001375 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001376 case BOOT_SWAP_TYPE_TEST:
1377 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001378 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001379 break;
1380 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001381 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001382 break;
1383 default:
1384 break;
1385 }
David Vincze8b0b6372020-05-20 19:54:44 +02001386 } else {
1387 /* Dependency satisfied. */
1388 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001389 }
1390
1391 return rc;
1392}
1393
1394/**
1395 * Read all dependency TLVs of an image from the flash and verify
1396 * one after another to see if they are all satisfied.
1397 *
1398 * @param slot Image slot number.
1399 *
1400 * @return 0 on success; nonzero on failure.
1401 */
1402static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001403boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001404{
1405 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001406 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001407 struct image_dependency dep;
1408 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001409 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001410 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001411 int rc;
1412
Fabio Utzig10ee6482019-08-01 12:04:52 -03001413 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001414 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001415 if (rc != 0) {
1416 rc = BOOT_EFLASH;
1417 goto done;
1418 }
1419
Fabio Utzig61fd8882019-09-14 20:00:20 -03001420 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1421 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001422 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001423 goto done;
1424 }
1425
Fabio Utzig61fd8882019-09-14 20:00:20 -03001426 while (true) {
1427 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1428 if (rc < 0) {
1429 return -1;
1430 } else if (rc > 0) {
1431 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001432 break;
1433 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001434
1435 if (len != sizeof(dep)) {
1436 rc = BOOT_EBADIMAGE;
1437 goto done;
1438 }
1439
1440 rc = flash_area_read(fap, off, &dep, len);
1441 if (rc != 0) {
1442 rc = BOOT_EFLASH;
1443 goto done;
1444 }
1445
1446 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1447 rc = BOOT_EBADARGS;
1448 goto done;
1449 }
1450
1451 /* Verify dependency and modify the swap type if not satisfied. */
1452 rc = boot_verify_slot_dependency(state, &dep);
1453 if (rc != 0) {
1454 /* Dependency not satisfied. */
1455 goto done;
1456 }
David Vinczee32483f2019-06-13 10:46:24 +02001457 }
1458
1459done:
1460 flash_area_close(fap);
1461 return rc;
1462}
1463
1464/**
David Vinczee32483f2019-06-13 10:46:24 +02001465 * Iterate over all the images and verify whether the image dependencies in the
1466 * TLV area are all satisfied and update the related swap type if necessary.
1467 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001468static int
1469boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001470{
Erik Johnson49063752020-02-06 09:59:31 -06001471 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001472 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001473
Fabio Utzig10ee6482019-08-01 12:04:52 -03001474 BOOT_CURR_IMG(state) = 0;
1475 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001476 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1477 BOOT_CURR_IMG(state)++;
1478 continue;
1479 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001480 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1481 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1482 slot = BOOT_SECONDARY_SLOT;
1483 } else {
1484 slot = BOOT_PRIMARY_SLOT;
1485 }
1486
1487 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001488 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001489 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001490 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001491 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001492 /* Cannot upgrade due to non-met dependencies, so disable all
1493 * image upgrades.
1494 */
1495 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1496 BOOT_CURR_IMG(state) = idx;
1497 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1498 }
1499 break;
David Vinczee32483f2019-06-13 10:46:24 +02001500 }
1501 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001502 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001503}
1504#endif /* (BOOT_IMAGE_NUMBER > 1) */
1505
Christopher Collins92ea77f2016-12-12 15:59:26 -08001506/**
David Vinczeba3bd602019-06-17 16:01:43 +02001507 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001508 *
David Vinczeba3bd602019-06-17 16:01:43 +02001509 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001510 *
1511 * @return 0 on success; nonzero on failure.
1512 */
1513static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001514boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001515{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001516 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001517#ifndef MCUBOOT_OVERWRITE_ONLY
1518 uint8_t swap_type;
1519#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001520
David Vinczeba3bd602019-06-17 16:01:43 +02001521 /* At this point there are no aborted swaps. */
1522#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001523 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001524#elif defined(MCUBOOT_BOOTSTRAP)
1525 /* Check if the image update was triggered by a bad image in the
1526 * primary slot (the validity of the image in the secondary slot had
1527 * already been checked).
1528 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001529 fih_int fih_rc = FIH_FAILURE;
1530 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1531 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1532 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001533 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001534 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001535 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001536 }
1537#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001538 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001539#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001540 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001541
1542#ifndef MCUBOOT_OVERWRITE_ONLY
1543 /* The following state needs image_ok be explicitly set after the
1544 * swap was finished to avoid a new revert.
1545 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001546 swap_type = BOOT_SWAP_TYPE(state);
1547 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1548 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001549 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001550 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001551 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001552 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001553 }
1554
David Vinczec3084132020-02-18 14:50:47 +01001555#ifdef MCUBOOT_HW_ROLLBACK_PROT
1556 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1557 /* Update the stored security counter with the new image's security
1558 * counter value. The primary slot holds the new image at this point,
1559 * but the secondary slot's image header must be passed since image
1560 * headers in the boot_data structure have not been updated yet.
1561 *
1562 * In case of a permanent image swap mcuboot will never attempt to
1563 * revert the images on the next reboot. Therefore, the security
1564 * counter must be increased right after the image upgrade.
1565 */
1566 rc = boot_update_security_counter(
1567 BOOT_CURR_IMG(state),
1568 BOOT_PRIMARY_SLOT,
1569 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1570 if (rc != 0) {
1571 BOOT_LOG_ERR("Security counter update failed after "
1572 "image upgrade.");
1573 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1574 }
1575 }
1576#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1577
Fabio Utzige575b0b2019-09-11 12:34:23 -03001578 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001579 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001580 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001581 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001582 }
David Vinczeba3bd602019-06-17 16:01:43 +02001583 }
1584#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001585
David Vinczeba3bd602019-06-17 16:01:43 +02001586 return rc;
1587}
1588
1589/**
1590 * Completes a previously aborted image swap.
1591 *
1592 * @param bs The current boot status.
1593 *
1594 * @return 0 on success; nonzero on failure.
1595 */
1596#if !defined(MCUBOOT_OVERWRITE_ONLY)
1597static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001598boot_complete_partial_swap(struct boot_loader_state *state,
1599 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001600{
1601 int rc;
1602
1603 /* Determine the type of swap operation being resumed from the
1604 * `swap-type` trailer field.
1605 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001606 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001607 assert(rc == 0);
1608
Fabio Utzig10ee6482019-08-01 12:04:52 -03001609 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001610
1611 /* The following states need image_ok be explicitly set after the
1612 * swap was finished to avoid a new revert.
1613 */
1614 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1615 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001616 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001617 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001618 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001619 }
1620 }
1621
Fabio Utzige575b0b2019-09-11 12:34:23 -03001622 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001623 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001624 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001625 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001626 }
1627 }
1628
Fabio Utzig10ee6482019-08-01 12:04:52 -03001629 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001630 BOOT_LOG_ERR("panic!");
1631 assert(0);
1632
1633 /* Loop forever... */
1634 while (1) {}
1635 }
1636
1637 return rc;
1638}
1639#endif /* !MCUBOOT_OVERWRITE_ONLY */
1640
1641#if (BOOT_IMAGE_NUMBER > 1)
1642/**
1643 * Review the validity of previously determined swap types of other images.
1644 *
1645 * @param aborted_swap The current image upgrade is a
1646 * partial/aborted swap.
1647 */
1648static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001649boot_review_image_swap_types(struct boot_loader_state *state,
1650 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001651{
1652 /* In that case if we rebooted in the middle of an image upgrade process, we
1653 * must review the validity of swap types, that were previously determined
1654 * for other images. The image_ok flag had not been set before the reboot
1655 * for any of the updated images (only the copy_done flag) and thus falsely
1656 * the REVERT swap type has been determined for the previous images that had
1657 * been updated before the reboot.
1658 *
1659 * There are two separate scenarios that we have to deal with:
1660 *
1661 * 1. The reboot has happened during swapping an image:
1662 * The current image upgrade has been determined as a
1663 * partial/aborted swap.
1664 * 2. The reboot has happened between two separate image upgrades:
1665 * In this scenario we must check the swap type of the current image.
1666 * In those cases if it is NONE or REVERT we cannot certainly determine
1667 * the fact of a reboot. In a consistent state images must move in the
1668 * same direction or stay in place, e.g. in practice REVERT and TEST
1669 * swap types cannot be present at the same time. If the swap type of
1670 * the current image is either TEST, PERM or FAIL we must review the
1671 * already determined swap types of other images and set each false
1672 * REVERT swap types to NONE (these images had been successfully
1673 * updated before the system rebooted between two separate image
1674 * upgrades).
1675 */
1676
Fabio Utzig10ee6482019-08-01 12:04:52 -03001677 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001678 /* Nothing to do */
1679 return;
1680 }
1681
1682 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001683 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1684 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001685 /* Nothing to do */
1686 return;
1687 }
1688 }
1689
Fabio Utzig10ee6482019-08-01 12:04:52 -03001690 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1691 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1692 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001693 }
1694 }
1695}
1696#endif
1697
1698/**
1699 * Prepare image to be updated if required.
1700 *
1701 * Prepare image to be updated if required with completing an image swap
1702 * operation if one was aborted and/or determining the type of the
1703 * swap operation. In case of any error set the swap type to NONE.
1704 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001705 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001706 * @param bs Pointer where the read and possibly updated
1707 * boot status can be written to.
1708 */
1709static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001710boot_prepare_image_for_update(struct boot_loader_state *state,
1711 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001712{
1713 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001714 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001715
1716 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001717 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001718 if (rc != 0) {
1719 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1720 " - too small?", BOOT_MAX_IMG_SECTORS);
1721 /* Unable to determine sector layout, continue with next image
1722 * if there is one.
1723 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001725 if (rc == BOOT_EFLASH)
1726 {
1727 /* Only return on error from the primary image flash */
1728 return;
1729 }
David Vinczeba3bd602019-06-17 16:01:43 +02001730 }
1731
1732 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001733 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001734 if (rc != 0) {
1735 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001736 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001737 BOOT_CURR_IMG(state));
1738 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001739 return;
1740 }
1741
1742 /* If the current image's slots aren't compatible, no swap is possible.
1743 * Just boot into primary slot.
1744 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001745 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001746 boot_status_reset(bs);
1747
1748#ifndef MCUBOOT_OVERWRITE_ONLY
1749 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001750 if (rc != 0) {
1751 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001752 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001753 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001754 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001755 return;
1756 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001757#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001758
Fabio Utzig74aef312019-11-28 11:05:34 -03001759#ifdef MCUBOOT_SWAP_USING_MOVE
1760 /*
1761 * Must re-read image headers because the boot status might
1762 * have been updated in the previous function call.
1763 */
1764 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001765#ifdef MCUBOOT_BOOTSTRAP
1766 /* When bootstrapping it's OK to not have image magic in the primary slot */
1767 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1768 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1769#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001770 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001771#endif
1772
Fabio Utzig74aef312019-11-28 11:05:34 -03001773 /* Continue with next image if there is one. */
1774 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1775 BOOT_CURR_IMG(state));
1776 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1777 return;
1778 }
1779#endif
1780
David Vinczeba3bd602019-06-17 16:01:43 +02001781 /* Determine if we rebooted in the middle of an image swap
1782 * operation. If a partial swap was detected, complete it.
1783 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001784 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001785
1786#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001787 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001788#endif
1789
1790#ifdef MCUBOOT_OVERWRITE_ONLY
1791 /* Should never arrive here, overwrite-only mode has
1792 * no swap state.
1793 */
1794 assert(0);
1795#else
1796 /* Determine the type of swap operation being resumed from the
1797 * `swap-type` trailer field.
1798 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001799 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001800 assert(rc == 0);
1801#endif
1802 /* Attempt to read an image header from each slot. Ensure that
1803 * image headers in slots are aligned with headers in boot_data.
1804 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001805 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001806 assert(rc == 0);
1807
1808 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001809 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001810 } else {
1811 /* There was no partial swap, determine swap type. */
1812 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001813 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001814 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001815 FIH_CALL(boot_validate_slot, fih_rc,
1816 state, BOOT_SECONDARY_SLOT, bs);
1817 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1818 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1819 } else {
1820 BOOT_SWAP_TYPE(state) = bs->swap_type;
1821 }
David Vinczeba3bd602019-06-17 16:01:43 +02001822 }
1823
1824#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001825 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001826#endif
1827
1828#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001829 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001830 /* Header checks are done first because they are
1831 * inexpensive. Since overwrite-only copies starting from
1832 * offset 0, if interrupted, it might leave a valid header
1833 * magic, so also run validation on the primary slot to be
1834 * sure it's not OK.
1835 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001836 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1837 FIH_CALL(boot_validate_slot, fih_rc,
1838 state, BOOT_PRIMARY_SLOT, bs);
1839
1840 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1841
Fabio Utzig3d77c952020-10-04 10:23:17 -03001842 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001843 FIH_CALL(boot_validate_slot, fih_rc,
1844 state, BOOT_SECONDARY_SLOT, bs);
1845
Fabio Utzig3d77c952020-10-04 10:23:17 -03001846 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001847 /* Set swap type to REVERT to overwrite the primary
1848 * slot with the image contained in secondary slot
1849 * and to trigger the explicit setting of the
1850 * image_ok flag.
1851 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001852 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001853 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001854 }
1855 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001856#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001857 }
David Vinczeba3bd602019-06-17 16:01:43 +02001858 } else {
1859 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001860 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001861 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001862}
1863
Mark Horvathccaf7f82021-01-04 18:16:42 +01001864/**
1865 * Updates the security counter for the current image.
1866 *
1867 * @param state Boot loader status information.
1868 *
1869 * @return 0 on success; nonzero on failure.
1870 */
1871static int
1872boot_update_hw_rollback_protection(struct boot_loader_state *state)
1873{
1874#ifdef MCUBOOT_HW_ROLLBACK_PROT
1875 int rc;
1876
1877 /* Update the stored security counter with the active image's security
1878 * counter value. It will only be updated if the new security counter is
1879 * greater than the stored value.
1880 *
1881 * In case of a successful image swapping when the swap type is TEST the
1882 * security counter can be increased only after a reset, when the swap
1883 * type is NONE and the image has marked itself "OK" (the image_ok flag
1884 * has been set). This way a "revert" can be performed when it's
1885 * necessary.
1886 */
1887 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1888 rc = boot_update_security_counter(
1889 BOOT_CURR_IMG(state),
1890 BOOT_PRIMARY_SLOT,
1891 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1892 if (rc != 0) {
1893 BOOT_LOG_ERR("Security counter update failed after image "
1894 "validation.");
1895 return rc;
1896 }
1897 }
1898
1899 return 0;
1900
1901#else /* MCUBOOT_HW_ROLLBACK_PROT */
1902 (void) (state);
1903
1904 return 0;
1905#endif
1906}
1907
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001908/**
1909 * Checks test swap downgrade prevention conditions.
1910 *
1911 * Function called only for swap upgrades test run. It may prevent
1912 * swap if slot 1 image has <= version number or < security counter
1913 *
1914 * @param state Boot loader status information.
1915 *
1916 * @return 0 - image can be swapped, -1 downgrade prevention
1917 */
1918static int
1919check_downgrade_prevention(struct boot_loader_state *state)
1920{
1921#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
1922 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
1923 uint32_t security_counter[2];
1924 int rc;
1925
1926 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
1927 /* If there was security no counter in slot 0, allow swap */
1928 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
1929 BOOT_IMG(state, 0).area,
1930 &security_counter[0]);
1931 if (rc != 0) {
1932 return 0;
1933 }
1934 /* If there is no security counter in slot 1, or it's lower than
1935 * that of slot 0, prevent downgrade */
1936 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
1937 BOOT_IMG(state, 1).area,
1938 &security_counter[1]);
1939 if (rc != 0 || security_counter[0] > security_counter[1]) {
1940 rc = -1;
1941 }
1942 }
1943 else {
1944 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1945 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1946 }
1947 if (rc < 0) {
1948 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
1949 BOOT_LOG_INF("Image in slot 1 erased due to downgrade prevention");
1950 flash_area_erase(BOOT_IMG(state, 1).area, 0,
1951 flash_area_get_size(BOOT_IMG(state, 1).area));
1952 } else {
1953 rc = 0;
1954 }
1955 return rc;
1956#else
1957 (void)state;
1958 return 0;
1959#endif
1960}
1961
Raef Colese8fe6cf2020-05-26 13:07:40 +01001962fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001963context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001964{
Marti Bolivar84898652017-06-13 17:20:22 -04001965 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001966 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001967 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001968 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001969 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001970 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001971 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001972
1973 /* The array of slot sectors are defined here (as opposed to file scope) so
1974 * that they don't get allocated for non-boot-loader apps. This is
1975 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001976 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001977 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001978 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1979 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001980#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001981 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001982#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001983
Fabio Utzig298913b2019-08-28 11:22:45 -03001984 has_upgrade = false;
1985
1986#if (BOOT_IMAGE_NUMBER == 1)
1987 (void)has_upgrade;
1988#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001989
David Vinczeba3bd602019-06-17 16:01:43 +02001990 /* Iterate over all the images. By the end of the loop the swap type has
1991 * to be determined for each image and all aborted swaps have to be
1992 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001993 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001994 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01001995#if BOOT_IMAGE_NUMBER > 1
1996 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1997 continue;
1998 }
1999#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002000#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2001 /* The keys used for encryption may no longer be valid (could belong to
2002 * another images). Therefore, mark them as invalid to force their reload
2003 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002004 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002005 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002006#endif
2007
Fabio Utzig10ee6482019-08-01 12:04:52 -03002008 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002009
Fabio Utzig10ee6482019-08-01 12:04:52 -03002010 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002011 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002012 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002013 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002014#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002015 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002016#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002017
2018 /* Open primary and secondary image areas for the duration
2019 * of this call.
2020 */
2021 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002022 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002023 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002024 assert(rc == 0);
2025 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002026#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002027 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002028 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002029 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03002030#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002031
2032 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002033 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002034
Fabio Utzige575b0b2019-09-11 12:34:23 -03002035 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002036 has_upgrade = true;
2037 }
David Vinczeba3bd602019-06-17 16:01:43 +02002038 }
2039
David Vinczee32483f2019-06-13 10:46:24 +02002040#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002041 if (has_upgrade) {
2042 /* Iterate over all the images and verify whether the image dependencies
2043 * are all satisfied and update swap type if necessary.
2044 */
2045 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002046 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002047 /*
2048 * It was impossible to upgrade because the expected dependency version
2049 * was not available. Here we already changed the swap_type so that
2050 * instead of asserting the bootloader, we continue and no upgrade is
2051 * performed.
2052 */
2053 rc = 0;
2054 }
2055 }
David Vinczee32483f2019-06-13 10:46:24 +02002056#endif
2057
Jamie McCrae56cb6102022-03-23 11:57:03 +00002058 /* Trigger status change callback with upgrading status */
2059 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2060
David Vinczeba3bd602019-06-17 16:01:43 +02002061 /* Iterate over all the images. At this point there are no aborted swaps
2062 * and the swap types are determined for each image. By the end of the loop
2063 * all required update operations will have been finished.
2064 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002065 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002066#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002067 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2068 continue;
2069 }
2070
David Vinczeba3bd602019-06-17 16:01:43 +02002071#ifdef MCUBOOT_ENC_IMAGES
2072 /* The keys used for encryption may no longer be valid (could belong to
2073 * another images). Therefore, mark them as invalid to force their reload
2074 * by boot_enc_load().
2075 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002076 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002077#endif /* MCUBOOT_ENC_IMAGES */
2078
2079 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002080 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002081#endif /* (BOOT_IMAGE_NUMBER > 1) */
2082
2083 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002084 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002085
Fabio Utzig10ee6482019-08-01 12:04:52 -03002086 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002087 case BOOT_SWAP_TYPE_NONE:
2088 break;
2089
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002090 case BOOT_SWAP_TYPE_TEST:
2091 if (check_downgrade_prevention(state) != 0) {
2092 /* Downgrade prevented */
2093 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2094 break;
2095 }
2096 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002097 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2098 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002099 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2100 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2101 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2102 if (rc == BOOT_HOOK_REGULAR)
2103 {
2104 rc = boot_perform_update(state, &bs);
2105 }
David Vinczeba3bd602019-06-17 16:01:43 +02002106 assert(rc == 0);
2107 break;
2108
2109 case BOOT_SWAP_TYPE_FAIL:
2110 /* The image in secondary slot was invalid and is now erased. Ensure
2111 * we don't try to boot into it again on the next reboot. Do this by
2112 * pretending we just reverted back to primary slot.
2113 */
2114#ifndef MCUBOOT_OVERWRITE_ONLY
2115 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002116 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002117 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002118 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002119 }
2120#endif /* !MCUBOOT_OVERWRITE_ONLY */
2121 break;
2122
2123 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002124 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002125 }
2126
Fabio Utzig10ee6482019-08-01 12:04:52 -03002127 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002128 BOOT_LOG_ERR("panic!");
2129 assert(0);
2130
2131 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002132 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002133 }
2134 }
2135
2136 /* Iterate over all the images. At this point all required update operations
2137 * have finished. By the end of the loop each image in the primary slot will
2138 * have been re-validated.
2139 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002140 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002141#if BOOT_IMAGE_NUMBER > 1
2142 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2143 continue;
2144 }
2145#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002146 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002147 /* Attempt to read an image header from each slot. Ensure that image
2148 * headers in slots are aligned with headers in boot_data.
2149 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002150 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002151 if (rc != 0) {
2152 goto out;
2153 }
2154 /* Since headers were reloaded, it can be assumed we just performed
2155 * a swap or overwrite. Now the header info that should be used to
2156 * provide the data for the bootstrap, which previously was at
2157 * secondary slot, was updated to primary slot.
2158 */
2159 }
2160
2161#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002162 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2163 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002164 goto out;
2165 }
2166#else
2167 /* Even if we're not re-validating the primary slot, we could be booting
2168 * onto an empty flash chip. At least do a basic sanity check that
2169 * the magic number on the image is OK.
2170 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002171 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002172 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002173 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002174 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002175 rc = BOOT_EBADIMAGE;
2176 goto out;
2177 }
David Vinczec3084132020-02-18 14:50:47 +01002178#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2179
Mark Horvathccaf7f82021-01-04 18:16:42 +01002180 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002181 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002182 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002183 }
David Vincze1cf11b52020-03-24 07:51:09 +01002184
Mark Horvathccaf7f82021-01-04 18:16:42 +01002185 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002186 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002187 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002188 }
David Vinczeba3bd602019-06-17 16:01:43 +02002189 }
2190
Fabio Utzigf616c542019-12-19 15:23:32 -03002191 /*
2192 * Since the boot_status struct stores plaintext encryption keys, reset
2193 * them here to avoid the possibility of jumping into an image that could
2194 * easily recover them.
2195 */
2196 memset(&bs, 0, sizeof(struct boot_status));
2197
Raef Colesfe57e7d2021-10-15 11:07:09 +01002198 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002199
Raef Colese8fe6cf2020-05-26 13:07:40 +01002200 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002201out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002202 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002203
2204 if (rc) {
2205 fih_rc = fih_int_encode(rc);
2206 }
2207
2208 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002209}
2210
Raef Colese8fe6cf2020-05-26 13:07:40 +01002211fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002212split_go(int loader_slot, int split_slot, void **entry)
2213{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002214 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002215 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002216 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002217 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002218 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002219 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002220
Christopher Collins92ea77f2016-12-12 15:59:26 -08002221 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2222 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002223 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002224 }
David Vinczeba3bd602019-06-17 16:01:43 +02002225 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2226 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002227
2228 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2229 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002230 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002231 assert(rc == 0);
2232 split_flash_id = flash_area_id_from_image_slot(split_slot);
2233 rc = flash_area_open(split_flash_id,
2234 &BOOT_IMG_AREA(&boot_data, split_slot));
2235 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002236
2237 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002238 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002239 if (rc != 0) {
2240 rc = SPLIT_GO_ERR;
2241 goto done;
2242 }
2243
Fabio Utzig12d59162019-11-28 10:01:59 -03002244 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002245 if (rc != 0) {
2246 goto done;
2247 }
2248
Christopher Collins92ea77f2016-12-12 15:59:26 -08002249 /* Don't check the bootable image flag because we could really call a
2250 * bootable or non-bootable image. Just validate that the image check
2251 * passes which is distinct from the normal check.
2252 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002253 FIH_CALL(split_image_check, fih_rc,
2254 boot_img_hdr(&boot_data, split_slot),
2255 BOOT_IMG_AREA(&boot_data, split_slot),
2256 boot_img_hdr(&boot_data, loader_slot),
2257 BOOT_IMG_AREA(&boot_data, loader_slot));
2258 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002259 goto done;
2260 }
2261
Marti Bolivarea088872017-06-12 17:10:49 -04002262 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002263 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002264 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002265 rc = SPLIT_GO_OK;
2266
2267done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002268 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2269 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002270 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002271
2272 if (rc) {
2273 fih_rc = fih_int_encode(rc);
2274 }
2275
2276 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002277}
David Vinczee574f2d2020-07-10 11:42:03 +02002278
Tamas Banfe031092020-09-10 17:32:39 +02002279#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002280
Mark Horvathccaf7f82021-01-04 18:16:42 +01002281#define NO_ACTIVE_SLOT UINT32_MAX
2282
David Vinczee574f2d2020-07-10 11:42:03 +02002283/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002284 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002285 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002286 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002287 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002288 * @return 0 on success; nonzero on failure.
2289 */
2290static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002291boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002292{
2293 uint32_t slot;
2294 int fa_id;
2295 int rc;
2296 struct image_header *hdr = NULL;
2297
2298 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002299#if BOOT_IMAGE_NUMBER > 1
2300 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2301 continue;
2302 }
2303#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002304 /* Open all the slots */
2305 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2306 fa_id = flash_area_id_from_multi_image_slot(
2307 BOOT_CURR_IMG(state), slot);
2308 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2309 assert(rc == 0);
2310 }
2311
2312 /* Attempt to read an image header from each slot. */
2313 rc = boot_read_image_headers(state, false, NULL);
2314 if (rc != 0) {
2315 BOOT_LOG_WRN("Failed reading image headers.");
2316 return rc;
2317 }
2318
2319 /* Check headers in all slots */
2320 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2321 hdr = boot_img_hdr(state, slot);
2322
2323 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002324 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002325 BOOT_LOG_IMAGE_INFO(slot, hdr);
2326 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002327 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002328 BOOT_LOG_INF("Image %d %s slot: Image not found",
2329 BOOT_CURR_IMG(state),
2330 (slot == BOOT_PRIMARY_SLOT)
2331 ? "Primary" : "Secondary");
2332 }
2333 }
2334
Raef Colesfe57e7d2021-10-15 11:07:09 +01002335 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002336 }
2337
2338 return 0;
2339}
2340
2341/**
2342 * Finds the slot containing the image with the highest version number for the
2343 * current image.
2344 *
2345 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002346 *
2347 * @return NO_ACTIVE_SLOT if no available slot found, number of
2348 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002349 */
2350static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002351find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002352{
David Vinczee574f2d2020-07-10 11:42:03 +02002353 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002354 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2355 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002356
Mark Horvathccaf7f82021-01-04 18:16:42 +01002357 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002358 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002359 if (candidate_slot == NO_ACTIVE_SLOT) {
2360 candidate_slot = slot;
2361 } else {
2362 rc = boot_version_cmp(
2363 &boot_img_hdr(state, slot)->ih_ver,
2364 &boot_img_hdr(state, candidate_slot)->ih_ver);
2365 if (rc == 1) {
2366 /* The version of the image being examined is greater than
2367 * the version of the current candidate.
2368 */
2369 candidate_slot = slot;
2370 }
2371 }
David Vinczee574f2d2020-07-10 11:42:03 +02002372 }
2373 }
2374
Mark Horvathccaf7f82021-01-04 18:16:42 +01002375 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002376}
2377
Mark Horvathccaf7f82021-01-04 18:16:42 +01002378#ifdef MCUBOOT_HAVE_LOGGING
2379/**
2380 * Prints the state of the loaded images.
2381 *
2382 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002383 */
2384static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002385print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002386{
2387 uint32_t active_slot;
2388
David Brown695e5912021-05-24 16:58:01 -06002389 (void)state;
2390
Mark Horvathccaf7f82021-01-04 18:16:42 +01002391 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002392#if BOOT_IMAGE_NUMBER > 1
2393 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2394 continue;
2395 }
2396#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002397 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002398
2399 BOOT_LOG_INF("Image %d loaded from the %s slot",
2400 BOOT_CURR_IMG(state),
2401 (active_slot == BOOT_PRIMARY_SLOT) ?
2402 "primary" : "secondary");
2403 }
2404}
2405#endif
2406
David Vincze1c456242021-06-29 15:25:24 +02002407#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002408/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002409 * Checks whether the active slot of the current image was previously selected
2410 * to run. Erases the image if it was selected but its execution failed,
2411 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002412 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002413 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002414 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002415 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002416 */
2417static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002418boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002419{
2420 const struct flash_area *fap;
2421 int fa_id;
2422 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002423 uint32_t active_slot;
2424 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002425
Raef Colesfe57e7d2021-10-15 11:07:09 +01002426 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002427
2428 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002429 rc = flash_area_open(fa_id, &fap);
2430 assert(rc == 0);
2431
Raef Colesfe57e7d2021-10-15 11:07:09 +01002432 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002433
2434 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2435 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002436 assert(rc == 0);
2437
Mark Horvathccaf7f82021-01-04 18:16:42 +01002438 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2439 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2440 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002441 /*
2442 * A reboot happened without the image being confirmed at
2443 * runtime or its trailer is corrupted/invalid. Erase the image
2444 * to prevent it from being selected again on the next reboot.
2445 */
2446 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002447 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002448 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002449 assert(rc == 0);
2450
2451 flash_area_close(fap);
2452 rc = -1;
2453 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002454 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2455 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002456 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2457 "value was neither 'set' nor 'unset', but 'bad'.");
2458 }
2459 /*
2460 * Set the copy_done flag, indicating that the image has been
2461 * selected to boot. It can be set in advance, before even
2462 * validating the image, because in case the validation fails, the
2463 * entire image slot will be erased (including the trailer).
2464 */
2465 rc = boot_write_copy_done(fap);
2466 if (rc != 0) {
2467 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002468 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002469 "primary" : "secondary");
2470 rc = 0;
2471 }
2472 }
2473 flash_area_close(fap);
2474 }
2475
2476 return rc;
2477}
David Vincze1c456242021-06-29 15:25:24 +02002478#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002479
Tamas Banfe031092020-09-10 17:32:39 +02002480#ifdef MCUBOOT_RAM_LOAD
2481
Mark Horvathccaf7f82021-01-04 18:16:42 +01002482#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002483#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2484#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2485#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002486#endif
Tamas Banfe031092020-09-10 17:32:39 +02002487
2488/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002489 * Verifies that the active slot of the current image can be loaded within the
2490 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002491 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002492 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002493 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002494 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002495 */
2496static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002497boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002498{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002499 uint32_t img_dst;
2500 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002501 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002502 uint32_t exec_ram_start;
2503 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002504
2505 (void)state;
2506
Mark Horvathccaf7f82021-01-04 18:16:42 +01002507#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2508 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002509
Mark Horvathccaf7f82021-01-04 18:16:42 +01002510 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2511 &exec_ram_size);
2512 if (rc != 0) {
2513 return BOOT_EBADSTATUS;
2514 }
2515#else
2516 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2517 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2518#endif
2519
Raef Colesfe57e7d2021-10-15 11:07:09 +01002520 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2521 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002522
2523 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002524 return BOOT_EBADIMAGE;
2525 }
2526
2527 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2528 return BOOT_EBADIMAGE;
2529 }
2530
Mark Horvathccaf7f82021-01-04 18:16:42 +01002531 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002532 return BOOT_EBADIMAGE;
2533 }
2534
2535 return 0;
2536}
2537
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002538#ifdef MCUBOOT_ENC_IMAGES
2539
2540/**
2541 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2542 *
2543 * @param state Boot loader status information.
2544 * @param slot The flash slot of the image to be copied to SRAM.
2545 * @param hdr The image header.
2546 * @param src_sz Size of the image.
2547 * @param img_dst Pointer to the address at which the image needs to be
2548 * copied to SRAM.
2549 *
2550 * @return 0 on success; nonzero on failure.
2551 */
2552static int
2553boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2554 uint32_t slot, struct image_header *hdr,
2555 uint32_t src_sz, uint32_t img_dst)
2556{
2557 /* The flow for the decryption and copy of the image is as follows :
2558 * 1. The whole image is copied to the RAM (header + payload + TLV).
2559 * 2. The encryption key is loaded from the TLV in flash.
2560 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2561 * is 1024 bytes). Only the payload section is decrypted.
2562 * 4. The image is authenticated in RAM.
2563 */
2564 const struct flash_area *fap_src = NULL;
2565 struct boot_status bs;
2566 uint32_t blk_off;
2567 uint32_t tlv_off;
2568 uint32_t blk_sz;
2569 uint32_t bytes_copied = hdr->ih_hdr_size;
2570 uint32_t chunk_sz;
2571 uint32_t max_sz = 1024;
2572 uint16_t idx;
2573 uint8_t image_index;
2574 uint8_t * cur_dst;
2575 int area_id;
2576 int rc;
2577 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2578
2579 image_index = BOOT_CURR_IMG(state);
2580 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2581 rc = flash_area_open(area_id, &fap_src);
2582 if (rc != 0){
2583 return BOOT_EFLASH;
2584 }
2585
2586 tlv_off = BOOT_TLV_OFF(hdr);
2587
2588 /* Copying the whole image in RAM */
2589 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2590 if (rc != 0) {
2591 goto done;
2592 }
2593
2594 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2595 if (rc < 0) {
2596 goto done;
2597 }
2598
2599 /* if rc > 0 then the key has already been loaded */
2600 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2601 goto done;
2602 }
2603
2604 /* Starting at the end of the header as the header section is not encrypted */
2605 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2606 if (src_sz - bytes_copied > max_sz) {
2607 chunk_sz = max_sz;
2608 } else {
2609 chunk_sz = src_sz - bytes_copied;
2610 }
2611
2612 cur_dst = ram_dst + bytes_copied;
2613 blk_sz = chunk_sz;
2614 idx = 0;
2615 if (bytes_copied + chunk_sz > tlv_off) {
2616 /* Going over TLV section
2617 * Part of the chunk is encrypted payload */
2618 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2619 blk_sz = tlv_off - (bytes_copied);
2620 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2621 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2622 blk_off, cur_dst);
2623 } else {
2624 /* Image encrypted payload section */
2625 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2626 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2627 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2628 blk_off, cur_dst);
2629 }
2630
2631 bytes_copied += chunk_sz;
2632 }
2633 rc = 0;
2634
2635done:
2636 flash_area_close(fap_src);
2637
2638 return rc;
2639}
2640
2641#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002642/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002643 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002644 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002645 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002646 * @param slot The flash slot of the image to be copied to SRAM.
2647 * @param img_dst The address at which the image needs to be copied to
2648 * SRAM.
2649 * @param img_sz The size of the image that needs to be copied to SRAM.
2650 *
2651 * @return 0 on success; nonzero on failure.
2652 */
2653static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002654boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2655 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002656{
2657 int rc;
2658 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002659 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002660
Mark Horvathccaf7f82021-01-04 18:16:42 +01002661#if (BOOT_IMAGE_NUMBER == 1)
2662 (void)state;
2663#endif
2664
2665 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2666
2667 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002668 if (rc != 0) {
2669 return BOOT_EFLASH;
2670 }
2671
2672 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002673 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002674 if (rc != 0) {
2675 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2676 }
2677
2678 flash_area_close(fap_src);
2679
2680 return rc;
2681}
2682
Mark Horvathccaf7f82021-01-04 18:16:42 +01002683#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002684/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002685 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002686 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002687 * @param start_a Start of the A region.
2688 * @param end_a End of the A region.
2689 * @param start_b Start of the B region.
2690 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002691 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002692 * @return true if there is overlap; false otherwise.
2693 */
2694static bool
2695do_regions_overlap(uint32_t start_a, uint32_t end_a,
2696 uint32_t start_b, uint32_t end_b)
2697{
2698 if (start_b > end_a) {
2699 return false;
2700 } else if (start_b >= start_a) {
2701 return true;
2702 } else if (end_b > start_a) {
2703 return true;
2704 }
2705
2706 return false;
2707}
2708
2709/**
2710 * Checks if the image we want to load to memory overlap with an already
2711 * ramloaded image.
2712 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002713 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002714 *
2715 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002716 */
2717static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002718boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002719{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002720 uint32_t i;
2721
2722 uint32_t start_a;
2723 uint32_t end_a;
2724 uint32_t start_b;
2725 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002726 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002727
Raef Colesfe57e7d2021-10-15 11:07:09 +01002728 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002729 /* Safe to add here, values are already verified in
2730 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002731 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002732
2733 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002734 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002735 || i == image_id_to_check) {
2736 continue;
2737 }
2738
Raef Colesfe57e7d2021-10-15 11:07:09 +01002739 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002740 /* Safe to add here, values are already verified in
2741 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002742 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002743
2744 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2745 return -1;
2746 }
2747 }
2748
2749 return 0;
2750}
2751#endif
2752
2753/**
2754 * Loads the active slot of the current image into SRAM. The load address and
2755 * image size is extracted from the image header.
2756 *
2757 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002758 *
2759 * @return 0 on success; nonzero on failure.
2760 */
2761static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002762boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002763{
2764 uint32_t active_slot;
2765 struct image_header *hdr = NULL;
2766 uint32_t img_dst;
2767 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002768 int rc;
2769
Raef Colesfe57e7d2021-10-15 11:07:09 +01002770 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002771 hdr = boot_img_hdr(state, active_slot);
2772
Tamas Banfe031092020-09-10 17:32:39 +02002773 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2774
Mark Horvathccaf7f82021-01-04 18:16:42 +01002775 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002776
Mark Horvathccaf7f82021-01-04 18:16:42 +01002777 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002778 if (rc != 0) {
2779 return rc;
2780 }
2781
Raef Colesfe57e7d2021-10-15 11:07:09 +01002782 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2783 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002784
Raef Colesfe57e7d2021-10-15 11:07:09 +01002785 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002786 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002787 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002788 return rc;
2789 }
2790
Mark Horvathccaf7f82021-01-04 18:16:42 +01002791#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002792 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002793 if (rc != 0) {
2794 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2795 another image.", img_dst);
2796 return rc;
2797 }
2798#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002799#ifdef MCUBOOT_ENC_IMAGES
2800 /* decrypt image if encrypted and copy it to RAM */
2801 if (IS_ENCRYPTED(hdr)) {
2802 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2803 } else {
2804 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2805 }
2806#else
Tamas Banfe031092020-09-10 17:32:39 +02002807 /* Copy image to the load address from where it currently resides in
2808 * flash.
2809 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002810 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002811#endif
Tamas Banfe031092020-09-10 17:32:39 +02002812 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002813 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002814 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002815 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002816 }
2817 } else {
2818 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2819 * MCUBOOT_RAM_LOAD is set.
2820 */
2821 rc = BOOT_EBADIMAGE;
2822 }
2823
Mark Horvathccaf7f82021-01-04 18:16:42 +01002824 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002825 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2826 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002827 }
2828
Tamas Banfe031092020-09-10 17:32:39 +02002829 return rc;
2830}
2831
2832/**
2833 * Removes an image from SRAM, by overwriting it with zeros.
2834 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002835 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002836 *
2837 * @return 0 on success; nonzero on failure.
2838 */
2839static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002840boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002841{
David Brown695e5912021-05-24 16:58:01 -06002842 (void)state;
2843
Mark Horvathccaf7f82021-01-04 18:16:42 +01002844 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
Raef Colesfe57e7d2021-10-15 11:07:09 +01002845 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002846
Raef Colesfe57e7d2021-10-15 11:07:09 +01002847 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2848 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002849
Raef Colesfe57e7d2021-10-15 11:07:09 +01002850 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2851 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002852
2853 return 0;
2854}
2855
2856/**
2857 * Removes an image from flash by erasing the corresponding flash area
2858 *
2859 * @param state Boot loader status information.
2860 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002861 *
2862 * @return 0 on success; nonzero on failure.
2863 */
2864static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002865boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002866{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002867 int area_id;
2868 int rc;
2869 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002870
David Brown695e5912021-05-24 16:58:01 -06002871 (void)state;
2872
Mark Horvathccaf7f82021-01-04 18:16:42 +01002873 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2874 slot);
2875 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2876 rc = flash_area_open(area_id, &fap);
2877 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002878 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00002879 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002880 }
2881
2882 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002883}
2884#endif /* MCUBOOT_RAM_LOAD */
2885
Mark Horvathccaf7f82021-01-04 18:16:42 +01002886#if (BOOT_IMAGE_NUMBER > 1)
2887/**
2888 * Checks the image dependency whether it is satisfied.
2889 *
2890 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002891 * @param dep Image dependency which has to be verified.
2892 *
2893 * @return 0 if dependencies are met; nonzero otherwise.
2894 */
2895static int
2896boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002897 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002898{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002899 struct image_version *dep_version;
2900 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002901 int rc;
2902
Mark Horvathccaf7f82021-01-04 18:16:42 +01002903 /* Determine the source of the image which is the subject of
2904 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002905 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002906 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002907 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2908
2909 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2910 if (rc >= 0) {
2911 /* Dependency satisfied. */
2912 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002913 }
2914
Mark Horvathccaf7f82021-01-04 18:16:42 +01002915 return rc;
2916}
2917
2918/**
2919 * Reads all dependency TLVs of an image and verifies one after another to see
2920 * if they are all satisfied.
2921 *
2922 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002923 *
2924 * @return 0 if dependencies are met; nonzero otherwise.
2925 */
2926static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002927boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002928{
2929 uint32_t active_slot;
2930 const struct flash_area *fap;
2931 struct image_tlv_iter it;
2932 struct image_dependency dep;
2933 uint32_t off;
2934 uint16_t len;
2935 int area_id;
2936 int rc;
2937
Raef Colesfe57e7d2021-10-15 11:07:09 +01002938 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002939
2940 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2941 active_slot);
2942 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002943 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002944 rc = BOOT_EFLASH;
2945 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002946 }
2947
Mark Horvathccaf7f82021-01-04 18:16:42 +01002948 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2949 IMAGE_TLV_DEPENDENCY, true);
2950 if (rc != 0) {
2951 goto done;
2952 }
David Vinczee574f2d2020-07-10 11:42:03 +02002953
Mark Horvathccaf7f82021-01-04 18:16:42 +01002954 while (true) {
2955 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2956 if (rc < 0) {
2957 return -1;
2958 } else if (rc > 0) {
2959 rc = 0;
2960 break;
2961 }
David Vinczee574f2d2020-07-10 11:42:03 +02002962
Mark Horvathccaf7f82021-01-04 18:16:42 +01002963 if (len != sizeof(dep)) {
2964 rc = BOOT_EBADIMAGE;
2965 goto done;
2966 }
2967
2968 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2969 fap, off, &dep, len);
2970 if (rc != 0) {
2971 rc = BOOT_EFLASH;
2972 goto done;
2973 }
2974
2975 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2976 rc = BOOT_EBADARGS;
2977 goto done;
2978 }
2979
Raef Colesfe57e7d2021-10-15 11:07:09 +01002980 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002981 if (rc != 0) {
2982 /* Dependency not satisfied. */
2983 goto done;
2984 }
2985 }
2986
2987done:
2988 flash_area_close(fap);
2989 return rc;
2990}
2991
2992/**
2993 * Checks the dependency of all the active slots. If an image found with
2994 * invalid or not satisfied dependencies the image is removed from SRAM (in
2995 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2996 *
2997 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002998 *
2999 * @return 0 if dependencies are met; nonzero otherwise.
3000 */
3001static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003002boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003003{
3004 int rc = -1;
3005 uint32_t active_slot;
3006
3007 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003008 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3009 continue;
3010 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01003011 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003012 if (rc != 0) {
3013 /* Dependencies not met or invalid dependencies. */
3014
3015#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003016 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003017#endif /* MCUBOOT_RAM_LOAD */
3018
Raef Colesfe57e7d2021-10-15 11:07:09 +01003019 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
3020 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3021 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003022
3023 return rc;
3024 }
3025 }
3026
3027 return rc;
3028}
3029#endif /* (BOOT_IMAGE_NUMBER > 1) */
3030
3031/**
3032 * Tries to load a slot for all the images with validation.
3033 *
3034 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003035 *
3036 * @return 0 on success; nonzero on failure.
3037 */
3038fih_int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003039boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003040{
3041 uint32_t active_slot;
3042 int rc;
3043 fih_int fih_rc;
3044
3045 /* Go over all the images and try to load one */
3046 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3047 /* All slots tried until a valid image found. Breaking from this loop
3048 * means that a valid image found or already loaded. If no slot is
3049 * found the function returns with error code. */
3050 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003051 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003052 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003053 if (active_slot != NO_ACTIVE_SLOT){
3054 /* A slot is already active, go to next image. */
3055 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003056 }
David Vincze505fba22020-10-22 13:53:29 +02003057
Raef Colesfe57e7d2021-10-15 11:07:09 +01003058 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003059 if (active_slot == NO_ACTIVE_SLOT) {
3060 BOOT_LOG_INF("No slot to load for image %d",
3061 BOOT_CURR_IMG(state));
3062 FIH_RET(FIH_FAILURE);
3063 }
3064
3065 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003066 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003067
Raef Colesf11de642021-10-15 11:11:56 +01003068#if BOOT_IMAGE_NUMBER > 1
3069 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3070 continue;
3071 }
3072#endif
3073
Mark Horvathccaf7f82021-01-04 18:16:42 +01003074#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003075 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003076 if (rc != 0) {
3077 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003078 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3079 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003080 continue;
3081 }
George Becksteind4d90f82021-05-11 02:00:00 -04003082
David Vincze505fba22020-10-22 13:53:29 +02003083#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003084 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003085 if (rc != 0) {
3086 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003087 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3088 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003089 continue;
3090 }
3091#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003092#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003093
Tamas Banfe031092020-09-10 17:32:39 +02003094#ifdef MCUBOOT_RAM_LOAD
3095 /* Image is first loaded to RAM and authenticated there in order to
3096 * prevent TOCTOU attack during image copy. This could be applied
3097 * when loading images from external (untrusted) flash to internal
3098 * (trusted) RAM and image is authenticated before copying.
3099 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003100 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003101 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003102 /* Image cannot be ramloaded. */
3103 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003104 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3105 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003106 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003107 }
3108#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003109
3110 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
3111 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3112 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003113#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003114 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003115#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003116 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3117 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003118 continue;
David Vincze505fba22020-10-22 13:53:29 +02003119 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003120
3121 /* Valid image loaded from a slot, go to next image. */
3122 break;
3123 }
3124 }
3125
3126 FIH_RET(FIH_SUCCESS);
3127}
3128
3129/**
3130 * Updates the security counter for the current image.
3131 *
3132 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003133 *
3134 * @return 0 on success; nonzero on failure.
3135 */
3136static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003137boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003138{
3139#ifdef MCUBOOT_HW_ROLLBACK_PROT
3140 int rc;
3141
3142 /* Update the stored security counter with the newer (active) image's
3143 * security counter value.
3144 */
David Vincze1c456242021-06-29 15:25:24 +02003145#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003146 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3147 * security counter can be increased only after reboot, if the image
3148 * has been confirmed at runtime (the image_ok flag has been set).
3149 * This way a 'revert' can be performed when it's necessary.
3150 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003151 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003152#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003153 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003154 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3155 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003156 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003157 BOOT_LOG_ERR("Security counter update failed after image "
3158 "validation.");
3159 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003160 }
David Vincze1c456242021-06-29 15:25:24 +02003161#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003162 }
3163#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003164
Mark Horvathccaf7f82021-01-04 18:16:42 +01003165 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003166
Mark Horvathccaf7f82021-01-04 18:16:42 +01003167#else /* MCUBOOT_HW_ROLLBACK_PROT */
3168 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003169 return 0;
3170#endif
3171}
3172
3173fih_int
3174context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3175{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003176 int rc;
David Brown695e5912021-05-24 16:58:01 -06003177 fih_int fih_rc = fih_int_encode(0);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003178
Raef Colesfe57e7d2021-10-15 11:07:09 +01003179 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003180 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003181 goto out;
3182 }
3183
Mark Horvathccaf7f82021-01-04 18:16:42 +01003184#if (BOOT_IMAGE_NUMBER > 1)
3185 while (true) {
3186#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003187 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003188 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3189 goto out;
3190 }
3191
3192#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003193 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003194 if (rc != 0) {
3195 /* Dependency check failed for an image, it has been removed from
3196 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3197 * unavailable. Try to load an image from another slot.
3198 */
3199 continue;
3200 }
3201 /* Dependency check was successful. */
3202 break;
3203 }
3204#endif
3205
3206 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003207#if BOOT_IMAGE_NUMBER > 1
3208 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3209 continue;
3210 }
3211#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003212 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003213 if (rc != 0) {
3214 goto out;
3215 }
3216
Raef Colesfe57e7d2021-10-15 11:07:09 +01003217 rc = boot_add_shared_data(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003218 if (rc != 0) {
3219 goto out;
3220 }
3221 }
3222
3223 /* All image loaded successfully. */
3224#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003225 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003226#endif
3227
Raef Colesfe57e7d2021-10-15 11:07:09 +01003228 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003229
David Vinczee574f2d2020-07-10 11:42:03 +02003230out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003231 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003232
Mark Horvathccaf7f82021-01-04 18:16:42 +01003233 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3234 fih_rc = fih_int_encode(rc);
3235 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003236
Mark Horvathccaf7f82021-01-04 18:16:42 +01003237 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003238}
Tamas Banfe031092020-09-10 17:32:39 +02003239#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003240
3241/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003242 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003243 * appropriate, and tells you what address to boot from.
3244 *
3245 * @param rsp On success, indicates how booting should occur.
3246 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003247 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003248 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003249fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003250boot_go(struct boot_rsp *rsp)
3251{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003252 fih_int fih_rc = FIH_FAILURE;
Raef Colesf11de642021-10-15 11:11:56 +01003253
3254 boot_state_clear(NULL);
3255
Raef Colese8fe6cf2020-05-26 13:07:40 +01003256 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3257 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003258}
Raef Colesf11de642021-10-15 11:11:56 +01003259
3260/**
3261 * Prepares the booting process, considering only a single image. This function
3262 * moves images around in flash as appropriate, and tells you what address to
3263 * boot from.
3264 *
3265 * @param rsp On success, indicates how booting should occur.
3266 *
3267 * @param image_id The image ID to prepare the boot process for.
3268 *
3269 * @return FIH_SUCCESS on success; nonzero on failure.
3270 */
3271fih_int
3272boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3273{
3274 fih_int fih_rc = FIH_FAILURE;
3275
3276 if (image_id >= BOOT_IMAGE_NUMBER) {
3277 FIH_RET(FIH_FAILURE);
3278 }
3279
3280#if BOOT_IMAGE_NUMBER > 1
3281 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3282 boot_data.img_mask[image_id] = 0;
3283#endif
3284
3285 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3286 FIH_RET(fih_rc);
3287}
3288
3289/**
3290 * Clears the boot state, so that previous operations have no effect on new
3291 * ones.
3292 *
3293 * @param state The state that should be cleared. If the value
3294 * is NULL, the default bootloader state will be
3295 * cleared.
3296 */
3297void boot_state_clear(struct boot_loader_state *state)
3298{
3299 if (state != NULL) {
3300 memset(state, 0, sizeof(struct boot_loader_state));
3301 } else {
3302 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3303 }
3304}