blob: 4b8a3a20632a95c8d5f8f1276002121aa218a0d5 [file] [log] [blame]
Christopher Collins92ea77f2016-12-12 15:59:26 -08001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
David Vinczee2453472019-06-17 12:31:59 +020020/*
21 * Modifications are Copyright (c) 2019 Arm Limited.
22 */
23
Christopher Collins92ea77f2016-12-12 15:59:26 -080024/**
25 * This file provides an interface to the boot loader. Functions defined in
26 * this file should only be called while the boot loader is running.
27 */
28
29#include <assert.h>
30#include <stddef.h>
David Brown52eee562017-07-05 11:25:09 -060031#include <stdbool.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080032#include <inttypes.h>
33#include <stdlib.h>
34#include <string.h>
Christopher Collins92ea77f2016-12-12 15:59:26 -080035#include <os/os_malloc.h>
36#include "bootutil/bootutil.h"
37#include "bootutil/image.h"
38#include "bootutil_priv.h"
Fabio Utzig12d59162019-11-28 10:01:59 -030039#include "swap_priv.h"
Marti Bolivarfd20c762017-02-07 16:52:50 -050040#include "bootutil/bootutil_log.h"
41
Fabio Utzigba829042018-09-18 08:29:34 -030042#ifdef MCUBOOT_ENC_IMAGES
43#include "bootutil/enc_key.h"
44#endif
45
Fabio Utzigba1fbe62017-07-21 14:01:20 -030046#include "mcuboot_config/mcuboot_config.h"
Fabio Utzigeed80b62017-06-10 08:03:05 -030047
Emanuele Di Santo9f1933d2018-11-20 10:59:59 +010048MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
49
Marti Bolivar9b1f8bb2017-06-12 15:24:13 -040050static struct boot_loader_state boot_data;
Christopher Collins92ea77f2016-12-12 15:59:26 -080051
Fabio Utzigabec0732019-07-31 08:40:22 -030052#if (BOOT_IMAGE_NUMBER > 1)
53#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
54#else
55#define IMAGES_ITER(x)
56#endif
57
Fabio Utzig10ee6482019-08-01 12:04:52 -030058/*
59 * This macro allows some control on the allocation of local variables.
60 * When running natively on a target, we don't want to allocated huge
61 * variables on the stack, so make them global instead. For the simulator
62 * we want to run as many threads as there are tests, and it's safer
63 * to just make those variables stack allocated.
64 */
65#if !defined(__BOOTSIM__)
66#define TARGET_STATIC static
67#else
68#define TARGET_STATIC
69#endif
70
David Brownf5b33d82017-09-01 10:58:27 -060071/*
72 * Compute the total size of the given image. Includes the size of
73 * the TLVs.
74 */
Fabio Utzig13d9e352017-10-05 20:32:31 -030075#if !defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_OVERWRITE_ONLY_FAST)
David Brownf5b33d82017-09-01 10:58:27 -060076static int
Fabio Utzigd638b172019-08-09 10:38:05 -030077boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
David Brownf5b33d82017-09-01 10:58:27 -060078{
79 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -030080 struct image_tlv_info info;
Fabio Utzig233af7d2019-08-26 12:06:16 -030081 uint32_t off;
Fabio Utzige52c08e2019-09-11 19:32:00 -030082 uint32_t protect_tlv_size;
David Brownf5b33d82017-09-01 10:58:27 -060083 int area_id;
84 int rc;
85
Fabio Utzig10ee6482019-08-01 12:04:52 -030086#if (BOOT_IMAGE_NUMBER == 1)
87 (void)state;
88#endif
89
90 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
David Brownf5b33d82017-09-01 10:58:27 -060091 rc = flash_area_open(area_id, &fap);
92 if (rc != 0) {
93 rc = BOOT_EFLASH;
94 goto done;
95 }
96
Fabio Utzig61fd8882019-09-14 20:00:20 -030097 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
98
99 if (flash_area_read(fap, off, &info, sizeof(info))) {
100 rc = BOOT_EFLASH;
David Brownf5b33d82017-09-01 10:58:27 -0600101 goto done;
102 }
Fabio Utzig61fd8882019-09-14 20:00:20 -0300103
Fabio Utzige52c08e2019-09-11 19:32:00 -0300104 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
105 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
106 if (protect_tlv_size != info.it_tlv_tot) {
107 rc = BOOT_EBADIMAGE;
108 goto done;
109 }
110
111 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
112 rc = BOOT_EFLASH;
113 goto done;
114 }
115 } else if (protect_tlv_size != 0) {
116 rc = BOOT_EBADIMAGE;
117 goto done;
118 }
119
Fabio Utzig61fd8882019-09-14 20:00:20 -0300120 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
121 rc = BOOT_EBADIMAGE;
122 goto done;
123 }
124
Fabio Utzige52c08e2019-09-11 19:32:00 -0300125 *size = off + protect_tlv_size + info.it_tlv_tot;
David Brownf5b33d82017-09-01 10:58:27 -0600126 rc = 0;
127
128done:
129 flash_area_close(fap);
Fabio Utzig2eebf112017-09-04 15:25:08 -0300130 return rc;
David Brownf5b33d82017-09-01 10:58:27 -0600131}
Fabio Utzig36ec0e72017-09-05 08:10:33 -0300132#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Brownf5b33d82017-09-01 10:58:27 -0600133
Fabio Utzigc08ed212017-06-20 19:28:36 -0300134static int
Fabio Utzig12d59162019-11-28 10:01:59 -0300135boot_read_image_headers(struct boot_loader_state *state, bool require_all,
136 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800137{
138 int rc;
139 int i;
140
141 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
Fabio Utzig12d59162019-11-28 10:01:59 -0300142 rc = boot_read_image_header(state, i, boot_img_hdr(state, i), bs);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800143 if (rc != 0) {
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200144 /* If `require_all` is set, fail on any single fail, otherwise
145 * if at least the first slot's header was read successfully,
146 * then the boot loader can attempt a boot.
147 *
148 * Failure to read any headers is a fatal error.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800149 */
Fabio Utzig9c25fa72017-12-12 14:57:20 -0200150 if (i > 0 && !require_all) {
Christopher Collins0ff3c6c2016-12-21 12:04:17 -0800151 return 0;
152 } else {
153 return rc;
154 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800155 }
156 }
157
158 return 0;
159}
160
David Brownab449182019-11-15 09:32:52 -0700161static uint32_t
Fabio Utzig10ee6482019-08-01 12:04:52 -0300162boot_write_sz(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800163{
David Brownab449182019-11-15 09:32:52 -0700164 uint32_t elem_sz;
Fabio Utzig12d59162019-11-28 10:01:59 -0300165#if MCUBOOT_SWAP_USING_SCRATCH
David Brownab449182019-11-15 09:32:52 -0700166 uint32_t align;
Fabio Utzig12d59162019-11-28 10:01:59 -0300167#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800168
169 /* Figure out what size to write update status update as. The size depends
170 * on what the minimum write size is for scratch area, active image slot.
171 * We need to use the bigger of those 2 values.
172 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300173 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
Fabio Utzig12d59162019-11-28 10:01:59 -0300174#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300175 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800176 if (align > elem_sz) {
177 elem_sz = align;
178 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300179#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800180
181 return elem_sz;
182}
183
Fabio Utzig10ee6482019-08-01 12:04:52 -0300184#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
185static int
186boot_initialize_area(struct boot_loader_state *state, int flash_area)
187{
188 int num_sectors = BOOT_MAX_IMG_SECTORS;
189 int rc;
190
191 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
192 rc = flash_area_to_sectors(flash_area, &num_sectors,
193 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
194 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
195
196 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
197 rc = flash_area_to_sectors(flash_area, &num_sectors,
198 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
199 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
200
Fabio Utzig12d59162019-11-28 10:01:59 -0300201#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300202 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
203 rc = flash_area_to_sectors(flash_area, &num_sectors,
204 state->scratch.sectors);
205 state->scratch.num_sectors = (size_t)num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300206#endif
207
Fabio Utzig10ee6482019-08-01 12:04:52 -0300208 } else {
209 return BOOT_EFLASH;
210 }
211
212 return rc;
213}
214#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
215static int
216boot_initialize_area(struct boot_loader_state *state, int flash_area)
217{
218 uint32_t num_sectors;
219 struct flash_sector *out_sectors;
220 size_t *out_num_sectors;
221 int rc;
222
223 num_sectors = BOOT_MAX_IMG_SECTORS;
224
225 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
226 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
227 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
228 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
229 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
230 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300231#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300232 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
233 out_sectors = state->scratch.sectors;
234 out_num_sectors = &state->scratch.num_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -0300235#endif
Fabio Utzig10ee6482019-08-01 12:04:52 -0300236 } else {
237 return BOOT_EFLASH;
238 }
239
240 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
241 if (rc != 0) {
242 return rc;
243 }
244 *out_num_sectors = num_sectors;
245 return 0;
246}
247#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
248
Christopher Collins92ea77f2016-12-12 15:59:26 -0800249/**
250 * Determines the sector layout of both image slots and the scratch area.
251 * This information is necessary for calculating the number of bytes to erase
252 * and copy during an image swap. The information collected during this
Fabio Utzig10ee6482019-08-01 12:04:52 -0300253 * function is used to populate the state.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800254 */
255static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300256boot_read_sectors(struct boot_loader_state *state)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800257{
Fabio Utzigb0f04732019-07-31 09:49:19 -0300258 uint8_t image_index;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800259 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800260
Fabio Utzig10ee6482019-08-01 12:04:52 -0300261 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300262
263 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800264 if (rc != 0) {
265 return BOOT_EFLASH;
266 }
267
Fabio Utzig10ee6482019-08-01 12:04:52 -0300268 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Christopher Collins92ea77f2016-12-12 15:59:26 -0800269 if (rc != 0) {
270 return BOOT_EFLASH;
271 }
272
Fabio Utzig12d59162019-11-28 10:01:59 -0300273#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -0300274 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200275 if (rc != 0) {
276 return BOOT_EFLASH;
277 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300278#endif
Fabio Utzig2bd980a2018-11-26 10:38:17 -0200279
Fabio Utzig10ee6482019-08-01 12:04:52 -0300280 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800281
282 return 0;
283}
284
Fabio Utzig12d59162019-11-28 10:01:59 -0300285void
286boot_status_reset(struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800287{
Fabio Utzig4741c452019-12-19 15:32:41 -0300288#ifdef MCUBOOT_ENC_IMAGES
289 memset(&bs->enckey, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_KEY_SIZE);
290#if MCUBOOT_SWAP_SAVE_ENCTLV
291 memset(&bs->enctlv, 0xff, BOOT_NUM_SLOTS * BOOT_ENC_TLV_ALIGN_SIZE);
292#endif
293#endif /* MCUBOOT_ENC_IMAGES */
294
295 bs->use_scratch = 0;
296 bs->swap_size = 0;
297 bs->source = 0;
298
Fabio Utzig74aef312019-11-28 11:05:34 -0300299 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300300 bs->idx = BOOT_STATUS_IDX_0;
301 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700302 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300303}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800304
Fabio Utzig12d59162019-11-28 10:01:59 -0300305bool
306boot_status_is_reset(const struct boot_status *bs)
307{
Fabio Utzig74aef312019-11-28 11:05:34 -0300308 return (bs->op == BOOT_STATUS_OP_MOVE &&
309 bs->idx == BOOT_STATUS_IDX_0 &&
310 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800311}
312
313/**
314 * Writes the supplied boot status to the flash file system. The boot status
315 * contains the current state of an in-progress image copy operation.
316 *
317 * @param bs The boot status to write.
318 *
319 * @return 0 on success; nonzero on failure.
320 */
321int
Fabio Utzig12d59162019-11-28 10:01:59 -0300322boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800323{
324 const struct flash_area *fap;
325 uint32_t off;
326 int area_id;
327 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300328 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700329 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300330 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800331
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300332 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100333 * the trailer. Since in the last step the primary slot is erased, the
334 * first two status writes go to the scratch which will be copied to
335 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300336 */
337
Fabio Utzig12d59162019-11-28 10:01:59 -0300338#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300339 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800340 /* Write to scratch. */
341 area_id = FLASH_AREA_IMAGE_SCRATCH;
342 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300343#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100344 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300345 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300346#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800347 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300348#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800349
350 rc = flash_area_open(area_id, &fap);
351 if (rc != 0) {
352 rc = BOOT_EFLASH;
353 goto done;
354 }
355
356 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300357 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200358 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300359 erased_val = flash_area_erased_val(fap);
360 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700361 buf[0] = bs->state;
362
363 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800364 if (rc != 0) {
365 rc = BOOT_EFLASH;
366 goto done;
367 }
368
369 rc = 0;
370
371done:
372 flash_area_close(fap);
373 return rc;
374}
375
376/*
377 * Validate image hash/signature in a slot.
378 */
379static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300380boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
381 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800382{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300383 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300384 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300385 int rc;
386
Fabio Utzig10ee6482019-08-01 12:04:52 -0300387#if (BOOT_IMAGE_NUMBER == 1)
388 (void)state;
389#endif
390
Fabio Utzigba829042018-09-18 08:29:34 -0300391 (void)bs;
392 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300393
394 image_index = BOOT_CURR_IMG(state);
395
396#ifdef MCUBOOT_ENC_IMAGES
397 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300398 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300399 if (rc < 0) {
400 return BOOT_EBADIMAGE;
401 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300402 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300403 return BOOT_EBADIMAGE;
404 }
405 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300406#endif
407
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300408 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300409 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800410 return BOOT_EBADIMAGE;
411 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300412
Christopher Collins92ea77f2016-12-12 15:59:26 -0800413 return 0;
414}
415
416static int
417split_image_check(struct image_header *app_hdr,
418 const struct flash_area *app_fap,
419 struct image_header *loader_hdr,
420 const struct flash_area *loader_fap)
421{
422 static void *tmpbuf;
423 uint8_t loader_hash[32];
424
425 if (!tmpbuf) {
426 tmpbuf = malloc(BOOT_TMPBUF_SZ);
427 if (!tmpbuf) {
428 return BOOT_ENOMEM;
429 }
430 }
431
Fabio Utzig10ee6482019-08-01 12:04:52 -0300432 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
433 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800434 return BOOT_EBADIMAGE;
435 }
436
Fabio Utzig10ee6482019-08-01 12:04:52 -0300437 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
438 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800439 return BOOT_EBADIMAGE;
440 }
441
442 return 0;
443}
444
Fabio Utzig338a19f2018-12-03 08:37:08 -0200445/*
David Brown9bf95af2019-10-10 15:36:36 -0600446 * Check that this is a valid header. Valid means that the magic is
447 * correct, and that the sizes/offsets are "sane". Sane means that
448 * there is no overflow on the arithmetic, and that the result fits
449 * within the flash area we are in.
450 */
451static bool
452boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
453{
454 uint32_t size;
455
456 if (hdr->ih_magic != IMAGE_MAGIC) {
457 return false;
458 }
459
460 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
461 return false;
462 }
463
464 if (size >= fap->fa_size) {
465 return false;
466 }
467
468 return true;
469}
470
471/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200472 * Check that a memory area consists of a given value.
473 */
474static inline bool
475boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300476{
477 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200478 uint8_t *p = (uint8_t *)data;
479 for (i = 0; i < len; i++) {
480 if (val != p[i]) {
481 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300482 }
483 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200484 return true;
485}
486
487static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300488boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200489{
490 const struct flash_area *fap;
491 struct image_header *hdr;
492 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300493 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200494 int rc;
495
Fabio Utzig10ee6482019-08-01 12:04:52 -0300496 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300497 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200498 if (rc != 0) {
499 return -1;
500 }
501
502 erased_val = flash_area_erased_val(fap);
503 flash_area_close(fap);
504
Fabio Utzig10ee6482019-08-01 12:04:52 -0300505 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200506 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
507 return -1;
508 }
509
510 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300511}
512
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000513#if (BOOT_IMAGE_NUMBER > 1) || \
514 (defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION))
515/**
516 * Check if the version of the image is not older than required.
517 *
518 * @param req Required minimal image version.
519 * @param ver Version of the image to be checked.
520 *
521 * @return 0 if the version is sufficient, nonzero otherwise.
522 */
523static int
524boot_is_version_sufficient(struct image_version *req,
525 struct image_version *ver)
526{
527 if (ver->iv_major > req->iv_major) {
528 return 0;
529 }
530 if (ver->iv_major < req->iv_major) {
531 return BOOT_EBADVERSION;
532 }
533 /* The major version numbers are equal. */
534 if (ver->iv_minor > req->iv_minor) {
535 return 0;
536 }
537 if (ver->iv_minor < req->iv_minor) {
538 return BOOT_EBADVERSION;
539 }
540 /* The minor version numbers are equal. */
541 if (ver->iv_revision < req->iv_revision) {
542 return BOOT_EBADVERSION;
543 }
544
545 return 0;
546}
547#endif
548
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300549/*
550 * Check that there is a valid image in a slot
551 *
552 * @returns
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300553 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300554 * 1 if no bootloable image was found
555 * -1 on any errors
556 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800557static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300558boot_validate_slot(struct boot_loader_state *state, int slot,
559 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560{
561 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400562 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300563 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800564 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300565
Fabio Utzig10ee6482019-08-01 12:04:52 -0300566 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300567 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800568 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300569 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800570 }
571
Fabio Utzig10ee6482019-08-01 12:04:52 -0300572 hdr = boot_img_hdr(state, slot);
573 if (boot_check_header_erased(state, slot) == 0 ||
574 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100575 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300576 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200577 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300578 }
579
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000580#if defined(MCUBOOT_OVERWRITE_ONLY) && defined(MCUBOOT_DOWNGRADE_PREVENTION)
581 if (slot != BOOT_PRIMARY_SLOT) {
582 /* Check if version of secondary slot is sufficient */
583 rc = boot_is_version_sufficient(
584 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_ver,
585 &boot_img_hdr(state, BOOT_SECONDARY_SLOT)->ih_ver);
586 if (rc != 0 && boot_check_header_erased(state, BOOT_PRIMARY_SLOT)) {
587 BOOT_LOG_ERR("insufficient version in secondary slot");
588 flash_area_erase(fap, 0, fap->fa_size);
589 /* Image in the secondary slot does not satisfy version requirement.
590 * Erase the image and continue booting from the primary slot.
591 */
592 rc = 1;
593 goto out;
594 }
595 }
596#endif
597
David Brown9bf95af2019-10-10 15:36:36 -0600598 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100599 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700600 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100601 /* Image in the secondary slot is invalid. Erase the image and
602 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700603 */
604 }
David Brown098de832019-12-10 11:58:01 -0700605#if !defined(__BOOTSIM__)
David Vincze2d736ad2019-02-18 11:50:22 +0100606 BOOT_LOG_ERR("Image in the %s slot is not valid!",
607 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Brown098de832019-12-10 11:58:01 -0700608#endif
Håkon Øye Amundsen2d1bac12020-01-03 13:08:09 +0000609 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200610 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800611 }
612
David Vincze2d736ad2019-02-18 11:50:22 +0100613 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200614 rc = 0;
615
616out:
617 flash_area_close(fap);
618 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800619}
620
621/**
622 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100623 * that a swap operation is required, the image in the secondary slot is checked
624 * for validity. If the image in the secondary slot is invalid, it is erased,
625 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800626 *
627 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
628 */
629static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300630boot_validated_swap_type(struct boot_loader_state *state,
631 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800632{
633 int swap_type;
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300634 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800635
Fabio Utzigb0f04732019-07-31 09:49:19 -0300636 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzige575b0b2019-09-11 12:34:23 -0300637 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100638 /* Boot loader wants to switch to the secondary slot.
639 * Ensure image is valid.
640 */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300641 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
642 if (rc == 1) {
643 swap_type = BOOT_SWAP_TYPE_NONE;
644 } else if (rc != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300645 swap_type = BOOT_SWAP_TYPE_FAIL;
646 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800647 }
648
649 return swap_type;
650}
651
652/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800653 * Erases a region of flash.
654 *
Fabio Utzigba829042018-09-18 08:29:34 -0300655 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800656 * @param off The offset within the flash area to start the
657 * erase.
658 * @param sz The number of bytes to erase.
659 *
660 * @return 0 on success; nonzero on failure.
661 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300662int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300663boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800664{
Fabio Utzigba829042018-09-18 08:29:34 -0300665 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800666}
667
668/**
669 * Copies the contents of one flash region to another. You must erase the
670 * destination region prior to calling this function.
671 *
672 * @param flash_area_id_src The ID of the source flash area.
673 * @param flash_area_id_dst The ID of the destination flash area.
674 * @param off_src The offset within the source flash area to
675 * copy from.
676 * @param off_dst The offset within the destination flash area to
677 * copy to.
678 * @param sz The number of bytes to copy.
679 *
680 * @return 0 on success; nonzero on failure.
681 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300682int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300683boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300684 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300685 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800686 uint32_t off_src, uint32_t off_dst, uint32_t sz)
687{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800688 uint32_t bytes_copied;
689 int chunk_sz;
690 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300691#ifdef MCUBOOT_ENC_IMAGES
692 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300693 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300694 size_t blk_off;
695 struct image_header *hdr;
696 uint16_t idx;
697 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300698 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300699#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800700
Fabio Utzig10ee6482019-08-01 12:04:52 -0300701 TARGET_STATIC uint8_t buf[1024];
702
703#if !defined(MCUBOOT_ENC_IMAGES)
704 (void)state;
705#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800706
Christopher Collins92ea77f2016-12-12 15:59:26 -0800707 bytes_copied = 0;
708 while (bytes_copied < sz) {
709 if (sz - bytes_copied > sizeof buf) {
710 chunk_sz = sizeof buf;
711 } else {
712 chunk_sz = sz - bytes_copied;
713 }
714
715 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
716 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300717 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800718 }
719
Fabio Utzigba829042018-09-18 08:29:34 -0300720#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300721 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300722 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
723 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
724 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
725 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100726 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300727 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300728#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300729 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300730 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100731 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300732 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300733 off = off_dst;
734 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300735#else
736 off = off_dst;
737 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
738 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
739 }
740#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200741 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300742 blk_sz = chunk_sz;
743 idx = 0;
744 if (off + bytes_copied < hdr->ih_hdr_size) {
745 /* do not decrypt header */
746 blk_off = 0;
747 blk_sz = chunk_sz - hdr->ih_hdr_size;
748 idx = hdr->ih_hdr_size;
749 } else {
750 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
751 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300752 tlv_off = BOOT_TLV_OFF(hdr);
753 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300754 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300755 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300756 blk_sz = 0;
757 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300758 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300759 }
760 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300761 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300762 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
763 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300764 }
765 }
766#endif
767
Christopher Collins92ea77f2016-12-12 15:59:26 -0800768 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
769 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300770 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800771 }
772
773 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300774
775 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800776 }
777
Fabio Utzigba829042018-09-18 08:29:34 -0300778 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800779}
780
Christopher Collins92ea77f2016-12-12 15:59:26 -0800781/**
David Vincze2d736ad2019-02-18 11:50:22 +0100782 * Overwrite primary slot with the image contained in the secondary slot.
783 * If a prior copy operation was interrupted by a system reset, this function
784 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800785 *
786 * @param bs The current boot status. This function reads
787 * this struct to determine if it is resuming
788 * an interrupted swap operation. This
789 * function writes the updated status to this
790 * function on return.
791 *
792 * @return 0 on success; nonzero on failure.
793 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200794#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600795static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300796boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600797{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400798 size_t sect_count;
799 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600800 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300801 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400802 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300803 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100804 const struct flash_area *fap_primary_slot;
805 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300806 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100807
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200808 (void)bs;
809
Fabio Utzig13d9e352017-10-05 20:32:31 -0300810#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
811 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300812 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300813 assert(rc == 0);
814#endif
David Brown17609d82017-05-05 09:41:34 -0600815
David Vincze2d736ad2019-02-18 11:50:22 +0100816 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
817 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600818
Fabio Utzigb0f04732019-07-31 09:49:19 -0300819 image_index = BOOT_CURR_IMG(state);
820
821 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
822 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300823 assert (rc == 0);
824
Fabio Utzigb0f04732019-07-31 09:49:19 -0300825 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
826 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300827 assert (rc == 0);
828
Fabio Utzig10ee6482019-08-01 12:04:52 -0300829 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300830 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300831 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300832 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600833 assert(rc == 0);
834
835 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300836
837#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
838 if (size >= src_size) {
839 break;
840 }
841#endif
David Brown17609d82017-05-05 09:41:34 -0600842 }
843
Fabio Utzigba829042018-09-18 08:29:34 -0300844#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300845 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300846 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300847 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzig4741c452019-12-19 15:32:41 -0300848 fap_secondary_slot, bs);
David Vincze2d736ad2019-02-18 11:50:22 +0100849
Fabio Utzigba829042018-09-18 08:29:34 -0300850 if (rc < 0) {
851 return BOOT_EBADIMAGE;
852 }
Fabio Utzig4741c452019-12-19 15:32:41 -0300853 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300854 return BOOT_EBADIMAGE;
855 }
856 }
857#endif
858
David Vincze2d736ad2019-02-18 11:50:22 +0100859 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
860 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300861 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600862
Fabio Utzig13d9e352017-10-05 20:32:31 -0300863 /*
864 * Erases header and trailer. The trailer is erased because when a new
865 * image is written without a trailer as is the case when using newt, the
866 * trailer that was left might trigger a new upgrade.
867 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700868 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300869 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300870 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
871 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600872 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300873 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700874 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300875 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300876 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
877 last_sector),
878 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
879 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300880 assert(rc == 0);
881
David Vincze2d736ad2019-02-18 11:50:22 +0100882 flash_area_close(fap_primary_slot);
883 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300884
David Vincze2d736ad2019-02-18 11:50:22 +0100885 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600886
887 return 0;
888}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200889#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300890
Christopher Collinsa1c12042019-05-23 14:00:28 -0700891#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300892/**
893 * Swaps the two images in flash. If a prior copy operation was interrupted
894 * by a system reset, this function completes that operation.
895 *
896 * @param bs The current boot status. This function reads
897 * this struct to determine if it is resuming
898 * an interrupted swap operation. This
899 * function writes the updated status to this
900 * function on return.
901 *
902 * @return 0 on success; nonzero on failure.
903 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800904static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300905boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800906{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300907 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -0300908#ifdef MCUBOOT_ENC_IMAGES
909 const struct flash_area *fap;
910 uint8_t slot;
911 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -0300912#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300913 uint32_t size;
914 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300915 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300916 int rc;
917
918 /* FIXME: just do this if asked by user? */
919
920 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300921 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300922
Fabio Utzig12d59162019-11-28 10:01:59 -0300923 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -0300924 /*
925 * No swap ever happened, so need to find the largest image which
926 * will be used to determine the amount of sectors to swap.
927 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300928 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300929 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -0300930 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -0600931 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300932 }
Fabio Utzig2473ac02017-05-02 12:45:02 -0300933
Fabio Utzigba829042018-09-18 08:29:34 -0300934#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200935 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300936 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -0300937 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300938 assert(rc >= 0);
939
940 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300941 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300942 assert(rc == 0);
943 } else {
944 rc = 0;
945 }
946 } else {
947 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
948 }
949#endif
950
Fabio Utzig10ee6482019-08-01 12:04:52 -0300951 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -0300952 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -0300953 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -0300954 assert(rc == 0);
955 }
956
Fabio Utzigba829042018-09-18 08:29:34 -0300957#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300958 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200959 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300960 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig4741c452019-12-19 15:32:41 -0300961 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300962 assert(rc >= 0);
963
964 if (rc == 0) {
Fabio Utzig4741c452019-12-19 15:32:41 -0300965 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300966 assert(rc == 0);
967 } else {
968 rc = 0;
969 }
970 } else {
971 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
972 }
973#endif
974
Fabio Utzig46490722017-09-04 15:34:32 -0300975 if (size > copy_size) {
976 copy_size = size;
977 }
978
979 bs->swap_size = copy_size;
980 } else {
981 /*
982 * If a swap was under way, the swap_size should already be present
983 * in the trailer...
984 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300985 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300986 assert(rc == 0);
987
988 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -0300989
990#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig4741c452019-12-19 15:32:41 -0300991 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
992 rc = boot_read_enc_key(image_index, slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -0300993 assert(rc == 0);
994
995 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -0200996 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -0300997 break;
998 }
999 }
1000
1001 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig4741c452019-12-19 15:32:41 -03001002 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs);
Fabio Utzigba829042018-09-18 08:29:34 -03001003 }
1004 }
1005#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -03001006 }
1007
Fabio Utzig12d59162019-11-28 10:01:59 -03001008 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001009
David Vincze2d736ad2019-02-18 11:50:22 +01001010#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -03001011 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001012 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -07001013 BOOT_LOG_WRN("%d status write fails performing the swap",
1014 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -02001015 }
1016#endif
1017
Christopher Collins92ea77f2016-12-12 15:59:26 -08001018 return 0;
1019}
David Brown17609d82017-05-05 09:41:34 -06001020#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001021
David Vinczee32483f2019-06-13 10:46:24 +02001022#if (BOOT_IMAGE_NUMBER > 1)
1023/**
1024 * Check the image dependency whether it is satisfied and modify
1025 * the swap type if necessary.
1026 *
1027 * @param dep Image dependency which has to be verified.
1028 *
1029 * @return 0 on success; nonzero on failure.
1030 */
1031static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001032boot_verify_slot_dependency(struct boot_loader_state *state,
1033 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001034{
1035 struct image_version *dep_version;
1036 size_t dep_slot;
1037 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001038 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001039
1040 /* Determine the source of the image which is the subject of
1041 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001042 swap_type = state->swap_type[dep->image_id];
Fabio Utzigb1adb1e2019-09-11 11:42:53 -03001043 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001044 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001045 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001046
1047 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1048 if (rc != 0) {
1049 /* Dependency not satisfied.
1050 * Modify the swap type to decrease the version number of the image
1051 * (which will be located in the primary slot after the boot process),
1052 * consequently the number of unsatisfied dependencies will be
1053 * decreased or remain the same.
1054 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001055 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001056 case BOOT_SWAP_TYPE_TEST:
1057 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001058 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001059 break;
1060 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001061 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001062 break;
1063 default:
1064 break;
1065 }
1066 }
1067
1068 return rc;
1069}
1070
1071/**
1072 * Read all dependency TLVs of an image from the flash and verify
1073 * one after another to see if they are all satisfied.
1074 *
1075 * @param slot Image slot number.
1076 *
1077 * @return 0 on success; nonzero on failure.
1078 */
1079static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001080boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001081{
1082 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001083 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001084 struct image_dependency dep;
1085 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001086 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001087 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001088 int rc;
1089
Fabio Utzig10ee6482019-08-01 12:04:52 -03001090 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001091 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001092 if (rc != 0) {
1093 rc = BOOT_EFLASH;
1094 goto done;
1095 }
1096
Fabio Utzig61fd8882019-09-14 20:00:20 -03001097 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1098 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001099 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001100 goto done;
1101 }
1102
Fabio Utzig61fd8882019-09-14 20:00:20 -03001103 while (true) {
1104 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1105 if (rc < 0) {
1106 return -1;
1107 } else if (rc > 0) {
1108 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001109 break;
1110 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001111
1112 if (len != sizeof(dep)) {
1113 rc = BOOT_EBADIMAGE;
1114 goto done;
1115 }
1116
1117 rc = flash_area_read(fap, off, &dep, len);
1118 if (rc != 0) {
1119 rc = BOOT_EFLASH;
1120 goto done;
1121 }
1122
1123 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1124 rc = BOOT_EBADARGS;
1125 goto done;
1126 }
1127
1128 /* Verify dependency and modify the swap type if not satisfied. */
1129 rc = boot_verify_slot_dependency(state, &dep);
1130 if (rc != 0) {
1131 /* Dependency not satisfied. */
1132 goto done;
1133 }
David Vinczee32483f2019-06-13 10:46:24 +02001134 }
1135
1136done:
1137 flash_area_close(fap);
1138 return rc;
1139}
1140
1141/**
David Vinczee32483f2019-06-13 10:46:24 +02001142 * Iterate over all the images and verify whether the image dependencies in the
1143 * TLV area are all satisfied and update the related swap type if necessary.
1144 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001145static int
1146boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001147{
David Vinczee32483f2019-06-13 10:46:24 +02001148 int rc;
Fabio Utzig298913b2019-08-28 11:22:45 -03001149 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001150
Fabio Utzig10ee6482019-08-01 12:04:52 -03001151 BOOT_CURR_IMG(state) = 0;
1152 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001153 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1154 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1155 slot = BOOT_SECONDARY_SLOT;
1156 } else {
1157 slot = BOOT_PRIMARY_SLOT;
1158 }
1159
1160 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001161 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001162 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001163 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001164 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001165 /* Cannot upgrade due to non-met dependencies, so disable all
1166 * image upgrades.
1167 */
1168 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1169 BOOT_CURR_IMG(state) = idx;
1170 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1171 }
1172 break;
David Vinczee32483f2019-06-13 10:46:24 +02001173 } else {
1174 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03001175 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001176 }
1177 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001178 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001179}
1180#endif /* (BOOT_IMAGE_NUMBER > 1) */
1181
Christopher Collins92ea77f2016-12-12 15:59:26 -08001182/**
David Vinczeba3bd602019-06-17 16:01:43 +02001183 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001184 *
David Vinczeba3bd602019-06-17 16:01:43 +02001185 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001186 *
1187 * @return 0 on success; nonzero on failure.
1188 */
1189static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001190boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001191{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001192 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001193#ifndef MCUBOOT_OVERWRITE_ONLY
1194 uint8_t swap_type;
1195#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001196
David Vinczeba3bd602019-06-17 16:01:43 +02001197 /* At this point there are no aborted swaps. */
1198#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001199 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001200#elif defined(MCUBOOT_BOOTSTRAP)
1201 /* Check if the image update was triggered by a bad image in the
1202 * primary slot (the validity of the image in the secondary slot had
1203 * already been checked).
1204 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001205 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1206 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1207 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001208 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001209 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001210 }
1211#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001212 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001213#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001214 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001215
1216#ifndef MCUBOOT_OVERWRITE_ONLY
1217 /* The following state needs image_ok be explicitly set after the
1218 * swap was finished to avoid a new revert.
1219 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001220 swap_type = BOOT_SWAP_TYPE(state);
1221 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1222 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001223 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001224 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001225 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001226 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001227 }
1228
Fabio Utzige575b0b2019-09-11 12:34:23 -03001229 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001230 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001231 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001232 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001233 }
David Vinczeba3bd602019-06-17 16:01:43 +02001234 }
1235#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001236
David Vinczeba3bd602019-06-17 16:01:43 +02001237 return rc;
1238}
1239
1240/**
1241 * Completes a previously aborted image swap.
1242 *
1243 * @param bs The current boot status.
1244 *
1245 * @return 0 on success; nonzero on failure.
1246 */
1247#if !defined(MCUBOOT_OVERWRITE_ONLY)
1248static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001249boot_complete_partial_swap(struct boot_loader_state *state,
1250 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001251{
1252 int rc;
1253
1254 /* Determine the type of swap operation being resumed from the
1255 * `swap-type` trailer field.
1256 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001257 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001258 assert(rc == 0);
1259
Fabio Utzig10ee6482019-08-01 12:04:52 -03001260 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001261
1262 /* The following states need image_ok be explicitly set after the
1263 * swap was finished to avoid a new revert.
1264 */
1265 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1266 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001267 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001268 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001269 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001270 }
1271 }
1272
Fabio Utzige575b0b2019-09-11 12:34:23 -03001273 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001274 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001275 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001276 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001277 }
1278 }
1279
Fabio Utzig10ee6482019-08-01 12:04:52 -03001280 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001281 BOOT_LOG_ERR("panic!");
1282 assert(0);
1283
1284 /* Loop forever... */
1285 while (1) {}
1286 }
1287
1288 return rc;
1289}
1290#endif /* !MCUBOOT_OVERWRITE_ONLY */
1291
1292#if (BOOT_IMAGE_NUMBER > 1)
1293/**
1294 * Review the validity of previously determined swap types of other images.
1295 *
1296 * @param aborted_swap The current image upgrade is a
1297 * partial/aborted swap.
1298 */
1299static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001300boot_review_image_swap_types(struct boot_loader_state *state,
1301 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001302{
1303 /* In that case if we rebooted in the middle of an image upgrade process, we
1304 * must review the validity of swap types, that were previously determined
1305 * for other images. The image_ok flag had not been set before the reboot
1306 * for any of the updated images (only the copy_done flag) and thus falsely
1307 * the REVERT swap type has been determined for the previous images that had
1308 * been updated before the reboot.
1309 *
1310 * There are two separate scenarios that we have to deal with:
1311 *
1312 * 1. The reboot has happened during swapping an image:
1313 * The current image upgrade has been determined as a
1314 * partial/aborted swap.
1315 * 2. The reboot has happened between two separate image upgrades:
1316 * In this scenario we must check the swap type of the current image.
1317 * In those cases if it is NONE or REVERT we cannot certainly determine
1318 * the fact of a reboot. In a consistent state images must move in the
1319 * same direction or stay in place, e.g. in practice REVERT and TEST
1320 * swap types cannot be present at the same time. If the swap type of
1321 * the current image is either TEST, PERM or FAIL we must review the
1322 * already determined swap types of other images and set each false
1323 * REVERT swap types to NONE (these images had been successfully
1324 * updated before the system rebooted between two separate image
1325 * upgrades).
1326 */
1327
Fabio Utzig10ee6482019-08-01 12:04:52 -03001328 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001329 /* Nothing to do */
1330 return;
1331 }
1332
1333 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001334 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1335 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001336 /* Nothing to do */
1337 return;
1338 }
1339 }
1340
Fabio Utzig10ee6482019-08-01 12:04:52 -03001341 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1342 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1343 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001344 }
1345 }
1346}
1347#endif
1348
1349/**
1350 * Prepare image to be updated if required.
1351 *
1352 * Prepare image to be updated if required with completing an image swap
1353 * operation if one was aborted and/or determining the type of the
1354 * swap operation. In case of any error set the swap type to NONE.
1355 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001356 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001357 * @param bs Pointer where the read and possibly updated
1358 * boot status can be written to.
1359 */
1360static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001361boot_prepare_image_for_update(struct boot_loader_state *state,
1362 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001363{
1364 int rc;
1365
1366 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001367 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001368 if (rc != 0) {
1369 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1370 " - too small?", BOOT_MAX_IMG_SECTORS);
1371 /* Unable to determine sector layout, continue with next image
1372 * if there is one.
1373 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001374 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001375 return;
1376 }
1377
1378 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001379 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001380 if (rc != 0) {
1381 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001382 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001383 BOOT_CURR_IMG(state));
1384 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001385 return;
1386 }
1387
1388 /* If the current image's slots aren't compatible, no swap is possible.
1389 * Just boot into primary slot.
1390 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001391 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001392 boot_status_reset(bs);
1393
1394#ifndef MCUBOOT_OVERWRITE_ONLY
1395 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001396 if (rc != 0) {
1397 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001398 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001399 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001400 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001401 return;
1402 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001403#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001404
Fabio Utzig74aef312019-11-28 11:05:34 -03001405#ifdef MCUBOOT_SWAP_USING_MOVE
1406 /*
1407 * Must re-read image headers because the boot status might
1408 * have been updated in the previous function call.
1409 */
1410 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1411 if (rc != 0) {
1412 /* Continue with next image if there is one. */
1413 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1414 BOOT_CURR_IMG(state));
1415 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1416 return;
1417 }
1418#endif
1419
David Vinczeba3bd602019-06-17 16:01:43 +02001420 /* Determine if we rebooted in the middle of an image swap
1421 * operation. If a partial swap was detected, complete it.
1422 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001423 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001424
1425#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001426 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001427#endif
1428
1429#ifdef MCUBOOT_OVERWRITE_ONLY
1430 /* Should never arrive here, overwrite-only mode has
1431 * no swap state.
1432 */
1433 assert(0);
1434#else
1435 /* Determine the type of swap operation being resumed from the
1436 * `swap-type` trailer field.
1437 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001438 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001439 assert(rc == 0);
1440#endif
1441 /* Attempt to read an image header from each slot. Ensure that
1442 * image headers in slots are aligned with headers in boot_data.
1443 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001444 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001445 assert(rc == 0);
1446
1447 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001448 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001449 } else {
1450 /* There was no partial swap, determine swap type. */
1451 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001452 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1453 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1454 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001455 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001456 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001457 }
1458
1459#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001460 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001461#endif
1462
1463#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001464 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001465 /* Header checks are done first because they are
1466 * inexpensive. Since overwrite-only copies starting from
1467 * offset 0, if interrupted, it might leave a valid header
1468 * magic, so also run validation on the primary slot to be
1469 * sure it's not OK.
1470 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001471 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1472 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1473 if (boot_img_hdr(state,
1474 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1475 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001476 /* Set swap type to REVERT to overwrite the primary
1477 * slot with the image contained in secondary slot
1478 * and to trigger the explicit setting of the
1479 * image_ok flag.
1480 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001481 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001482 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001483 }
1484 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001485#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001486 }
David Vinczeba3bd602019-06-17 16:01:43 +02001487 } else {
1488 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001489 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001490 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001491}
1492
Christopher Collins92ea77f2016-12-12 15:59:26 -08001493int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001494context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001495{
Marti Bolivar84898652017-06-13 17:20:22 -04001496 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001497 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001498 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001499 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001500 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001501 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001502
1503 /* The array of slot sectors are defined here (as opposed to file scope) so
1504 * that they don't get allocated for non-boot-loader apps. This is
1505 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001506 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001507 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001508 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1509 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001510#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001511 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001512#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001513
Fabio Utzig10ee6482019-08-01 12:04:52 -03001514 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001515 has_upgrade = false;
1516
1517#if (BOOT_IMAGE_NUMBER == 1)
1518 (void)has_upgrade;
1519#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001520
David Vinczeba3bd602019-06-17 16:01:43 +02001521 /* Iterate over all the images. By the end of the loop the swap type has
1522 * to be determined for each image and all aborted swaps have to be
1523 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001524 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001525 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001526
David Vinczeba3bd602019-06-17 16:01:43 +02001527#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1528 /* The keys used for encryption may no longer be valid (could belong to
1529 * another images). Therefore, mark them as invalid to force their reload
1530 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001531 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001532 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001533#endif
1534
Fabio Utzig10ee6482019-08-01 12:04:52 -03001535 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001536
Fabio Utzig10ee6482019-08-01 12:04:52 -03001537 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001538 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001539 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001540 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001541#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001542 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001543#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001544
1545 /* Open primary and secondary image areas for the duration
1546 * of this call.
1547 */
1548 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001549 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001550 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001551 assert(rc == 0);
1552 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001553#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001554 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001555 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001556 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001557#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001558
1559 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001560 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001561
Fabio Utzige575b0b2019-09-11 12:34:23 -03001562 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001563 has_upgrade = true;
1564 }
David Vinczeba3bd602019-06-17 16:01:43 +02001565 }
1566
David Vinczee32483f2019-06-13 10:46:24 +02001567#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001568 if (has_upgrade) {
1569 /* Iterate over all the images and verify whether the image dependencies
1570 * are all satisfied and update swap type if necessary.
1571 */
1572 rc = boot_verify_dependencies(state);
1573 if (rc == BOOT_EBADVERSION) {
1574 /*
1575 * It was impossible to upgrade because the expected dependency version
1576 * was not available. Here we already changed the swap_type so that
1577 * instead of asserting the bootloader, we continue and no upgrade is
1578 * performed.
1579 */
1580 rc = 0;
1581 }
1582 }
David Vinczee32483f2019-06-13 10:46:24 +02001583#endif
1584
David Vinczeba3bd602019-06-17 16:01:43 +02001585 /* Iterate over all the images. At this point there are no aborted swaps
1586 * and the swap types are determined for each image. By the end of the loop
1587 * all required update operations will have been finished.
1588 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001589 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001590
1591#if (BOOT_IMAGE_NUMBER > 1)
1592#ifdef MCUBOOT_ENC_IMAGES
1593 /* The keys used for encryption may no longer be valid (could belong to
1594 * another images). Therefore, mark them as invalid to force their reload
1595 * by boot_enc_load().
1596 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001597 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001598#endif /* MCUBOOT_ENC_IMAGES */
1599
1600 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001601 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001602#endif /* (BOOT_IMAGE_NUMBER > 1) */
1603
1604 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001605 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001606
Fabio Utzig10ee6482019-08-01 12:04:52 -03001607 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001608 case BOOT_SWAP_TYPE_NONE:
1609 break;
1610
1611 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1612 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1613 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001614 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001615 assert(rc == 0);
1616 break;
1617
1618 case BOOT_SWAP_TYPE_FAIL:
1619 /* The image in secondary slot was invalid and is now erased. Ensure
1620 * we don't try to boot into it again on the next reboot. Do this by
1621 * pretending we just reverted back to primary slot.
1622 */
1623#ifndef MCUBOOT_OVERWRITE_ONLY
1624 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001625 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001626 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001627 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001628 }
1629#endif /* !MCUBOOT_OVERWRITE_ONLY */
1630 break;
1631
1632 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001633 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001634 }
1635
Fabio Utzig10ee6482019-08-01 12:04:52 -03001636 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001637 BOOT_LOG_ERR("panic!");
1638 assert(0);
1639
1640 /* Loop forever... */
1641 while (1) {}
1642 }
1643 }
1644
1645 /* Iterate over all the images. At this point all required update operations
1646 * have finished. By the end of the loop each image in the primary slot will
1647 * have been re-validated.
1648 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001649 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1650 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001651 /* Attempt to read an image header from each slot. Ensure that image
1652 * headers in slots are aligned with headers in boot_data.
1653 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001654 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001655 if (rc != 0) {
1656 goto out;
1657 }
1658 /* Since headers were reloaded, it can be assumed we just performed
1659 * a swap or overwrite. Now the header info that should be used to
1660 * provide the data for the bootstrap, which previously was at
1661 * secondary slot, was updated to primary slot.
1662 */
1663 }
1664
1665#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001666 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001667 if (rc != 0) {
1668 rc = BOOT_EBADIMAGE;
1669 goto out;
1670 }
1671#else
1672 /* Even if we're not re-validating the primary slot, we could be booting
1673 * onto an empty flash chip. At least do a basic sanity check that
1674 * the magic number on the image is OK.
1675 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001676 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001677 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001678 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1679 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001680 rc = BOOT_EBADIMAGE;
1681 goto out;
1682 }
1683#endif
1684 }
1685
Fabio Utzigb0f04732019-07-31 09:49:19 -03001686#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001687 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001688 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001689#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001690
Fabio Utzigf616c542019-12-19 15:23:32 -03001691 /*
1692 * Since the boot_status struct stores plaintext encryption keys, reset
1693 * them here to avoid the possibility of jumping into an image that could
1694 * easily recover them.
1695 */
1696 memset(&bs, 0, sizeof(struct boot_status));
1697
Fabio Utzig10ee6482019-08-01 12:04:52 -03001698 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1699 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1700 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001701
Fabio Utzig298913b2019-08-28 11:22:45 -03001702out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001703 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001704#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001705 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001706#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001707 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001708 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001709 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001710 }
1711 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001712}
1713
Fabio Utzig10ee6482019-08-01 12:04:52 -03001714/**
1715 * Prepares the booting process. This function moves images around in flash as
1716 * appropriate, and tells you what address to boot from.
1717 *
1718 * @param rsp On success, indicates how booting should occur.
1719 *
1720 * @return 0 on success; nonzero on failure.
1721 */
1722int
1723boot_go(struct boot_rsp *rsp)
1724{
1725 return context_boot_go(&boot_data, rsp);
1726}
1727
Christopher Collins92ea77f2016-12-12 15:59:26 -08001728int
1729split_go(int loader_slot, int split_slot, void **entry)
1730{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001731 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001732 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001733 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001734 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001735 int rc;
1736
Christopher Collins92ea77f2016-12-12 15:59:26 -08001737 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1738 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001739 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001740 }
David Vinczeba3bd602019-06-17 16:01:43 +02001741 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1742 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001743
1744 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1745 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001746 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001747 assert(rc == 0);
1748 split_flash_id = flash_area_id_from_image_slot(split_slot);
1749 rc = flash_area_open(split_flash_id,
1750 &BOOT_IMG_AREA(&boot_data, split_slot));
1751 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001752
1753 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001754 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001755 if (rc != 0) {
1756 rc = SPLIT_GO_ERR;
1757 goto done;
1758 }
1759
Fabio Utzig12d59162019-11-28 10:01:59 -03001760 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001761 if (rc != 0) {
1762 goto done;
1763 }
1764
Christopher Collins92ea77f2016-12-12 15:59:26 -08001765 /* Don't check the bootable image flag because we could really call a
1766 * bootable or non-bootable image. Just validate that the image check
1767 * passes which is distinct from the normal check.
1768 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001769 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001770 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001771 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001772 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001773 if (rc != 0) {
1774 rc = SPLIT_GO_NON_MATCHING;
1775 goto done;
1776 }
1777
Marti Bolivarea088872017-06-12 17:10:49 -04001778 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001779 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001780 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001781 rc = SPLIT_GO_OK;
1782
1783done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001784 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1785 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001786 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001787 return rc;
1788}