blob: f216dca8616a7e2a7ec7c2519bcecf9969feedcf [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"
Marti Bolivarfd20c762017-02-07 16:52:50 -050047
Fabio Utzigba829042018-09-18 08:29:34 -030048#ifdef MCUBOOT_ENC_IMAGES
49#include "bootutil/enc_key.h"
50#endif
51
Carlos Falgueras García391b1972021-06-21 16:58:07 +020052#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
53#include <os/os_malloc.h>
54#endif
55
Fabio Utzigba1fbe62017-07-21 14:01:20 -030056#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030057
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020058BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010059
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040060static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080061
Fabio Utzigabec0732019-07-31 08:40:22 -030062#if (BOOT_IMAGE_NUMBER > 1)
63#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
64#else
65#define IMAGES_ITER(x)
66#endif
67
Mark Horvathccaf7f82021-01-04 18:16:42 +010068#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
69struct slot_usage_t {
70 /* Index of the slot chosen to be loaded */
71 uint32_t active_slot;
72 bool slot_available[BOOT_NUM_SLOTS];
David Vincze1c456242021-06-29 15:25:24 +020073#if defined(MCUBOOT_RAM_LOAD)
Mark Horvathccaf7f82021-01-04 18:16:42 +010074 /* Image destination and size for the active slot */
75 uint32_t img_dst;
76 uint32_t img_sz;
David Vincze1c456242021-06-29 15:25:24 +020077#elif defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +010078 /* Swap status for the active slot */
Carlos Falgueras Garcíaafb424d2021-06-21 17:05:44 +020079 struct boot_swap_state swap_state;
Mark Horvathccaf7f82021-01-04 18:16:42 +010080#endif
David Vincze1c456242021-06-29 15:25:24 +020081};
82#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +010083
Fabio Utzig10ee6482019-08-01 12:04:52 -030084/*
85 * This macro allows some control on the allocation of local variables.
86 * When running natively on a target, we don't want to allocated huge
87 * variables on the stack, so make them global instead. For the simulator
88 * we want to run as many threads as there are tests, and it's safer
89 * to just make those variables stack allocated.
90 */
91#if !defined(__BOOTSIM__)
92#define TARGET_STATIC static
93#else
94#define TARGET_STATIC
95#endif
96
David Vinczee574f2d2020-07-10 11:42:03 +020097static int
98boot_read_image_headers(struct boot_loader_state *state, bool require_all,
99 struct boot_status *bs)
100{
101 int rc;
102 int i;
103
104 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
105 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
106 if (rc != 0) {
107 /* If `require_all` is set, fail on any single fail, otherwise
108 * if at least the first slot's header was read successfully,
109 * then the boot loader can attempt a boot.
110 *
111 * Failure to read any headers is a fatal error.
112 */
113 if (i > 0 && !require_all) {
114 return 0;
115 } else {
116 return rc;
117 }
118 }
119 }
120
121 return 0;
122}
123
Mark Horvathccaf7f82021-01-04 18:16:42 +0100124/**
125 * Saves boot status and shared data for current image.
126 *
127 * @param state Boot loader status information.
128 * @param active_slot Index of the slot will be loaded for current image.
129 *
130 * @return 0 on success; nonzero on failure.
131 */
132static int
133boot_add_shared_data(struct boot_loader_state *state,
134 uint32_t active_slot)
135{
136#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
137 int rc;
138
139#ifdef MCUBOOT_MEASURED_BOOT
140 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
141 boot_img_hdr(state, active_slot),
142 BOOT_IMG_AREA(state, active_slot));
143 if (rc != 0) {
144 BOOT_LOG_ERR("Failed to add image data to shared area");
145 return rc;
146 }
147#endif /* MCUBOOT_MEASURED_BOOT */
148
149#ifdef MCUBOOT_DATA_SHARING
150 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
151 BOOT_IMG_AREA(state, active_slot));
152 if (rc != 0) {
153 BOOT_LOG_ERR("Failed to add data to shared memory area.");
154 return rc;
155 }
156#endif /* MCUBOOT_DATA_SHARING */
157
158 return 0;
159
160#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
161 (void) (state);
162 (void) (active_slot);
163
164 return 0;
165#endif
166}
167
168/**
169 * Fills rsp to indicate how booting should occur.
170 *
171 * @param state Boot loader status information.
172 * @param slot_usage Information about the active and available slots.
173 * Only used in MCUBOOT_DIRECT_XIP and MCUBOOT_RAM_LOAD
174 * @param rsp boot_rsp struct to fill.
175 */
176static void
177fill_rsp(struct boot_loader_state *state, void *slot_usage,
178 struct boot_rsp *rsp)
179{
180 uint32_t active_slot;
181
182#if (BOOT_IMAGE_NUMBER > 1)
183 /* Always boot from Image 0. */
184 BOOT_CURR_IMG(state) = 0;
185#endif
186
187#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
188 active_slot = ((struct slot_usage_t *)slot_usage)[BOOT_CURR_IMG(state)].active_slot;
189#else
190 (void) (slot_usage);
191 active_slot = BOOT_PRIMARY_SLOT;
192#endif
193
Dominik Ermel260ae092021-04-23 05:38:45 +0000194 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100195 rsp->br_image_off = boot_img_slot_off(state, active_slot);
196 rsp->br_hdr = boot_img_hdr(state, active_slot);
197}
198
199/**
200 * Closes all flash areas.
201 *
202 * @param state Boot loader status information.
203 */
204static void
205close_all_flash_areas(struct boot_loader_state *state)
206{
207 uint32_t slot;
208
209 IMAGES_ITER(BOOT_CURR_IMG(state)) {
210#if MCUBOOT_SWAP_USING_SCRATCH
211 flash_area_close(BOOT_SCRATCH_AREA(state));
212#endif
213 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
214 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
215 }
216 }
217}
218
Tamas Banfe031092020-09-10 17:32:39 +0200219#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600220/*
221 * Compute the total size of the given image. Includes the size of
222 * the TLVs.
223 */
Tamas Banfe031092020-09-10 17:32:39 +0200224#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600225static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300226boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600227{
228 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300229 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300230 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300231 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600232 int area_id;
233 int rc;
234
Fabio Utzig10ee6482019-08-01 12:04:52 -0300235#if (BOOT_IMAGE_NUMBER == 1)
236 (void)state;
237#endif
238
239 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600240 rc = flash_area_open(area_id, &fap);
241 if (rc != 0) {
242 rc = BOOT_EFLASH;
243 goto done;
244 }
245
Fabio Utzig61fd8882019-09-14 20:00:20 -0300246 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
247
248 if (flash_area_read(fap, off, &info, sizeof(info))) {
249 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600250 goto done;
251 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300252
Fabio Utzige52c08e2019-09-11 19:32:00 -0300253 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
254 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
255 if (protect_tlv_size != info.it_tlv_tot) {
256 rc = BOOT_EBADIMAGE;
257 goto done;
258 }
259
260 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
261 rc = BOOT_EFLASH;
262 goto done;
263 }
264 } else if (protect_tlv_size != 0) {
265 rc = BOOT_EBADIMAGE;
266 goto done;
267 }
268
Fabio Utzig61fd8882019-09-14 20:00:20 -0300269 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
270 rc = BOOT_EBADIMAGE;
271 goto done;
272 }
273
Fabio Utzige52c08e2019-09-11 19:32:00 -0300274 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600275 rc = 0;
276
277done:
278 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300279 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600280}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300281#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600282
Tamas Banfe031092020-09-10 17:32:39 +0200283#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700284static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300285boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800286{
David Brownab449182019-11-15 09:32:52 -0700287 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300288#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700289 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300290#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800291
292 /* Figure out what size to write update status update as. The size depends
293 * on what the minimum write size is for scratch area, active image slot.
294 * We need to use the bigger of those 2 values.
295 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300296 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300297#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300298 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800299 if (align > elem_sz) {
300 elem_sz = align;
301 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300302#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800303
304 return elem_sz;
305}
306
Fabio Utzig10ee6482019-08-01 12:04:52 -0300307static int
308boot_initialize_area(struct boot_loader_state *state, int flash_area)
309{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000310 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
311 boot_sector_t *out_sectors;
312 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300313 int rc;
314
315 num_sectors = BOOT_MAX_IMG_SECTORS;
316
317 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
318 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
319 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
320 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
321 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
322 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300323#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300324 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
325 out_sectors = state->scratch.sectors;
326 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300327#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300328 } else {
329 return BOOT_EFLASH;
330 }
331
Dominik Ermel51c8d762021-05-24 15:34:01 +0000332#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300333 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000334#else
335 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
336 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
337#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300338 if (rc != 0) {
339 return rc;
340 }
341 *out_num_sectors = num_sectors;
342 return 0;
343}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300344
Christopher Collins92ea77f2016-12-12 15:59:26 -0800345/**
346 * Determines the sector layout of both image slots and the scratch area.
347 * This information is necessary for calculating the number of bytes to erase
348 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300349 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350 */
351static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300354 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800355 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800356
Fabio Utzig10ee6482019-08-01 12:04:52 -0300357 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300358
359 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800360 if (rc != 0) {
361 return BOOT_EFLASH;
362 }
363
Fabio Utzig10ee6482019-08-01 12:04:52 -0300364 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200366 /* We need to differentiate from the primary image issue */
367 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368 }
369
Fabio Utzig12d59162019-11-28 10:01:59 -0300370#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300371 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200372 if (rc != 0) {
373 return BOOT_EFLASH;
374 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300375#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200376
Fabio Utzig10ee6482019-08-01 12:04:52 -0300377 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378
379 return 0;
380}
381
Fabio Utzig12d59162019-11-28 10:01:59 -0300382void
383boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384{
Fabio Utzig4741c452019-12-19 15:32:41 -0300385#ifdef MCUBOOT_ENC_IMAGES
386 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
387#if MCUBOOT_SWAP_SAVE_ENCTLV
388 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
389#endif
390#endif /* MCUBOOT_ENC_IMAGES */
391
392 bs->use_scratch = 0;
393 bs->swap_size = 0;
394 bs->source = 0;
395
Fabio Utzig74aef312019-11-28 11:05:34 -0300396 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300397 bs->idx = BOOT_STATUS_IDX_0;
398 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700399 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300400}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800401
Fabio Utzig12d59162019-11-28 10:01:59 -0300402bool
403boot_status_is_reset(const struct boot_status *bs)
404{
Fabio Utzig74aef312019-11-28 11:05:34 -0300405 return (bs->op == BOOT_STATUS_OP_MOVE &&
406 bs->idx == BOOT_STATUS_IDX_0 &&
407 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408}
409
410/**
411 * Writes the supplied boot status to the flash file system. The boot status
412 * contains the current state of an in-progress image copy operation.
413 *
414 * @param bs The boot status to write.
415 *
416 * @return 0 on success; nonzero on failure.
417 */
418int
Fabio Utzig12d59162019-11-28 10:01:59 -0300419boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800420{
421 const struct flash_area *fap;
422 uint32_t off;
423 int area_id;
424 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300425 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700426 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300427 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800428
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300429 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100430 * the trailer. Since in the last step the primary slot is erased, the
431 * first two status writes go to the scratch which will be copied to
432 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300433 */
434
Fabio Utzig12d59162019-11-28 10:01:59 -0300435#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300436 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800437 /* Write to scratch. */
438 area_id = FLASH_AREA_IMAGE_SCRATCH;
439 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300440#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100441 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300442 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300443#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300445#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446
447 rc = flash_area_open(area_id, &fap);
448 if (rc != 0) {
449 rc = BOOT_EFLASH;
450 goto done;
451 }
452
453 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300454 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200455 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300456 erased_val = flash_area_erased_val(fap);
457 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700458 buf[0] = bs->state;
459
460 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461 if (rc != 0) {
462 rc = BOOT_EFLASH;
463 goto done;
464 }
465
466 rc = 0;
467
468done:
469 flash_area_close(fap);
470 return rc;
471}
Tamas Banfe031092020-09-10 17:32:39 +0200472#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200473#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474
475/*
David Vinczec3084132020-02-18 14:50:47 +0100476 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800477 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100478static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300479boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
480 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800481{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300482 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300483 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300484 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100485 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300486
Fabio Utzig10ee6482019-08-01 12:04:52 -0300487#if (BOOT_IMAGE_NUMBER == 1)
488 (void)state;
489#endif
490
Fabio Utzigba829042018-09-18 08:29:34 -0300491 (void)bs;
492 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300493
494 image_index = BOOT_CURR_IMG(state);
495
496#ifdef MCUBOOT_ENC_IMAGES
497 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300498 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300499 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100500 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300501 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300502 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100503 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300504 }
505 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300506#endif
507
Raef Colese8fe6cf2020-05-26 13:07:40 +0100508 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
509 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300510
Raef Colese8fe6cf2020-05-26 13:07:40 +0100511 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800512}
513
Tamas Banfe031092020-09-10 17:32:39 +0200514#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100515static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800516split_image_check(struct image_header *app_hdr,
517 const struct flash_area *app_fap,
518 struct image_header *loader_hdr,
519 const struct flash_area *loader_fap)
520{
521 static void *tmpbuf;
522 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100523 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524
525 if (!tmpbuf) {
526 tmpbuf = malloc(BOOT_TMPBUF_SZ);
527 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100528 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800529 }
530 }
531
Raef Colese8fe6cf2020-05-26 13:07:40 +0100532 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
533 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
534 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
535 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536 }
537
Raef Colese8fe6cf2020-05-26 13:07:40 +0100538 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
539 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540
Raef Colese8fe6cf2020-05-26 13:07:40 +0100541out:
542 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800543}
Tamas Banfe031092020-09-10 17:32:39 +0200544#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545
Fabio Utzig338a19f2018-12-03 08:37:08 -0200546/*
David Brown9bf95af2019-10-10 15:36:36 -0600547 * Check that this is a valid header. Valid means that the magic is
548 * correct, and that the sizes/offsets are "sane". Sane means that
549 * there is no overflow on the arithmetic, and that the result fits
550 * within the flash area we are in.
551 */
552static bool
553boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
554{
555 uint32_t size;
556
557 if (hdr->ih_magic != IMAGE_MAGIC) {
558 return false;
559 }
560
561 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
562 return false;
563 }
564
Dominik Ermel260ae092021-04-23 05:38:45 +0000565 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600566 return false;
567 }
568
569 return true;
570}
571
572/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200573 * Check that a memory area consists of a given value.
574 */
575static inline bool
576boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300577{
578 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200579 uint8_t *p = (uint8_t *)data;
580 for (i = 0; i < len; i++) {
581 if (val != p[i]) {
582 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300583 }
584 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200585 return true;
586}
587
588static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300589boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200590{
591 const struct flash_area *fap;
592 struct image_header *hdr;
593 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300594 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200595 int rc;
596
Fabio Utzig10ee6482019-08-01 12:04:52 -0300597 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300598 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200599 if (rc != 0) {
600 return -1;
601 }
602
603 erased_val = flash_area_erased_val(fap);
604 flash_area_close(fap);
605
Fabio Utzig10ee6482019-08-01 12:04:52 -0300606 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200607 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
608 return -1;
609 }
610
611 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300612}
613
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000614#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200615 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200616 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000617 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
618/**
David Vincze8b0b6372020-05-20 19:54:44 +0200619 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000620 *
David Vincze8b0b6372020-05-20 19:54:44 +0200621 * @param ver1 Pointer to the first image version to compare.
622 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000623 *
David Vincze8b0b6372020-05-20 19:54:44 +0200624 * @retval -1 If ver1 is strictly less than ver2.
625 * @retval 0 If the image version numbers are equal,
626 * (not including the build number).
627 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000628 */
629static int
David Vincze8b0b6372020-05-20 19:54:44 +0200630boot_version_cmp(const struct image_version *ver1,
631 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000632{
David Vincze8b0b6372020-05-20 19:54:44 +0200633 if (ver1->iv_major > ver2->iv_major) {
634 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000635 }
David Vincze8b0b6372020-05-20 19:54:44 +0200636 if (ver1->iv_major < ver2->iv_major) {
637 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000638 }
David Vincze8b0b6372020-05-20 19:54:44 +0200639 /* The major version numbers are equal, continue comparison. */
640 if (ver1->iv_minor > ver2->iv_minor) {
641 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000642 }
David Vincze8b0b6372020-05-20 19:54:44 +0200643 if (ver1->iv_minor < ver2->iv_minor) {
644 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000645 }
David Vincze8b0b6372020-05-20 19:54:44 +0200646 /* The minor version numbers are equal, continue comparison. */
647 if (ver1->iv_revision > ver2->iv_revision) {
648 return 1;
649 }
650 if (ver1->iv_revision < ver2->iv_revision) {
651 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000652 }
653
654 return 0;
655}
656#endif
657
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000658#if defined(MCUBOOT_DIRECT_XIP)
659/**
660 * Check if image in slot has been set with specific ROM address to run from
661 * and whether the slot starts at that address.
662 *
663 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
664 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
665 * header matches the slot address;
666 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
667 * does not match the slot address.
668 */
669static bool
Mark Horvathccaf7f82021-01-04 18:16:42 +0100670boot_rom_address_check(struct boot_loader_state *state,
671 struct slot_usage_t slot_usage[])
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000672{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100673 uint32_t active_slot;
674 const struct image_header *hdr;
675 uint32_t f_off;
676
677 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
678 hdr = boot_img_hdr(state, active_slot);
679 f_off = boot_img_slot_off(state, active_slot);
680
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000681 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
682 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100683 ", skipping",
684 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000685 hdr->ih_load_addr);
686
687 /* If there is address mismatch, the image is not bootable from this
688 * slot.
689 */
690 return 1;
691 }
692 return 0;
693}
694#endif
695
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300696/*
697 * Check that there is a valid image in a slot
698 *
699 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100700 * FIH_SUCCESS if image was successfully validated
701 * 1 (or its fih_int encoded form) if no bootloable image was found
702 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300703 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100704static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300705boot_validate_slot(struct boot_loader_state *state, int slot,
706 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800707{
708 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400709 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300710 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100711 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300713
Fabio Utzig10ee6482019-08-01 12:04:52 -0300714 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300715 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800716 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100717 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718 }
719
Fabio Utzig10ee6482019-08-01 12:04:52 -0300720 hdr = boot_img_hdr(state, slot);
721 if (boot_check_header_erased(state, slot) == 0 ||
722 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300723
724#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
725 /*
726 * This fixes an issue where an image might be erased, but a trailer
727 * be left behind. It can happen if the image is in the secondary slot
728 * and did not pass validation, in which case the whole slot is erased.
729 * If during the erase operation, a reset occurs, parts of the slot
730 * might have been erased while some did not. The concerning part is
731 * the trailer because it might disable a new image from being loaded
732 * through mcumgr; so we just get rid of the trailer here, if the header
733 * is erased.
734 */
735 if (slot != BOOT_PRIMARY_SLOT) {
736 swap_erase_trailer_sectors(state, fap);
737 }
738#endif
739
David Vincze2d736ad2019-02-18 11:50:22 +0100740 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100741 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200742 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300743 }
744
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000745#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
746 if (slot != BOOT_PRIMARY_SLOT) {
747 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200748 rc = boot_version_cmp(
749 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
750 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
751 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000752 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000753 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000754 /* Image in the secondary slot does not satisfy version requirement.
755 * Erase the image and continue booting from the primary slot.
756 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100757 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000758 goto out;
759 }
760 }
761#endif
762
Raef Colese8fe6cf2020-05-26 13:07:40 +0100763 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
764 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200765 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000766 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200767 /* Image is invalid, erase it to prevent further unnecessary
768 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700769 */
770 }
David Brown098de832019-12-10 11:58:01 -0700771#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100772 BOOT_LOG_ERR("Image in the %s slot is not valid!",
773 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700774#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100775 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200776 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800777 }
778
Fabio Utzig338a19f2018-12-03 08:37:08 -0200779out:
780 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100781
782 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800783}
784
David Vinczec3084132020-02-18 14:50:47 +0100785#ifdef MCUBOOT_HW_ROLLBACK_PROT
786/**
787 * Updates the stored security counter value with the image's security counter
788 * value which resides in the given slot, only if it's greater than the stored
789 * value.
790 *
791 * @param image_index Index of the image to determine which security
792 * counter to update.
793 * @param slot Slot number of the image.
794 * @param hdr Pointer to the image header structure of the image
795 * that is currently stored in the given slot.
796 *
797 * @return 0 on success; nonzero on failure.
798 */
799static int
800boot_update_security_counter(uint8_t image_index, int slot,
801 struct image_header *hdr)
802{
803 const struct flash_area *fap = NULL;
804 uint32_t img_security_cnt;
805 int rc;
806
807 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
808 &fap);
809 if (rc != 0) {
810 rc = BOOT_EFLASH;
811 goto done;
812 }
813
814 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
815 if (rc != 0) {
816 goto done;
817 }
818
819 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
820 if (rc != 0) {
821 goto done;
822 }
823
824done:
825 flash_area_close(fap);
826 return rc;
827}
828#endif /* MCUBOOT_HW_ROLLBACK_PROT */
829
Tamas Banfe031092020-09-10 17:32:39 +0200830#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200831/**
832 * Determines which swap operation to perform, if any. If it is determined
833 * that a swap operation is required, the image in the secondary slot is checked
834 * for validity. If the image in the secondary slot is invalid, it is erased,
835 * and a swap type of "none" is indicated.
836 *
837 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
838 */
839static int
840boot_validated_swap_type(struct boot_loader_state *state,
841 struct boot_status *bs)
842{
843 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100844 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200845
846 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
847 if (BOOT_IS_UPGRADE(swap_type)) {
848 /* Boot loader wants to switch to the secondary slot.
849 * Ensure image is valid.
850 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100851 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
852 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
853 if (fih_eq(fih_rc, fih_int_encode(1))) {
854 swap_type = BOOT_SWAP_TYPE_NONE;
855 } else {
856 swap_type = BOOT_SWAP_TYPE_FAIL;
857 }
David Vinczee574f2d2020-07-10 11:42:03 +0200858 }
859 }
860
861 return swap_type;
862}
David Brown94ed12c2021-05-26 16:28:14 -0600863#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200864
Christopher Collins92ea77f2016-12-12 15:59:26 -0800865/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800866 * Erases a region of flash.
867 *
Fabio Utzigba829042018-09-18 08:29:34 -0300868 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800869 * @param off The offset within the flash area to start the
870 * erase.
871 * @param sz The number of bytes to erase.
872 *
873 * @return 0 on success; nonzero on failure.
874 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300875int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300876boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877{
Fabio Utzigba829042018-09-18 08:29:34 -0300878 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879}
880
David Brown94ed12c2021-05-26 16:28:14 -0600881#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800882/**
883 * Copies the contents of one flash region to another. You must erase the
884 * destination region prior to calling this function.
885 *
886 * @param flash_area_id_src The ID of the source flash area.
887 * @param flash_area_id_dst The ID of the destination flash area.
888 * @param off_src The offset within the source flash area to
889 * copy from.
890 * @param off_dst The offset within the destination flash area to
891 * copy to.
892 * @param sz The number of bytes to copy.
893 *
894 * @return 0 on success; nonzero on failure.
895 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300896int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300897boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300898 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300899 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900 uint32_t off_src, uint32_t off_dst, uint32_t sz)
901{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902 uint32_t bytes_copied;
903 int chunk_sz;
904 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300905#ifdef MCUBOOT_ENC_IMAGES
906 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300907 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300908 size_t blk_off;
909 struct image_header *hdr;
910 uint16_t idx;
911 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300912 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300913#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800914
Marek Pietae51ec072021-07-15 14:53:10 +0200915 TARGET_STATIC uint8_t buf[1024] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300916
917#if !defined(MCUBOOT_ENC_IMAGES)
918 (void)state;
919#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800920
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921 bytes_copied = 0;
922 while (bytes_copied < sz) {
923 if (sz - bytes_copied > sizeof buf) {
924 chunk_sz = sizeof buf;
925 } else {
926 chunk_sz = sz - bytes_copied;
927 }
928
929 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
930 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300931 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800932 }
933
Fabio Utzigba829042018-09-18 08:29:34 -0300934#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300935 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000936 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
937 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
938 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
939 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100940 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300941 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300942#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300943 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000944 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100945 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300946 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300947 off = off_dst;
948 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300949#else
950 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000951 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300952 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
953 }
954#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200955 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300956 blk_sz = chunk_sz;
957 idx = 0;
958 if (off + bytes_copied < hdr->ih_hdr_size) {
959 /* do not decrypt header */
960 blk_off = 0;
961 blk_sz = chunk_sz - hdr->ih_hdr_size;
962 idx = hdr->ih_hdr_size;
963 } else {
964 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
965 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300966 tlv_off = BOOT_TLV_OFF(hdr);
967 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300968 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300969 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300970 blk_sz = 0;
971 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300972 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300973 }
974 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300975 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300976 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
977 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300978 }
979 }
980#endif
981
Christopher Collins92ea77f2016-12-12 15:59:26 -0800982 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
983 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300984 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985 }
986
987 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300988
989 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800990 }
991
Fabio Utzigba829042018-09-18 08:29:34 -0300992 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800993}
994
Christopher Collins92ea77f2016-12-12 15:59:26 -0800995/**
David Vincze2d736ad2019-02-18 11:50:22 +0100996 * Overwrite primary slot with the image contained in the secondary slot.
997 * If a prior copy operation was interrupted by a system reset, this function
998 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800999 *
1000 * @param bs The current boot status. This function reads
1001 * this struct to determine if it is resuming
1002 * an interrupted swap operation. This
1003 * function writes the updated status to this
1004 * function on return.
1005 *
1006 * @return 0 on success; nonzero on failure.
1007 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001008#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001009static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001010boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001011{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001012 size_t sect_count;
1013 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001014 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001015 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001016 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001017 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001018 const struct flash_area *fap_primary_slot;
1019 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001020 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001021
Fabio Utzigb4f88102020-10-04 10:16:24 -03001022#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1023 uint32_t sector;
1024 uint32_t trailer_sz;
1025 uint32_t off;
1026 uint32_t sz;
1027#endif
1028
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001029 (void)bs;
1030
Fabio Utzig13d9e352017-10-05 20:32:31 -03001031#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1032 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001033 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001034 assert(rc == 0);
1035#endif
David Brown17609d82017-05-05 09:41:34 -06001036
David Vincze2d736ad2019-02-18 11:50:22 +01001037 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1038 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001039
Fabio Utzigb0f04732019-07-31 09:49:19 -03001040 image_index = BOOT_CURR_IMG(state);
1041
1042 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1043 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001044 assert (rc == 0);
1045
Fabio Utzigb0f04732019-07-31 09:49:19 -03001046 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1047 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001048 assert (rc == 0);
1049
Fabio Utzig10ee6482019-08-01 12:04:52 -03001050 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001051 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001052 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001053 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001054 assert(rc == 0);
1055
Fabio Utzig13d9e352017-10-05 20:32:31 -03001056#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001057 if ((size + this_size) >= src_size) {
1058 size += src_size - size;
1059 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001060 break;
1061 }
1062#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001063
1064 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001065 }
1066
Fabio Utzigb4f88102020-10-04 10:16:24 -03001067#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1068 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1069 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1070 sz = 0;
1071 do {
1072 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1073 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1074 sector--;
1075 } while (sz < trailer_sz);
1076
1077 rc = boot_erase_region(fap_primary_slot, off, sz);
1078 assert(rc == 0);
1079#endif
1080
Fabio Utzigba829042018-09-18 08:29:34 -03001081#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001082 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001083 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001084 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001085 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001086
Fabio Utzigba829042018-09-18 08:29:34 -03001087 if (rc < 0) {
1088 return BOOT_EBADIMAGE;
1089 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001090 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001091 return BOOT_EBADIMAGE;
1092 }
1093 }
1094#endif
1095
David Vincze2d736ad2019-02-18 11:50:22 +01001096 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1097 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001098 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001099 if (rc != 0) {
1100 return rc;
1101 }
1102
1103#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1104 rc = boot_write_magic(fap_primary_slot);
1105 if (rc != 0) {
1106 return rc;
1107 }
1108#endif
David Brown17609d82017-05-05 09:41:34 -06001109
David Vinczec3084132020-02-18 14:50:47 +01001110#ifdef MCUBOOT_HW_ROLLBACK_PROT
1111 /* Update the stored security counter with the new image's security counter
1112 * value. Both slots hold the new image at this point, but the secondary
1113 * slot's image header must be passed since the image headers in the
1114 * boot_data structure have not been updated yet.
1115 */
1116 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1117 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1118 if (rc != 0) {
1119 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1120 return rc;
1121 }
1122#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1123
Fabio Utzig13d9e352017-10-05 20:32:31 -03001124 /*
1125 * Erases header and trailer. The trailer is erased because when a new
1126 * image is written without a trailer as is the case when using newt, the
1127 * trailer that was left might trigger a new upgrade.
1128 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001129 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001130 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001131 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1132 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001133 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001134 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001135 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001136 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001137 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1138 last_sector),
1139 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1140 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001141 assert(rc == 0);
1142
David Vincze2d736ad2019-02-18 11:50:22 +01001143 flash_area_close(fap_primary_slot);
1144 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001145
David Vincze2d736ad2019-02-18 11:50:22 +01001146 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001147
1148 return 0;
1149}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001150#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001151
Christopher Collinsa1c12042019-05-23 14:00:28 -07001152#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001153/**
1154 * Swaps the two images in flash. If a prior copy operation was interrupted
1155 * by a system reset, this function completes that operation.
1156 *
1157 * @param bs The current boot status. This function reads
1158 * this struct to determine if it is resuming
1159 * an interrupted swap operation. This
1160 * function writes the updated status to this
1161 * function on return.
1162 *
1163 * @return 0 on success; nonzero on failure.
1164 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001165static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001166boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001167{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001168 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001169#ifdef MCUBOOT_ENC_IMAGES
1170 const struct flash_area *fap;
1171 uint8_t slot;
1172 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001173#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001174 uint32_t size;
1175 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001176 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001177 int rc;
1178
1179 /* FIXME: just do this if asked by user? */
1180
1181 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001182 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001183
Fabio Utzig12d59162019-11-28 10:01:59 -03001184 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001185 /*
1186 * No swap ever happened, so need to find the largest image which
1187 * will be used to determine the amount of sectors to swap.
1188 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001189 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001190 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001191 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001192 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001193 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001194
Fabio Utzigba829042018-09-18 08:29:34 -03001195#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001196 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001197 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001198 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001199 assert(rc >= 0);
1200
1201 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001202 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001203 assert(rc == 0);
1204 } else {
1205 rc = 0;
1206 }
1207 } else {
1208 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1209 }
1210#endif
1211
Fabio Utzig10ee6482019-08-01 12:04:52 -03001212 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001213 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001214 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001215 assert(rc == 0);
1216 }
1217
Fabio Utzigba829042018-09-18 08:29:34 -03001218#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001219 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001220 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001221 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001222 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001223 assert(rc >= 0);
1224
1225 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001226 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001227 assert(rc == 0);
1228 } else {
1229 rc = 0;
1230 }
1231 } else {
1232 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1233 }
1234#endif
1235
Fabio Utzig46490722017-09-04 15:34:32 -03001236 if (size > copy_size) {
1237 copy_size = size;
1238 }
1239
1240 bs->swap_size = copy_size;
1241 } else {
1242 /*
1243 * If a swap was under way, the swap_size should already be present
1244 * in the trailer...
1245 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001246 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001247 assert(rc == 0);
1248
1249 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001250
1251#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001252 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1253 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001254 assert(rc == 0);
1255
1256 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001257 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001258 break;
1259 }
1260 }
1261
1262 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001263 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001264 }
1265 }
1266#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001267 }
1268
Fabio Utzig12d59162019-11-28 10:01:59 -03001269 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001270
David Vincze2d736ad2019-02-18 11:50:22 +01001271#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001272 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001273 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001274 BOOT_LOG_WRN("%d status write fails performing the swap",
1275 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001276 }
1277#endif
1278
Christopher Collins92ea77f2016-12-12 15:59:26 -08001279 return 0;
1280}
David Brown17609d82017-05-05 09:41:34 -06001281#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001282
David Vinczee32483f2019-06-13 10:46:24 +02001283#if (BOOT_IMAGE_NUMBER > 1)
1284/**
1285 * Check the image dependency whether it is satisfied and modify
1286 * the swap type if necessary.
1287 *
1288 * @param dep Image dependency which has to be verified.
1289 *
1290 * @return 0 on success; nonzero on failure.
1291 */
1292static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001293boot_verify_slot_dependency(struct boot_loader_state *state,
1294 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001295{
1296 struct image_version *dep_version;
1297 size_t dep_slot;
1298 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001299 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001300
1301 /* Determine the source of the image which is the subject of
1302 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001303 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001304 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1305 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001306 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001307
David Vincze8b0b6372020-05-20 19:54:44 +02001308 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1309 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001310 /* Dependency not satisfied.
1311 * Modify the swap type to decrease the version number of the image
1312 * (which will be located in the primary slot after the boot process),
1313 * consequently the number of unsatisfied dependencies will be
1314 * decreased or remain the same.
1315 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001316 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001317 case BOOT_SWAP_TYPE_TEST:
1318 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001319 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001320 break;
1321 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001322 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001323 break;
1324 default:
1325 break;
1326 }
David Vincze8b0b6372020-05-20 19:54:44 +02001327 } else {
1328 /* Dependency satisfied. */
1329 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001330 }
1331
1332 return rc;
1333}
1334
1335/**
1336 * Read all dependency TLVs of an image from the flash and verify
1337 * one after another to see if they are all satisfied.
1338 *
1339 * @param slot Image slot number.
1340 *
1341 * @return 0 on success; nonzero on failure.
1342 */
1343static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001344boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001345{
1346 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001347 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001348 struct image_dependency dep;
1349 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001350 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001351 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001352 int rc;
1353
Fabio Utzig10ee6482019-08-01 12:04:52 -03001354 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001355 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001356 if (rc != 0) {
1357 rc = BOOT_EFLASH;
1358 goto done;
1359 }
1360
Fabio Utzig61fd8882019-09-14 20:00:20 -03001361 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1362 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001363 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001364 goto done;
1365 }
1366
Fabio Utzig61fd8882019-09-14 20:00:20 -03001367 while (true) {
1368 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1369 if (rc < 0) {
1370 return -1;
1371 } else if (rc > 0) {
1372 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001373 break;
1374 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001375
1376 if (len != sizeof(dep)) {
1377 rc = BOOT_EBADIMAGE;
1378 goto done;
1379 }
1380
1381 rc = flash_area_read(fap, off, &dep, len);
1382 if (rc != 0) {
1383 rc = BOOT_EFLASH;
1384 goto done;
1385 }
1386
1387 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1388 rc = BOOT_EBADARGS;
1389 goto done;
1390 }
1391
1392 /* Verify dependency and modify the swap type if not satisfied. */
1393 rc = boot_verify_slot_dependency(state, &dep);
1394 if (rc != 0) {
1395 /* Dependency not satisfied. */
1396 goto done;
1397 }
David Vinczee32483f2019-06-13 10:46:24 +02001398 }
1399
1400done:
1401 flash_area_close(fap);
1402 return rc;
1403}
1404
1405/**
David Vinczee32483f2019-06-13 10:46:24 +02001406 * Iterate over all the images and verify whether the image dependencies in the
1407 * TLV area are all satisfied and update the related swap type if necessary.
1408 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001409static int
1410boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001411{
Erik Johnson49063752020-02-06 09:59:31 -06001412 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001413 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001414
Fabio Utzig10ee6482019-08-01 12:04:52 -03001415 BOOT_CURR_IMG(state) = 0;
1416 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001417 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1418 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1419 slot = BOOT_SECONDARY_SLOT;
1420 } else {
1421 slot = BOOT_PRIMARY_SLOT;
1422 }
1423
1424 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001425 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001426 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001427 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001428 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001429 /* Cannot upgrade due to non-met dependencies, so disable all
1430 * image upgrades.
1431 */
1432 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1433 BOOT_CURR_IMG(state) = idx;
1434 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1435 }
1436 break;
David Vinczee32483f2019-06-13 10:46:24 +02001437 }
1438 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001439 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001440}
1441#endif /* (BOOT_IMAGE_NUMBER > 1) */
1442
Christopher Collins92ea77f2016-12-12 15:59:26 -08001443/**
David Vinczeba3bd602019-06-17 16:01:43 +02001444 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001445 *
David Vinczeba3bd602019-06-17 16:01:43 +02001446 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001447 *
1448 * @return 0 on success; nonzero on failure.
1449 */
1450static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001451boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001452{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001453 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001454#ifndef MCUBOOT_OVERWRITE_ONLY
1455 uint8_t swap_type;
1456#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001457
David Vinczeba3bd602019-06-17 16:01:43 +02001458 /* At this point there are no aborted swaps. */
1459#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001460 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001461#elif defined(MCUBOOT_BOOTSTRAP)
1462 /* Check if the image update was triggered by a bad image in the
1463 * primary slot (the validity of the image in the secondary slot had
1464 * already been checked).
1465 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001466 fih_int fih_rc = FIH_FAILURE;
1467 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1468 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1469 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001470 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001471 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001472 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001473 }
1474#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001475 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001476#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001477 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001478
1479#ifndef MCUBOOT_OVERWRITE_ONLY
1480 /* The following state needs image_ok be explicitly set after the
1481 * swap was finished to avoid a new revert.
1482 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001483 swap_type = BOOT_SWAP_TYPE(state);
1484 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1485 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001486 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001487 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001488 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001489 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001490 }
1491
David Vinczec3084132020-02-18 14:50:47 +01001492#ifdef MCUBOOT_HW_ROLLBACK_PROT
1493 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1494 /* Update the stored security counter with the new image's security
1495 * counter value. The primary slot holds the new image at this point,
1496 * but the secondary slot's image header must be passed since image
1497 * headers in the boot_data structure have not been updated yet.
1498 *
1499 * In case of a permanent image swap mcuboot will never attempt to
1500 * revert the images on the next reboot. Therefore, the security
1501 * counter must be increased right after the image upgrade.
1502 */
1503 rc = boot_update_security_counter(
1504 BOOT_CURR_IMG(state),
1505 BOOT_PRIMARY_SLOT,
1506 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1507 if (rc != 0) {
1508 BOOT_LOG_ERR("Security counter update failed after "
1509 "image upgrade.");
1510 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1511 }
1512 }
1513#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1514
Fabio Utzige575b0b2019-09-11 12:34:23 -03001515 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001516 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001517 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001518 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001519 }
David Vinczeba3bd602019-06-17 16:01:43 +02001520 }
1521#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001522
David Vinczeba3bd602019-06-17 16:01:43 +02001523 return rc;
1524}
1525
1526/**
1527 * Completes a previously aborted image swap.
1528 *
1529 * @param bs The current boot status.
1530 *
1531 * @return 0 on success; nonzero on failure.
1532 */
1533#if !defined(MCUBOOT_OVERWRITE_ONLY)
1534static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001535boot_complete_partial_swap(struct boot_loader_state *state,
1536 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001537{
1538 int rc;
1539
1540 /* Determine the type of swap operation being resumed from the
1541 * `swap-type` trailer field.
1542 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001544 assert(rc == 0);
1545
Fabio Utzig10ee6482019-08-01 12:04:52 -03001546 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001547
1548 /* The following states need image_ok be explicitly set after the
1549 * swap was finished to avoid a new revert.
1550 */
1551 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1552 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001553 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001554 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001555 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001556 }
1557 }
1558
Fabio Utzige575b0b2019-09-11 12:34:23 -03001559 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001560 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001561 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001562 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001563 }
1564 }
1565
Fabio Utzig10ee6482019-08-01 12:04:52 -03001566 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001567 BOOT_LOG_ERR("panic!");
1568 assert(0);
1569
1570 /* Loop forever... */
1571 while (1) {}
1572 }
1573
1574 return rc;
1575}
1576#endif /* !MCUBOOT_OVERWRITE_ONLY */
1577
1578#if (BOOT_IMAGE_NUMBER > 1)
1579/**
1580 * Review the validity of previously determined swap types of other images.
1581 *
1582 * @param aborted_swap The current image upgrade is a
1583 * partial/aborted swap.
1584 */
1585static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001586boot_review_image_swap_types(struct boot_loader_state *state,
1587 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001588{
1589 /* In that case if we rebooted in the middle of an image upgrade process, we
1590 * must review the validity of swap types, that were previously determined
1591 * for other images. The image_ok flag had not been set before the reboot
1592 * for any of the updated images (only the copy_done flag) and thus falsely
1593 * the REVERT swap type has been determined for the previous images that had
1594 * been updated before the reboot.
1595 *
1596 * There are two separate scenarios that we have to deal with:
1597 *
1598 * 1. The reboot has happened during swapping an image:
1599 * The current image upgrade has been determined as a
1600 * partial/aborted swap.
1601 * 2. The reboot has happened between two separate image upgrades:
1602 * In this scenario we must check the swap type of the current image.
1603 * In those cases if it is NONE or REVERT we cannot certainly determine
1604 * the fact of a reboot. In a consistent state images must move in the
1605 * same direction or stay in place, e.g. in practice REVERT and TEST
1606 * swap types cannot be present at the same time. If the swap type of
1607 * the current image is either TEST, PERM or FAIL we must review the
1608 * already determined swap types of other images and set each false
1609 * REVERT swap types to NONE (these images had been successfully
1610 * updated before the system rebooted between two separate image
1611 * upgrades).
1612 */
1613
Fabio Utzig10ee6482019-08-01 12:04:52 -03001614 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001615 /* Nothing to do */
1616 return;
1617 }
1618
1619 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001620 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1621 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001622 /* Nothing to do */
1623 return;
1624 }
1625 }
1626
Fabio Utzig10ee6482019-08-01 12:04:52 -03001627 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1628 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1629 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001630 }
1631 }
1632}
1633#endif
1634
1635/**
1636 * Prepare image to be updated if required.
1637 *
1638 * Prepare image to be updated if required with completing an image swap
1639 * operation if one was aborted and/or determining the type of the
1640 * swap operation. In case of any error set the swap type to NONE.
1641 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001642 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001643 * @param bs Pointer where the read and possibly updated
1644 * boot status can be written to.
1645 */
1646static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001647boot_prepare_image_for_update(struct boot_loader_state *state,
1648 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001649{
1650 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001651 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001652
1653 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001654 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001655 if (rc != 0) {
1656 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1657 " - too small?", BOOT_MAX_IMG_SECTORS);
1658 /* Unable to determine sector layout, continue with next image
1659 * if there is one.
1660 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001661 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001662 if (rc == BOOT_EFLASH)
1663 {
1664 /* Only return on error from the primary image flash */
1665 return;
1666 }
David Vinczeba3bd602019-06-17 16:01:43 +02001667 }
1668
1669 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001670 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001671 if (rc != 0) {
1672 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001673 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001674 BOOT_CURR_IMG(state));
1675 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001676 return;
1677 }
1678
1679 /* If the current image's slots aren't compatible, no swap is possible.
1680 * Just boot into primary slot.
1681 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001682 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001683 boot_status_reset(bs);
1684
1685#ifndef MCUBOOT_OVERWRITE_ONLY
1686 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001687 if (rc != 0) {
1688 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001689 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001690 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001691 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001692 return;
1693 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001694#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001695
Fabio Utzig74aef312019-11-28 11:05:34 -03001696#ifdef MCUBOOT_SWAP_USING_MOVE
1697 /*
1698 * Must re-read image headers because the boot status might
1699 * have been updated in the previous function call.
1700 */
1701 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001702#ifdef MCUBOOT_BOOTSTRAP
1703 /* When bootstrapping it's OK to not have image magic in the primary slot */
1704 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1705 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1706#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001707 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001708#endif
1709
Fabio Utzig74aef312019-11-28 11:05:34 -03001710 /* Continue with next image if there is one. */
1711 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1712 BOOT_CURR_IMG(state));
1713 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1714 return;
1715 }
1716#endif
1717
David Vinczeba3bd602019-06-17 16:01:43 +02001718 /* Determine if we rebooted in the middle of an image swap
1719 * operation. If a partial swap was detected, complete it.
1720 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001721 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001722
1723#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001725#endif
1726
1727#ifdef MCUBOOT_OVERWRITE_ONLY
1728 /* Should never arrive here, overwrite-only mode has
1729 * no swap state.
1730 */
1731 assert(0);
1732#else
1733 /* Determine the type of swap operation being resumed from the
1734 * `swap-type` trailer field.
1735 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001736 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001737 assert(rc == 0);
1738#endif
1739 /* Attempt to read an image header from each slot. Ensure that
1740 * image headers in slots are aligned with headers in boot_data.
1741 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001742 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001743 assert(rc == 0);
1744
1745 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001746 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001747 } else {
1748 /* There was no partial swap, determine swap type. */
1749 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001750 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001751 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001752 FIH_CALL(boot_validate_slot, fih_rc,
1753 state, BOOT_SECONDARY_SLOT, bs);
1754 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1755 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1756 } else {
1757 BOOT_SWAP_TYPE(state) = bs->swap_type;
1758 }
David Vinczeba3bd602019-06-17 16:01:43 +02001759 }
1760
1761#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001762 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001763#endif
1764
1765#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001766 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001767 /* Header checks are done first because they are
1768 * inexpensive. Since overwrite-only copies starting from
1769 * offset 0, if interrupted, it might leave a valid header
1770 * magic, so also run validation on the primary slot to be
1771 * sure it's not OK.
1772 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001773 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1774 FIH_CALL(boot_validate_slot, fih_rc,
1775 state, BOOT_PRIMARY_SLOT, bs);
1776
1777 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1778
Fabio Utzig3d77c952020-10-04 10:23:17 -03001779 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001780 FIH_CALL(boot_validate_slot, fih_rc,
1781 state, BOOT_SECONDARY_SLOT, bs);
1782
Fabio Utzig3d77c952020-10-04 10:23:17 -03001783 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001784 /* Set swap type to REVERT to overwrite the primary
1785 * slot with the image contained in secondary slot
1786 * and to trigger the explicit setting of the
1787 * image_ok flag.
1788 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001789 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001790 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001791 }
1792 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001793#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001794 }
David Vinczeba3bd602019-06-17 16:01:43 +02001795 } else {
1796 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001797 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001798 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001799}
1800
Mark Horvathccaf7f82021-01-04 18:16:42 +01001801/**
1802 * Updates the security counter for the current image.
1803 *
1804 * @param state Boot loader status information.
1805 *
1806 * @return 0 on success; nonzero on failure.
1807 */
1808static int
1809boot_update_hw_rollback_protection(struct boot_loader_state *state)
1810{
1811#ifdef MCUBOOT_HW_ROLLBACK_PROT
1812 int rc;
1813
1814 /* Update the stored security counter with the active image's security
1815 * counter value. It will only be updated if the new security counter is
1816 * greater than the stored value.
1817 *
1818 * In case of a successful image swapping when the swap type is TEST the
1819 * security counter can be increased only after a reset, when the swap
1820 * type is NONE and the image has marked itself "OK" (the image_ok flag
1821 * has been set). This way a "revert" can be performed when it's
1822 * necessary.
1823 */
1824 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1825 rc = boot_update_security_counter(
1826 BOOT_CURR_IMG(state),
1827 BOOT_PRIMARY_SLOT,
1828 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1829 if (rc != 0) {
1830 BOOT_LOG_ERR("Security counter update failed after image "
1831 "validation.");
1832 return rc;
1833 }
1834 }
1835
1836 return 0;
1837
1838#else /* MCUBOOT_HW_ROLLBACK_PROT */
1839 (void) (state);
1840
1841 return 0;
1842#endif
1843}
1844
Raef Colese8fe6cf2020-05-26 13:07:40 +01001845fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001846context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001847{
Marti Bolivar84898652017-06-13 17:20:22 -04001848 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001849 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001850 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001851 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001852 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001853 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001854 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001855
1856 /* The array of slot sectors are defined here (as opposed to file scope) so
1857 * that they don't get allocated for non-boot-loader apps. This is
1858 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001859 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001860 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001861 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1862 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001863#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001864 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001865#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001866
Fabio Utzig10ee6482019-08-01 12:04:52 -03001867 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001868 has_upgrade = false;
1869
1870#if (BOOT_IMAGE_NUMBER == 1)
1871 (void)has_upgrade;
1872#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001873
David Vinczeba3bd602019-06-17 16:01:43 +02001874 /* Iterate over all the images. By the end of the loop the swap type has
1875 * to be determined for each image and all aborted swaps have to be
1876 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001877 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001878 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001879
David Vinczeba3bd602019-06-17 16:01:43 +02001880#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1881 /* The keys used for encryption may no longer be valid (could belong to
1882 * another images). Therefore, mark them as invalid to force their reload
1883 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001884 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001885 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001886#endif
1887
Fabio Utzig10ee6482019-08-01 12:04:52 -03001888 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001889
Fabio Utzig10ee6482019-08-01 12:04:52 -03001890 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001891 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001892 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001893 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001894#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001895 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001896#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001897
1898 /* Open primary and secondary image areas for the duration
1899 * of this call.
1900 */
1901 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001902 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001903 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001904 assert(rc == 0);
1905 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001906#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001907 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001908 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001909 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001910#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001911
1912 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001913 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001914
Fabio Utzige575b0b2019-09-11 12:34:23 -03001915 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001916 has_upgrade = true;
1917 }
David Vinczeba3bd602019-06-17 16:01:43 +02001918 }
1919
David Vinczee32483f2019-06-13 10:46:24 +02001920#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001921 if (has_upgrade) {
1922 /* Iterate over all the images and verify whether the image dependencies
1923 * are all satisfied and update swap type if necessary.
1924 */
1925 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001926 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001927 /*
1928 * It was impossible to upgrade because the expected dependency version
1929 * was not available. Here we already changed the swap_type so that
1930 * instead of asserting the bootloader, we continue and no upgrade is
1931 * performed.
1932 */
1933 rc = 0;
1934 }
1935 }
David Vinczee32483f2019-06-13 10:46:24 +02001936#endif
1937
David Vinczeba3bd602019-06-17 16:01:43 +02001938 /* Iterate over all the images. At this point there are no aborted swaps
1939 * and the swap types are determined for each image. By the end of the loop
1940 * all required update operations will have been finished.
1941 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001942 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001943
1944#if (BOOT_IMAGE_NUMBER > 1)
1945#ifdef MCUBOOT_ENC_IMAGES
1946 /* The keys used for encryption may no longer be valid (could belong to
1947 * another images). Therefore, mark them as invalid to force their reload
1948 * by boot_enc_load().
1949 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001950 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001951#endif /* MCUBOOT_ENC_IMAGES */
1952
1953 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001954 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001955#endif /* (BOOT_IMAGE_NUMBER > 1) */
1956
1957 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001958 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001959
Fabio Utzig10ee6482019-08-01 12:04:52 -03001960 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001961 case BOOT_SWAP_TYPE_NONE:
1962 break;
1963
1964 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1965 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1966 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001967 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001968 assert(rc == 0);
1969 break;
1970
1971 case BOOT_SWAP_TYPE_FAIL:
1972 /* The image in secondary slot was invalid and is now erased. Ensure
1973 * we don't try to boot into it again on the next reboot. Do this by
1974 * pretending we just reverted back to primary slot.
1975 */
1976#ifndef MCUBOOT_OVERWRITE_ONLY
1977 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001978 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001979 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001980 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001981 }
1982#endif /* !MCUBOOT_OVERWRITE_ONLY */
1983 break;
1984
1985 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001986 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001987 }
1988
Fabio Utzig10ee6482019-08-01 12:04:52 -03001989 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001990 BOOT_LOG_ERR("panic!");
1991 assert(0);
1992
1993 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001994 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001995 }
1996 }
1997
1998 /* Iterate over all the images. At this point all required update operations
1999 * have finished. By the end of the loop each image in the primary slot will
2000 * have been re-validated.
2001 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002002 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2003 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002004 /* Attempt to read an image header from each slot. Ensure that image
2005 * headers in slots are aligned with headers in boot_data.
2006 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002007 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002008 if (rc != 0) {
2009 goto out;
2010 }
2011 /* Since headers were reloaded, it can be assumed we just performed
2012 * a swap or overwrite. Now the header info that should be used to
2013 * provide the data for the bootstrap, which previously was at
2014 * secondary slot, was updated to primary slot.
2015 */
2016 }
2017
2018#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002019 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2020 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002021 goto out;
2022 }
2023#else
2024 /* Even if we're not re-validating the primary slot, we could be booting
2025 * onto an empty flash chip. At least do a basic sanity check that
2026 * the magic number on the image is OK.
2027 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002028 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002029 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002030 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2031 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002032 rc = BOOT_EBADIMAGE;
2033 goto out;
2034 }
David Vinczec3084132020-02-18 14:50:47 +01002035#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2036
Mark Horvathccaf7f82021-01-04 18:16:42 +01002037 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002038 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002039 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002040 }
David Vincze1cf11b52020-03-24 07:51:09 +01002041
Mark Horvathccaf7f82021-01-04 18:16:42 +01002042 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002043 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002044 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002045 }
David Vinczeba3bd602019-06-17 16:01:43 +02002046 }
2047
Fabio Utzigf616c542019-12-19 15:23:32 -03002048 /*
2049 * Since the boot_status struct stores plaintext encryption keys, reset
2050 * them here to avoid the possibility of jumping into an image that could
2051 * easily recover them.
2052 */
2053 memset(&bs, 0, sizeof(struct boot_status));
2054
Mark Horvathccaf7f82021-01-04 18:16:42 +01002055 fill_rsp(state, NULL, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002056
Raef Colese8fe6cf2020-05-26 13:07:40 +01002057 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002058out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002059 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002060
2061 if (rc) {
2062 fih_rc = fih_int_encode(rc);
2063 }
2064
2065 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002066}
2067
Raef Colese8fe6cf2020-05-26 13:07:40 +01002068fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002069split_go(int loader_slot, int split_slot, void **entry)
2070{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002071 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002072 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002073 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002074 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002075 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002076 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002077
Christopher Collins92ea77f2016-12-12 15:59:26 -08002078 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2079 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002080 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002081 }
David Vinczeba3bd602019-06-17 16:01:43 +02002082 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2083 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002084
2085 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2086 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002087 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002088 assert(rc == 0);
2089 split_flash_id = flash_area_id_from_image_slot(split_slot);
2090 rc = flash_area_open(split_flash_id,
2091 &BOOT_IMG_AREA(&boot_data, split_slot));
2092 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002093
2094 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002095 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002096 if (rc != 0) {
2097 rc = SPLIT_GO_ERR;
2098 goto done;
2099 }
2100
Fabio Utzig12d59162019-11-28 10:01:59 -03002101 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002102 if (rc != 0) {
2103 goto done;
2104 }
2105
Christopher Collins92ea77f2016-12-12 15:59:26 -08002106 /* Don't check the bootable image flag because we could really call a
2107 * bootable or non-bootable image. Just validate that the image check
2108 * passes which is distinct from the normal check.
2109 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002110 FIH_CALL(split_image_check, fih_rc,
2111 boot_img_hdr(&boot_data, split_slot),
2112 BOOT_IMG_AREA(&boot_data, split_slot),
2113 boot_img_hdr(&boot_data, loader_slot),
2114 BOOT_IMG_AREA(&boot_data, loader_slot));
2115 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002116 goto done;
2117 }
2118
Marti Bolivarea088872017-06-12 17:10:49 -04002119 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002120 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002121 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002122 rc = SPLIT_GO_OK;
2123
2124done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002125 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2126 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002127 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002128
2129 if (rc) {
2130 fih_rc = fih_int_encode(rc);
2131 }
2132
2133 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002134}
David Vinczee574f2d2020-07-10 11:42:03 +02002135
Tamas Banfe031092020-09-10 17:32:39 +02002136#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002137
Mark Horvathccaf7f82021-01-04 18:16:42 +01002138#define NO_ACTIVE_SLOT UINT32_MAX
2139
David Vinczee574f2d2020-07-10 11:42:03 +02002140/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002141 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002142 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002143 * @param state Boot loader status information.
2144 * @param slot_usage Structure to fill with information about the available
2145 * slots.
David Vinczee574f2d2020-07-10 11:42:03 +02002146 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002147 * @return 0 on success; nonzero on failure.
2148 */
2149static int
2150boot_get_slot_usage(struct boot_loader_state *state,
2151 struct slot_usage_t slot_usage[])
2152{
2153 uint32_t slot;
2154 int fa_id;
2155 int rc;
2156 struct image_header *hdr = NULL;
2157
2158 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2159 /* Open all the slots */
2160 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2161 fa_id = flash_area_id_from_multi_image_slot(
2162 BOOT_CURR_IMG(state), slot);
2163 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2164 assert(rc == 0);
2165 }
2166
2167 /* Attempt to read an image header from each slot. */
2168 rc = boot_read_image_headers(state, false, NULL);
2169 if (rc != 0) {
2170 BOOT_LOG_WRN("Failed reading image headers.");
2171 return rc;
2172 }
2173
2174 /* Check headers in all slots */
2175 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2176 hdr = boot_img_hdr(state, slot);
2177
2178 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2179 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
2180 BOOT_LOG_IMAGE_INFO(slot, hdr);
2181 } else {
2182 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
2183 BOOT_LOG_INF("Image %d %s slot: Image not found",
2184 BOOT_CURR_IMG(state),
2185 (slot == BOOT_PRIMARY_SLOT)
2186 ? "Primary" : "Secondary");
2187 }
2188 }
2189
2190 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2191 }
2192
2193 return 0;
2194}
2195
2196/**
2197 * Finds the slot containing the image with the highest version number for the
2198 * current image.
2199 *
2200 * @param state Boot loader status information.
2201 * @param slot_usage Information about the active and available slots.
2202 *
2203 * @return NO_ACTIVE_SLOT if no available slot found, number of
2204 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002205 */
2206static uint32_t
Mark Horvathccaf7f82021-01-04 18:16:42 +01002207find_slot_with_highest_version(struct boot_loader_state *state,
2208 struct slot_usage_t slot_usage[])
David Vinczee574f2d2020-07-10 11:42:03 +02002209{
David Vinczee574f2d2020-07-10 11:42:03 +02002210 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002211 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2212 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002213
Mark Horvathccaf7f82021-01-04 18:16:42 +01002214 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2215 if (slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
2216 if (candidate_slot == NO_ACTIVE_SLOT) {
2217 candidate_slot = slot;
2218 } else {
2219 rc = boot_version_cmp(
2220 &boot_img_hdr(state, slot)->ih_ver,
2221 &boot_img_hdr(state, candidate_slot)->ih_ver);
2222 if (rc == 1) {
2223 /* The version of the image being examined is greater than
2224 * the version of the current candidate.
2225 */
2226 candidate_slot = slot;
2227 }
2228 }
David Vinczee574f2d2020-07-10 11:42:03 +02002229 }
2230 }
2231
Mark Horvathccaf7f82021-01-04 18:16:42 +01002232 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002233}
2234
Mark Horvathccaf7f82021-01-04 18:16:42 +01002235#ifdef MCUBOOT_HAVE_LOGGING
2236/**
2237 * Prints the state of the loaded images.
2238 *
2239 * @param state Boot loader status information.
2240 * @param slot_usage Information about the active and available slots.
2241 */
2242static void
2243print_loaded_images(struct boot_loader_state *state,
2244 struct slot_usage_t slot_usage[])
2245{
2246 uint32_t active_slot;
2247
David Brown695e5912021-05-24 16:58:01 -06002248 (void)state;
2249
Mark Horvathccaf7f82021-01-04 18:16:42 +01002250 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2251 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2252
2253 BOOT_LOG_INF("Image %d loaded from the %s slot",
2254 BOOT_CURR_IMG(state),
2255 (active_slot == BOOT_PRIMARY_SLOT) ?
2256 "primary" : "secondary");
2257 }
2258}
2259#endif
2260
David Vincze1c456242021-06-29 15:25:24 +02002261#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002262/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002263 * Checks whether the active slot of the current image was previously selected
2264 * to run. Erases the image if it was selected but its execution failed,
2265 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002266 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002267 * @param state Boot loader status information.
2268 * @param slot_usage Information about the active and available slots.
David Vincze505fba22020-10-22 13:53:29 +02002269 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002270 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002271 */
2272static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002273boot_select_or_erase(struct boot_loader_state *state,
2274 struct slot_usage_t slot_usage[])
David Vincze505fba22020-10-22 13:53:29 +02002275{
2276 const struct flash_area *fap;
2277 int fa_id;
2278 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002279 uint32_t active_slot;
2280 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002281
Mark Horvathccaf7f82021-01-04 18:16:42 +01002282 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2283
2284 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002285 rc = flash_area_open(fa_id, &fap);
2286 assert(rc == 0);
2287
Mark Horvathccaf7f82021-01-04 18:16:42 +01002288 active_swap_state = &(slot_usage[BOOT_CURR_IMG(state)].swap_state);
2289
2290 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2291 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002292 assert(rc == 0);
2293
Mark Horvathccaf7f82021-01-04 18:16:42 +01002294 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2295 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2296 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002297 /*
2298 * A reboot happened without the image being confirmed at
2299 * runtime or its trailer is corrupted/invalid. Erase the image
2300 * to prevent it from being selected again on the next reboot.
2301 */
2302 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002303 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002304 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002305 assert(rc == 0);
2306
2307 flash_area_close(fap);
2308 rc = -1;
2309 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002310 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2311 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002312 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2313 "value was neither 'set' nor 'unset', but 'bad'.");
2314 }
2315 /*
2316 * Set the copy_done flag, indicating that the image has been
2317 * selected to boot. It can be set in advance, before even
2318 * validating the image, because in case the validation fails, the
2319 * entire image slot will be erased (including the trailer).
2320 */
2321 rc = boot_write_copy_done(fap);
2322 if (rc != 0) {
2323 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002324 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002325 "primary" : "secondary");
2326 rc = 0;
2327 }
2328 }
2329 flash_area_close(fap);
2330 }
2331
2332 return rc;
2333}
David Vincze1c456242021-06-29 15:25:24 +02002334#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002335
Tamas Banfe031092020-09-10 17:32:39 +02002336#ifdef MCUBOOT_RAM_LOAD
2337
Mark Horvathccaf7f82021-01-04 18:16:42 +01002338#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002339#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2340#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2341#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002342#endif
Tamas Banfe031092020-09-10 17:32:39 +02002343
2344/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002345 * Verifies that the active slot of the current image can be loaded within the
2346 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002347 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002348 * @param state Boot loader status information.
2349 * @param slot_usage Information about the active and available slots.
Tamas Banfe031092020-09-10 17:32:39 +02002350 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002351 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002352 */
2353static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002354boot_verify_ram_load_address(struct boot_loader_state *state,
2355 struct slot_usage_t slot_usage[])
Tamas Banfe031092020-09-10 17:32:39 +02002356{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002357 uint32_t img_dst;
2358 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002359 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002360 uint32_t exec_ram_start;
2361 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002362
2363 (void)state;
2364
Mark Horvathccaf7f82021-01-04 18:16:42 +01002365#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2366 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002367
Mark Horvathccaf7f82021-01-04 18:16:42 +01002368 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2369 &exec_ram_size);
2370 if (rc != 0) {
2371 return BOOT_EBADSTATUS;
2372 }
2373#else
2374 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2375 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2376#endif
2377
2378 img_dst = slot_usage[BOOT_CURR_IMG(state)].img_dst;
2379 img_sz = slot_usage[BOOT_CURR_IMG(state)].img_sz;
2380
2381 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002382 return BOOT_EBADIMAGE;
2383 }
2384
2385 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2386 return BOOT_EBADIMAGE;
2387 }
2388
Mark Horvathccaf7f82021-01-04 18:16:42 +01002389 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002390 return BOOT_EBADIMAGE;
2391 }
2392
2393 return 0;
2394}
2395
2396/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002397 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002398 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002399 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002400 * @param slot The flash slot of the image to be copied to SRAM.
2401 * @param img_dst The address at which the image needs to be copied to
2402 * SRAM.
2403 * @param img_sz The size of the image that needs to be copied to SRAM.
2404 *
2405 * @return 0 on success; nonzero on failure.
2406 */
2407static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002408boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2409 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002410{
2411 int rc;
2412 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002413 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002414
Mark Horvathccaf7f82021-01-04 18:16:42 +01002415#if (BOOT_IMAGE_NUMBER == 1)
2416 (void)state;
2417#endif
2418
2419 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2420
2421 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002422 if (rc != 0) {
2423 return BOOT_EFLASH;
2424 }
2425
2426 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002427 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002428 if (rc != 0) {
2429 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2430 }
2431
2432 flash_area_close(fap_src);
2433
2434 return rc;
2435}
2436
Mark Horvathccaf7f82021-01-04 18:16:42 +01002437#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002438/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002439 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002440 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002441 * @param start_a Start of the A region.
2442 * @param end_a End of the A region.
2443 * @param start_b Start of the B region.
2444 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002445 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002446 * @return true if there is overlap; false otherwise.
2447 */
2448static bool
2449do_regions_overlap(uint32_t start_a, uint32_t end_a,
2450 uint32_t start_b, uint32_t end_b)
2451{
2452 if (start_b > end_a) {
2453 return false;
2454 } else if (start_b >= start_a) {
2455 return true;
2456 } else if (end_b > start_a) {
2457 return true;
2458 }
2459
2460 return false;
2461}
2462
2463/**
2464 * Checks if the image we want to load to memory overlap with an already
2465 * ramloaded image.
2466 *
2467 * @param slot_usage Information about the active and available slots.
2468 * @param image_id_to_check The ID of the image we would like to load.
2469 *
2470 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002471 */
2472static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002473boot_check_ram_load_overlapping(struct slot_usage_t slot_usage[],
2474 uint32_t image_id_to_check)
Tamas Banfe031092020-09-10 17:32:39 +02002475{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002476 uint32_t i;
2477
2478 uint32_t start_a;
2479 uint32_t end_a;
2480 uint32_t start_b;
2481 uint32_t end_b;
2482
2483 start_a = slot_usage[image_id_to_check].img_dst;
2484 /* Safe to add here, values are already verified in
2485 * boot_verify_ram_load_address() */
2486 end_a = start_a + slot_usage[image_id_to_check].img_sz;
2487
2488 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
2489 if (slot_usage[i].active_slot == NO_ACTIVE_SLOT
2490 || i == image_id_to_check) {
2491 continue;
2492 }
2493
2494 start_b = slot_usage[i].img_dst;
2495 /* Safe to add here, values are already verified in
2496 * boot_verify_ram_load_address() */
2497 end_b = start_b + slot_usage[i].img_sz;
2498
2499 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2500 return -1;
2501 }
2502 }
2503
2504 return 0;
2505}
2506#endif
2507
2508/**
2509 * Loads the active slot of the current image into SRAM. The load address and
2510 * image size is extracted from the image header.
2511 *
2512 * @param state Boot loader status information.
2513 * @param slot_usage Information about the active and available slots.
2514 *
2515 * @return 0 on success; nonzero on failure.
2516 */
2517static int
2518boot_load_image_to_sram(struct boot_loader_state *state,
2519 struct slot_usage_t slot_usage[])
2520{
2521 uint32_t active_slot;
2522 struct image_header *hdr = NULL;
2523 uint32_t img_dst;
2524 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002525 int rc;
2526
Mark Horvathccaf7f82021-01-04 18:16:42 +01002527 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2528 hdr = boot_img_hdr(state, active_slot);
2529
Tamas Banfe031092020-09-10 17:32:39 +02002530 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2531
Mark Horvathccaf7f82021-01-04 18:16:42 +01002532 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002533
Mark Horvathccaf7f82021-01-04 18:16:42 +01002534 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002535 if (rc != 0) {
2536 return rc;
2537 }
2538
Mark Horvathccaf7f82021-01-04 18:16:42 +01002539 slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2540 slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
2541
2542 rc = boot_verify_ram_load_address(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002543 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002544 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002545 return rc;
2546 }
2547
Mark Horvathccaf7f82021-01-04 18:16:42 +01002548#if (BOOT_IMAGE_NUMBER > 1)
2549 rc = boot_check_ram_load_overlapping(slot_usage, BOOT_CURR_IMG(state));
2550 if (rc != 0) {
2551 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2552 another image.", img_dst);
2553 return rc;
2554 }
2555#endif
2556
Tamas Banfe031092020-09-10 17:32:39 +02002557 /* Copy image to the load address from where it currently resides in
2558 * flash.
2559 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002560 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002561 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002562 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002563 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002564 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002565 }
2566 } else {
2567 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2568 * MCUBOOT_RAM_LOAD is set.
2569 */
2570 rc = BOOT_EBADIMAGE;
2571 }
2572
Mark Horvathccaf7f82021-01-04 18:16:42 +01002573 if (rc != 0) {
2574 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2575 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2576 }
2577
Tamas Banfe031092020-09-10 17:32:39 +02002578 return rc;
2579}
2580
2581/**
2582 * Removes an image from SRAM, by overwriting it with zeros.
2583 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002584 * @param state Boot loader status information.
2585 * @param slot_usage Information about the active and available slots.
2586 *
2587 * @return 0 on success; nonzero on failure.
2588 */
2589static inline int
2590boot_remove_image_from_sram(struct boot_loader_state *state,
2591 struct slot_usage_t slot_usage[])
2592{
David Brown695e5912021-05-24 16:58:01 -06002593 (void)state;
2594
Mark Horvathccaf7f82021-01-04 18:16:42 +01002595 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
2596 slot_usage[BOOT_CURR_IMG(state)].img_dst);
2597
David Brown695e5912021-05-24 16:58:01 -06002598 memset((void*)(IMAGE_RAM_BASE + slot_usage[BOOT_CURR_IMG(state)].img_dst),
2599 0, slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002600
2601 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2602 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2603
2604 return 0;
2605}
2606
2607/**
2608 * Removes an image from flash by erasing the corresponding flash area
2609 *
2610 * @param state Boot loader status information.
2611 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002612 *
2613 * @return 0 on success; nonzero on failure.
2614 */
2615static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002616boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002617{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002618 int area_id;
2619 int rc;
2620 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002621
David Brown695e5912021-05-24 16:58:01 -06002622 (void)state;
2623
Mark Horvathccaf7f82021-01-04 18:16:42 +01002624 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2625 slot);
2626 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2627 rc = flash_area_open(area_id, &fap);
2628 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002629 flash_area_erase(fap, 0, flash_area_get_size(fap));
Mark Horvathccaf7f82021-01-04 18:16:42 +01002630 }
2631
2632 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002633}
2634#endif /* MCUBOOT_RAM_LOAD */
2635
Mark Horvathccaf7f82021-01-04 18:16:42 +01002636#if (BOOT_IMAGE_NUMBER > 1)
2637/**
2638 * Checks the image dependency whether it is satisfied.
2639 *
2640 * @param state Boot loader status information.
2641 * @param slot_usage Information about the active and available slots.
2642 * @param dep Image dependency which has to be verified.
2643 *
2644 * @return 0 if dependencies are met; nonzero otherwise.
2645 */
2646static int
2647boot_verify_slot_dependency(struct boot_loader_state *state,
2648 struct slot_usage_t slot_usage[],
2649 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002650{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002651 struct image_version *dep_version;
2652 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002653 int rc;
2654
Mark Horvathccaf7f82021-01-04 18:16:42 +01002655 /* Determine the source of the image which is the subject of
2656 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002657 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002658 dep_slot = slot_usage[dep->image_id].active_slot;
2659 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2660
2661 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2662 if (rc >= 0) {
2663 /* Dependency satisfied. */
2664 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002665 }
2666
Mark Horvathccaf7f82021-01-04 18:16:42 +01002667 return rc;
2668}
2669
2670/**
2671 * Reads all dependency TLVs of an image and verifies one after another to see
2672 * if they are all satisfied.
2673 *
2674 * @param state Boot loader status information.
2675 * @param slot_usage Information about the active and available slots.
2676 *
2677 * @return 0 if dependencies are met; nonzero otherwise.
2678 */
2679static int
2680boot_verify_slot_dependencies(struct boot_loader_state *state,
2681 struct slot_usage_t slot_usage[])
2682{
2683 uint32_t active_slot;
2684 const struct flash_area *fap;
2685 struct image_tlv_iter it;
2686 struct image_dependency dep;
2687 uint32_t off;
2688 uint16_t len;
2689 int area_id;
2690 int rc;
2691
2692 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2693
2694 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2695 active_slot);
2696 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002697 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002698 rc = BOOT_EFLASH;
2699 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002700 }
2701
Mark Horvathccaf7f82021-01-04 18:16:42 +01002702 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2703 IMAGE_TLV_DEPENDENCY, true);
2704 if (rc != 0) {
2705 goto done;
2706 }
David Vinczee574f2d2020-07-10 11:42:03 +02002707
Mark Horvathccaf7f82021-01-04 18:16:42 +01002708 while (true) {
2709 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2710 if (rc < 0) {
2711 return -1;
2712 } else if (rc > 0) {
2713 rc = 0;
2714 break;
2715 }
David Vinczee574f2d2020-07-10 11:42:03 +02002716
Mark Horvathccaf7f82021-01-04 18:16:42 +01002717 if (len != sizeof(dep)) {
2718 rc = BOOT_EBADIMAGE;
2719 goto done;
2720 }
2721
2722 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2723 fap, off, &dep, len);
2724 if (rc != 0) {
2725 rc = BOOT_EFLASH;
2726 goto done;
2727 }
2728
2729 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2730 rc = BOOT_EBADARGS;
2731 goto done;
2732 }
2733
2734 rc = boot_verify_slot_dependency(state, slot_usage, &dep);
2735 if (rc != 0) {
2736 /* Dependency not satisfied. */
2737 goto done;
2738 }
2739 }
2740
2741done:
2742 flash_area_close(fap);
2743 return rc;
2744}
2745
2746/**
2747 * Checks the dependency of all the active slots. If an image found with
2748 * invalid or not satisfied dependencies the image is removed from SRAM (in
2749 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2750 *
2751 * @param state Boot loader status information.
2752 * @param slot_usage Information about the active and available slots.
2753 *
2754 * @return 0 if dependencies are met; nonzero otherwise.
2755 */
2756static int
2757boot_verify_dependencies(struct boot_loader_state *state,
2758 struct slot_usage_t slot_usage[])
2759{
2760 int rc = -1;
2761 uint32_t active_slot;
2762
2763 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2764 rc = boot_verify_slot_dependencies(state, slot_usage);
2765 if (rc != 0) {
2766 /* Dependencies not met or invalid dependencies. */
2767
2768#ifdef MCUBOOT_RAM_LOAD
2769 boot_remove_image_from_sram(state, slot_usage);
2770#endif /* MCUBOOT_RAM_LOAD */
2771
2772 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2773 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2774 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2775
2776 return rc;
2777 }
2778 }
2779
2780 return rc;
2781}
2782#endif /* (BOOT_IMAGE_NUMBER > 1) */
2783
2784/**
2785 * Tries to load a slot for all the images with validation.
2786 *
2787 * @param state Boot loader status information.
2788 * @param slot_usage Information about the active and available slots.
2789 *
2790 * @return 0 on success; nonzero on failure.
2791 */
2792fih_int
2793boot_load_and_validate_images(struct boot_loader_state *state,
2794 struct slot_usage_t slot_usage[])
2795{
2796 uint32_t active_slot;
2797 int rc;
2798 fih_int fih_rc;
2799
2800 /* Go over all the images and try to load one */
2801 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2802 /* All slots tried until a valid image found. Breaking from this loop
2803 * means that a valid image found or already loaded. If no slot is
2804 * found the function returns with error code. */
2805 while (true) {
2806
2807 /* Go over all the slots and try to load one */
2808 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2809 if (active_slot != NO_ACTIVE_SLOT){
2810 /* A slot is already active, go to next image. */
2811 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002812 }
David Vincze505fba22020-10-22 13:53:29 +02002813
Mark Horvathccaf7f82021-01-04 18:16:42 +01002814 active_slot = find_slot_with_highest_version(state,
2815 slot_usage);
2816 if (active_slot == NO_ACTIVE_SLOT) {
2817 BOOT_LOG_INF("No slot to load for image %d",
2818 BOOT_CURR_IMG(state));
2819 FIH_RET(FIH_FAILURE);
2820 }
2821
2822 /* Save the number of the active slot. */
2823 slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
2824
2825#ifdef MCUBOOT_DIRECT_XIP
2826 rc = boot_rom_address_check(state, slot_usage);
2827 if (rc != 0) {
2828 /* The image is placed in an unsuitable slot. */
2829 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2830 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2831 continue;
2832 }
George Becksteind4d90f82021-05-11 02:00:00 -04002833
David Vincze505fba22020-10-22 13:53:29 +02002834#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002835 rc = boot_select_or_erase(state, slot_usage);
David Vincze505fba22020-10-22 13:53:29 +02002836 if (rc != 0) {
2837 /* The selected image slot has been erased. */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002838 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2839 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02002840 continue;
2841 }
2842#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02002843#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02002844
Tamas Banfe031092020-09-10 17:32:39 +02002845#ifdef MCUBOOT_RAM_LOAD
2846 /* Image is first loaded to RAM and authenticated there in order to
2847 * prevent TOCTOU attack during image copy. This could be applied
2848 * when loading images from external (untrusted) flash to internal
2849 * (trusted) RAM and image is authenticated before copying.
2850 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002851 rc = boot_load_image_to_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002852 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002853 /* Image cannot be ramloaded. */
2854 boot_remove_image_from_flash(state, active_slot);
2855 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2856 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02002857 continue;
Tamas Banfe031092020-09-10 17:32:39 +02002858 }
2859#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002860
2861 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
2862 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2863 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02002864#ifdef MCUBOOT_RAM_LOAD
Mark Horvathccaf7f82021-01-04 18:16:42 +01002865 boot_remove_image_from_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002866#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002867 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2868 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2869 continue;
David Vincze505fba22020-10-22 13:53:29 +02002870 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01002871
2872 /* Valid image loaded from a slot, go to next image. */
2873 break;
2874 }
2875 }
2876
2877 FIH_RET(FIH_SUCCESS);
2878}
2879
2880/**
2881 * Updates the security counter for the current image.
2882 *
2883 * @param state Boot loader status information.
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002884 * @param slot_usage Information about the active and available slots.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002885 *
2886 * @return 0 on success; nonzero on failure.
2887 */
2888static int
2889boot_update_hw_rollback_protection(struct boot_loader_state *state,
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002890 const struct slot_usage_t slot_usage[])
Mark Horvathccaf7f82021-01-04 18:16:42 +01002891{
2892#ifdef MCUBOOT_HW_ROLLBACK_PROT
2893 int rc;
2894
2895 /* Update the stored security counter with the newer (active) image's
2896 * security counter value.
2897 */
David Vincze1c456242021-06-29 15:25:24 +02002898#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002899 /* When the 'revert' mechanism is enabled in direct-xip mode, the
2900 * security counter can be increased only after reboot, if the image
2901 * has been confirmed at runtime (the image_ok flag has been set).
2902 * This way a 'revert' can be performed when it's necessary.
2903 */
2904 if (slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02002905#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002906 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
2907 slot_usage[BOOT_CURR_IMG(state)].active_slot,
2908 boot_img_hdr(state, slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02002909 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002910 BOOT_LOG_ERR("Security counter update failed after image "
2911 "validation.");
2912 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002913 }
David Vincze1c456242021-06-29 15:25:24 +02002914#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002915 }
2916#endif
David Vinczee574f2d2020-07-10 11:42:03 +02002917
Mark Horvathccaf7f82021-01-04 18:16:42 +01002918 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002919
Mark Horvathccaf7f82021-01-04 18:16:42 +01002920#else /* MCUBOOT_HW_ROLLBACK_PROT */
2921 (void) (state);
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002922 (void) (slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002923 return 0;
2924#endif
2925}
2926
2927fih_int
2928context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2929{
2930 struct slot_usage_t slot_usage[BOOT_IMAGE_NUMBER];
2931 int rc;
David Brown695e5912021-05-24 16:58:01 -06002932 fih_int fih_rc = fih_int_encode(0);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002933
2934 memset(state, 0, sizeof(struct boot_loader_state));
2935 memset(slot_usage, 0, sizeof(struct slot_usage_t) * BOOT_IMAGE_NUMBER);
2936
2937 rc = boot_get_slot_usage(state, slot_usage);
2938 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02002939 goto out;
2940 }
2941
Mark Horvathccaf7f82021-01-04 18:16:42 +01002942#if (BOOT_IMAGE_NUMBER > 1)
2943 while (true) {
2944#endif
2945 FIH_CALL(boot_load_and_validate_images, fih_rc, state, slot_usage);
2946 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2947 goto out;
2948 }
2949
2950#if (BOOT_IMAGE_NUMBER > 1)
2951 rc = boot_verify_dependencies(state, slot_usage);
2952 if (rc != 0) {
2953 /* Dependency check failed for an image, it has been removed from
2954 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
2955 * unavailable. Try to load an image from another slot.
2956 */
2957 continue;
2958 }
2959 /* Dependency check was successful. */
2960 break;
2961 }
2962#endif
2963
2964 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002965 rc = boot_update_hw_rollback_protection(state, slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002966 if (rc != 0) {
2967 goto out;
2968 }
2969
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002970 rc = boot_add_shared_data(state, slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002971 if (rc != 0) {
2972 goto out;
2973 }
2974 }
2975
2976 /* All image loaded successfully. */
2977#ifdef MCUBOOT_HAVE_LOGGING
2978 print_loaded_images(state, slot_usage);
2979#endif
2980
2981 fill_rsp(state, slot_usage, rsp);
2982
David Vinczee574f2d2020-07-10 11:42:03 +02002983out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002984 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002985
Mark Horvathccaf7f82021-01-04 18:16:42 +01002986 if (fih_eq(fih_rc, FIH_SUCCESS)) {
2987 fih_rc = fih_int_encode(rc);
2988 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01002989
Mark Horvathccaf7f82021-01-04 18:16:42 +01002990 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02002991}
Tamas Banfe031092020-09-10 17:32:39 +02002992#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002993
2994/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01002995 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02002996 * appropriate, and tells you what address to boot from.
2997 *
2998 * @param rsp On success, indicates how booting should occur.
2999 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003000 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003001 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003002fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003003boot_go(struct boot_rsp *rsp)
3004{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003005 fih_int fih_rc = FIH_FAILURE;
3006 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3007 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003008}