blob: b47edf26edaf34d1809fc4a2b8a1386d6fe7e759 [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
502#ifdef MCUBOOT_ENC_IMAGES
503 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300504 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300505 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100506 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300507 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300508 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100509 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300510 }
511 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300512#endif
513
Raef Colese8fe6cf2020-05-26 13:07:40 +0100514 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
515 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300516
Raef Colese8fe6cf2020-05-26 13:07:40 +0100517 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518}
519
Tamas Banfe031092020-09-10 17:32:39 +0200520#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100521static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800522split_image_check(struct image_header *app_hdr,
523 const struct flash_area *app_fap,
524 struct image_header *loader_hdr,
525 const struct flash_area *loader_fap)
526{
527 static void *tmpbuf;
528 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100529 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530
531 if (!tmpbuf) {
532 tmpbuf = malloc(BOOT_TMPBUF_SZ);
533 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100534 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800535 }
536 }
537
Raef Colese8fe6cf2020-05-26 13:07:40 +0100538 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
539 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
540 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
541 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542 }
543
Raef Colese8fe6cf2020-05-26 13:07:40 +0100544 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
545 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
Raef Colese8fe6cf2020-05-26 13:07:40 +0100547out:
548 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800549}
Tamas Banfe031092020-09-10 17:32:39 +0200550#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800551
Fabio Utzig338a19f2018-12-03 08:37:08 -0200552/*
David Brown9bf95af2019-10-10 15:36:36 -0600553 * Check that this is a valid header. Valid means that the magic is
554 * correct, and that the sizes/offsets are "sane". Sane means that
555 * there is no overflow on the arithmetic, and that the result fits
556 * within the flash area we are in.
557 */
558static bool
559boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
560{
561 uint32_t size;
562
563 if (hdr->ih_magic != IMAGE_MAGIC) {
564 return false;
565 }
566
567 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
568 return false;
569 }
570
Dominik Ermel260ae092021-04-23 05:38:45 +0000571 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600572 return false;
573 }
574
575 return true;
576}
577
578/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200579 * Check that a memory area consists of a given value.
580 */
581static inline bool
582boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300583{
584 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200585 uint8_t *p = (uint8_t *)data;
586 for (i = 0; i < len; i++) {
587 if (val != p[i]) {
588 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300589 }
590 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200591 return true;
592}
593
594static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300595boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200596{
597 const struct flash_area *fap;
598 struct image_header *hdr;
599 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300600 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200601 int rc;
602
Fabio Utzig10ee6482019-08-01 12:04:52 -0300603 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300604 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200605 if (rc != 0) {
606 return -1;
607 }
608
609 erased_val = flash_area_erased_val(fap);
610 flash_area_close(fap);
611
Fabio Utzig10ee6482019-08-01 12:04:52 -0300612 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200613 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
614 return -1;
615 }
616
617 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300618}
619
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000620#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200621 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200622 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000623 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
624/**
David Vincze8b0b6372020-05-20 19:54:44 +0200625 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000626 *
David Vincze8b0b6372020-05-20 19:54:44 +0200627 * @param ver1 Pointer to the first image version to compare.
628 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000629 *
David Vincze8b0b6372020-05-20 19:54:44 +0200630 * @retval -1 If ver1 is strictly less than ver2.
631 * @retval 0 If the image version numbers are equal,
632 * (not including the build number).
633 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000634 */
635static int
David Vincze8b0b6372020-05-20 19:54:44 +0200636boot_version_cmp(const struct image_version *ver1,
637 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000638{
David Vincze8b0b6372020-05-20 19:54:44 +0200639 if (ver1->iv_major > ver2->iv_major) {
640 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000641 }
David Vincze8b0b6372020-05-20 19:54:44 +0200642 if (ver1->iv_major < ver2->iv_major) {
643 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000644 }
David Vincze8b0b6372020-05-20 19:54:44 +0200645 /* The major version numbers are equal, continue comparison. */
646 if (ver1->iv_minor > ver2->iv_minor) {
647 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000648 }
David Vincze8b0b6372020-05-20 19:54:44 +0200649 if (ver1->iv_minor < ver2->iv_minor) {
650 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000651 }
David Vincze8b0b6372020-05-20 19:54:44 +0200652 /* The minor version numbers are equal, continue comparison. */
653 if (ver1->iv_revision > ver2->iv_revision) {
654 return 1;
655 }
656 if (ver1->iv_revision < ver2->iv_revision) {
657 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000658 }
659
660 return 0;
661}
662#endif
663
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000664#if defined(MCUBOOT_DIRECT_XIP)
665/**
666 * Check if image in slot has been set with specific ROM address to run from
667 * and whether the slot starts at that address.
668 *
669 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
670 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
671 * header matches the slot address;
672 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
673 * does not match the slot address.
674 */
675static bool
Mark Horvathccaf7f82021-01-04 18:16:42 +0100676boot_rom_address_check(struct boot_loader_state *state,
677 struct slot_usage_t slot_usage[])
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000678{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100679 uint32_t active_slot;
680 const struct image_header *hdr;
681 uint32_t f_off;
682
683 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
684 hdr = boot_img_hdr(state, active_slot);
685 f_off = boot_img_slot_off(state, active_slot);
686
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000687 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
688 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100689 ", skipping",
690 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000691 hdr->ih_load_addr);
692
693 /* If there is address mismatch, the image is not bootable from this
694 * slot.
695 */
696 return 1;
697 }
698 return 0;
699}
700#endif
701
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300702/*
703 * Check that there is a valid image in a slot
704 *
705 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100706 * FIH_SUCCESS if image was successfully validated
707 * 1 (or its fih_int encoded form) if no bootloable image was found
708 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300709 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100710static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300711boot_validate_slot(struct boot_loader_state *state, int slot,
712 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713{
714 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400715 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300716 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100717 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300719
Fabio Utzig10ee6482019-08-01 12:04:52 -0300720 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300721 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800722 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100723 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800724 }
725
Fabio Utzig10ee6482019-08-01 12:04:52 -0300726 hdr = boot_img_hdr(state, slot);
727 if (boot_check_header_erased(state, slot) == 0 ||
728 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300729
730#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
731 /*
732 * This fixes an issue where an image might be erased, but a trailer
733 * be left behind. It can happen if the image is in the secondary slot
734 * and did not pass validation, in which case the whole slot is erased.
735 * If during the erase operation, a reset occurs, parts of the slot
736 * might have been erased while some did not. The concerning part is
737 * the trailer because it might disable a new image from being loaded
738 * through mcumgr; so we just get rid of the trailer here, if the header
739 * is erased.
740 */
741 if (slot != BOOT_PRIMARY_SLOT) {
742 swap_erase_trailer_sectors(state, fap);
743 }
744#endif
745
David Vincze2d736ad2019-02-18 11:50:22 +0100746 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100747 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200748 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300749 }
750
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000751#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
752 if (slot != BOOT_PRIMARY_SLOT) {
753 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200754 rc = boot_version_cmp(
755 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
756 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
757 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000758 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000759 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000760 /* Image in the secondary slot does not satisfy version requirement.
761 * Erase the image and continue booting from the primary slot.
762 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100763 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000764 goto out;
765 }
766 }
767#endif
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200768 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
769 fih_rc, BOOT_CURR_IMG(state), slot);
Andrzej Puzdrowski9d4d45c2021-09-14 17:25:58 +0200770 if (fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200771 {
772 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
773 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100774 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200775 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000776 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200777 /* Image is invalid, erase it to prevent further unnecessary
778 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700779 */
780 }
David Brown098de832019-12-10 11:58:01 -0700781#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100782 BOOT_LOG_ERR("Image in the %s slot is not valid!",
783 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700784#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100785 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200786 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800787 }
788
Fabio Utzig338a19f2018-12-03 08:37:08 -0200789out:
790 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100791
792 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800793}
794
David Vinczec3084132020-02-18 14:50:47 +0100795#ifdef MCUBOOT_HW_ROLLBACK_PROT
796/**
797 * Updates the stored security counter value with the image's security counter
798 * value which resides in the given slot, only if it's greater than the stored
799 * value.
800 *
801 * @param image_index Index of the image to determine which security
802 * counter to update.
803 * @param slot Slot number of the image.
804 * @param hdr Pointer to the image header structure of the image
805 * that is currently stored in the given slot.
806 *
807 * @return 0 on success; nonzero on failure.
808 */
809static int
810boot_update_security_counter(uint8_t image_index, int slot,
811 struct image_header *hdr)
812{
813 const struct flash_area *fap = NULL;
814 uint32_t img_security_cnt;
815 int rc;
816
817 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
818 &fap);
819 if (rc != 0) {
820 rc = BOOT_EFLASH;
821 goto done;
822 }
823
824 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
825 if (rc != 0) {
826 goto done;
827 }
828
829 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
830 if (rc != 0) {
831 goto done;
832 }
833
834done:
835 flash_area_close(fap);
836 return rc;
837}
838#endif /* MCUBOOT_HW_ROLLBACK_PROT */
839
Tamas Banfe031092020-09-10 17:32:39 +0200840#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200841/**
842 * Determines which swap operation to perform, if any. If it is determined
843 * that a swap operation is required, the image in the secondary slot is checked
844 * for validity. If the image in the secondary slot is invalid, it is erased,
845 * and a swap type of "none" is indicated.
846 *
847 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
848 */
849static int
850boot_validated_swap_type(struct boot_loader_state *state,
851 struct boot_status *bs)
852{
853 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100854 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200855
856 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
857 if (BOOT_IS_UPGRADE(swap_type)) {
858 /* Boot loader wants to switch to the secondary slot.
859 * Ensure image is valid.
860 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100861 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
862 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
863 if (fih_eq(fih_rc, fih_int_encode(1))) {
864 swap_type = BOOT_SWAP_TYPE_NONE;
865 } else {
866 swap_type = BOOT_SWAP_TYPE_FAIL;
867 }
David Vinczee574f2d2020-07-10 11:42:03 +0200868 }
869 }
870
871 return swap_type;
872}
David Brown94ed12c2021-05-26 16:28:14 -0600873#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200874
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800876 * Erases a region of flash.
877 *
Fabio Utzigba829042018-09-18 08:29:34 -0300878 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800879 * @param off The offset within the flash area to start the
880 * erase.
881 * @param sz The number of bytes to erase.
882 *
883 * @return 0 on success; nonzero on failure.
884 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300885int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300886boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800887{
Fabio Utzigba829042018-09-18 08:29:34 -0300888 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800889}
890
David Brown94ed12c2021-05-26 16:28:14 -0600891#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800892/**
893 * Copies the contents of one flash region to another. You must erase the
894 * destination region prior to calling this function.
895 *
896 * @param flash_area_id_src The ID of the source flash area.
897 * @param flash_area_id_dst The ID of the destination flash area.
898 * @param off_src The offset within the source flash area to
899 * copy from.
900 * @param off_dst The offset within the destination flash area to
901 * copy to.
902 * @param sz The number of bytes to copy.
903 *
904 * @return 0 on success; nonzero on failure.
905 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300906int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300907boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300908 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300909 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800910 uint32_t off_src, uint32_t off_dst, uint32_t sz)
911{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800912 uint32_t bytes_copied;
913 int chunk_sz;
914 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300915#ifdef MCUBOOT_ENC_IMAGES
916 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300917 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300918 size_t blk_off;
919 struct image_header *hdr;
920 uint16_t idx;
921 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300922 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300923#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924
Marek Pietae51ec072021-07-15 14:53:10 +0200925 TARGET_STATIC uint8_t buf[1024] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300926
927#if !defined(MCUBOOT_ENC_IMAGES)
928 (void)state;
929#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800930
Christopher Collins92ea77f2016-12-12 15:59:26 -0800931 bytes_copied = 0;
932 while (bytes_copied < sz) {
933 if (sz - bytes_copied > sizeof buf) {
934 chunk_sz = sizeof buf;
935 } else {
936 chunk_sz = sz - bytes_copied;
937 }
938
939 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
940 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300941 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800942 }
943
Fabio Utzigba829042018-09-18 08:29:34 -0300944#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300945 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000946 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
947 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
948 !(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))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100950 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300951 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300952#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300953 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000954 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100955 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300956 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300957 off = off_dst;
958 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300959#else
960 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000961 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300962 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
963 }
964#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200965 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300966 blk_sz = chunk_sz;
967 idx = 0;
968 if (off + bytes_copied < hdr->ih_hdr_size) {
969 /* do not decrypt header */
970 blk_off = 0;
971 blk_sz = chunk_sz - hdr->ih_hdr_size;
972 idx = hdr->ih_hdr_size;
973 } else {
974 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
975 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300976 tlv_off = BOOT_TLV_OFF(hdr);
977 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300978 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300979 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300980 blk_sz = 0;
981 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300982 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300983 }
984 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300985 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300986 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
987 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300988 }
989 }
990#endif
991
Christopher Collins92ea77f2016-12-12 15:59:26 -0800992 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
993 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300994 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800995 }
996
997 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300998
999 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001000 }
1001
Fabio Utzigba829042018-09-18 08:29:34 -03001002 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001003}
1004
Christopher Collins92ea77f2016-12-12 15:59:26 -08001005/**
David Vincze2d736ad2019-02-18 11:50:22 +01001006 * Overwrite primary slot with the image contained in the secondary slot.
1007 * If a prior copy operation was interrupted by a system reset, this function
1008 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009 *
1010 * @param bs The current boot status. This function reads
1011 * this struct to determine if it is resuming
1012 * an interrupted swap operation. This
1013 * function writes the updated status to this
1014 * function on return.
1015 *
1016 * @return 0 on success; nonzero on failure.
1017 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001018#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001019static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001020boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001021{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001022 size_t sect_count;
1023 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001024 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001025 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001026 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001027 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001028 const struct flash_area *fap_primary_slot;
1029 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001030 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001031
Fabio Utzigb4f88102020-10-04 10:16:24 -03001032#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1033 uint32_t sector;
1034 uint32_t trailer_sz;
1035 uint32_t off;
1036 uint32_t sz;
1037#endif
1038
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001039 (void)bs;
1040
Fabio Utzig13d9e352017-10-05 20:32:31 -03001041#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1042 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001043 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001044 assert(rc == 0);
1045#endif
David Brown17609d82017-05-05 09:41:34 -06001046
David Vincze2d736ad2019-02-18 11:50:22 +01001047 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1048 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001049
Fabio Utzigb0f04732019-07-31 09:49:19 -03001050 image_index = BOOT_CURR_IMG(state);
1051
1052 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1053 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001054 assert (rc == 0);
1055
Fabio Utzigb0f04732019-07-31 09:49:19 -03001056 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1057 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001058 assert (rc == 0);
1059
Fabio Utzig10ee6482019-08-01 12:04:52 -03001060 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001061 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001062 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001063 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001064 assert(rc == 0);
1065
Fabio Utzig13d9e352017-10-05 20:32:31 -03001066#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001067 if ((size + this_size) >= src_size) {
1068 size += src_size - size;
1069 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001070 break;
1071 }
1072#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001073
1074 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001075 }
1076
Fabio Utzigb4f88102020-10-04 10:16:24 -03001077#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1078 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1079 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1080 sz = 0;
1081 do {
1082 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1083 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1084 sector--;
1085 } while (sz < trailer_sz);
1086
1087 rc = boot_erase_region(fap_primary_slot, off, sz);
1088 assert(rc == 0);
1089#endif
1090
Fabio Utzigba829042018-09-18 08:29:34 -03001091#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001092 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001093 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001094 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001095 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001096
Fabio Utzigba829042018-09-18 08:29:34 -03001097 if (rc < 0) {
1098 return BOOT_EBADIMAGE;
1099 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001100 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001101 return BOOT_EBADIMAGE;
1102 }
1103 }
1104#endif
1105
David Vincze2d736ad2019-02-18 11:50:22 +01001106 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1107 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001108 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001109 if (rc != 0) {
1110 return rc;
1111 }
1112
1113#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1114 rc = boot_write_magic(fap_primary_slot);
1115 if (rc != 0) {
1116 return rc;
1117 }
1118#endif
David Brown17609d82017-05-05 09:41:34 -06001119
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001120 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1121 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1122 if (rc != 0) {
1123 return rc;
1124 }
1125
David Vinczec3084132020-02-18 14:50:47 +01001126#ifdef MCUBOOT_HW_ROLLBACK_PROT
1127 /* Update the stored security counter with the new image's security counter
1128 * value. Both slots hold the new image at this point, but the secondary
1129 * slot's image header must be passed since the image headers in the
1130 * boot_data structure have not been updated yet.
1131 */
1132 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1133 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1134 if (rc != 0) {
1135 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1136 return rc;
1137 }
1138#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1139
Fabio Utzig13d9e352017-10-05 20:32:31 -03001140 /*
1141 * Erases header and trailer. The trailer is erased because when a new
1142 * image is written without a trailer as is the case when using newt, the
1143 * trailer that was left might trigger a new upgrade.
1144 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001145 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001146 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001147 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1148 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001149 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001150 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001151 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001152 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001153 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1154 last_sector),
1155 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1156 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001157 assert(rc == 0);
1158
David Vincze2d736ad2019-02-18 11:50:22 +01001159 flash_area_close(fap_primary_slot);
1160 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001161
David Vincze2d736ad2019-02-18 11:50:22 +01001162 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001163
1164 return 0;
1165}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001166#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001167
Christopher Collinsa1c12042019-05-23 14:00:28 -07001168#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001169/**
1170 * Swaps the two images in flash. If a prior copy operation was interrupted
1171 * by a system reset, this function completes that operation.
1172 *
1173 * @param bs The current boot status. This function reads
1174 * this struct to determine if it is resuming
1175 * an interrupted swap operation. This
1176 * function writes the updated status to this
1177 * function on return.
1178 *
1179 * @return 0 on success; nonzero on failure.
1180 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001181static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001182boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001183{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001184 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001185#ifdef MCUBOOT_ENC_IMAGES
1186 const struct flash_area *fap;
1187 uint8_t slot;
1188 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001189#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001190 uint32_t size;
1191 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001192 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001193 int rc;
1194
1195 /* FIXME: just do this if asked by user? */
1196
1197 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001198 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001199
Fabio Utzig12d59162019-11-28 10:01:59 -03001200 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001201 /*
1202 * No swap ever happened, so need to find the largest image which
1203 * will be used to determine the amount of sectors to swap.
1204 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001205 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001206 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001207 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001208 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001209 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001210
Fabio Utzigba829042018-09-18 08:29:34 -03001211#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001212 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001213 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001214 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001215 assert(rc >= 0);
1216
1217 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001218 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001219 assert(rc == 0);
1220 } else {
1221 rc = 0;
1222 }
1223 } else {
1224 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1225 }
1226#endif
1227
Fabio Utzig10ee6482019-08-01 12:04:52 -03001228 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001229 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001230 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001231 assert(rc == 0);
1232 }
1233
Fabio Utzigba829042018-09-18 08:29:34 -03001234#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001235 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001236 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001237 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001238 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001239 assert(rc >= 0);
1240
1241 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001242 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001243 assert(rc == 0);
1244 } else {
1245 rc = 0;
1246 }
1247 } else {
1248 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1249 }
1250#endif
1251
Fabio Utzig46490722017-09-04 15:34:32 -03001252 if (size > copy_size) {
1253 copy_size = size;
1254 }
1255
1256 bs->swap_size = copy_size;
1257 } else {
1258 /*
1259 * If a swap was under way, the swap_size should already be present
1260 * in the trailer...
1261 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001262 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001263 assert(rc == 0);
1264
1265 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001266
1267#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001268 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1269 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001270 assert(rc == 0);
1271
1272 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001273 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001274 break;
1275 }
1276 }
1277
1278 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001279 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001280 }
1281 }
1282#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001283 }
1284
Fabio Utzig12d59162019-11-28 10:01:59 -03001285 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001286
David Vincze2d736ad2019-02-18 11:50:22 +01001287#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001288 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001289 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001290 BOOT_LOG_WRN("%d status write fails performing the swap",
1291 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001292 }
1293#endif
1294
Christopher Collins92ea77f2016-12-12 15:59:26 -08001295 return 0;
1296}
David Brown17609d82017-05-05 09:41:34 -06001297#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001298
David Vinczee32483f2019-06-13 10:46:24 +02001299#if (BOOT_IMAGE_NUMBER > 1)
1300/**
1301 * Check the image dependency whether it is satisfied and modify
1302 * the swap type if necessary.
1303 *
1304 * @param dep Image dependency which has to be verified.
1305 *
1306 * @return 0 on success; nonzero on failure.
1307 */
1308static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001309boot_verify_slot_dependency(struct boot_loader_state *state,
1310 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001311{
1312 struct image_version *dep_version;
1313 size_t dep_slot;
1314 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001315 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001316
1317 /* Determine the source of the image which is the subject of
1318 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001319 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001320 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1321 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001322 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001323
David Vincze8b0b6372020-05-20 19:54:44 +02001324 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1325 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001326 /* Dependency not satisfied.
1327 * Modify the swap type to decrease the version number of the image
1328 * (which will be located in the primary slot after the boot process),
1329 * consequently the number of unsatisfied dependencies will be
1330 * decreased or remain the same.
1331 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001332 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001333 case BOOT_SWAP_TYPE_TEST:
1334 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001335 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001336 break;
1337 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001338 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001339 break;
1340 default:
1341 break;
1342 }
David Vincze8b0b6372020-05-20 19:54:44 +02001343 } else {
1344 /* Dependency satisfied. */
1345 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001346 }
1347
1348 return rc;
1349}
1350
1351/**
1352 * Read all dependency TLVs of an image from the flash and verify
1353 * one after another to see if they are all satisfied.
1354 *
1355 * @param slot Image slot number.
1356 *
1357 * @return 0 on success; nonzero on failure.
1358 */
1359static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001360boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001361{
1362 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001363 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001364 struct image_dependency dep;
1365 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001366 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001367 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001368 int rc;
1369
Fabio Utzig10ee6482019-08-01 12:04:52 -03001370 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001371 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001372 if (rc != 0) {
1373 rc = BOOT_EFLASH;
1374 goto done;
1375 }
1376
Fabio Utzig61fd8882019-09-14 20:00:20 -03001377 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1378 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001379 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001380 goto done;
1381 }
1382
Fabio Utzig61fd8882019-09-14 20:00:20 -03001383 while (true) {
1384 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1385 if (rc < 0) {
1386 return -1;
1387 } else if (rc > 0) {
1388 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001389 break;
1390 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001391
1392 if (len != sizeof(dep)) {
1393 rc = BOOT_EBADIMAGE;
1394 goto done;
1395 }
1396
1397 rc = flash_area_read(fap, off, &dep, len);
1398 if (rc != 0) {
1399 rc = BOOT_EFLASH;
1400 goto done;
1401 }
1402
1403 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1404 rc = BOOT_EBADARGS;
1405 goto done;
1406 }
1407
1408 /* Verify dependency and modify the swap type if not satisfied. */
1409 rc = boot_verify_slot_dependency(state, &dep);
1410 if (rc != 0) {
1411 /* Dependency not satisfied. */
1412 goto done;
1413 }
David Vinczee32483f2019-06-13 10:46:24 +02001414 }
1415
1416done:
1417 flash_area_close(fap);
1418 return rc;
1419}
1420
1421/**
David Vinczee32483f2019-06-13 10:46:24 +02001422 * Iterate over all the images and verify whether the image dependencies in the
1423 * TLV area are all satisfied and update the related swap type if necessary.
1424 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001425static int
1426boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001427{
Erik Johnson49063752020-02-06 09:59:31 -06001428 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001429 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001430
Fabio Utzig10ee6482019-08-01 12:04:52 -03001431 BOOT_CURR_IMG(state) = 0;
1432 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001433 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1434 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1435 slot = BOOT_SECONDARY_SLOT;
1436 } else {
1437 slot = BOOT_PRIMARY_SLOT;
1438 }
1439
1440 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001441 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001442 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001443 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001444 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001445 /* Cannot upgrade due to non-met dependencies, so disable all
1446 * image upgrades.
1447 */
1448 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1449 BOOT_CURR_IMG(state) = idx;
1450 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1451 }
1452 break;
David Vinczee32483f2019-06-13 10:46:24 +02001453 }
1454 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001455 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001456}
1457#endif /* (BOOT_IMAGE_NUMBER > 1) */
1458
Christopher Collins92ea77f2016-12-12 15:59:26 -08001459/**
David Vinczeba3bd602019-06-17 16:01:43 +02001460 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001461 *
David Vinczeba3bd602019-06-17 16:01:43 +02001462 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001463 *
1464 * @return 0 on success; nonzero on failure.
1465 */
1466static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001467boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001468{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001469 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001470#ifndef MCUBOOT_OVERWRITE_ONLY
1471 uint8_t swap_type;
1472#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001473
David Vinczeba3bd602019-06-17 16:01:43 +02001474 /* At this point there are no aborted swaps. */
1475#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001476 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001477#elif defined(MCUBOOT_BOOTSTRAP)
1478 /* Check if the image update was triggered by a bad image in the
1479 * primary slot (the validity of the image in the secondary slot had
1480 * already been checked).
1481 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001482 fih_int fih_rc = FIH_FAILURE;
1483 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1484 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1485 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001486 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001487 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001488 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001489 }
1490#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001491 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001492#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001493 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001494
1495#ifndef MCUBOOT_OVERWRITE_ONLY
1496 /* The following state needs image_ok be explicitly set after the
1497 * swap was finished to avoid a new revert.
1498 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001499 swap_type = BOOT_SWAP_TYPE(state);
1500 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1501 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001502 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001503 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001504 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001505 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001506 }
1507
David Vinczec3084132020-02-18 14:50:47 +01001508#ifdef MCUBOOT_HW_ROLLBACK_PROT
1509 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1510 /* Update the stored security counter with the new image's security
1511 * counter value. The primary slot holds the new image at this point,
1512 * but the secondary slot's image header must be passed since image
1513 * headers in the boot_data structure have not been updated yet.
1514 *
1515 * In case of a permanent image swap mcuboot will never attempt to
1516 * revert the images on the next reboot. Therefore, the security
1517 * counter must be increased right after the image upgrade.
1518 */
1519 rc = boot_update_security_counter(
1520 BOOT_CURR_IMG(state),
1521 BOOT_PRIMARY_SLOT,
1522 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1523 if (rc != 0) {
1524 BOOT_LOG_ERR("Security counter update failed after "
1525 "image upgrade.");
1526 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1527 }
1528 }
1529#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1530
Fabio Utzige575b0b2019-09-11 12:34:23 -03001531 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001532 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001533 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001534 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001535 }
David Vinczeba3bd602019-06-17 16:01:43 +02001536 }
1537#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001538
David Vinczeba3bd602019-06-17 16:01:43 +02001539 return rc;
1540}
1541
1542/**
1543 * Completes a previously aborted image swap.
1544 *
1545 * @param bs The current boot status.
1546 *
1547 * @return 0 on success; nonzero on failure.
1548 */
1549#if !defined(MCUBOOT_OVERWRITE_ONLY)
1550static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001551boot_complete_partial_swap(struct boot_loader_state *state,
1552 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001553{
1554 int rc;
1555
1556 /* Determine the type of swap operation being resumed from the
1557 * `swap-type` trailer field.
1558 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001559 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001560 assert(rc == 0);
1561
Fabio Utzig10ee6482019-08-01 12:04:52 -03001562 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001563
1564 /* The following states need image_ok be explicitly set after the
1565 * swap was finished to avoid a new revert.
1566 */
1567 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1568 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001569 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001570 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001571 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001572 }
1573 }
1574
Fabio Utzige575b0b2019-09-11 12:34:23 -03001575 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001576 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001577 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001578 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001579 }
1580 }
1581
Fabio Utzig10ee6482019-08-01 12:04:52 -03001582 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001583 BOOT_LOG_ERR("panic!");
1584 assert(0);
1585
1586 /* Loop forever... */
1587 while (1) {}
1588 }
1589
1590 return rc;
1591}
1592#endif /* !MCUBOOT_OVERWRITE_ONLY */
1593
1594#if (BOOT_IMAGE_NUMBER > 1)
1595/**
1596 * Review the validity of previously determined swap types of other images.
1597 *
1598 * @param aborted_swap The current image upgrade is a
1599 * partial/aborted swap.
1600 */
1601static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001602boot_review_image_swap_types(struct boot_loader_state *state,
1603 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001604{
1605 /* In that case if we rebooted in the middle of an image upgrade process, we
1606 * must review the validity of swap types, that were previously determined
1607 * for other images. The image_ok flag had not been set before the reboot
1608 * for any of the updated images (only the copy_done flag) and thus falsely
1609 * the REVERT swap type has been determined for the previous images that had
1610 * been updated before the reboot.
1611 *
1612 * There are two separate scenarios that we have to deal with:
1613 *
1614 * 1. The reboot has happened during swapping an image:
1615 * The current image upgrade has been determined as a
1616 * partial/aborted swap.
1617 * 2. The reboot has happened between two separate image upgrades:
1618 * In this scenario we must check the swap type of the current image.
1619 * In those cases if it is NONE or REVERT we cannot certainly determine
1620 * the fact of a reboot. In a consistent state images must move in the
1621 * same direction or stay in place, e.g. in practice REVERT and TEST
1622 * swap types cannot be present at the same time. If the swap type of
1623 * the current image is either TEST, PERM or FAIL we must review the
1624 * already determined swap types of other images and set each false
1625 * REVERT swap types to NONE (these images had been successfully
1626 * updated before the system rebooted between two separate image
1627 * upgrades).
1628 */
1629
Fabio Utzig10ee6482019-08-01 12:04:52 -03001630 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001631 /* Nothing to do */
1632 return;
1633 }
1634
1635 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001636 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1637 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001638 /* Nothing to do */
1639 return;
1640 }
1641 }
1642
Fabio Utzig10ee6482019-08-01 12:04:52 -03001643 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1644 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1645 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001646 }
1647 }
1648}
1649#endif
1650
1651/**
1652 * Prepare image to be updated if required.
1653 *
1654 * Prepare image to be updated if required with completing an image swap
1655 * operation if one was aborted and/or determining the type of the
1656 * swap operation. In case of any error set the swap type to NONE.
1657 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001658 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001659 * @param bs Pointer where the read and possibly updated
1660 * boot status can be written to.
1661 */
1662static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001663boot_prepare_image_for_update(struct boot_loader_state *state,
1664 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001665{
1666 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001667 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001668
1669 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001670 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001671 if (rc != 0) {
1672 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1673 " - too small?", BOOT_MAX_IMG_SECTORS);
1674 /* Unable to determine sector layout, continue with next image
1675 * if there is one.
1676 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001677 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001678 if (rc == BOOT_EFLASH)
1679 {
1680 /* Only return on error from the primary image flash */
1681 return;
1682 }
David Vinczeba3bd602019-06-17 16:01:43 +02001683 }
1684
1685 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001686 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001687 if (rc != 0) {
1688 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001689 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001690 BOOT_CURR_IMG(state));
1691 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001692 return;
1693 }
1694
1695 /* If the current image's slots aren't compatible, no swap is possible.
1696 * Just boot into primary slot.
1697 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001698 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001699 boot_status_reset(bs);
1700
1701#ifndef MCUBOOT_OVERWRITE_ONLY
1702 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001703 if (rc != 0) {
1704 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001705 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001706 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001707 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001708 return;
1709 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001710#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001711
Fabio Utzig74aef312019-11-28 11:05:34 -03001712#ifdef MCUBOOT_SWAP_USING_MOVE
1713 /*
1714 * Must re-read image headers because the boot status might
1715 * have been updated in the previous function call.
1716 */
1717 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001718#ifdef MCUBOOT_BOOTSTRAP
1719 /* When bootstrapping it's OK to not have image magic in the primary slot */
1720 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1721 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1722#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001723 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001724#endif
1725
Fabio Utzig74aef312019-11-28 11:05:34 -03001726 /* Continue with next image if there is one. */
1727 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1728 BOOT_CURR_IMG(state));
1729 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1730 return;
1731 }
1732#endif
1733
David Vinczeba3bd602019-06-17 16:01:43 +02001734 /* Determine if we rebooted in the middle of an image swap
1735 * operation. If a partial swap was detected, complete it.
1736 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001737 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001738
1739#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001740 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001741#endif
1742
1743#ifdef MCUBOOT_OVERWRITE_ONLY
1744 /* Should never arrive here, overwrite-only mode has
1745 * no swap state.
1746 */
1747 assert(0);
1748#else
1749 /* Determine the type of swap operation being resumed from the
1750 * `swap-type` trailer field.
1751 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001752 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001753 assert(rc == 0);
1754#endif
1755 /* Attempt to read an image header from each slot. Ensure that
1756 * image headers in slots are aligned with headers in boot_data.
1757 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001758 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001759 assert(rc == 0);
1760
1761 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001762 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001763 } else {
1764 /* There was no partial swap, determine swap type. */
1765 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001766 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001767 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001768 FIH_CALL(boot_validate_slot, fih_rc,
1769 state, BOOT_SECONDARY_SLOT, bs);
1770 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1771 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1772 } else {
1773 BOOT_SWAP_TYPE(state) = bs->swap_type;
1774 }
David Vinczeba3bd602019-06-17 16:01:43 +02001775 }
1776
1777#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001778 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001779#endif
1780
1781#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001782 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001783 /* Header checks are done first because they are
1784 * inexpensive. Since overwrite-only copies starting from
1785 * offset 0, if interrupted, it might leave a valid header
1786 * magic, so also run validation on the primary slot to be
1787 * sure it's not OK.
1788 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001789 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1790 FIH_CALL(boot_validate_slot, fih_rc,
1791 state, BOOT_PRIMARY_SLOT, bs);
1792
1793 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1794
Fabio Utzig3d77c952020-10-04 10:23:17 -03001795 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001796 FIH_CALL(boot_validate_slot, fih_rc,
1797 state, BOOT_SECONDARY_SLOT, bs);
1798
Fabio Utzig3d77c952020-10-04 10:23:17 -03001799 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001800 /* Set swap type to REVERT to overwrite the primary
1801 * slot with the image contained in secondary slot
1802 * and to trigger the explicit setting of the
1803 * image_ok flag.
1804 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001805 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001806 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001807 }
1808 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001809#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001810 }
David Vinczeba3bd602019-06-17 16:01:43 +02001811 } else {
1812 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001813 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001814 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001815}
1816
Mark Horvathccaf7f82021-01-04 18:16:42 +01001817/**
1818 * Updates the security counter for the current image.
1819 *
1820 * @param state Boot loader status information.
1821 *
1822 * @return 0 on success; nonzero on failure.
1823 */
1824static int
1825boot_update_hw_rollback_protection(struct boot_loader_state *state)
1826{
1827#ifdef MCUBOOT_HW_ROLLBACK_PROT
1828 int rc;
1829
1830 /* Update the stored security counter with the active image's security
1831 * counter value. It will only be updated if the new security counter is
1832 * greater than the stored value.
1833 *
1834 * In case of a successful image swapping when the swap type is TEST the
1835 * security counter can be increased only after a reset, when the swap
1836 * type is NONE and the image has marked itself "OK" (the image_ok flag
1837 * has been set). This way a "revert" can be performed when it's
1838 * necessary.
1839 */
1840 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1841 rc = boot_update_security_counter(
1842 BOOT_CURR_IMG(state),
1843 BOOT_PRIMARY_SLOT,
1844 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1845 if (rc != 0) {
1846 BOOT_LOG_ERR("Security counter update failed after image "
1847 "validation.");
1848 return rc;
1849 }
1850 }
1851
1852 return 0;
1853
1854#else /* MCUBOOT_HW_ROLLBACK_PROT */
1855 (void) (state);
1856
1857 return 0;
1858#endif
1859}
1860
Raef Colese8fe6cf2020-05-26 13:07:40 +01001861fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001862context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001863{
Marti Bolivar84898652017-06-13 17:20:22 -04001864 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001865 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001866 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001867 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001868 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001869 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001870 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001871
1872 /* The array of slot sectors are defined here (as opposed to file scope) so
1873 * that they don't get allocated for non-boot-loader apps. This is
1874 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001875 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001876 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001877 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1878 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001879#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001880 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001881#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001882
Fabio Utzig10ee6482019-08-01 12:04:52 -03001883 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001884 has_upgrade = false;
1885
1886#if (BOOT_IMAGE_NUMBER == 1)
1887 (void)has_upgrade;
1888#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001889
David Vinczeba3bd602019-06-17 16:01:43 +02001890 /* Iterate over all the images. By the end of the loop the swap type has
1891 * to be determined for each image and all aborted swaps have to be
1892 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001893 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001894 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001895
David Vinczeba3bd602019-06-17 16:01:43 +02001896#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1897 /* The keys used for encryption may no longer be valid (could belong to
1898 * another images). Therefore, mark them as invalid to force their reload
1899 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001900 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001901 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001902#endif
1903
Fabio Utzig10ee6482019-08-01 12:04:52 -03001904 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001905
Fabio Utzig10ee6482019-08-01 12:04:52 -03001906 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001907 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001908 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001909 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001910#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001911 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001912#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001913
1914 /* Open primary and secondary image areas for the duration
1915 * of this call.
1916 */
1917 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001918 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001919 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001920 assert(rc == 0);
1921 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001922#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001923 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001924 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001925 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001926#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001927
1928 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001929 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001930
Fabio Utzige575b0b2019-09-11 12:34:23 -03001931 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001932 has_upgrade = true;
1933 }
David Vinczeba3bd602019-06-17 16:01:43 +02001934 }
1935
David Vinczee32483f2019-06-13 10:46:24 +02001936#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001937 if (has_upgrade) {
1938 /* Iterate over all the images and verify whether the image dependencies
1939 * are all satisfied and update swap type if necessary.
1940 */
1941 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001942 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001943 /*
1944 * It was impossible to upgrade because the expected dependency version
1945 * was not available. Here we already changed the swap_type so that
1946 * instead of asserting the bootloader, we continue and no upgrade is
1947 * performed.
1948 */
1949 rc = 0;
1950 }
1951 }
David Vinczee32483f2019-06-13 10:46:24 +02001952#endif
1953
David Vinczeba3bd602019-06-17 16:01:43 +02001954 /* Iterate over all the images. At this point there are no aborted swaps
1955 * and the swap types are determined for each image. By the end of the loop
1956 * all required update operations will have been finished.
1957 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001958 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001959
1960#if (BOOT_IMAGE_NUMBER > 1)
1961#ifdef MCUBOOT_ENC_IMAGES
1962 /* The keys used for encryption may no longer be valid (could belong to
1963 * another images). Therefore, mark them as invalid to force their reload
1964 * by boot_enc_load().
1965 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001966 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001967#endif /* MCUBOOT_ENC_IMAGES */
1968
1969 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001970 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001971#endif /* (BOOT_IMAGE_NUMBER > 1) */
1972
1973 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001974 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001975
Fabio Utzig10ee6482019-08-01 12:04:52 -03001976 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001977 case BOOT_SWAP_TYPE_NONE:
1978 break;
1979
1980 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1981 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1982 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001983 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
1984 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
1985 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
1986 if (rc == BOOT_HOOK_REGULAR)
1987 {
1988 rc = boot_perform_update(state, &bs);
1989 }
David Vinczeba3bd602019-06-17 16:01:43 +02001990 assert(rc == 0);
1991 break;
1992
1993 case BOOT_SWAP_TYPE_FAIL:
1994 /* The image in secondary slot was invalid and is now erased. Ensure
1995 * we don't try to boot into it again on the next reboot. Do this by
1996 * pretending we just reverted back to primary slot.
1997 */
1998#ifndef MCUBOOT_OVERWRITE_ONLY
1999 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002000 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002001 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002002 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002003 }
2004#endif /* !MCUBOOT_OVERWRITE_ONLY */
2005 break;
2006
2007 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002008 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002009 }
2010
Fabio Utzig10ee6482019-08-01 12:04:52 -03002011 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002012 BOOT_LOG_ERR("panic!");
2013 assert(0);
2014
2015 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002016 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002017 }
2018 }
2019
2020 /* Iterate over all the images. At this point all required update operations
2021 * have finished. By the end of the loop each image in the primary slot will
2022 * have been re-validated.
2023 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002024 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2025 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002026 /* Attempt to read an image header from each slot. Ensure that image
2027 * headers in slots are aligned with headers in boot_data.
2028 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002029 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002030 if (rc != 0) {
2031 goto out;
2032 }
2033 /* Since headers were reloaded, it can be assumed we just performed
2034 * a swap or overwrite. Now the header info that should be used to
2035 * provide the data for the bootstrap, which previously was at
2036 * secondary slot, was updated to primary slot.
2037 */
2038 }
2039
2040#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002041 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2042 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002043 goto out;
2044 }
2045#else
2046 /* Even if we're not re-validating the primary slot, we could be booting
2047 * onto an empty flash chip. At least do a basic sanity check that
2048 * the magic number on the image is OK.
2049 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002050 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002051 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002052 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002053 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002054 rc = BOOT_EBADIMAGE;
2055 goto out;
2056 }
David Vinczec3084132020-02-18 14:50:47 +01002057#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2058
Mark Horvathccaf7f82021-01-04 18:16:42 +01002059 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002060 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002061 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002062 }
David Vincze1cf11b52020-03-24 07:51:09 +01002063
Mark Horvathccaf7f82021-01-04 18:16:42 +01002064 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002065 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002066 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002067 }
David Vinczeba3bd602019-06-17 16:01:43 +02002068 }
2069
Fabio Utzigf616c542019-12-19 15:23:32 -03002070 /*
2071 * Since the boot_status struct stores plaintext encryption keys, reset
2072 * them here to avoid the possibility of jumping into an image that could
2073 * easily recover them.
2074 */
2075 memset(&bs, 0, sizeof(struct boot_status));
2076
Mark Horvathccaf7f82021-01-04 18:16:42 +01002077 fill_rsp(state, NULL, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002078
Raef Colese8fe6cf2020-05-26 13:07:40 +01002079 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002080out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002081 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002082
2083 if (rc) {
2084 fih_rc = fih_int_encode(rc);
2085 }
2086
2087 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002088}
2089
Raef Colese8fe6cf2020-05-26 13:07:40 +01002090fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002091split_go(int loader_slot, int split_slot, void **entry)
2092{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002093 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002094 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002095 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002096 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002097 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002098 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002099
Christopher Collins92ea77f2016-12-12 15:59:26 -08002100 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2101 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002102 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002103 }
David Vinczeba3bd602019-06-17 16:01:43 +02002104 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2105 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002106
2107 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2108 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002109 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002110 assert(rc == 0);
2111 split_flash_id = flash_area_id_from_image_slot(split_slot);
2112 rc = flash_area_open(split_flash_id,
2113 &BOOT_IMG_AREA(&boot_data, split_slot));
2114 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002115
2116 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002117 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002118 if (rc != 0) {
2119 rc = SPLIT_GO_ERR;
2120 goto done;
2121 }
2122
Fabio Utzig12d59162019-11-28 10:01:59 -03002123 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002124 if (rc != 0) {
2125 goto done;
2126 }
2127
Christopher Collins92ea77f2016-12-12 15:59:26 -08002128 /* Don't check the bootable image flag because we could really call a
2129 * bootable or non-bootable image. Just validate that the image check
2130 * passes which is distinct from the normal check.
2131 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002132 FIH_CALL(split_image_check, fih_rc,
2133 boot_img_hdr(&boot_data, split_slot),
2134 BOOT_IMG_AREA(&boot_data, split_slot),
2135 boot_img_hdr(&boot_data, loader_slot),
2136 BOOT_IMG_AREA(&boot_data, loader_slot));
2137 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002138 goto done;
2139 }
2140
Marti Bolivarea088872017-06-12 17:10:49 -04002141 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002142 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002143 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002144 rc = SPLIT_GO_OK;
2145
2146done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002147 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2148 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002149 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002150
2151 if (rc) {
2152 fih_rc = fih_int_encode(rc);
2153 }
2154
2155 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002156}
David Vinczee574f2d2020-07-10 11:42:03 +02002157
Tamas Banfe031092020-09-10 17:32:39 +02002158#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002159
Mark Horvathccaf7f82021-01-04 18:16:42 +01002160#define NO_ACTIVE_SLOT UINT32_MAX
2161
David Vinczee574f2d2020-07-10 11:42:03 +02002162/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002163 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002164 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002165 * @param state Boot loader status information.
2166 * @param slot_usage Structure to fill with information about the available
2167 * slots.
David Vinczee574f2d2020-07-10 11:42:03 +02002168 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002169 * @return 0 on success; nonzero on failure.
2170 */
2171static int
2172boot_get_slot_usage(struct boot_loader_state *state,
2173 struct slot_usage_t slot_usage[])
2174{
2175 uint32_t slot;
2176 int fa_id;
2177 int rc;
2178 struct image_header *hdr = NULL;
2179
2180 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2181 /* Open all the slots */
2182 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2183 fa_id = flash_area_id_from_multi_image_slot(
2184 BOOT_CURR_IMG(state), slot);
2185 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2186 assert(rc == 0);
2187 }
2188
2189 /* Attempt to read an image header from each slot. */
2190 rc = boot_read_image_headers(state, false, NULL);
2191 if (rc != 0) {
2192 BOOT_LOG_WRN("Failed reading image headers.");
2193 return rc;
2194 }
2195
2196 /* Check headers in all slots */
2197 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2198 hdr = boot_img_hdr(state, slot);
2199
2200 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2201 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
2202 BOOT_LOG_IMAGE_INFO(slot, hdr);
2203 } else {
2204 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
2205 BOOT_LOG_INF("Image %d %s slot: Image not found",
2206 BOOT_CURR_IMG(state),
2207 (slot == BOOT_PRIMARY_SLOT)
2208 ? "Primary" : "Secondary");
2209 }
2210 }
2211
2212 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2213 }
2214
2215 return 0;
2216}
2217
2218/**
2219 * Finds the slot containing the image with the highest version number for the
2220 * current image.
2221 *
2222 * @param state Boot loader status information.
2223 * @param slot_usage Information about the active and available slots.
2224 *
2225 * @return NO_ACTIVE_SLOT if no available slot found, number of
2226 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002227 */
2228static uint32_t
Mark Horvathccaf7f82021-01-04 18:16:42 +01002229find_slot_with_highest_version(struct boot_loader_state *state,
2230 struct slot_usage_t slot_usage[])
David Vinczee574f2d2020-07-10 11:42:03 +02002231{
David Vinczee574f2d2020-07-10 11:42:03 +02002232 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002233 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2234 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002235
Mark Horvathccaf7f82021-01-04 18:16:42 +01002236 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2237 if (slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
2238 if (candidate_slot == NO_ACTIVE_SLOT) {
2239 candidate_slot = slot;
2240 } else {
2241 rc = boot_version_cmp(
2242 &boot_img_hdr(state, slot)->ih_ver,
2243 &boot_img_hdr(state, candidate_slot)->ih_ver);
2244 if (rc == 1) {
2245 /* The version of the image being examined is greater than
2246 * the version of the current candidate.
2247 */
2248 candidate_slot = slot;
2249 }
2250 }
David Vinczee574f2d2020-07-10 11:42:03 +02002251 }
2252 }
2253
Mark Horvathccaf7f82021-01-04 18:16:42 +01002254 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002255}
2256
Mark Horvathccaf7f82021-01-04 18:16:42 +01002257#ifdef MCUBOOT_HAVE_LOGGING
2258/**
2259 * Prints the state of the loaded images.
2260 *
2261 * @param state Boot loader status information.
2262 * @param slot_usage Information about the active and available slots.
2263 */
2264static void
2265print_loaded_images(struct boot_loader_state *state,
2266 struct slot_usage_t slot_usage[])
2267{
2268 uint32_t active_slot;
2269
David Brown695e5912021-05-24 16:58:01 -06002270 (void)state;
2271
Mark Horvathccaf7f82021-01-04 18:16:42 +01002272 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2273 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2274
2275 BOOT_LOG_INF("Image %d loaded from the %s slot",
2276 BOOT_CURR_IMG(state),
2277 (active_slot == BOOT_PRIMARY_SLOT) ?
2278 "primary" : "secondary");
2279 }
2280}
2281#endif
2282
David Vincze1c456242021-06-29 15:25:24 +02002283#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002284/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002285 * Checks whether the active slot of the current image was previously selected
2286 * to run. Erases the image if it was selected but its execution failed,
2287 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002288 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002289 * @param state Boot loader status information.
2290 * @param slot_usage Information about the active and available slots.
David Vincze505fba22020-10-22 13:53:29 +02002291 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002292 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002293 */
2294static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002295boot_select_or_erase(struct boot_loader_state *state,
2296 struct slot_usage_t slot_usage[])
David Vincze505fba22020-10-22 13:53:29 +02002297{
2298 const struct flash_area *fap;
2299 int fa_id;
2300 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002301 uint32_t active_slot;
2302 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002303
Mark Horvathccaf7f82021-01-04 18:16:42 +01002304 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2305
2306 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002307 rc = flash_area_open(fa_id, &fap);
2308 assert(rc == 0);
2309
Mark Horvathccaf7f82021-01-04 18:16:42 +01002310 active_swap_state = &(slot_usage[BOOT_CURR_IMG(state)].swap_state);
2311
2312 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2313 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002314 assert(rc == 0);
2315
Mark Horvathccaf7f82021-01-04 18:16:42 +01002316 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2317 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2318 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002319 /*
2320 * A reboot happened without the image being confirmed at
2321 * runtime or its trailer is corrupted/invalid. Erase the image
2322 * to prevent it from being selected again on the next reboot.
2323 */
2324 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002325 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002326 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002327 assert(rc == 0);
2328
2329 flash_area_close(fap);
2330 rc = -1;
2331 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002332 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2333 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002334 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2335 "value was neither 'set' nor 'unset', but 'bad'.");
2336 }
2337 /*
2338 * Set the copy_done flag, indicating that the image has been
2339 * selected to boot. It can be set in advance, before even
2340 * validating the image, because in case the validation fails, the
2341 * entire image slot will be erased (including the trailer).
2342 */
2343 rc = boot_write_copy_done(fap);
2344 if (rc != 0) {
2345 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002346 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002347 "primary" : "secondary");
2348 rc = 0;
2349 }
2350 }
2351 flash_area_close(fap);
2352 }
2353
2354 return rc;
2355}
David Vincze1c456242021-06-29 15:25:24 +02002356#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002357
Tamas Banfe031092020-09-10 17:32:39 +02002358#ifdef MCUBOOT_RAM_LOAD
2359
Mark Horvathccaf7f82021-01-04 18:16:42 +01002360#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002361#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2362#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2363#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002364#endif
Tamas Banfe031092020-09-10 17:32:39 +02002365
2366/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002367 * Verifies that the active slot of the current image can be loaded within the
2368 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002369 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002370 * @param state Boot loader status information.
2371 * @param slot_usage Information about the active and available slots.
Tamas Banfe031092020-09-10 17:32:39 +02002372 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002373 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002374 */
2375static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002376boot_verify_ram_load_address(struct boot_loader_state *state,
2377 struct slot_usage_t slot_usage[])
Tamas Banfe031092020-09-10 17:32:39 +02002378{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002379 uint32_t img_dst;
2380 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002381 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002382 uint32_t exec_ram_start;
2383 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002384
2385 (void)state;
2386
Mark Horvathccaf7f82021-01-04 18:16:42 +01002387#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2388 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002389
Mark Horvathccaf7f82021-01-04 18:16:42 +01002390 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2391 &exec_ram_size);
2392 if (rc != 0) {
2393 return BOOT_EBADSTATUS;
2394 }
2395#else
2396 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2397 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2398#endif
2399
2400 img_dst = slot_usage[BOOT_CURR_IMG(state)].img_dst;
2401 img_sz = slot_usage[BOOT_CURR_IMG(state)].img_sz;
2402
2403 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002404 return BOOT_EBADIMAGE;
2405 }
2406
2407 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2408 return BOOT_EBADIMAGE;
2409 }
2410
Mark Horvathccaf7f82021-01-04 18:16:42 +01002411 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002412 return BOOT_EBADIMAGE;
2413 }
2414
2415 return 0;
2416}
2417
2418/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002419 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002420 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002421 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002422 * @param slot The flash slot of the image to be copied to SRAM.
2423 * @param img_dst The address at which the image needs to be copied to
2424 * SRAM.
2425 * @param img_sz The size of the image that needs to be copied to SRAM.
2426 *
2427 * @return 0 on success; nonzero on failure.
2428 */
2429static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002430boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2431 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002432{
2433 int rc;
2434 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002435 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002436
Mark Horvathccaf7f82021-01-04 18:16:42 +01002437#if (BOOT_IMAGE_NUMBER == 1)
2438 (void)state;
2439#endif
2440
2441 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2442
2443 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002444 if (rc != 0) {
2445 return BOOT_EFLASH;
2446 }
2447
2448 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002449 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002450 if (rc != 0) {
2451 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2452 }
2453
2454 flash_area_close(fap_src);
2455
2456 return rc;
2457}
2458
Mark Horvathccaf7f82021-01-04 18:16:42 +01002459#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002460/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002461 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002462 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002463 * @param start_a Start of the A region.
2464 * @param end_a End of the A region.
2465 * @param start_b Start of the B region.
2466 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002467 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002468 * @return true if there is overlap; false otherwise.
2469 */
2470static bool
2471do_regions_overlap(uint32_t start_a, uint32_t end_a,
2472 uint32_t start_b, uint32_t end_b)
2473{
2474 if (start_b > end_a) {
2475 return false;
2476 } else if (start_b >= start_a) {
2477 return true;
2478 } else if (end_b > start_a) {
2479 return true;
2480 }
2481
2482 return false;
2483}
2484
2485/**
2486 * Checks if the image we want to load to memory overlap with an already
2487 * ramloaded image.
2488 *
2489 * @param slot_usage Information about the active and available slots.
2490 * @param image_id_to_check The ID of the image we would like to load.
2491 *
2492 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002493 */
2494static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002495boot_check_ram_load_overlapping(struct slot_usage_t slot_usage[],
2496 uint32_t image_id_to_check)
Tamas Banfe031092020-09-10 17:32:39 +02002497{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002498 uint32_t i;
2499
2500 uint32_t start_a;
2501 uint32_t end_a;
2502 uint32_t start_b;
2503 uint32_t end_b;
2504
2505 start_a = slot_usage[image_id_to_check].img_dst;
2506 /* Safe to add here, values are already verified in
2507 * boot_verify_ram_load_address() */
2508 end_a = start_a + slot_usage[image_id_to_check].img_sz;
2509
2510 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
2511 if (slot_usage[i].active_slot == NO_ACTIVE_SLOT
2512 || i == image_id_to_check) {
2513 continue;
2514 }
2515
2516 start_b = slot_usage[i].img_dst;
2517 /* Safe to add here, values are already verified in
2518 * boot_verify_ram_load_address() */
2519 end_b = start_b + slot_usage[i].img_sz;
2520
2521 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2522 return -1;
2523 }
2524 }
2525
2526 return 0;
2527}
2528#endif
2529
2530/**
2531 * Loads the active slot of the current image into SRAM. The load address and
2532 * image size is extracted from the image header.
2533 *
2534 * @param state Boot loader status information.
2535 * @param slot_usage Information about the active and available slots.
2536 *
2537 * @return 0 on success; nonzero on failure.
2538 */
2539static int
2540boot_load_image_to_sram(struct boot_loader_state *state,
2541 struct slot_usage_t slot_usage[])
2542{
2543 uint32_t active_slot;
2544 struct image_header *hdr = NULL;
2545 uint32_t img_dst;
2546 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002547 int rc;
2548
Mark Horvathccaf7f82021-01-04 18:16:42 +01002549 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2550 hdr = boot_img_hdr(state, active_slot);
2551
Tamas Banfe031092020-09-10 17:32:39 +02002552 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2553
Mark Horvathccaf7f82021-01-04 18:16:42 +01002554 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002555
Mark Horvathccaf7f82021-01-04 18:16:42 +01002556 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002557 if (rc != 0) {
2558 return rc;
2559 }
2560
Mark Horvathccaf7f82021-01-04 18:16:42 +01002561 slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2562 slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
2563
2564 rc = boot_verify_ram_load_address(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002565 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002566 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002567 return rc;
2568 }
2569
Mark Horvathccaf7f82021-01-04 18:16:42 +01002570#if (BOOT_IMAGE_NUMBER > 1)
2571 rc = boot_check_ram_load_overlapping(slot_usage, BOOT_CURR_IMG(state));
2572 if (rc != 0) {
2573 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2574 another image.", img_dst);
2575 return rc;
2576 }
2577#endif
2578
Tamas Banfe031092020-09-10 17:32:39 +02002579 /* Copy image to the load address from where it currently resides in
2580 * flash.
2581 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002582 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002583 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002584 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002585 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002586 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002587 }
2588 } else {
2589 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2590 * MCUBOOT_RAM_LOAD is set.
2591 */
2592 rc = BOOT_EBADIMAGE;
2593 }
2594
Mark Horvathccaf7f82021-01-04 18:16:42 +01002595 if (rc != 0) {
2596 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2597 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2598 }
2599
Tamas Banfe031092020-09-10 17:32:39 +02002600 return rc;
2601}
2602
2603/**
2604 * Removes an image from SRAM, by overwriting it with zeros.
2605 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002606 * @param state Boot loader status information.
2607 * @param slot_usage Information about the active and available slots.
2608 *
2609 * @return 0 on success; nonzero on failure.
2610 */
2611static inline int
2612boot_remove_image_from_sram(struct boot_loader_state *state,
2613 struct slot_usage_t slot_usage[])
2614{
David Brown695e5912021-05-24 16:58:01 -06002615 (void)state;
2616
Mark Horvathccaf7f82021-01-04 18:16:42 +01002617 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
2618 slot_usage[BOOT_CURR_IMG(state)].img_dst);
2619
David Brown695e5912021-05-24 16:58:01 -06002620 memset((void*)(IMAGE_RAM_BASE + slot_usage[BOOT_CURR_IMG(state)].img_dst),
2621 0, slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002622
2623 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2624 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2625
2626 return 0;
2627}
2628
2629/**
2630 * Removes an image from flash by erasing the corresponding flash area
2631 *
2632 * @param state Boot loader status information.
2633 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002634 *
2635 * @return 0 on success; nonzero on failure.
2636 */
2637static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002638boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002639{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002640 int area_id;
2641 int rc;
2642 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002643
David Brown695e5912021-05-24 16:58:01 -06002644 (void)state;
2645
Mark Horvathccaf7f82021-01-04 18:16:42 +01002646 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2647 slot);
2648 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2649 rc = flash_area_open(area_id, &fap);
2650 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002651 flash_area_erase(fap, 0, flash_area_get_size(fap));
Mark Horvathccaf7f82021-01-04 18:16:42 +01002652 }
2653
2654 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002655}
2656#endif /* MCUBOOT_RAM_LOAD */
2657
Mark Horvathccaf7f82021-01-04 18:16:42 +01002658#if (BOOT_IMAGE_NUMBER > 1)
2659/**
2660 * Checks the image dependency whether it is satisfied.
2661 *
2662 * @param state Boot loader status information.
2663 * @param slot_usage Information about the active and available slots.
2664 * @param dep Image dependency which has to be verified.
2665 *
2666 * @return 0 if dependencies are met; nonzero otherwise.
2667 */
2668static int
2669boot_verify_slot_dependency(struct boot_loader_state *state,
2670 struct slot_usage_t slot_usage[],
2671 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002672{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002673 struct image_version *dep_version;
2674 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002675 int rc;
2676
Mark Horvathccaf7f82021-01-04 18:16:42 +01002677 /* Determine the source of the image which is the subject of
2678 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002679 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002680 dep_slot = slot_usage[dep->image_id].active_slot;
2681 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2682
2683 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2684 if (rc >= 0) {
2685 /* Dependency satisfied. */
2686 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002687 }
2688
Mark Horvathccaf7f82021-01-04 18:16:42 +01002689 return rc;
2690}
2691
2692/**
2693 * Reads all dependency TLVs of an image and verifies one after another to see
2694 * if they are all satisfied.
2695 *
2696 * @param state Boot loader status information.
2697 * @param slot_usage Information about the active and available slots.
2698 *
2699 * @return 0 if dependencies are met; nonzero otherwise.
2700 */
2701static int
2702boot_verify_slot_dependencies(struct boot_loader_state *state,
2703 struct slot_usage_t slot_usage[])
2704{
2705 uint32_t active_slot;
2706 const struct flash_area *fap;
2707 struct image_tlv_iter it;
2708 struct image_dependency dep;
2709 uint32_t off;
2710 uint16_t len;
2711 int area_id;
2712 int rc;
2713
2714 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2715
2716 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2717 active_slot);
2718 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002719 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002720 rc = BOOT_EFLASH;
2721 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002722 }
2723
Mark Horvathccaf7f82021-01-04 18:16:42 +01002724 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2725 IMAGE_TLV_DEPENDENCY, true);
2726 if (rc != 0) {
2727 goto done;
2728 }
David Vinczee574f2d2020-07-10 11:42:03 +02002729
Mark Horvathccaf7f82021-01-04 18:16:42 +01002730 while (true) {
2731 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2732 if (rc < 0) {
2733 return -1;
2734 } else if (rc > 0) {
2735 rc = 0;
2736 break;
2737 }
David Vinczee574f2d2020-07-10 11:42:03 +02002738
Mark Horvathccaf7f82021-01-04 18:16:42 +01002739 if (len != sizeof(dep)) {
2740 rc = BOOT_EBADIMAGE;
2741 goto done;
2742 }
2743
2744 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2745 fap, off, &dep, len);
2746 if (rc != 0) {
2747 rc = BOOT_EFLASH;
2748 goto done;
2749 }
2750
2751 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2752 rc = BOOT_EBADARGS;
2753 goto done;
2754 }
2755
2756 rc = boot_verify_slot_dependency(state, slot_usage, &dep);
2757 if (rc != 0) {
2758 /* Dependency not satisfied. */
2759 goto done;
2760 }
2761 }
2762
2763done:
2764 flash_area_close(fap);
2765 return rc;
2766}
2767
2768/**
2769 * Checks the dependency of all the active slots. If an image found with
2770 * invalid or not satisfied dependencies the image is removed from SRAM (in
2771 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2772 *
2773 * @param state Boot loader status information.
2774 * @param slot_usage Information about the active and available slots.
2775 *
2776 * @return 0 if dependencies are met; nonzero otherwise.
2777 */
2778static int
2779boot_verify_dependencies(struct boot_loader_state *state,
2780 struct slot_usage_t slot_usage[])
2781{
2782 int rc = -1;
2783 uint32_t active_slot;
2784
2785 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2786 rc = boot_verify_slot_dependencies(state, slot_usage);
2787 if (rc != 0) {
2788 /* Dependencies not met or invalid dependencies. */
2789
2790#ifdef MCUBOOT_RAM_LOAD
2791 boot_remove_image_from_sram(state, slot_usage);
2792#endif /* MCUBOOT_RAM_LOAD */
2793
2794 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2795 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2796 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2797
2798 return rc;
2799 }
2800 }
2801
2802 return rc;
2803}
2804#endif /* (BOOT_IMAGE_NUMBER > 1) */
2805
2806/**
2807 * Tries to load a slot for all the images with validation.
2808 *
2809 * @param state Boot loader status information.
2810 * @param slot_usage Information about the active and available slots.
2811 *
2812 * @return 0 on success; nonzero on failure.
2813 */
2814fih_int
2815boot_load_and_validate_images(struct boot_loader_state *state,
2816 struct slot_usage_t slot_usage[])
2817{
2818 uint32_t active_slot;
2819 int rc;
2820 fih_int fih_rc;
2821
2822 /* Go over all the images and try to load one */
2823 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2824 /* All slots tried until a valid image found. Breaking from this loop
2825 * means that a valid image found or already loaded. If no slot is
2826 * found the function returns with error code. */
2827 while (true) {
2828
2829 /* Go over all the slots and try to load one */
2830 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2831 if (active_slot != NO_ACTIVE_SLOT){
2832 /* A slot is already active, go to next image. */
2833 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002834 }
David Vincze505fba22020-10-22 13:53:29 +02002835
Mark Horvathccaf7f82021-01-04 18:16:42 +01002836 active_slot = find_slot_with_highest_version(state,
2837 slot_usage);
2838 if (active_slot == NO_ACTIVE_SLOT) {
2839 BOOT_LOG_INF("No slot to load for image %d",
2840 BOOT_CURR_IMG(state));
2841 FIH_RET(FIH_FAILURE);
2842 }
2843
2844 /* Save the number of the active slot. */
2845 slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
2846
2847#ifdef MCUBOOT_DIRECT_XIP
2848 rc = boot_rom_address_check(state, slot_usage);
2849 if (rc != 0) {
2850 /* The image is placed in an unsuitable slot. */
2851 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2852 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2853 continue;
2854 }
George Becksteind4d90f82021-05-11 02:00:00 -04002855
David Vincze505fba22020-10-22 13:53:29 +02002856#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002857 rc = boot_select_or_erase(state, slot_usage);
David Vincze505fba22020-10-22 13:53:29 +02002858 if (rc != 0) {
2859 /* The selected image slot has been erased. */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002860 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2861 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02002862 continue;
2863 }
2864#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02002865#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02002866
Tamas Banfe031092020-09-10 17:32:39 +02002867#ifdef MCUBOOT_RAM_LOAD
2868 /* Image is first loaded to RAM and authenticated there in order to
2869 * prevent TOCTOU attack during image copy. This could be applied
2870 * when loading images from external (untrusted) flash to internal
2871 * (trusted) RAM and image is authenticated before copying.
2872 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002873 rc = boot_load_image_to_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002874 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002875 /* Image cannot be ramloaded. */
2876 boot_remove_image_from_flash(state, active_slot);
2877 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2878 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02002879 continue;
Tamas Banfe031092020-09-10 17:32:39 +02002880 }
2881#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002882
2883 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
2884 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2885 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02002886#ifdef MCUBOOT_RAM_LOAD
Mark Horvathccaf7f82021-01-04 18:16:42 +01002887 boot_remove_image_from_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002888#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002889 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2890 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2891 continue;
David Vincze505fba22020-10-22 13:53:29 +02002892 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01002893
2894 /* Valid image loaded from a slot, go to next image. */
2895 break;
2896 }
2897 }
2898
2899 FIH_RET(FIH_SUCCESS);
2900}
2901
2902/**
2903 * Updates the security counter for the current image.
2904 *
2905 * @param state Boot loader status information.
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002906 * @param slot_usage Information about the active and available slots.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002907 *
2908 * @return 0 on success; nonzero on failure.
2909 */
2910static int
2911boot_update_hw_rollback_protection(struct boot_loader_state *state,
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002912 const struct slot_usage_t slot_usage[])
Mark Horvathccaf7f82021-01-04 18:16:42 +01002913{
2914#ifdef MCUBOOT_HW_ROLLBACK_PROT
2915 int rc;
2916
2917 /* Update the stored security counter with the newer (active) image's
2918 * security counter value.
2919 */
David Vincze1c456242021-06-29 15:25:24 +02002920#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002921 /* When the 'revert' mechanism is enabled in direct-xip mode, the
2922 * security counter can be increased only after reboot, if the image
2923 * has been confirmed at runtime (the image_ok flag has been set).
2924 * This way a 'revert' can be performed when it's necessary.
2925 */
2926 if (slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02002927#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002928 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
2929 slot_usage[BOOT_CURR_IMG(state)].active_slot,
2930 boot_img_hdr(state, slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02002931 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002932 BOOT_LOG_ERR("Security counter update failed after image "
2933 "validation.");
2934 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002935 }
David Vincze1c456242021-06-29 15:25:24 +02002936#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002937 }
2938#endif
David Vinczee574f2d2020-07-10 11:42:03 +02002939
Mark Horvathccaf7f82021-01-04 18:16:42 +01002940 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002941
Mark Horvathccaf7f82021-01-04 18:16:42 +01002942#else /* MCUBOOT_HW_ROLLBACK_PROT */
2943 (void) (state);
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002944 (void) (slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002945 return 0;
2946#endif
2947}
2948
2949fih_int
2950context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2951{
2952 struct slot_usage_t slot_usage[BOOT_IMAGE_NUMBER];
2953 int rc;
David Brown695e5912021-05-24 16:58:01 -06002954 fih_int fih_rc = fih_int_encode(0);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002955
2956 memset(state, 0, sizeof(struct boot_loader_state));
2957 memset(slot_usage, 0, sizeof(struct slot_usage_t) * BOOT_IMAGE_NUMBER);
2958
2959 rc = boot_get_slot_usage(state, slot_usage);
2960 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02002961 goto out;
2962 }
2963
Mark Horvathccaf7f82021-01-04 18:16:42 +01002964#if (BOOT_IMAGE_NUMBER > 1)
2965 while (true) {
2966#endif
2967 FIH_CALL(boot_load_and_validate_images, fih_rc, state, slot_usage);
2968 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2969 goto out;
2970 }
2971
2972#if (BOOT_IMAGE_NUMBER > 1)
2973 rc = boot_verify_dependencies(state, slot_usage);
2974 if (rc != 0) {
2975 /* Dependency check failed for an image, it has been removed from
2976 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
2977 * unavailable. Try to load an image from another slot.
2978 */
2979 continue;
2980 }
2981 /* Dependency check was successful. */
2982 break;
2983 }
2984#endif
2985
2986 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002987 rc = boot_update_hw_rollback_protection(state, slot_usage);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002988 if (rc != 0) {
2989 goto out;
2990 }
2991
Sherry Zhang50b06ae2021-07-09 15:22:51 +08002992 rc = boot_add_shared_data(state, slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002993 if (rc != 0) {
2994 goto out;
2995 }
2996 }
2997
2998 /* All image loaded successfully. */
2999#ifdef MCUBOOT_HAVE_LOGGING
3000 print_loaded_images(state, slot_usage);
3001#endif
3002
3003 fill_rsp(state, slot_usage, rsp);
3004
David Vinczee574f2d2020-07-10 11:42:03 +02003005out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003006 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003007
Mark Horvathccaf7f82021-01-04 18:16:42 +01003008 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3009 fih_rc = fih_int_encode(rc);
3010 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003011
Mark Horvathccaf7f82021-01-04 18:16:42 +01003012 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003013}
Tamas Banfe031092020-09-10 17:32:39 +02003014#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003015
3016/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003017 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003018 * appropriate, and tells you what address to boot from.
3019 *
3020 * @param rsp On success, indicates how booting should occur.
3021 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003022 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003023 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003024fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003025boot_go(struct boot_rsp *rsp)
3026{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003027 fih_int fih_rc = FIH_FAILURE;
3028 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3029 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003030}