blob: 1c1d3a60d94428c4ef6d771b33832b348b5821c5 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
David Brownaac71112020-02-03 16:13:42 -07002 * SPDX-License-Identifier: Apache-2.0
3 *
4 * Copyright (c) 2016-2020 Linaro LTD
5 * Copyright (c) 2016-2019 JUUL Labs
6 * Copyright (c) 2019-2020 Arm Limited
7 *
8 * Original license:
9 *
Christopher Collins92ea77f2016-12-12 15:59:26 -080010 * Licensed to the Apache Software Foundation (ASF) under one
11 * or more contributor license agreements. See the NOTICE file
12 * distributed with this work for additional information
13 * regarding copyright ownership. The ASF licenses this file
14 * to you under the Apache License, Version 2.0 (the
15 * "License"); you may not use this file except in compliance
16 * with the License. You may obtain a copy of the License at
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
20 * Unless required by applicable law or agreed to in writing,
21 * software distributed under the License is distributed on an
22 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
23 * KIND, either express or implied. See the License for the
24 * specific language governing permissions and limitations
25 * under the License.
26 */
27
28/**
29 * This file provides an interface to the boot loader. Functions defined in
30 * this file should only be called while the boot loader is running.
31 */
32
Christopher Collins92ea77f2016-12-12 15:59:26 -080033#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060034#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080038#include <os/os_malloc.h>
39#include "bootutil/bootutil.h"
40#include "bootutil/image.h"
41#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030042#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050043#include "bootutil/bootutil_log.h"
David Vinczec3084132020-02-18 14:50:47 +010044#include "bootutil/security_cnt.h"
David Vincze1cf11b52020-03-24 07:51:09 +010045#include "bootutil/boot_record.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050046
Fabio Utzigba829042018-09-18 08:29:34 -030047#ifdef MCUBOOT_ENC_IMAGES
48#include "bootutil/enc_key.h"
49#endif
50
Fabio Utzigba1fbe62017-07-21 14:01:20 -030051#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030052
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010053MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
54
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040055static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080056
Fabio Utzigabec0732019-07-31 08:40:22 -030057#if (BOOT_IMAGE_NUMBER > 1)
58#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
59#else
60#define IMAGES_ITER(x)
61#endif
62
Fabio Utzig10ee6482019-08-01 12:04:52 -030063/*
64 * This macro allows some control on the allocation of local variables.
65 * When running natively on a target, we don't want to allocated huge
66 * variables on the stack, so make them global instead. For the simulator
67 * we want to run as many threads as there are tests, and it's safer
68 * to just make those variables stack allocated.
69 */
70#if !defined(__BOOTSIM__)
71#define TARGET_STATIC static
72#else
73#define TARGET_STATIC
74#endif
75
David Vinczee574f2d2020-07-10 11:42:03 +020076static int
77boot_read_image_headers(struct boot_loader_state *state, bool require_all,
78 struct boot_status *bs)
79{
80 int rc;
81 int i;
82
83 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
84 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
85 if (rc != 0) {
86 /* If `require_all` is set, fail on any single fail, otherwise
87 * if at least the first slot's header was read successfully,
88 * then the boot loader can attempt a boot.
89 *
90 * Failure to read any headers is a fatal error.
91 */
92 if (i > 0 && !require_all) {
93 return 0;
94 } else {
95 return rc;
96 }
97 }
98 }
99
100 return 0;
101}
102
Tamas Banfe031092020-09-10 17:32:39 +0200103#if !defined(MCUBOOT_DIRECT_XIP)
David Brownf5b33d82017-09-01 10:58:27 -0600104/*
105 * Compute the total size of the given image. Includes the size of
106 * the TLVs.
107 */
Tamas Banfe031092020-09-10 17:32:39 +0200108#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -0600109static int
Fabio Utzigd638b172019-08-09 10:38:05 -0300110boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -0600111{
112 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -0300113 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -0300114 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -0300115 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -0600116 int area_id;
117 int rc;
118
Fabio Utzig10ee6482019-08-01 12:04:52 -0300119#if (BOOT_IMAGE_NUMBER == 1)
120 (void)state;
121#endif
122
123 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -0600124 rc = flash_area_open(area_id, &fap);
125 if (rc != 0) {
126 rc = BOOT_EFLASH;
127 goto done;
128 }
129
Fabio Utzig61fd8882019-09-14 20:00:20 -0300130 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
131
132 if (flash_area_read(fap, off, &info, sizeof(info))) {
133 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600134 goto done;
135 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300136
Fabio Utzige52c08e2019-09-11 19:32:00 -0300137 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
138 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
139 if (protect_tlv_size != info.it_tlv_tot) {
140 rc = BOOT_EBADIMAGE;
141 goto done;
142 }
143
144 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
145 rc = BOOT_EFLASH;
146 goto done;
147 }
148 } else if (protect_tlv_size != 0) {
149 rc = BOOT_EBADIMAGE;
150 goto done;
151 }
152
Fabio Utzig61fd8882019-09-14 20:00:20 -0300153 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
154 rc = BOOT_EBADIMAGE;
155 goto done;
156 }
157
Fabio Utzige52c08e2019-09-11 19:32:00 -0300158 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600159 rc = 0;
160
161done:
162 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300163 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600164}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300165#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600166
Tamas Banfe031092020-09-10 17:32:39 +0200167#if !defined(MCUBOOT_RAM_LOAD)
David Brownab449182019-11-15 09:32:52 -0700168static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300169boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800170{
David Brownab449182019-11-15 09:32:52 -0700171 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300172#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700173 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300174#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800175
176 /* Figure out what size to write update status update as. The size depends
177 * on what the minimum write size is for scratch area, active image slot.
178 * We need to use the bigger of those 2 values.
179 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300180 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300181#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300182 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800183 if (align > elem_sz) {
184 elem_sz = align;
185 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300186#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800187
188 return elem_sz;
189}
190
Fabio Utzig10ee6482019-08-01 12:04:52 -0300191#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
192static int
193boot_initialize_area(struct boot_loader_state *state, int flash_area)
194{
195 int num_sectors = BOOT_MAX_IMG_SECTORS;
196 int rc;
197
198 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
199 rc = flash_area_to_sectors(flash_area, &num_sectors,
200 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
201 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
202
203 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
204 rc = flash_area_to_sectors(flash_area, &num_sectors,
205 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
206 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
207
Fabio Utzig12d59162019-11-28 10:01:59 -0300208#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300209 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
210 rc = flash_area_to_sectors(flash_area, &num_sectors,
211 state->scratch.sectors);
212 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300213#endif
214
Fabio Utzig10ee6482019-08-01 12:04:52 -0300215 } else {
216 return BOOT_EFLASH;
217 }
218
219 return rc;
220}
221#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
222static int
223boot_initialize_area(struct boot_loader_state *state, int flash_area)
224{
225 uint32_t num_sectors;
226 struct flash_sector *out_sectors;
227 size_t *out_num_sectors;
228 int rc;
229
230 num_sectors = BOOT_MAX_IMG_SECTORS;
231
232 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
233 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
234 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
235 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
236 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
237 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300238#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300239 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
240 out_sectors = state->scratch.sectors;
241 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300242#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300243 } else {
244 return BOOT_EFLASH;
245 }
246
247 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
248 if (rc != 0) {
249 return rc;
250 }
251 *out_num_sectors = num_sectors;
252 return 0;
253}
254#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
255
Christopher Collins92ea77f2016-12-12 15:59:26 -0800256/**
257 * Determines the sector layout of both image slots and the scratch area.
258 * This information is necessary for calculating the number of bytes to erase
259 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300260 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800261 */
262static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300263boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300265 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800266 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800267
Fabio Utzig10ee6482019-08-01 12:04:52 -0300268 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300269
270 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800271 if (rc != 0) {
272 return BOOT_EFLASH;
273 }
274
Fabio Utzig10ee6482019-08-01 12:04:52 -0300275 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800276 if (rc != 0) {
277 return BOOT_EFLASH;
278 }
279
Fabio Utzig12d59162019-11-28 10:01:59 -0300280#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300281 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200282 if (rc != 0) {
283 return BOOT_EFLASH;
284 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300285#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200286
Fabio Utzig10ee6482019-08-01 12:04:52 -0300287 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288
289 return 0;
290}
291
Fabio Utzig12d59162019-11-28 10:01:59 -0300292void
293boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294{
Fabio Utzig4741c452019-12-19 15:32:41 -0300295#ifdef MCUBOOT_ENC_IMAGES
296 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
297#if MCUBOOT_SWAP_SAVE_ENCTLV
298 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
299#endif
300#endif /* MCUBOOT_ENC_IMAGES */
301
302 bs->use_scratch = 0;
303 bs->swap_size = 0;
304 bs->source = 0;
305
Fabio Utzig74aef312019-11-28 11:05:34 -0300306 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300307 bs->idx = BOOT_STATUS_IDX_0;
308 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700309 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300310}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311
Fabio Utzig12d59162019-11-28 10:01:59 -0300312bool
313boot_status_is_reset(const struct boot_status *bs)
314{
Fabio Utzig74aef312019-11-28 11:05:34 -0300315 return (bs->op == BOOT_STATUS_OP_MOVE &&
316 bs->idx == BOOT_STATUS_IDX_0 &&
317 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800318}
319
320/**
321 * Writes the supplied boot status to the flash file system. The boot status
322 * contains the current state of an in-progress image copy operation.
323 *
324 * @param bs The boot status to write.
325 *
326 * @return 0 on success; nonzero on failure.
327 */
328int
Fabio Utzig12d59162019-11-28 10:01:59 -0300329boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800330{
331 const struct flash_area *fap;
332 uint32_t off;
333 int area_id;
334 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300335 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700336 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300337 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800338
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300339 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100340 * the trailer. Since in the last step the primary slot is erased, the
341 * first two status writes go to the scratch which will be copied to
342 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300343 */
344
Fabio Utzig12d59162019-11-28 10:01:59 -0300345#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300346 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347 /* Write to scratch. */
348 area_id = FLASH_AREA_IMAGE_SCRATCH;
349 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300350#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100351 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300352 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300353#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300355#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800356
357 rc = flash_area_open(area_id, &fap);
358 if (rc != 0) {
359 rc = BOOT_EFLASH;
360 goto done;
361 }
362
363 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300364 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200365 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300366 erased_val = flash_area_erased_val(fap);
367 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700368 buf[0] = bs->state;
369
370 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800371 if (rc != 0) {
372 rc = BOOT_EFLASH;
373 goto done;
374 }
375
376 rc = 0;
377
378done:
379 flash_area_close(fap);
380 return rc;
381}
Tamas Banfe031092020-09-10 17:32:39 +0200382#endif /* !MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +0200383#endif /* !MCUBOOT_DIRECT_XIP */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800384
385/*
David Vinczec3084132020-02-18 14:50:47 +0100386 * Validate image hash/signature and optionally the security counter in a slot.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800387 */
388static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300389boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
390 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800391{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300392 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300393 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300394 int rc;
395
Fabio Utzig10ee6482019-08-01 12:04:52 -0300396#if (BOOT_IMAGE_NUMBER == 1)
397 (void)state;
398#endif
399
Fabio Utzigba829042018-09-18 08:29:34 -0300400 (void)bs;
401 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300402
403 image_index = BOOT_CURR_IMG(state);
404
405#ifdef MCUBOOT_ENC_IMAGES
406 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300407 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300408 if (rc < 0) {
409 return BOOT_EBADIMAGE;
410 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300411 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300412 return BOOT_EBADIMAGE;
413 }
414 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300415#endif
416
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300417 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300418 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800419 return BOOT_EBADIMAGE;
420 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300421
Christopher Collins92ea77f2016-12-12 15:59:26 -0800422 return 0;
423}
424
Tamas Banfe031092020-09-10 17:32:39 +0200425#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800426static int
427split_image_check(struct image_header *app_hdr,
428 const struct flash_area *app_fap,
429 struct image_header *loader_hdr,
430 const struct flash_area *loader_fap)
431{
432 static void *tmpbuf;
433 uint8_t loader_hash[32];
434
435 if (!tmpbuf) {
436 tmpbuf = malloc(BOOT_TMPBUF_SZ);
437 if (!tmpbuf) {
438 return BOOT_ENOMEM;
439 }
440 }
441
Fabio Utzig10ee6482019-08-01 12:04:52 -0300442 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
443 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800444 return BOOT_EBADIMAGE;
445 }
446
Fabio Utzig10ee6482019-08-01 12:04:52 -0300447 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
448 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800449 return BOOT_EBADIMAGE;
450 }
451
452 return 0;
453}
Tamas Banfe031092020-09-10 17:32:39 +0200454#endif /* !MCUBOOT_DIRECT_XIP && !MCUBOOT_RAM_LOAD */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800455
Fabio Utzig338a19f2018-12-03 08:37:08 -0200456/*
David Brown9bf95af2019-10-10 15:36:36 -0600457 * Check that this is a valid header. Valid means that the magic is
458 * correct, and that the sizes/offsets are "sane". Sane means that
459 * there is no overflow on the arithmetic, and that the result fits
460 * within the flash area we are in.
461 */
462static bool
463boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
464{
465 uint32_t size;
466
467 if (hdr->ih_magic != IMAGE_MAGIC) {
468 return false;
469 }
470
471 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
472 return false;
473 }
474
475 if (size >= fap->fa_size) {
476 return false;
477 }
478
479 return true;
480}
481
482/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200483 * Check that a memory area consists of a given value.
484 */
485static inline bool
486boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300487{
488 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200489 uint8_t *p = (uint8_t *)data;
490 for (i = 0; i < len; i++) {
491 if (val != p[i]) {
492 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300493 }
494 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200495 return true;
496}
497
498static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300499boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200500{
501 const struct flash_area *fap;
502 struct image_header *hdr;
503 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300504 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200505 int rc;
506
Fabio Utzig10ee6482019-08-01 12:04:52 -0300507 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300508 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200509 if (rc != 0) {
510 return -1;
511 }
512
513 erased_val = flash_area_erased_val(fap);
514 flash_area_close(fap);
515
Fabio Utzig10ee6482019-08-01 12:04:52 -0300516 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200517 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
518 return -1;
519 }
520
521 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300522}
523
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000524#if (BOOT_IMAGE_NUMBER > 1) || \
David Vinczee574f2d2020-07-10 11:42:03 +0200525 defined(MCUBOOT_DIRECT_XIP) || \
Tamas Banfe031092020-09-10 17:32:39 +0200526 defined(MCUBOOT_RAM_LOAD) || \
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000527 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
528/**
David Vincze8b0b6372020-05-20 19:54:44 +0200529 * Compare image version numbers not including the build number
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000530 *
David Vincze8b0b6372020-05-20 19:54:44 +0200531 * @param ver1 Pointer to the first image version to compare.
532 * @param ver2 Pointer to the second image version to compare.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000533 *
David Vincze8b0b6372020-05-20 19:54:44 +0200534 * @retval -1 If ver1 is strictly less than ver2.
535 * @retval 0 If the image version numbers are equal,
536 * (not including the build number).
537 * @retval 1 If ver1 is strictly greater than ver2.
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000538 */
539static int
David Vincze8b0b6372020-05-20 19:54:44 +0200540boot_version_cmp(const struct image_version *ver1,
541 const struct image_version *ver2)
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000542{
David Vincze8b0b6372020-05-20 19:54:44 +0200543 if (ver1->iv_major > ver2->iv_major) {
544 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000545 }
David Vincze8b0b6372020-05-20 19:54:44 +0200546 if (ver1->iv_major < ver2->iv_major) {
547 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000548 }
David Vincze8b0b6372020-05-20 19:54:44 +0200549 /* The major version numbers are equal, continue comparison. */
550 if (ver1->iv_minor > ver2->iv_minor) {
551 return 1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000552 }
David Vincze8b0b6372020-05-20 19:54:44 +0200553 if (ver1->iv_minor < ver2->iv_minor) {
554 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000555 }
David Vincze8b0b6372020-05-20 19:54:44 +0200556 /* The minor version numbers are equal, continue comparison. */
557 if (ver1->iv_revision > ver2->iv_revision) {
558 return 1;
559 }
560 if (ver1->iv_revision < ver2->iv_revision) {
561 return -1;
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000562 }
563
564 return 0;
565}
566#endif
567
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300568/*
569 * Check that there is a valid image in a slot
570 *
571 * @returns
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300572 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300573 * 1 if no bootloable image was found
574 * -1 on any errors
575 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800576static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300577boot_validate_slot(struct boot_loader_state *state, int slot,
578 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800579{
580 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400581 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300582 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800583 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300584
Fabio Utzig10ee6482019-08-01 12:04:52 -0300585 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300586 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300588 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800589 }
590
Fabio Utzig10ee6482019-08-01 12:04:52 -0300591 hdr = boot_img_hdr(state, slot);
592 if (boot_check_header_erased(state, slot) == 0 ||
593 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
Fabio Utzig260ec452020-07-09 18:40:07 -0300594
595#if defined(MCUBOOT_SWAP_USING_SCRATCH) || defined(MCUBOOT_SWAP_USING_MOVE)
596 /*
597 * This fixes an issue where an image might be erased, but a trailer
598 * be left behind. It can happen if the image is in the secondary slot
599 * and did not pass validation, in which case the whole slot is erased.
600 * If during the erase operation, a reset occurs, parts of the slot
601 * might have been erased while some did not. The concerning part is
602 * the trailer because it might disable a new image from being loaded
603 * through mcumgr; so we just get rid of the trailer here, if the header
604 * is erased.
605 */
606 if (slot != BOOT_PRIMARY_SLOT) {
607 swap_erase_trailer_sectors(state, fap);
608 }
609#endif
610
David Vincze2d736ad2019-02-18 11:50:22 +0100611 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300612 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200613 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300614 }
615
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000616#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
617 if (slot != BOOT_PRIMARY_SLOT) {
618 /* Check if version of secondary slot is sufficient */
David Vincze8b0b6372020-05-20 19:54:44 +0200619 rc = boot_version_cmp(
620 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver,
621 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver);
622 if (rc < 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000623 BOOT_LOG_ERR("insufficient version in secondary slot");
624 flash_area_erase(fap, 0, fap->fa_size);
625 /* Image in the secondary slot does not satisfy version requirement.
626 * Erase the image and continue booting from the primary slot.
627 */
628 rc = 1;
629 goto out;
630 }
631 }
632#endif
633
David Brown9bf95af2019-10-10 15:36:36 -0600634 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
Tamas Banfe031092020-09-10 17:32:39 +0200635 if ((slot != BOOT_PRIMARY_SLOT) || ARE_SLOTS_EQUIVALENT()) {
David Brownb38e0442017-02-24 13:57:12 -0700636 flash_area_erase(fap, 0, fap->fa_size);
David Vinczee574f2d2020-07-10 11:42:03 +0200637 /* Image is invalid, erase it to prevent further unnecessary
638 * attempts to validate and boot it.
David Brownb38e0442017-02-24 13:57:12 -0700639 */
640 }
David Brown098de832019-12-10 11:58:01 -0700641#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100642 BOOT_LOG_ERR("Image in the %s slot is not valid!",
643 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700644#endif
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000645 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200646 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800647 }
648
David Vincze2d736ad2019-02-18 11:50:22 +0100649 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200650 rc = 0;
651
652out:
653 flash_area_close(fap);
654 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800655}
656
David Vinczec3084132020-02-18 14:50:47 +0100657#ifdef MCUBOOT_HW_ROLLBACK_PROT
658/**
659 * Updates the stored security counter value with the image's security counter
660 * value which resides in the given slot, only if it's greater than the stored
661 * value.
662 *
663 * @param image_index Index of the image to determine which security
664 * counter to update.
665 * @param slot Slot number of the image.
666 * @param hdr Pointer to the image header structure of the image
667 * that is currently stored in the given slot.
668 *
669 * @return 0 on success; nonzero on failure.
670 */
671static int
672boot_update_security_counter(uint8_t image_index, int slot,
673 struct image_header *hdr)
674{
675 const struct flash_area *fap = NULL;
676 uint32_t img_security_cnt;
677 int rc;
678
679 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
680 &fap);
681 if (rc != 0) {
682 rc = BOOT_EFLASH;
683 goto done;
684 }
685
686 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
687 if (rc != 0) {
688 goto done;
689 }
690
691 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
692 if (rc != 0) {
693 goto done;
694 }
695
696done:
697 flash_area_close(fap);
698 return rc;
699}
700#endif /* MCUBOOT_HW_ROLLBACK_PROT */
701
Tamas Banfe031092020-09-10 17:32:39 +0200702#if !defined(MCUBOOT_DIRECT_XIP) && !defined(MCUBOOT_RAM_LOAD)
David Vinczee574f2d2020-07-10 11:42:03 +0200703/**
704 * Determines which swap operation to perform, if any. If it is determined
705 * that a swap operation is required, the image in the secondary slot is checked
706 * for validity. If the image in the secondary slot is invalid, it is erased,
707 * and a swap type of "none" is indicated.
708 *
709 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
710 */
711static int
712boot_validated_swap_type(struct boot_loader_state *state,
713 struct boot_status *bs)
714{
715 int swap_type;
716 int rc;
717
718 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
719 if (BOOT_IS_UPGRADE(swap_type)) {
720 /* Boot loader wants to switch to the secondary slot.
721 * Ensure image is valid.
722 */
723 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
724 if (rc == 1) {
725 swap_type = BOOT_SWAP_TYPE_NONE;
726 } else if (rc != 0) {
727 swap_type = BOOT_SWAP_TYPE_FAIL;
728 }
729 }
730
731 return swap_type;
732}
733
Christopher Collins92ea77f2016-12-12 15:59:26 -0800734/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800735 * Erases a region of flash.
736 *
Fabio Utzigba829042018-09-18 08:29:34 -0300737 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800738 * @param off The offset within the flash area to start the
739 * erase.
740 * @param sz The number of bytes to erase.
741 *
742 * @return 0 on success; nonzero on failure.
743 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300744int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300745boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800746{
Fabio Utzigba829042018-09-18 08:29:34 -0300747 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800748}
749
750/**
751 * Copies the contents of one flash region to another. You must erase the
752 * destination region prior to calling this function.
753 *
754 * @param flash_area_id_src The ID of the source flash area.
755 * @param flash_area_id_dst The ID of the destination flash area.
756 * @param off_src The offset within the source flash area to
757 * copy from.
758 * @param off_dst The offset within the destination flash area to
759 * copy to.
760 * @param sz The number of bytes to copy.
761 *
762 * @return 0 on success; nonzero on failure.
763 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300764int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300765boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300766 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300767 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800768 uint32_t off_src, uint32_t off_dst, uint32_t sz)
769{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800770 uint32_t bytes_copied;
771 int chunk_sz;
772 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300773#ifdef MCUBOOT_ENC_IMAGES
774 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300775 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300776 size_t blk_off;
777 struct image_header *hdr;
778 uint16_t idx;
779 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300780 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300781#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800782
Fabio Utzig10ee6482019-08-01 12:04:52 -0300783 TARGET_STATIC uint8_t buf[1024];
784
785#if !defined(MCUBOOT_ENC_IMAGES)
786 (void)state;
787#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800788
Christopher Collins92ea77f2016-12-12 15:59:26 -0800789 bytes_copied = 0;
790 while (bytes_copied < sz) {
791 if (sz - bytes_copied > sizeof buf) {
792 chunk_sz = sizeof buf;
793 } else {
794 chunk_sz = sz - bytes_copied;
795 }
796
797 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
798 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300799 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800800 }
801
Fabio Utzigba829042018-09-18 08:29:34 -0300802#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300803 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300804 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
805 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
806 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
807 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100808 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300809 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300810#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300811 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300812 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100813 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300814 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300815 off = off_dst;
816 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300817#else
818 off = off_dst;
819 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
820 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
821 }
822#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200823 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300824 blk_sz = chunk_sz;
825 idx = 0;
826 if (off + bytes_copied < hdr->ih_hdr_size) {
827 /* do not decrypt header */
828 blk_off = 0;
829 blk_sz = chunk_sz - hdr->ih_hdr_size;
830 idx = hdr->ih_hdr_size;
831 } else {
832 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
833 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300834 tlv_off = BOOT_TLV_OFF(hdr);
835 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300836 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300837 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300838 blk_sz = 0;
839 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300840 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300841 }
842 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300843 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300844 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
845 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300846 }
847 }
848#endif
849
Christopher Collins92ea77f2016-12-12 15:59:26 -0800850 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
851 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300852 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800853 }
854
855 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300856
857 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800858 }
859
Fabio Utzigba829042018-09-18 08:29:34 -0300860 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800861}
862
Christopher Collins92ea77f2016-12-12 15:59:26 -0800863/**
David Vincze2d736ad2019-02-18 11:50:22 +0100864 * Overwrite primary slot with the image contained in the secondary slot.
865 * If a prior copy operation was interrupted by a system reset, this function
866 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800867 *
868 * @param bs The current boot status. This function reads
869 * this struct to determine if it is resuming
870 * an interrupted swap operation. This
871 * function writes the updated status to this
872 * function on return.
873 *
874 * @return 0 on success; nonzero on failure.
875 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200876#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600877static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300878boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600879{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400880 size_t sect_count;
881 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600882 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300883 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400884 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300885 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100886 const struct flash_area *fap_primary_slot;
887 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300888 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100889
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200890 (void)bs;
891
Fabio Utzig13d9e352017-10-05 20:32:31 -0300892#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
893 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300894 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300895 assert(rc == 0);
896#endif
David Brown17609d82017-05-05 09:41:34 -0600897
David Vincze2d736ad2019-02-18 11:50:22 +0100898 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
899 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600900
Fabio Utzigb0f04732019-07-31 09:49:19 -0300901 image_index = BOOT_CURR_IMG(state);
902
903 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
904 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300905 assert (rc == 0);
906
Fabio Utzigb0f04732019-07-31 09:49:19 -0300907 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
908 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300909 assert (rc == 0);
910
Fabio Utzig10ee6482019-08-01 12:04:52 -0300911 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300912 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300913 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300914 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600915 assert(rc == 0);
916
917 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300918
919#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
920 if (size >= src_size) {
921 break;
922 }
923#endif
David Brown17609d82017-05-05 09:41:34 -0600924 }
925
Fabio Utzigba829042018-09-18 08:29:34 -0300926#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300927 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300928 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300929 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300930 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100931
Fabio Utzigba829042018-09-18 08:29:34 -0300932 if (rc < 0) {
933 return BOOT_EBADIMAGE;
934 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300935 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300936 return BOOT_EBADIMAGE;
937 }
938 }
939#endif
940
David Vincze2d736ad2019-02-18 11:50:22 +0100941 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
942 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300943 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600944
David Vinczec3084132020-02-18 14:50:47 +0100945#ifdef MCUBOOT_HW_ROLLBACK_PROT
946 /* Update the stored security counter with the new image's security counter
947 * value. Both slots hold the new image at this point, but the secondary
948 * slot's image header must be passed since the image headers in the
949 * boot_data structure have not been updated yet.
950 */
951 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
952 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
953 if (rc != 0) {
954 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
955 return rc;
956 }
957#endif /* MCUBOOT_HW_ROLLBACK_PROT */
958
Fabio Utzig13d9e352017-10-05 20:32:31 -0300959 /*
960 * Erases header and trailer. The trailer is erased because when a new
961 * image is written without a trailer as is the case when using newt, the
962 * trailer that was left might trigger a new upgrade.
963 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700964 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300965 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300966 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
967 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600968 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300969 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700970 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300971 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300972 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
973 last_sector),
974 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
975 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300976 assert(rc == 0);
977
David Vincze2d736ad2019-02-18 11:50:22 +0100978 flash_area_close(fap_primary_slot);
979 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300980
David Vincze2d736ad2019-02-18 11:50:22 +0100981 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600982
983 return 0;
984}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200985#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300986
Christopher Collinsa1c12042019-05-23 14:00:28 -0700987#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300988/**
989 * Swaps the two images in flash. If a prior copy operation was interrupted
990 * by a system reset, this function completes that operation.
991 *
992 * @param bs The current boot status. This function reads
993 * this struct to determine if it is resuming
994 * an interrupted swap operation. This
995 * function writes the updated status to this
996 * function on return.
997 *
998 * @return 0 on success; nonzero on failure.
999 */
Christopher Collins92ea77f2016-12-12 15:59:26 -08001000static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001001boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001002{
Fabio Utzig2473ac02017-05-02 12:45:02 -03001003 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -03001004#ifdef MCUBOOT_ENC_IMAGES
1005 const struct flash_area *fap;
1006 uint8_t slot;
1007 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -03001008#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001009 uint32_t size;
1010 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001011 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -03001012 int rc;
1013
1014 /* FIXME: just do this if asked by user? */
1015
1016 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001017 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001018
Fabio Utzig12d59162019-11-28 10:01:59 -03001019 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -03001020 /*
1021 * No swap ever happened, so need to find the largest image which
1022 * will be used to determine the amount of sectors to swap.
1023 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001024 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001025 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001026 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -06001027 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -03001028 }
Fabio Utzig2473ac02017-05-02 12:45:02 -03001029
Fabio Utzigba829042018-09-18 08:29:34 -03001030#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001031 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001032 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001033 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001034 assert(rc >= 0);
1035
1036 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001037 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001038 assert(rc == 0);
1039 } else {
1040 rc = 0;
1041 }
1042 } else {
1043 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
1044 }
1045#endif
1046
Fabio Utzig10ee6482019-08-01 12:04:52 -03001047 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -03001048 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -03001049 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -03001050 assert(rc == 0);
1051 }
1052
Fabio Utzigba829042018-09-18 08:29:34 -03001053#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -03001054 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -02001055 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001056 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -03001057 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001058 assert(rc >= 0);
1059
1060 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001061 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001062 assert(rc == 0);
1063 } else {
1064 rc = 0;
1065 }
1066 } else {
1067 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
1068 }
1069#endif
1070
Fabio Utzig46490722017-09-04 15:34:32 -03001071 if (size > copy_size) {
1072 copy_size = size;
1073 }
1074
1075 bs->swap_size = copy_size;
1076 } else {
1077 /*
1078 * If a swap was under way, the swap_size should already be present
1079 * in the trailer...
1080 */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001081 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -03001082 assert(rc == 0);
1083
1084 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -03001085
1086#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -03001087 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1088 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001089 assert(rc == 0);
1090
1091 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -02001092 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -03001093 break;
1094 }
1095 }
1096
1097 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001098 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001099 }
1100 }
1101#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001102 }
1103
Fabio Utzig12d59162019-11-28 10:01:59 -03001104 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001105
David Vincze2d736ad2019-02-18 11:50:22 +01001106#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001107 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001108 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001109 BOOT_LOG_WRN("%d status write fails performing the swap",
1110 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001111 }
1112#endif
1113
Christopher Collins92ea77f2016-12-12 15:59:26 -08001114 return 0;
1115}
David Brown17609d82017-05-05 09:41:34 -06001116#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001117
David Vinczee32483f2019-06-13 10:46:24 +02001118#if (BOOT_IMAGE_NUMBER > 1)
1119/**
1120 * Check the image dependency whether it is satisfied and modify
1121 * the swap type if necessary.
1122 *
1123 * @param dep Image dependency which has to be verified.
1124 *
1125 * @return 0 on success; nonzero on failure.
1126 */
1127static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001128boot_verify_slot_dependency(struct boot_loader_state *state,
1129 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001130{
1131 struct image_version *dep_version;
1132 size_t dep_slot;
1133 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001134 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001135
1136 /* Determine the source of the image which is the subject of
1137 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001138 swap_type = state->swap_type[dep->image_id];
Barry Solomon04075532020-03-18 09:33:32 -04001139 dep_slot = BOOT_IS_UPGRADE(swap_type) ? BOOT_SECONDARY_SLOT
1140 : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001141 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001142
David Vincze8b0b6372020-05-20 19:54:44 +02001143 rc = boot_version_cmp(dep_version, &dep->image_min_version);
1144 if (rc < 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001145 /* Dependency not satisfied.
1146 * Modify the swap type to decrease the version number of the image
1147 * (which will be located in the primary slot after the boot process),
1148 * consequently the number of unsatisfied dependencies will be
1149 * decreased or remain the same.
1150 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001151 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001152 case BOOT_SWAP_TYPE_TEST:
1153 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001154 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001155 break;
1156 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001157 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001158 break;
1159 default:
1160 break;
1161 }
David Vincze8b0b6372020-05-20 19:54:44 +02001162 } else {
1163 /* Dependency satisfied. */
1164 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001165 }
1166
1167 return rc;
1168}
1169
1170/**
1171 * Read all dependency TLVs of an image from the flash and verify
1172 * one after another to see if they are all satisfied.
1173 *
1174 * @param slot Image slot number.
1175 *
1176 * @return 0 on success; nonzero on failure.
1177 */
1178static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001179boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001180{
1181 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001182 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001183 struct image_dependency dep;
1184 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001185 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001186 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001187 int rc;
1188
Fabio Utzig10ee6482019-08-01 12:04:52 -03001189 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001190 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001191 if (rc != 0) {
1192 rc = BOOT_EFLASH;
1193 goto done;
1194 }
1195
Fabio Utzig61fd8882019-09-14 20:00:20 -03001196 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1197 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001198 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001199 goto done;
1200 }
1201
Fabio Utzig61fd8882019-09-14 20:00:20 -03001202 while (true) {
1203 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1204 if (rc < 0) {
1205 return -1;
1206 } else if (rc > 0) {
1207 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001208 break;
1209 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001210
1211 if (len != sizeof(dep)) {
1212 rc = BOOT_EBADIMAGE;
1213 goto done;
1214 }
1215
1216 rc = flash_area_read(fap, off, &dep, len);
1217 if (rc != 0) {
1218 rc = BOOT_EFLASH;
1219 goto done;
1220 }
1221
1222 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1223 rc = BOOT_EBADARGS;
1224 goto done;
1225 }
1226
1227 /* Verify dependency and modify the swap type if not satisfied. */
1228 rc = boot_verify_slot_dependency(state, &dep);
1229 if (rc != 0) {
1230 /* Dependency not satisfied. */
1231 goto done;
1232 }
David Vinczee32483f2019-06-13 10:46:24 +02001233 }
1234
1235done:
1236 flash_area_close(fap);
1237 return rc;
1238}
1239
1240/**
David Vinczee32483f2019-06-13 10:46:24 +02001241 * Iterate over all the images and verify whether the image dependencies in the
1242 * TLV area are all satisfied and update the related swap type if necessary.
1243 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001244static int
1245boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001246{
Erik Johnson49063752020-02-06 09:59:31 -06001247 int rc = -1;
Fabio Utzig298913b2019-08-28 11:22:45 -03001248 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001249
Fabio Utzig10ee6482019-08-01 12:04:52 -03001250 BOOT_CURR_IMG(state) = 0;
1251 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001252 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1253 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1254 slot = BOOT_SECONDARY_SLOT;
1255 } else {
1256 slot = BOOT_PRIMARY_SLOT;
1257 }
1258
1259 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001260 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001261 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001262 BOOT_CURR_IMG(state)++;
David Vincze8b0b6372020-05-20 19:54:44 +02001263 } else {
Fabio Utzig298913b2019-08-28 11:22:45 -03001264 /* Cannot upgrade due to non-met dependencies, so disable all
1265 * image upgrades.
1266 */
1267 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1268 BOOT_CURR_IMG(state) = idx;
1269 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1270 }
1271 break;
David Vinczee32483f2019-06-13 10:46:24 +02001272 }
1273 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001274 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001275}
1276#endif /* (BOOT_IMAGE_NUMBER > 1) */
1277
Christopher Collins92ea77f2016-12-12 15:59:26 -08001278/**
David Vinczeba3bd602019-06-17 16:01:43 +02001279 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001280 *
David Vinczeba3bd602019-06-17 16:01:43 +02001281 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001282 *
1283 * @return 0 on success; nonzero on failure.
1284 */
1285static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001286boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001287{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001288 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001289#ifndef MCUBOOT_OVERWRITE_ONLY
1290 uint8_t swap_type;
1291#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001292
David Vinczeba3bd602019-06-17 16:01:43 +02001293 /* At this point there are no aborted swaps. */
1294#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001295 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001296#elif defined(MCUBOOT_BOOTSTRAP)
1297 /* Check if the image update was triggered by a bad image in the
1298 * primary slot (the validity of the image in the secondary slot had
1299 * already been checked).
1300 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001301 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1302 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1303 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001304 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001305 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001306 }
1307#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001308 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001309#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001310 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001311
1312#ifndef MCUBOOT_OVERWRITE_ONLY
1313 /* The following state needs image_ok be explicitly set after the
1314 * swap was finished to avoid a new revert.
1315 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001316 swap_type = BOOT_SWAP_TYPE(state);
1317 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1318 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001319 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001320 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001321 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001322 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001323 }
1324
David Vinczec3084132020-02-18 14:50:47 +01001325#ifdef MCUBOOT_HW_ROLLBACK_PROT
1326 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1327 /* Update the stored security counter with the new image's security
1328 * counter value. The primary slot holds the new image at this point,
1329 * but the secondary slot's image header must be passed since image
1330 * headers in the boot_data structure have not been updated yet.
1331 *
1332 * In case of a permanent image swap mcuboot will never attempt to
1333 * revert the images on the next reboot. Therefore, the security
1334 * counter must be increased right after the image upgrade.
1335 */
1336 rc = boot_update_security_counter(
1337 BOOT_CURR_IMG(state),
1338 BOOT_PRIMARY_SLOT,
1339 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
1340 if (rc != 0) {
1341 BOOT_LOG_ERR("Security counter update failed after "
1342 "image upgrade.");
1343 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
1344 }
1345 }
1346#endif /* MCUBOOT_HW_ROLLBACK_PROT */
1347
Fabio Utzige575b0b2019-09-11 12:34:23 -03001348 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001349 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001350 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001351 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001352 }
David Vinczeba3bd602019-06-17 16:01:43 +02001353 }
1354#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001355
David Vinczeba3bd602019-06-17 16:01:43 +02001356 return rc;
1357}
1358
1359/**
1360 * Completes a previously aborted image swap.
1361 *
1362 * @param bs The current boot status.
1363 *
1364 * @return 0 on success; nonzero on failure.
1365 */
1366#if !defined(MCUBOOT_OVERWRITE_ONLY)
1367static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001368boot_complete_partial_swap(struct boot_loader_state *state,
1369 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001370{
1371 int rc;
1372
1373 /* Determine the type of swap operation being resumed from the
1374 * `swap-type` trailer field.
1375 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001376 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001377 assert(rc == 0);
1378
Fabio Utzig10ee6482019-08-01 12:04:52 -03001379 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001380
1381 /* The following states need image_ok be explicitly set after the
1382 * swap was finished to avoid a new revert.
1383 */
1384 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1385 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001386 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001387 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001388 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001389 }
1390 }
1391
Fabio Utzige575b0b2019-09-11 12:34:23 -03001392 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001393 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001394 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001395 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001396 }
1397 }
1398
Fabio Utzig10ee6482019-08-01 12:04:52 -03001399 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001400 BOOT_LOG_ERR("panic!");
1401 assert(0);
1402
1403 /* Loop forever... */
1404 while (1) {}
1405 }
1406
1407 return rc;
1408}
1409#endif /* !MCUBOOT_OVERWRITE_ONLY */
1410
1411#if (BOOT_IMAGE_NUMBER > 1)
1412/**
1413 * Review the validity of previously determined swap types of other images.
1414 *
1415 * @param aborted_swap The current image upgrade is a
1416 * partial/aborted swap.
1417 */
1418static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001419boot_review_image_swap_types(struct boot_loader_state *state,
1420 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001421{
1422 /* In that case if we rebooted in the middle of an image upgrade process, we
1423 * must review the validity of swap types, that were previously determined
1424 * for other images. The image_ok flag had not been set before the reboot
1425 * for any of the updated images (only the copy_done flag) and thus falsely
1426 * the REVERT swap type has been determined for the previous images that had
1427 * been updated before the reboot.
1428 *
1429 * There are two separate scenarios that we have to deal with:
1430 *
1431 * 1. The reboot has happened during swapping an image:
1432 * The current image upgrade has been determined as a
1433 * partial/aborted swap.
1434 * 2. The reboot has happened between two separate image upgrades:
1435 * In this scenario we must check the swap type of the current image.
1436 * In those cases if it is NONE or REVERT we cannot certainly determine
1437 * the fact of a reboot. In a consistent state images must move in the
1438 * same direction or stay in place, e.g. in practice REVERT and TEST
1439 * swap types cannot be present at the same time. If the swap type of
1440 * the current image is either TEST, PERM or FAIL we must review the
1441 * already determined swap types of other images and set each false
1442 * REVERT swap types to NONE (these images had been successfully
1443 * updated before the system rebooted between two separate image
1444 * upgrades).
1445 */
1446
Fabio Utzig10ee6482019-08-01 12:04:52 -03001447 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001448 /* Nothing to do */
1449 return;
1450 }
1451
1452 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001453 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1454 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001455 /* Nothing to do */
1456 return;
1457 }
1458 }
1459
Fabio Utzig10ee6482019-08-01 12:04:52 -03001460 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1461 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1462 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001463 }
1464 }
1465}
1466#endif
1467
1468/**
1469 * Prepare image to be updated if required.
1470 *
1471 * Prepare image to be updated if required with completing an image swap
1472 * operation if one was aborted and/or determining the type of the
1473 * swap operation. In case of any error set the swap type to NONE.
1474 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001475 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001476 * @param bs Pointer where the read and possibly updated
1477 * boot status can be written to.
1478 */
1479static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001480boot_prepare_image_for_update(struct boot_loader_state *state,
1481 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001482{
1483 int rc;
1484
1485 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001486 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001487 if (rc != 0) {
1488 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1489 " - too small?", BOOT_MAX_IMG_SECTORS);
1490 /* Unable to determine sector layout, continue with next image
1491 * if there is one.
1492 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001493 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001494 return;
1495 }
1496
1497 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001498 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001499 if (rc != 0) {
1500 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001501 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001502 BOOT_CURR_IMG(state));
1503 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001504 return;
1505 }
1506
1507 /* If the current image's slots aren't compatible, no swap is possible.
1508 * Just boot into primary slot.
1509 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001510 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001511 boot_status_reset(bs);
1512
1513#ifndef MCUBOOT_OVERWRITE_ONLY
1514 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001515 if (rc != 0) {
1516 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001517 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001518 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001519 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001520 return;
1521 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001522#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001523
Fabio Utzig74aef312019-11-28 11:05:34 -03001524#ifdef MCUBOOT_SWAP_USING_MOVE
1525 /*
1526 * Must re-read image headers because the boot status might
1527 * have been updated in the previous function call.
1528 */
1529 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1530 if (rc != 0) {
1531 /* Continue with next image if there is one. */
1532 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1533 BOOT_CURR_IMG(state));
1534 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1535 return;
1536 }
1537#endif
1538
David Vinczeba3bd602019-06-17 16:01:43 +02001539 /* Determine if we rebooted in the middle of an image swap
1540 * operation. If a partial swap was detected, complete it.
1541 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001542 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001543
1544#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001545 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001546#endif
1547
1548#ifdef MCUBOOT_OVERWRITE_ONLY
1549 /* Should never arrive here, overwrite-only mode has
1550 * no swap state.
1551 */
1552 assert(0);
1553#else
1554 /* Determine the type of swap operation being resumed from the
1555 * `swap-type` trailer field.
1556 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001557 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001558 assert(rc == 0);
1559#endif
1560 /* Attempt to read an image header from each slot. Ensure that
1561 * image headers in slots are aligned with headers in boot_data.
1562 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001563 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001564 assert(rc == 0);
1565
1566 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001567 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001568 } else {
1569 /* There was no partial swap, determine swap type. */
1570 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001571 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1572 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1573 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001574 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001575 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001576 }
1577
1578#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001579 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001580#endif
1581
1582#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001583 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001584 /* Header checks are done first because they are
1585 * inexpensive. Since overwrite-only copies starting from
1586 * offset 0, if interrupted, it might leave a valid header
1587 * magic, so also run validation on the primary slot to be
1588 * sure it's not OK.
1589 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001590 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1591 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1592 if (boot_img_hdr(state,
1593 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1594 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001595 /* Set swap type to REVERT to overwrite the primary
1596 * slot with the image contained in secondary slot
1597 * and to trigger the explicit setting of the
1598 * image_ok flag.
1599 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001600 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001601 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001602 }
1603 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001604#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001605 }
David Vinczeba3bd602019-06-17 16:01:43 +02001606 } else {
1607 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001608 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001609 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001610}
1611
Christopher Collins92ea77f2016-12-12 15:59:26 -08001612int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001613context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001614{
Marti Bolivar84898652017-06-13 17:20:22 -04001615 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001616 struct boot_status bs;
David Vincze9015a5d2020-05-18 14:43:11 +02001617 int rc = -1;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001618 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001619 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001620 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001621
1622 /* The array of slot sectors are defined here (as opposed to file scope) so
1623 * that they don't get allocated for non-boot-loader apps. This is
1624 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001625 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001626 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001627 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1628 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001629#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001630 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001631#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001632
Fabio Utzig10ee6482019-08-01 12:04:52 -03001633 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001634 has_upgrade = false;
1635
1636#if (BOOT_IMAGE_NUMBER == 1)
1637 (void)has_upgrade;
1638#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001639
David Vinczeba3bd602019-06-17 16:01:43 +02001640 /* Iterate over all the images. By the end of the loop the swap type has
1641 * to be determined for each image and all aborted swaps have to be
1642 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001643 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001644 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001645
David Vinczeba3bd602019-06-17 16:01:43 +02001646#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1647 /* The keys used for encryption may no longer be valid (could belong to
1648 * another images). Therefore, mark them as invalid to force their reload
1649 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001650 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001651 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001652#endif
1653
Fabio Utzig10ee6482019-08-01 12:04:52 -03001654 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001655
Fabio Utzig10ee6482019-08-01 12:04:52 -03001656 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001657 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001658 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001659 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001660#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001661 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001662#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001663
1664 /* Open primary and secondary image areas for the duration
1665 * of this call.
1666 */
1667 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001668 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001669 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001670 assert(rc == 0);
1671 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001672#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001673 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001674 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001675 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001676#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001677
1678 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001679 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001680
Fabio Utzige575b0b2019-09-11 12:34:23 -03001681 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001682 has_upgrade = true;
1683 }
David Vinczeba3bd602019-06-17 16:01:43 +02001684 }
1685
David Vinczee32483f2019-06-13 10:46:24 +02001686#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001687 if (has_upgrade) {
1688 /* Iterate over all the images and verify whether the image dependencies
1689 * are all satisfied and update swap type if necessary.
1690 */
1691 rc = boot_verify_dependencies(state);
David Vincze8b0b6372020-05-20 19:54:44 +02001692 if (rc != 0) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001693 /*
1694 * It was impossible to upgrade because the expected dependency version
1695 * was not available. Here we already changed the swap_type so that
1696 * instead of asserting the bootloader, we continue and no upgrade is
1697 * performed.
1698 */
1699 rc = 0;
1700 }
1701 }
David Vinczee32483f2019-06-13 10:46:24 +02001702#endif
1703
David Vinczeba3bd602019-06-17 16:01:43 +02001704 /* Iterate over all the images. At this point there are no aborted swaps
1705 * and the swap types are determined for each image. By the end of the loop
1706 * all required update operations will have been finished.
1707 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001708 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001709
1710#if (BOOT_IMAGE_NUMBER > 1)
1711#ifdef MCUBOOT_ENC_IMAGES
1712 /* The keys used for encryption may no longer be valid (could belong to
1713 * another images). Therefore, mark them as invalid to force their reload
1714 * by boot_enc_load().
1715 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001716 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001717#endif /* MCUBOOT_ENC_IMAGES */
1718
1719 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001720 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001721#endif /* (BOOT_IMAGE_NUMBER > 1) */
1722
1723 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001724 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001725
Fabio Utzig10ee6482019-08-01 12:04:52 -03001726 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001727 case BOOT_SWAP_TYPE_NONE:
1728 break;
1729
1730 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1731 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1732 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001733 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001734 assert(rc == 0);
1735 break;
1736
1737 case BOOT_SWAP_TYPE_FAIL:
1738 /* The image in secondary slot was invalid and is now erased. Ensure
1739 * we don't try to boot into it again on the next reboot. Do this by
1740 * pretending we just reverted back to primary slot.
1741 */
1742#ifndef MCUBOOT_OVERWRITE_ONLY
1743 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001744 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001745 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001746 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001747 }
1748#endif /* !MCUBOOT_OVERWRITE_ONLY */
1749 break;
1750
1751 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001752 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001753 }
1754
Fabio Utzig10ee6482019-08-01 12:04:52 -03001755 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001756 BOOT_LOG_ERR("panic!");
1757 assert(0);
1758
1759 /* Loop forever... */
1760 while (1) {}
1761 }
1762 }
1763
1764 /* Iterate over all the images. At this point all required update operations
1765 * have finished. By the end of the loop each image in the primary slot will
1766 * have been re-validated.
1767 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001768 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1769 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001770 /* Attempt to read an image header from each slot. Ensure that image
1771 * headers in slots are aligned with headers in boot_data.
1772 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001773 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001774 if (rc != 0) {
1775 goto out;
1776 }
1777 /* Since headers were reloaded, it can be assumed we just performed
1778 * a swap or overwrite. Now the header info that should be used to
1779 * provide the data for the bootstrap, which previously was at
1780 * secondary slot, was updated to primary slot.
1781 */
1782 }
1783
1784#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001785 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001786 if (rc != 0) {
1787 rc = BOOT_EBADIMAGE;
1788 goto out;
1789 }
1790#else
1791 /* Even if we're not re-validating the primary slot, we could be booting
1792 * onto an empty flash chip. At least do a basic sanity check that
1793 * the magic number on the image is OK.
1794 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001795 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001796 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001797 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1798 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001799 rc = BOOT_EBADIMAGE;
1800 goto out;
1801 }
David Vinczec3084132020-02-18 14:50:47 +01001802#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
1803
1804#ifdef MCUBOOT_HW_ROLLBACK_PROT
1805 /* Update the stored security counter with the active image's security
1806 * counter value. It will only be updated if the new security counter is
1807 * greater than the stored value.
1808 *
1809 * In case of a successful image swapping when the swap type is TEST the
1810 * security counter can be increased only after a reset, when the swap
1811 * type is NONE and the image has marked itself "OK" (the image_ok flag
1812 * has been set). This way a "revert" can be performed when it's
1813 * necessary.
1814 */
1815 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
1816 rc = boot_update_security_counter(
1817 BOOT_CURR_IMG(state),
1818 BOOT_PRIMARY_SLOT,
1819 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
1820 if (rc != 0) {
1821 BOOT_LOG_ERR("Security counter update failed after image "
1822 "validation.");
1823 goto out;
1824 }
1825 }
1826#endif /* MCUBOOT_HW_ROLLBACK_PROT */
David Vincze1cf11b52020-03-24 07:51:09 +01001827
1828#ifdef MCUBOOT_MEASURED_BOOT
1829 rc = boot_save_boot_status(BOOT_CURR_IMG(state),
1830 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1831 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1832 if (rc != 0) {
1833 BOOT_LOG_ERR("Failed to add Image %u data to shared memory area",
1834 BOOT_CURR_IMG(state));
1835 }
1836#endif /* MCUBOOT_MEASURED_BOOT */
1837
1838#ifdef MCUBOOT_DATA_SHARING
1839 rc = boot_save_shared_data(boot_img_hdr(state, BOOT_PRIMARY_SLOT),
1840 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
1841 if (rc != 0) {
1842 BOOT_LOG_ERR("Failed to add data to shared memory area.");
1843 }
1844#endif /* MCUBOOT_DATA_SHARING */
David Vinczeba3bd602019-06-17 16:01:43 +02001845 }
1846
Fabio Utzigb0f04732019-07-31 09:49:19 -03001847#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001848 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001849 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001850#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001851
Fabio Utzigf616c542019-12-19 15:23:32 -03001852 /*
1853 * Since the boot_status struct stores plaintext encryption keys, reset
1854 * them here to avoid the possibility of jumping into an image that could
1855 * easily recover them.
1856 */
1857 memset(&bs, 0, sizeof(struct boot_status));
1858
Fabio Utzig10ee6482019-08-01 12:04:52 -03001859 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1860 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1861 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001862
Fabio Utzig298913b2019-08-28 11:22:45 -03001863out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001864 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001865#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001866 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001867#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001868 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001869 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001870 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001871 }
1872 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001873}
1874
1875int
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}
David Vinczee574f2d2020-07-10 11:42:03 +02001936
Tamas Banfe031092020-09-10 17:32:39 +02001937#else /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02001938
1939/**
1940 * Iterates over all slots and determines which contain a firmware image.
1941 *
1942 * @param state Boot loader status information.
1943 * @param slot_usage Pointer to an array, which aim is to carry information
1944 * about slots that contain an image. After return the
1945 * corresponding array elements are set to a non-zero
1946 * value if the given slots are in use (contain a firmware
1947 * image), otherwise they are set to zero.
1948 * @param slot_cnt The number of slots, which can contain firmware images.
1949 * (Equal to or smaller than the size of the
1950 * slot_usage array.)
1951 *
1952 * @return The number of found images.
1953 */
1954static uint32_t
1955boot_get_slot_usage(struct boot_loader_state *state, uint8_t slot_usage[],
1956 uint32_t slot_cnt)
1957{
1958 struct image_header *hdr = NULL;
1959 uint32_t image_cnt = 0;
1960 uint32_t slot;
1961
1962 memset(slot_usage, 0, slot_cnt);
1963
1964 for (slot = 0; slot < slot_cnt; slot++) {
1965 hdr = boot_img_hdr(state, slot);
1966
1967 if (boot_is_header_valid(hdr, BOOT_IMG_AREA(state, slot))) {
1968 slot_usage[slot] = 1;
1969 image_cnt++;
1970 BOOT_LOG_IMAGE_INFO(slot, hdr);
1971 } else {
1972 BOOT_LOG_INF("%s slot: Image not found", (slot == BOOT_PRIMARY_SLOT)
1973 ? "Primary" : "Secondary");
1974 }
1975 }
1976
1977 return image_cnt;
1978}
1979
Tamas Banfe031092020-09-10 17:32:39 +02001980#ifdef MCUBOOT_RAM_LOAD
1981
1982#if !defined(IMAGE_EXECUTABLE_RAM_START) || !defined(IMAGE_EXECUTABLE_RAM_SIZE)
1983#error "Platform MUST define executable RAM bounds in case of RAM_LOAD"
1984#endif
1985
1986/**
1987 * Verifies that the image in a slot will be loaded within the predefined bounds
1988 * that are allowed to be used by executable images.
1989 *
1990 * @param img_dst The address to which the image is going to be copied.
1991 * @param img_sz The size of the image.
1992 *
1993 * @return 0 on success; nonzero on failure.
1994 */
1995static int
1996boot_verify_ram_load_address(uint32_t img_dst, uint32_t img_sz)
1997{
1998 uint32_t img_end_addr;
1999
2000 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2001 return BOOT_EBADIMAGE;
2002 }
2003
2004 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
2005 return BOOT_EBADIMAGE;
2006 }
2007
2008 if (img_end_addr > (IMAGE_EXECUTABLE_RAM_START +
2009 IMAGE_EXECUTABLE_RAM_SIZE)) {
2010 return BOOT_EBADIMAGE;
2011 }
2012
2013 return 0;
2014}
2015
2016/**
2017 * Copies an image from a slot in the flash to an SRAM address.
2018 *
2019 * @param slot The flash slot of the image to be copied to SRAM.
2020 * @param img_dst The address at which the image needs to be copied to
2021 * SRAM.
2022 * @param img_sz The size of the image that needs to be copied to SRAM.
2023 *
2024 * @return 0 on success; nonzero on failure.
2025 */
2026static int
2027boot_copy_image_to_sram(int slot, uint32_t img_dst, uint32_t img_sz)
2028{
2029 int rc;
2030 const struct flash_area *fap_src = NULL;
2031
2032 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2033 if (rc != 0) {
2034 return BOOT_EFLASH;
2035 }
2036
2037 /* Direct copy from flash to its new location in SRAM. */
2038 rc = flash_area_read(fap_src, 0, (void *)img_dst, img_sz);
2039 if (rc != 0) {
2040 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM: %d", rc);
2041 }
2042
2043 flash_area_close(fap_src);
2044
2045 return rc;
2046}
2047
2048/**
2049 * Copies an image from a slot in the flash to an SRAM address. The load
2050 * address and image size is extracted from the image header.
2051 *
2052 * @param state Boot loader status information.
2053 * @param slot The flash slot of the image to be copied to SRAM.
2054 * @param hdr Pointer to the image header structure of the image
2055 * @param img_dst Pointer to the address at which the image needs to be
2056 * copied to SRAM.
2057 * @param img_sz Pointer to the size of the image that needs to be
2058 * copied to SRAM.
2059 *
2060 * @return 0 on success; nonzero on failure.
2061 */
2062static int
2063boot_load_image_to_sram(struct boot_loader_state *state, uint32_t slot,
2064 struct image_header *hdr, uint32_t *img_dst,
2065 uint32_t *img_sz)
2066{
2067 int rc;
2068
2069 if (hdr->ih_flags & IMAGE_F_RAM_LOAD) {
2070
2071 *img_dst = hdr->ih_load_addr;
2072
2073 rc = boot_read_image_size(state, slot, img_sz);
2074 if (rc != 0) {
2075 return rc;
2076 }
2077
2078 rc = boot_verify_ram_load_address(*img_dst, *img_sz);
2079 if (rc != 0) {
2080 BOOT_LOG_INF("Image RAM load address 0x%x is invalid.", *img_dst);
2081 return rc;
2082 }
2083
2084 /* Copy image to the load address from where it currently resides in
2085 * flash.
2086 */
2087 rc = boot_copy_image_to_sram(slot, *img_dst, *img_sz);
2088 if (rc != 0) {
2089 BOOT_LOG_INF("RAM loading to 0x%x is failed.", *img_dst);
2090 } else {
2091 BOOT_LOG_INF("RAM loading to 0x%x is succeeded.", *img_dst);
2092 }
2093 } else {
2094 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2095 * MCUBOOT_RAM_LOAD is set.
2096 */
2097 rc = BOOT_EBADIMAGE;
2098 }
2099
2100 return rc;
2101}
2102
2103/**
2104 * Removes an image from SRAM, by overwriting it with zeros.
2105 *
2106 * @param img_dst The address of the image that needs to be removed from
2107 * SRAM.
2108 * @param img_sz The size of the image that needs to be removed from
2109 * SRAM.
2110 *
2111 * @return 0 on success; nonzero on failure.
2112 */
2113static inline int
2114boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
2115{
2116 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2117 memset((void*)img_dst, 0, img_sz);
2118
2119 return 0;
2120}
2121#endif /* MCUBOOT_RAM_LOAD */
2122
David Vinczee574f2d2020-07-10 11:42:03 +02002123int
2124context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
2125{
2126 struct image_header *hdr = NULL;
2127 struct image_header *selected_image_header = NULL;
2128 uint8_t slot_usage[BOOT_NUM_SLOTS];
2129 uint32_t selected_slot;
2130 uint32_t slot;
2131 uint32_t img_cnt;
2132 uint32_t i;
2133 int fa_id;
2134 int rc;
Tamas Banfe031092020-09-10 17:32:39 +02002135#ifdef MCUBOOT_RAM_LOAD
2136 uint32_t img_dst;
2137 uint32_t img_sz;
2138 uint32_t img_loaded = 0;
2139#endif /* MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002140
2141 memset(state, 0, sizeof(struct boot_loader_state));
2142
2143 /* Open primary and secondary image areas for the duration
2144 * of this call.
2145 */
2146 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2147 fa_id = flash_area_id_from_image_slot(slot);
2148 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
2149 assert(rc == 0);
2150 }
2151
2152 /* Attempt to read an image header from each slot. */
2153 rc = boot_read_image_headers(state, false, NULL);
2154 if (rc != 0) {
2155 BOOT_LOG_WRN("Failed reading image headers.");
2156 goto out;
2157 }
2158
2159 img_cnt = boot_get_slot_usage(state, slot_usage,
2160 sizeof(slot_usage)/sizeof(slot_usage[0]));
2161
2162 if (img_cnt) {
2163 /* Select the newest and valid image. */
2164 for (i = 0; i < img_cnt; i++) {
2165 selected_slot = 0;
2166 selected_image_header = NULL;
2167
2168 /* Iterate over all the slots that are in use (contain an image)
2169 * and select the one that holds the newest image.
2170 */
2171 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2172 if (slot_usage[slot]) {
2173 hdr = boot_img_hdr(state, slot);
2174 if (selected_image_header != NULL) {
2175 rc = boot_version_cmp(&hdr->ih_ver,
2176 &selected_image_header->ih_ver);
2177 if (rc < 1) {
2178 /* The version of the image being examined wasn't
2179 * greater than the currently selected image's
2180 * version.
2181 */
2182 continue;
2183 }
2184 }
2185 selected_slot = slot;
2186 selected_image_header = hdr;
2187 }
2188 }
Tamas Banfe031092020-09-10 17:32:39 +02002189#ifdef MCUBOOT_RAM_LOAD
2190 /* Image is first loaded to RAM and authenticated there in order to
2191 * prevent TOCTOU attack during image copy. This could be applied
2192 * when loading images from external (untrusted) flash to internal
2193 * (trusted) RAM and image is authenticated before copying.
2194 */
2195 rc = boot_load_image_to_sram(state, selected_slot,
2196 selected_image_header, &img_dst,
2197 &img_sz);
2198 if (rc != 0 ) {
2199 /* Image loading failed try the next one. */
2200 continue;
2201 } else {
2202 img_loaded = 1;
2203 }
2204#endif /* MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002205 rc = boot_validate_slot(state, selected_slot, NULL);
2206 if (rc == 0) {
2207 /* If a valid image is found then there is no reason to check
2208 * the rest of the images, as each of them has a smaller version
2209 * number.
2210 */
2211 break;
2212 }
Tamas Banfe031092020-09-10 17:32:39 +02002213#ifdef MCUBOOT_RAM_LOAD
2214 else if (img_loaded) {
2215 /* If an image is found to be invalid then it is removed from
2216 * RAM to prevent it being a shellcode vector.
2217 */
2218 boot_remove_image_from_sram(img_dst, img_sz);
2219 img_loaded = 0;
2220 }
2221#endif /* MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002222 /* The selected image is invalid, mark its slot as "unused"
2223 * and start over.
2224 */
2225 slot_usage[selected_slot] = 0;
2226 }
2227
2228 if (rc || (selected_image_header == NULL)) {
2229 /* If there was no valid image at all */
2230 rc = BOOT_EBADIMAGE;
2231 goto out;
2232 }
2233
2234#ifdef MCUBOOT_HW_ROLLBACK_PROT
2235 /* Update the stored security counter with the newer (active) image's
2236 * security counter value.
2237 */
2238 rc = boot_update_security_counter(0, selected_slot,
2239 selected_image_header);
2240 if (rc != 0) {
2241 BOOT_LOG_ERR("Security counter update failed after image "
2242 "validation.");
2243 goto out;
2244 }
2245#endif /* MCUBOOT_HW_ROLLBACK_PROT */
2246
2247#ifdef MCUBOOT_MEASURED_BOOT
2248 rc = boot_save_boot_status(0, selected_image_header,
2249 BOOT_IMG_AREA(state, selected_slot));
2250 if (rc != 0) {
2251 BOOT_LOG_ERR("Failed to add image data to shared area");
2252 }
2253#endif /* MCUBOOT_MEASURED_BOOT */
2254
2255#ifdef MCUBOOT_DATA_SHARING
2256 rc = boot_save_shared_data(selected_image_header,
2257 BOOT_IMG_AREA(state, selected_slot));
2258 if (rc != 0) {
2259 BOOT_LOG_ERR("Failed to add data to shared memory area.");
2260 }
2261#endif /* MCUBOOT_DATA_SHARING */
2262
2263 BOOT_LOG_INF("Booting image from the %s slot",
2264 (selected_slot == BOOT_PRIMARY_SLOT) ?
2265 "primary" : "secondary");
2266
2267 rsp->br_flash_dev_id =
2268 BOOT_IMG_AREA(state, selected_slot)->fa_device_id;
2269 rsp->br_image_off = boot_img_slot_off(state, selected_slot);
2270 rsp->br_hdr = selected_image_header;
2271 } else {
2272 /* No candidate image available */
2273 rc = BOOT_EBADIMAGE;
2274 goto out;
2275 }
2276
2277out:
2278 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2279 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
2280 }
2281 return rc;
2282}
Tamas Banfe031092020-09-10 17:32:39 +02002283#endif /* MCUBOOT_DIRECT_XIP || MCUBOOT_RAM_LOAD */
David Vinczee574f2d2020-07-10 11:42:03 +02002284
2285/**
2286 * Prepares the booting process. This function moves images around in flash as
2287 * appropriate, and tells you what address to boot from.
2288 *
2289 * @param rsp On success, indicates how booting should occur.
2290 *
2291 * @return 0 on success; nonzero on failure.
2292 */
2293int
2294boot_go(struct boot_rsp *rsp)
2295{
2296 return context_boot_go(&boot_data, rsp);
2297}