blob: b1a45898cda3eba5285ae41841aad89bbd023feb [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
Mark Horvathccaf7f82021-01-04 18:16:42 +010069#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
70struct slot_usage_t {
71 /* Index of the slot chosen to be loaded */
72 uint32_t active_slot;
73 bool slot_available[BOOT_NUM_SLOTS];
David Vincze1c456242021-06-29 15:25:24 +020074#if defined(MCUBOOT_RAM_LOAD)
Mark Horvathccaf7f82021-01-04 18:16:42 +010075 /* Image destination and size for the active slot */
76 uint32_t img_dst;
77 uint32_t img_sz;
David Vincze1c456242021-06-29 15:25:24 +020078#elif defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +010079 /* Swap status for the active slot */
Carlos Falgueras Garcíaafb424d2021-06-21 17:05:44 +020080 struct boot_swap_state swap_state;
Mark Horvathccaf7f82021-01-04 18:16:42 +010081#endif
David Vincze1c456242021-06-29 15:25:24 +020082};
83#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +010084
Fabio Utzig10ee6482019-08-01 12:04:52 -030085/*
86 * This macro allows some control on the allocation of local variables.
87 * When running natively on a target, we don't want to allocated huge
88 * variables on the stack, so make them global instead. For the simulator
89 * we want to run as many threads as there are tests, and it's safer
90 * to just make those variables stack allocated.
91 */
92#if !defined(__BOOTSIM__)
93#define TARGET_STATIC static
94#else
95#define TARGET_STATIC
96#endif
97
David Vinczee574f2d2020-07-10 11:42:03 +020098static int
99boot_read_image_headers(struct boot_loader_state *state, bool require_all,
100 struct boot_status *bs)
101{
102 int rc;
103 int i;
104
105 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200106 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
107 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
108 if (rc == BOOT_HOOK_REGULAR)
109 {
110 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
111 }
David Vinczee574f2d2020-07-10 11:42:03 +0200112 if (rc != 0) {
113 /* If `require_all` is set, fail on any single fail, otherwise
114 * if at least the first slot's header was read successfully,
115 * then the boot loader can attempt a boot.
116 *
117 * Failure to read any headers is a fatal error.
118 */
119 if (i > 0 && !require_all) {
120 return 0;
121 } else {
122 return rc;
123 }
124 }
125 }
126
127 return 0;
128}
129
Mark Horvathccaf7f82021-01-04 18:16:42 +0100130/**
131 * Saves boot status and shared data for current image.
132 *
133 * @param state Boot loader status information.
134 * @param active_slot Index of the slot will be loaded for current image.
135 *
136 * @return 0 on success; nonzero on failure.
137 */
138static int
139boot_add_shared_data(struct boot_loader_state *state,
140 uint32_t active_slot)
141{
142#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
143 int rc;
144
145#ifdef MCUBOOT_MEASURED_BOOT
146 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
147 boot_img_hdr(state, active_slot),
148 BOOT_IMG_AREA(state, active_slot));
149 if (rc != 0) {
150 BOOT_LOG_ERR("Failed to add image data to shared area");
151 return rc;
152 }
153#endif /* MCUBOOT_MEASURED_BOOT */
154
155#ifdef MCUBOOT_DATA_SHARING
156 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
157 BOOT_IMG_AREA(state, active_slot));
158 if (rc != 0) {
159 BOOT_LOG_ERR("Failed to add data to shared memory area.");
160 return rc;
161 }
162#endif /* MCUBOOT_DATA_SHARING */
163
164 return 0;
165
166#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
167 (void) (state);
168 (void) (active_slot);
169
170 return 0;
171#endif
172}
173
174/**
175 * Fills rsp to indicate how booting should occur.
176 *
177 * @param state Boot loader status information.
178 * @param slot_usage Information about the active and available slots.
179 * Only used in MCUBOOT_DIRECT_XIP and MCUBOOT_RAM_LOAD
180 * @param rsp boot_rsp struct to fill.
181 */
182static void
183fill_rsp(struct boot_loader_state *state, void *slot_usage,
184 struct boot_rsp *rsp)
185{
186 uint32_t active_slot;
187
188#if (BOOT_IMAGE_NUMBER > 1)
189 /* Always boot from Image 0. */
190 BOOT_CURR_IMG(state) = 0;
191#endif
192
193#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
194 active_slot = ((struct slot_usage_t *)slot_usage)[BOOT_CURR_IMG(state)].active_slot;
195#else
196 (void) (slot_usage);
197 active_slot = BOOT_PRIMARY_SLOT;
198#endif
199
Dominik Ermel260ae092021-04-23 05:38:45 +0000200 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100201 rsp->br_image_off = boot_img_slot_off(state, active_slot);
202 rsp->br_hdr = boot_img_hdr(state, active_slot);
203}
204
205/**
206 * Closes all flash areas.
207 *
208 * @param state Boot loader status information.
209 */
210static void
211close_all_flash_areas(struct boot_loader_state *state)
212{
213 uint32_t slot;
214
215 IMAGES_ITER(BOOT_CURR_IMG(state)) {
216#if MCUBOOT_SWAP_USING_SCRATCH
217 flash_area_close(BOOT_SCRATCH_AREA(state));
218#endif
219 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
220 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
221 }
222 }
223}
224
Tamas Banfe031092020-09-10 17:32:39 +0200225#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600226/*
227 * Compute the total size of the given image. Includes the size of
228 * the TLVs.
229 */
Tamas Banfe031092020-09-10 17:32:39 +0200230#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600231static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300232boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600233{
234 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300235 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300236 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300237 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600238 int area_id;
239 int rc;
240
Fabio Utzig10ee6482019-08-01 12:04:52 -0300241#if (BOOT_IMAGE_NUMBER == 1)
242 (void)state;
243#endif
244
245 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600246 rc = flash_area_open(area_id, &fap);
247 if (rc != 0) {
248 rc = BOOT_EFLASH;
249 goto done;
250 }
251
Fabio Utzig61fd8882019-09-14 20:00:20 -0300252 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
253
254 if (flash_area_read(fap, off, &info, sizeof(info))) {
255 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600256 goto done;
257 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300258
Fabio Utzige52c08e2019-09-11 19:32:00 -0300259 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
260 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
261 if (protect_tlv_size != info.it_tlv_tot) {
262 rc = BOOT_EBADIMAGE;
263 goto done;
264 }
265
266 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
267 rc = BOOT_EFLASH;
268 goto done;
269 }
270 } else if (protect_tlv_size != 0) {
271 rc = BOOT_EBADIMAGE;
272 goto done;
273 }
274
Fabio Utzig61fd8882019-09-14 20:00:20 -0300275 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
276 rc = BOOT_EBADIMAGE;
277 goto done;
278 }
279
Fabio Utzige52c08e2019-09-11 19:32:00 -0300280 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600281 rc = 0;
282
283done:
284 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300285 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600286}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300287#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600288
Tamas Banfe031092020-09-10 17:32:39 +0200289#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700290static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300291boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292{
David Brownab449182019-11-15 09:32:52 -0700293 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300294#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700295 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300296#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297
298 /* Figure out what size to write update status update as. The size depends
299 * on what the minimum write size is for scratch area, active image slot.
300 * We need to use the bigger of those 2 values.
301 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300302 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300303#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300304 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 if (align > elem_sz) {
306 elem_sz = align;
307 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300308#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309
310 return elem_sz;
311}
312
Fabio Utzig10ee6482019-08-01 12:04:52 -0300313static int
314boot_initialize_area(struct boot_loader_state *state, int flash_area)
315{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000316 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
317 boot_sector_t *out_sectors;
318 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300319 int rc;
320
321 num_sectors = BOOT_MAX_IMG_SECTORS;
322
323 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
324 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
325 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
326 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
327 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
328 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300329#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300330 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
331 out_sectors = state->scratch.sectors;
332 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300333#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300334 } else {
335 return BOOT_EFLASH;
336 }
337
Dominik Ermel51c8d762021-05-24 15:34:01 +0000338#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300339 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000340#else
341 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
342 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
343#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300344 if (rc != 0) {
345 return rc;
346 }
347 *out_num_sectors = num_sectors;
348 return 0;
349}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300350
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351/**
352 * Determines the sector layout of both image slots and the scratch area.
353 * This information is necessary for calculating the number of bytes to erase
354 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300355 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800356 */
357static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300358boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300360 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800362
Fabio Utzig10ee6482019-08-01 12:04:52 -0300363 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300364
365 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 if (rc != 0) {
367 return BOOT_EFLASH;
368 }
369
Fabio Utzig10ee6482019-08-01 12:04:52 -0300370 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200372 /* We need to differentiate from the primary image issue */
373 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374 }
375
Fabio Utzig12d59162019-11-28 10:01:59 -0300376#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300377 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200378 if (rc != 0) {
379 return BOOT_EFLASH;
380 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300381#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200382
Fabio Utzig10ee6482019-08-01 12:04:52 -0300383 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384
385 return 0;
386}
387
Fabio Utzig12d59162019-11-28 10:01:59 -0300388void
389boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390{
Fabio Utzig4741c452019-12-19 15:32:41 -0300391#ifdef MCUBOOT_ENC_IMAGES
392 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
393#if MCUBOOT_SWAP_SAVE_ENCTLV
394 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
395#endif
396#endif /* MCUBOOT_ENC_IMAGES */
397
398 bs->use_scratch = 0;
399 bs->swap_size = 0;
400 bs->source = 0;
401
Fabio Utzig74aef312019-11-28 11:05:34 -0300402 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300403 bs->idx = BOOT_STATUS_IDX_0;
404 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700405 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300406}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407
Fabio Utzig12d59162019-11-28 10:01:59 -0300408bool
409boot_status_is_reset(const struct boot_status *bs)
410{
Fabio Utzig74aef312019-11-28 11:05:34 -0300411 return (bs->op == BOOT_STATUS_OP_MOVE &&
412 bs->idx == BOOT_STATUS_IDX_0 &&
413 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414}
415
416/**
417 * Writes the supplied boot status to the flash file system. The boot status
418 * contains the current state of an in-progress image copy operation.
419 *
420 * @param bs The boot status to write.
421 *
422 * @return 0 on success; nonzero on failure.
423 */
424int
Fabio Utzig12d59162019-11-28 10:01:59 -0300425boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800426{
427 const struct flash_area *fap;
428 uint32_t off;
429 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000430 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300431 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700432 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300433 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300435 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100436 * the trailer. Since in the last step the primary slot is erased, the
437 * first two status writes go to the scratch which will be copied to
438 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300439 */
440
Fabio Utzig12d59162019-11-28 10:01:59 -0300441#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300442 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443 /* Write to scratch. */
444 area_id = FLASH_AREA_IMAGE_SCRATCH;
445 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300446#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100447 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300448 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300449#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800450 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300451#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452
453 rc = flash_area_open(area_id, &fap);
454 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000455 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456 }
457
458 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300459 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200460 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300461 erased_val = flash_area_erased_val(fap);
462 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700463 buf[0] = bs->state;
464
465 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800466 if (rc != 0) {
467 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 }
469
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000471
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472 return rc;
473}
Tamas Banfe031092020-09-10 17:32:39 +0200474#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200475#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476
477/*
David Vinczec3084132020-02-18 14:50:47 +0100478 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800479 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100480static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300481boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
482 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300484 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300485 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300486 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100487 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300488
Fabio Utzig10ee6482019-08-01 12:04:52 -0300489#if (BOOT_IMAGE_NUMBER == 1)
490 (void)state;
491#endif
492
Fabio Utzigba829042018-09-18 08:29:34 -0300493 (void)bs;
494 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300495
496 image_index = BOOT_CURR_IMG(state);
497
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000498/* In the case of ram loading the image has already been decrypted as it is
499 * decrypted when copied in ram */
500#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300501 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300502 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300503 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100504 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300505 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300506 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100507 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300508 }
509 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300510#endif
511
Raef Colese8fe6cf2020-05-26 13:07:40 +0100512 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
513 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300514
Raef Colese8fe6cf2020-05-26 13:07:40 +0100515 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516}
517
Tamas Banfe031092020-09-10 17:32:39 +0200518#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100519static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520split_image_check(struct image_header *app_hdr,
521 const struct flash_area *app_fap,
522 struct image_header *loader_hdr,
523 const struct flash_area *loader_fap)
524{
525 static void *tmpbuf;
526 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100527 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800528
529 if (!tmpbuf) {
530 tmpbuf = malloc(BOOT_TMPBUF_SZ);
531 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100532 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533 }
534 }
535
Raef Colese8fe6cf2020-05-26 13:07:40 +0100536 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
537 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
538 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
539 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540 }
541
Raef Colese8fe6cf2020-05-26 13:07:40 +0100542 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
543 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800544
Raef Colese8fe6cf2020-05-26 13:07:40 +0100545out:
546 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800547}
Tamas Banfe031092020-09-10 17:32:39 +0200548#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800549
Fabio Utzig338a19f2018-12-03 08:37:08 -0200550/*
David Brown9bf95af2019-10-10 15:36:36 -0600551 * Check that this is a valid header. Valid means that the magic is
552 * correct, and that the sizes/offsets are "sane". Sane means that
553 * there is no overflow on the arithmetic, and that the result fits
554 * within the flash area we are in.
555 */
556static bool
557boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
558{
559 uint32_t size;
560
561 if (hdr->ih_magic != IMAGE_MAGIC) {
562 return false;
563 }
564
565 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
566 return false;
567 }
568
Dominik Ermel260ae092021-04-23 05:38:45 +0000569 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600570 return false;
571 }
572
573 return true;
574}
575
576/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200577 * Check that a memory area consists of a given value.
578 */
579static inline bool
580boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300581{
582 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200583 uint8_t *p = (uint8_t *)data;
584 for (i = 0; i < len; i++) {
585 if (val != p[i]) {
586 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300587 }
588 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200589 return true;
590}
591
592static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300593boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200594{
595 const struct flash_area *fap;
596 struct image_header *hdr;
597 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300598 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200599 int rc;
600
Fabio Utzig10ee6482019-08-01 12:04:52 -0300601 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300602 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200603 if (rc != 0) {
604 return -1;
605 }
606
607 erased_val = flash_area_erased_val(fap);
608 flash_area_close(fap);
609
Fabio Utzig10ee6482019-08-01 12:04:52 -0300610 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200611 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
612 return -1;
613 }
614
615 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300616}
617
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000618#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200619 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200620 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000621 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
622/**
David Vincze8b0b6372020-05-20 19:54:44 +0200623 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000624 *
David Vincze8b0b6372020-05-20 19:54:44 +0200625 * @param ver1 Pointer to the first image version to compare.
626 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000627 *
David Vincze8b0b6372020-05-20 19:54:44 +0200628 * @retval -1 If ver1 is strictly less than ver2.
629 * @retval 0 If the image version numbers are equal,
630 * (not including the build number).
631 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000632 */
633static int
David Vincze8b0b6372020-05-20 19:54:44 +0200634boot_version_cmp(const struct image_version *ver1,
635 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000636{
David Vincze8b0b6372020-05-20 19:54:44 +0200637 if (ver1->iv_major > ver2->iv_major) {
638 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000639 }
David Vincze8b0b6372020-05-20 19:54:44 +0200640 if (ver1->iv_major < ver2->iv_major) {
641 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000642 }
David Vincze8b0b6372020-05-20 19:54:44 +0200643 /* The major version numbers are equal, continue comparison. */
644 if (ver1->iv_minor > ver2->iv_minor) {
645 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000646 }
David Vincze8b0b6372020-05-20 19:54:44 +0200647 if (ver1->iv_minor < ver2->iv_minor) {
648 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000649 }
David Vincze8b0b6372020-05-20 19:54:44 +0200650 /* The minor version numbers are equal, continue comparison. */
651 if (ver1->iv_revision > ver2->iv_revision) {
652 return 1;
653 }
654 if (ver1->iv_revision < ver2->iv_revision) {
655 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000656 }
657
658 return 0;
659}
660#endif
661
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000662#if defined(MCUBOOT_DIRECT_XIP)
663/**
664 * Check if image in slot has been set with specific ROM address to run from
665 * and whether the slot starts at that address.
666 *
667 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
668 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
669 * header matches the slot address;
670 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
671 * does not match the slot address.
672 */
673static bool
Mark Horvathccaf7f82021-01-04 18:16:42 +0100674boot_rom_address_check(struct boot_loader_state *state,
675 struct slot_usage_t slot_usage[])
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000676{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100677 uint32_t active_slot;
678 const struct image_header *hdr;
679 uint32_t f_off;
680
681 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
682 hdr = boot_img_hdr(state, active_slot);
683 f_off = boot_img_slot_off(state, active_slot);
684
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000685 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
686 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100687 ", skipping",
688 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000689 hdr->ih_load_addr);
690
691 /* If there is address mismatch, the image is not bootable from this
692 * slot.
693 */
694 return 1;
695 }
696 return 0;
697}
698#endif
699
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300700/*
701 * Check that there is a valid image in a slot
702 *
703 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100704 * FIH_SUCCESS if image was successfully validated
705 * 1 (or its fih_int encoded form) if no bootloable image was found
706 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300707 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100708static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300709boot_validate_slot(struct boot_loader_state *state, int slot,
710 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800711{
712 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400713 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300714 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100715 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800716 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300717
Fabio Utzig10ee6482019-08-01 12:04:52 -0300718 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300719 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800720 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100721 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800722 }
723
Fabio Utzig10ee6482019-08-01 12:04:52 -0300724 hdr = boot_img_hdr(state, slot);
725 if (boot_check_header_erased(state, slot) == 0 ||
726 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300727
728#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
729 /*
730 * This fixes an issue where an image might be erased, but a trailer
731 * be left behind. It can happen if the image is in the secondary slot
732 * and did not pass validation, in which case the whole slot is erased.
733 * If during the erase operation, a reset occurs, parts of the slot
734 * might have been erased while some did not. The concerning part is
735 * the trailer because it might disable a new image from being loaded
736 * through mcumgr; so we just get rid of the trailer here, if the header
737 * is erased.
738 */
739 if (slot != BOOT_PRIMARY_SLOT) {
740 swap_erase_trailer_sectors(state, fap);
741 }
742#endif
743
David Vincze2d736ad2019-02-18 11:50:22 +0100744 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100745 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200746 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300747 }
748
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000749#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
750 if (slot != BOOT_PRIMARY_SLOT) {
751 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200752 rc = boot_version_cmp(
753 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
754 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
755 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000756 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000757 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000758 /* Image in the secondary slot does not satisfy version requirement.
759 * Erase the image and continue booting from the primary slot.
760 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100761 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000762 goto out;
763 }
764 }
765#endif
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200766 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
767 fih_rc, BOOT_CURR_IMG(state), slot);
Andrzej Puzdrowski9d4d45c2021-09-14 17:25:58 +0200768 if (fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200769 {
770 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
771 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100772 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200773 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000774 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200775 /* Image is invalid, erase it to prevent further unnecessary
776 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700777 */
778 }
David Brown098de832019-12-10 11:58:01 -0700779#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100780 BOOT_LOG_ERR("Image in the %s slot is not valid!",
781 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700782#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100783 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200784 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800785 }
786
Fabio Utzig338a19f2018-12-03 08:37:08 -0200787out:
788 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100789
790 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800791}
792
David Vinczec3084132020-02-18 14:50:47 +0100793#ifdef MCUBOOT_HW_ROLLBACK_PROT
794/**
795 * Updates the stored security counter value with the image's security counter
796 * value which resides in the given slot, only if it's greater than the stored
797 * value.
798 *
799 * @param image_index Index of the image to determine which security
800 * counter to update.
801 * @param slot Slot number of the image.
802 * @param hdr Pointer to the image header structure of the image
803 * that is currently stored in the given slot.
804 *
805 * @return 0 on success; nonzero on failure.
806 */
807static int
808boot_update_security_counter(uint8_t image_index, int slot,
809 struct image_header *hdr)
810{
811 const struct flash_area *fap = NULL;
812 uint32_t img_security_cnt;
813 int rc;
814
815 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
816 &fap);
817 if (rc != 0) {
818 rc = BOOT_EFLASH;
819 goto done;
820 }
821
822 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
823 if (rc != 0) {
824 goto done;
825 }
826
827 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
828 if (rc != 0) {
829 goto done;
830 }
831
832done:
833 flash_area_close(fap);
834 return rc;
835}
836#endif /* MCUBOOT_HW_ROLLBACK_PROT */
837
Tamas Banfe031092020-09-10 17:32:39 +0200838#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200839/**
840 * Determines which swap operation to perform, if any. If it is determined
841 * that a swap operation is required, the image in the secondary slot is checked
842 * for validity. If the image in the secondary slot is invalid, it is erased,
843 * and a swap type of "none" is indicated.
844 *
845 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
846 */
847static int
848boot_validated_swap_type(struct boot_loader_state *state,
849 struct boot_status *bs)
850{
851 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100852 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200853
854 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
855 if (BOOT_IS_UPGRADE(swap_type)) {
856 /* Boot loader wants to switch to the secondary slot.
857 * Ensure image is valid.
858 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100859 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
860 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
861 if (fih_eq(fih_rc, fih_int_encode(1))) {
862 swap_type = BOOT_SWAP_TYPE_NONE;
863 } else {
864 swap_type = BOOT_SWAP_TYPE_FAIL;
865 }
David Vinczee574f2d2020-07-10 11:42:03 +0200866 }
867 }
868
869 return swap_type;
870}
David Brown94ed12c2021-05-26 16:28:14 -0600871#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200872
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800874 * Erases a region of flash.
875 *
Fabio Utzigba829042018-09-18 08:29:34 -0300876 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877 * @param off The offset within the flash area to start the
878 * erase.
879 * @param sz The number of bytes to erase.
880 *
881 * @return 0 on success; nonzero on failure.
882 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300883int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300884boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800885{
Fabio Utzigba829042018-09-18 08:29:34 -0300886 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800887}
888
David Brown94ed12c2021-05-26 16:28:14 -0600889#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800890/**
891 * Copies the contents of one flash region to another. You must erase the
892 * destination region prior to calling this function.
893 *
894 * @param flash_area_id_src The ID of the source flash area.
895 * @param flash_area_id_dst The ID of the destination flash area.
896 * @param off_src The offset within the source flash area to
897 * copy from.
898 * @param off_dst The offset within the destination flash area to
899 * copy to.
900 * @param sz The number of bytes to copy.
901 *
902 * @return 0 on success; nonzero on failure.
903 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300904int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300905boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300906 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300907 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908 uint32_t off_src, uint32_t off_dst, uint32_t sz)
909{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800910 uint32_t bytes_copied;
911 int chunk_sz;
912 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300913#ifdef MCUBOOT_ENC_IMAGES
914 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300915 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300916 size_t blk_off;
917 struct image_header *hdr;
918 uint16_t idx;
919 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300920 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300921#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922
Marek Pietae51ec072021-07-15 14:53:10 +0200923 TARGET_STATIC uint8_t buf[1024] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300924
925#if !defined(MCUBOOT_ENC_IMAGES)
926 (void)state;
927#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929 bytes_copied = 0;
930 while (bytes_copied < sz) {
931 if (sz - bytes_copied > sizeof buf) {
932 chunk_sz = sizeof buf;
933 } else {
934 chunk_sz = sz - bytes_copied;
935 }
936
937 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
938 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300939 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800940 }
941
Fabio Utzigba829042018-09-18 08:29:34 -0300942#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300943 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000944 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
945 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
946 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
947 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100948 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300949 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300950#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300951 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000952 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100953 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300954 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300955 off = off_dst;
956 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300957#else
958 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000959 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300960 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
961 }
962#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200963 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300964 blk_sz = chunk_sz;
965 idx = 0;
966 if (off + bytes_copied < hdr->ih_hdr_size) {
967 /* do not decrypt header */
968 blk_off = 0;
969 blk_sz = chunk_sz - hdr->ih_hdr_size;
970 idx = hdr->ih_hdr_size;
971 } else {
972 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
973 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300974 tlv_off = BOOT_TLV_OFF(hdr);
975 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300976 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300977 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300978 blk_sz = 0;
979 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300980 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300981 }
982 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300983 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300984 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
985 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300986 }
987 }
988#endif
989
Christopher Collins92ea77f2016-12-12 15:59:26 -0800990 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
991 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300992 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800993 }
994
995 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300996
997 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800998 }
999
Fabio Utzigba829042018-09-18 08:29:34 -03001000 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001001}
1002
Christopher Collins92ea77f2016-12-12 15:59:26 -08001003/**
David Vincze2d736ad2019-02-18 11:50:22 +01001004 * Overwrite primary slot with the image contained in the secondary slot.
1005 * If a prior copy operation was interrupted by a system reset, this function
1006 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001007 *
1008 * @param bs The current boot status. This function reads
1009 * this struct to determine if it is resuming
1010 * an interrupted swap operation. This
1011 * function writes the updated status to this
1012 * function on return.
1013 *
1014 * @return 0 on success; nonzero on failure.
1015 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001016#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001017static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001018boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001019{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001020 size_t sect_count;
1021 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001022 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001023 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001024 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001025 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001026 const struct flash_area *fap_primary_slot;
1027 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001028 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001029
Fabio Utzigb4f88102020-10-04 10:16:24 -03001030#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1031 uint32_t sector;
1032 uint32_t trailer_sz;
1033 uint32_t off;
1034 uint32_t sz;
1035#endif
1036
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001037 (void)bs;
1038
Fabio Utzig13d9e352017-10-05 20:32:31 -03001039#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1040 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001041 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001042 assert(rc == 0);
1043#endif
David Brown17609d82017-05-05 09:41:34 -06001044
David Vincze2d736ad2019-02-18 11:50:22 +01001045 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1046 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001047
Fabio Utzigb0f04732019-07-31 09:49:19 -03001048 image_index = BOOT_CURR_IMG(state);
1049
1050 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1051 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001052 assert (rc == 0);
1053
Fabio Utzigb0f04732019-07-31 09:49:19 -03001054 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1055 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001056 assert (rc == 0);
1057
Fabio Utzig10ee6482019-08-01 12:04:52 -03001058 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001059 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001060 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001061 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001062 assert(rc == 0);
1063
Fabio Utzig13d9e352017-10-05 20:32:31 -03001064#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001065 if ((size + this_size) >= src_size) {
1066 size += src_size - size;
1067 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001068 break;
1069 }
1070#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001071
1072 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001073 }
1074
Fabio Utzigb4f88102020-10-04 10:16:24 -03001075#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1076 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1077 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1078 sz = 0;
1079 do {
1080 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1081 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1082 sector--;
1083 } while (sz < trailer_sz);
1084
1085 rc = boot_erase_region(fap_primary_slot, off, sz);
1086 assert(rc == 0);
1087#endif
1088
Fabio Utzigba829042018-09-18 08:29:34 -03001089#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001090 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001091 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001092 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001093 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001094
Fabio Utzigba829042018-09-18 08:29:34 -03001095 if (rc < 0) {
1096 return BOOT_EBADIMAGE;
1097 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001098 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001099 return BOOT_EBADIMAGE;
1100 }
1101 }
1102#endif
1103
David Vincze2d736ad2019-02-18 11:50:22 +01001104 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1105 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001106 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001107 if (rc != 0) {
1108 return rc;
1109 }
1110
1111#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1112 rc = boot_write_magic(fap_primary_slot);
1113 if (rc != 0) {
1114 return rc;
1115 }
1116#endif
David Brown17609d82017-05-05 09:41:34 -06001117
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001118 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1119 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1120 if (rc != 0) {
1121 return rc;
1122 }
1123
David Vinczec3084132020-02-18 14:50:47 +01001124#ifdef MCUBOOT_HW_ROLLBACK_PROT
1125 /* Update the stored security counter with the new image's security counter
1126 * value. Both slots hold the new image at this point, but the secondary
1127 * slot's image header must be passed since the image headers in the
1128 * boot_data structure have not been updated yet.
1129 */
1130 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1131 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1132 if (rc != 0) {
1133 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1134 return rc;
1135 }
1136#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1137
Fabio Utzig13d9e352017-10-05 20:32:31 -03001138 /*
1139 * Erases header and trailer. The trailer is erased because when a new
1140 * image is written without a trailer as is the case when using newt, the
1141 * trailer that was left might trigger a new upgrade.
1142 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001143 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001144 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001145 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1146 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001147 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001148 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001149 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001150 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001151 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1152 last_sector),
1153 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1154 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001155 assert(rc == 0);
1156
David Vincze2d736ad2019-02-18 11:50:22 +01001157 flash_area_close(fap_primary_slot);
1158 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001159
David Vincze2d736ad2019-02-18 11:50:22 +01001160 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001161
1162 return 0;
1163}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001164#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001165
Christopher Collinsa1c12042019-05-23 14:00:28 -07001166#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001167/**
1168 * Swaps the two images in flash. If a prior copy operation was interrupted
1169 * by a system reset, this function completes that operation.
1170 *
1171 * @param bs The current boot status. This function reads
1172 * this struct to determine if it is resuming
1173 * an interrupted swap operation. This
1174 * function writes the updated status to this
1175 * function on return.
1176 *
1177 * @return 0 on success; nonzero on failure.
1178 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001179static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001180boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001181{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001182 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001183#ifdef MCUBOOT_ENC_IMAGES
1184 const struct flash_area *fap;
1185 uint8_t slot;
1186 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001187#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001188 uint32_t size;
1189 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001190 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001191 int rc;
1192
1193 /* FIXME: just do this if asked by user? */
1194
1195 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001196 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001197
Fabio Utzig12d59162019-11-28 10:01:59 -03001198 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001199 /*
1200 * No swap ever happened, so need to find the largest image which
1201 * will be used to determine the amount of sectors to swap.
1202 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001203 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001204 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001205 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001206 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001207 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001208
Fabio Utzigba829042018-09-18 08:29:34 -03001209#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001210 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001211 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001212 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001213 assert(rc >= 0);
1214
1215 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001216 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001217 assert(rc == 0);
1218 } else {
1219 rc = 0;
1220 }
1221 } else {
1222 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1223 }
1224#endif
1225
Fabio Utzig10ee6482019-08-01 12:04:52 -03001226 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001227 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001228 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001229 assert(rc == 0);
1230 }
1231
Fabio Utzigba829042018-09-18 08:29:34 -03001232#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001233 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001234 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001235 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001236 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001237 assert(rc >= 0);
1238
1239 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001240 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001241 assert(rc == 0);
1242 } else {
1243 rc = 0;
1244 }
1245 } else {
1246 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1247 }
1248#endif
1249
Fabio Utzig46490722017-09-04 15:34:32 -03001250 if (size > copy_size) {
1251 copy_size = size;
1252 }
1253
1254 bs->swap_size = copy_size;
1255 } else {
1256 /*
1257 * If a swap was under way, the swap_size should already be present
1258 * in the trailer...
1259 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001260 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001261 assert(rc == 0);
1262
1263 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001264
1265#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001266 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1267 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001268 assert(rc == 0);
1269
1270 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001271 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001272 break;
1273 }
1274 }
1275
1276 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001277 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001278 }
1279 }
1280#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001281 }
1282
Fabio Utzig12d59162019-11-28 10:01:59 -03001283 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001284
David Vincze2d736ad2019-02-18 11:50:22 +01001285#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001286 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001287 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001288 BOOT_LOG_WRN("%d status write fails performing the swap",
1289 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001290 }
1291#endif
1292
Christopher Collins92ea77f2016-12-12 15:59:26 -08001293 return 0;
1294}
David Brown17609d82017-05-05 09:41:34 -06001295#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001296
David Vinczee32483f2019-06-13 10:46:24 +02001297#if (BOOT_IMAGE_NUMBER > 1)
1298/**
1299 * Check the image dependency whether it is satisfied and modify
1300 * the swap type if necessary.
1301 *
1302 * @param dep Image dependency which has to be verified.
1303 *
1304 * @return 0 on success; nonzero on failure.
1305 */
1306static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001307boot_verify_slot_dependency(struct boot_loader_state *state,
1308 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001309{
1310 struct image_version *dep_version;
1311 size_t dep_slot;
1312 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001313 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001314
1315 /* Determine the source of the image which is the subject of
1316 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001317 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001318 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1319 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001320 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001321
David Vincze8b0b6372020-05-20 19:54:44 +02001322 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1323 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001324 /* Dependency not satisfied.
1325 * Modify the swap type to decrease the version number of the image
1326 * (which will be located in the primary slot after the boot process),
1327 * consequently the number of unsatisfied dependencies will be
1328 * decreased or remain the same.
1329 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001330 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001331 case BOOT_SWAP_TYPE_TEST:
1332 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001333 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001334 break;
1335 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001336 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001337 break;
1338 default:
1339 break;
1340 }
David Vincze8b0b6372020-05-20 19:54:44 +02001341 } else {
1342 /* Dependency satisfied. */
1343 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001344 }
1345
1346 return rc;
1347}
1348
1349/**
1350 * Read all dependency TLVs of an image from the flash and verify
1351 * one after another to see if they are all satisfied.
1352 *
1353 * @param slot Image slot number.
1354 *
1355 * @return 0 on success; nonzero on failure.
1356 */
1357static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001358boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001359{
1360 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001361 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001362 struct image_dependency dep;
1363 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001364 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001365 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001366 int rc;
1367
Fabio Utzig10ee6482019-08-01 12:04:52 -03001368 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001369 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001370 if (rc != 0) {
1371 rc = BOOT_EFLASH;
1372 goto done;
1373 }
1374
Fabio Utzig61fd8882019-09-14 20:00:20 -03001375 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1376 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001377 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001378 goto done;
1379 }
1380
Fabio Utzig61fd8882019-09-14 20:00:20 -03001381 while (true) {
1382 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1383 if (rc < 0) {
1384 return -1;
1385 } else if (rc > 0) {
1386 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001387 break;
1388 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001389
1390 if (len != sizeof(dep)) {
1391 rc = BOOT_EBADIMAGE;
1392 goto done;
1393 }
1394
1395 rc = flash_area_read(fap, off, &dep, len);
1396 if (rc != 0) {
1397 rc = BOOT_EFLASH;
1398 goto done;
1399 }
1400
1401 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1402 rc = BOOT_EBADARGS;
1403 goto done;
1404 }
1405
1406 /* Verify dependency and modify the swap type if not satisfied. */
1407 rc = boot_verify_slot_dependency(state, &dep);
1408 if (rc != 0) {
1409 /* Dependency not satisfied. */
1410 goto done;
1411 }
David Vinczee32483f2019-06-13 10:46:24 +02001412 }
1413
1414done:
1415 flash_area_close(fap);
1416 return rc;
1417}
1418
1419/**
David Vinczee32483f2019-06-13 10:46:24 +02001420 * Iterate over all the images and verify whether the image dependencies in the
1421 * TLV area are all satisfied and update the related swap type if necessary.
1422 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001423static int
1424boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001425{
Erik Johnson49063752020-02-06 09:59:31 -06001426 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001427 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001428
Fabio Utzig10ee6482019-08-01 12:04:52 -03001429 BOOT_CURR_IMG(state) = 0;
1430 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001431 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1432 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1433 slot = BOOT_SECONDARY_SLOT;
1434 } else {
1435 slot = BOOT_PRIMARY_SLOT;
1436 }
1437
1438 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001439 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001440 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001441 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001442 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001443 /* Cannot upgrade due to non-met dependencies, so disable all
1444 * image upgrades.
1445 */
1446 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1447 BOOT_CURR_IMG(state) = idx;
1448 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1449 }
1450 break;
David Vinczee32483f2019-06-13 10:46:24 +02001451 }
1452 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001453 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001454}
1455#endif /* (BOOT_IMAGE_NUMBER > 1) */
1456
Christopher Collins92ea77f2016-12-12 15:59:26 -08001457/**
David Vinczeba3bd602019-06-17 16:01:43 +02001458 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001459 *
David Vinczeba3bd602019-06-17 16:01:43 +02001460 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001461 *
1462 * @return 0 on success; nonzero on failure.
1463 */
1464static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001465boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001466{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001467 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001468#ifndef MCUBOOT_OVERWRITE_ONLY
1469 uint8_t swap_type;
1470#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001471
David Vinczeba3bd602019-06-17 16:01:43 +02001472 /* At this point there are no aborted swaps. */
1473#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001474 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001475#elif defined(MCUBOOT_BOOTSTRAP)
1476 /* Check if the image update was triggered by a bad image in the
1477 * primary slot (the validity of the image in the secondary slot had
1478 * already been checked).
1479 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001480 fih_int fih_rc = FIH_FAILURE;
1481 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1482 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1483 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001484 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001485 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001486 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001487 }
1488#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001489 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001490#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001491 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001492
1493#ifndef MCUBOOT_OVERWRITE_ONLY
1494 /* The following state needs image_ok be explicitly set after the
1495 * swap was finished to avoid a new revert.
1496 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001497 swap_type = BOOT_SWAP_TYPE(state);
1498 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1499 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001500 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001501 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001502 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001503 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001504 }
1505
David Vinczec3084132020-02-18 14:50:47 +01001506#ifdef MCUBOOT_HW_ROLLBACK_PROT
1507 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1508 /* Update the stored security counter with the new image's security
1509 * counter value. The primary slot holds the new image at this point,
1510 * but the secondary slot's image header must be passed since image
1511 * headers in the boot_data structure have not been updated yet.
1512 *
1513 * In case of a permanent image swap mcuboot will never attempt to
1514 * revert the images on the next reboot. Therefore, the security
1515 * counter must be increased right after the image upgrade.
1516 */
1517 rc = boot_update_security_counter(
1518 BOOT_CURR_IMG(state),
1519 BOOT_PRIMARY_SLOT,
1520 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1521 if (rc != 0) {
1522 BOOT_LOG_ERR("Security counter update failed after "
1523 "image upgrade.");
1524 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1525 }
1526 }
1527#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1528
Fabio Utzige575b0b2019-09-11 12:34:23 -03001529 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001530 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001531 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001532 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001533 }
David Vinczeba3bd602019-06-17 16:01:43 +02001534 }
1535#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001536
David Vinczeba3bd602019-06-17 16:01:43 +02001537 return rc;
1538}
1539
1540/**
1541 * Completes a previously aborted image swap.
1542 *
1543 * @param bs The current boot status.
1544 *
1545 * @return 0 on success; nonzero on failure.
1546 */
1547#if !defined(MCUBOOT_OVERWRITE_ONLY)
1548static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001549boot_complete_partial_swap(struct boot_loader_state *state,
1550 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001551{
1552 int rc;
1553
1554 /* Determine the type of swap operation being resumed from the
1555 * `swap-type` trailer field.
1556 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001557 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001558 assert(rc == 0);
1559
Fabio Utzig10ee6482019-08-01 12:04:52 -03001560 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001561
1562 /* The following states need image_ok be explicitly set after the
1563 * swap was finished to avoid a new revert.
1564 */
1565 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1566 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001567 rc = swap_set_image_ok(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 Utzige575b0b2019-09-11 12:34:23 -03001573 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001574 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001575 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001576 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001577 }
1578 }
1579
Fabio Utzig10ee6482019-08-01 12:04:52 -03001580 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001581 BOOT_LOG_ERR("panic!");
1582 assert(0);
1583
1584 /* Loop forever... */
1585 while (1) {}
1586 }
1587
1588 return rc;
1589}
1590#endif /* !MCUBOOT_OVERWRITE_ONLY */
1591
1592#if (BOOT_IMAGE_NUMBER > 1)
1593/**
1594 * Review the validity of previously determined swap types of other images.
1595 *
1596 * @param aborted_swap The current image upgrade is a
1597 * partial/aborted swap.
1598 */
1599static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001600boot_review_image_swap_types(struct boot_loader_state *state,
1601 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001602{
1603 /* In that case if we rebooted in the middle of an image upgrade process, we
1604 * must review the validity of swap types, that were previously determined
1605 * for other images. The image_ok flag had not been set before the reboot
1606 * for any of the updated images (only the copy_done flag) and thus falsely
1607 * the REVERT swap type has been determined for the previous images that had
1608 * been updated before the reboot.
1609 *
1610 * There are two separate scenarios that we have to deal with:
1611 *
1612 * 1. The reboot has happened during swapping an image:
1613 * The current image upgrade has been determined as a
1614 * partial/aborted swap.
1615 * 2. The reboot has happened between two separate image upgrades:
1616 * In this scenario we must check the swap type of the current image.
1617 * In those cases if it is NONE or REVERT we cannot certainly determine
1618 * the fact of a reboot. In a consistent state images must move in the
1619 * same direction or stay in place, e.g. in practice REVERT and TEST
1620 * swap types cannot be present at the same time. If the swap type of
1621 * the current image is either TEST, PERM or FAIL we must review the
1622 * already determined swap types of other images and set each false
1623 * REVERT swap types to NONE (these images had been successfully
1624 * updated before the system rebooted between two separate image
1625 * upgrades).
1626 */
1627
Fabio Utzig10ee6482019-08-01 12:04:52 -03001628 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001629 /* Nothing to do */
1630 return;
1631 }
1632
1633 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001634 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1635 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001636 /* Nothing to do */
1637 return;
1638 }
1639 }
1640
Fabio Utzig10ee6482019-08-01 12:04:52 -03001641 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1642 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1643 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001644 }
1645 }
1646}
1647#endif
1648
1649/**
1650 * Prepare image to be updated if required.
1651 *
1652 * Prepare image to be updated if required with completing an image swap
1653 * operation if one was aborted and/or determining the type of the
1654 * swap operation. In case of any error set the swap type to NONE.
1655 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001656 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001657 * @param bs Pointer where the read and possibly updated
1658 * boot status can be written to.
1659 */
1660static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001661boot_prepare_image_for_update(struct boot_loader_state *state,
1662 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001663{
1664 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001665 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001666
1667 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001668 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001669 if (rc != 0) {
1670 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1671 " - too small?", BOOT_MAX_IMG_SECTORS);
1672 /* Unable to determine sector layout, continue with next image
1673 * if there is one.
1674 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001675 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001676 if (rc == BOOT_EFLASH)
1677 {
1678 /* Only return on error from the primary image flash */
1679 return;
1680 }
David Vinczeba3bd602019-06-17 16:01:43 +02001681 }
1682
1683 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001684 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001685 if (rc != 0) {
1686 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001687 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001688 BOOT_CURR_IMG(state));
1689 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001690 return;
1691 }
1692
1693 /* If the current image's slots aren't compatible, no swap is possible.
1694 * Just boot into primary slot.
1695 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001696 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001697 boot_status_reset(bs);
1698
1699#ifndef MCUBOOT_OVERWRITE_ONLY
1700 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001701 if (rc != 0) {
1702 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001703 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001704 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001705 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001706 return;
1707 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001708#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001709
Fabio Utzig74aef312019-11-28 11:05:34 -03001710#ifdef MCUBOOT_SWAP_USING_MOVE
1711 /*
1712 * Must re-read image headers because the boot status might
1713 * have been updated in the previous function call.
1714 */
1715 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001716#ifdef MCUBOOT_BOOTSTRAP
1717 /* When bootstrapping it's OK to not have image magic in the primary slot */
1718 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1719 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1720#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001721 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001722#endif
1723
Fabio Utzig74aef312019-11-28 11:05:34 -03001724 /* Continue with next image if there is one. */
1725 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1726 BOOT_CURR_IMG(state));
1727 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1728 return;
1729 }
1730#endif
1731
David Vinczeba3bd602019-06-17 16:01:43 +02001732 /* Determine if we rebooted in the middle of an image swap
1733 * operation. If a partial swap was detected, complete it.
1734 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001735 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001736
1737#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001738 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001739#endif
1740
1741#ifdef MCUBOOT_OVERWRITE_ONLY
1742 /* Should never arrive here, overwrite-only mode has
1743 * no swap state.
1744 */
1745 assert(0);
1746#else
1747 /* Determine the type of swap operation being resumed from the
1748 * `swap-type` trailer field.
1749 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001750 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001751 assert(rc == 0);
1752#endif
1753 /* Attempt to read an image header from each slot. Ensure that
1754 * image headers in slots are aligned with headers in boot_data.
1755 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001756 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001757 assert(rc == 0);
1758
1759 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001760 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001761 } else {
1762 /* There was no partial swap, determine swap type. */
1763 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001764 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001765 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001766 FIH_CALL(boot_validate_slot, fih_rc,
1767 state, BOOT_SECONDARY_SLOT, bs);
1768 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1769 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1770 } else {
1771 BOOT_SWAP_TYPE(state) = bs->swap_type;
1772 }
David Vinczeba3bd602019-06-17 16:01:43 +02001773 }
1774
1775#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001776 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001777#endif
1778
1779#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001780 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001781 /* Header checks are done first because they are
1782 * inexpensive. Since overwrite-only copies starting from
1783 * offset 0, if interrupted, it might leave a valid header
1784 * magic, so also run validation on the primary slot to be
1785 * sure it's not OK.
1786 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001787 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1788 FIH_CALL(boot_validate_slot, fih_rc,
1789 state, BOOT_PRIMARY_SLOT, bs);
1790
1791 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1792
Fabio Utzig3d77c952020-10-04 10:23:17 -03001793 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001794 FIH_CALL(boot_validate_slot, fih_rc,
1795 state, BOOT_SECONDARY_SLOT, bs);
1796
Fabio Utzig3d77c952020-10-04 10:23:17 -03001797 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001798 /* Set swap type to REVERT to overwrite the primary
1799 * slot with the image contained in secondary slot
1800 * and to trigger the explicit setting of the
1801 * image_ok flag.
1802 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001803 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001804 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001805 }
1806 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001807#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001808 }
David Vinczeba3bd602019-06-17 16:01:43 +02001809 } else {
1810 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001811 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001812 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001813}
1814
Mark Horvathccaf7f82021-01-04 18:16:42 +01001815/**
1816 * Updates the security counter for the current image.
1817 *
1818 * @param state Boot loader status information.
1819 *
1820 * @return 0 on success; nonzero on failure.
1821 */
1822static int
1823boot_update_hw_rollback_protection(struct boot_loader_state *state)
1824{
1825#ifdef MCUBOOT_HW_ROLLBACK_PROT
1826 int rc;
1827
1828 /* Update the stored security counter with the active image's security
1829 * counter value. It will only be updated if the new security counter is
1830 * greater than the stored value.
1831 *
1832 * In case of a successful image swapping when the swap type is TEST the
1833 * security counter can be increased only after a reset, when the swap
1834 * type is NONE and the image has marked itself "OK" (the image_ok flag
1835 * has been set). This way a "revert" can be performed when it's
1836 * necessary.
1837 */
1838 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1839 rc = boot_update_security_counter(
1840 BOOT_CURR_IMG(state),
1841 BOOT_PRIMARY_SLOT,
1842 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1843 if (rc != 0) {
1844 BOOT_LOG_ERR("Security counter update failed after image "
1845 "validation.");
1846 return rc;
1847 }
1848 }
1849
1850 return 0;
1851
1852#else /* MCUBOOT_HW_ROLLBACK_PROT */
1853 (void) (state);
1854
1855 return 0;
1856#endif
1857}
1858
Raef Colese8fe6cf2020-05-26 13:07:40 +01001859fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001860context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001861{
Marti Bolivar84898652017-06-13 17:20:22 -04001862 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001863 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001864 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001865 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001866 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001867 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001868 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001869
1870 /* The array of slot sectors are defined here (as opposed to file scope) so
1871 * that they don't get allocated for non-boot-loader apps. This is
1872 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001873 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001874 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001875 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1876 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001877#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001878 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001879#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001880
Fabio Utzig10ee6482019-08-01 12:04:52 -03001881 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001882 has_upgrade = false;
1883
1884#if (BOOT_IMAGE_NUMBER == 1)
1885 (void)has_upgrade;
1886#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001887
David Vinczeba3bd602019-06-17 16:01:43 +02001888 /* Iterate over all the images. By the end of the loop the swap type has
1889 * to be determined for each image and all aborted swaps have to be
1890 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001891 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001892 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001893
David Vinczeba3bd602019-06-17 16:01:43 +02001894#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1895 /* The keys used for encryption may no longer be valid (could belong to
1896 * another images). Therefore, mark them as invalid to force their reload
1897 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001898 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001899 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001900#endif
1901
Fabio Utzig10ee6482019-08-01 12:04:52 -03001902 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001903
Fabio Utzig10ee6482019-08-01 12:04:52 -03001904 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001905 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001906 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001907 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001908#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001909 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001910#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001911
1912 /* Open primary and secondary image areas for the duration
1913 * of this call.
1914 */
1915 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001916 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001917 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001918 assert(rc == 0);
1919 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001920#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001921 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001922 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001923 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001924#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001925
1926 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001927 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001928
Fabio Utzige575b0b2019-09-11 12:34:23 -03001929 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001930 has_upgrade = true;
1931 }
David Vinczeba3bd602019-06-17 16:01:43 +02001932 }
1933
David Vinczee32483f2019-06-13 10:46:24 +02001934#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001935 if (has_upgrade) {
1936 /* Iterate over all the images and verify whether the image dependencies
1937 * are all satisfied and update swap type if necessary.
1938 */
1939 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001940 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001941 /*
1942 * It was impossible to upgrade because the expected dependency version
1943 * was not available. Here we already changed the swap_type so that
1944 * instead of asserting the bootloader, we continue and no upgrade is
1945 * performed.
1946 */
1947 rc = 0;
1948 }
1949 }
David Vinczee32483f2019-06-13 10:46:24 +02001950#endif
1951
David Vinczeba3bd602019-06-17 16:01:43 +02001952 /* Iterate over all the images. At this point there are no aborted swaps
1953 * and the swap types are determined for each image. By the end of the loop
1954 * all required update operations will have been finished.
1955 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001956 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001957
1958#if (BOOT_IMAGE_NUMBER > 1)
1959#ifdef MCUBOOT_ENC_IMAGES
1960 /* The keys used for encryption may no longer be valid (could belong to
1961 * another images). Therefore, mark them as invalid to force their reload
1962 * by boot_enc_load().
1963 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001964 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001965#endif /* MCUBOOT_ENC_IMAGES */
1966
1967 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001968 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001969#endif /* (BOOT_IMAGE_NUMBER > 1) */
1970
1971 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001972 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001973
Fabio Utzig10ee6482019-08-01 12:04:52 -03001974 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001975 case BOOT_SWAP_TYPE_NONE:
1976 break;
1977
1978 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1979 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1980 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001981 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
1982 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
1983 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
1984 if (rc == BOOT_HOOK_REGULAR)
1985 {
1986 rc = boot_perform_update(state, &bs);
1987 }
David Vinczeba3bd602019-06-17 16:01:43 +02001988 assert(rc == 0);
1989 break;
1990
1991 case BOOT_SWAP_TYPE_FAIL:
1992 /* The image in secondary slot was invalid and is now erased. Ensure
1993 * we don't try to boot into it again on the next reboot. Do this by
1994 * pretending we just reverted back to primary slot.
1995 */
1996#ifndef MCUBOOT_OVERWRITE_ONLY
1997 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001998 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001999 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002000 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002001 }
2002#endif /* !MCUBOOT_OVERWRITE_ONLY */
2003 break;
2004
2005 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002006 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002007 }
2008
Fabio Utzig10ee6482019-08-01 12:04:52 -03002009 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002010 BOOT_LOG_ERR("panic!");
2011 assert(0);
2012
2013 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002014 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002015 }
2016 }
2017
2018 /* Iterate over all the images. At this point all required update operations
2019 * have finished. By the end of the loop each image in the primary slot will
2020 * have been re-validated.
2021 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002022 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2023 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002024 /* Attempt to read an image header from each slot. Ensure that image
2025 * headers in slots are aligned with headers in boot_data.
2026 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002027 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002028 if (rc != 0) {
2029 goto out;
2030 }
2031 /* Since headers were reloaded, it can be assumed we just performed
2032 * a swap or overwrite. Now the header info that should be used to
2033 * provide the data for the bootstrap, which previously was at
2034 * secondary slot, was updated to primary slot.
2035 */
2036 }
2037
2038#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002039 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2040 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002041 goto out;
2042 }
2043#else
2044 /* Even if we're not re-validating the primary slot, we could be booting
2045 * onto an empty flash chip. At least do a basic sanity check that
2046 * the magic number on the image is OK.
2047 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002048 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002049 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002050 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002051 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002052 rc = BOOT_EBADIMAGE;
2053 goto out;
2054 }
David Vinczec3084132020-02-18 14:50:47 +01002055#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2056
Mark Horvathccaf7f82021-01-04 18:16:42 +01002057 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002058 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002059 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002060 }
David Vincze1cf11b52020-03-24 07:51:09 +01002061
Mark Horvathccaf7f82021-01-04 18:16:42 +01002062 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002063 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002064 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002065 }
David Vinczeba3bd602019-06-17 16:01:43 +02002066 }
2067
Fabio Utzigf616c542019-12-19 15:23:32 -03002068 /*
2069 * Since the boot_status struct stores plaintext encryption keys, reset
2070 * them here to avoid the possibility of jumping into an image that could
2071 * easily recover them.
2072 */
2073 memset(&bs, 0, sizeof(struct boot_status));
2074
Mark Horvathccaf7f82021-01-04 18:16:42 +01002075 fill_rsp(state, NULL, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002076
Raef Colese8fe6cf2020-05-26 13:07:40 +01002077 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002078out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002079 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002080
2081 if (rc) {
2082 fih_rc = fih_int_encode(rc);
2083 }
2084
2085 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002086}
2087
Raef Colese8fe6cf2020-05-26 13:07:40 +01002088fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002089split_go(int loader_slot, int split_slot, void **entry)
2090{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002091 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002092 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002093 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002094 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002095 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002096 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002097
Christopher Collins92ea77f2016-12-12 15:59:26 -08002098 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2099 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002100 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002101 }
David Vinczeba3bd602019-06-17 16:01:43 +02002102 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2103 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002104
2105 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2106 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002107 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002108 assert(rc == 0);
2109 split_flash_id = flash_area_id_from_image_slot(split_slot);
2110 rc = flash_area_open(split_flash_id,
2111 &BOOT_IMG_AREA(&boot_data, split_slot));
2112 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002113
2114 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002115 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002116 if (rc != 0) {
2117 rc = SPLIT_GO_ERR;
2118 goto done;
2119 }
2120
Fabio Utzig12d59162019-11-28 10:01:59 -03002121 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002122 if (rc != 0) {
2123 goto done;
2124 }
2125
Christopher Collins92ea77f2016-12-12 15:59:26 -08002126 /* Don't check the bootable image flag because we could really call a
2127 * bootable or non-bootable image. Just validate that the image check
2128 * passes which is distinct from the normal check.
2129 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002130 FIH_CALL(split_image_check, fih_rc,
2131 boot_img_hdr(&boot_data, split_slot),
2132 BOOT_IMG_AREA(&boot_data, split_slot),
2133 boot_img_hdr(&boot_data, loader_slot),
2134 BOOT_IMG_AREA(&boot_data, loader_slot));
2135 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002136 goto done;
2137 }
2138
Marti Bolivarea088872017-06-12 17:10:49 -04002139 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002140 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002141 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002142 rc = SPLIT_GO_OK;
2143
2144done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002145 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2146 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002147 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002148
2149 if (rc) {
2150 fih_rc = fih_int_encode(rc);
2151 }
2152
2153 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002154}
David Vinczee574f2d2020-07-10 11:42:03 +02002155
Tamas Banfe031092020-09-10 17:32:39 +02002156#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002157
Mark Horvathccaf7f82021-01-04 18:16:42 +01002158#define NO_ACTIVE_SLOT UINT32_MAX
2159
David Vinczee574f2d2020-07-10 11:42:03 +02002160/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002161 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002162 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002163 * @param state Boot loader status information.
2164 * @param slot_usage Structure to fill with information about the available
2165 * slots.
David Vinczee574f2d2020-07-10 11:42:03 +02002166 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002167 * @return 0 on success; nonzero on failure.
2168 */
2169static int
2170boot_get_slot_usage(struct boot_loader_state *state,
2171 struct slot_usage_t slot_usage[])
2172{
2173 uint32_t slot;
2174 int fa_id;
2175 int rc;
2176 struct image_header *hdr = NULL;
2177
2178 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2179 /* Open all the slots */
2180 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2181 fa_id = flash_area_id_from_multi_image_slot(
2182 BOOT_CURR_IMG(state), slot);
2183 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2184 assert(rc == 0);
2185 }
2186
2187 /* Attempt to read an image header from each slot. */
2188 rc = boot_read_image_headers(state, false, NULL);
2189 if (rc != 0) {
2190 BOOT_LOG_WRN("Failed reading image headers.");
2191 return rc;
2192 }
2193
2194 /* Check headers in all slots */
2195 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2196 hdr = boot_img_hdr(state, slot);
2197
2198 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2199 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
2200 BOOT_LOG_IMAGE_INFO(slot, hdr);
2201 } else {
2202 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
2203 BOOT_LOG_INF("Image %d %s slot: Image not found",
2204 BOOT_CURR_IMG(state),
2205 (slot == BOOT_PRIMARY_SLOT)
2206 ? "Primary" : "Secondary");
2207 }
2208 }
2209
2210 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2211 }
2212
2213 return 0;
2214}
2215
2216/**
2217 * Finds the slot containing the image with the highest version number for the
2218 * current image.
2219 *
2220 * @param state Boot loader status information.
2221 * @param slot_usage Information about the active and available slots.
2222 *
2223 * @return NO_ACTIVE_SLOT if no available slot found, number of
2224 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002225 */
2226static uint32_t
Mark Horvathccaf7f82021-01-04 18:16:42 +01002227find_slot_with_highest_version(struct boot_loader_state *state,
2228 struct slot_usage_t slot_usage[])
David Vinczee574f2d2020-07-10 11:42:03 +02002229{
David Vinczee574f2d2020-07-10 11:42:03 +02002230 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002231 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2232 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002233
Mark Horvathccaf7f82021-01-04 18:16:42 +01002234 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2235 if (slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
2236 if (candidate_slot == NO_ACTIVE_SLOT) {
2237 candidate_slot = slot;
2238 } else {
2239 rc = boot_version_cmp(
2240 &boot_img_hdr(state, slot)->ih_ver,
2241 &boot_img_hdr(state, candidate_slot)->ih_ver);
2242 if (rc == 1) {
2243 /* The version of the image being examined is greater than
2244 * the version of the current candidate.
2245 */
2246 candidate_slot = slot;
2247 }
2248 }
David Vinczee574f2d2020-07-10 11:42:03 +02002249 }
2250 }
2251
Mark Horvathccaf7f82021-01-04 18:16:42 +01002252 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002253}
2254
Mark Horvathccaf7f82021-01-04 18:16:42 +01002255#ifdef MCUBOOT_HAVE_LOGGING
2256/**
2257 * Prints the state of the loaded images.
2258 *
2259 * @param state Boot loader status information.
2260 * @param slot_usage Information about the active and available slots.
2261 */
2262static void
2263print_loaded_images(struct boot_loader_state *state,
2264 struct slot_usage_t slot_usage[])
2265{
2266 uint32_t active_slot;
2267
David Brown695e5912021-05-24 16:58:01 -06002268 (void)state;
2269
Mark Horvathccaf7f82021-01-04 18:16:42 +01002270 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2271 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2272
2273 BOOT_LOG_INF("Image %d loaded from the %s slot",
2274 BOOT_CURR_IMG(state),
2275 (active_slot == BOOT_PRIMARY_SLOT) ?
2276 "primary" : "secondary");
2277 }
2278}
2279#endif
2280
David Vincze1c456242021-06-29 15:25:24 +02002281#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002282/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002283 * Checks whether the active slot of the current image was previously selected
2284 * to run. Erases the image if it was selected but its execution failed,
2285 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002286 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002287 * @param state Boot loader status information.
2288 * @param slot_usage Information about the active and available slots.
David Vincze505fba22020-10-22 13:53:29 +02002289 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002290 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002291 */
2292static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002293boot_select_or_erase(struct boot_loader_state *state,
2294 struct slot_usage_t slot_usage[])
David Vincze505fba22020-10-22 13:53:29 +02002295{
2296 const struct flash_area *fap;
2297 int fa_id;
2298 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002299 uint32_t active_slot;
2300 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002301
Mark Horvathccaf7f82021-01-04 18:16:42 +01002302 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2303
2304 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002305 rc = flash_area_open(fa_id, &fap);
2306 assert(rc == 0);
2307
Mark Horvathccaf7f82021-01-04 18:16:42 +01002308 active_swap_state = &(slot_usage[BOOT_CURR_IMG(state)].swap_state);
2309
2310 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2311 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002312 assert(rc == 0);
2313
Mark Horvathccaf7f82021-01-04 18:16:42 +01002314 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2315 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2316 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002317 /*
2318 * A reboot happened without the image being confirmed at
2319 * runtime or its trailer is corrupted/invalid. Erase the image
2320 * to prevent it from being selected again on the next reboot.
2321 */
2322 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002323 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002324 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002325 assert(rc == 0);
2326
2327 flash_area_close(fap);
2328 rc = -1;
2329 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002330 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2331 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002332 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2333 "value was neither 'set' nor 'unset', but 'bad'.");
2334 }
2335 /*
2336 * Set the copy_done flag, indicating that the image has been
2337 * selected to boot. It can be set in advance, before even
2338 * validating the image, because in case the validation fails, the
2339 * entire image slot will be erased (including the trailer).
2340 */
2341 rc = boot_write_copy_done(fap);
2342 if (rc != 0) {
2343 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002344 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002345 "primary" : "secondary");
2346 rc = 0;
2347 }
2348 }
2349 flash_area_close(fap);
2350 }
2351
2352 return rc;
2353}
David Vincze1c456242021-06-29 15:25:24 +02002354#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002355
Tamas Banfe031092020-09-10 17:32:39 +02002356#ifdef MCUBOOT_RAM_LOAD
2357
Mark Horvathccaf7f82021-01-04 18:16:42 +01002358#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002359#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2360#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2361#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002362#endif
Tamas Banfe031092020-09-10 17:32:39 +02002363
2364/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002365 * Verifies that the active slot of the current image can be loaded within the
2366 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002367 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002368 * @param state Boot loader status information.
2369 * @param slot_usage Information about the active and available slots.
Tamas Banfe031092020-09-10 17:32:39 +02002370 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002371 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002372 */
2373static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002374boot_verify_ram_load_address(struct boot_loader_state *state,
2375 struct slot_usage_t slot_usage[])
Tamas Banfe031092020-09-10 17:32:39 +02002376{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002377 uint32_t img_dst;
2378 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002379 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002380 uint32_t exec_ram_start;
2381 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002382
2383 (void)state;
2384
Mark Horvathccaf7f82021-01-04 18:16:42 +01002385#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2386 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002387
Mark Horvathccaf7f82021-01-04 18:16:42 +01002388 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2389 &exec_ram_size);
2390 if (rc != 0) {
2391 return BOOT_EBADSTATUS;
2392 }
2393#else
2394 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2395 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2396#endif
2397
2398 img_dst = slot_usage[BOOT_CURR_IMG(state)].img_dst;
2399 img_sz = slot_usage[BOOT_CURR_IMG(state)].img_sz;
2400
2401 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002402 return BOOT_EBADIMAGE;
2403 }
2404
2405 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2406 return BOOT_EBADIMAGE;
2407 }
2408
Mark Horvathccaf7f82021-01-04 18:16:42 +01002409 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002410 return BOOT_EBADIMAGE;
2411 }
2412
2413 return 0;
2414}
2415
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002416#ifdef MCUBOOT_ENC_IMAGES
2417
2418/**
2419 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2420 *
2421 * @param state Boot loader status information.
2422 * @param slot The flash slot of the image to be copied to SRAM.
2423 * @param hdr The image header.
2424 * @param src_sz Size of the image.
2425 * @param img_dst Pointer to the address at which the image needs to be
2426 * copied to SRAM.
2427 *
2428 * @return 0 on success; nonzero on failure.
2429 */
2430static int
2431boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2432 uint32_t slot, struct image_header *hdr,
2433 uint32_t src_sz, uint32_t img_dst)
2434{
2435 /* The flow for the decryption and copy of the image is as follows :
2436 * 1. The whole image is copied to the RAM (header + payload + TLV).
2437 * 2. The encryption key is loaded from the TLV in flash.
2438 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2439 * is 1024 bytes). Only the payload section is decrypted.
2440 * 4. The image is authenticated in RAM.
2441 */
2442 const struct flash_area *fap_src = NULL;
2443 struct boot_status bs;
2444 uint32_t blk_off;
2445 uint32_t tlv_off;
2446 uint32_t blk_sz;
2447 uint32_t bytes_copied = hdr->ih_hdr_size;
2448 uint32_t chunk_sz;
2449 uint32_t max_sz = 1024;
2450 uint16_t idx;
2451 uint8_t image_index;
2452 uint8_t * cur_dst;
2453 int area_id;
2454 int rc;
2455 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2456
2457 image_index = BOOT_CURR_IMG(state);
2458 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2459 rc = flash_area_open(area_id, &fap_src);
2460 if (rc != 0){
2461 return BOOT_EFLASH;
2462 }
2463
2464 tlv_off = BOOT_TLV_OFF(hdr);
2465
2466 /* Copying the whole image in RAM */
2467 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2468 if (rc != 0) {
2469 goto done;
2470 }
2471
2472 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2473 if (rc < 0) {
2474 goto done;
2475 }
2476
2477 /* if rc > 0 then the key has already been loaded */
2478 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2479 goto done;
2480 }
2481
2482 /* Starting at the end of the header as the header section is not encrypted */
2483 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2484 if (src_sz - bytes_copied > max_sz) {
2485 chunk_sz = max_sz;
2486 } else {
2487 chunk_sz = src_sz - bytes_copied;
2488 }
2489
2490 cur_dst = ram_dst + bytes_copied;
2491 blk_sz = chunk_sz;
2492 idx = 0;
2493 if (bytes_copied + chunk_sz > tlv_off) {
2494 /* Going over TLV section
2495 * Part of the chunk is encrypted payload */
2496 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2497 blk_sz = tlv_off - (bytes_copied);
2498 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2499 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2500 blk_off, cur_dst);
2501 } else {
2502 /* Image encrypted payload section */
2503 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2504 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2505 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2506 blk_off, cur_dst);
2507 }
2508
2509 bytes_copied += chunk_sz;
2510 }
2511 rc = 0;
2512
2513done:
2514 flash_area_close(fap_src);
2515
2516 return rc;
2517}
2518
2519#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002520/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002521 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002522 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002523 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002524 * @param slot The flash slot of the image to be copied to SRAM.
2525 * @param img_dst The address at which the image needs to be copied to
2526 * SRAM.
2527 * @param img_sz The size of the image that needs to be copied to SRAM.
2528 *
2529 * @return 0 on success; nonzero on failure.
2530 */
2531static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002532boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2533 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002534{
2535 int rc;
2536 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002537 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002538
Mark Horvathccaf7f82021-01-04 18:16:42 +01002539#if (BOOT_IMAGE_NUMBER == 1)
2540 (void)state;
2541#endif
2542
2543 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2544
2545 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002546 if (rc != 0) {
2547 return BOOT_EFLASH;
2548 }
2549
2550 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002551 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002552 if (rc != 0) {
2553 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2554 }
2555
2556 flash_area_close(fap_src);
2557
2558 return rc;
2559}
2560
Mark Horvathccaf7f82021-01-04 18:16:42 +01002561#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002562/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002563 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002564 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002565 * @param start_a Start of the A region.
2566 * @param end_a End of the A region.
2567 * @param start_b Start of the B region.
2568 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002569 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002570 * @return true if there is overlap; false otherwise.
2571 */
2572static bool
2573do_regions_overlap(uint32_t start_a, uint32_t end_a,
2574 uint32_t start_b, uint32_t end_b)
2575{
2576 if (start_b > end_a) {
2577 return false;
2578 } else if (start_b >= start_a) {
2579 return true;
2580 } else if (end_b > start_a) {
2581 return true;
2582 }
2583
2584 return false;
2585}
2586
2587/**
2588 * Checks if the image we want to load to memory overlap with an already
2589 * ramloaded image.
2590 *
2591 * @param slot_usage Information about the active and available slots.
2592 * @param image_id_to_check The ID of the image we would like to load.
2593 *
2594 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002595 */
2596static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002597boot_check_ram_load_overlapping(struct slot_usage_t slot_usage[],
2598 uint32_t image_id_to_check)
Tamas Banfe031092020-09-10 17:32:39 +02002599{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002600 uint32_t i;
2601
2602 uint32_t start_a;
2603 uint32_t end_a;
2604 uint32_t start_b;
2605 uint32_t end_b;
2606
2607 start_a = slot_usage[image_id_to_check].img_dst;
2608 /* Safe to add here, values are already verified in
2609 * boot_verify_ram_load_address() */
2610 end_a = start_a + slot_usage[image_id_to_check].img_sz;
2611
2612 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
2613 if (slot_usage[i].active_slot == NO_ACTIVE_SLOT
2614 || i == image_id_to_check) {
2615 continue;
2616 }
2617
2618 start_b = slot_usage[i].img_dst;
2619 /* Safe to add here, values are already verified in
2620 * boot_verify_ram_load_address() */
2621 end_b = start_b + slot_usage[i].img_sz;
2622
2623 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2624 return -1;
2625 }
2626 }
2627
2628 return 0;
2629}
2630#endif
2631
2632/**
2633 * Loads the active slot of the current image into SRAM. The load address and
2634 * image size is extracted from the image header.
2635 *
2636 * @param state Boot loader status information.
2637 * @param slot_usage Information about the active and available slots.
2638 *
2639 * @return 0 on success; nonzero on failure.
2640 */
2641static int
2642boot_load_image_to_sram(struct boot_loader_state *state,
2643 struct slot_usage_t slot_usage[])
2644{
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
Mark Horvathccaf7f82021-01-04 18:16:42 +01002651 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2652 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
Mark Horvathccaf7f82021-01-04 18:16:42 +01002663 slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2664 slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
2665
2666 rc = boot_verify_ram_load_address(state, slot_usage);
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)
2673 rc = boot_check_ram_load_overlapping(slot_usage, BOOT_CURR_IMG(state));
2674 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) {
2706 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2707 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2708 }
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.
2717 * @param slot_usage Information about the active and available slots.
2718 *
2719 * @return 0 on success; nonzero on failure.
2720 */
2721static inline int
2722boot_remove_image_from_sram(struct boot_loader_state *state,
2723 struct slot_usage_t slot_usage[])
2724{
David Brown695e5912021-05-24 16:58:01 -06002725 (void)state;
2726
Mark Horvathccaf7f82021-01-04 18:16:42 +01002727 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
2728 slot_usage[BOOT_CURR_IMG(state)].img_dst);
2729
David Brown695e5912021-05-24 16:58:01 -06002730 memset((void*)(IMAGE_RAM_BASE + slot_usage[BOOT_CURR_IMG(state)].img_dst),
2731 0, slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002732
2733 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2734 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2735
2736 return 0;
2737}
2738
2739/**
2740 * Removes an image from flash by erasing the corresponding flash area
2741 *
2742 * @param state Boot loader status information.
2743 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002744 *
2745 * @return 0 on success; nonzero on failure.
2746 */
2747static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002748boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002749{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002750 int area_id;
2751 int rc;
2752 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002753
David Brown695e5912021-05-24 16:58:01 -06002754 (void)state;
2755
Mark Horvathccaf7f82021-01-04 18:16:42 +01002756 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2757 slot);
2758 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2759 rc = flash_area_open(area_id, &fap);
2760 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002761 flash_area_erase(fap, 0, flash_area_get_size(fap));
Mark Horvathccaf7f82021-01-04 18:16:42 +01002762 }
2763
2764 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002765}
2766#endif /* MCUBOOT_RAM_LOAD */
2767
Mark Horvathccaf7f82021-01-04 18:16:42 +01002768#if (BOOT_IMAGE_NUMBER > 1)
2769/**
2770 * Checks the image dependency whether it is satisfied.
2771 *
2772 * @param state Boot loader status information.
2773 * @param slot_usage Information about the active and available slots.
2774 * @param dep Image dependency which has to be verified.
2775 *
2776 * @return 0 if dependencies are met; nonzero otherwise.
2777 */
2778static int
2779boot_verify_slot_dependency(struct boot_loader_state *state,
2780 struct slot_usage_t slot_usage[],
2781 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002782{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002783 struct image_version *dep_version;
2784 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002785 int rc;
2786
Mark Horvathccaf7f82021-01-04 18:16:42 +01002787 /* Determine the source of the image which is the subject of
2788 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002789 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002790 dep_slot = slot_usage[dep->image_id].active_slot;
2791 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2792
2793 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2794 if (rc >= 0) {
2795 /* Dependency satisfied. */
2796 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002797 }
2798
Mark Horvathccaf7f82021-01-04 18:16:42 +01002799 return rc;
2800}
2801
2802/**
2803 * Reads all dependency TLVs of an image and verifies one after another to see
2804 * if they are all satisfied.
2805 *
2806 * @param state Boot loader status information.
2807 * @param slot_usage Information about the active and available slots.
2808 *
2809 * @return 0 if dependencies are met; nonzero otherwise.
2810 */
2811static int
2812boot_verify_slot_dependencies(struct boot_loader_state *state,
2813 struct slot_usage_t slot_usage[])
2814{
2815 uint32_t active_slot;
2816 const struct flash_area *fap;
2817 struct image_tlv_iter it;
2818 struct image_dependency dep;
2819 uint32_t off;
2820 uint16_t len;
2821 int area_id;
2822 int rc;
2823
2824 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2825
2826 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2827 active_slot);
2828 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002829 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002830 rc = BOOT_EFLASH;
2831 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002832 }
2833
Mark Horvathccaf7f82021-01-04 18:16:42 +01002834 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2835 IMAGE_TLV_DEPENDENCY, true);
2836 if (rc != 0) {
2837 goto done;
2838 }
David Vinczee574f2d2020-07-10 11:42:03 +02002839
Mark Horvathccaf7f82021-01-04 18:16:42 +01002840 while (true) {
2841 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2842 if (rc < 0) {
2843 return -1;
2844 } else if (rc > 0) {
2845 rc = 0;
2846 break;
2847 }
David Vinczee574f2d2020-07-10 11:42:03 +02002848
Mark Horvathccaf7f82021-01-04 18:16:42 +01002849 if (len != sizeof(dep)) {
2850 rc = BOOT_EBADIMAGE;
2851 goto done;
2852 }
2853
2854 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2855 fap, off, &dep, len);
2856 if (rc != 0) {
2857 rc = BOOT_EFLASH;
2858 goto done;
2859 }
2860
2861 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2862 rc = BOOT_EBADARGS;
2863 goto done;
2864 }
2865
2866 rc = boot_verify_slot_dependency(state, slot_usage, &dep);
2867 if (rc != 0) {
2868 /* Dependency not satisfied. */
2869 goto done;
2870 }
2871 }
2872
2873done:
2874 flash_area_close(fap);
2875 return rc;
2876}
2877
2878/**
2879 * Checks the dependency of all the active slots. If an image found with
2880 * invalid or not satisfied dependencies the image is removed from SRAM (in
2881 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2882 *
2883 * @param state Boot loader status information.
2884 * @param slot_usage Information about the active and available slots.
2885 *
2886 * @return 0 if dependencies are met; nonzero otherwise.
2887 */
2888static int
2889boot_verify_dependencies(struct boot_loader_state *state,
2890 struct slot_usage_t slot_usage[])
2891{
2892 int rc = -1;
2893 uint32_t active_slot;
2894
2895 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2896 rc = boot_verify_slot_dependencies(state, slot_usage);
2897 if (rc != 0) {
2898 /* Dependencies not met or invalid dependencies. */
2899
2900#ifdef MCUBOOT_RAM_LOAD
2901 boot_remove_image_from_sram(state, slot_usage);
2902#endif /* MCUBOOT_RAM_LOAD */
2903
2904 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2905 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2906 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2907
2908 return rc;
2909 }
2910 }
2911
2912 return rc;
2913}
2914#endif /* (BOOT_IMAGE_NUMBER > 1) */
2915
2916/**
2917 * Tries to load a slot for all the images with validation.
2918 *
2919 * @param state Boot loader status information.
2920 * @param slot_usage Information about the active and available slots.
2921 *
2922 * @return 0 on success; nonzero on failure.
2923 */
2924fih_int
2925boot_load_and_validate_images(struct boot_loader_state *state,
2926 struct slot_usage_t slot_usage[])
2927{
2928 uint32_t active_slot;
2929 int rc;
2930 fih_int fih_rc;
2931
2932 /* Go over all the images and try to load one */
2933 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2934 /* All slots tried until a valid image found. Breaking from this loop
2935 * means that a valid image found or already loaded. If no slot is
2936 * found the function returns with error code. */
2937 while (true) {
2938
2939 /* Go over all the slots and try to load one */
2940 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2941 if (active_slot != NO_ACTIVE_SLOT){
2942 /* A slot is already active, go to next image. */
2943 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002944 }
David Vincze505fba22020-10-22 13:53:29 +02002945
Mark Horvathccaf7f82021-01-04 18:16:42 +01002946 active_slot = find_slot_with_highest_version(state,
2947 slot_usage);
2948 if (active_slot == NO_ACTIVE_SLOT) {
2949 BOOT_LOG_INF("No slot to load for image %d",
2950 BOOT_CURR_IMG(state));
2951 FIH_RET(FIH_FAILURE);
2952 }
2953
2954 /* Save the number of the active slot. */
2955 slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
2956
2957#ifdef MCUBOOT_DIRECT_XIP
2958 rc = boot_rom_address_check(state, slot_usage);
2959 if (rc != 0) {
2960 /* The image is placed in an unsuitable slot. */
2961 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2962 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2963 continue;
2964 }
George Becksteind4d90f82021-05-11 02:00:00 -04002965
David Vincze505fba22020-10-22 13:53:29 +02002966#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002967 rc = boot_select_or_erase(state, slot_usage);
David Vincze505fba22020-10-22 13:53:29 +02002968 if (rc != 0) {
2969 /* The selected image slot has been erased. */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002970 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2971 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02002972 continue;
2973 }
2974#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02002975#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02002976
Tamas Banfe031092020-09-10 17:32:39 +02002977#ifdef MCUBOOT_RAM_LOAD
2978 /* Image is first loaded to RAM and authenticated there in order to
2979 * prevent TOCTOU attack during image copy. This could be applied
2980 * when loading images from external (untrusted) flash to internal
2981 * (trusted) RAM and image is authenticated before copying.
2982 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002983 rc = boot_load_image_to_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002984 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002985 /* Image cannot be ramloaded. */
2986 boot_remove_image_from_flash(state, active_slot);
2987 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2988 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02002989 continue;
Tamas Banfe031092020-09-10 17:32:39 +02002990 }
2991#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002992
2993 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
2994 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2995 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02002996#ifdef MCUBOOT_RAM_LOAD
Mark Horvathccaf7f82021-01-04 18:16:42 +01002997 boot_remove_image_from_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002998#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002999 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3000 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
3001 continue;
David Vincze505fba22020-10-22 13:53:29 +02003002 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003003
3004 /* Valid image loaded from a slot, go to next image. */
3005 break;
3006 }
3007 }
3008
3009 FIH_RET(FIH_SUCCESS);
3010}
3011
3012/**
3013 * Updates the security counter for the current image.
3014 *
3015 * @param state Boot loader status information.
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003016 * @param slot_usage Information about the active and available slots.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003017 *
3018 * @return 0 on success; nonzero on failure.
3019 */
3020static int
3021boot_update_hw_rollback_protection(struct boot_loader_state *state,
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003022 const struct slot_usage_t slot_usage[])
Mark Horvathccaf7f82021-01-04 18:16:42 +01003023{
3024#ifdef MCUBOOT_HW_ROLLBACK_PROT
3025 int rc;
3026
3027 /* Update the stored security counter with the newer (active) image's
3028 * security counter value.
3029 */
David Vincze1c456242021-06-29 15:25:24 +02003030#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003031 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3032 * security counter can be increased only after reboot, if the image
3033 * has been confirmed at runtime (the image_ok flag has been set).
3034 * This way a 'revert' can be performed when it's necessary.
3035 */
3036 if (slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003037#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003038 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
3039 slot_usage[BOOT_CURR_IMG(state)].active_slot,
3040 boot_img_hdr(state, slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003041 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003042 BOOT_LOG_ERR("Security counter update failed after image "
3043 "validation.");
3044 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003045 }
David Vincze1c456242021-06-29 15:25:24 +02003046#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003047 }
3048#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003049
Mark Horvathccaf7f82021-01-04 18:16:42 +01003050 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003051
Mark Horvathccaf7f82021-01-04 18:16:42 +01003052#else /* MCUBOOT_HW_ROLLBACK_PROT */
3053 (void) (state);
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003054 (void) (slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003055 return 0;
3056#endif
3057}
3058
3059fih_int
3060context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3061{
3062 struct slot_usage_t slot_usage[BOOT_IMAGE_NUMBER];
3063 int rc;
David Brown695e5912021-05-24 16:58:01 -06003064 fih_int fih_rc = fih_int_encode(0);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003065
3066 memset(state, 0, sizeof(struct boot_loader_state));
3067 memset(slot_usage, 0, sizeof(struct slot_usage_t) * BOOT_IMAGE_NUMBER);
3068
3069 rc = boot_get_slot_usage(state, slot_usage);
3070 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003071 goto out;
3072 }
3073
Mark Horvathccaf7f82021-01-04 18:16:42 +01003074#if (BOOT_IMAGE_NUMBER > 1)
3075 while (true) {
3076#endif
3077 FIH_CALL(boot_load_and_validate_images, fih_rc, state, slot_usage);
3078 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3079 goto out;
3080 }
3081
3082#if (BOOT_IMAGE_NUMBER > 1)
3083 rc = boot_verify_dependencies(state, slot_usage);
3084 if (rc != 0) {
3085 /* Dependency check failed for an image, it has been removed from
3086 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3087 * unavailable. Try to load an image from another slot.
3088 */
3089 continue;
3090 }
3091 /* Dependency check was successful. */
3092 break;
3093 }
3094#endif
3095
3096 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003097 rc = boot_update_hw_rollback_protection(state, slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003098 if (rc != 0) {
3099 goto out;
3100 }
3101
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003102 rc = boot_add_shared_data(state, slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003103 if (rc != 0) {
3104 goto out;
3105 }
3106 }
3107
3108 /* All image loaded successfully. */
3109#ifdef MCUBOOT_HAVE_LOGGING
3110 print_loaded_images(state, slot_usage);
3111#endif
3112
3113 fill_rsp(state, slot_usage, rsp);
3114
David Vinczee574f2d2020-07-10 11:42:03 +02003115out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003116 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003117
Mark Horvathccaf7f82021-01-04 18:16:42 +01003118 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3119 fih_rc = fih_int_encode(rc);
3120 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003121
Mark Horvathccaf7f82021-01-04 18:16:42 +01003122 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003123}
Tamas Banfe031092020-09-10 17:32:39 +02003124#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003125
3126/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003127 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003128 * appropriate, and tells you what address to boot from.
3129 *
3130 * @param rsp On success, indicates how booting should occur.
3131 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003132 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003133 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003134fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003135boot_go(struct boot_rsp *rsp)
3136{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003137 fih_int fih_rc = FIH_FAILURE;
3138 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3139 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003140}