blob: 982236400500c1582848cd0afd619136eb260da0 [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
6 * Copyright (c) 2019-2020 Arm Limited
7 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28/**
29 * This file provides an interface to the boot loader. Functions defined in
30 * this file should only be called while the boot loader is running.
31 */
32
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060034#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include <os/os_malloc.h>
39#include "bootutil/bootutil.h"
40#include "bootutil/image.h"
41#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030042#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050043#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010044#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010045#include "bootutil/boot_record.h"
Raef Colese8fe6cf2020-05-26 13:07:40 +010046#include "bootutil/fault_injection_hardening.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050047
Fabio Utzigba829042018-09-18 08:29:34 -030048#ifdef MCUBOOT_ENC_IMAGES
49#include "bootutil/enc_key.h"
50#endif
51
Fabio Utzigba1fbe62017-07-21 14:01:20 -030052#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030053
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010054MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
55
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040056static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080057
Fabio Utzigabec0732019-07-31 08:40:22 -030058#if (BOOT_IMAGE_NUMBER > 1)
59#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
60#else
61#define IMAGES_ITER(x)
62#endif
63
Fabio Utzig10ee6482019-08-01 12:04:52 -030064/*
65 * This macro allows some control on the allocation of local variables.
66 * When running natively on a target, we don't want to allocated huge
67 * variables on the stack, so make them global instead. For the simulator
68 * we want to run as many threads as there are tests, and it's safer
69 * to just make those variables stack allocated.
70 */
71#if !defined(__BOOTSIM__)
72#define TARGET_STATIC static
73#else
74#define TARGET_STATIC
75#endif
76
David Vinczee574f2d2020-07-10 11:42:03 +020077static int
78boot_read_image_headers(struct boot_loader_state *state, bool require_all,
79 struct boot_status *bs)
80{
81 int rc;
82 int i;
83
84 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
85 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
86 if (rc != 0) {
87 /* If `require_all` is set, fail on any single fail, otherwise
88 * if at least the first slot's header was read successfully,
89 * then the boot loader can attempt a boot.
90 *
91 * Failure to read any headers is a fatal error.
92 */
93 if (i > 0 && !require_all) {
94 return 0;
95 } else {
96 return rc;
97 }
98 }
99 }
100
101 return 0;
102}
103
Tamas Banfe031092020-09-10 17:32:39 +0200104#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600105/*
106 * Compute the total size of the given image. Includes the size of
107 * the TLVs.
108 */
Tamas Banfe031092020-09-10 17:32:39 +0200109#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600110static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300111boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600112{
113 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300114 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300115 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300116 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600117 int area_id;
118 int rc;
119
Fabio Utzig10ee6482019-08-01 12:04:52 -0300120#if (BOOT_IMAGE_NUMBER == 1)
121 (void)state;
122#endif
123
124 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600125 rc = flash_area_open(area_id, &fap);
126 if (rc != 0) {
127 rc = BOOT_EFLASH;
128 goto done;
129 }
130
Fabio Utzig61fd8882019-09-14 20:00:20 -0300131 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
132
133 if (flash_area_read(fap, off, &info, sizeof(info))) {
134 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600135 goto done;
136 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300137
Fabio Utzige52c08e2019-09-11 19:32:00 -0300138 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
139 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
140 if (protect_tlv_size != info.it_tlv_tot) {
141 rc = BOOT_EBADIMAGE;
142 goto done;
143 }
144
145 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
146 rc = BOOT_EFLASH;
147 goto done;
148 }
149 } else if (protect_tlv_size != 0) {
150 rc = BOOT_EBADIMAGE;
151 goto done;
152 }
153
Fabio Utzig61fd8882019-09-14 20:00:20 -0300154 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
155 rc = BOOT_EBADIMAGE;
156 goto done;
157 }
158
Fabio Utzige52c08e2019-09-11 19:32:00 -0300159 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600160 rc = 0;
161
162done:
163 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300164 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600165}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300166#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600167
Tamas Banfe031092020-09-10 17:32:39 +0200168#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700169static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300170boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800171{
David Brownab449182019-11-15 09:32:52 -0700172 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300173#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700174 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300175#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176
177 /* Figure out what size to write update status update as. The size depends
178 * on what the minimum write size is for scratch area, active image slot.
179 * We need to use the bigger of those 2 values.
180 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300181 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300182#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300183 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800184 if (align > elem_sz) {
185 elem_sz = align;
186 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300187#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800188
189 return elem_sz;
190}
191
Fabio Utzig10ee6482019-08-01 12:04:52 -0300192#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
193static int
194boot_initialize_area(struct boot_loader_state *state, int flash_area)
195{
196 int num_sectors = BOOT_MAX_IMG_SECTORS;
197 int rc;
198
199 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
200 rc = flash_area_to_sectors(flash_area, &num_sectors,
201 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
202 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
203
204 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
205 rc = flash_area_to_sectors(flash_area, &num_sectors,
206 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
207 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
208
Fabio Utzig12d59162019-11-28 10:01:59 -0300209#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300210 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
211 rc = flash_area_to_sectors(flash_area, &num_sectors,
212 state->scratch.sectors);
213 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300214#endif
215
Fabio Utzig10ee6482019-08-01 12:04:52 -0300216 } else {
217 return BOOT_EFLASH;
218 }
219
220 return rc;
221}
222#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
223static int
224boot_initialize_area(struct boot_loader_state *state, int flash_area)
225{
226 uint32_t num_sectors;
227 struct flash_sector *out_sectors;
228 size_t *out_num_sectors;
229 int rc;
230
231 num_sectors = BOOT_MAX_IMG_SECTORS;
232
233 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
234 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
235 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
236 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
237 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
238 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300239#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300240 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
241 out_sectors = state->scratch.sectors;
242 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300243#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300244 } else {
245 return BOOT_EFLASH;
246 }
247
248 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
249 if (rc != 0) {
250 return rc;
251 }
252 *out_num_sectors = num_sectors;
253 return 0;
254}
255#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
256
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257/**
258 * Determines the sector layout of both image slots and the scratch area.
259 * This information is necessary for calculating the number of bytes to erase
260 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300261 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800262 */
263static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300264boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800265{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300266 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800268
Fabio Utzig10ee6482019-08-01 12:04:52 -0300269 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300270
271 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800272 if (rc != 0) {
273 return BOOT_EFLASH;
274 }
275
Fabio Utzig10ee6482019-08-01 12:04:52 -0300276 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800277 if (rc != 0) {
278 return BOOT_EFLASH;
279 }
280
Fabio Utzig12d59162019-11-28 10:01:59 -0300281#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300282 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200283 if (rc != 0) {
284 return BOOT_EFLASH;
285 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300286#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200287
Fabio Utzig10ee6482019-08-01 12:04:52 -0300288 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800289
290 return 0;
291}
292
Fabio Utzig12d59162019-11-28 10:01:59 -0300293void
294boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800295{
Fabio Utzig4741c452019-12-19 15:32:41 -0300296#ifdef MCUBOOT_ENC_IMAGES
297 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
298#if MCUBOOT_SWAP_SAVE_ENCTLV
299 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
300#endif
301#endif /* MCUBOOT_ENC_IMAGES */
302
303 bs->use_scratch = 0;
304 bs->swap_size = 0;
305 bs->source = 0;
306
Fabio Utzig74aef312019-11-28 11:05:34 -0300307 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300308 bs->idx = BOOT_STATUS_IDX_0;
309 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700310 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300311}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800312
Fabio Utzig12d59162019-11-28 10:01:59 -0300313bool
314boot_status_is_reset(const struct boot_status *bs)
315{
Fabio Utzig74aef312019-11-28 11:05:34 -0300316 return (bs->op == BOOT_STATUS_OP_MOVE &&
317 bs->idx == BOOT_STATUS_IDX_0 &&
318 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800319}
320
321/**
322 * Writes the supplied boot status to the flash file system. The boot status
323 * contains the current state of an in-progress image copy operation.
324 *
325 * @param bs The boot status to write.
326 *
327 * @return 0 on success; nonzero on failure.
328 */
329int
Fabio Utzig12d59162019-11-28 10:01:59 -0300330boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800331{
332 const struct flash_area *fap;
333 uint32_t off;
334 int area_id;
335 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300336 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700337 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300338 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800339
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300340 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100341 * the trailer. Since in the last step the primary slot is erased, the
342 * first two status writes go to the scratch which will be copied to
343 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300344 */
345
Fabio Utzig12d59162019-11-28 10:01:59 -0300346#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300347 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348 /* Write to scratch. */
349 area_id = FLASH_AREA_IMAGE_SCRATCH;
350 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300351#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100352 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300353 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300354#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800355 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300356#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800357
358 rc = flash_area_open(area_id, &fap);
359 if (rc != 0) {
360 rc = BOOT_EFLASH;
361 goto done;
362 }
363
364 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300365 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200366 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300367 erased_val = flash_area_erased_val(fap);
368 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700369 buf[0] = bs->state;
370
371 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800372 if (rc != 0) {
373 rc = BOOT_EFLASH;
374 goto done;
375 }
376
377 rc = 0;
378
379done:
380 flash_area_close(fap);
381 return rc;
382}
Tamas Banfe031092020-09-10 17:32:39 +0200383#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200384#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800385
386/*
David Vinczec3084132020-02-18 14:50:47 +0100387 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100389static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300390boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
391 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800392{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300393 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300394 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300395 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100396 fih_int fih_rc = FIH_FAILURE;
Fabio Utzigba829042018-09-18 08:29:34 -0300397
Fabio Utzig10ee6482019-08-01 12:04:52 -0300398#if (BOOT_IMAGE_NUMBER == 1)
399 (void)state;
400#endif
401
Fabio Utzigba829042018-09-18 08:29:34 -0300402 (void)bs;
403 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300404
405 image_index = BOOT_CURR_IMG(state);
406
407#ifdef MCUBOOT_ENC_IMAGES
408 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300409 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300410 if (rc < 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100411 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300412 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300413 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100414 FIH_RET(fih_rc);
Fabio Utzigba829042018-09-18 08:29:34 -0300415 }
416 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300417#endif
418
Raef Colese8fe6cf2020-05-26 13:07:40 +0100419 FIH_CALL(bootutil_img_validate, fih_rc, BOOT_CURR_ENC(state), image_index,
420 hdr, fap, tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, NULL);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300421
Raef Colese8fe6cf2020-05-26 13:07:40 +0100422 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423}
424
Tamas Banfe031092020-09-10 17:32:39 +0200425#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Raef Colese8fe6cf2020-05-26 13:07:40 +0100426static fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427split_image_check(struct image_header *app_hdr,
428 const struct flash_area *app_fap,
429 struct image_header *loader_hdr,
430 const struct flash_area *loader_fap)
431{
432 static void *tmpbuf;
433 uint8_t loader_hash[32];
Raef Colese8fe6cf2020-05-26 13:07:40 +0100434 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800435
436 if (!tmpbuf) {
437 tmpbuf = malloc(BOOT_TMPBUF_SZ);
438 if (!tmpbuf) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100439 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440 }
441 }
442
Raef Colese8fe6cf2020-05-26 13:07:40 +0100443 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, loader_hdr, loader_fap,
444 tmpbuf, BOOT_TMPBUF_SZ, NULL, 0, loader_hash);
445 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
446 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800447 }
448
Raef Colese8fe6cf2020-05-26 13:07:40 +0100449 FIH_CALL(bootutil_img_validate, fih_rc, NULL, 0, app_hdr, app_fap,
450 tmpbuf, BOOT_TMPBUF_SZ, loader_hash, 32, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800451
Raef Colese8fe6cf2020-05-26 13:07:40 +0100452out:
453 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800454}
Tamas Banfe031092020-09-10 17:32:39 +0200455#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800456
Fabio Utzig338a19f2018-12-03 08:37:08 -0200457/*
David Brown9bf95af2019-10-10 15:36:36 -0600458 * Check that this is a valid header. Valid means that the magic is
459 * correct, and that the sizes/offsets are "sane". Sane means that
460 * there is no overflow on the arithmetic, and that the result fits
461 * within the flash area we are in.
462 */
463static bool
464boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
465{
466 uint32_t size;
467
468 if (hdr->ih_magic != IMAGE_MAGIC) {
469 return false;
470 }
471
472 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
473 return false;
474 }
475
476 if (size >= fap->fa_size) {
477 return false;
478 }
479
480 return true;
481}
482
483/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200484 * Check that a memory area consists of a given value.
485 */
486static inline bool
487boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300488{
489 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200490 uint8_t *p = (uint8_t *)data;
491 for (i = 0; i < len; i++) {
492 if (val != p[i]) {
493 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300494 }
495 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200496 return true;
497}
498
499static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300500boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200501{
502 const struct flash_area *fap;
503 struct image_header *hdr;
504 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300505 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200506 int rc;
507
Fabio Utzig10ee6482019-08-01 12:04:52 -0300508 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300509 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200510 if (rc != 0) {
511 return -1;
512 }
513
514 erased_val = flash_area_erased_val(fap);
515 flash_area_close(fap);
516
Fabio Utzig10ee6482019-08-01 12:04:52 -0300517 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200518 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
519 return -1;
520 }
521
522 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300523}
524
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000525#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200526 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200527 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000528 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
529/**
David Vincze8b0b6372020-05-20 19:54:44 +0200530 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000531 *
David Vincze8b0b6372020-05-20 19:54:44 +0200532 * @param ver1 Pointer to the first image version to compare.
533 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000534 *
David Vincze8b0b6372020-05-20 19:54:44 +0200535 * @retval -1 If ver1 is strictly less than ver2.
536 * @retval 0 If the image version numbers are equal,
537 * (not including the build number).
538 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000539 */
540static int
David Vincze8b0b6372020-05-20 19:54:44 +0200541boot_version_cmp(const struct image_version *ver1,
542 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000543{
David Vincze8b0b6372020-05-20 19:54:44 +0200544 if (ver1->iv_major > ver2->iv_major) {
545 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000546 }
David Vincze8b0b6372020-05-20 19:54:44 +0200547 if (ver1->iv_major < ver2->iv_major) {
548 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000549 }
David Vincze8b0b6372020-05-20 19:54:44 +0200550 /* The major version numbers are equal, continue comparison. */
551 if (ver1->iv_minor > ver2->iv_minor) {
552 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000553 }
David Vincze8b0b6372020-05-20 19:54:44 +0200554 if (ver1->iv_minor < ver2->iv_minor) {
555 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000556 }
David Vincze8b0b6372020-05-20 19:54:44 +0200557 /* The minor version numbers are equal, continue comparison. */
558 if (ver1->iv_revision > ver2->iv_revision) {
559 return 1;
560 }
561 if (ver1->iv_revision < ver2->iv_revision) {
562 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000563 }
564
565 return 0;
566}
567#endif
568
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300569/*
570 * Check that there is a valid image in a slot
571 *
572 * @returns
Raef Colese8fe6cf2020-05-26 13:07:40 +0100573 * FIH_SUCCESS if image was successfully validated
574 * 1 (or its fih_int encoded form) if no bootloable image was found
575 * FIH_FAILURE on any errors
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300576 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100577static fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300578boot_validate_slot(struct boot_loader_state *state, int slot,
579 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800580{
581 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400582 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300583 int area_id;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100584 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800585 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300586
Fabio Utzig10ee6482019-08-01 12:04:52 -0300587 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300588 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589 if (rc != 0) {
Raef Colese8fe6cf2020-05-26 13:07:40 +0100590 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800591 }
592
Fabio Utzig10ee6482019-08-01 12:04:52 -0300593 hdr = boot_img_hdr(state, slot);
594 if (boot_check_header_erased(state, slot) == 0 ||
595 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300596
597#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
598 /*
599 * This fixes an issue where an image might be erased, but a trailer
600 * be left behind. It can happen if the image is in the secondary slot
601 * and did not pass validation, in which case the whole slot is erased.
602 * If during the erase operation, a reset occurs, parts of the slot
603 * might have been erased while some did not. The concerning part is
604 * the trailer because it might disable a new image from being loaded
605 * through mcumgr; so we just get rid of the trailer here, if the header
606 * is erased.
607 */
608 if (slot != BOOT_PRIMARY_SLOT) {
609 swap_erase_trailer_sectors(state, fap);
610 }
611#endif
612
David Vincze2d736ad2019-02-18 11:50:22 +0100613 /* No bootable image in slot; continue booting from the primary slot. */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100614 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200615 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300616 }
617
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000618#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
619 if (slot != BOOT_PRIMARY_SLOT) {
620 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200621 rc = boot_version_cmp(
622 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
623 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
624 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000625 BOOT_LOG_ERR("insufficient version in secondary slot");
626 flash_area_erase(fap, 0, fap->fa_size);
627 /* Image in the secondary slot does not satisfy version requirement.
628 * Erase the image and continue booting from the primary slot.
629 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100630 fih_rc = fih_int_encode(1);
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000631 goto out;
632 }
633 }
634#endif
635
Raef Colese8fe6cf2020-05-26 13:07:40 +0100636 FIH_CALL(boot_image_check, fih_rc, state, hdr, fap, bs);
637 if (!boot_is_header_valid(hdr, fap) || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Tamas Banfe031092020-09-10 17:32:39 +0200638 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
David Brownb38e0442017-02-24 13:57:12 -0700639 flash_area_erase(fap, 0, fap->fa_size);
David Vinczee574f2d2020-07-10 11:42:03 +0200640 /* Image is invalid, erase it to prevent further unnecessary
641 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700642 */
643 }
David Brown098de832019-12-10 11:58:01 -0700644#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100645 BOOT_LOG_ERR("Image in the %s slot is not valid!",
646 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700647#endif
Raef Colese8fe6cf2020-05-26 13:07:40 +0100648 fih_rc = fih_int_encode(1);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200649 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800650 }
651
Fabio Utzig338a19f2018-12-03 08:37:08 -0200652out:
653 flash_area_close(fap);
Raef Colese8fe6cf2020-05-26 13:07:40 +0100654
655 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656}
657
David Vinczec3084132020-02-18 14:50:47 +0100658#ifdef MCUBOOT_HW_ROLLBACK_PROT
659/**
660 * Updates the stored security counter value with the image's security counter
661 * value which resides in the given slot, only if it's greater than the stored
662 * value.
663 *
664 * @param image_index Index of the image to determine which security
665 * counter to update.
666 * @param slot Slot number of the image.
667 * @param hdr Pointer to the image header structure of the image
668 * that is currently stored in the given slot.
669 *
670 * @return 0 on success; nonzero on failure.
671 */
672static int
673boot_update_security_counter(uint8_t image_index, int slot,
674 struct image_header *hdr)
675{
676 const struct flash_area *fap = NULL;
677 uint32_t img_security_cnt;
678 int rc;
679
680 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
681 &fap);
682 if (rc != 0) {
683 rc = BOOT_EFLASH;
684 goto done;
685 }
686
687 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
688 if (rc != 0) {
689 goto done;
690 }
691
692 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
693 if (rc != 0) {
694 goto done;
695 }
696
697done:
698 flash_area_close(fap);
699 return rc;
700}
701#endif /* MCUBOOT_HW_ROLLBACK_PROT */
702
Tamas Banfe031092020-09-10 17:32:39 +0200703#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200704/**
705 * Determines which swap operation to perform, if any. If it is determined
706 * that a swap operation is required, the image in the secondary slot is checked
707 * for validity. If the image in the secondary slot is invalid, it is erased,
708 * and a swap type of "none" is indicated.
709 *
710 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
711 */
712static int
713boot_validated_swap_type(struct boot_loader_state *state,
714 struct boot_status *bs)
715{
716 int swap_type;
Raef Colese8fe6cf2020-05-26 13:07:40 +0100717 fih_int fih_rc = FIH_FAILURE;
David Vinczee574f2d2020-07-10 11:42:03 +0200718
719 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
720 if (BOOT_IS_UPGRADE(swap_type)) {
721 /* Boot loader wants to switch to the secondary slot.
722 * Ensure image is valid.
723 */
Raef Colese8fe6cf2020-05-26 13:07:40 +0100724 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_SECONDARY_SLOT, bs);
725 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
726 if (fih_eq(fih_rc, fih_int_encode(1))) {
727 swap_type = BOOT_SWAP_TYPE_NONE;
728 } else {
729 swap_type = BOOT_SWAP_TYPE_FAIL;
730 }
David Vinczee574f2d2020-07-10 11:42:03 +0200731 }
732 }
733
734 return swap_type;
735}
736
Christopher Collins92ea77f2016-12-12 15:59:26 -0800737/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800738 * Erases a region of flash.
739 *
Fabio Utzigba829042018-09-18 08:29:34 -0300740 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800741 * @param off The offset within the flash area to start the
742 * erase.
743 * @param sz The number of bytes to erase.
744 *
745 * @return 0 on success; nonzero on failure.
746 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300747int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300748boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800749{
Fabio Utzigba829042018-09-18 08:29:34 -0300750 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800751}
752
753/**
754 * Copies the contents of one flash region to another. You must erase the
755 * destination region prior to calling this function.
756 *
757 * @param flash_area_id_src The ID of the source flash area.
758 * @param flash_area_id_dst The ID of the destination flash area.
759 * @param off_src The offset within the source flash area to
760 * copy from.
761 * @param off_dst The offset within the destination flash area to
762 * copy to.
763 * @param sz The number of bytes to copy.
764 *
765 * @return 0 on success; nonzero on failure.
766 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300767int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300768boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300769 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300770 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800771 uint32_t off_src, uint32_t off_dst, uint32_t sz)
772{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800773 uint32_t bytes_copied;
774 int chunk_sz;
775 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300776#ifdef MCUBOOT_ENC_IMAGES
777 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300778 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300779 size_t blk_off;
780 struct image_header *hdr;
781 uint16_t idx;
782 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300783 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300784#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800785
Fabio Utzig10ee6482019-08-01 12:04:52 -0300786 TARGET_STATIC uint8_t buf[1024];
787
788#if !defined(MCUBOOT_ENC_IMAGES)
789 (void)state;
790#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800791
Christopher Collins92ea77f2016-12-12 15:59:26 -0800792 bytes_copied = 0;
793 while (bytes_copied < sz) {
794 if (sz - bytes_copied > sizeof buf) {
795 chunk_sz = sizeof buf;
796 } else {
797 chunk_sz = sz - bytes_copied;
798 }
799
800 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
801 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300802 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800803 }
804
Fabio Utzigba829042018-09-18 08:29:34 -0300805#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300806 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300807 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
808 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
809 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
810 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100811 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300812 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300813#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300814 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300815 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100816 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300817 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300818 off = off_dst;
819 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300820#else
821 off = off_dst;
822 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
823 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
824 }
825#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200826 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300827 blk_sz = chunk_sz;
828 idx = 0;
829 if (off + bytes_copied < hdr->ih_hdr_size) {
830 /* do not decrypt header */
831 blk_off = 0;
832 blk_sz = chunk_sz - hdr->ih_hdr_size;
833 idx = hdr->ih_hdr_size;
834 } else {
835 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
836 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300837 tlv_off = BOOT_TLV_OFF(hdr);
838 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300839 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300840 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300841 blk_sz = 0;
842 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300843 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300844 }
845 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300846 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300847 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
848 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300849 }
850 }
851#endif
852
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
854 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300855 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800856 }
857
858 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300859
860 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861 }
862
Fabio Utzigba829042018-09-18 08:29:34 -0300863 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800864}
865
Christopher Collins92ea77f2016-12-12 15:59:26 -0800866/**
David Vincze2d736ad2019-02-18 11:50:22 +0100867 * Overwrite primary slot with the image contained in the secondary slot.
868 * If a prior copy operation was interrupted by a system reset, this function
869 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800870 *
871 * @param bs The current boot status. This function reads
872 * this struct to determine if it is resuming
873 * an interrupted swap operation. This
874 * function writes the updated status to this
875 * function on return.
876 *
877 * @return 0 on success; nonzero on failure.
878 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200879#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600880static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300881boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600882{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400883 size_t sect_count;
884 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600885 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300886 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400887 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300888 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100889 const struct flash_area *fap_primary_slot;
890 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300891 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100892
Fabio Utzigb4f88102020-10-04 10:16:24 -0300893#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
894 uint32_t sector;
895 uint32_t trailer_sz;
896 uint32_t off;
897 uint32_t sz;
898#endif
899
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200900 (void)bs;
901
Fabio Utzig13d9e352017-10-05 20:32:31 -0300902#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
903 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300904 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300905 assert(rc == 0);
906#endif
David Brown17609d82017-05-05 09:41:34 -0600907
David Vincze2d736ad2019-02-18 11:50:22 +0100908 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
909 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600910
Fabio Utzigb0f04732019-07-31 09:49:19 -0300911 image_index = BOOT_CURR_IMG(state);
912
913 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
914 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300915 assert (rc == 0);
916
Fabio Utzigb0f04732019-07-31 09:49:19 -0300917 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
918 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300919 assert (rc == 0);
920
Fabio Utzig10ee6482019-08-01 12:04:52 -0300921 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300922 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300923 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300924 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600925 assert(rc == 0);
926
Fabio Utzig13d9e352017-10-05 20:32:31 -0300927#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
Fabio Utzigb4f88102020-10-04 10:16:24 -0300928 if ((size + this_size) >= src_size) {
929 size += src_size - size;
930 size += BOOT_WRITE_SZ(state) - (size % BOOT_WRITE_SZ(state));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300931 break;
932 }
933#endif
Fabio Utzigb4f88102020-10-04 10:16:24 -0300934
935 size += this_size;
David Brown17609d82017-05-05 09:41:34 -0600936 }
937
Fabio Utzigb4f88102020-10-04 10:16:24 -0300938#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
939 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
940 sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
941 sz = 0;
942 do {
943 sz += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sector);
944 off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, sector);
945 sector--;
946 } while (sz < trailer_sz);
947
948 rc = boot_erase_region(fap_primary_slot, off, sz);
949 assert(rc == 0);
950#endif
951
Fabio Utzigba829042018-09-18 08:29:34 -0300952#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300953 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300954 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300955 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300956 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100957
Fabio Utzigba829042018-09-18 08:29:34 -0300958 if (rc < 0) {
959 return BOOT_EBADIMAGE;
960 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300961 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300962 return BOOT_EBADIMAGE;
963 }
964 }
965#endif
966
David Vincze2d736ad2019-02-18 11:50:22 +0100967 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
968 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300969 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
Fabio Utzigb4f88102020-10-04 10:16:24 -0300970 if (rc != 0) {
971 return rc;
972 }
973
974#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
975 rc = boot_write_magic(fap_primary_slot);
976 if (rc != 0) {
977 return rc;
978 }
979#endif
David Brown17609d82017-05-05 09:41:34 -0600980
David Vinczec3084132020-02-18 14:50:47 +0100981#ifdef MCUBOOT_HW_ROLLBACK_PROT
982 /* Update the stored security counter with the new image's security counter
983 * value. Both slots hold the new image at this point, but the secondary
984 * slot's image header must be passed since the image headers in the
985 * boot_data structure have not been updated yet.
986 */
987 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
988 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
989 if (rc != 0) {
990 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
991 return rc;
992 }
993#endif /* MCUBOOT_HW_ROLLBACK_PROT */
994
Fabio Utzig13d9e352017-10-05 20:32:31 -0300995 /*
996 * Erases header and trailer. The trailer is erased because when a new
997 * image is written without a trailer as is the case when using newt, the
998 * trailer that was left might trigger a new upgrade.
999 */
Christopher Collins2c88e692019-05-22 15:10:14 -07001000 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001001 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001002 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1003 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -06001004 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001005 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -07001006 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -03001007 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001008 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1009 last_sector),
1010 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1011 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -03001012 assert(rc == 0);
1013
David Vincze2d736ad2019-02-18 11:50:22 +01001014 flash_area_close(fap_primary_slot);
1015 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -03001016
David Vincze2d736ad2019-02-18 11:50:22 +01001017 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -06001018
1019 return 0;
1020}
Fabio Utzig338a19f2018-12-03 08:37:08 -02001021#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001022
Christopher Collinsa1c12042019-05-23 14:00:28 -07001023#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -03001024/**
1025 * Swaps the two images in flash. If a prior copy operation was interrupted
1026 * by a system reset, this function completes that operation.
1027 *
1028 * @param bs The current boot status. This function reads
1029 * this struct to determine if it is resuming
1030 * an interrupted swap operation. This
1031 * function writes the updated status to this
1032 * function on return.
1033 *
1034 * @return 0 on success; nonzero on failure.
1035 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001036static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001037boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001038{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001039 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001040#ifdef MCUBOOT_ENC_IMAGES
1041 const struct flash_area *fap;
1042 uint8_t slot;
1043 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001044#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001045 uint32_t size;
1046 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001047 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001048 int rc;
1049
1050 /* FIXME: just do this if asked by user? */
1051
1052 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001053 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001054
Fabio Utzig12d59162019-11-28 10:01:59 -03001055 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001056 /*
1057 * No swap ever happened, so need to find the largest image which
1058 * will be used to determine the amount of sectors to swap.
1059 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001060 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001061 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001062 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001063 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001064 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001065
Fabio Utzigba829042018-09-18 08:29:34 -03001066#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001067 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001068 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001069 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001070 assert(rc >= 0);
1071
1072 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001073 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001074 assert(rc == 0);
1075 } else {
1076 rc = 0;
1077 }
1078 } else {
1079 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1080 }
1081#endif
1082
Fabio Utzig10ee6482019-08-01 12:04:52 -03001083 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001084 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001085 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001086 assert(rc == 0);
1087 }
1088
Fabio Utzigba829042018-09-18 08:29:34 -03001089#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001090 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001091 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001092 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001093 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001094 assert(rc >= 0);
1095
1096 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001097 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001098 assert(rc == 0);
1099 } else {
1100 rc = 0;
1101 }
1102 } else {
1103 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1104 }
1105#endif
1106
Fabio Utzig46490722017-09-04 15:34:32 -03001107 if (size > copy_size) {
1108 copy_size = size;
1109 }
1110
1111 bs->swap_size = copy_size;
1112 } else {
1113 /*
1114 * If a swap was under way, the swap_size should already be present
1115 * in the trailer...
1116 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001117 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001118 assert(rc == 0);
1119
1120 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001121
1122#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001123 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1124 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001125 assert(rc == 0);
1126
1127 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001128 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001129 break;
1130 }
1131 }
1132
1133 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001134 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001135 }
1136 }
1137#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001138 }
1139
Fabio Utzig12d59162019-11-28 10:01:59 -03001140 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001141
David Vincze2d736ad2019-02-18 11:50:22 +01001142#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001143 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001144 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001145 BOOT_LOG_WRN("%d status write fails performing the swap",
1146 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001147 }
1148#endif
1149
Christopher Collins92ea77f2016-12-12 15:59:26 -08001150 return 0;
1151}
David Brown17609d82017-05-05 09:41:34 -06001152#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001153
David Vinczee32483f2019-06-13 10:46:24 +02001154#if (BOOT_IMAGE_NUMBER > 1)
1155/**
1156 * Check the image dependency whether it is satisfied and modify
1157 * the swap type if necessary.
1158 *
1159 * @param dep Image dependency which has to be verified.
1160 *
1161 * @return 0 on success; nonzero on failure.
1162 */
1163static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001164boot_verify_slot_dependency(struct boot_loader_state *state,
1165 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001166{
1167 struct image_version *dep_version;
1168 size_t dep_slot;
1169 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001170 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001171
1172 /* Determine the source of the image which is the subject of
1173 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001174 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001175 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1176 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001177 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001178
David Vincze8b0b6372020-05-20 19:54:44 +02001179 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1180 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001181 /* Dependency not satisfied.
1182 * Modify the swap type to decrease the version number of the image
1183 * (which will be located in the primary slot after the boot process),
1184 * consequently the number of unsatisfied dependencies will be
1185 * decreased or remain the same.
1186 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001187 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001188 case BOOT_SWAP_TYPE_TEST:
1189 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001190 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001191 break;
1192 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001193 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001194 break;
1195 default:
1196 break;
1197 }
David Vincze8b0b6372020-05-20 19:54:44 +02001198 } else {
1199 /* Dependency satisfied. */
1200 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001201 }
1202
1203 return rc;
1204}
1205
1206/**
1207 * Read all dependency TLVs of an image from the flash and verify
1208 * one after another to see if they are all satisfied.
1209 *
1210 * @param slot Image slot number.
1211 *
1212 * @return 0 on success; nonzero on failure.
1213 */
1214static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001215boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001216{
1217 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001218 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001219 struct image_dependency dep;
1220 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001221 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001222 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001223 int rc;
1224
Fabio Utzig10ee6482019-08-01 12:04:52 -03001225 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001226 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001227 if (rc != 0) {
1228 rc = BOOT_EFLASH;
1229 goto done;
1230 }
1231
Fabio Utzig61fd8882019-09-14 20:00:20 -03001232 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1233 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001234 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001235 goto done;
1236 }
1237
Fabio Utzig61fd8882019-09-14 20:00:20 -03001238 while (true) {
1239 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1240 if (rc < 0) {
1241 return -1;
1242 } else if (rc > 0) {
1243 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001244 break;
1245 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001246
1247 if (len != sizeof(dep)) {
1248 rc = BOOT_EBADIMAGE;
1249 goto done;
1250 }
1251
1252 rc = flash_area_read(fap, off, &dep, len);
1253 if (rc != 0) {
1254 rc = BOOT_EFLASH;
1255 goto done;
1256 }
1257
1258 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1259 rc = BOOT_EBADARGS;
1260 goto done;
1261 }
1262
1263 /* Verify dependency and modify the swap type if not satisfied. */
1264 rc = boot_verify_slot_dependency(state, &dep);
1265 if (rc != 0) {
1266 /* Dependency not satisfied. */
1267 goto done;
1268 }
David Vinczee32483f2019-06-13 10:46:24 +02001269 }
1270
1271done:
1272 flash_area_close(fap);
1273 return rc;
1274}
1275
1276/**
David Vinczee32483f2019-06-13 10:46:24 +02001277 * Iterate over all the images and verify whether the image dependencies in the
1278 * TLV area are all satisfied and update the related swap type if necessary.
1279 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001280static int
1281boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001282{
Erik Johnson49063752020-02-06 09:59:31 -06001283 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001284 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001285
Fabio Utzig10ee6482019-08-01 12:04:52 -03001286 BOOT_CURR_IMG(state) = 0;
1287 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001288 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1289 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1290 slot = BOOT_SECONDARY_SLOT;
1291 } else {
1292 slot = BOOT_PRIMARY_SLOT;
1293 }
1294
1295 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001296 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001297 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001298 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001299 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001300 /* Cannot upgrade due to non-met dependencies, so disable all
1301 * image upgrades.
1302 */
1303 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1304 BOOT_CURR_IMG(state) = idx;
1305 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1306 }
1307 break;
David Vinczee32483f2019-06-13 10:46:24 +02001308 }
1309 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001310 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001311}
1312#endif /* (BOOT_IMAGE_NUMBER > 1) */
1313
Christopher Collins92ea77f2016-12-12 15:59:26 -08001314/**
David Vinczeba3bd602019-06-17 16:01:43 +02001315 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001316 *
David Vinczeba3bd602019-06-17 16:01:43 +02001317 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001318 *
1319 * @return 0 on success; nonzero on failure.
1320 */
1321static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001322boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001323{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001324 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001325#ifndef MCUBOOT_OVERWRITE_ONLY
1326 uint8_t swap_type;
1327#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001328
David Vinczeba3bd602019-06-17 16:01:43 +02001329 /* At this point there are no aborted swaps. */
1330#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001331 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001332#elif defined(MCUBOOT_BOOTSTRAP)
1333 /* Check if the image update was triggered by a bad image in the
1334 * primary slot (the validity of the image in the secondary slot had
1335 * already been checked).
1336 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001337 fih_int fih_rc = FIH_FAILURE;
1338 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1339 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, bs);
1340 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001341 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001342 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001343 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001344 }
1345#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001346 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001347#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001348 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001349
1350#ifndef MCUBOOT_OVERWRITE_ONLY
1351 /* The following state needs image_ok be explicitly set after the
1352 * swap was finished to avoid a new revert.
1353 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001354 swap_type = BOOT_SWAP_TYPE(state);
1355 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1356 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001357 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001358 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001359 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001360 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001361 }
1362
David Vinczec3084132020-02-18 14:50:47 +01001363#ifdef MCUBOOT_HW_ROLLBACK_PROT
1364 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1365 /* Update the stored security counter with the new image's security
1366 * counter value. The primary slot holds the new image at this point,
1367 * but the secondary slot's image header must be passed since image
1368 * headers in the boot_data structure have not been updated yet.
1369 *
1370 * In case of a permanent image swap mcuboot will never attempt to
1371 * revert the images on the next reboot. Therefore, the security
1372 * counter must be increased right after the image upgrade.
1373 */
1374 rc = boot_update_security_counter(
1375 BOOT_CURR_IMG(state),
1376 BOOT_PRIMARY_SLOT,
1377 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1378 if (rc != 0) {
1379 BOOT_LOG_ERR("Security counter update failed after "
1380 "image upgrade.");
1381 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1382 }
1383 }
1384#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1385
Fabio Utzige575b0b2019-09-11 12:34:23 -03001386 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001387 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001388 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001389 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001390 }
David Vinczeba3bd602019-06-17 16:01:43 +02001391 }
1392#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001393
David Vinczeba3bd602019-06-17 16:01:43 +02001394 return rc;
1395}
1396
1397/**
1398 * Completes a previously aborted image swap.
1399 *
1400 * @param bs The current boot status.
1401 *
1402 * @return 0 on success; nonzero on failure.
1403 */
1404#if !defined(MCUBOOT_OVERWRITE_ONLY)
1405static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001406boot_complete_partial_swap(struct boot_loader_state *state,
1407 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001408{
1409 int rc;
1410
1411 /* Determine the type of swap operation being resumed from the
1412 * `swap-type` trailer field.
1413 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001414 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001415 assert(rc == 0);
1416
Fabio Utzig10ee6482019-08-01 12:04:52 -03001417 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001418
1419 /* The following states need image_ok be explicitly set after the
1420 * swap was finished to avoid a new revert.
1421 */
1422 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1423 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001424 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001425 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001426 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001427 }
1428 }
1429
Fabio Utzige575b0b2019-09-11 12:34:23 -03001430 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001431 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001432 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001433 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001434 }
1435 }
1436
Fabio Utzig10ee6482019-08-01 12:04:52 -03001437 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001438 BOOT_LOG_ERR("panic!");
1439 assert(0);
1440
1441 /* Loop forever... */
1442 while (1) {}
1443 }
1444
1445 return rc;
1446}
1447#endif /* !MCUBOOT_OVERWRITE_ONLY */
1448
1449#if (BOOT_IMAGE_NUMBER > 1)
1450/**
1451 * Review the validity of previously determined swap types of other images.
1452 *
1453 * @param aborted_swap The current image upgrade is a
1454 * partial/aborted swap.
1455 */
1456static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001457boot_review_image_swap_types(struct boot_loader_state *state,
1458 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001459{
1460 /* In that case if we rebooted in the middle of an image upgrade process, we
1461 * must review the validity of swap types, that were previously determined
1462 * for other images. The image_ok flag had not been set before the reboot
1463 * for any of the updated images (only the copy_done flag) and thus falsely
1464 * the REVERT swap type has been determined for the previous images that had
1465 * been updated before the reboot.
1466 *
1467 * There are two separate scenarios that we have to deal with:
1468 *
1469 * 1. The reboot has happened during swapping an image:
1470 * The current image upgrade has been determined as a
1471 * partial/aborted swap.
1472 * 2. The reboot has happened between two separate image upgrades:
1473 * In this scenario we must check the swap type of the current image.
1474 * In those cases if it is NONE or REVERT we cannot certainly determine
1475 * the fact of a reboot. In a consistent state images must move in the
1476 * same direction or stay in place, e.g. in practice REVERT and TEST
1477 * swap types cannot be present at the same time. If the swap type of
1478 * the current image is either TEST, PERM or FAIL we must review the
1479 * already determined swap types of other images and set each false
1480 * REVERT swap types to NONE (these images had been successfully
1481 * updated before the system rebooted between two separate image
1482 * upgrades).
1483 */
1484
Fabio Utzig10ee6482019-08-01 12:04:52 -03001485 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001486 /* Nothing to do */
1487 return;
1488 }
1489
1490 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001491 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1492 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001493 /* Nothing to do */
1494 return;
1495 }
1496 }
1497
Fabio Utzig10ee6482019-08-01 12:04:52 -03001498 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1499 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1500 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001501 }
1502 }
1503}
1504#endif
1505
1506/**
1507 * Prepare image to be updated if required.
1508 *
1509 * Prepare image to be updated if required with completing an image swap
1510 * operation if one was aborted and/or determining the type of the
1511 * swap operation. In case of any error set the swap type to NONE.
1512 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001513 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001514 * @param bs Pointer where the read and possibly updated
1515 * boot status can be written to.
1516 */
1517static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001518boot_prepare_image_for_update(struct boot_loader_state *state,
1519 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001520{
1521 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001522 fih_int fih_rc = FIH_FAILURE;
David Vinczeba3bd602019-06-17 16:01:43 +02001523
1524 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001525 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001526 if (rc != 0) {
1527 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1528 " - too small?", BOOT_MAX_IMG_SECTORS);
1529 /* Unable to determine sector layout, continue with next image
1530 * if there is one.
1531 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001532 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001533 return;
1534 }
1535
1536 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001537 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001538 if (rc != 0) {
1539 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001540 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001541 BOOT_CURR_IMG(state));
1542 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001543 return;
1544 }
1545
1546 /* If the current image's slots aren't compatible, no swap is possible.
1547 * Just boot into primary slot.
1548 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001549 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001550 boot_status_reset(bs);
1551
1552#ifndef MCUBOOT_OVERWRITE_ONLY
1553 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001554 if (rc != 0) {
1555 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001556 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001557 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001558 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001559 return;
1560 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001561#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001562
Fabio Utzig74aef312019-11-28 11:05:34 -03001563#ifdef MCUBOOT_SWAP_USING_MOVE
1564 /*
1565 * Must re-read image headers because the boot status might
1566 * have been updated in the previous function call.
1567 */
1568 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1569 if (rc != 0) {
1570 /* Continue with next image if there is one. */
1571 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1572 BOOT_CURR_IMG(state));
1573 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1574 return;
1575 }
1576#endif
1577
David Vinczeba3bd602019-06-17 16:01:43 +02001578 /* Determine if we rebooted in the middle of an image swap
1579 * operation. If a partial swap was detected, complete it.
1580 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001581 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001582
1583#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001584 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001585#endif
1586
1587#ifdef MCUBOOT_OVERWRITE_ONLY
1588 /* Should never arrive here, overwrite-only mode has
1589 * no swap state.
1590 */
1591 assert(0);
1592#else
1593 /* Determine the type of swap operation being resumed from the
1594 * `swap-type` trailer field.
1595 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001596 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001597 assert(rc == 0);
1598#endif
1599 /* Attempt to read an image header from each slot. Ensure that
1600 * image headers in slots are aligned with headers in boot_data.
1601 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001602 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001603 assert(rc == 0);
1604
1605 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001606 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001607 } else {
1608 /* There was no partial swap, determine swap type. */
1609 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001610 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001611 } else {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001612 FIH_CALL(boot_validate_slot, fih_rc,
1613 state, BOOT_SECONDARY_SLOT, bs);
1614 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
1615 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
1616 } else {
1617 BOOT_SWAP_TYPE(state) = bs->swap_type;
1618 }
David Vinczeba3bd602019-06-17 16:01:43 +02001619 }
1620
1621#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001622 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001623#endif
1624
1625#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001626 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001627 /* Header checks are done first because they are
1628 * inexpensive. Since overwrite-only copies starting from
1629 * offset 0, if interrupted, it might leave a valid header
1630 * magic, so also run validation on the primary slot to be
1631 * sure it's not OK.
1632 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001633 rc = boot_check_header_erased(state, BOOT_PRIMARY_SLOT);
1634 FIH_CALL(boot_validate_slot, fih_rc,
1635 state, BOOT_PRIMARY_SLOT, bs);
1636
1637 if (rc == 0 || fih_not_eq(fih_rc, FIH_SUCCESS)) {
1638
1639 rc = boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC;
1640 FIH_CALL(boot_validate_slot, fih_rc,
1641 state, BOOT_SECONDARY_SLOT, bs);
1642
1643 if (rc == 0 && fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001644 /* Set swap type to REVERT to overwrite the primary
1645 * slot with the image contained in secondary slot
1646 * and to trigger the explicit setting of the
1647 * image_ok flag.
1648 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001649 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001650 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001651 }
1652 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001653#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001654 }
David Vinczeba3bd602019-06-17 16:01:43 +02001655 } else {
1656 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001657 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001658 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001659}
1660
Raef Colese8fe6cf2020-05-26 13:07:40 +01001661fih_int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001662context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001663{
Marti Bolivar84898652017-06-13 17:20:22 -04001664 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001665 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001666 int rc = -1;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001667 fih_int fih_rc = FIH_FAILURE;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001668 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001669 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001670 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001671
1672 /* The array of slot sectors are defined here (as opposed to file scope) so
1673 * that they don't get allocated for non-boot-loader apps. This is
1674 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001675 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001676 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001677 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1678 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001679#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001680 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001681#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001682
Fabio Utzig10ee6482019-08-01 12:04:52 -03001683 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001684 has_upgrade = false;
1685
1686#if (BOOT_IMAGE_NUMBER == 1)
1687 (void)has_upgrade;
1688#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001689
David Vinczeba3bd602019-06-17 16:01:43 +02001690 /* Iterate over all the images. By the end of the loop the swap type has
1691 * to be determined for each image and all aborted swaps have to be
1692 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001693 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001694 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001695
David Vinczeba3bd602019-06-17 16:01:43 +02001696#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1697 /* The keys used for encryption may no longer be valid (could belong to
1698 * another images). Therefore, mark them as invalid to force their reload
1699 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001700 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001701 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001702#endif
1703
Fabio Utzig10ee6482019-08-01 12:04:52 -03001704 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001705
Fabio Utzig10ee6482019-08-01 12:04:52 -03001706 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001707 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001708 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001709 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001710#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001711 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001712#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001713
1714 /* Open primary and secondary image areas for the duration
1715 * of this call.
1716 */
1717 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001718 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001719 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001720 assert(rc == 0);
1721 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001722#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001723 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001725 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001726#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001727
1728 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001729 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001730
Fabio Utzige575b0b2019-09-11 12:34:23 -03001731 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001732 has_upgrade = true;
1733 }
David Vinczeba3bd602019-06-17 16:01:43 +02001734 }
1735
David Vinczee32483f2019-06-13 10:46:24 +02001736#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001737 if (has_upgrade) {
1738 /* Iterate over all the images and verify whether the image dependencies
1739 * are all satisfied and update swap type if necessary.
1740 */
1741 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001742 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001743 /*
1744 * It was impossible to upgrade because the expected dependency version
1745 * was not available. Here we already changed the swap_type so that
1746 * instead of asserting the bootloader, we continue and no upgrade is
1747 * performed.
1748 */
1749 rc = 0;
1750 }
1751 }
David Vinczee32483f2019-06-13 10:46:24 +02001752#endif
1753
David Vinczeba3bd602019-06-17 16:01:43 +02001754 /* Iterate over all the images. At this point there are no aborted swaps
1755 * and the swap types are determined for each image. By the end of the loop
1756 * all required update operations will have been finished.
1757 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001758 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001759
1760#if (BOOT_IMAGE_NUMBER > 1)
1761#ifdef MCUBOOT_ENC_IMAGES
1762 /* The keys used for encryption may no longer be valid (could belong to
1763 * another images). Therefore, mark them as invalid to force their reload
1764 * by boot_enc_load().
1765 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001766 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001767#endif /* MCUBOOT_ENC_IMAGES */
1768
1769 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001770 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001771#endif /* (BOOT_IMAGE_NUMBER > 1) */
1772
1773 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001774 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001775
Fabio Utzig10ee6482019-08-01 12:04:52 -03001776 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001777 case BOOT_SWAP_TYPE_NONE:
1778 break;
1779
1780 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1781 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1782 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001783 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001784 assert(rc == 0);
1785 break;
1786
1787 case BOOT_SWAP_TYPE_FAIL:
1788 /* The image in secondary slot was invalid and is now erased. Ensure
1789 * we don't try to boot into it again on the next reboot. Do this by
1790 * pretending we just reverted back to primary slot.
1791 */
1792#ifndef MCUBOOT_OVERWRITE_ONLY
1793 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001794 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001795 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001796 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001797 }
1798#endif /* !MCUBOOT_OVERWRITE_ONLY */
1799 break;
1800
1801 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001802 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001803 }
1804
Fabio Utzig10ee6482019-08-01 12:04:52 -03001805 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001806 BOOT_LOG_ERR("panic!");
1807 assert(0);
1808
1809 /* Loop forever... */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001810 FIH_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001811 }
1812 }
1813
1814 /* Iterate over all the images. At this point all required update operations
1815 * have finished. By the end of the loop each image in the primary slot will
1816 * have been re-validated.
1817 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001818 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1819 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001820 /* Attempt to read an image header from each slot. Ensure that image
1821 * headers in slots are aligned with headers in boot_data.
1822 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001823 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001824 if (rc != 0) {
1825 goto out;
1826 }
1827 /* Since headers were reloaded, it can be assumed we just performed
1828 * a swap or overwrite. Now the header info that should be used to
1829 * provide the data for the bootstrap, which previously was at
1830 * secondary slot, was updated to primary slot.
1831 */
1832 }
1833
1834#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Raef Colese8fe6cf2020-05-26 13:07:40 +01001835 FIH_CALL(boot_validate_slot, fih_rc, state, BOOT_PRIMARY_SLOT, NULL);
1836 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001837 goto out;
1838 }
1839#else
1840 /* Even if we're not re-validating the primary slot, we could be booting
1841 * onto an empty flash chip. At least do a basic sanity check that
1842 * the magic number on the image is OK.
1843 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001844 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001845 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001846 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1847 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001848 rc = BOOT_EBADIMAGE;
1849 goto out;
1850 }
David Vinczec3084132020-02-18 14:50:47 +01001851#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1852
1853#ifdef MCUBOOT_HW_ROLLBACK_PROT
1854 /* Update the stored security counter with the active image's security
1855 * counter value. It will only be updated if the new security counter is
1856 * greater than the stored value.
1857 *
1858 * In case of a successful image swapping when the swap type is TEST the
1859 * security counter can be increased only after a reset, when the swap
1860 * type is NONE and the image has marked itself "OK" (the image_ok flag
1861 * has been set). This way a "revert" can be performed when it's
1862 * necessary.
1863 */
1864 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1865 rc = boot_update_security_counter(
1866 BOOT_CURR_IMG(state),
1867 BOOT_PRIMARY_SLOT,
1868 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1869 if (rc != 0) {
1870 BOOT_LOG_ERR("Security counter update failed after image "
1871 "validation.");
1872 goto out;
1873 }
1874 }
1875#endif /* MCUBOOT_HW_ROLLBACK_PROT */
David Vincze1cf11b52020-03-24 07:51:09 +01001876
1877#ifdef MCUBOOT_MEASURED_BOOT
1878 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
1879 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1880 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1881 if (rc != 0) {
1882 BOOT_LOG_ERR("Failed to add Image %u data to shared memory area",
1883 BOOT_CURR_IMG(state));
Raef Colese8fe6cf2020-05-26 13:07:40 +01001884 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01001885 }
1886#endif /* MCUBOOT_MEASURED_BOOT */
1887
1888#ifdef MCUBOOT_DATA_SHARING
1889 rc = boot_save_shared_data(boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1890 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1891 if (rc != 0) {
1892 BOOT_LOG_ERR("Failed to add data to shared memory area.");
Raef Colese8fe6cf2020-05-26 13:07:40 +01001893 goto out;
David Vincze1cf11b52020-03-24 07:51:09 +01001894 }
1895#endif /* MCUBOOT_DATA_SHARING */
David Vinczeba3bd602019-06-17 16:01:43 +02001896 }
1897
Fabio Utzigb0f04732019-07-31 09:49:19 -03001898#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001899 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001900 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001901#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001902
Fabio Utzigf616c542019-12-19 15:23:32 -03001903 /*
1904 * Since the boot_status struct stores plaintext encryption keys, reset
1905 * them here to avoid the possibility of jumping into an image that could
1906 * easily recover them.
1907 */
1908 memset(&bs, 0, sizeof(struct boot_status));
1909
Fabio Utzig10ee6482019-08-01 12:04:52 -03001910 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1911 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1912 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001913
Raef Colese8fe6cf2020-05-26 13:07:40 +01001914 fih_rc = FIH_SUCCESS;
Fabio Utzig298913b2019-08-28 11:22:45 -03001915out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001916 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001917#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001918 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001919#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001920 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001921 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001922 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001923 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01001924
1925 if (rc) {
1926 fih_rc = fih_int_encode(rc);
1927 }
1928
1929 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001930}
1931
Raef Colese8fe6cf2020-05-26 13:07:40 +01001932fih_int
Christopher Collins92ea77f2016-12-12 15:59:26 -08001933split_go(int loader_slot, int split_slot, void **entry)
1934{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001935 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001936 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001937 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001938 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001939 int rc;
Raef Colese8fe6cf2020-05-26 13:07:40 +01001940 fih_int fih_rc = FIH_FAILURE;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001941
Christopher Collins92ea77f2016-12-12 15:59:26 -08001942 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1943 if (sectors == NULL) {
Raef Colese8fe6cf2020-05-26 13:07:40 +01001944 FIH_RET(FIH_FAILURE);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001945 }
David Vinczeba3bd602019-06-17 16:01:43 +02001946 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1947 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001948
1949 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1950 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001951 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001952 assert(rc == 0);
1953 split_flash_id = flash_area_id_from_image_slot(split_slot);
1954 rc = flash_area_open(split_flash_id,
1955 &BOOT_IMG_AREA(&boot_data, split_slot));
1956 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001957
1958 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001959 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001960 if (rc != 0) {
1961 rc = SPLIT_GO_ERR;
1962 goto done;
1963 }
1964
Fabio Utzig12d59162019-11-28 10:01:59 -03001965 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001966 if (rc != 0) {
1967 goto done;
1968 }
1969
Christopher Collins92ea77f2016-12-12 15:59:26 -08001970 /* Don't check the bootable image flag because we could really call a
1971 * bootable or non-bootable image. Just validate that the image check
1972 * passes which is distinct from the normal check.
1973 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01001974 FIH_CALL(split_image_check, fih_rc,
1975 boot_img_hdr(&boot_data, split_slot),
1976 BOOT_IMG_AREA(&boot_data, split_slot),
1977 boot_img_hdr(&boot_data, loader_slot),
1978 BOOT_IMG_AREA(&boot_data, loader_slot));
1979 if (fih_not_eq(fih_rc, FIH_SUCCESS)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -08001980 goto done;
1981 }
1982
Marti Bolivarea088872017-06-12 17:10:49 -04001983 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001984 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001985 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001986 rc = SPLIT_GO_OK;
1987
1988done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001989 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1990 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001991 free(sectors);
Raef Colese8fe6cf2020-05-26 13:07:40 +01001992
1993 if (rc) {
1994 fih_rc = fih_int_encode(rc);
1995 }
1996
1997 FIH_RET(fih_rc);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001998}
David Vinczee574f2d2020-07-10 11:42:03 +02001999
Tamas Banfe031092020-09-10 17:32:39 +02002000#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002001
2002/**
2003 * Iterates over all slots and determines which contain a firmware image.
2004 *
2005 * @param state Boot loader status information.
2006 * @param slot_usage Pointer to an array, which aim is to carry information
2007 * about slots that contain an image. After return the
2008 * corresponding array elements are set to a non-zero
2009 * value if the given slots are in use (contain a firmware
2010 * image), otherwise they are set to zero.
2011 * @param slot_cnt The number of slots, which can contain firmware images.
2012 * (Equal to or smaller than the size of the
2013 * slot_usage array.)
2014 *
2015 * @return The number of found images.
2016 */
2017static uint32_t
2018boot_get_slot_usage(struct boot_loader_state *state, uint8_t slot_usage[],
2019 uint32_t slot_cnt)
2020{
2021 struct image_header *hdr = NULL;
2022 uint32_t image_cnt = 0;
2023 uint32_t slot;
2024
2025 memset(slot_usage, 0, slot_cnt);
2026
2027 for (slot = 0; slot < slot_cnt; slot++) {
2028 hdr = boot_img_hdr(state, slot);
2029
2030 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
2031 slot_usage[slot] = 1;
2032 image_cnt++;
2033 BOOT_LOG_IMAGE_INFO(slot, hdr);
2034 } else {
2035 BOOT_LOG_INF("%s slot: Image not found", (slot == BOOT_PRIMARY_SLOT)
2036 ? "Primary" : "Secondary");
2037 }
2038 }
2039
2040 return image_cnt;
2041}
2042
Tamas Banfe031092020-09-10 17:32:39 +02002043#ifdef MCUBOOT_RAM_LOAD
2044
2045#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
2046#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
2047#endif
2048
2049/**
2050 * Verifies that the image in a slot will be loaded within the predefined bounds
2051 * that are allowed to be used by executable images.
2052 *
2053 * @param img_dst The address to which the image is going to be copied.
2054 * @param img_sz The size of the image.
2055 *
2056 * @return 0 on success; nonzero on failure.
2057 */
2058static int
2059boot_verify_ram_load_address(uint32_t img_dst, uint32_t img_sz)
2060{
2061 uint32_t img_end_addr;
2062
2063 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2064 return BOOT_EBADIMAGE;
2065 }
2066
2067 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2068 return BOOT_EBADIMAGE;
2069 }
2070
2071 if (img_end_addr > (IMAGE_EXECUTABLE_RAM_START +
2072 IMAGE_EXECUTABLE_RAM_SIZE)) {
2073 return BOOT_EBADIMAGE;
2074 }
2075
2076 return 0;
2077}
2078
2079/**
2080 * Copies an image from a slot in the flash to an SRAM address.
2081 *
2082 * @param slot The flash slot of the image to be copied to SRAM.
2083 * @param img_dst The address at which the image needs to be copied to
2084 * SRAM.
2085 * @param img_sz The size of the image that needs to be copied to SRAM.
2086 *
2087 * @return 0 on success; nonzero on failure.
2088 */
2089static int
2090boot_copy_image_to_sram(int slot, uint32_t img_dst, uint32_t img_sz)
2091{
2092 int rc;
2093 const struct flash_area *fap_src = NULL;
2094
2095 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2096 if (rc != 0) {
2097 return BOOT_EFLASH;
2098 }
2099
2100 /* Direct copy from flash to its new location in SRAM. */
2101 rc = flash_area_read(fap_src, 0, (void *)img_dst, img_sz);
2102 if (rc != 0) {
2103 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2104 }
2105
2106 flash_area_close(fap_src);
2107
2108 return rc;
2109}
2110
2111/**
2112 * Copies an image from a slot in the flash to an SRAM address. The load
2113 * address and image size is extracted from the image header.
2114 *
2115 * @param state Boot loader status information.
2116 * @param slot The flash slot of the image to be copied to SRAM.
2117 * @param hdr Pointer to the image header structure of the image
2118 * @param img_dst Pointer to the address at which the image needs to be
2119 * copied to SRAM.
2120 * @param img_sz Pointer to the size of the image that needs to be
2121 * copied to SRAM.
2122 *
2123 * @return 0 on success; nonzero on failure.
2124 */
2125static int
2126boot_load_image_to_sram(struct boot_loader_state *state, uint32_t slot,
2127 struct image_header *hdr, uint32_t *img_dst,
2128 uint32_t *img_sz)
2129{
2130 int rc;
2131
2132 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2133
2134 *img_dst = hdr->ih_load_addr;
2135
2136 rc = boot_read_image_size(state, slot, img_sz);
2137 if (rc != 0) {
2138 return rc;
2139 }
2140
2141 rc = boot_verify_ram_load_address(*img_dst, *img_sz);
2142 if (rc != 0) {
2143 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", *img_dst);
2144 return rc;
2145 }
2146
2147 /* Copy image to the load address from where it currently resides in
2148 * flash.
2149 */
2150 rc = boot_copy_image_to_sram(slot, *img_dst, *img_sz);
2151 if (rc != 0) {
2152 BOOT_LOG_INF("RAM loading to 0x%x is failed.", *img_dst);
2153 } else {
2154 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", *img_dst);
2155 }
2156 } else {
2157 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2158 * MCUBOOT_RAM_LOAD is set.
2159 */
2160 rc = BOOT_EBADIMAGE;
2161 }
2162
2163 return rc;
2164}
2165
2166/**
2167 * Removes an image from SRAM, by overwriting it with zeros.
2168 *
2169 * @param img_dst The address of the image that needs to be removed from
2170 * SRAM.
2171 * @param img_sz The size of the image that needs to be removed from
2172 * SRAM.
2173 *
2174 * @return 0 on success; nonzero on failure.
2175 */
2176static inline int
2177boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
2178{
2179 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2180 memset((void*)img_dst, 0, img_sz);
2181
2182 return 0;
2183}
2184#endif /* MCUBOOT_RAM_LOAD */
2185
Raef Colese8fe6cf2020-05-26 13:07:40 +01002186fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02002187context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2188{
2189 struct image_header *hdr = NULL;
2190 struct image_header *selected_image_header = NULL;
2191 uint8_t slot_usage[BOOT_NUM_SLOTS];
2192 uint32_t selected_slot;
2193 uint32_t slot;
2194 uint32_t img_cnt;
2195 uint32_t i;
2196 int fa_id;
2197 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002198#ifdef MCUBOOT_RAM_LOAD
2199 uint32_t img_dst;
2200 uint32_t img_sz;
2201 uint32_t img_loaded = 0;
2202#endif /* MCUBOOT_RAM_LOAD */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002203 fih_int fih_rc;
David Vinczee574f2d2020-07-10 11:42:03 +02002204
2205 memset(state, 0, sizeof(struct boot_loader_state));
2206
2207 /* Open primary and secondary image areas for the duration
2208 * of this call.
2209 */
2210 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2211 fa_id = flash_area_id_from_image_slot(slot);
2212 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2213 assert(rc == 0);
2214 }
2215
2216 /* Attempt to read an image header from each slot. */
2217 rc = boot_read_image_headers(state, false, NULL);
2218 if (rc != 0) {
2219 BOOT_LOG_WRN("Failed reading image headers.");
2220 goto out;
2221 }
2222
2223 img_cnt = boot_get_slot_usage(state, slot_usage,
2224 sizeof(slot_usage)/sizeof(slot_usage[0]));
2225
2226 if (img_cnt) {
2227 /* Select the newest and valid image. */
2228 for (i = 0; i < img_cnt; i++) {
2229 selected_slot = 0;
2230 selected_image_header = NULL;
2231
2232 /* Iterate over all the slots that are in use (contain an image)
2233 * and select the one that holds the newest image.
2234 */
2235 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2236 if (slot_usage[slot]) {
2237 hdr = boot_img_hdr(state, slot);
2238 if (selected_image_header != NULL) {
2239 rc = boot_version_cmp(&hdr->ih_ver,
2240 &selected_image_header->ih_ver);
2241 if (rc < 1) {
2242 /* The version of the image being examined wasn't
2243 * greater than the currently selected image's
2244 * version.
2245 */
2246 continue;
2247 }
2248 }
2249 selected_slot = slot;
2250 selected_image_header = hdr;
2251 }
2252 }
Tamas Banfe031092020-09-10 17:32:39 +02002253#ifdef MCUBOOT_RAM_LOAD
2254 /* Image is first loaded to RAM and authenticated there in order to
2255 * prevent TOCTOU attack during image copy. This could be applied
2256 * when loading images from external (untrusted) flash to internal
2257 * (trusted) RAM and image is authenticated before copying.
2258 */
2259 rc = boot_load_image_to_sram(state, selected_slot,
2260 selected_image_header, &img_dst,
2261 &img_sz);
2262 if (rc != 0 ) {
2263 /* Image loading failed try the next one. */
2264 continue;
2265 } else {
2266 img_loaded = 1;
2267 }
2268#endif /* MCUBOOT_RAM_LOAD */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002269 FIH_CALL(boot_validate_slot, fih_rc, state, selected_slot, NULL);
2270 if (fih_eq(fih_rc, FIH_SUCCESS)) {
David Vinczee574f2d2020-07-10 11:42:03 +02002271 /* If a valid image is found then there is no reason to check
2272 * the rest of the images, as each of them has a smaller version
2273 * number.
2274 */
2275 break;
2276 }
Tamas Banfe031092020-09-10 17:32:39 +02002277#ifdef MCUBOOT_RAM_LOAD
2278 else if (img_loaded) {
2279 /* If an image is found to be invalid then it is removed from
2280 * RAM to prevent it being a shellcode vector.
2281 */
2282 boot_remove_image_from_sram(img_dst, img_sz);
2283 img_loaded = 0;
2284 }
2285#endif /* MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002286 /* The selected image is invalid, mark its slot as "unused"
2287 * and start over.
2288 */
2289 slot_usage[selected_slot] = 0;
2290 }
2291
Raef Colese8fe6cf2020-05-26 13:07:40 +01002292 if (fih_not_eq(fih_rc, FIH_SUCCESS) || (selected_image_header == NULL)) {
David Vinczee574f2d2020-07-10 11:42:03 +02002293 /* If there was no valid image at all */
David Vinczee574f2d2020-07-10 11:42:03 +02002294 goto out;
2295 }
2296
2297#ifdef MCUBOOT_HW_ROLLBACK_PROT
2298 /* Update the stored security counter with the newer (active) image's
2299 * security counter value.
2300 */
2301 rc = boot_update_security_counter(0, selected_slot,
2302 selected_image_header);
2303 if (rc != 0) {
2304 BOOT_LOG_ERR("Security counter update failed after image "
2305 "validation.");
2306 goto out;
2307 }
2308#endif /* MCUBOOT_HW_ROLLBACK_PROT */
2309
2310#ifdef MCUBOOT_MEASURED_BOOT
2311 rc = boot_save_boot_status(0, selected_image_header,
2312 BOOT_IMG_AREA(state, selected_slot));
2313 if (rc != 0) {
2314 BOOT_LOG_ERR("Failed to add image data to shared area");
2315 }
2316#endif /* MCUBOOT_MEASURED_BOOT */
2317
2318#ifdef MCUBOOT_DATA_SHARING
2319 rc = boot_save_shared_data(selected_image_header,
2320 BOOT_IMG_AREA(state, selected_slot));
2321 if (rc != 0) {
2322 BOOT_LOG_ERR("Failed to add data to shared memory area.");
2323 }
2324#endif /* MCUBOOT_DATA_SHARING */
2325
2326 BOOT_LOG_INF("Booting image from the %s slot",
2327 (selected_slot == BOOT_PRIMARY_SLOT) ?
2328 "primary" : "secondary");
2329
2330 rsp->br_flash_dev_id =
2331 BOOT_IMG_AREA(state, selected_slot)->fa_device_id;
2332 rsp->br_image_off = boot_img_slot_off(state, selected_slot);
2333 rsp->br_hdr = selected_image_header;
2334 } else {
2335 /* No candidate image available */
2336 rc = BOOT_EBADIMAGE;
2337 goto out;
2338 }
2339
2340out:
2341 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2342 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
2343 }
Raef Colese8fe6cf2020-05-26 13:07:40 +01002344
2345 if (rc) {
2346 fih_rc = fih_int_encode(rc);
2347 }
2348
2349 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02002350}
Tamas Banfe031092020-09-10 17:32:39 +02002351#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002352
2353/**
Raef Colese8fe6cf2020-05-26 13:07:40 +01002354 * Prepares the booting process. This function moves images around in flash as
David Vinczee574f2d2020-07-10 11:42:03 +02002355 * appropriate, and tells you what address to boot from.
2356 *
2357 * @param rsp On success, indicates how booting should occur.
2358 *
Raef Colese8fe6cf2020-05-26 13:07:40 +01002359 * @return FIH_SUCCESS on success; nonzero on failure.
David Vinczee574f2d2020-07-10 11:42:03 +02002360 */
Raef Colese8fe6cf2020-05-26 13:07:40 +01002361fih_int
David Vinczee574f2d2020-07-10 11:42:03 +02002362boot_go(struct boot_rsp *rsp)
2363{
Raef Colese8fe6cf2020-05-26 13:07:40 +01002364 fih_int fih_rc = FIH_FAILURE;
2365 FIH_CALL(context_boot_go, fih_rc, &boot_data, rsp);
2366 FIH_RET(fih_rc);
David Vinczee574f2d2020-07-10 11:42:03 +02002367}