blob: 10bd5c610b007cef7183922607b4241f464fbfd9 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2016-2020 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
Mark Horvathccaf7f82021-01-04 18:16:42 +01006 * Copyright (c) 2019-2021 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28/**
29 * This file provides an interface to the boot loader. Functions defined in
30 * this file should only be called while the boot loader is running.
31 */
32
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060034#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "bootutil/bootutil.h"
Kristine Jassmann73c38c62021-02-03 16:56:14 +000039#include "bootutil/bootutil_public.h"
Christopher Collins92ea77f2016-12-12 15:59:26 -080040#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"
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020048#include "bootutil/boot_hooks.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050049
Fabio Utzigba829042018-09-18 08:29:34 -030050#ifdef MCUBOOT_ENC_IMAGES
51#include "bootutil/enc_key.h"
52#endif
53
Carlos Falgueras García391b1972021-06-21 16:58:07 +020054#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
55#include <os/os_malloc.h>
56#endif
57
Fabio Utzigba1fbe62017-07-21 14:01:20 -030058#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030059
Carlos Falgueras Garcíaa4b4b0f2021-06-22 10:00:22 +020060BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010061
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040062static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080063
Fabio Utzigabec0732019-07-31 08:40:22 -030064#if (BOOT_IMAGE_NUMBER > 1)
65#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
66#else
67#define IMAGES_ITER(x)
68#endif
69
Fabio Utzig10ee6482019-08-01 12:04:52 -030070/*
71 * This macro allows some control on the allocation of local variables.
72 * When running natively on a target, we don't want to allocated huge
73 * variables on the stack, so make them global instead. For the simulator
74 * we want to run as many threads as there are tests, and it's safer
75 * to just make those variables stack allocated.
76 */
77#if !defined(__BOOTSIM__)
78#define TARGET_STATIC static
79#else
80#define TARGET_STATIC
81#endif
82
Kristine Jassmann73c38c62021-02-03 16:56:14 +000083#if BOOT_MAX_ALIGN > 1024
84#define BUF_SZ BOOT_MAX_ALIGN
85#else
86#define BUF_SZ 1024
87#endif
88
David Vinczee574f2d2020-07-10 11:42:03 +020089static int
90boot_read_image_headers(struct boot_loader_state *state, bool require_all,
91 struct boot_status *bs)
92{
93 int rc;
94 int i;
95
96 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +020097 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
98 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
99 if (rc == BOOT_HOOK_REGULAR)
100 {
101 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
102 }
David Vinczee574f2d2020-07-10 11:42:03 +0200103 if (rc != 0) {
104 /* If `require_all` is set, fail on any single fail, otherwise
105 * if at least the first slot's header was read successfully,
106 * then the boot loader can attempt a boot.
107 *
108 * Failure to read any headers is a fatal error.
109 */
110 if (i > 0 && !require_all) {
111 return 0;
112 } else {
113 return rc;
114 }
115 }
116 }
117
118 return 0;
119}
120
Mark Horvathccaf7f82021-01-04 18:16:42 +0100121/**
122 * Saves boot status and shared data for current image.
123 *
124 * @param state Boot loader status information.
125 * @param active_slot Index of the slot will be loaded for current image.
126 *
127 * @return 0 on success; nonzero on failure.
128 */
129static int
130boot_add_shared_data(struct boot_loader_state *state,
131 uint32_t active_slot)
132{
133#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
134 int rc;
135
136#ifdef MCUBOOT_MEASURED_BOOT
137 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
138 boot_img_hdr(state, active_slot),
139 BOOT_IMG_AREA(state, active_slot));
140 if (rc != 0) {
141 BOOT_LOG_ERR("Failed to add image data to shared area");
142 return rc;
143 }
144#endif /* MCUBOOT_MEASURED_BOOT */
145
146#ifdef MCUBOOT_DATA_SHARING
147 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
148 BOOT_IMG_AREA(state, active_slot));
149 if (rc != 0) {
150 BOOT_LOG_ERR("Failed to add data to shared memory area.");
151 return rc;
152 }
153#endif /* MCUBOOT_DATA_SHARING */
154
155 return 0;
156
157#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
158 (void) (state);
159 (void) (active_slot);
160
161 return 0;
162#endif
163}
164
165/**
166 * Fills rsp to indicate how booting should occur.
167 *
168 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +0100169 * @param rsp boot_rsp struct to fill.
170 */
171static void
Raef Colesfe57e7d2021-10-15 11:07:09 +0100172fill_rsp(struct boot_loader_state *state, struct boot_rsp *rsp)
Mark Horvathccaf7f82021-01-04 18:16:42 +0100173{
174 uint32_t active_slot;
175
176#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +0100177 /* Always boot from the first enabled image. */
Mark Horvathccaf7f82021-01-04 18:16:42 +0100178 BOOT_CURR_IMG(state) = 0;
Raef Colesf11de642021-10-15 11:11:56 +0100179 IMAGES_ITER(BOOT_CURR_IMG(state)) {
180 if (!state->img_mask[BOOT_CURR_IMG(state)]) {
181 break;
182 }
183 }
Mark Horvathccaf7f82021-01-04 18:16:42 +0100184#endif
185
186#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
Raef Colesfe57e7d2021-10-15 11:07:09 +0100187 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100188#else
Mark Horvathccaf7f82021-01-04 18:16:42 +0100189 active_slot = BOOT_PRIMARY_SLOT;
190#endif
191
Dominik Ermel260ae092021-04-23 05:38:45 +0000192 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
Mark Horvathccaf7f82021-01-04 18:16:42 +0100193 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)) {
Raef Colesf11de642021-10-15 11:11:56 +0100208#if BOOT_IMAGE_NUMBER > 1
209 if (state->img_mask[BOOT_CURR_IMG(state)]) {
210 continue;
211 }
212#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +0100213#if MCUBOOT_SWAP_USING_SCRATCH
214 flash_area_close(BOOT_SCRATCH_AREA(state));
215#endif
216 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
217 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
218 }
219 }
220}
221
Tamas Banfe031092020-09-10 17:32:39 +0200222#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600223/*
224 * Compute the total size of the given image. Includes the size of
225 * the TLVs.
226 */
Tamas Banfe031092020-09-10 17:32:39 +0200227#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600228static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300229boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600230{
231 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300232 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300233 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300234 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600235 int area_id;
236 int rc;
237
Fabio Utzig10ee6482019-08-01 12:04:52 -0300238#if (BOOT_IMAGE_NUMBER == 1)
239 (void)state;
240#endif
241
242 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600243 rc = flash_area_open(area_id, &fap);
244 if (rc != 0) {
245 rc = BOOT_EFLASH;
246 goto done;
247 }
248
Fabio Utzig61fd8882019-09-14 20:00:20 -0300249 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
250
251 if (flash_area_read(fap, off, &info, sizeof(info))) {
252 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600253 goto done;
254 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300255
Fabio Utzige52c08e2019-09-11 19:32:00 -0300256 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
257 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
258 if (protect_tlv_size != info.it_tlv_tot) {
259 rc = BOOT_EBADIMAGE;
260 goto done;
261 }
262
263 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
264 rc = BOOT_EFLASH;
265 goto done;
266 }
267 } else if (protect_tlv_size != 0) {
268 rc = BOOT_EBADIMAGE;
269 goto done;
270 }
271
Fabio Utzig61fd8882019-09-14 20:00:20 -0300272 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
273 rc = BOOT_EBADIMAGE;
274 goto done;
275 }
276
Fabio Utzige52c08e2019-09-11 19:32:00 -0300277 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600278 rc = 0;
279
280done:
281 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300282 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600283}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300284#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600285
Tamas Banfe031092020-09-10 17:32:39 +0200286#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700287static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300288boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289{
David Brownab449182019-11-15 09:32:52 -0700290 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300291#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700292 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300293#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294
295 /* Figure out what size to write update status update as. The size depends
296 * on what the minimum write size is for scratch area, active image slot.
297 * We need to use the bigger of those 2 values.
298 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300299 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300300#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300301 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800302 if (align > elem_sz) {
303 elem_sz = align;
304 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300305#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800306
307 return elem_sz;
308}
309
Fabio Utzig10ee6482019-08-01 12:04:52 -0300310static int
311boot_initialize_area(struct boot_loader_state *state, int flash_area)
312{
Dominik Ermel51c8d762021-05-24 15:34:01 +0000313 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
314 boot_sector_t *out_sectors;
315 uint32_t *out_num_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300316 int rc;
317
318 num_sectors = BOOT_MAX_IMG_SECTORS;
319
320 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
321 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
322 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
323 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
324 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
325 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300326#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300327 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
328 out_sectors = state->scratch.sectors;
329 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300330#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300331 } else {
332 return BOOT_EFLASH;
333 }
334
Dominik Ermel51c8d762021-05-24 15:34:01 +0000335#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300336 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Dominik Ermel51c8d762021-05-24 15:34:01 +0000337#else
338 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
339 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
340#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300341 if (rc != 0) {
342 return rc;
343 }
344 *out_num_sectors = num_sectors;
345 return 0;
346}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300347
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348/**
349 * Determines the sector layout of both image slots and the scratch area.
350 * This information is necessary for calculating the number of bytes to erase
351 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353 */
354static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300355boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800356{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300357 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800358 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800359
Fabio Utzig10ee6482019-08-01 12:04:52 -0300360 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300361
362 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800363 if (rc != 0) {
364 return BOOT_EFLASH;
365 }
366
Fabio Utzig10ee6482019-08-01 12:04:52 -0300367 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800368 if (rc != 0) {
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +0200369 /* We need to differentiate from the primary image issue */
370 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 }
372
Fabio Utzig12d59162019-11-28 10:01:59 -0300373#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300374 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200375 if (rc != 0) {
376 return BOOT_EFLASH;
377 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300378#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200379
Fabio Utzig10ee6482019-08-01 12:04:52 -0300380 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381
382 return 0;
383}
384
Fabio Utzig12d59162019-11-28 10:01:59 -0300385void
386boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800387{
Fabio Utzig4741c452019-12-19 15:32:41 -0300388#ifdef MCUBOOT_ENC_IMAGES
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000389 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300390#if MCUBOOT_SWAP_SAVE_ENCTLV
391 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
392#endif
393#endif /* MCUBOOT_ENC_IMAGES */
394
395 bs->use_scratch = 0;
396 bs->swap_size = 0;
397 bs->source = 0;
398
Fabio Utzig74aef312019-11-28 11:05:34 -0300399 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300400 bs->idx = BOOT_STATUS_IDX_0;
401 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700402 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300403}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800404
Fabio Utzig12d59162019-11-28 10:01:59 -0300405bool
406boot_status_is_reset(const struct boot_status *bs)
407{
Fabio Utzig74aef312019-11-28 11:05:34 -0300408 return (bs->op == BOOT_STATUS_OP_MOVE &&
409 bs->idx == BOOT_STATUS_IDX_0 &&
410 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800411}
412
413/**
414 * Writes the supplied boot status to the flash file system. The boot status
415 * contains the current state of an in-progress image copy operation.
416 *
417 * @param bs The boot status to write.
418 *
419 * @return 0 on success; nonzero on failure.
420 */
421int
Fabio Utzig12d59162019-11-28 10:01:59 -0300422boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423{
424 const struct flash_area *fap;
425 uint32_t off;
426 int area_id;
Dominik Ermel9479af02021-10-19 10:06:44 +0000427 int rc = 0;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300428 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700429 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300430 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800431
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300432 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100433 * the trailer. Since in the last step the primary slot is erased, the
434 * first two status writes go to the scratch which will be copied to
435 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300436 */
437
Fabio Utzig12d59162019-11-28 10:01:59 -0300438#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300439 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440 /* Write to scratch. */
441 area_id = FLASH_AREA_IMAGE_SCRATCH;
442 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300443#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100444 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300445 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300446#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800447 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300448#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449
450 rc = flash_area_open(area_id, &fap);
451 if (rc != 0) {
Dominik Ermel9479af02021-10-19 10:06:44 +0000452 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800453 }
454
455 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300456 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200457 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300458 erased_val = flash_area_erased_val(fap);
459 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700460 buf[0] = bs->state;
461
462 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463 if (rc != 0) {
464 rc = BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800465 }
466
Christopher Collins92ea77f2016-12-12 15:59:26 -0800467 flash_area_close(fap);
Dominik Ermel9479af02021-10-19 10:06:44 +0000468
Christopher Collins92ea77f2016-12-12 15:59:26 -0800469 return rc;
470}
Tamas Banfe031092020-09-10 17:32:39 +0200471#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200472#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800473
474/*
David Vinczec3084132020-02-18 14:50:47 +0100475 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800476 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100477static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300478boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
479 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800480{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300481 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300482 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300483 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100484 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300485
Fabio Utzig10ee6482019-08-01 12:04:52 -0300486#if (BOOT_IMAGE_NUMBER == 1)
487 (void)state;
488#endif
489
Fabio Utzigba829042018-09-18 08:29:34 -0300490 (void)bs;
491 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300492
493 image_index = BOOT_CURR_IMG(state);
494
Hugo L'Hostisdb543e52021-03-09 18:00:31 +0000495/* In the case of ram loading the image has already been decrypted as it is
496 * decrypted when copied in ram */
497#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300498 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300499 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300500 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100501 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300502 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300503 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100504 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300505 }
506 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300507#endif
508
Raef Colese8fe6cf2020-05-26 13:07:40 +0100509 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
510 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300511
Raef Colese8fe6cf2020-05-26 13:07:40 +0100512 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800513}
514
Tamas Banfe031092020-09-10 17:32:39 +0200515#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100516static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800517split_image_check(struct image_header *app_hdr,
518 const struct flash_area *app_fap,
519 struct image_header *loader_hdr,
520 const struct flash_area *loader_fap)
521{
522 static void *tmpbuf;
523 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100524 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800525
526 if (!tmpbuf) {
527 tmpbuf = malloc(BOOT_TMPBUF_SZ);
528 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100529 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800530 }
531 }
532
Raef Colese8fe6cf2020-05-26 13:07:40 +0100533 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
534 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
535 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
536 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800537 }
538
Raef Colese8fe6cf2020-05-26 13:07:40 +0100539 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
540 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800541
Raef Colese8fe6cf2020-05-26 13:07:40 +0100542out:
543 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800544}
Tamas Banfe031092020-09-10 17:32:39 +0200545#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800546
Fabio Utzig338a19f2018-12-03 08:37:08 -0200547/*
David Brown9bf95af2019-10-10 15:36:36 -0600548 * Check that this is a valid header. Valid means that the magic is
549 * correct, and that the sizes/offsets are "sane". Sane means that
550 * there is no overflow on the arithmetic, and that the result fits
551 * within the flash area we are in.
552 */
553static bool
554boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
555{
556 uint32_t size;
557
558 if (hdr->ih_magic != IMAGE_MAGIC) {
559 return false;
560 }
561
562 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
563 return false;
564 }
565
Dominik Ermel260ae092021-04-23 05:38:45 +0000566 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600567 return false;
568 }
569
570 return true;
571}
572
573/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200574 * Check that a memory area consists of a given value.
575 */
576static inline bool
577boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300578{
579 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200580 uint8_t *p = (uint8_t *)data;
581 for (i = 0; i < len; i++) {
582 if (val != p[i]) {
583 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300584 }
585 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200586 return true;
587}
588
589static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300590boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200591{
592 const struct flash_area *fap;
593 struct image_header *hdr;
594 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300595 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200596 int rc;
597
Fabio Utzig10ee6482019-08-01 12:04:52 -0300598 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300599 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200600 if (rc != 0) {
601 return -1;
602 }
603
604 erased_val = flash_area_erased_val(fap);
605 flash_area_close(fap);
606
Fabio Utzig10ee6482019-08-01 12:04:52 -0300607 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200608 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
609 return -1;
610 }
611
612 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300613}
614
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000615#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200616 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200617 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000618 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
619/**
David Vincze8b0b6372020-05-20 19:54:44 +0200620 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000621 *
David Vincze8b0b6372020-05-20 19:54:44 +0200622 * @param ver1 Pointer to the first image version to compare.
623 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000624 *
David Vincze8b0b6372020-05-20 19:54:44 +0200625 * @retval -1 If ver1 is strictly less than ver2.
626 * @retval 0 If the image version numbers are equal,
627 * (not including the build number).
628 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000629 */
630static int
David Vincze8b0b6372020-05-20 19:54:44 +0200631boot_version_cmp(const struct image_version *ver1,
632 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000633{
David Vincze8b0b6372020-05-20 19:54:44 +0200634 if (ver1->iv_major > ver2->iv_major) {
635 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000636 }
David Vincze8b0b6372020-05-20 19:54:44 +0200637 if (ver1->iv_major < ver2->iv_major) {
638 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000639 }
David Vincze8b0b6372020-05-20 19:54:44 +0200640 /* The major version numbers are equal, continue comparison. */
641 if (ver1->iv_minor > ver2->iv_minor) {
642 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000643 }
David Vincze8b0b6372020-05-20 19:54:44 +0200644 if (ver1->iv_minor < ver2->iv_minor) {
645 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000646 }
David Vincze8b0b6372020-05-20 19:54:44 +0200647 /* The minor version numbers are equal, continue comparison. */
648 if (ver1->iv_revision > ver2->iv_revision) {
649 return 1;
650 }
651 if (ver1->iv_revision < ver2->iv_revision) {
652 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000653 }
654
655 return 0;
656}
657#endif
658
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000659#if defined(MCUBOOT_DIRECT_XIP)
660/**
661 * Check if image in slot has been set with specific ROM address to run from
662 * and whether the slot starts at that address.
663 *
664 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
665 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
666 * header matches the slot address;
667 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
668 * does not match the slot address.
669 */
670static bool
Raef Colesfe57e7d2021-10-15 11:07:09 +0100671boot_rom_address_check(struct boot_loader_state *state)
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000672{
Mark Horvathccaf7f82021-01-04 18:16:42 +0100673 uint32_t active_slot;
674 const struct image_header *hdr;
675 uint32_t f_off;
676
Raef Colesfe57e7d2021-10-15 11:07:09 +0100677 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +0100678 hdr = boot_img_hdr(state, active_slot);
679 f_off = boot_img_slot_off(state, active_slot);
680
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000681 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
682 BOOT_LOG_WRN("Image in %s slot at 0x%x has been built for offset 0x%x"\
Mark Horvathccaf7f82021-01-04 18:16:42 +0100683 ", skipping",
684 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000685 hdr->ih_load_addr);
686
687 /* If there is address mismatch, the image is not bootable from this
688 * slot.
689 */
690 return 1;
691 }
692 return 0;
693}
694#endif
695
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300696/*
697 * Check that there is a valid image in a slot
698 *
699 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100700 * FIH_SUCCESS if image was successfully validated
701 * 1 (or its fih_int encoded form) if no bootloable image was found
702 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300703 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100704static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300705boot_validate_slot(struct boot_loader_state *state, int slot,
706 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800707{
708 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400709 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300710 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100711 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300713
Fabio Utzig10ee6482019-08-01 12:04:52 -0300714 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300715 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800716 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100717 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718 }
719
Fabio Utzig10ee6482019-08-01 12:04:52 -0300720 hdr = boot_img_hdr(state, slot);
721 if (boot_check_header_erased(state, slot) == 0 ||
722 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300723
724#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
725 /*
726 * This fixes an issue where an image might be erased, but a trailer
727 * be left behind. It can happen if the image is in the secondary slot
728 * and did not pass validation, in which case the whole slot is erased.
729 * If during the erase operation, a reset occurs, parts of the slot
730 * might have been erased while some did not. The concerning part is
731 * the trailer because it might disable a new image from being loaded
732 * through mcumgr; so we just get rid of the trailer here, if the header
733 * is erased.
734 */
735 if (slot != BOOT_PRIMARY_SLOT) {
736 swap_erase_trailer_sectors(state, fap);
737 }
738#endif
739
David Vincze2d736ad2019-02-18 11:50:22 +0100740 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100741 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200742 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300743 }
744
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000745#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
746 if (slot != BOOT_PRIMARY_SLOT) {
747 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200748 rc = boot_version_cmp(
749 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
750 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
751 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000752 BOOT_LOG_ERR("insufficient version in secondary slot");
Dominik Ermel260ae092021-04-23 05:38:45 +0000753 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000754 /* Image in the secondary slot does not satisfy version requirement.
755 * Erase the image and continue booting from the primary slot.
756 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100757 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000758 goto out;
759 }
760 }
761#endif
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200762 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
763 fih_rc, BOOT_CURR_IMG(state), slot);
Andrzej Puzdrowski9d4d45c2021-09-14 17:25:58 +0200764 if (fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +0200765 {
766 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
767 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100768 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200769 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Dominik Ermel260ae092021-04-23 05:38:45 +0000770 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200771 /* Image is invalid, erase it to prevent further unnecessary
772 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700773 */
774 }
David Brown098de832019-12-10 11:58:01 -0700775#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100776 BOOT_LOG_ERR("Image in the %s slot is not valid!",
777 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700778#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100779 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200780 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800781 }
782
Håkon Øye Amundsene829e9d2021-11-12 14:01:01 +0000783#if MCUBOOT_IMAGE_NUMBER > 1 && !defined(MCUBOOT_ENC_IMAGES) && defined(MCUBOOT_VERIFY_IMG_ADDRESS)
784 /* Verify that the image in the secondary slot has a reset address
785 * located in the primary slot. This is done to avoid users incorrectly
786 * overwriting an application written to the incorrect slot.
787 * This feature is only supported by ARM platforms.
788 */
789 if (area_id == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
790 const struct flash_area *pri_fa = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
791 struct image_header *secondary_hdr = boot_img_hdr(state, slot);
792 uint32_t reset_value = 0;
793 uint32_t reset_addr = secondary_hdr->ih_hdr_size + sizeof(reset_value);
794
795 rc = flash_area_read(fap, reset_addr, &reset_value, sizeof(reset_value));
796 if (rc != 0) {
797 fih_rc = fih_int_encode(1);
798 goto out;
799 }
800
801 if (reset_value < pri_fa->fa_off || reset_value> (pri_fa->fa_off + pri_fa->fa_size)) {
802 BOOT_LOG_ERR("Reset address of image in secondary slot is not in the primary slot");
803 BOOT_LOG_ERR("Erasing image from secondary slot");
804
805 /* The vector table in the image located in the secondary
806 * slot does not target the primary slot. This might
807 * indicate that the image was loaded to the wrong slot.
808 *
809 * Erase the image and continue booting from the primary slot.
810 */
811 flash_area_erase(fap, 0, fap->fa_size);
812 fih_rc = fih_int_encode(1);
813 goto out;
814 }
815 }
816#endif
817
Fabio Utzig338a19f2018-12-03 08:37:08 -0200818out:
819 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100820
821 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800822}
823
David Vinczec3084132020-02-18 14:50:47 +0100824#ifdef MCUBOOT_HW_ROLLBACK_PROT
825/**
826 * Updates the stored security counter value with the image's security counter
827 * value which resides in the given slot, only if it's greater than the stored
828 * value.
829 *
830 * @param image_index Index of the image to determine which security
831 * counter to update.
832 * @param slot Slot number of the image.
833 * @param hdr Pointer to the image header structure of the image
834 * that is currently stored in the given slot.
835 *
836 * @return 0 on success; nonzero on failure.
837 */
838static int
839boot_update_security_counter(uint8_t image_index, int slot,
840 struct image_header *hdr)
841{
842 const struct flash_area *fap = NULL;
843 uint32_t img_security_cnt;
844 int rc;
845
846 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
847 &fap);
848 if (rc != 0) {
849 rc = BOOT_EFLASH;
850 goto done;
851 }
852
853 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
854 if (rc != 0) {
855 goto done;
856 }
857
858 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
859 if (rc != 0) {
860 goto done;
861 }
862
863done:
864 flash_area_close(fap);
865 return rc;
866}
867#endif /* MCUBOOT_HW_ROLLBACK_PROT */
868
Tamas Banfe031092020-09-10 17:32:39 +0200869#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200870/**
871 * Determines which swap operation to perform, if any. If it is determined
872 * that a swap operation is required, the image in the secondary slot is checked
873 * for validity. If the image in the secondary slot is invalid, it is erased,
874 * and a swap type of "none" is indicated.
875 *
876 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
877 */
878static int
879boot_validated_swap_type(struct boot_loader_state *state,
880 struct boot_status *bs)
881{
882 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100883 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200884
885 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
886 if (BOOT_IS_UPGRADE(swap_type)) {
887 /* Boot loader wants to switch to the secondary slot.
888 * Ensure image is valid.
889 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100890 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
891 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
892 if (fih_eq(fih_rc, fih_int_encode(1))) {
893 swap_type = BOOT_SWAP_TYPE_NONE;
894 } else {
895 swap_type = BOOT_SWAP_TYPE_FAIL;
896 }
David Vinczee574f2d2020-07-10 11:42:03 +0200897 }
898 }
899
900 return swap_type;
901}
David Brown94ed12c2021-05-26 16:28:14 -0600902#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200903
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800905 * Erases a region of flash.
906 *
Fabio Utzigba829042018-09-18 08:29:34 -0300907 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800908 * @param off The offset within the flash area to start the
909 * erase.
910 * @param sz The number of bytes to erase.
911 *
912 * @return 0 on success; nonzero on failure.
913 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300914int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300915boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800916{
Fabio Utzigba829042018-09-18 08:29:34 -0300917 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800918}
919
David Brown94ed12c2021-05-26 16:28:14 -0600920#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800921/**
922 * Copies the contents of one flash region to another. You must erase the
923 * destination region prior to calling this function.
924 *
925 * @param flash_area_id_src The ID of the source flash area.
926 * @param flash_area_id_dst The ID of the destination flash area.
927 * @param off_src The offset within the source flash area to
928 * copy from.
929 * @param off_dst The offset within the destination flash area to
930 * copy to.
931 * @param sz The number of bytes to copy.
932 *
933 * @return 0 on success; nonzero on failure.
934 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300935int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300936boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300937 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300938 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800939 uint32_t off_src, uint32_t off_dst, uint32_t sz)
940{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800941 uint32_t bytes_copied;
942 int chunk_sz;
943 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300944#ifdef MCUBOOT_ENC_IMAGES
945 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300946 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300947 size_t blk_off;
948 struct image_header *hdr;
949 uint16_t idx;
950 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300951 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300952#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800953
Kristine Jassmann73c38c62021-02-03 16:56:14 +0000954 TARGET_STATIC uint8_t buf[BUF_SZ] __attribute__((aligned(4)));
Fabio Utzig10ee6482019-08-01 12:04:52 -0300955
956#if !defined(MCUBOOT_ENC_IMAGES)
957 (void)state;
958#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800959
Christopher Collins92ea77f2016-12-12 15:59:26 -0800960 bytes_copied = 0;
961 while (bytes_copied < sz) {
962 if (sz - bytes_copied > sizeof buf) {
963 chunk_sz = sizeof buf;
964 } else {
965 chunk_sz = sz - bytes_copied;
966 }
967
968 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
969 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300970 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800971 }
972
Fabio Utzigba829042018-09-18 08:29:34 -0300973#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300974 image_index = BOOT_CURR_IMG(state);
Dominik Ermel260ae092021-04-23 05:38:45 +0000975 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
976 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
977 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
978 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100979 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300980 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300981#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300982 off = off_src;
Dominik Ermel260ae092021-04-23 05:38:45 +0000983 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100984 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300985 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300986 off = off_dst;
987 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300988#else
989 off = off_dst;
Dominik Ermel260ae092021-04-23 05:38:45 +0000990 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
Fabio Utzig74aef312019-11-28 11:05:34 -0300991 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
992 }
993#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200994 if (IS_ENCRYPTED(hdr)) {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +0100995 uint32_t abs_off = off + bytes_copied;
996 if (abs_off < hdr->ih_hdr_size) {
Fabio Utzigba829042018-09-18 08:29:34 -0300997 /* do not decrypt header */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +0100998 if (abs_off + chunk_sz > hdr->ih_hdr_size) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +0100999 /* The lower part of the chunk contains header data */
1000 blk_off = 0;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001001 blk_sz = chunk_sz - (hdr->ih_hdr_size - abs_off);
1002 idx = hdr->ih_hdr_size - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001003 } else {
1004 /* The chunk contains exclusively header data */
1005 blk_sz = 0; /* nothing to decrypt */
1006 }
Fabio Utzigba829042018-09-18 08:29:34 -03001007 } else {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001008 idx = 0;
1009 blk_sz = chunk_sz;
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001010 blk_off = (abs_off - hdr->ih_hdr_size) & 0xf;
Fabio Utzigba829042018-09-18 08:29:34 -03001011 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001012
1013 if (blk_sz > 0)
1014 {
1015 tlv_off = BOOT_TLV_OFF(hdr);
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001016 if (abs_off + chunk_sz > tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001017 /* do not decrypt TLVs */
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001018 if (abs_off >= tlv_off) {
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001019 blk_sz = 0;
1020 } else {
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001021 blk_sz = tlv_off - abs_off;
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001022 }
Fabio Utzigba829042018-09-18 08:29:34 -03001023 }
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001024 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Andrzej Puzdrowskifa39e3a2021-11-15 11:51:36 +01001025 (abs_off + idx) - hdr->ih_hdr_size, blk_sz,
Andrzej Puzdrowskie38b0af2021-11-04 13:34:11 +01001026 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -03001027 }
Fabio Utzigba829042018-09-18 08:29:34 -03001028 }
1029 }
1030#endif
1031
Christopher Collins92ea77f2016-12-12 15:59:26 -08001032 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1033 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001034 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001035 }
1036
1037 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001038
1039 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001040 }
1041
Fabio Utzigba829042018-09-18 08:29:34 -03001042 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001043}
1044
Christopher Collins92ea77f2016-12-12 15:59:26 -08001045/**
David Vincze2d736ad2019-02-18 11:50:22 +01001046 * Overwrite primary slot with the image contained in the secondary slot.
1047 * If a prior copy operation was interrupted by a system reset, this function
1048 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001049 *
1050 * @param bs The current boot status. This function reads
1051 * this struct to determine if it is resuming
1052 * an interrupted swap operation. This
1053 * function writes the updated status to this
1054 * function on return.
1055 *
1056 * @return 0 on success; nonzero on failure.
1057 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001058#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001059static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001060boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001061{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001062 size_t sect_count;
1063 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001064 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001065 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001066 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001067 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +01001068 const struct flash_area *fap_primary_slot;
1069 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001070 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001071
Fabio Utzigb4f88102020-10-04 10:16:24 -03001072#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1073 uint32_t sector;
1074 uint32_t trailer_sz;
1075 uint32_t off;
1076 uint32_t sz;
1077#endif
1078
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001079 (void)bs;
1080
Fabio Utzig13d9e352017-10-05 20:32:31 -03001081#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1082 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001083 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001084 assert(rc == 0);
1085#endif
David Brown17609d82017-05-05 09:41:34 -06001086
David Vincze2d736ad2019-02-18 11:50:22 +01001087 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1088 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001089
Fabio Utzigb0f04732019-07-31 09:49:19 -03001090 image_index = BOOT_CURR_IMG(state);
1091
1092 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1093 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001094 assert (rc == 0);
1095
Fabio Utzigb0f04732019-07-31 09:49:19 -03001096 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1097 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001098 assert (rc == 0);
1099
Fabio Utzig10ee6482019-08-01 12:04:52 -03001100 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001101 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001102 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001103 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001104 assert(rc == 0);
1105
Fabio Utzig13d9e352017-10-05 20:32:31 -03001106#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001107 if ((size + this_size) >= src_size) {
1108 size += src_size - size;
1109 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001110 break;
1111 }
1112#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001113
1114 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001115 }
1116
Fabio Utzigb4f88102020-10-04 10:16:24 -03001117#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1118 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1119 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1120 sz = 0;
1121 do {
1122 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1123 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1124 sector--;
1125 } while (sz < trailer_sz);
1126
1127 rc = boot_erase_region(fap_primary_slot, off, sz);
1128 assert(rc == 0);
1129#endif
1130
Fabio Utzigba829042018-09-18 08:29:34 -03001131#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001132 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001133 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001134 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001135 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001136
Fabio Utzigba829042018-09-18 08:29:34 -03001137 if (rc < 0) {
1138 return BOOT_EBADIMAGE;
1139 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001140 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001141 return BOOT_EBADIMAGE;
1142 }
1143 }
1144#endif
1145
David Vincze2d736ad2019-02-18 11:50:22 +01001146 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1147 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001148 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001149 if (rc != 0) {
1150 return rc;
1151 }
1152
1153#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1154 rc = boot_write_magic(fap_primary_slot);
1155 if (rc != 0) {
1156 return rc;
1157 }
1158#endif
David Brown17609d82017-05-05 09:41:34 -06001159
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02001160 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1161 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1162 if (rc != 0) {
1163 return rc;
1164 }
1165
David Vinczec3084132020-02-18 14:50:47 +01001166#ifdef MCUBOOT_HW_ROLLBACK_PROT
1167 /* Update the stored security counter with the new image's security counter
1168 * value. Both slots hold the new image at this point, but the secondary
1169 * slot's image header must be passed since the image headers in the
1170 * boot_data structure have not been updated yet.
1171 */
1172 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1173 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1174 if (rc != 0) {
1175 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1176 return rc;
1177 }
1178#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1179
Fabio Utzig13d9e352017-10-05 20:32:31 -03001180 /*
1181 * Erases header and trailer. The trailer is erased because when a new
1182 * image is written without a trailer as is the case when using newt, the
1183 * trailer that was left might trigger a new upgrade.
1184 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001185 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001186 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001187 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1188 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001189 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001190 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001191 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001192 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001193 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1194 last_sector),
1195 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1196 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001197 assert(rc == 0);
1198
David Vincze2d736ad2019-02-18 11:50:22 +01001199 flash_area_close(fap_primary_slot);
1200 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001201
David Vincze2d736ad2019-02-18 11:50:22 +01001202 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001203
1204 return 0;
1205}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001206#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001207
Christopher Collinsa1c12042019-05-23 14:00:28 -07001208#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001209/**
1210 * Swaps the two images in flash. If a prior copy operation was interrupted
1211 * by a system reset, this function completes that operation.
1212 *
1213 * @param bs The current boot status. This function reads
1214 * this struct to determine if it is resuming
1215 * an interrupted swap operation. This
1216 * function writes the updated status to this
1217 * function on return.
1218 *
1219 * @return 0 on success; nonzero on failure.
1220 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001221static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001222boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001223{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001224 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001225#ifdef MCUBOOT_ENC_IMAGES
1226 const struct flash_area *fap;
1227 uint8_t slot;
1228 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001229#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001230 uint32_t size;
1231 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001232 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001233 int rc;
1234
1235 /* FIXME: just do this if asked by user? */
1236
1237 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001238 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001239
Fabio Utzig12d59162019-11-28 10:01:59 -03001240 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001241 /*
1242 * No swap ever happened, so need to find the largest image which
1243 * will be used to determine the amount of sectors to swap.
1244 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001245 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001246 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001247 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001248 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001249 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001250
Fabio Utzigba829042018-09-18 08:29:34 -03001251#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001252 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001253 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001254 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001255 assert(rc >= 0);
1256
1257 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001258 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001259 assert(rc == 0);
1260 } else {
1261 rc = 0;
1262 }
1263 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001264 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001265 }
1266#endif
1267
Fabio Utzig10ee6482019-08-01 12:04:52 -03001268 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001269 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001270 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001271 assert(rc == 0);
1272 }
1273
Fabio Utzigba829042018-09-18 08:29:34 -03001274#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001275 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001276 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001277 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001278 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001279 assert(rc >= 0);
1280
1281 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001282 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001283 assert(rc == 0);
1284 } else {
1285 rc = 0;
1286 }
1287 } else {
Kristine Jassmann73c38c62021-02-03 16:56:14 +00001288 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_ALIGN_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001289 }
1290#endif
1291
Fabio Utzig46490722017-09-04 15:34:32 -03001292 if (size > copy_size) {
1293 copy_size = size;
1294 }
1295
1296 bs->swap_size = copy_size;
1297 } else {
1298 /*
1299 * If a swap was under way, the swap_size should already be present
1300 * in the trailer...
1301 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001302 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001303 assert(rc == 0);
1304
1305 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001306
1307#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001308 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1309 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001310 assert(rc == 0);
1311
1312 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001313 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001314 break;
1315 }
1316 }
1317
1318 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001319 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001320 }
1321 }
1322#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001323 }
1324
Fabio Utzig12d59162019-11-28 10:01:59 -03001325 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001326
David Vincze2d736ad2019-02-18 11:50:22 +01001327#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001328 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001329 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001330 BOOT_LOG_WRN("%d status write fails performing the swap",
1331 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001332 }
1333#endif
1334
Christopher Collins92ea77f2016-12-12 15:59:26 -08001335 return 0;
1336}
David Brown17609d82017-05-05 09:41:34 -06001337#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001338
David Vinczee32483f2019-06-13 10:46:24 +02001339#if (BOOT_IMAGE_NUMBER > 1)
1340/**
1341 * Check the image dependency whether it is satisfied and modify
1342 * the swap type if necessary.
1343 *
1344 * @param dep Image dependency which has to be verified.
1345 *
1346 * @return 0 on success; nonzero on failure.
1347 */
1348static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001349boot_verify_slot_dependency(struct boot_loader_state *state,
1350 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001351{
1352 struct image_version *dep_version;
1353 size_t dep_slot;
1354 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001355 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001356
1357 /* Determine the source of the image which is the subject of
1358 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001359 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001360 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1361 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001362 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001363
David Vincze8b0b6372020-05-20 19:54:44 +02001364 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1365 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001366 /* Dependency not satisfied.
1367 * Modify the swap type to decrease the version number of the image
1368 * (which will be located in the primary slot after the boot process),
1369 * consequently the number of unsatisfied dependencies will be
1370 * decreased or remain the same.
1371 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001372 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001373 case BOOT_SWAP_TYPE_TEST:
1374 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001375 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001376 break;
1377 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001378 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001379 break;
1380 default:
1381 break;
1382 }
David Vincze8b0b6372020-05-20 19:54:44 +02001383 } else {
1384 /* Dependency satisfied. */
1385 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001386 }
1387
1388 return rc;
1389}
1390
1391/**
1392 * Read all dependency TLVs of an image from the flash and verify
1393 * one after another to see if they are all satisfied.
1394 *
1395 * @param slot Image slot number.
1396 *
1397 * @return 0 on success; nonzero on failure.
1398 */
1399static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001400boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001401{
1402 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001403 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001404 struct image_dependency dep;
1405 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001406 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001407 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001408 int rc;
1409
Fabio Utzig10ee6482019-08-01 12:04:52 -03001410 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001411 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001412 if (rc != 0) {
1413 rc = BOOT_EFLASH;
1414 goto done;
1415 }
1416
Fabio Utzig61fd8882019-09-14 20:00:20 -03001417 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1418 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001419 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001420 goto done;
1421 }
1422
Fabio Utzig61fd8882019-09-14 20:00:20 -03001423 while (true) {
1424 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1425 if (rc < 0) {
1426 return -1;
1427 } else if (rc > 0) {
1428 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001429 break;
1430 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001431
1432 if (len != sizeof(dep)) {
1433 rc = BOOT_EBADIMAGE;
1434 goto done;
1435 }
1436
1437 rc = flash_area_read(fap, off, &dep, len);
1438 if (rc != 0) {
1439 rc = BOOT_EFLASH;
1440 goto done;
1441 }
1442
1443 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1444 rc = BOOT_EBADARGS;
1445 goto done;
1446 }
1447
1448 /* Verify dependency and modify the swap type if not satisfied. */
1449 rc = boot_verify_slot_dependency(state, &dep);
1450 if (rc != 0) {
1451 /* Dependency not satisfied. */
1452 goto done;
1453 }
David Vinczee32483f2019-06-13 10:46:24 +02001454 }
1455
1456done:
1457 flash_area_close(fap);
1458 return rc;
1459}
1460
1461/**
David Vinczee32483f2019-06-13 10:46:24 +02001462 * Iterate over all the images and verify whether the image dependencies in the
1463 * TLV area are all satisfied and update the related swap type if necessary.
1464 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001465static int
1466boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001467{
Erik Johnson49063752020-02-06 09:59:31 -06001468 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001469 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001470
Fabio Utzig10ee6482019-08-01 12:04:52 -03001471 BOOT_CURR_IMG(state) = 0;
1472 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Raef Colesf11de642021-10-15 11:11:56 +01001473 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1474 BOOT_CURR_IMG(state)++;
1475 continue;
1476 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001477 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1478 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1479 slot = BOOT_SECONDARY_SLOT;
1480 } else {
1481 slot = BOOT_PRIMARY_SLOT;
1482 }
1483
1484 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001485 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001486 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001487 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001488 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001489 /* Cannot upgrade due to non-met dependencies, so disable all
1490 * image upgrades.
1491 */
1492 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1493 BOOT_CURR_IMG(state) = idx;
1494 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1495 }
1496 break;
David Vinczee32483f2019-06-13 10:46:24 +02001497 }
1498 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001499 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001500}
1501#endif /* (BOOT_IMAGE_NUMBER > 1) */
1502
Christopher Collins92ea77f2016-12-12 15:59:26 -08001503/**
David Vinczeba3bd602019-06-17 16:01:43 +02001504 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001505 *
David Vinczeba3bd602019-06-17 16:01:43 +02001506 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001507 *
1508 * @return 0 on success; nonzero on failure.
1509 */
1510static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001511boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001512{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001513 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001514#ifndef MCUBOOT_OVERWRITE_ONLY
1515 uint8_t swap_type;
1516#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001517
David Vinczeba3bd602019-06-17 16:01:43 +02001518 /* At this point there are no aborted swaps. */
1519#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001520 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001521#elif defined(MCUBOOT_BOOTSTRAP)
1522 /* Check if the image update was triggered by a bad image in the
1523 * primary slot (the validity of the image in the secondary slot had
1524 * already been checked).
1525 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001526 fih_int fih_rc = FIH_FAILURE;
1527 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1528 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1529 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001530 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001531 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001532 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001533 }
1534#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001535 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001536#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001537 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001538
1539#ifndef MCUBOOT_OVERWRITE_ONLY
1540 /* The following state needs image_ok be explicitly set after the
1541 * swap was finished to avoid a new revert.
1542 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543 swap_type = BOOT_SWAP_TYPE(state);
1544 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1545 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001546 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001547 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001548 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001549 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001550 }
1551
David Vinczec3084132020-02-18 14:50:47 +01001552#ifdef MCUBOOT_HW_ROLLBACK_PROT
1553 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1554 /* Update the stored security counter with the new image's security
1555 * counter value. The primary slot holds the new image at this point,
1556 * but the secondary slot's image header must be passed since image
1557 * headers in the boot_data structure have not been updated yet.
1558 *
1559 * In case of a permanent image swap mcuboot will never attempt to
1560 * revert the images on the next reboot. Therefore, the security
1561 * counter must be increased right after the image upgrade.
1562 */
1563 rc = boot_update_security_counter(
1564 BOOT_CURR_IMG(state),
1565 BOOT_PRIMARY_SLOT,
1566 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1567 if (rc != 0) {
1568 BOOT_LOG_ERR("Security counter update failed after "
1569 "image upgrade.");
1570 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1571 }
1572 }
1573#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1574
Fabio Utzige575b0b2019-09-11 12:34:23 -03001575 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001576 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001577 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001578 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001579 }
David Vinczeba3bd602019-06-17 16:01:43 +02001580 }
1581#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001582
David Vinczeba3bd602019-06-17 16:01:43 +02001583 return rc;
1584}
1585
1586/**
1587 * Completes a previously aborted image swap.
1588 *
1589 * @param bs The current boot status.
1590 *
1591 * @return 0 on success; nonzero on failure.
1592 */
1593#if !defined(MCUBOOT_OVERWRITE_ONLY)
1594static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001595boot_complete_partial_swap(struct boot_loader_state *state,
1596 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001597{
1598 int rc;
1599
1600 /* Determine the type of swap operation being resumed from the
1601 * `swap-type` trailer field.
1602 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001603 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001604 assert(rc == 0);
1605
Fabio Utzig10ee6482019-08-01 12:04:52 -03001606 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001607
1608 /* The following states need image_ok be explicitly set after the
1609 * swap was finished to avoid a new revert.
1610 */
1611 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1612 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001613 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001614 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001615 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001616 }
1617 }
1618
Fabio Utzige575b0b2019-09-11 12:34:23 -03001619 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001620 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001621 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001622 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001623 }
1624 }
1625
Fabio Utzig10ee6482019-08-01 12:04:52 -03001626 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001627 BOOT_LOG_ERR("panic!");
1628 assert(0);
1629
1630 /* Loop forever... */
1631 while (1) {}
1632 }
1633
1634 return rc;
1635}
1636#endif /* !MCUBOOT_OVERWRITE_ONLY */
1637
1638#if (BOOT_IMAGE_NUMBER > 1)
1639/**
1640 * Review the validity of previously determined swap types of other images.
1641 *
1642 * @param aborted_swap The current image upgrade is a
1643 * partial/aborted swap.
1644 */
1645static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001646boot_review_image_swap_types(struct boot_loader_state *state,
1647 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001648{
1649 /* In that case if we rebooted in the middle of an image upgrade process, we
1650 * must review the validity of swap types, that were previously determined
1651 * for other images. The image_ok flag had not been set before the reboot
1652 * for any of the updated images (only the copy_done flag) and thus falsely
1653 * the REVERT swap type has been determined for the previous images that had
1654 * been updated before the reboot.
1655 *
1656 * There are two separate scenarios that we have to deal with:
1657 *
1658 * 1. The reboot has happened during swapping an image:
1659 * The current image upgrade has been determined as a
1660 * partial/aborted swap.
1661 * 2. The reboot has happened between two separate image upgrades:
1662 * In this scenario we must check the swap type of the current image.
1663 * In those cases if it is NONE or REVERT we cannot certainly determine
1664 * the fact of a reboot. In a consistent state images must move in the
1665 * same direction or stay in place, e.g. in practice REVERT and TEST
1666 * swap types cannot be present at the same time. If the swap type of
1667 * the current image is either TEST, PERM or FAIL we must review the
1668 * already determined swap types of other images and set each false
1669 * REVERT swap types to NONE (these images had been successfully
1670 * updated before the system rebooted between two separate image
1671 * upgrades).
1672 */
1673
Fabio Utzig10ee6482019-08-01 12:04:52 -03001674 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001675 /* Nothing to do */
1676 return;
1677 }
1678
1679 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001680 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1681 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001682 /* Nothing to do */
1683 return;
1684 }
1685 }
1686
Fabio Utzig10ee6482019-08-01 12:04:52 -03001687 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1688 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1689 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001690 }
1691 }
1692}
1693#endif
1694
1695/**
1696 * Prepare image to be updated if required.
1697 *
1698 * Prepare image to be updated if required with completing an image swap
1699 * operation if one was aborted and/or determining the type of the
1700 * swap operation. In case of any error set the swap type to NONE.
1701 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001702 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001703 * @param bs Pointer where the read and possibly updated
1704 * boot status can be written to.
1705 */
1706static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001707boot_prepare_image_for_update(struct boot_loader_state *state,
1708 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001709{
1710 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001711 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001712
1713 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001714 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001715 if (rc != 0) {
1716 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1717 " - too small?", BOOT_MAX_IMG_SECTORS);
1718 /* Unable to determine sector layout, continue with next image
1719 * if there is one.
1720 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001721 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Andrzej Puzdrowski54b4ad92021-06-22 13:21:22 +02001722 if (rc == BOOT_EFLASH)
1723 {
1724 /* Only return on error from the primary image flash */
1725 return;
1726 }
David Vinczeba3bd602019-06-17 16:01:43 +02001727 }
1728
1729 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001730 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001731 if (rc != 0) {
1732 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001733 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001734 BOOT_CURR_IMG(state));
1735 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001736 return;
1737 }
1738
1739 /* If the current image's slots aren't compatible, no swap is possible.
1740 * Just boot into primary slot.
1741 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001742 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001743 boot_status_reset(bs);
1744
1745#ifndef MCUBOOT_OVERWRITE_ONLY
1746 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001747 if (rc != 0) {
1748 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001749 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001750 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001751 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001752 return;
1753 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001754#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001755
Fabio Utzig74aef312019-11-28 11:05:34 -03001756#ifdef MCUBOOT_SWAP_USING_MOVE
1757 /*
1758 * Must re-read image headers because the boot status might
1759 * have been updated in the previous function call.
1760 */
1761 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Fabio Utzig32afe852020-10-04 10:36:02 -03001762#ifdef MCUBOOT_BOOTSTRAP
1763 /* When bootstrapping it's OK to not have image magic in the primary slot */
1764 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1765 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1766#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001767 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001768#endif
1769
Fabio Utzig74aef312019-11-28 11:05:34 -03001770 /* Continue with next image if there is one. */
1771 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1772 BOOT_CURR_IMG(state));
1773 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1774 return;
1775 }
1776#endif
1777
David Vinczeba3bd602019-06-17 16:01:43 +02001778 /* Determine if we rebooted in the middle of an image swap
1779 * operation. If a partial swap was detected, complete it.
1780 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001781 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001782
1783#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001784 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001785#endif
1786
1787#ifdef MCUBOOT_OVERWRITE_ONLY
1788 /* Should never arrive here, overwrite-only mode has
1789 * no swap state.
1790 */
1791 assert(0);
1792#else
1793 /* Determine the type of swap operation being resumed from the
1794 * `swap-type` trailer field.
1795 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001796 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001797 assert(rc == 0);
1798#endif
1799 /* Attempt to read an image header from each slot. Ensure that
1800 * image headers in slots are aligned with headers in boot_data.
1801 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001802 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001803 assert(rc == 0);
1804
1805 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001806 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001807 } else {
1808 /* There was no partial swap, determine swap type. */
1809 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001810 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001811 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001812 FIH_CALL(boot_validate_slot, fih_rc,
1813 state, BOOT_SECONDARY_SLOT, bs);
1814 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1815 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1816 } else {
1817 BOOT_SWAP_TYPE(state) = bs->swap_type;
1818 }
David Vinczeba3bd602019-06-17 16:01:43 +02001819 }
1820
1821#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001822 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001823#endif
1824
1825#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001826 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001827 /* Header checks are done first because they are
1828 * inexpensive. Since overwrite-only copies starting from
1829 * offset 0, if interrupted, it might leave a valid header
1830 * magic, so also run validation on the primary slot to be
1831 * sure it's not OK.
1832 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001833 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1834 FIH_CALL(boot_validate_slot, fih_rc,
1835 state, BOOT_PRIMARY_SLOT, bs);
1836
1837 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1838
Fabio Utzig3d77c952020-10-04 10:23:17 -03001839 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001840 FIH_CALL(boot_validate_slot, fih_rc,
1841 state, BOOT_SECONDARY_SLOT, bs);
1842
Fabio Utzig3d77c952020-10-04 10:23:17 -03001843 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001844 /* Set swap type to REVERT to overwrite the primary
1845 * slot with the image contained in secondary slot
1846 * and to trigger the explicit setting of the
1847 * image_ok flag.
1848 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001849 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001850 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001851 }
1852 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001853#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001854 }
David Vinczeba3bd602019-06-17 16:01:43 +02001855 } else {
1856 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001857 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001858 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001859}
1860
Mark Horvathccaf7f82021-01-04 18:16:42 +01001861/**
1862 * Updates the security counter for the current image.
1863 *
1864 * @param state Boot loader status information.
1865 *
1866 * @return 0 on success; nonzero on failure.
1867 */
1868static int
1869boot_update_hw_rollback_protection(struct boot_loader_state *state)
1870{
1871#ifdef MCUBOOT_HW_ROLLBACK_PROT
1872 int rc;
1873
1874 /* Update the stored security counter with the active image's security
1875 * counter value. It will only be updated if the new security counter is
1876 * greater than the stored value.
1877 *
1878 * In case of a successful image swapping when the swap type is TEST the
1879 * security counter can be increased only after a reset, when the swap
1880 * type is NONE and the image has marked itself "OK" (the image_ok flag
1881 * has been set). This way a "revert" can be performed when it's
1882 * necessary.
1883 */
1884 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1885 rc = boot_update_security_counter(
1886 BOOT_CURR_IMG(state),
1887 BOOT_PRIMARY_SLOT,
1888 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1889 if (rc != 0) {
1890 BOOT_LOG_ERR("Security counter update failed after image "
1891 "validation.");
1892 return rc;
1893 }
1894 }
1895
1896 return 0;
1897
1898#else /* MCUBOOT_HW_ROLLBACK_PROT */
1899 (void) (state);
1900
1901 return 0;
1902#endif
1903}
1904
Raef Colese8fe6cf2020-05-26 13:07:40 +01001905fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001906context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001907{
Marti Bolivar84898652017-06-13 17:20:22 -04001908 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001909 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001910 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001911 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001912 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001913 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001914 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001915
1916 /* The array of slot sectors are defined here (as opposed to file scope) so
1917 * that they don't get allocated for non-boot-loader apps. This is
1918 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001919 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001920 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001921 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1922 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001923#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001924 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001925#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001926
Fabio Utzig298913b2019-08-28 11:22:45 -03001927 has_upgrade = false;
1928
1929#if (BOOT_IMAGE_NUMBER == 1)
1930 (void)has_upgrade;
1931#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001932
David Vinczeba3bd602019-06-17 16:01:43 +02001933 /* Iterate over all the images. By the end of the loop the swap type has
1934 * to be determined for each image and all aborted swaps have to be
1935 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001936 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001937 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01001938#if BOOT_IMAGE_NUMBER > 1
1939 if (state->img_mask[BOOT_CURR_IMG(state)]) {
1940 continue;
1941 }
1942#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001943#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1944 /* The keys used for encryption may no longer be valid (could belong to
1945 * another images). Therefore, mark them as invalid to force their reload
1946 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001947 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001948 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001949#endif
1950
Fabio Utzig10ee6482019-08-01 12:04:52 -03001951 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001952
Fabio Utzig10ee6482019-08-01 12:04:52 -03001953 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001954 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001955 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001956 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001957#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001958 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001959#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001960
1961 /* Open primary and secondary image areas for the duration
1962 * of this call.
1963 */
1964 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001965 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001966 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001967 assert(rc == 0);
1968 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001969#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001970 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001971 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001972 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001973#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001974
1975 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001976 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001977
Fabio Utzige575b0b2019-09-11 12:34:23 -03001978 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001979 has_upgrade = true;
1980 }
David Vinczeba3bd602019-06-17 16:01:43 +02001981 }
1982
David Vinczee32483f2019-06-13 10:46:24 +02001983#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001984 if (has_upgrade) {
1985 /* Iterate over all the images and verify whether the image dependencies
1986 * are all satisfied and update swap type if necessary.
1987 */
1988 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001989 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001990 /*
1991 * It was impossible to upgrade because the expected dependency version
1992 * was not available. Here we already changed the swap_type so that
1993 * instead of asserting the bootloader, we continue and no upgrade is
1994 * performed.
1995 */
1996 rc = 0;
1997 }
1998 }
David Vinczee32483f2019-06-13 10:46:24 +02001999#endif
2000
David Vinczeba3bd602019-06-17 16:01:43 +02002001 /* Iterate over all the images. At this point there are no aborted swaps
2002 * and the swap types are determined for each image. By the end of the loop
2003 * all required update operations will have been finished.
2004 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002005 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002006#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesf11de642021-10-15 11:11:56 +01002007 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2008 continue;
2009 }
2010
David Vinczeba3bd602019-06-17 16:01:43 +02002011#ifdef MCUBOOT_ENC_IMAGES
2012 /* The keys used for encryption may no longer be valid (could belong to
2013 * another images). Therefore, mark them as invalid to force their reload
2014 * by boot_enc_load().
2015 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002016 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002017#endif /* MCUBOOT_ENC_IMAGES */
2018
2019 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002020 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002021#endif /* (BOOT_IMAGE_NUMBER > 1) */
2022
2023 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002024 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002025
Fabio Utzig10ee6482019-08-01 12:04:52 -03002026 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002027 case BOOT_SWAP_TYPE_NONE:
2028 break;
2029
2030 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2031 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2032 case BOOT_SWAP_TYPE_REVERT:
Andrzej Puzdrowskib8f39692021-07-02 15:05:37 +02002033 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2034 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2035 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2036 if (rc == BOOT_HOOK_REGULAR)
2037 {
2038 rc = boot_perform_update(state, &bs);
2039 }
David Vinczeba3bd602019-06-17 16:01:43 +02002040 assert(rc == 0);
2041 break;
2042
2043 case BOOT_SWAP_TYPE_FAIL:
2044 /* The image in secondary slot was invalid and is now erased. Ensure
2045 * we don't try to boot into it again on the next reboot. Do this by
2046 * pretending we just reverted back to primary slot.
2047 */
2048#ifndef MCUBOOT_OVERWRITE_ONLY
2049 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002050 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002051 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002052 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002053 }
2054#endif /* !MCUBOOT_OVERWRITE_ONLY */
2055 break;
2056
2057 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002058 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002059 }
2060
Fabio Utzig10ee6482019-08-01 12:04:52 -03002061 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002062 BOOT_LOG_ERR("panic!");
2063 assert(0);
2064
2065 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002066 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002067 }
2068 }
2069
2070 /* Iterate over all the images. At this point all required update operations
2071 * have finished. By the end of the loop each image in the primary slot will
2072 * have been re-validated.
2073 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002074 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002075#if BOOT_IMAGE_NUMBER > 1
2076 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2077 continue;
2078 }
2079#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -03002080 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002081 /* Attempt to read an image header from each slot. Ensure that image
2082 * headers in slots are aligned with headers in boot_data.
2083 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002084 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002085 if (rc != 0) {
2086 goto out;
2087 }
2088 /* Since headers were reloaded, it can be assumed we just performed
2089 * a swap or overwrite. Now the header info that should be used to
2090 * provide the data for the bootstrap, which previously was at
2091 * secondary slot, was updated to primary slot.
2092 */
2093 }
2094
2095#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01002096 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
2097 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002098 goto out;
2099 }
2100#else
2101 /* Even if we're not re-validating the primary slot, we could be booting
2102 * onto an empty flash chip. At least do a basic sanity check that
2103 * the magic number on the image is OK.
2104 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002105 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002106 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Dominik Ermel4a4d1ac2021-08-06 11:23:52 +00002107 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002108 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002109 rc = BOOT_EBADIMAGE;
2110 goto out;
2111 }
David Vinczec3084132020-02-18 14:50:47 +01002112#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2113
Mark Horvathccaf7f82021-01-04 18:16:42 +01002114 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002115 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002116 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002117 }
David Vincze1cf11b52020-03-24 07:51:09 +01002118
Mark Horvathccaf7f82021-01-04 18:16:42 +01002119 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002120 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002121 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002122 }
David Vinczeba3bd602019-06-17 16:01:43 +02002123 }
2124
Fabio Utzigf616c542019-12-19 15:23:32 -03002125 /*
2126 * Since the boot_status struct stores plaintext encryption keys, reset
2127 * them here to avoid the possibility of jumping into an image that could
2128 * easily recover them.
2129 */
2130 memset(&bs, 0, sizeof(struct boot_status));
2131
Raef Colesfe57e7d2021-10-15 11:07:09 +01002132 fill_rsp(state, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002133
Raef Colese8fe6cf2020-05-26 13:07:40 +01002134 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002135out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01002136 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002137
2138 if (rc) {
2139 fih_rc = fih_int_encode(rc);
2140 }
2141
2142 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002143}
2144
Raef Colese8fe6cf2020-05-26 13:07:40 +01002145fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002146split_go(int loader_slot, int split_slot, void **entry)
2147{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002148 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002149 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002150 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002151 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002152 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002153 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002154
Christopher Collins92ea77f2016-12-12 15:59:26 -08002155 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2156 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002157 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002158 }
David Vinczeba3bd602019-06-17 16:01:43 +02002159 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2160 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002161
2162 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2163 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002164 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002165 assert(rc == 0);
2166 split_flash_id = flash_area_id_from_image_slot(split_slot);
2167 rc = flash_area_open(split_flash_id,
2168 &BOOT_IMG_AREA(&boot_data, split_slot));
2169 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002170
2171 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002172 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002173 if (rc != 0) {
2174 rc = SPLIT_GO_ERR;
2175 goto done;
2176 }
2177
Fabio Utzig12d59162019-11-28 10:01:59 -03002178 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002179 if (rc != 0) {
2180 goto done;
2181 }
2182
Christopher Collins92ea77f2016-12-12 15:59:26 -08002183 /* Don't check the bootable image flag because we could really call a
2184 * bootable or non-bootable image. Just validate that the image check
2185 * passes which is distinct from the normal check.
2186 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002187 FIH_CALL(split_image_check, fih_rc,
2188 boot_img_hdr(&boot_data, split_slot),
2189 BOOT_IMG_AREA(&boot_data, split_slot),
2190 boot_img_hdr(&boot_data, loader_slot),
2191 BOOT_IMG_AREA(&boot_data, loader_slot));
2192 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002193 goto done;
2194 }
2195
Marti Bolivarea088872017-06-12 17:10:49 -04002196 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002197 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002198 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002199 rc = SPLIT_GO_OK;
2200
2201done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002202 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2203 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002204 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002205
2206 if (rc) {
2207 fih_rc = fih_int_encode(rc);
2208 }
2209
2210 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002211}
David Vinczee574f2d2020-07-10 11:42:03 +02002212
Tamas Banfe031092020-09-10 17:32:39 +02002213#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002214
Mark Horvathccaf7f82021-01-04 18:16:42 +01002215#define NO_ACTIVE_SLOT UINT32_MAX
2216
David Vinczee574f2d2020-07-10 11:42:03 +02002217/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002218 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002219 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002220 * @param state Boot loader status information.
David Vinczee574f2d2020-07-10 11:42:03 +02002221 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002222 * @return 0 on success; nonzero on failure.
2223 */
2224static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002225boot_get_slot_usage(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002226{
2227 uint32_t slot;
2228 int fa_id;
2229 int rc;
2230 struct image_header *hdr = NULL;
2231
2232 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002233#if BOOT_IMAGE_NUMBER > 1
2234 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2235 continue;
2236 }
2237#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002238 /* Open all the slots */
2239 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2240 fa_id = flash_area_id_from_multi_image_slot(
2241 BOOT_CURR_IMG(state), slot);
2242 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2243 assert(rc == 0);
2244 }
2245
2246 /* Attempt to read an image header from each slot. */
2247 rc = boot_read_image_headers(state, false, NULL);
2248 if (rc != 0) {
2249 BOOT_LOG_WRN("Failed reading image headers.");
2250 return rc;
2251 }
2252
2253 /* Check headers in all slots */
2254 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2255 hdr = boot_img_hdr(state, slot);
2256
2257 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002258 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002259 BOOT_LOG_IMAGE_INFO(slot, hdr);
2260 } else {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002261 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002262 BOOT_LOG_INF("Image %d %s slot: Image not found",
2263 BOOT_CURR_IMG(state),
2264 (slot == BOOT_PRIMARY_SLOT)
2265 ? "Primary" : "Secondary");
2266 }
2267 }
2268
Raef Colesfe57e7d2021-10-15 11:07:09 +01002269 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002270 }
2271
2272 return 0;
2273}
2274
2275/**
2276 * Finds the slot containing the image with the highest version number for the
2277 * current image.
2278 *
2279 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002280 *
2281 * @return NO_ACTIVE_SLOT if no available slot found, number of
2282 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002283 */
2284static uint32_t
Raef Colesfe57e7d2021-10-15 11:07:09 +01002285find_slot_with_highest_version(struct boot_loader_state *state)
David Vinczee574f2d2020-07-10 11:42:03 +02002286{
David Vinczee574f2d2020-07-10 11:42:03 +02002287 uint32_t slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002288 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2289 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002290
Mark Horvathccaf7f82021-01-04 18:16:42 +01002291 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002292 if (state->slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002293 if (candidate_slot == NO_ACTIVE_SLOT) {
2294 candidate_slot = slot;
2295 } else {
2296 rc = boot_version_cmp(
2297 &boot_img_hdr(state, slot)->ih_ver,
2298 &boot_img_hdr(state, candidate_slot)->ih_ver);
2299 if (rc == 1) {
2300 /* The version of the image being examined is greater than
2301 * the version of the current candidate.
2302 */
2303 candidate_slot = slot;
2304 }
2305 }
David Vinczee574f2d2020-07-10 11:42:03 +02002306 }
2307 }
2308
Mark Horvathccaf7f82021-01-04 18:16:42 +01002309 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002310}
2311
Mark Horvathccaf7f82021-01-04 18:16:42 +01002312#ifdef MCUBOOT_HAVE_LOGGING
2313/**
2314 * Prints the state of the loaded images.
2315 *
2316 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002317 */
2318static void
Raef Colesfe57e7d2021-10-15 11:07:09 +01002319print_loaded_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002320{
2321 uint32_t active_slot;
2322
David Brown695e5912021-05-24 16:58:01 -06002323 (void)state;
2324
Mark Horvathccaf7f82021-01-04 18:16:42 +01002325 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002326#if BOOT_IMAGE_NUMBER > 1
2327 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2328 continue;
2329 }
2330#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01002331 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002332
2333 BOOT_LOG_INF("Image %d loaded from the %s slot",
2334 BOOT_CURR_IMG(state),
2335 (active_slot == BOOT_PRIMARY_SLOT) ?
2336 "primary" : "secondary");
2337 }
2338}
2339#endif
2340
David Vincze1c456242021-06-29 15:25:24 +02002341#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
David Vincze505fba22020-10-22 13:53:29 +02002342/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002343 * Checks whether the active slot of the current image was previously selected
2344 * to run. Erases the image if it was selected but its execution failed,
2345 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002346 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002347 * @param state Boot loader status information.
David Vincze505fba22020-10-22 13:53:29 +02002348 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002349 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002350 */
2351static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002352boot_select_or_erase(struct boot_loader_state *state)
David Vincze505fba22020-10-22 13:53:29 +02002353{
2354 const struct flash_area *fap;
2355 int fa_id;
2356 int rc;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002357 uint32_t active_slot;
2358 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002359
Raef Colesfe57e7d2021-10-15 11:07:09 +01002360 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002361
2362 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002363 rc = flash_area_open(fa_id, &fap);
2364 assert(rc == 0);
2365
Raef Colesfe57e7d2021-10-15 11:07:09 +01002366 active_swap_state = &(state->slot_usage[BOOT_CURR_IMG(state)].swap_state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002367
2368 memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2369 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002370 assert(rc == 0);
2371
Mark Horvathccaf7f82021-01-04 18:16:42 +01002372 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2373 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2374 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002375 /*
2376 * A reboot happened without the image being confirmed at
2377 * runtime or its trailer is corrupted/invalid. Erase the image
2378 * to prevent it from being selected again on the next reboot.
2379 */
2380 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002381 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Dominik Ermel260ae092021-04-23 05:38:45 +00002382 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002383 assert(rc == 0);
2384
2385 flash_area_close(fap);
2386 rc = -1;
2387 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002388 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2389 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002390 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2391 "value was neither 'set' nor 'unset', but 'bad'.");
2392 }
2393 /*
2394 * Set the copy_done flag, indicating that the image has been
2395 * selected to boot. It can be set in advance, before even
2396 * validating the image, because in case the validation fails, the
2397 * entire image slot will be erased (including the trailer).
2398 */
2399 rc = boot_write_copy_done(fap);
2400 if (rc != 0) {
2401 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Carlos Falgueras Garcíaae13c3c2021-06-21 17:20:31 +02002402 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002403 "primary" : "secondary");
2404 rc = 0;
2405 }
2406 }
2407 flash_area_close(fap);
2408 }
2409
2410 return rc;
2411}
David Vincze1c456242021-06-29 15:25:24 +02002412#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002413
Tamas Banfe031092020-09-10 17:32:39 +02002414#ifdef MCUBOOT_RAM_LOAD
2415
Mark Horvathccaf7f82021-01-04 18:16:42 +01002416#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002417#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2418#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2419#endif
Mark Horvathccaf7f82021-01-04 18:16:42 +01002420#endif
Tamas Banfe031092020-09-10 17:32:39 +02002421
2422/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002423 * Verifies that the active slot of the current image can be loaded within the
2424 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002425 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002426 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002427 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002428 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002429 */
2430static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002431boot_verify_ram_load_address(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002432{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002433 uint32_t img_dst;
2434 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002435 uint32_t img_end_addr;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002436 uint32_t exec_ram_start;
2437 uint32_t exec_ram_size;
David Brown695e5912021-05-24 16:58:01 -06002438
2439 (void)state;
2440
Mark Horvathccaf7f82021-01-04 18:16:42 +01002441#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2442 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002443
Mark Horvathccaf7f82021-01-04 18:16:42 +01002444 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2445 &exec_ram_size);
2446 if (rc != 0) {
2447 return BOOT_EBADSTATUS;
2448 }
2449#else
2450 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2451 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2452#endif
2453
Raef Colesfe57e7d2021-10-15 11:07:09 +01002454 img_dst = state->slot_usage[BOOT_CURR_IMG(state)].img_dst;
2455 img_sz = state->slot_usage[BOOT_CURR_IMG(state)].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002456
2457 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002458 return BOOT_EBADIMAGE;
2459 }
2460
2461 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2462 return BOOT_EBADIMAGE;
2463 }
2464
Mark Horvathccaf7f82021-01-04 18:16:42 +01002465 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002466 return BOOT_EBADIMAGE;
2467 }
2468
2469 return 0;
2470}
2471
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002472#ifdef MCUBOOT_ENC_IMAGES
2473
2474/**
2475 * Copies and decrypts an image from a slot in the flash to an SRAM address.
2476 *
2477 * @param state Boot loader status information.
2478 * @param slot The flash slot of the image to be copied to SRAM.
2479 * @param hdr The image header.
2480 * @param src_sz Size of the image.
2481 * @param img_dst Pointer to the address at which the image needs to be
2482 * copied to SRAM.
2483 *
2484 * @return 0 on success; nonzero on failure.
2485 */
2486static int
2487boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2488 uint32_t slot, struct image_header *hdr,
2489 uint32_t src_sz, uint32_t img_dst)
2490{
2491 /* The flow for the decryption and copy of the image is as follows :
2492 * 1. The whole image is copied to the RAM (header + payload + TLV).
2493 * 2. The encryption key is loaded from the TLV in flash.
2494 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2495 * is 1024 bytes). Only the payload section is decrypted.
2496 * 4. The image is authenticated in RAM.
2497 */
2498 const struct flash_area *fap_src = NULL;
2499 struct boot_status bs;
2500 uint32_t blk_off;
2501 uint32_t tlv_off;
2502 uint32_t blk_sz;
2503 uint32_t bytes_copied = hdr->ih_hdr_size;
2504 uint32_t chunk_sz;
2505 uint32_t max_sz = 1024;
2506 uint16_t idx;
2507 uint8_t image_index;
2508 uint8_t * cur_dst;
2509 int area_id;
2510 int rc;
2511 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2512
2513 image_index = BOOT_CURR_IMG(state);
2514 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2515 rc = flash_area_open(area_id, &fap_src);
2516 if (rc != 0){
2517 return BOOT_EFLASH;
2518 }
2519
2520 tlv_off = BOOT_TLV_OFF(hdr);
2521
2522 /* Copying the whole image in RAM */
2523 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2524 if (rc != 0) {
2525 goto done;
2526 }
2527
2528 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2529 if (rc < 0) {
2530 goto done;
2531 }
2532
2533 /* if rc > 0 then the key has already been loaded */
2534 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2535 goto done;
2536 }
2537
2538 /* Starting at the end of the header as the header section is not encrypted */
2539 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2540 if (src_sz - bytes_copied > max_sz) {
2541 chunk_sz = max_sz;
2542 } else {
2543 chunk_sz = src_sz - bytes_copied;
2544 }
2545
2546 cur_dst = ram_dst + bytes_copied;
2547 blk_sz = chunk_sz;
2548 idx = 0;
2549 if (bytes_copied + chunk_sz > tlv_off) {
2550 /* Going over TLV section
2551 * Part of the chunk is encrypted payload */
2552 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2553 blk_sz = tlv_off - (bytes_copied);
2554 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2555 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2556 blk_off, cur_dst);
2557 } else {
2558 /* Image encrypted payload section */
2559 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2560 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2561 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2562 blk_off, cur_dst);
2563 }
2564
2565 bytes_copied += chunk_sz;
2566 }
2567 rc = 0;
2568
2569done:
2570 flash_area_close(fap_src);
2571
2572 return rc;
2573}
2574
2575#endif /* MCUBOOT_ENC_IMAGES */
Tamas Banfe031092020-09-10 17:32:39 +02002576/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002577 * Copies a slot of the current image into SRAM.
Tamas Banfe031092020-09-10 17:32:39 +02002578 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002579 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002580 * @param slot The flash slot of the image to be copied to SRAM.
2581 * @param img_dst The address at which the image needs to be copied to
2582 * SRAM.
2583 * @param img_sz The size of the image that needs to be copied to SRAM.
2584 *
2585 * @return 0 on success; nonzero on failure.
2586 */
2587static int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002588boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2589 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002590{
2591 int rc;
2592 const struct flash_area *fap_src = NULL;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002593 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002594
Mark Horvathccaf7f82021-01-04 18:16:42 +01002595#if (BOOT_IMAGE_NUMBER == 1)
2596 (void)state;
2597#endif
2598
2599 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2600
2601 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002602 if (rc != 0) {
2603 return BOOT_EFLASH;
2604 }
2605
2606 /* Direct copy from flash to its new location in SRAM. */
David Brown9bd7f902021-05-26 16:31:14 -06002607 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002608 if (rc != 0) {
2609 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2610 }
2611
2612 flash_area_close(fap_src);
2613
2614 return rc;
2615}
2616
Mark Horvathccaf7f82021-01-04 18:16:42 +01002617#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002618/**
Mark Horvathccaf7f82021-01-04 18:16:42 +01002619 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002620 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002621 * @param start_a Start of the A region.
2622 * @param end_a End of the A region.
2623 * @param start_b Start of the B region.
2624 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002625 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002626 * @return true if there is overlap; false otherwise.
2627 */
2628static bool
2629do_regions_overlap(uint32_t start_a, uint32_t end_a,
2630 uint32_t start_b, uint32_t end_b)
2631{
2632 if (start_b > end_a) {
2633 return false;
2634 } else if (start_b >= start_a) {
2635 return true;
2636 } else if (end_b > start_a) {
2637 return true;
2638 }
2639
2640 return false;
2641}
2642
2643/**
2644 * Checks if the image we want to load to memory overlap with an already
2645 * ramloaded image.
2646 *
Raef Colesfe57e7d2021-10-15 11:07:09 +01002647 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002648 *
2649 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002650 */
2651static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002652boot_check_ram_load_overlapping(struct boot_loader_state *state)
Tamas Banfe031092020-09-10 17:32:39 +02002653{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002654 uint32_t i;
2655
2656 uint32_t start_a;
2657 uint32_t end_a;
2658 uint32_t start_b;
2659 uint32_t end_b;
Raef Colesfe57e7d2021-10-15 11:07:09 +01002660 uint32_t image_id_to_check = BOOT_CURR_IMG(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002661
Raef Colesfe57e7d2021-10-15 11:07:09 +01002662 start_a = state->slot_usage[image_id_to_check].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002663 /* Safe to add here, values are already verified in
2664 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002665 end_a = start_a + state->slot_usage[image_id_to_check].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002666
2667 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002668 if (state->slot_usage[i].active_slot == NO_ACTIVE_SLOT
Mark Horvathccaf7f82021-01-04 18:16:42 +01002669 || i == image_id_to_check) {
2670 continue;
2671 }
2672
Raef Colesfe57e7d2021-10-15 11:07:09 +01002673 start_b = state->slot_usage[i].img_dst;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002674 /* Safe to add here, values are already verified in
2675 * boot_verify_ram_load_address() */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002676 end_b = start_b + state->slot_usage[i].img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002677
2678 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2679 return -1;
2680 }
2681 }
2682
2683 return 0;
2684}
2685#endif
2686
2687/**
2688 * Loads the active slot of the current image into SRAM. The load address and
2689 * image size is extracted from the image header.
2690 *
2691 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002692 *
2693 * @return 0 on success; nonzero on failure.
2694 */
2695static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002696boot_load_image_to_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002697{
2698 uint32_t active_slot;
2699 struct image_header *hdr = NULL;
2700 uint32_t img_dst;
2701 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002702 int rc;
2703
Raef Colesfe57e7d2021-10-15 11:07:09 +01002704 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002705 hdr = boot_img_hdr(state, active_slot);
2706
Tamas Banfe031092020-09-10 17:32:39 +02002707 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2708
Mark Horvathccaf7f82021-01-04 18:16:42 +01002709 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002710
Mark Horvathccaf7f82021-01-04 18:16:42 +01002711 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002712 if (rc != 0) {
2713 return rc;
2714 }
2715
Raef Colesfe57e7d2021-10-15 11:07:09 +01002716 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2717 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002718
Raef Colesfe57e7d2021-10-15 11:07:09 +01002719 rc = boot_verify_ram_load_address(state);
Tamas Banfe031092020-09-10 17:32:39 +02002720 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002721 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002722 return rc;
2723 }
2724
Mark Horvathccaf7f82021-01-04 18:16:42 +01002725#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01002726 rc = boot_check_ram_load_overlapping(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002727 if (rc != 0) {
2728 BOOT_LOG_INF("Image RAM loading to address 0x%x would overlap with\
2729 another image.", img_dst);
2730 return rc;
2731 }
2732#endif
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002733#ifdef MCUBOOT_ENC_IMAGES
2734 /* decrypt image if encrypted and copy it to RAM */
2735 if (IS_ENCRYPTED(hdr)) {
2736 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2737 } else {
2738 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2739 }
2740#else
Tamas Banfe031092020-09-10 17:32:39 +02002741 /* Copy image to the load address from where it currently resides in
2742 * flash.
2743 */
Mark Horvathccaf7f82021-01-04 18:16:42 +01002744 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
Hugo L'Hostisdb543e52021-03-09 18:00:31 +00002745#endif
Tamas Banfe031092020-09-10 17:32:39 +02002746 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002747 BOOT_LOG_INF("RAM loading to 0x%x is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002748 } else {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002749 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002750 }
2751 } else {
2752 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2753 * MCUBOOT_RAM_LOAD is set.
2754 */
2755 rc = BOOT_EBADIMAGE;
2756 }
2757
Mark Horvathccaf7f82021-01-04 18:16:42 +01002758 if (rc != 0) {
Raef Colesfe57e7d2021-10-15 11:07:09 +01002759 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2760 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002761 }
2762
Tamas Banfe031092020-09-10 17:32:39 +02002763 return rc;
2764}
2765
2766/**
2767 * Removes an image from SRAM, by overwriting it with zeros.
2768 *
Mark Horvathccaf7f82021-01-04 18:16:42 +01002769 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002770 *
2771 * @return 0 on success; nonzero on failure.
2772 */
2773static inline int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002774boot_remove_image_from_sram(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002775{
David Brown695e5912021-05-24 16:58:01 -06002776 (void)state;
2777
Mark Horvathccaf7f82021-01-04 18:16:42 +01002778 BOOT_LOG_INF("Removing image from SRAM at address 0x%x",
Raef Colesfe57e7d2021-10-15 11:07:09 +01002779 state->slot_usage[BOOT_CURR_IMG(state)].img_dst);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002780
Raef Colesfe57e7d2021-10-15 11:07:09 +01002781 memset((void*)(IMAGE_RAM_BASE + state->slot_usage[BOOT_CURR_IMG(state)].img_dst),
2782 0, state->slot_usage[BOOT_CURR_IMG(state)].img_sz);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002783
Raef Colesfe57e7d2021-10-15 11:07:09 +01002784 state->slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2785 state->slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002786
2787 return 0;
2788}
2789
2790/**
2791 * Removes an image from flash by erasing the corresponding flash area
2792 *
2793 * @param state Boot loader status information.
2794 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002795 *
2796 * @return 0 on success; nonzero on failure.
2797 */
2798static inline int
Mark Horvathccaf7f82021-01-04 18:16:42 +01002799boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002800{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002801 int area_id;
2802 int rc;
2803 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002804
David Brown695e5912021-05-24 16:58:01 -06002805 (void)state;
2806
Mark Horvathccaf7f82021-01-04 18:16:42 +01002807 BOOT_LOG_INF("Removing image %d slot %d from flash", BOOT_CURR_IMG(state),
2808 slot);
2809 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2810 rc = flash_area_open(area_id, &fap);
2811 if (rc == 0) {
Dominik Ermel260ae092021-04-23 05:38:45 +00002812 flash_area_erase(fap, 0, flash_area_get_size(fap));
Mark Horvathccaf7f82021-01-04 18:16:42 +01002813 }
2814
2815 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002816}
2817#endif /* MCUBOOT_RAM_LOAD */
2818
Mark Horvathccaf7f82021-01-04 18:16:42 +01002819#if (BOOT_IMAGE_NUMBER > 1)
2820/**
2821 * Checks the image dependency whether it is satisfied.
2822 *
2823 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002824 * @param dep Image dependency which has to be verified.
2825 *
2826 * @return 0 if dependencies are met; nonzero otherwise.
2827 */
2828static int
2829boot_verify_slot_dependency(struct boot_loader_state *state,
Mark Horvathccaf7f82021-01-04 18:16:42 +01002830 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002831{
Mark Horvathccaf7f82021-01-04 18:16:42 +01002832 struct image_version *dep_version;
2833 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002834 int rc;
2835
Mark Horvathccaf7f82021-01-04 18:16:42 +01002836 /* Determine the source of the image which is the subject of
2837 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002838 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002839 dep_slot = state->slot_usage[dep->image_id].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002840 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2841
2842 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2843 if (rc >= 0) {
2844 /* Dependency satisfied. */
2845 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002846 }
2847
Mark Horvathccaf7f82021-01-04 18:16:42 +01002848 return rc;
2849}
2850
2851/**
2852 * Reads all dependency TLVs of an image and verifies one after another to see
2853 * if they are all satisfied.
2854 *
2855 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002856 *
2857 * @return 0 if dependencies are met; nonzero otherwise.
2858 */
2859static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002860boot_verify_slot_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002861{
2862 uint32_t active_slot;
2863 const struct flash_area *fap;
2864 struct image_tlv_iter it;
2865 struct image_dependency dep;
2866 uint32_t off;
2867 uint16_t len;
2868 int area_id;
2869 int rc;
2870
Raef Colesfe57e7d2021-10-15 11:07:09 +01002871 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002872
2873 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2874 active_slot);
2875 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002876 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002877 rc = BOOT_EFLASH;
2878 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002879 }
2880
Mark Horvathccaf7f82021-01-04 18:16:42 +01002881 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2882 IMAGE_TLV_DEPENDENCY, true);
2883 if (rc != 0) {
2884 goto done;
2885 }
David Vinczee574f2d2020-07-10 11:42:03 +02002886
Mark Horvathccaf7f82021-01-04 18:16:42 +01002887 while (true) {
2888 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2889 if (rc < 0) {
2890 return -1;
2891 } else if (rc > 0) {
2892 rc = 0;
2893 break;
2894 }
David Vinczee574f2d2020-07-10 11:42:03 +02002895
Mark Horvathccaf7f82021-01-04 18:16:42 +01002896 if (len != sizeof(dep)) {
2897 rc = BOOT_EBADIMAGE;
2898 goto done;
2899 }
2900
2901 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2902 fap, off, &dep, len);
2903 if (rc != 0) {
2904 rc = BOOT_EFLASH;
2905 goto done;
2906 }
2907
2908 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
2909 rc = BOOT_EBADARGS;
2910 goto done;
2911 }
2912
Raef Colesfe57e7d2021-10-15 11:07:09 +01002913 rc = boot_verify_slot_dependency(state, &dep);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002914 if (rc != 0) {
2915 /* Dependency not satisfied. */
2916 goto done;
2917 }
2918 }
2919
2920done:
2921 flash_area_close(fap);
2922 return rc;
2923}
2924
2925/**
2926 * Checks the dependency of all the active slots. If an image found with
2927 * invalid or not satisfied dependencies the image is removed from SRAM (in
2928 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
2929 *
2930 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002931 *
2932 * @return 0 if dependencies are met; nonzero otherwise.
2933 */
2934static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002935boot_verify_dependencies(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002936{
2937 int rc = -1;
2938 uint32_t active_slot;
2939
2940 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01002941 if (state->img_mask[BOOT_CURR_IMG(state)]) {
2942 continue;
2943 }
Raef Colesfe57e7d2021-10-15 11:07:09 +01002944 rc = boot_verify_slot_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002945 if (rc != 0) {
2946 /* Dependencies not met or invalid dependencies. */
2947
2948#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01002949 boot_remove_image_from_sram(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002950#endif /* MCUBOOT_RAM_LOAD */
2951
Raef Colesfe57e7d2021-10-15 11:07:09 +01002952 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
2953 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
2954 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002955
2956 return rc;
2957 }
2958 }
2959
2960 return rc;
2961}
2962#endif /* (BOOT_IMAGE_NUMBER > 1) */
2963
2964/**
2965 * Tries to load a slot for all the images with validation.
2966 *
2967 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01002968 *
2969 * @return 0 on success; nonzero on failure.
2970 */
2971fih_int
Raef Colesfe57e7d2021-10-15 11:07:09 +01002972boot_load_and_validate_images(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01002973{
2974 uint32_t active_slot;
2975 int rc;
2976 fih_int fih_rc;
2977
2978 /* Go over all the images and try to load one */
2979 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2980 /* All slots tried until a valid image found. Breaking from this loop
2981 * means that a valid image found or already loaded. If no slot is
2982 * found the function returns with error code. */
2983 while (true) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01002984 /* Go over all the slots and try to load one */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002985 active_slot = state->slot_usage[BOOT_CURR_IMG(state)].active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01002986 if (active_slot != NO_ACTIVE_SLOT){
2987 /* A slot is already active, go to next image. */
2988 break;
David Vinczee574f2d2020-07-10 11:42:03 +02002989 }
David Vincze505fba22020-10-22 13:53:29 +02002990
Raef Colesfe57e7d2021-10-15 11:07:09 +01002991 active_slot = find_slot_with_highest_version(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01002992 if (active_slot == NO_ACTIVE_SLOT) {
2993 BOOT_LOG_INF("No slot to load for image %d",
2994 BOOT_CURR_IMG(state));
2995 FIH_RET(FIH_FAILURE);
2996 }
2997
2998 /* Save the number of the active slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01002999 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003000
Raef Colesf11de642021-10-15 11:11:56 +01003001#if BOOT_IMAGE_NUMBER > 1
3002 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3003 continue;
3004 }
3005#endif
3006
Mark Horvathccaf7f82021-01-04 18:16:42 +01003007#ifdef MCUBOOT_DIRECT_XIP
Raef Colesfe57e7d2021-10-15 11:07:09 +01003008 rc = boot_rom_address_check(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003009 if (rc != 0) {
3010 /* The image is placed in an unsuitable slot. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003011 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3012 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003013 continue;
3014 }
George Becksteind4d90f82021-05-11 02:00:00 -04003015
David Vincze505fba22020-10-22 13:53:29 +02003016#ifdef MCUBOOT_DIRECT_XIP_REVERT
Raef Colesfe57e7d2021-10-15 11:07:09 +01003017 rc = boot_select_or_erase(state);
David Vincze505fba22020-10-22 13:53:29 +02003018 if (rc != 0) {
3019 /* The selected image slot has been erased. */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003020 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3021 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003022 continue;
3023 }
3024#endif /* MCUBOOT_DIRECT_XIP_REVERT */
David Vincze1c456242021-06-29 15:25:24 +02003025#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003026
Tamas Banfe031092020-09-10 17:32:39 +02003027#ifdef MCUBOOT_RAM_LOAD
3028 /* Image is first loaded to RAM and authenticated there in order to
3029 * prevent TOCTOU attack during image copy. This could be applied
3030 * when loading images from external (untrusted) flash to internal
3031 * (trusted) RAM and image is authenticated before copying.
3032 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003033 rc = boot_load_image_to_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003034 if (rc != 0 ) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003035 /* Image cannot be ramloaded. */
3036 boot_remove_image_from_flash(state, active_slot);
Raef Colesfe57e7d2021-10-15 11:07:09 +01003037 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3038 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003039 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003040 }
3041#endif /* MCUBOOT_RAM_LOAD */
Mark Horvathccaf7f82021-01-04 18:16:42 +01003042
3043 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
3044 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3045 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003046#ifdef MCUBOOT_RAM_LOAD
Raef Colesfe57e7d2021-10-15 11:07:09 +01003047 boot_remove_image_from_sram(state);
Tamas Banfe031092020-09-10 17:32:39 +02003048#endif /* MCUBOOT_RAM_LOAD */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003049 state->slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3050 state->slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Mark Horvathccaf7f82021-01-04 18:16:42 +01003051 continue;
David Vincze505fba22020-10-22 13:53:29 +02003052 }
Mark Horvathccaf7f82021-01-04 18:16:42 +01003053
3054 /* Valid image loaded from a slot, go to next image. */
3055 break;
3056 }
3057 }
3058
3059 FIH_RET(FIH_SUCCESS);
3060}
3061
3062/**
3063 * Updates the security counter for the current image.
3064 *
3065 * @param state Boot loader status information.
Mark Horvathccaf7f82021-01-04 18:16:42 +01003066 *
3067 * @return 0 on success; nonzero on failure.
3068 */
3069static int
Raef Colesfe57e7d2021-10-15 11:07:09 +01003070boot_update_hw_rollback_protection(struct boot_loader_state *state)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003071{
3072#ifdef MCUBOOT_HW_ROLLBACK_PROT
3073 int rc;
3074
3075 /* Update the stored security counter with the newer (active) image's
3076 * security counter value.
3077 */
David Vincze1c456242021-06-29 15:25:24 +02003078#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003079 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3080 * security counter can be increased only after reboot, if the image
3081 * has been confirmed at runtime (the image_ok flag has been set).
3082 * This way a 'revert' can be performed when it's necessary.
3083 */
Raef Colesfe57e7d2021-10-15 11:07:09 +01003084 if (state->slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003085#endif
Sherry Zhang50b06ae2021-07-09 15:22:51 +08003086 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
Raef Colesfe57e7d2021-10-15 11:07:09 +01003087 state->slot_usage[BOOT_CURR_IMG(state)].active_slot,
3088 boot_img_hdr(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003089 if (rc != 0) {
Mark Horvathccaf7f82021-01-04 18:16:42 +01003090 BOOT_LOG_ERR("Security counter update failed after image "
3091 "validation.");
3092 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003093 }
David Vincze1c456242021-06-29 15:25:24 +02003094#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
Mark Horvathccaf7f82021-01-04 18:16:42 +01003095 }
3096#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003097
Mark Horvathccaf7f82021-01-04 18:16:42 +01003098 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003099
Mark Horvathccaf7f82021-01-04 18:16:42 +01003100#else /* MCUBOOT_HW_ROLLBACK_PROT */
3101 (void) (state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003102 return 0;
3103#endif
3104}
3105
3106fih_int
3107context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3108{
Mark Horvathccaf7f82021-01-04 18:16:42 +01003109 int rc;
David Brown695e5912021-05-24 16:58:01 -06003110 fih_int fih_rc = fih_int_encode(0);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003111
Raef Colesfe57e7d2021-10-15 11:07:09 +01003112 rc = boot_get_slot_usage(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003113 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003114 goto out;
3115 }
3116
Mark Horvathccaf7f82021-01-04 18:16:42 +01003117#if (BOOT_IMAGE_NUMBER > 1)
3118 while (true) {
3119#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003120 FIH_CALL(boot_load_and_validate_images, fih_rc, state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003121 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3122 goto out;
3123 }
3124
3125#if (BOOT_IMAGE_NUMBER > 1)
Raef Colesfe57e7d2021-10-15 11:07:09 +01003126 rc = boot_verify_dependencies(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003127 if (rc != 0) {
3128 /* Dependency check failed for an image, it has been removed from
3129 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3130 * unavailable. Try to load an image from another slot.
3131 */
3132 continue;
3133 }
3134 /* Dependency check was successful. */
3135 break;
3136 }
3137#endif
3138
3139 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Raef Colesf11de642021-10-15 11:11:56 +01003140#if BOOT_IMAGE_NUMBER > 1
3141 if (state->img_mask[BOOT_CURR_IMG(state)]) {
3142 continue;
3143 }
3144#endif
Raef Colesfe57e7d2021-10-15 11:07:09 +01003145 rc = boot_update_hw_rollback_protection(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003146 if (rc != 0) {
3147 goto out;
3148 }
3149
Raef Colesfe57e7d2021-10-15 11:07:09 +01003150 rc = boot_add_shared_data(state, state->slot_usage[BOOT_CURR_IMG(state)].active_slot);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003151 if (rc != 0) {
3152 goto out;
3153 }
3154 }
3155
3156 /* All image loaded successfully. */
3157#ifdef MCUBOOT_HAVE_LOGGING
Raef Colesfe57e7d2021-10-15 11:07:09 +01003158 print_loaded_images(state);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003159#endif
3160
Raef Colesfe57e7d2021-10-15 11:07:09 +01003161 fill_rsp(state, rsp);
Mark Horvathccaf7f82021-01-04 18:16:42 +01003162
David Vinczee574f2d2020-07-10 11:42:03 +02003163out:
Mark Horvathccaf7f82021-01-04 18:16:42 +01003164 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003165
Mark Horvathccaf7f82021-01-04 18:16:42 +01003166 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3167 fih_rc = fih_int_encode(rc);
3168 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003169
Mark Horvathccaf7f82021-01-04 18:16:42 +01003170 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003171}
Tamas Banfe031092020-09-10 17:32:39 +02003172#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003173
3174/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003175 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003176 * appropriate, and tells you what address to boot from.
3177 *
3178 * @param rsp On success, indicates how booting should occur.
3179 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003180 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003181 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003182fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003183boot_go(struct boot_rsp *rsp)
3184{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003185 fih_int fih_rc = FIH_FAILURE;
Raef Colesf11de642021-10-15 11:11:56 +01003186
3187 boot_state_clear(NULL);
3188
Raef Colese8fe6cf2020-05-26 13:07:40 +01003189 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3190 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003191}
Raef Colesf11de642021-10-15 11:11:56 +01003192
3193/**
3194 * Prepares the booting process, considering only a single image. This function
3195 * moves images around in flash as appropriate, and tells you what address to
3196 * boot from.
3197 *
3198 * @param rsp On success, indicates how booting should occur.
3199 *
3200 * @param image_id The image ID to prepare the boot process for.
3201 *
3202 * @return FIH_SUCCESS on success; nonzero on failure.
3203 */
3204fih_int
3205boot_go_for_image_id(struct boot_rsp *rsp, uint32_t image_id)
3206{
3207 fih_int fih_rc = FIH_FAILURE;
3208
3209 if (image_id >= BOOT_IMAGE_NUMBER) {
3210 FIH_RET(FIH_FAILURE);
3211 }
3212
3213#if BOOT_IMAGE_NUMBER > 1
3214 memset(&boot_data.img_mask, 1, BOOT_IMAGE_NUMBER);
3215 boot_data.img_mask[image_id] = 0;
3216#endif
3217
3218 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3219 FIH_RET(fih_rc);
3220}
3221
3222/**
3223 * Clears the boot state, so that previous operations have no effect on new
3224 * ones.
3225 *
3226 * @param state The state that should be cleared. If the value
3227 * is NULL, the default bootloader state will be
3228 * cleared.
3229 */
3230void boot_state_clear(struct boot_loader_state *state)
3231{
3232 if (state != NULL) {
3233 memset(state, 0, sizeof(struct boot_loader_state));
3234 } else {
3235 memset(&boot_data, 0, sizeof(struct boot_loader_state));
3236 }
3237}