blob: 1094ff25763f128b7c79f6583e845fe4902f0b81 [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 Brownf5b33d82017-09-01 10:58:27 -060077/*
78 * Compute the total size of the given image. Includes the size of
79 * the TLVs.
80 */
Fabio Utzig13d9e352017-10-05 20:32:31 -030081#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -060082static int
Fabio Utzigd638b172019-08-09 10:38:05 -030083boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -060084{
85 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -030086 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -030087 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030088 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -060089 int area_id;
90 int rc;
91
Fabio Utzig10ee6482019-08-01 12:04:52 -030092#if (BOOT_IMAGE_NUMBER == 1)
93 (void)state;
94#endif
95
96 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -060097 rc = flash_area_open(area_id, &fap);
98 if (rc != 0) {
99 rc = BOOT_EFLASH;
100 goto done;
101 }
102
Fabio Utzig61fd8882019-09-14 20:00:20 -0300103 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
104
105 if (flash_area_read(fap, off, &info, sizeof(info))) {
106 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600107 goto done;
108 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300109
Fabio Utzige52c08e2019-09-11 19:32:00 -0300110 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
111 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
112 if (protect_tlv_size != info.it_tlv_tot) {
113 rc = BOOT_EBADIMAGE;
114 goto done;
115 }
116
117 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
118 rc = BOOT_EFLASH;
119 goto done;
120 }
121 } else if (protect_tlv_size != 0) {
122 rc = BOOT_EBADIMAGE;
123 goto done;
124 }
125
Fabio Utzig61fd8882019-09-14 20:00:20 -0300126 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
127 rc = BOOT_EBADIMAGE;
128 goto done;
129 }
130
Fabio Utzige52c08e2019-09-11 19:32:00 -0300131 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600132 rc = 0;
133
134done:
135 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300136 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600137}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300138#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600139
Fabio Utzigc08ed212017-06-20 19:28:36 -0300140static int
Fabio Utzig12d59162019-11-28 10:01:59 -0300141boot_read_image_headers(struct boot_loader_state *state, bool require_all,
142 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143{
144 int rc;
145 int i;
146
147 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig12d59162019-11-28 10:01:59 -0300148 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800149 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200150 /* If `require_all` is set, fail on any single fail, otherwise
151 * if at least the first slot's header was read successfully,
152 * then the boot loader can attempt a boot.
153 *
154 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800155 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200156 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800157 return 0;
158 } else {
159 return rc;
160 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800161 }
162 }
163
164 return 0;
165}
166
David Brownab449182019-11-15 09:32:52 -0700167static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300168boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800169{
David Brownab449182019-11-15 09:32:52 -0700170 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300171#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700172 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300173#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800174
175 /* Figure out what size to write update status update as. The size depends
176 * on what the minimum write size is for scratch area, active image slot.
177 * We need to use the bigger of those 2 values.
178 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300179 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300180#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300181 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800182 if (align > elem_sz) {
183 elem_sz = align;
184 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300185#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800186
187 return elem_sz;
188}
189
Fabio Utzig10ee6482019-08-01 12:04:52 -0300190#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
191static int
192boot_initialize_area(struct boot_loader_state *state, int flash_area)
193{
194 int num_sectors = BOOT_MAX_IMG_SECTORS;
195 int rc;
196
197 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
198 rc = flash_area_to_sectors(flash_area, &num_sectors,
199 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
200 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
201
202 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
203 rc = flash_area_to_sectors(flash_area, &num_sectors,
204 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
205 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
206
Fabio Utzig12d59162019-11-28 10:01:59 -0300207#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300208 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
209 rc = flash_area_to_sectors(flash_area, &num_sectors,
210 state->scratch.sectors);
211 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300212#endif
213
Fabio Utzig10ee6482019-08-01 12:04:52 -0300214 } else {
215 return BOOT_EFLASH;
216 }
217
218 return rc;
219}
220#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
221static int
222boot_initialize_area(struct boot_loader_state *state, int flash_area)
223{
224 uint32_t num_sectors;
225 struct flash_sector *out_sectors;
226 size_t *out_num_sectors;
227 int rc;
228
229 num_sectors = BOOT_MAX_IMG_SECTORS;
230
231 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
232 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
233 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
234 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
235 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
236 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300237#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300238 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
239 out_sectors = state->scratch.sectors;
240 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300241#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300242 } else {
243 return BOOT_EFLASH;
244 }
245
246 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
247 if (rc != 0) {
248 return rc;
249 }
250 *out_num_sectors = num_sectors;
251 return 0;
252}
253#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
254
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255/**
256 * Determines the sector layout of both image slots and the scratch area.
257 * This information is necessary for calculating the number of bytes to erase
258 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300259 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260 */
261static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300262boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800263{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300264 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800265 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266
Fabio Utzig10ee6482019-08-01 12:04:52 -0300267 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300268
269 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800270 if (rc != 0) {
271 return BOOT_EFLASH;
272 }
273
Fabio Utzig10ee6482019-08-01 12:04:52 -0300274 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800275 if (rc != 0) {
276 return BOOT_EFLASH;
277 }
278
Fabio Utzig12d59162019-11-28 10:01:59 -0300279#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300280 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200281 if (rc != 0) {
282 return BOOT_EFLASH;
283 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300284#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200285
Fabio Utzig10ee6482019-08-01 12:04:52 -0300286 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287
288 return 0;
289}
290
Fabio Utzig12d59162019-11-28 10:01:59 -0300291void
292boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800293{
Fabio Utzig4741c452019-12-19 15:32:41 -0300294#ifdef MCUBOOT_ENC_IMAGES
295 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
296#if MCUBOOT_SWAP_SAVE_ENCTLV
297 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
298#endif
299#endif /* MCUBOOT_ENC_IMAGES */
300
301 bs->use_scratch = 0;
302 bs->swap_size = 0;
303 bs->source = 0;
304
Fabio Utzig74aef312019-11-28 11:05:34 -0300305 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300306 bs->idx = BOOT_STATUS_IDX_0;
307 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700308 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300309}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800310
Fabio Utzig12d59162019-11-28 10:01:59 -0300311bool
312boot_status_is_reset(const struct boot_status *bs)
313{
Fabio Utzig74aef312019-11-28 11:05:34 -0300314 return (bs->op == BOOT_STATUS_OP_MOVE &&
315 bs->idx == BOOT_STATUS_IDX_0 &&
316 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800317}
318
319/**
320 * Writes the supplied boot status to the flash file system. The boot status
321 * contains the current state of an in-progress image copy operation.
322 *
323 * @param bs The boot status to write.
324 *
325 * @return 0 on success; nonzero on failure.
326 */
327int
Fabio Utzig12d59162019-11-28 10:01:59 -0300328boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800329{
330 const struct flash_area *fap;
331 uint32_t off;
332 int area_id;
333 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300334 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700335 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300336 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300338 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100339 * the trailer. Since in the last step the primary slot is erased, the
340 * first two status writes go to the scratch which will be copied to
341 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300342 */
343
Fabio Utzig12d59162019-11-28 10:01:59 -0300344#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300345 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800346 /* Write to scratch. */
347 area_id = FLASH_AREA_IMAGE_SCRATCH;
348 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300349#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100350 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300351 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300352#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800353 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300354#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800355
356 rc = flash_area_open(area_id, &fap);
357 if (rc != 0) {
358 rc = BOOT_EFLASH;
359 goto done;
360 }
361
362 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300363 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200364 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300365 erased_val = flash_area_erased_val(fap);
366 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700367 buf[0] = bs->state;
368
369 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800370 if (rc != 0) {
371 rc = BOOT_EFLASH;
372 goto done;
373 }
374
375 rc = 0;
376
377done:
378 flash_area_close(fap);
379 return rc;
380}
381
382/*
David Vinczec3084132020-02-18 14:50:47 +0100383 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384 */
385static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300386boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
387 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800388{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300389 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300390 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300391 int rc;
392
Fabio Utzig10ee6482019-08-01 12:04:52 -0300393#if (BOOT_IMAGE_NUMBER == 1)
394 (void)state;
395#endif
396
Fabio Utzigba829042018-09-18 08:29:34 -0300397 (void)bs;
398 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300399
400 image_index = BOOT_CURR_IMG(state);
401
402#ifdef MCUBOOT_ENC_IMAGES
403 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300404 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300405 if (rc < 0) {
406 return BOOT_EBADIMAGE;
407 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300408 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300409 return BOOT_EBADIMAGE;
410 }
411 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300412#endif
413
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300414 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300415 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800416 return BOOT_EBADIMAGE;
417 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300418
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419 return 0;
420}
421
422static int
423split_image_check(struct image_header *app_hdr,
424 const struct flash_area *app_fap,
425 struct image_header *loader_hdr,
426 const struct flash_area *loader_fap)
427{
428 static void *tmpbuf;
429 uint8_t loader_hash[32];
430
431 if (!tmpbuf) {
432 tmpbuf = malloc(BOOT_TMPBUF_SZ);
433 if (!tmpbuf) {
434 return BOOT_ENOMEM;
435 }
436 }
437
Fabio Utzig10ee6482019-08-01 12:04:52 -0300438 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
439 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440 return BOOT_EBADIMAGE;
441 }
442
Fabio Utzig10ee6482019-08-01 12:04:52 -0300443 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
444 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800445 return BOOT_EBADIMAGE;
446 }
447
448 return 0;
449}
450
Fabio Utzig338a19f2018-12-03 08:37:08 -0200451/*
David Brown9bf95af2019-10-10 15:36:36 -0600452 * Check that this is a valid header. Valid means that the magic is
453 * correct, and that the sizes/offsets are "sane". Sane means that
454 * there is no overflow on the arithmetic, and that the result fits
455 * within the flash area we are in.
456 */
457static bool
458boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
459{
460 uint32_t size;
461
462 if (hdr->ih_magic != IMAGE_MAGIC) {
463 return false;
464 }
465
466 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
467 return false;
468 }
469
470 if (size >= fap->fa_size) {
471 return false;
472 }
473
474 return true;
475}
476
477/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200478 * Check that a memory area consists of a given value.
479 */
480static inline bool
481boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300482{
483 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200484 uint8_t *p = (uint8_t *)data;
485 for (i = 0; i < len; i++) {
486 if (val != p[i]) {
487 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300488 }
489 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200490 return true;
491}
492
493static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300494boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200495{
496 const struct flash_area *fap;
497 struct image_header *hdr;
498 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300499 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200500 int rc;
501
Fabio Utzig10ee6482019-08-01 12:04:52 -0300502 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300503 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200504 if (rc != 0) {
505 return -1;
506 }
507
508 erased_val = flash_area_erased_val(fap);
509 flash_area_close(fap);
510
Fabio Utzig10ee6482019-08-01 12:04:52 -0300511 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200512 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
513 return -1;
514 }
515
516 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300517}
518
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000519#if (BOOT_IMAGE_NUMBER > 1) || \
520 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
521/**
522 * Check if the version of the image is not older than required.
523 *
524 * @param req Required minimal image version.
525 * @param ver Version of the image to be checked.
526 *
527 * @return 0 if the version is sufficient, nonzero otherwise.
528 */
529static int
530boot_is_version_sufficient(struct image_version *req,
531 struct image_version *ver)
532{
533 if (ver->iv_major > req->iv_major) {
534 return 0;
535 }
536 if (ver->iv_major < req->iv_major) {
537 return BOOT_EBADVERSION;
538 }
539 /* The major version numbers are equal. */
540 if (ver->iv_minor > req->iv_minor) {
541 return 0;
542 }
543 if (ver->iv_minor < req->iv_minor) {
544 return BOOT_EBADVERSION;
545 }
546 /* The minor version numbers are equal. */
547 if (ver->iv_revision < req->iv_revision) {
548 return BOOT_EBADVERSION;
549 }
550
551 return 0;
552}
553#endif
554
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300555/*
556 * Check that there is a valid image in a slot
557 *
558 * @returns
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300559 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300560 * 1 if no bootloable image was found
561 * -1 on any errors
562 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800563static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300564boot_validate_slot(struct boot_loader_state *state, int slot,
565 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566{
567 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400568 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300569 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800570 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300571
Fabio Utzig10ee6482019-08-01 12:04:52 -0300572 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300573 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800574 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300575 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576 }
577
Fabio Utzig10ee6482019-08-01 12:04:52 -0300578 hdr = boot_img_hdr(state, slot);
579 if (boot_check_header_erased(state, slot) == 0 ||
580 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300581
582#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
583 /*
584 * This fixes an issue where an image might be erased, but a trailer
585 * be left behind. It can happen if the image is in the secondary slot
586 * and did not pass validation, in which case the whole slot is erased.
587 * If during the erase operation, a reset occurs, parts of the slot
588 * might have been erased while some did not. The concerning part is
589 * the trailer because it might disable a new image from being loaded
590 * through mcumgr; so we just get rid of the trailer here, if the header
591 * is erased.
592 */
593 if (slot != BOOT_PRIMARY_SLOT) {
594 swap_erase_trailer_sectors(state, fap);
595 }
596#endif
597
David Vincze2d736ad2019-02-18 11:50:22 +0100598 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300599 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200600 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300601 }
602
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000603#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
604 if (slot != BOOT_PRIMARY_SLOT) {
605 /* Check if version of secondary slot is sufficient */
606 rc = boot_is_version_sufficient(
607 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver,
608 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver);
609 if (rc != 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
610 BOOT_LOG_ERR("insufficient version in secondary slot");
611 flash_area_erase(fap, 0, fap->fa_size);
612 /* Image in the secondary slot does not satisfy version requirement.
613 * Erase the image and continue booting from the primary slot.
614 */
615 rc = 1;
616 goto out;
617 }
618 }
619#endif
620
David Brown9bf95af2019-10-10 15:36:36 -0600621 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100622 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700623 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100624 /* Image in the secondary slot is invalid. Erase the image and
625 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700626 */
627 }
David Brown098de832019-12-10 11:58:01 -0700628#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100629 BOOT_LOG_ERR("Image in the %s slot is not valid!",
630 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700631#endif
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000632 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200633 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634 }
635
David Vincze2d736ad2019-02-18 11:50:22 +0100636 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200637 rc = 0;
638
639out:
640 flash_area_close(fap);
641 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800642}
643
644/**
645 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100646 * that a swap operation is required, the image in the secondary slot is checked
647 * for validity. If the image in the secondary slot is invalid, it is erased,
648 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800649 *
650 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
651 */
652static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300653boot_validated_swap_type(struct boot_loader_state *state,
654 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800655{
656 int swap_type;
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300657 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800658
Fabio Utzigb0f04732019-07-31 09:49:19 -0300659 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzige575b0b2019-09-11 12:34:23 -0300660 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100661 /* Boot loader wants to switch to the secondary slot.
662 * Ensure image is valid.
663 */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300664 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
665 if (rc == 1) {
666 swap_type = BOOT_SWAP_TYPE_NONE;
667 } else if (rc != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300668 swap_type = BOOT_SWAP_TYPE_FAIL;
669 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800670 }
671
672 return swap_type;
673}
674
David Vinczec3084132020-02-18 14:50:47 +0100675#ifdef MCUBOOT_HW_ROLLBACK_PROT
676/**
677 * Updates the stored security counter value with the image's security counter
678 * value which resides in the given slot, only if it's greater than the stored
679 * value.
680 *
681 * @param image_index Index of the image to determine which security
682 * counter to update.
683 * @param slot Slot number of the image.
684 * @param hdr Pointer to the image header structure of the image
685 * that is currently stored in the given slot.
686 *
687 * @return 0 on success; nonzero on failure.
688 */
689static int
690boot_update_security_counter(uint8_t image_index, int slot,
691 struct image_header *hdr)
692{
693 const struct flash_area *fap = NULL;
694 uint32_t img_security_cnt;
695 int rc;
696
697 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
698 &fap);
699 if (rc != 0) {
700 rc = BOOT_EFLASH;
701 goto done;
702 }
703
704 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
705 if (rc != 0) {
706 goto done;
707 }
708
709 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
710 if (rc != 0) {
711 goto done;
712 }
713
714done:
715 flash_area_close(fap);
716 return rc;
717}
718#endif /* MCUBOOT_HW_ROLLBACK_PROT */
719
Christopher Collins92ea77f2016-12-12 15:59:26 -0800720/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800721 * Erases a region of flash.
722 *
Fabio Utzigba829042018-09-18 08:29:34 -0300723 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800724 * @param off The offset within the flash area to start the
725 * erase.
726 * @param sz The number of bytes to erase.
727 *
728 * @return 0 on success; nonzero on failure.
729 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300730int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300731boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800732{
Fabio Utzigba829042018-09-18 08:29:34 -0300733 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800734}
735
736/**
737 * Copies the contents of one flash region to another. You must erase the
738 * destination region prior to calling this function.
739 *
740 * @param flash_area_id_src The ID of the source flash area.
741 * @param flash_area_id_dst The ID of the destination flash area.
742 * @param off_src The offset within the source flash area to
743 * copy from.
744 * @param off_dst The offset within the destination flash area to
745 * copy to.
746 * @param sz The number of bytes to copy.
747 *
748 * @return 0 on success; nonzero on failure.
749 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300750int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300751boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300752 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300753 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800754 uint32_t off_src, uint32_t off_dst, uint32_t sz)
755{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800756 uint32_t bytes_copied;
757 int chunk_sz;
758 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300759#ifdef MCUBOOT_ENC_IMAGES
760 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300761 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300762 size_t blk_off;
763 struct image_header *hdr;
764 uint16_t idx;
765 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300766 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300767#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800768
Fabio Utzig10ee6482019-08-01 12:04:52 -0300769 TARGET_STATIC uint8_t buf[1024];
770
771#if !defined(MCUBOOT_ENC_IMAGES)
772 (void)state;
773#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800774
Christopher Collins92ea77f2016-12-12 15:59:26 -0800775 bytes_copied = 0;
776 while (bytes_copied < sz) {
777 if (sz - bytes_copied > sizeof buf) {
778 chunk_sz = sizeof buf;
779 } else {
780 chunk_sz = sz - bytes_copied;
781 }
782
783 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
784 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300785 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800786 }
787
Fabio Utzigba829042018-09-18 08:29:34 -0300788#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300789 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300790 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
791 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
792 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
793 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100794 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300795 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300796#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300797 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300798 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100799 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300800 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300801 off = off_dst;
802 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300803#else
804 off = off_dst;
805 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
806 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
807 }
808#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200809 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300810 blk_sz = chunk_sz;
811 idx = 0;
812 if (off + bytes_copied < hdr->ih_hdr_size) {
813 /* do not decrypt header */
814 blk_off = 0;
815 blk_sz = chunk_sz - hdr->ih_hdr_size;
816 idx = hdr->ih_hdr_size;
817 } else {
818 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
819 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300820 tlv_off = BOOT_TLV_OFF(hdr);
821 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300822 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300823 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300824 blk_sz = 0;
825 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300826 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300827 }
828 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300829 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300830 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
831 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300832 }
833 }
834#endif
835
Christopher Collins92ea77f2016-12-12 15:59:26 -0800836 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
837 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300838 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800839 }
840
841 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300842
843 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800844 }
845
Fabio Utzigba829042018-09-18 08:29:34 -0300846 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800847}
848
Christopher Collins92ea77f2016-12-12 15:59:26 -0800849/**
David Vincze2d736ad2019-02-18 11:50:22 +0100850 * Overwrite primary slot with the image contained in the secondary slot.
851 * If a prior copy operation was interrupted by a system reset, this function
852 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853 *
854 * @param bs The current boot status. This function reads
855 * this struct to determine if it is resuming
856 * an interrupted swap operation. This
857 * function writes the updated status to this
858 * function on return.
859 *
860 * @return 0 on success; nonzero on failure.
861 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200862#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600863static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300864boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600865{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400866 size_t sect_count;
867 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600868 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300869 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400870 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300871 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100872 const struct flash_area *fap_primary_slot;
873 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300874 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100875
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200876 (void)bs;
877
Fabio Utzig13d9e352017-10-05 20:32:31 -0300878#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
879 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300880 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300881 assert(rc == 0);
882#endif
David Brown17609d82017-05-05 09:41:34 -0600883
David Vincze2d736ad2019-02-18 11:50:22 +0100884 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
885 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600886
Fabio Utzigb0f04732019-07-31 09:49:19 -0300887 image_index = BOOT_CURR_IMG(state);
888
889 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
890 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300891 assert (rc == 0);
892
Fabio Utzigb0f04732019-07-31 09:49:19 -0300893 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
894 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300895 assert (rc == 0);
896
Fabio Utzig10ee6482019-08-01 12:04:52 -0300897 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300898 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300899 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300900 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600901 assert(rc == 0);
902
903 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300904
905#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
906 if (size >= src_size) {
907 break;
908 }
909#endif
David Brown17609d82017-05-05 09:41:34 -0600910 }
911
Fabio Utzigba829042018-09-18 08:29:34 -0300912#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300913 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300914 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300915 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300916 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100917
Fabio Utzigba829042018-09-18 08:29:34 -0300918 if (rc < 0) {
919 return BOOT_EBADIMAGE;
920 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300921 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300922 return BOOT_EBADIMAGE;
923 }
924 }
925#endif
926
David Vincze2d736ad2019-02-18 11:50:22 +0100927 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
928 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300929 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600930
David Vinczec3084132020-02-18 14:50:47 +0100931#ifdef MCUBOOT_HW_ROLLBACK_PROT
932 /* Update the stored security counter with the new image's security counter
933 * value. Both slots hold the new image at this point, but the secondary
934 * slot's image header must be passed since the image headers in the
935 * boot_data structure have not been updated yet.
936 */
937 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
938 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
939 if (rc != 0) {
940 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
941 return rc;
942 }
943#endif /* MCUBOOT_HW_ROLLBACK_PROT */
944
Fabio Utzig13d9e352017-10-05 20:32:31 -0300945 /*
946 * Erases header and trailer. The trailer is erased because when a new
947 * image is written without a trailer as is the case when using newt, the
948 * trailer that was left might trigger a new upgrade.
949 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700950 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300951 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300952 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
953 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600954 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300955 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700956 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300957 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300958 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
959 last_sector),
960 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
961 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300962 assert(rc == 0);
963
David Vincze2d736ad2019-02-18 11:50:22 +0100964 flash_area_close(fap_primary_slot);
965 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300966
David Vincze2d736ad2019-02-18 11:50:22 +0100967 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600968
969 return 0;
970}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200971#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300972
Christopher Collinsa1c12042019-05-23 14:00:28 -0700973#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300974/**
975 * Swaps the two images in flash. If a prior copy operation was interrupted
976 * by a system reset, this function completes that operation.
977 *
978 * @param bs The current boot status. This function reads
979 * this struct to determine if it is resuming
980 * an interrupted swap operation. This
981 * function writes the updated status to this
982 * function on return.
983 *
984 * @return 0 on success; nonzero on failure.
985 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800986static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300987boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800988{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300989 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -0300990#ifdef MCUBOOT_ENC_IMAGES
991 const struct flash_area *fap;
992 uint8_t slot;
993 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -0300994#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300995 uint32_t size;
996 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300997 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300998 int rc;
999
1000 /* FIXME: just do this if asked by user? */
1001
1002 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001003 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001004
Fabio Utzig12d59162019-11-28 10:01:59 -03001005 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001006 /*
1007 * No swap ever happened, so need to find the largest image which
1008 * will be used to determine the amount of sectors to swap.
1009 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001010 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001011 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001012 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001013 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001014 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001015
Fabio Utzigba829042018-09-18 08:29:34 -03001016#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001017 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001018 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001019 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001020 assert(rc >= 0);
1021
1022 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001023 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001024 assert(rc == 0);
1025 } else {
1026 rc = 0;
1027 }
1028 } else {
1029 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1030 }
1031#endif
1032
Fabio Utzig10ee6482019-08-01 12:04:52 -03001033 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001034 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001035 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001036 assert(rc == 0);
1037 }
1038
Fabio Utzigba829042018-09-18 08:29:34 -03001039#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001040 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001041 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001042 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001043 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001044 assert(rc >= 0);
1045
1046 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001047 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001048 assert(rc == 0);
1049 } else {
1050 rc = 0;
1051 }
1052 } else {
1053 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1054 }
1055#endif
1056
Fabio Utzig46490722017-09-04 15:34:32 -03001057 if (size > copy_size) {
1058 copy_size = size;
1059 }
1060
1061 bs->swap_size = copy_size;
1062 } else {
1063 /*
1064 * If a swap was under way, the swap_size should already be present
1065 * in the trailer...
1066 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001067 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001068 assert(rc == 0);
1069
1070 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001071
1072#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001073 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1074 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001075 assert(rc == 0);
1076
1077 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001078 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001079 break;
1080 }
1081 }
1082
1083 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001084 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001085 }
1086 }
1087#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001088 }
1089
Fabio Utzig12d59162019-11-28 10:01:59 -03001090 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001091
David Vincze2d736ad2019-02-18 11:50:22 +01001092#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001093 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001094 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001095 BOOT_LOG_WRN("%d status write fails performing the swap",
1096 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001097 }
1098#endif
1099
Christopher Collins92ea77f2016-12-12 15:59:26 -08001100 return 0;
1101}
David Brown17609d82017-05-05 09:41:34 -06001102#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001103
David Vinczee32483f2019-06-13 10:46:24 +02001104#if (BOOT_IMAGE_NUMBER > 1)
1105/**
1106 * Check the image dependency whether it is satisfied and modify
1107 * the swap type if necessary.
1108 *
1109 * @param dep Image dependency which has to be verified.
1110 *
1111 * @return 0 on success; nonzero on failure.
1112 */
1113static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001114boot_verify_slot_dependency(struct boot_loader_state *state,
1115 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001116{
1117 struct image_version *dep_version;
1118 size_t dep_slot;
1119 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001120 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001121
1122 /* Determine the source of the image which is the subject of
1123 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001124 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001125 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1126 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001127 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001128
1129 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1130 if (rc != 0) {
1131 /* Dependency not satisfied.
1132 * Modify the swap type to decrease the version number of the image
1133 * (which will be located in the primary slot after the boot process),
1134 * consequently the number of unsatisfied dependencies will be
1135 * decreased or remain the same.
1136 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001137 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001138 case BOOT_SWAP_TYPE_TEST:
1139 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001140 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001141 break;
1142 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001143 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001144 break;
1145 default:
1146 break;
1147 }
1148 }
1149
1150 return rc;
1151}
1152
1153/**
1154 * Read all dependency TLVs of an image from the flash and verify
1155 * one after another to see if they are all satisfied.
1156 *
1157 * @param slot Image slot number.
1158 *
1159 * @return 0 on success; nonzero on failure.
1160 */
1161static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001162boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001163{
1164 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001165 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001166 struct image_dependency dep;
1167 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001168 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001169 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001170 int rc;
1171
Fabio Utzig10ee6482019-08-01 12:04:52 -03001172 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001173 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001174 if (rc != 0) {
1175 rc = BOOT_EFLASH;
1176 goto done;
1177 }
1178
Fabio Utzig61fd8882019-09-14 20:00:20 -03001179 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1180 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001181 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001182 goto done;
1183 }
1184
Fabio Utzig61fd8882019-09-14 20:00:20 -03001185 while (true) {
1186 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1187 if (rc < 0) {
1188 return -1;
1189 } else if (rc > 0) {
1190 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001191 break;
1192 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001193
1194 if (len != sizeof(dep)) {
1195 rc = BOOT_EBADIMAGE;
1196 goto done;
1197 }
1198
1199 rc = flash_area_read(fap, off, &dep, len);
1200 if (rc != 0) {
1201 rc = BOOT_EFLASH;
1202 goto done;
1203 }
1204
1205 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1206 rc = BOOT_EBADARGS;
1207 goto done;
1208 }
1209
1210 /* Verify dependency and modify the swap type if not satisfied. */
1211 rc = boot_verify_slot_dependency(state, &dep);
1212 if (rc != 0) {
1213 /* Dependency not satisfied. */
1214 goto done;
1215 }
David Vinczee32483f2019-06-13 10:46:24 +02001216 }
1217
1218done:
1219 flash_area_close(fap);
1220 return rc;
1221}
1222
1223/**
David Vinczee32483f2019-06-13 10:46:24 +02001224 * Iterate over all the images and verify whether the image dependencies in the
1225 * TLV area are all satisfied and update the related swap type if necessary.
1226 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001227static int
1228boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001229{
Erik Johnson49063752020-02-06 09:59:31 -06001230 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001231 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001232
Fabio Utzig10ee6482019-08-01 12:04:52 -03001233 BOOT_CURR_IMG(state) = 0;
1234 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001235 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1236 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1237 slot = BOOT_SECONDARY_SLOT;
1238 } else {
1239 slot = BOOT_PRIMARY_SLOT;
1240 }
1241
1242 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001243 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001244 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001245 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001246 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001247 /* Cannot upgrade due to non-met dependencies, so disable all
1248 * image upgrades.
1249 */
1250 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1251 BOOT_CURR_IMG(state) = idx;
1252 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1253 }
1254 break;
David Vinczee32483f2019-06-13 10:46:24 +02001255 } else {
1256 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03001257 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001258 }
1259 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001260 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001261}
1262#endif /* (BOOT_IMAGE_NUMBER > 1) */
1263
Christopher Collins92ea77f2016-12-12 15:59:26 -08001264/**
David Vinczeba3bd602019-06-17 16:01:43 +02001265 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001266 *
David Vinczeba3bd602019-06-17 16:01:43 +02001267 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001268 *
1269 * @return 0 on success; nonzero on failure.
1270 */
1271static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001272boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001273{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001274 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001275#ifndef MCUBOOT_OVERWRITE_ONLY
1276 uint8_t swap_type;
1277#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001278
David Vinczeba3bd602019-06-17 16:01:43 +02001279 /* At this point there are no aborted swaps. */
1280#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001281 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001282#elif defined(MCUBOOT_BOOTSTRAP)
1283 /* Check if the image update was triggered by a bad image in the
1284 * primary slot (the validity of the image in the secondary slot had
1285 * already been checked).
1286 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001287 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1288 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1289 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001290 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001291 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001292 }
1293#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001294 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001295#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001296 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001297
1298#ifndef MCUBOOT_OVERWRITE_ONLY
1299 /* The following state needs image_ok be explicitly set after the
1300 * swap was finished to avoid a new revert.
1301 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001302 swap_type = BOOT_SWAP_TYPE(state);
1303 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1304 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001305 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001306 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001307 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001308 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001309 }
1310
David Vinczec3084132020-02-18 14:50:47 +01001311#ifdef MCUBOOT_HW_ROLLBACK_PROT
1312 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1313 /* Update the stored security counter with the new image's security
1314 * counter value. The primary slot holds the new image at this point,
1315 * but the secondary slot's image header must be passed since image
1316 * headers in the boot_data structure have not been updated yet.
1317 *
1318 * In case of a permanent image swap mcuboot will never attempt to
1319 * revert the images on the next reboot. Therefore, the security
1320 * counter must be increased right after the image upgrade.
1321 */
1322 rc = boot_update_security_counter(
1323 BOOT_CURR_IMG(state),
1324 BOOT_PRIMARY_SLOT,
1325 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1326 if (rc != 0) {
1327 BOOT_LOG_ERR("Security counter update failed after "
1328 "image upgrade.");
1329 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1330 }
1331 }
1332#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1333
Fabio Utzige575b0b2019-09-11 12:34:23 -03001334 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001335 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001336 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001337 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001338 }
David Vinczeba3bd602019-06-17 16:01:43 +02001339 }
1340#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001341
David Vinczeba3bd602019-06-17 16:01:43 +02001342 return rc;
1343}
1344
1345/**
1346 * Completes a previously aborted image swap.
1347 *
1348 * @param bs The current boot status.
1349 *
1350 * @return 0 on success; nonzero on failure.
1351 */
1352#if !defined(MCUBOOT_OVERWRITE_ONLY)
1353static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001354boot_complete_partial_swap(struct boot_loader_state *state,
1355 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001356{
1357 int rc;
1358
1359 /* Determine the type of swap operation being resumed from the
1360 * `swap-type` trailer field.
1361 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001362 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001363 assert(rc == 0);
1364
Fabio Utzig10ee6482019-08-01 12:04:52 -03001365 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001366
1367 /* The following states need image_ok be explicitly set after the
1368 * swap was finished to avoid a new revert.
1369 */
1370 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1371 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001372 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001373 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001374 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001375 }
1376 }
1377
Fabio Utzige575b0b2019-09-11 12:34:23 -03001378 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001379 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001380 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001381 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001382 }
1383 }
1384
Fabio Utzig10ee6482019-08-01 12:04:52 -03001385 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001386 BOOT_LOG_ERR("panic!");
1387 assert(0);
1388
1389 /* Loop forever... */
1390 while (1) {}
1391 }
1392
1393 return rc;
1394}
1395#endif /* !MCUBOOT_OVERWRITE_ONLY */
1396
1397#if (BOOT_IMAGE_NUMBER > 1)
1398/**
1399 * Review the validity of previously determined swap types of other images.
1400 *
1401 * @param aborted_swap The current image upgrade is a
1402 * partial/aborted swap.
1403 */
1404static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001405boot_review_image_swap_types(struct boot_loader_state *state,
1406 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001407{
1408 /* In that case if we rebooted in the middle of an image upgrade process, we
1409 * must review the validity of swap types, that were previously determined
1410 * for other images. The image_ok flag had not been set before the reboot
1411 * for any of the updated images (only the copy_done flag) and thus falsely
1412 * the REVERT swap type has been determined for the previous images that had
1413 * been updated before the reboot.
1414 *
1415 * There are two separate scenarios that we have to deal with:
1416 *
1417 * 1. The reboot has happened during swapping an image:
1418 * The current image upgrade has been determined as a
1419 * partial/aborted swap.
1420 * 2. The reboot has happened between two separate image upgrades:
1421 * In this scenario we must check the swap type of the current image.
1422 * In those cases if it is NONE or REVERT we cannot certainly determine
1423 * the fact of a reboot. In a consistent state images must move in the
1424 * same direction or stay in place, e.g. in practice REVERT and TEST
1425 * swap types cannot be present at the same time. If the swap type of
1426 * the current image is either TEST, PERM or FAIL we must review the
1427 * already determined swap types of other images and set each false
1428 * REVERT swap types to NONE (these images had been successfully
1429 * updated before the system rebooted between two separate image
1430 * upgrades).
1431 */
1432
Fabio Utzig10ee6482019-08-01 12:04:52 -03001433 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001434 /* Nothing to do */
1435 return;
1436 }
1437
1438 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001439 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1440 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001441 /* Nothing to do */
1442 return;
1443 }
1444 }
1445
Fabio Utzig10ee6482019-08-01 12:04:52 -03001446 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1447 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1448 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001449 }
1450 }
1451}
1452#endif
1453
1454/**
1455 * Prepare image to be updated if required.
1456 *
1457 * Prepare image to be updated if required with completing an image swap
1458 * operation if one was aborted and/or determining the type of the
1459 * swap operation. In case of any error set the swap type to NONE.
1460 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001461 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001462 * @param bs Pointer where the read and possibly updated
1463 * boot status can be written to.
1464 */
1465static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001466boot_prepare_image_for_update(struct boot_loader_state *state,
1467 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001468{
1469 int rc;
1470
1471 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001472 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001473 if (rc != 0) {
1474 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1475 " - too small?", BOOT_MAX_IMG_SECTORS);
1476 /* Unable to determine sector layout, continue with next image
1477 * if there is one.
1478 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001479 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001480 return;
1481 }
1482
1483 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001484 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001485 if (rc != 0) {
1486 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001487 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001488 BOOT_CURR_IMG(state));
1489 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001490 return;
1491 }
1492
1493 /* If the current image's slots aren't compatible, no swap is possible.
1494 * Just boot into primary slot.
1495 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001496 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001497 boot_status_reset(bs);
1498
1499#ifndef MCUBOOT_OVERWRITE_ONLY
1500 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001501 if (rc != 0) {
1502 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001503 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001504 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001505 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001506 return;
1507 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001508#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001509
Fabio Utzig74aef312019-11-28 11:05:34 -03001510#ifdef MCUBOOT_SWAP_USING_MOVE
1511 /*
1512 * Must re-read image headers because the boot status might
1513 * have been updated in the previous function call.
1514 */
1515 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1516 if (rc != 0) {
1517 /* Continue with next image if there is one. */
1518 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1519 BOOT_CURR_IMG(state));
1520 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1521 return;
1522 }
1523#endif
1524
David Vinczeba3bd602019-06-17 16:01:43 +02001525 /* Determine if we rebooted in the middle of an image swap
1526 * operation. If a partial swap was detected, complete it.
1527 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001528 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001529
1530#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001531 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001532#endif
1533
1534#ifdef MCUBOOT_OVERWRITE_ONLY
1535 /* Should never arrive here, overwrite-only mode has
1536 * no swap state.
1537 */
1538 assert(0);
1539#else
1540 /* Determine the type of swap operation being resumed from the
1541 * `swap-type` trailer field.
1542 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001544 assert(rc == 0);
1545#endif
1546 /* Attempt to read an image header from each slot. Ensure that
1547 * image headers in slots are aligned with headers in boot_data.
1548 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001549 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001550 assert(rc == 0);
1551
1552 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001553 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001554 } else {
1555 /* There was no partial swap, determine swap type. */
1556 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001557 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1558 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1559 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001560 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001561 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001562 }
1563
1564#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001565 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001566#endif
1567
1568#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001569 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001570 /* Header checks are done first because they are
1571 * inexpensive. Since overwrite-only copies starting from
1572 * offset 0, if interrupted, it might leave a valid header
1573 * magic, so also run validation on the primary slot to be
1574 * sure it's not OK.
1575 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001576 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1577 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1578 if (boot_img_hdr(state,
1579 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1580 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001581 /* Set swap type to REVERT to overwrite the primary
1582 * slot with the image contained in secondary slot
1583 * and to trigger the explicit setting of the
1584 * image_ok flag.
1585 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001586 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001587 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001588 }
1589 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001590#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001591 }
David Vinczeba3bd602019-06-17 16:01:43 +02001592 } else {
1593 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001594 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001595 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001596}
1597
Christopher Collins92ea77f2016-12-12 15:59:26 -08001598int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001599context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001600{
Marti Bolivar84898652017-06-13 17:20:22 -04001601 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001602 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001603 int rc = -1;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001604 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001605 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001606 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001607
1608 /* The array of slot sectors are defined here (as opposed to file scope) so
1609 * that they don't get allocated for non-boot-loader apps. This is
1610 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001611 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001612 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001613 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1614 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001615#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001616 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001617#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001618
Fabio Utzig10ee6482019-08-01 12:04:52 -03001619 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001620 has_upgrade = false;
1621
1622#if (BOOT_IMAGE_NUMBER == 1)
1623 (void)has_upgrade;
1624#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001625
David Vinczeba3bd602019-06-17 16:01:43 +02001626 /* Iterate over all the images. By the end of the loop the swap type has
1627 * to be determined for each image and all aborted swaps have to be
1628 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001629 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001630 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001631
David Vinczeba3bd602019-06-17 16:01:43 +02001632#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1633 /* The keys used for encryption may no longer be valid (could belong to
1634 * another images). Therefore, mark them as invalid to force their reload
1635 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001636 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001637 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001638#endif
1639
Fabio Utzig10ee6482019-08-01 12:04:52 -03001640 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001641
Fabio Utzig10ee6482019-08-01 12:04:52 -03001642 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001643 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001644 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001645 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001646#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001647 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001648#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001649
1650 /* Open primary and secondary image areas for the duration
1651 * of this call.
1652 */
1653 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001654 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001655 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001656 assert(rc == 0);
1657 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001658#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001659 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001660 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001661 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001662#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001663
1664 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001665 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001666
Fabio Utzige575b0b2019-09-11 12:34:23 -03001667 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001668 has_upgrade = true;
1669 }
David Vinczeba3bd602019-06-17 16:01:43 +02001670 }
1671
David Vinczee32483f2019-06-13 10:46:24 +02001672#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001673 if (has_upgrade) {
1674 /* Iterate over all the images and verify whether the image dependencies
1675 * are all satisfied and update swap type if necessary.
1676 */
1677 rc = boot_verify_dependencies(state);
1678 if (rc == BOOT_EBADVERSION) {
1679 /*
1680 * It was impossible to upgrade because the expected dependency version
1681 * was not available. Here we already changed the swap_type so that
1682 * instead of asserting the bootloader, we continue and no upgrade is
1683 * performed.
1684 */
1685 rc = 0;
1686 }
1687 }
David Vinczee32483f2019-06-13 10:46:24 +02001688#endif
1689
David Vinczeba3bd602019-06-17 16:01:43 +02001690 /* Iterate over all the images. At this point there are no aborted swaps
1691 * and the swap types are determined for each image. By the end of the loop
1692 * all required update operations will have been finished.
1693 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001694 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001695
1696#if (BOOT_IMAGE_NUMBER > 1)
1697#ifdef MCUBOOT_ENC_IMAGES
1698 /* The keys used for encryption may no longer be valid (could belong to
1699 * another images). Therefore, mark them as invalid to force their reload
1700 * by boot_enc_load().
1701 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001702 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001703#endif /* MCUBOOT_ENC_IMAGES */
1704
1705 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001706 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001707#endif /* (BOOT_IMAGE_NUMBER > 1) */
1708
1709 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001710 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001711
Fabio Utzig10ee6482019-08-01 12:04:52 -03001712 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001713 case BOOT_SWAP_TYPE_NONE:
1714 break;
1715
1716 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1717 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1718 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001719 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001720 assert(rc == 0);
1721 break;
1722
1723 case BOOT_SWAP_TYPE_FAIL:
1724 /* The image in secondary slot was invalid and is now erased. Ensure
1725 * we don't try to boot into it again on the next reboot. Do this by
1726 * pretending we just reverted back to primary slot.
1727 */
1728#ifndef MCUBOOT_OVERWRITE_ONLY
1729 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001730 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001731 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001732 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001733 }
1734#endif /* !MCUBOOT_OVERWRITE_ONLY */
1735 break;
1736
1737 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001738 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001739 }
1740
Fabio Utzig10ee6482019-08-01 12:04:52 -03001741 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001742 BOOT_LOG_ERR("panic!");
1743 assert(0);
1744
1745 /* Loop forever... */
1746 while (1) {}
1747 }
1748 }
1749
1750 /* Iterate over all the images. At this point all required update operations
1751 * have finished. By the end of the loop each image in the primary slot will
1752 * have been re-validated.
1753 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001754 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1755 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001756 /* Attempt to read an image header from each slot. Ensure that image
1757 * headers in slots are aligned with headers in boot_data.
1758 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001759 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001760 if (rc != 0) {
1761 goto out;
1762 }
1763 /* Since headers were reloaded, it can be assumed we just performed
1764 * a swap or overwrite. Now the header info that should be used to
1765 * provide the data for the bootstrap, which previously was at
1766 * secondary slot, was updated to primary slot.
1767 */
1768 }
1769
1770#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001771 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001772 if (rc != 0) {
1773 rc = BOOT_EBADIMAGE;
1774 goto out;
1775 }
1776#else
1777 /* Even if we're not re-validating the primary slot, we could be booting
1778 * onto an empty flash chip. At least do a basic sanity check that
1779 * the magic number on the image is OK.
1780 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001781 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001782 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001783 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1784 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001785 rc = BOOT_EBADIMAGE;
1786 goto out;
1787 }
David Vinczec3084132020-02-18 14:50:47 +01001788#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1789
1790#ifdef MCUBOOT_HW_ROLLBACK_PROT
1791 /* Update the stored security counter with the active image's security
1792 * counter value. It will only be updated if the new security counter is
1793 * greater than the stored value.
1794 *
1795 * In case of a successful image swapping when the swap type is TEST the
1796 * security counter can be increased only after a reset, when the swap
1797 * type is NONE and the image has marked itself "OK" (the image_ok flag
1798 * has been set). This way a "revert" can be performed when it's
1799 * necessary.
1800 */
1801 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1802 rc = boot_update_security_counter(
1803 BOOT_CURR_IMG(state),
1804 BOOT_PRIMARY_SLOT,
1805 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1806 if (rc != 0) {
1807 BOOT_LOG_ERR("Security counter update failed after image "
1808 "validation.");
1809 goto out;
1810 }
1811 }
1812#endif /* MCUBOOT_HW_ROLLBACK_PROT */
David Vincze1cf11b52020-03-24 07:51:09 +01001813
1814#ifdef MCUBOOT_MEASURED_BOOT
1815 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
1816 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1817 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1818 if (rc != 0) {
1819 BOOT_LOG_ERR("Failed to add Image %u data to shared memory area",
1820 BOOT_CURR_IMG(state));
1821 }
1822#endif /* MCUBOOT_MEASURED_BOOT */
1823
1824#ifdef MCUBOOT_DATA_SHARING
1825 rc = boot_save_shared_data(boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1826 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1827 if (rc != 0) {
1828 BOOT_LOG_ERR("Failed to add data to shared memory area.");
1829 }
1830#endif /* MCUBOOT_DATA_SHARING */
David Vinczeba3bd602019-06-17 16:01:43 +02001831 }
1832
Fabio Utzigb0f04732019-07-31 09:49:19 -03001833#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001834 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001835 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001836#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001837
Fabio Utzigf616c542019-12-19 15:23:32 -03001838 /*
1839 * Since the boot_status struct stores plaintext encryption keys, reset
1840 * them here to avoid the possibility of jumping into an image that could
1841 * easily recover them.
1842 */
1843 memset(&bs, 0, sizeof(struct boot_status));
1844
Fabio Utzig10ee6482019-08-01 12:04:52 -03001845 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1846 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1847 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001848
Fabio Utzig298913b2019-08-28 11:22:45 -03001849out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001850 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001851#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001852 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001853#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001854 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001855 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001856 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001857 }
1858 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001859}
1860
Fabio Utzig10ee6482019-08-01 12:04:52 -03001861/**
1862 * Prepares the booting process. This function moves images around in flash as
1863 * appropriate, and tells you what address to boot from.
1864 *
1865 * @param rsp On success, indicates how booting should occur.
1866 *
1867 * @return 0 on success; nonzero on failure.
1868 */
1869int
1870boot_go(struct boot_rsp *rsp)
1871{
1872 return context_boot_go(&boot_data, rsp);
1873}
1874
Christopher Collins92ea77f2016-12-12 15:59:26 -08001875int
1876split_go(int loader_slot, int split_slot, void **entry)
1877{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001878 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001879 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001880 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001881 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001882 int rc;
1883
Christopher Collins92ea77f2016-12-12 15:59:26 -08001884 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1885 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001886 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001887 }
David Vinczeba3bd602019-06-17 16:01:43 +02001888 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1889 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001890
1891 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1892 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001893 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001894 assert(rc == 0);
1895 split_flash_id = flash_area_id_from_image_slot(split_slot);
1896 rc = flash_area_open(split_flash_id,
1897 &BOOT_IMG_AREA(&boot_data, split_slot));
1898 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001899
1900 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001901 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001902 if (rc != 0) {
1903 rc = SPLIT_GO_ERR;
1904 goto done;
1905 }
1906
Fabio Utzig12d59162019-11-28 10:01:59 -03001907 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001908 if (rc != 0) {
1909 goto done;
1910 }
1911
Christopher Collins92ea77f2016-12-12 15:59:26 -08001912 /* Don't check the bootable image flag because we could really call a
1913 * bootable or non-bootable image. Just validate that the image check
1914 * passes which is distinct from the normal check.
1915 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001916 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001917 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001918 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001919 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001920 if (rc != 0) {
1921 rc = SPLIT_GO_NON_MATCHING;
1922 goto done;
1923 }
1924
Marti Bolivarea088872017-06-12 17:10:49 -04001925 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001926 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001927 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001928 rc = SPLIT_GO_OK;
1929
1930done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001931 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1932 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001933 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001934 return rc;
1935}