blob: 9d4f0544d0164c29b0f08a8054b711b995322d20 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
David Vinczec3084132020-02-18 14:50:47 +010021 * Modifications are Copyright (c) 2019-2020 Arm Limited.
David Vinczee2453472019-06-17 12:31:59 +020022 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030039#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050040#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010041#include "bootutil/security_cnt.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050042
Fabio Utzigba829042018-09-18 08:29:34 -030043#ifdef MCUBOOT_ENC_IMAGES
44#include "bootutil/enc_key.h"
45#endif
46
Fabio Utzigba1fbe62017-07-21 14:01:20 -030047#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030048
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010049MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
50
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040051static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080052
Fabio Utzigabec0732019-07-31 08:40:22 -030053#if (BOOT_IMAGE_NUMBER > 1)
54#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
55#else
56#define IMAGES_ITER(x)
57#endif
58
Fabio Utzig10ee6482019-08-01 12:04:52 -030059/*
60 * This macro allows some control on the allocation of local variables.
61 * When running natively on a target, we don't want to allocated huge
62 * variables on the stack, so make them global instead. For the simulator
63 * we want to run as many threads as there are tests, and it's safer
64 * to just make those variables stack allocated.
65 */
66#if !defined(__BOOTSIM__)
67#define TARGET_STATIC static
68#else
69#define TARGET_STATIC
70#endif
71
David Brownf5b33d82017-09-01 10:58:27 -060072/*
73 * Compute the total size of the given image. Includes the size of
74 * the TLVs.
75 */
Fabio Utzig13d9e352017-10-05 20:32:31 -030076#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -060077static int
Fabio Utzigd638b172019-08-09 10:38:05 -030078boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -060079{
80 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -030081 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -030082 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030083 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -060084 int area_id;
85 int rc;
86
Fabio Utzig10ee6482019-08-01 12:04:52 -030087#if (BOOT_IMAGE_NUMBER == 1)
88 (void)state;
89#endif
90
91 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -060092 rc = flash_area_open(area_id, &fap);
93 if (rc != 0) {
94 rc = BOOT_EFLASH;
95 goto done;
96 }
97
Fabio Utzig61fd8882019-09-14 20:00:20 -030098 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
99
100 if (flash_area_read(fap, off, &info, sizeof(info))) {
101 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600102 goto done;
103 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300104
Fabio Utzige52c08e2019-09-11 19:32:00 -0300105 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
106 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
107 if (protect_tlv_size != info.it_tlv_tot) {
108 rc = BOOT_EBADIMAGE;
109 goto done;
110 }
111
112 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
113 rc = BOOT_EFLASH;
114 goto done;
115 }
116 } else if (protect_tlv_size != 0) {
117 rc = BOOT_EBADIMAGE;
118 goto done;
119 }
120
Fabio Utzig61fd8882019-09-14 20:00:20 -0300121 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
122 rc = BOOT_EBADIMAGE;
123 goto done;
124 }
125
Fabio Utzige52c08e2019-09-11 19:32:00 -0300126 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600127 rc = 0;
128
129done:
130 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300131 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600132}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300133#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600134
Fabio Utzigc08ed212017-06-20 19:28:36 -0300135static int
Fabio Utzig12d59162019-11-28 10:01:59 -0300136boot_read_image_headers(struct boot_loader_state *state, bool require_all,
137 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800138{
139 int rc;
140 int i;
141
142 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig12d59162019-11-28 10:01:59 -0300143 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800144 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200145 /* If `require_all` is set, fail on any single fail, otherwise
146 * if at least the first slot's header was read successfully,
147 * then the boot loader can attempt a boot.
148 *
149 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800150 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200151 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800152 return 0;
153 } else {
154 return rc;
155 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800156 }
157 }
158
159 return 0;
160}
161
David Brownab449182019-11-15 09:32:52 -0700162static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300163boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800164{
David Brownab449182019-11-15 09:32:52 -0700165 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300166#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700167 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300168#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800169
170 /* Figure out what size to write update status update as. The size depends
171 * on what the minimum write size is for scratch area, active image slot.
172 * We need to use the bigger of those 2 values.
173 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300174 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300175#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300176 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800177 if (align > elem_sz) {
178 elem_sz = align;
179 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300180#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800181
182 return elem_sz;
183}
184
Fabio Utzig10ee6482019-08-01 12:04:52 -0300185#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
186static int
187boot_initialize_area(struct boot_loader_state *state, int flash_area)
188{
189 int num_sectors = BOOT_MAX_IMG_SECTORS;
190 int rc;
191
192 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
193 rc = flash_area_to_sectors(flash_area, &num_sectors,
194 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
195 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
196
197 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
198 rc = flash_area_to_sectors(flash_area, &num_sectors,
199 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
200 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
201
Fabio Utzig12d59162019-11-28 10:01:59 -0300202#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300203 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
204 rc = flash_area_to_sectors(flash_area, &num_sectors,
205 state->scratch.sectors);
206 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300207#endif
208
Fabio Utzig10ee6482019-08-01 12:04:52 -0300209 } else {
210 return BOOT_EFLASH;
211 }
212
213 return rc;
214}
215#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
216static int
217boot_initialize_area(struct boot_loader_state *state, int flash_area)
218{
219 uint32_t num_sectors;
220 struct flash_sector *out_sectors;
221 size_t *out_num_sectors;
222 int rc;
223
224 num_sectors = BOOT_MAX_IMG_SECTORS;
225
226 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
227 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
228 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
229 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
230 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
231 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300232#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300233 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
234 out_sectors = state->scratch.sectors;
235 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300236#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300237 } else {
238 return BOOT_EFLASH;
239 }
240
241 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
242 if (rc != 0) {
243 return rc;
244 }
245 *out_num_sectors = num_sectors;
246 return 0;
247}
248#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
249
Christopher Collins92ea77f2016-12-12 15:59:26 -0800250/**
251 * Determines the sector layout of both image slots and the scratch area.
252 * This information is necessary for calculating the number of bytes to erase
253 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300254 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800255 */
256static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300257boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800258{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300259 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261
Fabio Utzig10ee6482019-08-01 12:04:52 -0300262 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300263
264 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800265 if (rc != 0) {
266 return BOOT_EFLASH;
267 }
268
Fabio Utzig10ee6482019-08-01 12:04:52 -0300269 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800270 if (rc != 0) {
271 return BOOT_EFLASH;
272 }
273
Fabio Utzig12d59162019-11-28 10:01:59 -0300274#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300275 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200276 if (rc != 0) {
277 return BOOT_EFLASH;
278 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300279#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200280
Fabio Utzig10ee6482019-08-01 12:04:52 -0300281 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800282
283 return 0;
284}
285
Fabio Utzig12d59162019-11-28 10:01:59 -0300286void
287boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288{
Fabio Utzig4741c452019-12-19 15:32:41 -0300289#ifdef MCUBOOT_ENC_IMAGES
290 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
291#if MCUBOOT_SWAP_SAVE_ENCTLV
292 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
293#endif
294#endif /* MCUBOOT_ENC_IMAGES */
295
296 bs->use_scratch = 0;
297 bs->swap_size = 0;
298 bs->source = 0;
299
Fabio Utzig74aef312019-11-28 11:05:34 -0300300 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300301 bs->idx = BOOT_STATUS_IDX_0;
302 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700303 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300304}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800305
Fabio Utzig12d59162019-11-28 10:01:59 -0300306bool
307boot_status_is_reset(const struct boot_status *bs)
308{
Fabio Utzig74aef312019-11-28 11:05:34 -0300309 return (bs->op == BOOT_STATUS_OP_MOVE &&
310 bs->idx == BOOT_STATUS_IDX_0 &&
311 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800312}
313
314/**
315 * Writes the supplied boot status to the flash file system. The boot status
316 * contains the current state of an in-progress image copy operation.
317 *
318 * @param bs The boot status to write.
319 *
320 * @return 0 on success; nonzero on failure.
321 */
322int
Fabio Utzig12d59162019-11-28 10:01:59 -0300323boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800324{
325 const struct flash_area *fap;
326 uint32_t off;
327 int area_id;
328 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300329 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700330 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300331 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800332
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300333 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100334 * the trailer. Since in the last step the primary slot is erased, the
335 * first two status writes go to the scratch which will be copied to
336 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300337 */
338
Fabio Utzig12d59162019-11-28 10:01:59 -0300339#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300340 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800341 /* Write to scratch. */
342 area_id = FLASH_AREA_IMAGE_SCRATCH;
343 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300344#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100345 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300346 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300347#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800348 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300349#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800350
351 rc = flash_area_open(area_id, &fap);
352 if (rc != 0) {
353 rc = BOOT_EFLASH;
354 goto done;
355 }
356
357 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300358 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200359 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300360 erased_val = flash_area_erased_val(fap);
361 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700362 buf[0] = bs->state;
363
364 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800365 if (rc != 0) {
366 rc = BOOT_EFLASH;
367 goto done;
368 }
369
370 rc = 0;
371
372done:
373 flash_area_close(fap);
374 return rc;
375}
376
377/*
David Vinczec3084132020-02-18 14:50:47 +0100378 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800379 */
380static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300381boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
382 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800383{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300384 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300385 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300386 int rc;
387
Fabio Utzig10ee6482019-08-01 12:04:52 -0300388#if (BOOT_IMAGE_NUMBER == 1)
389 (void)state;
390#endif
391
Fabio Utzigba829042018-09-18 08:29:34 -0300392 (void)bs;
393 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300394
395 image_index = BOOT_CURR_IMG(state);
396
397#ifdef MCUBOOT_ENC_IMAGES
398 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300399 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300400 if (rc < 0) {
401 return BOOT_EBADIMAGE;
402 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300403 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300404 return BOOT_EBADIMAGE;
405 }
406 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300407#endif
408
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300409 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300410 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800411 return BOOT_EBADIMAGE;
412 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300413
Christopher Collins92ea77f2016-12-12 15:59:26 -0800414 return 0;
415}
416
417static int
418split_image_check(struct image_header *app_hdr,
419 const struct flash_area *app_fap,
420 struct image_header *loader_hdr,
421 const struct flash_area *loader_fap)
422{
423 static void *tmpbuf;
424 uint8_t loader_hash[32];
425
426 if (!tmpbuf) {
427 tmpbuf = malloc(BOOT_TMPBUF_SZ);
428 if (!tmpbuf) {
429 return BOOT_ENOMEM;
430 }
431 }
432
Fabio Utzig10ee6482019-08-01 12:04:52 -0300433 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
434 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800435 return BOOT_EBADIMAGE;
436 }
437
Fabio Utzig10ee6482019-08-01 12:04:52 -0300438 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
439 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800440 return BOOT_EBADIMAGE;
441 }
442
443 return 0;
444}
445
Fabio Utzig338a19f2018-12-03 08:37:08 -0200446/*
David Brown9bf95af2019-10-10 15:36:36 -0600447 * Check that this is a valid header. Valid means that the magic is
448 * correct, and that the sizes/offsets are "sane". Sane means that
449 * there is no overflow on the arithmetic, and that the result fits
450 * within the flash area we are in.
451 */
452static bool
453boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
454{
455 uint32_t size;
456
457 if (hdr->ih_magic != IMAGE_MAGIC) {
458 return false;
459 }
460
461 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
462 return false;
463 }
464
465 if (size >= fap->fa_size) {
466 return false;
467 }
468
469 return true;
470}
471
472/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200473 * Check that a memory area consists of a given value.
474 */
475static inline bool
476boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300477{
478 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200479 uint8_t *p = (uint8_t *)data;
480 for (i = 0; i < len; i++) {
481 if (val != p[i]) {
482 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300483 }
484 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200485 return true;
486}
487
488static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300489boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200490{
491 const struct flash_area *fap;
492 struct image_header *hdr;
493 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300494 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200495 int rc;
496
Fabio Utzig10ee6482019-08-01 12:04:52 -0300497 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300498 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200499 if (rc != 0) {
500 return -1;
501 }
502
503 erased_val = flash_area_erased_val(fap);
504 flash_area_close(fap);
505
Fabio Utzig10ee6482019-08-01 12:04:52 -0300506 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200507 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
508 return -1;
509 }
510
511 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300512}
513
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000514#if (BOOT_IMAGE_NUMBER > 1) || \
515 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
516/**
517 * Check if the version of the image is not older than required.
518 *
519 * @param req Required minimal image version.
520 * @param ver Version of the image to be checked.
521 *
522 * @return 0 if the version is sufficient, nonzero otherwise.
523 */
524static int
525boot_is_version_sufficient(struct image_version *req,
526 struct image_version *ver)
527{
528 if (ver->iv_major > req->iv_major) {
529 return 0;
530 }
531 if (ver->iv_major < req->iv_major) {
532 return BOOT_EBADVERSION;
533 }
534 /* The major version numbers are equal. */
535 if (ver->iv_minor > req->iv_minor) {
536 return 0;
537 }
538 if (ver->iv_minor < req->iv_minor) {
539 return BOOT_EBADVERSION;
540 }
541 /* The minor version numbers are equal. */
542 if (ver->iv_revision < req->iv_revision) {
543 return BOOT_EBADVERSION;
544 }
545
546 return 0;
547}
548#endif
549
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300550/*
551 * Check that there is a valid image in a slot
552 *
553 * @returns
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300554 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300555 * 1 if no bootloable image was found
556 * -1 on any errors
557 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800558static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300559boot_validate_slot(struct boot_loader_state *state, int slot,
560 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800561{
562 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400563 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300564 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800565 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300566
Fabio Utzig10ee6482019-08-01 12:04:52 -0300567 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300568 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300570 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800571 }
572
Fabio Utzig10ee6482019-08-01 12:04:52 -0300573 hdr = boot_img_hdr(state, slot);
574 if (boot_check_header_erased(state, slot) == 0 ||
575 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100576 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300577 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200578 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300579 }
580
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000581#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
582 if (slot != BOOT_PRIMARY_SLOT) {
583 /* Check if version of secondary slot is sufficient */
584 rc = boot_is_version_sufficient(
585 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver,
586 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver);
587 if (rc != 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
588 BOOT_LOG_ERR("insufficient version in secondary slot");
589 flash_area_erase(fap, 0, fap->fa_size);
590 /* Image in the secondary slot does not satisfy version requirement.
591 * Erase the image and continue booting from the primary slot.
592 */
593 rc = 1;
594 goto out;
595 }
596 }
597#endif
598
David Brown9bf95af2019-10-10 15:36:36 -0600599 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100600 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700601 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100602 /* Image in the secondary slot is invalid. Erase the image and
603 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700604 */
605 }
David Brown098de832019-12-10 11:58:01 -0700606#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100607 BOOT_LOG_ERR("Image in the %s slot is not valid!",
608 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700609#endif
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000610 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200611 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800612 }
613
David Vincze2d736ad2019-02-18 11:50:22 +0100614 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200615 rc = 0;
616
617out:
618 flash_area_close(fap);
619 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620}
621
622/**
623 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100624 * that a swap operation is required, the image in the secondary slot is checked
625 * for validity. If the image in the secondary slot is invalid, it is erased,
626 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800627 *
628 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
629 */
630static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300631boot_validated_swap_type(struct boot_loader_state *state,
632 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800633{
634 int swap_type;
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300635 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800636
Fabio Utzigb0f04732019-07-31 09:49:19 -0300637 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzige575b0b2019-09-11 12:34:23 -0300638 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100639 /* Boot loader wants to switch to the secondary slot.
640 * Ensure image is valid.
641 */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300642 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
643 if (rc == 1) {
644 swap_type = BOOT_SWAP_TYPE_NONE;
645 } else if (rc != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300646 swap_type = BOOT_SWAP_TYPE_FAIL;
647 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800648 }
649
650 return swap_type;
651}
652
David Vinczec3084132020-02-18 14:50:47 +0100653#ifdef MCUBOOT_HW_ROLLBACK_PROT
654/**
655 * Updates the stored security counter value with the image's security counter
656 * value which resides in the given slot, only if it's greater than the stored
657 * value.
658 *
659 * @param image_index Index of the image to determine which security
660 * counter to update.
661 * @param slot Slot number of the image.
662 * @param hdr Pointer to the image header structure of the image
663 * that is currently stored in the given slot.
664 *
665 * @return 0 on success; nonzero on failure.
666 */
667static int
668boot_update_security_counter(uint8_t image_index, int slot,
669 struct image_header *hdr)
670{
671 const struct flash_area *fap = NULL;
672 uint32_t img_security_cnt;
673 int rc;
674
675 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
676 &fap);
677 if (rc != 0) {
678 rc = BOOT_EFLASH;
679 goto done;
680 }
681
682 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
683 if (rc != 0) {
684 goto done;
685 }
686
687 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
688 if (rc != 0) {
689 goto done;
690 }
691
692done:
693 flash_area_close(fap);
694 return rc;
695}
696#endif /* MCUBOOT_HW_ROLLBACK_PROT */
697
Christopher Collins92ea77f2016-12-12 15:59:26 -0800698/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800699 * Erases a region of flash.
700 *
Fabio Utzigba829042018-09-18 08:29:34 -0300701 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800702 * @param off The offset within the flash area to start the
703 * erase.
704 * @param sz The number of bytes to erase.
705 *
706 * @return 0 on success; nonzero on failure.
707 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300708int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300709boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800710{
Fabio Utzigba829042018-09-18 08:29:34 -0300711 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800712}
713
714/**
715 * Copies the contents of one flash region to another. You must erase the
716 * destination region prior to calling this function.
717 *
718 * @param flash_area_id_src The ID of the source flash area.
719 * @param flash_area_id_dst The ID of the destination flash area.
720 * @param off_src The offset within the source flash area to
721 * copy from.
722 * @param off_dst The offset within the destination flash area to
723 * copy to.
724 * @param sz The number of bytes to copy.
725 *
726 * @return 0 on success; nonzero on failure.
727 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300728int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300729boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300730 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300731 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800732 uint32_t off_src, uint32_t off_dst, uint32_t sz)
733{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800734 uint32_t bytes_copied;
735 int chunk_sz;
736 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300737#ifdef MCUBOOT_ENC_IMAGES
738 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300739 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300740 size_t blk_off;
741 struct image_header *hdr;
742 uint16_t idx;
743 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300744 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300745#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800746
Fabio Utzig10ee6482019-08-01 12:04:52 -0300747 TARGET_STATIC uint8_t buf[1024];
748
749#if !defined(MCUBOOT_ENC_IMAGES)
750 (void)state;
751#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800752
Christopher Collins92ea77f2016-12-12 15:59:26 -0800753 bytes_copied = 0;
754 while (bytes_copied < sz) {
755 if (sz - bytes_copied > sizeof buf) {
756 chunk_sz = sizeof buf;
757 } else {
758 chunk_sz = sz - bytes_copied;
759 }
760
761 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
762 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300763 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800764 }
765
Fabio Utzigba829042018-09-18 08:29:34 -0300766#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300767 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300768 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
769 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
770 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
771 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100772 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300773 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300774#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300775 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300776 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100777 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300778 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300779 off = off_dst;
780 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300781#else
782 off = off_dst;
783 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
784 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
785 }
786#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200787 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300788 blk_sz = chunk_sz;
789 idx = 0;
790 if (off + bytes_copied < hdr->ih_hdr_size) {
791 /* do not decrypt header */
792 blk_off = 0;
793 blk_sz = chunk_sz - hdr->ih_hdr_size;
794 idx = hdr->ih_hdr_size;
795 } else {
796 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
797 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300798 tlv_off = BOOT_TLV_OFF(hdr);
799 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300800 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300801 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300802 blk_sz = 0;
803 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300804 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300805 }
806 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300807 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300808 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
809 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300810 }
811 }
812#endif
813
Christopher Collins92ea77f2016-12-12 15:59:26 -0800814 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
815 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300816 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800817 }
818
819 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300820
821 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800822 }
823
Fabio Utzigba829042018-09-18 08:29:34 -0300824 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800825}
826
Christopher Collins92ea77f2016-12-12 15:59:26 -0800827/**
David Vincze2d736ad2019-02-18 11:50:22 +0100828 * Overwrite primary slot with the image contained in the secondary slot.
829 * If a prior copy operation was interrupted by a system reset, this function
830 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800831 *
832 * @param bs The current boot status. This function reads
833 * this struct to determine if it is resuming
834 * an interrupted swap operation. This
835 * function writes the updated status to this
836 * function on return.
837 *
838 * @return 0 on success; nonzero on failure.
839 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200840#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600841static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300842boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600843{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400844 size_t sect_count;
845 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600846 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300847 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400848 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300849 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100850 const struct flash_area *fap_primary_slot;
851 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300852 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100853
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200854 (void)bs;
855
Fabio Utzig13d9e352017-10-05 20:32:31 -0300856#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
857 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300858 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300859 assert(rc == 0);
860#endif
David Brown17609d82017-05-05 09:41:34 -0600861
David Vincze2d736ad2019-02-18 11:50:22 +0100862 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
863 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600864
Fabio Utzigb0f04732019-07-31 09:49:19 -0300865 image_index = BOOT_CURR_IMG(state);
866
867 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
868 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300869 assert (rc == 0);
870
Fabio Utzigb0f04732019-07-31 09:49:19 -0300871 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
872 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300873 assert (rc == 0);
874
Fabio Utzig10ee6482019-08-01 12:04:52 -0300875 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300876 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300877 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300878 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600879 assert(rc == 0);
880
881 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300882
883#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
884 if (size >= src_size) {
885 break;
886 }
887#endif
David Brown17609d82017-05-05 09:41:34 -0600888 }
889
Fabio Utzigba829042018-09-18 08:29:34 -0300890#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300891 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300892 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300893 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300894 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100895
Fabio Utzigba829042018-09-18 08:29:34 -0300896 if (rc < 0) {
897 return BOOT_EBADIMAGE;
898 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300899 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300900 return BOOT_EBADIMAGE;
901 }
902 }
903#endif
904
David Vincze2d736ad2019-02-18 11:50:22 +0100905 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
906 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300907 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600908
David Vinczec3084132020-02-18 14:50:47 +0100909#ifdef MCUBOOT_HW_ROLLBACK_PROT
910 /* Update the stored security counter with the new image's security counter
911 * value. Both slots hold the new image at this point, but the secondary
912 * slot's image header must be passed since the image headers in the
913 * boot_data structure have not been updated yet.
914 */
915 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
916 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
917 if (rc != 0) {
918 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
919 return rc;
920 }
921#endif /* MCUBOOT_HW_ROLLBACK_PROT */
922
Fabio Utzig13d9e352017-10-05 20:32:31 -0300923 /*
924 * Erases header and trailer. The trailer is erased because when a new
925 * image is written without a trailer as is the case when using newt, the
926 * trailer that was left might trigger a new upgrade.
927 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700928 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300929 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300930 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
931 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600932 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300933 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700934 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300935 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300936 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
937 last_sector),
938 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
939 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300940 assert(rc == 0);
941
David Vincze2d736ad2019-02-18 11:50:22 +0100942 flash_area_close(fap_primary_slot);
943 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300944
David Vincze2d736ad2019-02-18 11:50:22 +0100945 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600946
947 return 0;
948}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200949#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300950
Christopher Collinsa1c12042019-05-23 14:00:28 -0700951#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300952/**
953 * Swaps the two images in flash. If a prior copy operation was interrupted
954 * by a system reset, this function completes that operation.
955 *
956 * @param bs The current boot status. This function reads
957 * this struct to determine if it is resuming
958 * an interrupted swap operation. This
959 * function writes the updated status to this
960 * function on return.
961 *
962 * @return 0 on success; nonzero on failure.
963 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800964static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300965boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800966{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300967 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -0300968#ifdef MCUBOOT_ENC_IMAGES
969 const struct flash_area *fap;
970 uint8_t slot;
971 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -0300972#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300973 uint32_t size;
974 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300975 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300976 int rc;
977
978 /* FIXME: just do this if asked by user? */
979
980 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300981 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300982
Fabio Utzig12d59162019-11-28 10:01:59 -0300983 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -0300984 /*
985 * No swap ever happened, so need to find the largest image which
986 * will be used to determine the amount of sectors to swap.
987 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300988 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300989 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -0300990 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -0600991 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300992 }
Fabio Utzig2473ac02017-05-02 12:45:02 -0300993
Fabio Utzigba829042018-09-18 08:29:34 -0300994#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200995 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300996 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -0300997 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300998 assert(rc >= 0);
999
1000 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001001 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001002 assert(rc == 0);
1003 } else {
1004 rc = 0;
1005 }
1006 } else {
1007 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1008 }
1009#endif
1010
Fabio Utzig10ee6482019-08-01 12:04:52 -03001011 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001012 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001013 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001014 assert(rc == 0);
1015 }
1016
Fabio Utzigba829042018-09-18 08:29:34 -03001017#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001018 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001019 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001020 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001021 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001022 assert(rc >= 0);
1023
1024 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001025 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001026 assert(rc == 0);
1027 } else {
1028 rc = 0;
1029 }
1030 } else {
1031 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1032 }
1033#endif
1034
Fabio Utzig46490722017-09-04 15:34:32 -03001035 if (size > copy_size) {
1036 copy_size = size;
1037 }
1038
1039 bs->swap_size = copy_size;
1040 } else {
1041 /*
1042 * If a swap was under way, the swap_size should already be present
1043 * in the trailer...
1044 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001045 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001046 assert(rc == 0);
1047
1048 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001049
1050#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001051 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1052 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001053 assert(rc == 0);
1054
1055 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001056 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001057 break;
1058 }
1059 }
1060
1061 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001062 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001063 }
1064 }
1065#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001066 }
1067
Fabio Utzig12d59162019-11-28 10:01:59 -03001068 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001069
David Vincze2d736ad2019-02-18 11:50:22 +01001070#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001071 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001072 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001073 BOOT_LOG_WRN("%d status write fails performing the swap",
1074 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001075 }
1076#endif
1077
Christopher Collins92ea77f2016-12-12 15:59:26 -08001078 return 0;
1079}
David Brown17609d82017-05-05 09:41:34 -06001080#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001081
David Vinczee32483f2019-06-13 10:46:24 +02001082#if (BOOT_IMAGE_NUMBER > 1)
1083/**
1084 * Check the image dependency whether it is satisfied and modify
1085 * the swap type if necessary.
1086 *
1087 * @param dep Image dependency which has to be verified.
1088 *
1089 * @return 0 on success; nonzero on failure.
1090 */
1091static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001092boot_verify_slot_dependency(struct boot_loader_state *state,
1093 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001094{
1095 struct image_version *dep_version;
1096 size_t dep_slot;
1097 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001098 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001099
1100 /* Determine the source of the image which is the subject of
1101 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001102 swap_type = state->swap_type[dep->image_id];
Fabio Utzigb1adb1e2019-09-11 11:42:53 -03001103 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001104 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001105 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001106
1107 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1108 if (rc != 0) {
1109 /* Dependency not satisfied.
1110 * Modify the swap type to decrease the version number of the image
1111 * (which will be located in the primary slot after the boot process),
1112 * consequently the number of unsatisfied dependencies will be
1113 * decreased or remain the same.
1114 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001115 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001116 case BOOT_SWAP_TYPE_TEST:
1117 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001118 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001119 break;
1120 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001121 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001122 break;
1123 default:
1124 break;
1125 }
1126 }
1127
1128 return rc;
1129}
1130
1131/**
1132 * Read all dependency TLVs of an image from the flash and verify
1133 * one after another to see if they are all satisfied.
1134 *
1135 * @param slot Image slot number.
1136 *
1137 * @return 0 on success; nonzero on failure.
1138 */
1139static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001140boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001141{
1142 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001143 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001144 struct image_dependency dep;
1145 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001146 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001147 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001148 int rc;
1149
Fabio Utzig10ee6482019-08-01 12:04:52 -03001150 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001151 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001152 if (rc != 0) {
1153 rc = BOOT_EFLASH;
1154 goto done;
1155 }
1156
Fabio Utzig61fd8882019-09-14 20:00:20 -03001157 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1158 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001159 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001160 goto done;
1161 }
1162
Fabio Utzig61fd8882019-09-14 20:00:20 -03001163 while (true) {
1164 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1165 if (rc < 0) {
1166 return -1;
1167 } else if (rc > 0) {
1168 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001169 break;
1170 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001171
1172 if (len != sizeof(dep)) {
1173 rc = BOOT_EBADIMAGE;
1174 goto done;
1175 }
1176
1177 rc = flash_area_read(fap, off, &dep, len);
1178 if (rc != 0) {
1179 rc = BOOT_EFLASH;
1180 goto done;
1181 }
1182
1183 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1184 rc = BOOT_EBADARGS;
1185 goto done;
1186 }
1187
1188 /* Verify dependency and modify the swap type if not satisfied. */
1189 rc = boot_verify_slot_dependency(state, &dep);
1190 if (rc != 0) {
1191 /* Dependency not satisfied. */
1192 goto done;
1193 }
David Vinczee32483f2019-06-13 10:46:24 +02001194 }
1195
1196done:
1197 flash_area_close(fap);
1198 return rc;
1199}
1200
1201/**
David Vinczee32483f2019-06-13 10:46:24 +02001202 * Iterate over all the images and verify whether the image dependencies in the
1203 * TLV area are all satisfied and update the related swap type if necessary.
1204 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001205static int
1206boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001207{
David Vinczee32483f2019-06-13 10:46:24 +02001208 int rc;
Fabio Utzig298913b2019-08-28 11:22:45 -03001209 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001210
Fabio Utzig10ee6482019-08-01 12:04:52 -03001211 BOOT_CURR_IMG(state) = 0;
1212 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001213 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1214 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1215 slot = BOOT_SECONDARY_SLOT;
1216 } else {
1217 slot = BOOT_PRIMARY_SLOT;
1218 }
1219
1220 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001221 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001222 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001223 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001224 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001225 /* Cannot upgrade due to non-met dependencies, so disable all
1226 * image upgrades.
1227 */
1228 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1229 BOOT_CURR_IMG(state) = idx;
1230 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1231 }
1232 break;
David Vinczee32483f2019-06-13 10:46:24 +02001233 } else {
1234 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03001235 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001236 }
1237 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001238 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001239}
1240#endif /* (BOOT_IMAGE_NUMBER > 1) */
1241
Christopher Collins92ea77f2016-12-12 15:59:26 -08001242/**
David Vinczeba3bd602019-06-17 16:01:43 +02001243 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001244 *
David Vinczeba3bd602019-06-17 16:01:43 +02001245 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001246 *
1247 * @return 0 on success; nonzero on failure.
1248 */
1249static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001250boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001251{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001252 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001253#ifndef MCUBOOT_OVERWRITE_ONLY
1254 uint8_t swap_type;
1255#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001256
David Vinczeba3bd602019-06-17 16:01:43 +02001257 /* At this point there are no aborted swaps. */
1258#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001259 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001260#elif defined(MCUBOOT_BOOTSTRAP)
1261 /* Check if the image update was triggered by a bad image in the
1262 * primary slot (the validity of the image in the secondary slot had
1263 * already been checked).
1264 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001265 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1266 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1267 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001268 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001269 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001270 }
1271#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001272 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001273#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001274 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001275
1276#ifndef MCUBOOT_OVERWRITE_ONLY
1277 /* The following state needs image_ok be explicitly set after the
1278 * swap was finished to avoid a new revert.
1279 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001280 swap_type = BOOT_SWAP_TYPE(state);
1281 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1282 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001283 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001284 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001285 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001286 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001287 }
1288
David Vinczec3084132020-02-18 14:50:47 +01001289#ifdef MCUBOOT_HW_ROLLBACK_PROT
1290 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1291 /* Update the stored security counter with the new image's security
1292 * counter value. The primary slot holds the new image at this point,
1293 * but the secondary slot's image header must be passed since image
1294 * headers in the boot_data structure have not been updated yet.
1295 *
1296 * In case of a permanent image swap mcuboot will never attempt to
1297 * revert the images on the next reboot. Therefore, the security
1298 * counter must be increased right after the image upgrade.
1299 */
1300 rc = boot_update_security_counter(
1301 BOOT_CURR_IMG(state),
1302 BOOT_PRIMARY_SLOT,
1303 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1304 if (rc != 0) {
1305 BOOT_LOG_ERR("Security counter update failed after "
1306 "image upgrade.");
1307 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1308 }
1309 }
1310#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1311
Fabio Utzige575b0b2019-09-11 12:34:23 -03001312 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001313 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001314 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001315 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001316 }
David Vinczeba3bd602019-06-17 16:01:43 +02001317 }
1318#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001319
David Vinczeba3bd602019-06-17 16:01:43 +02001320 return rc;
1321}
1322
1323/**
1324 * Completes a previously aborted image swap.
1325 *
1326 * @param bs The current boot status.
1327 *
1328 * @return 0 on success; nonzero on failure.
1329 */
1330#if !defined(MCUBOOT_OVERWRITE_ONLY)
1331static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001332boot_complete_partial_swap(struct boot_loader_state *state,
1333 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001334{
1335 int rc;
1336
1337 /* Determine the type of swap operation being resumed from the
1338 * `swap-type` trailer field.
1339 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001340 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001341 assert(rc == 0);
1342
Fabio Utzig10ee6482019-08-01 12:04:52 -03001343 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001344
1345 /* The following states need image_ok be explicitly set after the
1346 * swap was finished to avoid a new revert.
1347 */
1348 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1349 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001350 rc = swap_set_image_ok(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;
David Vinczeba3bd602019-06-17 16:01:43 +02001353 }
1354 }
1355
Fabio Utzige575b0b2019-09-11 12:34:23 -03001356 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001357 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001358 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001359 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001360 }
1361 }
1362
Fabio Utzig10ee6482019-08-01 12:04:52 -03001363 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001364 BOOT_LOG_ERR("panic!");
1365 assert(0);
1366
1367 /* Loop forever... */
1368 while (1) {}
1369 }
1370
1371 return rc;
1372}
1373#endif /* !MCUBOOT_OVERWRITE_ONLY */
1374
1375#if (BOOT_IMAGE_NUMBER > 1)
1376/**
1377 * Review the validity of previously determined swap types of other images.
1378 *
1379 * @param aborted_swap The current image upgrade is a
1380 * partial/aborted swap.
1381 */
1382static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001383boot_review_image_swap_types(struct boot_loader_state *state,
1384 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001385{
1386 /* In that case if we rebooted in the middle of an image upgrade process, we
1387 * must review the validity of swap types, that were previously determined
1388 * for other images. The image_ok flag had not been set before the reboot
1389 * for any of the updated images (only the copy_done flag) and thus falsely
1390 * the REVERT swap type has been determined for the previous images that had
1391 * been updated before the reboot.
1392 *
1393 * There are two separate scenarios that we have to deal with:
1394 *
1395 * 1. The reboot has happened during swapping an image:
1396 * The current image upgrade has been determined as a
1397 * partial/aborted swap.
1398 * 2. The reboot has happened between two separate image upgrades:
1399 * In this scenario we must check the swap type of the current image.
1400 * In those cases if it is NONE or REVERT we cannot certainly determine
1401 * the fact of a reboot. In a consistent state images must move in the
1402 * same direction or stay in place, e.g. in practice REVERT and TEST
1403 * swap types cannot be present at the same time. If the swap type of
1404 * the current image is either TEST, PERM or FAIL we must review the
1405 * already determined swap types of other images and set each false
1406 * REVERT swap types to NONE (these images had been successfully
1407 * updated before the system rebooted between two separate image
1408 * upgrades).
1409 */
1410
Fabio Utzig10ee6482019-08-01 12:04:52 -03001411 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001412 /* Nothing to do */
1413 return;
1414 }
1415
1416 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001417 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1418 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001419 /* Nothing to do */
1420 return;
1421 }
1422 }
1423
Fabio Utzig10ee6482019-08-01 12:04:52 -03001424 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1425 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1426 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001427 }
1428 }
1429}
1430#endif
1431
1432/**
1433 * Prepare image to be updated if required.
1434 *
1435 * Prepare image to be updated if required with completing an image swap
1436 * operation if one was aborted and/or determining the type of the
1437 * swap operation. In case of any error set the swap type to NONE.
1438 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001439 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001440 * @param bs Pointer where the read and possibly updated
1441 * boot status can be written to.
1442 */
1443static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001444boot_prepare_image_for_update(struct boot_loader_state *state,
1445 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001446{
1447 int rc;
1448
1449 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001450 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001451 if (rc != 0) {
1452 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1453 " - too small?", BOOT_MAX_IMG_SECTORS);
1454 /* Unable to determine sector layout, continue with next image
1455 * if there is one.
1456 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001457 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001458 return;
1459 }
1460
1461 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001462 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001463 if (rc != 0) {
1464 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001465 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001466 BOOT_CURR_IMG(state));
1467 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001468 return;
1469 }
1470
1471 /* If the current image's slots aren't compatible, no swap is possible.
1472 * Just boot into primary slot.
1473 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001474 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001475 boot_status_reset(bs);
1476
1477#ifndef MCUBOOT_OVERWRITE_ONLY
1478 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001479 if (rc != 0) {
1480 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001481 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001482 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001483 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001484 return;
1485 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001486#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001487
Fabio Utzig74aef312019-11-28 11:05:34 -03001488#ifdef MCUBOOT_SWAP_USING_MOVE
1489 /*
1490 * Must re-read image headers because the boot status might
1491 * have been updated in the previous function call.
1492 */
1493 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1494 if (rc != 0) {
1495 /* Continue with next image if there is one. */
1496 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1497 BOOT_CURR_IMG(state));
1498 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1499 return;
1500 }
1501#endif
1502
David Vinczeba3bd602019-06-17 16:01:43 +02001503 /* Determine if we rebooted in the middle of an image swap
1504 * operation. If a partial swap was detected, complete it.
1505 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001506 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001507
1508#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001509 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001510#endif
1511
1512#ifdef MCUBOOT_OVERWRITE_ONLY
1513 /* Should never arrive here, overwrite-only mode has
1514 * no swap state.
1515 */
1516 assert(0);
1517#else
1518 /* Determine the type of swap operation being resumed from the
1519 * `swap-type` trailer field.
1520 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001521 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001522 assert(rc == 0);
1523#endif
1524 /* Attempt to read an image header from each slot. Ensure that
1525 * image headers in slots are aligned with headers in boot_data.
1526 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001527 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001528 assert(rc == 0);
1529
1530 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001531 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001532 } else {
1533 /* There was no partial swap, determine swap type. */
1534 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001535 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1536 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1537 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001538 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001539 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001540 }
1541
1542#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001543 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001544#endif
1545
1546#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001547 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001548 /* Header checks are done first because they are
1549 * inexpensive. Since overwrite-only copies starting from
1550 * offset 0, if interrupted, it might leave a valid header
1551 * magic, so also run validation on the primary slot to be
1552 * sure it's not OK.
1553 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001554 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1555 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1556 if (boot_img_hdr(state,
1557 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1558 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001559 /* Set swap type to REVERT to overwrite the primary
1560 * slot with the image contained in secondary slot
1561 * and to trigger the explicit setting of the
1562 * image_ok flag.
1563 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001564 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001565 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001566 }
1567 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001568#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001569 }
David Vinczeba3bd602019-06-17 16:01:43 +02001570 } else {
1571 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001572 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001573 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001574}
1575
Christopher Collins92ea77f2016-12-12 15:59:26 -08001576int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001577context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001578{
Marti Bolivar84898652017-06-13 17:20:22 -04001579 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001580 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001581 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001582 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001583 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001584 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001585
1586 /* The array of slot sectors are defined here (as opposed to file scope) so
1587 * that they don't get allocated for non-boot-loader apps. This is
1588 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001589 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001590 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001591 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1592 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001593#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001594 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001595#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001596
Fabio Utzig10ee6482019-08-01 12:04:52 -03001597 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001598 has_upgrade = false;
1599
1600#if (BOOT_IMAGE_NUMBER == 1)
1601 (void)has_upgrade;
1602#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001603
David Vinczeba3bd602019-06-17 16:01:43 +02001604 /* Iterate over all the images. By the end of the loop the swap type has
1605 * to be determined for each image and all aborted swaps have to be
1606 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001607 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001608 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001609
David Vinczeba3bd602019-06-17 16:01:43 +02001610#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1611 /* The keys used for encryption may no longer be valid (could belong to
1612 * another images). Therefore, mark them as invalid to force their reload
1613 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001614 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001615 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001616#endif
1617
Fabio Utzig10ee6482019-08-01 12:04:52 -03001618 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001619
Fabio Utzig10ee6482019-08-01 12:04:52 -03001620 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001621 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001622 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001623 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001624#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001625 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001626#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001627
1628 /* Open primary and secondary image areas for the duration
1629 * of this call.
1630 */
1631 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001632 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001633 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001634 assert(rc == 0);
1635 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001636#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001637 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001638 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001639 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001640#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001641
1642 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001643 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001644
Fabio Utzige575b0b2019-09-11 12:34:23 -03001645 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001646 has_upgrade = true;
1647 }
David Vinczeba3bd602019-06-17 16:01:43 +02001648 }
1649
David Vinczee32483f2019-06-13 10:46:24 +02001650#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001651 if (has_upgrade) {
1652 /* Iterate over all the images and verify whether the image dependencies
1653 * are all satisfied and update swap type if necessary.
1654 */
1655 rc = boot_verify_dependencies(state);
1656 if (rc == BOOT_EBADVERSION) {
1657 /*
1658 * It was impossible to upgrade because the expected dependency version
1659 * was not available. Here we already changed the swap_type so that
1660 * instead of asserting the bootloader, we continue and no upgrade is
1661 * performed.
1662 */
1663 rc = 0;
1664 }
1665 }
David Vinczee32483f2019-06-13 10:46:24 +02001666#endif
1667
David Vinczeba3bd602019-06-17 16:01:43 +02001668 /* Iterate over all the images. At this point there are no aborted swaps
1669 * and the swap types are determined for each image. By the end of the loop
1670 * all required update operations will have been finished.
1671 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001672 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001673
1674#if (BOOT_IMAGE_NUMBER > 1)
1675#ifdef MCUBOOT_ENC_IMAGES
1676 /* The keys used for encryption may no longer be valid (could belong to
1677 * another images). Therefore, mark them as invalid to force their reload
1678 * by boot_enc_load().
1679 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001680 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001681#endif /* MCUBOOT_ENC_IMAGES */
1682
1683 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001684 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001685#endif /* (BOOT_IMAGE_NUMBER > 1) */
1686
1687 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001688 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001689
Fabio Utzig10ee6482019-08-01 12:04:52 -03001690 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001691 case BOOT_SWAP_TYPE_NONE:
1692 break;
1693
1694 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1695 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1696 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001697 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001698 assert(rc == 0);
1699 break;
1700
1701 case BOOT_SWAP_TYPE_FAIL:
1702 /* The image in secondary slot was invalid and is now erased. Ensure
1703 * we don't try to boot into it again on the next reboot. Do this by
1704 * pretending we just reverted back to primary slot.
1705 */
1706#ifndef MCUBOOT_OVERWRITE_ONLY
1707 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001708 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001709 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001710 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001711 }
1712#endif /* !MCUBOOT_OVERWRITE_ONLY */
1713 break;
1714
1715 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001716 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001717 }
1718
Fabio Utzig10ee6482019-08-01 12:04:52 -03001719 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001720 BOOT_LOG_ERR("panic!");
1721 assert(0);
1722
1723 /* Loop forever... */
1724 while (1) {}
1725 }
1726 }
1727
1728 /* Iterate over all the images. At this point all required update operations
1729 * have finished. By the end of the loop each image in the primary slot will
1730 * have been re-validated.
1731 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001732 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1733 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001734 /* Attempt to read an image header from each slot. Ensure that image
1735 * headers in slots are aligned with headers in boot_data.
1736 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001737 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001738 if (rc != 0) {
1739 goto out;
1740 }
1741 /* Since headers were reloaded, it can be assumed we just performed
1742 * a swap or overwrite. Now the header info that should be used to
1743 * provide the data for the bootstrap, which previously was at
1744 * secondary slot, was updated to primary slot.
1745 */
1746 }
1747
1748#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001749 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001750 if (rc != 0) {
1751 rc = BOOT_EBADIMAGE;
1752 goto out;
1753 }
1754#else
1755 /* Even if we're not re-validating the primary slot, we could be booting
1756 * onto an empty flash chip. At least do a basic sanity check that
1757 * the magic number on the image is OK.
1758 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001759 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001760 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001761 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1762 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001763 rc = BOOT_EBADIMAGE;
1764 goto out;
1765 }
David Vinczec3084132020-02-18 14:50:47 +01001766#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1767
1768#ifdef MCUBOOT_HW_ROLLBACK_PROT
1769 /* Update the stored security counter with the active image's security
1770 * counter value. It will only be updated if the new security counter is
1771 * greater than the stored value.
1772 *
1773 * In case of a successful image swapping when the swap type is TEST the
1774 * security counter can be increased only after a reset, when the swap
1775 * type is NONE and the image has marked itself "OK" (the image_ok flag
1776 * has been set). This way a "revert" can be performed when it's
1777 * necessary.
1778 */
1779 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1780 rc = boot_update_security_counter(
1781 BOOT_CURR_IMG(state),
1782 BOOT_PRIMARY_SLOT,
1783 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1784 if (rc != 0) {
1785 BOOT_LOG_ERR("Security counter update failed after image "
1786 "validation.");
1787 goto out;
1788 }
1789 }
1790#endif /* MCUBOOT_HW_ROLLBACK_PROT */
David Vinczeba3bd602019-06-17 16:01:43 +02001791 }
1792
Fabio Utzigb0f04732019-07-31 09:49:19 -03001793#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001794 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001795 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001796#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001797
Fabio Utzigf616c542019-12-19 15:23:32 -03001798 /*
1799 * Since the boot_status struct stores plaintext encryption keys, reset
1800 * them here to avoid the possibility of jumping into an image that could
1801 * easily recover them.
1802 */
1803 memset(&bs, 0, sizeof(struct boot_status));
1804
Fabio Utzig10ee6482019-08-01 12:04:52 -03001805 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1806 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1807 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001808
Fabio Utzig298913b2019-08-28 11:22:45 -03001809out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001810 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001811#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001812 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001813#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001814 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001815 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001816 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001817 }
1818 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001819}
1820
Fabio Utzig10ee6482019-08-01 12:04:52 -03001821/**
1822 * Prepares the booting process. This function moves images around in flash as
1823 * appropriate, and tells you what address to boot from.
1824 *
1825 * @param rsp On success, indicates how booting should occur.
1826 *
1827 * @return 0 on success; nonzero on failure.
1828 */
1829int
1830boot_go(struct boot_rsp *rsp)
1831{
1832 return context_boot_go(&boot_data, rsp);
1833}
1834
Christopher Collins92ea77f2016-12-12 15:59:26 -08001835int
1836split_go(int loader_slot, int split_slot, void **entry)
1837{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001838 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001839 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001840 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001841 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001842 int rc;
1843
Christopher Collins92ea77f2016-12-12 15:59:26 -08001844 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1845 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001846 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001847 }
David Vinczeba3bd602019-06-17 16:01:43 +02001848 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1849 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001850
1851 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1852 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001853 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001854 assert(rc == 0);
1855 split_flash_id = flash_area_id_from_image_slot(split_slot);
1856 rc = flash_area_open(split_flash_id,
1857 &BOOT_IMG_AREA(&boot_data, split_slot));
1858 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001859
1860 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001861 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001862 if (rc != 0) {
1863 rc = SPLIT_GO_ERR;
1864 goto done;
1865 }
1866
Fabio Utzig12d59162019-11-28 10:01:59 -03001867 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001868 if (rc != 0) {
1869 goto done;
1870 }
1871
Christopher Collins92ea77f2016-12-12 15:59:26 -08001872 /* Don't check the bootable image flag because we could really call a
1873 * bootable or non-bootable image. Just validate that the image check
1874 * passes which is distinct from the normal check.
1875 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001876 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001877 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001878 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001879 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001880 if (rc != 0) {
1881 rc = SPLIT_GO_NON_MATCHING;
1882 goto done;
1883 }
1884
Marti Bolivarea088872017-06-12 17:10:49 -04001885 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001886 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001887 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001888 rc = SPLIT_GO_OK;
1889
1890done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001891 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1892 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001893 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001894 return rc;
1895}