blob: ac449a4e9ec9cc6abe6fabb84a1a2a39a7e02d55 [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"
39#include "bootutil/image.h"
40#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030041#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050042#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010043#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010044#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010045#include "bootutil/fault_injection_hardening.h"
Mark Horvathccaf7f82021-01-04 18:16:42 +010046#include "bootutil/ramload.h"
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020047#include "bootutil/boot_hooks.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050048
Fabio Utzigba829042018-09-18 08:29:34 -030049#ifdef MCUBOOT_ENC_IMAGES
50#include "bootutil/enc_key.h"
51#endif
52
Carlos Falgueras García391b1972021-06-21 16:58:07 +020053#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
54#include <os/os_malloc.h>
55#endif
56
Fabio Utzigba1fbe62017-07-21 14:01:20 -030057#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030058
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020059BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010060
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040061static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080062
Fabio Utzigabec0732019-07-31 08:40:22 -030063#if (BOOT_IMAGE_NUMBER > 1)
64#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
65#else
66#define IMAGES_ITER(x)
67#endif
68
Fabio Utzig10ee6482019-08-01 12:04:52 -030069/*
70 * This macro allows some control on the allocation of local variables.
71 * When running natively on a target, we don't want to allocated huge
72 * variables on the stack, so make them global instead. For the simulator
73 * we want to run as many threads as there are tests, and it's safer
74 * to just make those variables stack allocated.
75 */
76#if !defined(__BOOTSIM__)
77#define TARGET_STATIC static
78#else
79#define TARGET_STATIC
80#endif
81
David Vinczee574f2d2020-07-10 11:42:03 +020082static int
83boot_read_image_headers(struct boot_loader_state *state, bool require_all,
84 struct boot_status *bs)
85{
86 int rc;
87 int i;
88
89 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020090 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
91 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
92 if (rc == BOOT_HOOK_REGULAR)
93 {
94 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
95 }
David Vinczee574f2d2020-07-10 11:42:03 +020096 if (rc != 0) {
97 /* If `require_all` is set, fail on any single fail, otherwise
98 * if at least the first slot's header was read successfully,
99 * then the boot loader can attempt a boot.
100 *
101 * Failure to read any headers is a fatal error.
102 */
103 if (i > 0 && !require_all) {
104 return 0;
105 } else {
106 return rc;
107 }
108 }
109 }
110
111 return 0;
112}
113
Mark Horvathccaf7f82021-01-04 18:16:42 +0100114/**
115 * Saves boot status and shared data for current image.
116 *
117 * @param state Boot loader status information.
118 * @param active_slot Index of the slot will be loaded for current image.
119 *
120 * @return 0 on success; nonzero on failure.
121 */
122static int
123boot_add_shared_data(struct boot_loader_state *state,
124 uint32_t active_slot)
125{
126#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
127 int rc;
128
129#ifdef MCUBOOT_MEASURED_BOOT
130 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
131 boot_img_hdr(state, active_slot),
132 BOOT_IMG_AREA(state, active_slot));
133 if (rc != 0) {
134 BOOT_LOG_ERR("Failed to add image data to shared area");
135 return rc;
136 }
137#endif /* MCUBOOT_MEASURED_BOOT */
138
139#ifdef MCUBOOT_DATA_SHARING
140 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
141 BOOT_IMG_AREA(state, active_slot));
142 if (rc != 0) {
143 BOOT_LOG_ERR("Failed to add data to shared memory area.");
144 return rc;
145 }
146#endif /* MCUBOOT_DATA_SHARING */
147
148 return 0;
149
150#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
151 (void) (state);
152 (void) (active_slot);
153
154 return 0;
155#endif
156}
157
158/**
159 * Fills rsp to indicate how booting should occur.
160 *
161 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100162 * @param rsp boot_rsp struct to fill.
163 */
164static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100165fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100166{
167 uint32_t active_slot;
168
169#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100170 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100171 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100172 IMAGES_ITER(BOOT_CURR_IMG(state)) {
173 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
174 break;
175 }
176 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100177#endif
178
179#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100180 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100181#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100182 active_slot = BOOT_PRIMARY_SLOT;
183#endif
184
Dominik Ermel260ae092021-04-23 05:38:45 +0000185 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100186 rsp->br_image_off = boot_img_slot_off(state, active_slot);
187 rsp->br_hdr = boot_img_hdr(state, active_slot);
188}
189
190/**
191 * Closes all flash areas.
192 *
193 * @param state Boot loader status information.
194 */
195static void
196close_all_flash_areas(struct boot_loader_state *state)
197{
198 uint32_t slot;
199
200 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +0100201#if BOOT_IMAGE_NUMBER > 1
202 if (state->img_mask[BOOT_CURR_IMG(state)]) {
203 continue;
204 }
205#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100206#if MCUBOOT_SWAP_USING_SCRATCH
207 flash_area_close(BOOT_SCRATCH_AREA(state));
208#endif
209 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
210 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
211 }
212 }
213}
214
Tamas Banfe031092020-09-10 17:32:39 +0200215#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600216/*
217 * Compute the total size of the given image. Includes the size of
218 * the TLVs.
219 */
Tamas Banfe031092020-09-10 17:32:39 +0200220#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600221static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300222boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600223{
224 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300225 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300226 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300227 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600228 int area_id;
229 int rc;
230
Fabio Utzig10ee6482019-08-01 12:04:52 -0300231#if (BOOT_IMAGE_NUMBER == 1)
232 (void)state;
233#endif
234
235 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600236 rc = flash_area_open(area_id, &fap);
237 if (rc != 0) {
238 rc = BOOT_EFLASH;
239 goto done;
240 }
241
Fabio Utzig61fd8882019-09-14 20:00:20 -0300242 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
243
244 if (flash_area_read(fap, off, &info, sizeof(info))) {
245 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600246 goto done;
247 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300248
Fabio Utzige52c08e2019-09-11 19:32:00 -0300249 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
250 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
251 if (protect_tlv_size != info.it_tlv_tot) {
252 rc = BOOT_EBADIMAGE;
253 goto done;
254 }
255
256 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
257 rc = BOOT_EFLASH;
258 goto done;
259 }
260 } else if (protect_tlv_size != 0) {
261 rc = BOOT_EBADIMAGE;
262 goto done;
263 }
264
Fabio Utzig61fd8882019-09-14 20:00:20 -0300265 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
266 rc = BOOT_EBADIMAGE;
267 goto done;
268 }
269
Fabio Utzige52c08e2019-09-11 19:32:00 -0300270 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600271 rc = 0;
272
273done:
274 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300275 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600276}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300277#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600278
Tamas Banfe031092020-09-10 17:32:39 +0200279#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700280static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300281boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800282{
David Brownab449182019-11-15 09:32:52 -0700283 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300284#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700285 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300286#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287
288 /* Figure out what size to write update status update as. The size depends
289 * on what the minimum write size is for scratch area, active image slot.
290 * We need to use the bigger of those 2 values.
291 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300292 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300293#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300294 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295 if (align > elem_sz) {
296 elem_sz = align;
297 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300298#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299
300 return elem_sz;
301}
302
Fabio Utzig10ee6482019-08-01 12:04:52 -0300303static int
304boot_initialize_area(struct boot_loader_state *state, int flash_area)
305{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000306 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
307 boot_sector_t *out_sectors;
308 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300309 int rc;
310
311 num_sectors = BOOT_MAX_IMG_SECTORS;
312
313 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
314 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
315 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
316 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
317 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
318 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300319#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300320 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
321 out_sectors = state->scratch.sectors;
322 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300323#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300324 } else {
325 return BOOT_EFLASH;
326 }
327
Dominik Ermel51c8d762021-05-24 15:34:01 +0000328#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300329 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000330#else
331 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
332 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
333#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300334 if (rc != 0) {
335 return rc;
336 }
337 *out_num_sectors = num_sectors;
338 return 0;
339}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300340
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341/**
342 * Determines the sector layout of both image slots and the scratch area.
343 * This information is necessary for calculating the number of bytes to erase
344 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300345 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346 */
347static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300348boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300350 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800352
Fabio Utzig10ee6482019-08-01 12:04:52 -0300353 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300354
355 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800356 if (rc != 0) {
357 return BOOT_EFLASH;
358 }
359
Fabio Utzig10ee6482019-08-01 12:04:52 -0300360 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200362 /* We need to differentiate from the primary image issue */
363 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364 }
365
Fabio Utzig12d59162019-11-28 10:01:59 -0300366#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300367 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200368 if (rc != 0) {
369 return BOOT_EFLASH;
370 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300371#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200372
Fabio Utzig10ee6482019-08-01 12:04:52 -0300373 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374
375 return 0;
376}
377
Fabio Utzig12d59162019-11-28 10:01:59 -0300378void
379boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380{
Fabio Utzig4741c452019-12-19 15:32:41 -0300381#ifdef MCUBOOT_ENC_IMAGES
382 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
383#if MCUBOOT_SWAP_SAVE_ENCTLV
384 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
385#endif
386#endif /* MCUBOOT_ENC_IMAGES */
387
388 bs->use_scratch = 0;
389 bs->swap_size = 0;
390 bs->source = 0;
391
Fabio Utzig74aef312019-11-28 11:05:34 -0300392 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300393 bs->idx = BOOT_STATUS_IDX_0;
394 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700395 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300396}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800397
Fabio Utzig12d59162019-11-28 10:01:59 -0300398bool
399boot_status_is_reset(const struct boot_status *bs)
400{
Fabio Utzig74aef312019-11-28 11:05:34 -0300401 return (bs->op == BOOT_STATUS_OP_MOVE &&
402 bs->idx == BOOT_STATUS_IDX_0 &&
403 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404}
405
406/**
407 * Writes the supplied boot status to the flash file system. The boot status
408 * contains the current state of an in-progress image copy operation.
409 *
410 * @param bs The boot status to write.
411 *
412 * @return 0 on success; nonzero on failure.
413 */
414int
Fabio Utzig12d59162019-11-28 10:01:59 -0300415boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416{
417 const struct flash_area *fap;
418 uint32_t off;
419 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000420 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300421 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700422 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300423 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300425 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100426 * the trailer. Since in the last step the primary slot is erased, the
427 * first two status writes go to the scratch which will be copied to
428 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300429 */
430
Fabio Utzig12d59162019-11-28 10:01:59 -0300431#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300432 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433 /* Write to scratch. */
434 area_id = FLASH_AREA_IMAGE_SCRATCH;
435 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300436#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100437 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300438 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300439#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300441#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800442
443 rc = flash_area_open(area_id, &fap);
444 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000445 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446 }
447
448 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300449 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200450 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300451 erased_val = flash_area_erased_val(fap);
452 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700453 buf[0] = bs->state;
454
455 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456 if (rc != 0) {
457 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 }
459
Christopher Collins92ea77f2016-12-12 15:59:26 -0800460 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000461
Christopher Collins92ea77f2016-12-12 15:59:26 -0800462 return rc;
463}
Tamas Banfe031092020-09-10 17:32:39 +0200464#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200465#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466
467/*
David Vinczec3084132020-02-18 14:50:47 +0100468 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100470static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300471boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
472 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300474 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300475 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300476 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100477 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300478
Fabio Utzig10ee6482019-08-01 12:04:52 -0300479#if (BOOT_IMAGE_NUMBER == 1)
480 (void)state;
481#endif
482
Fabio Utzigba829042018-09-18 08:29:34 -0300483 (void)bs;
484 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300485
486 image_index = BOOT_CURR_IMG(state);
487
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000488/* In the case of ram loading the image has already been decrypted as it is
489 * decrypted when copied in ram */
490#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300491 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300492 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300493 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100494 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300495 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300496 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100497 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300498 }
499 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300500#endif
501
Raef Colese8fe6cf2020-05-26 13:07:40 +0100502 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
503 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300504
Raef Colese8fe6cf2020-05-26 13:07:40 +0100505 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800506}
507
Tamas Banfe031092020-09-10 17:32:39 +0200508#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100509static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800510split_image_check(struct image_header *app_hdr,
511 const struct flash_area *app_fap,
512 struct image_header *loader_hdr,
513 const struct flash_area *loader_fap)
514{
515 static void *tmpbuf;
516 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100517 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518
519 if (!tmpbuf) {
520 tmpbuf = malloc(BOOT_TMPBUF_SZ);
521 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100522 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800523 }
524 }
525
Raef Colese8fe6cf2020-05-26 13:07:40 +0100526 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
527 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
528 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
529 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530 }
531
Raef Colese8fe6cf2020-05-26 13:07:40 +0100532 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
533 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800534
Raef Colese8fe6cf2020-05-26 13:07:40 +0100535out:
536 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537}
Tamas Banfe031092020-09-10 17:32:39 +0200538#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800539
Fabio Utzig338a19f2018-12-03 08:37:08 -0200540/*
David Brown9bf95af2019-10-10 15:36:36 -0600541 * Check that this is a valid header. Valid means that the magic is
542 * correct, and that the sizes/offsets are "sane". Sane means that
543 * there is no overflow on the arithmetic, and that the result fits
544 * within the flash area we are in.
545 */
546static bool
547boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
548{
549 uint32_t size;
550
551 if (hdr->ih_magic != IMAGE_MAGIC) {
552 return false;
553 }
554
555 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
556 return false;
557 }
558
Dominik Ermel260ae092021-04-23 05:38:45 +0000559 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600560 return false;
561 }
562
563 return true;
564}
565
566/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200567 * Check that a memory area consists of a given value.
568 */
569static inline bool
570boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300571{
572 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200573 uint8_t *p = (uint8_t *)data;
574 for (i = 0; i < len; i++) {
575 if (val != p[i]) {
576 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300577 }
578 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200579 return true;
580}
581
582static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300583boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200584{
585 const struct flash_area *fap;
586 struct image_header *hdr;
587 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300588 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200589 int rc;
590
Fabio Utzig10ee6482019-08-01 12:04:52 -0300591 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300592 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200593 if (rc != 0) {
594 return -1;
595 }
596
597 erased_val = flash_area_erased_val(fap);
598 flash_area_close(fap);
599
Fabio Utzig10ee6482019-08-01 12:04:52 -0300600 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200601 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
602 return -1;
603 }
604
605 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300606}
607
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000608#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200609 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200610 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000611 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
612/**
David Vincze8b0b6372020-05-20 19:54:44 +0200613 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000614 *
David Vincze8b0b6372020-05-20 19:54:44 +0200615 * @param ver1 Pointer to the first image version to compare.
616 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000617 *
David Vincze8b0b6372020-05-20 19:54:44 +0200618 * @retval -1 If ver1 is strictly less than ver2.
619 * @retval 0 If the image version numbers are equal,
620 * (not including the build number).
621 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000622 */
623static int
David Vincze8b0b6372020-05-20 19:54:44 +0200624boot_version_cmp(const struct image_version *ver1,
625 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000626{
David Vincze8b0b6372020-05-20 19:54:44 +0200627 if (ver1->iv_major > ver2->iv_major) {
628 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000629 }
David Vincze8b0b6372020-05-20 19:54:44 +0200630 if (ver1->iv_major < ver2->iv_major) {
631 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000632 }
David Vincze8b0b6372020-05-20 19:54:44 +0200633 /* The major version numbers are equal, continue comparison. */
634 if (ver1->iv_minor > ver2->iv_minor) {
635 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000636 }
David Vincze8b0b6372020-05-20 19:54:44 +0200637 if (ver1->iv_minor < ver2->iv_minor) {
638 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000639 }
David Vincze8b0b6372020-05-20 19:54:44 +0200640 /* The minor version numbers are equal, continue comparison. */
641 if (ver1->iv_revision > ver2->iv_revision) {
642 return 1;
643 }
644 if (ver1->iv_revision < ver2->iv_revision) {
645 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000646 }
647
648 return 0;
649}
650#endif
651
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000652#if defined(MCUBOOT_DIRECT_XIP)
653/**
654 * Check if image in slot has been set with specific ROM address to run from
655 * and whether the slot starts at that address.
656 *
657 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
658 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
659 * header matches the slot address;
660 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
661 * does not match the slot address.
662 */
663static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100664boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000665{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100666 uint32_t active_slot;
667 const struct image_header *hdr;
668 uint32_t f_off;
669
Raef Colesfe57e7d2021-10-15 11:07:09 +0100670 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100671 hdr = boot_img_hdr(state, active_slot);
672 f_off = boot_img_slot_off(state, active_slot);
673
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000674 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
675 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100676 ", skipping",
677 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000678 hdr->ih_load_addr);
679
680 /* If there is address mismatch, the image is not bootable from this
681 * slot.
682 */
683 return 1;
684 }
685 return 0;
686}
687#endif
688
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300689/*
690 * Check that there is a valid image in a slot
691 *
692 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100693 * FIH_SUCCESS if image was successfully validated
694 * 1 (or its fih_int encoded form) if no bootloable image was found
695 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300696 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100697static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300698boot_validate_slot(struct boot_loader_state *state, int slot,
699 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800700{
701 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400702 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300703 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100704 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800705 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300706
Fabio Utzig10ee6482019-08-01 12:04:52 -0300707 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300708 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100710 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711 }
712
Fabio Utzig10ee6482019-08-01 12:04:52 -0300713 hdr = boot_img_hdr(state, slot);
714 if (boot_check_header_erased(state, slot) == 0 ||
715 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300716
717#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
718 /*
719 * This fixes an issue where an image might be erased, but a trailer
720 * be left behind. It can happen if the image is in the secondary slot
721 * and did not pass validation, in which case the whole slot is erased.
722 * If during the erase operation, a reset occurs, parts of the slot
723 * might have been erased while some did not. The concerning part is
724 * the trailer because it might disable a new image from being loaded
725 * through mcumgr; so we just get rid of the trailer here, if the header
726 * is erased.
727 */
728 if (slot != BOOT_PRIMARY_SLOT) {
729 swap_erase_trailer_sectors(state, fap);
730 }
731#endif
732
David Vincze2d736ad2019-02-18 11:50:22 +0100733 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100734 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200735 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300736 }
737
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000738#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
739 if (slot != BOOT_PRIMARY_SLOT) {
740 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200741 rc = boot_version_cmp(
742 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
743 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
744 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000745 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000746 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000747 /* Image in the secondary slot does not satisfy version requirement.
748 * Erase the image and continue booting from the primary slot.
749 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100750 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000751 goto out;
752 }
753 }
754#endif
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200755 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
756 fih_rc, BOOT_CURR_IMG(state), slot);
Andrzej Puzdrowski9d4d45c2021-09-14 17:25:58 +0200757 if (fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200758 {
759 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
760 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100761 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200762 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000763 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200764 /* Image is invalid, erase it to prevent further unnecessary
765 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700766 */
767 }
David Brown098de832019-12-10 11:58:01 -0700768#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100769 BOOT_LOG_ERR("Image in the %s slot is not valid!",
770 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700771#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100772 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200773 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800774 }
775
Fabio Utzig338a19f2018-12-03 08:37:08 -0200776out:
777 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100778
779 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800780}
781
David Vinczec3084132020-02-18 14:50:47 +0100782#ifdef MCUBOOT_HW_ROLLBACK_PROT
783/**
784 * Updates the stored security counter value with the image's security counter
785 * value which resides in the given slot, only if it's greater than the stored
786 * value.
787 *
788 * @param image_index Index of the image to determine which security
789 * counter to update.
790 * @param slot Slot number of the image.
791 * @param hdr Pointer to the image header structure of the image
792 * that is currently stored in the given slot.
793 *
794 * @return 0 on success; nonzero on failure.
795 */
796static int
797boot_update_security_counter(uint8_t image_index, int slot,
798 struct image_header *hdr)
799{
800 const struct flash_area *fap = NULL;
801 uint32_t img_security_cnt;
802 int rc;
803
804 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
805 &fap);
806 if (rc != 0) {
807 rc = BOOT_EFLASH;
808 goto done;
809 }
810
811 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
812 if (rc != 0) {
813 goto done;
814 }
815
816 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
817 if (rc != 0) {
818 goto done;
819 }
820
821done:
822 flash_area_close(fap);
823 return rc;
824}
825#endif /* MCUBOOT_HW_ROLLBACK_PROT */
826
Tamas Banfe031092020-09-10 17:32:39 +0200827#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200828/**
829 * Determines which swap operation to perform, if any. If it is determined
830 * that a swap operation is required, the image in the secondary slot is checked
831 * for validity. If the image in the secondary slot is invalid, it is erased,
832 * and a swap type of "none" is indicated.
833 *
834 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
835 */
836static int
837boot_validated_swap_type(struct boot_loader_state *state,
838 struct boot_status *bs)
839{
840 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100841 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200842
843 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
844 if (BOOT_IS_UPGRADE(swap_type)) {
845 /* Boot loader wants to switch to the secondary slot.
846 * Ensure image is valid.
847 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100848 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
849 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
850 if (fih_eq(fih_rc, fih_int_encode(1))) {
851 swap_type = BOOT_SWAP_TYPE_NONE;
852 } else {
853 swap_type = BOOT_SWAP_TYPE_FAIL;
854 }
David Vinczee574f2d2020-07-10 11:42:03 +0200855 }
856 }
857
858 return swap_type;
859}
David Brown94ed12c2021-05-26 16:28:14 -0600860#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200861
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800863 * Erases a region of flash.
864 *
Fabio Utzigba829042018-09-18 08:29:34 -0300865 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800866 * @param off The offset within the flash area to start the
867 * erase.
868 * @param sz The number of bytes to erase.
869 *
870 * @return 0 on success; nonzero on failure.
871 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300872int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300873boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800874{
Fabio Utzigba829042018-09-18 08:29:34 -0300875 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876}
877
David Brown94ed12c2021-05-26 16:28:14 -0600878#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879/**
880 * Copies the contents of one flash region to another. You must erase the
881 * destination region prior to calling this function.
882 *
883 * @param flash_area_id_src The ID of the source flash area.
884 * @param flash_area_id_dst The ID of the destination flash area.
885 * @param off_src The offset within the source flash area to
886 * copy from.
887 * @param off_dst The offset within the destination flash area to
888 * copy to.
889 * @param sz The number of bytes to copy.
890 *
891 * @return 0 on success; nonzero on failure.
892 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300893int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300894boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300895 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300896 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800897 uint32_t off_src, uint32_t off_dst, uint32_t sz)
898{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800899 uint32_t bytes_copied;
900 int chunk_sz;
901 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300902#ifdef MCUBOOT_ENC_IMAGES
903 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300904 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300905 size_t blk_off;
906 struct image_header *hdr;
907 uint16_t idx;
908 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300909 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300910#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800911
Marek Pietae51ec072021-07-15 14:53:10 +0200912 TARGET_STATIC uint8_t buf[1024] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300913
914#if !defined(MCUBOOT_ENC_IMAGES)
915 (void)state;
916#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800917
Christopher Collins92ea77f2016-12-12 15:59:26 -0800918 bytes_copied = 0;
919 while (bytes_copied < sz) {
920 if (sz - bytes_copied > sizeof buf) {
921 chunk_sz = sizeof buf;
922 } else {
923 chunk_sz = sz - bytes_copied;
924 }
925
926 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
927 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300928 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929 }
930
Fabio Utzigba829042018-09-18 08:29:34 -0300931#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300932 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000933 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
934 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
935 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
936 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100937 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300938 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300939#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300940 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000941 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100942 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300943 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300944 off = off_dst;
945 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300946#else
947 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000948 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300949 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
950 }
951#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200952 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300953 blk_sz = chunk_sz;
954 idx = 0;
955 if (off + bytes_copied < hdr->ih_hdr_size) {
956 /* do not decrypt header */
957 blk_off = 0;
958 blk_sz = chunk_sz - hdr->ih_hdr_size;
959 idx = hdr->ih_hdr_size;
960 } else {
961 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
962 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300963 tlv_off = BOOT_TLV_OFF(hdr);
964 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300965 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300966 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300967 blk_sz = 0;
968 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300969 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300970 }
971 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300972 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300973 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
974 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300975 }
976 }
977#endif
978
Christopher Collins92ea77f2016-12-12 15:59:26 -0800979 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
980 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300981 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982 }
983
984 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300985
986 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800987 }
988
Fabio Utzigba829042018-09-18 08:29:34 -0300989 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800990}
991
Christopher Collins92ea77f2016-12-12 15:59:26 -0800992/**
David Vincze2d736ad2019-02-18 11:50:22 +0100993 * Overwrite primary slot with the image contained in the secondary slot.
994 * If a prior copy operation was interrupted by a system reset, this function
995 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800996 *
997 * @param bs The current boot status. This function reads
998 * this struct to determine if it is resuming
999 * an interrupted swap operation. This
1000 * function writes the updated status to this
1001 * function on return.
1002 *
1003 * @return 0 on success; nonzero on failure.
1004 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001005#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001006static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001007boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001008{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001009 size_t sect_count;
1010 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001011 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001012 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001013 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001014 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001015 const struct flash_area *fap_primary_slot;
1016 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001017 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001018
Fabio Utzigb4f88102020-10-04 10:16:24 -03001019#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1020 uint32_t sector;
1021 uint32_t trailer_sz;
1022 uint32_t off;
1023 uint32_t sz;
1024#endif
1025
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001026 (void)bs;
1027
Fabio Utzig13d9e352017-10-05 20:32:31 -03001028#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1029 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001030 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001031 assert(rc == 0);
1032#endif
David Brown17609d82017-05-05 09:41:34 -06001033
David Vincze2d736ad2019-02-18 11:50:22 +01001034 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1035 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001036
Fabio Utzigb0f04732019-07-31 09:49:19 -03001037 image_index = BOOT_CURR_IMG(state);
1038
1039 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1040 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001041 assert (rc == 0);
1042
Fabio Utzigb0f04732019-07-31 09:49:19 -03001043 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1044 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001045 assert (rc == 0);
1046
Fabio Utzig10ee6482019-08-01 12:04:52 -03001047 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001048 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001049 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001050 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001051 assert(rc == 0);
1052
Fabio Utzig13d9e352017-10-05 20:32:31 -03001053#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001054 if ((size + this_size) >= src_size) {
1055 size += src_size - size;
1056 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001057 break;
1058 }
1059#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001060
1061 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001062 }
1063
Fabio Utzigb4f88102020-10-04 10:16:24 -03001064#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1065 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1066 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1067 sz = 0;
1068 do {
1069 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1070 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1071 sector--;
1072 } while (sz < trailer_sz);
1073
1074 rc = boot_erase_region(fap_primary_slot, off, sz);
1075 assert(rc == 0);
1076#endif
1077
Fabio Utzigba829042018-09-18 08:29:34 -03001078#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001079 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001080 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001081 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001082 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001083
Fabio Utzigba829042018-09-18 08:29:34 -03001084 if (rc < 0) {
1085 return BOOT_EBADIMAGE;
1086 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001087 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001088 return BOOT_EBADIMAGE;
1089 }
1090 }
1091#endif
1092
David Vincze2d736ad2019-02-18 11:50:22 +01001093 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1094 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001095 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001096 if (rc != 0) {
1097 return rc;
1098 }
1099
1100#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1101 rc = boot_write_magic(fap_primary_slot);
1102 if (rc != 0) {
1103 return rc;
1104 }
1105#endif
David Brown17609d82017-05-05 09:41:34 -06001106
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001107 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1108 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1109 if (rc != 0) {
1110 return rc;
1111 }
1112
David Vinczec3084132020-02-18 14:50:47 +01001113#ifdef MCUBOOT_HW_ROLLBACK_PROT
1114 /* Update the stored security counter with the new image's security counter
1115 * value. Both slots hold the new image at this point, but the secondary
1116 * slot's image header must be passed since the image headers in the
1117 * boot_data structure have not been updated yet.
1118 */
1119 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1120 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1121 if (rc != 0) {
1122 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1123 return rc;
1124 }
1125#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1126
Fabio Utzig13d9e352017-10-05 20:32:31 -03001127 /*
1128 * Erases header and trailer. The trailer is erased because when a new
1129 * image is written without a trailer as is the case when using newt, the
1130 * trailer that was left might trigger a new upgrade.
1131 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001132 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001133 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001134 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1135 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001136 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001137 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001138 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001139 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001140 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1141 last_sector),
1142 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1143 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001144 assert(rc == 0);
1145
David Vincze2d736ad2019-02-18 11:50:22 +01001146 flash_area_close(fap_primary_slot);
1147 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001148
David Vincze2d736ad2019-02-18 11:50:22 +01001149 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001150
1151 return 0;
1152}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001153#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001154
Christopher Collinsa1c12042019-05-23 14:00:28 -07001155#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001156/**
1157 * Swaps the two images in flash. If a prior copy operation was interrupted
1158 * by a system reset, this function completes that operation.
1159 *
1160 * @param bs The current boot status. This function reads
1161 * this struct to determine if it is resuming
1162 * an interrupted swap operation. This
1163 * function writes the updated status to this
1164 * function on return.
1165 *
1166 * @return 0 on success; nonzero on failure.
1167 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001168static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001169boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001170{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001171 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001172#ifdef MCUBOOT_ENC_IMAGES
1173 const struct flash_area *fap;
1174 uint8_t slot;
1175 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001176#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001177 uint32_t size;
1178 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001179 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001180 int rc;
1181
1182 /* FIXME: just do this if asked by user? */
1183
1184 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001185 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001186
Fabio Utzig12d59162019-11-28 10:01:59 -03001187 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001188 /*
1189 * No swap ever happened, so need to find the largest image which
1190 * will be used to determine the amount of sectors to swap.
1191 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001192 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001193 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001194 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001195 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001196 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001197
Fabio Utzigba829042018-09-18 08:29:34 -03001198#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001199 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001200 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001201 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001202 assert(rc >= 0);
1203
1204 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001205 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001206 assert(rc == 0);
1207 } else {
1208 rc = 0;
1209 }
1210 } else {
1211 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1212 }
1213#endif
1214
Fabio Utzig10ee6482019-08-01 12:04:52 -03001215 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001216 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001217 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001218 assert(rc == 0);
1219 }
1220
Fabio Utzigba829042018-09-18 08:29:34 -03001221#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001222 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001223 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001224 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001225 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001226 assert(rc >= 0);
1227
1228 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001229 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001230 assert(rc == 0);
1231 } else {
1232 rc = 0;
1233 }
1234 } else {
1235 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1236 }
1237#endif
1238
Fabio Utzig46490722017-09-04 15:34:32 -03001239 if (size > copy_size) {
1240 copy_size = size;
1241 }
1242
1243 bs->swap_size = copy_size;
1244 } else {
1245 /*
1246 * If a swap was under way, the swap_size should already be present
1247 * in the trailer...
1248 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001249 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001250 assert(rc == 0);
1251
1252 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001253
1254#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001255 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1256 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001257 assert(rc == 0);
1258
1259 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001260 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001261 break;
1262 }
1263 }
1264
1265 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001266 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001267 }
1268 }
1269#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001270 }
1271
Fabio Utzig12d59162019-11-28 10:01:59 -03001272 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001273
David Vincze2d736ad2019-02-18 11:50:22 +01001274#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001275 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001276 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001277 BOOT_LOG_WRN("%d status write fails performing the swap",
1278 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001279 }
1280#endif
1281
Christopher Collins92ea77f2016-12-12 15:59:26 -08001282 return 0;
1283}
David Brown17609d82017-05-05 09:41:34 -06001284#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001285
David Vinczee32483f2019-06-13 10:46:24 +02001286#if (BOOT_IMAGE_NUMBER > 1)
1287/**
1288 * Check the image dependency whether it is satisfied and modify
1289 * the swap type if necessary.
1290 *
1291 * @param dep Image dependency which has to be verified.
1292 *
1293 * @return 0 on success; nonzero on failure.
1294 */
1295static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001296boot_verify_slot_dependency(struct boot_loader_state *state,
1297 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001298{
1299 struct image_version *dep_version;
1300 size_t dep_slot;
1301 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001302 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001303
1304 /* Determine the source of the image which is the subject of
1305 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001306 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001307 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1308 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001309 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001310
David Vincze8b0b6372020-05-20 19:54:44 +02001311 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1312 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001313 /* Dependency not satisfied.
1314 * Modify the swap type to decrease the version number of the image
1315 * (which will be located in the primary slot after the boot process),
1316 * consequently the number of unsatisfied dependencies will be
1317 * decreased or remain the same.
1318 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001319 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001320 case BOOT_SWAP_TYPE_TEST:
1321 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001322 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001323 break;
1324 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001325 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001326 break;
1327 default:
1328 break;
1329 }
David Vincze8b0b6372020-05-20 19:54:44 +02001330 } else {
1331 /* Dependency satisfied. */
1332 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001333 }
1334
1335 return rc;
1336}
1337
1338/**
1339 * Read all dependency TLVs of an image from the flash and verify
1340 * one after another to see if they are all satisfied.
1341 *
1342 * @param slot Image slot number.
1343 *
1344 * @return 0 on success; nonzero on failure.
1345 */
1346static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001347boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001348{
1349 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001350 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001351 struct image_dependency dep;
1352 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001353 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001354 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001355 int rc;
1356
Fabio Utzig10ee6482019-08-01 12:04:52 -03001357 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001358 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001359 if (rc != 0) {
1360 rc = BOOT_EFLASH;
1361 goto done;
1362 }
1363
Fabio Utzig61fd8882019-09-14 20:00:20 -03001364 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1365 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001366 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001367 goto done;
1368 }
1369
Fabio Utzig61fd8882019-09-14 20:00:20 -03001370 while (true) {
1371 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1372 if (rc < 0) {
1373 return -1;
1374 } else if (rc > 0) {
1375 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001376 break;
1377 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001378
1379 if (len != sizeof(dep)) {
1380 rc = BOOT_EBADIMAGE;
1381 goto done;
1382 }
1383
1384 rc = flash_area_read(fap, off, &dep, len);
1385 if (rc != 0) {
1386 rc = BOOT_EFLASH;
1387 goto done;
1388 }
1389
1390 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1391 rc = BOOT_EBADARGS;
1392 goto done;
1393 }
1394
1395 /* Verify dependency and modify the swap type if not satisfied. */
1396 rc = boot_verify_slot_dependency(state, &dep);
1397 if (rc != 0) {
1398 /* Dependency not satisfied. */
1399 goto done;
1400 }
David Vinczee32483f2019-06-13 10:46:24 +02001401 }
1402
1403done:
1404 flash_area_close(fap);
1405 return rc;
1406}
1407
1408/**
David Vinczee32483f2019-06-13 10:46:24 +02001409 * Iterate over all the images and verify whether the image dependencies in the
1410 * TLV area are all satisfied and update the related swap type if necessary.
1411 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001412static int
1413boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001414{
Erik Johnson49063752020-02-06 09:59:31 -06001415 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001416 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001417
Fabio Utzig10ee6482019-08-01 12:04:52 -03001418 BOOT_CURR_IMG(state) = 0;
1419 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001420 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1421 BOOT_CURR_IMG(state)++;
1422 continue;
1423 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001424 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1425 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1426 slot = BOOT_SECONDARY_SLOT;
1427 } else {
1428 slot = BOOT_PRIMARY_SLOT;
1429 }
1430
1431 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001432 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001433 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001434 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001435 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001436 /* Cannot upgrade due to non-met dependencies, so disable all
1437 * image upgrades.
1438 */
1439 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1440 BOOT_CURR_IMG(state) = idx;
1441 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1442 }
1443 break;
David Vinczee32483f2019-06-13 10:46:24 +02001444 }
1445 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001446 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001447}
1448#endif /* (BOOT_IMAGE_NUMBER > 1) */
1449
Christopher Collins92ea77f2016-12-12 15:59:26 -08001450/**
David Vinczeba3bd602019-06-17 16:01:43 +02001451 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001452 *
David Vinczeba3bd602019-06-17 16:01:43 +02001453 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001454 *
1455 * @return 0 on success; nonzero on failure.
1456 */
1457static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001458boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001459{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001460 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001461#ifndef MCUBOOT_OVERWRITE_ONLY
1462 uint8_t swap_type;
1463#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001464
David Vinczeba3bd602019-06-17 16:01:43 +02001465 /* At this point there are no aborted swaps. */
1466#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001467 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001468#elif defined(MCUBOOT_BOOTSTRAP)
1469 /* Check if the image update was triggered by a bad image in the
1470 * primary slot (the validity of the image in the secondary slot had
1471 * already been checked).
1472 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001473 fih_int fih_rc = FIH_FAILURE;
1474 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1475 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1476 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001477 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001478 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001479 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001480 }
1481#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001482 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001483#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001484 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001485
1486#ifndef MCUBOOT_OVERWRITE_ONLY
1487 /* The following state needs image_ok be explicitly set after the
1488 * swap was finished to avoid a new revert.
1489 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001490 swap_type = BOOT_SWAP_TYPE(state);
1491 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1492 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001493 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001494 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001495 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001496 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001497 }
1498
David Vinczec3084132020-02-18 14:50:47 +01001499#ifdef MCUBOOT_HW_ROLLBACK_PROT
1500 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1501 /* Update the stored security counter with the new image's security
1502 * counter value. The primary slot holds the new image at this point,
1503 * but the secondary slot's image header must be passed since image
1504 * headers in the boot_data structure have not been updated yet.
1505 *
1506 * In case of a permanent image swap mcuboot will never attempt to
1507 * revert the images on the next reboot. Therefore, the security
1508 * counter must be increased right after the image upgrade.
1509 */
1510 rc = boot_update_security_counter(
1511 BOOT_CURR_IMG(state),
1512 BOOT_PRIMARY_SLOT,
1513 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1514 if (rc != 0) {
1515 BOOT_LOG_ERR("Security counter update failed after "
1516 "image upgrade.");
1517 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1518 }
1519 }
1520#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1521
Fabio Utzige575b0b2019-09-11 12:34:23 -03001522 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001523 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001524 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001525 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001526 }
David Vinczeba3bd602019-06-17 16:01:43 +02001527 }
1528#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001529
David Vinczeba3bd602019-06-17 16:01:43 +02001530 return rc;
1531}
1532
1533/**
1534 * Completes a previously aborted image swap.
1535 *
1536 * @param bs The current boot status.
1537 *
1538 * @return 0 on success; nonzero on failure.
1539 */
1540#if !defined(MCUBOOT_OVERWRITE_ONLY)
1541static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001542boot_complete_partial_swap(struct boot_loader_state *state,
1543 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001544{
1545 int rc;
1546
1547 /* Determine the type of swap operation being resumed from the
1548 * `swap-type` trailer field.
1549 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001550 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001551 assert(rc == 0);
1552
Fabio Utzig10ee6482019-08-01 12:04:52 -03001553 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001554
1555 /* The following states need image_ok be explicitly set after the
1556 * swap was finished to avoid a new revert.
1557 */
1558 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1559 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001560 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001561 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001562 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001563 }
1564 }
1565
Fabio Utzige575b0b2019-09-11 12:34:23 -03001566 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001567 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001568 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001569 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001570 }
1571 }
1572
Fabio Utzig10ee6482019-08-01 12:04:52 -03001573 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001574 BOOT_LOG_ERR("panic!");
1575 assert(0);
1576
1577 /* Loop forever... */
1578 while (1) {}
1579 }
1580
1581 return rc;
1582}
1583#endif /* !MCUBOOT_OVERWRITE_ONLY */
1584
1585#if (BOOT_IMAGE_NUMBER > 1)
1586/**
1587 * Review the validity of previously determined swap types of other images.
1588 *
1589 * @param aborted_swap The current image upgrade is a
1590 * partial/aborted swap.
1591 */
1592static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001593boot_review_image_swap_types(struct boot_loader_state *state,
1594 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001595{
1596 /* In that case if we rebooted in the middle of an image upgrade process, we
1597 * must review the validity of swap types, that were previously determined
1598 * for other images. The image_ok flag had not been set before the reboot
1599 * for any of the updated images (only the copy_done flag) and thus falsely
1600 * the REVERT swap type has been determined for the previous images that had
1601 * been updated before the reboot.
1602 *
1603 * There are two separate scenarios that we have to deal with:
1604 *
1605 * 1. The reboot has happened during swapping an image:
1606 * The current image upgrade has been determined as a
1607 * partial/aborted swap.
1608 * 2. The reboot has happened between two separate image upgrades:
1609 * In this scenario we must check the swap type of the current image.
1610 * In those cases if it is NONE or REVERT we cannot certainly determine
1611 * the fact of a reboot. In a consistent state images must move in the
1612 * same direction or stay in place, e.g. in practice REVERT and TEST
1613 * swap types cannot be present at the same time. If the swap type of
1614 * the current image is either TEST, PERM or FAIL we must review the
1615 * already determined swap types of other images and set each false
1616 * REVERT swap types to NONE (these images had been successfully
1617 * updated before the system rebooted between two separate image
1618 * upgrades).
1619 */
1620
Fabio Utzig10ee6482019-08-01 12:04:52 -03001621 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001622 /* Nothing to do */
1623 return;
1624 }
1625
1626 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001627 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1628 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001629 /* Nothing to do */
1630 return;
1631 }
1632 }
1633
Fabio Utzig10ee6482019-08-01 12:04:52 -03001634 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1635 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1636 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001637 }
1638 }
1639}
1640#endif
1641
1642/**
1643 * Prepare image to be updated if required.
1644 *
1645 * Prepare image to be updated if required with completing an image swap
1646 * operation if one was aborted and/or determining the type of the
1647 * swap operation. In case of any error set the swap type to NONE.
1648 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001649 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001650 * @param bs Pointer where the read and possibly updated
1651 * boot status can be written to.
1652 */
1653static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001654boot_prepare_image_for_update(struct boot_loader_state *state,
1655 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001656{
1657 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001658 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001659
1660 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001661 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001662 if (rc != 0) {
1663 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1664 " - too small?", BOOT_MAX_IMG_SECTORS);
1665 /* Unable to determine sector layout, continue with next image
1666 * if there is one.
1667 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001668 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001669 if (rc == BOOT_EFLASH)
1670 {
1671 /* Only return on error from the primary image flash */
1672 return;
1673 }
David Vinczeba3bd602019-06-17 16:01:43 +02001674 }
1675
1676 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001677 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001678 if (rc != 0) {
1679 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001680 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001681 BOOT_CURR_IMG(state));
1682 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001683 return;
1684 }
1685
1686 /* If the current image's slots aren't compatible, no swap is possible.
1687 * Just boot into primary slot.
1688 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001689 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001690 boot_status_reset(bs);
1691
1692#ifndef MCUBOOT_OVERWRITE_ONLY
1693 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001694 if (rc != 0) {
1695 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001696 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001697 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001698 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001699 return;
1700 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001701#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001702
Fabio Utzig74aef312019-11-28 11:05:34 -03001703#ifdef MCUBOOT_SWAP_USING_MOVE
1704 /*
1705 * Must re-read image headers because the boot status might
1706 * have been updated in the previous function call.
1707 */
1708 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001709#ifdef MCUBOOT_BOOTSTRAP
1710 /* When bootstrapping it's OK to not have image magic in the primary slot */
1711 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1712 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1713#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001714 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001715#endif
1716
Fabio Utzig74aef312019-11-28 11:05:34 -03001717 /* Continue with next image if there is one. */
1718 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1719 BOOT_CURR_IMG(state));
1720 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1721 return;
1722 }
1723#endif
1724
David Vinczeba3bd602019-06-17 16:01:43 +02001725 /* Determine if we rebooted in the middle of an image swap
1726 * operation. If a partial swap was detected, complete it.
1727 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001728 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001729
1730#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001731 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001732#endif
1733
1734#ifdef MCUBOOT_OVERWRITE_ONLY
1735 /* Should never arrive here, overwrite-only mode has
1736 * no swap state.
1737 */
1738 assert(0);
1739#else
1740 /* Determine the type of swap operation being resumed from the
1741 * `swap-type` trailer field.
1742 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001743 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001744 assert(rc == 0);
1745#endif
1746 /* Attempt to read an image header from each slot. Ensure that
1747 * image headers in slots are aligned with headers in boot_data.
1748 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001749 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001750 assert(rc == 0);
1751
1752 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001753 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001754 } else {
1755 /* There was no partial swap, determine swap type. */
1756 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001757 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001758 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001759 FIH_CALL(boot_validate_slot, fih_rc,
1760 state, BOOT_SECONDARY_SLOT, bs);
1761 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1762 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1763 } else {
1764 BOOT_SWAP_TYPE(state) = bs->swap_type;
1765 }
David Vinczeba3bd602019-06-17 16:01:43 +02001766 }
1767
1768#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001769 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001770#endif
1771
1772#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001773 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001774 /* Header checks are done first because they are
1775 * inexpensive. Since overwrite-only copies starting from
1776 * offset 0, if interrupted, it might leave a valid header
1777 * magic, so also run validation on the primary slot to be
1778 * sure it's not OK.
1779 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001780 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1781 FIH_CALL(boot_validate_slot, fih_rc,
1782 state, BOOT_PRIMARY_SLOT, bs);
1783
1784 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1785
Fabio Utzig3d77c952020-10-04 10:23:17 -03001786 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001787 FIH_CALL(boot_validate_slot, fih_rc,
1788 state, BOOT_SECONDARY_SLOT, bs);
1789
Fabio Utzig3d77c952020-10-04 10:23:17 -03001790 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001791 /* Set swap type to REVERT to overwrite the primary
1792 * slot with the image contained in secondary slot
1793 * and to trigger the explicit setting of the
1794 * image_ok flag.
1795 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001796 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001797 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001798 }
1799 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001800#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001801 }
David Vinczeba3bd602019-06-17 16:01:43 +02001802 } else {
1803 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001804 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001805 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001806}
1807
Mark Horvathccaf7f82021-01-04 18:16:42 +01001808/**
1809 * Updates the security counter for the current image.
1810 *
1811 * @param state Boot loader status information.
1812 *
1813 * @return 0 on success; nonzero on failure.
1814 */
1815static int
1816boot_update_hw_rollback_protection(struct boot_loader_state *state)
1817{
1818#ifdef MCUBOOT_HW_ROLLBACK_PROT
1819 int rc;
1820
1821 /* Update the stored security counter with the active image's security
1822 * counter value. It will only be updated if the new security counter is
1823 * greater than the stored value.
1824 *
1825 * In case of a successful image swapping when the swap type is TEST the
1826 * security counter can be increased only after a reset, when the swap
1827 * type is NONE and the image has marked itself "OK" (the image_ok flag
1828 * has been set). This way a "revert" can be performed when it's
1829 * necessary.
1830 */
1831 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1832 rc = boot_update_security_counter(
1833 BOOT_CURR_IMG(state),
1834 BOOT_PRIMARY_SLOT,
1835 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1836 if (rc != 0) {
1837 BOOT_LOG_ERR("Security counter update failed after image "
1838 "validation.");
1839 return rc;
1840 }
1841 }
1842
1843 return 0;
1844
1845#else /* MCUBOOT_HW_ROLLBACK_PROT */
1846 (void) (state);
1847
1848 return 0;
1849#endif
1850}
1851
Raef Colese8fe6cf2020-05-26 13:07:40 +01001852fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001853context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001854{
Marti Bolivar84898652017-06-13 17:20:22 -04001855 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001856 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001857 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001858 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001859 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001860 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001861 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001862
1863 /* The array of slot sectors are defined here (as opposed to file scope) so
1864 * that they don't get allocated for non-boot-loader apps. This is
1865 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001866 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001867 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001868 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1869 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001870#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001871 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001872#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001873
Fabio Utzig298913b2019-08-28 11:22:45 -03001874 has_upgrade = false;
1875
1876#if (BOOT_IMAGE_NUMBER == 1)
1877 (void)has_upgrade;
1878#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001879
David Vinczeba3bd602019-06-17 16:01:43 +02001880 /* Iterate over all the images. By the end of the loop the swap type has
1881 * to be determined for each image and all aborted swaps have to be
1882 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001883 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001884 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01001885#if BOOT_IMAGE_NUMBER > 1
1886 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1887 continue;
1888 }
1889#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001890#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1891 /* The keys used for encryption may no longer be valid (could belong to
1892 * another images). Therefore, mark them as invalid to force their reload
1893 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001894 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001895 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001896#endif
1897
Fabio Utzig10ee6482019-08-01 12:04:52 -03001898 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001899
Fabio Utzig10ee6482019-08-01 12:04:52 -03001900 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001901 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001902 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001903 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001904#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001905 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001906#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001907
1908 /* Open primary and secondary image areas for the duration
1909 * of this call.
1910 */
1911 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001912 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001913 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001914 assert(rc == 0);
1915 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001916#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001917 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001918 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001919 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001920#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001921
1922 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001923 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001924
Fabio Utzige575b0b2019-09-11 12:34:23 -03001925 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001926 has_upgrade = true;
1927 }
David Vinczeba3bd602019-06-17 16:01:43 +02001928 }
1929
David Vinczee32483f2019-06-13 10:46:24 +02001930#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001931 if (has_upgrade) {
1932 /* Iterate over all the images and verify whether the image dependencies
1933 * are all satisfied and update swap type if necessary.
1934 */
1935 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001936 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001937 /*
1938 * It was impossible to upgrade because the expected dependency version
1939 * was not available. Here we already changed the swap_type so that
1940 * instead of asserting the bootloader, we continue and no upgrade is
1941 * performed.
1942 */
1943 rc = 0;
1944 }
1945 }
David Vinczee32483f2019-06-13 10:46:24 +02001946#endif
1947
David Vinczeba3bd602019-06-17 16:01:43 +02001948 /* Iterate over all the images. At this point there are no aborted swaps
1949 * and the swap types are determined for each image. By the end of the loop
1950 * all required update operations will have been finished.
1951 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001952 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001953#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01001954 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1955 continue;
1956 }
1957
David Vinczeba3bd602019-06-17 16:01:43 +02001958#ifdef MCUBOOT_ENC_IMAGES
1959 /* The keys used for encryption may no longer be valid (could belong to
1960 * another images). Therefore, mark them as invalid to force their reload
1961 * by boot_enc_load().
1962 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001963 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001964#endif /* MCUBOOT_ENC_IMAGES */
1965
1966 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001967 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001968#endif /* (BOOT_IMAGE_NUMBER > 1) */
1969
1970 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001971 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001972
Fabio Utzig10ee6482019-08-01 12:04:52 -03001973 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001974 case BOOT_SWAP_TYPE_NONE:
1975 break;
1976
1977 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1978 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1979 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001980 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
1981 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
1982 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
1983 if (rc == BOOT_HOOK_REGULAR)
1984 {
1985 rc = boot_perform_update(state, &bs);
1986 }
David Vinczeba3bd602019-06-17 16:01:43 +02001987 assert(rc == 0);
1988 break;
1989
1990 case BOOT_SWAP_TYPE_FAIL:
1991 /* The image in secondary slot was invalid and is now erased. Ensure
1992 * we don't try to boot into it again on the next reboot. Do this by
1993 * pretending we just reverted back to primary slot.
1994 */
1995#ifndef MCUBOOT_OVERWRITE_ONLY
1996 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001997 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001998 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001999 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002000 }
2001#endif /* !MCUBOOT_OVERWRITE_ONLY */
2002 break;
2003
2004 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002005 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002006 }
2007
Fabio Utzig10ee6482019-08-01 12:04:52 -03002008 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002009 BOOT_LOG_ERR("panic!");
2010 assert(0);
2011
2012 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002013 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002014 }
2015 }
2016
2017 /* Iterate over all the images. At this point all required update operations
2018 * have finished. By the end of the loop each image in the primary slot will
2019 * have been re-validated.
2020 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002021 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002022#if BOOT_IMAGE_NUMBER > 1
2023 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2024 continue;
2025 }
2026#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002027 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002028 /* Attempt to read an image header from each slot. Ensure that image
2029 * headers in slots are aligned with headers in boot_data.
2030 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002031 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002032 if (rc != 0) {
2033 goto out;
2034 }
2035 /* Since headers were reloaded, it can be assumed we just performed
2036 * a swap or overwrite. Now the header info that should be used to
2037 * provide the data for the bootstrap, which previously was at
2038 * secondary slot, was updated to primary slot.
2039 */
2040 }
2041
2042#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002043 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2044 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002045 goto out;
2046 }
2047#else
2048 /* Even if we're not re-validating the primary slot, we could be booting
2049 * onto an empty flash chip. At least do a basic sanity check that
2050 * the magic number on the image is OK.
2051 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002052 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002053 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002054 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002055 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002056 rc = BOOT_EBADIMAGE;
2057 goto out;
2058 }
David Vinczec3084132020-02-18 14:50:47 +01002059#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2060
Mark Horvathccaf7f82021-01-04 18:16:42 +01002061 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002062 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002063 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002064 }
David Vincze1cf11b52020-03-24 07:51:09 +01002065
Mark Horvathccaf7f82021-01-04 18:16:42 +01002066 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002067 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002068 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002069 }
David Vinczeba3bd602019-06-17 16:01:43 +02002070 }
2071
Fabio Utzigf616c542019-12-19 15:23:32 -03002072 /*
2073 * Since the boot_status struct stores plaintext encryption keys, reset
2074 * them here to avoid the possibility of jumping into an image that could
2075 * easily recover them.
2076 */
2077 memset(&bs, 0, sizeof(struct boot_status));
2078
Raef Colesfe57e7d2021-10-15 11:07:09 +01002079 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002080
Raef Colese8fe6cf2020-05-26 13:07:40 +01002081 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002082out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002083 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002084
2085 if (rc) {
2086 fih_rc = fih_int_encode(rc);
2087 }
2088
2089 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002090}
2091
Raef Colese8fe6cf2020-05-26 13:07:40 +01002092fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002093split_go(int loader_slot, int split_slot, void **entry)
2094{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002095 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002096 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002097 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002098 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002099 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002100 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002101
Christopher Collins92ea77f2016-12-12 15:59:26 -08002102 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2103 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002104 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002105 }
David Vinczeba3bd602019-06-17 16:01:43 +02002106 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2107 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002108
2109 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2110 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002111 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002112 assert(rc == 0);
2113 split_flash_id = flash_area_id_from_image_slot(split_slot);
2114 rc = flash_area_open(split_flash_id,
2115 &BOOT_IMG_AREA(&boot_data, split_slot));
2116 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002117
2118 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002119 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002120 if (rc != 0) {
2121 rc = SPLIT_GO_ERR;
2122 goto done;
2123 }
2124
Fabio Utzig12d59162019-11-28 10:01:59 -03002125 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002126 if (rc != 0) {
2127 goto done;
2128 }
2129
Christopher Collins92ea77f2016-12-12 15:59:26 -08002130 /* Don't check the bootable image flag because we could really call a
2131 * bootable or non-bootable image. Just validate that the image check
2132 * passes which is distinct from the normal check.
2133 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002134 FIH_CALL(split_image_check, fih_rc,
2135 boot_img_hdr(&boot_data, split_slot),
2136 BOOT_IMG_AREA(&boot_data, split_slot),
2137 boot_img_hdr(&boot_data, loader_slot),
2138 BOOT_IMG_AREA(&boot_data, loader_slot));
2139 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002140 goto done;
2141 }
2142
Marti Bolivarea088872017-06-12 17:10:49 -04002143 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002144 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002145 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002146 rc = SPLIT_GO_OK;
2147
2148done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002149 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2150 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002151 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002152
2153 if (rc) {
2154 fih_rc = fih_int_encode(rc);
2155 }
2156
2157 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002158}
David Vinczee574f2d2020-07-10 11:42:03 +02002159
Tamas Banfe031092020-09-10 17:32:39 +02002160#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002161
Mark Horvathccaf7f82021-01-04 18:16:42 +01002162#define NO_ACTIVE_SLOT UINT32_MAX
2163
David Vinczee574f2d2020-07-10 11:42:03 +02002164/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002165 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002166 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002167 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002168 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002169 * @return 0 on success; nonzero on failure.
2170 */
2171static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002172boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002173{
2174 uint32_t slot;
2175 int fa_id;
2176 int rc;
2177 struct image_header *hdr = NULL;
2178
2179 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002180#if BOOT_IMAGE_NUMBER > 1
2181 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2182 continue;
2183 }
2184#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002185 /* Open all the slots */
2186 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2187 fa_id = flash_area_id_from_multi_image_slot(
2188 BOOT_CURR_IMG(state), slot);
2189 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2190 assert(rc == 0);
2191 }
2192
2193 /* Attempt to read an image header from each slot. */
2194 rc = boot_read_image_headers(state, false, NULL);
2195 if (rc != 0) {
2196 BOOT_LOG_WRN("Failed reading image headers.");
2197 return rc;
2198 }
2199
2200 /* Check headers in all slots */
2201 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2202 hdr = boot_img_hdr(state, slot);
2203
2204 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002205 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002206 BOOT_LOG_IMAGE_INFO(slot, hdr);
2207 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002208 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002209 BOOT_LOG_INF("Image %d %s slot: Image not found",
2210 BOOT_CURR_IMG(state),
2211 (slot == BOOT_PRIMARY_SLOT)
2212 ? "Primary" : "Secondary");
2213 }
2214 }
2215
Raef Colesfe57e7d2021-10-15 11:07:09 +01002216 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002217 }
2218
2219 return 0;
2220}
2221
2222/**
2223 * Finds the slot containing the image with the highest version number for the
2224 * current image.
2225 *
2226 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002227 *
2228 * @return NO_ACTIVE_SLOT if no available slot found, number of
2229 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002230 */
2231static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002232find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002233{
David Vinczee574f2d2020-07-10 11:42:03 +02002234 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002235 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2236 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002237
Mark Horvathccaf7f82021-01-04 18:16:42 +01002238 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002239 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002240 if (candidate_slot == NO_ACTIVE_SLOT) {
2241 candidate_slot = slot;
2242 } else {
2243 rc = boot_version_cmp(
2244 &boot_img_hdr(state, slot)->ih_ver,
2245 &boot_img_hdr(state, candidate_slot)->ih_ver);
2246 if (rc == 1) {
2247 /* The version of the image being examined is greater than
2248 * the version of the current candidate.
2249 */
2250 candidate_slot = slot;
2251 }
2252 }
David Vinczee574f2d2020-07-10 11:42:03 +02002253 }
2254 }
2255
Mark Horvathccaf7f82021-01-04 18:16:42 +01002256 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002257}
2258
Mark Horvathccaf7f82021-01-04 18:16:42 +01002259#ifdef MCUBOOT_HAVE_LOGGING
2260/**
2261 * Prints the state of the loaded images.
2262 *
2263 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002264 */
2265static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002266print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002267{
2268 uint32_t active_slot;
2269
David Brown695e5912021-05-24 16:58:01 -06002270 (void)state;
2271
Mark Horvathccaf7f82021-01-04 18:16:42 +01002272 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002273#if BOOT_IMAGE_NUMBER > 1
2274 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2275 continue;
2276 }
2277#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002278 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002279
2280 BOOT_LOG_INF("Image %d loaded from the %s slot",
2281 BOOT_CURR_IMG(state),
2282 (active_slot == BOOT_PRIMARY_SLOT) ?
2283 "primary" : "secondary");
2284 }
2285}
2286#endif
2287
David Vincze1c456242021-06-29 15:25:24 +02002288#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002289/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002290 * Checks whether the active slot of the current image was previously selected
2291 * to run. Erases the image if it was selected but its execution failed,
2292 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002293 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002294 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002295 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002296 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002297 */
2298static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002299boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002300{
2301 const struct flash_area *fap;
2302 int fa_id;
2303 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002304 uint32_t active_slot;
2305 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002306
Raef Colesfe57e7d2021-10-15 11:07:09 +01002307 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002308
2309 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002310 rc = flash_area_open(fa_id, &fap);
2311 assert(rc == 0);
2312
Raef Colesfe57e7d2021-10-15 11:07:09 +01002313 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002314
2315 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2316 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002317 assert(rc == 0);
2318
Mark Horvathccaf7f82021-01-04 18:16:42 +01002319 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2320 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2321 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002322 /*
2323 * A reboot happened without the image being confirmed at
2324 * runtime or its trailer is corrupted/invalid. Erase the image
2325 * to prevent it from being selected again on the next reboot.
2326 */
2327 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002328 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002329 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002330 assert(rc == 0);
2331
2332 flash_area_close(fap);
2333 rc = -1;
2334 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002335 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2336 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002337 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2338 "value was neither 'set' nor 'unset', but 'bad'.");
2339 }
2340 /*
2341 * Set the copy_done flag, indicating that the image has been
2342 * selected to boot. It can be set in advance, before even
2343 * validating the image, because in case the validation fails, the
2344 * entire image slot will be erased (including the trailer).
2345 */
2346 rc = boot_write_copy_done(fap);
2347 if (rc != 0) {
2348 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002349 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002350 "primary" : "secondary");
2351 rc = 0;
2352 }
2353 }
2354 flash_area_close(fap);
2355 }
2356
2357 return rc;
2358}
David Vincze1c456242021-06-29 15:25:24 +02002359#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002360
Tamas Banfe031092020-09-10 17:32:39 +02002361#ifdef MCUBOOT_RAM_LOAD
2362
Mark Horvathccaf7f82021-01-04 18:16:42 +01002363#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002364#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2365#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2366#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002367#endif
Tamas Banfe031092020-09-10 17:32:39 +02002368
2369/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002370 * Verifies that the active slot of the current image can be loaded within the
2371 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002372 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002373 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002374 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002375 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002376 */
2377static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002378boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002379{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002380 uint32_t img_dst;
2381 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002382 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002383 uint32_t exec_ram_start;
2384 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002385
2386 (void)state;
2387
Mark Horvathccaf7f82021-01-04 18:16:42 +01002388#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2389 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002390
Mark Horvathccaf7f82021-01-04 18:16:42 +01002391 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2392 &exec_ram_size);
2393 if (rc != 0) {
2394 return BOOT_EBADSTATUS;
2395 }
2396#else
2397 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2398 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2399#endif
2400
Raef Colesfe57e7d2021-10-15 11:07:09 +01002401 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2402 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002403
2404 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002405 return BOOT_EBADIMAGE;
2406 }
2407
2408 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2409 return BOOT_EBADIMAGE;
2410 }
2411
Mark Horvathccaf7f82021-01-04 18:16:42 +01002412 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002413 return BOOT_EBADIMAGE;
2414 }
2415
2416 return 0;
2417}
2418
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002419#ifdef MCUBOOT_ENC_IMAGES
2420
2421/**
2422 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2423 *
2424 * @param state Boot loader status information.
2425 * @param slot The flash slot of the image to be copied to SRAM.
2426 * @param hdr The image header.
2427 * @param src_sz Size of the image.
2428 * @param img_dst Pointer to the address at which the image needs to be
2429 * copied to SRAM.
2430 *
2431 * @return 0 on success; nonzero on failure.
2432 */
2433static int
2434boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2435 uint32_t slot, struct image_header *hdr,
2436 uint32_t src_sz, uint32_t img_dst)
2437{
2438 /* The flow for the decryption and copy of the image is as follows :
2439 * 1. The whole image is copied to the RAM (header + payload + TLV).
2440 * 2. The encryption key is loaded from the TLV in flash.
2441 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2442 * is 1024 bytes). Only the payload section is decrypted.
2443 * 4. The image is authenticated in RAM.
2444 */
2445 const struct flash_area *fap_src = NULL;
2446 struct boot_status bs;
2447 uint32_t blk_off;
2448 uint32_t tlv_off;
2449 uint32_t blk_sz;
2450 uint32_t bytes_copied = hdr->ih_hdr_size;
2451 uint32_t chunk_sz;
2452 uint32_t max_sz = 1024;
2453 uint16_t idx;
2454 uint8_t image_index;
2455 uint8_t * cur_dst;
2456 int area_id;
2457 int rc;
2458 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2459
2460 image_index = BOOT_CURR_IMG(state);
2461 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2462 rc = flash_area_open(area_id, &fap_src);
2463 if (rc != 0){
2464 return BOOT_EFLASH;
2465 }
2466
2467 tlv_off = BOOT_TLV_OFF(hdr);
2468
2469 /* Copying the whole image in RAM */
2470 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2471 if (rc != 0) {
2472 goto done;
2473 }
2474
2475 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2476 if (rc < 0) {
2477 goto done;
2478 }
2479
2480 /* if rc > 0 then the key has already been loaded */
2481 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2482 goto done;
2483 }
2484
2485 /* Starting at the end of the header as the header section is not encrypted */
2486 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2487 if (src_sz - bytes_copied > max_sz) {
2488 chunk_sz = max_sz;
2489 } else {
2490 chunk_sz = src_sz - bytes_copied;
2491 }
2492
2493 cur_dst = ram_dst + bytes_copied;
2494 blk_sz = chunk_sz;
2495 idx = 0;
2496 if (bytes_copied + chunk_sz > tlv_off) {
2497 /* Going over TLV section
2498 * Part of the chunk is encrypted payload */
2499 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2500 blk_sz = tlv_off - (bytes_copied);
2501 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2502 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2503 blk_off, cur_dst);
2504 } else {
2505 /* Image encrypted payload section */
2506 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2507 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2508 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2509 blk_off, cur_dst);
2510 }
2511
2512 bytes_copied += chunk_sz;
2513 }
2514 rc = 0;
2515
2516done:
2517 flash_area_close(fap_src);
2518
2519 return rc;
2520}
2521
2522#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002523/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002524 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002525 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002526 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002527 * @param slot The flash slot of the image to be copied to SRAM.
2528 * @param img_dst The address at which the image needs to be copied to
2529 * SRAM.
2530 * @param img_sz The size of the image that needs to be copied to SRAM.
2531 *
2532 * @return 0 on success; nonzero on failure.
2533 */
2534static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002535boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2536 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002537{
2538 int rc;
2539 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002540 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002541
Mark Horvathccaf7f82021-01-04 18:16:42 +01002542#if (BOOT_IMAGE_NUMBER == 1)
2543 (void)state;
2544#endif
2545
2546 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2547
2548 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002549 if (rc != 0) {
2550 return BOOT_EFLASH;
2551 }
2552
2553 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002554 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002555 if (rc != 0) {
2556 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2557 }
2558
2559 flash_area_close(fap_src);
2560
2561 return rc;
2562}
2563
Mark Horvathccaf7f82021-01-04 18:16:42 +01002564#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002565/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002566 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002567 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002568 * @param start_a Start of the A region.
2569 * @param end_a End of the A region.
2570 * @param start_b Start of the B region.
2571 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002572 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002573 * @return true if there is overlap; false otherwise.
2574 */
2575static bool
2576do_regions_overlap(uint32_t start_a, uint32_t end_a,
2577 uint32_t start_b, uint32_t end_b)
2578{
2579 if (start_b > end_a) {
2580 return false;
2581 } else if (start_b >= start_a) {
2582 return true;
2583 } else if (end_b > start_a) {
2584 return true;
2585 }
2586
2587 return false;
2588}
2589
2590/**
2591 * Checks if the image we want to load to memory overlap with an already
2592 * ramloaded image.
2593 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002594 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002595 *
2596 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002597 */
2598static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002599boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002600{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002601 uint32_t i;
2602
2603 uint32_t start_a;
2604 uint32_t end_a;
2605 uint32_t start_b;
2606 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002607 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002608
Raef Colesfe57e7d2021-10-15 11:07:09 +01002609 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002610 /* Safe to add here, values are already verified in
2611 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002612 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002613
2614 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002615 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002616 || i == image_id_to_check) {
2617 continue;
2618 }
2619
Raef Colesfe57e7d2021-10-15 11:07:09 +01002620 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002621 /* Safe to add here, values are already verified in
2622 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002623 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002624
2625 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2626 return -1;
2627 }
2628 }
2629
2630 return 0;
2631}
2632#endif
2633
2634/**
2635 * Loads the active slot of the current image into SRAM. The load address and
2636 * image size is extracted from the image header.
2637 *
2638 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002639 *
2640 * @return 0 on success; nonzero on failure.
2641 */
2642static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002643boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002644{
2645 uint32_t active_slot;
2646 struct image_header *hdr = NULL;
2647 uint32_t img_dst;
2648 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002649 int rc;
2650
Raef Colesfe57e7d2021-10-15 11:07:09 +01002651 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002652 hdr = boot_img_hdr(state, active_slot);
2653
Tamas Banfe031092020-09-10 17:32:39 +02002654 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2655
Mark Horvathccaf7f82021-01-04 18:16:42 +01002656 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002657
Mark Horvathccaf7f82021-01-04 18:16:42 +01002658 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002659 if (rc != 0) {
2660 return rc;
2661 }
2662
Raef Colesfe57e7d2021-10-15 11:07:09 +01002663 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2664 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002665
Raef Colesfe57e7d2021-10-15 11:07:09 +01002666 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002667 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002668 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002669 return rc;
2670 }
2671
Mark Horvathccaf7f82021-01-04 18:16:42 +01002672#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002673 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002674 if (rc != 0) {
2675 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2676 another image.", img_dst);
2677 return rc;
2678 }
2679#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002680#ifdef MCUBOOT_ENC_IMAGES
2681 /* decrypt image if encrypted and copy it to RAM */
2682 if (IS_ENCRYPTED(hdr)) {
2683 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2684 } else {
2685 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2686 }
2687#else
Tamas Banfe031092020-09-10 17:32:39 +02002688 /* Copy image to the load address from where it currently resides in
2689 * flash.
2690 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002691 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002692#endif
Tamas Banfe031092020-09-10 17:32:39 +02002693 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002694 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002695 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002696 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002697 }
2698 } else {
2699 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2700 * MCUBOOT_RAM_LOAD is set.
2701 */
2702 rc = BOOT_EBADIMAGE;
2703 }
2704
Mark Horvathccaf7f82021-01-04 18:16:42 +01002705 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002706 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2707 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002708 }
2709
Tamas Banfe031092020-09-10 17:32:39 +02002710 return rc;
2711}
2712
2713/**
2714 * Removes an image from SRAM, by overwriting it with zeros.
2715 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002716 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002717 *
2718 * @return 0 on success; nonzero on failure.
2719 */
2720static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002721boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002722{
David Brown695e5912021-05-24 16:58:01 -06002723 (void)state;
2724
Mark Horvathccaf7f82021-01-04 18:16:42 +01002725 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
Raef Colesfe57e7d2021-10-15 11:07:09 +01002726 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002727
Raef Colesfe57e7d2021-10-15 11:07:09 +01002728 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2729 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002730
Raef Colesfe57e7d2021-10-15 11:07:09 +01002731 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2732 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002733
2734 return 0;
2735}
2736
2737/**
2738 * Removes an image from flash by erasing the corresponding flash area
2739 *
2740 * @param state Boot loader status information.
2741 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002742 *
2743 * @return 0 on success; nonzero on failure.
2744 */
2745static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002746boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002747{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002748 int area_id;
2749 int rc;
2750 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002751
David Brown695e5912021-05-24 16:58:01 -06002752 (void)state;
2753
Mark Horvathccaf7f82021-01-04 18:16:42 +01002754 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2755 slot);
2756 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2757 rc = flash_area_open(area_id, &fap);
2758 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002759 flash_area_erase(fap, 0, flash_area_get_size(fap));
Mark Horvathccaf7f82021-01-04 18:16:42 +01002760 }
2761
2762 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002763}
2764#endif /* MCUBOOT_RAM_LOAD */
2765
Mark Horvathccaf7f82021-01-04 18:16:42 +01002766#if (BOOT_IMAGE_NUMBER > 1)
2767/**
2768 * Checks the image dependency whether it is satisfied.
2769 *
2770 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002771 * @param dep Image dependency which has to be verified.
2772 *
2773 * @return 0 if dependencies are met; nonzero otherwise.
2774 */
2775static int
2776boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002777 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002778{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002779 struct image_version *dep_version;
2780 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002781 int rc;
2782
Mark Horvathccaf7f82021-01-04 18:16:42 +01002783 /* Determine the source of the image which is the subject of
2784 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002785 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002786 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002787 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2788
2789 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2790 if (rc >= 0) {
2791 /* Dependency satisfied. */
2792 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002793 }
2794
Mark Horvathccaf7f82021-01-04 18:16:42 +01002795 return rc;
2796}
2797
2798/**
2799 * Reads all dependency TLVs of an image and verifies one after another to see
2800 * if they are all satisfied.
2801 *
2802 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002803 *
2804 * @return 0 if dependencies are met; nonzero otherwise.
2805 */
2806static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002807boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002808{
2809 uint32_t active_slot;
2810 const struct flash_area *fap;
2811 struct image_tlv_iter it;
2812 struct image_dependency dep;
2813 uint32_t off;
2814 uint16_t len;
2815 int area_id;
2816 int rc;
2817
Raef Colesfe57e7d2021-10-15 11:07:09 +01002818 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002819
2820 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2821 active_slot);
2822 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002823 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002824 rc = BOOT_EFLASH;
2825 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002826 }
2827
Mark Horvathccaf7f82021-01-04 18:16:42 +01002828 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2829 IMAGE_TLV_DEPENDENCY, true);
2830 if (rc != 0) {
2831 goto done;
2832 }
David Vinczee574f2d2020-07-10 11:42:03 +02002833
Mark Horvathccaf7f82021-01-04 18:16:42 +01002834 while (true) {
2835 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2836 if (rc < 0) {
2837 return -1;
2838 } else if (rc > 0) {
2839 rc = 0;
2840 break;
2841 }
David Vinczee574f2d2020-07-10 11:42:03 +02002842
Mark Horvathccaf7f82021-01-04 18:16:42 +01002843 if (len != sizeof(dep)) {
2844 rc = BOOT_EBADIMAGE;
2845 goto done;
2846 }
2847
2848 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2849 fap, off, &dep, len);
2850 if (rc != 0) {
2851 rc = BOOT_EFLASH;
2852 goto done;
2853 }
2854
2855 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2856 rc = BOOT_EBADARGS;
2857 goto done;
2858 }
2859
Raef Colesfe57e7d2021-10-15 11:07:09 +01002860 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002861 if (rc != 0) {
2862 /* Dependency not satisfied. */
2863 goto done;
2864 }
2865 }
2866
2867done:
2868 flash_area_close(fap);
2869 return rc;
2870}
2871
2872/**
2873 * Checks the dependency of all the active slots. If an image found with
2874 * invalid or not satisfied dependencies the image is removed from SRAM (in
2875 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2876 *
2877 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002878 *
2879 * @return 0 if dependencies are met; nonzero otherwise.
2880 */
2881static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002882boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002883{
2884 int rc = -1;
2885 uint32_t active_slot;
2886
2887 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002888 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2889 continue;
2890 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01002891 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002892 if (rc != 0) {
2893 /* Dependencies not met or invalid dependencies. */
2894
2895#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01002896 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002897#endif /* MCUBOOT_RAM_LOAD */
2898
Raef Colesfe57e7d2021-10-15 11:07:09 +01002899 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
2900 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2901 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002902
2903 return rc;
2904 }
2905 }
2906
2907 return rc;
2908}
2909#endif /* (BOOT_IMAGE_NUMBER > 1) */
2910
2911/**
2912 * Tries to load a slot for all the images with validation.
2913 *
2914 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002915 *
2916 * @return 0 on success; nonzero on failure.
2917 */
2918fih_int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002919boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002920{
2921 uint32_t active_slot;
2922 int rc;
2923 fih_int fih_rc;
2924
2925 /* Go over all the images and try to load one */
2926 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2927 /* All slots tried until a valid image found. Breaking from this loop
2928 * means that a valid image found or already loaded. If no slot is
2929 * found the function returns with error code. */
2930 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002931 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002932 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002933 if (active_slot != NO_ACTIVE_SLOT){
2934 /* A slot is already active, go to next image. */
2935 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002936 }
David Vincze505fba22020-10-22 13:53:29 +02002937
Raef Colesfe57e7d2021-10-15 11:07:09 +01002938 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002939 if (active_slot == NO_ACTIVE_SLOT) {
2940 BOOT_LOG_INF("No slot to load for image %d",
2941 BOOT_CURR_IMG(state));
2942 FIH_RET(FIH_FAILURE);
2943 }
2944
2945 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002946 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002947
Raef Colesf11de642021-10-15 11:11:56 +01002948#if BOOT_IMAGE_NUMBER > 1
2949 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2950 continue;
2951 }
2952#endif
2953
Mark Horvathccaf7f82021-01-04 18:16:42 +01002954#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01002955 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002956 if (rc != 0) {
2957 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002958 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2959 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002960 continue;
2961 }
George Becksteind4d90f82021-05-11 02:00:00 -04002962
David Vincze505fba22020-10-22 13:53:29 +02002963#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01002964 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02002965 if (rc != 0) {
2966 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002967 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2968 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02002969 continue;
2970 }
2971#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02002972#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02002973
Tamas Banfe031092020-09-10 17:32:39 +02002974#ifdef MCUBOOT_RAM_LOAD
2975 /* Image is first loaded to RAM and authenticated there in order to
2976 * prevent TOCTOU attack during image copy. This could be applied
2977 * when loading images from external (untrusted) flash to internal
2978 * (trusted) RAM and image is authenticated before copying.
2979 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002980 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02002981 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002982 /* Image cannot be ramloaded. */
2983 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01002984 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2985 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02002986 continue;
Tamas Banfe031092020-09-10 17:32:39 +02002987 }
2988#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002989
2990 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
2991 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2992 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02002993#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01002994 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02002995#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002996 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2997 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002998 continue;
David Vincze505fba22020-10-22 13:53:29 +02002999 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003000
3001 /* Valid image loaded from a slot, go to next image. */
3002 break;
3003 }
3004 }
3005
3006 FIH_RET(FIH_SUCCESS);
3007}
3008
3009/**
3010 * Updates the security counter for the current image.
3011 *
3012 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003013 *
3014 * @return 0 on success; nonzero on failure.
3015 */
3016static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003017boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003018{
3019#ifdef MCUBOOT_HW_ROLLBACK_PROT
3020 int rc;
3021
3022 /* Update the stored security counter with the newer (active) image's
3023 * security counter value.
3024 */
David Vincze1c456242021-06-29 15:25:24 +02003025#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003026 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3027 * security counter can be increased only after reboot, if the image
3028 * has been confirmed at runtime (the image_ok flag has been set).
3029 * This way a 'revert' can be performed when it's necessary.
3030 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003031 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003032#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003033 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003034 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3035 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003036 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003037 BOOT_LOG_ERR("Security counter update failed after image "
3038 "validation.");
3039 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003040 }
David Vincze1c456242021-06-29 15:25:24 +02003041#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003042 }
3043#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003044
Mark Horvathccaf7f82021-01-04 18:16:42 +01003045 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003046
Mark Horvathccaf7f82021-01-04 18:16:42 +01003047#else /* MCUBOOT_HW_ROLLBACK_PROT */
3048 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003049 return 0;
3050#endif
3051}
3052
3053fih_int
3054context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3055{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003056 int rc;
David Brown695e5912021-05-24 16:58:01 -06003057 fih_int fih_rc = fih_int_encode(0);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003058
Raef Colesfe57e7d2021-10-15 11:07:09 +01003059 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003060 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003061 goto out;
3062 }
3063
Mark Horvathccaf7f82021-01-04 18:16:42 +01003064#if (BOOT_IMAGE_NUMBER > 1)
3065 while (true) {
3066#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003067 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003068 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3069 goto out;
3070 }
3071
3072#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003073 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003074 if (rc != 0) {
3075 /* Dependency check failed for an image, it has been removed from
3076 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3077 * unavailable. Try to load an image from another slot.
3078 */
3079 continue;
3080 }
3081 /* Dependency check was successful. */
3082 break;
3083 }
3084#endif
3085
3086 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003087#if BOOT_IMAGE_NUMBER > 1
3088 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3089 continue;
3090 }
3091#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003092 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003093 if (rc != 0) {
3094 goto out;
3095 }
3096
Raef Colesfe57e7d2021-10-15 11:07:09 +01003097 rc = boot_add_shared_data(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003098 if (rc != 0) {
3099 goto out;
3100 }
3101 }
3102
3103 /* All image loaded successfully. */
3104#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003105 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003106#endif
3107
Raef Colesfe57e7d2021-10-15 11:07:09 +01003108 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003109
David Vinczee574f2d2020-07-10 11:42:03 +02003110out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003111 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003112
Mark Horvathccaf7f82021-01-04 18:16:42 +01003113 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3114 fih_rc = fih_int_encode(rc);
3115 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003116
Mark Horvathccaf7f82021-01-04 18:16:42 +01003117 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003118}
Tamas Banfe031092020-09-10 17:32:39 +02003119#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003120
3121/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003122 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003123 * appropriate, and tells you what address to boot from.
3124 *
3125 * @param rsp On success, indicates how booting should occur.
3126 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003127 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003128 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003129fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003130boot_go(struct boot_rsp *rsp)
3131{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003132 fih_int fih_rc = FIH_FAILURE;
Raef Colesf11de642021-10-15 11:11:56 +01003133
3134 boot_state_clear(NULL);
3135
Raef Colese8fe6cf2020-05-26 13:07:40 +01003136 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3137 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003138}
Raef Colesf11de642021-10-15 11:11:56 +01003139
3140/**
3141 * Prepares the booting process, considering only a single image. This function
3142 * moves images around in flash as appropriate, and tells you what address to
3143 * boot from.
3144 *
3145 * @param rsp On success, indicates how booting should occur.
3146 *
3147 * @param image_id The image ID to prepare the boot process for.
3148 *
3149 * @return FIH_SUCCESS on success; nonzero on failure.
3150 */
3151fih_int
3152boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3153{
3154 fih_int fih_rc = FIH_FAILURE;
3155
3156 if (image_id >= BOOT_IMAGE_NUMBER) {
3157 FIH_RET(FIH_FAILURE);
3158 }
3159
3160#if BOOT_IMAGE_NUMBER > 1
3161 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3162 boot_data.img_mask[image_id] = 0;
3163#endif
3164
3165 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3166 FIH_RET(fih_rc);
3167}
3168
3169/**
3170 * Clears the boot state, so that previous operations have no effect on new
3171 * ones.
3172 *
3173 * @param state The state that should be cleared. If the value
3174 * is NULL, the default bootloader state will be
3175 * cleared.
3176 */
3177void boot_state_clear(struct boot_loader_state *state)
3178{
3179 if (state != NULL) {
3180 memset(state, 0, sizeof(struct boot_loader_state));
3181 } else {
3182 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3183 }
3184}