blob: 5a3de4fb74a03db47ba5a990c413d86e31ba8079 [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
33#include <assert.h>
34#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060035#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080036#include <inttypes.h>
37#include <stdlib.h>
38#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080039#include <os/os_malloc.h>
40#include "bootutil/bootutil.h"
41#include "bootutil/image.h"
42#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030043#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050044#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010045#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010046#include "bootutil/boot_record.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 */
389static 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;
396
Fabio Utzig10ee6482019-08-01 12:04:52 -0300397#if (BOOT_IMAGE_NUMBER == 1)
398 (void)state;
399#endif
400
Fabio Utzigba829042018-09-18 08:29:34 -0300401 (void)bs;
402 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300403
404 image_index = BOOT_CURR_IMG(state);
405
406#ifdef MCUBOOT_ENC_IMAGES
407 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300408 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300409 if (rc < 0) {
410 return BOOT_EBADIMAGE;
411 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300412 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300413 return BOOT_EBADIMAGE;
414 }
415 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300416#endif
417
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300418 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300419 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800420 return BOOT_EBADIMAGE;
421 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300422
Christopher Collins92ea77f2016-12-12 15:59:26 -0800423 return 0;
424}
425
Tamas Banfe031092020-09-10 17:32:39 +0200426#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800427static int
428split_image_check(struct image_header *app_hdr,
429 const struct flash_area *app_fap,
430 struct image_header *loader_hdr,
431 const struct flash_area *loader_fap)
432{
433 static void *tmpbuf;
434 uint8_t loader_hash[32];
435
436 if (!tmpbuf) {
437 tmpbuf = malloc(BOOT_TMPBUF_SZ);
438 if (!tmpbuf) {
439 return BOOT_ENOMEM;
440 }
441 }
442
Fabio Utzig10ee6482019-08-01 12:04:52 -0300443 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
444 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445 return BOOT_EBADIMAGE;
446 }
447
Fabio Utzig10ee6482019-08-01 12:04:52 -0300448 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
449 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800450 return BOOT_EBADIMAGE;
451 }
452
453 return 0;
454}
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
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300573 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300574 * 1 if no bootloable image was found
575 * -1 on any errors
576 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800577static 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;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800584 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300585
Fabio Utzig10ee6482019-08-01 12:04:52 -0300586 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300587 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800588 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300589 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590 }
591
Fabio Utzig10ee6482019-08-01 12:04:52 -0300592 hdr = boot_img_hdr(state, slot);
593 if (boot_check_header_erased(state, slot) == 0 ||
594 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300595
596#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
597 /*
598 * This fixes an issue where an image might be erased, but a trailer
599 * be left behind. It can happen if the image is in the secondary slot
600 * and did not pass validation, in which case the whole slot is erased.
601 * If during the erase operation, a reset occurs, parts of the slot
602 * might have been erased while some did not. The concerning part is
603 * the trailer because it might disable a new image from being loaded
604 * through mcumgr; so we just get rid of the trailer here, if the header
605 * is erased.
606 */
607 if (slot != BOOT_PRIMARY_SLOT) {
608 swap_erase_trailer_sectors(state, fap);
609 }
610#endif
611
David Vincze2d736ad2019-02-18 11:50:22 +0100612 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300613 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200614 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300615 }
616
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000617#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
618 if (slot != BOOT_PRIMARY_SLOT) {
619 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200620 rc = boot_version_cmp(
621 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
622 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
623 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000624 BOOT_LOG_ERR("insufficient version in secondary slot");
625 flash_area_erase(fap, 0, fap->fa_size);
626 /* Image in the secondary slot does not satisfy version requirement.
627 * Erase the image and continue booting from the primary slot.
628 */
629 rc = 1;
630 goto out;
631 }
632 }
633#endif
634
David Brown9bf95af2019-10-10 15:36:36 -0600635 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
Tamas Banfe031092020-09-10 17:32:39 +0200636 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
David Brownb38e0442017-02-24 13:57:12 -0700637 flash_area_erase(fap, 0, fap->fa_size);
David Vinczee574f2d2020-07-10 11:42:03 +0200638 /* Image is invalid, erase it to prevent further unnecessary
639 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700640 */
641 }
David Brown098de832019-12-10 11:58:01 -0700642#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100643 BOOT_LOG_ERR("Image in the %s slot is not valid!",
644 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700645#endif
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000646 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200647 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800648 }
649
David Vincze2d736ad2019-02-18 11:50:22 +0100650 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200651 rc = 0;
652
653out:
654 flash_area_close(fap);
655 return 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;
717 int rc;
718
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 */
724 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
725 if (rc == 1) {
726 swap_type = BOOT_SWAP_TYPE_NONE;
727 } else if (rc != 0) {
728 swap_type = BOOT_SWAP_TYPE_FAIL;
729 }
730 }
731
732 return swap_type;
733}
734
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800736 * Erases a region of flash.
737 *
Fabio Utzigba829042018-09-18 08:29:34 -0300738 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800739 * @param off The offset within the flash area to start the
740 * erase.
741 * @param sz The number of bytes to erase.
742 *
743 * @return 0 on success; nonzero on failure.
744 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300745int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300746boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800747{
Fabio Utzigba829042018-09-18 08:29:34 -0300748 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800749}
750
751/**
752 * Copies the contents of one flash region to another. You must erase the
753 * destination region prior to calling this function.
754 *
755 * @param flash_area_id_src The ID of the source flash area.
756 * @param flash_area_id_dst The ID of the destination flash area.
757 * @param off_src The offset within the source flash area to
758 * copy from.
759 * @param off_dst The offset within the destination flash area to
760 * copy to.
761 * @param sz The number of bytes to copy.
762 *
763 * @return 0 on success; nonzero on failure.
764 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300765int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300766boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300767 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300768 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800769 uint32_t off_src, uint32_t off_dst, uint32_t sz)
770{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800771 uint32_t bytes_copied;
772 int chunk_sz;
773 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300774#ifdef MCUBOOT_ENC_IMAGES
775 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300776 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300777 size_t blk_off;
778 struct image_header *hdr;
779 uint16_t idx;
780 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300781 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300782#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800783
Fabio Utzig10ee6482019-08-01 12:04:52 -0300784 TARGET_STATIC uint8_t buf[1024];
785
786#if !defined(MCUBOOT_ENC_IMAGES)
787 (void)state;
788#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789
Christopher Collins92ea77f2016-12-12 15:59:26 -0800790 bytes_copied = 0;
791 while (bytes_copied < sz) {
792 if (sz - bytes_copied > sizeof buf) {
793 chunk_sz = sizeof buf;
794 } else {
795 chunk_sz = sz - bytes_copied;
796 }
797
798 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
799 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300800 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800801 }
802
Fabio Utzigba829042018-09-18 08:29:34 -0300803#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300804 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300805 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
806 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
807 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
808 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100809 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300810 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300811#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300812 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300813 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100814 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300815 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300816 off = off_dst;
817 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300818#else
819 off = off_dst;
820 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
821 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
822 }
823#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200824 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300825 blk_sz = chunk_sz;
826 idx = 0;
827 if (off + bytes_copied < hdr->ih_hdr_size) {
828 /* do not decrypt header */
829 blk_off = 0;
830 blk_sz = chunk_sz - hdr->ih_hdr_size;
831 idx = hdr->ih_hdr_size;
832 } else {
833 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
834 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300835 tlv_off = BOOT_TLV_OFF(hdr);
836 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300837 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300838 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300839 blk_sz = 0;
840 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300841 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300842 }
843 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300844 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300845 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
846 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300847 }
848 }
849#endif
850
Christopher Collins92ea77f2016-12-12 15:59:26 -0800851 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
852 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300853 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800854 }
855
856 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300857
858 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800859 }
860
Fabio Utzigba829042018-09-18 08:29:34 -0300861 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800862}
863
Christopher Collins92ea77f2016-12-12 15:59:26 -0800864/**
David Vincze2d736ad2019-02-18 11:50:22 +0100865 * Overwrite primary slot with the image contained in the secondary slot.
866 * If a prior copy operation was interrupted by a system reset, this function
867 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800868 *
869 * @param bs The current boot status. This function reads
870 * this struct to determine if it is resuming
871 * an interrupted swap operation. This
872 * function writes the updated status to this
873 * function on return.
874 *
875 * @return 0 on success; nonzero on failure.
876 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200877#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600878static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300879boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600880{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400881 size_t sect_count;
882 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600883 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300884 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400885 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300886 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100887 const struct flash_area *fap_primary_slot;
888 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300889 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100890
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200891 (void)bs;
892
Fabio Utzig13d9e352017-10-05 20:32:31 -0300893#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
894 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300895 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300896 assert(rc == 0);
897#endif
David Brown17609d82017-05-05 09:41:34 -0600898
David Vincze2d736ad2019-02-18 11:50:22 +0100899 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
900 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600901
Fabio Utzigb0f04732019-07-31 09:49:19 -0300902 image_index = BOOT_CURR_IMG(state);
903
904 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
905 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300906 assert (rc == 0);
907
Fabio Utzigb0f04732019-07-31 09:49:19 -0300908 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
909 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300910 assert (rc == 0);
911
Fabio Utzig10ee6482019-08-01 12:04:52 -0300912 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300913 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300914 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300915 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600916 assert(rc == 0);
917
918 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300919
920#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
921 if (size >= src_size) {
922 break;
923 }
924#endif
David Brown17609d82017-05-05 09:41:34 -0600925 }
926
Fabio Utzigba829042018-09-18 08:29:34 -0300927#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300928 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300929 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300930 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300931 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100932
Fabio Utzigba829042018-09-18 08:29:34 -0300933 if (rc < 0) {
934 return BOOT_EBADIMAGE;
935 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300936 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300937 return BOOT_EBADIMAGE;
938 }
939 }
940#endif
941
David Vincze2d736ad2019-02-18 11:50:22 +0100942 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
943 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300944 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600945
David Vinczec3084132020-02-18 14:50:47 +0100946#ifdef MCUBOOT_HW_ROLLBACK_PROT
947 /* Update the stored security counter with the new image's security counter
948 * value. Both slots hold the new image at this point, but the secondary
949 * slot's image header must be passed since the image headers in the
950 * boot_data structure have not been updated yet.
951 */
952 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
953 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
954 if (rc != 0) {
955 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
956 return rc;
957 }
958#endif /* MCUBOOT_HW_ROLLBACK_PROT */
959
Fabio Utzig13d9e352017-10-05 20:32:31 -0300960 /*
961 * Erases header and trailer. The trailer is erased because when a new
962 * image is written without a trailer as is the case when using newt, the
963 * trailer that was left might trigger a new upgrade.
964 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700965 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300966 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300967 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
968 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600969 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300970 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700971 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300972 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300973 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
974 last_sector),
975 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
976 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300977 assert(rc == 0);
978
David Vincze2d736ad2019-02-18 11:50:22 +0100979 flash_area_close(fap_primary_slot);
980 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300981
David Vincze2d736ad2019-02-18 11:50:22 +0100982 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600983
984 return 0;
985}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200986#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300987
Christopher Collinsa1c12042019-05-23 14:00:28 -0700988#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300989/**
990 * Swaps the two images in flash. If a prior copy operation was interrupted
991 * by a system reset, this function completes that operation.
992 *
993 * @param bs The current boot status. This function reads
994 * this struct to determine if it is resuming
995 * an interrupted swap operation. This
996 * function writes the updated status to this
997 * function on return.
998 *
999 * @return 0 on success; nonzero on failure.
1000 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001001static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001002boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001003{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001004 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001005#ifdef MCUBOOT_ENC_IMAGES
1006 const struct flash_area *fap;
1007 uint8_t slot;
1008 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001009#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001010 uint32_t size;
1011 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001012 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001013 int rc;
1014
1015 /* FIXME: just do this if asked by user? */
1016
1017 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001018 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001019
Fabio Utzig12d59162019-11-28 10:01:59 -03001020 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001021 /*
1022 * No swap ever happened, so need to find the largest image which
1023 * will be used to determine the amount of sectors to swap.
1024 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001025 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001026 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001027 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001028 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001029 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001030
Fabio Utzigba829042018-09-18 08:29:34 -03001031#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001032 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001033 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001034 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001035 assert(rc >= 0);
1036
1037 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001038 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001039 assert(rc == 0);
1040 } else {
1041 rc = 0;
1042 }
1043 } else {
1044 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1045 }
1046#endif
1047
Fabio Utzig10ee6482019-08-01 12:04:52 -03001048 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001049 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001050 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001051 assert(rc == 0);
1052 }
1053
Fabio Utzigba829042018-09-18 08:29:34 -03001054#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001055 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001056 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001057 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001058 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001059 assert(rc >= 0);
1060
1061 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001062 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001063 assert(rc == 0);
1064 } else {
1065 rc = 0;
1066 }
1067 } else {
1068 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1069 }
1070#endif
1071
Fabio Utzig46490722017-09-04 15:34:32 -03001072 if (size > copy_size) {
1073 copy_size = size;
1074 }
1075
1076 bs->swap_size = copy_size;
1077 } else {
1078 /*
1079 * If a swap was under way, the swap_size should already be present
1080 * in the trailer...
1081 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001082 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001083 assert(rc == 0);
1084
1085 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001086
1087#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001088 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1089 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001090 assert(rc == 0);
1091
1092 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001093 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001094 break;
1095 }
1096 }
1097
1098 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001099 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001100 }
1101 }
1102#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001103 }
1104
Fabio Utzig12d59162019-11-28 10:01:59 -03001105 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001106
David Vincze2d736ad2019-02-18 11:50:22 +01001107#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001108 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001109 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001110 BOOT_LOG_WRN("%d status write fails performing the swap",
1111 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001112 }
1113#endif
1114
Christopher Collins92ea77f2016-12-12 15:59:26 -08001115 return 0;
1116}
David Brown17609d82017-05-05 09:41:34 -06001117#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001118
David Vinczee32483f2019-06-13 10:46:24 +02001119#if (BOOT_IMAGE_NUMBER > 1)
1120/**
1121 * Check the image dependency whether it is satisfied and modify
1122 * the swap type if necessary.
1123 *
1124 * @param dep Image dependency which has to be verified.
1125 *
1126 * @return 0 on success; nonzero on failure.
1127 */
1128static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001129boot_verify_slot_dependency(struct boot_loader_state *state,
1130 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001131{
1132 struct image_version *dep_version;
1133 size_t dep_slot;
1134 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001135 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001136
1137 /* Determine the source of the image which is the subject of
1138 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001139 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001140 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1141 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001142 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001143
David Vincze8b0b6372020-05-20 19:54:44 +02001144 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1145 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001146 /* Dependency not satisfied.
1147 * Modify the swap type to decrease the version number of the image
1148 * (which will be located in the primary slot after the boot process),
1149 * consequently the number of unsatisfied dependencies will be
1150 * decreased or remain the same.
1151 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001152 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001153 case BOOT_SWAP_TYPE_TEST:
1154 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001155 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001156 break;
1157 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001158 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001159 break;
1160 default:
1161 break;
1162 }
David Vincze8b0b6372020-05-20 19:54:44 +02001163 } else {
1164 /* Dependency satisfied. */
1165 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001166 }
1167
1168 return rc;
1169}
1170
1171/**
1172 * Read all dependency TLVs of an image from the flash and verify
1173 * one after another to see if they are all satisfied.
1174 *
1175 * @param slot Image slot number.
1176 *
1177 * @return 0 on success; nonzero on failure.
1178 */
1179static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001180boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001181{
1182 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001183 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001184 struct image_dependency dep;
1185 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001186 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001187 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001188 int rc;
1189
Fabio Utzig10ee6482019-08-01 12:04:52 -03001190 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001191 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001192 if (rc != 0) {
1193 rc = BOOT_EFLASH;
1194 goto done;
1195 }
1196
Fabio Utzig61fd8882019-09-14 20:00:20 -03001197 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1198 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001199 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001200 goto done;
1201 }
1202
Fabio Utzig61fd8882019-09-14 20:00:20 -03001203 while (true) {
1204 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1205 if (rc < 0) {
1206 return -1;
1207 } else if (rc > 0) {
1208 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001209 break;
1210 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001211
1212 if (len != sizeof(dep)) {
1213 rc = BOOT_EBADIMAGE;
1214 goto done;
1215 }
1216
1217 rc = flash_area_read(fap, off, &dep, len);
1218 if (rc != 0) {
1219 rc = BOOT_EFLASH;
1220 goto done;
1221 }
1222
1223 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1224 rc = BOOT_EBADARGS;
1225 goto done;
1226 }
1227
1228 /* Verify dependency and modify the swap type if not satisfied. */
1229 rc = boot_verify_slot_dependency(state, &dep);
1230 if (rc != 0) {
1231 /* Dependency not satisfied. */
1232 goto done;
1233 }
David Vinczee32483f2019-06-13 10:46:24 +02001234 }
1235
1236done:
1237 flash_area_close(fap);
1238 return rc;
1239}
1240
1241/**
David Vinczee32483f2019-06-13 10:46:24 +02001242 * Iterate over all the images and verify whether the image dependencies in the
1243 * TLV area are all satisfied and update the related swap type if necessary.
1244 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001245static int
1246boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001247{
Erik Johnson49063752020-02-06 09:59:31 -06001248 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001249 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001250
Fabio Utzig10ee6482019-08-01 12:04:52 -03001251 BOOT_CURR_IMG(state) = 0;
1252 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001253 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1254 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1255 slot = BOOT_SECONDARY_SLOT;
1256 } else {
1257 slot = BOOT_PRIMARY_SLOT;
1258 }
1259
1260 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001261 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001262 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001263 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001264 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001265 /* Cannot upgrade due to non-met dependencies, so disable all
1266 * image upgrades.
1267 */
1268 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1269 BOOT_CURR_IMG(state) = idx;
1270 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1271 }
1272 break;
David Vinczee32483f2019-06-13 10:46:24 +02001273 }
1274 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001275 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001276}
1277#endif /* (BOOT_IMAGE_NUMBER > 1) */
1278
Christopher Collins92ea77f2016-12-12 15:59:26 -08001279/**
David Vinczeba3bd602019-06-17 16:01:43 +02001280 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001281 *
David Vinczeba3bd602019-06-17 16:01:43 +02001282 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001283 *
1284 * @return 0 on success; nonzero on failure.
1285 */
1286static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001287boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001288{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001289 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001290#ifndef MCUBOOT_OVERWRITE_ONLY
1291 uint8_t swap_type;
1292#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001293
David Vinczeba3bd602019-06-17 16:01:43 +02001294 /* At this point there are no aborted swaps. */
1295#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001296 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001297#elif defined(MCUBOOT_BOOTSTRAP)
1298 /* Check if the image update was triggered by a bad image in the
1299 * primary slot (the validity of the image in the secondary slot had
1300 * already been checked).
1301 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001302 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1303 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1304 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001305 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001306 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001307 }
1308#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001309 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001310#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001311 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001312
1313#ifndef MCUBOOT_OVERWRITE_ONLY
1314 /* The following state needs image_ok be explicitly set after the
1315 * swap was finished to avoid a new revert.
1316 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001317 swap_type = BOOT_SWAP_TYPE(state);
1318 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1319 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001320 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001321 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001322 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001323 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001324 }
1325
David Vinczec3084132020-02-18 14:50:47 +01001326#ifdef MCUBOOT_HW_ROLLBACK_PROT
1327 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1328 /* Update the stored security counter with the new image's security
1329 * counter value. The primary slot holds the new image at this point,
1330 * but the secondary slot's image header must be passed since image
1331 * headers in the boot_data structure have not been updated yet.
1332 *
1333 * In case of a permanent image swap mcuboot will never attempt to
1334 * revert the images on the next reboot. Therefore, the security
1335 * counter must be increased right after the image upgrade.
1336 */
1337 rc = boot_update_security_counter(
1338 BOOT_CURR_IMG(state),
1339 BOOT_PRIMARY_SLOT,
1340 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1341 if (rc != 0) {
1342 BOOT_LOG_ERR("Security counter update failed after "
1343 "image upgrade.");
1344 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1345 }
1346 }
1347#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1348
Fabio Utzige575b0b2019-09-11 12:34:23 -03001349 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001350 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001351 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001352 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001353 }
David Vinczeba3bd602019-06-17 16:01:43 +02001354 }
1355#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001356
David Vinczeba3bd602019-06-17 16:01:43 +02001357 return rc;
1358}
1359
1360/**
1361 * Completes a previously aborted image swap.
1362 *
1363 * @param bs The current boot status.
1364 *
1365 * @return 0 on success; nonzero on failure.
1366 */
1367#if !defined(MCUBOOT_OVERWRITE_ONLY)
1368static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001369boot_complete_partial_swap(struct boot_loader_state *state,
1370 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001371{
1372 int rc;
1373
1374 /* Determine the type of swap operation being resumed from the
1375 * `swap-type` trailer field.
1376 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001377 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001378 assert(rc == 0);
1379
Fabio Utzig10ee6482019-08-01 12:04:52 -03001380 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001381
1382 /* The following states need image_ok be explicitly set after the
1383 * swap was finished to avoid a new revert.
1384 */
1385 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1386 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001387 rc = swap_set_image_ok(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;
David Vinczeba3bd602019-06-17 16:01:43 +02001390 }
1391 }
1392
Fabio Utzige575b0b2019-09-11 12:34:23 -03001393 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001394 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001395 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001396 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001397 }
1398 }
1399
Fabio Utzig10ee6482019-08-01 12:04:52 -03001400 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001401 BOOT_LOG_ERR("panic!");
1402 assert(0);
1403
1404 /* Loop forever... */
1405 while (1) {}
1406 }
1407
1408 return rc;
1409}
1410#endif /* !MCUBOOT_OVERWRITE_ONLY */
1411
1412#if (BOOT_IMAGE_NUMBER > 1)
1413/**
1414 * Review the validity of previously determined swap types of other images.
1415 *
1416 * @param aborted_swap The current image upgrade is a
1417 * partial/aborted swap.
1418 */
1419static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001420boot_review_image_swap_types(struct boot_loader_state *state,
1421 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001422{
1423 /* In that case if we rebooted in the middle of an image upgrade process, we
1424 * must review the validity of swap types, that were previously determined
1425 * for other images. The image_ok flag had not been set before the reboot
1426 * for any of the updated images (only the copy_done flag) and thus falsely
1427 * the REVERT swap type has been determined for the previous images that had
1428 * been updated before the reboot.
1429 *
1430 * There are two separate scenarios that we have to deal with:
1431 *
1432 * 1. The reboot has happened during swapping an image:
1433 * The current image upgrade has been determined as a
1434 * partial/aborted swap.
1435 * 2. The reboot has happened between two separate image upgrades:
1436 * In this scenario we must check the swap type of the current image.
1437 * In those cases if it is NONE or REVERT we cannot certainly determine
1438 * the fact of a reboot. In a consistent state images must move in the
1439 * same direction or stay in place, e.g. in practice REVERT and TEST
1440 * swap types cannot be present at the same time. If the swap type of
1441 * the current image is either TEST, PERM or FAIL we must review the
1442 * already determined swap types of other images and set each false
1443 * REVERT swap types to NONE (these images had been successfully
1444 * updated before the system rebooted between two separate image
1445 * upgrades).
1446 */
1447
Fabio Utzig10ee6482019-08-01 12:04:52 -03001448 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001449 /* Nothing to do */
1450 return;
1451 }
1452
1453 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001454 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1455 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001456 /* Nothing to do */
1457 return;
1458 }
1459 }
1460
Fabio Utzig10ee6482019-08-01 12:04:52 -03001461 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1462 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1463 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001464 }
1465 }
1466}
1467#endif
1468
1469/**
1470 * Prepare image to be updated if required.
1471 *
1472 * Prepare image to be updated if required with completing an image swap
1473 * operation if one was aborted and/or determining the type of the
1474 * swap operation. In case of any error set the swap type to NONE.
1475 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001476 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001477 * @param bs Pointer where the read and possibly updated
1478 * boot status can be written to.
1479 */
1480static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001481boot_prepare_image_for_update(struct boot_loader_state *state,
1482 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001483{
1484 int rc;
1485
1486 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001487 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001488 if (rc != 0) {
1489 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1490 " - too small?", BOOT_MAX_IMG_SECTORS);
1491 /* Unable to determine sector layout, continue with next image
1492 * if there is one.
1493 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001494 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001495 return;
1496 }
1497
1498 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001499 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001500 if (rc != 0) {
1501 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001502 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001503 BOOT_CURR_IMG(state));
1504 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001505 return;
1506 }
1507
1508 /* If the current image's slots aren't compatible, no swap is possible.
1509 * Just boot into primary slot.
1510 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001511 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001512 boot_status_reset(bs);
1513
1514#ifndef MCUBOOT_OVERWRITE_ONLY
1515 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001516 if (rc != 0) {
1517 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001518 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001519 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001520 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001521 return;
1522 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001523#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001524
Fabio Utzig74aef312019-11-28 11:05:34 -03001525#ifdef MCUBOOT_SWAP_USING_MOVE
1526 /*
1527 * Must re-read image headers because the boot status might
1528 * have been updated in the previous function call.
1529 */
1530 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1531 if (rc != 0) {
1532 /* Continue with next image if there is one. */
1533 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1534 BOOT_CURR_IMG(state));
1535 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1536 return;
1537 }
1538#endif
1539
David Vinczeba3bd602019-06-17 16:01:43 +02001540 /* Determine if we rebooted in the middle of an image swap
1541 * operation. If a partial swap was detected, complete it.
1542 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001543 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001544
1545#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001546 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001547#endif
1548
1549#ifdef MCUBOOT_OVERWRITE_ONLY
1550 /* Should never arrive here, overwrite-only mode has
1551 * no swap state.
1552 */
1553 assert(0);
1554#else
1555 /* Determine the type of swap operation being resumed from the
1556 * `swap-type` trailer field.
1557 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001558 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001559 assert(rc == 0);
1560#endif
1561 /* Attempt to read an image header from each slot. Ensure that
1562 * image headers in slots are aligned with headers in boot_data.
1563 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001564 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001565 assert(rc == 0);
1566
1567 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001568 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001569 } else {
1570 /* There was no partial swap, determine swap type. */
1571 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001572 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1573 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1574 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001575 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001576 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001577 }
1578
1579#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001580 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001581#endif
1582
1583#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001584 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001585 /* Header checks are done first because they are
1586 * inexpensive. Since overwrite-only copies starting from
1587 * offset 0, if interrupted, it might leave a valid header
1588 * magic, so also run validation on the primary slot to be
1589 * sure it's not OK.
1590 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001591 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1592 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1593 if (boot_img_hdr(state,
1594 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1595 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001596 /* Set swap type to REVERT to overwrite the primary
1597 * slot with the image contained in secondary slot
1598 * and to trigger the explicit setting of the
1599 * image_ok flag.
1600 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001601 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001602 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001603 }
1604 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001605#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001606 }
David Vinczeba3bd602019-06-17 16:01:43 +02001607 } else {
1608 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001609 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001610 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001611}
1612
Christopher Collins92ea77f2016-12-12 15:59:26 -08001613int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001614context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001615{
Marti Bolivar84898652017-06-13 17:20:22 -04001616 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001617 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001618 int rc = -1;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001619 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001620 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001621 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001622
1623 /* The array of slot sectors are defined here (as opposed to file scope) so
1624 * that they don't get allocated for non-boot-loader apps. This is
1625 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001626 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001627 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001628 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1629 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001630#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001631 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001632#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001633
Fabio Utzig10ee6482019-08-01 12:04:52 -03001634 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001635 has_upgrade = false;
1636
1637#if (BOOT_IMAGE_NUMBER == 1)
1638 (void)has_upgrade;
1639#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001640
David Vinczeba3bd602019-06-17 16:01:43 +02001641 /* Iterate over all the images. By the end of the loop the swap type has
1642 * to be determined for each image and all aborted swaps have to be
1643 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001644 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001645 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001646
David Vinczeba3bd602019-06-17 16:01:43 +02001647#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1648 /* The keys used for encryption may no longer be valid (could belong to
1649 * another images). Therefore, mark them as invalid to force their reload
1650 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001651 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001652 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001653#endif
1654
Fabio Utzig10ee6482019-08-01 12:04:52 -03001655 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001656
Fabio Utzig10ee6482019-08-01 12:04:52 -03001657 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001658 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001659 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001660 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001661#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001662 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001663#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001664
1665 /* Open primary and secondary image areas for the duration
1666 * of this call.
1667 */
1668 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001669 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001670 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001671 assert(rc == 0);
1672 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001673#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001674 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001675 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001676 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001677#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001678
1679 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001680 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001681
Fabio Utzige575b0b2019-09-11 12:34:23 -03001682 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001683 has_upgrade = true;
1684 }
David Vinczeba3bd602019-06-17 16:01:43 +02001685 }
1686
David Vinczee32483f2019-06-13 10:46:24 +02001687#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001688 if (has_upgrade) {
1689 /* Iterate over all the images and verify whether the image dependencies
1690 * are all satisfied and update swap type if necessary.
1691 */
1692 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001693 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001694 /*
1695 * It was impossible to upgrade because the expected dependency version
1696 * was not available. Here we already changed the swap_type so that
1697 * instead of asserting the bootloader, we continue and no upgrade is
1698 * performed.
1699 */
1700 rc = 0;
1701 }
1702 }
David Vinczee32483f2019-06-13 10:46:24 +02001703#endif
1704
David Vinczeba3bd602019-06-17 16:01:43 +02001705 /* Iterate over all the images. At this point there are no aborted swaps
1706 * and the swap types are determined for each image. By the end of the loop
1707 * all required update operations will have been finished.
1708 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001709 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001710
1711#if (BOOT_IMAGE_NUMBER > 1)
1712#ifdef MCUBOOT_ENC_IMAGES
1713 /* The keys used for encryption may no longer be valid (could belong to
1714 * another images). Therefore, mark them as invalid to force their reload
1715 * by boot_enc_load().
1716 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001717 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001718#endif /* MCUBOOT_ENC_IMAGES */
1719
1720 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001721 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001722#endif /* (BOOT_IMAGE_NUMBER > 1) */
1723
1724 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001725 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001726
Fabio Utzig10ee6482019-08-01 12:04:52 -03001727 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001728 case BOOT_SWAP_TYPE_NONE:
1729 break;
1730
1731 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1732 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1733 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001734 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001735 assert(rc == 0);
1736 break;
1737
1738 case BOOT_SWAP_TYPE_FAIL:
1739 /* The image in secondary slot was invalid and is now erased. Ensure
1740 * we don't try to boot into it again on the next reboot. Do this by
1741 * pretending we just reverted back to primary slot.
1742 */
1743#ifndef MCUBOOT_OVERWRITE_ONLY
1744 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001745 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001746 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001747 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001748 }
1749#endif /* !MCUBOOT_OVERWRITE_ONLY */
1750 break;
1751
1752 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001753 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001754 }
1755
Fabio Utzig10ee6482019-08-01 12:04:52 -03001756 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001757 BOOT_LOG_ERR("panic!");
1758 assert(0);
1759
1760 /* Loop forever... */
1761 while (1) {}
1762 }
1763 }
1764
1765 /* Iterate over all the images. At this point all required update operations
1766 * have finished. By the end of the loop each image in the primary slot will
1767 * have been re-validated.
1768 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001769 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1770 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001771 /* Attempt to read an image header from each slot. Ensure that image
1772 * headers in slots are aligned with headers in boot_data.
1773 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001774 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001775 if (rc != 0) {
1776 goto out;
1777 }
1778 /* Since headers were reloaded, it can be assumed we just performed
1779 * a swap or overwrite. Now the header info that should be used to
1780 * provide the data for the bootstrap, which previously was at
1781 * secondary slot, was updated to primary slot.
1782 */
1783 }
1784
1785#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001786 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001787 if (rc != 0) {
1788 rc = BOOT_EBADIMAGE;
1789 goto out;
1790 }
1791#else
1792 /* Even if we're not re-validating the primary slot, we could be booting
1793 * onto an empty flash chip. At least do a basic sanity check that
1794 * the magic number on the image is OK.
1795 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001796 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001797 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001798 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1799 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001800 rc = BOOT_EBADIMAGE;
1801 goto out;
1802 }
David Vinczec3084132020-02-18 14:50:47 +01001803#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1804
1805#ifdef MCUBOOT_HW_ROLLBACK_PROT
1806 /* Update the stored security counter with the active image's security
1807 * counter value. It will only be updated if the new security counter is
1808 * greater than the stored value.
1809 *
1810 * In case of a successful image swapping when the swap type is TEST the
1811 * security counter can be increased only after a reset, when the swap
1812 * type is NONE and the image has marked itself "OK" (the image_ok flag
1813 * has been set). This way a "revert" can be performed when it's
1814 * necessary.
1815 */
1816 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1817 rc = boot_update_security_counter(
1818 BOOT_CURR_IMG(state),
1819 BOOT_PRIMARY_SLOT,
1820 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1821 if (rc != 0) {
1822 BOOT_LOG_ERR("Security counter update failed after image "
1823 "validation.");
1824 goto out;
1825 }
1826 }
1827#endif /* MCUBOOT_HW_ROLLBACK_PROT */
David Vincze1cf11b52020-03-24 07:51:09 +01001828
1829#ifdef MCUBOOT_MEASURED_BOOT
1830 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
1831 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1832 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1833 if (rc != 0) {
1834 BOOT_LOG_ERR("Failed to add Image %u data to shared memory area",
1835 BOOT_CURR_IMG(state));
1836 }
1837#endif /* MCUBOOT_MEASURED_BOOT */
1838
1839#ifdef MCUBOOT_DATA_SHARING
1840 rc = boot_save_shared_data(boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1841 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1842 if (rc != 0) {
1843 BOOT_LOG_ERR("Failed to add data to shared memory area.");
1844 }
1845#endif /* MCUBOOT_DATA_SHARING */
David Vinczeba3bd602019-06-17 16:01:43 +02001846 }
1847
Fabio Utzigb0f04732019-07-31 09:49:19 -03001848#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001849 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001850 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001851#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001852
Fabio Utzigf616c542019-12-19 15:23:32 -03001853 /*
1854 * Since the boot_status struct stores plaintext encryption keys, reset
1855 * them here to avoid the possibility of jumping into an image that could
1856 * easily recover them.
1857 */
1858 memset(&bs, 0, sizeof(struct boot_status));
1859
Fabio Utzig10ee6482019-08-01 12:04:52 -03001860 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1861 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1862 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001863
Fabio Utzig298913b2019-08-28 11:22:45 -03001864out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001865 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001866#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001867 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001868#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001869 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001870 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001871 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001872 }
1873 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001874}
1875
1876int
1877split_go(int loader_slot, int split_slot, void **entry)
1878{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001879 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001880 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001881 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001882 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001883 int rc;
1884
Christopher Collins92ea77f2016-12-12 15:59:26 -08001885 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1886 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001887 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001888 }
David Vinczeba3bd602019-06-17 16:01:43 +02001889 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1890 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001891
1892 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1893 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001894 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001895 assert(rc == 0);
1896 split_flash_id = flash_area_id_from_image_slot(split_slot);
1897 rc = flash_area_open(split_flash_id,
1898 &BOOT_IMG_AREA(&boot_data, split_slot));
1899 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001900
1901 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001902 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001903 if (rc != 0) {
1904 rc = SPLIT_GO_ERR;
1905 goto done;
1906 }
1907
Fabio Utzig12d59162019-11-28 10:01:59 -03001908 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001909 if (rc != 0) {
1910 goto done;
1911 }
1912
Christopher Collins92ea77f2016-12-12 15:59:26 -08001913 /* Don't check the bootable image flag because we could really call a
1914 * bootable or non-bootable image. Just validate that the image check
1915 * passes which is distinct from the normal check.
1916 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001917 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001918 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001919 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001920 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001921 if (rc != 0) {
1922 rc = SPLIT_GO_NON_MATCHING;
1923 goto done;
1924 }
1925
Marti Bolivarea088872017-06-12 17:10:49 -04001926 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001927 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001928 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001929 rc = SPLIT_GO_OK;
1930
1931done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001932 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1933 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001934 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001935 return rc;
1936}
David Vinczee574f2d2020-07-10 11:42:03 +02001937
Tamas Banfe031092020-09-10 17:32:39 +02001938#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02001939
1940/**
1941 * Iterates over all slots and determines which contain a firmware image.
1942 *
1943 * @param state Boot loader status information.
1944 * @param slot_usage Pointer to an array, which aim is to carry information
1945 * about slots that contain an image. After return the
1946 * corresponding array elements are set to a non-zero
1947 * value if the given slots are in use (contain a firmware
1948 * image), otherwise they are set to zero.
1949 * @param slot_cnt The number of slots, which can contain firmware images.
1950 * (Equal to or smaller than the size of the
1951 * slot_usage array.)
1952 *
1953 * @return The number of found images.
1954 */
1955static uint32_t
1956boot_get_slot_usage(struct boot_loader_state *state, uint8_t slot_usage[],
1957 uint32_t slot_cnt)
1958{
1959 struct image_header *hdr = NULL;
1960 uint32_t image_cnt = 0;
1961 uint32_t slot;
1962
1963 memset(slot_usage, 0, slot_cnt);
1964
1965 for (slot = 0; slot < slot_cnt; slot++) {
1966 hdr = boot_img_hdr(state, slot);
1967
1968 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
1969 slot_usage[slot] = 1;
1970 image_cnt++;
1971 BOOT_LOG_IMAGE_INFO(slot, hdr);
1972 } else {
1973 BOOT_LOG_INF("%s slot: Image not found", (slot == BOOT_PRIMARY_SLOT)
1974 ? "Primary" : "Secondary");
1975 }
1976 }
1977
1978 return image_cnt;
1979}
1980
Tamas Banfe031092020-09-10 17:32:39 +02001981#ifdef MCUBOOT_RAM_LOAD
1982
1983#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
1984#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
1985#endif
1986
1987/**
1988 * Verifies that the image in a slot will be loaded within the predefined bounds
1989 * that are allowed to be used by executable images.
1990 *
1991 * @param img_dst The address to which the image is going to be copied.
1992 * @param img_sz The size of the image.
1993 *
1994 * @return 0 on success; nonzero on failure.
1995 */
1996static int
1997boot_verify_ram_load_address(uint32_t img_dst, uint32_t img_sz)
1998{
1999 uint32_t img_end_addr;
2000
2001 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2002 return BOOT_EBADIMAGE;
2003 }
2004
2005 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2006 return BOOT_EBADIMAGE;
2007 }
2008
2009 if (img_end_addr > (IMAGE_EXECUTABLE_RAM_START +
2010 IMAGE_EXECUTABLE_RAM_SIZE)) {
2011 return BOOT_EBADIMAGE;
2012 }
2013
2014 return 0;
2015}
2016
2017/**
2018 * Copies an image from a slot in the flash to an SRAM address.
2019 *
2020 * @param slot The flash slot of the image to be copied to SRAM.
2021 * @param img_dst The address at which the image needs to be copied to
2022 * SRAM.
2023 * @param img_sz The size of the image that needs to be copied to SRAM.
2024 *
2025 * @return 0 on success; nonzero on failure.
2026 */
2027static int
2028boot_copy_image_to_sram(int slot, uint32_t img_dst, uint32_t img_sz)
2029{
2030 int rc;
2031 const struct flash_area *fap_src = NULL;
2032
2033 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2034 if (rc != 0) {
2035 return BOOT_EFLASH;
2036 }
2037
2038 /* Direct copy from flash to its new location in SRAM. */
2039 rc = flash_area_read(fap_src, 0, (void *)img_dst, img_sz);
2040 if (rc != 0) {
2041 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2042 }
2043
2044 flash_area_close(fap_src);
2045
2046 return rc;
2047}
2048
2049/**
2050 * Copies an image from a slot in the flash to an SRAM address. The load
2051 * address and image size is extracted from the image header.
2052 *
2053 * @param state Boot loader status information.
2054 * @param slot The flash slot of the image to be copied to SRAM.
2055 * @param hdr Pointer to the image header structure of the image
2056 * @param img_dst Pointer to the address at which the image needs to be
2057 * copied to SRAM.
2058 * @param img_sz Pointer to the size of the image that needs to be
2059 * copied to SRAM.
2060 *
2061 * @return 0 on success; nonzero on failure.
2062 */
2063static int
2064boot_load_image_to_sram(struct boot_loader_state *state, uint32_t slot,
2065 struct image_header *hdr, uint32_t *img_dst,
2066 uint32_t *img_sz)
2067{
2068 int rc;
2069
2070 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2071
2072 *img_dst = hdr->ih_load_addr;
2073
2074 rc = boot_read_image_size(state, slot, img_sz);
2075 if (rc != 0) {
2076 return rc;
2077 }
2078
2079 rc = boot_verify_ram_load_address(*img_dst, *img_sz);
2080 if (rc != 0) {
2081 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", *img_dst);
2082 return rc;
2083 }
2084
2085 /* Copy image to the load address from where it currently resides in
2086 * flash.
2087 */
2088 rc = boot_copy_image_to_sram(slot, *img_dst, *img_sz);
2089 if (rc != 0) {
2090 BOOT_LOG_INF("RAM loading to 0x%x is failed.", *img_dst);
2091 } else {
2092 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", *img_dst);
2093 }
2094 } else {
2095 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2096 * MCUBOOT_RAM_LOAD is set.
2097 */
2098 rc = BOOT_EBADIMAGE;
2099 }
2100
2101 return rc;
2102}
2103
2104/**
2105 * Removes an image from SRAM, by overwriting it with zeros.
2106 *
2107 * @param img_dst The address of the image that needs to be removed from
2108 * SRAM.
2109 * @param img_sz The size of the image that needs to be removed from
2110 * SRAM.
2111 *
2112 * @return 0 on success; nonzero on failure.
2113 */
2114static inline int
2115boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
2116{
2117 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2118 memset((void*)img_dst, 0, img_sz);
2119
2120 return 0;
2121}
2122#endif /* MCUBOOT_RAM_LOAD */
2123
David Vinczee574f2d2020-07-10 11:42:03 +02002124int
2125context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2126{
2127 struct image_header *hdr = NULL;
2128 struct image_header *selected_image_header = NULL;
2129 uint8_t slot_usage[BOOT_NUM_SLOTS];
2130 uint32_t selected_slot;
2131 uint32_t slot;
2132 uint32_t img_cnt;
2133 uint32_t i;
2134 int fa_id;
2135 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002136#ifdef MCUBOOT_RAM_LOAD
2137 uint32_t img_dst;
2138 uint32_t img_sz;
2139 uint32_t img_loaded = 0;
2140#endif /* MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002141
2142 memset(state, 0, sizeof(struct boot_loader_state));
2143
2144 /* Open primary and secondary image areas for the duration
2145 * of this call.
2146 */
2147 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2148 fa_id = flash_area_id_from_image_slot(slot);
2149 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2150 assert(rc == 0);
2151 }
2152
2153 /* Attempt to read an image header from each slot. */
2154 rc = boot_read_image_headers(state, false, NULL);
2155 if (rc != 0) {
2156 BOOT_LOG_WRN("Failed reading image headers.");
2157 goto out;
2158 }
2159
2160 img_cnt = boot_get_slot_usage(state, slot_usage,
2161 sizeof(slot_usage)/sizeof(slot_usage[0]));
2162
2163 if (img_cnt) {
2164 /* Select the newest and valid image. */
2165 for (i = 0; i < img_cnt; i++) {
2166 selected_slot = 0;
2167 selected_image_header = NULL;
2168
2169 /* Iterate over all the slots that are in use (contain an image)
2170 * and select the one that holds the newest image.
2171 */
2172 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2173 if (slot_usage[slot]) {
2174 hdr = boot_img_hdr(state, slot);
2175 if (selected_image_header != NULL) {
2176 rc = boot_version_cmp(&hdr->ih_ver,
2177 &selected_image_header->ih_ver);
2178 if (rc < 1) {
2179 /* The version of the image being examined wasn't
2180 * greater than the currently selected image's
2181 * version.
2182 */
2183 continue;
2184 }
2185 }
2186 selected_slot = slot;
2187 selected_image_header = hdr;
2188 }
2189 }
Tamas Banfe031092020-09-10 17:32:39 +02002190#ifdef MCUBOOT_RAM_LOAD
2191 /* Image is first loaded to RAM and authenticated there in order to
2192 * prevent TOCTOU attack during image copy. This could be applied
2193 * when loading images from external (untrusted) flash to internal
2194 * (trusted) RAM and image is authenticated before copying.
2195 */
2196 rc = boot_load_image_to_sram(state, selected_slot,
2197 selected_image_header, &img_dst,
2198 &img_sz);
2199 if (rc != 0 ) {
2200 /* Image loading failed try the next one. */
2201 continue;
2202 } else {
2203 img_loaded = 1;
2204 }
2205#endif /* MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002206 rc = boot_validate_slot(state, selected_slot, NULL);
2207 if (rc == 0) {
2208 /* If a valid image is found then there is no reason to check
2209 * the rest of the images, as each of them has a smaller version
2210 * number.
2211 */
2212 break;
2213 }
Tamas Banfe031092020-09-10 17:32:39 +02002214#ifdef MCUBOOT_RAM_LOAD
2215 else if (img_loaded) {
2216 /* If an image is found to be invalid then it is removed from
2217 * RAM to prevent it being a shellcode vector.
2218 */
2219 boot_remove_image_from_sram(img_dst, img_sz);
2220 img_loaded = 0;
2221 }
2222#endif /* MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002223 /* The selected image is invalid, mark its slot as "unused"
2224 * and start over.
2225 */
2226 slot_usage[selected_slot] = 0;
2227 }
2228
2229 if (rc || (selected_image_header == NULL)) {
2230 /* If there was no valid image at all */
2231 rc = BOOT_EBADIMAGE;
2232 goto out;
2233 }
2234
2235#ifdef MCUBOOT_HW_ROLLBACK_PROT
2236 /* Update the stored security counter with the newer (active) image's
2237 * security counter value.
2238 */
2239 rc = boot_update_security_counter(0, selected_slot,
2240 selected_image_header);
2241 if (rc != 0) {
2242 BOOT_LOG_ERR("Security counter update failed after image "
2243 "validation.");
2244 goto out;
2245 }
2246#endif /* MCUBOOT_HW_ROLLBACK_PROT */
2247
2248#ifdef MCUBOOT_MEASURED_BOOT
2249 rc = boot_save_boot_status(0, selected_image_header,
2250 BOOT_IMG_AREA(state, selected_slot));
2251 if (rc != 0) {
2252 BOOT_LOG_ERR("Failed to add image data to shared area");
2253 }
2254#endif /* MCUBOOT_MEASURED_BOOT */
2255
2256#ifdef MCUBOOT_DATA_SHARING
2257 rc = boot_save_shared_data(selected_image_header,
2258 BOOT_IMG_AREA(state, selected_slot));
2259 if (rc != 0) {
2260 BOOT_LOG_ERR("Failed to add data to shared memory area.");
2261 }
2262#endif /* MCUBOOT_DATA_SHARING */
2263
2264 BOOT_LOG_INF("Booting image from the %s slot",
2265 (selected_slot == BOOT_PRIMARY_SLOT) ?
2266 "primary" : "secondary");
2267
2268 rsp->br_flash_dev_id =
2269 BOOT_IMG_AREA(state, selected_slot)->fa_device_id;
2270 rsp->br_image_off = boot_img_slot_off(state, selected_slot);
2271 rsp->br_hdr = selected_image_header;
2272 } else {
2273 /* No candidate image available */
2274 rc = BOOT_EBADIMAGE;
2275 goto out;
2276 }
2277
2278out:
2279 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2280 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
2281 }
2282 return rc;
2283}
Tamas Banfe031092020-09-10 17:32:39 +02002284#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002285
2286/**
2287 * Prepares the booting process. This function moves images around in flash as
2288 * appropriate, and tells you what address to boot from.
2289 *
2290 * @param rsp On success, indicates how booting should occur.
2291 *
2292 * @return 0 on success; nonzero on failure.
2293 */
2294int
2295boot_go(struct boot_rsp *rsp)
2296{
2297 return context_boot_go(&boot_data, rsp);
2298}