blob: 252fb247a63bd9afb7c8a69f9b8e80ce004b0bcf [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 */
Carlos Falgueras Garcíaafb424d2021-06-21 17:05:44 +020077 struct boot_swap_state swap_state;
Mark Horvathccaf7f82021-01-04 18:16:42 +010078#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 -0300305static int
306boot_initialize_area(struct boot_loader_state *state, int flash_area)
307{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000308 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
309 boot_sector_t *out_sectors;
310 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300311 int rc;
312
313 num_sectors = BOOT_MAX_IMG_SECTORS;
314
315 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
316 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
317 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
318 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
319 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
320 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300321#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300322 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
323 out_sectors = state->scratch.sectors;
324 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300325#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300326 } else {
327 return BOOT_EFLASH;
328 }
329
Dominik Ermel51c8d762021-05-24 15:34:01 +0000330#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300331 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000332#else
333 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
334 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
335#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300336 if (rc != 0) {
337 return rc;
338 }
339 *out_num_sectors = num_sectors;
340 return 0;
341}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300342
Christopher Collins92ea77f2016-12-12 15:59:26 -0800343/**
344 * Determines the sector layout of both image slots and the scratch area.
345 * This information is necessary for calculating the number of bytes to erase
346 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300347 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348 */
349static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300350boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800351{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300352 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354
Fabio Utzig10ee6482019-08-01 12:04:52 -0300355 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300356
357 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358 if (rc != 0) {
359 return BOOT_EFLASH;
360 }
361
Fabio Utzig10ee6482019-08-01 12:04:52 -0300362 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363 if (rc != 0) {
364 return BOOT_EFLASH;
365 }
366
Fabio Utzig12d59162019-11-28 10:01:59 -0300367#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300368 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200369 if (rc != 0) {
370 return BOOT_EFLASH;
371 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300372#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200373
Fabio Utzig10ee6482019-08-01 12:04:52 -0300374 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800375
376 return 0;
377}
378
Fabio Utzig12d59162019-11-28 10:01:59 -0300379void
380boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381{
Fabio Utzig4741c452019-12-19 15:32:41 -0300382#ifdef MCUBOOT_ENC_IMAGES
383 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
384#if MCUBOOT_SWAP_SAVE_ENCTLV
385 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
386#endif
387#endif /* MCUBOOT_ENC_IMAGES */
388
389 bs->use_scratch = 0;
390 bs->swap_size = 0;
391 bs->source = 0;
392
Fabio Utzig74aef312019-11-28 11:05:34 -0300393 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300394 bs->idx = BOOT_STATUS_IDX_0;
395 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700396 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300397}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800398
Fabio Utzig12d59162019-11-28 10:01:59 -0300399bool
400boot_status_is_reset(const struct boot_status *bs)
401{
Fabio Utzig74aef312019-11-28 11:05:34 -0300402 return (bs->op == BOOT_STATUS_OP_MOVE &&
403 bs->idx == BOOT_STATUS_IDX_0 &&
404 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800405}
406
407/**
408 * Writes the supplied boot status to the flash file system. The boot status
409 * contains the current state of an in-progress image copy operation.
410 *
411 * @param bs The boot status to write.
412 *
413 * @return 0 on success; nonzero on failure.
414 */
415int
Fabio Utzig12d59162019-11-28 10:01:59 -0300416boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800417{
418 const struct flash_area *fap;
419 uint32_t off;
420 int area_id;
421 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300422 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700423 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300424 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800425
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300426 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100427 * the trailer. Since in the last step the primary slot is erased, the
428 * first two status writes go to the scratch which will be copied to
429 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300430 */
431
Fabio Utzig12d59162019-11-28 10:01:59 -0300432#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300433 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434 /* Write to scratch. */
435 area_id = FLASH_AREA_IMAGE_SCRATCH;
436 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300437#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100438 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300439 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300440#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800441 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300442#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800443
444 rc = flash_area_open(area_id, &fap);
445 if (rc != 0) {
446 rc = BOOT_EFLASH;
447 goto done;
448 }
449
450 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300451 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200452 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300453 erased_val = flash_area_erased_val(fap);
454 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700455 buf[0] = bs->state;
456
457 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800458 if (rc != 0) {
459 rc = BOOT_EFLASH;
460 goto done;
461 }
462
463 rc = 0;
464
465done:
466 flash_area_close(fap);
467 return rc;
468}
Tamas Banfe031092020-09-10 17:32:39 +0200469#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200470#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800471
472/*
David Vinczec3084132020-02-18 14:50:47 +0100473 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800474 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100475static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300476boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
477 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800478{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300479 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300480 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300481 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100482 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300483
Fabio Utzig10ee6482019-08-01 12:04:52 -0300484#if (BOOT_IMAGE_NUMBER == 1)
485 (void)state;
486#endif
487
Fabio Utzigba829042018-09-18 08:29:34 -0300488 (void)bs;
489 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300490
491 image_index = BOOT_CURR_IMG(state);
492
493#ifdef MCUBOOT_ENC_IMAGES
494 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300495 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300496 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100497 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300498 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300499 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100500 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300501 }
502 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300503#endif
504
Raef Colese8fe6cf2020-05-26 13:07:40 +0100505 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
506 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300507
Raef Colese8fe6cf2020-05-26 13:07:40 +0100508 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800509}
510
Tamas Banfe031092020-09-10 17:32:39 +0200511#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100512static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513split_image_check(struct image_header *app_hdr,
514 const struct flash_area *app_fap,
515 struct image_header *loader_hdr,
516 const struct flash_area *loader_fap)
517{
518 static void *tmpbuf;
519 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100520 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800521
522 if (!tmpbuf) {
523 tmpbuf = malloc(BOOT_TMPBUF_SZ);
524 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100525 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800526 }
527 }
528
Raef Colese8fe6cf2020-05-26 13:07:40 +0100529 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
530 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
531 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
532 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800533 }
534
Raef Colese8fe6cf2020-05-26 13:07:40 +0100535 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
536 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537
Raef Colese8fe6cf2020-05-26 13:07:40 +0100538out:
539 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800540}
Tamas Banfe031092020-09-10 17:32:39 +0200541#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800542
Fabio Utzig338a19f2018-12-03 08:37:08 -0200543/*
David Brown9bf95af2019-10-10 15:36:36 -0600544 * Check that this is a valid header. Valid means that the magic is
545 * correct, and that the sizes/offsets are "sane". Sane means that
546 * there is no overflow on the arithmetic, and that the result fits
547 * within the flash area we are in.
548 */
549static bool
550boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
551{
552 uint32_t size;
553
554 if (hdr->ih_magic != IMAGE_MAGIC) {
555 return false;
556 }
557
558 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
559 return false;
560 }
561
562 if (size >= fap->fa_size) {
563 return false;
564 }
565
566 return true;
567}
568
569/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200570 * Check that a memory area consists of a given value.
571 */
572static inline bool
573boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300574{
575 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200576 uint8_t *p = (uint8_t *)data;
577 for (i = 0; i < len; i++) {
578 if (val != p[i]) {
579 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300580 }
581 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200582 return true;
583}
584
585static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300586boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200587{
588 const struct flash_area *fap;
589 struct image_header *hdr;
590 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300591 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200592 int rc;
593
Fabio Utzig10ee6482019-08-01 12:04:52 -0300594 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300595 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200596 if (rc != 0) {
597 return -1;
598 }
599
600 erased_val = flash_area_erased_val(fap);
601 flash_area_close(fap);
602
Fabio Utzig10ee6482019-08-01 12:04:52 -0300603 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200604 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
605 return -1;
606 }
607
608 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300609}
610
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000611#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200612 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200613 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000614 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
615/**
David Vincze8b0b6372020-05-20 19:54:44 +0200616 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000617 *
David Vincze8b0b6372020-05-20 19:54:44 +0200618 * @param ver1 Pointer to the first image version to compare.
619 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000620 *
David Vincze8b0b6372020-05-20 19:54:44 +0200621 * @retval -1 If ver1 is strictly less than ver2.
622 * @retval 0 If the image version numbers are equal,
623 * (not including the build number).
624 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000625 */
626static int
David Vincze8b0b6372020-05-20 19:54:44 +0200627boot_version_cmp(const struct image_version *ver1,
628 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000629{
David Vincze8b0b6372020-05-20 19:54:44 +0200630 if (ver1->iv_major > ver2->iv_major) {
631 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000632 }
David Vincze8b0b6372020-05-20 19:54:44 +0200633 if (ver1->iv_major < ver2->iv_major) {
634 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000635 }
David Vincze8b0b6372020-05-20 19:54:44 +0200636 /* The major version numbers are equal, continue comparison. */
637 if (ver1->iv_minor > ver2->iv_minor) {
638 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000639 }
David Vincze8b0b6372020-05-20 19:54:44 +0200640 if (ver1->iv_minor < ver2->iv_minor) {
641 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000642 }
David Vincze8b0b6372020-05-20 19:54:44 +0200643 /* The minor version numbers are equal, continue comparison. */
644 if (ver1->iv_revision > ver2->iv_revision) {
645 return 1;
646 }
647 if (ver1->iv_revision < ver2->iv_revision) {
648 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000649 }
650
651 return 0;
652}
653#endif
654
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000655#if defined(MCUBOOT_DIRECT_XIP)
656/**
657 * Check if image in slot has been set with specific ROM address to run from
658 * and whether the slot starts at that address.
659 *
660 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
661 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
662 * header matches the slot address;
663 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
664 * does not match the slot address.
665 */
666static bool
Mark Horvathccaf7f82021-01-04 18:16:42 +0100667boot_rom_address_check(struct boot_loader_state *state,
668 struct slot_usage_t slot_usage[])
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000669{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100670 uint32_t active_slot;
671 const struct image_header *hdr;
672 uint32_t f_off;
673
674 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
675 hdr = boot_img_hdr(state, active_slot);
676 f_off = boot_img_slot_off(state, active_slot);
677
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000678 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
679 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100680 ", skipping",
681 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000682 hdr->ih_load_addr);
683
684 /* If there is address mismatch, the image is not bootable from this
685 * slot.
686 */
687 return 1;
688 }
689 return 0;
690}
691#endif
692
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300693/*
694 * Check that there is a valid image in a slot
695 *
696 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100697 * FIH_SUCCESS if image was successfully validated
698 * 1 (or its fih_int encoded form) if no bootloable image was found
699 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300700 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100701static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300702boot_validate_slot(struct boot_loader_state *state, int slot,
703 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800704{
705 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400706 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300707 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100708 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800709 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300710
Fabio Utzig10ee6482019-08-01 12:04:52 -0300711 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300712 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100714 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800715 }
716
Fabio Utzig10ee6482019-08-01 12:04:52 -0300717 hdr = boot_img_hdr(state, slot);
718 if (boot_check_header_erased(state, slot) == 0 ||
719 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300720
721#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
722 /*
723 * This fixes an issue where an image might be erased, but a trailer
724 * be left behind. It can happen if the image is in the secondary slot
725 * and did not pass validation, in which case the whole slot is erased.
726 * If during the erase operation, a reset occurs, parts of the slot
727 * might have been erased while some did not. The concerning part is
728 * the trailer because it might disable a new image from being loaded
729 * through mcumgr; so we just get rid of the trailer here, if the header
730 * is erased.
731 */
732 if (slot != BOOT_PRIMARY_SLOT) {
733 swap_erase_trailer_sectors(state, fap);
734 }
735#endif
736
David Vincze2d736ad2019-02-18 11:50:22 +0100737 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100738 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200739 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300740 }
741
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000742#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
743 if (slot != BOOT_PRIMARY_SLOT) {
744 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200745 rc = boot_version_cmp(
746 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
747 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
748 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000749 BOOT_LOG_ERR("insufficient version in secondary slot");
750 flash_area_erase(fap, 0, fap->fa_size);
751 /* Image in the secondary slot does not satisfy version requirement.
752 * Erase the image and continue booting from the primary slot.
753 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100754 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000755 goto out;
756 }
757 }
758#endif
759
Raef Colese8fe6cf2020-05-26 13:07:40 +0100760 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
761 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200762 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
David Brownb38e0442017-02-24 13:57:12 -0700763 flash_area_erase(fap, 0, fap->fa_size);
David Vinczee574f2d2020-07-10 11:42:03 +0200764 /* Image is invalid, erase it to prevent further unnecessary
765 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700766 */
767 }
David Brown098de832019-12-10 11:58:01 -0700768#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100769 BOOT_LOG_ERR("Image in the %s slot is not valid!",
770 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700771#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100772 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200773 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800774 }
775
Fabio Utzig338a19f2018-12-03 08:37:08 -0200776out:
777 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100778
779 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800780}
781
David Vinczec3084132020-02-18 14:50:47 +0100782#ifdef MCUBOOT_HW_ROLLBACK_PROT
783/**
784 * Updates the stored security counter value with the image's security counter
785 * value which resides in the given slot, only if it's greater than the stored
786 * value.
787 *
788 * @param image_index Index of the image to determine which security
789 * counter to update.
790 * @param slot Slot number of the image.
791 * @param hdr Pointer to the image header structure of the image
792 * that is currently stored in the given slot.
793 *
794 * @return 0 on success; nonzero on failure.
795 */
796static int
797boot_update_security_counter(uint8_t image_index, int slot,
798 struct image_header *hdr)
799{
800 const struct flash_area *fap = NULL;
801 uint32_t img_security_cnt;
802 int rc;
803
804 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
805 &fap);
806 if (rc != 0) {
807 rc = BOOT_EFLASH;
808 goto done;
809 }
810
811 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
812 if (rc != 0) {
813 goto done;
814 }
815
816 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
817 if (rc != 0) {
818 goto done;
819 }
820
821done:
822 flash_area_close(fap);
823 return rc;
824}
825#endif /* MCUBOOT_HW_ROLLBACK_PROT */
826
Tamas Banfe031092020-09-10 17:32:39 +0200827#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200828/**
829 * Determines which swap operation to perform, if any. If it is determined
830 * that a swap operation is required, the image in the secondary slot is checked
831 * for validity. If the image in the secondary slot is invalid, it is erased,
832 * and a swap type of "none" is indicated.
833 *
834 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
835 */
836static int
837boot_validated_swap_type(struct boot_loader_state *state,
838 struct boot_status *bs)
839{
840 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100841 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200842
843 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
844 if (BOOT_IS_UPGRADE(swap_type)) {
845 /* Boot loader wants to switch to the secondary slot.
846 * Ensure image is valid.
847 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100848 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
849 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
850 if (fih_eq(fih_rc, fih_int_encode(1))) {
851 swap_type = BOOT_SWAP_TYPE_NONE;
852 } else {
853 swap_type = BOOT_SWAP_TYPE_FAIL;
854 }
David Vinczee574f2d2020-07-10 11:42:03 +0200855 }
856 }
857
858 return swap_type;
859}
860
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862 * Erases a region of flash.
863 *
Fabio Utzigba829042018-09-18 08:29:34 -0300864 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800865 * @param off The offset within the flash area to start the
866 * erase.
867 * @param sz The number of bytes to erase.
868 *
869 * @return 0 on success; nonzero on failure.
870 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300871int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300872boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800873{
Fabio Utzigba829042018-09-18 08:29:34 -0300874 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800875}
876
877/**
878 * Copies the contents of one flash region to another. You must erase the
879 * destination region prior to calling this function.
880 *
881 * @param flash_area_id_src The ID of the source flash area.
882 * @param flash_area_id_dst The ID of the destination flash area.
883 * @param off_src The offset within the source flash area to
884 * copy from.
885 * @param off_dst The offset within the destination flash area to
886 * copy to.
887 * @param sz The number of bytes to copy.
888 *
889 * @return 0 on success; nonzero on failure.
890 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300891int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300892boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300893 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300894 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800895 uint32_t off_src, uint32_t off_dst, uint32_t sz)
896{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800897 uint32_t bytes_copied;
898 int chunk_sz;
899 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300900#ifdef MCUBOOT_ENC_IMAGES
901 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300902 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300903 size_t blk_off;
904 struct image_header *hdr;
905 uint16_t idx;
906 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300907 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300908#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800909
Fabio Utzig10ee6482019-08-01 12:04:52 -0300910 TARGET_STATIC uint8_t buf[1024];
911
912#if !defined(MCUBOOT_ENC_IMAGES)
913 (void)state;
914#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800915
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916 bytes_copied = 0;
917 while (bytes_copied < sz) {
918 if (sz - bytes_copied > sizeof buf) {
919 chunk_sz = sizeof buf;
920 } else {
921 chunk_sz = sz - bytes_copied;
922 }
923
924 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
925 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300926 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800927 }
928
Fabio Utzigba829042018-09-18 08:29:34 -0300929#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300930 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300931 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
932 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
933 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
934 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100935 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300936 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300937#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300938 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300939 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100940 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300941 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300942 off = off_dst;
943 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300944#else
945 off = off_dst;
946 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
947 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
948 }
949#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200950 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300951 blk_sz = chunk_sz;
952 idx = 0;
953 if (off + bytes_copied < hdr->ih_hdr_size) {
954 /* do not decrypt header */
955 blk_off = 0;
956 blk_sz = chunk_sz - hdr->ih_hdr_size;
957 idx = hdr->ih_hdr_size;
958 } else {
959 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
960 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300961 tlv_off = BOOT_TLV_OFF(hdr);
962 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300963 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300964 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300965 blk_sz = 0;
966 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300967 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300968 }
969 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300970 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300971 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
972 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300973 }
974 }
975#endif
976
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
978 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300979 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800980 }
981
982 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300983
984 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800985 }
986
Fabio Utzigba829042018-09-18 08:29:34 -0300987 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800988}
989
Christopher Collins92ea77f2016-12-12 15:59:26 -0800990/**
David Vincze2d736ad2019-02-18 11:50:22 +0100991 * Overwrite primary slot with the image contained in the secondary slot.
992 * If a prior copy operation was interrupted by a system reset, this function
993 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800994 *
995 * @param bs The current boot status. This function reads
996 * this struct to determine if it is resuming
997 * an interrupted swap operation. This
998 * function writes the updated status to this
999 * function on return.
1000 *
1001 * @return 0 on success; nonzero on failure.
1002 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001003#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001004static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001005boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001006{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001007 size_t sect_count;
1008 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001009 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001010 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001011 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001012 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001013 const struct flash_area *fap_primary_slot;
1014 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001015 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001016
Fabio Utzigb4f88102020-10-04 10:16:24 -03001017#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1018 uint32_t sector;
1019 uint32_t trailer_sz;
1020 uint32_t off;
1021 uint32_t sz;
1022#endif
1023
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001024 (void)bs;
1025
Fabio Utzig13d9e352017-10-05 20:32:31 -03001026#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1027 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001028 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001029 assert(rc == 0);
1030#endif
David Brown17609d82017-05-05 09:41:34 -06001031
David Vincze2d736ad2019-02-18 11:50:22 +01001032 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1033 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001034
Fabio Utzigb0f04732019-07-31 09:49:19 -03001035 image_index = BOOT_CURR_IMG(state);
1036
1037 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1038 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001039 assert (rc == 0);
1040
Fabio Utzigb0f04732019-07-31 09:49:19 -03001041 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1042 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001043 assert (rc == 0);
1044
Fabio Utzig10ee6482019-08-01 12:04:52 -03001045 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001046 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001047 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001048 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001049 assert(rc == 0);
1050
Fabio Utzig13d9e352017-10-05 20:32:31 -03001051#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001052 if ((size + this_size) >= src_size) {
1053 size += src_size - size;
1054 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001055 break;
1056 }
1057#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001058
1059 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001060 }
1061
Fabio Utzigb4f88102020-10-04 10:16:24 -03001062#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1063 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1064 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1065 sz = 0;
1066 do {
1067 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1068 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1069 sector--;
1070 } while (sz < trailer_sz);
1071
1072 rc = boot_erase_region(fap_primary_slot, off, sz);
1073 assert(rc == 0);
1074#endif
1075
Fabio Utzigba829042018-09-18 08:29:34 -03001076#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001077 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001078 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001079 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001080 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001081
Fabio Utzigba829042018-09-18 08:29:34 -03001082 if (rc < 0) {
1083 return BOOT_EBADIMAGE;
1084 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001085 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001086 return BOOT_EBADIMAGE;
1087 }
1088 }
1089#endif
1090
David Vincze2d736ad2019-02-18 11:50:22 +01001091 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1092 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001093 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001094 if (rc != 0) {
1095 return rc;
1096 }
1097
1098#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1099 rc = boot_write_magic(fap_primary_slot);
1100 if (rc != 0) {
1101 return rc;
1102 }
1103#endif
David Brown17609d82017-05-05 09:41:34 -06001104
David Vinczec3084132020-02-18 14:50:47 +01001105#ifdef MCUBOOT_HW_ROLLBACK_PROT
1106 /* Update the stored security counter with the new image's security counter
1107 * value. Both slots hold the new image at this point, but the secondary
1108 * slot's image header must be passed since the image headers in the
1109 * boot_data structure have not been updated yet.
1110 */
1111 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1112 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1113 if (rc != 0) {
1114 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1115 return rc;
1116 }
1117#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1118
Fabio Utzig13d9e352017-10-05 20:32:31 -03001119 /*
1120 * Erases header and trailer. The trailer is erased because when a new
1121 * image is written without a trailer as is the case when using newt, the
1122 * trailer that was left might trigger a new upgrade.
1123 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001124 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001125 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001126 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1127 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001128 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001129 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001130 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001131 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001132 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1133 last_sector),
1134 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1135 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001136 assert(rc == 0);
1137
David Vincze2d736ad2019-02-18 11:50:22 +01001138 flash_area_close(fap_primary_slot);
1139 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001140
David Vincze2d736ad2019-02-18 11:50:22 +01001141 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001142
1143 return 0;
1144}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001145#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001146
Christopher Collinsa1c12042019-05-23 14:00:28 -07001147#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001148/**
1149 * Swaps the two images in flash. If a prior copy operation was interrupted
1150 * by a system reset, this function completes that operation.
1151 *
1152 * @param bs The current boot status. This function reads
1153 * this struct to determine if it is resuming
1154 * an interrupted swap operation. This
1155 * function writes the updated status to this
1156 * function on return.
1157 *
1158 * @return 0 on success; nonzero on failure.
1159 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001160static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001161boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001162{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001163 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001164#ifdef MCUBOOT_ENC_IMAGES
1165 const struct flash_area *fap;
1166 uint8_t slot;
1167 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001168#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001169 uint32_t size;
1170 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001171 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001172 int rc;
1173
1174 /* FIXME: just do this if asked by user? */
1175
1176 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001177 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001178
Fabio Utzig12d59162019-11-28 10:01:59 -03001179 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001180 /*
1181 * No swap ever happened, so need to find the largest image which
1182 * will be used to determine the amount of sectors to swap.
1183 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001184 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001185 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001186 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001187 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001188 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001189
Fabio Utzigba829042018-09-18 08:29:34 -03001190#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001191 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001192 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001193 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001194 assert(rc >= 0);
1195
1196 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001197 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001198 assert(rc == 0);
1199 } else {
1200 rc = 0;
1201 }
1202 } else {
1203 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1204 }
1205#endif
1206
Fabio Utzig10ee6482019-08-01 12:04:52 -03001207 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001208 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001209 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001210 assert(rc == 0);
1211 }
1212
Fabio Utzigba829042018-09-18 08:29:34 -03001213#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001214 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001215 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001216 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001217 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001218 assert(rc >= 0);
1219
1220 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001221 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001222 assert(rc == 0);
1223 } else {
1224 rc = 0;
1225 }
1226 } else {
1227 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1228 }
1229#endif
1230
Fabio Utzig46490722017-09-04 15:34:32 -03001231 if (size > copy_size) {
1232 copy_size = size;
1233 }
1234
1235 bs->swap_size = copy_size;
1236 } else {
1237 /*
1238 * If a swap was under way, the swap_size should already be present
1239 * in the trailer...
1240 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001241 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001242 assert(rc == 0);
1243
1244 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001245
1246#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001247 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1248 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001249 assert(rc == 0);
1250
1251 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001252 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001253 break;
1254 }
1255 }
1256
1257 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001258 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001259 }
1260 }
1261#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001262 }
1263
Fabio Utzig12d59162019-11-28 10:01:59 -03001264 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001265
David Vincze2d736ad2019-02-18 11:50:22 +01001266#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001267 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001268 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001269 BOOT_LOG_WRN("%d status write fails performing the swap",
1270 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001271 }
1272#endif
1273
Christopher Collins92ea77f2016-12-12 15:59:26 -08001274 return 0;
1275}
David Brown17609d82017-05-05 09:41:34 -06001276#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001277
David Vinczee32483f2019-06-13 10:46:24 +02001278#if (BOOT_IMAGE_NUMBER > 1)
1279/**
1280 * Check the image dependency whether it is satisfied and modify
1281 * the swap type if necessary.
1282 *
1283 * @param dep Image dependency which has to be verified.
1284 *
1285 * @return 0 on success; nonzero on failure.
1286 */
1287static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001288boot_verify_slot_dependency(struct boot_loader_state *state,
1289 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001290{
1291 struct image_version *dep_version;
1292 size_t dep_slot;
1293 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001294 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001295
1296 /* Determine the source of the image which is the subject of
1297 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001298 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001299 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1300 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001301 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001302
David Vincze8b0b6372020-05-20 19:54:44 +02001303 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1304 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001305 /* Dependency not satisfied.
1306 * Modify the swap type to decrease the version number of the image
1307 * (which will be located in the primary slot after the boot process),
1308 * consequently the number of unsatisfied dependencies will be
1309 * decreased or remain the same.
1310 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001311 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001312 case BOOT_SWAP_TYPE_TEST:
1313 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001314 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001315 break;
1316 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001317 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001318 break;
1319 default:
1320 break;
1321 }
David Vincze8b0b6372020-05-20 19:54:44 +02001322 } else {
1323 /* Dependency satisfied. */
1324 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001325 }
1326
1327 return rc;
1328}
1329
1330/**
1331 * Read all dependency TLVs of an image from the flash and verify
1332 * one after another to see if they are all satisfied.
1333 *
1334 * @param slot Image slot number.
1335 *
1336 * @return 0 on success; nonzero on failure.
1337 */
1338static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001339boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001340{
1341 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001342 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001343 struct image_dependency dep;
1344 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001345 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001346 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001347 int rc;
1348
Fabio Utzig10ee6482019-08-01 12:04:52 -03001349 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001350 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001351 if (rc != 0) {
1352 rc = BOOT_EFLASH;
1353 goto done;
1354 }
1355
Fabio Utzig61fd8882019-09-14 20:00:20 -03001356 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1357 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001358 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001359 goto done;
1360 }
1361
Fabio Utzig61fd8882019-09-14 20:00:20 -03001362 while (true) {
1363 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1364 if (rc < 0) {
1365 return -1;
1366 } else if (rc > 0) {
1367 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001368 break;
1369 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001370
1371 if (len != sizeof(dep)) {
1372 rc = BOOT_EBADIMAGE;
1373 goto done;
1374 }
1375
1376 rc = flash_area_read(fap, off, &dep, len);
1377 if (rc != 0) {
1378 rc = BOOT_EFLASH;
1379 goto done;
1380 }
1381
1382 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1383 rc = BOOT_EBADARGS;
1384 goto done;
1385 }
1386
1387 /* Verify dependency and modify the swap type if not satisfied. */
1388 rc = boot_verify_slot_dependency(state, &dep);
1389 if (rc != 0) {
1390 /* Dependency not satisfied. */
1391 goto done;
1392 }
David Vinczee32483f2019-06-13 10:46:24 +02001393 }
1394
1395done:
1396 flash_area_close(fap);
1397 return rc;
1398}
1399
1400/**
David Vinczee32483f2019-06-13 10:46:24 +02001401 * Iterate over all the images and verify whether the image dependencies in the
1402 * TLV area are all satisfied and update the related swap type if necessary.
1403 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001404static int
1405boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001406{
Erik Johnson49063752020-02-06 09:59:31 -06001407 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001408 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001409
Fabio Utzig10ee6482019-08-01 12:04:52 -03001410 BOOT_CURR_IMG(state) = 0;
1411 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001412 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1413 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1414 slot = BOOT_SECONDARY_SLOT;
1415 } else {
1416 slot = BOOT_PRIMARY_SLOT;
1417 }
1418
1419 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001420 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001421 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001422 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001423 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001424 /* Cannot upgrade due to non-met dependencies, so disable all
1425 * image upgrades.
1426 */
1427 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1428 BOOT_CURR_IMG(state) = idx;
1429 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1430 }
1431 break;
David Vinczee32483f2019-06-13 10:46:24 +02001432 }
1433 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001434 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001435}
1436#endif /* (BOOT_IMAGE_NUMBER > 1) */
1437
Christopher Collins92ea77f2016-12-12 15:59:26 -08001438/**
David Vinczeba3bd602019-06-17 16:01:43 +02001439 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001440 *
David Vinczeba3bd602019-06-17 16:01:43 +02001441 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001442 *
1443 * @return 0 on success; nonzero on failure.
1444 */
1445static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001446boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001447{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001448 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001449#ifndef MCUBOOT_OVERWRITE_ONLY
1450 uint8_t swap_type;
1451#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001452
David Vinczeba3bd602019-06-17 16:01:43 +02001453 /* At this point there are no aborted swaps. */
1454#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001455 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001456#elif defined(MCUBOOT_BOOTSTRAP)
1457 /* Check if the image update was triggered by a bad image in the
1458 * primary slot (the validity of the image in the secondary slot had
1459 * already been checked).
1460 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001461 fih_int fih_rc = FIH_FAILURE;
1462 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1463 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1464 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001465 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001466 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001467 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001468 }
1469#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001470 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001471#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001472 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001473
1474#ifndef MCUBOOT_OVERWRITE_ONLY
1475 /* The following state needs image_ok be explicitly set after the
1476 * swap was finished to avoid a new revert.
1477 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001478 swap_type = BOOT_SWAP_TYPE(state);
1479 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1480 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001481 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001482 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001483 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001484 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001485 }
1486
David Vinczec3084132020-02-18 14:50:47 +01001487#ifdef MCUBOOT_HW_ROLLBACK_PROT
1488 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1489 /* Update the stored security counter with the new image's security
1490 * counter value. The primary slot holds the new image at this point,
1491 * but the secondary slot's image header must be passed since image
1492 * headers in the boot_data structure have not been updated yet.
1493 *
1494 * In case of a permanent image swap mcuboot will never attempt to
1495 * revert the images on the next reboot. Therefore, the security
1496 * counter must be increased right after the image upgrade.
1497 */
1498 rc = boot_update_security_counter(
1499 BOOT_CURR_IMG(state),
1500 BOOT_PRIMARY_SLOT,
1501 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1502 if (rc != 0) {
1503 BOOT_LOG_ERR("Security counter update failed after "
1504 "image upgrade.");
1505 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1506 }
1507 }
1508#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1509
Fabio Utzige575b0b2019-09-11 12:34:23 -03001510 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001511 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001512 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001513 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001514 }
David Vinczeba3bd602019-06-17 16:01:43 +02001515 }
1516#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001517
David Vinczeba3bd602019-06-17 16:01:43 +02001518 return rc;
1519}
1520
1521/**
1522 * Completes a previously aborted image swap.
1523 *
1524 * @param bs The current boot status.
1525 *
1526 * @return 0 on success; nonzero on failure.
1527 */
1528#if !defined(MCUBOOT_OVERWRITE_ONLY)
1529static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001530boot_complete_partial_swap(struct boot_loader_state *state,
1531 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001532{
1533 int rc;
1534
1535 /* Determine the type of swap operation being resumed from the
1536 * `swap-type` trailer field.
1537 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001538 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001539 assert(rc == 0);
1540
Fabio Utzig10ee6482019-08-01 12:04:52 -03001541 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001542
1543 /* The following states need image_ok be explicitly set after the
1544 * swap was finished to avoid a new revert.
1545 */
1546 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1547 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001548 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001549 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001550 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001551 }
1552 }
1553
Fabio Utzige575b0b2019-09-11 12:34:23 -03001554 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001555 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001556 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001557 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001558 }
1559 }
1560
Fabio Utzig10ee6482019-08-01 12:04:52 -03001561 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001562 BOOT_LOG_ERR("panic!");
1563 assert(0);
1564
1565 /* Loop forever... */
1566 while (1) {}
1567 }
1568
1569 return rc;
1570}
1571#endif /* !MCUBOOT_OVERWRITE_ONLY */
1572
1573#if (BOOT_IMAGE_NUMBER > 1)
1574/**
1575 * Review the validity of previously determined swap types of other images.
1576 *
1577 * @param aborted_swap The current image upgrade is a
1578 * partial/aborted swap.
1579 */
1580static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001581boot_review_image_swap_types(struct boot_loader_state *state,
1582 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001583{
1584 /* In that case if we rebooted in the middle of an image upgrade process, we
1585 * must review the validity of swap types, that were previously determined
1586 * for other images. The image_ok flag had not been set before the reboot
1587 * for any of the updated images (only the copy_done flag) and thus falsely
1588 * the REVERT swap type has been determined for the previous images that had
1589 * been updated before the reboot.
1590 *
1591 * There are two separate scenarios that we have to deal with:
1592 *
1593 * 1. The reboot has happened during swapping an image:
1594 * The current image upgrade has been determined as a
1595 * partial/aborted swap.
1596 * 2. The reboot has happened between two separate image upgrades:
1597 * In this scenario we must check the swap type of the current image.
1598 * In those cases if it is NONE or REVERT we cannot certainly determine
1599 * the fact of a reboot. In a consistent state images must move in the
1600 * same direction or stay in place, e.g. in practice REVERT and TEST
1601 * swap types cannot be present at the same time. If the swap type of
1602 * the current image is either TEST, PERM or FAIL we must review the
1603 * already determined swap types of other images and set each false
1604 * REVERT swap types to NONE (these images had been successfully
1605 * updated before the system rebooted between two separate image
1606 * upgrades).
1607 */
1608
Fabio Utzig10ee6482019-08-01 12:04:52 -03001609 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001610 /* Nothing to do */
1611 return;
1612 }
1613
1614 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001615 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1616 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001617 /* Nothing to do */
1618 return;
1619 }
1620 }
1621
Fabio Utzig10ee6482019-08-01 12:04:52 -03001622 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1623 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1624 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001625 }
1626 }
1627}
1628#endif
1629
1630/**
1631 * Prepare image to be updated if required.
1632 *
1633 * Prepare image to be updated if required with completing an image swap
1634 * operation if one was aborted and/or determining the type of the
1635 * swap operation. In case of any error set the swap type to NONE.
1636 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001637 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001638 * @param bs Pointer where the read and possibly updated
1639 * boot status can be written to.
1640 */
1641static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001642boot_prepare_image_for_update(struct boot_loader_state *state,
1643 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001644{
1645 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001646 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001647
1648 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001649 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001650 if (rc != 0) {
1651 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1652 " - too small?", BOOT_MAX_IMG_SECTORS);
1653 /* Unable to determine sector layout, continue with next image
1654 * if there is one.
1655 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001656 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001657 return;
1658 }
1659
1660 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001661 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001662 if (rc != 0) {
1663 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001664 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001665 BOOT_CURR_IMG(state));
1666 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001667 return;
1668 }
1669
1670 /* If the current image's slots aren't compatible, no swap is possible.
1671 * Just boot into primary slot.
1672 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001673 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001674 boot_status_reset(bs);
1675
1676#ifndef MCUBOOT_OVERWRITE_ONLY
1677 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001678 if (rc != 0) {
1679 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001680 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001681 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001682 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001683 return;
1684 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001685#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001686
Fabio Utzig74aef312019-11-28 11:05:34 -03001687#ifdef MCUBOOT_SWAP_USING_MOVE
1688 /*
1689 * Must re-read image headers because the boot status might
1690 * have been updated in the previous function call.
1691 */
1692 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001693#ifdef MCUBOOT_BOOTSTRAP
1694 /* When bootstrapping it's OK to not have image magic in the primary slot */
1695 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1696 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1697#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001698 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001699#endif
1700
Fabio Utzig74aef312019-11-28 11:05:34 -03001701 /* Continue with next image if there is one. */
1702 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1703 BOOT_CURR_IMG(state));
1704 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1705 return;
1706 }
1707#endif
1708
David Vinczeba3bd602019-06-17 16:01:43 +02001709 /* Determine if we rebooted in the middle of an image swap
1710 * operation. If a partial swap was detected, complete it.
1711 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001712 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001713
1714#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001715 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001716#endif
1717
1718#ifdef MCUBOOT_OVERWRITE_ONLY
1719 /* Should never arrive here, overwrite-only mode has
1720 * no swap state.
1721 */
1722 assert(0);
1723#else
1724 /* Determine the type of swap operation being resumed from the
1725 * `swap-type` trailer field.
1726 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001727 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001728 assert(rc == 0);
1729#endif
1730 /* Attempt to read an image header from each slot. Ensure that
1731 * image headers in slots are aligned with headers in boot_data.
1732 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001733 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001734 assert(rc == 0);
1735
1736 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001737 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001738 } else {
1739 /* There was no partial swap, determine swap type. */
1740 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001741 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001742 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001743 FIH_CALL(boot_validate_slot, fih_rc,
1744 state, BOOT_SECONDARY_SLOT, bs);
1745 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1746 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1747 } else {
1748 BOOT_SWAP_TYPE(state) = bs->swap_type;
1749 }
David Vinczeba3bd602019-06-17 16:01:43 +02001750 }
1751
1752#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001753 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001754#endif
1755
1756#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001757 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001758 /* Header checks are done first because they are
1759 * inexpensive. Since overwrite-only copies starting from
1760 * offset 0, if interrupted, it might leave a valid header
1761 * magic, so also run validation on the primary slot to be
1762 * sure it's not OK.
1763 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001764 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1765 FIH_CALL(boot_validate_slot, fih_rc,
1766 state, BOOT_PRIMARY_SLOT, bs);
1767
1768 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1769
Fabio Utzig3d77c952020-10-04 10:23:17 -03001770 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001771 FIH_CALL(boot_validate_slot, fih_rc,
1772 state, BOOT_SECONDARY_SLOT, bs);
1773
Fabio Utzig3d77c952020-10-04 10:23:17 -03001774 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001775 /* Set swap type to REVERT to overwrite the primary
1776 * slot with the image contained in secondary slot
1777 * and to trigger the explicit setting of the
1778 * image_ok flag.
1779 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001780 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001781 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001782 }
1783 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001784#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001785 }
David Vinczeba3bd602019-06-17 16:01:43 +02001786 } else {
1787 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001788 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001789 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001790}
1791
Mark Horvathccaf7f82021-01-04 18:16:42 +01001792/**
1793 * Updates the security counter for the current image.
1794 *
1795 * @param state Boot loader status information.
1796 *
1797 * @return 0 on success; nonzero on failure.
1798 */
1799static int
1800boot_update_hw_rollback_protection(struct boot_loader_state *state)
1801{
1802#ifdef MCUBOOT_HW_ROLLBACK_PROT
1803 int rc;
1804
1805 /* Update the stored security counter with the active image's security
1806 * counter value. It will only be updated if the new security counter is
1807 * greater than the stored value.
1808 *
1809 * In case of a successful image swapping when the swap type is TEST the
1810 * security counter can be increased only after a reset, when the swap
1811 * type is NONE and the image has marked itself "OK" (the image_ok flag
1812 * has been set). This way a "revert" can be performed when it's
1813 * necessary.
1814 */
1815 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1816 rc = boot_update_security_counter(
1817 BOOT_CURR_IMG(state),
1818 BOOT_PRIMARY_SLOT,
1819 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1820 if (rc != 0) {
1821 BOOT_LOG_ERR("Security counter update failed after image "
1822 "validation.");
1823 return rc;
1824 }
1825 }
1826
1827 return 0;
1828
1829#else /* MCUBOOT_HW_ROLLBACK_PROT */
1830 (void) (state);
1831
1832 return 0;
1833#endif
1834}
1835
Raef Colese8fe6cf2020-05-26 13:07:40 +01001836fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001837context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001838{
Marti Bolivar84898652017-06-13 17:20:22 -04001839 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001840 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001841 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001842 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001843 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001844 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001845 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001846
1847 /* The array of slot sectors are defined here (as opposed to file scope) so
1848 * that they don't get allocated for non-boot-loader apps. This is
1849 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001850 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001851 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001852 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1853 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001854#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001855 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001856#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001857
Fabio Utzig10ee6482019-08-01 12:04:52 -03001858 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001859 has_upgrade = false;
1860
1861#if (BOOT_IMAGE_NUMBER == 1)
1862 (void)has_upgrade;
1863#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001864
David Vinczeba3bd602019-06-17 16:01:43 +02001865 /* Iterate over all the images. By the end of the loop the swap type has
1866 * to be determined for each image and all aborted swaps have to be
1867 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001868 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001869 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001870
David Vinczeba3bd602019-06-17 16:01:43 +02001871#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1872 /* The keys used for encryption may no longer be valid (could belong to
1873 * another images). Therefore, mark them as invalid to force their reload
1874 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001875 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001876 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001877#endif
1878
Fabio Utzig10ee6482019-08-01 12:04:52 -03001879 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001880
Fabio Utzig10ee6482019-08-01 12:04:52 -03001881 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001882 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001883 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001884 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001885#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001886 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001887#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001888
1889 /* Open primary and secondary image areas for the duration
1890 * of this call.
1891 */
1892 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001893 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001894 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001895 assert(rc == 0);
1896 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001897#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001898 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001899 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001900 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001901#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001902
1903 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001904 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001905
Fabio Utzige575b0b2019-09-11 12:34:23 -03001906 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001907 has_upgrade = true;
1908 }
David Vinczeba3bd602019-06-17 16:01:43 +02001909 }
1910
David Vinczee32483f2019-06-13 10:46:24 +02001911#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001912 if (has_upgrade) {
1913 /* Iterate over all the images and verify whether the image dependencies
1914 * are all satisfied and update swap type if necessary.
1915 */
1916 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001917 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001918 /*
1919 * It was impossible to upgrade because the expected dependency version
1920 * was not available. Here we already changed the swap_type so that
1921 * instead of asserting the bootloader, we continue and no upgrade is
1922 * performed.
1923 */
1924 rc = 0;
1925 }
1926 }
David Vinczee32483f2019-06-13 10:46:24 +02001927#endif
1928
David Vinczeba3bd602019-06-17 16:01:43 +02001929 /* Iterate over all the images. At this point there are no aborted swaps
1930 * and the swap types are determined for each image. By the end of the loop
1931 * all required update operations will have been finished.
1932 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001933 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001934
1935#if (BOOT_IMAGE_NUMBER > 1)
1936#ifdef MCUBOOT_ENC_IMAGES
1937 /* The keys used for encryption may no longer be valid (could belong to
1938 * another images). Therefore, mark them as invalid to force their reload
1939 * by boot_enc_load().
1940 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001941 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001942#endif /* MCUBOOT_ENC_IMAGES */
1943
1944 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001945 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001946#endif /* (BOOT_IMAGE_NUMBER > 1) */
1947
1948 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001949 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001950
Fabio Utzig10ee6482019-08-01 12:04:52 -03001951 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001952 case BOOT_SWAP_TYPE_NONE:
1953 break;
1954
1955 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1956 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1957 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001958 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001959 assert(rc == 0);
1960 break;
1961
1962 case BOOT_SWAP_TYPE_FAIL:
1963 /* The image in secondary slot was invalid and is now erased. Ensure
1964 * we don't try to boot into it again on the next reboot. Do this by
1965 * pretending we just reverted back to primary slot.
1966 */
1967#ifndef MCUBOOT_OVERWRITE_ONLY
1968 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001969 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001970 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001971 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001972 }
1973#endif /* !MCUBOOT_OVERWRITE_ONLY */
1974 break;
1975
1976 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001977 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001978 }
1979
Fabio Utzig10ee6482019-08-01 12:04:52 -03001980 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001981 BOOT_LOG_ERR("panic!");
1982 assert(0);
1983
1984 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001985 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001986 }
1987 }
1988
1989 /* Iterate over all the images. At this point all required update operations
1990 * have finished. By the end of the loop each image in the primary slot will
1991 * have been re-validated.
1992 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001993 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1994 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001995 /* Attempt to read an image header from each slot. Ensure that image
1996 * headers in slots are aligned with headers in boot_data.
1997 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001998 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001999 if (rc != 0) {
2000 goto out;
2001 }
2002 /* Since headers were reloaded, it can be assumed we just performed
2003 * a swap or overwrite. Now the header info that should be used to
2004 * provide the data for the bootstrap, which previously was at
2005 * secondary slot, was updated to primary slot.
2006 */
2007 }
2008
2009#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002010 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2011 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002012 goto out;
2013 }
2014#else
2015 /* Even if we're not re-validating the primary slot, we could be booting
2016 * onto an empty flash chip. At least do a basic sanity check that
2017 * the magic number on the image is OK.
2018 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002019 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002020 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03002021 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
2022 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002023 rc = BOOT_EBADIMAGE;
2024 goto out;
2025 }
David Vinczec3084132020-02-18 14:50:47 +01002026#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2027
Mark Horvathccaf7f82021-01-04 18:16:42 +01002028 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002029 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002030 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002031 }
David Vincze1cf11b52020-03-24 07:51:09 +01002032
Mark Horvathccaf7f82021-01-04 18:16:42 +01002033 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002034 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002035 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002036 }
David Vinczeba3bd602019-06-17 16:01:43 +02002037 }
2038
Fabio Utzigf616c542019-12-19 15:23:32 -03002039 /*
2040 * Since the boot_status struct stores plaintext encryption keys, reset
2041 * them here to avoid the possibility of jumping into an image that could
2042 * easily recover them.
2043 */
2044 memset(&bs, 0, sizeof(struct boot_status));
2045
Mark Horvathccaf7f82021-01-04 18:16:42 +01002046 fill_rsp(state, NULL, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002047
Raef Colese8fe6cf2020-05-26 13:07:40 +01002048 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002049out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002050 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002051
2052 if (rc) {
2053 fih_rc = fih_int_encode(rc);
2054 }
2055
2056 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002057}
2058
Raef Colese8fe6cf2020-05-26 13:07:40 +01002059fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002060split_go(int loader_slot, int split_slot, void **entry)
2061{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002062 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002063 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002064 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002065 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002066 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002067 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002068
Christopher Collins92ea77f2016-12-12 15:59:26 -08002069 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2070 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002071 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002072 }
David Vinczeba3bd602019-06-17 16:01:43 +02002073 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2074 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002075
2076 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2077 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002078 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002079 assert(rc == 0);
2080 split_flash_id = flash_area_id_from_image_slot(split_slot);
2081 rc = flash_area_open(split_flash_id,
2082 &BOOT_IMG_AREA(&boot_data, split_slot));
2083 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002084
2085 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002086 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002087 if (rc != 0) {
2088 rc = SPLIT_GO_ERR;
2089 goto done;
2090 }
2091
Fabio Utzig12d59162019-11-28 10:01:59 -03002092 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002093 if (rc != 0) {
2094 goto done;
2095 }
2096
Christopher Collins92ea77f2016-12-12 15:59:26 -08002097 /* Don't check the bootable image flag because we could really call a
2098 * bootable or non-bootable image. Just validate that the image check
2099 * passes which is distinct from the normal check.
2100 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002101 FIH_CALL(split_image_check, fih_rc,
2102 boot_img_hdr(&boot_data, split_slot),
2103 BOOT_IMG_AREA(&boot_data, split_slot),
2104 boot_img_hdr(&boot_data, loader_slot),
2105 BOOT_IMG_AREA(&boot_data, loader_slot));
2106 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002107 goto done;
2108 }
2109
Marti Bolivarea088872017-06-12 17:10:49 -04002110 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002111 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002112 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002113 rc = SPLIT_GO_OK;
2114
2115done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002116 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2117 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002118 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002119
2120 if (rc) {
2121 fih_rc = fih_int_encode(rc);
2122 }
2123
2124 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002125}
David Vinczee574f2d2020-07-10 11:42:03 +02002126
Tamas Banfe031092020-09-10 17:32:39 +02002127#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002128
Mark Horvathccaf7f82021-01-04 18:16:42 +01002129#define NO_ACTIVE_SLOT UINT32_MAX
2130
David Vinczee574f2d2020-07-10 11:42:03 +02002131/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002132 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002133 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002134 * @param state Boot loader status information.
2135 * @param slot_usage Structure to fill with information about the available
2136 * slots.
David Vinczee574f2d2020-07-10 11:42:03 +02002137 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002138 * @return 0 on success; nonzero on failure.
2139 */
2140static int
2141boot_get_slot_usage(struct boot_loader_state *state,
2142 struct slot_usage_t slot_usage[])
2143{
2144 uint32_t slot;
2145 int fa_id;
2146 int rc;
2147 struct image_header *hdr = NULL;
2148
2149 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2150 /* Open all the slots */
2151 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2152 fa_id = flash_area_id_from_multi_image_slot(
2153 BOOT_CURR_IMG(state), slot);
2154 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2155 assert(rc == 0);
2156 }
2157
2158 /* Attempt to read an image header from each slot. */
2159 rc = boot_read_image_headers(state, false, NULL);
2160 if (rc != 0) {
2161 BOOT_LOG_WRN("Failed reading image headers.");
2162 return rc;
2163 }
2164
2165 /* Check headers in all slots */
2166 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2167 hdr = boot_img_hdr(state, slot);
2168
2169 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2170 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
2171 BOOT_LOG_IMAGE_INFO(slot, hdr);
2172 } else {
2173 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
2174 BOOT_LOG_INF("Image %d %s slot: Image not found",
2175 BOOT_CURR_IMG(state),
2176 (slot == BOOT_PRIMARY_SLOT)
2177 ? "Primary" : "Secondary");
2178 }
2179 }
2180
2181 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2182 }
2183
2184 return 0;
2185}
2186
2187/**
2188 * Finds the slot containing the image with the highest version number for the
2189 * current image.
2190 *
2191 * @param state Boot loader status information.
2192 * @param slot_usage Information about the active and available slots.
2193 *
2194 * @return NO_ACTIVE_SLOT if no available slot found, number of
2195 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002196 */
2197static uint32_t
Mark Horvathccaf7f82021-01-04 18:16:42 +01002198find_slot_with_highest_version(struct boot_loader_state *state,
2199 struct slot_usage_t slot_usage[])
David Vinczee574f2d2020-07-10 11:42:03 +02002200{
David Vinczee574f2d2020-07-10 11:42:03 +02002201 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002202 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2203 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002204
Mark Horvathccaf7f82021-01-04 18:16:42 +01002205 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2206 if (slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
2207 if (candidate_slot == NO_ACTIVE_SLOT) {
2208 candidate_slot = slot;
2209 } else {
2210 rc = boot_version_cmp(
2211 &boot_img_hdr(state, slot)->ih_ver,
2212 &boot_img_hdr(state, candidate_slot)->ih_ver);
2213 if (rc == 1) {
2214 /* The version of the image being examined is greater than
2215 * the version of the current candidate.
2216 */
2217 candidate_slot = slot;
2218 }
2219 }
David Vinczee574f2d2020-07-10 11:42:03 +02002220 }
2221 }
2222
Mark Horvathccaf7f82021-01-04 18:16:42 +01002223 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002224}
2225
Mark Horvathccaf7f82021-01-04 18:16:42 +01002226#ifdef MCUBOOT_HAVE_LOGGING
2227/**
2228 * Prints the state of the loaded images.
2229 *
2230 * @param state Boot loader status information.
2231 * @param slot_usage Information about the active and available slots.
2232 */
2233static void
2234print_loaded_images(struct boot_loader_state *state,
2235 struct slot_usage_t slot_usage[])
2236{
2237 uint32_t active_slot;
2238
2239 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2240 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2241
2242 BOOT_LOG_INF("Image %d loaded from the %s slot",
2243 BOOT_CURR_IMG(state),
2244 (active_slot == BOOT_PRIMARY_SLOT) ?
2245 "primary" : "secondary");
2246 }
2247}
2248#endif
2249
David Vincze505fba22020-10-22 13:53:29 +02002250#ifdef MCUBOOT_DIRECT_XIP_REVERT
2251/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002252 * Checks whether the active slot of the current image was previously selected
2253 * to run. Erases the image if it was selected but its execution failed,
2254 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002255 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002256 * @param state Boot loader status information.
2257 * @param slot_usage Information about the active and available slots.
David Vincze505fba22020-10-22 13:53:29 +02002258 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002259 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002260 */
2261static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002262boot_select_or_erase(struct boot_loader_state *state,
2263 struct slot_usage_t slot_usage[])
David Vincze505fba22020-10-22 13:53:29 +02002264{
2265 const struct flash_area *fap;
2266 int fa_id;
2267 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002268 uint32_t active_slot;
2269 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002270
Mark Horvathccaf7f82021-01-04 18:16:42 +01002271 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2272
2273 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002274 rc = flash_area_open(fa_id, &fap);
2275 assert(rc == 0);
2276
Mark Horvathccaf7f82021-01-04 18:16:42 +01002277 active_swap_state = &(slot_usage[BOOT_CURR_IMG(state)].swap_state);
2278
2279 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2280 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002281 assert(rc == 0);
2282
Mark Horvathccaf7f82021-01-04 18:16:42 +01002283 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2284 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2285 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002286 /*
2287 * A reboot happened without the image being confirmed at
2288 * runtime or its trailer is corrupted/invalid. Erase the image
2289 * to prevent it from being selected again on the next reboot.
2290 */
2291 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
2292 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2293 rc = flash_area_erase(fap, 0, fap->fa_size);
2294 assert(rc == 0);
2295
2296 flash_area_close(fap);
2297 rc = -1;
2298 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002299 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2300 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002301 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2302 "value was neither 'set' nor 'unset', but 'bad'.");
2303 }
2304 /*
2305 * Set the copy_done flag, indicating that the image has been
2306 * selected to boot. It can be set in advance, before even
2307 * validating the image, because in case the validation fails, the
2308 * entire image slot will be erased (including the trailer).
2309 */
2310 rc = boot_write_copy_done(fap);
2311 if (rc != 0) {
2312 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
2313 "the %s slot.", (slot == BOOT_PRIMARY_SLOT) ?
2314 "primary" : "secondary");
2315 rc = 0;
2316 }
2317 }
2318 flash_area_close(fap);
2319 }
2320
2321 return rc;
2322}
2323#endif /* MCUBOOT_DIRECT_XIP_REVERT */
2324
Tamas Banfe031092020-09-10 17:32:39 +02002325#ifdef MCUBOOT_RAM_LOAD
2326
Mark Horvathccaf7f82021-01-04 18:16:42 +01002327#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002328#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2329#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2330#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002331#endif
Tamas Banfe031092020-09-10 17:32:39 +02002332
2333/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002334 * Verifies that the active slot of the current image can be loaded within the
2335 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002336 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002337 * @param state Boot loader status information.
2338 * @param slot_usage Information about the active and available slots.
Tamas Banfe031092020-09-10 17:32:39 +02002339 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002340 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002341 */
2342static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002343boot_verify_ram_load_address(struct boot_loader_state *state,
2344 struct slot_usage_t slot_usage[])
Tamas Banfe031092020-09-10 17:32:39 +02002345{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002346 uint32_t img_dst;
2347 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002348 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002349 uint32_t exec_ram_start;
2350 uint32_t exec_ram_size;
2351#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2352 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002353
Mark Horvathccaf7f82021-01-04 18:16:42 +01002354 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2355 &exec_ram_size);
2356 if (rc != 0) {
2357 return BOOT_EBADSTATUS;
2358 }
2359#else
2360 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2361 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2362#endif
2363
2364 img_dst = slot_usage[BOOT_CURR_IMG(state)].img_dst;
2365 img_sz = slot_usage[BOOT_CURR_IMG(state)].img_sz;
2366
2367 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002368 return BOOT_EBADIMAGE;
2369 }
2370
2371 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2372 return BOOT_EBADIMAGE;
2373 }
2374
Mark Horvathccaf7f82021-01-04 18:16:42 +01002375 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002376 return BOOT_EBADIMAGE;
2377 }
2378
2379 return 0;
2380}
2381
2382/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002383 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002384 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002385 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002386 * @param slot The flash slot of the image to be copied to SRAM.
2387 * @param img_dst The address at which the image needs to be copied to
2388 * SRAM.
2389 * @param img_sz The size of the image that needs to be copied to SRAM.
2390 *
2391 * @return 0 on success; nonzero on failure.
2392 */
2393static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002394boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2395 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002396{
2397 int rc;
2398 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002399 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002400
Mark Horvathccaf7f82021-01-04 18:16:42 +01002401#if (BOOT_IMAGE_NUMBER == 1)
2402 (void)state;
2403#endif
2404
2405 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2406
2407 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002408 if (rc != 0) {
2409 return BOOT_EFLASH;
2410 }
2411
2412 /* Direct copy from flash to its new location in SRAM. */
2413 rc = flash_area_read(fap_src, 0, (void *)img_dst, img_sz);
2414 if (rc != 0) {
2415 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2416 }
2417
2418 flash_area_close(fap_src);
2419
2420 return rc;
2421}
2422
Mark Horvathccaf7f82021-01-04 18:16:42 +01002423#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002424/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002425 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002426 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002427 * @param start_a Start of the A region.
2428 * @param end_a End of the A region.
2429 * @param start_b Start of the B region.
2430 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002431 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002432 * @return true if there is overlap; false otherwise.
2433 */
2434static bool
2435do_regions_overlap(uint32_t start_a, uint32_t end_a,
2436 uint32_t start_b, uint32_t end_b)
2437{
2438 if (start_b > end_a) {
2439 return false;
2440 } else if (start_b >= start_a) {
2441 return true;
2442 } else if (end_b > start_a) {
2443 return true;
2444 }
2445
2446 return false;
2447}
2448
2449/**
2450 * Checks if the image we want to load to memory overlap with an already
2451 * ramloaded image.
2452 *
2453 * @param slot_usage Information about the active and available slots.
2454 * @param image_id_to_check The ID of the image we would like to load.
2455 *
2456 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002457 */
2458static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002459boot_check_ram_load_overlapping(struct slot_usage_t slot_usage[],
2460 uint32_t image_id_to_check)
Tamas Banfe031092020-09-10 17:32:39 +02002461{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002462 uint32_t i;
2463
2464 uint32_t start_a;
2465 uint32_t end_a;
2466 uint32_t start_b;
2467 uint32_t end_b;
2468
2469 start_a = slot_usage[image_id_to_check].img_dst;
2470 /* Safe to add here, values are already verified in
2471 * boot_verify_ram_load_address() */
2472 end_a = start_a + slot_usage[image_id_to_check].img_sz;
2473
2474 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
2475 if (slot_usage[i].active_slot == NO_ACTIVE_SLOT
2476 || i == image_id_to_check) {
2477 continue;
2478 }
2479
2480 start_b = slot_usage[i].img_dst;
2481 /* Safe to add here, values are already verified in
2482 * boot_verify_ram_load_address() */
2483 end_b = start_b + slot_usage[i].img_sz;
2484
2485 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2486 return -1;
2487 }
2488 }
2489
2490 return 0;
2491}
2492#endif
2493
2494/**
2495 * Loads the active slot of the current image into SRAM. The load address and
2496 * image size is extracted from the image header.
2497 *
2498 * @param state Boot loader status information.
2499 * @param slot_usage Information about the active and available slots.
2500 *
2501 * @return 0 on success; nonzero on failure.
2502 */
2503static int
2504boot_load_image_to_sram(struct boot_loader_state *state,
2505 struct slot_usage_t slot_usage[])
2506{
2507 uint32_t active_slot;
2508 struct image_header *hdr = NULL;
2509 uint32_t img_dst;
2510 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002511 int rc;
2512
Mark Horvathccaf7f82021-01-04 18:16:42 +01002513 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2514 hdr = boot_img_hdr(state, active_slot);
2515
Tamas Banfe031092020-09-10 17:32:39 +02002516 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2517
Mark Horvathccaf7f82021-01-04 18:16:42 +01002518 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002519
Mark Horvathccaf7f82021-01-04 18:16:42 +01002520 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002521 if (rc != 0) {
2522 return rc;
2523 }
2524
Mark Horvathccaf7f82021-01-04 18:16:42 +01002525 slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2526 slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
2527
2528 rc = boot_verify_ram_load_address(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002529 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002530 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002531 return rc;
2532 }
2533
Mark Horvathccaf7f82021-01-04 18:16:42 +01002534#if (BOOT_IMAGE_NUMBER > 1)
2535 rc = boot_check_ram_load_overlapping(slot_usage, BOOT_CURR_IMG(state));
2536 if (rc != 0) {
2537 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2538 another image.", img_dst);
2539 return rc;
2540 }
2541#endif
2542
Tamas Banfe031092020-09-10 17:32:39 +02002543 /* Copy image to the load address from where it currently resides in
2544 * flash.
2545 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002546 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002547 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002548 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002549 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002550 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002551 }
2552 } else {
2553 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2554 * MCUBOOT_RAM_LOAD is set.
2555 */
2556 rc = BOOT_EBADIMAGE;
2557 }
2558
Mark Horvathccaf7f82021-01-04 18:16:42 +01002559 if (rc != 0) {
2560 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2561 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2562 }
2563
Tamas Banfe031092020-09-10 17:32:39 +02002564 return rc;
2565}
2566
2567/**
2568 * Removes an image from SRAM, by overwriting it with zeros.
2569 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002570 * @param state Boot loader status information.
2571 * @param slot_usage Information about the active and available slots.
2572 *
2573 * @return 0 on success; nonzero on failure.
2574 */
2575static inline int
2576boot_remove_image_from_sram(struct boot_loader_state *state,
2577 struct slot_usage_t slot_usage[])
2578{
2579 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
2580 slot_usage[BOOT_CURR_IMG(state)].img_dst);
2581
2582 memset((void*)slot_usage[BOOT_CURR_IMG(state)].img_dst, 0,
2583 slot_usage[BOOT_CURR_IMG(state)].img_sz);
2584
2585 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2586 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2587
2588 return 0;
2589}
2590
2591/**
2592 * Removes an image from flash by erasing the corresponding flash area
2593 *
2594 * @param state Boot loader status information.
2595 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002596 *
2597 * @return 0 on success; nonzero on failure.
2598 */
2599static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002600boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002601{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002602 int area_id;
2603 int rc;
2604 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002605
Mark Horvathccaf7f82021-01-04 18:16:42 +01002606 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2607 slot);
2608 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2609 rc = flash_area_open(area_id, &fap);
2610 if (rc == 0) {
2611 flash_area_erase(fap, 0, fap->fa_size);
2612 }
2613
2614 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002615}
2616#endif /* MCUBOOT_RAM_LOAD */
2617
Mark Horvathccaf7f82021-01-04 18:16:42 +01002618#if (BOOT_IMAGE_NUMBER > 1)
2619/**
2620 * Checks the image dependency whether it is satisfied.
2621 *
2622 * @param state Boot loader status information.
2623 * @param slot_usage Information about the active and available slots.
2624 * @param dep Image dependency which has to be verified.
2625 *
2626 * @return 0 if dependencies are met; nonzero otherwise.
2627 */
2628static int
2629boot_verify_slot_dependency(struct boot_loader_state *state,
2630 struct slot_usage_t slot_usage[],
2631 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002632{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002633 struct image_version *dep_version;
2634 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002635 int rc;
2636
Mark Horvathccaf7f82021-01-04 18:16:42 +01002637 /* Determine the source of the image which is the subject of
2638 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002639 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002640 dep_slot = slot_usage[dep->image_id].active_slot;
2641 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2642
2643 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2644 if (rc >= 0) {
2645 /* Dependency satisfied. */
2646 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002647 }
2648
Mark Horvathccaf7f82021-01-04 18:16:42 +01002649 return rc;
2650}
2651
2652/**
2653 * Reads all dependency TLVs of an image and verifies one after another to see
2654 * if they are all satisfied.
2655 *
2656 * @param state Boot loader status information.
2657 * @param slot_usage Information about the active and available slots.
2658 *
2659 * @return 0 if dependencies are met; nonzero otherwise.
2660 */
2661static int
2662boot_verify_slot_dependencies(struct boot_loader_state *state,
2663 struct slot_usage_t slot_usage[])
2664{
2665 uint32_t active_slot;
2666 const struct flash_area *fap;
2667 struct image_tlv_iter it;
2668 struct image_dependency dep;
2669 uint32_t off;
2670 uint16_t len;
2671 int area_id;
2672 int rc;
2673
2674 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2675
2676 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2677 active_slot);
2678 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002679 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002680 rc = BOOT_EFLASH;
2681 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002682 }
2683
Mark Horvathccaf7f82021-01-04 18:16:42 +01002684 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2685 IMAGE_TLV_DEPENDENCY, true);
2686 if (rc != 0) {
2687 goto done;
2688 }
David Vinczee574f2d2020-07-10 11:42:03 +02002689
Mark Horvathccaf7f82021-01-04 18:16:42 +01002690 while (true) {
2691 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2692 if (rc < 0) {
2693 return -1;
2694 } else if (rc > 0) {
2695 rc = 0;
2696 break;
2697 }
David Vinczee574f2d2020-07-10 11:42:03 +02002698
Mark Horvathccaf7f82021-01-04 18:16:42 +01002699 if (len != sizeof(dep)) {
2700 rc = BOOT_EBADIMAGE;
2701 goto done;
2702 }
2703
2704 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2705 fap, off, &dep, len);
2706 if (rc != 0) {
2707 rc = BOOT_EFLASH;
2708 goto done;
2709 }
2710
2711 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2712 rc = BOOT_EBADARGS;
2713 goto done;
2714 }
2715
2716 rc = boot_verify_slot_dependency(state, slot_usage, &dep);
2717 if (rc != 0) {
2718 /* Dependency not satisfied. */
2719 goto done;
2720 }
2721 }
2722
2723done:
2724 flash_area_close(fap);
2725 return rc;
2726}
2727
2728/**
2729 * Checks the dependency of all the active slots. If an image found with
2730 * invalid or not satisfied dependencies the image is removed from SRAM (in
2731 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2732 *
2733 * @param state Boot loader status information.
2734 * @param slot_usage Information about the active and available slots.
2735 *
2736 * @return 0 if dependencies are met; nonzero otherwise.
2737 */
2738static int
2739boot_verify_dependencies(struct boot_loader_state *state,
2740 struct slot_usage_t slot_usage[])
2741{
2742 int rc = -1;
2743 uint32_t active_slot;
2744
2745 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2746 rc = boot_verify_slot_dependencies(state, slot_usage);
2747 if (rc != 0) {
2748 /* Dependencies not met or invalid dependencies. */
2749
2750#ifdef MCUBOOT_RAM_LOAD
2751 boot_remove_image_from_sram(state, slot_usage);
2752#endif /* MCUBOOT_RAM_LOAD */
2753
2754 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2755 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2756 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2757
2758 return rc;
2759 }
2760 }
2761
2762 return rc;
2763}
2764#endif /* (BOOT_IMAGE_NUMBER > 1) */
2765
2766/**
2767 * Tries to load a slot for all the images with validation.
2768 *
2769 * @param state Boot loader status information.
2770 * @param slot_usage Information about the active and available slots.
2771 *
2772 * @return 0 on success; nonzero on failure.
2773 */
2774fih_int
2775boot_load_and_validate_images(struct boot_loader_state *state,
2776 struct slot_usage_t slot_usage[])
2777{
2778 uint32_t active_slot;
2779 int rc;
2780 fih_int fih_rc;
2781
2782 /* Go over all the images and try to load one */
2783 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2784 /* All slots tried until a valid image found. Breaking from this loop
2785 * means that a valid image found or already loaded. If no slot is
2786 * found the function returns with error code. */
2787 while (true) {
2788
2789 /* Go over all the slots and try to load one */
2790 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2791 if (active_slot != NO_ACTIVE_SLOT){
2792 /* A slot is already active, go to next image. */
2793 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002794 }
David Vincze505fba22020-10-22 13:53:29 +02002795
Mark Horvathccaf7f82021-01-04 18:16:42 +01002796 active_slot = find_slot_with_highest_version(state,
2797 slot_usage);
2798 if (active_slot == NO_ACTIVE_SLOT) {
2799 BOOT_LOG_INF("No slot to load for image %d",
2800 BOOT_CURR_IMG(state));
2801 FIH_RET(FIH_FAILURE);
2802 }
2803
2804 /* Save the number of the active slot. */
2805 slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
2806
2807#ifdef MCUBOOT_DIRECT_XIP
2808 rc = boot_rom_address_check(state, slot_usage);
2809 if (rc != 0) {
2810 /* The image is placed in an unsuitable slot. */
2811 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2812 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2813 continue;
2814 }
2815#endif /* MCUBOOT_DIRECT_XIP */
George Becksteind4d90f82021-05-11 02:00:00 -04002816
David Vincze505fba22020-10-22 13:53:29 +02002817#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002818 rc = boot_select_or_erase(state, slot_usage);
David Vincze505fba22020-10-22 13:53:29 +02002819 if (rc != 0) {
2820 /* The selected image slot has been erased. */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002821 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2822 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02002823 continue;
2824 }
2825#endif /* MCUBOOT_DIRECT_XIP_REVERT */
2826
Tamas Banfe031092020-09-10 17:32:39 +02002827#ifdef MCUBOOT_RAM_LOAD
2828 /* Image is first loaded to RAM and authenticated there in order to
2829 * prevent TOCTOU attack during image copy. This could be applied
2830 * when loading images from external (untrusted) flash to internal
2831 * (trusted) RAM and image is authenticated before copying.
2832 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002833 rc = boot_load_image_to_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002834 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002835 /* Image cannot be ramloaded. */
2836 boot_remove_image_from_flash(state, active_slot);
2837 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2838 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02002839 continue;
Tamas Banfe031092020-09-10 17:32:39 +02002840 }
2841#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002842
2843 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
2844 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2845 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02002846#ifdef MCUBOOT_RAM_LOAD
Mark Horvathccaf7f82021-01-04 18:16:42 +01002847 boot_remove_image_from_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002848#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002849 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2850 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2851 continue;
David Vincze505fba22020-10-22 13:53:29 +02002852 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01002853
2854 /* Valid image loaded from a slot, go to next image. */
2855 break;
2856 }
2857 }
2858
2859 FIH_RET(FIH_SUCCESS);
2860}
2861
2862/**
2863 * Updates the security counter for the current image.
2864 *
2865 * @param state Boot loader status information.
2866 * @param active_slot Index of the slot will be loaded for current image.
2867 *
2868 * @return 0 on success; nonzero on failure.
2869 */
2870static int
2871boot_update_hw_rollback_protection(struct boot_loader_state *state,
2872 uint32_t active_slot)
2873{
2874#ifdef MCUBOOT_HW_ROLLBACK_PROT
2875 int rc;
2876
2877 /* Update the stored security counter with the newer (active) image's
2878 * security counter value.
2879 */
David Vincze505fba22020-10-22 13:53:29 +02002880#ifdef MCUBOOT_DIRECT_XIP_REVERT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002881 /* When the 'revert' mechanism is enabled in direct-xip mode, the
2882 * security counter can be increased only after reboot, if the image
2883 * has been confirmed at runtime (the image_ok flag has been set).
2884 * This way a 'revert' can be performed when it's necessary.
2885 */
2886 if (slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02002887#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002888 rc = boot_update_security_counter(BOOT_CURR_IMG(state), active_slot,
2889 boot_img_hdr(state, active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02002890 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002891 BOOT_LOG_ERR("Security counter update failed after image "
2892 "validation.");
2893 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002894 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01002895#ifdef MCUBOOT_DIRECT_XIP_REVERT
2896 }
2897#endif
David Vinczee574f2d2020-07-10 11:42:03 +02002898
Mark Horvathccaf7f82021-01-04 18:16:42 +01002899 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002900
Mark Horvathccaf7f82021-01-04 18:16:42 +01002901#else /* MCUBOOT_HW_ROLLBACK_PROT */
2902 (void) (state);
2903 (void) (active_slot);
David Vinczee574f2d2020-07-10 11:42:03 +02002904
Mark Horvathccaf7f82021-01-04 18:16:42 +01002905 return 0;
2906#endif
2907}
2908
2909fih_int
2910context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2911{
2912 struct slot_usage_t slot_usage[BOOT_IMAGE_NUMBER];
2913 int rc;
2914 fih_int fih_rc;
2915 uint32_t active_slot;
2916
2917 memset(state, 0, sizeof(struct boot_loader_state));
2918 memset(slot_usage, 0, sizeof(struct slot_usage_t) * BOOT_IMAGE_NUMBER);
2919
2920 rc = boot_get_slot_usage(state, slot_usage);
2921 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02002922 goto out;
2923 }
2924
Mark Horvathccaf7f82021-01-04 18:16:42 +01002925#if (BOOT_IMAGE_NUMBER > 1)
2926 while (true) {
2927#endif
2928 FIH_CALL(boot_load_and_validate_images, fih_rc, state, slot_usage);
2929 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
2930 goto out;
2931 }
2932
2933#if (BOOT_IMAGE_NUMBER > 1)
2934 rc = boot_verify_dependencies(state, slot_usage);
2935 if (rc != 0) {
2936 /* Dependency check failed for an image, it has been removed from
2937 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
2938 * unavailable. Try to load an image from another slot.
2939 */
2940 continue;
2941 }
2942 /* Dependency check was successful. */
2943 break;
2944 }
2945#endif
2946
2947 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2948
2949 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2950
2951 rc = boot_update_hw_rollback_protection(state, active_slot);
2952 if (rc != 0) {
2953 goto out;
2954 }
2955
2956 rc = boot_add_shared_data(state, active_slot);
2957 if (rc != 0) {
2958 goto out;
2959 }
2960 }
2961
2962 /* All image loaded successfully. */
2963#ifdef MCUBOOT_HAVE_LOGGING
2964 print_loaded_images(state, slot_usage);
2965#endif
2966
2967 fill_rsp(state, slot_usage, rsp);
2968
David Vinczee574f2d2020-07-10 11:42:03 +02002969out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002970 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002971
Mark Horvathccaf7f82021-01-04 18:16:42 +01002972 if (fih_eq(fih_rc, FIH_SUCCESS)) {
2973 fih_rc = fih_int_encode(rc);
2974 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01002975
Mark Horvathccaf7f82021-01-04 18:16:42 +01002976 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02002977}
Tamas Banfe031092020-09-10 17:32:39 +02002978#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002979
2980/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01002981 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02002982 * appropriate, and tells you what address to boot from.
2983 *
2984 * @param rsp On success, indicates how booting should occur.
2985 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01002986 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02002987 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002988fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02002989boot_go(struct boot_rsp *rsp)
2990{
Raef Colese8fe6cf2020-05-26 13:07:40 +01002991 fih_int fih_rc = FIH_FAILURE;
2992 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
2993 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02002994}