blob: 311e72e1bda8a0154e53cb913e718c58a39fb30c [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 }
INFINEON\DovhalA94360d52023-01-18 17:21:56 +0200185 /* At least one image must be active, otherwise skip the execution */
186 if (BOOT_CURR_IMG(state) >= BOOT_IMAGE_NUMBER) {
187 return;
188 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100189#endif
190
191#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100192 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100193#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100194 active_slot = BOOT_PRIMARY_SLOT;
195#endif
196
Dominik Ermel260ae092021-04-23 05:38:45 +0000197 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100198 rsp->br_image_off = boot_img_slot_off(state, active_slot);
199 rsp->br_hdr = boot_img_hdr(state, active_slot);
200}
201
202/**
203 * Closes all flash areas.
204 *
205 * @param state Boot loader status information.
206 */
207static void
208close_all_flash_areas(struct boot_loader_state *state)
209{
210 uint32_t slot;
211
212 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100213#if BOOT_IMAGE_NUMBER > 1
214 if (state->img_mask[BOOT_CURR_IMG(state)]) {
215 continue;
216 }
217#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100218#if MCUBOOT_SWAP_USING_SCRATCH
219 flash_area_close(BOOT_SCRATCH_AREA(state));
220#endif
221 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
222 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
223 }
224 }
225}
226
Tamas Banfe031092020-09-10 17:32:39 +0200227#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600228/*
229 * Compute the total size of the given image. Includes the size of
230 * the TLVs.
231 */
Tamas Banfe031092020-09-10 17:32:39 +0200232#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600233static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300234boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600235{
236 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300237 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300238 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300239 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600240 int area_id;
241 int rc;
242
Fabio Utzig10ee6482019-08-01 12:04:52 -0300243#if (BOOT_IMAGE_NUMBER == 1)
244 (void)state;
245#endif
246
247 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600248 rc = flash_area_open(area_id, &fap);
249 if (rc != 0) {
250 rc = BOOT_EFLASH;
251 goto done;
252 }
253
Fabio Utzig61fd8882019-09-14 20:00:20 -0300254 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
255
256 if (flash_area_read(fap, off, &info, sizeof(info))) {
257 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600258 goto done;
259 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300260
Fabio Utzige52c08e2019-09-11 19:32:00 -0300261 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
262 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
263 if (protect_tlv_size != info.it_tlv_tot) {
264 rc = BOOT_EBADIMAGE;
265 goto done;
266 }
267
268 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
269 rc = BOOT_EFLASH;
270 goto done;
271 }
272 } else if (protect_tlv_size != 0) {
273 rc = BOOT_EBADIMAGE;
274 goto done;
275 }
276
Fabio Utzig61fd8882019-09-14 20:00:20 -0300277 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
278 rc = BOOT_EBADIMAGE;
279 goto done;
280 }
281
Fabio Utzige52c08e2019-09-11 19:32:00 -0300282 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600283 rc = 0;
284
285done:
286 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300287 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600288}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300289#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600290
Tamas Banfe031092020-09-10 17:32:39 +0200291#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700292static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300293boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294{
David Brownab449182019-11-15 09:32:52 -0700295 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300296#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700297 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300298#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299
300 /* Figure out what size to write update status update as. The size depends
301 * on what the minimum write size is for scratch area, active image slot.
302 * We need to use the bigger of those 2 values.
303 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300304 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300305#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300306 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800307 if (align > elem_sz) {
308 elem_sz = align;
309 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300310#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311
312 return elem_sz;
313}
314
Fabio Utzig10ee6482019-08-01 12:04:52 -0300315static int
316boot_initialize_area(struct boot_loader_state *state, int flash_area)
317{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000318 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
319 boot_sector_t *out_sectors;
320 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300321 int rc;
322
323 num_sectors = BOOT_MAX_IMG_SECTORS;
324
325 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
326 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
327 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
328 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
329 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
330 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300331#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300332 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
333 out_sectors = state->scratch.sectors;
334 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300335#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300336 } else {
337 return BOOT_EFLASH;
338 }
339
Dominik Ermel51c8d762021-05-24 15:34:01 +0000340#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300341 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000342#else
343 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
344 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
345#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300346 if (rc != 0) {
347 return rc;
348 }
349 *out_num_sectors = num_sectors;
350 return 0;
351}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353/**
354 * Determines the sector layout of both image slots and the scratch area.
355 * This information is necessary for calculating the number of bytes to erase
356 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300357 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358 */
359static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300360boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300362 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364
Fabio Utzig10ee6482019-08-01 12:04:52 -0300365 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300366
367 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368 if (rc != 0) {
369 return BOOT_EFLASH;
370 }
371
Fabio Utzig10ee6482019-08-01 12:04:52 -0300372 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800373 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200374 /* We need to differentiate from the primary image issue */
375 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376 }
377
Fabio Utzig12d59162019-11-28 10:01:59 -0300378#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300379 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200380 if (rc != 0) {
381 return BOOT_EFLASH;
382 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300383#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200384
Fabio Utzig10ee6482019-08-01 12:04:52 -0300385 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800386
387 return 0;
388}
389
Fabio Utzig12d59162019-11-28 10:01:59 -0300390void
391boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392{
Fabio Utzig4741c452019-12-19 15:32:41 -0300393#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000394 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300395#if MCUBOOT_SWAP_SAVE_ENCTLV
396 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
397#endif
398#endif /* MCUBOOT_ENC_IMAGES */
399
400 bs->use_scratch = 0;
401 bs->swap_size = 0;
402 bs->source = 0;
403
Fabio Utzig74aef312019-11-28 11:05:34 -0300404 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300405 bs->idx = BOOT_STATUS_IDX_0;
406 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700407 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300408}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800409
Fabio Utzig12d59162019-11-28 10:01:59 -0300410bool
411boot_status_is_reset(const struct boot_status *bs)
412{
Fabio Utzig74aef312019-11-28 11:05:34 -0300413 return (bs->op == BOOT_STATUS_OP_MOVE &&
414 bs->idx == BOOT_STATUS_IDX_0 &&
415 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416}
417
418/**
419 * Writes the supplied boot status to the flash file system. The boot status
420 * contains the current state of an in-progress image copy operation.
421 *
422 * @param bs The boot status to write.
423 *
424 * @return 0 on success; nonzero on failure.
425 */
426int
Fabio Utzig12d59162019-11-28 10:01:59 -0300427boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428{
429 const struct flash_area *fap;
430 uint32_t off;
431 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000432 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300433 uint8_t buf[BOOT_MAX_ALIGN];
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300434 uint32_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300435 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300437 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100438 * the trailer. Since in the last step the primary slot is erased, the
439 * first two status writes go to the scratch which will be copied to
440 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300441 */
442
Fabio Utzig12d59162019-11-28 10:01:59 -0300443#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300444 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445 /* Write to scratch. */
446 area_id = FLASH_AREA_IMAGE_SCRATCH;
447 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300448#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100449 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300450 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300451#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300453#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454
455 rc = flash_area_open(area_id, &fap);
456 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000457 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 }
459
460 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300461 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200462 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300463 erased_val = flash_area_erased_val(fap);
464 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700465 buf[0] = bs->state;
466
467 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 if (rc != 0) {
469 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 }
471
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000473
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 return rc;
475}
Tamas Banfe031092020-09-10 17:32:39 +0200476#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200477#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478
479/*
David Vinczec3084132020-02-18 14:50:47 +0100480 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481 */
Michael Grand5047f032022-11-24 16:49:56 +0100482static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300483boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
484 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300486 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300487 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300488 int rc;
Michael Grand5047f032022-11-24 16:49:56 +0100489 FIH_DECLARE(fih_rc, FIH_FAILURE);
Fabio Utzigba829042018-09-18 08:29:34 -0300490
Fabio Utzig10ee6482019-08-01 12:04:52 -0300491#if (BOOT_IMAGE_NUMBER == 1)
492 (void)state;
493#endif
494
Fabio Utzigba829042018-09-18 08:29:34 -0300495 (void)bs;
496 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300497
498 image_index = BOOT_CURR_IMG(state);
499
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000500/* In the case of ram loading the image has already been decrypted as it is
501 * decrypted when copied in ram */
502#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300503 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300504 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300505 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100506 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300507 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300508 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100509 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300510 }
511 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300512#endif
513
Raef Colese8fe6cf2020-05-26 13:07:40 +0100514 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
515 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300516
Raef Colese8fe6cf2020-05-26 13:07:40 +0100517 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518}
519
Tamas Banfe031092020-09-10 17:32:39 +0200520#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Michael Grand5047f032022-11-24 16:49:56 +0100521static fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -0800522split_image_check(struct image_header *app_hdr,
523 const struct flash_area *app_fap,
524 struct image_header *loader_hdr,
525 const struct flash_area *loader_fap)
526{
527 static void *tmpbuf;
528 uint8_t loader_hash[32];
Michael Grand5047f032022-11-24 16:49:56 +0100529 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530
531 if (!tmpbuf) {
532 tmpbuf = malloc(BOOT_TMPBUF_SZ);
533 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100534 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535 }
536 }
537
Raef Colese8fe6cf2020-05-26 13:07:40 +0100538 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
539 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
Michael Grand5047f032022-11-24 16:49:56 +0100540 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100541 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542 }
543
Raef Colese8fe6cf2020-05-26 13:07:40 +0100544 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
545 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
Raef Colese8fe6cf2020-05-26 13:07:40 +0100547out:
548 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800549}
Tamas Banfe031092020-09-10 17:32:39 +0200550#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
Fabio Utzig338a19f2018-12-03 08:37:08 -0200552/*
David Brown9bf95af2019-10-10 15:36:36 -0600553 * Check that this is a valid header. Valid means that the magic is
554 * correct, and that the sizes/offsets are "sane". Sane means that
555 * there is no overflow on the arithmetic, and that the result fits
556 * within the flash area we are in.
557 */
558static bool
559boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
560{
561 uint32_t size;
562
563 if (hdr->ih_magic != IMAGE_MAGIC) {
564 return false;
565 }
566
567 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
568 return false;
569 }
570
Dominik Ermel260ae092021-04-23 05:38:45 +0000571 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600572 return false;
573 }
574
575 return true;
576}
577
578/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200579 * Check that a memory area consists of a given value.
580 */
581static inline bool
582boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300583{
584 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200585 uint8_t *p = (uint8_t *)data;
586 for (i = 0; i < len; i++) {
587 if (val != p[i]) {
588 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300589 }
590 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200591 return true;
592}
593
594static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300595boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200596{
597 const struct flash_area *fap;
598 struct image_header *hdr;
599 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300600 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200601 int rc;
602
Fabio Utzig10ee6482019-08-01 12:04:52 -0300603 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300604 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200605 if (rc != 0) {
606 return -1;
607 }
608
609 erased_val = flash_area_erased_val(fap);
610 flash_area_close(fap);
611
Fabio Utzig10ee6482019-08-01 12:04:52 -0300612 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200613 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
614 return -1;
615 }
616
617 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300618}
619
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000620#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200621 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200622 defined(MCUBOOT_RAM_LOAD) || \
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +0200623 defined(MCUBOOT_DOWNGRADE_PREVENTION)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000624/**
Marek Pietaa95a41b2023-04-21 14:03:19 +0200625 * Compare image version numbers
626 *
627 * By default, the comparison does not take build number into account.
628 * Enable MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER to take the build number into account.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000629 *
David Vincze8b0b6372020-05-20 19:54:44 +0200630 * @param ver1 Pointer to the first image version to compare.
631 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000632 *
Marek Pietaa95a41b2023-04-21 14:03:19 +0200633 * @retval -1 If ver1 is less than ver2.
634 * @retval 0 If the image version numbers are equal.
635 * @retval 1 If ver1 is greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000636 */
637static int
David Vincze8b0b6372020-05-20 19:54:44 +0200638boot_version_cmp(const struct image_version *ver1,
639 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000640{
David Vincze8b0b6372020-05-20 19:54:44 +0200641 if (ver1->iv_major > ver2->iv_major) {
642 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000643 }
David Vincze8b0b6372020-05-20 19:54:44 +0200644 if (ver1->iv_major < ver2->iv_major) {
645 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000646 }
David Vincze8b0b6372020-05-20 19:54:44 +0200647 /* The major version numbers are equal, continue comparison. */
648 if (ver1->iv_minor > ver2->iv_minor) {
649 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000650 }
David Vincze8b0b6372020-05-20 19:54:44 +0200651 if (ver1->iv_minor < ver2->iv_minor) {
652 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000653 }
David Vincze8b0b6372020-05-20 19:54:44 +0200654 /* The minor version numbers are equal, continue comparison. */
655 if (ver1->iv_revision > ver2->iv_revision) {
656 return 1;
657 }
658 if (ver1->iv_revision < ver2->iv_revision) {
659 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000660 }
661
Marek Pietaa95a41b2023-04-21 14:03:19 +0200662#if defined(MCUBOOT_VERSION_CMP_USE_BUILD_NUMBER)
663 /* The revisions are equal, continue comparison. */
664 if (ver1->iv_build_num > ver2->iv_build_num) {
665 return 1;
666 }
667 if (ver1->iv_build_num < ver2->iv_build_num) {
668 return -1;
669 }
670#endif
671
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000672 return 0;
673}
674#endif
675
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000676#if defined(MCUBOOT_DIRECT_XIP)
677/**
678 * Check if image in slot has been set with specific ROM address to run from
679 * and whether the slot starts at that address.
680 *
681 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
682 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
683 * header matches the slot address;
684 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
685 * does not match the slot address.
686 */
687static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100688boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000689{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100690 uint32_t active_slot;
691 const struct image_header *hdr;
692 uint32_t f_off;
693
Raef Colesfe57e7d2021-10-15 11:07:09 +0100694 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100695 hdr = boot_img_hdr(state, active_slot);
696 f_off = boot_img_slot_off(state, active_slot);
697
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000698 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
699 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100700 ", skipping",
701 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000702 hdr->ih_load_addr);
703
704 /* If there is address mismatch, the image is not bootable from this
705 * slot.
706 */
707 return 1;
708 }
709 return 0;
710}
711#endif
712
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300713/*
714 * Check that there is a valid image in a slot
715 *
716 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100717 * FIH_SUCCESS if image was successfully validated
Michael Grand5047f032022-11-24 16:49:56 +0100718 * FIH_NO_BOOTABLE_IMAGE if no bootloable image was found
Raef Colese8fe6cf2020-05-26 13:07:40 +0100719 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300720 */
Michael Grand5047f032022-11-24 16:49:56 +0100721static fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -0300722boot_validate_slot(struct boot_loader_state *state, int slot,
723 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800724{
725 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400726 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300727 int area_id;
Michael Grand5047f032022-11-24 16:49:56 +0100728 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800729 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300730
Fabio Utzig10ee6482019-08-01 12:04:52 -0300731 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300732 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800733 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100734 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735 }
736
Fabio Utzig10ee6482019-08-01 12:04:52 -0300737 hdr = boot_img_hdr(state, slot);
738 if (boot_check_header_erased(state, slot) == 0 ||
739 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300740
741#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
742 /*
743 * This fixes an issue where an image might be erased, but a trailer
744 * be left behind. It can happen if the image is in the secondary slot
745 * and did not pass validation, in which case the whole slot is erased.
746 * If during the erase operation, a reset occurs, parts of the slot
747 * might have been erased while some did not. The concerning part is
748 * the trailer because it might disable a new image from being loaded
749 * through mcumgr; so we just get rid of the trailer here, if the header
750 * is erased.
751 */
752 if (slot != BOOT_PRIMARY_SLOT) {
753 swap_erase_trailer_sectors(state, fap);
754 }
755#endif
756
David Vincze2d736ad2019-02-18 11:50:22 +0100757 /* No bootable image in slot; continue booting from the primary slot. */
Michael Grand5047f032022-11-24 16:49:56 +0100758 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200759 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300760 }
761
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000762#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
763 if (slot != BOOT_PRIMARY_SLOT) {
764 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200765 rc = boot_version_cmp(
766 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
767 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
768 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000769 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000770 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000771 /* Image in the secondary slot does not satisfy version requirement.
772 * Erase the image and continue booting from the primary slot.
773 */
Michael Grand5047f032022-11-24 16:49:56 +0100774 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000775 goto out;
776 }
777 }
778#endif
Michael Grand5047f032022-11-24 16:49:56 +0100779 BOOT_HOOK_CALL_FIH(boot_image_check_hook, FIH_BOOT_HOOK_REGULAR,
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200780 fih_rc, BOOT_CURR_IMG(state), slot);
Michael Grand5047f032022-11-24 16:49:56 +0100781 if (FIH_EQ(fih_rc, FIH_BOOT_HOOK_REGULAR))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200782 {
783 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
784 }
Michael Grand5047f032022-11-24 16:49:56 +0100785 if (!boot_is_header_valid(hdr, fap) || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200786 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000787 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200788 /* Image is invalid, erase it to prevent further unnecessary
789 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700790 */
791 }
David Brown098de832019-12-10 11:58:01 -0700792#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100793 BOOT_LOG_ERR("Image in the %s slot is not valid!",
794 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700795#endif
Michael Grand5047f032022-11-24 16:49:56 +0100796 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200797 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800798 }
799
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000800#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
801 /* Verify that the image in the secondary slot has a reset address
802 * located in the primary slot. This is done to avoid users incorrectly
803 * overwriting an application written to the incorrect slot.
804 * This feature is only supported by ARM platforms.
805 */
806 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
807 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
808 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
809 uint32_t reset_value = 0;
810 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
811
812 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
813 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +0100814 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000815 goto out;
816 }
817
818 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
819 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
820 BOOT_LOG_ERR("Erasing image from secondary slot");
821
822 /* The vector table in the image located in the secondary
823 * slot does not target the primary slot. This might
824 * indicate that the image was loaded to the wrong slot.
825 *
826 * Erase the image and continue booting from the primary slot.
827 */
828 flash_area_erase(fap, 0, fap->fa_size);
Michael Grand5047f032022-11-24 16:49:56 +0100829 fih_rc = FIH_NO_BOOTABLE_IMAGE;
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000830 goto out;
831 }
832 }
833#endif
834
Fabio Utzig338a19f2018-12-03 08:37:08 -0200835out:
836 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100837
838 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839}
840
David Vinczec3084132020-02-18 14:50:47 +0100841#ifdef MCUBOOT_HW_ROLLBACK_PROT
842/**
843 * Updates the stored security counter value with the image's security counter
844 * value which resides in the given slot, only if it's greater than the stored
845 * value.
846 *
847 * @param image_index Index of the image to determine which security
848 * counter to update.
849 * @param slot Slot number of the image.
850 * @param hdr Pointer to the image header structure of the image
851 * that is currently stored in the given slot.
852 *
853 * @return 0 on success; nonzero on failure.
854 */
855static int
856boot_update_security_counter(uint8_t image_index, int slot,
857 struct image_header *hdr)
858{
859 const struct flash_area *fap = NULL;
860 uint32_t img_security_cnt;
861 int rc;
862
863 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
864 &fap);
865 if (rc != 0) {
866 rc = BOOT_EFLASH;
867 goto done;
868 }
869
870 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
871 if (rc != 0) {
872 goto done;
873 }
874
875 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
876 if (rc != 0) {
877 goto done;
878 }
879
880done:
881 flash_area_close(fap);
882 return rc;
883}
884#endif /* MCUBOOT_HW_ROLLBACK_PROT */
885
Tamas Banfe031092020-09-10 17:32:39 +0200886#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200887/**
888 * Determines which swap operation to perform, if any. If it is determined
889 * that a swap operation is required, the image in the secondary slot is checked
890 * for validity. If the image in the secondary slot is invalid, it is erased,
891 * and a swap type of "none" is indicated.
892 *
893 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
894 */
895static int
896boot_validated_swap_type(struct boot_loader_state *state,
897 struct boot_status *bs)
898{
899 int swap_type;
Michael Grand5047f032022-11-24 16:49:56 +0100900 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczee574f2d2020-07-10 11:42:03 +0200901
902 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
903 if (BOOT_IS_UPGRADE(swap_type)) {
904 /* Boot loader wants to switch to the secondary slot.
905 * Ensure image is valid.
906 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100907 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +0100908 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
909 if (FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100910 swap_type = BOOT_SWAP_TYPE_NONE;
911 } else {
912 swap_type = BOOT_SWAP_TYPE_FAIL;
913 }
David Vinczee574f2d2020-07-10 11:42:03 +0200914 }
915 }
916
917 return swap_type;
918}
David Brown94ed12c2021-05-26 16:28:14 -0600919#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200920
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922 * Erases a region of flash.
923 *
Fabio Utzigba829042018-09-18 08:29:34 -0300924 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800925 * @param off The offset within the flash area to start the
926 * erase.
927 * @param sz The number of bytes to erase.
928 *
929 * @return 0 on success; nonzero on failure.
930 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300931int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300932boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933{
Fabio Utzigba829042018-09-18 08:29:34 -0300934 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800935}
936
David Brown94ed12c2021-05-26 16:28:14 -0600937#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800938/**
939 * Copies the contents of one flash region to another. You must erase the
940 * destination region prior to calling this function.
941 *
942 * @param flash_area_id_src The ID of the source flash area.
943 * @param flash_area_id_dst The ID of the destination flash area.
944 * @param off_src The offset within the source flash area to
945 * copy from.
946 * @param off_dst The offset within the destination flash area to
947 * copy to.
948 * @param sz The number of bytes to copy.
949 *
950 * @return 0 on success; nonzero on failure.
951 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300952int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300953boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300954 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300955 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800956 uint32_t off_src, uint32_t off_dst, uint32_t sz)
957{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800958 uint32_t bytes_copied;
959 int chunk_sz;
960 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300961#ifdef MCUBOOT_ENC_IMAGES
962 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300963 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300964 size_t blk_off;
965 struct image_header *hdr;
966 uint16_t idx;
967 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300968 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300969#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800970
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000971 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300972
973#if !defined(MCUBOOT_ENC_IMAGES)
974 (void)state;
975#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800976
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977 bytes_copied = 0;
978 while (bytes_copied < sz) {
979 if (sz - bytes_copied > sizeof buf) {
980 chunk_sz = sizeof buf;
981 } else {
982 chunk_sz = sz - bytes_copied;
983 }
984
985 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
986 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300987 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800988 }
989
Fabio Utzigba829042018-09-18 08:29:34 -0300990#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300991 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000992 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
993 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
994 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
995 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100996 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300997 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300998#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300999 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +00001000 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +01001001 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001002 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001003 off = off_dst;
1004 }
Fabio Utzig74aef312019-11-28 11:05:34 -03001005#else
1006 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +00001007 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -03001008 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1009 }
1010#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001011 if (IS_ENCRYPTED(hdr)) {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001012 uint32_t abs_off = off + bytes_copied;
1013 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -03001014 /* do not decrypt header */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001015 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001016 /* The lower part of the chunk contains header data */
1017 blk_off = 0;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001018 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1019 idx = hdr->ih_hdr_size - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001020 } else {
1021 /* The chunk contains exclusively header data */
1022 blk_sz = 0; /* nothing to decrypt */
1023 }
Fabio Utzigba829042018-09-18 08:29:34 -03001024 } else {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001025 idx = 0;
1026 blk_sz = chunk_sz;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001027 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001028 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001029
1030 if (blk_sz > 0)
1031 {
1032 tlv_off = BOOT_TLV_OFF(hdr);
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001033 if (abs_off + chunk_sz > tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001034 /* do not decrypt TLVs */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001035 if (abs_off >= tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001036 blk_sz = 0;
1037 } else {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001038 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001039 }
Fabio Utzigba829042018-09-18 08:29:34 -03001040 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001041 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001042 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001043 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001044 }
Fabio Utzigba829042018-09-18 08:29:34 -03001045 }
1046 }
1047#endif
1048
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1050 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001051 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001052 }
1053
1054 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001055
1056 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001057 }
1058
Fabio Utzigba829042018-09-18 08:29:34 -03001059 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001060}
1061
Christopher Collins92ea77f2016-12-12 15:59:26 -08001062/**
David Vincze2d736ad2019-02-18 11:50:22 +01001063 * Overwrite primary slot with the image contained in the secondary slot.
1064 * If a prior copy operation was interrupted by a system reset, this function
1065 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001066 *
1067 * @param bs The current boot status. This function reads
1068 * this struct to determine if it is resuming
1069 * an interrupted swap operation. This
1070 * function writes the updated status to this
1071 * function on return.
1072 *
1073 * @return 0 on success; nonzero on failure.
1074 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001075#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001076static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001077boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001078{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001079 size_t sect_count;
1080 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001081 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001082 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001083 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001084 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001085 const struct flash_area *fap_primary_slot;
1086 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001087 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001088
Fabio Utzigb4f88102020-10-04 10:16:24 -03001089#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1090 uint32_t sector;
1091 uint32_t trailer_sz;
1092 uint32_t off;
1093 uint32_t sz;
1094#endif
1095
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001096 (void)bs;
1097
Fabio Utzig13d9e352017-10-05 20:32:31 -03001098#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1099 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001100 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001101 assert(rc == 0);
1102#endif
David Brown17609d82017-05-05 09:41:34 -06001103
David Vincze2d736ad2019-02-18 11:50:22 +01001104 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1105 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001106
Fabio Utzigb0f04732019-07-31 09:49:19 -03001107 image_index = BOOT_CURR_IMG(state);
1108
1109 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1110 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001111 assert (rc == 0);
1112
Fabio Utzigb0f04732019-07-31 09:49:19 -03001113 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1114 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001115 assert (rc == 0);
1116
Fabio Utzig10ee6482019-08-01 12:04:52 -03001117 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001118 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001119 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001120 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001121 assert(rc == 0);
1122
Fabio Utzig13d9e352017-10-05 20:32:31 -03001123#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001124 if ((size + this_size) >= src_size) {
1125 size += src_size - size;
1126 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001127 break;
1128 }
1129#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001130
1131 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001132 }
1133
Fabio Utzigb4f88102020-10-04 10:16:24 -03001134#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1135 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1136 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1137 sz = 0;
1138 do {
1139 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1140 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1141 sector--;
1142 } while (sz < trailer_sz);
1143
1144 rc = boot_erase_region(fap_primary_slot, off, sz);
1145 assert(rc == 0);
1146#endif
1147
Fabio Utzigba829042018-09-18 08:29:34 -03001148#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001149 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001150 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001151 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001152 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001153
Fabio Utzigba829042018-09-18 08:29:34 -03001154 if (rc < 0) {
1155 return BOOT_EBADIMAGE;
1156 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001157 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001158 return BOOT_EBADIMAGE;
1159 }
1160 }
1161#endif
1162
David Vincze2d736ad2019-02-18 11:50:22 +01001163 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1164 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001165 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001166 if (rc != 0) {
1167 return rc;
1168 }
1169
1170#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1171 rc = boot_write_magic(fap_primary_slot);
1172 if (rc != 0) {
1173 return rc;
1174 }
1175#endif
David Brown17609d82017-05-05 09:41:34 -06001176
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001177 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1178 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1179 if (rc != 0) {
1180 return rc;
1181 }
1182
David Vinczec3084132020-02-18 14:50:47 +01001183#ifdef MCUBOOT_HW_ROLLBACK_PROT
1184 /* Update the stored security counter with the new image's security counter
1185 * value. Both slots hold the new image at this point, but the secondary
1186 * slot's image header must be passed since the image headers in the
1187 * boot_data structure have not been updated yet.
1188 */
1189 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1190 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1191 if (rc != 0) {
1192 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1193 return rc;
1194 }
1195#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1196
Fabio Utzig13d9e352017-10-05 20:32:31 -03001197 /*
1198 * Erases header and trailer. The trailer is erased because when a new
1199 * image is written without a trailer as is the case when using newt, the
1200 * trailer that was left might trigger a new upgrade.
1201 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001202 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001203 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001204 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1205 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001206 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001207 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001208 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001209 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001210 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1211 last_sector),
1212 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1213 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001214 assert(rc == 0);
1215
David Vincze2d736ad2019-02-18 11:50:22 +01001216 flash_area_close(fap_primary_slot);
1217 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001218
David Vincze2d736ad2019-02-18 11:50:22 +01001219 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001220
1221 return 0;
1222}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001223#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001224
Christopher Collinsa1c12042019-05-23 14:00:28 -07001225#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001226/**
1227 * Swaps the two images in flash. If a prior copy operation was interrupted
1228 * by a system reset, this function completes that operation.
1229 *
1230 * @param bs The current boot status. This function reads
1231 * this struct to determine if it is resuming
1232 * an interrupted swap operation. This
1233 * function writes the updated status to this
1234 * function on return.
1235 *
1236 * @return 0 on success; nonzero on failure.
1237 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001238static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001239boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001240{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001241 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001242 const struct flash_area *fap;
Dominik Ermel472d4c72023-02-10 13:29:58 +00001243#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzigba829042018-09-18 08:29:34 -03001244 uint8_t slot;
1245 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001246#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001247 uint32_t size;
1248 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001249 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001250 int rc;
1251
1252 /* FIXME: just do this if asked by user? */
1253
1254 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001255 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001256
Fabio Utzig12d59162019-11-28 10:01:59 -03001257 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001258 /*
1259 * No swap ever happened, so need to find the largest image which
1260 * will be used to determine the amount of sectors to swap.
1261 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001262 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001263 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001264 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001265 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001266 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001267
Fabio Utzigba829042018-09-18 08:29:34 -03001268#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001269 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001270 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001271 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001272 assert(rc >= 0);
1273
1274 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001275 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001276 assert(rc == 0);
1277 } else {
1278 rc = 0;
1279 }
1280 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001281 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001282 }
1283#endif
1284
Fabio Utzig10ee6482019-08-01 12:04:52 -03001285 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001286 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001287 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001288 assert(rc == 0);
1289 }
1290
Fabio Utzigba829042018-09-18 08:29:34 -03001291#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001292 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001293 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001294 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001295 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001296 assert(rc >= 0);
1297
1298 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001299 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001300 assert(rc == 0);
1301 } else {
1302 rc = 0;
1303 }
1304 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001305 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001306 }
1307#endif
1308
Fabio Utzig46490722017-09-04 15:34:32 -03001309 if (size > copy_size) {
1310 copy_size = size;
1311 }
1312
1313 bs->swap_size = copy_size;
1314 } else {
1315 /*
1316 * If a swap was under way, the swap_size should already be present
1317 * in the trailer...
1318 */
Dominik Ermel472d4c72023-02-10 13:29:58 +00001319
1320 rc = boot_find_status(image_index, &fap);
1321 assert(fap != NULL);
1322 rc = boot_read_swap_size(fap, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001323 assert(rc == 0);
1324
1325 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001326
1327#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001328 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Dominik Ermel472d4c72023-02-10 13:29:58 +00001329 rc = boot_read_enc_key(fap, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001330 assert(rc == 0);
1331
1332 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001333 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001334 break;
1335 }
1336 }
1337
1338 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001339 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001340 }
1341 }
1342#endif
Dominik Ermel472d4c72023-02-10 13:29:58 +00001343 flash_area_close(fap);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001344 }
1345
Fabio Utzig12d59162019-11-28 10:01:59 -03001346 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001347
David Vincze2d736ad2019-02-18 11:50:22 +01001348#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001349 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001350 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001351 BOOT_LOG_WRN("%d status write fails performing the swap",
1352 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001353 }
1354#endif
Sigvart Hovland3fd4cd42022-09-09 13:21:18 +02001355 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1356 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001357
Christopher Collins92ea77f2016-12-12 15:59:26 -08001358 return 0;
1359}
David Brown17609d82017-05-05 09:41:34 -06001360#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001361
David Vinczee32483f2019-06-13 10:46:24 +02001362#if (BOOT_IMAGE_NUMBER > 1)
1363/**
1364 * Check the image dependency whether it is satisfied and modify
1365 * the swap type if necessary.
1366 *
1367 * @param dep Image dependency which has to be verified.
1368 *
1369 * @return 0 on success; nonzero on failure.
1370 */
1371static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001372boot_verify_slot_dependency(struct boot_loader_state *state,
1373 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001374{
1375 struct image_version *dep_version;
1376 size_t dep_slot;
1377 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001378 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001379
1380 /* Determine the source of the image which is the subject of
1381 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001382 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001383 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1384 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001385 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001386
David Vincze8b0b6372020-05-20 19:54:44 +02001387 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1388 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001389 /* Dependency not satisfied.
1390 * Modify the swap type to decrease the version number of the image
1391 * (which will be located in the primary slot after the boot process),
1392 * consequently the number of unsatisfied dependencies will be
1393 * decreased or remain the same.
1394 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001395 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001396 case BOOT_SWAP_TYPE_TEST:
1397 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001398 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001399 break;
1400 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001401 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001402 break;
1403 default:
1404 break;
1405 }
David Vincze8b0b6372020-05-20 19:54:44 +02001406 } else {
1407 /* Dependency satisfied. */
1408 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001409 }
1410
1411 return rc;
1412}
1413
1414/**
1415 * Read all dependency TLVs of an image from the flash and verify
1416 * one after another to see if they are all satisfied.
1417 *
1418 * @param slot Image slot number.
1419 *
1420 * @return 0 on success; nonzero on failure.
1421 */
1422static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001423boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001424{
1425 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001426 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001427 struct image_dependency dep;
1428 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001429 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001430 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001431 int rc;
1432
Fabio Utzig10ee6482019-08-01 12:04:52 -03001433 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001434 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001435 if (rc != 0) {
1436 rc = BOOT_EFLASH;
1437 goto done;
1438 }
1439
Fabio Utzig61fd8882019-09-14 20:00:20 -03001440 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1441 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001442 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001443 goto done;
1444 }
1445
Fabio Utzig61fd8882019-09-14 20:00:20 -03001446 while (true) {
1447 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1448 if (rc < 0) {
1449 return -1;
1450 } else if (rc > 0) {
1451 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001452 break;
1453 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001454
1455 if (len != sizeof(dep)) {
1456 rc = BOOT_EBADIMAGE;
1457 goto done;
1458 }
1459
1460 rc = flash_area_read(fap, off, &dep, len);
1461 if (rc != 0) {
1462 rc = BOOT_EFLASH;
1463 goto done;
1464 }
1465
1466 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1467 rc = BOOT_EBADARGS;
1468 goto done;
1469 }
1470
1471 /* Verify dependency and modify the swap type if not satisfied. */
1472 rc = boot_verify_slot_dependency(state, &dep);
1473 if (rc != 0) {
1474 /* Dependency not satisfied. */
1475 goto done;
1476 }
David Vinczee32483f2019-06-13 10:46:24 +02001477 }
1478
1479done:
1480 flash_area_close(fap);
1481 return rc;
1482}
1483
1484/**
David Vinczee32483f2019-06-13 10:46:24 +02001485 * Iterate over all the images and verify whether the image dependencies in the
1486 * TLV area are all satisfied and update the related swap type if necessary.
1487 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001488static int
1489boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001490{
Erik Johnson49063752020-02-06 09:59:31 -06001491 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001492 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001493
Fabio Utzig10ee6482019-08-01 12:04:52 -03001494 BOOT_CURR_IMG(state) = 0;
1495 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001496 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1497 BOOT_CURR_IMG(state)++;
1498 continue;
1499 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001500 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1501 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1502 slot = BOOT_SECONDARY_SLOT;
1503 } else {
1504 slot = BOOT_PRIMARY_SLOT;
1505 }
1506
1507 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001508 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001509 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001510 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001511 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001512 /* Cannot upgrade due to non-met dependencies, so disable all
1513 * image upgrades.
1514 */
1515 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1516 BOOT_CURR_IMG(state) = idx;
1517 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1518 }
1519 break;
David Vinczee32483f2019-06-13 10:46:24 +02001520 }
1521 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001522 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001523}
1524#endif /* (BOOT_IMAGE_NUMBER > 1) */
1525
Christopher Collins92ea77f2016-12-12 15:59:26 -08001526/**
David Vinczeba3bd602019-06-17 16:01:43 +02001527 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001528 *
David Vinczeba3bd602019-06-17 16:01:43 +02001529 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001530 *
1531 * @return 0 on success; nonzero on failure.
1532 */
1533static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001534boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001535{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001536 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001537#ifndef MCUBOOT_OVERWRITE_ONLY
1538 uint8_t swap_type;
1539#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001540
David Vinczeba3bd602019-06-17 16:01:43 +02001541 /* At this point there are no aborted swaps. */
1542#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001544#elif defined(MCUBOOT_BOOTSTRAP)
1545 /* Check if the image update was triggered by a bad image in the
1546 * primary slot (the validity of the image in the secondary slot had
1547 * already been checked).
1548 */
Michael Grand5047f032022-11-24 16:49:56 +01001549 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001550 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1551 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001552 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001553 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001554 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001555 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001556 }
1557#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001558 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001559#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001560 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001561
1562#ifndef MCUBOOT_OVERWRITE_ONLY
1563 /* The following state needs image_ok be explicitly set after the
1564 * swap was finished to avoid a new revert.
1565 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001566 swap_type = BOOT_SWAP_TYPE(state);
1567 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1568 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001569 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001570 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001571 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001572 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001573 }
1574
David Vinczec3084132020-02-18 14:50:47 +01001575#ifdef MCUBOOT_HW_ROLLBACK_PROT
1576 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1577 /* Update the stored security counter with the new image's security
1578 * counter value. The primary slot holds the new image at this point,
1579 * but the secondary slot's image header must be passed since image
1580 * headers in the boot_data structure have not been updated yet.
1581 *
1582 * In case of a permanent image swap mcuboot will never attempt to
1583 * revert the images on the next reboot. Therefore, the security
1584 * counter must be increased right after the image upgrade.
1585 */
1586 rc = boot_update_security_counter(
1587 BOOT_CURR_IMG(state),
1588 BOOT_PRIMARY_SLOT,
1589 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1590 if (rc != 0) {
1591 BOOT_LOG_ERR("Security counter update failed after "
1592 "image upgrade.");
1593 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1594 }
1595 }
1596#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1597
Fabio Utzige575b0b2019-09-11 12:34:23 -03001598 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001599 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001600 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001601 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001602 }
David Vinczeba3bd602019-06-17 16:01:43 +02001603 }
1604#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001605
David Vinczeba3bd602019-06-17 16:01:43 +02001606 return rc;
1607}
1608
1609/**
1610 * Completes a previously aborted image swap.
1611 *
1612 * @param bs The current boot status.
1613 *
1614 * @return 0 on success; nonzero on failure.
1615 */
1616#if !defined(MCUBOOT_OVERWRITE_ONLY)
1617static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001618boot_complete_partial_swap(struct boot_loader_state *state,
1619 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001620{
1621 int rc;
1622
1623 /* Determine the type of swap operation being resumed from the
1624 * `swap-type` trailer field.
1625 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001626 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001627 assert(rc == 0);
1628
Fabio Utzig10ee6482019-08-01 12:04:52 -03001629 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001630
1631 /* The following states need image_ok be explicitly set after the
1632 * swap was finished to avoid a new revert.
1633 */
1634 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1635 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001636 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001637 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001638 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001639 }
1640 }
1641
Fabio Utzige575b0b2019-09-11 12:34:23 -03001642 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001643 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001644 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001645 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001646 }
1647 }
1648
Fabio Utzig10ee6482019-08-01 12:04:52 -03001649 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001650 BOOT_LOG_ERR("panic!");
1651 assert(0);
1652
1653 /* Loop forever... */
1654 while (1) {}
1655 }
1656
1657 return rc;
1658}
1659#endif /* !MCUBOOT_OVERWRITE_ONLY */
1660
1661#if (BOOT_IMAGE_NUMBER > 1)
1662/**
1663 * Review the validity of previously determined swap types of other images.
1664 *
1665 * @param aborted_swap The current image upgrade is a
1666 * partial/aborted swap.
1667 */
1668static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001669boot_review_image_swap_types(struct boot_loader_state *state,
1670 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001671{
1672 /* In that case if we rebooted in the middle of an image upgrade process, we
1673 * must review the validity of swap types, that were previously determined
1674 * for other images. The image_ok flag had not been set before the reboot
1675 * for any of the updated images (only the copy_done flag) and thus falsely
1676 * the REVERT swap type has been determined for the previous images that had
1677 * been updated before the reboot.
1678 *
1679 * There are two separate scenarios that we have to deal with:
1680 *
1681 * 1. The reboot has happened during swapping an image:
1682 * The current image upgrade has been determined as a
1683 * partial/aborted swap.
1684 * 2. The reboot has happened between two separate image upgrades:
1685 * In this scenario we must check the swap type of the current image.
1686 * In those cases if it is NONE or REVERT we cannot certainly determine
1687 * the fact of a reboot. In a consistent state images must move in the
1688 * same direction or stay in place, e.g. in practice REVERT and TEST
1689 * swap types cannot be present at the same time. If the swap type of
1690 * the current image is either TEST, PERM or FAIL we must review the
1691 * already determined swap types of other images and set each false
1692 * REVERT swap types to NONE (these images had been successfully
1693 * updated before the system rebooted between two separate image
1694 * upgrades).
1695 */
1696
Fabio Utzig10ee6482019-08-01 12:04:52 -03001697 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001698 /* Nothing to do */
1699 return;
1700 }
1701
1702 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001703 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1704 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001705 /* Nothing to do */
1706 return;
1707 }
1708 }
1709
Fabio Utzig10ee6482019-08-01 12:04:52 -03001710 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1711 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1712 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001713 }
1714 }
1715}
1716#endif
1717
1718/**
1719 * Prepare image to be updated if required.
1720 *
1721 * Prepare image to be updated if required with completing an image swap
1722 * operation if one was aborted and/or determining the type of the
1723 * swap operation. In case of any error set the swap type to NONE.
1724 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001725 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001726 * @param bs Pointer where the read and possibly updated
1727 * boot status can be written to.
1728 */
1729static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001730boot_prepare_image_for_update(struct boot_loader_state *state,
1731 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001732{
1733 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01001734 FIH_DECLARE(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02001735
1736 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001737 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001738 if (rc != 0) {
1739 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1740 " - too small?", BOOT_MAX_IMG_SECTORS);
1741 /* Unable to determine sector layout, continue with next image
1742 * if there is one.
1743 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001744 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001745 if (rc == BOOT_EFLASH)
1746 {
1747 /* Only return on error from the primary image flash */
1748 return;
1749 }
David Vinczeba3bd602019-06-17 16:01:43 +02001750 }
1751
1752 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001753 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001754 if (rc != 0) {
1755 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001756 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001757 BOOT_CURR_IMG(state));
1758 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001759 return;
1760 }
1761
1762 /* If the current image's slots aren't compatible, no swap is possible.
1763 * Just boot into primary slot.
1764 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001765 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001766 boot_status_reset(bs);
1767
1768#ifndef MCUBOOT_OVERWRITE_ONLY
1769 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001770 if (rc != 0) {
1771 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001772 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001773 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001774 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001775 return;
1776 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001777#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001778
Fabio Utzig74aef312019-11-28 11:05:34 -03001779#ifdef MCUBOOT_SWAP_USING_MOVE
1780 /*
1781 * Must re-read image headers because the boot status might
1782 * have been updated in the previous function call.
1783 */
1784 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001785#ifdef MCUBOOT_BOOTSTRAP
1786 /* When bootstrapping it's OK to not have image magic in the primary slot */
1787 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1788 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1789#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001790 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001791#endif
1792
Fabio Utzig74aef312019-11-28 11:05:34 -03001793 /* Continue with next image if there is one. */
1794 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1795 BOOT_CURR_IMG(state));
1796 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1797 return;
1798 }
1799#endif
1800
David Vinczeba3bd602019-06-17 16:01:43 +02001801 /* Determine if we rebooted in the middle of an image swap
1802 * operation. If a partial swap was detected, complete it.
1803 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001804 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001805
1806#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001807 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001808#endif
1809
1810#ifdef MCUBOOT_OVERWRITE_ONLY
1811 /* Should never arrive here, overwrite-only mode has
1812 * no swap state.
1813 */
1814 assert(0);
1815#else
1816 /* Determine the type of swap operation being resumed from the
1817 * `swap-type` trailer field.
1818 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001819 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001820 assert(rc == 0);
1821#endif
1822 /* Attempt to read an image header from each slot. Ensure that
1823 * image headers in slots are aligned with headers in boot_data.
1824 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001825 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001826 assert(rc == 0);
1827
1828 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001829 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001830 } else {
1831 /* There was no partial swap, determine swap type. */
1832 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001833 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001834 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001835 FIH_CALL(boot_validate_slot, fih_rc,
1836 state, BOOT_SECONDARY_SLOT, bs);
Michael Grand5047f032022-11-24 16:49:56 +01001837 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001838 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1839 } else {
1840 BOOT_SWAP_TYPE(state) = bs->swap_type;
1841 }
David Vinczeba3bd602019-06-17 16:01:43 +02001842 }
1843
1844#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001845 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001846#endif
1847
1848#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001849 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001850 /* Header checks are done first because they are
1851 * inexpensive. Since overwrite-only copies starting from
1852 * offset 0, if interrupted, it might leave a valid header
1853 * magic, so also run validation on the primary slot to be
1854 * sure it's not OK.
1855 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001856 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1857 FIH_CALL(boot_validate_slot, fih_rc,
1858 state, BOOT_PRIMARY_SLOT, bs);
1859
Michael Grand5047f032022-11-24 16:49:56 +01001860 if (rc == 0 || FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001861
Fabio Utzig3d77c952020-10-04 10:23:17 -03001862 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001863 FIH_CALL(boot_validate_slot, fih_rc,
1864 state, BOOT_SECONDARY_SLOT, bs);
1865
Michael Grand5047f032022-11-24 16:49:56 +01001866 if (rc == 1 && FIH_EQ(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001867 /* Set swap type to REVERT to overwrite the primary
1868 * slot with the image contained in secondary slot
1869 * and to trigger the explicit setting of the
1870 * image_ok flag.
1871 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001872 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001873 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001874 }
1875 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001876#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001877 }
David Vinczeba3bd602019-06-17 16:01:43 +02001878 } else {
1879 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001880 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001881 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001882}
1883
Mark Horvathccaf7f82021-01-04 18:16:42 +01001884/**
1885 * Updates the security counter for the current image.
1886 *
1887 * @param state Boot loader status information.
1888 *
1889 * @return 0 on success; nonzero on failure.
1890 */
1891static int
1892boot_update_hw_rollback_protection(struct boot_loader_state *state)
1893{
1894#ifdef MCUBOOT_HW_ROLLBACK_PROT
1895 int rc;
1896
1897 /* Update the stored security counter with the active image's security
1898 * counter value. It will only be updated if the new security counter is
1899 * greater than the stored value.
1900 *
1901 * In case of a successful image swapping when the swap type is TEST the
1902 * security counter can be increased only after a reset, when the swap
1903 * type is NONE and the image has marked itself "OK" (the image_ok flag
1904 * has been set). This way a "revert" can be performed when it's
1905 * necessary.
1906 */
1907 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1908 rc = boot_update_security_counter(
1909 BOOT_CURR_IMG(state),
1910 BOOT_PRIMARY_SLOT,
1911 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1912 if (rc != 0) {
1913 BOOT_LOG_ERR("Security counter update failed after image "
1914 "validation.");
1915 return rc;
1916 }
1917 }
1918
1919 return 0;
1920
1921#else /* MCUBOOT_HW_ROLLBACK_PROT */
1922 (void) (state);
1923
1924 return 0;
1925#endif
1926}
1927
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02001928/**
1929 * Checks test swap downgrade prevention conditions.
1930 *
1931 * Function called only for swap upgrades test run. It may prevent
1932 * swap if slot 1 image has <= version number or < security counter
1933 *
1934 * @param state Boot loader status information.
1935 *
1936 * @return 0 - image can be swapped, -1 downgrade prevention
1937 */
1938static int
1939check_downgrade_prevention(struct boot_loader_state *state)
1940{
1941#if defined(MCUBOOT_DOWNGRADE_PREVENTION) && \
1942 (defined(MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH))
1943 uint32_t security_counter[2];
1944 int rc;
1945
1946 if (MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER) {
1947 /* If there was security no counter in slot 0, allow swap */
1948 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 0).hdr),
1949 BOOT_IMG(state, 0).area,
1950 &security_counter[0]);
1951 if (rc != 0) {
1952 return 0;
1953 }
1954 /* If there is no security counter in slot 1, or it's lower than
1955 * that of slot 0, prevent downgrade */
1956 rc = bootutil_get_img_security_cnt(&(BOOT_IMG(state, 1).hdr),
1957 BOOT_IMG(state, 1).area,
1958 &security_counter[1]);
1959 if (rc != 0 || security_counter[0] > security_counter[1]) {
1960 rc = -1;
1961 }
1962 }
1963 else {
1964 rc = boot_version_cmp(&boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
1965 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
1966 }
1967 if (rc < 0) {
1968 /* Image in slot 0 prevents downgrade, delete image in slot 1 */
1969 BOOT_LOG_INF("Image in slot 1 erased due to downgrade prevention");
1970 flash_area_erase(BOOT_IMG(state, 1).area, 0,
1971 flash_area_get_size(BOOT_IMG(state, 1).area));
1972 } else {
1973 rc = 0;
1974 }
1975 return rc;
1976#else
1977 (void)state;
1978 return 0;
1979#endif
1980}
1981
Michael Grand5047f032022-11-24 16:49:56 +01001982fih_ret
Fabio Utzig10ee6482019-08-01 12:04:52 -03001983context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001984{
Marti Bolivar84898652017-06-13 17:20:22 -04001985 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001986 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001987 int rc = -1;
Michael Grand5047f032022-11-24 16:49:56 +01001988 FIH_DECLARE(fih_rc, FIH_FAILURE);
Marti Bolivarc0b47912017-06-13 17:18:09 -04001989 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001990 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001991 bool has_upgrade;
Michael Grand5047f032022-11-24 16:49:56 +01001992 volatile int fih_cnt;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001993
1994 /* The array of slot sectors are defined here (as opposed to file scope) so
1995 * that they don't get allocated for non-boot-loader apps. This is
1996 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001997 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001998 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001999 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2000 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002001#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002002 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03002003#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08002004
Fabio Utzig298913b2019-08-28 11:22:45 -03002005 has_upgrade = false;
2006
2007#if (BOOT_IMAGE_NUMBER == 1)
2008 (void)has_upgrade;
2009#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002010
David Vinczeba3bd602019-06-17 16:01:43 +02002011 /* Iterate over all the images. By the end of the loop the swap type has
2012 * to be determined for each image and all aborted swaps have to be
2013 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002014 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002015 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002016#if BOOT_IMAGE_NUMBER > 1
2017 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2018 continue;
2019 }
2020#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002021#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2022 /* The keys used for encryption may no longer be valid (could belong to
2023 * another images). Therefore, mark them as invalid to force their reload
2024 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002025 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002026 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002027#endif
2028
Fabio Utzig10ee6482019-08-01 12:04:52 -03002029 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002030
Fabio Utzig10ee6482019-08-01 12:04:52 -03002031 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002032 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002033 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002034 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002035#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002036 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002037#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002038
2039 /* Open primary and secondary image areas for the duration
2040 * of this call.
2041 */
2042 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002043 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002044 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002045 assert(rc == 0);
2046 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002047#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002048 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002049 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002050 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03002051#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002052
2053 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002054 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002055
Fabio Utzige575b0b2019-09-11 12:34:23 -03002056 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002057 has_upgrade = true;
2058 }
David Vinczeba3bd602019-06-17 16:01:43 +02002059 }
2060
David Vinczee32483f2019-06-13 10:46:24 +02002061#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002062 if (has_upgrade) {
2063 /* Iterate over all the images and verify whether the image dependencies
2064 * are all satisfied and update swap type if necessary.
2065 */
2066 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002067 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002068 /*
2069 * It was impossible to upgrade because the expected dependency version
2070 * was not available. Here we already changed the swap_type so that
2071 * instead of asserting the bootloader, we continue and no upgrade is
2072 * performed.
2073 */
2074 rc = 0;
2075 }
2076 }
David Vinczee32483f2019-06-13 10:46:24 +02002077#endif
2078
Jamie McCrae56cb6102022-03-23 11:57:03 +00002079 /* Trigger status change callback with upgrading status */
2080 mcuboot_status_change(MCUBOOT_STATUS_UPGRADING);
2081
David Vinczeba3bd602019-06-17 16:01:43 +02002082 /* Iterate over all the images. At this point there are no aborted swaps
2083 * and the swap types are determined for each image. By the end of the loop
2084 * all required update operations will have been finished.
2085 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002086 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002087#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002088 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2089 continue;
2090 }
2091
David Vinczeba3bd602019-06-17 16:01:43 +02002092#ifdef MCUBOOT_ENC_IMAGES
2093 /* The keys used for encryption may no longer be valid (could belong to
2094 * another images). Therefore, mark them as invalid to force their reload
2095 * by boot_enc_load().
2096 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002097 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002098#endif /* MCUBOOT_ENC_IMAGES */
2099
2100 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002101 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002102#endif /* (BOOT_IMAGE_NUMBER > 1) */
2103
2104 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002105 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002106
Fabio Utzig10ee6482019-08-01 12:04:52 -03002107 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002108 case BOOT_SWAP_TYPE_NONE:
2109 break;
2110
Jerzy Kasenberge3f895d2022-04-12 15:05:33 +02002111 case BOOT_SWAP_TYPE_TEST:
2112 if (check_downgrade_prevention(state) != 0) {
2113 /* Downgrade prevented */
2114 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
2115 break;
2116 }
2117 /* fallthrough */
David Vinczeba3bd602019-06-17 16:01:43 +02002118 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2119 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002120 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2121 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2122 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2123 if (rc == BOOT_HOOK_REGULAR)
2124 {
2125 rc = boot_perform_update(state, &bs);
2126 }
David Vinczeba3bd602019-06-17 16:01:43 +02002127 assert(rc == 0);
2128 break;
2129
2130 case BOOT_SWAP_TYPE_FAIL:
2131 /* The image in secondary slot was invalid and is now erased. Ensure
2132 * we don't try to boot into it again on the next reboot. Do this by
2133 * pretending we just reverted back to primary slot.
2134 */
2135#ifndef MCUBOOT_OVERWRITE_ONLY
2136 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002137 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002138 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002139 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002140 }
2141#endif /* !MCUBOOT_OVERWRITE_ONLY */
2142 break;
2143
2144 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002145 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002146 }
2147
Fabio Utzig10ee6482019-08-01 12:04:52 -03002148 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002149 BOOT_LOG_ERR("panic!");
2150 assert(0);
2151
2152 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002153 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002154 }
2155 }
2156
2157 /* Iterate over all the images. At this point all required update operations
2158 * have finished. By the end of the loop each image in the primary slot will
2159 * have been re-validated.
2160 */
Michael Grand5047f032022-11-24 16:49:56 +01002161 FIH_SET(fih_cnt, 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002162 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002163#if BOOT_IMAGE_NUMBER > 1
Michael Grand5047f032022-11-24 16:49:56 +01002164 /* Hardenned to prevent from skipping check of a given image,
2165 * tmp_img_mask is declared volatile
2166 */
2167 volatile bool tmp_img_mask;
2168 FIH_SET(tmp_img_mask, state->img_mask[BOOT_CURR_IMG(state)]);
2169 if (FIH_EQ(tmp_img_mask, true)) {
2170 ++fih_cnt;
Raef Colesf11de642021-10-15 11:11:56 +01002171 continue;
2172 }
2173#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002174 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002175 /* Attempt to read an image header from each slot. Ensure that image
2176 * headers in slots are aligned with headers in boot_data.
2177 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002178 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002179 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002180 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002181 goto out;
2182 }
2183 /* Since headers were reloaded, it can be assumed we just performed
2184 * a swap or overwrite. Now the header info that should be used to
2185 * provide the data for the bootstrap, which previously was at
2186 * secondary slot, was updated to primary slot.
2187 */
2188 }
2189
2190#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002191 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01002192 /* Check for all possible values is redundant in normal operation it
2193 * is meant to prevent FI attack.
2194 */
2195 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS) ||
2196 FIH_EQ(fih_rc, FIH_FAILURE) ||
2197 FIH_EQ(fih_rc, FIH_NO_BOOTABLE_IMAGE)) {
2198 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002199 goto out;
2200 }
2201#else
2202 /* Even if we're not re-validating the primary slot, we could be booting
2203 * onto an empty flash chip. At least do a basic sanity check that
2204 * the magic number on the image is OK.
2205 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002206 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002207 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002208 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002209 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002210 rc = BOOT_EBADIMAGE;
Michael Grand5047f032022-11-24 16:49:56 +01002211 FIH_SET(fih_rc, FIH_FAILURE);
David Vinczeba3bd602019-06-17 16:01:43 +02002212 goto out;
2213 }
David Vinczec3084132020-02-18 14:50:47 +01002214#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2215
Mark Horvathccaf7f82021-01-04 18:16:42 +01002216 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002217 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002218 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002219 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002220 }
David Vincze1cf11b52020-03-24 07:51:09 +01002221
Mark Horvathccaf7f82021-01-04 18:16:42 +01002222 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002223 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01002224 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002225 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002226 }
Michael Grand5047f032022-11-24 16:49:56 +01002227 ++fih_cnt;
David Vinczeba3bd602019-06-17 16:01:43 +02002228 }
Michael Grand5047f032022-11-24 16:49:56 +01002229 /*
2230 * fih_cnt should be equal to BOOT_IMAGE_NUMBER now.
2231 * If this is not the case, at least one iteration of the loop
2232 * has been skipped.
2233 */
2234 if(FIH_NOT_EQ(fih_cnt, BOOT_IMAGE_NUMBER)) {
2235 FIH_PANIC;
2236 }
Fabio Utzigf616c542019-12-19 15:23:32 -03002237 /*
2238 * Since the boot_status struct stores plaintext encryption keys, reset
2239 * them here to avoid the possibility of jumping into an image that could
2240 * easily recover them.
2241 */
2242 memset(&bs, 0, sizeof(struct boot_status));
2243
Raef Colesfe57e7d2021-10-15 11:07:09 +01002244 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002245
Raef Colese8fe6cf2020-05-26 13:07:40 +01002246 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002247out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002248 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002249 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002250}
2251
Michael Grand5047f032022-11-24 16:49:56 +01002252fih_ret
Christopher Collins92ea77f2016-12-12 15:59:26 -08002253split_go(int loader_slot, int split_slot, void **entry)
2254{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002255 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002256 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002257 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002258 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002259 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01002260 FIH_DECLARE(fih_rc, FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002261
Christopher Collins92ea77f2016-12-12 15:59:26 -08002262 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2263 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002264 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002265 }
David Vinczeba3bd602019-06-17 16:01:43 +02002266 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2267 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002268
2269 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2270 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002271 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002272 assert(rc == 0);
2273 split_flash_id = flash_area_id_from_image_slot(split_slot);
2274 rc = flash_area_open(split_flash_id,
2275 &BOOT_IMG_AREA(&boot_data, split_slot));
2276 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002277
2278 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002279 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002280 if (rc != 0) {
2281 rc = SPLIT_GO_ERR;
2282 goto done;
2283 }
2284
Fabio Utzig12d59162019-11-28 10:01:59 -03002285 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002286 if (rc != 0) {
2287 goto done;
2288 }
2289
Christopher Collins92ea77f2016-12-12 15:59:26 -08002290 /* Don't check the bootable image flag because we could really call a
2291 * bootable or non-bootable image. Just validate that the image check
2292 * passes which is distinct from the normal check.
2293 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002294 FIH_CALL(split_image_check, fih_rc,
2295 boot_img_hdr(&boot_data, split_slot),
2296 BOOT_IMG_AREA(&boot_data, split_slot),
2297 boot_img_hdr(&boot_data, loader_slot),
2298 BOOT_IMG_AREA(&boot_data, loader_slot));
Michael Grand5047f032022-11-24 16:49:56 +01002299 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002300 goto done;
2301 }
2302
Marti Bolivarea088872017-06-12 17:10:49 -04002303 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002304 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002305 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002306 rc = SPLIT_GO_OK;
2307
2308done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002309 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2310 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002311 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002312
2313 if (rc) {
Michael Grand5047f032022-11-24 16:49:56 +01002314 FIH_SET(fih_rc, FIH_FAILURE);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002315 }
2316
2317 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002318}
David Vinczee574f2d2020-07-10 11:42:03 +02002319
Tamas Banfe031092020-09-10 17:32:39 +02002320#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002321
Mark Horvathccaf7f82021-01-04 18:16:42 +01002322#define NO_ACTIVE_SLOT UINT32_MAX
2323
David Vinczee574f2d2020-07-10 11:42:03 +02002324/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002325 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002326 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002327 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002328 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002329 * @return 0 on success; nonzero on failure.
2330 */
2331static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002332boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002333{
2334 uint32_t slot;
2335 int fa_id;
2336 int rc;
2337 struct image_header *hdr = NULL;
2338
2339 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002340#if BOOT_IMAGE_NUMBER > 1
2341 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2342 continue;
2343 }
2344#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002345 /* Open all the slots */
2346 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2347 fa_id = flash_area_id_from_multi_image_slot(
2348 BOOT_CURR_IMG(state), slot);
2349 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2350 assert(rc == 0);
2351 }
2352
2353 /* Attempt to read an image header from each slot. */
2354 rc = boot_read_image_headers(state, false, NULL);
2355 if (rc != 0) {
2356 BOOT_LOG_WRN("Failed reading image headers.");
2357 return rc;
2358 }
2359
2360 /* Check headers in all slots */
2361 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2362 hdr = boot_img_hdr(state, slot);
2363
2364 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002365 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002366 BOOT_LOG_IMAGE_INFO(slot, hdr);
2367 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002368 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002369 BOOT_LOG_INF("Image %d %s slot: Image not found",
2370 BOOT_CURR_IMG(state),
2371 (slot == BOOT_PRIMARY_SLOT)
2372 ? "Primary" : "Secondary");
2373 }
2374 }
2375
Raef Colesfe57e7d2021-10-15 11:07:09 +01002376 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002377 }
2378
2379 return 0;
2380}
2381
2382/**
2383 * Finds the slot containing the image with the highest version number for the
2384 * current image.
2385 *
2386 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002387 *
2388 * @return NO_ACTIVE_SLOT if no available slot found, number of
2389 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002390 */
2391static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002392find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002393{
David Vinczee574f2d2020-07-10 11:42:03 +02002394 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002395 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2396 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002397
Mark Horvathccaf7f82021-01-04 18:16:42 +01002398 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002399 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002400 if (candidate_slot == NO_ACTIVE_SLOT) {
2401 candidate_slot = slot;
2402 } else {
2403 rc = boot_version_cmp(
2404 &boot_img_hdr(state, slot)->ih_ver,
2405 &boot_img_hdr(state, candidate_slot)->ih_ver);
2406 if (rc == 1) {
2407 /* The version of the image being examined is greater than
2408 * the version of the current candidate.
2409 */
2410 candidate_slot = slot;
2411 }
2412 }
David Vinczee574f2d2020-07-10 11:42:03 +02002413 }
2414 }
2415
Mark Horvathccaf7f82021-01-04 18:16:42 +01002416 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002417}
2418
Mark Horvathccaf7f82021-01-04 18:16:42 +01002419#ifdef MCUBOOT_HAVE_LOGGING
2420/**
2421 * Prints the state of the loaded images.
2422 *
2423 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002424 */
2425static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002426print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002427{
2428 uint32_t active_slot;
2429
David Brown695e5912021-05-24 16:58:01 -06002430 (void)state;
2431
Mark Horvathccaf7f82021-01-04 18:16:42 +01002432 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002433#if BOOT_IMAGE_NUMBER > 1
2434 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2435 continue;
2436 }
2437#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002438 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002439
2440 BOOT_LOG_INF("Image %d loaded from the %s slot",
2441 BOOT_CURR_IMG(state),
2442 (active_slot == BOOT_PRIMARY_SLOT) ?
2443 "primary" : "secondary");
2444 }
2445}
2446#endif
2447
David Vincze1c456242021-06-29 15:25:24 +02002448#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002449/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002450 * Checks whether the active slot of the current image was previously selected
2451 * to run. Erases the image if it was selected but its execution failed,
2452 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002453 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002454 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002455 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002456 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002457 */
2458static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002459boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002460{
2461 const struct flash_area *fap;
2462 int fa_id;
2463 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002464 uint32_t active_slot;
2465 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002466
Raef Colesfe57e7d2021-10-15 11:07:09 +01002467 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002468
2469 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002470 rc = flash_area_open(fa_id, &fap);
2471 assert(rc == 0);
2472
Raef Colesfe57e7d2021-10-15 11:07:09 +01002473 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002474
2475 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2476 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002477 assert(rc == 0);
2478
Mark Horvathccaf7f82021-01-04 18:16:42 +01002479 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2480 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2481 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002482 /*
2483 * A reboot happened without the image being confirmed at
2484 * runtime or its trailer is corrupted/invalid. Erase the image
2485 * to prevent it from being selected again on the next reboot.
2486 */
2487 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002488 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002489 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002490 assert(rc == 0);
2491
2492 flash_area_close(fap);
2493 rc = -1;
2494 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002495 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2496 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002497 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2498 "value was neither 'set' nor 'unset', but 'bad'.");
2499 }
2500 /*
2501 * Set the copy_done flag, indicating that the image has been
2502 * selected to boot. It can be set in advance, before even
2503 * validating the image, because in case the validation fails, the
2504 * entire image slot will be erased (including the trailer).
2505 */
2506 rc = boot_write_copy_done(fap);
2507 if (rc != 0) {
2508 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002509 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002510 "primary" : "secondary");
2511 rc = 0;
2512 }
2513 }
2514 flash_area_close(fap);
2515 }
2516
2517 return rc;
2518}
David Vincze1c456242021-06-29 15:25:24 +02002519#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002520
Tamas Banfe031092020-09-10 17:32:39 +02002521#ifdef MCUBOOT_RAM_LOAD
2522
Mark Horvathccaf7f82021-01-04 18:16:42 +01002523#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002524#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2525#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2526#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002527#endif
Tamas Banfe031092020-09-10 17:32:39 +02002528
2529/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002530 * Verifies that the active slot of the current image can be loaded within the
2531 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002532 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002533 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002534 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002535 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002536 */
2537static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002538boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002539{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002540 uint32_t img_dst;
2541 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002542 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002543 uint32_t exec_ram_start;
2544 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002545
2546 (void)state;
2547
Mark Horvathccaf7f82021-01-04 18:16:42 +01002548#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2549 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002550
Mark Horvathccaf7f82021-01-04 18:16:42 +01002551 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2552 &exec_ram_size);
2553 if (rc != 0) {
2554 return BOOT_EBADSTATUS;
2555 }
2556#else
2557 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2558 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2559#endif
2560
Raef Colesfe57e7d2021-10-15 11:07:09 +01002561 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2562 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002563
2564 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002565 return BOOT_EBADIMAGE;
2566 }
2567
2568 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2569 return BOOT_EBADIMAGE;
2570 }
2571
Mark Horvathccaf7f82021-01-04 18:16:42 +01002572 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002573 return BOOT_EBADIMAGE;
2574 }
2575
2576 return 0;
2577}
2578
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002579#ifdef MCUBOOT_ENC_IMAGES
2580
2581/**
2582 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2583 *
2584 * @param state Boot loader status information.
2585 * @param slot The flash slot of the image to be copied to SRAM.
2586 * @param hdr The image header.
2587 * @param src_sz Size of the image.
2588 * @param img_dst Pointer to the address at which the image needs to be
2589 * copied to SRAM.
2590 *
2591 * @return 0 on success; nonzero on failure.
2592 */
2593static int
2594boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2595 uint32_t slot, struct image_header *hdr,
2596 uint32_t src_sz, uint32_t img_dst)
2597{
2598 /* The flow for the decryption and copy of the image is as follows :
2599 * 1. The whole image is copied to the RAM (header + payload + TLV).
2600 * 2. The encryption key is loaded from the TLV in flash.
2601 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2602 * is 1024 bytes). Only the payload section is decrypted.
2603 * 4. The image is authenticated in RAM.
2604 */
2605 const struct flash_area *fap_src = NULL;
2606 struct boot_status bs;
2607 uint32_t blk_off;
2608 uint32_t tlv_off;
2609 uint32_t blk_sz;
2610 uint32_t bytes_copied = hdr->ih_hdr_size;
2611 uint32_t chunk_sz;
2612 uint32_t max_sz = 1024;
2613 uint16_t idx;
2614 uint8_t image_index;
2615 uint8_t * cur_dst;
2616 int area_id;
2617 int rc;
2618 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2619
2620 image_index = BOOT_CURR_IMG(state);
2621 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2622 rc = flash_area_open(area_id, &fap_src);
2623 if (rc != 0){
2624 return BOOT_EFLASH;
2625 }
2626
2627 tlv_off = BOOT_TLV_OFF(hdr);
2628
2629 /* Copying the whole image in RAM */
2630 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2631 if (rc != 0) {
2632 goto done;
2633 }
2634
2635 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2636 if (rc < 0) {
2637 goto done;
2638 }
2639
2640 /* if rc > 0 then the key has already been loaded */
2641 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2642 goto done;
2643 }
2644
2645 /* Starting at the end of the header as the header section is not encrypted */
2646 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2647 if (src_sz - bytes_copied > max_sz) {
2648 chunk_sz = max_sz;
2649 } else {
2650 chunk_sz = src_sz - bytes_copied;
2651 }
2652
2653 cur_dst = ram_dst + bytes_copied;
2654 blk_sz = chunk_sz;
2655 idx = 0;
2656 if (bytes_copied + chunk_sz > tlv_off) {
2657 /* Going over TLV section
2658 * Part of the chunk is encrypted payload */
2659 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2660 blk_sz = tlv_off - (bytes_copied);
2661 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2662 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2663 blk_off, cur_dst);
2664 } else {
2665 /* Image encrypted payload section */
2666 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2667 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2668 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2669 blk_off, cur_dst);
2670 }
2671
2672 bytes_copied += chunk_sz;
2673 }
2674 rc = 0;
2675
2676done:
2677 flash_area_close(fap_src);
2678
2679 return rc;
2680}
2681
2682#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002683/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002684 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002685 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002686 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002687 * @param slot The flash slot of the image to be copied to SRAM.
2688 * @param img_dst The address at which the image needs to be copied to
2689 * SRAM.
2690 * @param img_sz The size of the image that needs to be copied to SRAM.
2691 *
2692 * @return 0 on success; nonzero on failure.
2693 */
2694static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002695boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2696 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002697{
2698 int rc;
2699 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002700 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002701
Mark Horvathccaf7f82021-01-04 18:16:42 +01002702#if (BOOT_IMAGE_NUMBER == 1)
2703 (void)state;
2704#endif
2705
2706 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2707
2708 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002709 if (rc != 0) {
2710 return BOOT_EFLASH;
2711 }
2712
2713 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002714 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002715 if (rc != 0) {
2716 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2717 }
2718
2719 flash_area_close(fap_src);
2720
2721 return rc;
2722}
2723
Mark Horvathccaf7f82021-01-04 18:16:42 +01002724#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002725/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002726 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002727 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002728 * @param start_a Start of the A region.
2729 * @param end_a End of the A region.
2730 * @param start_b Start of the B region.
2731 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002732 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002733 * @return true if there is overlap; false otherwise.
2734 */
2735static bool
2736do_regions_overlap(uint32_t start_a, uint32_t end_a,
2737 uint32_t start_b, uint32_t end_b)
2738{
2739 if (start_b > end_a) {
2740 return false;
2741 } else if (start_b >= start_a) {
2742 return true;
2743 } else if (end_b > start_a) {
2744 return true;
2745 }
2746
2747 return false;
2748}
2749
2750/**
2751 * Checks if the image we want to load to memory overlap with an already
2752 * ramloaded image.
2753 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002754 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002755 *
2756 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002757 */
2758static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002759boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002760{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002761 uint32_t i;
2762
2763 uint32_t start_a;
2764 uint32_t end_a;
2765 uint32_t start_b;
2766 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002767 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002768
Raef Colesfe57e7d2021-10-15 11:07:09 +01002769 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002770 /* Safe to add here, values are already verified in
2771 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002772 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002773
2774 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002775 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002776 || i == image_id_to_check) {
2777 continue;
2778 }
2779
Raef Colesfe57e7d2021-10-15 11:07:09 +01002780 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002781 /* Safe to add here, values are already verified in
2782 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002783 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002784
2785 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2786 return -1;
2787 }
2788 }
2789
2790 return 0;
2791}
2792#endif
2793
2794/**
2795 * Loads the active slot of the current image into SRAM. The load address and
2796 * image size is extracted from the image header.
2797 *
2798 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002799 *
2800 * @return 0 on success; nonzero on failure.
2801 */
2802static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002803boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002804{
2805 uint32_t active_slot;
2806 struct image_header *hdr = NULL;
2807 uint32_t img_dst;
2808 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002809 int rc;
2810
Raef Colesfe57e7d2021-10-15 11:07:09 +01002811 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002812 hdr = boot_img_hdr(state, active_slot);
2813
Tamas Banfe031092020-09-10 17:32:39 +02002814 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2815
Mark Horvathccaf7f82021-01-04 18:16:42 +01002816 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002817
Mark Horvathccaf7f82021-01-04 18:16:42 +01002818 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002819 if (rc != 0) {
2820 return rc;
2821 }
2822
Raef Colesfe57e7d2021-10-15 11:07:09 +01002823 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2824 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002825
Raef Colesfe57e7d2021-10-15 11:07:09 +01002826 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002827 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002828 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002829 return rc;
2830 }
2831
Mark Horvathccaf7f82021-01-04 18:16:42 +01002832#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002833 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002834 if (rc != 0) {
2835 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2836 another image.", img_dst);
2837 return rc;
2838 }
2839#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002840#ifdef MCUBOOT_ENC_IMAGES
2841 /* decrypt image if encrypted and copy it to RAM */
2842 if (IS_ENCRYPTED(hdr)) {
2843 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2844 } else {
2845 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2846 }
2847#else
Tamas Banfe031092020-09-10 17:32:39 +02002848 /* Copy image to the load address from where it currently resides in
2849 * flash.
2850 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002851 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002852#endif
Tamas Banfe031092020-09-10 17:32:39 +02002853 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002854 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002855 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002856 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002857 }
2858 } else {
2859 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2860 * MCUBOOT_RAM_LOAD is set.
2861 */
2862 rc = BOOT_EBADIMAGE;
2863 }
2864
Mark Horvathccaf7f82021-01-04 18:16:42 +01002865 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002866 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2867 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002868 }
2869
Tamas Banfe031092020-09-10 17:32:39 +02002870 return rc;
2871}
2872
2873/**
2874 * Removes an image from SRAM, by overwriting it with zeros.
2875 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002876 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002877 *
2878 * @return 0 on success; nonzero on failure.
2879 */
2880static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002881boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002882{
David Brown695e5912021-05-24 16:58:01 -06002883 (void)state;
2884
Mark Horvathccaf7f82021-01-04 18:16:42 +01002885 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
Raef Colesfe57e7d2021-10-15 11:07:09 +01002886 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002887
Raef Colesfe57e7d2021-10-15 11:07:09 +01002888 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2889 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002890
Raef Colesfe57e7d2021-10-15 11:07:09 +01002891 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2892 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002893
2894 return 0;
2895}
2896
2897/**
2898 * Removes an image from flash by erasing the corresponding flash area
2899 *
2900 * @param state Boot loader status information.
2901 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002902 *
2903 * @return 0 on success; nonzero on failure.
2904 */
2905static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002906boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002907{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002908 int area_id;
2909 int rc;
2910 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002911
David Brown695e5912021-05-24 16:58:01 -06002912 (void)state;
2913
Mark Horvathccaf7f82021-01-04 18:16:42 +01002914 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2915 slot);
2916 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2917 rc = flash_area_open(area_id, &fap);
2918 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002919 flash_area_erase(fap, 0, flash_area_get_size(fap));
Dominik Ermel245de812022-09-02 13:39:23 +00002920 flash_area_close(fap);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002921 }
2922
2923 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002924}
2925#endif /* MCUBOOT_RAM_LOAD */
2926
Mark Horvathccaf7f82021-01-04 18:16:42 +01002927#if (BOOT_IMAGE_NUMBER > 1)
2928/**
2929 * Checks the image dependency whether it is satisfied.
2930 *
2931 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002932 * @param dep Image dependency which has to be verified.
2933 *
2934 * @return 0 if dependencies are met; nonzero otherwise.
2935 */
2936static int
2937boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002938 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002939{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002940 struct image_version *dep_version;
2941 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002942 int rc;
2943
Mark Horvathccaf7f82021-01-04 18:16:42 +01002944 /* Determine the source of the image which is the subject of
2945 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002946 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002947 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002948 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2949
2950 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2951 if (rc >= 0) {
2952 /* Dependency satisfied. */
2953 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002954 }
2955
Mark Horvathccaf7f82021-01-04 18:16:42 +01002956 return rc;
2957}
2958
2959/**
2960 * Reads all dependency TLVs of an image and verifies one after another to see
2961 * if they are all satisfied.
2962 *
2963 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002964 *
2965 * @return 0 if dependencies are met; nonzero otherwise.
2966 */
2967static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002968boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002969{
2970 uint32_t active_slot;
2971 const struct flash_area *fap;
2972 struct image_tlv_iter it;
2973 struct image_dependency dep;
2974 uint32_t off;
2975 uint16_t len;
2976 int area_id;
2977 int rc;
2978
Raef Colesfe57e7d2021-10-15 11:07:09 +01002979 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002980
2981 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2982 active_slot);
2983 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002984 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002985 rc = BOOT_EFLASH;
2986 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002987 }
2988
Mark Horvathccaf7f82021-01-04 18:16:42 +01002989 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2990 IMAGE_TLV_DEPENDENCY, true);
2991 if (rc != 0) {
2992 goto done;
2993 }
David Vinczee574f2d2020-07-10 11:42:03 +02002994
Mark Horvathccaf7f82021-01-04 18:16:42 +01002995 while (true) {
2996 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2997 if (rc < 0) {
2998 return -1;
2999 } else if (rc > 0) {
3000 rc = 0;
3001 break;
3002 }
David Vinczee574f2d2020-07-10 11:42:03 +02003003
Mark Horvathccaf7f82021-01-04 18:16:42 +01003004 if (len != sizeof(dep)) {
3005 rc = BOOT_EBADIMAGE;
3006 goto done;
3007 }
3008
3009 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
3010 fap, off, &dep, len);
3011 if (rc != 0) {
3012 rc = BOOT_EFLASH;
3013 goto done;
3014 }
3015
3016 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
3017 rc = BOOT_EBADARGS;
3018 goto done;
3019 }
3020
Raef Colesfe57e7d2021-10-15 11:07:09 +01003021 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003022 if (rc != 0) {
3023 /* Dependency not satisfied. */
3024 goto done;
3025 }
3026 }
3027
3028done:
3029 flash_area_close(fap);
3030 return rc;
3031}
3032
3033/**
3034 * Checks the dependency of all the active slots. If an image found with
3035 * invalid or not satisfied dependencies the image is removed from SRAM (in
3036 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
3037 *
3038 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003039 *
3040 * @return 0 if dependencies are met; nonzero otherwise.
3041 */
3042static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003043boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003044{
3045 int rc = -1;
3046 uint32_t active_slot;
3047
3048 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003049 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3050 continue;
3051 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01003052 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003053 if (rc != 0) {
3054 /* Dependencies not met or invalid dependencies. */
3055
3056#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003057 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003058#endif /* MCUBOOT_RAM_LOAD */
3059
Raef Colesfe57e7d2021-10-15 11:07:09 +01003060 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
3061 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3062 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003063
3064 return rc;
3065 }
3066 }
3067
3068 return rc;
3069}
3070#endif /* (BOOT_IMAGE_NUMBER > 1) */
3071
3072/**
3073 * Tries to load a slot for all the images with validation.
3074 *
3075 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003076 *
3077 * @return 0 on success; nonzero on failure.
3078 */
Michael Grand5047f032022-11-24 16:49:56 +01003079fih_ret
Raef Colesfe57e7d2021-10-15 11:07:09 +01003080boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003081{
3082 uint32_t active_slot;
3083 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003084 fih_ret fih_rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003085
3086 /* Go over all the images and try to load one */
3087 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3088 /* All slots tried until a valid image found. Breaking from this loop
3089 * means that a valid image found or already loaded. If no slot is
3090 * found the function returns with error code. */
3091 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003092 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003093 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003094 if (active_slot != NO_ACTIVE_SLOT){
3095 /* A slot is already active, go to next image. */
3096 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003097 }
David Vincze505fba22020-10-22 13:53:29 +02003098
Raef Colesfe57e7d2021-10-15 11:07:09 +01003099 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003100 if (active_slot == NO_ACTIVE_SLOT) {
3101 BOOT_LOG_INF("No slot to load for image %d",
3102 BOOT_CURR_IMG(state));
3103 FIH_RET(FIH_FAILURE);
3104 }
3105
3106 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003107 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003108
Raef Colesf11de642021-10-15 11:11:56 +01003109#if BOOT_IMAGE_NUMBER > 1
3110 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3111 continue;
3112 }
3113#endif
3114
Mark Horvathccaf7f82021-01-04 18:16:42 +01003115#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003116 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003117 if (rc != 0) {
3118 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003119 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3120 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003121 continue;
3122 }
George Becksteind4d90f82021-05-11 02:00:00 -04003123
David Vincze505fba22020-10-22 13:53:29 +02003124#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003125 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003126 if (rc != 0) {
3127 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003128 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3129 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003130 continue;
3131 }
3132#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003133#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003134
Tamas Banfe031092020-09-10 17:32:39 +02003135#ifdef MCUBOOT_RAM_LOAD
3136 /* Image is first loaded to RAM and authenticated there in order to
3137 * prevent TOCTOU attack during image copy. This could be applied
3138 * when loading images from external (untrusted) flash to internal
3139 * (trusted) RAM and image is authenticated before copying.
3140 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003141 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003142 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003143 /* Image cannot be ramloaded. */
3144 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003145 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3146 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003147 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003148 }
3149#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003150
3151 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
Michael Grand5047f032022-11-24 16:49:56 +01003152 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003153 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003154#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003155 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003156#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003157 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3158 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003159 continue;
David Vincze505fba22020-10-22 13:53:29 +02003160 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003161
3162 /* Valid image loaded from a slot, go to next image. */
3163 break;
3164 }
3165 }
3166
3167 FIH_RET(FIH_SUCCESS);
3168}
3169
3170/**
3171 * Updates the security counter for the current image.
3172 *
3173 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003174 *
3175 * @return 0 on success; nonzero on failure.
3176 */
3177static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003178boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003179{
3180#ifdef MCUBOOT_HW_ROLLBACK_PROT
3181 int rc;
3182
3183 /* Update the stored security counter with the newer (active) image's
3184 * security counter value.
3185 */
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 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3188 * security counter can be increased only after reboot, if the image
3189 * has been confirmed at runtime (the image_ok flag has been set).
3190 * This way a 'revert' can be performed when it's necessary.
3191 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003192 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003193#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003194 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003195 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3196 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003197 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003198 BOOT_LOG_ERR("Security counter update failed after image "
3199 "validation.");
3200 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003201 }
David Vincze1c456242021-06-29 15:25:24 +02003202#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003203 }
3204#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003205
Mark Horvathccaf7f82021-01-04 18:16:42 +01003206 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003207
Mark Horvathccaf7f82021-01-04 18:16:42 +01003208#else /* MCUBOOT_HW_ROLLBACK_PROT */
3209 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003210 return 0;
3211#endif
3212}
3213
Michael Grand5047f032022-11-24 16:49:56 +01003214fih_ret
Mark Horvathccaf7f82021-01-04 18:16:42 +01003215context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3216{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003217 int rc;
Michael Grand5047f032022-11-24 16:49:56 +01003218 FIH_DECLARE(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003219
Raef Colesfe57e7d2021-10-15 11:07:09 +01003220 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003221 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003222 goto out;
3223 }
3224
Mark Horvathccaf7f82021-01-04 18:16:42 +01003225#if (BOOT_IMAGE_NUMBER > 1)
3226 while (true) {
3227#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003228 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Michael Grand5047f032022-11-24 16:49:56 +01003229 if (FIH_NOT_EQ(fih_rc, FIH_SUCCESS)) {
3230 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003231 goto out;
3232 }
3233
3234#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003235 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003236 if (rc != 0) {
3237 /* Dependency check failed for an image, it has been removed from
3238 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3239 * unavailable. Try to load an image from another slot.
3240 */
3241 continue;
3242 }
3243 /* Dependency check was successful. */
3244 break;
3245 }
3246#endif
3247
3248 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003249#if BOOT_IMAGE_NUMBER > 1
3250 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3251 continue;
3252 }
3253#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003254 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003255 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003256 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003257 goto out;
3258 }
3259
Raef Colesfe57e7d2021-10-15 11:07:09 +01003260 rc = boot_add_shared_data(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003261 if (rc != 0) {
Michael Grand5047f032022-11-24 16:49:56 +01003262 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003263 goto out;
3264 }
3265 }
3266
3267 /* All image loaded successfully. */
3268#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003269 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003270#endif
3271
Raef Colesfe57e7d2021-10-15 11:07:09 +01003272 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003273
David Vinczee574f2d2020-07-10 11:42:03 +02003274out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003275 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003276
Michael Grand5047f032022-11-24 16:49:56 +01003277 if (rc != 0) {
3278 FIH_SET(fih_rc, FIH_FAILURE);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003279 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003280
Mark Horvathccaf7f82021-01-04 18:16:42 +01003281 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003282}
Tamas Banfe031092020-09-10 17:32:39 +02003283#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003284
3285/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003286 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003287 * appropriate, and tells you what address to boot from.
3288 *
3289 * @param rsp On success, indicates how booting should occur.
3290 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003291 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003292 */
Michael Grand5047f032022-11-24 16:49:56 +01003293fih_ret
David Vinczee574f2d2020-07-10 11:42:03 +02003294boot_go(struct boot_rsp *rsp)
3295{
Michael Grand5047f032022-11-24 16:49:56 +01003296 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003297
3298 boot_state_clear(NULL);
3299
Raef Colese8fe6cf2020-05-26 13:07:40 +01003300 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3301 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003302}
Raef Colesf11de642021-10-15 11:11:56 +01003303
3304/**
3305 * Prepares the booting process, considering only a single image. This function
3306 * moves images around in flash as appropriate, and tells you what address to
3307 * boot from.
3308 *
3309 * @param rsp On success, indicates how booting should occur.
3310 *
3311 * @param image_id The image ID to prepare the boot process for.
3312 *
3313 * @return FIH_SUCCESS on success; nonzero on failure.
3314 */
Michael Grand5047f032022-11-24 16:49:56 +01003315fih_ret
Raef Colesf11de642021-10-15 11:11:56 +01003316boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3317{
Michael Grand5047f032022-11-24 16:49:56 +01003318 FIH_DECLARE(fih_rc, FIH_FAILURE);
Raef Colesf11de642021-10-15 11:11:56 +01003319
3320 if (image_id >= BOOT_IMAGE_NUMBER) {
3321 FIH_RET(FIH_FAILURE);
3322 }
3323
3324#if BOOT_IMAGE_NUMBER > 1
3325 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3326 boot_data.img_mask[image_id] = 0;
3327#endif
3328
3329 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3330 FIH_RET(fih_rc);
3331}
3332
3333/**
3334 * Clears the boot state, so that previous operations have no effect on new
3335 * ones.
3336 *
3337 * @param state The state that should be cleared. If the value
3338 * is NULL, the default bootloader state will be
3339 * cleared.
3340 */
3341void boot_state_clear(struct boot_loader_state *state)
3342{
3343 if (state != NULL) {
3344 memset(state, 0, sizeof(struct boot_loader_state));
3345 } else {
3346 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3347 }
3348}