blob: 7f4261bdac97fb8c94ff80be352bd9d96c9ed40a [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 <os/os_malloc.h>
39#include "bootutil/bootutil.h"
40#include "bootutil/image.h"
41#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030042#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050043#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010044#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010045#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010046#include "bootutil/fault_injection_hardening.h"
Mark Horvathccaf7f82021-01-04 18:16:42 +010047#include "bootutil/ramload.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
Fabio Utzigba1fbe62017-07-21 14:01:20 -030053#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030054
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010055MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
56
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040057static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080058
Fabio Utzigabec0732019-07-31 08:40:22 -030059#if (BOOT_IMAGE_NUMBER > 1)
60#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
61#else
62#define IMAGES_ITER(x)
63#endif
64
Mark Horvathccaf7f82021-01-04 18:16:42 +010065#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
66struct slot_usage_t {
67 /* Index of the slot chosen to be loaded */
68 uint32_t active_slot;
69 bool slot_available[BOOT_NUM_SLOTS];
70#ifdef MCUBOOT_RAM_LOAD
71 /* Image destination and size for the active slot */
72 uint32_t img_dst;
73 uint32_t img_sz;
74#endif /* MCUBOOT_RAM_LOAD */
75#ifdef MCUBOOT_DIRECT_XIP_REVERT
76 /* Swap status for the active slot */
77 struct boot_swap_state swap_state
78#endif /* MCUBOOT_DIRECT_XIP_REVERT */
79};
80#endif
81
Fabio Utzig10ee6482019-08-01 12:04:52 -030082/*
83 * This macro allows some control on the allocation of local variables.
84 * When running natively on a target, we don't want to allocated huge
85 * variables on the stack, so make them global instead. For the simulator
86 * we want to run as many threads as there are tests, and it's safer
87 * to just make those variables stack allocated.
88 */
89#if !defined(__BOOTSIM__)
90#define TARGET_STATIC static
91#else
92#define TARGET_STATIC
93#endif
94
David Vinczee574f2d2020-07-10 11:42:03 +020095static int
96boot_read_image_headers(struct boot_loader_state *state, bool require_all,
97 struct boot_status *bs)
98{
99 int rc;
100 int i;
101
102 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
103 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
104 if (rc != 0) {
105 /* If `require_all` is set, fail on any single fail, otherwise
106 * if at least the first slot's header was read successfully,
107 * then the boot loader can attempt a boot.
108 *
109 * Failure to read any headers is a fatal error.
110 */
111 if (i > 0 && !require_all) {
112 return 0;
113 } else {
114 return rc;
115 }
116 }
117 }
118
119 return 0;
120}
121
Mark Horvathccaf7f82021-01-04 18:16:42 +0100122/**
123 * Saves boot status and shared data for current image.
124 *
125 * @param state Boot loader status information.
126 * @param active_slot Index of the slot will be loaded for current image.
127 *
128 * @return 0 on success; nonzero on failure.
129 */
130static int
131boot_add_shared_data(struct boot_loader_state *state,
132 uint32_t active_slot)
133{
134#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
135 int rc;
136
137#ifdef MCUBOOT_MEASURED_BOOT
138 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
139 boot_img_hdr(state, active_slot),
140 BOOT_IMG_AREA(state, active_slot));
141 if (rc != 0) {
142 BOOT_LOG_ERR("Failed to add image data to shared area");
143 return rc;
144 }
145#endif /* MCUBOOT_MEASURED_BOOT */
146
147#ifdef MCUBOOT_DATA_SHARING
148 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
149 BOOT_IMG_AREA(state, active_slot));
150 if (rc != 0) {
151 BOOT_LOG_ERR("Failed to add data to shared memory area.");
152 return rc;
153 }
154#endif /* MCUBOOT_DATA_SHARING */
155
156 return 0;
157
158#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
159 (void) (state);
160 (void) (active_slot);
161
162 return 0;
163#endif
164}
165
166/**
167 * Fills rsp to indicate how booting should occur.
168 *
169 * @param state Boot loader status information.
170 * @param slot_usage Information about the active and available slots.
171 * Only used in MCUBOOT_DIRECT_XIP and MCUBOOT_RAM_LOAD
172 * @param rsp boot_rsp struct to fill.
173 */
174static void
175fill_rsp(struct boot_loader_state *state, void *slot_usage,
176 struct boot_rsp *rsp)
177{
178 uint32_t active_slot;
179
180#if (BOOT_IMAGE_NUMBER > 1)
181 /* Always boot from Image 0. */
182 BOOT_CURR_IMG(state) = 0;
183#endif
184
185#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
186 active_slot = ((struct slot_usage_t *)slot_usage)[BOOT_CURR_IMG(state)].active_slot;
187#else
188 (void) (slot_usage);
189 active_slot = BOOT_PRIMARY_SLOT;
190#endif
191
192 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, active_slot)->fa_device_id;
193 rsp->br_image_off = boot_img_slot_off(state, active_slot);
194 rsp->br_hdr = boot_img_hdr(state, active_slot);
195}
196
197/**
198 * Closes all flash areas.
199 *
200 * @param state Boot loader status information.
201 */
202static void
203close_all_flash_areas(struct boot_loader_state *state)
204{
205 uint32_t slot;
206
207 IMAGES_ITER(BOOT_CURR_IMG(state)) {
208#if MCUBOOT_SWAP_USING_SCRATCH
209 flash_area_close(BOOT_SCRATCH_AREA(state));
210#endif
211 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
212 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
213 }
214 }
215}
216
Tamas Banfe031092020-09-10 17:32:39 +0200217#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600218/*
219 * Compute the total size of the given image. Includes the size of
220 * the TLVs.
221 */
Tamas Banfe031092020-09-10 17:32:39 +0200222#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600223static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300224boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600225{
226 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300227 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300228 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300229 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600230 int area_id;
231 int rc;
232
Fabio Utzig10ee6482019-08-01 12:04:52 -0300233#if (BOOT_IMAGE_NUMBER == 1)
234 (void)state;
235#endif
236
237 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600238 rc = flash_area_open(area_id, &fap);
239 if (rc != 0) {
240 rc = BOOT_EFLASH;
241 goto done;
242 }
243
Fabio Utzig61fd8882019-09-14 20:00:20 -0300244 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
245
246 if (flash_area_read(fap, off, &info, sizeof(info))) {
247 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600248 goto done;
249 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300250
Fabio Utzige52c08e2019-09-11 19:32:00 -0300251 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
252 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
253 if (protect_tlv_size != info.it_tlv_tot) {
254 rc = BOOT_EBADIMAGE;
255 goto done;
256 }
257
258 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
259 rc = BOOT_EFLASH;
260 goto done;
261 }
262 } else if (protect_tlv_size != 0) {
263 rc = BOOT_EBADIMAGE;
264 goto done;
265 }
266
Fabio Utzig61fd8882019-09-14 20:00:20 -0300267 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
268 rc = BOOT_EBADIMAGE;
269 goto done;
270 }
271
Fabio Utzige52c08e2019-09-11 19:32:00 -0300272 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600273 rc = 0;
274
275done:
276 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300277 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600278}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300279#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600280
Tamas Banfe031092020-09-10 17:32:39 +0200281#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700282static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300283boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800284{
David Brownab449182019-11-15 09:32:52 -0700285 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300286#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700287 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300288#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289
290 /* Figure out what size to write update status update as. The size depends
291 * on what the minimum write size is for scratch area, active image slot.
292 * We need to use the bigger of those 2 values.
293 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300294 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300295#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300296 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800297 if (align > elem_sz) {
298 elem_sz = align;
299 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300300#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301
302 return elem_sz;
303}
304
Fabio Utzig10ee6482019-08-01 12:04:52 -0300305#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
306static int
307boot_initialize_area(struct boot_loader_state *state, int flash_area)
308{
309 int num_sectors = BOOT_MAX_IMG_SECTORS;
310 int rc;
311
312 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
313 rc = flash_area_to_sectors(flash_area, &num_sectors,
314 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
315 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
316
317 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
318 rc = flash_area_to_sectors(flash_area, &num_sectors,
319 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
320 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
321
Fabio Utzig12d59162019-11-28 10:01:59 -0300322#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300323 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
324 rc = flash_area_to_sectors(flash_area, &num_sectors,
325 state->scratch.sectors);
326 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300327#endif
328
Fabio Utzig10ee6482019-08-01 12:04:52 -0300329 } else {
330 return BOOT_EFLASH;
331 }
332
333 return rc;
334}
335#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
336static int
337boot_initialize_area(struct boot_loader_state *state, int flash_area)
338{
339 uint32_t num_sectors;
340 struct flash_sector *out_sectors;
341 size_t *out_num_sectors;
342 int rc;
343
344 num_sectors = BOOT_MAX_IMG_SECTORS;
345
346 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
347 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
348 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
349 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
350 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
351 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300352#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300353 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
354 out_sectors = state->scratch.sectors;
355 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300356#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300357 } else {
358 return BOOT_EFLASH;
359 }
360
361 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
362 if (rc != 0) {
363 return rc;
364 }
365 *out_num_sectors = num_sectors;
366 return 0;
367}
368#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
369
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370/**
371 * Determines the sector layout of both image slots and the scratch area.
372 * This information is necessary for calculating the number of bytes to erase
373 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300374 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375 */
376static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300377boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800378{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300379 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800380 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381
Fabio Utzig10ee6482019-08-01 12:04:52 -0300382 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300383
384 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385 if (rc != 0) {
386 return BOOT_EFLASH;
387 }
388
Fabio Utzig10ee6482019-08-01 12:04:52 -0300389 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800390 if (rc != 0) {
391 return BOOT_EFLASH;
392 }
393
Fabio Utzig12d59162019-11-28 10:01:59 -0300394#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300395 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200396 if (rc != 0) {
397 return BOOT_EFLASH;
398 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300399#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200400
Fabio Utzig10ee6482019-08-01 12:04:52 -0300401 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800402
403 return 0;
404}
405
Fabio Utzig12d59162019-11-28 10:01:59 -0300406void
407boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800408{
Fabio Utzig4741c452019-12-19 15:32:41 -0300409#ifdef MCUBOOT_ENC_IMAGES
410 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
411#if MCUBOOT_SWAP_SAVE_ENCTLV
412 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
413#endif
414#endif /* MCUBOOT_ENC_IMAGES */
415
416 bs->use_scratch = 0;
417 bs->swap_size = 0;
418 bs->source = 0;
419
Fabio Utzig74aef312019-11-28 11:05:34 -0300420 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300421 bs->idx = BOOT_STATUS_IDX_0;
422 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700423 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300424}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425
Fabio Utzig12d59162019-11-28 10:01:59 -0300426bool
427boot_status_is_reset(const struct boot_status *bs)
428{
Fabio Utzig74aef312019-11-28 11:05:34 -0300429 return (bs->op == BOOT_STATUS_OP_MOVE &&
430 bs->idx == BOOT_STATUS_IDX_0 &&
431 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800432}
433
434/**
435 * Writes the supplied boot status to the flash file system. The boot status
436 * contains the current state of an in-progress image copy operation.
437 *
438 * @param bs The boot status to write.
439 *
440 * @return 0 on success; nonzero on failure.
441 */
442int
Fabio Utzig12d59162019-11-28 10:01:59 -0300443boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444{
445 const struct flash_area *fap;
446 uint32_t off;
447 int area_id;
448 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300449 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700450 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300451 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800452
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300453 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100454 * the trailer. Since in the last step the primary slot is erased, the
455 * first two status writes go to the scratch which will be copied to
456 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300457 */
458
Fabio Utzig12d59162019-11-28 10:01:59 -0300459#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300460 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800461 /* Write to scratch. */
462 area_id = FLASH_AREA_IMAGE_SCRATCH;
463 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300464#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100465 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300466 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300467#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800468 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300469#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470
471 rc = flash_area_open(area_id, &fap);
472 if (rc != 0) {
473 rc = BOOT_EFLASH;
474 goto done;
475 }
476
477 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300478 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200479 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300480 erased_val = flash_area_erased_val(fap);
481 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700482 buf[0] = bs->state;
483
484 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800485 if (rc != 0) {
486 rc = BOOT_EFLASH;
487 goto done;
488 }
489
490 rc = 0;
491
492done:
493 flash_area_close(fap);
494 return rc;
495}
Tamas Banfe031092020-09-10 17:32:39 +0200496#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200497#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800498
499/*
David Vinczec3084132020-02-18 14:50:47 +0100500 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800501 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100502static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300503boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
504 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800505{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300506 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300507 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300508 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100509 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300510
Fabio Utzig10ee6482019-08-01 12:04:52 -0300511#if (BOOT_IMAGE_NUMBER == 1)
512 (void)state;
513#endif
514
Fabio Utzigba829042018-09-18 08:29:34 -0300515 (void)bs;
516 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300517
518 image_index = BOOT_CURR_IMG(state);
519
520#ifdef MCUBOOT_ENC_IMAGES
521 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300522 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300523 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100524 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300525 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300526 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100527 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300528 }
529 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300530#endif
531
Raef Colese8fe6cf2020-05-26 13:07:40 +0100532 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
533 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300534
Raef Colese8fe6cf2020-05-26 13:07:40 +0100535 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800536}
537
Tamas Banfe031092020-09-10 17:32:39 +0200538#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100539static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540split_image_check(struct image_header *app_hdr,
541 const struct flash_area *app_fap,
542 struct image_header *loader_hdr,
543 const struct flash_area *loader_fap)
544{
545 static void *tmpbuf;
546 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100547 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800548
549 if (!tmpbuf) {
550 tmpbuf = malloc(BOOT_TMPBUF_SZ);
551 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100552 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553 }
554 }
555
Raef Colese8fe6cf2020-05-26 13:07:40 +0100556 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
557 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
558 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
559 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560 }
561
Raef Colese8fe6cf2020-05-26 13:07:40 +0100562 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
563 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800564
Raef Colese8fe6cf2020-05-26 13:07:40 +0100565out:
566 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800567}
Tamas Banfe031092020-09-10 17:32:39 +0200568#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569
Fabio Utzig338a19f2018-12-03 08:37:08 -0200570/*
David Brown9bf95af2019-10-10 15:36:36 -0600571 * Check that this is a valid header. Valid means that the magic is
572 * correct, and that the sizes/offsets are "sane". Sane means that
573 * there is no overflow on the arithmetic, and that the result fits
574 * within the flash area we are in.
575 */
576static bool
577boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
578{
579 uint32_t size;
580
581 if (hdr->ih_magic != IMAGE_MAGIC) {
582 return false;
583 }
584
585 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
586 return false;
587 }
588
589 if (size >= fap->fa_size) {
590 return false;
591 }
592
593 return true;
594}
595
596/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200597 * Check that a memory area consists of a given value.
598 */
599static inline bool
600boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300601{
602 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200603 uint8_t *p = (uint8_t *)data;
604 for (i = 0; i < len; i++) {
605 if (val != p[i]) {
606 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300607 }
608 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200609 return true;
610}
611
612static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300613boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200614{
615 const struct flash_area *fap;
616 struct image_header *hdr;
617 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300618 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200619 int rc;
620
Fabio Utzig10ee6482019-08-01 12:04:52 -0300621 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300622 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200623 if (rc != 0) {
624 return -1;
625 }
626
627 erased_val = flash_area_erased_val(fap);
628 flash_area_close(fap);
629
Fabio Utzig10ee6482019-08-01 12:04:52 -0300630 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200631 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
632 return -1;
633 }
634
635 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300636}
637
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000638#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200639 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200640 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000641 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
642/**
David Vincze8b0b6372020-05-20 19:54:44 +0200643 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000644 *
David Vincze8b0b6372020-05-20 19:54:44 +0200645 * @param ver1 Pointer to the first image version to compare.
646 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000647 *
David Vincze8b0b6372020-05-20 19:54:44 +0200648 * @retval -1 If ver1 is strictly less than ver2.
649 * @retval 0 If the image version numbers are equal,
650 * (not including the build number).
651 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000652 */
653static int
David Vincze8b0b6372020-05-20 19:54:44 +0200654boot_version_cmp(const struct image_version *ver1,
655 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000656{
David Vincze8b0b6372020-05-20 19:54:44 +0200657 if (ver1->iv_major > ver2->iv_major) {
658 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000659 }
David Vincze8b0b6372020-05-20 19:54:44 +0200660 if (ver1->iv_major < ver2->iv_major) {
661 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000662 }
David Vincze8b0b6372020-05-20 19:54:44 +0200663 /* The major version numbers are equal, continue comparison. */
664 if (ver1->iv_minor > ver2->iv_minor) {
665 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000666 }
David Vincze8b0b6372020-05-20 19:54:44 +0200667 if (ver1->iv_minor < ver2->iv_minor) {
668 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000669 }
David Vincze8b0b6372020-05-20 19:54:44 +0200670 /* The minor version numbers are equal, continue comparison. */
671 if (ver1->iv_revision > ver2->iv_revision) {
672 return 1;
673 }
674 if (ver1->iv_revision < ver2->iv_revision) {
675 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000676 }
677
678 return 0;
679}
680#endif
681
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000682#if defined(MCUBOOT_DIRECT_XIP)
683/**
684 * Check if image in slot has been set with specific ROM address to run from
685 * and whether the slot starts at that address.
686 *
687 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
688 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
689 * header matches the slot address;
690 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
691 * does not match the slot address.
692 */
693static bool
Mark Horvathccaf7f82021-01-04 18:16:42 +0100694boot_rom_address_check(struct boot_loader_state *state,
695 struct slot_usage_t slot_usage[])
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000696{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100697 uint32_t active_slot;
698 const struct image_header *hdr;
699 uint32_t f_off;
700
701 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
702 hdr = boot_img_hdr(state, active_slot);
703 f_off = boot_img_slot_off(state, active_slot);
704
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000705 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
706 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100707 ", skipping",
708 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000709 hdr->ih_load_addr);
710
711 /* If there is address mismatch, the image is not bootable from this
712 * slot.
713 */
714 return 1;
715 }
716 return 0;
717}
718#endif
719
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300720/*
721 * Check that there is a valid image in a slot
722 *
723 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100724 * FIH_SUCCESS if image was successfully validated
725 * 1 (or its fih_int encoded form) if no bootloable image was found
726 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300727 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100728static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300729boot_validate_slot(struct boot_loader_state *state, int slot,
730 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800731{
732 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400733 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300734 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100735 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800736 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300737
Fabio Utzig10ee6482019-08-01 12:04:52 -0300738 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300739 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800740 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100741 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800742 }
743
Fabio Utzig10ee6482019-08-01 12:04:52 -0300744 hdr = boot_img_hdr(state, slot);
745 if (boot_check_header_erased(state, slot) == 0 ||
746 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300747
748#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
749 /*
750 * This fixes an issue where an image might be erased, but a trailer
751 * be left behind. It can happen if the image is in the secondary slot
752 * and did not pass validation, in which case the whole slot is erased.
753 * If during the erase operation, a reset occurs, parts of the slot
754 * might have been erased while some did not. The concerning part is
755 * the trailer because it might disable a new image from being loaded
756 * through mcumgr; so we just get rid of the trailer here, if the header
757 * is erased.
758 */
759 if (slot != BOOT_PRIMARY_SLOT) {
760 swap_erase_trailer_sectors(state, fap);
761 }
762#endif
763
David Vincze2d736ad2019-02-18 11:50:22 +0100764 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100765 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200766 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300767 }
768
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000769#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
770 if (slot != BOOT_PRIMARY_SLOT) {
771 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200772 rc = boot_version_cmp(
773 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
774 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
775 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000776 BOOT_LOG_ERR("insufficient version in secondary slot");
777 flash_area_erase(fap, 0, fap->fa_size);
778 /* Image in the secondary slot does not satisfy version requirement.
779 * Erase the image and continue booting from the primary slot.
780 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100781 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000782 goto out;
783 }
784 }
785#endif
786
Raef Colese8fe6cf2020-05-26 13:07:40 +0100787 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
788 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200789 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
David Brownb38e0442017-02-24 13:57:12 -0700790 flash_area_erase(fap, 0, fap->fa_size);
David Vinczee574f2d2020-07-10 11:42:03 +0200791 /* Image is invalid, erase it to prevent further unnecessary
792 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700793 */
794 }
David Brown098de832019-12-10 11:58:01 -0700795#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100796 BOOT_LOG_ERR("Image in the %s slot is not valid!",
797 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700798#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100799 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200800 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800801 }
802
Fabio Utzig338a19f2018-12-03 08:37:08 -0200803out:
804 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100805
806 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800807}
808
David Vinczec3084132020-02-18 14:50:47 +0100809#ifdef MCUBOOT_HW_ROLLBACK_PROT
810/**
811 * Updates the stored security counter value with the image's security counter
812 * value which resides in the given slot, only if it's greater than the stored
813 * value.
814 *
815 * @param image_index Index of the image to determine which security
816 * counter to update.
817 * @param slot Slot number of the image.
818 * @param hdr Pointer to the image header structure of the image
819 * that is currently stored in the given slot.
820 *
821 * @return 0 on success; nonzero on failure.
822 */
823static int
824boot_update_security_counter(uint8_t image_index, int slot,
825 struct image_header *hdr)
826{
827 const struct flash_area *fap = NULL;
828 uint32_t img_security_cnt;
829 int rc;
830
831 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
832 &fap);
833 if (rc != 0) {
834 rc = BOOT_EFLASH;
835 goto done;
836 }
837
838 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
839 if (rc != 0) {
840 goto done;
841 }
842
843 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
844 if (rc != 0) {
845 goto done;
846 }
847
848done:
849 flash_area_close(fap);
850 return rc;
851}
852#endif /* MCUBOOT_HW_ROLLBACK_PROT */
853
Tamas Banfe031092020-09-10 17:32:39 +0200854#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200855/**
856 * Determines which swap operation to perform, if any. If it is determined
857 * that a swap operation is required, the image in the secondary slot is checked
858 * for validity. If the image in the secondary slot is invalid, it is erased,
859 * and a swap type of "none" is indicated.
860 *
861 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
862 */
863static int
864boot_validated_swap_type(struct boot_loader_state *state,
865 struct boot_status *bs)
866{
867 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100868 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200869
870 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
871 if (BOOT_IS_UPGRADE(swap_type)) {
872 /* Boot loader wants to switch to the secondary slot.
873 * Ensure image is valid.
874 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100875 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
876 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
877 if (fih_eq(fih_rc, fih_int_encode(1))) {
878 swap_type = BOOT_SWAP_TYPE_NONE;
879 } else {
880 swap_type = BOOT_SWAP_TYPE_FAIL;
881 }
David Vinczee574f2d2020-07-10 11:42:03 +0200882 }
883 }
884
885 return swap_type;
886}
887
Christopher Collins92ea77f2016-12-12 15:59:26 -0800888/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800889 * Erases a region of flash.
890 *
Fabio Utzigba829042018-09-18 08:29:34 -0300891 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800892 * @param off The offset within the flash area to start the
893 * erase.
894 * @param sz The number of bytes to erase.
895 *
896 * @return 0 on success; nonzero on failure.
897 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300898int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300899boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800900{
Fabio Utzigba829042018-09-18 08:29:34 -0300901 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800902}
903
904/**
905 * Copies the contents of one flash region to another. You must erase the
906 * destination region prior to calling this function.
907 *
908 * @param flash_area_id_src The ID of the source flash area.
909 * @param flash_area_id_dst The ID of the destination flash area.
910 * @param off_src The offset within the source flash area to
911 * copy from.
912 * @param off_dst The offset within the destination flash area to
913 * copy to.
914 * @param sz The number of bytes to copy.
915 *
916 * @return 0 on success; nonzero on failure.
917 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300918int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300919boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300920 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300921 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800922 uint32_t off_src, uint32_t off_dst, uint32_t sz)
923{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800924 uint32_t bytes_copied;
925 int chunk_sz;
926 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300927#ifdef MCUBOOT_ENC_IMAGES
928 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300929 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300930 size_t blk_off;
931 struct image_header *hdr;
932 uint16_t idx;
933 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300934 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300935#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800936
Fabio Utzig10ee6482019-08-01 12:04:52 -0300937 TARGET_STATIC uint8_t buf[1024];
938
939#if !defined(MCUBOOT_ENC_IMAGES)
940 (void)state;
941#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800942
Christopher Collins92ea77f2016-12-12 15:59:26 -0800943 bytes_copied = 0;
944 while (bytes_copied < sz) {
945 if (sz - bytes_copied > sizeof buf) {
946 chunk_sz = sizeof buf;
947 } else {
948 chunk_sz = sz - bytes_copied;
949 }
950
951 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
952 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300953 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800954 }
955
Fabio Utzigba829042018-09-18 08:29:34 -0300956#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300957 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300958 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
959 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
960 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
961 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100962 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300963 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300964#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300965 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300966 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100967 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300968 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300969 off = off_dst;
970 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300971#else
972 off = off_dst;
973 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
974 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
975 }
976#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200977 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300978 blk_sz = chunk_sz;
979 idx = 0;
980 if (off + bytes_copied < hdr->ih_hdr_size) {
981 /* do not decrypt header */
982 blk_off = 0;
983 blk_sz = chunk_sz - hdr->ih_hdr_size;
984 idx = hdr->ih_hdr_size;
985 } else {
986 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
987 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300988 tlv_off = BOOT_TLV_OFF(hdr);
989 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300990 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300991 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300992 blk_sz = 0;
993 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300994 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300995 }
996 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300997 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300998 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
999 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001000 }
1001 }
1002#endif
1003
Christopher Collins92ea77f2016-12-12 15:59:26 -08001004 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1005 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001006 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001007 }
1008
1009 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001010
1011 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001012 }
1013
Fabio Utzigba829042018-09-18 08:29:34 -03001014 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001015}
1016
Christopher Collins92ea77f2016-12-12 15:59:26 -08001017/**
David Vincze2d736ad2019-02-18 11:50:22 +01001018 * Overwrite primary slot with the image contained in the secondary slot.
1019 * If a prior copy operation was interrupted by a system reset, this function
1020 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001021 *
1022 * @param bs The current boot status. This function reads
1023 * this struct to determine if it is resuming
1024 * an interrupted swap operation. This
1025 * function writes the updated status to this
1026 * function on return.
1027 *
1028 * @return 0 on success; nonzero on failure.
1029 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001030#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001031static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001032boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001033{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001034 size_t sect_count;
1035 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001036 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001037 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001038 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001039 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001040 const struct flash_area *fap_primary_slot;
1041 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001042 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001043
Fabio Utzigb4f88102020-10-04 10:16:24 -03001044#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1045 uint32_t sector;
1046 uint32_t trailer_sz;
1047 uint32_t off;
1048 uint32_t sz;
1049#endif
1050
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001051 (void)bs;
1052
Fabio Utzig13d9e352017-10-05 20:32:31 -03001053#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1054 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001055 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001056 assert(rc == 0);
1057#endif
David Brown17609d82017-05-05 09:41:34 -06001058
David Vincze2d736ad2019-02-18 11:50:22 +01001059 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1060 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001061
Fabio Utzigb0f04732019-07-31 09:49:19 -03001062 image_index = BOOT_CURR_IMG(state);
1063
1064 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1065 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001066 assert (rc == 0);
1067
Fabio Utzigb0f04732019-07-31 09:49:19 -03001068 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1069 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001070 assert (rc == 0);
1071
Fabio Utzig10ee6482019-08-01 12:04:52 -03001072 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001073 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001074 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001075 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001076 assert(rc == 0);
1077
Fabio Utzig13d9e352017-10-05 20:32:31 -03001078#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001079 if ((size + this_size) >= src_size) {
1080 size += src_size - size;
1081 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001082 break;
1083 }
1084#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001085
1086 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001087 }
1088
Fabio Utzigb4f88102020-10-04 10:16:24 -03001089#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1090 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1091 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1092 sz = 0;
1093 do {
1094 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1095 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1096 sector--;
1097 } while (sz < trailer_sz);
1098
1099 rc = boot_erase_region(fap_primary_slot, off, sz);
1100 assert(rc == 0);
1101#endif
1102
Fabio Utzigba829042018-09-18 08:29:34 -03001103#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001104 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001105 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001106 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001107 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001108
Fabio Utzigba829042018-09-18 08:29:34 -03001109 if (rc < 0) {
1110 return BOOT_EBADIMAGE;
1111 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001112 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001113 return BOOT_EBADIMAGE;
1114 }
1115 }
1116#endif
1117
David Vincze2d736ad2019-02-18 11:50:22 +01001118 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1119 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001120 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001121 if (rc != 0) {
1122 return rc;
1123 }
1124
1125#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1126 rc = boot_write_magic(fap_primary_slot);
1127 if (rc != 0) {
1128 return rc;
1129 }
1130#endif
David Brown17609d82017-05-05 09:41:34 -06001131
David Vinczec3084132020-02-18 14:50:47 +01001132#ifdef MCUBOOT_HW_ROLLBACK_PROT
1133 /* Update the stored security counter with the new image's security counter
1134 * value. Both slots hold the new image at this point, but the secondary
1135 * slot's image header must be passed since the image headers in the
1136 * boot_data structure have not been updated yet.
1137 */
1138 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1139 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1140 if (rc != 0) {
1141 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1142 return rc;
1143 }
1144#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1145
Fabio Utzig13d9e352017-10-05 20:32:31 -03001146 /*
1147 * Erases header and trailer. The trailer is erased because when a new
1148 * image is written without a trailer as is the case when using newt, the
1149 * trailer that was left might trigger a new upgrade.
1150 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001151 BOOT_LOG_DBG("erasing secondary header");
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, 0),
1154 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001155 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001156 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001157 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001158 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001159 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1160 last_sector),
1161 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1162 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001163 assert(rc == 0);
1164
David Vincze2d736ad2019-02-18 11:50:22 +01001165 flash_area_close(fap_primary_slot);
1166 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001167
David Vincze2d736ad2019-02-18 11:50:22 +01001168 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001169
1170 return 0;
1171}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001172#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001173
Christopher Collinsa1c12042019-05-23 14:00:28 -07001174#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001175/**
1176 * Swaps the two images in flash. If a prior copy operation was interrupted
1177 * by a system reset, this function completes that operation.
1178 *
1179 * @param bs The current boot status. This function reads
1180 * this struct to determine if it is resuming
1181 * an interrupted swap operation. This
1182 * function writes the updated status to this
1183 * function on return.
1184 *
1185 * @return 0 on success; nonzero on failure.
1186 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001187static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001188boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001189{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001190 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001191#ifdef MCUBOOT_ENC_IMAGES
1192 const struct flash_area *fap;
1193 uint8_t slot;
1194 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001195#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001196 uint32_t size;
1197 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001198 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001199 int rc;
1200
1201 /* FIXME: just do this if asked by user? */
1202
1203 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001204 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001205
Fabio Utzig12d59162019-11-28 10:01:59 -03001206 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001207 /*
1208 * No swap ever happened, so need to find the largest image which
1209 * will be used to determine the amount of sectors to swap.
1210 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001211 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001212 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001213 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001214 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001215 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001216
Fabio Utzigba829042018-09-18 08:29:34 -03001217#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001218 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001219 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001220 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001221 assert(rc >= 0);
1222
1223 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001224 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001225 assert(rc == 0);
1226 } else {
1227 rc = 0;
1228 }
1229 } else {
1230 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1231 }
1232#endif
1233
Fabio Utzig10ee6482019-08-01 12:04:52 -03001234 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001235 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001236 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001237 assert(rc == 0);
1238 }
1239
Fabio Utzigba829042018-09-18 08:29:34 -03001240#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001241 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001242 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001243 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001244 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001245 assert(rc >= 0);
1246
1247 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001248 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001249 assert(rc == 0);
1250 } else {
1251 rc = 0;
1252 }
1253 } else {
1254 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1255 }
1256#endif
1257
Fabio Utzig46490722017-09-04 15:34:32 -03001258 if (size > copy_size) {
1259 copy_size = size;
1260 }
1261
1262 bs->swap_size = copy_size;
1263 } else {
1264 /*
1265 * If a swap was under way, the swap_size should already be present
1266 * in the trailer...
1267 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001268 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001269 assert(rc == 0);
1270
1271 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001272
1273#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001274 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1275 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001276 assert(rc == 0);
1277
1278 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001279 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001280 break;
1281 }
1282 }
1283
1284 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001285 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001286 }
1287 }
1288#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001289 }
1290
Fabio Utzig12d59162019-11-28 10:01:59 -03001291 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001292
David Vincze2d736ad2019-02-18 11:50:22 +01001293#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001294 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001295 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001296 BOOT_LOG_WRN("%d status write fails performing the swap",
1297 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001298 }
1299#endif
1300
Christopher Collins92ea77f2016-12-12 15:59:26 -08001301 return 0;
1302}
David Brown17609d82017-05-05 09:41:34 -06001303#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001304
David Vinczee32483f2019-06-13 10:46:24 +02001305#if (BOOT_IMAGE_NUMBER > 1)
1306/**
1307 * Check the image dependency whether it is satisfied and modify
1308 * the swap type if necessary.
1309 *
1310 * @param dep Image dependency which has to be verified.
1311 *
1312 * @return 0 on success; nonzero on failure.
1313 */
1314static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001315boot_verify_slot_dependency(struct boot_loader_state *state,
1316 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001317{
1318 struct image_version *dep_version;
1319 size_t dep_slot;
1320 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001321 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001322
1323 /* Determine the source of the image which is the subject of
1324 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001325 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001326 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1327 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001328 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001329
David Vincze8b0b6372020-05-20 19:54:44 +02001330 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1331 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001332 /* Dependency not satisfied.
1333 * Modify the swap type to decrease the version number of the image
1334 * (which will be located in the primary slot after the boot process),
1335 * consequently the number of unsatisfied dependencies will be
1336 * decreased or remain the same.
1337 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001338 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001339 case BOOT_SWAP_TYPE_TEST:
1340 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001341 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001342 break;
1343 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001344 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001345 break;
1346 default:
1347 break;
1348 }
David Vincze8b0b6372020-05-20 19:54:44 +02001349 } else {
1350 /* Dependency satisfied. */
1351 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001352 }
1353
1354 return rc;
1355}
1356
1357/**
1358 * Read all dependency TLVs of an image from the flash and verify
1359 * one after another to see if they are all satisfied.
1360 *
1361 * @param slot Image slot number.
1362 *
1363 * @return 0 on success; nonzero on failure.
1364 */
1365static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001366boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001367{
1368 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001369 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001370 struct image_dependency dep;
1371 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001372 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001373 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001374 int rc;
1375
Fabio Utzig10ee6482019-08-01 12:04:52 -03001376 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001377 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001378 if (rc != 0) {
1379 rc = BOOT_EFLASH;
1380 goto done;
1381 }
1382
Fabio Utzig61fd8882019-09-14 20:00:20 -03001383 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1384 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001385 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001386 goto done;
1387 }
1388
Fabio Utzig61fd8882019-09-14 20:00:20 -03001389 while (true) {
1390 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1391 if (rc < 0) {
1392 return -1;
1393 } else if (rc > 0) {
1394 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001395 break;
1396 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001397
1398 if (len != sizeof(dep)) {
1399 rc = BOOT_EBADIMAGE;
1400 goto done;
1401 }
1402
1403 rc = flash_area_read(fap, off, &dep, len);
1404 if (rc != 0) {
1405 rc = BOOT_EFLASH;
1406 goto done;
1407 }
1408
1409 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1410 rc = BOOT_EBADARGS;
1411 goto done;
1412 }
1413
1414 /* Verify dependency and modify the swap type if not satisfied. */
1415 rc = boot_verify_slot_dependency(state, &dep);
1416 if (rc != 0) {
1417 /* Dependency not satisfied. */
1418 goto done;
1419 }
David Vinczee32483f2019-06-13 10:46:24 +02001420 }
1421
1422done:
1423 flash_area_close(fap);
1424 return rc;
1425}
1426
1427/**
David Vinczee32483f2019-06-13 10:46:24 +02001428 * Iterate over all the images and verify whether the image dependencies in the
1429 * TLV area are all satisfied and update the related swap type if necessary.
1430 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001431static int
1432boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001433{
Erik Johnson49063752020-02-06 09:59:31 -06001434 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001435 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001436
Fabio Utzig10ee6482019-08-01 12:04:52 -03001437 BOOT_CURR_IMG(state) = 0;
1438 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001439 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1440 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1441 slot = BOOT_SECONDARY_SLOT;
1442 } else {
1443 slot = BOOT_PRIMARY_SLOT;
1444 }
1445
1446 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001447 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001448 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001449 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001450 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001451 /* Cannot upgrade due to non-met dependencies, so disable all
1452 * image upgrades.
1453 */
1454 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1455 BOOT_CURR_IMG(state) = idx;
1456 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1457 }
1458 break;
David Vinczee32483f2019-06-13 10:46:24 +02001459 }
1460 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001461 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001462}
1463#endif /* (BOOT_IMAGE_NUMBER > 1) */
1464
Christopher Collins92ea77f2016-12-12 15:59:26 -08001465/**
David Vinczeba3bd602019-06-17 16:01:43 +02001466 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001467 *
David Vinczeba3bd602019-06-17 16:01:43 +02001468 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001469 *
1470 * @return 0 on success; nonzero on failure.
1471 */
1472static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001473boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001474{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001475 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001476#ifndef MCUBOOT_OVERWRITE_ONLY
1477 uint8_t swap_type;
1478#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001479
David Vinczeba3bd602019-06-17 16:01:43 +02001480 /* At this point there are no aborted swaps. */
1481#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001482 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001483#elif defined(MCUBOOT_BOOTSTRAP)
1484 /* Check if the image update was triggered by a bad image in the
1485 * primary slot (the validity of the image in the secondary slot had
1486 * already been checked).
1487 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001488 fih_int fih_rc = FIH_FAILURE;
1489 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1490 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1491 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001492 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001493 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001494 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001495 }
1496#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001497 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001498#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001499 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001500
1501#ifndef MCUBOOT_OVERWRITE_ONLY
1502 /* The following state needs image_ok be explicitly set after the
1503 * swap was finished to avoid a new revert.
1504 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001505 swap_type = BOOT_SWAP_TYPE(state);
1506 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1507 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001508 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001509 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001510 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001511 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001512 }
1513
David Vinczec3084132020-02-18 14:50:47 +01001514#ifdef MCUBOOT_HW_ROLLBACK_PROT
1515 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1516 /* Update the stored security counter with the new image's security
1517 * counter value. The primary slot holds the new image at this point,
1518 * but the secondary slot's image header must be passed since image
1519 * headers in the boot_data structure have not been updated yet.
1520 *
1521 * In case of a permanent image swap mcuboot will never attempt to
1522 * revert the images on the next reboot. Therefore, the security
1523 * counter must be increased right after the image upgrade.
1524 */
1525 rc = boot_update_security_counter(
1526 BOOT_CURR_IMG(state),
1527 BOOT_PRIMARY_SLOT,
1528 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1529 if (rc != 0) {
1530 BOOT_LOG_ERR("Security counter update failed after "
1531 "image upgrade.");
1532 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1533 }
1534 }
1535#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1536
Fabio Utzige575b0b2019-09-11 12:34:23 -03001537 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001538 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001539 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001540 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001541 }
David Vinczeba3bd602019-06-17 16:01:43 +02001542 }
1543#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001544
David Vinczeba3bd602019-06-17 16:01:43 +02001545 return rc;
1546}
1547
1548/**
1549 * Completes a previously aborted image swap.
1550 *
1551 * @param bs The current boot status.
1552 *
1553 * @return 0 on success; nonzero on failure.
1554 */
1555#if !defined(MCUBOOT_OVERWRITE_ONLY)
1556static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001557boot_complete_partial_swap(struct boot_loader_state *state,
1558 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001559{
1560 int rc;
1561
1562 /* Determine the type of swap operation being resumed from the
1563 * `swap-type` trailer field.
1564 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001565 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001566 assert(rc == 0);
1567
Fabio Utzig10ee6482019-08-01 12:04:52 -03001568 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001569
1570 /* The following states need image_ok be explicitly set after the
1571 * swap was finished to avoid a new revert.
1572 */
1573 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1574 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001575 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001576 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001577 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001578 }
1579 }
1580
Fabio Utzige575b0b2019-09-11 12:34:23 -03001581 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001582 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001583 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001584 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001585 }
1586 }
1587
Fabio Utzig10ee6482019-08-01 12:04:52 -03001588 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001589 BOOT_LOG_ERR("panic!");
1590 assert(0);
1591
1592 /* Loop forever... */
1593 while (1) {}
1594 }
1595
1596 return rc;
1597}
1598#endif /* !MCUBOOT_OVERWRITE_ONLY */
1599
1600#if (BOOT_IMAGE_NUMBER > 1)
1601/**
1602 * Review the validity of previously determined swap types of other images.
1603 *
1604 * @param aborted_swap The current image upgrade is a
1605 * partial/aborted swap.
1606 */
1607static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001608boot_review_image_swap_types(struct boot_loader_state *state,
1609 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001610{
1611 /* In that case if we rebooted in the middle of an image upgrade process, we
1612 * must review the validity of swap types, that were previously determined
1613 * for other images. The image_ok flag had not been set before the reboot
1614 * for any of the updated images (only the copy_done flag) and thus falsely
1615 * the REVERT swap type has been determined for the previous images that had
1616 * been updated before the reboot.
1617 *
1618 * There are two separate scenarios that we have to deal with:
1619 *
1620 * 1. The reboot has happened during swapping an image:
1621 * The current image upgrade has been determined as a
1622 * partial/aborted swap.
1623 * 2. The reboot has happened between two separate image upgrades:
1624 * In this scenario we must check the swap type of the current image.
1625 * In those cases if it is NONE or REVERT we cannot certainly determine
1626 * the fact of a reboot. In a consistent state images must move in the
1627 * same direction or stay in place, e.g. in practice REVERT and TEST
1628 * swap types cannot be present at the same time. If the swap type of
1629 * the current image is either TEST, PERM or FAIL we must review the
1630 * already determined swap types of other images and set each false
1631 * REVERT swap types to NONE (these images had been successfully
1632 * updated before the system rebooted between two separate image
1633 * upgrades).
1634 */
1635
Fabio Utzig10ee6482019-08-01 12:04:52 -03001636 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001637 /* Nothing to do */
1638 return;
1639 }
1640
1641 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001642 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1643 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001644 /* Nothing to do */
1645 return;
1646 }
1647 }
1648
Fabio Utzig10ee6482019-08-01 12:04:52 -03001649 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1650 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1651 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001652 }
1653 }
1654}
1655#endif
1656
1657/**
1658 * Prepare image to be updated if required.
1659 *
1660 * Prepare image to be updated if required with completing an image swap
1661 * operation if one was aborted and/or determining the type of the
1662 * swap operation. In case of any error set the swap type to NONE.
1663 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001664 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001665 * @param bs Pointer where the read and possibly updated
1666 * boot status can be written to.
1667 */
1668static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001669boot_prepare_image_for_update(struct boot_loader_state *state,
1670 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001671{
1672 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001673 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001674
1675 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001676 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001677 if (rc != 0) {
1678 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1679 " - too small?", BOOT_MAX_IMG_SECTORS);
1680 /* Unable to determine sector layout, continue with next image
1681 * if there is one.
1682 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001683 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001684 return;
1685 }
1686
1687 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001688 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001689 if (rc != 0) {
1690 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001691 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001692 BOOT_CURR_IMG(state));
1693 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001694 return;
1695 }
1696
1697 /* If the current image's slots aren't compatible, no swap is possible.
1698 * Just boot into primary slot.
1699 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001700 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001701 boot_status_reset(bs);
1702
1703#ifndef MCUBOOT_OVERWRITE_ONLY
1704 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001705 if (rc != 0) {
1706 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001707 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001708 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001709 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001710 return;
1711 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001712#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001713
Fabio Utzig74aef312019-11-28 11:05:34 -03001714#ifdef MCUBOOT_SWAP_USING_MOVE
1715 /*
1716 * Must re-read image headers because the boot status might
1717 * have been updated in the previous function call.
1718 */
1719 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001720#ifdef MCUBOOT_BOOTSTRAP
1721 /* When bootstrapping it's OK to not have image magic in the primary slot */
1722 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1723 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1724#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001725 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001726#endif
1727
Fabio Utzig74aef312019-11-28 11:05:34 -03001728 /* Continue with next image if there is one. */
1729 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1730 BOOT_CURR_IMG(state));
1731 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1732 return;
1733 }
1734#endif
1735
David Vinczeba3bd602019-06-17 16:01:43 +02001736 /* Determine if we rebooted in the middle of an image swap
1737 * operation. If a partial swap was detected, complete it.
1738 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001739 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001740
1741#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001742 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001743#endif
1744
1745#ifdef MCUBOOT_OVERWRITE_ONLY
1746 /* Should never arrive here, overwrite-only mode has
1747 * no swap state.
1748 */
1749 assert(0);
1750#else
1751 /* Determine the type of swap operation being resumed from the
1752 * `swap-type` trailer field.
1753 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001754 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001755 assert(rc == 0);
1756#endif
1757 /* Attempt to read an image header from each slot. Ensure that
1758 * image headers in slots are aligned with headers in boot_data.
1759 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001760 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001761 assert(rc == 0);
1762
1763 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001764 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001765 } else {
1766 /* There was no partial swap, determine swap type. */
1767 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001768 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001769 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001770 FIH_CALL(boot_validate_slot, fih_rc,
1771 state, BOOT_SECONDARY_SLOT, bs);
1772 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1773 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1774 } else {
1775 BOOT_SWAP_TYPE(state) = bs->swap_type;
1776 }
David Vinczeba3bd602019-06-17 16:01:43 +02001777 }
1778
1779#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001780 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001781#endif
1782
1783#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001784 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001785 /* Header checks are done first because they are
1786 * inexpensive. Since overwrite-only copies starting from
1787 * offset 0, if interrupted, it might leave a valid header
1788 * magic, so also run validation on the primary slot to be
1789 * sure it's not OK.
1790 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001791 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1792 FIH_CALL(boot_validate_slot, fih_rc,
1793 state, BOOT_PRIMARY_SLOT, bs);
1794
1795 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1796
Fabio Utzig3d77c952020-10-04 10:23:17 -03001797 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001798 FIH_CALL(boot_validate_slot, fih_rc,
1799 state, BOOT_SECONDARY_SLOT, bs);
1800
Fabio Utzig3d77c952020-10-04 10:23:17 -03001801 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001802 /* Set swap type to REVERT to overwrite the primary
1803 * slot with the image contained in secondary slot
1804 * and to trigger the explicit setting of the
1805 * image_ok flag.
1806 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001807 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001808 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001809 }
1810 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001811#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001812 }
David Vinczeba3bd602019-06-17 16:01:43 +02001813 } else {
1814 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001815 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001816 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001817}
1818
Mark Horvathccaf7f82021-01-04 18:16:42 +01001819/**
1820 * Updates the security counter for the current image.
1821 *
1822 * @param state Boot loader status information.
1823 *
1824 * @return 0 on success; nonzero on failure.
1825 */
1826static int
1827boot_update_hw_rollback_protection(struct boot_loader_state *state)
1828{
1829#ifdef MCUBOOT_HW_ROLLBACK_PROT
1830 int rc;
1831
1832 /* Update the stored security counter with the active image's security
1833 * counter value. It will only be updated if the new security counter is
1834 * greater than the stored value.
1835 *
1836 * In case of a successful image swapping when the swap type is TEST the
1837 * security counter can be increased only after a reset, when the swap
1838 * type is NONE and the image has marked itself "OK" (the image_ok flag
1839 * has been set). This way a "revert" can be performed when it's
1840 * necessary.
1841 */
1842 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1843 rc = boot_update_security_counter(
1844 BOOT_CURR_IMG(state),
1845 BOOT_PRIMARY_SLOT,
1846 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1847 if (rc != 0) {
1848 BOOT_LOG_ERR("Security counter update failed after image "
1849 "validation.");
1850 return rc;
1851 }
1852 }
1853
1854 return 0;
1855
1856#else /* MCUBOOT_HW_ROLLBACK_PROT */
1857 (void) (state);
1858
1859 return 0;
1860#endif
1861}
1862
Raef Colese8fe6cf2020-05-26 13:07:40 +01001863fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001864context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001865{
Marti Bolivar84898652017-06-13 17:20:22 -04001866 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001867 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001868 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001869 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001870 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001871 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001872 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001873
1874 /* The array of slot sectors are defined here (as opposed to file scope) so
1875 * that they don't get allocated for non-boot-loader apps. This is
1876 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001877 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001878 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001879 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1880 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001881#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001882 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001883#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001884
Fabio Utzig10ee6482019-08-01 12:04:52 -03001885 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001886 has_upgrade = false;
1887
1888#if (BOOT_IMAGE_NUMBER == 1)
1889 (void)has_upgrade;
1890#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001891
David Vinczeba3bd602019-06-17 16:01:43 +02001892 /* Iterate over all the images. By the end of the loop the swap type has
1893 * to be determined for each image and all aborted swaps have to be
1894 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001895 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001896 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001897
David Vinczeba3bd602019-06-17 16:01:43 +02001898#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1899 /* The keys used for encryption may no longer be valid (could belong to
1900 * another images). Therefore, mark them as invalid to force their reload
1901 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001902 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001903 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001904#endif
1905
Fabio Utzig10ee6482019-08-01 12:04:52 -03001906 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001907
Fabio Utzig10ee6482019-08-01 12:04:52 -03001908 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001909 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001910 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001911 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001912#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001913 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001914#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001915
1916 /* Open primary and secondary image areas for the duration
1917 * of this call.
1918 */
1919 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001920 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001921 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001922 assert(rc == 0);
1923 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001924#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001925 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001926 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001927 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001928#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001929
1930 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001931 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001932
Fabio Utzige575b0b2019-09-11 12:34:23 -03001933 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001934 has_upgrade = true;
1935 }
David Vinczeba3bd602019-06-17 16:01:43 +02001936 }
1937
David Vinczee32483f2019-06-13 10:46:24 +02001938#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001939 if (has_upgrade) {
1940 /* Iterate over all the images and verify whether the image dependencies
1941 * are all satisfied and update swap type if necessary.
1942 */
1943 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001944 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001945 /*
1946 * It was impossible to upgrade because the expected dependency version
1947 * was not available. Here we already changed the swap_type so that
1948 * instead of asserting the bootloader, we continue and no upgrade is
1949 * performed.
1950 */
1951 rc = 0;
1952 }
1953 }
David Vinczee32483f2019-06-13 10:46:24 +02001954#endif
1955
David Vinczeba3bd602019-06-17 16:01:43 +02001956 /* Iterate over all the images. At this point there are no aborted swaps
1957 * and the swap types are determined for each image. By the end of the loop
1958 * all required update operations will have been finished.
1959 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001960 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001961
1962#if (BOOT_IMAGE_NUMBER > 1)
1963#ifdef MCUBOOT_ENC_IMAGES
1964 /* The keys used for encryption may no longer be valid (could belong to
1965 * another images). Therefore, mark them as invalid to force their reload
1966 * by boot_enc_load().
1967 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001968 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001969#endif /* MCUBOOT_ENC_IMAGES */
1970
1971 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001972 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001973#endif /* (BOOT_IMAGE_NUMBER > 1) */
1974
1975 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001976 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001977
Fabio Utzig10ee6482019-08-01 12:04:52 -03001978 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001979 case BOOT_SWAP_TYPE_NONE:
1980 break;
1981
1982 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1983 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1984 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001985 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001986 assert(rc == 0);
1987 break;
1988
1989 case BOOT_SWAP_TYPE_FAIL:
1990 /* The image in secondary slot was invalid and is now erased. Ensure
1991 * we don't try to boot into it again on the next reboot. Do this by
1992 * pretending we just reverted back to primary slot.
1993 */
1994#ifndef MCUBOOT_OVERWRITE_ONLY
1995 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001996 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001997 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001998 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001999 }
2000#endif /* !MCUBOOT_OVERWRITE_ONLY */
2001 break;
2002
2003 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002004 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002005 }
2006
Fabio Utzig10ee6482019-08-01 12:04:52 -03002007 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002008 BOOT_LOG_ERR("panic!");
2009 assert(0);
2010
2011 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002012 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002013 }
2014 }
2015
2016 /* Iterate over all the images. At this point all required update operations
2017 * have finished. By the end of the loop each image in the primary slot will
2018 * have been re-validated.
2019 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002020 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2021 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002022 /* Attempt to read an image header from each slot. Ensure that image
2023 * headers in slots are aligned with headers in boot_data.
2024 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002025 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002026 if (rc != 0) {
2027 goto out;
2028 }
2029 /* Since headers were reloaded, it can be assumed we just performed
2030 * a swap or overwrite. Now the header info that should be used to
2031 * provide the data for the bootstrap, which previously was at
2032 * secondary slot, was updated to primary slot.
2033 */
2034 }
2035
2036#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002037 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2038 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002039 goto out;
2040 }
2041#else
2042 /* Even if we're not re-validating the primary slot, we could be booting
2043 * onto an empty flash chip. At least do a basic sanity check that
2044 * the magic number on the image is OK.
2045 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002046 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002047 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002048 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2049 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002050 rc = BOOT_EBADIMAGE;
2051 goto out;
2052 }
David Vinczec3084132020-02-18 14:50:47 +01002053#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2054
Mark Horvathccaf7f82021-01-04 18:16:42 +01002055 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002056 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002057 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002058 }
David Vincze1cf11b52020-03-24 07:51:09 +01002059
Mark Horvathccaf7f82021-01-04 18:16:42 +01002060 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002061 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002062 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002063 }
David Vinczeba3bd602019-06-17 16:01:43 +02002064 }
2065
Fabio Utzigf616c542019-12-19 15:23:32 -03002066 /*
2067 * Since the boot_status struct stores plaintext encryption keys, reset
2068 * them here to avoid the possibility of jumping into an image that could
2069 * easily recover them.
2070 */
2071 memset(&bs, 0, sizeof(struct boot_status));
2072
Mark Horvathccaf7f82021-01-04 18:16:42 +01002073 fill_rsp(state, NULL, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002074
Raef Colese8fe6cf2020-05-26 13:07:40 +01002075 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002076out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002077 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002078
2079 if (rc) {
2080 fih_rc = fih_int_encode(rc);
2081 }
2082
2083 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002084}
2085
Raef Colese8fe6cf2020-05-26 13:07:40 +01002086fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002087split_go(int loader_slot, int split_slot, void **entry)
2088{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002089 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002090 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002091 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002092 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002093 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002094 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002095
Christopher Collins92ea77f2016-12-12 15:59:26 -08002096 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2097 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002098 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002099 }
David Vinczeba3bd602019-06-17 16:01:43 +02002100 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2101 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002102
2103 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2104 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002105 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002106 assert(rc == 0);
2107 split_flash_id = flash_area_id_from_image_slot(split_slot);
2108 rc = flash_area_open(split_flash_id,
2109 &BOOT_IMG_AREA(&boot_data, split_slot));
2110 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002111
2112 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002113 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002114 if (rc != 0) {
2115 rc = SPLIT_GO_ERR;
2116 goto done;
2117 }
2118
Fabio Utzig12d59162019-11-28 10:01:59 -03002119 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002120 if (rc != 0) {
2121 goto done;
2122 }
2123
Christopher Collins92ea77f2016-12-12 15:59:26 -08002124 /* Don't check the bootable image flag because we could really call a
2125 * bootable or non-bootable image. Just validate that the image check
2126 * passes which is distinct from the normal check.
2127 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002128 FIH_CALL(split_image_check, fih_rc,
2129 boot_img_hdr(&boot_data, split_slot),
2130 BOOT_IMG_AREA(&boot_data, split_slot),
2131 boot_img_hdr(&boot_data, loader_slot),
2132 BOOT_IMG_AREA(&boot_data, loader_slot));
2133 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002134 goto done;
2135 }
2136
Marti Bolivarea088872017-06-12 17:10:49 -04002137 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002138 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002139 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002140 rc = SPLIT_GO_OK;
2141
2142done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002143 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2144 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002145 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002146
2147 if (rc) {
2148 fih_rc = fih_int_encode(rc);
2149 }
2150
2151 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002152}
David Vinczee574f2d2020-07-10 11:42:03 +02002153
Tamas Banfe031092020-09-10 17:32:39 +02002154#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002155
Mark Horvathccaf7f82021-01-04 18:16:42 +01002156#define NO_ACTIVE_SLOT UINT32_MAX
2157
David Vinczee574f2d2020-07-10 11:42:03 +02002158/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002159 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002160 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002161 * @param state Boot loader status information.
2162 * @param slot_usage Structure to fill with information about the available
2163 * slots.
David Vinczee574f2d2020-07-10 11:42:03 +02002164 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002165 * @return 0 on success; nonzero on failure.
2166 */
2167static int
2168boot_get_slot_usage(struct boot_loader_state *state,
2169 struct slot_usage_t slot_usage[])
2170{
2171 uint32_t slot;
2172 int fa_id;
2173 int rc;
2174 struct image_header *hdr = NULL;
2175
2176 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2177 /* Open all the slots */
2178 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2179 fa_id = flash_area_id_from_multi_image_slot(
2180 BOOT_CURR_IMG(state), slot);
2181 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2182 assert(rc == 0);
2183 }
2184
2185 /* Attempt to read an image header from each slot. */
2186 rc = boot_read_image_headers(state, false, NULL);
2187 if (rc != 0) {
2188 BOOT_LOG_WRN("Failed reading image headers.");
2189 return rc;
2190 }
2191
2192 /* Check headers in all slots */
2193 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2194 hdr = boot_img_hdr(state, slot);
2195
2196 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2197 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
2198 BOOT_LOG_IMAGE_INFO(slot, hdr);
2199 } else {
2200 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
2201 BOOT_LOG_INF("Image %d %s slot: Image not found",
2202 BOOT_CURR_IMG(state),
2203 (slot == BOOT_PRIMARY_SLOT)
2204 ? "Primary" : "Secondary");
2205 }
2206 }
2207
2208 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2209 }
2210
2211 return 0;
2212}
2213
2214/**
2215 * Finds the slot containing the image with the highest version number for the
2216 * current image.
2217 *
2218 * @param state Boot loader status information.
2219 * @param slot_usage Information about the active and available slots.
2220 *
2221 * @return NO_ACTIVE_SLOT if no available slot found, number of
2222 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002223 */
2224static uint32_t
Mark Horvathccaf7f82021-01-04 18:16:42 +01002225find_slot_with_highest_version(struct boot_loader_state *state,
2226 struct slot_usage_t slot_usage[])
David Vinczee574f2d2020-07-10 11:42:03 +02002227{
David Vinczee574f2d2020-07-10 11:42:03 +02002228 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002229 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2230 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002231
Mark Horvathccaf7f82021-01-04 18:16:42 +01002232 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2233 if (slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
2234 if (candidate_slot == NO_ACTIVE_SLOT) {
2235 candidate_slot = slot;
2236 } else {
2237 rc = boot_version_cmp(
2238 &boot_img_hdr(state, slot)->ih_ver,
2239 &boot_img_hdr(state, candidate_slot)->ih_ver);
2240 if (rc == 1) {
2241 /* The version of the image being examined is greater than
2242 * the version of the current candidate.
2243 */
2244 candidate_slot = slot;
2245 }
2246 }
David Vinczee574f2d2020-07-10 11:42:03 +02002247 }
2248 }
2249
Mark Horvathccaf7f82021-01-04 18:16:42 +01002250 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002251}
2252
Mark Horvathccaf7f82021-01-04 18:16:42 +01002253#ifdef MCUBOOT_HAVE_LOGGING
2254/**
2255 * Prints the state of the loaded images.
2256 *
2257 * @param state Boot loader status information.
2258 * @param slot_usage Information about the active and available slots.
2259 */
2260static void
2261print_loaded_images(struct boot_loader_state *state,
2262 struct slot_usage_t slot_usage[])
2263{
2264 uint32_t active_slot;
2265
2266 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2267 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2268
2269 BOOT_LOG_INF("Image %d loaded from the %s slot",
2270 BOOT_CURR_IMG(state),
2271 (active_slot == BOOT_PRIMARY_SLOT) ?
2272 "primary" : "secondary");
2273 }
2274}
2275#endif
2276
David Vincze505fba22020-10-22 13:53:29 +02002277#ifdef MCUBOOT_DIRECT_XIP_REVERT
2278/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002279 * Checks whether the active slot of the current image was previously selected
2280 * to run. Erases the image if it was selected but its execution failed,
2281 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002282 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002283 * @param state Boot loader status information.
2284 * @param slot_usage Information about the active and available slots.
David Vincze505fba22020-10-22 13:53:29 +02002285 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002286 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002287 */
2288static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002289boot_select_or_erase(struct boot_loader_state *state,
2290 struct slot_usage_t slot_usage[])
David Vincze505fba22020-10-22 13:53:29 +02002291{
2292 const struct flash_area *fap;
2293 int fa_id;
2294 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002295 uint32_t active_slot;
2296 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002297
Mark Horvathccaf7f82021-01-04 18:16:42 +01002298 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2299
2300 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002301 rc = flash_area_open(fa_id, &fap);
2302 assert(rc == 0);
2303
Mark Horvathccaf7f82021-01-04 18:16:42 +01002304 active_swap_state = &(slot_usage[BOOT_CURR_IMG(state)].swap_state);
2305
2306 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2307 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002308 assert(rc == 0);
2309
Mark Horvathccaf7f82021-01-04 18:16:42 +01002310 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2311 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2312 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002313 /*
2314 * A reboot happened without the image being confirmed at
2315 * runtime or its trailer is corrupted/invalid. Erase the image
2316 * to prevent it from being selected again on the next reboot.
2317 */
2318 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
2319 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2320 rc = flash_area_erase(fap, 0, fap->fa_size);
2321 assert(rc == 0);
2322
2323 flash_area_close(fap);
2324 rc = -1;
2325 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002326 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2327 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002328 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2329 "value was neither 'set' nor 'unset', but 'bad'.");
2330 }
2331 /*
2332 * Set the copy_done flag, indicating that the image has been
2333 * selected to boot. It can be set in advance, before even
2334 * validating the image, because in case the validation fails, the
2335 * entire image slot will be erased (including the trailer).
2336 */
2337 rc = boot_write_copy_done(fap);
2338 if (rc != 0) {
2339 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
2340 "the %s slot.", (slot == BOOT_PRIMARY_SLOT) ?
2341 "primary" : "secondary");
2342 rc = 0;
2343 }
2344 }
2345 flash_area_close(fap);
2346 }
2347
2348 return rc;
2349}
2350#endif /* MCUBOOT_DIRECT_XIP_REVERT */
2351
Tamas Banfe031092020-09-10 17:32:39 +02002352#ifdef MCUBOOT_RAM_LOAD
2353
Mark Horvathccaf7f82021-01-04 18:16:42 +01002354#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002355#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2356#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2357#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002358#endif
Tamas Banfe031092020-09-10 17:32:39 +02002359
2360/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002361 * Verifies that the active slot of the current image can be loaded within the
2362 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002363 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002364 * @param state Boot loader status information.
2365 * @param slot_usage Information about the active and available slots.
Tamas Banfe031092020-09-10 17:32:39 +02002366 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002367 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002368 */
2369static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002370boot_verify_ram_load_address(struct boot_loader_state *state,
2371 struct slot_usage_t slot_usage[])
Tamas Banfe031092020-09-10 17:32:39 +02002372{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002373 uint32_t img_dst;
2374 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002375 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002376 uint32_t exec_ram_start;
2377 uint32_t exec_ram_size;
2378#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2379 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002380
Mark Horvathccaf7f82021-01-04 18:16:42 +01002381 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2382 &exec_ram_size);
2383 if (rc != 0) {
2384 return BOOT_EBADSTATUS;
2385 }
2386#else
2387 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2388 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2389#endif
2390
2391 img_dst = slot_usage[BOOT_CURR_IMG(state)].img_dst;
2392 img_sz = slot_usage[BOOT_CURR_IMG(state)].img_sz;
2393
2394 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002395 return BOOT_EBADIMAGE;
2396 }
2397
2398 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2399 return BOOT_EBADIMAGE;
2400 }
2401
Mark Horvathccaf7f82021-01-04 18:16:42 +01002402 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002403 return BOOT_EBADIMAGE;
2404 }
2405
2406 return 0;
2407}
2408
2409/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002410 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002411 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002412 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002413 * @param slot The flash slot of the image to be copied to SRAM.
2414 * @param img_dst The address at which the image needs to be copied to
2415 * SRAM.
2416 * @param img_sz The size of the image that needs to be copied to SRAM.
2417 *
2418 * @return 0 on success; nonzero on failure.
2419 */
2420static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002421boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2422 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002423{
2424 int rc;
2425 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002426 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002427
Mark Horvathccaf7f82021-01-04 18:16:42 +01002428#if (BOOT_IMAGE_NUMBER == 1)
2429 (void)state;
2430#endif
2431
2432 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2433
2434 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002435 if (rc != 0) {
2436 return BOOT_EFLASH;
2437 }
2438
2439 /* Direct copy from flash to its new location in SRAM. */
2440 rc = flash_area_read(fap_src, 0, (void *)img_dst, img_sz);
2441 if (rc != 0) {
2442 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2443 }
2444
2445 flash_area_close(fap_src);
2446
2447 return rc;
2448}
2449
Mark Horvathccaf7f82021-01-04 18:16:42 +01002450#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002451/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002452 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002453 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002454 * @param start_a Start of the A region.
2455 * @param end_a End of the A region.
2456 * @param start_b Start of the B region.
2457 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002458 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002459 * @return true if there is overlap; false otherwise.
2460 */
2461static bool
2462do_regions_overlap(uint32_t start_a, uint32_t end_a,
2463 uint32_t start_b, uint32_t end_b)
2464{
2465 if (start_b > end_a) {
2466 return false;
2467 } else if (start_b >= start_a) {
2468 return true;
2469 } else if (end_b > start_a) {
2470 return true;
2471 }
2472
2473 return false;
2474}
2475
2476/**
2477 * Checks if the image we want to load to memory overlap with an already
2478 * ramloaded image.
2479 *
2480 * @param slot_usage Information about the active and available slots.
2481 * @param image_id_to_check The ID of the image we would like to load.
2482 *
2483 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002484 */
2485static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002486boot_check_ram_load_overlapping(struct slot_usage_t slot_usage[],
2487 uint32_t image_id_to_check)
Tamas Banfe031092020-09-10 17:32:39 +02002488{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002489 uint32_t i;
2490
2491 uint32_t start_a;
2492 uint32_t end_a;
2493 uint32_t start_b;
2494 uint32_t end_b;
2495
2496 start_a = slot_usage[image_id_to_check].img_dst;
2497 /* Safe to add here, values are already verified in
2498 * boot_verify_ram_load_address() */
2499 end_a = start_a + slot_usage[image_id_to_check].img_sz;
2500
2501 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
2502 if (slot_usage[i].active_slot == NO_ACTIVE_SLOT
2503 || i == image_id_to_check) {
2504 continue;
2505 }
2506
2507 start_b = slot_usage[i].img_dst;
2508 /* Safe to add here, values are already verified in
2509 * boot_verify_ram_load_address() */
2510 end_b = start_b + slot_usage[i].img_sz;
2511
2512 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2513 return -1;
2514 }
2515 }
2516
2517 return 0;
2518}
2519#endif
2520
2521/**
2522 * Loads the active slot of the current image into SRAM. The load address and
2523 * image size is extracted from the image header.
2524 *
2525 * @param state Boot loader status information.
2526 * @param slot_usage Information about the active and available slots.
2527 *
2528 * @return 0 on success; nonzero on failure.
2529 */
2530static int
2531boot_load_image_to_sram(struct boot_loader_state *state,
2532 struct slot_usage_t slot_usage[])
2533{
2534 uint32_t active_slot;
2535 struct image_header *hdr = NULL;
2536 uint32_t img_dst;
2537 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002538 int rc;
2539
Mark Horvathccaf7f82021-01-04 18:16:42 +01002540 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2541 hdr = boot_img_hdr(state, active_slot);
2542
Tamas Banfe031092020-09-10 17:32:39 +02002543 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2544
Mark Horvathccaf7f82021-01-04 18:16:42 +01002545 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002546
Mark Horvathccaf7f82021-01-04 18:16:42 +01002547 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002548 if (rc != 0) {
2549 return rc;
2550 }
2551
Mark Horvathccaf7f82021-01-04 18:16:42 +01002552 slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2553 slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
2554
2555 rc = boot_verify_ram_load_address(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002556 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002557 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002558 return rc;
2559 }
2560
Mark Horvathccaf7f82021-01-04 18:16:42 +01002561#if (BOOT_IMAGE_NUMBER > 1)
2562 rc = boot_check_ram_load_overlapping(slot_usage, BOOT_CURR_IMG(state));
2563 if (rc != 0) {
2564 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2565 another image.", img_dst);
2566 return rc;
2567 }
2568#endif
2569
Tamas Banfe031092020-09-10 17:32:39 +02002570 /* Copy image to the load address from where it currently resides in
2571 * flash.
2572 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002573 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002574 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002575 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002576 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002577 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002578 }
2579 } else {
2580 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2581 * MCUBOOT_RAM_LOAD is set.
2582 */
2583 rc = BOOT_EBADIMAGE;
2584 }
2585
Mark Horvathccaf7f82021-01-04 18:16:42 +01002586 if (rc != 0) {
2587 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2588 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2589 }
2590
Tamas Banfe031092020-09-10 17:32:39 +02002591 return rc;
2592}
2593
2594/**
2595 * Removes an image from SRAM, by overwriting it with zeros.
2596 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002597 * @param state Boot loader status information.
2598 * @param slot_usage Information about the active and available slots.
2599 *
2600 * @return 0 on success; nonzero on failure.
2601 */
2602static inline int
2603boot_remove_image_from_sram(struct boot_loader_state *state,
2604 struct slot_usage_t slot_usage[])
2605{
2606 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
2607 slot_usage[BOOT_CURR_IMG(state)].img_dst);
2608
2609 memset((void*)slot_usage[BOOT_CURR_IMG(state)].img_dst, 0,
2610 slot_usage[BOOT_CURR_IMG(state)].img_sz);
2611
2612 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2613 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2614
2615 return 0;
2616}
2617
2618/**
2619 * Removes an image from flash by erasing the corresponding flash area
2620 *
2621 * @param state Boot loader status information.
2622 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002623 *
2624 * @return 0 on success; nonzero on failure.
2625 */
2626static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002627boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002628{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002629 int area_id;
2630 int rc;
2631 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002632
Mark Horvathccaf7f82021-01-04 18:16:42 +01002633 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2634 slot);
2635 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2636 rc = flash_area_open(area_id, &fap);
2637 if (rc == 0) {
2638 flash_area_erase(fap, 0, fap->fa_size);
2639 }
2640
2641 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002642}
2643#endif /* MCUBOOT_RAM_LOAD */
2644
Mark Horvathccaf7f82021-01-04 18:16:42 +01002645#if (BOOT_IMAGE_NUMBER > 1)
2646/**
2647 * Checks the image dependency whether it is satisfied.
2648 *
2649 * @param state Boot loader status information.
2650 * @param slot_usage Information about the active and available slots.
2651 * @param dep Image dependency which has to be verified.
2652 *
2653 * @return 0 if dependencies are met; nonzero otherwise.
2654 */
2655static int
2656boot_verify_slot_dependency(struct boot_loader_state *state,
2657 struct slot_usage_t slot_usage[],
2658 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002659{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002660 struct image_version *dep_version;
2661 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002662 int rc;
2663
Mark Horvathccaf7f82021-01-04 18:16:42 +01002664 /* Determine the source of the image which is the subject of
2665 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002666 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002667 dep_slot = slot_usage[dep->image_id].active_slot;
2668 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2669
2670 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2671 if (rc >= 0) {
2672 /* Dependency satisfied. */
2673 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002674 }
2675
Mark Horvathccaf7f82021-01-04 18:16:42 +01002676 return rc;
2677}
2678
2679/**
2680 * Reads all dependency TLVs of an image and verifies one after another to see
2681 * if they are all satisfied.
2682 *
2683 * @param state Boot loader status information.
2684 * @param slot_usage Information about the active and available slots.
2685 *
2686 * @return 0 if dependencies are met; nonzero otherwise.
2687 */
2688static int
2689boot_verify_slot_dependencies(struct boot_loader_state *state,
2690 struct slot_usage_t slot_usage[])
2691{
2692 uint32_t active_slot;
2693 const struct flash_area *fap;
2694 struct image_tlv_iter it;
2695 struct image_dependency dep;
2696 uint32_t off;
2697 uint16_t len;
2698 int area_id;
2699 int rc;
2700
2701 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2702
2703 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2704 active_slot);
2705 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002706 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002707 rc = BOOT_EFLASH;
2708 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002709 }
2710
Mark Horvathccaf7f82021-01-04 18:16:42 +01002711 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2712 IMAGE_TLV_DEPENDENCY, true);
2713 if (rc != 0) {
2714 goto done;
2715 }
David Vinczee574f2d2020-07-10 11:42:03 +02002716
Mark Horvathccaf7f82021-01-04 18:16:42 +01002717 while (true) {
2718 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2719 if (rc < 0) {
2720 return -1;
2721 } else if (rc > 0) {
2722 rc = 0;
2723 break;
2724 }
David Vinczee574f2d2020-07-10 11:42:03 +02002725
Mark Horvathccaf7f82021-01-04 18:16:42 +01002726 if (len != sizeof(dep)) {
2727 rc = BOOT_EBADIMAGE;
2728 goto done;
2729 }
2730
2731 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2732 fap, off, &dep, len);
2733 if (rc != 0) {
2734 rc = BOOT_EFLASH;
2735 goto done;
2736 }
2737
2738 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2739 rc = BOOT_EBADARGS;
2740 goto done;
2741 }
2742
2743 rc = boot_verify_slot_dependency(state, slot_usage, &dep);
2744 if (rc != 0) {
2745 /* Dependency not satisfied. */
2746 goto done;
2747 }
2748 }
2749
2750done:
2751 flash_area_close(fap);
2752 return rc;
2753}
2754
2755/**
2756 * Checks the dependency of all the active slots. If an image found with
2757 * invalid or not satisfied dependencies the image is removed from SRAM (in
2758 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2759 *
2760 * @param state Boot loader status information.
2761 * @param slot_usage Information about the active and available slots.
2762 *
2763 * @return 0 if dependencies are met; nonzero otherwise.
2764 */
2765static int
2766boot_verify_dependencies(struct boot_loader_state *state,
2767 struct slot_usage_t slot_usage[])
2768{
2769 int rc = -1;
2770 uint32_t active_slot;
2771
2772 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2773 rc = boot_verify_slot_dependencies(state, slot_usage);
2774 if (rc != 0) {
2775 /* Dependencies not met or invalid dependencies. */
2776
2777#ifdef MCUBOOT_RAM_LOAD
2778 boot_remove_image_from_sram(state, slot_usage);
2779#endif /* MCUBOOT_RAM_LOAD */
2780
2781 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2782 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2783 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2784
2785 return rc;
2786 }
2787 }
2788
2789 return rc;
2790}
2791#endif /* (BOOT_IMAGE_NUMBER > 1) */
2792
2793/**
2794 * Tries to load a slot for all the images with validation.
2795 *
2796 * @param state Boot loader status information.
2797 * @param slot_usage Information about the active and available slots.
2798 *
2799 * @return 0 on success; nonzero on failure.
2800 */
2801fih_int
2802boot_load_and_validate_images(struct boot_loader_state *state,
2803 struct slot_usage_t slot_usage[])
2804{
2805 uint32_t active_slot;
2806 int rc;
2807 fih_int fih_rc;
2808
2809 /* Go over all the images and try to load one */
2810 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2811 /* All slots tried until a valid image found. Breaking from this loop
2812 * means that a valid image found or already loaded. If no slot is
2813 * found the function returns with error code. */
2814 while (true) {
2815
2816 /* Go over all the slots and try to load one */
2817 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2818 if (active_slot != NO_ACTIVE_SLOT){
2819 /* A slot is already active, go to next image. */
2820 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002821 }
David Vincze505fba22020-10-22 13:53:29 +02002822
Mark Horvathccaf7f82021-01-04 18:16:42 +01002823 active_slot = find_slot_with_highest_version(state,
2824 slot_usage);
2825 if (active_slot == NO_ACTIVE_SLOT) {
2826 BOOT_LOG_INF("No slot to load for image %d",
2827 BOOT_CURR_IMG(state));
2828 FIH_RET(FIH_FAILURE);
2829 }
2830
2831 /* Save the number of the active slot. */
2832 slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
2833
2834#ifdef MCUBOOT_DIRECT_XIP
2835 rc = boot_rom_address_check(state, slot_usage);
2836 if (rc != 0) {
2837 /* The image is placed in an unsuitable slot. */
2838 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2839 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2840 continue;
2841 }
2842#endif /* MCUBOOT_DIRECT_XIP */
George Becksteind4d90f82021-05-11 02:00:00 -04002843
David Vincze505fba22020-10-22 13:53:29 +02002844#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002845 rc = boot_select_or_erase(state, slot_usage);
David Vincze505fba22020-10-22 13:53:29 +02002846 if (rc != 0) {
2847 /* The selected image slot has been erased. */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002848 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2849 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02002850 continue;
2851 }
2852#endif /* MCUBOOT_DIRECT_XIP_REVERT */
2853
Tamas Banfe031092020-09-10 17:32:39 +02002854#ifdef MCUBOOT_RAM_LOAD
2855 /* Image is first loaded to RAM and authenticated there in order to
2856 * prevent TOCTOU attack during image copy. This could be applied
2857 * when loading images from external (untrusted) flash to internal
2858 * (trusted) RAM and image is authenticated before copying.
2859 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002860 rc = boot_load_image_to_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002861 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002862 /* Image cannot be ramloaded. */
2863 boot_remove_image_from_flash(state, active_slot);
2864 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2865 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02002866 continue;
Tamas Banfe031092020-09-10 17:32:39 +02002867 }
2868#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002869
2870 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
2871 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2872 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02002873#ifdef MCUBOOT_RAM_LOAD
Mark Horvathccaf7f82021-01-04 18:16:42 +01002874 boot_remove_image_from_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002875#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002876 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2877 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2878 continue;
David Vincze505fba22020-10-22 13:53:29 +02002879 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01002880
2881 /* Valid image loaded from a slot, go to next image. */
2882 break;
2883 }
2884 }
2885
2886 FIH_RET(FIH_SUCCESS);
2887}
2888
2889/**
2890 * Updates the security counter for the current image.
2891 *
2892 * @param state Boot loader status information.
2893 * @param active_slot Index of the slot will be loaded for current image.
2894 *
2895 * @return 0 on success; nonzero on failure.
2896 */
2897static int
2898boot_update_hw_rollback_protection(struct boot_loader_state *state,
2899 uint32_t active_slot)
2900{
2901#ifdef MCUBOOT_HW_ROLLBACK_PROT
2902 int rc;
2903
2904 /* Update the stored security counter with the newer (active) image's
2905 * security counter value.
2906 */
David Vincze505fba22020-10-22 13:53:29 +02002907#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002908 /* When the 'revert' mechanism is enabled in direct-xip mode, the
2909 * security counter can be increased only after reboot, if the image
2910 * has been confirmed at runtime (the image_ok flag has been set).
2911 * This way a 'revert' can be performed when it's necessary.
2912 */
2913 if (slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02002914#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002915 rc = boot_update_security_counter(BOOT_CURR_IMG(state), active_slot,
2916 boot_img_hdr(state, active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02002917 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002918 BOOT_LOG_ERR("Security counter update failed after image "
2919 "validation.");
2920 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002921 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01002922#ifdef MCUBOOT_DIRECT_XIP_REVERT
2923 }
2924#endif
David Vinczee574f2d2020-07-10 11:42:03 +02002925
Mark Horvathccaf7f82021-01-04 18:16:42 +01002926 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002927
Mark Horvathccaf7f82021-01-04 18:16:42 +01002928#else /* MCUBOOT_HW_ROLLBACK_PROT */
2929 (void) (state);
2930 (void) (active_slot);
David Vinczee574f2d2020-07-10 11:42:03 +02002931
Mark Horvathccaf7f82021-01-04 18:16:42 +01002932 return 0;
2933#endif
2934}
2935
2936fih_int
2937context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2938{
2939 struct slot_usage_t slot_usage[BOOT_IMAGE_NUMBER];
2940 int rc;
2941 fih_int fih_rc;
2942 uint32_t active_slot;
2943
2944 memset(state, 0, sizeof(struct boot_loader_state));
2945 memset(slot_usage, 0, sizeof(struct slot_usage_t) * BOOT_IMAGE_NUMBER);
2946
2947 rc = boot_get_slot_usage(state, slot_usage);
2948 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02002949 goto out;
2950 }
2951
Mark Horvathccaf7f82021-01-04 18:16:42 +01002952#if (BOOT_IMAGE_NUMBER > 1)
2953 while (true) {
2954#endif
2955 FIH_CALL(boot_load_and_validate_images, fih_rc, state, slot_usage);
2956 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2957 goto out;
2958 }
2959
2960#if (BOOT_IMAGE_NUMBER > 1)
2961 rc = boot_verify_dependencies(state, slot_usage);
2962 if (rc != 0) {
2963 /* Dependency check failed for an image, it has been removed from
2964 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
2965 * unavailable. Try to load an image from another slot.
2966 */
2967 continue;
2968 }
2969 /* Dependency check was successful. */
2970 break;
2971 }
2972#endif
2973
2974 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2975
2976 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2977
2978 rc = boot_update_hw_rollback_protection(state, active_slot);
2979 if (rc != 0) {
2980 goto out;
2981 }
2982
2983 rc = boot_add_shared_data(state, active_slot);
2984 if (rc != 0) {
2985 goto out;
2986 }
2987 }
2988
2989 /* All image loaded successfully. */
2990#ifdef MCUBOOT_HAVE_LOGGING
2991 print_loaded_images(state, slot_usage);
2992#endif
2993
2994 fill_rsp(state, slot_usage, rsp);
2995
David Vinczee574f2d2020-07-10 11:42:03 +02002996out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002997 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002998
Mark Horvathccaf7f82021-01-04 18:16:42 +01002999 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3000 fih_rc = fih_int_encode(rc);
3001 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003002
Mark Horvathccaf7f82021-01-04 18:16:42 +01003003 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003004}
Tamas Banfe031092020-09-10 17:32:39 +02003005#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003006
3007/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003008 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003009 * appropriate, and tells you what address to boot from.
3010 *
3011 * @param rsp On success, indicates how booting should occur.
3012 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003013 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003014 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003015fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003016boot_go(struct boot_rsp *rsp)
3017{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003018 fih_int fih_rc = FIH_FAILURE;
3019 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3020 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003021}