blob: 36ea75425887c4752aa1bb3bba4fbb9b26b499df [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{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800288 memset(bs, 0, sizeof *bs);
Fabio Utzig74aef312019-11-28 11:05:34 -0300289 bs->op = BOOT_STATUS_OP_MOVE;
Fabio Utzig39000012018-07-30 12:40:20 -0300290 bs->idx = BOOT_STATUS_IDX_0;
291 bs->state = BOOT_STATUS_STATE_0;
Christopher Collinsa1c12042019-05-23 14:00:28 -0700292 bs->swap_type = BOOT_SWAP_TYPE_NONE;
Fabio Utzig12d59162019-11-28 10:01:59 -0300293}
Christopher Collins92ea77f2016-12-12 15:59:26 -0800294
Fabio Utzig12d59162019-11-28 10:01:59 -0300295bool
296boot_status_is_reset(const struct boot_status *bs)
297{
Fabio Utzig74aef312019-11-28 11:05:34 -0300298 return (bs->op == BOOT_STATUS_OP_MOVE &&
299 bs->idx == BOOT_STATUS_IDX_0 &&
300 bs->state == BOOT_STATUS_STATE_0);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800301}
302
303/**
304 * Writes the supplied boot status to the flash file system. The boot status
305 * contains the current state of an in-progress image copy operation.
306 *
307 * @param bs The boot status to write.
308 *
309 * @return 0 on success; nonzero on failure.
310 */
311int
Fabio Utzig12d59162019-11-28 10:01:59 -0300312boot_write_status(const struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800313{
314 const struct flash_area *fap;
315 uint32_t off;
316 int area_id;
317 int rc;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300318 uint8_t buf[BOOT_MAX_ALIGN];
David Brown9d725462017-01-23 15:50:58 -0700319 uint8_t align;
Fabio Utzig39000012018-07-30 12:40:20 -0300320 uint8_t erased_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800321
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300322 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze2d736ad2019-02-18 11:50:22 +0100323 * the trailer. Since in the last step the primary slot is erased, the
324 * first two status writes go to the scratch which will be copied to
325 * the primary slot!
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300326 */
327
Fabio Utzig12d59162019-11-28 10:01:59 -0300328#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig2473ac02017-05-02 12:45:02 -0300329 if (bs->use_scratch) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800330 /* Write to scratch. */
331 area_id = FLASH_AREA_IMAGE_SCRATCH;
332 } else {
Fabio Utzig12d59162019-11-28 10:01:59 -0300333#endif
David Vincze2d736ad2019-02-18 11:50:22 +0100334 /* Write to the primary slot. */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300335 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Fabio Utzig12d59162019-11-28 10:01:59 -0300336#if MCUBOOT_SWAP_USING_SCRATCH
Christopher Collins92ea77f2016-12-12 15:59:26 -0800337 }
Fabio Utzig12d59162019-11-28 10:01:59 -0300338#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800339
340 rc = flash_area_open(area_id, &fap);
341 if (rc != 0) {
342 rc = BOOT_EFLASH;
343 goto done;
344 }
345
346 off = boot_status_off(fap) +
Fabio Utzig12d59162019-11-28 10:01:59 -0300347 boot_status_internal_off(bs, BOOT_WRITE_SZ(state));
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +0200348 align = flash_area_align(fap);
Fabio Utzig39000012018-07-30 12:40:20 -0300349 erased_val = flash_area_erased_val(fap);
350 memset(buf, erased_val, BOOT_MAX_ALIGN);
David Brown9d725462017-01-23 15:50:58 -0700351 buf[0] = bs->state;
352
353 rc = flash_area_write(fap, off, buf, align);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800354 if (rc != 0) {
355 rc = BOOT_EFLASH;
356 goto done;
357 }
358
359 rc = 0;
360
361done:
362 flash_area_close(fap);
363 return rc;
364}
365
366/*
367 * Validate image hash/signature in a slot.
368 */
369static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300370boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
371 const struct flash_area *fap, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800372{
Fabio Utzig10ee6482019-08-01 12:04:52 -0300373 TARGET_STATIC uint8_t tmpbuf[BOOT_TMPBUF_SZ];
Fabio Utzigb0f04732019-07-31 09:49:19 -0300374 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300375 int rc;
376
Fabio Utzig10ee6482019-08-01 12:04:52 -0300377#if (BOOT_IMAGE_NUMBER == 1)
378 (void)state;
379#endif
380
Fabio Utzigba829042018-09-18 08:29:34 -0300381 (void)bs;
382 (void)rc;
Fabio Utzigbc077932019-08-26 11:16:34 -0300383
384 image_index = BOOT_CURR_IMG(state);
385
386#ifdef MCUBOOT_ENC_IMAGES
387 if (MUST_DECRYPT(fap, image_index, hdr)) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300388 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300389 if (rc < 0) {
390 return BOOT_EBADIMAGE;
391 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300392 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300393 return BOOT_EBADIMAGE;
394 }
395 }
Fabio Utzigbc077932019-08-26 11:16:34 -0300396#endif
397
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300398 if (bootutil_img_validate(BOOT_CURR_ENC(state), image_index, hdr, fap, tmpbuf,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300399 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800400 return BOOT_EBADIMAGE;
401 }
Fabio Utzig10ee6482019-08-01 12:04:52 -0300402
Christopher Collins92ea77f2016-12-12 15:59:26 -0800403 return 0;
404}
405
406static int
407split_image_check(struct image_header *app_hdr,
408 const struct flash_area *app_fap,
409 struct image_header *loader_hdr,
410 const struct flash_area *loader_fap)
411{
412 static void *tmpbuf;
413 uint8_t loader_hash[32];
414
415 if (!tmpbuf) {
416 tmpbuf = malloc(BOOT_TMPBUF_SZ);
417 if (!tmpbuf) {
418 return BOOT_ENOMEM;
419 }
420 }
421
Fabio Utzig10ee6482019-08-01 12:04:52 -0300422 if (bootutil_img_validate(NULL, 0, loader_hdr, loader_fap, tmpbuf,
423 BOOT_TMPBUF_SZ, NULL, 0, loader_hash)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800424 return BOOT_EBADIMAGE;
425 }
426
Fabio Utzig10ee6482019-08-01 12:04:52 -0300427 if (bootutil_img_validate(NULL, 0, app_hdr, app_fap, tmpbuf,
428 BOOT_TMPBUF_SZ, loader_hash, 32, NULL)) {
Christopher Collins92ea77f2016-12-12 15:59:26 -0800429 return BOOT_EBADIMAGE;
430 }
431
432 return 0;
433}
434
Fabio Utzig338a19f2018-12-03 08:37:08 -0200435/*
David Brown9bf95af2019-10-10 15:36:36 -0600436 * Check that this is a valid header. Valid means that the magic is
437 * correct, and that the sizes/offsets are "sane". Sane means that
438 * there is no overflow on the arithmetic, and that the result fits
439 * within the flash area we are in.
440 */
441static bool
442boot_is_header_valid(const struct image_header *hdr, const struct flash_area *fap)
443{
444 uint32_t size;
445
446 if (hdr->ih_magic != IMAGE_MAGIC) {
447 return false;
448 }
449
450 if (!boot_u32_safe_add(&size, hdr->ih_img_size, hdr->ih_hdr_size)) {
451 return false;
452 }
453
454 if (size >= fap->fa_size) {
455 return false;
456 }
457
458 return true;
459}
460
461/*
Fabio Utzig338a19f2018-12-03 08:37:08 -0200462 * Check that a memory area consists of a given value.
463 */
464static inline bool
465boot_data_is_set_to(uint8_t val, void *data, size_t len)
Fabio Utzig39000012018-07-30 12:40:20 -0300466{
467 uint8_t i;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200468 uint8_t *p = (uint8_t *)data;
469 for (i = 0; i < len; i++) {
470 if (val != p[i]) {
471 return false;
Fabio Utzig39000012018-07-30 12:40:20 -0300472 }
473 }
Fabio Utzig338a19f2018-12-03 08:37:08 -0200474 return true;
475}
476
477static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300478boot_check_header_erased(struct boot_loader_state *state, int slot)
Fabio Utzig338a19f2018-12-03 08:37:08 -0200479{
480 const struct flash_area *fap;
481 struct image_header *hdr;
482 uint8_t erased_val;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300483 int area_id;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200484 int rc;
485
Fabio Utzig10ee6482019-08-01 12:04:52 -0300486 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300487 rc = flash_area_open(area_id, &fap);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200488 if (rc != 0) {
489 return -1;
490 }
491
492 erased_val = flash_area_erased_val(fap);
493 flash_area_close(fap);
494
Fabio Utzig10ee6482019-08-01 12:04:52 -0300495 hdr = boot_img_hdr(state, slot);
Fabio Utzig338a19f2018-12-03 08:37:08 -0200496 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic, sizeof(hdr->ih_magic))) {
497 return -1;
498 }
499
500 return 0;
Fabio Utzig39000012018-07-30 12:40:20 -0300501}
502
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300503/*
504 * Check that there is a valid image in a slot
505 *
506 * @returns
Sam Bristowd0ca0ff2019-10-30 20:51:35 +1300507 * 0 if image was successfully validated
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300508 * 1 if no bootloable image was found
509 * -1 on any errors
510 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800511static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300512boot_validate_slot(struct boot_loader_state *state, int slot,
513 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800514{
515 const struct flash_area *fap;
Marti Bolivarf804f622017-06-12 15:41:48 -0400516 struct image_header *hdr;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300517 int area_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800518 int rc;
Fabio Utzig7ebb7c22017-04-26 10:59:31 -0300519
Fabio Utzig10ee6482019-08-01 12:04:52 -0300520 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -0300521 rc = flash_area_open(area_id, &fap);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800522 if (rc != 0) {
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300523 return -1;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800524 }
525
Fabio Utzig10ee6482019-08-01 12:04:52 -0300526 hdr = boot_img_hdr(state, slot);
527 if (boot_check_header_erased(state, slot) == 0 ||
528 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100529 /* No bootable image in slot; continue booting from the primary slot. */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300530 rc = 1;
Fabio Utzig338a19f2018-12-03 08:37:08 -0200531 goto out;
Fabio Utzig39000012018-07-30 12:40:20 -0300532 }
533
David Brown9bf95af2019-10-10 15:36:36 -0600534 if (!boot_is_header_valid(hdr, fap) || boot_image_check(state, hdr, fap, bs)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100535 if (slot != BOOT_PRIMARY_SLOT) {
David Brownb38e0442017-02-24 13:57:12 -0700536 flash_area_erase(fap, 0, fap->fa_size);
David Vincze2d736ad2019-02-18 11:50:22 +0100537 /* Image in the secondary slot is invalid. Erase the image and
538 * continue booting from the primary slot.
David Brownb38e0442017-02-24 13:57:12 -0700539 */
540 }
David Vincze2d736ad2019-02-18 11:50:22 +0100541 BOOT_LOG_ERR("Image in the %s slot is not valid!",
542 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
Fabio Utzig338a19f2018-12-03 08:37:08 -0200543 rc = -1;
544 goto out;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800545 }
546
David Vincze2d736ad2019-02-18 11:50:22 +0100547 /* Image in the secondary slot is valid. */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200548 rc = 0;
549
550out:
551 flash_area_close(fap);
552 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800553}
554
555/**
556 * Determines which swap operation to perform, if any. If it is determined
David Vincze2d736ad2019-02-18 11:50:22 +0100557 * that a swap operation is required, the image in the secondary slot is checked
558 * for validity. If the image in the secondary slot is invalid, it is erased,
559 * and a swap type of "none" is indicated.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800560 *
561 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
562 */
563static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300564boot_validated_swap_type(struct boot_loader_state *state,
565 struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800566{
567 int swap_type;
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300568 int rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800569
Fabio Utzigb0f04732019-07-31 09:49:19 -0300570 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
Fabio Utzige575b0b2019-09-11 12:34:23 -0300571 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100572 /* Boot loader wants to switch to the secondary slot.
573 * Ensure image is valid.
574 */
Fabio Utzigb1adb1e2019-09-11 11:42:53 -0300575 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
576 if (rc == 1) {
577 swap_type = BOOT_SWAP_TYPE_NONE;
578 } else if (rc != 0) {
Fabio Utzigb5b2f552017-06-30 10:03:47 -0300579 swap_type = BOOT_SWAP_TYPE_FAIL;
580 }
Christopher Collins92ea77f2016-12-12 15:59:26 -0800581 }
582
583 return swap_type;
584}
585
586/**
Christopher Collins92ea77f2016-12-12 15:59:26 -0800587 * Erases a region of flash.
588 *
Fabio Utzigba829042018-09-18 08:29:34 -0300589 * @param flash_area The flash_area containing the region to erase.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800590 * @param off The offset within the flash area to start the
591 * erase.
592 * @param sz The number of bytes to erase.
593 *
594 * @return 0 on success; nonzero on failure.
595 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300596int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300597boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800598{
Fabio Utzigba829042018-09-18 08:29:34 -0300599 return flash_area_erase(fap, off, sz);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800600}
601
602/**
603 * Copies the contents of one flash region to another. You must erase the
604 * destination region prior to calling this function.
605 *
606 * @param flash_area_id_src The ID of the source flash area.
607 * @param flash_area_id_dst The ID of the destination flash area.
608 * @param off_src The offset within the source flash area to
609 * copy from.
610 * @param off_dst The offset within the destination flash area to
611 * copy to.
612 * @param sz The number of bytes to copy.
613 *
614 * @return 0 on success; nonzero on failure.
615 */
Fabio Utzig12d59162019-11-28 10:01:59 -0300616int
Fabio Utzigc28005b2019-09-10 12:18:29 -0300617boot_copy_region(struct boot_loader_state *state,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300618 const struct flash_area *fap_src,
Fabio Utzigba829042018-09-18 08:29:34 -0300619 const struct flash_area *fap_dst,
Christopher Collins92ea77f2016-12-12 15:59:26 -0800620 uint32_t off_src, uint32_t off_dst, uint32_t sz)
621{
Christopher Collins92ea77f2016-12-12 15:59:26 -0800622 uint32_t bytes_copied;
623 int chunk_sz;
624 int rc;
Fabio Utzigba829042018-09-18 08:29:34 -0300625#ifdef MCUBOOT_ENC_IMAGES
626 uint32_t off;
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300627 uint32_t tlv_off;
Fabio Utzigba829042018-09-18 08:29:34 -0300628 size_t blk_off;
629 struct image_header *hdr;
630 uint16_t idx;
631 uint32_t blk_sz;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300632 uint8_t image_index;
Fabio Utzigba829042018-09-18 08:29:34 -0300633#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800634
Fabio Utzig10ee6482019-08-01 12:04:52 -0300635 TARGET_STATIC uint8_t buf[1024];
636
637#if !defined(MCUBOOT_ENC_IMAGES)
638 (void)state;
639#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800640
Christopher Collins92ea77f2016-12-12 15:59:26 -0800641 bytes_copied = 0;
642 while (bytes_copied < sz) {
643 if (sz - bytes_copied > sizeof buf) {
644 chunk_sz = sizeof buf;
645 } else {
646 chunk_sz = sz - bytes_copied;
647 }
648
649 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
650 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300651 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800652 }
653
Fabio Utzigba829042018-09-18 08:29:34 -0300654#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300655 image_index = BOOT_CURR_IMG(state);
Fabio Utzig74aef312019-11-28 11:05:34 -0300656 if ((fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) ||
657 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) &&
658 !(fap_src->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index) &&
659 fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index))) {
David Vincze2d736ad2019-02-18 11:50:22 +0100660 /* assume the secondary slot as src, needs decryption */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300661 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig74aef312019-11-28 11:05:34 -0300662#if !defined(MCUBOOT_SWAP_USING_MOVE)
Fabio Utzigba829042018-09-18 08:29:34 -0300663 off = off_src;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300664 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
David Vincze2d736ad2019-02-18 11:50:22 +0100665 /* might need encryption (metadata from the primary slot) */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300666 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzigba829042018-09-18 08:29:34 -0300667 off = off_dst;
668 }
Fabio Utzig74aef312019-11-28 11:05:34 -0300669#else
670 off = off_dst;
671 if (fap_dst->fa_id == FLASH_AREA_IMAGE_SECONDARY(image_index)) {
672 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
673 }
674#endif
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200675 if (IS_ENCRYPTED(hdr)) {
Fabio Utzigba829042018-09-18 08:29:34 -0300676 blk_sz = chunk_sz;
677 idx = 0;
678 if (off + bytes_copied < hdr->ih_hdr_size) {
679 /* do not decrypt header */
680 blk_off = 0;
681 blk_sz = chunk_sz - hdr->ih_hdr_size;
682 idx = hdr->ih_hdr_size;
683 } else {
684 blk_off = ((off + bytes_copied) - hdr->ih_hdr_size) & 0xf;
685 }
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300686 tlv_off = BOOT_TLV_OFF(hdr);
687 if (off + bytes_copied + chunk_sz > tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300688 /* do not decrypt TLVs */
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300689 if (off + bytes_copied >= tlv_off) {
Fabio Utzigba829042018-09-18 08:29:34 -0300690 blk_sz = 0;
691 } else {
Fabio Utziga87cc7d2019-08-26 11:21:45 -0300692 blk_sz = tlv_off - (off + bytes_copied);
Fabio Utzigba829042018-09-18 08:29:34 -0300693 }
694 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300695 boot_encrypt(BOOT_CURR_ENC(state), image_index, fap_src,
Fabio Utzigb0f04732019-07-31 09:49:19 -0300696 (off + bytes_copied + idx) - hdr->ih_hdr_size, blk_sz,
697 blk_off, &buf[idx]);
Fabio Utzigba829042018-09-18 08:29:34 -0300698 }
699 }
700#endif
701
Christopher Collins92ea77f2016-12-12 15:59:26 -0800702 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
703 if (rc != 0) {
Fabio Utzigba829042018-09-18 08:29:34 -0300704 return BOOT_EFLASH;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800705 }
706
707 bytes_copied += chunk_sz;
Fabio Utzig853657c2019-05-07 08:06:07 -0300708
709 MCUBOOT_WATCHDOG_FEED();
Christopher Collins92ea77f2016-12-12 15:59:26 -0800710 }
711
Fabio Utzigba829042018-09-18 08:29:34 -0300712 return 0;
Christopher Collins92ea77f2016-12-12 15:59:26 -0800713}
714
Christopher Collins92ea77f2016-12-12 15:59:26 -0800715/**
David Vincze2d736ad2019-02-18 11:50:22 +0100716 * Overwrite primary slot with the image contained in the secondary slot.
717 * If a prior copy operation was interrupted by a system reset, this function
718 * redos the copy.
Christopher Collins92ea77f2016-12-12 15:59:26 -0800719 *
720 * @param bs The current boot status. This function reads
721 * this struct to determine if it is resuming
722 * an interrupted swap operation. This
723 * function writes the updated status to this
724 * function on return.
725 *
726 * @return 0 on success; nonzero on failure.
727 */
Fabio Utzig338a19f2018-12-03 08:37:08 -0200728#if defined(MCUBOOT_OVERWRITE_ONLY) || defined(MCUBOOT_BOOTSTRAP)
David Brown17609d82017-05-05 09:41:34 -0600729static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300730boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
David Brown17609d82017-05-05 09:41:34 -0600731{
Marti Bolivard3269fd2017-06-12 16:31:12 -0400732 size_t sect_count;
733 size_t sect;
David Brown17609d82017-05-05 09:41:34 -0600734 int rc;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300735 size_t size;
Marti Bolivard3269fd2017-06-12 16:31:12 -0400736 size_t this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300737 size_t last_sector;
David Vincze2d736ad2019-02-18 11:50:22 +0100738 const struct flash_area *fap_primary_slot;
739 const struct flash_area *fap_secondary_slot;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300740 uint8_t image_index;
David Vincze2d736ad2019-02-18 11:50:22 +0100741
Fabio Utzigaaf767c2017-12-05 10:22:46 -0200742 (void)bs;
743
Fabio Utzig13d9e352017-10-05 20:32:31 -0300744#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
745 uint32_t src_size = 0;
Fabio Utzigd638b172019-08-09 10:38:05 -0300746 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &src_size);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300747 assert(rc == 0);
748#endif
David Brown17609d82017-05-05 09:41:34 -0600749
David Vincze2d736ad2019-02-18 11:50:22 +0100750 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
751 BOOT_LOG_INF("Erasing the primary slot");
David Brown17609d82017-05-05 09:41:34 -0600752
Fabio Utzigb0f04732019-07-31 09:49:19 -0300753 image_index = BOOT_CURR_IMG(state);
754
755 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
756 &fap_primary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300757 assert (rc == 0);
758
Fabio Utzigb0f04732019-07-31 09:49:19 -0300759 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
760 &fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300761 assert (rc == 0);
762
Fabio Utzig10ee6482019-08-01 12:04:52 -0300763 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300764 for (sect = 0, size = 0; sect < sect_count; sect++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300765 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300766 rc = boot_erase_region(fap_primary_slot, size, this_size);
David Brown17609d82017-05-05 09:41:34 -0600767 assert(rc == 0);
768
769 size += this_size;
Fabio Utzig13d9e352017-10-05 20:32:31 -0300770
771#if defined(MCUBOOT_OVERWRITE_ONLY_FAST)
772 if (size >= src_size) {
773 break;
774 }
775#endif
David Brown17609d82017-05-05 09:41:34 -0600776 }
777
Fabio Utzigba829042018-09-18 08:29:34 -0300778#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300779 if (IS_ENCRYPTED(boot_img_hdr(state, BOOT_SECONDARY_SLOT))) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300780 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300781 boot_img_hdr(state, BOOT_SECONDARY_SLOT),
Fabio Utzigb0f04732019-07-31 09:49:19 -0300782 fap_secondary_slot, bs->enckey[1]);
David Vincze2d736ad2019-02-18 11:50:22 +0100783
Fabio Utzigba829042018-09-18 08:29:34 -0300784 if (rc < 0) {
785 return BOOT_EBADIMAGE;
786 }
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300787 if (rc == 0 && boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1])) {
Fabio Utzigba829042018-09-18 08:29:34 -0300788 return BOOT_EBADIMAGE;
789 }
790 }
791#endif
792
David Vincze2d736ad2019-02-18 11:50:22 +0100793 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
794 size);
Fabio Utzigc28005b2019-09-10 12:18:29 -0300795 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot, 0, 0, size);
David Brown17609d82017-05-05 09:41:34 -0600796
Fabio Utzig13d9e352017-10-05 20:32:31 -0300797 /*
798 * Erases header and trailer. The trailer is erased because when a new
799 * image is written without a trailer as is the case when using newt, the
800 * trailer that was left might trigger a new upgrade.
801 */
Christopher Collins2c88e692019-05-22 15:10:14 -0700802 BOOT_LOG_DBG("erasing secondary header");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300803 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300804 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
805 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
David Brown17609d82017-05-05 09:41:34 -0600806 assert(rc == 0);
Fabio Utzig10ee6482019-08-01 12:04:52 -0300807 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
Christopher Collins2c88e692019-05-22 15:10:14 -0700808 BOOT_LOG_DBG("erasing secondary trailer");
Fabio Utzigc28005b2019-09-10 12:18:29 -0300809 rc = boot_erase_region(fap_secondary_slot,
Fabio Utzig10ee6482019-08-01 12:04:52 -0300810 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
811 last_sector),
812 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
813 last_sector));
Fabio Utzig13d9e352017-10-05 20:32:31 -0300814 assert(rc == 0);
815
David Vincze2d736ad2019-02-18 11:50:22 +0100816 flash_area_close(fap_primary_slot);
817 flash_area_close(fap_secondary_slot);
Fabio Utzigba829042018-09-18 08:29:34 -0300818
David Vincze2d736ad2019-02-18 11:50:22 +0100819 /* TODO: Perhaps verify the primary slot's signature again? */
David Brown17609d82017-05-05 09:41:34 -0600820
821 return 0;
822}
Fabio Utzig338a19f2018-12-03 08:37:08 -0200823#endif
Fabio Utzigba829042018-09-18 08:29:34 -0300824
Christopher Collinsa1c12042019-05-23 14:00:28 -0700825#if !defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzigba829042018-09-18 08:29:34 -0300826/**
827 * Swaps the two images in flash. If a prior copy operation was interrupted
828 * by a system reset, this function completes that operation.
829 *
830 * @param bs The current boot status. This function reads
831 * this struct to determine if it is resuming
832 * an interrupted swap operation. This
833 * function writes the updated status to this
834 * function on return.
835 *
836 * @return 0 on success; nonzero on failure.
837 */
Christopher Collins92ea77f2016-12-12 15:59:26 -0800838static int
Fabio Utzig10ee6482019-08-01 12:04:52 -0300839boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins92ea77f2016-12-12 15:59:26 -0800840{
Fabio Utzig2473ac02017-05-02 12:45:02 -0300841 struct image_header *hdr;
Fabio Utzigba829042018-09-18 08:29:34 -0300842#ifdef MCUBOOT_ENC_IMAGES
843 const struct flash_area *fap;
844 uint8_t slot;
845 uint8_t i;
Fabio Utzigba829042018-09-18 08:29:34 -0300846#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300847 uint32_t size;
848 uint32_t copy_size;
Fabio Utzigb0f04732019-07-31 09:49:19 -0300849 uint8_t image_index;
Fabio Utzig2473ac02017-05-02 12:45:02 -0300850 int rc;
851
852 /* FIXME: just do this if asked by user? */
853
854 size = copy_size = 0;
Fabio Utzig10ee6482019-08-01 12:04:52 -0300855 image_index = BOOT_CURR_IMG(state);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300856
Fabio Utzig12d59162019-11-28 10:01:59 -0300857 if (boot_status_is_reset(bs)) {
Fabio Utzig46490722017-09-04 15:34:32 -0300858 /*
859 * No swap ever happened, so need to find the largest image which
860 * will be used to determine the amount of sectors to swap.
861 */
Fabio Utzig10ee6482019-08-01 12:04:52 -0300862 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300863 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -0300864 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
David Brownf5b33d82017-09-01 10:58:27 -0600865 assert(rc == 0);
Fabio Utzig2473ac02017-05-02 12:45:02 -0300866 }
Fabio Utzig2473ac02017-05-02 12:45:02 -0300867
Fabio Utzigba829042018-09-18 08:29:34 -0300868#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200869 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300870 fap = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300871 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -0300872 assert(rc >= 0);
873
874 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300875 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 0, bs->enckey[0]);
Fabio Utzigba829042018-09-18 08:29:34 -0300876 assert(rc == 0);
877 } else {
878 rc = 0;
879 }
880 } else {
881 memset(bs->enckey[0], 0xff, BOOT_ENC_KEY_SIZE);
882 }
883#endif
884
Fabio Utzig10ee6482019-08-01 12:04:52 -0300885 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig46490722017-09-04 15:34:32 -0300886 if (hdr->ih_magic == IMAGE_MAGIC) {
Fabio Utzigd638b172019-08-09 10:38:05 -0300887 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
Fabio Utzig46490722017-09-04 15:34:32 -0300888 assert(rc == 0);
889 }
890
Fabio Utzigba829042018-09-18 08:29:34 -0300891#ifdef MCUBOOT_ENC_IMAGES
Fabio Utzig10ee6482019-08-01 12:04:52 -0300892 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
Fabio Utzig2fc80df2018-12-14 06:47:38 -0200893 if (IS_ENCRYPTED(hdr)) {
Fabio Utzig10ee6482019-08-01 12:04:52 -0300894 fap = BOOT_IMG_AREA(state, BOOT_SECONDARY_SLOT);
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300895 rc = boot_enc_load(BOOT_CURR_ENC(state), image_index, hdr, fap, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300896 assert(rc >= 0);
897
898 if (rc == 0) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300899 rc = boot_enc_set_key(BOOT_CURR_ENC(state), 1, bs->enckey[1]);
Fabio Utzigba829042018-09-18 08:29:34 -0300900 assert(rc == 0);
901 } else {
902 rc = 0;
903 }
904 } else {
905 memset(bs->enckey[1], 0xff, BOOT_ENC_KEY_SIZE);
906 }
907#endif
908
Fabio Utzig46490722017-09-04 15:34:32 -0300909 if (size > copy_size) {
910 copy_size = size;
911 }
912
913 bs->swap_size = copy_size;
914 } else {
915 /*
916 * If a swap was under way, the swap_size should already be present
917 * in the trailer...
918 */
Fabio Utzigb0f04732019-07-31 09:49:19 -0300919 rc = boot_read_swap_size(image_index, &bs->swap_size);
Fabio Utzig46490722017-09-04 15:34:32 -0300920 assert(rc == 0);
921
922 copy_size = bs->swap_size;
Fabio Utzigba829042018-09-18 08:29:34 -0300923
924#ifdef MCUBOOT_ENC_IMAGES
925 for (slot = 0; slot <= 1; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -0300926 rc = boot_read_enc_key(image_index, slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -0300927 assert(rc == 0);
928
929 for (i = 0; i < BOOT_ENC_KEY_SIZE; i++) {
Fabio Utzig1c7d9592018-12-03 10:35:56 -0200930 if (bs->enckey[slot][i] != 0xff) {
Fabio Utzigba829042018-09-18 08:29:34 -0300931 break;
932 }
933 }
934
935 if (i != BOOT_ENC_KEY_SIZE) {
Fabio Utzig1e4284b2019-08-23 11:55:27 -0300936 boot_enc_set_key(BOOT_CURR_ENC(state), slot, bs->enckey[slot]);
Fabio Utzigba829042018-09-18 08:29:34 -0300937 }
938 }
939#endif
Fabio Utzig2473ac02017-05-02 12:45:02 -0300940 }
941
Fabio Utzig12d59162019-11-28 10:01:59 -0300942 swap_run(state, bs, copy_size);
Christopher Collins92ea77f2016-12-12 15:59:26 -0800943
David Vincze2d736ad2019-02-18 11:50:22 +0100944#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig12d59162019-11-28 10:01:59 -0300945 extern int boot_status_fails;
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200946 if (boot_status_fails > 0) {
Christopher Collins2c88e692019-05-22 15:10:14 -0700947 BOOT_LOG_WRN("%d status write fails performing the swap",
948 boot_status_fails);
Fabio Utziga0e1cce2017-11-23 20:04:01 -0200949 }
950#endif
951
Christopher Collins92ea77f2016-12-12 15:59:26 -0800952 return 0;
953}
David Brown17609d82017-05-05 09:41:34 -0600954#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -0800955
David Vinczee32483f2019-06-13 10:46:24 +0200956#if (BOOT_IMAGE_NUMBER > 1)
957/**
Fabio Utzig019a81a2019-08-27 10:33:39 -0300958 * Check if the version of the image is not older than required.
959 *
960 * @param req Required minimal image version.
961 * @param ver Version of the image to be checked.
962 *
963 * @return 0 if the version is sufficient, nonzero otherwise.
964 */
965static int
966boot_is_version_sufficient(struct image_version *req,
967 struct image_version *ver)
968{
969 if (ver->iv_major > req->iv_major) {
970 return 0;
971 }
972 if (ver->iv_major < req->iv_major) {
973 return BOOT_EBADVERSION;
974 }
975 /* The major version numbers are equal. */
976 if (ver->iv_minor > req->iv_minor) {
977 return 0;
978 }
979 if (ver->iv_minor < req->iv_minor) {
980 return BOOT_EBADVERSION;
981 }
982 /* The minor version numbers are equal. */
983 if (ver->iv_revision < req->iv_revision) {
984 return BOOT_EBADVERSION;
985 }
986
987 return 0;
988}
989
990/**
David Vinczee32483f2019-06-13 10:46:24 +0200991 * Check the image dependency whether it is satisfied and modify
992 * the swap type if necessary.
993 *
994 * @param dep Image dependency which has to be verified.
995 *
996 * @return 0 on success; nonzero on failure.
997 */
998static int
Fabio Utzig298913b2019-08-28 11:22:45 -0300999boot_verify_slot_dependency(struct boot_loader_state *state,
1000 struct image_dependency *dep)
David Vinczee32483f2019-06-13 10:46:24 +02001001{
1002 struct image_version *dep_version;
1003 size_t dep_slot;
1004 int rc;
David Browne6ab34c2019-09-03 12:24:21 -06001005 uint8_t swap_type;
David Vinczee32483f2019-06-13 10:46:24 +02001006
1007 /* Determine the source of the image which is the subject of
1008 * the dependency and get it's version. */
David Browne6ab34c2019-09-03 12:24:21 -06001009 swap_type = state->swap_type[dep->image_id];
Fabio Utzigb1adb1e2019-09-11 11:42:53 -03001010 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczee32483f2019-06-13 10:46:24 +02001011 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001012 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczee32483f2019-06-13 10:46:24 +02001013
1014 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1015 if (rc != 0) {
1016 /* Dependency not satisfied.
1017 * Modify the swap type to decrease the version number of the image
1018 * (which will be located in the primary slot after the boot process),
1019 * consequently the number of unsatisfied dependencies will be
1020 * decreased or remain the same.
1021 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001022 switch (BOOT_SWAP_TYPE(state)) {
David Vinczee32483f2019-06-13 10:46:24 +02001023 case BOOT_SWAP_TYPE_TEST:
1024 case BOOT_SWAP_TYPE_PERM:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001025 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczee32483f2019-06-13 10:46:24 +02001026 break;
1027 case BOOT_SWAP_TYPE_NONE:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001028 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczee32483f2019-06-13 10:46:24 +02001029 break;
1030 default:
1031 break;
1032 }
1033 }
1034
1035 return rc;
1036}
1037
1038/**
1039 * Read all dependency TLVs of an image from the flash and verify
1040 * one after another to see if they are all satisfied.
1041 *
1042 * @param slot Image slot number.
1043 *
1044 * @return 0 on success; nonzero on failure.
1045 */
1046static int
Fabio Utzig298913b2019-08-28 11:22:45 -03001047boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczee32483f2019-06-13 10:46:24 +02001048{
1049 const struct flash_area *fap;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001050 struct image_tlv_iter it;
David Vinczee32483f2019-06-13 10:46:24 +02001051 struct image_dependency dep;
1052 uint32_t off;
Fabio Utzig61fd8882019-09-14 20:00:20 -03001053 uint16_t len;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001054 int area_id;
David Vinczee32483f2019-06-13 10:46:24 +02001055 int rc;
1056
Fabio Utzig10ee6482019-08-01 12:04:52 -03001057 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001058 rc = flash_area_open(area_id, &fap);
David Vinczee32483f2019-06-13 10:46:24 +02001059 if (rc != 0) {
1060 rc = BOOT_EFLASH;
1061 goto done;
1062 }
1063
Fabio Utzig61fd8882019-09-14 20:00:20 -03001064 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1065 IMAGE_TLV_DEPENDENCY, true);
David Vinczee32483f2019-06-13 10:46:24 +02001066 if (rc != 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001067 goto done;
1068 }
1069
Fabio Utzig61fd8882019-09-14 20:00:20 -03001070 while (true) {
1071 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1072 if (rc < 0) {
1073 return -1;
1074 } else if (rc > 0) {
1075 rc = 0;
David Vinczee32483f2019-06-13 10:46:24 +02001076 break;
1077 }
Fabio Utzig61fd8882019-09-14 20:00:20 -03001078
1079 if (len != sizeof(dep)) {
1080 rc = BOOT_EBADIMAGE;
1081 goto done;
1082 }
1083
1084 rc = flash_area_read(fap, off, &dep, len);
1085 if (rc != 0) {
1086 rc = BOOT_EFLASH;
1087 goto done;
1088 }
1089
1090 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1091 rc = BOOT_EBADARGS;
1092 goto done;
1093 }
1094
1095 /* Verify dependency and modify the swap type if not satisfied. */
1096 rc = boot_verify_slot_dependency(state, &dep);
1097 if (rc != 0) {
1098 /* Dependency not satisfied. */
1099 goto done;
1100 }
David Vinczee32483f2019-06-13 10:46:24 +02001101 }
1102
1103done:
1104 flash_area_close(fap);
1105 return rc;
1106}
1107
1108/**
David Vinczee32483f2019-06-13 10:46:24 +02001109 * Iterate over all the images and verify whether the image dependencies in the
1110 * TLV area are all satisfied and update the related swap type if necessary.
1111 */
Fabio Utzig298913b2019-08-28 11:22:45 -03001112static int
1113boot_verify_dependencies(struct boot_loader_state *state)
David Vinczee32483f2019-06-13 10:46:24 +02001114{
David Vinczee32483f2019-06-13 10:46:24 +02001115 int rc;
Fabio Utzig298913b2019-08-28 11:22:45 -03001116 uint8_t slot;
David Vinczee32483f2019-06-13 10:46:24 +02001117
Fabio Utzig10ee6482019-08-01 12:04:52 -03001118 BOOT_CURR_IMG(state) = 0;
1119 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001120 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1121 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1122 slot = BOOT_SECONDARY_SLOT;
1123 } else {
1124 slot = BOOT_PRIMARY_SLOT;
1125 }
1126
1127 rc = boot_verify_slot_dependencies(state, slot);
Fabio Utzigabec0732019-07-31 08:40:22 -03001128 if (rc == 0) {
David Vinczee32483f2019-06-13 10:46:24 +02001129 /* All dependencies've been satisfied, continue with next image. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001130 BOOT_CURR_IMG(state)++;
David Vinczee32483f2019-06-13 10:46:24 +02001131 } else if (rc == BOOT_EBADVERSION) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001132 /* Cannot upgrade due to non-met dependencies, so disable all
1133 * image upgrades.
1134 */
1135 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1136 BOOT_CURR_IMG(state) = idx;
1137 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1138 }
1139 break;
David Vinczee32483f2019-06-13 10:46:24 +02001140 } else {
1141 /* Other error happened, images are inconsistent */
Fabio Utzig298913b2019-08-28 11:22:45 -03001142 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001143 }
1144 }
Fabio Utzig298913b2019-08-28 11:22:45 -03001145 return rc;
David Vinczee32483f2019-06-13 10:46:24 +02001146}
1147#endif /* (BOOT_IMAGE_NUMBER > 1) */
1148
Christopher Collins92ea77f2016-12-12 15:59:26 -08001149/**
David Vinczeba3bd602019-06-17 16:01:43 +02001150 * Performs a clean (not aborted) image update.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001151 *
David Vinczeba3bd602019-06-17 16:01:43 +02001152 * @param bs The current boot status.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001153 *
1154 * @return 0 on success; nonzero on failure.
1155 */
1156static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001157boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001158{
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001159 int rc;
Fabio Utzig10ee6482019-08-01 12:04:52 -03001160#ifndef MCUBOOT_OVERWRITE_ONLY
1161 uint8_t swap_type;
1162#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001163
David Vinczeba3bd602019-06-17 16:01:43 +02001164 /* At this point there are no aborted swaps. */
1165#if defined(MCUBOOT_OVERWRITE_ONLY)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001166 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001167#elif defined(MCUBOOT_BOOTSTRAP)
1168 /* Check if the image update was triggered by a bad image in the
1169 * primary slot (the validity of the image in the secondary slot had
1170 * already been checked).
1171 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001172 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1173 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1174 rc = boot_copy_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001175 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001176 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001177 }
1178#else
Fabio Utzig10ee6482019-08-01 12:04:52 -03001179 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001180#endif
Fabio Utzig7ebb7c22017-04-26 10:59:31 -03001181 assert(rc == 0);
David Vinczeba3bd602019-06-17 16:01:43 +02001182
1183#ifndef MCUBOOT_OVERWRITE_ONLY
1184 /* The following state needs image_ok be explicitly set after the
1185 * swap was finished to avoid a new revert.
1186 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001187 swap_type = BOOT_SWAP_TYPE(state);
1188 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1189 swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001190 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001191 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001192 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001193 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001194 }
1195
Fabio Utzige575b0b2019-09-11 12:34:23 -03001196 if (BOOT_IS_UPGRADE(swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001197 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001198 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001199 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Christopher Collinsa1c12042019-05-23 14:00:28 -07001200 }
David Vinczeba3bd602019-06-17 16:01:43 +02001201 }
1202#endif /* !MCUBOOT_OVERWRITE_ONLY */
Fabio Utzig338a19f2018-12-03 08:37:08 -02001203
David Vinczeba3bd602019-06-17 16:01:43 +02001204 return rc;
1205}
1206
1207/**
1208 * Completes a previously aborted image swap.
1209 *
1210 * @param bs The current boot status.
1211 *
1212 * @return 0 on success; nonzero on failure.
1213 */
1214#if !defined(MCUBOOT_OVERWRITE_ONLY)
1215static int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001216boot_complete_partial_swap(struct boot_loader_state *state,
1217 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001218{
1219 int rc;
1220
1221 /* Determine the type of swap operation being resumed from the
1222 * `swap-type` trailer field.
1223 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001224 rc = boot_swap_image(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001225 assert(rc == 0);
1226
Fabio Utzig10ee6482019-08-01 12:04:52 -03001227 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001228
1229 /* The following states need image_ok be explicitly set after the
1230 * swap was finished to avoid a new revert.
1231 */
1232 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1233 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001234 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001235 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001236 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001237 }
1238 }
1239
Fabio Utzige575b0b2019-09-11 12:34:23 -03001240 if (BOOT_IS_UPGRADE(bs->swap_type)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001241 rc = swap_set_copy_done(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001242 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001243 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001244 }
1245 }
1246
Fabio Utzig10ee6482019-08-01 12:04:52 -03001247 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001248 BOOT_LOG_ERR("panic!");
1249 assert(0);
1250
1251 /* Loop forever... */
1252 while (1) {}
1253 }
1254
1255 return rc;
1256}
1257#endif /* !MCUBOOT_OVERWRITE_ONLY */
1258
1259#if (BOOT_IMAGE_NUMBER > 1)
1260/**
1261 * Review the validity of previously determined swap types of other images.
1262 *
1263 * @param aborted_swap The current image upgrade is a
1264 * partial/aborted swap.
1265 */
1266static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001267boot_review_image_swap_types(struct boot_loader_state *state,
1268 bool aborted_swap)
David Vinczeba3bd602019-06-17 16:01:43 +02001269{
1270 /* In that case if we rebooted in the middle of an image upgrade process, we
1271 * must review the validity of swap types, that were previously determined
1272 * for other images. The image_ok flag had not been set before the reboot
1273 * for any of the updated images (only the copy_done flag) and thus falsely
1274 * the REVERT swap type has been determined for the previous images that had
1275 * been updated before the reboot.
1276 *
1277 * There are two separate scenarios that we have to deal with:
1278 *
1279 * 1. The reboot has happened during swapping an image:
1280 * The current image upgrade has been determined as a
1281 * partial/aborted swap.
1282 * 2. The reboot has happened between two separate image upgrades:
1283 * In this scenario we must check the swap type of the current image.
1284 * In those cases if it is NONE or REVERT we cannot certainly determine
1285 * the fact of a reboot. In a consistent state images must move in the
1286 * same direction or stay in place, e.g. in practice REVERT and TEST
1287 * swap types cannot be present at the same time. If the swap type of
1288 * the current image is either TEST, PERM or FAIL we must review the
1289 * already determined swap types of other images and set each false
1290 * REVERT swap types to NONE (these images had been successfully
1291 * updated before the system rebooted between two separate image
1292 * upgrades).
1293 */
1294
Fabio Utzig10ee6482019-08-01 12:04:52 -03001295 if (BOOT_CURR_IMG(state) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001296 /* Nothing to do */
1297 return;
1298 }
1299
1300 if (!aborted_swap) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001301 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
1302 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001303 /* Nothing to do */
1304 return;
1305 }
1306 }
1307
Fabio Utzig10ee6482019-08-01 12:04:52 -03001308 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
1309 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
1310 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001311 }
1312 }
1313}
1314#endif
1315
1316/**
1317 * Prepare image to be updated if required.
1318 *
1319 * Prepare image to be updated if required with completing an image swap
1320 * operation if one was aborted and/or determining the type of the
1321 * swap operation. In case of any error set the swap type to NONE.
1322 *
Fabio Utzig10ee6482019-08-01 12:04:52 -03001323 * @param state TODO
David Vinczeba3bd602019-06-17 16:01:43 +02001324 * @param bs Pointer where the read and possibly updated
1325 * boot status can be written to.
1326 */
1327static void
Fabio Utzig10ee6482019-08-01 12:04:52 -03001328boot_prepare_image_for_update(struct boot_loader_state *state,
1329 struct boot_status *bs)
David Vinczeba3bd602019-06-17 16:01:43 +02001330{
1331 int rc;
1332
1333 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001334 rc = boot_read_sectors(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001335 if (rc != 0) {
1336 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
1337 " - too small?", BOOT_MAX_IMG_SECTORS);
1338 /* Unable to determine sector layout, continue with next image
1339 * if there is one.
1340 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001341 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001342 return;
1343 }
1344
1345 /* Attempt to read an image header from each slot. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001346 rc = boot_read_image_headers(state, false, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001347 if (rc != 0) {
1348 /* Continue with next image if there is one. */
Fabio Utzigb0f04732019-07-31 09:49:19 -03001349 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001350 BOOT_CURR_IMG(state));
1351 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001352 return;
1353 }
1354
1355 /* If the current image's slots aren't compatible, no swap is possible.
1356 * Just boot into primary slot.
1357 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001358 if (boot_slots_compatible(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001359 boot_status_reset(bs);
1360
1361#ifndef MCUBOOT_OVERWRITE_ONLY
1362 rc = swap_read_status(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001363 if (rc != 0) {
1364 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
Fabio Utzig10ee6482019-08-01 12:04:52 -03001365 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001366 /* Continue with next image if there is one. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001367 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001368 return;
1369 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001370#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001371
Fabio Utzig74aef312019-11-28 11:05:34 -03001372#ifdef MCUBOOT_SWAP_USING_MOVE
1373 /*
1374 * Must re-read image headers because the boot status might
1375 * have been updated in the previous function call.
1376 */
1377 rc = boot_read_image_headers(state, !boot_status_is_reset(bs), bs);
1378 if (rc != 0) {
1379 /* Continue with next image if there is one. */
1380 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
1381 BOOT_CURR_IMG(state));
1382 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1383 return;
1384 }
1385#endif
1386
David Vinczeba3bd602019-06-17 16:01:43 +02001387 /* Determine if we rebooted in the middle of an image swap
1388 * operation. If a partial swap was detected, complete it.
1389 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001390 if (!boot_status_is_reset(bs)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001391
1392#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001393 boot_review_image_swap_types(state, true);
David Vinczeba3bd602019-06-17 16:01:43 +02001394#endif
1395
1396#ifdef MCUBOOT_OVERWRITE_ONLY
1397 /* Should never arrive here, overwrite-only mode has
1398 * no swap state.
1399 */
1400 assert(0);
1401#else
1402 /* Determine the type of swap operation being resumed from the
1403 * `swap-type` trailer field.
1404 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001405 rc = boot_complete_partial_swap(state, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001406 assert(rc == 0);
1407#endif
1408 /* Attempt to read an image header from each slot. Ensure that
1409 * image headers in slots are aligned with headers in boot_data.
1410 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001411 rc = boot_read_image_headers(state, false, bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001412 assert(rc == 0);
1413
1414 /* Swap has finished set to NONE */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001415 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczeba3bd602019-06-17 16:01:43 +02001416 } else {
1417 /* There was no partial swap, determine swap type. */
1418 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001419 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
1420 } else if (boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) != 0) {
1421 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vinczeba3bd602019-06-17 16:01:43 +02001422 } else {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001423 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vinczeba3bd602019-06-17 16:01:43 +02001424 }
1425
1426#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001427 boot_review_image_swap_types(state, false);
David Vinczeba3bd602019-06-17 16:01:43 +02001428#endif
1429
1430#ifdef MCUBOOT_BOOTSTRAP
Fabio Utzig10ee6482019-08-01 12:04:52 -03001431 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001432 /* Header checks are done first because they are
1433 * inexpensive. Since overwrite-only copies starting from
1434 * offset 0, if interrupted, it might leave a valid header
1435 * magic, so also run validation on the primary slot to be
1436 * sure it's not OK.
1437 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001438 if (boot_check_header_erased(state, BOOT_PRIMARY_SLOT) == 0 ||
1439 boot_validate_slot(state, BOOT_PRIMARY_SLOT, bs) != 0) {
1440 if (boot_img_hdr(state,
1441 BOOT_SECONDARY_SLOT)->ih_magic == IMAGE_MAGIC &&
1442 boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs) == 0) {
David Vinczeba3bd602019-06-17 16:01:43 +02001443 /* Set swap type to REVERT to overwrite the primary
1444 * slot with the image contained in secondary slot
1445 * and to trigger the explicit setting of the
1446 * image_ok flag.
1447 */
Fabio Utzig59b63e52019-09-10 12:22:35 -03001448 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczeba3bd602019-06-17 16:01:43 +02001449 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001450 }
1451 }
Fabio Utzig338a19f2018-12-03 08:37:08 -02001452#endif
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001453 }
David Vinczeba3bd602019-06-17 16:01:43 +02001454 } else {
1455 /* In that case if slots are not compatible. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001456 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001457 }
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001458}
1459
Christopher Collins92ea77f2016-12-12 15:59:26 -08001460int
Fabio Utzig10ee6482019-08-01 12:04:52 -03001461context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Christopher Collins92ea77f2016-12-12 15:59:26 -08001462{
Marti Bolivar84898652017-06-13 17:20:22 -04001463 size_t slot;
David Vinczeba3bd602019-06-17 16:01:43 +02001464 struct boot_status bs;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001465 int rc;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001466 int fa_id;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001467 int image_index;
Fabio Utzig298913b2019-08-28 11:22:45 -03001468 bool has_upgrade;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001469
1470 /* The array of slot sectors are defined here (as opposed to file scope) so
1471 * that they don't get allocated for non-boot-loader apps. This is
1472 * necessary because the gcc option "-fdata-sections" doesn't seem to have
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001473 * any effect in older gcc versions (e.g., 4.8.4).
Christopher Collins92ea77f2016-12-12 15:59:26 -08001474 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001475 TARGET_STATIC boot_sector_t primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
1476 TARGET_STATIC boot_sector_t secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001477#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001478 TARGET_STATIC boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Fabio Utzig12d59162019-11-28 10:01:59 -03001479#endif
Christopher Collins92ea77f2016-12-12 15:59:26 -08001480
Fabio Utzig10ee6482019-08-01 12:04:52 -03001481 memset(state, 0, sizeof(struct boot_loader_state));
Fabio Utzig298913b2019-08-28 11:22:45 -03001482 has_upgrade = false;
1483
1484#if (BOOT_IMAGE_NUMBER == 1)
1485 (void)has_upgrade;
1486#endif
Fabio Utzigba829042018-09-18 08:29:34 -03001487
David Vinczeba3bd602019-06-17 16:01:43 +02001488 /* Iterate over all the images. By the end of the loop the swap type has
1489 * to be determined for each image and all aborted swaps have to be
1490 * completed.
Christopher Collins0ff3c6c2016-12-21 12:04:17 -08001491 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001492 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001493
David Vinczeba3bd602019-06-17 16:01:43 +02001494#if defined(MCUBOOT_ENC_IMAGES) && (BOOT_IMAGE_NUMBER > 1)
1495 /* The keys used for encryption may no longer be valid (could belong to
1496 * another images). Therefore, mark them as invalid to force their reload
1497 * by boot_enc_load().
Fabio Utzigdb5bd3c2017-07-13 22:20:22 -03001498 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001499 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Brown554c52e2017-06-30 16:01:07 -06001500#endif
1501
Fabio Utzig10ee6482019-08-01 12:04:52 -03001502 image_index = BOOT_CURR_IMG(state);
Fabio Utzigb0f04732019-07-31 09:49:19 -03001503
Fabio Utzig10ee6482019-08-01 12:04:52 -03001504 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001505 primary_slot_sectors[image_index];
Fabio Utzig10ee6482019-08-01 12:04:52 -03001506 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
Fabio Utzigb0f04732019-07-31 09:49:19 -03001507 secondary_slot_sectors[image_index];
Fabio Utzig12d59162019-11-28 10:01:59 -03001508#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001509 state->scratch.sectors = scratch_sectors;
Fabio Utzig12d59162019-11-28 10:01:59 -03001510#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001511
1512 /* Open primary and secondary image areas for the duration
1513 * of this call.
1514 */
1515 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzigb0f04732019-07-31 09:49:19 -03001516 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
Fabio Utzig10ee6482019-08-01 12:04:52 -03001517 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001518 assert(rc == 0);
1519 }
Fabio Utzig12d59162019-11-28 10:01:59 -03001520#if MCUBOOT_SWAP_USING_SCRATCH
David Vinczeba3bd602019-06-17 16:01:43 +02001521 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
Fabio Utzig10ee6482019-08-01 12:04:52 -03001522 &BOOT_SCRATCH_AREA(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001523 assert(rc == 0);
Fabio Utzig12d59162019-11-28 10:01:59 -03001524#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001525
1526 /* Determine swap type and complete swap if it has been aborted. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001527 boot_prepare_image_for_update(state, &bs);
Fabio Utzig298913b2019-08-28 11:22:45 -03001528
Fabio Utzige575b0b2019-09-11 12:34:23 -03001529 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
Fabio Utzig298913b2019-08-28 11:22:45 -03001530 has_upgrade = true;
1531 }
David Vinczeba3bd602019-06-17 16:01:43 +02001532 }
1533
David Vinczee32483f2019-06-13 10:46:24 +02001534#if (BOOT_IMAGE_NUMBER > 1)
Fabio Utzig298913b2019-08-28 11:22:45 -03001535 if (has_upgrade) {
1536 /* Iterate over all the images and verify whether the image dependencies
1537 * are all satisfied and update swap type if necessary.
1538 */
1539 rc = boot_verify_dependencies(state);
1540 if (rc == BOOT_EBADVERSION) {
1541 /*
1542 * It was impossible to upgrade because the expected dependency version
1543 * was not available. Here we already changed the swap_type so that
1544 * instead of asserting the bootloader, we continue and no upgrade is
1545 * performed.
1546 */
1547 rc = 0;
1548 }
1549 }
David Vinczee32483f2019-06-13 10:46:24 +02001550#endif
1551
David Vinczeba3bd602019-06-17 16:01:43 +02001552 /* Iterate over all the images. At this point there are no aborted swaps
1553 * and the swap types are determined for each image. By the end of the loop
1554 * all required update operations will have been finished.
1555 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001556 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001557
1558#if (BOOT_IMAGE_NUMBER > 1)
1559#ifdef MCUBOOT_ENC_IMAGES
1560 /* The keys used for encryption may no longer be valid (could belong to
1561 * another images). Therefore, mark them as invalid to force their reload
1562 * by boot_enc_load().
1563 */
Fabio Utzig1e4284b2019-08-23 11:55:27 -03001564 boot_enc_zeroize(BOOT_CURR_ENC(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001565#endif /* MCUBOOT_ENC_IMAGES */
1566
1567 /* Indicate that swap is not aborted */
Fabio Utzig12d59162019-11-28 10:01:59 -03001568 boot_status_reset(&bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001569#endif /* (BOOT_IMAGE_NUMBER > 1) */
1570
1571 /* Set the previously determined swap type */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001572 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vinczeba3bd602019-06-17 16:01:43 +02001573
Fabio Utzig10ee6482019-08-01 12:04:52 -03001574 switch (BOOT_SWAP_TYPE(state)) {
David Vinczeba3bd602019-06-17 16:01:43 +02001575 case BOOT_SWAP_TYPE_NONE:
1576 break;
1577
1578 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1579 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1580 case BOOT_SWAP_TYPE_REVERT:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001581 rc = boot_perform_update(state, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001582 assert(rc == 0);
1583 break;
1584
1585 case BOOT_SWAP_TYPE_FAIL:
1586 /* The image in secondary slot was invalid and is now erased. Ensure
1587 * we don't try to boot into it again on the next reboot. Do this by
1588 * pretending we just reverted back to primary slot.
1589 */
1590#ifndef MCUBOOT_OVERWRITE_ONLY
1591 /* image_ok needs to be explicitly set to avoid a new revert. */
Fabio Utzig12d59162019-11-28 10:01:59 -03001592 rc = swap_set_image_ok(BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001593 if (rc != 0) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001594 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001595 }
1596#endif /* !MCUBOOT_OVERWRITE_ONLY */
1597 break;
1598
1599 default:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001600 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vinczeba3bd602019-06-17 16:01:43 +02001601 }
1602
Fabio Utzig10ee6482019-08-01 12:04:52 -03001603 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001604 BOOT_LOG_ERR("panic!");
1605 assert(0);
1606
1607 /* Loop forever... */
1608 while (1) {}
1609 }
1610 }
1611
1612 /* Iterate over all the images. At this point all required update operations
1613 * have finished. By the end of the loop each image in the primary slot will
1614 * have been re-validated.
1615 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001616 IMAGES_ITER(BOOT_CURR_IMG(state)) {
1617 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vinczeba3bd602019-06-17 16:01:43 +02001618 /* Attempt to read an image header from each slot. Ensure that image
1619 * headers in slots are aligned with headers in boot_data.
1620 */
Fabio Utzig12d59162019-11-28 10:01:59 -03001621 rc = boot_read_image_headers(state, false, &bs);
David Vinczeba3bd602019-06-17 16:01:43 +02001622 if (rc != 0) {
1623 goto out;
1624 }
1625 /* Since headers were reloaded, it can be assumed we just performed
1626 * a swap or overwrite. Now the header info that should be used to
1627 * provide the data for the bootstrap, which previously was at
1628 * secondary slot, was updated to primary slot.
1629 */
1630 }
1631
1632#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
Fabio Utzig10ee6482019-08-01 12:04:52 -03001633 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vinczeba3bd602019-06-17 16:01:43 +02001634 if (rc != 0) {
1635 rc = BOOT_EBADIMAGE;
1636 goto out;
1637 }
1638#else
1639 /* Even if we're not re-validating the primary slot, we could be booting
1640 * onto an empty flash chip. At least do a basic sanity check that
1641 * the magic number on the image is OK.
1642 */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001643 if (BOOT_IMG(state, BOOT_PRIMARY_SLOT).hdr.ih_magic != IMAGE_MAGIC) {
David Vinczeba3bd602019-06-17 16:01:43 +02001644 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
Fabio Utzig10ee6482019-08-01 12:04:52 -03001645 &boot_img_hdr(state,BOOT_PRIMARY_SLOT)->ih_magic,
1646 BOOT_CURR_IMG(state));
David Vinczeba3bd602019-06-17 16:01:43 +02001647 rc = BOOT_EBADIMAGE;
1648 goto out;
1649 }
1650#endif
1651 }
1652
Fabio Utzigb0f04732019-07-31 09:49:19 -03001653#if (BOOT_IMAGE_NUMBER > 1)
David Vinczeba3bd602019-06-17 16:01:43 +02001654 /* Always boot from the primary slot of Image 0. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001655 BOOT_CURR_IMG(state) = 0;
Fabio Utzigb0f04732019-07-31 09:49:19 -03001656#endif
Fabio Utzig298913b2019-08-28 11:22:45 -03001657
Fabio Utzig10ee6482019-08-01 12:04:52 -03001658 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
1659 rsp->br_image_off = boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
1660 rsp->br_hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001661
Fabio Utzig298913b2019-08-28 11:22:45 -03001662out:
Fabio Utzig10ee6482019-08-01 12:04:52 -03001663 IMAGES_ITER(BOOT_CURR_IMG(state)) {
Fabio Utzig12d59162019-11-28 10:01:59 -03001664#if MCUBOOT_SWAP_USING_SCRATCH
Fabio Utzig10ee6482019-08-01 12:04:52 -03001665 flash_area_close(BOOT_SCRATCH_AREA(state));
Fabio Utzig12d59162019-11-28 10:01:59 -03001666#endif
David Vinczeba3bd602019-06-17 16:01:43 +02001667 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
Fabio Utzig10ee6482019-08-01 12:04:52 -03001668 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
David Vinczeba3bd602019-06-17 16:01:43 +02001669 }
Marti Bolivarc0b47912017-06-13 17:18:09 -04001670 }
1671 return rc;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001672}
1673
Fabio Utzig10ee6482019-08-01 12:04:52 -03001674/**
1675 * Prepares the booting process. This function moves images around in flash as
1676 * appropriate, and tells you what address to boot from.
1677 *
1678 * @param rsp On success, indicates how booting should occur.
1679 *
1680 * @return 0 on success; nonzero on failure.
1681 */
1682int
1683boot_go(struct boot_rsp *rsp)
1684{
1685 return context_boot_go(&boot_data, rsp);
1686}
1687
Christopher Collins92ea77f2016-12-12 15:59:26 -08001688int
1689split_go(int loader_slot, int split_slot, void **entry)
1690{
Marti Bolivarc50926f2017-06-14 09:35:40 -04001691 boot_sector_t *sectors;
Christopher Collins034a6202017-01-11 12:19:37 -08001692 uintptr_t entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001693 int loader_flash_id;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001694 int split_flash_id;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001695 int rc;
1696
Christopher Collins92ea77f2016-12-12 15:59:26 -08001697 sectors = malloc(BOOT_MAX_IMG_SECTORS * 2 * sizeof *sectors);
1698 if (sectors == NULL) {
Marti Bolivarc0b47912017-06-13 17:18:09 -04001699 return SPLIT_GO_ERR;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001700 }
David Vinczeba3bd602019-06-17 16:01:43 +02001701 BOOT_IMG(&boot_data, loader_slot).sectors = sectors + 0;
1702 BOOT_IMG(&boot_data, split_slot).sectors = sectors + BOOT_MAX_IMG_SECTORS;
Marti Bolivarc0b47912017-06-13 17:18:09 -04001703
1704 loader_flash_id = flash_area_id_from_image_slot(loader_slot);
1705 rc = flash_area_open(loader_flash_id,
Alvaro Prieto63a2bdb2019-07-04 12:18:49 -07001706 &BOOT_IMG_AREA(&boot_data, loader_slot));
Marti Bolivarc0b47912017-06-13 17:18:09 -04001707 assert(rc == 0);
1708 split_flash_id = flash_area_id_from_image_slot(split_slot);
1709 rc = flash_area_open(split_flash_id,
1710 &BOOT_IMG_AREA(&boot_data, split_slot));
1711 assert(rc == 0);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001712
1713 /* Determine the sector layout of the image slots and scratch area. */
Fabio Utzig10ee6482019-08-01 12:04:52 -03001714 rc = boot_read_sectors(&boot_data);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001715 if (rc != 0) {
1716 rc = SPLIT_GO_ERR;
1717 goto done;
1718 }
1719
Fabio Utzig12d59162019-11-28 10:01:59 -03001720 rc = boot_read_image_headers(&boot_data, true, NULL);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001721 if (rc != 0) {
1722 goto done;
1723 }
1724
Christopher Collins92ea77f2016-12-12 15:59:26 -08001725 /* Don't check the bootable image flag because we could really call a
1726 * bootable or non-bootable image. Just validate that the image check
1727 * passes which is distinct from the normal check.
1728 */
Marti Bolivarf804f622017-06-12 15:41:48 -04001729 rc = split_image_check(boot_img_hdr(&boot_data, split_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001730 BOOT_IMG_AREA(&boot_data, split_slot),
Marti Bolivarf804f622017-06-12 15:41:48 -04001731 boot_img_hdr(&boot_data, loader_slot),
Marti Bolivarc0b47912017-06-13 17:18:09 -04001732 BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001733 if (rc != 0) {
1734 rc = SPLIT_GO_NON_MATCHING;
1735 goto done;
1736 }
1737
Marti Bolivarea088872017-06-12 17:10:49 -04001738 entry_val = boot_img_slot_off(&boot_data, split_slot) +
Marti Bolivarf804f622017-06-12 15:41:48 -04001739 boot_img_hdr(&boot_data, split_slot)->ih_hdr_size;
Christopher Collins034a6202017-01-11 12:19:37 -08001740 *entry = (void *) entry_val;
Christopher Collins92ea77f2016-12-12 15:59:26 -08001741 rc = SPLIT_GO_OK;
1742
1743done:
Marti Bolivarc0b47912017-06-13 17:18:09 -04001744 flash_area_close(BOOT_IMG_AREA(&boot_data, split_slot));
1745 flash_area_close(BOOT_IMG_AREA(&boot_data, loader_slot));
Christopher Collins92ea77f2016-12-12 15:59:26 -08001746 free(sectors);
Christopher Collins92ea77f2016-12-12 15:59:26 -08001747 return rc;
1748}