blob: 7794b5a444faca55cffc1be174ff4301106b84c3 [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
Roman Okhrimenko977b3752022-03-31 14:40:48 +03006 * Copyright (c) 2019-2021 Arm Limited
David Brownaac71112020-02-03 16:13:42 -07007 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28/**
29 * This file provides an interface to the boot loader. Functions defined in
30 * this file should only be called while the boot loader is running.
31 */
32
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060034#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include "bootutil/bootutil.h"
39#include "bootutil/image.h"
40#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030041#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050042#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010043#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010044#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010045#include "bootutil/fault_injection_hardening.h"
Roman Okhrimenko977b3752022-03-31 14:40:48 +030046#include "bootutil/ramload.h"
47#include "bootutil/boot_hooks.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050048
Fabio Utzigba829042018-09-18 08:29:34 -030049#ifdef MCUBOOT_ENC_IMAGES
50#include "bootutil/enc_key.h"
51#endif
52
Roman Okhrimenko977b3752022-03-31 14:40:48 +030053#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
54#include <os/os_malloc.h>
55#endif
56
Fabio Utzigba1fbe62017-07-21 14:01:20 -030057#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030058
Roman Okhrimenko977b3752022-03-31 14:40:48 +030059BOOT_LOG_MODULE_DECLARE(mcuboot);
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010060
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040061static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080062
Fabio Utzigabec0732019-07-31 08:40:22 -030063#if (BOOT_IMAGE_NUMBER > 1)
64#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
65#else
66#define IMAGES_ITER(x)
67#endif
68
Roman Okhrimenko977b3752022-03-31 14:40:48 +030069#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
70struct slot_usage_t {
71 /* Index of the slot chosen to be loaded */
72 uint32_t active_slot;
73 bool slot_available[BOOT_NUM_SLOTS];
74#if defined(MCUBOOT_RAM_LOAD)
75 /* Image destination and size for the active slot */
76 uint32_t img_dst;
77 uint32_t img_sz;
78#elif defined(MCUBOOT_DIRECT_XIP_REVERT)
79 /* Swap status for the active slot */
80 struct boot_swap_state swap_state;
81#endif
82};
83#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
84
Fabio Utzig10ee6482019-08-01 12:04:52 -030085/*
86 * This macro allows some control on the allocation of local variables.
87 * When running natively on a target, we don't want to allocated huge
88 * variables on the stack, so make them global instead. For the simulator
89 * we want to run as many threads as there are tests, and it's safer
90 * to just make those variables stack allocated.
91 */
92#if !defined(__BOOTSIM__)
93#define TARGET_STATIC static
94#else
95#define TARGET_STATIC
96#endif
97
David Vinczee574f2d2020-07-10 11:42:03 +020098static int
99boot_read_image_headers(struct boot_loader_state *state, bool require_all,
100 struct boot_status *bs)
101{
102 int rc;
103 int i;
104
105 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300106 rc = BOOT_HOOK_CALL(boot_read_image_header_hook, BOOT_HOOK_REGULAR,
107 BOOT_CURR_IMG(state), i, boot_img_hdr(state, i));
108 if (rc == BOOT_HOOK_REGULAR)
109 {
110 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
111 }
David Vinczee574f2d2020-07-10 11:42:03 +0200112 if (rc != 0) {
113 /* If `require_all` is set, fail on any single fail, otherwise
114 * if at least the first slot's header was read successfully,
115 * then the boot loader can attempt a boot.
116 *
117 * Failure to read any headers is a fatal error.
118 */
119 if (i > 0 && !require_all) {
120 return 0;
121 } else {
122 return rc;
123 }
124 }
125 }
126
127 return 0;
128}
129
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300130/**
131 * Saves boot status and shared data for current image.
132 *
133 * @param state Boot loader status information.
134 * @param active_slot Index of the slot will be loaded for current image.
135 *
136 * @return 0 on success; nonzero on failure.
137 */
138static int
139boot_add_shared_data(struct boot_loader_state *state,
140 uint32_t active_slot)
141{
142#if defined(MCUBOOT_MEASURED_BOOT) || defined(MCUBOOT_DATA_SHARING)
143 int rc;
144
145#ifdef MCUBOOT_MEASURED_BOOT
146 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
147 boot_img_hdr(state, active_slot),
148 BOOT_IMG_AREA(state, active_slot));
149 if (rc != 0) {
150 BOOT_LOG_ERR("Failed to add image data to shared area");
151 return rc;
152 }
153#endif /* MCUBOOT_MEASURED_BOOT */
154
155#ifdef MCUBOOT_DATA_SHARING
156 rc = boot_save_shared_data(boot_img_hdr(state, active_slot),
157 BOOT_IMG_AREA(state, active_slot));
158 if (rc != 0) {
159 BOOT_LOG_ERR("Failed to add data to shared memory area.");
160 return rc;
161 }
162#endif /* MCUBOOT_DATA_SHARING */
163
164 return 0;
165
166#else /* MCUBOOT_MEASURED_BOOT || MCUBOOT_DATA_SHARING */
167 (void) (state);
168 (void) (active_slot);
169
170 return 0;
171#endif
172}
173
174/**
175 * Fills rsp to indicate how booting should occur.
176 *
177 * @param state Boot loader status information.
178 * @param slot_usage Information about the active and available slots.
179 * Only used in MCUBOOT_DIRECT_XIP and MCUBOOT_RAM_LOAD
180 * @param rsp boot_rsp struct to fill.
181 */
182static void
183fill_rsp(struct boot_loader_state *state, void *slot_usage,
184 struct boot_rsp *rsp)
185{
186 uint32_t active_slot;
187
188#if (BOOT_IMAGE_NUMBER > 1)
189 /* Always boot from Image 0. */
190 BOOT_CURR_IMG(state) = 0;
191#endif
192
193#if defined(MCUBOOT_DIRECT_XIP) || defined(MCUBOOT_RAM_LOAD)
194 active_slot = ((struct slot_usage_t *)slot_usage)[BOOT_CURR_IMG(state)].active_slot;
195#else
196 (void) (slot_usage);
197 active_slot = BOOT_PRIMARY_SLOT;
198#endif
199
200 rsp->br_flash_dev_id = flash_area_get_device_id(BOOT_IMG_AREA(state, active_slot));
201 rsp->br_image_off = boot_img_slot_off(state, active_slot);
202 rsp->br_hdr = boot_img_hdr(state, active_slot);
203}
204
205/**
206 * Closes all flash areas.
207 *
208 * @param state Boot loader status information.
209 */
210static void
211close_all_flash_areas(struct boot_loader_state *state)
212{
213 uint32_t slot;
214
215 IMAGES_ITER(BOOT_CURR_IMG(state)) {
216#if MCUBOOT_SWAP_USING_SCRATCH
217 flash_area_close(BOOT_SCRATCH_AREA(state));
218#endif
219 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
220 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
221 }
222 }
223}
224
Tamas Banfe031092020-09-10 17:32:39 +0200225#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600226/*
227 * Compute the total size of the given image. Includes the size of
228 * the TLVs.
229 */
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200230#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600231static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300232boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600233{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300234 const struct flash_area *fap = NULL;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300235 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300236 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300237 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600238 int area_id;
239 int rc;
240
Fabio Utzig10ee6482019-08-01 12:04:52 -0300241#if (BOOT_IMAGE_NUMBER == 1)
242 (void)state;
243#endif
244
245 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600246 rc = flash_area_open(area_id, &fap);
247 if (rc != 0) {
248 rc = BOOT_EFLASH;
249 goto done;
250 }
251
Fabio Utzig61fd8882019-09-14 20:00:20 -0300252 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
253
254 if (flash_area_read(fap, off, &info, sizeof(info))) {
255 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600256 goto done;
257 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300258
Fabio Utzige52c08e2019-09-11 19:32:00 -0300259 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
260 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
261 if (protect_tlv_size != info.it_tlv_tot) {
262 rc = BOOT_EBADIMAGE;
263 goto done;
264 }
265
266 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
267 rc = BOOT_EFLASH;
268 goto done;
269 }
270 } else if (protect_tlv_size != 0) {
271 rc = BOOT_EBADIMAGE;
272 goto done;
273 }
274
Fabio Utzig61fd8882019-09-14 20:00:20 -0300275 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
276 rc = BOOT_EBADIMAGE;
277 goto done;
278 }
279
Fabio Utzige52c08e2019-09-11 19:32:00 -0300280 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600281 rc = 0;
282
283done:
284 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300285 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600286}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300287#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600288
Tamas Banfe031092020-09-10 17:32:39 +0200289#if !defined(MCUBOOT_RAM_LOAD)
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200290
David Brownab449182019-11-15 09:32:52 -0700291static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300292boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800293{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300294 size_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300295#if MCUBOOT_SWAP_USING_SCRATCH
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300296 size_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300297#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800298
299 /* Figure out what size to write update status update as. The size depends
300 * on what the minimum write size is for scratch area, active image slot.
301 * We need to use the bigger of those 2 values.
302 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300303
Fabio Utzig10ee6482019-08-01 12:04:52 -0300304 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300305 assert(elem_sz != 0u);
306
Fabio Utzig12d59162019-11-28 10:01:59 -0300307#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300308 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300309 assert(align != 0u);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310 if (align > elem_sz) {
311 elem_sz = align;
312 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300313#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800314
315 return elem_sz;
316}
317
Fabio Utzig10ee6482019-08-01 12:04:52 -0300318static int
319boot_initialize_area(struct boot_loader_state *state, int flash_area)
320{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300321 uint32_t num_sectors = BOOT_MAX_IMG_SECTORS;
322 boot_sector_t *out_sectors;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300323 size_t *out_num_sectors;
324 int rc;
325
326 num_sectors = BOOT_MAX_IMG_SECTORS;
327
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300328 if (flash_area == (int) FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300329 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
330 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300331 } else if (flash_area == (int) FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300332 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
333 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300334#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300335 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
336 out_sectors = state->scratch.sectors;
337 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300338#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200339#if MCUBOOT_SWAP_USING_STATUS
340 } else if (flash_area == FLASH_AREA_IMAGE_SWAP_STATUS) {
341 out_sectors = state->status.sectors;
342 out_num_sectors = &state->status.num_sectors;
343#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300344 } else {
345 return BOOT_EFLASH;
346 }
347
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300348#ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
Fabio Utzig10ee6482019-08-01 12:04:52 -0300349 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300350#else
351 _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
352 rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
353#endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300354 if (rc != 0) {
355 return rc;
356 }
357 *out_num_sectors = num_sectors;
358 return 0;
359}
Fabio Utzig10ee6482019-08-01 12:04:52 -0300360
Christopher Collins92ea77f2016-12-12 15:59:26 -0800361/**
362 * Determines the sector layout of both image slots and the scratch area.
363 * This information is necessary for calculating the number of bytes to erase
364 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300365 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800366 */
367static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300368boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800369{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300370 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800372
Fabio Utzig10ee6482019-08-01 12:04:52 -0300373 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300374
375 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800376 if (rc != 0) {
377 return BOOT_EFLASH;
378 }
379
Fabio Utzig10ee6482019-08-01 12:04:52 -0300380 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800381 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300382 /* We need to differentiate from the primary image issue */
383 return BOOT_EFLASH_SEC;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384 }
385
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200386#if MCUBOOT_SWAP_USING_STATUS
387 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SWAP_STATUS);
388 if (rc != 0) {
389 return BOOT_EFLASH;
390 }
391#endif
392
Fabio Utzig12d59162019-11-28 10:01:59 -0300393#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300394 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200395 if (rc != 0) {
396 return BOOT_EFLASH;
397 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300398#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200399
Fabio Utzig10ee6482019-08-01 12:04:52 -0300400 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800401
402 return 0;
403}
404
Fabio Utzig12d59162019-11-28 10:01:59 -0300405void
406boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800407{
Fabio Utzig4741c452019-12-19 15:32:41 -0300408#ifdef MCUBOOT_ENC_IMAGES
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300409 (void)memset(&bs->enckey, BOOT_UNINITIALIZED_KEY_FILL,
410 BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300411#if MCUBOOT_SWAP_SAVE_ENCTLV
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300412 (void)memset(&bs->enctlv, BOOT_UNINITIALIZED_TLV_FILL,
413 BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
Fabio Utzig4741c452019-12-19 15:32:41 -0300414#endif
415#endif /* MCUBOOT_ENC_IMAGES */
416
417 bs->use_scratch = 0;
418 bs->swap_size = 0;
419 bs->source = 0;
420
Fabio Utzig74aef312019-11-28 11:05:34 -0300421 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300422 bs->idx = BOOT_STATUS_IDX_0;
423 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700424 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300425}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800426
Fabio Utzig12d59162019-11-28 10:01:59 -0300427bool
428boot_status_is_reset(const struct boot_status *bs)
429{
Fabio Utzig74aef312019-11-28 11:05:34 -0300430 return (bs->op == BOOT_STATUS_OP_MOVE &&
431 bs->idx == BOOT_STATUS_IDX_0 &&
432 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800433}
434
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200435#ifndef MCUBOOT_SWAP_USING_STATUS
Christopher Collins92ea77f2016-12-12 15:59:26 -0800436/**
437 * Writes the supplied boot status to the flash file system. The boot status
438 * contains the current state of an in-progress image copy operation.
439 *
440 * @param bs The boot status to write.
441 *
442 * @return 0 on success; nonzero on failure.
443 */
444int
Fabio Utzig12d59162019-11-28 10:01:59 -0300445boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800446{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300447 const struct flash_area *fap = NULL;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800448 uint32_t off;
449 int area_id;
450 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300451 uint8_t buf[BOOT_MAX_ALIGN];
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300452 size_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300453 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300455 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100456 * the trailer. Since in the last step the primary slot is erased, the
457 * first two status writes go to the scratch which will be copied to
458 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300459 */
460
Fabio Utzig12d59162019-11-28 10:01:59 -0300461#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300462 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800463 /* Write to scratch. */
464 area_id = FLASH_AREA_IMAGE_SCRATCH;
465 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300466#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100467 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300468 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300469#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800470 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300471#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800472
473 rc = flash_area_open(area_id, &fap);
474 if (rc != 0) {
475 rc = BOOT_EFLASH;
476 goto done;
477 }
478
479 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300480 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200481 align = flash_area_align(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300482 if (align == 0u) {
483 rc = BOOT_EFLASH;
484 goto done;
485 }
486
Fabio Utzig39000012018-07-30 12:40:20 -0300487 erased_val = flash_area_erased_val(fap);
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300488 (void)memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700489 buf[0] = bs->state;
490
491 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800492 if (rc != 0) {
493 rc = BOOT_EFLASH;
494 goto done;
495 }
496
497 rc = 0;
498
499done:
500 flash_area_close(fap);
501 return rc;
502}
Tamas Banfe031092020-09-10 17:32:39 +0200503#endif /* !MCUBOOT_RAM_LOAD */
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200504
505#endif /* MCUBOOT_SWAP_USING_STATUS */
506
David Vinczee574f2d2020-07-10 11:42:03 +0200507#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800508
509/*
David Vinczec3084132020-02-18 14:50:47 +0100510 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800511 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100512static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300513boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
514 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800515{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300516 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300517 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300518 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100519 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300520
Fabio Utzig10ee6482019-08-01 12:04:52 -0300521#if (BOOT_IMAGE_NUMBER == 1)
522 (void)state;
523#endif
524
Fabio Utzigba829042018-09-18 08:29:34 -0300525 (void)bs;
526 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300527
528 image_index = BOOT_CURR_IMG(state);
529
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300530/* In the case of ram loading the image has already been decrypted as it is
531 * decrypted when copied in ram */
532#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_RAM_LOAD)
Fabio Utzigbc077932019-08-26 11:16:34 -0300533 if (MUST_DECRYPT(fap, image_index, hdr)) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300534 rc = flash_area_id_to_multi_image_slot(image_index, fap->fa_id);
Fabio Utzigba829042018-09-18 08:29:34 -0300535 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100536 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300537 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300538 else {
539 uint8_t slot = (uint8_t)rc;
540
541 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
542 if (rc < 0) {
543 FIH_RET(fih_rc);
544 }
545 if (0 == rc && boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs)) {
546 FIH_RET(fih_rc);
547 }
Fabio Utzigba829042018-09-18 08:29:34 -0300548 }
549 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300550#endif
551
Raef Colese8fe6cf2020-05-26 13:07:40 +0100552 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
553 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300554
Raef Colese8fe6cf2020-05-26 13:07:40 +0100555 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800556}
557
Tamas Banfe031092020-09-10 17:32:39 +0200558#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100559static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560split_image_check(struct image_header *app_hdr,
561 const struct flash_area *app_fap,
562 struct image_header *loader_hdr,
563 const struct flash_area *loader_fap)
564{
565 static void *tmpbuf;
566 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100567 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568
569 if (!tmpbuf) {
570 tmpbuf = malloc(BOOT_TMPBUF_SZ);
571 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100572 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800573 }
574 }
575
Raef Colese8fe6cf2020-05-26 13:07:40 +0100576 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
577 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
578 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
579 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800580 }
581
Raef Colese8fe6cf2020-05-26 13:07:40 +0100582 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
583 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584
Raef Colese8fe6cf2020-05-26 13:07:40 +0100585out:
586 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587}
Tamas Banfe031092020-09-10 17:32:39 +0200588#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589
Fabio Utzig338a19f2018-12-03 08:37:08 -0200590/*
David Brown9bf95af2019-10-10 15:36:36 -0600591 * Check that this is a valid header. Valid means that the magic is
592 * correct, and that the sizes/offsets are "sane". Sane means that
593 * there is no overflow on the arithmetic, and that the result fits
594 * within the flash area we are in.
595 */
596static bool
597boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
598{
599 uint32_t size;
600
601 if (hdr->ih_magic != IMAGE_MAGIC) {
602 return false;
603 }
604
605 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
606 return false;
607 }
608
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300609 if (size >= flash_area_get_size(fap)) {
David Brown9bf95af2019-10-10 15:36:36 -0600610 return false;
611 }
612
613 return true;
614}
615
616/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200617 * Check that a memory area consists of a given value.
618 */
619static inline bool
620boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300621{
622 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200623 uint8_t *p = (uint8_t *)data;
624 for (i = 0; i < len; i++) {
625 if (val != p[i]) {
626 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300627 }
628 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200629 return true;
630}
631
632static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300633boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200634{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300635 const struct flash_area *fap = NULL;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200636 struct image_header *hdr;
637 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300638 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200639 int rc;
640
Fabio Utzig10ee6482019-08-01 12:04:52 -0300641 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300642 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200643 if (rc != 0) {
644 return -1;
645 }
646
647 erased_val = flash_area_erased_val(fap);
648 flash_area_close(fap);
649
Fabio Utzig10ee6482019-08-01 12:04:52 -0300650 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200651 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
652 return -1;
653 }
654
655 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300656}
657
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000658#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200659 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200660 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000661 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
662/**
David Vincze8b0b6372020-05-20 19:54:44 +0200663 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000664 *
David Vincze8b0b6372020-05-20 19:54:44 +0200665 * @param ver1 Pointer to the first image version to compare.
666 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000667 *
David Vincze8b0b6372020-05-20 19:54:44 +0200668 * @retval -1 If ver1 is strictly less than ver2.
669 * @retval 0 If the image version numbers are equal,
670 * (not including the build number).
671 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000672 */
673static int
David Vincze8b0b6372020-05-20 19:54:44 +0200674boot_version_cmp(const struct image_version *ver1,
675 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000676{
David Vincze8b0b6372020-05-20 19:54:44 +0200677 if (ver1->iv_major > ver2->iv_major) {
678 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000679 }
David Vincze8b0b6372020-05-20 19:54:44 +0200680 if (ver1->iv_major < ver2->iv_major) {
681 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000682 }
David Vincze8b0b6372020-05-20 19:54:44 +0200683 /* The major version numbers are equal, continue comparison. */
684 if (ver1->iv_minor > ver2->iv_minor) {
685 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000686 }
David Vincze8b0b6372020-05-20 19:54:44 +0200687 if (ver1->iv_minor < ver2->iv_minor) {
688 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000689 }
David Vincze8b0b6372020-05-20 19:54:44 +0200690 /* The minor version numbers are equal, continue comparison. */
691 if (ver1->iv_revision > ver2->iv_revision) {
692 return 1;
693 }
694 if (ver1->iv_revision < ver2->iv_revision) {
695 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000696 }
697
698 return 0;
699}
700#endif
701
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000702#if defined(MCUBOOT_DIRECT_XIP)
703/**
704 * Check if image in slot has been set with specific ROM address to run from
705 * and whether the slot starts at that address.
706 *
707 * @returns 0 if IMAGE_F_ROM_FIXED flag is not set;
708 * 0 if IMAGE_F_ROM_FIXED flag is set and ROM address specified in
709 * header matches the slot address;
710 * 1 if IMF_F_ROM_FIXED flag is set but ROM address specified in header
711 * does not match the slot address.
712 */
713static bool
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300714boot_rom_address_check(struct boot_loader_state *state,
715 struct slot_usage_t slot_usage[])
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000716{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300717 uint32_t active_slot;
718 const struct image_header *hdr;
719 uint32_t f_off;
720
721 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
722 hdr = boot_img_hdr(state, active_slot);
723 f_off = boot_img_slot_off(state, active_slot);
724
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000725 if (hdr->ih_flags & IMAGE_F_ROM_FIXED && hdr->ih_load_addr != f_off) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300726 BOOT_LOG_WRN("Image in %s slot at 0x%" PRIx32
727 " has been built for offset 0x%" PRIx32 ", skipping",
728 active_slot == 0 ? "primary" : "secondary", f_off,
Dominik Ermel0c8c8d52021-02-17 14:45:02 +0000729 hdr->ih_load_addr);
730
731 /* If there is address mismatch, the image is not bootable from this
732 * slot.
733 */
734 return 1;
735 }
736 return 0;
737}
738#endif
739
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300740/*
741 * Check that there is a valid image in a slot
742 *
743 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100744 * FIH_SUCCESS if image was successfully validated
745 * 1 (or its fih_int encoded form) if no bootloable image was found
746 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300747 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100748static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300749boot_validate_slot(struct boot_loader_state *state, int slot,
750 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800751{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300752 const struct flash_area *fap = NULL;
Marti Bolivarf804f622017-06-12 15:41:48 -0400753 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300754 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100755 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800756 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300757
Fabio Utzig10ee6482019-08-01 12:04:52 -0300758 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300759 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800760 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100761 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800762 }
763
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300764 BOOT_LOG_DBG("> boot_validate_slot: fa_id = %u", (unsigned)fap->fa_id);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200765
Fabio Utzig10ee6482019-08-01 12:04:52 -0300766 hdr = boot_img_hdr(state, slot);
767 if (boot_check_header_erased(state, slot) == 0 ||
768 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300769
770#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
771 /*
772 * This fixes an issue where an image might be erased, but a trailer
773 * be left behind. It can happen if the image is in the secondary slot
774 * and did not pass validation, in which case the whole slot is erased.
775 * If during the erase operation, a reset occurs, parts of the slot
776 * might have been erased while some did not. The concerning part is
777 * the trailer because it might disable a new image from being loaded
778 * through mcumgr; so we just get rid of the trailer here, if the header
779 * is erased.
780 */
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200781 BOOT_LOG_DBG(" * Fix the secondary slot when image is invalid.");
Fabio Utzig260ec452020-07-09 18:40:07 -0300782 if (slot != BOOT_PRIMARY_SLOT) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200783 BOOT_LOG_DBG(" * Erase secondary image trailer.");
Fabio Utzig260ec452020-07-09 18:40:07 -0300784 swap_erase_trailer_sectors(state, fap);
785 }
786#endif
787
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200788 BOOT_LOG_DBG(" * No bootable image in slot(%d); continue booting from the primary slot.", slot);
David Vincze2d736ad2019-02-18 11:50:22 +0100789 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100790 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200791 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300792 }
793
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000794#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
795 if (slot != BOOT_PRIMARY_SLOT) {
796 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200797 rc = boot_version_cmp(
798 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
799 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
800 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000801 BOOT_LOG_ERR("insufficient version in secondary slot");
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300802 flash_area_erase(fap, 0, flash_area_get_size(fap));
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000803 /* Image in the secondary slot does not satisfy version requirement.
804 * Erase the image and continue booting from the primary slot.
805 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100806 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000807 goto out;
808 }
809 }
810#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300811 BOOT_HOOK_CALL_FIH(boot_image_check_hook, fih_int_encode(BOOT_HOOK_REGULAR),
812 fih_rc, BOOT_CURR_IMG(state), slot);
813 if (fih_eq(fih_rc, fih_int_encode(BOOT_HOOK_REGULAR)))
814 {
815 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
816 }
Raef Colese8fe6cf2020-05-26 13:07:40 +0100817 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200818 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200819 BOOT_LOG_DBG(" * Image in the secondary slot is invalid. Erase the image");
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300820 flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vinczee574f2d2020-07-10 11:42:03 +0200821 /* Image is invalid, erase it to prevent further unnecessary
822 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700823 */
824 }
David Brown098de832019-12-10 11:58:01 -0700825#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100826 BOOT_LOG_ERR("Image in the %s slot is not valid!",
827 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700828#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100829 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200830 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800831 }
832
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200833 /* Image in the secondary slot is valid. */
834
Fabio Utzig338a19f2018-12-03 08:37:08 -0200835out:
836 flash_area_close(fap);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +0200837 BOOT_LOG_DBG("< boot_validate_slot = %d", fih_int_decode(fih_rc));
Raef Colese8fe6cf2020-05-26 13:07:40 +0100838 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839}
840
David Vinczec3084132020-02-18 14:50:47 +0100841#ifdef MCUBOOT_HW_ROLLBACK_PROT
842/**
843 * Updates the stored security counter value with the image's security counter
844 * value which resides in the given slot, only if it's greater than the stored
845 * value.
846 *
847 * @param image_index Index of the image to determine which security
848 * counter to update.
849 * @param slot Slot number of the image.
850 * @param hdr Pointer to the image header structure of the image
851 * that is currently stored in the given slot.
852 *
853 * @return 0 on success; nonzero on failure.
854 */
855static int
856boot_update_security_counter(uint8_t image_index, int slot,
857 struct image_header *hdr)
858{
859 const struct flash_area *fap = NULL;
860 uint32_t img_security_cnt;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300861 void * custom_data = NULL;
David Vinczec3084132020-02-18 14:50:47 +0100862 int rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300863#ifdef CYW20829
864 uint8_t buff[REPROV_PACK_SIZE];
865#endif /* CYW20829 */
David Vinczec3084132020-02-18 14:50:47 +0100866
867 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
868 &fap);
869 if (rc != 0) {
870 rc = BOOT_EFLASH;
871 goto done;
872 }
873
874 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
875 if (rc != 0) {
876 goto done;
877 }
878
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300879#ifdef CYW20829
880 rc = bootutil_get_img_reprov_packet(hdr, fap, buff);
881 if (rc == 0) {
882 custom_data = (void *)buff;
David Vinczec3084132020-02-18 14:50:47 +0100883 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300884#endif /* CYW20829 */
885 rc = boot_nv_security_counter_update(image_index, img_security_cnt, custom_data);
David Vinczec3084132020-02-18 14:50:47 +0100886
887done:
888 flash_area_close(fap);
889 return rc;
890}
891#endif /* MCUBOOT_HW_ROLLBACK_PROT */
892
Tamas Banfe031092020-09-10 17:32:39 +0200893#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200894/**
895 * Determines which swap operation to perform, if any. If it is determined
896 * that a swap operation is required, the image in the secondary slot is checked
897 * for validity. If the image in the secondary slot is invalid, it is erased,
898 * and a swap type of "none" is indicated.
899 *
900 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
901 */
902static int
903boot_validated_swap_type(struct boot_loader_state *state,
904 struct boot_status *bs)
905{
906 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100907 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200908
909 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
910 if (BOOT_IS_UPGRADE(swap_type)) {
911 /* Boot loader wants to switch to the secondary slot.
912 * Ensure image is valid.
913 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100914 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
915 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
916 if (fih_eq(fih_rc, fih_int_encode(1))) {
917 swap_type = BOOT_SWAP_TYPE_NONE;
918 } else {
919 swap_type = BOOT_SWAP_TYPE_FAIL;
920 }
David Vinczee574f2d2020-07-10 11:42:03 +0200921 }
922 }
923
924 return swap_type;
925}
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300926#endif
David Vinczee574f2d2020-07-10 11:42:03 +0200927
Christopher Collins92ea77f2016-12-12 15:59:26 -0800928/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800929 * Erases a region of flash.
930 *
Fabio Utzigba829042018-09-18 08:29:34 -0300931 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800932 * @param off The offset within the flash area to start the
933 * erase.
934 * @param sz The number of bytes to erase.
935 *
936 * @return 0 on success; nonzero on failure.
937 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300938int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300939boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800940{
Fabio Utzigba829042018-09-18 08:29:34 -0300941 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800942}
943
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300944#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800945/**
946 * Copies the contents of one flash region to another. You must erase the
947 * destination region prior to calling this function.
948 *
949 * @param flash_area_id_src The ID of the source flash area.
950 * @param flash_area_id_dst The ID of the destination flash area.
951 * @param off_src The offset within the source flash area to
952 * copy from.
953 * @param off_dst The offset within the destination flash area to
954 * copy to.
955 * @param sz The number of bytes to copy.
956 *
957 * @return 0 on success; nonzero on failure.
958 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300959int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300960boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300961 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300962 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800963 uint32_t off_src, uint32_t off_dst, uint32_t sz)
964{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800965 uint32_t bytes_copied;
966 int chunk_sz;
967 int rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300968#if defined (MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_ENC_IMAGES_XIP)
Fabio Utzigba829042018-09-18 08:29:34 -0300969 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300970 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300971 size_t blk_off;
972 struct image_header *hdr;
973 uint16_t idx;
974 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300975 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300976#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800977
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300978/* NOTE:
979 * Default value 1024 is not suitable for platforms with larger erase size.
980 * Give user ability to define platform tolerant chunk size. In most cases
981 * it would be flash erase alignment.
982 */
983#ifdef MCUBOOT_PLATFORM_CHUNK_SIZE
984 #define MCUBOOT_CHUNK_SIZE MCUBOOT_PLATFORM_CHUNK_SIZE
985#else
986 #define MCUBOOT_CHUNK_SIZE 1024
987#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300988
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300989 TARGET_STATIC uint8_t buf[MCUBOOT_CHUNK_SIZE] __attribute__((aligned(4)));
990
991#if !defined(MCUBOOT_ENC_IMAGES) || defined(MCUBOOT_ENC_IMAGES_XIP)
Fabio Utzig10ee6482019-08-01 12:04:52 -0300992 (void)state;
993#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800994
Christopher Collins92ea77f2016-12-12 15:59:26 -0800995 bytes_copied = 0;
996 while (bytes_copied < sz) {
997 if (sz - bytes_copied > sizeof buf) {
998 chunk_sz = sizeof buf;
999 } else {
1000 chunk_sz = sz - bytes_copied;
1001 }
1002
1003 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1004 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001005 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001006 }
1007
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001008#if defined(MCUBOOT_ENC_IMAGES) && !defined(MCUBOOT_ENC_IMAGES_XIP)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001009 image_index = BOOT_CURR_IMG(state);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001010 if ((flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_PRIMARY(image_index) ||
1011 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) &&
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001012
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001013 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_PRIMARY(image_index) &&
1014 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) &&
1015 !(flash_area_get_id(fap_src) == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
1016 flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_SECONDARY(image_index)))
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001017 {
1018 /* assume the primary slot as src, needs encryption */
1019 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -03001020#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -03001021 off = off_src;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001022 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001023 /* might need decryption (metadata from the secondary slot) */
1024 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -03001025 off = off_dst;
1026 }
Fabio Utzig74aef312019-11-28 11:05:34 -03001027#else
1028 off = off_dst;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001029 if (flash_area_get_id(fap_dst) == FLASH_AREA_IMAGE_PRIMARY(image_index)) {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001030 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -03001031 }
1032#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001033 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001034 blk_sz = chunk_sz;
1035 idx = 0;
1036 if (off + bytes_copied < hdr->ih_hdr_size) {
1037 /* do not decrypt header */
1038 blk_off = 0;
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001039 if(chunk_sz > hdr->ih_hdr_size) {
1040 blk_sz = chunk_sz - hdr->ih_hdr_size;
1041 idx = hdr->ih_hdr_size - (off + bytes_copied);
1042 } else {
1043 /* still in header-area, no need to decrypt */
1044 blk_sz = 0;
1045 }
Fabio Utzigba829042018-09-18 08:29:34 -03001046 } else {
1047 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
1048 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001049 tlv_off = BOOT_TLV_OFF(hdr);
1050 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001051 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001052 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -03001053 blk_sz = 0;
1054 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -03001055 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -03001056 }
1057 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001058 rc = boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
1059 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
1060 blk_off, &buf[idx]);
1061 if (rc != 0) {
1062 return rc;
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001063 }
Fabio Utzigba829042018-09-18 08:29:34 -03001064 }
1065 }
1066#endif
1067
Christopher Collins92ea77f2016-12-12 15:59:26 -08001068 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1069 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -03001070 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001071 }
1072
1073 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -03001074
1075 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -08001076 }
1077
Fabio Utzigba829042018-09-18 08:29:34 -03001078 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001079}
1080
Christopher Collins92ea77f2016-12-12 15:59:26 -08001081/**
David Vincze2d736ad2019-02-18 11:50:22 +01001082 * Overwrite primary slot with the image contained in the secondary slot.
1083 * If a prior copy operation was interrupted by a system reset, this function
1084 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -08001085 *
1086 * @param bs The current boot status. This function reads
1087 * this struct to determine if it is resuming
1088 * an interrupted swap operation. This
1089 * function writes the updated status to this
1090 * function on return.
1091 *
1092 * @return 0 on success; nonzero on failure.
1093 */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001094#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -06001095static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001096boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -06001097{
Marti Bolivard3269fd2017-06-12 16:31:12 -04001098 size_t sect_count;
1099 size_t sect;
David Brown17609d82017-05-05 09:41:34 -06001100 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001101 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -04001102 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -03001103 size_t last_sector;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001104 const struct flash_area *fap_primary_slot = NULL;
1105 const struct flash_area *fap_secondary_slot = NULL;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001106 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +01001107
Fabio Utzigb4f88102020-10-04 10:16:24 -03001108#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1109 uint32_t sector;
1110 uint32_t trailer_sz;
1111 uint32_t off;
1112 uint32_t sz;
1113#endif
1114
Fabio Utzigaaf767c2017-12-05 10:22:46 -02001115 (void)bs;
1116
Fabio Utzig13d9e352017-10-05 20:32:31 -03001117#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1118 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -03001119 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001120 assert(rc == 0);
1121#endif
David Brown17609d82017-05-05 09:41:34 -06001122
David Vincze2d736ad2019-02-18 11:50:22 +01001123 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1124 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -06001125
Fabio Utzigb0f04732019-07-31 09:49:19 -03001126 image_index = BOOT_CURR_IMG(state);
1127
1128 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1129 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001130 assert (rc == 0);
1131
Fabio Utzigb0f04732019-07-31 09:49:19 -03001132 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1133 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001134 assert (rc == 0);
1135
Fabio Utzig10ee6482019-08-01 12:04:52 -03001136 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001137 BOOT_LOG_DBG(" * primary slot sectors: %lu", (unsigned long)sect_count);
Fabio Utzig13d9e352017-10-05 20:32:31 -03001138 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001139 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001140 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -06001141 assert(rc == 0);
1142
Fabio Utzig13d9e352017-10-05 20:32:31 -03001143#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -03001144 if ((size + this_size) >= src_size) {
1145 size += src_size - size;
1146 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001147 break;
1148 }
1149#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -03001150
1151 size += this_size;
David Brown17609d82017-05-05 09:41:34 -06001152 }
1153
Fabio Utzigb4f88102020-10-04 10:16:24 -03001154#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1155 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
1156 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
1157 sz = 0;
1158 do {
1159 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
1160 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
1161 sector--;
1162 } while (sz < trailer_sz);
1163
1164 rc = boot_erase_region(fap_primary_slot, off, sz);
1165 assert(rc == 0);
1166#endif
1167
Fabio Utzigba829042018-09-18 08:29:34 -03001168#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001169 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001170 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001171 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -03001172 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +01001173
Fabio Utzigba829042018-09-18 08:29:34 -03001174 if (rc < 0) {
1175 return BOOT_EBADIMAGE;
1176 }
Fabio Utzig4741c452019-12-19 15:32:41 -03001177 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -03001178 return BOOT_EBADIMAGE;
1179 }
1180 }
1181#endif
1182
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001183 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%lx bytes", (unsigned long)size);
Fabio Utzigc28005b2019-09-10 12:18:29 -03001184 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -03001185 if (rc != 0) {
1186 return rc;
1187 }
1188
1189#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
1190 rc = boot_write_magic(fap_primary_slot);
1191 if (rc != 0) {
1192 return rc;
1193 }
1194#endif
David Brown17609d82017-05-05 09:41:34 -06001195
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001196 rc = BOOT_HOOK_CALL(boot_copy_region_post_hook, 0, BOOT_CURR_IMG(state),
1197 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT), size);
1198 if (rc != 0) {
1199 return rc;
1200 }
1201
David Vinczec3084132020-02-18 14:50:47 +01001202#ifdef MCUBOOT_HW_ROLLBACK_PROT
1203 /* Update the stored security counter with the new image's security counter
1204 * value. Both slots hold the new image at this point, but the secondary
1205 * slot's image header must be passed since the image headers in the
1206 * boot_data structure have not been updated yet.
1207 */
1208 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1209 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1210 if (rc != 0) {
1211 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1212 return rc;
1213 }
1214#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1215
Fabio Utzig13d9e352017-10-05 20:32:31 -03001216 /*
1217 * Erases header and trailer. The trailer is erased because when a new
1218 * image is written without a trailer as is the case when using newt, the
1219 * trailer that was left might trigger a new upgrade.
1220 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001221 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001222 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001223 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1224 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001225 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001226 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001227 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001228 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001229 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1230 last_sector),
1231 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1232 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001233 assert(rc == 0);
1234
David Vincze2d736ad2019-02-18 11:50:22 +01001235 flash_area_close(fap_primary_slot);
1236 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001237
David Vincze2d736ad2019-02-18 11:50:22 +01001238 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001239
1240 return 0;
1241}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001242#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001243
Christopher Collinsa1c12042019-05-23 14:00:28 -07001244#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001245/**
1246 * Swaps the two images in flash. If a prior copy operation was interrupted
1247 * by a system reset, this function completes that operation.
1248 *
1249 * @param bs The current boot status. This function reads
1250 * this struct to determine if it is resuming
1251 * an interrupted swap operation. This
1252 * function writes the updated status to this
1253 * function on return.
1254 *
1255 * @return 0 on success; nonzero on failure.
1256 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001257static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001258boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001259{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001260 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001261#ifdef MCUBOOT_ENC_IMAGES
1262 const struct flash_area *fap;
1263 uint8_t slot;
Fabio Utzigba829042018-09-18 08:29:34 -03001264#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001265 uint32_t size;
1266 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001267 uint8_t image_index;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001268 int rc = -1;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001269
1270 /* FIXME: just do this if asked by user? */
1271
1272 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001273 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001274
Fabio Utzig12d59162019-11-28 10:01:59 -03001275 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001276 /*
1277 * No swap ever happened, so need to find the largest image which
1278 * will be used to determine the amount of sectors to swap.
1279 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001280 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001281 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001282 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001283 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001284 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001285
Fabio Utzigba829042018-09-18 08:29:34 -03001286#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001287 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001288 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001289 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001290 assert(rc >= 0);
1291
1292 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001293 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001294 assert(rc == 0);
1295 } else {
1296 rc = 0;
1297 }
1298 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001299 (void)memset(bs->enckey[0], BOOT_UNINITIALIZED_KEY_FILL,
1300 BOOT_ENC_KEY_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001301 }
1302#endif
1303
Fabio Utzig10ee6482019-08-01 12:04:52 -03001304 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001305 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001306 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001307 assert(rc == 0);
1308 }
1309
Fabio Utzigba829042018-09-18 08:29:34 -03001310#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001311 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001312 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001313 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001314 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001315 assert(rc >= 0);
1316
1317 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001318 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001319 assert(rc == 0);
1320 } else {
1321 rc = 0;
1322 }
1323 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001324 (void)memset(bs->enckey[1], BOOT_UNINITIALIZED_KEY_FILL,
1325 BOOT_ENC_KEY_SIZE);
Fabio Utzigba829042018-09-18 08:29:34 -03001326 }
1327#endif
1328
Fabio Utzig46490722017-09-04 15:34:32 -03001329 if (size > copy_size) {
1330 copy_size = size;
1331 }
1332
1333 bs->swap_size = copy_size;
1334 } else {
1335 /*
1336 * If a swap was under way, the swap_size should already be present
1337 * in the trailer...
1338 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001339 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001340 assert(rc == 0);
1341
1342 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001343
1344#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001345 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1346 rc = boot_read_enc_key(image_index, slot, bs);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001347 assert(0 == rc);
Fabio Utzigba829042018-09-18 08:29:34 -03001348
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001349 /* Set only an initialized key */
1350 if (!bootutil_buffer_is_filled(bs->enckey[slot],
1351 BOOT_UNINITIALIZED_KEY_FILL,
1352 BOOT_ENC_KEY_SIZE)) {
1353 rc = boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
1354 assert(rc == 0);
Fabio Utzigba829042018-09-18 08:29:34 -03001355 }
1356 }
1357#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001358 }
1359
Fabio Utzig12d59162019-11-28 10:01:59 -03001360 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001361
David Vincze2d736ad2019-02-18 11:50:22 +01001362#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001363 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001364 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001365 BOOT_LOG_WRN("%d status write fails performing the swap",
1366 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001367 }
1368#endif
1369
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001370 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001371}
David Brown17609d82017-05-05 09:41:34 -06001372#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001373
David Vinczee32483f2019-06-13 10:46:24 +02001374#if (BOOT_IMAGE_NUMBER > 1)
1375/**
1376 * Check the image dependency whether it is satisfied and modify
1377 * the swap type if necessary.
1378 *
1379 * @param dep Image dependency which has to be verified.
1380 *
1381 * @return 0 on success; nonzero on failure.
1382 */
1383static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001384boot_verify_slot_dependency(struct boot_loader_state *state,
1385 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001386{
1387 struct image_version *dep_version;
1388 size_t dep_slot;
1389 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001390 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001391
1392 /* Determine the source of the image which is the subject of
1393 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001394 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001395 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1396 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001397 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001398
David Vincze8b0b6372020-05-20 19:54:44 +02001399 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1400 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001401 /* Dependency not satisfied.
1402 * Modify the swap type to decrease the version number of the image
1403 * (which will be located in the primary slot after the boot process),
1404 * consequently the number of unsatisfied dependencies will be
1405 * decreased or remain the same.
1406 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001407 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001408 case BOOT_SWAP_TYPE_TEST:
1409 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001410 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001411 break;
1412 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001413 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001414 break;
1415 default:
1416 break;
1417 }
David Vincze8b0b6372020-05-20 19:54:44 +02001418 } else {
1419 /* Dependency satisfied. */
1420 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001421 }
1422
1423 return rc;
1424}
1425
1426/**
1427 * Read all dependency TLVs of an image from the flash and verify
1428 * one after another to see if they are all satisfied.
1429 *
1430 * @param slot Image slot number.
1431 *
1432 * @return 0 on success; nonzero on failure.
1433 */
1434static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001435boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001436{
1437 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001438 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001439 struct image_dependency dep;
1440 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001441 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001442 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001443 int rc;
1444
Fabio Utzig10ee6482019-08-01 12:04:52 -03001445 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001446 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001447 if (rc != 0) {
1448 rc = BOOT_EFLASH;
1449 goto done;
1450 }
1451
Fabio Utzig61fd8882019-09-14 20:00:20 -03001452 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1453 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001454 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001455 goto done;
1456 }
1457
Fabio Utzig61fd8882019-09-14 20:00:20 -03001458 while (true) {
1459 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1460 if (rc < 0) {
1461 return -1;
1462 } else if (rc > 0) {
1463 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001464 break;
1465 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001466
1467 if (len != sizeof(dep)) {
1468 rc = BOOT_EBADIMAGE;
1469 goto done;
1470 }
1471
1472 rc = flash_area_read(fap, off, &dep, len);
1473 if (rc != 0) {
1474 rc = BOOT_EFLASH;
1475 goto done;
1476 }
1477
1478 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1479 rc = BOOT_EBADARGS;
1480 goto done;
1481 }
1482
1483 /* Verify dependency and modify the swap type if not satisfied. */
1484 rc = boot_verify_slot_dependency(state, &dep);
1485 if (rc != 0) {
1486 /* Dependency not satisfied. */
1487 goto done;
1488 }
David Vinczee32483f2019-06-13 10:46:24 +02001489 }
1490
1491done:
1492 flash_area_close(fap);
1493 return rc;
1494}
1495
1496/**
David Vinczee32483f2019-06-13 10:46:24 +02001497 * Iterate over all the images and verify whether the image dependencies in the
1498 * TLV area are all satisfied and update the related swap type if necessary.
1499 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001500static int
1501boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001502{
Erik Johnson49063752020-02-06 09:59:31 -06001503 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001504 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001505
Fabio Utzig10ee6482019-08-01 12:04:52 -03001506 BOOT_CURR_IMG(state) = 0;
1507 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001508 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1509 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1510 slot = BOOT_SECONDARY_SLOT;
1511 } else {
1512 slot = BOOT_PRIMARY_SLOT;
1513 }
1514
1515 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001516 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001517 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001518 BOOT_CURR_IMG(state)++;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001519 } else {
1520#if (USE_SHARED_SLOT == 1)
1521 /* Disable an upgrading of this image.*/
1522 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1523 BOOT_CURR_IMG(state)++;
1524#else
Fabio Utzig298913b2019-08-28 11:22:45 -03001525 /* Cannot upgrade due to non-met dependencies, so disable all
1526 * image upgrades.
1527 */
1528 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1529 BOOT_CURR_IMG(state) = idx;
1530 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1531 }
1532 break;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001533#endif /* (USE_SHARED_SLOT == 1) */
David Vinczee32483f2019-06-13 10:46:24 +02001534 }
1535 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001536 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001537}
1538#endif /* (BOOT_IMAGE_NUMBER > 1) */
1539
Christopher Collins92ea77f2016-12-12 15:59:26 -08001540/**
David Vinczeba3bd602019-06-17 16:01:43 +02001541 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001542 *
David Vinczeba3bd602019-06-17 16:01:43 +02001543 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001544 *
1545 * @return 0 on success; nonzero on failure.
1546 */
1547static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001548boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001549{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001550 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001551#ifndef MCUBOOT_OVERWRITE_ONLY
1552 uint8_t swap_type;
1553#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001554
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001555 BOOT_LOG_DBG("> boot_perform_update: bs->idx = %" PRIu32, bs->idx);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001556
David Vinczeba3bd602019-06-17 16:01:43 +02001557 /* At this point there are no aborted swaps. */
1558#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001559 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001560#elif defined(MCUBOOT_BOOTSTRAP)
1561 /* Check if the image update was triggered by a bad image in the
1562 * primary slot (the validity of the image in the secondary slot had
1563 * already been checked).
1564 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001565 fih_int fih_rc = FIH_FAILURE;
1566 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1567 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1568 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001569 /* Initialize swap status partition for primary slot, because
1570 * in swap mode it is needed to properly complete copying the image
1571 * to the primary slot.
1572 */
1573 const struct flash_area *fap_primary_slot = NULL;
1574
1575 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state)),
1576 &fap_primary_slot);
1577 assert(rc == 0);
1578
1579 rc = swap_status_init(state, fap_primary_slot, bs);
1580 assert(rc == 0);
1581
1582 flash_area_close(fap_primary_slot);
1583
Fabio Utzig10ee6482019-08-01 12:04:52 -03001584 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001585 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001586 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001587 }
1588#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001589 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001590#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001591 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001592
1593#ifndef MCUBOOT_OVERWRITE_ONLY
1594 /* The following state needs image_ok be explicitly set after the
1595 * swap was finished to avoid a new revert.
1596 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001597 swap_type = BOOT_SWAP_TYPE(state);
1598 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1599 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001600 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001601 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001602 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001603 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001604 }
1605
David Vinczec3084132020-02-18 14:50:47 +01001606#ifdef MCUBOOT_HW_ROLLBACK_PROT
1607 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1608 /* Update the stored security counter with the new image's security
1609 * counter value. The primary slot holds the new image at this point,
1610 * but the secondary slot's image header must be passed since image
1611 * headers in the boot_data structure have not been updated yet.
1612 *
1613 * In case of a permanent image swap mcuboot will never attempt to
1614 * revert the images on the next reboot. Therefore, the security
1615 * counter must be increased right after the image upgrade.
1616 */
1617 rc = boot_update_security_counter(
1618 BOOT_CURR_IMG(state),
1619 BOOT_PRIMARY_SLOT,
1620 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1621 if (rc != 0) {
1622 BOOT_LOG_ERR("Security counter update failed after "
1623 "image upgrade.");
1624 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1625 }
1626 }
1627#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1628
Fabio Utzige575b0b2019-09-11 12:34:23 -03001629 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001630 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001631 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001632 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001633 }
David Vinczeba3bd602019-06-17 16:01:43 +02001634 }
1635#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001636
David Vinczeba3bd602019-06-17 16:01:43 +02001637 return rc;
1638}
1639
1640/**
1641 * Completes a previously aborted image swap.
1642 *
1643 * @param bs The current boot status.
1644 *
1645 * @return 0 on success; nonzero on failure.
1646 */
1647#if !defined(MCUBOOT_OVERWRITE_ONLY)
1648static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001649boot_complete_partial_swap(struct boot_loader_state *state,
1650 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001651{
1652 int rc;
1653
1654 /* Determine the type of swap operation being resumed from the
1655 * `swap-type` trailer field.
1656 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001657 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001658 assert(rc == 0);
1659
Fabio Utzig10ee6482019-08-01 12:04:52 -03001660 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001661
1662 /* The following states need image_ok be explicitly set after the
1663 * swap was finished to avoid a new revert.
1664 */
1665 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1666 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001667 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001668 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001669 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001670 }
1671 }
1672
Fabio Utzige575b0b2019-09-11 12:34:23 -03001673 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001674 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001675 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001676 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001677 }
1678 }
1679
Fabio Utzig10ee6482019-08-01 12:04:52 -03001680 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001681 BOOT_LOG_ERR("panic!");
1682 assert(0);
1683
1684 /* Loop forever... */
1685 while (1) {}
1686 }
1687
1688 return rc;
1689}
1690#endif /* !MCUBOOT_OVERWRITE_ONLY */
1691
1692#if (BOOT_IMAGE_NUMBER > 1)
1693/**
1694 * Review the validity of previously determined swap types of other images.
1695 *
1696 * @param aborted_swap The current image upgrade is a
1697 * partial/aborted swap.
1698 */
1699static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001700boot_review_image_swap_types(struct boot_loader_state *state,
1701 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001702{
1703 /* In that case if we rebooted in the middle of an image upgrade process, we
1704 * must review the validity of swap types, that were previously determined
1705 * for other images. The image_ok flag had not been set before the reboot
1706 * for any of the updated images (only the copy_done flag) and thus falsely
1707 * the REVERT swap type has been determined for the previous images that had
1708 * been updated before the reboot.
1709 *
1710 * There are two separate scenarios that we have to deal with:
1711 *
1712 * 1. The reboot has happened during swapping an image:
1713 * The current image upgrade has been determined as a
1714 * partial/aborted swap.
1715 * 2. The reboot has happened between two separate image upgrades:
1716 * In this scenario we must check the swap type of the current image.
1717 * In those cases if it is NONE or REVERT we cannot certainly determine
1718 * the fact of a reboot. In a consistent state images must move in the
1719 * same direction or stay in place, e.g. in practice REVERT and TEST
1720 * swap types cannot be present at the same time. If the swap type of
1721 * the current image is either TEST, PERM or FAIL we must review the
1722 * already determined swap types of other images and set each false
1723 * REVERT swap types to NONE (these images had been successfully
1724 * updated before the system rebooted between two separate image
1725 * upgrades).
1726 */
1727
Fabio Utzig10ee6482019-08-01 12:04:52 -03001728 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001729 /* Nothing to do */
1730 return;
1731 }
1732
1733 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001734 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1735 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001736 /* Nothing to do */
1737 return;
1738 }
1739 }
1740
Fabio Utzig10ee6482019-08-01 12:04:52 -03001741 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1742 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1743 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001744 }
1745 }
1746}
1747#endif
1748
1749/**
1750 * Prepare image to be updated if required.
1751 *
1752 * Prepare image to be updated if required with completing an image swap
1753 * operation if one was aborted and/or determining the type of the
1754 * swap operation. In case of any error set the swap type to NONE.
1755 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001756 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001757 * @param bs Pointer where the read and possibly updated
1758 * boot status can be written to.
1759 */
1760static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001761boot_prepare_image_for_update(struct boot_loader_state *state,
1762 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001763{
1764 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001765 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001766
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001767 BOOT_LOG_DBG("> boot_prepare_image_for_update: image = %u",
1768 (unsigned)BOOT_CURR_IMG(state));
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001769
David Vinczeba3bd602019-06-17 16:01:43 +02001770 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001771 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001772 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001773 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%u"
1774 " - too small?", (unsigned int) BOOT_MAX_IMG_SECTORS);
David Vinczeba3bd602019-06-17 16:01:43 +02001775 /* Unable to determine sector layout, continue with next image
1776 * if there is one.
1777 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001778 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001779 if (rc == BOOT_EFLASH)
1780 {
1781 /* Only return on error from the primary image flash */
1782 return;
1783 }
David Vinczeba3bd602019-06-17 16:01:43 +02001784 }
1785
1786 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001787 rc = boot_read_image_headers(state, false, NULL);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001788 BOOT_LOG_DBG(" * Read an image (%u) header from each slot: rc = %d",
1789 (unsigned)BOOT_CURR_IMG(state), rc);
David Vinczeba3bd602019-06-17 16:01:43 +02001790 if (rc != 0) {
1791 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001792 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001793 (unsigned)BOOT_CURR_IMG(state));
Fabio Utzig10ee6482019-08-01 12:04:52 -03001794 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001795 return;
1796 }
1797
1798 /* If the current image's slots aren't compatible, no swap is possible.
1799 * Just boot into primary slot.
1800 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001801 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001802 boot_status_reset(bs);
1803
1804#ifndef MCUBOOT_OVERWRITE_ONLY
1805 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001806 if (rc != 0) {
1807 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001808 (unsigned)BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001809 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001810 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001811 return;
1812 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001813#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001814
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001815#if defined (MCUBOOT_SWAP_USING_MOVE) || defined(MCUBOOT_SWAP_USING_SCRATCH)
Fabio Utzig74aef312019-11-28 11:05:34 -03001816 /*
1817 * Must re-read image headers because the boot status might
1818 * have been updated in the previous function call.
1819 */
1820 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001821 BOOT_LOG_DBG(" * re-read image(%u) headers: rc = %d.",
1822 (unsigned)BOOT_CURR_IMG(state), rc);
Fabio Utzig32afe852020-10-04 10:36:02 -03001823#ifdef MCUBOOT_BOOTSTRAP
1824 /* When bootstrapping it's OK to not have image magic in the primary slot */
1825 if (rc != 0 && (BOOT_CURR_IMG(state) != BOOT_PRIMARY_SLOT ||
1826 boot_check_header_erased(state, BOOT_PRIMARY_SLOT) != 0)) {
1827#else
Fabio Utzig74aef312019-11-28 11:05:34 -03001828 if (rc != 0) {
Fabio Utzig32afe852020-10-04 10:36:02 -03001829#endif
Fabio Utzig74aef312019-11-28 11:05:34 -03001830 /* Continue with next image if there is one. */
1831 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1832 BOOT_CURR_IMG(state));
1833 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1834 return;
1835 }
1836#endif
1837
David Vinczeba3bd602019-06-17 16:01:43 +02001838 /* Determine if we rebooted in the middle of an image swap
1839 * operation. If a partial swap was detected, complete it.
1840 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001841 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001842
1843#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001844 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001845#endif
1846
1847#ifdef MCUBOOT_OVERWRITE_ONLY
1848 /* Should never arrive here, overwrite-only mode has
1849 * no swap state.
1850 */
1851 assert(0);
1852#else
1853 /* Determine the type of swap operation being resumed from the
1854 * `swap-type` trailer field.
1855 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001856 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001857 assert(rc == 0);
1858#endif
1859 /* Attempt to read an image header from each slot. Ensure that
1860 * image headers in slots are aligned with headers in boot_data.
1861 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001862 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001863 assert(rc == 0);
1864
1865 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001866 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001867 } else {
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001868 BOOT_LOG_DBG(" * There was no partial swap, determine swap type.");
1869
David Vinczeba3bd602019-06-17 16:01:43 +02001870 /* There was no partial swap, determine swap type. */
1871 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001872 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001873 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001874 FIH_CALL(boot_validate_slot, fih_rc,
1875 state, BOOT_SECONDARY_SLOT, bs);
1876 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1877 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1878 } else {
1879 BOOT_SWAP_TYPE(state) = bs->swap_type;
1880 }
David Vinczeba3bd602019-06-17 16:01:43 +02001881 }
1882
1883#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001884 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001885#endif
1886
1887#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001888 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001889 /* Header checks are done first because they are
1890 * inexpensive. Since overwrite-only copies starting from
1891 * offset 0, if interrupted, it might leave a valid header
1892 * magic, so also run validation on the primary slot to be
1893 * sure it's not OK.
1894 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001895 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1896 FIH_CALL(boot_validate_slot, fih_rc,
1897 state, BOOT_PRIMARY_SLOT, bs);
1898
1899 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1900
Fabio Utzig3d77c952020-10-04 10:23:17 -03001901 rc = (boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC) ? 1: 0;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001902 FIH_CALL(boot_validate_slot, fih_rc,
1903 state, BOOT_SECONDARY_SLOT, bs);
1904
Fabio Utzig3d77c952020-10-04 10:23:17 -03001905 if (rc == 1 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001906 /* Set swap type to REVERT to overwrite the primary
1907 * slot with the image contained in secondary slot
1908 * and to trigger the explicit setting of the
1909 * image_ok flag.
1910 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001911 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001912 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001913 }
1914 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001915#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001916 }
David Vinczeba3bd602019-06-17 16:01:43 +02001917 } else {
1918 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001919 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001920 }
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001921 BOOT_LOG_DBG("< boot_prepare_image_for_update");
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001922}
1923
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001924/**
1925 * Updates the security counter for the current image.
1926 *
1927 * @param state Boot loader status information.
1928 *
1929 * @return 0 on success; nonzero on failure.
1930 */
1931static int
1932boot_update_hw_rollback_protection(struct boot_loader_state *state)
1933{
1934#ifdef MCUBOOT_HW_ROLLBACK_PROT
1935 int rc;
1936
1937 /* Update the stored security counter with the active image's security
1938 * counter value. It will only be updated if the new security counter is
1939 * greater than the stored value.
1940 *
1941 * In case of a successful image swapping when the swap type is TEST the
1942 * security counter can be increased only after a reset, when the swap
1943 * type is NONE and the image has marked itself "OK" (the image_ok flag
1944 * has been set). This way a "revert" can be performed when it's
1945 * necessary.
1946 */
1947 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1948 rc = boot_update_security_counter(
1949 BOOT_CURR_IMG(state),
1950 BOOT_PRIMARY_SLOT,
1951 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1952 if (rc != 0) {
1953 BOOT_LOG_ERR("Security counter update failed after image "
1954 "validation.");
1955 return rc;
1956 }
1957 }
1958
1959 return 0;
1960
1961#else /* MCUBOOT_HW_ROLLBACK_PROT */
1962 (void) (state);
1963
1964 return 0;
1965#endif
1966}
1967
Raef Colese8fe6cf2020-05-26 13:07:40 +01001968fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001969context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001970{
Marti Bolivar84898652017-06-13 17:20:22 -04001971 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001972 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001973 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001974 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001975 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001976 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001977 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001978
1979 /* The array of slot sectors are defined here (as opposed to file scope) so
1980 * that they don't get allocated for non-boot-loader apps. This is
1981 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001982 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001983 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001984 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1985 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001986#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001987 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001988#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02001989#if MCUBOOT_SWAP_USING_STATUS
1990 TARGET_STATIC boot_sector_t status_sectors[BOOT_MAX_SWAP_STATUS_SECTORS];
1991#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001992
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001993 (void)memset(&bs, 0, sizeof(bs));
1994 (void)memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001995 has_upgrade = false;
1996
1997#if (BOOT_IMAGE_NUMBER == 1)
1998 (void)has_upgrade;
1999#endif
Fabio Utzigba829042018-09-18 08:29:34 -03002000
David Vinczeba3bd602019-06-17 16:01:43 +02002001 /* Iterate over all the images. By the end of the loop the swap type has
2002 * to be determined for each image and all aborted swaps have to be
2003 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08002004 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002005 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002006
David Vinczeba3bd602019-06-17 16:01:43 +02002007#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
2008 /* The keys used for encryption may no longer be valid (could belong to
2009 * another images). Therefore, mark them as invalid to force their reload
2010 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03002011 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002012 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06002013#endif
2014
Fabio Utzig10ee6482019-08-01 12:04:52 -03002015 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03002016
Fabio Utzig10ee6482019-08-01 12:04:52 -03002017 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002018 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03002019 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03002020 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03002021#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03002022 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03002023#endif
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002024#if MCUBOOT_SWAP_USING_STATUS
2025 state->status.sectors = status_sectors;
2026#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002027
2028 /* Open primary and secondary image areas for the duration
2029 * of this call.
2030 */
2031 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03002032 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03002033 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02002034 assert(rc == 0);
2035 }
Fabio Utzig12d59162019-11-28 10:01:59 -03002036#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02002037 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03002038 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002039 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03002040#endif
David Vinczeba3bd602019-06-17 16:01:43 +02002041
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002042 BOOT_LOG_DBG(" * boot_prepare_image_for_update...");
David Vinczeba3bd602019-06-17 16:01:43 +02002043 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002044 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03002045
Fabio Utzige575b0b2019-09-11 12:34:23 -03002046 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002047 has_upgrade = true;
2048 }
David Vinczeba3bd602019-06-17 16:01:43 +02002049 }
2050
David Vinczee32483f2019-06-13 10:46:24 +02002051#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03002052 if (has_upgrade) {
2053 /* Iterate over all the images and verify whether the image dependencies
2054 * are all satisfied and update swap type if necessary.
2055 */
2056 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02002057 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03002058 /*
2059 * It was impossible to upgrade because the expected dependency version
2060 * was not available. Here we already changed the swap_type so that
2061 * instead of asserting the bootloader, we continue and no upgrade is
2062 * performed.
2063 */
2064 rc = 0;
2065 }
2066 }
David Vinczee32483f2019-06-13 10:46:24 +02002067#endif
2068
David Vinczeba3bd602019-06-17 16:01:43 +02002069 /* Iterate over all the images. At this point there are no aborted swaps
2070 * and the swap types are determined for each image. By the end of the loop
2071 * all required update operations will have been finished.
2072 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002073 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002074
2075#if (BOOT_IMAGE_NUMBER > 1)
2076#ifdef MCUBOOT_ENC_IMAGES
2077 /* The keys used for encryption may no longer be valid (could belong to
2078 * another images). Therefore, mark them as invalid to force their reload
2079 * by boot_enc_load().
2080 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03002081 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002082#endif /* MCUBOOT_ENC_IMAGES */
2083
2084 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03002085 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002086#endif /* (BOOT_IMAGE_NUMBER > 1) */
2087
2088 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002089 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02002090
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002091 BOOT_LOG_DBG(" * process swap_type = %u", (unsigned)bs.swap_type);
Roman Okhrimenko13f79ed2021-03-11 19:05:41 +02002092
Fabio Utzig10ee6482019-08-01 12:04:52 -03002093 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002094 case BOOT_SWAP_TYPE_NONE:
2095 break;
2096
2097 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2098 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2099 case BOOT_SWAP_TYPE_REVERT:
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002100 BOOT_LOG_DBG(" * perform update, mode %u...", (unsigned)bs.swap_type);
2101 rc = BOOT_HOOK_CALL(boot_perform_update_hook, BOOT_HOOK_REGULAR,
2102 BOOT_CURR_IMG(state), &(BOOT_IMG(state, 1).hdr),
2103 BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT));
2104 if (rc == BOOT_HOOK_REGULAR)
2105 {
2106 rc = boot_perform_update(state, &bs);
2107 }
David Vinczeba3bd602019-06-17 16:01:43 +02002108 assert(rc == 0);
2109 break;
2110
2111 case BOOT_SWAP_TYPE_FAIL:
2112 /* The image in secondary slot was invalid and is now erased. Ensure
2113 * we don't try to boot into it again on the next reboot. Do this by
2114 * pretending we just reverted back to primary slot.
2115 */
2116#ifndef MCUBOOT_OVERWRITE_ONLY
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002117 BOOT_LOG_DBG(" * update failed! Set image_ok manually for image(%u)",
2118 (unsigned)BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002119 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03002120 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002121 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03002122 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002123 }
2124#endif /* !MCUBOOT_OVERWRITE_ONLY */
2125 break;
2126
2127 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03002128 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002129 }
2130
Fabio Utzig10ee6482019-08-01 12:04:52 -03002131 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02002132 BOOT_LOG_ERR("panic!");
2133 assert(0);
2134
2135 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002136 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02002137 }
2138 }
2139
2140 /* Iterate over all the images. At this point all required update operations
2141 * have finished. By the end of the loop each image in the primary slot will
2142 * have been re-validated.
2143 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002144 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2145 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02002146 /* Attempt to read an image header from each slot. Ensure that image
2147 * headers in slots are aligned with headers in boot_data.
2148 */
Fabio Utzig12d59162019-11-28 10:01:59 -03002149 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02002150 if (rc != 0) {
2151 goto out;
2152 }
2153 /* Since headers were reloaded, it can be assumed we just performed
2154 * a swap or overwrite. Now the header info that should be used to
2155 * provide the data for the bootstrap, which previously was at
2156 * secondary slot, was updated to primary slot.
2157 */
2158 }
2159
2160#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002161 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, &bs);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002162 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02002163 goto out;
2164 }
2165#else
2166 /* Even if we're not re-validating the primary slot, we could be booting
2167 * onto an empty flash chip. At least do a basic sanity check that
2168 * the magic number on the image is OK.
2169 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002170 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002171 BOOT_LOG_ERR("bad image magic 0x%" PRIx32 "; Image=%u",
2172 BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic,
2173 (unsigned)BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02002174 rc = BOOT_EBADIMAGE;
2175 goto out;
2176 }
David Vinczec3084132020-02-18 14:50:47 +01002177#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2178
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002179#ifdef MCUBOOT_ENC_IMAGES_XIP
2180 if (0 == BOOT_CURR_IMG(state)) {
2181 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_PRIMARY_SLOT)))
2182 {
2183 (void)memcpy((uint8_t*)rsp->xip_iv, BOOT_CURR_ENC(state)->aes_iv, BOOTUTIL_CRYPTO_AES_CTR_BLOCK_SIZE);
2184 (void)memcpy((uint8_t*)rsp->xip_key, bs.enckey[BOOT_CURR_IMG(state)], BOOTUTIL_CRYPTO_AES_CTR_KEY_SIZE);
David Vinczec3084132020-02-18 14:50:47 +01002185 }
2186 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002187#endif /* MCUBOOT_ENC_IMAGES_XIP */
David Vincze1cf11b52020-03-24 07:51:09 +01002188
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002189 rc = boot_update_hw_rollback_protection(state);
David Vincze1cf11b52020-03-24 07:51:09 +01002190 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002191 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002192 }
David Vincze1cf11b52020-03-24 07:51:09 +01002193
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002194 rc = boot_add_shared_data(state, BOOT_PRIMARY_SLOT);
David Vincze1cf11b52020-03-24 07:51:09 +01002195 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002196 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01002197 }
David Vinczeba3bd602019-06-17 16:01:43 +02002198 }
2199
Fabio Utzigb0f04732019-07-31 09:49:19 -03002200#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02002201 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002202 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03002203#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03002204
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002205 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2206 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2207 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigf616c542019-12-19 15:23:32 -03002208 /*
2209 * Since the boot_status struct stores plaintext encryption keys, reset
2210 * them here to avoid the possibility of jumping into an image that could
2211 * easily recover them.
2212 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002213 (void)memset(&bs, 0, sizeof(struct boot_status));
Fabio Utzigf616c542019-12-19 15:23:32 -03002214
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002215 fill_rsp(state, NULL, rsp);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002216
Raef Colese8fe6cf2020-05-26 13:07:40 +01002217 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03002218out:
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002219 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002220
2221 if (rc) {
2222 fih_rc = fih_int_encode(rc);
2223 }
2224
2225 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002226}
2227
Raef Colese8fe6cf2020-05-26 13:07:40 +01002228fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08002229split_go(int loader_slot, int split_slot, void **entry)
2230{
Marti Bolivarc50926f2017-06-14 09:35:40 -04002231 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08002232 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002233 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002234 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002235 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01002236 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002237
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002238 if ((loader_slot < 0) || (split_slot < 0)) {
2239 FIH_RET(FIH_FAILURE);
2240 }
2241
Christopher Collins92ea77f2016-12-12 15:59:26 -08002242 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
2243 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01002244 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002245 }
David Vinczeba3bd602019-06-17 16:01:43 +02002246 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
2247 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04002248
2249 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
2250 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07002251 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04002252 assert(rc == 0);
2253 split_flash_id = flash_area_id_from_image_slot(split_slot);
2254 rc = flash_area_open(split_flash_id,
2255 &BOOT_IMG_AREA(&boot_data, split_slot));
2256 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002257
2258 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03002259 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002260 if (rc != 0) {
2261 rc = SPLIT_GO_ERR;
2262 goto done;
2263 }
2264
Fabio Utzig12d59162019-11-28 10:01:59 -03002265 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002266 if (rc != 0) {
2267 goto done;
2268 }
2269
Christopher Collins92ea77f2016-12-12 15:59:26 -08002270 /* Don't check the bootable image flag because we could really call a
2271 * bootable or non-bootable image. Just validate that the image check
2272 * passes which is distinct from the normal check.
2273 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002274 FIH_CALL(split_image_check, fih_rc,
2275 boot_img_hdr(&boot_data, split_slot),
2276 BOOT_IMG_AREA(&boot_data, split_slot),
2277 boot_img_hdr(&boot_data, loader_slot),
2278 BOOT_IMG_AREA(&boot_data, loader_slot));
2279 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08002280 goto done;
2281 }
2282
Marti Bolivarea088872017-06-12 17:10:49 -04002283 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04002284 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08002285 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08002286 rc = SPLIT_GO_OK;
2287
2288done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04002289 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
2290 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08002291 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01002292
2293 if (rc) {
2294 fih_rc = fih_int_encode(rc);
2295 }
2296
2297 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08002298}
David Vinczee574f2d2020-07-10 11:42:03 +02002299
Tamas Banfe031092020-09-10 17:32:39 +02002300#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002301
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002302#define NO_ACTIVE_SLOT UINT32_MAX
2303
David Vinczee574f2d2020-07-10 11:42:03 +02002304/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002305 * Opens all flash areas and checks which contain an image with a valid header.
David Vinczee574f2d2020-07-10 11:42:03 +02002306 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002307 * @param state Boot loader status information.
2308 * @param slot_usage Structure to fill with information about the available
2309 * slots.
David Vinczee574f2d2020-07-10 11:42:03 +02002310 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002311 * @return 0 on success; nonzero on failure.
2312 */
2313static int
2314boot_get_slot_usage(struct boot_loader_state *state,
2315 struct slot_usage_t slot_usage[])
2316{
2317 uint32_t slot;
2318 int fa_id;
2319 int rc;
2320 struct image_header *hdr = NULL;
2321
2322 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2323 /* Open all the slots */
2324 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2325 fa_id = flash_area_id_from_multi_image_slot(
2326 BOOT_CURR_IMG(state), slot);
2327 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2328 assert(rc == 0);
2329 }
2330
2331 /* Attempt to read an image header from each slot. */
2332 rc = boot_read_image_headers(state, false, NULL);
2333 if (rc != 0) {
2334 BOOT_LOG_WRN("Failed reading image headers.");
2335 return rc;
2336 }
2337
2338 /* Check headers in all slots */
2339 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2340 hdr = boot_img_hdr(state, slot);
2341
2342 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2343 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = true;
2344 BOOT_LOG_IMAGE_INFO(slot, hdr);
2345 } else {
2346 slot_usage[BOOT_CURR_IMG(state)].slot_available[slot] = false;
2347 BOOT_LOG_INF("Image %u %s slot: Image not found",
2348 (unsigned)BOOT_CURR_IMG(state),
2349 (slot == BOOT_PRIMARY_SLOT)
2350 ? "Primary" : "Secondary");
2351 }
2352 }
2353
2354 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
2355 }
2356
2357 return 0;
2358}
2359
2360/**
2361 * Finds the slot containing the image with the highest version number for the
2362 * current image.
2363 *
2364 * @param state Boot loader status information.
2365 * @param slot_usage Information about the active and available slots.
2366 *
2367 * @return NO_ACTIVE_SLOT if no available slot found, number of
2368 * the found slot otherwise.
David Vinczee574f2d2020-07-10 11:42:03 +02002369 */
2370static uint32_t
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002371find_slot_with_highest_version(struct boot_loader_state *state,
2372 struct slot_usage_t slot_usage[])
David Vinczee574f2d2020-07-10 11:42:03 +02002373{
David Vinczee574f2d2020-07-10 11:42:03 +02002374 uint32_t slot;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002375 uint32_t candidate_slot = NO_ACTIVE_SLOT;
2376 int rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002377
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002378 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2379 if (slot_usage[BOOT_CURR_IMG(state)].slot_available[slot]) {
2380 if (candidate_slot == NO_ACTIVE_SLOT) {
2381 candidate_slot = slot;
2382 } else {
2383 rc = boot_version_cmp(
2384 &boot_img_hdr(state, slot)->ih_ver,
2385 &boot_img_hdr(state, candidate_slot)->ih_ver);
2386 if (rc == 1) {
2387 /* The version of the image being examined is greater than
2388 * the version of the current candidate.
2389 */
2390 candidate_slot = slot;
2391 }
2392 }
David Vinczee574f2d2020-07-10 11:42:03 +02002393 }
2394 }
2395
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002396 return candidate_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002397}
2398
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002399#ifdef MCUBOOT_HAVE_LOGGING
David Vincze505fba22020-10-22 13:53:29 +02002400/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002401 * Prints the state of the loaded images.
David Vincze505fba22020-10-22 13:53:29 +02002402 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002403 * @param state Boot loader status information.
2404 * @param slot_usage Information about the active and available slots.
2405 */
2406static void
2407print_loaded_images(struct boot_loader_state *state,
2408 struct slot_usage_t slot_usage[])
2409{
2410 uint32_t active_slot;
2411
2412 (void)state;
2413
2414 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2415 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2416
2417 BOOT_LOG_INF("Image %u loaded from the %s slot",
2418 (unsigned)BOOT_CURR_IMG(state),
2419 (active_slot == BOOT_PRIMARY_SLOT) ?
2420 "primary" : "secondary");
2421 }
2422}
2423#endif
2424
2425#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
2426/**
2427 * Checks whether the active slot of the current image was previously selected
2428 * to run. Erases the image if it was selected but its execution failed,
2429 * otherwise marks it as selected if it has not been before.
David Vincze505fba22020-10-22 13:53:29 +02002430 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002431 * @param state Boot loader status information.
2432 * @param slot_usage Information about the active and available slots.
2433 *
2434 * @return 0 on success; nonzero on failure.
David Vincze505fba22020-10-22 13:53:29 +02002435 */
2436static int
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002437boot_select_or_erase(struct boot_loader_state *state,
2438 struct slot_usage_t slot_usage[])
David Vincze505fba22020-10-22 13:53:29 +02002439{
2440 const struct flash_area *fap;
2441 int fa_id;
2442 int rc;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002443 uint32_t active_slot;
2444 struct boot_swap_state* active_swap_state;
David Vincze505fba22020-10-22 13:53:29 +02002445
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002446 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2447
2448 fa_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), active_slot);
David Vincze505fba22020-10-22 13:53:29 +02002449 rc = flash_area_open(fa_id, &fap);
2450 assert(rc == 0);
2451
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002452 active_swap_state = &(slot_usage[BOOT_CURR_IMG(state)].swap_state);
2453
2454 (void)memset(active_swap_state, 0, sizeof(struct boot_swap_state));
2455 rc = boot_read_swap_state(fap, active_swap_state);
David Vincze505fba22020-10-22 13:53:29 +02002456 assert(rc == 0);
2457
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002458 if (active_swap_state->magic != BOOT_MAGIC_GOOD ||
2459 (active_swap_state->copy_done == BOOT_FLAG_SET &&
2460 active_swap_state->image_ok != BOOT_FLAG_SET)) {
David Vincze505fba22020-10-22 13:53:29 +02002461 /*
2462 * A reboot happened without the image being confirmed at
2463 * runtime or its trailer is corrupted/invalid. Erase the image
2464 * to prevent it from being selected again on the next reboot.
2465 */
2466 BOOT_LOG_DBG("Erasing faulty image in the %s slot.",
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002467 (active_slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2468 rc = flash_area_erase(fap, 0, flash_area_get_size(fap));
David Vincze505fba22020-10-22 13:53:29 +02002469 assert(rc == 0);
2470
2471 flash_area_close(fap);
2472 rc = -1;
2473 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002474 if (active_swap_state->copy_done != BOOT_FLAG_SET) {
2475 if (active_swap_state->copy_done == BOOT_FLAG_BAD) {
David Vincze505fba22020-10-22 13:53:29 +02002476 BOOT_LOG_DBG("The copy_done flag had an unexpected value. Its "
2477 "value was neither 'set' nor 'unset', but 'bad'.");
2478 }
2479 /*
2480 * Set the copy_done flag, indicating that the image has been
2481 * selected to boot. It can be set in advance, before even
2482 * validating the image, because in case the validation fails, the
2483 * entire image slot will be erased (including the trailer).
2484 */
2485 rc = boot_write_copy_done(fap);
2486 if (rc != 0) {
2487 BOOT_LOG_WRN("Failed to set copy_done flag of the image in "
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002488 "the %s slot.", (active_slot == BOOT_PRIMARY_SLOT) ?
David Vincze505fba22020-10-22 13:53:29 +02002489 "primary" : "secondary");
2490 rc = 0;
2491 }
2492 }
2493 flash_area_close(fap);
2494 }
2495
2496 return rc;
2497}
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002498#endif /* MCUBOOT_DIRECT_XIP && MCUBOOT_DIRECT_XIP_REVERT */
David Vincze505fba22020-10-22 13:53:29 +02002499
Tamas Banfe031092020-09-10 17:32:39 +02002500#ifdef MCUBOOT_RAM_LOAD
2501
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002502#ifndef MULTIPLE_EXECUTABLE_RAM_REGIONS
Tamas Banfe031092020-09-10 17:32:39 +02002503#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2504#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2505#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002506#endif
Tamas Banfe031092020-09-10 17:32:39 +02002507
2508/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002509 * Verifies that the active slot of the current image can be loaded within the
2510 * predefined bounds that are allowed to be used by executable images.
Tamas Banfe031092020-09-10 17:32:39 +02002511 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002512 * @param state Boot loader status information.
2513 * @param slot_usage Information about the active and available slots.
Tamas Banfe031092020-09-10 17:32:39 +02002514 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002515 * @return 0 on success; nonzero on failure.
Tamas Banfe031092020-09-10 17:32:39 +02002516 */
2517static int
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002518boot_verify_ram_load_address(struct boot_loader_state *state,
2519 struct slot_usage_t slot_usage[])
Tamas Banfe031092020-09-10 17:32:39 +02002520{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002521 uint32_t img_dst;
2522 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002523 uint32_t img_end_addr;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002524 uint32_t exec_ram_start;
2525 uint32_t exec_ram_size;
Tamas Banfe031092020-09-10 17:32:39 +02002526
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002527 (void)state;
2528
2529#ifdef MULTIPLE_EXECUTABLE_RAM_REGIONS
2530 int rc;
2531
2532 rc = boot_get_image_exec_ram_info(BOOT_CURR_IMG(state), &exec_ram_start,
2533 &exec_ram_size);
2534 if (rc != 0) {
2535 return BOOT_EBADSTATUS;
2536 }
2537#else
2538 exec_ram_start = IMAGE_EXECUTABLE_RAM_START;
2539 exec_ram_size = IMAGE_EXECUTABLE_RAM_SIZE;
2540#endif
2541
2542 img_dst = slot_usage[BOOT_CURR_IMG(state)].img_dst;
2543 img_sz = slot_usage[BOOT_CURR_IMG(state)].img_sz;
2544
2545 if (img_dst < exec_ram_start) {
Tamas Banfe031092020-09-10 17:32:39 +02002546 return BOOT_EBADIMAGE;
2547 }
2548
2549 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2550 return BOOT_EBADIMAGE;
2551 }
2552
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002553 if (img_end_addr > (exec_ram_start + exec_ram_size)) {
Tamas Banfe031092020-09-10 17:32:39 +02002554 return BOOT_EBADIMAGE;
2555 }
2556
2557 return 0;
2558}
2559
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002560#ifdef MCUBOOT_ENC_IMAGES
2561
Tamas Banfe031092020-09-10 17:32:39 +02002562/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002563 * Copies and decrypts an image from a slot in the flash to an SRAM address.
Tamas Banfe031092020-09-10 17:32:39 +02002564 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002565 * @param state Boot loader status information.
2566 * @param slot The flash slot of the image to be copied to SRAM.
2567 * @param hdr The image header.
2568 * @param src_sz Size of the image.
2569 * @param img_dst Pointer to the address at which the image needs to be
2570 * copied to SRAM.
2571 *
2572 * @return 0 on success; nonzero on failure.
2573 */
2574static int
2575boot_decrypt_and_copy_image_to_sram(struct boot_loader_state *state,
2576 uint32_t slot, struct image_header *hdr,
2577 uint32_t src_sz, uint32_t img_dst)
2578{
2579 /* The flow for the decryption and copy of the image is as follows :
2580 * 1. The whole image is copied to the RAM (header + payload + TLV).
2581 * 2. The encryption key is loaded from the TLV in flash.
2582 * 3. The image is then decrypted chunk by chunk in RAM (1 chunk
2583 * is 1024 bytes). Only the payload section is decrypted.
2584 * 4. The image is authenticated in RAM.
2585 */
2586 const struct flash_area *fap_src = NULL;
2587 struct boot_status bs;
2588 uint32_t blk_off;
2589 uint32_t tlv_off;
2590 uint32_t blk_sz;
2591 uint32_t bytes_copied = hdr->ih_hdr_size;
2592 uint32_t chunk_sz;
2593 uint32_t max_sz = 1024;
2594 uint16_t idx;
2595 uint8_t image_index;
2596 uint8_t * cur_dst;
2597 int area_id;
2598 int rc;
2599 uint8_t * ram_dst = (void *)(IMAGE_RAM_BASE + img_dst);
2600
2601 image_index = BOOT_CURR_IMG(state);
2602 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2603 rc = flash_area_open(area_id, &fap_src);
2604 if (rc != 0){
2605 return BOOT_EFLASH;
2606 }
2607
2608 tlv_off = BOOT_TLV_OFF(hdr);
2609
2610 /* Copying the whole image in RAM */
2611 rc = flash_area_read(fap_src, 0, ram_dst, src_sz);
2612 if (rc != 0) {
2613 goto done;
2614 }
2615
2616 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap_src, &bs);
2617 if (rc < 0) {
2618 goto done;
2619 }
2620
2621 /* if rc > 0 then the key has already been loaded */
2622 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), slot, &bs)) {
2623 goto done;
2624 }
2625
2626 /* Starting at the end of the header as the header section is not encrypted */
2627 while (bytes_copied < tlv_off) { /* TLV section copied previously */
2628 if (src_sz - bytes_copied > max_sz) {
2629 chunk_sz = max_sz;
2630 } else {
2631 chunk_sz = src_sz - bytes_copied;
2632 }
2633
2634 cur_dst = ram_dst + bytes_copied;
2635 blk_sz = chunk_sz;
2636 idx = 0;
2637 if (bytes_copied + chunk_sz > tlv_off) {
2638 /* Going over TLV section
2639 * Part of the chunk is encrypted payload */
2640 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2641 blk_sz = tlv_off - (bytes_copied);
2642 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2643 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2644 blk_off, cur_dst);
2645 } else {
2646 /* Image encrypted payload section */
2647 blk_off = ((bytes_copied) - hdr->ih_hdr_size) & 0xf;
2648 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
2649 (bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
2650 blk_off, cur_dst);
2651 }
2652
2653 bytes_copied += chunk_sz;
2654 }
2655 rc = 0;
2656
2657done:
2658 flash_area_close(fap_src);
2659
2660 return rc;
2661}
2662
2663#endif /* MCUBOOT_ENC_IMAGES */
2664/**
2665 * Copies a slot of the current image into SRAM.
2666 *
2667 * @param state Boot loader status information.
Tamas Banfe031092020-09-10 17:32:39 +02002668 * @param slot The flash slot of the image to be copied to SRAM.
2669 * @param img_dst The address at which the image needs to be copied to
2670 * SRAM.
2671 * @param img_sz The size of the image that needs to be copied to SRAM.
2672 *
2673 * @return 0 on success; nonzero on failure.
2674 */
2675static int
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002676boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2677 uint32_t img_dst, uint32_t img_sz)
Tamas Banfe031092020-09-10 17:32:39 +02002678{
2679 int rc;
2680 const struct flash_area *fap_src = NULL;
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002681 int area_id;
Tamas Banfe031092020-09-10 17:32:39 +02002682
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002683#if (BOOT_IMAGE_NUMBER == 1)
2684 (void)state;
2685#endif
2686
2687 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2688
2689 rc = flash_area_open(area_id, &fap_src);
Tamas Banfe031092020-09-10 17:32:39 +02002690 if (rc != 0) {
2691 return BOOT_EFLASH;
2692 }
2693
2694 /* Direct copy from flash to its new location in SRAM. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002695 rc = flash_area_read(fap_src, 0, (void *)(IMAGE_RAM_BASE + img_dst), img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002696 if (rc != 0) {
2697 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2698 }
2699
2700 flash_area_close(fap_src);
2701
2702 return rc;
2703}
2704
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002705#if (BOOT_IMAGE_NUMBER > 1)
Tamas Banfe031092020-09-10 17:32:39 +02002706/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002707 * Checks if two memory regions (A and B) are overlap or not.
Tamas Banfe031092020-09-10 17:32:39 +02002708 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002709 * @param start_a Start of the A region.
2710 * @param end_a End of the A region.
2711 * @param start_b Start of the B region.
2712 * @param end_b End of the B region.
Tamas Banfe031092020-09-10 17:32:39 +02002713 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002714 * @return true if there is overlap; false otherwise.
2715 */
2716static bool
2717do_regions_overlap(uint32_t start_a, uint32_t end_a,
2718 uint32_t start_b, uint32_t end_b)
2719{
2720 if (start_b > end_a) {
2721 return false;
2722 } else if (start_b >= start_a) {
2723 return true;
2724 } else if (end_b > start_a) {
2725 return true;
2726 }
2727
2728 return false;
2729}
2730
2731/**
2732 * Checks if the image we want to load to memory overlap with an already
2733 * ramloaded image.
2734 *
2735 * @param slot_usage Information about the active and available slots.
2736 * @param image_id_to_check The ID of the image we would like to load.
2737 *
2738 * @return 0 if there is no overlap; nonzero otherwise.
Tamas Banfe031092020-09-10 17:32:39 +02002739 */
2740static int
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002741boot_check_ram_load_overlapping(struct slot_usage_t slot_usage[],
2742 uint32_t image_id_to_check)
Tamas Banfe031092020-09-10 17:32:39 +02002743{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002744 uint32_t i;
2745
2746 uint32_t start_a;
2747 uint32_t end_a;
2748 uint32_t start_b;
2749 uint32_t end_b;
2750
2751 start_a = slot_usage[image_id_to_check].img_dst;
2752 /* Safe to add here, values are already verified in
2753 * boot_verify_ram_load_address() */
2754 end_a = start_a + slot_usage[image_id_to_check].img_sz;
2755
2756 for (i = 0; i < BOOT_IMAGE_NUMBER; i++) {
2757 if (slot_usage[i].active_slot == NO_ACTIVE_SLOT
2758 || i == image_id_to_check) {
2759 continue;
2760 }
2761
2762 start_b = slot_usage[i].img_dst;
2763 /* Safe to add here, values are already verified in
2764 * boot_verify_ram_load_address() */
2765 end_b = start_b + slot_usage[i].img_sz;
2766
2767 if (do_regions_overlap(start_a, end_a, start_b, end_b)) {
2768 return -1;
2769 }
2770 }
2771
2772 return 0;
2773}
2774#endif
2775
2776/**
2777 * Loads the active slot of the current image into SRAM. The load address and
2778 * image size is extracted from the image header.
2779 *
2780 * @param state Boot loader status information.
2781 * @param slot_usage Information about the active and available slots.
2782 *
2783 * @return 0 on success; nonzero on failure.
2784 */
2785static int
2786boot_load_image_to_sram(struct boot_loader_state *state,
2787 struct slot_usage_t slot_usage[])
2788{
2789 uint32_t active_slot;
2790 struct image_header *hdr = NULL;
2791 uint32_t img_dst;
2792 uint32_t img_sz;
Tamas Banfe031092020-09-10 17:32:39 +02002793 int rc;
2794
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002795 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2796 hdr = boot_img_hdr(state, active_slot);
2797
Tamas Banfe031092020-09-10 17:32:39 +02002798 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2799
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002800 img_dst = hdr->ih_load_addr;
Tamas Banfe031092020-09-10 17:32:39 +02002801
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002802 rc = boot_read_image_size(state, active_slot, &img_sz);
Tamas Banfe031092020-09-10 17:32:39 +02002803 if (rc != 0) {
2804 return rc;
2805 }
2806
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002807 slot_usage[BOOT_CURR_IMG(state)].img_dst = img_dst;
2808 slot_usage[BOOT_CURR_IMG(state)].img_sz = img_sz;
2809
2810 rc = boot_verify_ram_load_address(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02002811 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002812 BOOT_LOG_INF("Image RAM load address 0x%" PRIx32 " is invalid.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002813 return rc;
2814 }
2815
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002816#if (BOOT_IMAGE_NUMBER > 1)
2817 rc = boot_check_ram_load_overlapping(slot_usage, BOOT_CURR_IMG(state));
2818 if (rc != 0) {
2819 BOOT_LOG_INF("Image RAM loading to address 0x%" PRIx32
2820 " would overlap with another image.", img_dst);
2821 return rc;
2822 }
2823#endif
2824#ifdef MCUBOOT_ENC_IMAGES
2825 /* decrypt image if encrypted and copy it to RAM */
2826 if (IS_ENCRYPTED(hdr)) {
2827 rc = boot_decrypt_and_copy_image_to_sram(state, active_slot, hdr, img_sz, img_dst);
2828 } else {
2829 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2830 }
2831#else
Tamas Banfe031092020-09-10 17:32:39 +02002832 /* Copy image to the load address from where it currently resides in
2833 * flash.
2834 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002835 rc = boot_copy_image_to_sram(state, active_slot, img_dst, img_sz);
2836#endif
Tamas Banfe031092020-09-10 17:32:39 +02002837 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002838 BOOT_LOG_INF("RAM loading to 0x%" PRIx32 " is failed.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002839 } else {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002840 BOOT_LOG_INF("RAM loading to 0x%" PRIx32 " is succeeded.", img_dst);
Tamas Banfe031092020-09-10 17:32:39 +02002841 }
2842 } else {
2843 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2844 * MCUBOOT_RAM_LOAD is set.
2845 */
2846 rc = BOOT_EBADIMAGE;
2847 }
2848
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002849 if (rc != 0) {
2850 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2851 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2852 }
2853
Tamas Banfe031092020-09-10 17:32:39 +02002854 return rc;
2855}
2856
2857/**
2858 * Removes an image from SRAM, by overwriting it with zeros.
2859 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002860 * @param state Boot loader status information.
2861 * @param slot_usage Information about the active and available slots.
2862 *
2863 * @return 0 on success; nonzero on failure.
2864 */
2865static inline int
2866boot_remove_image_from_sram(struct boot_loader_state *state,
2867 struct slot_usage_t slot_usage[])
2868{
2869 (void)state;
2870
2871 BOOT_LOG_INF("Removing image from SRAM at address 0x%" PRIx32,
2872 slot_usage[BOOT_CURR_IMG(state)].img_dst);
2873
2874 memset((void*)(IMAGE_RAM_BASE + slot_usage[BOOT_CURR_IMG(state)].img_dst),
2875 0, slot_usage[BOOT_CURR_IMG(state)].img_sz);
2876
2877 slot_usage[BOOT_CURR_IMG(state)].img_dst = 0;
2878 slot_usage[BOOT_CURR_IMG(state)].img_sz = 0;
2879
2880 return 0;
2881}
2882
2883/**
2884 * Removes an image from flash by erasing the corresponding flash area
2885 *
2886 * @param state Boot loader status information.
2887 * @param slot The flash slot of the image to be erased.
Tamas Banfe031092020-09-10 17:32:39 +02002888 *
2889 * @return 0 on success; nonzero on failure.
2890 */
2891static inline int
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002892boot_remove_image_from_flash(struct boot_loader_state *state, uint32_t slot)
Tamas Banfe031092020-09-10 17:32:39 +02002893{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002894 int area_id;
2895 int rc;
2896 const struct flash_area *fap;
Tamas Banfe031092020-09-10 17:32:39 +02002897
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002898 (void)state;
2899
2900 BOOT_LOG_INF("Removing image %u slot %" PRIu32 " from flash",
2901 (unsigned)BOOT_CURR_IMG(state), slot);
2902 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
2903 rc = flash_area_open(area_id, &fap);
2904 if (rc == 0) {
2905 flash_area_erase(fap, 0, flash_area_get_size(fap));
2906 }
2907
2908 return rc;
Tamas Banfe031092020-09-10 17:32:39 +02002909}
2910#endif /* MCUBOOT_RAM_LOAD */
2911
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002912#if (BOOT_IMAGE_NUMBER > 1)
2913/**
2914 * Checks the image dependency whether it is satisfied.
2915 *
2916 * @param state Boot loader status information.
2917 * @param slot_usage Information about the active and available slots.
2918 * @param dep Image dependency which has to be verified.
2919 *
2920 * @return 0 if dependencies are met; nonzero otherwise.
2921 */
2922static int
2923boot_verify_slot_dependency(struct boot_loader_state *state,
2924 struct slot_usage_t slot_usage[],
2925 struct image_dependency *dep)
David Vinczee574f2d2020-07-10 11:42:03 +02002926{
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002927 struct image_version *dep_version;
2928 uint32_t dep_slot;
David Vinczee574f2d2020-07-10 11:42:03 +02002929 int rc;
2930
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002931 /* Determine the source of the image which is the subject of
2932 * the dependency and get it's version.
David Vinczee574f2d2020-07-10 11:42:03 +02002933 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002934 dep_slot = slot_usage[dep->image_id].active_slot;
2935 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
2936
2937 rc = boot_version_cmp(dep_version, &dep->image_min_version);
2938 if (rc >= 0) {
2939 /* Dependency satisfied. */
2940 rc = 0;
David Vinczee574f2d2020-07-10 11:42:03 +02002941 }
2942
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002943 return rc;
2944}
2945
2946/**
2947 * Reads all dependency TLVs of an image and verifies one after another to see
2948 * if they are all satisfied.
2949 *
2950 * @param state Boot loader status information.
2951 * @param slot_usage Information about the active and available slots.
2952 *
2953 * @return 0 if dependencies are met; nonzero otherwise.
2954 */
2955static int
2956boot_verify_slot_dependencies(struct boot_loader_state *state,
2957 struct slot_usage_t slot_usage[])
2958{
2959 uint32_t active_slot;
2960 const struct flash_area *fap;
2961 struct image_tlv_iter it;
2962 struct image_dependency dep;
2963 uint32_t off;
2964 uint16_t len;
2965 int area_id;
2966 int rc;
2967
2968 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
2969
2970 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state),
2971 active_slot);
2972 rc = flash_area_open(area_id, &fap);
David Vinczee574f2d2020-07-10 11:42:03 +02002973 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002974 rc = BOOT_EFLASH;
2975 goto done;
David Vinczee574f2d2020-07-10 11:42:03 +02002976 }
2977
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002978 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, active_slot), fap,
2979 IMAGE_TLV_DEPENDENCY, true);
2980 if (rc != 0) {
2981 goto done;
2982 }
David Vinczee574f2d2020-07-10 11:42:03 +02002983
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002984 while (true) {
2985 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
2986 if (rc < 0) {
2987 return -1;
2988 } else if (rc > 0) {
2989 rc = 0;
2990 break;
2991 }
David Vinczee574f2d2020-07-10 11:42:03 +02002992
Roman Okhrimenko977b3752022-03-31 14:40:48 +03002993 if (len != sizeof(dep)) {
2994 rc = BOOT_EBADIMAGE;
2995 goto done;
2996 }
2997
2998 rc = LOAD_IMAGE_DATA(boot_img_hdr(state, active_slot),
2999 fap, off, &dep, len);
3000 if (rc != 0) {
3001 rc = BOOT_EFLASH;
3002 goto done;
3003 }
3004
3005 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
3006 rc = BOOT_EBADARGS;
3007 goto done;
3008 }
3009
3010 rc = boot_verify_slot_dependency(state, slot_usage, &dep);
3011 if (rc != 0) {
3012 /* Dependency not satisfied. */
3013 goto done;
3014 }
3015 }
3016
3017done:
3018 flash_area_close(fap);
3019 return rc;
3020}
3021
3022/**
3023 * Checks the dependency of all the active slots. If an image found with
3024 * invalid or not satisfied dependencies the image is removed from SRAM (in
3025 * case of MCUBOOT_RAM_LOAD strategy) and its slot is set to unavailable.
3026 *
3027 * @param state Boot loader status information.
3028 * @param slot_usage Information about the active and available slots.
3029 *
3030 * @return 0 if dependencies are met; nonzero otherwise.
3031 */
3032static int
3033boot_verify_dependencies(struct boot_loader_state *state,
3034 struct slot_usage_t slot_usage[])
3035{
3036 int rc = -1;
3037 uint32_t active_slot;
3038
3039 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3040 rc = boot_verify_slot_dependencies(state, slot_usage);
3041 if (rc != 0) {
3042 /* Dependencies not met or invalid dependencies. */
3043
3044#ifdef MCUBOOT_RAM_LOAD
3045 boot_remove_image_from_sram(state, slot_usage);
3046#endif /* MCUBOOT_RAM_LOAD */
3047
3048 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
3049 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3050 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
3051
3052 return rc;
3053 }
3054 }
3055
3056 return rc;
3057}
3058#endif /* (BOOT_IMAGE_NUMBER > 1) */
3059
3060/**
3061 * Tries to load a slot for all the images with validation.
3062 *
3063 * @param state Boot loader status information.
3064 * @param slot_usage Information about the active and available slots.
3065 *
3066 * @return 0 on success; nonzero on failure.
3067 */
3068fih_int
3069boot_load_and_validate_images(struct boot_loader_state *state,
3070 struct slot_usage_t slot_usage[])
3071{
3072 uint32_t active_slot;
3073 int rc;
3074 fih_int fih_rc;
3075
3076 /* Go over all the images and try to load one */
3077 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3078 /* All slots tried until a valid image found. Breaking from this loop
3079 * means that a valid image found or already loaded. If no slot is
3080 * found the function returns with error code. */
3081 while (true) {
3082
3083 /* Go over all the slots and try to load one */
3084 active_slot = slot_usage[BOOT_CURR_IMG(state)].active_slot;
3085 if (active_slot != NO_ACTIVE_SLOT){
3086 /* A slot is already active, go to next image. */
3087 break;
3088 }
3089
3090 active_slot = find_slot_with_highest_version(state,
3091 slot_usage);
3092 if (active_slot == NO_ACTIVE_SLOT) {
3093 BOOT_LOG_INF("No slot to load for image %u",
3094 (unsigned)BOOT_CURR_IMG(state));
3095 FIH_RET(FIH_FAILURE);
3096 }
3097
3098 /* Save the number of the active slot. */
3099 slot_usage[BOOT_CURR_IMG(state)].active_slot = active_slot;
3100
3101#ifdef MCUBOOT_DIRECT_XIP
3102 rc = boot_rom_address_check(state, slot_usage);
3103 if (rc != 0) {
3104 /* The image is placed in an unsuitable slot. */
3105 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3106 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
3107 continue;
David Vinczee574f2d2020-07-10 11:42:03 +02003108 }
David Vincze505fba22020-10-22 13:53:29 +02003109
3110#ifdef MCUBOOT_DIRECT_XIP_REVERT
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003111 rc = boot_select_or_erase(state, slot_usage);
David Vincze505fba22020-10-22 13:53:29 +02003112 if (rc != 0) {
3113 /* The selected image slot has been erased. */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003114 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3115 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
David Vincze505fba22020-10-22 13:53:29 +02003116 continue;
3117 }
3118#endif /* MCUBOOT_DIRECT_XIP_REVERT */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003119#endif /* MCUBOOT_DIRECT_XIP */
David Vincze505fba22020-10-22 13:53:29 +02003120
Tamas Banfe031092020-09-10 17:32:39 +02003121#ifdef MCUBOOT_RAM_LOAD
3122 /* Image is first loaded to RAM and authenticated there in order to
3123 * prevent TOCTOU attack during image copy. This could be applied
3124 * when loading images from external (untrusted) flash to internal
3125 * (trusted) RAM and image is authenticated before copying.
3126 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003127 rc = boot_load_image_to_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02003128 if (rc != 0 ) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003129 /* Image cannot be ramloaded. */
3130 boot_remove_image_from_flash(state, active_slot);
3131 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3132 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
Tamas Banfe031092020-09-10 17:32:39 +02003133 continue;
Tamas Banfe031092020-09-10 17:32:39 +02003134 }
3135#endif /* MCUBOOT_RAM_LOAD */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003136
3137 FIH_CALL(boot_validate_slot, fih_rc, state, active_slot, NULL);
3138 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3139 /* Image is invalid. */
Tamas Banfe031092020-09-10 17:32:39 +02003140#ifdef MCUBOOT_RAM_LOAD
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003141 boot_remove_image_from_sram(state, slot_usage);
Tamas Banfe031092020-09-10 17:32:39 +02003142#endif /* MCUBOOT_RAM_LOAD */
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003143 slot_usage[BOOT_CURR_IMG(state)].slot_available[active_slot] = false;
3144 slot_usage[BOOT_CURR_IMG(state)].active_slot = NO_ACTIVE_SLOT;
3145 continue;
David Vincze505fba22020-10-22 13:53:29 +02003146 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003147
3148 /* Valid image loaded from a slot, go to next image. */
3149 break;
David Vinczee574f2d2020-07-10 11:42:03 +02003150 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003151 }
3152
3153 FIH_RET(FIH_SUCCESS);
3154}
3155
3156/**
3157 * Updates the security counter for the current image.
3158 *
3159 * @param state Boot loader status information.
3160 * @param slot_usage Information about the active and available slots.
3161 *
3162 * @return 0 on success; nonzero on failure.
3163 */
3164static int
3165boot_update_hw_rollback_protection(struct boot_loader_state *state,
3166 const struct slot_usage_t slot_usage[])
3167{
3168#ifdef MCUBOOT_HW_ROLLBACK_PROT
3169 int rc;
3170
3171 /* Update the stored security counter with the newer (active) image's
3172 * security counter value.
3173 */
3174#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
3175 /* When the 'revert' mechanism is enabled in direct-xip mode, the
3176 * security counter can be increased only after reboot, if the image
3177 * has been confirmed at runtime (the image_ok flag has been set).
3178 * This way a 'revert' can be performed when it's necessary.
3179 */
3180 if (slot_usage[BOOT_CURR_IMG(state)].swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze505fba22020-10-22 13:53:29 +02003181#endif
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003182 rc = boot_update_security_counter(BOOT_CURR_IMG(state),
3183 slot_usage[BOOT_CURR_IMG(state)].active_slot,
3184 boot_img_hdr(state, slot_usage[BOOT_CURR_IMG(state)].active_slot));
David Vinczee574f2d2020-07-10 11:42:03 +02003185 if (rc != 0) {
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003186 BOOT_LOG_ERR("Security counter update failed after image "
3187 "validation.");
3188 return rc;
David Vinczee574f2d2020-07-10 11:42:03 +02003189 }
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003190#if defined(MCUBOOT_DIRECT_XIP) && defined(MCUBOOT_DIRECT_XIP_REVERT)
3191 }
3192#endif
David Vinczee574f2d2020-07-10 11:42:03 +02003193
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003194 return 0;
David Vinczee574f2d2020-07-10 11:42:03 +02003195
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003196#else /* MCUBOOT_HW_ROLLBACK_PROT */
3197 (void) (state);
3198 (void) (slot_usage);
3199 return 0;
3200#endif
3201}
David Vinczee574f2d2020-07-10 11:42:03 +02003202
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003203fih_int
3204context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
3205{
3206 struct slot_usage_t slot_usage[BOOT_IMAGE_NUMBER];
3207 int rc;
3208 fih_int fih_rc = fih_int_encode(0);
3209
3210 memset(state, 0, sizeof(struct boot_loader_state));
3211 memset(slot_usage, 0, sizeof(struct slot_usage_t) * BOOT_IMAGE_NUMBER);
3212
3213 rc = boot_get_slot_usage(state, slot_usage);
3214 if (rc != 0) {
David Vinczee574f2d2020-07-10 11:42:03 +02003215 goto out;
3216 }
3217
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003218#if (BOOT_IMAGE_NUMBER > 1)
3219 while (true) {
3220#endif
3221 FIH_CALL(boot_load_and_validate_images, fih_rc, state, slot_usage);
3222 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
3223 goto out;
3224 }
3225
3226#if (BOOT_IMAGE_NUMBER > 1)
3227 rc = boot_verify_dependencies(state, slot_usage);
3228 if (rc != 0) {
3229 /* Dependency check failed for an image, it has been removed from
3230 * SRAM in case of MCUBOOT_RAM_LOAD strategy, and set to
3231 * unavailable. Try to load an image from another slot.
3232 */
3233 continue;
3234 }
3235 /* Dependency check was successful. */
3236 break;
3237 }
3238#endif
3239
3240 IMAGES_ITER(BOOT_CURR_IMG(state)) {
3241 rc = boot_update_hw_rollback_protection(state, slot_usage);
3242 if (rc != 0) {
3243 goto out;
3244 }
3245
3246 rc = boot_add_shared_data(state, slot_usage[BOOT_CURR_IMG(state)].active_slot);
3247 if (rc != 0) {
3248 goto out;
3249 }
3250 }
3251
3252 /* All image loaded successfully. */
3253#ifdef MCUBOOT_HAVE_LOGGING
3254 print_loaded_images(state, slot_usage);
3255#endif
3256
3257 fill_rsp(state, slot_usage, rsp);
3258
David Vinczee574f2d2020-07-10 11:42:03 +02003259out:
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003260 close_all_flash_areas(state);
Raef Colese8fe6cf2020-05-26 13:07:40 +01003261
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003262 if (fih_eq(fih_rc, FIH_SUCCESS)) {
3263 fih_rc = fih_int_encode(rc);
3264 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01003265
Roman Okhrimenko977b3752022-03-31 14:40:48 +03003266 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003267}
Tamas Banfe031092020-09-10 17:32:39 +02003268#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02003269
3270/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01003271 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02003272 * appropriate, and tells you what address to boot from.
3273 *
3274 * @param rsp On success, indicates how booting should occur.
3275 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01003276 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02003277 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01003278fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02003279boot_go(struct boot_rsp *rsp)
3280{
Raef Colese8fe6cf2020-05-26 13:07:40 +01003281 fih_int fih_rc = FIH_FAILURE;
3282 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
3283 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02003284}