blob: 645a789598ec17a8ddf3152862096d798b0c4323 [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 */
Michael Grand5047f032022-11-24 16:49:56 +0100478static fih_ret
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;
Michael Grand5047f032022-11-24 16:49:56 +0100485 FIH_DECLARE(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)
Michael Grand5047f032022-11-24 16:49:56 +0100517static fih_ret
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];
Michael Grand5047f032022-11-24 16:49:56 +0100525 FIH_DECLARE(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);
Michael Grand5047f032022-11-24 16:49:56 +0100536 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100537 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
Michael Grand5047f032022-11-24 16:49:56 +0100702 * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
Raef Colese8fe6cf2020-05-26 13:07:40 +0100703 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300704 */
Michael Grand5047f032022-11-24 16:49:56 +0100705static fih_ret
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;
Michael Grand5047f032022-11-24 16:49:56 +0100712 FIH_DECLARE(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. */
Michael Grand5047f032022-11-24 16:49:56 +0100742 fih_rc = FIH_NO_BOOTABLE_IMAGE;
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 */
Michael Grand5047f032022-11-24 16:49:56 +0100758 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000759 goto out;
760 }
761 }
762#endif
Michael Grand5047f032022-11-24 16:49:56 +0100763 BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200764 fih_rc, BOOT_CURR_IMG(state), slot);
Michael Grand5047f032022-11-24 16:49:56 +0100765 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200766 {
767 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
768 }
Michael Grand5047f032022-11-24 16:49:56 +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
Michael Grand5047f032022-11-24 16:49:56 +0100780 fih_rc = FIH_NO_BOOTABLE_IMAGE;
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) {
Michael Grand5047f032022-11-24 16:49:56 +0100798 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000799 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);
Michael Grand5047f032022-11-24 16:49:56 +0100813 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000814 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;
Michael Grand5047f032022-11-24 16:49:56 +0100884 FIH_DECLARE(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);
Michael Grand5047f032022-11-24 16:49:56 +0100892 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
893 if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100894 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 const struct flash_area *fap;
Dominik Ermel472d4c72023-02-10 13:29:58 +00001227#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigba829042018-09-18 08:29:34 -03001228 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 */
Dominik Ermel472d4c72023-02-10 13:29:58 +00001303
1304 rc = boot_find_status(image_index, &fap);
1305 assert(fap != NULL);
1306 rc = boot_read_swap_size(fap, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001307 assert(rc == 0);
1308
1309 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001310
1311#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001312 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Dominik Ermel472d4c72023-02-10 13:29:58 +00001313 rc = boot_read_enc_key(fap, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001314 assert(rc == 0);
1315
1316 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001317 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001318 break;
1319 }
1320 }
1321
1322 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001323 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001324 }
1325 }
1326#endif
Dominik Ermel472d4c72023-02-10 13:29:58 +00001327 flash_area_close(fap);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001328 }
1329
Fabio Utzig12d59162019-11-28 10:01:59 -03001330 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001331
David Vincze2d736ad2019-02-18 11:50:22 +01001332#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001333 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001334 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001335 BOOT_LOG_WRN("%d status write fails performing the swap",
1336 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001337 }
1338#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001339 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1340 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001341
Christopher Collins92ea77f2016-12-12 15:59:26 -08001342 return 0;
1343}
David Brown17609d82017-05-05 09:41:34 -06001344#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001345
David Vinczee32483f2019-06-13 10:46:24 +02001346#if (BOOT_IMAGE_NUMBER > 1)
1347/**
1348 * Check the image dependency whether it is satisfied and modify
1349 * the swap type if necessary.
1350 *
1351 * @param dep Image dependency which has to be verified.
1352 *
1353 * @return 0 on success; nonzero on failure.
1354 */
1355static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001356boot_verify_slot_dependency(struct boot_loader_state *state,
1357 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001358{
1359 struct image_version *dep_version;
1360 size_t dep_slot;
1361 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001362 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001363
1364 /* Determine the source of the image which is the subject of
1365 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001366 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001367 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1368 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001369 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001370
David Vincze8b0b6372020-05-20 19:54:44 +02001371 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1372 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001373 /* Dependency not satisfied.
1374 * Modify the swap type to decrease the version number of the image
1375 * (which will be located in the primary slot after the boot process),
1376 * consequently the number of unsatisfied dependencies will be
1377 * decreased or remain the same.
1378 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001379 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001380 case BOOT_SWAP_TYPE_TEST:
1381 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001382 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001383 break;
1384 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001385 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001386 break;
1387 default:
1388 break;
1389 }
David Vincze8b0b6372020-05-20 19:54:44 +02001390 } else {
1391 /* Dependency satisfied. */
1392 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001393 }
1394
1395 return rc;
1396}
1397
1398/**
1399 * Read all dependency TLVs of an image from the flash and verify
1400 * one after another to see if they are all satisfied.
1401 *
1402 * @param slot Image slot number.
1403 *
1404 * @return 0 on success; nonzero on failure.
1405 */
1406static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001407boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001408{
1409 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001410 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001411 struct image_dependency dep;
1412 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001413 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001414 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001415 int rc;
1416
Fabio Utzig10ee6482019-08-01 12:04:52 -03001417 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001418 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001419 if (rc != 0) {
1420 rc = BOOT_EFLASH;
1421 goto done;
1422 }
1423
Fabio Utzig61fd8882019-09-14 20:00:20 -03001424 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1425 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001426 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001427 goto done;
1428 }
1429
Fabio Utzig61fd8882019-09-14 20:00:20 -03001430 while (true) {
1431 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1432 if (rc < 0) {
1433 return -1;
1434 } else if (rc > 0) {
1435 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001436 break;
1437 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001438
1439 if (len != sizeof(dep)) {
1440 rc = BOOT_EBADIMAGE;
1441 goto done;
1442 }
1443
1444 rc = flash_area_read(fap, off, &dep, len);
1445 if (rc != 0) {
1446 rc = BOOT_EFLASH;
1447 goto done;
1448 }
1449
1450 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1451 rc = BOOT_EBADARGS;
1452 goto done;
1453 }
1454
1455 /* Verify dependency and modify the swap type if not satisfied. */
1456 rc = boot_verify_slot_dependency(state, &dep);
1457 if (rc != 0) {
1458 /* Dependency not satisfied. */
1459 goto done;
1460 }
David Vinczee32483f2019-06-13 10:46:24 +02001461 }
1462
1463done:
1464 flash_area_close(fap);
1465 return rc;
1466}
1467
1468/**
David Vinczee32483f2019-06-13 10:46:24 +02001469 * Iterate over all the images and verify whether the image dependencies in the
1470 * TLV area are all satisfied and update the related swap type if necessary.
1471 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001472static int
1473boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001474{
Erik Johnson49063752020-02-06 09:59:31 -06001475 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001476 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001477
Fabio Utzig10ee6482019-08-01 12:04:52 -03001478 BOOT_CURR_IMG(state) = 0;
1479 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001480 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1481 BOOT_CURR_IMG(state)++;
1482 continue;
1483 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001484 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1485 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1486 slot = BOOT_SECONDARY_SLOT;
1487 } else {
1488 slot = BOOT_PRIMARY_SLOT;
1489 }
1490
1491 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001492 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001493 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001494 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001495 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001496 /* Cannot upgrade due to non-met dependencies, so disable all
1497 * image upgrades.
1498 */
1499 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1500 BOOT_CURR_IMG(state) = idx;
1501 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1502 }
1503 break;
David Vinczee32483f2019-06-13 10:46:24 +02001504 }
1505 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001506 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001507}
1508#endif /* (BOOT_IMAGE_NUMBER > 1) */
1509
Christopher Collins92ea77f2016-12-12 15:59:26 -08001510/**
David Vinczeba3bd602019-06-17 16:01:43 +02001511 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001512 *
David Vinczeba3bd602019-06-17 16:01:43 +02001513 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001514 *
1515 * @return 0 on success; nonzero on failure.
1516 */
1517static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001518boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001519{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001520 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001521#ifndef MCUBOOT_OVERWRITE_ONLY
1522 uint8_t swap_type;
1523#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001524
David Vinczeba3bd602019-06-17 16:01:43 +02001525 /* At this point there are no aborted swaps. */
1526#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001527 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001528#elif defined(MCUBOOT_BOOTSTRAP)
1529 /* Check if the image update was triggered by a bad image in the
1530 * primary slot (the validity of the image in the secondary slot had
1531 * already been checked).
1532 */
Michael Grand5047f032022-11-24 16:49:56 +01001533 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001534 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1535 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001536 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001537 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001538 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001539 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001540 }
1541#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001542 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001543#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001544 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001545
1546#ifndef MCUBOOT_OVERWRITE_ONLY
1547 /* The following state needs image_ok be explicitly set after the
1548 * swap was finished to avoid a new revert.
1549 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001550 swap_type = BOOT_SWAP_TYPE(state);
1551 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1552 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001553 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001554 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001555 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001556 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001557 }
1558
David Vinczec3084132020-02-18 14:50:47 +01001559#ifdef MCUBOOT_HW_ROLLBACK_PROT
1560 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1561 /* Update the stored security counter with the new image's security
1562 * counter value. The primary slot holds the new image at this point,
1563 * but the secondary slot's image header must be passed since image
1564 * headers in the boot_data structure have not been updated yet.
1565 *
1566 * In case of a permanent image swap mcuboot will never attempt to
1567 * revert the images on the next reboot. Therefore, the security
1568 * counter must be increased right after the image upgrade.
1569 */
1570 rc = boot_update_security_counter(
1571 BOOT_CURR_IMG(state),
1572 BOOT_PRIMARY_SLOT,
1573 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1574 if (rc != 0) {
1575 BOOT_LOG_ERR("Security counter update failed after "
1576 "image upgrade.");
1577 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1578 }
1579 }
1580#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1581
Fabio Utzige575b0b2019-09-11 12:34:23 -03001582 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001583 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001584 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001585 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001586 }
David Vinczeba3bd602019-06-17 16:01:43 +02001587 }
1588#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001589
David Vinczeba3bd602019-06-17 16:01:43 +02001590 return rc;
1591}
1592
1593/**
1594 * Completes a previously aborted image swap.
1595 *
1596 * @param bs The current boot status.
1597 *
1598 * @return 0 on success; nonzero on failure.
1599 */
1600#if !defined(MCUBOOT_OVERWRITE_ONLY)
1601static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001602boot_complete_partial_swap(struct boot_loader_state *state,
1603 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001604{
1605 int rc;
1606
1607 /* Determine the type of swap operation being resumed from the
1608 * `swap-type` trailer field.
1609 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001610 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001611 assert(rc == 0);
1612
Fabio Utzig10ee6482019-08-01 12:04:52 -03001613 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001614
1615 /* The following states need image_ok be explicitly set after the
1616 * swap was finished to avoid a new revert.
1617 */
1618 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1619 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001620 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001621 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001622 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001623 }
1624 }
1625
Fabio Utzige575b0b2019-09-11 12:34:23 -03001626 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001627 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001628 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001629 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001630 }
1631 }
1632
Fabio Utzig10ee6482019-08-01 12:04:52 -03001633 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001634 BOOT_LOG_ERR("panic!");
1635 assert(0);
1636
1637 /* Loop forever... */
1638 while (1) {}
1639 }
1640
1641 return rc;
1642}
1643#endif /* !MCUBOOT_OVERWRITE_ONLY */
1644
1645#if (BOOT_IMAGE_NUMBER > 1)
1646/**
1647 * Review the validity of previously determined swap types of other images.
1648 *
1649 * @param aborted_swap The current image upgrade is a
1650 * partial/aborted swap.
1651 */
1652static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001653boot_review_image_swap_types(struct boot_loader_state *state,
1654 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001655{
1656 /* In that case if we rebooted in the middle of an image upgrade process, we
1657 * must review the validity of swap types, that were previously determined
1658 * for other images. The image_ok flag had not been set before the reboot
1659 * for any of the updated images (only the copy_done flag) and thus falsely
1660 * the REVERT swap type has been determined for the previous images that had
1661 * been updated before the reboot.
1662 *
1663 * There are two separate scenarios that we have to deal with:
1664 *
1665 * 1. The reboot has happened during swapping an image:
1666 * The current image upgrade has been determined as a
1667 * partial/aborted swap.
1668 * 2. The reboot has happened between two separate image upgrades:
1669 * In this scenario we must check the swap type of the current image.
1670 * In those cases if it is NONE or REVERT we cannot certainly determine
1671 * the fact of a reboot. In a consistent state images must move in the
1672 * same direction or stay in place, e.g. in practice REVERT and TEST
1673 * swap types cannot be present at the same time. If the swap type of
1674 * the current image is either TEST, PERM or FAIL we must review the
1675 * already determined swap types of other images and set each false
1676 * REVERT swap types to NONE (these images had been successfully
1677 * updated before the system rebooted between two separate image
1678 * upgrades).
1679 */
1680
Fabio Utzig10ee6482019-08-01 12:04:52 -03001681 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001682 /* Nothing to do */
1683 return;
1684 }
1685
1686 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001687 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1688 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001689 /* Nothing to do */
1690 return;
1691 }
1692 }
1693
Fabio Utzig10ee6482019-08-01 12:04:52 -03001694 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1695 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1696 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001697 }
1698 }
1699}
1700#endif
1701
1702/**
1703 * Prepare image to be updated if required.
1704 *
1705 * Prepare image to be updated if required with completing an image swap
1706 * operation if one was aborted and/or determining the type of the
1707 * swap operation. In case of any error set the swap type to NONE.
1708 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001709 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001710 * @param bs Pointer where the read and possibly updated
1711 * boot status can be written to.
1712 */
1713static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001714boot_prepare_image_for_update(struct boot_loader_state *state,
1715 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001716{
1717 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01001718 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02001719
1720 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001721 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001722 if (rc != 0) {
1723 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1724 " - too small?", BOOT_MAX_IMG_SECTORS);
1725 /* Unable to determine sector layout, continue with next image
1726 * if there is one.
1727 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001728 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001729 if (rc == BOOT_EFLASH)
1730 {
1731 /* Only return on error from the primary image flash */
1732 return;
1733 }
David Vinczeba3bd602019-06-17 16:01:43 +02001734 }
1735
1736 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001737 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001738 if (rc != 0) {
1739 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001740 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001741 BOOT_CURR_IMG(state));
1742 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001743 return;
1744 }
1745
1746 /* If the current image's slots aren't compatible, no swap is possible.
1747 * Just boot into primary slot.
1748 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001749 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001750 boot_status_reset(bs);
1751
1752#ifndef MCUBOOT_OVERWRITE_ONLY
1753 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001754 if (rc != 0) {
1755 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001756 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001757 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001758 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001759 return;
1760 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001761#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001762
Fabio Utzig74aef312019-11-28 11:05:34 -03001763#ifdef MCUBOOT_SWAP_USING_MOVE
1764 /*
1765 * Must re-read image headers because the boot status might
1766 * have been updated in the previous function call.
1767 */
1768 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001769#ifdef MCUBOOT_BOOTSTRAP
1770 /* When bootstrapping it's OK to not have image magic in the primary slot */
1771 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1772 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1773#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001774 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001775#endif
1776
Fabio Utzig74aef312019-11-28 11:05:34 -03001777 /* Continue with next image if there is one. */
1778 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1779 BOOT_CURR_IMG(state));
1780 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1781 return;
1782 }
1783#endif
1784
David Vinczeba3bd602019-06-17 16:01:43 +02001785 /* Determine if we rebooted in the middle of an image swap
1786 * operation. If a partial swap was detected, complete it.
1787 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001788 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001789
1790#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001791 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001792#endif
1793
1794#ifdef MCUBOOT_OVERWRITE_ONLY
1795 /* Should never arrive here, overwrite-only mode has
1796 * no swap state.
1797 */
1798 assert(0);
1799#else
1800 /* Determine the type of swap operation being resumed from the
1801 * `swap-type` trailer field.
1802 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001803 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001804 assert(rc == 0);
1805#endif
1806 /* Attempt to read an image header from each slot. Ensure that
1807 * image headers in slots are aligned with headers in boot_data.
1808 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001809 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001810 assert(rc == 0);
1811
1812 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001813 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001814 } else {
1815 /* There was no partial swap, determine swap type. */
1816 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001817 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001818 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001819 FIH_CALL(boot_validate_slot, fih_rc,
1820 state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001821 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001822 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1823 } else {
1824 BOOT_SWAP_TYPE(state) = bs->swap_type;
1825 }
David Vinczeba3bd602019-06-17 16:01:43 +02001826 }
1827
1828#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001829 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001830#endif
1831
1832#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001833 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001834 /* Header checks are done first because they are
1835 * inexpensive. Since overwrite-only copies starting from
1836 * offset 0, if interrupted, it might leave a valid header
1837 * magic, so also run validation on the primary slot to be
1838 * sure it's not OK.
1839 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001840 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1841 FIH_CALL(boot_validate_slot, fih_rc,
1842 state, BOOT_PRIMARY_SLOT, bs);
1843
Michael Grand5047f032022-11-24 16:49:56 +01001844 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001845
Fabio Utzig3d77c952020-10-04 10:23:17 -03001846 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001847 FIH_CALL(boot_validate_slot, fih_rc,
1848 state, BOOT_SECONDARY_SLOT, bs);
1849
Michael Grand5047f032022-11-24 16:49:56 +01001850 if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001851 /* Set swap type to REVERT to overwrite the primary
1852 * slot with the image contained in secondary slot
1853 * and to trigger the explicit setting of the
1854 * image_ok flag.
1855 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001856 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001857 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001858 }
1859 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001860#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001861 }
David Vinczeba3bd602019-06-17 16:01:43 +02001862 } else {
1863 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001864 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001865 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001866}
1867
Mark Horvathccaf7f82021-01-04 18:16:42 +01001868/**
1869 * Updates the security counter for the current image.
1870 *
1871 * @param state Boot loader status information.
1872 *
1873 * @return 0 on success; nonzero on failure.
1874 */
1875static int
1876boot_update_hw_rollback_protection(struct boot_loader_state *state)
1877{
1878#ifdef MCUBOOT_HW_ROLLBACK_PROT
1879 int rc;
1880
1881 /* Update the stored security counter with the active image's security
1882 * counter value. It will only be updated if the new security counter is
1883 * greater than the stored value.
1884 *
1885 * In case of a successful image swapping when the swap type is TEST the
1886 * security counter can be increased only after a reset, when the swap
1887 * type is NONE and the image has marked itself "OK" (the image_ok flag
1888 * has been set). This way a "revert" can be performed when it's
1889 * necessary.
1890 */
1891 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1892 rc = boot_update_security_counter(
1893 BOOT_CURR_IMG(state),
1894 BOOT_PRIMARY_SLOT,
1895 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1896 if (rc != 0) {
1897 BOOT_LOG_ERR("Security counter update failed after image "
1898 "validation.");
1899 return rc;
1900 }
1901 }
1902
1903 return 0;
1904
1905#else /* MCUBOOT_HW_ROLLBACK_PROT */
1906 (void) (state);
1907
1908 return 0;
1909#endif
1910}
1911
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001912/**
1913 * Checks test swap downgrade prevention conditions.
1914 *
1915 * Function called only for swap upgrades test run. It may prevent
1916 * swap if slot 1 image has <= version number or < security counter
1917 *
1918 * @param state Boot loader status information.
1919 *
1920 * @return 0 - image can be swapped, -1 downgrade prevention
1921 */
1922static int
1923check_downgrade_prevention(struct boot_loader_state *state)
1924{
1925#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
1926 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
1927 uint32_t security_counter[2];
1928 int rc;
1929
1930 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
1931 /* If there was security no counter in slot 0, allow swap */
1932 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
1933 BOOT_IMG(state, 0).area,
1934 &security_counter[0]);
1935 if (rc != 0) {
1936 return 0;
1937 }
1938 /* If there is no security counter in slot 1, or it's lower than
1939 * that of slot 0, prevent downgrade */
1940 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
1941 BOOT_IMG(state, 1).area,
1942 &security_counter[1]);
1943 if (rc != 0 || security_counter[0] > security_counter[1]) {
1944 rc = -1;
1945 }
1946 }
1947 else {
1948 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1949 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1950 }
1951 if (rc < 0) {
1952 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
1953 BOOT_LOG_INF("Image in slot 1 erased due to downgrade prevention");
1954 flash_area_erase(BOOT_IMG(state, 1).area, 0,
1955 flash_area_get_size(BOOT_IMG(state, 1).area));
1956 } else {
1957 rc = 0;
1958 }
1959 return rc;
1960#else
1961 (void)state;
1962 return 0;
1963#endif
1964}
1965
Michael Grand5047f032022-11-24 16:49:56 +01001966fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -03001967context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001968{
Marti Bolivar84898652017-06-13 17:20:22 -04001969 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001970 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001971 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +01001972 FIH_DECLARE(fih_rc, FIH_FAILURE);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001973 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001974 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001975 bool has_upgrade;
Michael Grand5047f032022-11-24 16:49:56 +01001976 volatile int fih_cnt;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001977
1978 /* The array of slot sectors are defined here (as opposed to file scope) so
1979 * that they don't get allocated for non-boot-loader apps. This is
1980 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001981 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001982 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001983 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1984 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001985#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001986 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001987#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001988
Fabio Utzig298913b2019-08-28 11:22:45 -03001989 has_upgrade = false;
1990
1991#if (BOOT_IMAGE_NUMBER == 1)
1992 (void)has_upgrade;
1993#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001994
David Vinczeba3bd602019-06-17 16:01:43 +02001995 /* Iterate over all the images. By the end of the loop the swap type has
1996 * to be determined for each image and all aborted swaps have to be
1997 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001998 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001999 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002000#if BOOT_IMAGE_NUMBER > 1
2001 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2002 continue;
2003 }
2004#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002005#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2006 /* The keys used for encryption may no longer be valid (could belong to
2007 * another images). Therefore, mark them as invalid to force their reload
2008 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002009 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002010 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002011#endif
2012
Fabio Utzig10ee6482019-08-01 12:04:52 -03002013 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002014
Fabio Utzig10ee6482019-08-01 12:04:52 -03002015 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002016 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002017 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002018 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002019#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002020 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002021#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002022
2023 /* Open primary and secondary image areas for the duration
2024 * of this call.
2025 */
2026 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002027 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002028 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002029 assert(rc == 0);
2030 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002031#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002032 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002033 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002034 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03002035#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002036
2037 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002038 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002039
Fabio Utzige575b0b2019-09-11 12:34:23 -03002040 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002041 has_upgrade = true;
2042 }
David Vinczeba3bd602019-06-17 16:01:43 +02002043 }
2044
David Vinczee32483f2019-06-13 10:46:24 +02002045#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002046 if (has_upgrade) {
2047 /* Iterate over all the images and verify whether the image dependencies
2048 * are all satisfied and update swap type if necessary.
2049 */
2050 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002051 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002052 /*
2053 * It was impossible to upgrade because the expected dependency version
2054 * was not available. Here we already changed the swap_type so that
2055 * instead of asserting the bootloader, we continue and no upgrade is
2056 * performed.
2057 */
2058 rc = 0;
2059 }
2060 }
David Vinczee32483f2019-06-13 10:46:24 +02002061#endif
2062
Jamie McCrae56cb6102022-03-23 11:57:03 +00002063 /* Trigger status change callback with upgrading status */
2064 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2065
David Vinczeba3bd602019-06-17 16:01:43 +02002066 /* Iterate over all the images. At this point there are no aborted swaps
2067 * and the swap types are determined for each image. By the end of the loop
2068 * all required update operations will have been finished.
2069 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002070 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002071#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002072 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2073 continue;
2074 }
2075
David Vinczeba3bd602019-06-17 16:01:43 +02002076#ifdef MCUBOOT_ENC_IMAGES
2077 /* The keys used for encryption may no longer be valid (could belong to
2078 * another images). Therefore, mark them as invalid to force their reload
2079 * by boot_enc_load().
2080 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002081 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002082#endif /* MCUBOOT_ENC_IMAGES */
2083
2084 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002085 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002086#endif /* (BOOT_IMAGE_NUMBER > 1) */
2087
2088 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002089 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002090
Fabio Utzig10ee6482019-08-01 12:04:52 -03002091 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002092 case BOOT_SWAP_TYPE_NONE:
2093 break;
2094
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002095 case BOOT_SWAP_TYPE_TEST:
2096 if (check_downgrade_prevention(state) != 0) {
2097 /* Downgrade prevented */
2098 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2099 break;
2100 }
2101 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002102 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2103 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002104 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2105 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2106 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2107 if (rc == BOOT_HOOK_REGULAR)
2108 {
2109 rc = boot_perform_update(state, &bs);
2110 }
David Vinczeba3bd602019-06-17 16:01:43 +02002111 assert(rc == 0);
2112 break;
2113
2114 case BOOT_SWAP_TYPE_FAIL:
2115 /* The image in secondary slot was invalid and is now erased. Ensure
2116 * we don't try to boot into it again on the next reboot. Do this by
2117 * pretending we just reverted back to primary slot.
2118 */
2119#ifndef MCUBOOT_OVERWRITE_ONLY
2120 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002121 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002122 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002123 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002124 }
2125#endif /* !MCUBOOT_OVERWRITE_ONLY */
2126 break;
2127
2128 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002129 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002130 }
2131
Fabio Utzig10ee6482019-08-01 12:04:52 -03002132 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002133 BOOT_LOG_ERR("panic!");
2134 assert(0);
2135
2136 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002137 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002138 }
2139 }
2140
2141 /* Iterate over all the images. At this point all required update operations
2142 * have finished. By the end of the loop each image in the primary slot will
2143 * have been re-validated.
2144 */
Michael Grand5047f032022-11-24 16:49:56 +01002145 FIH_SET(fih_cnt, 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002146 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002147#if BOOT_IMAGE_NUMBER > 1
Michael Grand5047f032022-11-24 16:49:56 +01002148 /* Hardenned to prevent from skipping check of a given image,
2149 * tmp_img_mask is declared volatile
2150 */
2151 volatile bool tmp_img_mask;
2152 FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]);
2153 if (FIH_EQ(tmp_img_mask, true)) {
2154 ++fih_cnt;
Raef Colesf11de642021-10-15 11:11:56 +01002155 continue;
2156 }
2157#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002158 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002159 /* Attempt to read an image header from each slot. Ensure that image
2160 * headers in slots are aligned with headers in boot_data.
2161 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002162 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002163 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002164 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002165 goto out;
2166 }
2167 /* Since headers were reloaded, it can be assumed we just performed
2168 * a swap or overwrite. Now the header info that should be used to
2169 * provide the data for the bootstrap, which previously was at
2170 * secondary slot, was updated to primary slot.
2171 */
2172 }
2173
2174#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002175 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01002176 /* Check for all possible values is redundant in normal operation it
2177 * is meant to prevent FI attack.
2178 */
2179 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) ||
2180 FIH_EQ(fih_rc, FIH_FAILURE) ||
2181 FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
2182 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002183 goto out;
2184 }
2185#else
2186 /* Even if we're not re-validating the primary slot, we could be booting
2187 * onto an empty flash chip. At least do a basic sanity check that
2188 * the magic number on the image is OK.
2189 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002190 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002191 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002192 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002193 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002194 rc = BOOT_EBADIMAGE;
Michael Grand5047f032022-11-24 16:49:56 +01002195 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002196 goto out;
2197 }
David Vinczec3084132020-02-18 14:50:47 +01002198#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2199
Mark Horvathccaf7f82021-01-04 18:16:42 +01002200 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002201 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002202 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002203 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002204 }
David Vincze1cf11b52020-03-24 07:51:09 +01002205
Mark Horvathccaf7f82021-01-04 18:16:42 +01002206 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002207 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002208 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002209 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002210 }
Michael Grand5047f032022-11-24 16:49:56 +01002211 ++fih_cnt;
David Vinczeba3bd602019-06-17 16:01:43 +02002212 }
Michael Grand5047f032022-11-24 16:49:56 +01002213 /*
2214 * fih_cnt should be equal to BOOT_IMAGE_NUMBER now.
2215 * If this is not the case, at least one iteration of the loop
2216 * has been skipped.
2217 */
2218 if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
2219 FIH_PANIC;
2220 }
Fabio Utzigf616c542019-12-19 15:23:32 -03002221 /*
2222 * Since the boot_status struct stores plaintext encryption keys, reset
2223 * them here to avoid the possibility of jumping into an image that could
2224 * easily recover them.
2225 */
2226 memset(&bs, 0, sizeof(struct boot_status));
2227
Raef Colesfe57e7d2021-10-15 11:07:09 +01002228 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002229
Raef Colese8fe6cf2020-05-26 13:07:40 +01002230 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002231out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002232 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002233 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002234}
2235
Michael Grand5047f032022-11-24 16:49:56 +01002236fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -08002237split_go(int loader_slot, int split_slot, void **entry)
2238{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002239 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002240 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002241 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002242 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002243 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01002244 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002245
Christopher Collins92ea77f2016-12-12 15:59:26 -08002246 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2247 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002248 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002249 }
David Vinczeba3bd602019-06-17 16:01:43 +02002250 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2251 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002252
2253 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2254 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002255 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002256 assert(rc == 0);
2257 split_flash_id = flash_area_id_from_image_slot(split_slot);
2258 rc = flash_area_open(split_flash_id,
2259 &BOOT_IMG_AREA(&boot_data, split_slot));
2260 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002261
2262 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002263 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002264 if (rc != 0) {
2265 rc = SPLIT_GO_ERR;
2266 goto done;
2267 }
2268
Fabio Utzig12d59162019-11-28 10:01:59 -03002269 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002270 if (rc != 0) {
2271 goto done;
2272 }
2273
Christopher Collins92ea77f2016-12-12 15:59:26 -08002274 /* Don't check the bootable image flag because we could really call a
2275 * bootable or non-bootable image. Just validate that the image check
2276 * passes which is distinct from the normal check.
2277 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002278 FIH_CALL(split_image_check, fih_rc,
2279 boot_img_hdr(&boot_data, split_slot),
2280 BOOT_IMG_AREA(&boot_data, split_slot),
2281 boot_img_hdr(&boot_data, loader_slot),
2282 BOOT_IMG_AREA(&boot_data, loader_slot));
Michael Grand5047f032022-11-24 16:49:56 +01002283 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002284 goto done;
2285 }
2286
Marti Bolivarea088872017-06-12 17:10:49 -04002287 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002288 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002289 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002290 rc = SPLIT_GO_OK;
2291
2292done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002293 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2294 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002295 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002296
2297 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +01002298 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002299 }
2300
2301 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002302}
David Vinczee574f2d2020-07-10 11:42:03 +02002303
Tamas Banfe031092020-09-10 17:32:39 +02002304#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002305
Mark Horvathccaf7f82021-01-04 18:16:42 +01002306#define NO_ACTIVE_SLOT UINT32_MAX
2307
David Vinczee574f2d2020-07-10 11:42:03 +02002308/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002309 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002310 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002311 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002312 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002313 * @return 0 on success; nonzero on failure.
2314 */
2315static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002316boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002317{
2318 uint32_t slot;
2319 int fa_id;
2320 int rc;
2321 struct image_header *hdr = NULL;
2322
2323 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002324#if BOOT_IMAGE_NUMBER > 1
2325 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2326 continue;
2327 }
2328#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002329 /* Open all the slots */
2330 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2331 fa_id = flash_area_id_from_multi_image_slot(
2332 BOOT_CURR_IMG(state), slot);
2333 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2334 assert(rc == 0);
2335 }
2336
2337 /* Attempt to read an image header from each slot. */
2338 rc = boot_read_image_headers(state, false, NULL);
2339 if (rc != 0) {
2340 BOOT_LOG_WRN("Failed reading image headers.");
2341 return rc;
2342 }
2343
2344 /* Check headers in all slots */
2345 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2346 hdr = boot_img_hdr(state, slot);
2347
2348 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002349 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002350 BOOT_LOG_IMAGE_INFO(slot, hdr);
2351 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002352 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002353 BOOT_LOG_INF("Image %d %s slot: Image not found",
2354 BOOT_CURR_IMG(state),
2355 (slot == BOOT_PRIMARY_SLOT)
2356 ? "Primary" : "Secondary");
2357 }
2358 }
2359
Raef Colesfe57e7d2021-10-15 11:07:09 +01002360 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002361 }
2362
2363 return 0;
2364}
2365
2366/**
2367 * Finds the slot containing the image with the highest version number for the
2368 * current image.
2369 *
2370 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002371 *
2372 * @return NO_ACTIVE_SLOT if no available slot found, number of
2373 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002374 */
2375static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002376find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002377{
David Vinczee574f2d2020-07-10 11:42:03 +02002378 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002379 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2380 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002381
Mark Horvathccaf7f82021-01-04 18:16:42 +01002382 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002383 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002384 if (candidate_slot == NO_ACTIVE_SLOT) {
2385 candidate_slot = slot;
2386 } else {
2387 rc = boot_version_cmp(
2388 &boot_img_hdr(state, slot)->ih_ver,
2389 &boot_img_hdr(state, candidate_slot)->ih_ver);
2390 if (rc == 1) {
2391 /* The version of the image being examined is greater than
2392 * the version of the current candidate.
2393 */
2394 candidate_slot = slot;
2395 }
2396 }
David Vinczee574f2d2020-07-10 11:42:03 +02002397 }
2398 }
2399
Mark Horvathccaf7f82021-01-04 18:16:42 +01002400 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002401}
2402
Mark Horvathccaf7f82021-01-04 18:16:42 +01002403#ifdef MCUBOOT_HAVE_LOGGING
2404/**
2405 * Prints the state of the loaded images.
2406 *
2407 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002408 */
2409static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002410print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002411{
2412 uint32_t active_slot;
2413
David Brown695e5912021-05-24 16:58:01 -06002414 (void)state;
2415
Mark Horvathccaf7f82021-01-04 18:16:42 +01002416 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002417#if BOOT_IMAGE_NUMBER > 1
2418 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2419 continue;
2420 }
2421#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002422 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002423
2424 BOOT_LOG_INF("Image %d loaded from the %s slot",
2425 BOOT_CURR_IMG(state),
2426 (active_slot == BOOT_PRIMARY_SLOT) ?
2427 "primary" : "secondary");
2428 }
2429}
2430#endif
2431
David Vincze1c456242021-06-29 15:25:24 +02002432#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002433/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002434 * Checks whether the active slot of the current image was previously selected
2435 * to run. Erases the image if it was selected but its execution failed,
2436 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002437 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002438 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002439 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002440 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002441 */
2442static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002443boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002444{
2445 const struct flash_area *fap;
2446 int fa_id;
2447 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002448 uint32_t active_slot;
2449 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002450
Raef Colesfe57e7d2021-10-15 11:07:09 +01002451 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002452
2453 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002454 rc = flash_area_open(fa_id, &fap);
2455 assert(rc == 0);
2456
Raef Colesfe57e7d2021-10-15 11:07:09 +01002457 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002458
2459 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2460 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002461 assert(rc == 0);
2462
Mark Horvathccaf7f82021-01-04 18:16:42 +01002463 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2464 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2465 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002466 /*
2467 * A reboot happened without the image being confirmed at
2468 * runtime or its trailer is corrupted/invalid. Erase the image
2469 * to prevent it from being selected again on the next reboot.
2470 */
2471 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002472 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002473 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002474 assert(rc == 0);
2475
2476 flash_area_close(fap);
2477 rc = -1;
2478 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002479 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2480 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002481 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2482 "value was neither 'set' nor 'unset', but 'bad'.");
2483 }
2484 /*
2485 * Set the copy_done flag, indicating that the image has been
2486 * selected to boot. It can be set in advance, before even
2487 * validating the image, because in case the validation fails, the
2488 * entire image slot will be erased (including the trailer).
2489 */
2490 rc = boot_write_copy_done(fap);
2491 if (rc != 0) {
2492 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002493 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002494 "primary" : "secondary");
2495 rc = 0;
2496 }
2497 }
2498 flash_area_close(fap);
2499 }
2500
2501 return rc;
2502}
David Vincze1c456242021-06-29 15:25:24 +02002503#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002504
Tamas Banfe031092020-09-10 17:32:39 +02002505#ifdef MCUBOOT_RAM_LOAD
2506
Mark Horvathccaf7f82021-01-04 18:16:42 +01002507#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002508#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2509#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2510#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002511#endif
Tamas Banfe031092020-09-10 17:32:39 +02002512
2513/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002514 * Verifies that the active slot of the current image can be loaded within the
2515 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002516 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002517 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002518 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002519 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002520 */
2521static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002522boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002523{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002524 uint32_t img_dst;
2525 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002526 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002527 uint32_t exec_ram_start;
2528 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002529
2530 (void)state;
2531
Mark Horvathccaf7f82021-01-04 18:16:42 +01002532#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2533 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002534
Mark Horvathccaf7f82021-01-04 18:16:42 +01002535 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2536 &exec_ram_size);
2537 if (rc != 0) {
2538 return BOOT_EBADSTATUS;
2539 }
2540#else
2541 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2542 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2543#endif
2544
Raef Colesfe57e7d2021-10-15 11:07:09 +01002545 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2546 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002547
2548 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002549 return BOOT_EBADIMAGE;
2550 }
2551
2552 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2553 return BOOT_EBADIMAGE;
2554 }
2555
Mark Horvathccaf7f82021-01-04 18:16:42 +01002556 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002557 return BOOT_EBADIMAGE;
2558 }
2559
2560 return 0;
2561}
2562
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002563#ifdef MCUBOOT_ENC_IMAGES
2564
2565/**
2566 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2567 *
2568 * @param state Boot loader status information.
2569 * @param slot The flash slot of the image to be copied to SRAM.
2570 * @param hdr The image header.
2571 * @param src_sz Size of the image.
2572 * @param img_dst Pointer to the address at which the image needs to be
2573 * copied to SRAM.
2574 *
2575 * @return 0 on success; nonzero on failure.
2576 */
2577static int
2578boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2579 uint32_t slot, struct image_header *hdr,
2580 uint32_t src_sz, uint32_t img_dst)
2581{
2582 /* The flow for the decryption and copy of the image is as follows :
2583 * 1. The whole image is copied to the RAM (header + payload + TLV).
2584 * 2. The encryption key is loaded from the TLV in flash.
2585 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2586 * is 1024 bytes). Only the payload section is decrypted.
2587 * 4. The image is authenticated in RAM.
2588 */
2589 const struct flash_area *fap_src = NULL;
2590 struct boot_status bs;
2591 uint32_t blk_off;
2592 uint32_t tlv_off;
2593 uint32_t blk_sz;
2594 uint32_t bytes_copied = hdr->ih_hdr_size;
2595 uint32_t chunk_sz;
2596 uint32_t max_sz = 1024;
2597 uint16_t idx;
2598 uint8_t image_index;
2599 uint8_t * cur_dst;
2600 int area_id;
2601 int rc;
2602 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2603
2604 image_index = BOOT_CURR_IMG(state);
2605 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2606 rc = flash_area_open(area_id, &fap_src);
2607 if (rc != 0){
2608 return BOOT_EFLASH;
2609 }
2610
2611 tlv_off = BOOT_TLV_OFF(hdr);
2612
2613 /* Copying the whole image in RAM */
2614 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2615 if (rc != 0) {
2616 goto done;
2617 }
2618
2619 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2620 if (rc < 0) {
2621 goto done;
2622 }
2623
2624 /* if rc > 0 then the key has already been loaded */
2625 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2626 goto done;
2627 }
2628
2629 /* Starting at the end of the header as the header section is not encrypted */
2630 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2631 if (src_sz - bytes_copied > max_sz) {
2632 chunk_sz = max_sz;
2633 } else {
2634 chunk_sz = src_sz - bytes_copied;
2635 }
2636
2637 cur_dst = ram_dst + bytes_copied;
2638 blk_sz = chunk_sz;
2639 idx = 0;
2640 if (bytes_copied + chunk_sz > tlv_off) {
2641 /* Going over TLV section
2642 * Part of the chunk is encrypted payload */
2643 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2644 blk_sz = tlv_off - (bytes_copied);
2645 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2646 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2647 blk_off, cur_dst);
2648 } else {
2649 /* Image encrypted payload section */
2650 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2651 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2652 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2653 blk_off, cur_dst);
2654 }
2655
2656 bytes_copied += chunk_sz;
2657 }
2658 rc = 0;
2659
2660done:
2661 flash_area_close(fap_src);
2662
2663 return rc;
2664}
2665
2666#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002667/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002668 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002669 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002670 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002671 * @param slot The flash slot of the image to be copied to SRAM.
2672 * @param img_dst The address at which the image needs to be copied to
2673 * SRAM.
2674 * @param img_sz The size of the image that needs to be copied to SRAM.
2675 *
2676 * @return 0 on success; nonzero on failure.
2677 */
2678static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002679boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2680 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002681{
2682 int rc;
2683 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002684 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002685
Mark Horvathccaf7f82021-01-04 18:16:42 +01002686#if (BOOT_IMAGE_NUMBER == 1)
2687 (void)state;
2688#endif
2689
2690 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2691
2692 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002693 if (rc != 0) {
2694 return BOOT_EFLASH;
2695 }
2696
2697 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002698 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002699 if (rc != 0) {
2700 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2701 }
2702
2703 flash_area_close(fap_src);
2704
2705 return rc;
2706}
2707
Mark Horvathccaf7f82021-01-04 18:16:42 +01002708#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002709/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002710 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002711 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002712 * @param start_a Start of the A region.
2713 * @param end_a End of the A region.
2714 * @param start_b Start of the B region.
2715 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002716 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002717 * @return true if there is overlap; false otherwise.
2718 */
2719static bool
2720do_regions_overlap(uint32_t start_a, uint32_t end_a,
2721 uint32_t start_b, uint32_t end_b)
2722{
2723 if (start_b > end_a) {
2724 return false;
2725 } else if (start_b >= start_a) {
2726 return true;
2727 } else if (end_b > start_a) {
2728 return true;
2729 }
2730
2731 return false;
2732}
2733
2734/**
2735 * Checks if the image we want to load to memory overlap with an already
2736 * ramloaded image.
2737 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002738 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002739 *
2740 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002741 */
2742static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002743boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002744{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002745 uint32_t i;
2746
2747 uint32_t start_a;
2748 uint32_t end_a;
2749 uint32_t start_b;
2750 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002751 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002752
Raef Colesfe57e7d2021-10-15 11:07:09 +01002753 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002754 /* Safe to add here, values are already verified in
2755 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002756 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002757
2758 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002759 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002760 || i == image_id_to_check) {
2761 continue;
2762 }
2763
Raef Colesfe57e7d2021-10-15 11:07:09 +01002764 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002765 /* Safe to add here, values are already verified in
2766 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002767 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002768
2769 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2770 return -1;
2771 }
2772 }
2773
2774 return 0;
2775}
2776#endif
2777
2778/**
2779 * Loads the active slot of the current image into SRAM. The load address and
2780 * image size is extracted from the image header.
2781 *
2782 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002783 *
2784 * @return 0 on success; nonzero on failure.
2785 */
2786static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002787boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002788{
2789 uint32_t active_slot;
2790 struct image_header *hdr = NULL;
2791 uint32_t img_dst;
2792 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002793 int rc;
2794
Raef Colesfe57e7d2021-10-15 11:07:09 +01002795 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002796 hdr = boot_img_hdr(state, active_slot);
2797
Tamas Banfe031092020-09-10 17:32:39 +02002798 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2799
Mark Horvathccaf7f82021-01-04 18:16:42 +01002800 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002801
Mark Horvathccaf7f82021-01-04 18:16:42 +01002802 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002803 if (rc != 0) {
2804 return rc;
2805 }
2806
Raef Colesfe57e7d2021-10-15 11:07:09 +01002807 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2808 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002809
Raef Colesfe57e7d2021-10-15 11:07:09 +01002810 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002811 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002812 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002813 return rc;
2814 }
2815
Mark Horvathccaf7f82021-01-04 18:16:42 +01002816#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002817 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002818 if (rc != 0) {
2819 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2820 another image.", img_dst);
2821 return rc;
2822 }
2823#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002824#ifdef MCUBOOT_ENC_IMAGES
2825 /* decrypt image if encrypted and copy it to RAM */
2826 if (IS_ENCRYPTED(hdr)) {
2827 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2828 } else {
2829 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2830 }
2831#else
Tamas Banfe031092020-09-10 17:32:39 +02002832 /* Copy image to the load address from where it currently resides in
2833 * flash.
2834 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002835 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002836#endif
Tamas Banfe031092020-09-10 17:32:39 +02002837 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002838 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002839 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002840 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002841 }
2842 } else {
2843 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2844 * MCUBOOT_RAM_LOAD is set.
2845 */
2846 rc = BOOT_EBADIMAGE;
2847 }
2848
Mark Horvathccaf7f82021-01-04 18:16:42 +01002849 if (rc != 0) {
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
Tamas Banfe031092020-09-10 17:32:39 +02002854 return rc;
2855}
2856
2857/**
2858 * Removes an image from SRAM, by overwriting it with zeros.
2859 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002860 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002861 *
2862 * @return 0 on success; nonzero on failure.
2863 */
2864static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002865boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002866{
David Brown695e5912021-05-24 16:58:01 -06002867 (void)state;
2868
Mark Horvathccaf7f82021-01-04 18:16:42 +01002869 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
Raef Colesfe57e7d2021-10-15 11:07:09 +01002870 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002871
Raef Colesfe57e7d2021-10-15 11:07:09 +01002872 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2873 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002874
Raef Colesfe57e7d2021-10-15 11:07:09 +01002875 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2876 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002877
2878 return 0;
2879}
2880
2881/**
2882 * Removes an image from flash by erasing the corresponding flash area
2883 *
2884 * @param state Boot loader status information.
2885 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002886 *
2887 * @return 0 on success; nonzero on failure.
2888 */
2889static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002890boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002891{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002892 int area_id;
2893 int rc;
2894 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002895
David Brown695e5912021-05-24 16:58:01 -06002896 (void)state;
2897
Mark Horvathccaf7f82021-01-04 18:16:42 +01002898 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2899 slot);
2900 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2901 rc = flash_area_open(area_id, &fap);
2902 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002903 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00002904 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002905 }
2906
2907 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002908}
2909#endif /* MCUBOOT_RAM_LOAD */
2910
Mark Horvathccaf7f82021-01-04 18:16:42 +01002911#if (BOOT_IMAGE_NUMBER > 1)
2912/**
2913 * Checks the image dependency whether it is satisfied.
2914 *
2915 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002916 * @param dep Image dependency which has to be verified.
2917 *
2918 * @return 0 if dependencies are met; nonzero otherwise.
2919 */
2920static int
2921boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002922 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002923{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002924 struct image_version *dep_version;
2925 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002926 int rc;
2927
Mark Horvathccaf7f82021-01-04 18:16:42 +01002928 /* Determine the source of the image which is the subject of
2929 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002930 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002931 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002932 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2933
2934 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2935 if (rc >= 0) {
2936 /* Dependency satisfied. */
2937 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002938 }
2939
Mark Horvathccaf7f82021-01-04 18:16:42 +01002940 return rc;
2941}
2942
2943/**
2944 * Reads all dependency TLVs of an image and verifies one after another to see
2945 * if they are all satisfied.
2946 *
2947 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002948 *
2949 * @return 0 if dependencies are met; nonzero otherwise.
2950 */
2951static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002952boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002953{
2954 uint32_t active_slot;
2955 const struct flash_area *fap;
2956 struct image_tlv_iter it;
2957 struct image_dependency dep;
2958 uint32_t off;
2959 uint16_t len;
2960 int area_id;
2961 int rc;
2962
Raef Colesfe57e7d2021-10-15 11:07:09 +01002963 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002964
2965 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2966 active_slot);
2967 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002968 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002969 rc = BOOT_EFLASH;
2970 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002971 }
2972
Mark Horvathccaf7f82021-01-04 18:16:42 +01002973 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2974 IMAGE_TLV_DEPENDENCY, true);
2975 if (rc != 0) {
2976 goto done;
2977 }
David Vinczee574f2d2020-07-10 11:42:03 +02002978
Mark Horvathccaf7f82021-01-04 18:16:42 +01002979 while (true) {
2980 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2981 if (rc < 0) {
2982 return -1;
2983 } else if (rc > 0) {
2984 rc = 0;
2985 break;
2986 }
David Vinczee574f2d2020-07-10 11:42:03 +02002987
Mark Horvathccaf7f82021-01-04 18:16:42 +01002988 if (len != sizeof(dep)) {
2989 rc = BOOT_EBADIMAGE;
2990 goto done;
2991 }
2992
2993 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2994 fap, off, &dep, len);
2995 if (rc != 0) {
2996 rc = BOOT_EFLASH;
2997 goto done;
2998 }
2999
3000 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
3001 rc = BOOT_EBADARGS;
3002 goto done;
3003 }
3004
Raef Colesfe57e7d2021-10-15 11:07:09 +01003005 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003006 if (rc != 0) {
3007 /* Dependency not satisfied. */
3008 goto done;
3009 }
3010 }
3011
3012done:
3013 flash_area_close(fap);
3014 return rc;
3015}
3016
3017/**
3018 * Checks the dependency of all the active slots. If an image found with
3019 * invalid or not satisfied dependencies the image is removed from SRAM (in
3020 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
3021 *
3022 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003023 *
3024 * @return 0 if dependencies are met; nonzero otherwise.
3025 */
3026static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003027boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003028{
3029 int rc = -1;
3030 uint32_t active_slot;
3031
3032 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003033 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3034 continue;
3035 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01003036 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003037 if (rc != 0) {
3038 /* Dependencies not met or invalid dependencies. */
3039
3040#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003041 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003042#endif /* MCUBOOT_RAM_LOAD */
3043
Raef Colesfe57e7d2021-10-15 11:07:09 +01003044 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
3045 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3046 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003047
3048 return rc;
3049 }
3050 }
3051
3052 return rc;
3053}
3054#endif /* (BOOT_IMAGE_NUMBER > 1) */
3055
3056/**
3057 * Tries to load a slot for all the images with validation.
3058 *
3059 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003060 *
3061 * @return 0 on success; nonzero on failure.
3062 */
Michael Grand5047f032022-11-24 16:49:56 +01003063fih_ret
Raef Colesfe57e7d2021-10-15 11:07:09 +01003064boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003065{
3066 uint32_t active_slot;
3067 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003068 fih_ret fih_rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003069
3070 /* Go over all the images and try to load one */
3071 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3072 /* All slots tried until a valid image found. Breaking from this loop
3073 * means that a valid image found or already loaded. If no slot is
3074 * found the function returns with error code. */
3075 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003076 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003077 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003078 if (active_slot != NO_ACTIVE_SLOT){
3079 /* A slot is already active, go to next image. */
3080 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003081 }
David Vincze505fba22020-10-22 13:53:29 +02003082
Raef Colesfe57e7d2021-10-15 11:07:09 +01003083 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003084 if (active_slot == NO_ACTIVE_SLOT) {
3085 BOOT_LOG_INF("No slot to load for image %d",
3086 BOOT_CURR_IMG(state));
3087 FIH_RET(FIH_FAILURE);
3088 }
3089
3090 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003091 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003092
Raef Colesf11de642021-10-15 11:11:56 +01003093#if BOOT_IMAGE_NUMBER > 1
3094 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3095 continue;
3096 }
3097#endif
3098
Mark Horvathccaf7f82021-01-04 18:16:42 +01003099#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003100 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003101 if (rc != 0) {
3102 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003103 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3104 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003105 continue;
3106 }
George Becksteind4d90f82021-05-11 02:00:00 -04003107
David Vincze505fba22020-10-22 13:53:29 +02003108#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003109 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003110 if (rc != 0) {
3111 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003112 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3113 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003114 continue;
3115 }
3116#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003117#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003118
Tamas Banfe031092020-09-10 17:32:39 +02003119#ifdef MCUBOOT_RAM_LOAD
3120 /* Image is first loaded to RAM and authenticated there in order to
3121 * prevent TOCTOU attack during image copy. This could be applied
3122 * when loading images from external (untrusted) flash to internal
3123 * (trusted) RAM and image is authenticated before copying.
3124 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003125 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003126 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003127 /* Image cannot be ramloaded. */
3128 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003129 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3130 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003131 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003132 }
3133#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003134
3135 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01003136 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003137 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003138#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003139 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003140#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003141 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3142 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003143 continue;
David Vincze505fba22020-10-22 13:53:29 +02003144 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003145
3146 /* Valid image loaded from a slot, go to next image. */
3147 break;
3148 }
3149 }
3150
3151 FIH_RET(FIH_SUCCESS);
3152}
3153
3154/**
3155 * Updates the security counter for the current image.
3156 *
3157 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003158 *
3159 * @return 0 on success; nonzero on failure.
3160 */
3161static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003162boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003163{
3164#ifdef MCUBOOT_HW_ROLLBACK_PROT
3165 int rc;
3166
3167 /* Update the stored security counter with the newer (active) image's
3168 * security counter value.
3169 */
David Vincze1c456242021-06-29 15:25:24 +02003170#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003171 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3172 * security counter can be increased only after reboot, if the image
3173 * has been confirmed at runtime (the image_ok flag has been set).
3174 * This way a 'revert' can be performed when it's necessary.
3175 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003176 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003177#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003178 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003179 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3180 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003181 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003182 BOOT_LOG_ERR("Security counter update failed after image "
3183 "validation.");
3184 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003185 }
David Vincze1c456242021-06-29 15:25:24 +02003186#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003187 }
3188#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003189
Mark Horvathccaf7f82021-01-04 18:16:42 +01003190 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003191
Mark Horvathccaf7f82021-01-04 18:16:42 +01003192#else /* MCUBOOT_HW_ROLLBACK_PROT */
3193 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003194 return 0;
3195#endif
3196}
3197
Michael Grand5047f032022-11-24 16:49:56 +01003198fih_ret
Mark Horvathccaf7f82021-01-04 18:16:42 +01003199context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3200{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003201 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003202 FIH_DECLARE(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003203
Raef Colesfe57e7d2021-10-15 11:07:09 +01003204 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003205 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003206 goto out;
3207 }
3208
Mark Horvathccaf7f82021-01-04 18:16:42 +01003209#if (BOOT_IMAGE_NUMBER > 1)
3210 while (true) {
3211#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003212 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Michael Grand5047f032022-11-24 16:49:56 +01003213 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
3214 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003215 goto out;
3216 }
3217
3218#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003219 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003220 if (rc != 0) {
3221 /* Dependency check failed for an image, it has been removed from
3222 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3223 * unavailable. Try to load an image from another slot.
3224 */
3225 continue;
3226 }
3227 /* Dependency check was successful. */
3228 break;
3229 }
3230#endif
3231
3232 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003233#if BOOT_IMAGE_NUMBER > 1
3234 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3235 continue;
3236 }
3237#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003238 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003239 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003240 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003241 goto out;
3242 }
3243
Raef Colesfe57e7d2021-10-15 11:07:09 +01003244 rc = boot_add_shared_data(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003245 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003246 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003247 goto out;
3248 }
3249 }
3250
3251 /* All image loaded successfully. */
3252#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003253 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003254#endif
3255
Raef Colesfe57e7d2021-10-15 11:07:09 +01003256 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003257
David Vinczee574f2d2020-07-10 11:42:03 +02003258out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003259 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003260
Michael Grand5047f032022-11-24 16:49:56 +01003261 if (rc != 0) {
3262 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003263 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003264
Mark Horvathccaf7f82021-01-04 18:16:42 +01003265 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003266}
Tamas Banfe031092020-09-10 17:32:39 +02003267#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003268
3269/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003270 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003271 * appropriate, and tells you what address to boot from.
3272 *
3273 * @param rsp On success, indicates how booting should occur.
3274 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003275 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003276 */
Michael Grand5047f032022-11-24 16:49:56 +01003277fih_ret
David Vinczee574f2d2020-07-10 11:42:03 +02003278boot_go(struct boot_rsp *rsp)
3279{
Michael Grand5047f032022-11-24 16:49:56 +01003280 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003281
3282 boot_state_clear(NULL);
3283
Raef Colese8fe6cf2020-05-26 13:07:40 +01003284 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3285 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003286}
Raef Colesf11de642021-10-15 11:11:56 +01003287
3288/**
3289 * Prepares the booting process, considering only a single image. This function
3290 * moves images around in flash as appropriate, and tells you what address to
3291 * boot from.
3292 *
3293 * @param rsp On success, indicates how booting should occur.
3294 *
3295 * @param image_id The image ID to prepare the boot process for.
3296 *
3297 * @return FIH_SUCCESS on success; nonzero on failure.
3298 */
Michael Grand5047f032022-11-24 16:49:56 +01003299fih_ret
Raef Colesf11de642021-10-15 11:11:56 +01003300boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3301{
Michael Grand5047f032022-11-24 16:49:56 +01003302 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003303
3304 if (image_id >= BOOT_IMAGE_NUMBER) {
3305 FIH_RET(FIH_FAILURE);
3306 }
3307
3308#if BOOT_IMAGE_NUMBER > 1
3309 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3310 boot_data.img_mask[image_id] = 0;
3311#endif
3312
3313 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3314 FIH_RET(fih_rc);
3315}
3316
3317/**
3318 * Clears the boot state, so that previous operations have no effect on new
3319 * ones.
3320 *
3321 * @param state The state that should be cleared. If the value
3322 * is NULL, the default bootloader state will be
3323 * cleared.
3324 */
3325void boot_state_clear(struct boot_loader_state *state)
3326{
3327 if (state != NULL) {
3328 memset(state, 0, sizeof(struct boot_loader_state));
3329 } else {
3330 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3331 }
3332}