blob: e5587dd16e78b9d487c0e7f70798dde76827557c [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2016-2020 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
Mark Horvathccaf7f82021-01-04 18:16:42 +01006 * Copyright (c) 2019-2021 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28/**
29 * This file provides an interface to the boot loader. Functions defined in
30 * this file should only be called while the boot loader is running.
31 */
32
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060034#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "bootutil/bootutil.h"
39#include "bootutil/image.h"
40#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030041#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050042#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010043#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010044#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010045#include "bootutil/fault_injection_hardening.h"
Mark Horvathccaf7f82021-01-04 18:16:42 +010046#include "bootutil/ramload.h"
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020047#include "bootutil/boot_hooks.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050048
Fabio Utzigba829042018-09-18 08:29:34 -030049#ifdef MCUBOOT_ENC_IMAGES
50#include "bootutil/enc_key.h"
51#endif
52
Carlos Falgueras García391b1972021-06-21 16:58:07 +020053#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
54#include <os/os_malloc.h>
55#endif
56
Fabio Utzigba1fbe62017-07-21 14:01:20 -030057#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030058
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020059BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010060
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040061static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080062
Fabio Utzigabec0732019-07-31 08:40:22 -030063#if (BOOT_IMAGE_NUMBER > 1)
64#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
65#else
66#define IMAGES_ITER(x)
67#endif
68
Mark Horvathccaf7f82021-01-04 18:16:42 +010069#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
70struct slot_usage_t {
71 /* Index of the slot chosen to be loaded */
72 uint32_t active_slot;
73 bool slot_available[BOOT_NUM_SLOTS];
David Vincze1c456242021-06-29 15:25:24 +020074#if defined(MCUBOOT_RAM_LOAD)
Mark Horvathccaf7f82021-01-04 18:16:42 +010075 /* Image destination and size for the active slot */
76 uint32_t img_dst;
77 uint32_t img_sz;
David Vincze1c456242021-06-29 15:25:24 +020078#elif defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +010079 /* Swap status for the active slot */
Carlos Falgueras Garcíaafb424d2021-06-21 17:05:44 +020080 struct boot_swap_state swap_state;
Mark Horvathccaf7f82021-01-04 18:16:42 +010081#endif
David Vincze1c456242021-06-29 15:25:24 +020082};
83#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +010084
Fabio Utzig10ee6482019-08-01 12:04:52 -030085/*
86 * This macro allows some control on the allocation of local variables.
87 * When running natively on a target, we don't want to allocated huge
88 * variables on the stack, so make them global instead. For the simulator
89 * we want to run as many threads as there are tests, and it's safer
90 * to just make those variables stack allocated.
91 */
92#if !defined(__BOOTSIM__)
93#define TARGET_STATIC static
94#else
95#define TARGET_STATIC
96#endif
97
David Vinczee574f2d2020-07-10 11:42:03 +020098static int
99boot_read_image_headers(struct boot_loader_state *state, bool require_all,
100 struct boot_status *bs)
101{
102 int rc;
103 int i;
104
105 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200106 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
107 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
108 if (rc == BOOT_HOOK_REGULAR)
109 {
110 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
111 }
David Vinczee574f2d2020-07-10 11:42:03 +0200112 if (rc != 0) {
113 /* If `require_all` is set, fail on any single fail, otherwise
114 * if at least the first slot's header was read successfully,
115 * then the boot loader can attempt a boot.
116 *
117 * Failure to read any headers is a fatal error.
118 */
119 if (i > 0 && !require_all) {
120 return 0;
121 } else {
122 return rc;
123 }
124 }
125 }
126
127 return 0;
128}
129
Mark Horvathccaf7f82021-01-04 18:16:42 +0100130/**
131 * Saves boot status and shared data for current image.
132 *
133 * @param state Boot loader status information.
134 * @param active_slot Index of the slot will be loaded for current image.
135 *
136 * @return 0 on success; nonzero on failure.
137 */
138static int
139boot_add_shared_data(struct boot_loader_state *state,
140 uint32_t active_slot)
141{
142#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
143 int rc;
144
145#ifdef MCUBOOT_MEASURED_BOOT
146 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
147 boot_img_hdr(state, active_slot),
148 BOOT_IMG_AREA(state, active_slot));
149 if (rc != 0) {
150 BOOT_LOG_ERR("Failed to add image data to shared area");
151 return rc;
152 }
153#endif /* MCUBOOT_MEASURED_BOOT */
154
155#ifdef MCUBOOT_DATA_SHARING
156 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
157 BOOT_IMG_AREA(state, active_slot));
158 if (rc != 0) {
159 BOOT_LOG_ERR("Failed to add data to shared memory area.");
160 return rc;
161 }
162#endif /* MCUBOOT_DATA_SHARING */
163
164 return 0;
165
166#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
167 (void) (state);
168 (void) (active_slot);
169
170 return 0;
171#endif
172}
173
174/**
175 * Fills rsp to indicate how booting should occur.
176 *
177 * @param state Boot loader status information.
178 * @param slot_usage Information about the active and available slots.
179 * Only used in MCUBOOT_DIRECT_XIP and MCUBOOT_RAM_LOAD
180 * @param rsp boot_rsp struct to fill.
181 */
182static void
183fill_rsp(struct boot_loader_state *state, void *slot_usage,
184 struct boot_rsp *rsp)
185{
186 uint32_t active_slot;
187
188#if (BOOT_IMAGE_NUMBER > 1)
189 /* Always boot from Image 0. */
190 BOOT_CURR_IMG(state) = 0;
191#endif
192
193#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
194 active_slot = ((struct slot_usage_t *)slot_usage)[BOOT_CURR_IMG(state)].active_slot;
195#else
196 (void) (slot_usage);
197 active_slot = BOOT_PRIMARY_SLOT;
198#endif
199
Dominik Ermel260ae092021-04-23 05:38:45 +0000200 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100201 rsp->br_image_off = boot_img_slot_off(state, active_slot);
202 rsp->br_hdr = boot_img_hdr(state, active_slot);
203}
204
205/**
206 * Closes all flash areas.
207 *
208 * @param state Boot loader status information.
209 */
210static void
211close_all_flash_areas(struct boot_loader_state *state)
212{
213 uint32_t slot;
214
215 IMAGES_ITER(BOOT_CURR_IMG(state)) {
216#if MCUBOOT_SWAP_USING_SCRATCH
217 flash_area_close(BOOT_SCRATCH_AREA(state));
218#endif
219 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
220 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
221 }
222 }
223}
224
Tamas Banfe031092020-09-10 17:32:39 +0200225#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600226/*
227 * Compute the total size of the given image. Includes the size of
228 * the TLVs.
229 */
Tamas Banfe031092020-09-10 17:32:39 +0200230#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600231static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300232boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600233{
234 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300235 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300236 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300237 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600238 int area_id;
239 int rc;
240
Fabio Utzig10ee6482019-08-01 12:04:52 -0300241#if (BOOT_IMAGE_NUMBER == 1)
242 (void)state;
243#endif
244
245 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600246 rc = flash_area_open(area_id, &fap);
247 if (rc != 0) {
248 rc = BOOT_EFLASH;
249 goto done;
250 }
251
Fabio Utzig61fd8882019-09-14 20:00:20 -0300252 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
253
254 if (flash_area_read(fap, off, &info, sizeof(info))) {
255 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600256 goto done;
257 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300258
Fabio Utzige52c08e2019-09-11 19:32:00 -0300259 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
260 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
261 if (protect_tlv_size != info.it_tlv_tot) {
262 rc = BOOT_EBADIMAGE;
263 goto done;
264 }
265
266 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
267 rc = BOOT_EFLASH;
268 goto done;
269 }
270 } else if (protect_tlv_size != 0) {
271 rc = BOOT_EBADIMAGE;
272 goto done;
273 }
274
Fabio Utzig61fd8882019-09-14 20:00:20 -0300275 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
276 rc = BOOT_EBADIMAGE;
277 goto done;
278 }
279
Fabio Utzige52c08e2019-09-11 19:32:00 -0300280 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600281 rc = 0;
282
283done:
284 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300285 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600286}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300287#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600288
Tamas Banfe031092020-09-10 17:32:39 +0200289#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700290static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300291boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800292{
David Brownab449182019-11-15 09:32:52 -0700293 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300294#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700295 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300296#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297
298 /* Figure out what size to write update status update as. The size depends
299 * on what the minimum write size is for scratch area, active image slot.
300 * We need to use the bigger of those 2 values.
301 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300302 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300303#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300304 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305 if (align > elem_sz) {
306 elem_sz = align;
307 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300308#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800309
310 return elem_sz;
311}
312
Fabio Utzig10ee6482019-08-01 12:04:52 -0300313static int
314boot_initialize_area(struct boot_loader_state *state, int flash_area)
315{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000316 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
317 boot_sector_t *out_sectors;
318 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300319 int rc;
320
321 num_sectors = BOOT_MAX_IMG_SECTORS;
322
323 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
324 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
325 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
326 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
327 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
328 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300329#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300330 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
331 out_sectors = state->scratch.sectors;
332 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300333#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300334 } else {
335 return BOOT_EFLASH;
336 }
337
Dominik Ermel51c8d762021-05-24 15:34:01 +0000338#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300339 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000340#else
341 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
342 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
343#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300344 if (rc != 0) {
345 return rc;
346 }
347 *out_num_sectors = num_sectors;
348 return 0;
349}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300350
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351/**
352 * Determines the sector layout of both image slots and the scratch area.
353 * This information is necessary for calculating the number of bytes to erase
354 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300355 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800356 */
357static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300358boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300360 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800362
Fabio Utzig10ee6482019-08-01 12:04:52 -0300363 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300364
365 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 if (rc != 0) {
367 return BOOT_EFLASH;
368 }
369
Fabio Utzig10ee6482019-08-01 12:04:52 -0300370 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200372 /* We need to differentiate from the primary image issue */
373 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800374 }
375
Fabio Utzig12d59162019-11-28 10:01:59 -0300376#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300377 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200378 if (rc != 0) {
379 return BOOT_EFLASH;
380 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300381#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200382
Fabio Utzig10ee6482019-08-01 12:04:52 -0300383 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384
385 return 0;
386}
387
Fabio Utzig12d59162019-11-28 10:01:59 -0300388void
389boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390{
Fabio Utzig4741c452019-12-19 15:32:41 -0300391#ifdef MCUBOOT_ENC_IMAGES
392 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
393#if MCUBOOT_SWAP_SAVE_ENCTLV
394 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
395#endif
396#endif /* MCUBOOT_ENC_IMAGES */
397
398 bs->use_scratch = 0;
399 bs->swap_size = 0;
400 bs->source = 0;
401
Fabio Utzig74aef312019-11-28 11:05:34 -0300402 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300403 bs->idx = BOOT_STATUS_IDX_0;
404 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700405 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300406}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407
Fabio Utzig12d59162019-11-28 10:01:59 -0300408bool
409boot_status_is_reset(const struct boot_status *bs)
410{
Fabio Utzig74aef312019-11-28 11:05:34 -0300411 return (bs->op == BOOT_STATUS_OP_MOVE &&
412 bs->idx == BOOT_STATUS_IDX_0 &&
413 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414}
415
416/**
417 * Writes the supplied boot status to the flash file system. The boot status
418 * contains the current state of an in-progress image copy operation.
419 *
420 * @param bs The boot status to write.
421 *
422 * @return 0 on success; nonzero on failure.
423 */
424int
Fabio Utzig12d59162019-11-28 10:01:59 -0300425boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800426{
427 const struct flash_area *fap;
428 uint32_t off;
429 int area_id;
430 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300431 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700432 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300433 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300435 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100436 * the trailer. Since in the last step the primary slot is erased, the
437 * first two status writes go to the scratch which will be copied to
438 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300439 */
440
Fabio Utzig12d59162019-11-28 10:01:59 -0300441#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300442 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443 /* Write to scratch. */
444 area_id = FLASH_AREA_IMAGE_SCRATCH;
445 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300446#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100447 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300448 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300449#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800450 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300451#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452
453 rc = flash_area_open(area_id, &fap);
454 if (rc != 0) {
455 rc = BOOT_EFLASH;
456 goto done;
457 }
458
459 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300460 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200461 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300462 erased_val = flash_area_erased_val(fap);
463 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700464 buf[0] = bs->state;
465
466 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467 if (rc != 0) {
468 rc = BOOT_EFLASH;
469 goto done;
470 }
471
472 rc = 0;
473
474done:
475 flash_area_close(fap);
476 return rc;
477}
Tamas Banfe031092020-09-10 17:32:39 +0200478#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200479#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480
481/*
David Vinczec3084132020-02-18 14:50:47 +0100482 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800483 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100484static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300485boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
486 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800487{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300488 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300489 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300490 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100491 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300492
Fabio Utzig10ee6482019-08-01 12:04:52 -0300493#if (BOOT_IMAGE_NUMBER == 1)
494 (void)state;
495#endif
496
Fabio Utzigba829042018-09-18 08:29:34 -0300497 (void)bs;
498 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300499
500 image_index = BOOT_CURR_IMG(state);
501
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000502/* In the case of ram loading the image has already been decrypted as it is
503 * decrypted when copied in ram */
504#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300505 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300506 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300507 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100508 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300509 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300510 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100511 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300512 }
513 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300514#endif
515
Raef Colese8fe6cf2020-05-26 13:07:40 +0100516 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
517 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300518
Raef Colese8fe6cf2020-05-26 13:07:40 +0100519 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800520}
521
Tamas Banfe031092020-09-10 17:32:39 +0200522#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100523static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524split_image_check(struct image_header *app_hdr,
525 const struct flash_area *app_fap,
526 struct image_header *loader_hdr,
527 const struct flash_area *loader_fap)
528{
529 static void *tmpbuf;
530 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100531 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800532
533 if (!tmpbuf) {
534 tmpbuf = malloc(BOOT_TMPBUF_SZ);
535 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100536 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537 }
538 }
539
Raef Colese8fe6cf2020-05-26 13:07:40 +0100540 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
541 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
542 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
543 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800544 }
545
Raef Colese8fe6cf2020-05-26 13:07:40 +0100546 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
547 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548
Raef Colese8fe6cf2020-05-26 13:07:40 +0100549out:
550 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551}
Tamas Banfe031092020-09-10 17:32:39 +0200552#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553
Fabio Utzig338a19f2018-12-03 08:37:08 -0200554/*
David Brown9bf95af2019-10-10 15:36:36 -0600555 * Check that this is a valid header. Valid means that the magic is
556 * correct, and that the sizes/offsets are "sane". Sane means that
557 * there is no overflow on the arithmetic, and that the result fits
558 * within the flash area we are in.
559 */
560static bool
561boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
562{
563 uint32_t size;
564
565 if (hdr->ih_magic != IMAGE_MAGIC) {
566 return false;
567 }
568
569 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
570 return false;
571 }
572
Dominik Ermel260ae092021-04-23 05:38:45 +0000573 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600574 return false;
575 }
576
577 return true;
578}
579
580/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200581 * Check that a memory area consists of a given value.
582 */
583static inline bool
584boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300585{
586 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200587 uint8_t *p = (uint8_t *)data;
588 for (i = 0; i < len; i++) {
589 if (val != p[i]) {
590 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300591 }
592 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200593 return true;
594}
595
596static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300597boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200598{
599 const struct flash_area *fap;
600 struct image_header *hdr;
601 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300602 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200603 int rc;
604
Fabio Utzig10ee6482019-08-01 12:04:52 -0300605 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300606 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200607 if (rc != 0) {
608 return -1;
609 }
610
611 erased_val = flash_area_erased_val(fap);
612 flash_area_close(fap);
613
Fabio Utzig10ee6482019-08-01 12:04:52 -0300614 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200615 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
616 return -1;
617 }
618
619 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300620}
621
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000622#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200623 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200624 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000625 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
626/**
David Vincze8b0b6372020-05-20 19:54:44 +0200627 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000628 *
David Vincze8b0b6372020-05-20 19:54:44 +0200629 * @param ver1 Pointer to the first image version to compare.
630 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000631 *
David Vincze8b0b6372020-05-20 19:54:44 +0200632 * @retval -1 If ver1 is strictly less than ver2.
633 * @retval 0 If the image version numbers are equal,
634 * (not including the build number).
635 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000636 */
637static int
David Vincze8b0b6372020-05-20 19:54:44 +0200638boot_version_cmp(const struct image_version *ver1,
639 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000640{
David Vincze8b0b6372020-05-20 19:54:44 +0200641 if (ver1->iv_major > ver2->iv_major) {
642 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000643 }
David Vincze8b0b6372020-05-20 19:54:44 +0200644 if (ver1->iv_major < ver2->iv_major) {
645 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000646 }
David Vincze8b0b6372020-05-20 19:54:44 +0200647 /* The major version numbers are equal, continue comparison. */
648 if (ver1->iv_minor > ver2->iv_minor) {
649 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000650 }
David Vincze8b0b6372020-05-20 19:54:44 +0200651 if (ver1->iv_minor < ver2->iv_minor) {
652 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000653 }
David Vincze8b0b6372020-05-20 19:54:44 +0200654 /* The minor version numbers are equal, continue comparison. */
655 if (ver1->iv_revision > ver2->iv_revision) {
656 return 1;
657 }
658 if (ver1->iv_revision < ver2->iv_revision) {
659 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000660 }
661
662 return 0;
663}
664#endif
665
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000666#if defined(MCUBOOT_DIRECT_XIP)
667/**
668 * Check if image in slot has been set with specific ROM address to run from
669 * and whether the slot starts at that address.
670 *
671 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
672 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
673 * header matches the slot address;
674 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
675 * does not match the slot address.
676 */
677static bool
Mark Horvathccaf7f82021-01-04 18:16:42 +0100678boot_rom_address_check(struct boot_loader_state *state,
679 struct slot_usage_t slot_usage[])
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000680{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100681 uint32_t active_slot;
682 const struct image_header *hdr;
683 uint32_t f_off;
684
685 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
686 hdr = boot_img_hdr(state, active_slot);
687 f_off = boot_img_slot_off(state, active_slot);
688
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000689 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
690 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100691 ", skipping",
692 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000693 hdr->ih_load_addr);
694
695 /* If there is address mismatch, the image is not bootable from this
696 * slot.
697 */
698 return 1;
699 }
700 return 0;
701}
702#endif
703
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300704/*
705 * Check that there is a valid image in a slot
706 *
707 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100708 * FIH_SUCCESS if image was successfully validated
709 * 1 (or its fih_int encoded form) if no bootloable image was found
710 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300711 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100712static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300713boot_validate_slot(struct boot_loader_state *state, int slot,
714 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800715{
716 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400717 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300718 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100719 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800720 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300721
Fabio Utzig10ee6482019-08-01 12:04:52 -0300722 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300723 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800724 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100725 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800726 }
727
Fabio Utzig10ee6482019-08-01 12:04:52 -0300728 hdr = boot_img_hdr(state, slot);
729 if (boot_check_header_erased(state, slot) == 0 ||
730 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300731
732#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
733 /*
734 * This fixes an issue where an image might be erased, but a trailer
735 * be left behind. It can happen if the image is in the secondary slot
736 * and did not pass validation, in which case the whole slot is erased.
737 * If during the erase operation, a reset occurs, parts of the slot
738 * might have been erased while some did not. The concerning part is
739 * the trailer because it might disable a new image from being loaded
740 * through mcumgr; so we just get rid of the trailer here, if the header
741 * is erased.
742 */
743 if (slot != BOOT_PRIMARY_SLOT) {
744 swap_erase_trailer_sectors(state, fap);
745 }
746#endif
747
David Vincze2d736ad2019-02-18 11:50:22 +0100748 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100749 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200750 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300751 }
752
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000753#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
754 if (slot != BOOT_PRIMARY_SLOT) {
755 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200756 rc = boot_version_cmp(
757 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
758 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
759 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000760 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000761 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000762 /* Image in the secondary slot does not satisfy version requirement.
763 * Erase the image and continue booting from the primary slot.
764 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100765 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000766 goto out;
767 }
768 }
769#endif
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200770 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
771 fih_rc, BOOT_CURR_IMG(state), slot);
Andrzej Puzdrowski9d4d45c2021-09-14 17:25:58 +0200772 if (fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200773 {
774 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
775 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100776 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200777 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000778 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200779 /* Image is invalid, erase it to prevent further unnecessary
780 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700781 */
782 }
David Brown098de832019-12-10 11:58:01 -0700783#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100784 BOOT_LOG_ERR("Image in the %s slot is not valid!",
785 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700786#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100787 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200788 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789 }
790
Fabio Utzig338a19f2018-12-03 08:37:08 -0200791out:
792 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100793
794 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800795}
796
David Vinczec3084132020-02-18 14:50:47 +0100797#ifdef MCUBOOT_HW_ROLLBACK_PROT
798/**
799 * Updates the stored security counter value with the image's security counter
800 * value which resides in the given slot, only if it's greater than the stored
801 * value.
802 *
803 * @param image_index Index of the image to determine which security
804 * counter to update.
805 * @param slot Slot number of the image.
806 * @param hdr Pointer to the image header structure of the image
807 * that is currently stored in the given slot.
808 *
809 * @return 0 on success; nonzero on failure.
810 */
811static int
812boot_update_security_counter(uint8_t image_index, int slot,
813 struct image_header *hdr)
814{
815 const struct flash_area *fap = NULL;
816 uint32_t img_security_cnt;
817 int rc;
818
819 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
820 &fap);
821 if (rc != 0) {
822 rc = BOOT_EFLASH;
823 goto done;
824 }
825
826 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
827 if (rc != 0) {
828 goto done;
829 }
830
831 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
832 if (rc != 0) {
833 goto done;
834 }
835
836done:
837 flash_area_close(fap);
838 return rc;
839}
840#endif /* MCUBOOT_HW_ROLLBACK_PROT */
841
Tamas Banfe031092020-09-10 17:32:39 +0200842#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200843/**
844 * Determines which swap operation to perform, if any. If it is determined
845 * that a swap operation is required, the image in the secondary slot is checked
846 * for validity. If the image in the secondary slot is invalid, it is erased,
847 * and a swap type of "none" is indicated.
848 *
849 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
850 */
851static int
852boot_validated_swap_type(struct boot_loader_state *state,
853 struct boot_status *bs)
854{
855 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100856 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200857
858 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
859 if (BOOT_IS_UPGRADE(swap_type)) {
860 /* Boot loader wants to switch to the secondary slot.
861 * Ensure image is valid.
862 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100863 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
864 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
865 if (fih_eq(fih_rc, fih_int_encode(1))) {
866 swap_type = BOOT_SWAP_TYPE_NONE;
867 } else {
868 swap_type = BOOT_SWAP_TYPE_FAIL;
869 }
David Vinczee574f2d2020-07-10 11:42:03 +0200870 }
871 }
872
873 return swap_type;
874}
David Brown94ed12c2021-05-26 16:28:14 -0600875#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200876
Christopher Collins92ea77f2016-12-12 15:59:26 -0800877/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800878 * Erases a region of flash.
879 *
Fabio Utzigba829042018-09-18 08:29:34 -0300880 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800881 * @param off The offset within the flash area to start the
882 * erase.
883 * @param sz The number of bytes to erase.
884 *
885 * @return 0 on success; nonzero on failure.
886 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300887int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300888boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800889{
Fabio Utzigba829042018-09-18 08:29:34 -0300890 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800891}
892
David Brown94ed12c2021-05-26 16:28:14 -0600893#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800894/**
895 * Copies the contents of one flash region to another. You must erase the
896 * destination region prior to calling this function.
897 *
898 * @param flash_area_id_src The ID of the source flash area.
899 * @param flash_area_id_dst The ID of the destination flash area.
900 * @param off_src The offset within the source flash area to
901 * copy from.
902 * @param off_dst The offset within the destination flash area to
903 * copy to.
904 * @param sz The number of bytes to copy.
905 *
906 * @return 0 on success; nonzero on failure.
907 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300908int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300909boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300910 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300911 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800912 uint32_t off_src, uint32_t off_dst, uint32_t sz)
913{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800914 uint32_t bytes_copied;
915 int chunk_sz;
916 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300917#ifdef MCUBOOT_ENC_IMAGES
918 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300919 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300920 size_t blk_off;
921 struct image_header *hdr;
922 uint16_t idx;
923 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300924 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300925#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800926
Marek Pietae51ec072021-07-15 14:53:10 +0200927 TARGET_STATIC uint8_t buf[1024] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300928
929#if !defined(MCUBOOT_ENC_IMAGES)
930 (void)state;
931#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800932
Christopher Collins92ea77f2016-12-12 15:59:26 -0800933 bytes_copied = 0;
934 while (bytes_copied < sz) {
935 if (sz - bytes_copied > sizeof buf) {
936 chunk_sz = sizeof buf;
937 } else {
938 chunk_sz = sz - bytes_copied;
939 }
940
941 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
942 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300943 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800944 }
945
Fabio Utzigba829042018-09-18 08:29:34 -0300946#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300947 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000948 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
949 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
950 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
951 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100952 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300953 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300954#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300955 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000956 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100957 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300958 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300959 off = off_dst;
960 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300961#else
962 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000963 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300964 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
965 }
966#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200967 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300968 blk_sz = chunk_sz;
969 idx = 0;
970 if (off + bytes_copied < hdr->ih_hdr_size) {
971 /* do not decrypt header */
972 blk_off = 0;
973 blk_sz = chunk_sz - hdr->ih_hdr_size;
974 idx = hdr->ih_hdr_size;
975 } else {
976 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
977 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300978 tlv_off = BOOT_TLV_OFF(hdr);
979 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300980 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300981 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300982 blk_sz = 0;
983 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300984 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300985 }
986 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300987 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300988 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
989 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300990 }
991 }
992#endif
993
Christopher Collins92ea77f2016-12-12 15:59:26 -0800994 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
995 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300996 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800997 }
998
999 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001000
1001 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001002 }
1003
Fabio Utzigba829042018-09-18 08:29:34 -03001004 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001005}
1006
Christopher Collins92ea77f2016-12-12 15:59:26 -08001007/**
David Vincze2d736ad2019-02-18 11:50:22 +01001008 * Overwrite primary slot with the image contained in the secondary slot.
1009 * If a prior copy operation was interrupted by a system reset, this function
1010 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001011 *
1012 * @param bs The current boot status. This function reads
1013 * this struct to determine if it is resuming
1014 * an interrupted swap operation. This
1015 * function writes the updated status to this
1016 * function on return.
1017 *
1018 * @return 0 on success; nonzero on failure.
1019 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001020#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001021static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001022boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001023{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001024 size_t sect_count;
1025 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001026 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001027 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001028 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001029 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001030 const struct flash_area *fap_primary_slot;
1031 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001032 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001033
Fabio Utzigb4f88102020-10-04 10:16:24 -03001034#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1035 uint32_t sector;
1036 uint32_t trailer_sz;
1037 uint32_t off;
1038 uint32_t sz;
1039#endif
1040
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001041 (void)bs;
1042
Fabio Utzig13d9e352017-10-05 20:32:31 -03001043#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1044 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001045 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001046 assert(rc == 0);
1047#endif
David Brown17609d82017-05-05 09:41:34 -06001048
David Vincze2d736ad2019-02-18 11:50:22 +01001049 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1050 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001051
Fabio Utzigb0f04732019-07-31 09:49:19 -03001052 image_index = BOOT_CURR_IMG(state);
1053
1054 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1055 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001056 assert (rc == 0);
1057
Fabio Utzigb0f04732019-07-31 09:49:19 -03001058 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1059 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001060 assert (rc == 0);
1061
Fabio Utzig10ee6482019-08-01 12:04:52 -03001062 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001063 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001064 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001065 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001066 assert(rc == 0);
1067
Fabio Utzig13d9e352017-10-05 20:32:31 -03001068#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001069 if ((size + this_size) >= src_size) {
1070 size += src_size - size;
1071 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001072 break;
1073 }
1074#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001075
1076 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001077 }
1078
Fabio Utzigb4f88102020-10-04 10:16:24 -03001079#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1080 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1081 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1082 sz = 0;
1083 do {
1084 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1085 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1086 sector--;
1087 } while (sz < trailer_sz);
1088
1089 rc = boot_erase_region(fap_primary_slot, off, sz);
1090 assert(rc == 0);
1091#endif
1092
Fabio Utzigba829042018-09-18 08:29:34 -03001093#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001094 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001095 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001096 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001097 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001098
Fabio Utzigba829042018-09-18 08:29:34 -03001099 if (rc < 0) {
1100 return BOOT_EBADIMAGE;
1101 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001102 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001103 return BOOT_EBADIMAGE;
1104 }
1105 }
1106#endif
1107
David Vincze2d736ad2019-02-18 11:50:22 +01001108 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1109 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001110 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001111 if (rc != 0) {
1112 return rc;
1113 }
1114
1115#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1116 rc = boot_write_magic(fap_primary_slot);
1117 if (rc != 0) {
1118 return rc;
1119 }
1120#endif
David Brown17609d82017-05-05 09:41:34 -06001121
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001122 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1123 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1124 if (rc != 0) {
1125 return rc;
1126 }
1127
David Vinczec3084132020-02-18 14:50:47 +01001128#ifdef MCUBOOT_HW_ROLLBACK_PROT
1129 /* Update the stored security counter with the new image's security counter
1130 * value. Both slots hold the new image at this point, but the secondary
1131 * slot's image header must be passed since the image headers in the
1132 * boot_data structure have not been updated yet.
1133 */
1134 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1135 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1136 if (rc != 0) {
1137 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1138 return rc;
1139 }
1140#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1141
Fabio Utzig13d9e352017-10-05 20:32:31 -03001142 /*
1143 * Erases header and trailer. The trailer is erased because when a new
1144 * image is written without a trailer as is the case when using newt, the
1145 * trailer that was left might trigger a new upgrade.
1146 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001147 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001148 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001149 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1150 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001151 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001152 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001153 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001154 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001155 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1156 last_sector),
1157 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1158 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001159 assert(rc == 0);
1160
David Vincze2d736ad2019-02-18 11:50:22 +01001161 flash_area_close(fap_primary_slot);
1162 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001163
David Vincze2d736ad2019-02-18 11:50:22 +01001164 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001165
1166 return 0;
1167}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001168#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001169
Christopher Collinsa1c12042019-05-23 14:00:28 -07001170#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001171/**
1172 * Swaps the two images in flash. If a prior copy operation was interrupted
1173 * by a system reset, this function completes that operation.
1174 *
1175 * @param bs The current boot status. This function reads
1176 * this struct to determine if it is resuming
1177 * an interrupted swap operation. This
1178 * function writes the updated status to this
1179 * function on return.
1180 *
1181 * @return 0 on success; nonzero on failure.
1182 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001183static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001184boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001185{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001186 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001187#ifdef MCUBOOT_ENC_IMAGES
1188 const struct flash_area *fap;
1189 uint8_t slot;
1190 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001191#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001192 uint32_t size;
1193 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001194 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001195 int rc;
1196
1197 /* FIXME: just do this if asked by user? */
1198
1199 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001200 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001201
Fabio Utzig12d59162019-11-28 10:01:59 -03001202 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001203 /*
1204 * No swap ever happened, so need to find the largest image which
1205 * will be used to determine the amount of sectors to swap.
1206 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001207 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001208 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001209 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001210 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001211 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001212
Fabio Utzigba829042018-09-18 08:29:34 -03001213#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001214 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001215 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001216 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001217 assert(rc >= 0);
1218
1219 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001220 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001221 assert(rc == 0);
1222 } else {
1223 rc = 0;
1224 }
1225 } else {
1226 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1227 }
1228#endif
1229
Fabio Utzig10ee6482019-08-01 12:04:52 -03001230 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001231 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001232 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001233 assert(rc == 0);
1234 }
1235
Fabio Utzigba829042018-09-18 08:29:34 -03001236#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001237 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001238 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001239 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001240 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001241 assert(rc >= 0);
1242
1243 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001244 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001245 assert(rc == 0);
1246 } else {
1247 rc = 0;
1248 }
1249 } else {
1250 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1251 }
1252#endif
1253
Fabio Utzig46490722017-09-04 15:34:32 -03001254 if (size > copy_size) {
1255 copy_size = size;
1256 }
1257
1258 bs->swap_size = copy_size;
1259 } else {
1260 /*
1261 * If a swap was under way, the swap_size should already be present
1262 * in the trailer...
1263 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001264 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001265 assert(rc == 0);
1266
1267 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001268
1269#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001270 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1271 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001272 assert(rc == 0);
1273
1274 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001275 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001276 break;
1277 }
1278 }
1279
1280 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001281 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001282 }
1283 }
1284#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001285 }
1286
Fabio Utzig12d59162019-11-28 10:01:59 -03001287 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001288
David Vincze2d736ad2019-02-18 11:50:22 +01001289#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001290 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001291 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001292 BOOT_LOG_WRN("%d status write fails performing the swap",
1293 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001294 }
1295#endif
1296
Christopher Collins92ea77f2016-12-12 15:59:26 -08001297 return 0;
1298}
David Brown17609d82017-05-05 09:41:34 -06001299#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001300
David Vinczee32483f2019-06-13 10:46:24 +02001301#if (BOOT_IMAGE_NUMBER > 1)
1302/**
1303 * Check the image dependency whether it is satisfied and modify
1304 * the swap type if necessary.
1305 *
1306 * @param dep Image dependency which has to be verified.
1307 *
1308 * @return 0 on success; nonzero on failure.
1309 */
1310static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001311boot_verify_slot_dependency(struct boot_loader_state *state,
1312 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001313{
1314 struct image_version *dep_version;
1315 size_t dep_slot;
1316 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001317 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001318
1319 /* Determine the source of the image which is the subject of
1320 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001321 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001322 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1323 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001324 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001325
David Vincze8b0b6372020-05-20 19:54:44 +02001326 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1327 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001328 /* Dependency not satisfied.
1329 * Modify the swap type to decrease the version number of the image
1330 * (which will be located in the primary slot after the boot process),
1331 * consequently the number of unsatisfied dependencies will be
1332 * decreased or remain the same.
1333 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001334 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001335 case BOOT_SWAP_TYPE_TEST:
1336 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001337 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001338 break;
1339 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001340 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001341 break;
1342 default:
1343 break;
1344 }
David Vincze8b0b6372020-05-20 19:54:44 +02001345 } else {
1346 /* Dependency satisfied. */
1347 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001348 }
1349
1350 return rc;
1351}
1352
1353/**
1354 * Read all dependency TLVs of an image from the flash and verify
1355 * one after another to see if they are all satisfied.
1356 *
1357 * @param slot Image slot number.
1358 *
1359 * @return 0 on success; nonzero on failure.
1360 */
1361static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001362boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001363{
1364 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001365 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001366 struct image_dependency dep;
1367 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001368 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001369 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001370 int rc;
1371
Fabio Utzig10ee6482019-08-01 12:04:52 -03001372 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001373 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001374 if (rc != 0) {
1375 rc = BOOT_EFLASH;
1376 goto done;
1377 }
1378
Fabio Utzig61fd8882019-09-14 20:00:20 -03001379 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1380 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001381 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001382 goto done;
1383 }
1384
Fabio Utzig61fd8882019-09-14 20:00:20 -03001385 while (true) {
1386 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1387 if (rc < 0) {
1388 return -1;
1389 } else if (rc > 0) {
1390 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001391 break;
1392 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001393
1394 if (len != sizeof(dep)) {
1395 rc = BOOT_EBADIMAGE;
1396 goto done;
1397 }
1398
1399 rc = flash_area_read(fap, off, &dep, len);
1400 if (rc != 0) {
1401 rc = BOOT_EFLASH;
1402 goto done;
1403 }
1404
1405 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1406 rc = BOOT_EBADARGS;
1407 goto done;
1408 }
1409
1410 /* Verify dependency and modify the swap type if not satisfied. */
1411 rc = boot_verify_slot_dependency(state, &dep);
1412 if (rc != 0) {
1413 /* Dependency not satisfied. */
1414 goto done;
1415 }
David Vinczee32483f2019-06-13 10:46:24 +02001416 }
1417
1418done:
1419 flash_area_close(fap);
1420 return rc;
1421}
1422
1423/**
David Vinczee32483f2019-06-13 10:46:24 +02001424 * Iterate over all the images and verify whether the image dependencies in the
1425 * TLV area are all satisfied and update the related swap type if necessary.
1426 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001427static int
1428boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001429{
Erik Johnson49063752020-02-06 09:59:31 -06001430 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001431 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001432
Fabio Utzig10ee6482019-08-01 12:04:52 -03001433 BOOT_CURR_IMG(state) = 0;
1434 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001435 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1436 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1437 slot = BOOT_SECONDARY_SLOT;
1438 } else {
1439 slot = BOOT_PRIMARY_SLOT;
1440 }
1441
1442 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001443 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001444 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001445 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001446 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001447 /* Cannot upgrade due to non-met dependencies, so disable all
1448 * image upgrades.
1449 */
1450 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1451 BOOT_CURR_IMG(state) = idx;
1452 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1453 }
1454 break;
David Vinczee32483f2019-06-13 10:46:24 +02001455 }
1456 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001457 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001458}
1459#endif /* (BOOT_IMAGE_NUMBER > 1) */
1460
Christopher Collins92ea77f2016-12-12 15:59:26 -08001461/**
David Vinczeba3bd602019-06-17 16:01:43 +02001462 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001463 *
David Vinczeba3bd602019-06-17 16:01:43 +02001464 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001465 *
1466 * @return 0 on success; nonzero on failure.
1467 */
1468static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001469boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001470{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001471 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001472#ifndef MCUBOOT_OVERWRITE_ONLY
1473 uint8_t swap_type;
1474#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001475
David Vinczeba3bd602019-06-17 16:01:43 +02001476 /* At this point there are no aborted swaps. */
1477#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001478 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001479#elif defined(MCUBOOT_BOOTSTRAP)
1480 /* Check if the image update was triggered by a bad image in the
1481 * primary slot (the validity of the image in the secondary slot had
1482 * already been checked).
1483 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001484 fih_int fih_rc = FIH_FAILURE;
1485 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1486 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1487 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001488 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001489 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001490 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001491 }
1492#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001493 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001494#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001495 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001496
1497#ifndef MCUBOOT_OVERWRITE_ONLY
1498 /* The following state needs image_ok be explicitly set after the
1499 * swap was finished to avoid a new revert.
1500 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001501 swap_type = BOOT_SWAP_TYPE(state);
1502 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1503 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001504 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001505 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001506 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001507 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001508 }
1509
David Vinczec3084132020-02-18 14:50:47 +01001510#ifdef MCUBOOT_HW_ROLLBACK_PROT
1511 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1512 /* Update the stored security counter with the new image's security
1513 * counter value. The primary slot holds the new image at this point,
1514 * but the secondary slot's image header must be passed since image
1515 * headers in the boot_data structure have not been updated yet.
1516 *
1517 * In case of a permanent image swap mcuboot will never attempt to
1518 * revert the images on the next reboot. Therefore, the security
1519 * counter must be increased right after the image upgrade.
1520 */
1521 rc = boot_update_security_counter(
1522 BOOT_CURR_IMG(state),
1523 BOOT_PRIMARY_SLOT,
1524 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1525 if (rc != 0) {
1526 BOOT_LOG_ERR("Security counter update failed after "
1527 "image upgrade.");
1528 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1529 }
1530 }
1531#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1532
Fabio Utzige575b0b2019-09-11 12:34:23 -03001533 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001534 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001535 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001536 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001537 }
David Vinczeba3bd602019-06-17 16:01:43 +02001538 }
1539#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001540
David Vinczeba3bd602019-06-17 16:01:43 +02001541 return rc;
1542}
1543
1544/**
1545 * Completes a previously aborted image swap.
1546 *
1547 * @param bs The current boot status.
1548 *
1549 * @return 0 on success; nonzero on failure.
1550 */
1551#if !defined(MCUBOOT_OVERWRITE_ONLY)
1552static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001553boot_complete_partial_swap(struct boot_loader_state *state,
1554 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001555{
1556 int rc;
1557
1558 /* Determine the type of swap operation being resumed from the
1559 * `swap-type` trailer field.
1560 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001561 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001562 assert(rc == 0);
1563
Fabio Utzig10ee6482019-08-01 12:04:52 -03001564 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001565
1566 /* The following states need image_ok be explicitly set after the
1567 * swap was finished to avoid a new revert.
1568 */
1569 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1570 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001571 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001572 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001573 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001574 }
1575 }
1576
Fabio Utzige575b0b2019-09-11 12:34:23 -03001577 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001578 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001579 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001580 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001581 }
1582 }
1583
Fabio Utzig10ee6482019-08-01 12:04:52 -03001584 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001585 BOOT_LOG_ERR("panic!");
1586 assert(0);
1587
1588 /* Loop forever... */
1589 while (1) {}
1590 }
1591
1592 return rc;
1593}
1594#endif /* !MCUBOOT_OVERWRITE_ONLY */
1595
1596#if (BOOT_IMAGE_NUMBER > 1)
1597/**
1598 * Review the validity of previously determined swap types of other images.
1599 *
1600 * @param aborted_swap The current image upgrade is a
1601 * partial/aborted swap.
1602 */
1603static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001604boot_review_image_swap_types(struct boot_loader_state *state,
1605 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001606{
1607 /* In that case if we rebooted in the middle of an image upgrade process, we
1608 * must review the validity of swap types, that were previously determined
1609 * for other images. The image_ok flag had not been set before the reboot
1610 * for any of the updated images (only the copy_done flag) and thus falsely
1611 * the REVERT swap type has been determined for the previous images that had
1612 * been updated before the reboot.
1613 *
1614 * There are two separate scenarios that we have to deal with:
1615 *
1616 * 1. The reboot has happened during swapping an image:
1617 * The current image upgrade has been determined as a
1618 * partial/aborted swap.
1619 * 2. The reboot has happened between two separate image upgrades:
1620 * In this scenario we must check the swap type of the current image.
1621 * In those cases if it is NONE or REVERT we cannot certainly determine
1622 * the fact of a reboot. In a consistent state images must move in the
1623 * same direction or stay in place, e.g. in practice REVERT and TEST
1624 * swap types cannot be present at the same time. If the swap type of
1625 * the current image is either TEST, PERM or FAIL we must review the
1626 * already determined swap types of other images and set each false
1627 * REVERT swap types to NONE (these images had been successfully
1628 * updated before the system rebooted between two separate image
1629 * upgrades).
1630 */
1631
Fabio Utzig10ee6482019-08-01 12:04:52 -03001632 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001633 /* Nothing to do */
1634 return;
1635 }
1636
1637 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001638 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1639 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001640 /* Nothing to do */
1641 return;
1642 }
1643 }
1644
Fabio Utzig10ee6482019-08-01 12:04:52 -03001645 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1646 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1647 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001648 }
1649 }
1650}
1651#endif
1652
1653/**
1654 * Prepare image to be updated if required.
1655 *
1656 * Prepare image to be updated if required with completing an image swap
1657 * operation if one was aborted and/or determining the type of the
1658 * swap operation. In case of any error set the swap type to NONE.
1659 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001660 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001661 * @param bs Pointer where the read and possibly updated
1662 * boot status can be written to.
1663 */
1664static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001665boot_prepare_image_for_update(struct boot_loader_state *state,
1666 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001667{
1668 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001669 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001670
1671 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001672 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001673 if (rc != 0) {
1674 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1675 " - too small?", BOOT_MAX_IMG_SECTORS);
1676 /* Unable to determine sector layout, continue with next image
1677 * if there is one.
1678 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001679 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001680 if (rc == BOOT_EFLASH)
1681 {
1682 /* Only return on error from the primary image flash */
1683 return;
1684 }
David Vinczeba3bd602019-06-17 16:01:43 +02001685 }
1686
1687 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001688 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001689 if (rc != 0) {
1690 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001691 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001692 BOOT_CURR_IMG(state));
1693 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001694 return;
1695 }
1696
1697 /* If the current image's slots aren't compatible, no swap is possible.
1698 * Just boot into primary slot.
1699 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001700 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001701 boot_status_reset(bs);
1702
1703#ifndef MCUBOOT_OVERWRITE_ONLY
1704 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001705 if (rc != 0) {
1706 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001707 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001708 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001709 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001710 return;
1711 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001712#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001713
Fabio Utzig74aef312019-11-28 11:05:34 -03001714#ifdef MCUBOOT_SWAP_USING_MOVE
1715 /*
1716 * Must re-read image headers because the boot status might
1717 * have been updated in the previous function call.
1718 */
1719 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001720#ifdef MCUBOOT_BOOTSTRAP
1721 /* When bootstrapping it's OK to not have image magic in the primary slot */
1722 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1723 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1724#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001725 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001726#endif
1727
Fabio Utzig74aef312019-11-28 11:05:34 -03001728 /* Continue with next image if there is one. */
1729 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1730 BOOT_CURR_IMG(state));
1731 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1732 return;
1733 }
1734#endif
1735
David Vinczeba3bd602019-06-17 16:01:43 +02001736 /* Determine if we rebooted in the middle of an image swap
1737 * operation. If a partial swap was detected, complete it.
1738 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001739 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001740
1741#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001742 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001743#endif
1744
1745#ifdef MCUBOOT_OVERWRITE_ONLY
1746 /* Should never arrive here, overwrite-only mode has
1747 * no swap state.
1748 */
1749 assert(0);
1750#else
1751 /* Determine the type of swap operation being resumed from the
1752 * `swap-type` trailer field.
1753 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001754 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001755 assert(rc == 0);
1756#endif
1757 /* Attempt to read an image header from each slot. Ensure that
1758 * image headers in slots are aligned with headers in boot_data.
1759 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001760 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001761 assert(rc == 0);
1762
1763 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001764 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001765 } else {
1766 /* There was no partial swap, determine swap type. */
1767 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001768 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001769 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001770 FIH_CALL(boot_validate_slot, fih_rc,
1771 state, BOOT_SECONDARY_SLOT, bs);
1772 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1773 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1774 } else {
1775 BOOT_SWAP_TYPE(state) = bs->swap_type;
1776 }
David Vinczeba3bd602019-06-17 16:01:43 +02001777 }
1778
1779#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001780 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001781#endif
1782
1783#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001784 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001785 /* Header checks are done first because they are
1786 * inexpensive. Since overwrite-only copies starting from
1787 * offset 0, if interrupted, it might leave a valid header
1788 * magic, so also run validation on the primary slot to be
1789 * sure it's not OK.
1790 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001791 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1792 FIH_CALL(boot_validate_slot, fih_rc,
1793 state, BOOT_PRIMARY_SLOT, bs);
1794
1795 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1796
Fabio Utzig3d77c952020-10-04 10:23:17 -03001797 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001798 FIH_CALL(boot_validate_slot, fih_rc,
1799 state, BOOT_SECONDARY_SLOT, bs);
1800
Fabio Utzig3d77c952020-10-04 10:23:17 -03001801 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001802 /* Set swap type to REVERT to overwrite the primary
1803 * slot with the image contained in secondary slot
1804 * and to trigger the explicit setting of the
1805 * image_ok flag.
1806 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001807 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001808 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001809 }
1810 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001811#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001812 }
David Vinczeba3bd602019-06-17 16:01:43 +02001813 } else {
1814 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001815 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001816 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001817}
1818
Mark Horvathccaf7f82021-01-04 18:16:42 +01001819/**
1820 * Updates the security counter for the current image.
1821 *
1822 * @param state Boot loader status information.
1823 *
1824 * @return 0 on success; nonzero on failure.
1825 */
1826static int
1827boot_update_hw_rollback_protection(struct boot_loader_state *state)
1828{
1829#ifdef MCUBOOT_HW_ROLLBACK_PROT
1830 int rc;
1831
1832 /* Update the stored security counter with the active image's security
1833 * counter value. It will only be updated if the new security counter is
1834 * greater than the stored value.
1835 *
1836 * In case of a successful image swapping when the swap type is TEST the
1837 * security counter can be increased only after a reset, when the swap
1838 * type is NONE and the image has marked itself "OK" (the image_ok flag
1839 * has been set). This way a "revert" can be performed when it's
1840 * necessary.
1841 */
1842 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1843 rc = boot_update_security_counter(
1844 BOOT_CURR_IMG(state),
1845 BOOT_PRIMARY_SLOT,
1846 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1847 if (rc != 0) {
1848 BOOT_LOG_ERR("Security counter update failed after image "
1849 "validation.");
1850 return rc;
1851 }
1852 }
1853
1854 return 0;
1855
1856#else /* MCUBOOT_HW_ROLLBACK_PROT */
1857 (void) (state);
1858
1859 return 0;
1860#endif
1861}
1862
Raef Colese8fe6cf2020-05-26 13:07:40 +01001863fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001864context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001865{
Marti Bolivar84898652017-06-13 17:20:22 -04001866 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001867 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001868 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001869 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001870 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001871 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001872 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001873
1874 /* The array of slot sectors are defined here (as opposed to file scope) so
1875 * that they don't get allocated for non-boot-loader apps. This is
1876 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001877 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001878 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001879 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1880 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001881#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001882 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001883#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001884
Fabio Utzig10ee6482019-08-01 12:04:52 -03001885 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001886 has_upgrade = false;
1887
1888#if (BOOT_IMAGE_NUMBER == 1)
1889 (void)has_upgrade;
1890#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001891
David Vinczeba3bd602019-06-17 16:01:43 +02001892 /* Iterate over all the images. By the end of the loop the swap type has
1893 * to be determined for each image and all aborted swaps have to be
1894 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001895 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001896 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001897
David Vinczeba3bd602019-06-17 16:01:43 +02001898#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1899 /* The keys used for encryption may no longer be valid (could belong to
1900 * another images). Therefore, mark them as invalid to force their reload
1901 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001902 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001903 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001904#endif
1905
Fabio Utzig10ee6482019-08-01 12:04:52 -03001906 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001907
Fabio Utzig10ee6482019-08-01 12:04:52 -03001908 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001909 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001910 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001911 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001912#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001913 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001914#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001915
1916 /* Open primary and secondary image areas for the duration
1917 * of this call.
1918 */
1919 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001920 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001921 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001922 assert(rc == 0);
1923 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001924#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001925 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001926 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001927 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001928#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001929
1930 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001931 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001932
Fabio Utzige575b0b2019-09-11 12:34:23 -03001933 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001934 has_upgrade = true;
1935 }
David Vinczeba3bd602019-06-17 16:01:43 +02001936 }
1937
David Vinczee32483f2019-06-13 10:46:24 +02001938#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001939 if (has_upgrade) {
1940 /* Iterate over all the images and verify whether the image dependencies
1941 * are all satisfied and update swap type if necessary.
1942 */
1943 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001944 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001945 /*
1946 * It was impossible to upgrade because the expected dependency version
1947 * was not available. Here we already changed the swap_type so that
1948 * instead of asserting the bootloader, we continue and no upgrade is
1949 * performed.
1950 */
1951 rc = 0;
1952 }
1953 }
David Vinczee32483f2019-06-13 10:46:24 +02001954#endif
1955
David Vinczeba3bd602019-06-17 16:01:43 +02001956 /* Iterate over all the images. At this point there are no aborted swaps
1957 * and the swap types are determined for each image. By the end of the loop
1958 * all required update operations will have been finished.
1959 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001960 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001961
1962#if (BOOT_IMAGE_NUMBER > 1)
1963#ifdef MCUBOOT_ENC_IMAGES
1964 /* The keys used for encryption may no longer be valid (could belong to
1965 * another images). Therefore, mark them as invalid to force their reload
1966 * by boot_enc_load().
1967 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001968 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001969#endif /* MCUBOOT_ENC_IMAGES */
1970
1971 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001972 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001973#endif /* (BOOT_IMAGE_NUMBER > 1) */
1974
1975 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001976 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001977
Fabio Utzig10ee6482019-08-01 12:04:52 -03001978 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001979 case BOOT_SWAP_TYPE_NONE:
1980 break;
1981
1982 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1983 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1984 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001985 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
1986 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
1987 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
1988 if (rc == BOOT_HOOK_REGULAR)
1989 {
1990 rc = boot_perform_update(state, &bs);
1991 }
David Vinczeba3bd602019-06-17 16:01:43 +02001992 assert(rc == 0);
1993 break;
1994
1995 case BOOT_SWAP_TYPE_FAIL:
1996 /* The image in secondary slot was invalid and is now erased. Ensure
1997 * we don't try to boot into it again on the next reboot. Do this by
1998 * pretending we just reverted back to primary slot.
1999 */
2000#ifndef MCUBOOT_OVERWRITE_ONLY
2001 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002002 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002003 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002004 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002005 }
2006#endif /* !MCUBOOT_OVERWRITE_ONLY */
2007 break;
2008
2009 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002010 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002011 }
2012
Fabio Utzig10ee6482019-08-01 12:04:52 -03002013 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002014 BOOT_LOG_ERR("panic!");
2015 assert(0);
2016
2017 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002018 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002019 }
2020 }
2021
2022 /* Iterate over all the images. At this point all required update operations
2023 * have finished. By the end of the loop each image in the primary slot will
2024 * have been re-validated.
2025 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002026 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2027 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002028 /* Attempt to read an image header from each slot. Ensure that image
2029 * headers in slots are aligned with headers in boot_data.
2030 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002031 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002032 if (rc != 0) {
2033 goto out;
2034 }
2035 /* Since headers were reloaded, it can be assumed we just performed
2036 * a swap or overwrite. Now the header info that should be used to
2037 * provide the data for the bootstrap, which previously was at
2038 * secondary slot, was updated to primary slot.
2039 */
2040 }
2041
2042#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002043 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2044 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002045 goto out;
2046 }
2047#else
2048 /* Even if we're not re-validating the primary slot, we could be booting
2049 * onto an empty flash chip. At least do a basic sanity check that
2050 * the magic number on the image is OK.
2051 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002052 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002053 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002054 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002055 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002056 rc = BOOT_EBADIMAGE;
2057 goto out;
2058 }
David Vinczec3084132020-02-18 14:50:47 +01002059#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2060
Mark Horvathccaf7f82021-01-04 18:16:42 +01002061 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002062 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002063 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002064 }
David Vincze1cf11b52020-03-24 07:51:09 +01002065
Mark Horvathccaf7f82021-01-04 18:16:42 +01002066 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002067 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002068 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002069 }
David Vinczeba3bd602019-06-17 16:01:43 +02002070 }
2071
Fabio Utzigf616c542019-12-19 15:23:32 -03002072 /*
2073 * Since the boot_status struct stores plaintext encryption keys, reset
2074 * them here to avoid the possibility of jumping into an image that could
2075 * easily recover them.
2076 */
2077 memset(&bs, 0, sizeof(struct boot_status));
2078
Mark Horvathccaf7f82021-01-04 18:16:42 +01002079 fill_rsp(state, NULL, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002080
Raef Colese8fe6cf2020-05-26 13:07:40 +01002081 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002082out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002083 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002084
2085 if (rc) {
2086 fih_rc = fih_int_encode(rc);
2087 }
2088
2089 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002090}
2091
Raef Colese8fe6cf2020-05-26 13:07:40 +01002092fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002093split_go(int loader_slot, int split_slot, void **entry)
2094{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002095 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002096 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002097 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002098 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002099 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002100 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002101
Christopher Collins92ea77f2016-12-12 15:59:26 -08002102 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2103 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002104 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002105 }
David Vinczeba3bd602019-06-17 16:01:43 +02002106 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2107 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002108
2109 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2110 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002111 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002112 assert(rc == 0);
2113 split_flash_id = flash_area_id_from_image_slot(split_slot);
2114 rc = flash_area_open(split_flash_id,
2115 &BOOT_IMG_AREA(&boot_data, split_slot));
2116 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002117
2118 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002119 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002120 if (rc != 0) {
2121 rc = SPLIT_GO_ERR;
2122 goto done;
2123 }
2124
Fabio Utzig12d59162019-11-28 10:01:59 -03002125 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002126 if (rc != 0) {
2127 goto done;
2128 }
2129
Christopher Collins92ea77f2016-12-12 15:59:26 -08002130 /* Don't check the bootable image flag because we could really call a
2131 * bootable or non-bootable image. Just validate that the image check
2132 * passes which is distinct from the normal check.
2133 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002134 FIH_CALL(split_image_check, fih_rc,
2135 boot_img_hdr(&boot_data, split_slot),
2136 BOOT_IMG_AREA(&boot_data, split_slot),
2137 boot_img_hdr(&boot_data, loader_slot),
2138 BOOT_IMG_AREA(&boot_data, loader_slot));
2139 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002140 goto done;
2141 }
2142
Marti Bolivarea088872017-06-12 17:10:49 -04002143 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002144 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002145 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002146 rc = SPLIT_GO_OK;
2147
2148done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002149 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2150 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002151 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002152
2153 if (rc) {
2154 fih_rc = fih_int_encode(rc);
2155 }
2156
2157 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002158}
David Vinczee574f2d2020-07-10 11:42:03 +02002159
Tamas Banfe031092020-09-10 17:32:39 +02002160#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002161
Mark Horvathccaf7f82021-01-04 18:16:42 +01002162#define NO_ACTIVE_SLOT UINT32_MAX
2163
David Vinczee574f2d2020-07-10 11:42:03 +02002164/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002165 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002166 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002167 * @param state Boot loader status information.
2168 * @param slot_usage Structure to fill with information about the available
2169 * slots.
David Vinczee574f2d2020-07-10 11:42:03 +02002170 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002171 * @return 0 on success; nonzero on failure.
2172 */
2173static int
2174boot_get_slot_usage(struct boot_loader_state *state,
2175 struct slot_usage_t slot_usage[])
2176{
2177 uint32_t slot;
2178 int fa_id;
2179 int rc;
2180 struct image_header *hdr = NULL;
2181
2182 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2183 /* Open all the slots */
2184 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2185 fa_id = flash_area_id_from_multi_image_slot(
2186 BOOT_CURR_IMG(state), slot);
2187 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2188 assert(rc == 0);
2189 }
2190
2191 /* Attempt to read an image header from each slot. */
2192 rc = boot_read_image_headers(state, false, NULL);
2193 if (rc != 0) {
2194 BOOT_LOG_WRN("Failed reading image headers.");
2195 return rc;
2196 }
2197
2198 /* Check headers in all slots */
2199 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2200 hdr = boot_img_hdr(state, slot);
2201
2202 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2203 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
2204 BOOT_LOG_IMAGE_INFO(slot, hdr);
2205 } else {
2206 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
2207 BOOT_LOG_INF("Image %d %s slot: Image not found",
2208 BOOT_CURR_IMG(state),
2209 (slot == BOOT_PRIMARY_SLOT)
2210 ? "Primary" : "Secondary");
2211 }
2212 }
2213
2214 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2215 }
2216
2217 return 0;
2218}
2219
2220/**
2221 * Finds the slot containing the image with the highest version number for the
2222 * current image.
2223 *
2224 * @param state Boot loader status information.
2225 * @param slot_usage Information about the active and available slots.
2226 *
2227 * @return NO_ACTIVE_SLOT if no available slot found, number of
2228 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002229 */
2230static uint32_t
Mark Horvathccaf7f82021-01-04 18:16:42 +01002231find_slot_with_highest_version(struct boot_loader_state *state,
2232 struct slot_usage_t slot_usage[])
David Vinczee574f2d2020-07-10 11:42:03 +02002233{
David Vinczee574f2d2020-07-10 11:42:03 +02002234 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002235 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2236 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002237
Mark Horvathccaf7f82021-01-04 18:16:42 +01002238 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2239 if (slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
2240 if (candidate_slot == NO_ACTIVE_SLOT) {
2241 candidate_slot = slot;
2242 } else {
2243 rc = boot_version_cmp(
2244 &boot_img_hdr(state, slot)->ih_ver,
2245 &boot_img_hdr(state, candidate_slot)->ih_ver);
2246 if (rc == 1) {
2247 /* The version of the image being examined is greater than
2248 * the version of the current candidate.
2249 */
2250 candidate_slot = slot;
2251 }
2252 }
David Vinczee574f2d2020-07-10 11:42:03 +02002253 }
2254 }
2255
Mark Horvathccaf7f82021-01-04 18:16:42 +01002256 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002257}
2258
Mark Horvathccaf7f82021-01-04 18:16:42 +01002259#ifdef MCUBOOT_HAVE_LOGGING
2260/**
2261 * Prints the state of the loaded images.
2262 *
2263 * @param state Boot loader status information.
2264 * @param slot_usage Information about the active and available slots.
2265 */
2266static void
2267print_loaded_images(struct boot_loader_state *state,
2268 struct slot_usage_t slot_usage[])
2269{
2270 uint32_t active_slot;
2271
David Brown695e5912021-05-24 16:58:01 -06002272 (void)state;
2273
Mark Horvathccaf7f82021-01-04 18:16:42 +01002274 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2275 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2276
2277 BOOT_LOG_INF("Image %d loaded from the %s slot",
2278 BOOT_CURR_IMG(state),
2279 (active_slot == BOOT_PRIMARY_SLOT) ?
2280 "primary" : "secondary");
2281 }
2282}
2283#endif
2284
David Vincze1c456242021-06-29 15:25:24 +02002285#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002286/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002287 * Checks whether the active slot of the current image was previously selected
2288 * to run. Erases the image if it was selected but its execution failed,
2289 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002290 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002291 * @param state Boot loader status information.
2292 * @param slot_usage Information about the active and available slots.
David Vincze505fba22020-10-22 13:53:29 +02002293 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002294 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002295 */
2296static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002297boot_select_or_erase(struct boot_loader_state *state,
2298 struct slot_usage_t slot_usage[])
David Vincze505fba22020-10-22 13:53:29 +02002299{
2300 const struct flash_area *fap;
2301 int fa_id;
2302 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002303 uint32_t active_slot;
2304 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002305
Mark Horvathccaf7f82021-01-04 18:16:42 +01002306 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2307
2308 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002309 rc = flash_area_open(fa_id, &fap);
2310 assert(rc == 0);
2311
Mark Horvathccaf7f82021-01-04 18:16:42 +01002312 active_swap_state = &(slot_usage[BOOT_CURR_IMG(state)].swap_state);
2313
2314 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2315 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002316 assert(rc == 0);
2317
Mark Horvathccaf7f82021-01-04 18:16:42 +01002318 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2319 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2320 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002321 /*
2322 * A reboot happened without the image being confirmed at
2323 * runtime or its trailer is corrupted/invalid. Erase the image
2324 * to prevent it from being selected again on the next reboot.
2325 */
2326 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002327 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002328 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002329 assert(rc == 0);
2330
2331 flash_area_close(fap);
2332 rc = -1;
2333 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002334 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2335 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002336 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2337 "value was neither 'set' nor 'unset', but 'bad'.");
2338 }
2339 /*
2340 * Set the copy_done flag, indicating that the image has been
2341 * selected to boot. It can be set in advance, before even
2342 * validating the image, because in case the validation fails, the
2343 * entire image slot will be erased (including the trailer).
2344 */
2345 rc = boot_write_copy_done(fap);
2346 if (rc != 0) {
2347 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002348 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002349 "primary" : "secondary");
2350 rc = 0;
2351 }
2352 }
2353 flash_area_close(fap);
2354 }
2355
2356 return rc;
2357}
David Vincze1c456242021-06-29 15:25:24 +02002358#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002359
Tamas Banfe031092020-09-10 17:32:39 +02002360#ifdef MCUBOOT_RAM_LOAD
2361
Mark Horvathccaf7f82021-01-04 18:16:42 +01002362#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002363#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2364#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2365#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002366#endif
Tamas Banfe031092020-09-10 17:32:39 +02002367
2368/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002369 * Verifies that the active slot of the current image can be loaded within the
2370 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002371 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002372 * @param state Boot loader status information.
2373 * @param slot_usage Information about the active and available slots.
Tamas Banfe031092020-09-10 17:32:39 +02002374 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002375 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002376 */
2377static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002378boot_verify_ram_load_address(struct boot_loader_state *state,
2379 struct slot_usage_t slot_usage[])
Tamas Banfe031092020-09-10 17:32:39 +02002380{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002381 uint32_t img_dst;
2382 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002383 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002384 uint32_t exec_ram_start;
2385 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002386
2387 (void)state;
2388
Mark Horvathccaf7f82021-01-04 18:16:42 +01002389#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2390 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002391
Mark Horvathccaf7f82021-01-04 18:16:42 +01002392 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2393 &exec_ram_size);
2394 if (rc != 0) {
2395 return BOOT_EBADSTATUS;
2396 }
2397#else
2398 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2399 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2400#endif
2401
2402 img_dst = slot_usage[BOOT_CURR_IMG(state)].img_dst;
2403 img_sz = slot_usage[BOOT_CURR_IMG(state)].img_sz;
2404
2405 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002406 return BOOT_EBADIMAGE;
2407 }
2408
2409 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2410 return BOOT_EBADIMAGE;
2411 }
2412
Mark Horvathccaf7f82021-01-04 18:16:42 +01002413 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002414 return BOOT_EBADIMAGE;
2415 }
2416
2417 return 0;
2418}
2419
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002420#ifdef MCUBOOT_ENC_IMAGES
2421
2422/**
2423 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2424 *
2425 * @param state Boot loader status information.
2426 * @param slot The flash slot of the image to be copied to SRAM.
2427 * @param hdr The image header.
2428 * @param src_sz Size of the image.
2429 * @param img_dst Pointer to the address at which the image needs to be
2430 * copied to SRAM.
2431 *
2432 * @return 0 on success; nonzero on failure.
2433 */
2434static int
2435boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2436 uint32_t slot, struct image_header *hdr,
2437 uint32_t src_sz, uint32_t img_dst)
2438{
2439 /* The flow for the decryption and copy of the image is as follows :
2440 * 1. The whole image is copied to the RAM (header + payload + TLV).
2441 * 2. The encryption key is loaded from the TLV in flash.
2442 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2443 * is 1024 bytes). Only the payload section is decrypted.
2444 * 4. The image is authenticated in RAM.
2445 */
2446 const struct flash_area *fap_src = NULL;
2447 struct boot_status bs;
2448 uint32_t blk_off;
2449 uint32_t tlv_off;
2450 uint32_t blk_sz;
2451 uint32_t bytes_copied = hdr->ih_hdr_size;
2452 uint32_t chunk_sz;
2453 uint32_t max_sz = 1024;
2454 uint16_t idx;
2455 uint8_t image_index;
2456 uint8_t * cur_dst;
2457 int area_id;
2458 int rc;
2459 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2460
2461 image_index = BOOT_CURR_IMG(state);
2462 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2463 rc = flash_area_open(area_id, &fap_src);
2464 if (rc != 0){
2465 return BOOT_EFLASH;
2466 }
2467
2468 tlv_off = BOOT_TLV_OFF(hdr);
2469
2470 /* Copying the whole image in RAM */
2471 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2472 if (rc != 0) {
2473 goto done;
2474 }
2475
2476 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2477 if (rc < 0) {
2478 goto done;
2479 }
2480
2481 /* if rc > 0 then the key has already been loaded */
2482 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2483 goto done;
2484 }
2485
2486 /* Starting at the end of the header as the header section is not encrypted */
2487 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2488 if (src_sz - bytes_copied > max_sz) {
2489 chunk_sz = max_sz;
2490 } else {
2491 chunk_sz = src_sz - bytes_copied;
2492 }
2493
2494 cur_dst = ram_dst + bytes_copied;
2495 blk_sz = chunk_sz;
2496 idx = 0;
2497 if (bytes_copied + chunk_sz > tlv_off) {
2498 /* Going over TLV section
2499 * Part of the chunk is encrypted payload */
2500 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2501 blk_sz = tlv_off - (bytes_copied);
2502 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2503 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2504 blk_off, cur_dst);
2505 } else {
2506 /* Image encrypted payload section */
2507 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2508 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2509 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2510 blk_off, cur_dst);
2511 }
2512
2513 bytes_copied += chunk_sz;
2514 }
2515 rc = 0;
2516
2517done:
2518 flash_area_close(fap_src);
2519
2520 return rc;
2521}
2522
2523#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002524/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002525 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002526 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002527 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002528 * @param slot The flash slot of the image to be copied to SRAM.
2529 * @param img_dst The address at which the image needs to be copied to
2530 * SRAM.
2531 * @param img_sz The size of the image that needs to be copied to SRAM.
2532 *
2533 * @return 0 on success; nonzero on failure.
2534 */
2535static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002536boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2537 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002538{
2539 int rc;
2540 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002541 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002542
Mark Horvathccaf7f82021-01-04 18:16:42 +01002543#if (BOOT_IMAGE_NUMBER == 1)
2544 (void)state;
2545#endif
2546
2547 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2548
2549 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002550 if (rc != 0) {
2551 return BOOT_EFLASH;
2552 }
2553
2554 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002555 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002556 if (rc != 0) {
2557 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2558 }
2559
2560 flash_area_close(fap_src);
2561
2562 return rc;
2563}
2564
Mark Horvathccaf7f82021-01-04 18:16:42 +01002565#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002566/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002567 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002568 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002569 * @param start_a Start of the A region.
2570 * @param end_a End of the A region.
2571 * @param start_b Start of the B region.
2572 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002573 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002574 * @return true if there is overlap; false otherwise.
2575 */
2576static bool
2577do_regions_overlap(uint32_t start_a, uint32_t end_a,
2578 uint32_t start_b, uint32_t end_b)
2579{
2580 if (start_b > end_a) {
2581 return false;
2582 } else if (start_b >= start_a) {
2583 return true;
2584 } else if (end_b > start_a) {
2585 return true;
2586 }
2587
2588 return false;
2589}
2590
2591/**
2592 * Checks if the image we want to load to memory overlap with an already
2593 * ramloaded image.
2594 *
2595 * @param slot_usage Information about the active and available slots.
2596 * @param image_id_to_check The ID of the image we would like to load.
2597 *
2598 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002599 */
2600static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002601boot_check_ram_load_overlapping(struct slot_usage_t slot_usage[],
2602 uint32_t image_id_to_check)
Tamas Banfe031092020-09-10 17:32:39 +02002603{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002604 uint32_t i;
2605
2606 uint32_t start_a;
2607 uint32_t end_a;
2608 uint32_t start_b;
2609 uint32_t end_b;
2610
2611 start_a = slot_usage[image_id_to_check].img_dst;
2612 /* Safe to add here, values are already verified in
2613 * boot_verify_ram_load_address() */
2614 end_a = start_a + slot_usage[image_id_to_check].img_sz;
2615
2616 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
2617 if (slot_usage[i].active_slot == NO_ACTIVE_SLOT
2618 || i == image_id_to_check) {
2619 continue;
2620 }
2621
2622 start_b = slot_usage[i].img_dst;
2623 /* Safe to add here, values are already verified in
2624 * boot_verify_ram_load_address() */
2625 end_b = start_b + slot_usage[i].img_sz;
2626
2627 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2628 return -1;
2629 }
2630 }
2631
2632 return 0;
2633}
2634#endif
2635
2636/**
2637 * Loads the active slot of the current image into SRAM. The load address and
2638 * image size is extracted from the image header.
2639 *
2640 * @param state Boot loader status information.
2641 * @param slot_usage Information about the active and available slots.
2642 *
2643 * @return 0 on success; nonzero on failure.
2644 */
2645static int
2646boot_load_image_to_sram(struct boot_loader_state *state,
2647 struct slot_usage_t slot_usage[])
2648{
2649 uint32_t active_slot;
2650 struct image_header *hdr = NULL;
2651 uint32_t img_dst;
2652 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002653 int rc;
2654
Mark Horvathccaf7f82021-01-04 18:16:42 +01002655 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2656 hdr = boot_img_hdr(state, active_slot);
2657
Tamas Banfe031092020-09-10 17:32:39 +02002658 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2659
Mark Horvathccaf7f82021-01-04 18:16:42 +01002660 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002661
Mark Horvathccaf7f82021-01-04 18:16:42 +01002662 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002663 if (rc != 0) {
2664 return rc;
2665 }
2666
Mark Horvathccaf7f82021-01-04 18:16:42 +01002667 slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2668 slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
2669
2670 rc = boot_verify_ram_load_address(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002671 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002672 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002673 return rc;
2674 }
2675
Mark Horvathccaf7f82021-01-04 18:16:42 +01002676#if (BOOT_IMAGE_NUMBER > 1)
2677 rc = boot_check_ram_load_overlapping(slot_usage, BOOT_CURR_IMG(state));
2678 if (rc != 0) {
2679 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2680 another image.", img_dst);
2681 return rc;
2682 }
2683#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002684#ifdef MCUBOOT_ENC_IMAGES
2685 /* decrypt image if encrypted and copy it to RAM */
2686 if (IS_ENCRYPTED(hdr)) {
2687 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2688 } else {
2689 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2690 }
2691#else
Tamas Banfe031092020-09-10 17:32:39 +02002692 /* Copy image to the load address from where it currently resides in
2693 * flash.
2694 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002695 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002696#endif
Tamas Banfe031092020-09-10 17:32:39 +02002697 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002698 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002699 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002700 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002701 }
2702 } else {
2703 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2704 * MCUBOOT_RAM_LOAD is set.
2705 */
2706 rc = BOOT_EBADIMAGE;
2707 }
2708
Mark Horvathccaf7f82021-01-04 18:16:42 +01002709 if (rc != 0) {
2710 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2711 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2712 }
2713
Tamas Banfe031092020-09-10 17:32:39 +02002714 return rc;
2715}
2716
2717/**
2718 * Removes an image from SRAM, by overwriting it with zeros.
2719 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002720 * @param state Boot loader status information.
2721 * @param slot_usage Information about the active and available slots.
2722 *
2723 * @return 0 on success; nonzero on failure.
2724 */
2725static inline int
2726boot_remove_image_from_sram(struct boot_loader_state *state,
2727 struct slot_usage_t slot_usage[])
2728{
David Brown695e5912021-05-24 16:58:01 -06002729 (void)state;
2730
Mark Horvathccaf7f82021-01-04 18:16:42 +01002731 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
2732 slot_usage[BOOT_CURR_IMG(state)].img_dst);
2733
David Brown695e5912021-05-24 16:58:01 -06002734 memset((void*)(IMAGE_RAM_BASE + slot_usage[BOOT_CURR_IMG(state)].img_dst),
2735 0, slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002736
2737 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2738 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2739
2740 return 0;
2741}
2742
2743/**
2744 * Removes an image from flash by erasing the corresponding flash area
2745 *
2746 * @param state Boot loader status information.
2747 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002748 *
2749 * @return 0 on success; nonzero on failure.
2750 */
2751static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002752boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002753{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002754 int area_id;
2755 int rc;
2756 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002757
David Brown695e5912021-05-24 16:58:01 -06002758 (void)state;
2759
Mark Horvathccaf7f82021-01-04 18:16:42 +01002760 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2761 slot);
2762 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2763 rc = flash_area_open(area_id, &fap);
2764 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002765 flash_area_erase(fap, 0, flash_area_get_size(fap));
Mark Horvathccaf7f82021-01-04 18:16:42 +01002766 }
2767
2768 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002769}
2770#endif /* MCUBOOT_RAM_LOAD */
2771
Mark Horvathccaf7f82021-01-04 18:16:42 +01002772#if (BOOT_IMAGE_NUMBER > 1)
2773/**
2774 * Checks the image dependency whether it is satisfied.
2775 *
2776 * @param state Boot loader status information.
2777 * @param slot_usage Information about the active and available slots.
2778 * @param dep Image dependency which has to be verified.
2779 *
2780 * @return 0 if dependencies are met; nonzero otherwise.
2781 */
2782static int
2783boot_verify_slot_dependency(struct boot_loader_state *state,
2784 struct slot_usage_t slot_usage[],
2785 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002786{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002787 struct image_version *dep_version;
2788 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002789 int rc;
2790
Mark Horvathccaf7f82021-01-04 18:16:42 +01002791 /* Determine the source of the image which is the subject of
2792 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002793 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002794 dep_slot = slot_usage[dep->image_id].active_slot;
2795 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2796
2797 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2798 if (rc >= 0) {
2799 /* Dependency satisfied. */
2800 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002801 }
2802
Mark Horvathccaf7f82021-01-04 18:16:42 +01002803 return rc;
2804}
2805
2806/**
2807 * Reads all dependency TLVs of an image and verifies one after another to see
2808 * if they are all satisfied.
2809 *
2810 * @param state Boot loader status information.
2811 * @param slot_usage Information about the active and available slots.
2812 *
2813 * @return 0 if dependencies are met; nonzero otherwise.
2814 */
2815static int
2816boot_verify_slot_dependencies(struct boot_loader_state *state,
2817 struct slot_usage_t slot_usage[])
2818{
2819 uint32_t active_slot;
2820 const struct flash_area *fap;
2821 struct image_tlv_iter it;
2822 struct image_dependency dep;
2823 uint32_t off;
2824 uint16_t len;
2825 int area_id;
2826 int rc;
2827
2828 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2829
2830 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2831 active_slot);
2832 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002833 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002834 rc = BOOT_EFLASH;
2835 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002836 }
2837
Mark Horvathccaf7f82021-01-04 18:16:42 +01002838 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2839 IMAGE_TLV_DEPENDENCY, true);
2840 if (rc != 0) {
2841 goto done;
2842 }
David Vinczee574f2d2020-07-10 11:42:03 +02002843
Mark Horvathccaf7f82021-01-04 18:16:42 +01002844 while (true) {
2845 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2846 if (rc < 0) {
2847 return -1;
2848 } else if (rc > 0) {
2849 rc = 0;
2850 break;
2851 }
David Vinczee574f2d2020-07-10 11:42:03 +02002852
Mark Horvathccaf7f82021-01-04 18:16:42 +01002853 if (len != sizeof(dep)) {
2854 rc = BOOT_EBADIMAGE;
2855 goto done;
2856 }
2857
2858 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2859 fap, off, &dep, len);
2860 if (rc != 0) {
2861 rc = BOOT_EFLASH;
2862 goto done;
2863 }
2864
2865 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2866 rc = BOOT_EBADARGS;
2867 goto done;
2868 }
2869
2870 rc = boot_verify_slot_dependency(state, slot_usage, &dep);
2871 if (rc != 0) {
2872 /* Dependency not satisfied. */
2873 goto done;
2874 }
2875 }
2876
2877done:
2878 flash_area_close(fap);
2879 return rc;
2880}
2881
2882/**
2883 * Checks the dependency of all the active slots. If an image found with
2884 * invalid or not satisfied dependencies the image is removed from SRAM (in
2885 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2886 *
2887 * @param state Boot loader status information.
2888 * @param slot_usage Information about the active and available slots.
2889 *
2890 * @return 0 if dependencies are met; nonzero otherwise.
2891 */
2892static int
2893boot_verify_dependencies(struct boot_loader_state *state,
2894 struct slot_usage_t slot_usage[])
2895{
2896 int rc = -1;
2897 uint32_t active_slot;
2898
2899 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2900 rc = boot_verify_slot_dependencies(state, slot_usage);
2901 if (rc != 0) {
2902 /* Dependencies not met or invalid dependencies. */
2903
2904#ifdef MCUBOOT_RAM_LOAD
2905 boot_remove_image_from_sram(state, slot_usage);
2906#endif /* MCUBOOT_RAM_LOAD */
2907
2908 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2909 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2910 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2911
2912 return rc;
2913 }
2914 }
2915
2916 return rc;
2917}
2918#endif /* (BOOT_IMAGE_NUMBER > 1) */
2919
2920/**
2921 * Tries to load a slot for all the images with validation.
2922 *
2923 * @param state Boot loader status information.
2924 * @param slot_usage Information about the active and available slots.
2925 *
2926 * @return 0 on success; nonzero on failure.
2927 */
2928fih_int
2929boot_load_and_validate_images(struct boot_loader_state *state,
2930 struct slot_usage_t slot_usage[])
2931{
2932 uint32_t active_slot;
2933 int rc;
2934 fih_int fih_rc;
2935
2936 /* Go over all the images and try to load one */
2937 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2938 /* All slots tried until a valid image found. Breaking from this loop
2939 * means that a valid image found or already loaded. If no slot is
2940 * found the function returns with error code. */
2941 while (true) {
2942
2943 /* Go over all the slots and try to load one */
2944 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2945 if (active_slot != NO_ACTIVE_SLOT){
2946 /* A slot is already active, go to next image. */
2947 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002948 }
David Vincze505fba22020-10-22 13:53:29 +02002949
Mark Horvathccaf7f82021-01-04 18:16:42 +01002950 active_slot = find_slot_with_highest_version(state,
2951 slot_usage);
2952 if (active_slot == NO_ACTIVE_SLOT) {
2953 BOOT_LOG_INF("No slot to load for image %d",
2954 BOOT_CURR_IMG(state));
2955 FIH_RET(FIH_FAILURE);
2956 }
2957
2958 /* Save the number of the active slot. */
2959 slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
2960
2961#ifdef MCUBOOT_DIRECT_XIP
2962 rc = boot_rom_address_check(state, slot_usage);
2963 if (rc != 0) {
2964 /* The image is placed in an unsuitable slot. */
2965 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2966 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2967 continue;
2968 }
George Becksteind4d90f82021-05-11 02:00:00 -04002969
David Vincze505fba22020-10-22 13:53:29 +02002970#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002971 rc = boot_select_or_erase(state, slot_usage);
David Vincze505fba22020-10-22 13:53:29 +02002972 if (rc != 0) {
2973 /* The selected image slot has been erased. */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002974 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2975 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02002976 continue;
2977 }
2978#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02002979#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02002980
Tamas Banfe031092020-09-10 17:32:39 +02002981#ifdef MCUBOOT_RAM_LOAD
2982 /* Image is first loaded to RAM and authenticated there in order to
2983 * prevent TOCTOU attack during image copy. This could be applied
2984 * when loading images from external (untrusted) flash to internal
2985 * (trusted) RAM and image is authenticated before copying.
2986 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002987 rc = boot_load_image_to_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002988 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002989 /* Image cannot be ramloaded. */
2990 boot_remove_image_from_flash(state, active_slot);
2991 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2992 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02002993 continue;
Tamas Banfe031092020-09-10 17:32:39 +02002994 }
2995#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002996
2997 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
2998 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2999 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003000#ifdef MCUBOOT_RAM_LOAD
Mark Horvathccaf7f82021-01-04 18:16:42 +01003001 boot_remove_image_from_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02003002#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003003 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3004 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
3005 continue;
David Vincze505fba22020-10-22 13:53:29 +02003006 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003007
3008 /* Valid image loaded from a slot, go to next image. */
3009 break;
3010 }
3011 }
3012
3013 FIH_RET(FIH_SUCCESS);
3014}
3015
3016/**
3017 * Updates the security counter for the current image.
3018 *
3019 * @param state Boot loader status information.
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003020 * @param slot_usage Information about the active and available slots.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003021 *
3022 * @return 0 on success; nonzero on failure.
3023 */
3024static int
3025boot_update_hw_rollback_protection(struct boot_loader_state *state,
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003026 const struct slot_usage_t slot_usage[])
Mark Horvathccaf7f82021-01-04 18:16:42 +01003027{
3028#ifdef MCUBOOT_HW_ROLLBACK_PROT
3029 int rc;
3030
3031 /* Update the stored security counter with the newer (active) image's
3032 * security counter value.
3033 */
David Vincze1c456242021-06-29 15:25:24 +02003034#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003035 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3036 * security counter can be increased only after reboot, if the image
3037 * has been confirmed at runtime (the image_ok flag has been set).
3038 * This way a 'revert' can be performed when it's necessary.
3039 */
3040 if (slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003041#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003042 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
3043 slot_usage[BOOT_CURR_IMG(state)].active_slot,
3044 boot_img_hdr(state, slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003045 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003046 BOOT_LOG_ERR("Security counter update failed after image "
3047 "validation.");
3048 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003049 }
David Vincze1c456242021-06-29 15:25:24 +02003050#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003051 }
3052#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003053
Mark Horvathccaf7f82021-01-04 18:16:42 +01003054 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003055
Mark Horvathccaf7f82021-01-04 18:16:42 +01003056#else /* MCUBOOT_HW_ROLLBACK_PROT */
3057 (void) (state);
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003058 (void) (slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003059 return 0;
3060#endif
3061}
3062
3063fih_int
3064context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3065{
3066 struct slot_usage_t slot_usage[BOOT_IMAGE_NUMBER];
3067 int rc;
David Brown695e5912021-05-24 16:58:01 -06003068 fih_int fih_rc = fih_int_encode(0);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003069
3070 memset(state, 0, sizeof(struct boot_loader_state));
3071 memset(slot_usage, 0, sizeof(struct slot_usage_t) * BOOT_IMAGE_NUMBER);
3072
3073 rc = boot_get_slot_usage(state, slot_usage);
3074 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003075 goto out;
3076 }
3077
Mark Horvathccaf7f82021-01-04 18:16:42 +01003078#if (BOOT_IMAGE_NUMBER > 1)
3079 while (true) {
3080#endif
3081 FIH_CALL(boot_load_and_validate_images, fih_rc, state, slot_usage);
3082 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3083 goto out;
3084 }
3085
3086#if (BOOT_IMAGE_NUMBER > 1)
3087 rc = boot_verify_dependencies(state, slot_usage);
3088 if (rc != 0) {
3089 /* Dependency check failed for an image, it has been removed from
3090 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3091 * unavailable. Try to load an image from another slot.
3092 */
3093 continue;
3094 }
3095 /* Dependency check was successful. */
3096 break;
3097 }
3098#endif
3099
3100 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003101 rc = boot_update_hw_rollback_protection(state, slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003102 if (rc != 0) {
3103 goto out;
3104 }
3105
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003106 rc = boot_add_shared_data(state, slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003107 if (rc != 0) {
3108 goto out;
3109 }
3110 }
3111
3112 /* All image loaded successfully. */
3113#ifdef MCUBOOT_HAVE_LOGGING
3114 print_loaded_images(state, slot_usage);
3115#endif
3116
3117 fill_rsp(state, slot_usage, rsp);
3118
David Vinczee574f2d2020-07-10 11:42:03 +02003119out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003120 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003121
Mark Horvathccaf7f82021-01-04 18:16:42 +01003122 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3123 fih_rc = fih_int_encode(rc);
3124 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003125
Mark Horvathccaf7f82021-01-04 18:16:42 +01003126 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003127}
Tamas Banfe031092020-09-10 17:32:39 +02003128#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003129
3130/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003131 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003132 * appropriate, and tells you what address to boot from.
3133 *
3134 * @param rsp On success, indicates how booting should occur.
3135 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003136 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003137 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003138fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003139boot_go(struct boot_rsp *rsp)
3140{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003141 fih_int fih_rc = FIH_FAILURE;
3142 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3143 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003144}