blob: c2b1b5fa561005d2b6cbb650c69d7569f1faf0d0 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
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
Tamas Ban581034a2017-12-19 19:54:37 +000020/*
Tamas Ban4fb8e9d2018-02-23 14:22:03 +000021 * Original code taken from mcuboot project at:
David Vincze39e78552018-10-10 17:10:01 +020022 * https://github.com/JuulLabs-OSS/mcuboot
David Vincze07706a42019-12-12 18:20:12 +010023 * Git SHA of the original version: 61fd888a7f4d741714553f36839dd49fb0065731
Tamas Ban5b647472019-01-05 08:59:30 +000024 * Modifications are Copyright (c) 2018-2019 Arm Limited.
Tamas Ban581034a2017-12-19 19:54:37 +000025 */
26
Tamas Banf70ef8c2017-12-19 15:35:09 +000027/**
28 * This file provides an interface to the boot loader. Functions defined in
29 * this file should only be called while the boot loader is running.
30 */
31
32#include <assert.h>
33#include <stddef.h>
34#include <stdbool.h>
35#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Tamas Banc3828852018-02-01 12:24:16 +000038#include "flash_map/flash_map.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000039#include "bootutil/bootutil.h"
40#include "bootutil/image.h"
41#include "bootutil_priv.h"
David Vincze73dfbc52019-10-11 13:54:58 +020042#include "bootutil/bootutil_log.h"
Tamas Bana9de4a62018-09-18 08:09:45 +010043#include "bl2/include/tfm_boot_status.h"
44#include "bl2/include/boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020045#include "security_cnt.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000046
Tamas Banf70ef8c2017-12-19 15:35:09 +000047static struct boot_loader_state boot_data;
David Vinczecea8b592019-10-29 16:09:51 +010048
49#if (BOOT_IMAGE_NUMBER > 1)
50#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
51#else
52#define IMAGES_ITER(x)
53#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000054
Oliver Swedef9982442018-08-24 18:37:44 +010055#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze39e78552018-10-10 17:10:01 +020056
David Vincze8bdfc2d2019-03-18 15:49:23 +010057#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020058static int boot_status_fails = 0;
59#define BOOT_STATUS_ASSERT(x) \
60 do { \
61 if (!(x)) { \
62 boot_status_fails++; \
63 } \
64 } while (0)
65#else
David Vincze401c7422019-06-21 20:44:05 +020066#define BOOT_STATUS_ASSERT(x) ASSERT(x)
David Vincze39e78552018-10-10 17:10:01 +020067#endif
68
Tamas Banf70ef8c2017-12-19 15:35:09 +000069struct boot_status_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010070 uint8_t bst_magic_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000071 uint8_t bst_magic_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +010072 uint8_t bst_copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000073 uint8_t bst_status_source;
74};
75
76/**
77 * This set of tables maps swap state contents to boot status location.
78 * When searching for a match, these tables must be iterated in order.
79 */
80static const struct boot_status_table boot_status_tables[] = {
81 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010082 /* | primary slot | scratch |
83 * ----------+--------------+--------------|
84 * magic | Good | Any |
85 * copy-done | Set | N/A |
86 * ----------+--------------+--------------'
87 * source: none |
88 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +000089 */
David Vincze8bdfc2d2019-03-18 15:49:23 +010090 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +020091 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +010092 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
93 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Tamas Banf70ef8c2017-12-19 15:35:09 +000094 },
95
96 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010097 /* | primary slot | scratch |
98 * ----------+--------------+--------------|
99 * magic | Good | Any |
100 * copy-done | Unset | N/A |
101 * ----------+--------------+--------------'
102 * source: primary slot |
103 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000104 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100105 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +0200106 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +0100107 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
108 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000109 },
110
111 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100112 /* | primary slot | scratch |
113 * ----------+--------------+--------------|
114 * magic | Any | Good |
115 * copy-done | Any | N/A |
116 * ----------+--------------+--------------'
117 * source: scratch |
118 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000119 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100120 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
121 .bst_magic_scratch = BOOT_MAGIC_GOOD,
122 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
123 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000124 },
125
126 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100127 /* | primary slot | scratch |
128 * ----------+--------------+--------------|
129 * magic | Unset | Any |
130 * copy-done | Unset | N/A |
131 * ----------+--------------+--------------|
132 * source: varies |
133 * ----------------------------------------+--------------------------+
Tamas Banf70ef8c2017-12-19 15:35:09 +0000134 * This represents one of two cases: |
135 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze8bdfc2d2019-03-18 15:49:23 +0100136 * o Mid-revert; status in the primary slot. |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000137 * -------------------------------------------------------------------'
138 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100139 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
140 .bst_magic_scratch = BOOT_MAGIC_ANY,
141 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
142 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000143 },
144};
145
146#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000147 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000148
149#define BOOT_LOG_SWAP_STATE(area, state) \
David Vincze401c7422019-06-21 20:44:05 +0200150 BOOT_LOG_INF("%s: magic=%5s, swap_type=0x%x, copy_done=0x%x, " \
151 "image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000152 (area), \
153 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
154 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
155 "bad"), \
David Vincze401c7422019-06-21 20:44:05 +0200156 (state)->swap_type, \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000157 (state)->copy_done, \
158 (state)->image_ok)
Oliver Swedef9982442018-08-24 18:37:44 +0100159#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000160
Tamas Ban056ed0b2019-09-16 12:48:32 +0100161/*
162 * \brief Verifies the image header: magic value, flags, integer overflow.
163 *
164 * \retval 0
165 * \retval BOOT_EBADIMAGE
166 */
167static int
168boot_verify_image_header(struct image_header *hdr)
169{
170 uint32_t image_end;
171
172 if (hdr->ih_magic != IMAGE_MAGIC) {
173 return BOOT_EBADIMAGE;
174 }
175
176 /* Check input parameters against integer overflow */
177 if (boot_add_uint32_overflow_check(hdr->ih_hdr_size, hdr->ih_img_size)) {
178 return BOOT_EBADIMAGE;
179 }
180
181 image_end = hdr->ih_hdr_size + hdr->ih_img_size;
182 if (boot_add_uint32_overflow_check(image_end, hdr->ih_protect_tlv_size)) {
183 return BOOT_EBADIMAGE;
184 }
185
186
187#if MCUBOOT_RAM_LOADING
188 if (!(hdr->ih_flags & IMAGE_F_RAM_LOAD)) {
189 return BOOT_EBADIMAGE;
190 }
191
192 /* Check input parameters against integer overflow */
193 if (boot_add_uint32_overflow_check(image_end, hdr->ih_load_addr)) {
194 return BOOT_EBADIMAGE;
195 }
196#endif
197
198 return 0;
199}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000200
201static int
David Vinczecea8b592019-10-29 16:09:51 +0100202boot_read_image_header(struct boot_loader_state *state, int slot,
203 struct image_header *out_hdr)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000204{
205 const struct flash_area *fap = NULL;
206 int area_id;
207 int rc;
208
David Vinczecea8b592019-10-29 16:09:51 +0100209#if (BOOT_IMAGE_NUMBER == 1)
210 (void)state;
211#endif
212
213 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000214 rc = flash_area_open(area_id, &fap);
215 if (rc != 0) {
216 rc = BOOT_EFLASH;
217 goto done;
218 }
219
220 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
221 if (rc != 0) {
222 rc = BOOT_EFLASH;
223 goto done;
224 }
225
Tamas Ban056ed0b2019-09-16 12:48:32 +0100226 rc = boot_verify_image_header(out_hdr);
David Vinczecea8b592019-10-29 16:09:51 +0100227 BOOT_IMG_HDR_IS_VALID(state, slot) = (rc == 0);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000228
229done:
230 flash_area_close(fap);
231 return rc;
232}
233
234static int
David Vinczecea8b592019-10-29 16:09:51 +0100235boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000236{
237 int rc;
238 int i;
239
240 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100241 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000242 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200243 /* If `require_all` is set, fail on any single fail, otherwise
244 * if at least the first slot's header was read successfully,
245 * then the boot loader can attempt a boot.
246 *
247 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000248 */
David Vincze39e78552018-10-10 17:10:01 +0200249 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000250 return 0;
251 } else {
252 return rc;
253 }
254 }
255 }
256
257 return 0;
258}
259
Raef Coles204c5b42019-09-05 09:18:47 +0100260static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +0100261boot_write_sz(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000262{
Raef Coles204c5b42019-09-05 09:18:47 +0100263 uint32_t elem_sz;
264 uint32_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000265
266 /* Figure out what size to write update status update as. The size depends
267 * on what the minimum write size is for scratch area, active image slot.
268 * We need to use the bigger of those 2 values.
269 */
David Vinczecea8b592019-10-29 16:09:51 +0100270 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
271 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000272 if (align > elem_sz) {
273 elem_sz = align;
274 }
275
276 return elem_sz;
277}
278
David Vinczecea8b592019-10-29 16:09:51 +0100279#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
280static int
281boot_initialize_area(struct boot_loader_state *state, int flash_area)
282{
283 int num_sectors = BOOT_MAX_IMG_SECTORS;
284 int rc;
285
286 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
287 rc = flash_area_to_sectors(flash_area, &num_sectors,
288 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
289 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
290
291 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
292 rc = flash_area_to_sectors(flash_area, &num_sectors,
293 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
294 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
295
296 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
297 rc = flash_area_to_sectors(flash_area, &num_sectors,
298 state->scratch.sectors);
299 state->scratch.num_sectors = (size_t)num_sectors;
300 } else {
301 return BOOT_EFLASH;
302 }
303
304 return rc;
305}
306#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
307static int
308boot_initialize_area(struct boot_loader_state *state, int flash_area)
309{
310 uint32_t num_sectors;
311 struct flash_sector *out_sectors;
312 size_t *out_num_sectors;
313 int rc;
314
315 num_sectors = BOOT_MAX_IMG_SECTORS;
316
317 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
318 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
319 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
320 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
321 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
322 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
323 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
324 out_sectors = state->scratch.sectors;
325 out_num_sectors = &state->scratch.num_sectors;
326 } else {
327 return BOOT_EFLASH;
328 }
329
330 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
331 if (rc != 0) {
332 return rc;
333 }
334 *out_num_sectors = num_sectors;
335 return 0;
336}
337#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
338
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000339/**
340 * Determines the sector layout of both image slots and the scratch area.
341 * This information is necessary for calculating the number of bytes to erase
342 * and copy during an image swap. The information collected during this
David Vinczecea8b592019-10-29 16:09:51 +0100343 * function is used to populate the state.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000344 */
345static int
David Vinczecea8b592019-10-29 16:09:51 +0100346boot_read_sectors(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000347{
David Vinczecea8b592019-10-29 16:09:51 +0100348 uint8_t image_index;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000349 int rc;
350
David Vinczecea8b592019-10-29 16:09:51 +0100351 image_index = BOOT_CURR_IMG(state);
352
353 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000354 if (rc != 0) {
355 return BOOT_EFLASH;
356 }
357
David Vinczecea8b592019-10-29 16:09:51 +0100358 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000359 if (rc != 0) {
360 return BOOT_EFLASH;
361 }
362
David Vinczecea8b592019-10-29 16:09:51 +0100363 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
David Vincze401c7422019-06-21 20:44:05 +0200364 if (rc != 0) {
365 return BOOT_EFLASH;
366 }
367
David Vinczecea8b592019-10-29 16:09:51 +0100368 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000369
370 return 0;
371}
372
David Vincze060968d2019-05-23 01:13:14 +0200373/**
374 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000375 */
376static int
David Vinczecea8b592019-10-29 16:09:51 +0100377boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
378 const struct flash_area *fap, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000379{
380 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
David Vinczecea8b592019-10-29 16:09:51 +0100381 uint8_t image_index;
382
383#if (BOOT_IMAGE_NUMBER == 1)
384 (void)state;
385#endif
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000386
David Vincze401c7422019-06-21 20:44:05 +0200387 (void)bs;
388
David Vinczecea8b592019-10-29 16:09:51 +0100389 image_index = BOOT_CURR_IMG(state);
390
391 if (bootutil_img_validate(image_index, hdr, fap, tmpbuf,
392 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000393 return BOOT_EBADIMAGE;
394 }
David Vinczecea8b592019-10-29 16:09:51 +0100395
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000396 return 0;
397}
398
David Vincze401c7422019-06-21 20:44:05 +0200399/*
400 * Check that a memory area consists of a given value.
401 */
402static inline bool
403boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200404{
405 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200406 uint8_t *p = (uint8_t *)data;
407 for (i = 0; i < len; i++) {
408 if (val != p[i]) {
409 return false;
David Vincze39e78552018-10-10 17:10:01 +0200410 }
411 }
David Vincze401c7422019-06-21 20:44:05 +0200412 return true;
David Vincze39e78552018-10-10 17:10:01 +0200413}
414
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000415static int
David Vinczecea8b592019-10-29 16:09:51 +0100416boot_check_header_erased(struct boot_loader_state *state, int slot)
David Vincze401c7422019-06-21 20:44:05 +0200417{
418 const struct flash_area *fap;
419 struct image_header *hdr;
420 uint8_t erased_val;
David Vinczecea8b592019-10-29 16:09:51 +0100421 int area_id;
David Vincze401c7422019-06-21 20:44:05 +0200422 int rc;
423
David Vinczecea8b592019-10-29 16:09:51 +0100424 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
425 rc = flash_area_open(area_id, &fap);
David Vincze401c7422019-06-21 20:44:05 +0200426 if (rc != 0) {
427 return -1;
428 }
429
430 erased_val = flash_area_erased_val(fap);
431 flash_area_close(fap);
432
David Vinczecea8b592019-10-29 16:09:51 +0100433 hdr = boot_img_hdr(state, slot);
David Vincze401c7422019-06-21 20:44:05 +0200434 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
435 sizeof(hdr->ih_magic))) {
436 return -1;
437 }
438
439 return 0;
440}
441
David Vinczecea8b592019-10-29 16:09:51 +0100442/*
443 * Check that there is a valid image in a slot
444 *
445 * @returns
446 * 0 if image was succesfully validated
447 * 1 if no bootloable image was found
448 * -1 on any errors
449 */
David Vincze401c7422019-06-21 20:44:05 +0200450static int
David Vinczecea8b592019-10-29 16:09:51 +0100451boot_validate_slot(struct boot_loader_state *state, int slot,
452 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000453{
454 const struct flash_area *fap;
455 struct image_header *hdr;
David Vinczecea8b592019-10-29 16:09:51 +0100456 int area_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000457 int rc;
458
David Vinczecea8b592019-10-29 16:09:51 +0100459 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
460 rc = flash_area_open(area_id, &fap);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000461 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +0100462 return -1;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000463 }
464
David Vinczecea8b592019-10-29 16:09:51 +0100465 hdr = boot_img_hdr(state, slot);
466 if ((boot_check_header_erased(state, slot) == 0) ||
David Vincze401c7422019-06-21 20:44:05 +0200467 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100468 /* No bootable image in slot; continue booting from the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100469 rc = 1;
David Vincze401c7422019-06-21 20:44:05 +0200470 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200471 }
472
David Vinczecea8b592019-10-29 16:09:51 +0100473 if ((!BOOT_IMG_HDR_IS_VALID(state, slot)) ||
474 (boot_image_check(state, hdr, fap, bs) != 0)) {
David Vincze401c7422019-06-21 20:44:05 +0200475 if (slot != BOOT_PRIMARY_SLOT) {
David Vinczecea8b592019-10-29 16:09:51 +0100476 flash_area_erase(fap, 0, fap->fa_size);
David Vincze8bdfc2d2019-03-18 15:49:23 +0100477 /* Image in the secondary slot is invalid. Erase the image and
478 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000479 */
480 }
David Vinczecea8b592019-10-29 16:09:51 +0100481 BOOT_LOG_ERR("Image in the %s slot is not valid!",
482 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200483 rc = -1;
484 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000485 }
486
David Vincze8bdfc2d2019-03-18 15:49:23 +0100487 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200488 rc = 0;
489
490out:
491 flash_area_close(fap);
492 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000493}
494
David Vincze060968d2019-05-23 01:13:14 +0200495/**
496 * Updates the stored security counter value with the image's security counter
497 * value which resides in the given slot if it's greater than the stored value.
498 *
David Vinczecea8b592019-10-29 16:09:51 +0100499 * @param image_index Index of the image to determine which security
500 * counter to update.
501 * @param slot Slot number of the image.
502 * @param hdr Pointer to the image header structure of the image
503 * that is currently stored in the given slot.
David Vincze060968d2019-05-23 01:13:14 +0200504 *
David Vinczecea8b592019-10-29 16:09:51 +0100505 * @return 0 on success; nonzero on failure.
David Vincze060968d2019-05-23 01:13:14 +0200506 */
507static int
David Vinczecea8b592019-10-29 16:09:51 +0100508boot_update_security_counter(uint8_t image_index, int slot,
509 struct image_header *hdr)
David Vincze060968d2019-05-23 01:13:14 +0200510{
511 const struct flash_area *fap = NULL;
512 uint32_t img_security_cnt;
513 int rc;
514
David Vinczecea8b592019-10-29 16:09:51 +0100515 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
516 &fap);
David Vincze060968d2019-05-23 01:13:14 +0200517 if (rc != 0) {
518 rc = BOOT_EFLASH;
519 goto done;
520 }
521
522 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
523 if (rc != 0) {
524 goto done;
525 }
526
David Vinczecea8b592019-10-29 16:09:51 +0100527 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
David Vincze060968d2019-05-23 01:13:14 +0200528 if (rc != 0) {
529 goto done;
530 }
531
532done:
533 flash_area_close(fap);
534 return rc;
535}
536
Oliver Swedef9982442018-08-24 18:37:44 +0100537#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
538/*
539 * Compute the total size of the given image. Includes the size of
540 * the TLVs.
541 */
542static int
David Vinczecea8b592019-10-29 16:09:51 +0100543boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
Oliver Swedef9982442018-08-24 18:37:44 +0100544{
545 const struct flash_area *fap = NULL;
David Vincze07706a42019-12-12 18:20:12 +0100546 struct image_tlv_info info;
David Vinczecea8b592019-10-29 16:09:51 +0100547 uint32_t off;
Oliver Swedef9982442018-08-24 18:37:44 +0100548 int area_id;
549 int rc;
550
David Vinczecea8b592019-10-29 16:09:51 +0100551#if (BOOT_IMAGE_NUMBER == 1)
552 (void)state;
553#endif
554
555 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Oliver Swedef9982442018-08-24 18:37:44 +0100556 rc = flash_area_open(area_id, &fap);
557 if (rc != 0) {
558 rc = BOOT_EFLASH;
559 goto done;
560 }
561
David Vincze07706a42019-12-12 18:20:12 +0100562 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
563
564 if (flash_area_read(fap, off, &info, sizeof(info))) {
565 rc = BOOT_EFLASH;
Oliver Swedef9982442018-08-24 18:37:44 +0100566 goto done;
567 }
David Vincze07706a42019-12-12 18:20:12 +0100568
569 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
570 rc = BOOT_EBADIMAGE;
571 goto done;
572 }
573
574 *size = off + info.it_tlv_tot;
Oliver Swedef9982442018-08-24 18:37:44 +0100575 rc = 0;
576
577done:
578 flash_area_close(fap);
579 return rc;
580}
581#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
582
583#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000584/**
Tamas Ban581034a2017-12-19 19:54:37 +0000585 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000586 * status is necessary for completing a swap that was interrupted by a boot
587 * loader reset.
588 *
David Vincze401c7422019-06-21 20:44:05 +0200589 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
590 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000591 */
592static int
David Vinczecea8b592019-10-29 16:09:51 +0100593boot_status_source(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000594{
595 const struct boot_status_table *table;
596 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100597 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000598 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200599 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000600 uint8_t source;
David Vinczecea8b592019-10-29 16:09:51 +0100601 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000602
David Vinczecea8b592019-10-29 16:09:51 +0100603#if (BOOT_IMAGE_NUMBER == 1)
604 (void)state;
605#endif
606
607 image_index = BOOT_CURR_IMG(state);
608 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
609 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000610 assert(rc == 0);
611
612 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
613 assert(rc == 0);
614
David Vincze401c7422019-06-21 20:44:05 +0200615 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000616 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
617
618 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
619 table = &boot_status_tables[i];
620
David Vincze401c7422019-06-21 20:44:05 +0200621 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
622 state_primary_slot.magic) &&
623 boot_magic_compatible_check(table->bst_magic_scratch,
624 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100625 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
626 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
627 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000628 source = table->bst_status_source;
David Vincze7384ee72019-07-23 17:00:42 +0200629
630#if (BOOT_IMAGE_NUMBER > 1)
631 /* In case of multi-image boot it can happen that if boot status
632 * info is found on scratch area then it does not belong to the
633 * currently examined image.
634 */
635 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
David Vinczecea8b592019-10-29 16:09:51 +0100636 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +0200637 source = BOOT_STATUS_SOURCE_NONE;
638 }
639#endif
640
Tamas Banf70ef8c2017-12-19 15:35:09 +0000641 BOOT_LOG_INF("Boot source: %s",
642 source == BOOT_STATUS_SOURCE_NONE ? "none" :
643 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100644 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
645 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000646 return source;
647 }
648 }
649
650 BOOT_LOG_INF("Boot source: none");
651 return BOOT_STATUS_SOURCE_NONE;
652}
653
David Vincze401c7422019-06-21 20:44:05 +0200654/*
655 * Slots are compatible when all sectors that store upto to size of the image
656 * round up to sector size, in both slot's are able to fit in the scratch
657 * area, and have sizes that are a multiple of each other (powers of two
658 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000659 */
660static int
David Vinczecea8b592019-10-29 16:09:51 +0100661boot_slots_compatible(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000662{
David Vincze401c7422019-06-21 20:44:05 +0200663 size_t num_sectors_primary;
664 size_t num_sectors_secondary;
665 size_t sz0, sz1;
666 size_t primary_slot_sz, secondary_slot_sz;
667 size_t scratch_sz;
668 size_t i, j;
669 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000670
David Vinczecea8b592019-10-29 16:09:51 +0100671 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
672 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze401c7422019-06-21 20:44:05 +0200673 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
674 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200675 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000676 return 0;
677 }
David Vincze39e78552018-10-10 17:10:01 +0200678
David Vinczecea8b592019-10-29 16:09:51 +0100679 scratch_sz = boot_scratch_area_size(state);
David Vincze401c7422019-06-21 20:44:05 +0200680
681 /*
682 * The following loop scans all sectors in a linear fashion, assuring that
683 * for each possible sector in each slot, it is able to fit in the other
684 * slot's sector or sectors. Slot's should be compatible as long as any
685 * number of a slot's sectors are able to fit into another, which only
686 * excludes cases where sector sizes are not a multiple of each other.
687 */
688 i = sz0 = primary_slot_sz = 0;
689 j = sz1 = secondary_slot_sz = 0;
690 smaller = 0;
691 while (i < num_sectors_primary || j < num_sectors_secondary) {
692 if (sz0 == sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100693 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
694 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200695 i++;
696 j++;
697 } else if (sz0 < sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100698 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200699 /* Guarantee that multiple sectors of the secondary slot
700 * fit into the primary slot.
701 */
702 if (smaller == 2) {
703 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
704 " sectors");
705 return 0;
706 }
707 smaller = 1;
708 i++;
709 } else {
David Vinczecea8b592019-10-29 16:09:51 +0100710 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200711 /* Guarantee that multiple sectors of the primary slot
712 * fit into the secondary slot.
713 */
714 if (smaller == 1) {
715 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
716 " sectors");
717 return 0;
718 }
719 smaller = 2;
720 j++;
721 }
722 if (sz0 == sz1) {
723 primary_slot_sz += sz0;
724 secondary_slot_sz += sz1;
725 /* Scratch has to fit each swap operation to the size of the larger
726 * sector among the primary slot and the secondary slot.
727 */
728 if (sz0 > scratch_sz || sz1 > scratch_sz) {
729 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
730 " scratch");
731 return 0;
732 }
733 smaller = sz0 = sz1 = 0;
734 }
David Vincze39e78552018-10-10 17:10:01 +0200735 }
736
David Vincze401c7422019-06-21 20:44:05 +0200737 if ((i != num_sectors_primary) ||
738 (j != num_sectors_secondary) ||
739 (primary_slot_sz != secondary_slot_sz)) {
740 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
741 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000742 }
743
744 return 1;
745}
746
Tamas Banf70ef8c2017-12-19 15:35:09 +0000747static uint32_t
748boot_status_internal_off(int idx, int state, int elem_sz)
749{
750 int idx_sz;
751
752 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
753
David Vincze39e78552018-10-10 17:10:01 +0200754 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
755 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000756}
757
758/**
759 * Reads the status of a partially-completed swap, if any. This is necessary
760 * to recover in case the boot lodaer was reset in the middle of a swap
761 * operation.
762 */
763static int
David Vinczecea8b592019-10-29 16:09:51 +0100764boot_read_status_bytes(const struct flash_area *fap,
765 struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000766{
767 uint32_t off;
768 uint8_t status;
769 int max_entries;
770 int found;
David Vincze39e78552018-10-10 17:10:01 +0200771 int found_idx;
772 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000773 int rc;
774 int i;
775
776 off = boot_status_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100777 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
778 if (max_entries < 0) {
779 return BOOT_EBADARGS;
780 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000781
782 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200783 found_idx = 0;
784 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000785 for (i = 0; i < max_entries; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100786 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
David Vincze39e78552018-10-10 17:10:01 +0200787 &status, 1);
788 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000789 return BOOT_EFLASH;
790 }
791
David Vincze39e78552018-10-10 17:10:01 +0200792 if (rc == 1) {
793 if (found && !found_idx) {
794 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000795 }
796 } else if (!found) {
797 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200798 } else if (found_idx) {
799 invalid = 1;
800 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000801 }
802 }
803
David Vincze39e78552018-10-10 17:10:01 +0200804 if (invalid) {
805 /* This means there was an error writing status on the last
806 * swap. Tell user and move on to validation!
807 */
808 BOOT_LOG_ERR("Detected inconsistent status!");
809
David Vincze8bdfc2d2019-03-18 15:49:23 +0100810#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
811 /* With validation of the primary slot disabled, there is no way
812 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200813 */
814 assert(0);
815#endif
816 }
817
Tamas Banf70ef8c2017-12-19 15:35:09 +0000818 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200819 if (!found_idx) {
820 found_idx = i;
821 }
David Vincze39e78552018-10-10 17:10:01 +0200822 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
823 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000824 }
825
826 return 0;
827}
828
829/**
830 * Reads the boot status from the flash. The boot status contains
831 * the current state of an interrupted image copy operation. If the boot
832 * status is not present, or it indicates that previous copy finished,
833 * there is no operation in progress.
834 */
835static int
David Vinczecea8b592019-10-29 16:09:51 +0100836boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000837{
838 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200839 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200840 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000841 int status_loc;
842 int area_id;
843 int rc;
844
David Vincze39e78552018-10-10 17:10:01 +0200845 memset(bs, 0, sizeof *bs);
846 bs->idx = BOOT_STATUS_IDX_0;
847 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200848 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200849
850#ifdef MCUBOOT_OVERWRITE_ONLY
851 /* Overwrite-only doesn't make use of the swap status area. */
852 return 0;
853#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000854
David Vinczecea8b592019-10-29 16:09:51 +0100855 status_loc = boot_status_source(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000856 switch (status_loc) {
857 case BOOT_STATUS_SOURCE_NONE:
858 return 0;
859
860 case BOOT_STATUS_SOURCE_SCRATCH:
861 area_id = FLASH_AREA_IMAGE_SCRATCH;
862 break;
863
David Vincze8bdfc2d2019-03-18 15:49:23 +0100864 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
David Vinczecea8b592019-10-29 16:09:51 +0100865 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000866 break;
867
868 default:
869 assert(0);
870 return BOOT_EBADARGS;
871 }
872
873 rc = flash_area_open(area_id, &fap);
874 if (rc != 0) {
875 return BOOT_EFLASH;
876 }
877
David Vinczecea8b592019-10-29 16:09:51 +0100878 rc = boot_read_status_bytes(fap, state, bs);
David Vincze401c7422019-06-21 20:44:05 +0200879 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200880 off = boot_swap_info_off(fap);
881 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200882 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200883 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200884 rc = 0;
885 }
David Vincze91b71ef2019-06-24 13:06:47 +0200886
887 /* Extract the swap type info */
888 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200889 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000890
891 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200892
Tamas Banf70ef8c2017-12-19 15:35:09 +0000893 return rc;
894}
895
896/**
897 * Writes the supplied boot status to the flash file system. The boot status
898 * contains the current state of an in-progress image copy operation.
899 *
900 * @param bs The boot status to write.
901 *
902 * @return 0 on success; nonzero on failure.
903 */
904int
David Vinczecea8b592019-10-29 16:09:51 +0100905boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000906{
Tamas Ban581034a2017-12-19 19:54:37 +0000907 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000908 uint32_t off;
909 int area_id;
910 int rc;
911 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100912 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200913 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000914
915 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100916 * the trailer. Since in the last step the primary slot is erased, the
917 * first two status writes go to the scratch which will be copied to
918 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000919 */
920
921 if (bs->use_scratch) {
922 /* Write to scratch. */
923 area_id = FLASH_AREA_IMAGE_SCRATCH;
924 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100925 /* Write to the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100926 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000927 }
928
929 rc = flash_area_open(area_id, &fap);
930 if (rc != 0) {
931 rc = BOOT_EFLASH;
932 goto done;
933 }
934
935 off = boot_status_off(fap) +
David Vinczecea8b592019-10-29 16:09:51 +0100936 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Tamas Banc3828852018-02-01 12:24:16 +0000937 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200938 erased_val = flash_area_erased_val(fap);
939 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000940 buf[0] = bs->state;
941
942 rc = flash_area_write(fap, off, buf, align);
943 if (rc != 0) {
944 rc = BOOT_EFLASH;
945 goto done;
946 }
947
948 rc = 0;
949
950done:
951 flash_area_close(fap);
952 return rc;
953}
954
Tamas Banf70ef8c2017-12-19 15:35:09 +0000955/**
956 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100957 * that a swap operation is required, the image in the secondary slot is checked
958 * for validity. If the image in the secondary slot is invalid, it is erased,
959 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000960 *
961 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
962 */
963static int
David Vinczecea8b592019-10-29 16:09:51 +0100964boot_validated_swap_type(struct boot_loader_state *state,
965 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000966{
967 int swap_type;
David Vinczecea8b592019-10-29 16:09:51 +0100968 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000969
David Vinczecea8b592019-10-29 16:09:51 +0100970 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
971 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100972 /* Boot loader wants to switch to the secondary slot.
973 * Ensure image is valid.
974 */
David Vinczecea8b592019-10-29 16:09:51 +0100975 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
976 if (rc == 1) {
977 swap_type = BOOT_SWAP_TYPE_NONE;
978 } else if (rc != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000979 swap_type = BOOT_SWAP_TYPE_FAIL;
980 }
981 }
982
983 return swap_type;
984}
985
986/**
987 * Calculates the number of sectors the scratch area can contain. A "last"
988 * source sector is specified because images are copied backwards in flash
989 * (final index to index number 0).
990 *
991 * @param last_sector_idx The index of the last source sector
992 * (inclusive).
993 * @param out_first_sector_idx The index of the first source sector
994 * (inclusive) gets written here.
995 *
996 * @return The number of bytes comprised by the
997 * [first-sector, last-sector] range.
998 */
999#ifndef MCUBOOT_OVERWRITE_ONLY
1000static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01001001boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
1002 int *out_first_sector_idx)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001003{
1004 size_t scratch_sz;
1005 uint32_t new_sz;
1006 uint32_t sz;
1007 int i;
1008
1009 sz = 0;
1010
David Vinczecea8b592019-10-29 16:09:51 +01001011 scratch_sz = boot_scratch_area_size(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001012 for (i = last_sector_idx; i >= 0; i--) {
David Vinczecea8b592019-10-29 16:09:51 +01001013 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +02001014 /*
1015 * The secondary slot is not being checked here, because
1016 * `boot_slots_compatible` already provides assurance that the copy size
1017 * will be compatible with the primary slot and scratch.
1018 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001019 if (new_sz > scratch_sz) {
1020 break;
1021 }
1022 sz = new_sz;
1023 }
1024
1025 /* i currently refers to a sector that doesn't fit or it is -1 because all
1026 * sectors have been processed. In both cases, exclude sector i.
1027 */
1028 *out_first_sector_idx = i + 1;
1029 return sz;
1030}
1031#endif /* !MCUBOOT_OVERWRITE_ONLY */
1032
1033/**
David Vinczef7641fa2018-09-04 18:29:46 +02001034 * Erases a region of flash.
1035 *
David Vincze401c7422019-06-21 20:44:05 +02001036 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +02001037 * @param off The offset within the flash area to start the
1038 * erase.
1039 * @param sz The number of bytes to erase.
1040 *
1041 * @return 0 on success; nonzero on failure.
1042 */
David Vincze401c7422019-06-21 20:44:05 +02001043static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001044boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +02001045{
David Vincze401c7422019-06-21 20:44:05 +02001046 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001047}
1048
1049/**
Tamas Banf70ef8c2017-12-19 15:35:09 +00001050 * Copies the contents of one flash region to another. You must erase the
1051 * destination region prior to calling this function.
1052 *
1053 * @param flash_area_id_src The ID of the source flash area.
1054 * @param flash_area_id_dst The ID of the destination flash area.
1055 * @param off_src The offset within the source flash area to
1056 * copy from.
1057 * @param off_dst The offset within the destination flash area to
1058 * copy to.
1059 * @param sz The number of bytes to copy.
1060 *
1061 * @return 0 on success; nonzero on failure.
1062 */
1063static int
David Vinczecea8b592019-10-29 16:09:51 +01001064boot_copy_region(struct boot_loader_state *state,
1065 const struct flash_area *fap_src,
David Vincze401c7422019-06-21 20:44:05 +02001066 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001067 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1068{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001069 uint32_t bytes_copied;
1070 int chunk_sz;
1071 int rc;
1072
1073 static uint8_t buf[1024];
1074
David Vinczecea8b592019-10-29 16:09:51 +01001075 (void)state;
1076
Tamas Banf70ef8c2017-12-19 15:35:09 +00001077 bytes_copied = 0;
1078 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +00001079 if (sz - bytes_copied > sizeof(buf)) {
1080 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001081 } else {
1082 chunk_sz = sz - bytes_copied;
1083 }
1084
1085 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1086 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001087 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001088 }
1089
1090 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1091 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001092 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001093 }
1094
1095 bytes_copied += chunk_sz;
1096 }
1097
David Vincze401c7422019-06-21 20:44:05 +02001098 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001099}
1100
1101#ifndef MCUBOOT_OVERWRITE_ONLY
1102static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001103boot_status_init(const struct boot_loader_state *state,
1104 const struct flash_area *fap,
1105 const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001106{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001107 struct boot_swap_state swap_state;
David Vinczecea8b592019-10-29 16:09:51 +01001108 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001109 int rc;
1110
David Vinczecea8b592019-10-29 16:09:51 +01001111#if (BOOT_IMAGE_NUMBER == 1)
1112 (void)state;
1113#endif
1114
1115 image_index = BOOT_CURR_IMG(state);
1116
David Vincze401c7422019-06-21 20:44:05 +02001117 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001118
David Vinczecea8b592019-10-29 16:09:51 +01001119 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1120 &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001121 assert(rc == 0);
1122
David Vincze401c7422019-06-21 20:44:05 +02001123 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01001124 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
David Vincze401c7422019-06-21 20:44:05 +02001125 assert(rc == 0);
1126 }
1127
Tamas Banf70ef8c2017-12-19 15:35:09 +00001128 if (swap_state.image_ok == BOOT_FLAG_SET) {
1129 rc = boot_write_image_ok(fap);
1130 assert(rc == 0);
1131 }
1132
1133 rc = boot_write_swap_size(fap, bs->swap_size);
1134 assert(rc == 0);
1135
1136 rc = boot_write_magic(fap);
1137 assert(rc == 0);
1138
Tamas Banf70ef8c2017-12-19 15:35:09 +00001139 return 0;
1140}
David Vinczef7641fa2018-09-04 18:29:46 +02001141
1142static int
David Vinczecea8b592019-10-29 16:09:51 +01001143boot_erase_trailer_sectors(const struct boot_loader_state *state,
1144 const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +02001145{
1146 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +02001147 uint32_t sector;
1148 uint32_t trailer_sz;
1149 uint32_t total_sz;
1150 uint32_t off;
1151 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +02001152 int fa_id_primary;
1153 int fa_id_secondary;
David Vinczecea8b592019-10-29 16:09:51 +01001154 uint8_t image_index;
David Vinczef7641fa2018-09-04 18:29:46 +02001155 int rc;
1156
David Vincze401c7422019-06-21 20:44:05 +02001157 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1158
David Vinczecea8b592019-10-29 16:09:51 +01001159 image_index = BOOT_CURR_IMG(state);
1160 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1161 BOOT_PRIMARY_SLOT);
1162 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1163 BOOT_SECONDARY_SLOT);
David Vinczebb207982019-08-21 15:13:04 +02001164
1165 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001166 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001167 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001168 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001169 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001170 return BOOT_EFLASH;
1171 }
1172
David Vincze401c7422019-06-21 20:44:05 +02001173 /* delete starting from last sector and moving to beginning */
David Vinczecea8b592019-10-29 16:09:51 +01001174 sector = boot_img_num_sectors(state, slot) - 1;
1175 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
David Vincze401c7422019-06-21 20:44:05 +02001176 total_sz = 0;
1177 do {
David Vinczecea8b592019-10-29 16:09:51 +01001178 sz = boot_img_sector_size(state, slot, sector);
1179 off = boot_img_sector_off(state, slot, sector);
1180 rc = boot_erase_region(fap, off, sz);
David Vincze401c7422019-06-21 20:44:05 +02001181 assert(rc == 0);
1182
1183 sector--;
1184 total_sz += sz;
1185 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001186
1187 return rc;
1188}
1189#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001190
Tamas Banf70ef8c2017-12-19 15:35:09 +00001191/**
1192 * Swaps the contents of two flash regions within the two image slots.
1193 *
1194 * @param idx The index of the first sector in the range of
1195 * sectors being swapped.
1196 * @param sz The number of bytes to swap.
1197 * @param bs The current boot status. This struct gets
1198 * updated according to the outcome.
1199 *
1200 * @return 0 on success; nonzero on failure.
1201 */
1202#ifndef MCUBOOT_OVERWRITE_ONLY
1203static void
David Vinczecea8b592019-10-29 16:09:51 +01001204boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1205 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001206{
David Vincze401c7422019-06-21 20:44:05 +02001207 const struct flash_area *fap_primary_slot;
1208 const struct flash_area *fap_secondary_slot;
1209 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001210 uint32_t copy_sz;
1211 uint32_t trailer_sz;
1212 uint32_t img_off;
1213 uint32_t scratch_trailer_off;
1214 struct boot_swap_state swap_state;
1215 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001216 bool erase_scratch;
David Vinczecea8b592019-10-29 16:09:51 +01001217 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001218 int rc;
1219
1220 /* Calculate offset from start of image area. */
David Vinczecea8b592019-10-29 16:09:51 +01001221 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001222
1223 copy_sz = sz;
David Vinczecea8b592019-10-29 16:09:51 +01001224 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001225
David Vincze401c7422019-06-21 20:44:05 +02001226 /* sz in this function is always sized on a multiple of the sector size.
1227 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001228 * is to determine if we're swapping the last sector. The last sector
1229 * needs special handling because it's where the trailer lives. If we're
1230 * copying it, we need to use scratch to write the trailer temporarily.
1231 *
1232 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1233 * controls if special handling is needed (swapping last sector).
1234 */
David Vinczecea8b592019-10-29 16:09:51 +01001235 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001236 if ((img_off + sz) >
David Vinczecea8b592019-10-29 16:09:51 +01001237 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001238 copy_sz -= trailer_sz;
1239 }
1240
David Vincze39e78552018-10-10 17:10:01 +02001241 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001242
David Vinczecea8b592019-10-29 16:09:51 +01001243 image_index = BOOT_CURR_IMG(state);
1244
1245 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1246 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001247 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001248
David Vinczecea8b592019-10-29 16:09:51 +01001249 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1250 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001251 assert (rc == 0);
1252
1253 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1254 assert (rc == 0);
1255
1256 if (bs->state == BOOT_STATUS_STATE_0) {
1257 BOOT_LOG_DBG("erasing scratch area");
David Vinczecea8b592019-10-29 16:09:51 +01001258 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001259 assert(rc == 0);
1260
David Vincze39e78552018-10-10 17:10:01 +02001261 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001262 /* Write a trailer to the scratch area, even if we don't need the
1263 * scratch area for status. We need a temporary place to store the
1264 * `swap-type` while we erase the primary trailer.
1265 */
David Vinczecea8b592019-10-29 16:09:51 +01001266 rc = boot_status_init(state, fap_scratch, bs);
David Vincze401c7422019-06-21 20:44:05 +02001267 assert(rc == 0);
1268
1269 if (!bs->use_scratch) {
1270 /* Prepare the primary status area... here it is known that the
1271 * last sector is not being used by the image data so it's safe
1272 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001273 */
David Vinczecea8b592019-10-29 16:09:51 +01001274 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001275 assert(rc == 0);
1276
David Vinczecea8b592019-10-29 16:09:51 +01001277 rc = boot_status_init(state, fap_primary_slot, bs);
David Vincze401c7422019-06-21 20:44:05 +02001278 assert(rc == 0);
1279
1280 /* Erase the temporary trailer from the scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01001281 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
David Vincze401c7422019-06-21 20:44:05 +02001282 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001283 }
1284 }
1285
David Vinczecea8b592019-10-29 16:09:51 +01001286 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
David Vincze401c7422019-06-21 20:44:05 +02001287 img_off, 0, copy_sz);
1288 assert(rc == 0);
1289
David Vinczecea8b592019-10-29 16:09:51 +01001290 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001291 bs->state = BOOT_STATUS_STATE_1;
David Vincze39e78552018-10-10 17:10:01 +02001292 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001293 }
1294
David Vincze39e78552018-10-10 17:10:01 +02001295 if (bs->state == BOOT_STATUS_STATE_1) {
David Vinczecea8b592019-10-29 16:09:51 +01001296 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001297 assert(rc == 0);
1298
David Vinczecea8b592019-10-29 16:09:51 +01001299 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001300 img_off, img_off, copy_sz);
1301 assert(rc == 0);
1302
David Vincze39e78552018-10-10 17:10:01 +02001303 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001304 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001305 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001306 */
David Vinczecea8b592019-10-29 16:09:51 +01001307 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001308 assert(rc == 0);
1309 }
1310
David Vinczecea8b592019-10-29 16:09:51 +01001311 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001312 bs->state = BOOT_STATUS_STATE_2;
David Vincze39e78552018-10-10 17:10:01 +02001313 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001314 }
1315
David Vincze39e78552018-10-10 17:10:01 +02001316 if (bs->state == BOOT_STATUS_STATE_2) {
David Vinczecea8b592019-10-29 16:09:51 +01001317 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001318 assert(rc == 0);
1319
David Vincze401c7422019-06-21 20:44:05 +02001320 /* NOTE: If this is the final sector, we exclude the image trailer from
1321 * this copy (copy_sz was truncated earlier).
1322 */
David Vinczecea8b592019-10-29 16:09:51 +01001323 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001324 0, img_off, copy_sz);
1325 assert(rc == 0);
1326
1327 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001328 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001329
1330 /* copy current status that is being maintained in scratch */
David Vinczecea8b592019-10-29 16:09:51 +01001331 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze401c7422019-06-21 20:44:05 +02001332 scratch_trailer_off, img_off + copy_sz,
David Vinczecea8b592019-10-29 16:09:51 +01001333 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
David Vincze39e78552018-10-10 17:10:01 +02001334 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001335
1336 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1337 &swap_state);
1338 assert(rc == 0);
1339
1340 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001341 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001342 assert(rc == 0);
1343 }
1344
David Vincze401c7422019-06-21 20:44:05 +02001345 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001346 rc = boot_write_swap_info(fap_primary_slot,
1347 swap_state.swap_type,
David Vinczecea8b592019-10-29 16:09:51 +01001348 image_index);
David Vincze401c7422019-06-21 20:44:05 +02001349 assert(rc == 0);
1350 }
1351
1352 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001353 assert(rc == 0);
1354
David Vincze401c7422019-06-21 20:44:05 +02001355 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001356 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001357 }
1358
David Vincze401c7422019-06-21 20:44:05 +02001359 /* If we wrote a trailer to the scratch area, erase it after we persist
1360 * a trailer to the primary slot. We do this to prevent mcuboot from
1361 * reading a stale status from the scratch area in case of immediate
1362 * reset.
1363 */
1364 erase_scratch = bs->use_scratch;
1365 bs->use_scratch = 0;
1366
David Vinczecea8b592019-10-29 16:09:51 +01001367 rc = boot_write_status(state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001368 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001369 bs->state = BOOT_STATUS_STATE_0;
David Vincze39e78552018-10-10 17:10:01 +02001370 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001371
1372 if (erase_scratch) {
David Vinczecea8b592019-10-29 16:09:51 +01001373 rc = boot_erase_region(fap_scratch, 0, sz);
David Vincze401c7422019-06-21 20:44:05 +02001374 assert(rc == 0);
1375 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001376 }
David Vincze401c7422019-06-21 20:44:05 +02001377
1378 flash_area_close(fap_primary_slot);
1379 flash_area_close(fap_secondary_slot);
1380 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001381}
1382#endif /* !MCUBOOT_OVERWRITE_ONLY */
1383
1384/**
David Vincze401c7422019-06-21 20:44:05 +02001385 * Overwrite primary slot with the image contained in the secondary slot.
1386 * If a prior copy operation was interrupted by a system reset, this function
1387 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001388 *
1389 * @param bs The current boot status. This function reads
1390 * this struct to determine if it is resuming
1391 * an interrupted swap operation. This
1392 * function writes the updated status to this
1393 * function on return.
1394 *
1395 * @return 0 on success; nonzero on failure.
1396 */
1397#ifdef MCUBOOT_OVERWRITE_ONLY
1398static int
David Vinczecea8b592019-10-29 16:09:51 +01001399boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001400{
1401 size_t sect_count;
1402 size_t sect;
1403 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001404 size_t size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001405 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001406 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001407 const struct flash_area *fap_primary_slot;
1408 const struct flash_area *fap_secondary_slot;
David Vinczecea8b592019-10-29 16:09:51 +01001409 uint8_t image_index;
David Vincze39e78552018-10-10 17:10:01 +02001410
1411 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001412
David Vincze8bdfc2d2019-03-18 15:49:23 +01001413 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1414 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001415
David Vinczecea8b592019-10-29 16:09:51 +01001416 image_index = BOOT_CURR_IMG(state);
1417
1418 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1419 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001420 assert (rc == 0);
1421
David Vinczecea8b592019-10-29 16:09:51 +01001422 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1423 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001424 assert (rc == 0);
1425
David Vinczecea8b592019-10-29 16:09:51 +01001426 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
1427 for (sect = 0, size = 0; sect < sect_count; sect++) {
1428 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
1429 rc = boot_erase_region(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001430 assert(rc == 0);
1431
1432 size += this_size;
1433 }
1434
David Vincze8bdfc2d2019-03-18 15:49:23 +01001435 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1436 size);
David Vinczecea8b592019-10-29 16:09:51 +01001437 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot,
1438 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001439
David Vincze060968d2019-05-23 01:13:14 +02001440 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001441 * value. Both slots hold the new image at this point, but the secondary
1442 * slot's image header must be passed because the read image headers in the
1443 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001444 */
David Vinczecea8b592019-10-29 16:09:51 +01001445 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1446 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001447 if (rc != 0) {
1448 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1449 return rc;
1450 }
1451
David Vincze39e78552018-10-10 17:10:01 +02001452 /*
1453 * Erases header and trailer. The trailer is erased because when a new
1454 * image is written without a trailer as is the case when using newt, the
1455 * trailer that was left might trigger a new upgrade.
1456 */
David Vincze401c7422019-06-21 20:44:05 +02001457 BOOT_LOG_DBG("erasing secondary header");
David Vinczecea8b592019-10-29 16:09:51 +01001458 rc = boot_erase_region(fap_secondary_slot,
1459 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1460 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001461 assert(rc == 0);
David Vinczecea8b592019-10-29 16:09:51 +01001462 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001463 BOOT_LOG_DBG("erasing secondary trailer");
David Vinczecea8b592019-10-29 16:09:51 +01001464 rc = boot_erase_region(fap_secondary_slot,
1465 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1466 last_sector),
1467 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1468 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001469 assert(rc == 0);
1470
David Vincze401c7422019-06-21 20:44:05 +02001471 flash_area_close(fap_primary_slot);
1472 flash_area_close(fap_secondary_slot);
1473
David Vincze8bdfc2d2019-03-18 15:49:23 +01001474 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001475
1476 return 0;
1477}
1478#else
David Vincze401c7422019-06-21 20:44:05 +02001479/**
1480 * Swaps the two images in flash. If a prior copy operation was interrupted
1481 * by a system reset, this function completes that operation.
1482 *
1483 * @param bs The current boot status. This function reads
1484 * this struct to determine if it is resuming
1485 * an interrupted swap operation. This
1486 * function writes the updated status to this
1487 * function on return.
1488 *
1489 * @return 0 on success; nonzero on failure.
1490 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001491static int
David Vinczecea8b592019-10-29 16:09:51 +01001492boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001493{
1494 uint32_t sz;
1495 int first_sector_idx;
1496 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001497 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001498 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001499 struct image_header *hdr;
1500 uint32_t size;
1501 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001502 uint32_t primary_slot_size;
1503 uint32_t secondary_slot_size;
David Vinczecea8b592019-10-29 16:09:51 +01001504 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001505 int rc;
1506
1507 /* FIXME: just do this if asked by user? */
1508
1509 size = copy_size = 0;
David Vinczecea8b592019-10-29 16:09:51 +01001510 image_index = BOOT_CURR_IMG(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001511
David Vincze39e78552018-10-10 17:10:01 +02001512 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001513 /*
1514 * No swap ever happened, so need to find the largest image which
1515 * will be used to determine the amount of sectors to swap.
1516 */
David Vinczecea8b592019-10-29 16:09:51 +01001517 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1518 if (hdr->ih_magic == IMAGE_MAGIC) {
1519 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
1520 assert(rc == 0);
1521 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001522
David Vinczecea8b592019-10-29 16:09:51 +01001523 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1524 if (hdr->ih_magic == IMAGE_MAGIC) {
1525 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
1526 assert(rc == 0);
1527 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001528
1529 if (size > copy_size) {
1530 copy_size = size;
1531 }
1532
1533 bs->swap_size = copy_size;
1534 } else {
1535 /*
1536 * If a swap was under way, the swap_size should already be present
1537 * in the trailer...
1538 */
David Vinczecea8b592019-10-29 16:09:51 +01001539 rc = boot_read_swap_size(image_index, &bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001540 assert(rc == 0);
1541
1542 copy_size = bs->swap_size;
1543 }
1544
David Vincze401c7422019-06-21 20:44:05 +02001545 primary_slot_size = 0;
1546 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001547 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001548 last_idx_secondary_slot = 0;
1549
1550 /*
1551 * Knowing the size of the largest image between both slots, here we
1552 * find what is the last sector in the primary slot that needs swapping.
1553 * Since we already know that both slots are compatible, the secondary
1554 * slot's last sector is not really required after this check is finished.
1555 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001556 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001557 if ((primary_slot_size < copy_size) ||
1558 (primary_slot_size < secondary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001559 primary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001560 BOOT_PRIMARY_SLOT,
1561 last_sector_idx);
1562 }
1563 if ((secondary_slot_size < copy_size) ||
1564 (secondary_slot_size < primary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001565 secondary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001566 BOOT_SECONDARY_SLOT,
1567 last_idx_secondary_slot);
1568 }
1569 if (primary_slot_size >= copy_size &&
1570 secondary_slot_size >= copy_size &&
1571 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001572 break;
1573 }
1574 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001575 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001576 }
1577
1578 swap_idx = 0;
1579 while (last_sector_idx >= 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001580 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001581 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
David Vinczecea8b592019-10-29 16:09:51 +01001582 boot_swap_sectors(first_sector_idx, sz, state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001583 }
1584
1585 last_sector_idx = first_sector_idx - 1;
1586 swap_idx++;
1587 }
1588
David Vincze8bdfc2d2019-03-18 15:49:23 +01001589#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001590 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001591 BOOT_LOG_WRN("%d status write fails performing the swap",
1592 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001593 }
1594#endif
1595
Tamas Banf70ef8c2017-12-19 15:35:09 +00001596 return 0;
1597}
1598#endif
1599
1600/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001601 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001602 */
1603#ifndef MCUBOOT_OVERWRITE_ONLY
1604static int
David Vinczecea8b592019-10-29 16:09:51 +01001605boot_set_copy_done(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001606{
1607 const struct flash_area *fap;
1608 int rc;
1609
David Vinczecea8b592019-10-29 16:09:51 +01001610 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1611 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001612 if (rc != 0) {
1613 return BOOT_EFLASH;
1614 }
1615
1616 rc = boot_write_copy_done(fap);
1617 flash_area_close(fap);
1618 return rc;
1619}
1620#endif /* !MCUBOOT_OVERWRITE_ONLY */
1621
1622/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001623 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1624 * ensure the status bytes from the image revert operation don't get processed
1625 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001626 *
1627 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001628 * image installed on the primary slot and the new image to be upgrade to has a
1629 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001630 */
1631#ifndef MCUBOOT_OVERWRITE_ONLY
1632static int
David Vinczecea8b592019-10-29 16:09:51 +01001633boot_set_image_ok(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001634{
1635 const struct flash_area *fap;
1636 struct boot_swap_state state;
1637 int rc;
1638
David Vinczecea8b592019-10-29 16:09:51 +01001639 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1640 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001641 if (rc != 0) {
1642 return BOOT_EFLASH;
1643 }
1644
1645 rc = boot_read_swap_state(fap, &state);
1646 if (rc != 0) {
1647 rc = BOOT_EFLASH;
1648 goto out;
1649 }
1650
1651 if (state.image_ok == BOOT_FLAG_UNSET) {
1652 rc = boot_write_image_ok(fap);
1653 }
1654
1655out:
1656 flash_area_close(fap);
1657 return rc;
1658}
1659#endif /* !MCUBOOT_OVERWRITE_ONLY */
1660
David Vinczebb6e3b62019-07-31 14:24:05 +02001661#if (BOOT_IMAGE_NUMBER > 1)
1662/**
David Vinczecea8b592019-10-29 16:09:51 +01001663 * Check if the version of the image is not older than required.
1664 *
1665 * @param req Required minimal image version.
1666 * @param ver Version of the image to be checked.
1667 *
1668 * @return 0 if the version is sufficient, nonzero otherwise.
1669 */
1670static int
1671boot_is_version_sufficient(struct image_version *req,
1672 struct image_version *ver)
1673{
1674 if (ver->iv_major > req->iv_major) {
1675 return 0;
1676 }
1677 if (ver->iv_major < req->iv_major) {
1678 return BOOT_EBADVERSION;
1679 }
1680 /* The major version numbers are equal. */
1681 if (ver->iv_minor > req->iv_minor) {
1682 return 0;
1683 }
1684 if (ver->iv_minor < req->iv_minor) {
1685 return BOOT_EBADVERSION;
1686 }
1687 /* The minor version numbers are equal. */
1688 if (ver->iv_revision < req->iv_revision) {
1689 return BOOT_EBADVERSION;
1690 }
1691
1692 return 0;
1693}
1694
1695/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001696 * Check the image dependency whether it is satisfied and modify
1697 * the swap type if necessary.
1698 *
1699 * @param dep Image dependency which has to be verified.
1700 *
1701 * @return 0 on success; nonzero on failure.
1702 */
1703static int
David Vinczecea8b592019-10-29 16:09:51 +01001704boot_verify_slot_dependency(struct boot_loader_state *state,
1705 struct image_dependency *dep)
David Vinczebb6e3b62019-07-31 14:24:05 +02001706{
1707 struct image_version *dep_version;
1708 size_t dep_slot;
1709 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001710 uint8_t swap_type;
David Vinczebb6e3b62019-07-31 14:24:05 +02001711
1712 /* Determine the source of the image which is the subject of
1713 * the dependency and get it's version. */
David Vinczecea8b592019-10-29 16:09:51 +01001714 swap_type = state->swap_type[dep->image_id];
1715 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczebb6e3b62019-07-31 14:24:05 +02001716 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
David Vinczecea8b592019-10-29 16:09:51 +01001717 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczebb6e3b62019-07-31 14:24:05 +02001718
1719 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1720 if (rc != 0) {
1721 /* Dependency not satisfied.
1722 * Modify the swap type to decrease the version number of the image
1723 * (which will be located in the primary slot after the boot process),
1724 * consequently the number of unsatisfied dependencies will be
1725 * decreased or remain the same.
1726 */
David Vinczecea8b592019-10-29 16:09:51 +01001727 switch (BOOT_SWAP_TYPE(state)) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001728 case BOOT_SWAP_TYPE_TEST:
1729 case BOOT_SWAP_TYPE_PERM:
David Vinczecea8b592019-10-29 16:09:51 +01001730 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczebb6e3b62019-07-31 14:24:05 +02001731 break;
1732 case BOOT_SWAP_TYPE_NONE:
David Vinczecea8b592019-10-29 16:09:51 +01001733 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczebb6e3b62019-07-31 14:24:05 +02001734 break;
1735 default:
1736 break;
1737 }
1738 }
1739
1740 return rc;
1741}
1742
1743/**
1744 * Read all dependency TLVs of an image from the flash and verify
1745 * one after another to see if they are all satisfied.
1746 *
1747 * @param slot Image slot number.
1748 *
1749 * @return 0 on success; nonzero on failure.
1750 */
1751static int
David Vinczecea8b592019-10-29 16:09:51 +01001752boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczebb6e3b62019-07-31 14:24:05 +02001753{
1754 const struct flash_area *fap;
David Vincze07706a42019-12-12 18:20:12 +01001755 struct image_tlv_iter it;
David Vinczebb6e3b62019-07-31 14:24:05 +02001756 struct image_dependency dep;
1757 uint32_t off;
David Vincze07706a42019-12-12 18:20:12 +01001758 uint16_t len;
David Vinczecea8b592019-10-29 16:09:51 +01001759 int area_id;
David Vinczebb6e3b62019-07-31 14:24:05 +02001760 int rc;
1761
David Vinczecea8b592019-10-29 16:09:51 +01001762 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1763 rc = flash_area_open(area_id, &fap);
David Vinczebb6e3b62019-07-31 14:24:05 +02001764 if (rc != 0) {
1765 rc = BOOT_EFLASH;
1766 goto done;
1767 }
1768
David Vincze07706a42019-12-12 18:20:12 +01001769 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1770 IMAGE_TLV_DEPENDENCY, true);
David Vinczebb6e3b62019-07-31 14:24:05 +02001771 if (rc != 0) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001772 goto done;
1773 }
1774
David Vincze07706a42019-12-12 18:20:12 +01001775 while (true) {
1776 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1777 if (rc < 0) {
1778 return -1;
1779 } else if (rc > 0) {
1780 rc = 0;
David Vinczebb6e3b62019-07-31 14:24:05 +02001781 break;
1782 }
David Vincze07706a42019-12-12 18:20:12 +01001783
1784 if (len != sizeof(dep)) {
1785 rc = BOOT_EBADIMAGE;
1786 goto done;
1787 }
1788
1789 rc = flash_area_read(fap, off, &dep, len);
1790 if (rc != 0) {
1791 rc = BOOT_EFLASH;
1792 goto done;
1793 }
1794
1795 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1796 rc = BOOT_EBADARGS;
1797 goto done;
1798 }
1799
1800 /* Verify dependency and modify the swap type if not satisfied. */
1801 rc = boot_verify_slot_dependency(state, &dep);
1802 if (rc != 0) {
1803 /* Dependency not satisfied. */
1804 goto done;
Tamas Ban056ed0b2019-09-16 12:48:32 +01001805 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001806 }
1807
1808done:
1809 flash_area_close(fap);
1810 return rc;
1811}
1812
1813/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001814 * Iterate over all the images and verify whether the image dependencies in the
1815 * TLV area are all satisfied and update the related swap type if necessary.
1816 */
David Vinczecea8b592019-10-29 16:09:51 +01001817static int
1818boot_verify_dependencies(struct boot_loader_state *state)
David Vinczebb6e3b62019-07-31 14:24:05 +02001819{
David Vinczebb6e3b62019-07-31 14:24:05 +02001820 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001821 uint8_t slot;
David Vinczebb6e3b62019-07-31 14:24:05 +02001822
David Vinczecea8b592019-10-29 16:09:51 +01001823 BOOT_CURR_IMG(state) = 0;
1824 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1825 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1826 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1827 slot = BOOT_SECONDARY_SLOT;
1828 } else {
1829 slot = BOOT_PRIMARY_SLOT;
1830 }
1831
1832 rc = boot_verify_slot_dependencies(state, slot);
David Vinczebb6e3b62019-07-31 14:24:05 +02001833 if (rc == 0) {
1834 /* All dependencies've been satisfied, continue with next image. */
David Vinczecea8b592019-10-29 16:09:51 +01001835 BOOT_CURR_IMG(state)++;
David Vinczebb6e3b62019-07-31 14:24:05 +02001836 } else if (rc == BOOT_EBADVERSION) {
David Vinczecea8b592019-10-29 16:09:51 +01001837 /* Cannot upgrade due to non-met dependencies, so disable all
1838 * image upgrades.
1839 */
1840 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1841 BOOT_CURR_IMG(state) = idx;
1842 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1843 }
1844 break;
David Vinczebb6e3b62019-07-31 14:24:05 +02001845 } else {
1846 /* Other error happened, images are inconsistent */
David Vinczecea8b592019-10-29 16:09:51 +01001847 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001848 }
1849 }
David Vinczecea8b592019-10-29 16:09:51 +01001850 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001851}
1852#endif /* (BOOT_IMAGE_NUMBER > 1) */
1853
Tamas Banf70ef8c2017-12-19 15:35:09 +00001854/**
David Vincze7384ee72019-07-23 17:00:42 +02001855 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001856 *
David Vincze7384ee72019-07-23 17:00:42 +02001857 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001858 *
1859 * @return 0 on success; nonzero on failure.
1860 */
1861static int
David Vinczecea8b592019-10-29 16:09:51 +01001862boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001863{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001864 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001865#ifndef MCUBOOT_OVERWRITE_ONLY
1866 uint8_t swap_type;
1867#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001868
David Vincze7384ee72019-07-23 17:00:42 +02001869 /* At this point there are no aborted swaps. */
1870#if defined(MCUBOOT_OVERWRITE_ONLY)
David Vinczecea8b592019-10-29 16:09:51 +01001871 rc = boot_copy_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001872#else
David Vinczecea8b592019-10-29 16:09:51 +01001873 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001874#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001875 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001876
1877#ifndef MCUBOOT_OVERWRITE_ONLY
1878 /* The following state needs image_ok be explicitly set after the
1879 * swap was finished to avoid a new revert.
1880 */
David Vinczecea8b592019-10-29 16:09:51 +01001881 swap_type = BOOT_SWAP_TYPE(state);
1882 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1883 swap_type == BOOT_SWAP_TYPE_PERM) {
1884 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001885 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001886 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001887 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001888 }
1889
David Vinczecea8b592019-10-29 16:09:51 +01001890 if (swap_type == BOOT_SWAP_TYPE_PERM) {
David Vincze7384ee72019-07-23 17:00:42 +02001891 /* Update the stored security counter with the new image's security
1892 * counter value. The primary slot holds the new image at this
1893 * point, but the secondary slot's image header must be passed
1894 * because the read image headers in the boot_data structure have
1895 * not been updated yet.
1896 *
1897 * In case of a permanent image swap mcuboot will never attempt to
1898 * revert the images on the next reboot. Therefore, the security
1899 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001900 */
David Vinczecea8b592019-10-29 16:09:51 +01001901 rc = boot_update_security_counter(
1902 BOOT_CURR_IMG(state),
1903 BOOT_PRIMARY_SLOT,
1904 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02001905 if (rc != 0) {
1906 BOOT_LOG_ERR("Security counter update failed after "
1907 "image upgrade.");
David Vinczecea8b592019-10-29 16:09:51 +01001908 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001909 }
1910 }
1911
David Vinczecea8b592019-10-29 16:09:51 +01001912 if (BOOT_IS_UPGRADE(swap_type)) {
1913 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001914 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001915 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001916 }
1917 }
1918#endif /* !MCUBOOT_OVERWRITE_ONLY */
1919
1920 return rc;
1921}
1922
1923/**
1924 * Completes a previously aborted image swap.
1925 *
1926 * @param bs The current boot status.
1927 *
1928 * @return 0 on success; nonzero on failure.
1929 */
1930#if !defined(MCUBOOT_OVERWRITE_ONLY)
1931static int
David Vinczecea8b592019-10-29 16:09:51 +01001932boot_complete_partial_swap(struct boot_loader_state *state,
1933 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02001934{
1935 int rc;
1936
1937 /* Determine the type of swap operation being resumed from the
1938 * `swap-type` trailer field.
1939 */
David Vinczecea8b592019-10-29 16:09:51 +01001940 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001941 assert(rc == 0);
1942
David Vinczecea8b592019-10-29 16:09:51 +01001943 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02001944
1945 /* The following states need image_ok be explicitly set after the
1946 * swap was finished to avoid a new revert.
1947 */
1948 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1949 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
David Vinczecea8b592019-10-29 16:09:51 +01001950 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001951 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001952 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001953 }
1954 }
1955
David Vinczecea8b592019-10-29 16:09:51 +01001956 if (BOOT_IS_UPGRADE(bs->swap_type)) {
1957 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001958 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001959 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001960 }
1961 }
1962
David Vinczecea8b592019-10-29 16:09:51 +01001963 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02001964 BOOT_LOG_ERR("panic!");
1965 assert(0);
1966
1967 /* Loop forever... */
1968 while (1) {}
1969 }
1970
1971 return rc;
1972}
1973#endif /* !MCUBOOT_OVERWRITE_ONLY */
1974
1975#if (BOOT_IMAGE_NUMBER > 1)
1976/**
1977 * Review the validity of previously determined swap types of other images.
1978 *
1979 * @param aborted_swap The current image upgrade is a
1980 * partial/aborted swap.
1981 */
1982static void
David Vinczecea8b592019-10-29 16:09:51 +01001983boot_review_image_swap_types(struct boot_loader_state *state,
1984 bool aborted_swap)
David Vincze7384ee72019-07-23 17:00:42 +02001985{
1986 /* In that case if we rebooted in the middle of an image upgrade process, we
1987 * must review the validity of swap types, that were previously determined
1988 * for other images. The image_ok flag had not been set before the reboot
1989 * for any of the updated images (only the copy_done flag) and thus falsely
1990 * the REVERT swap type has been determined for the previous images that had
1991 * been updated before the reboot.
1992 *
1993 * There are two separate scenarios that we have to deal with:
1994 *
1995 * 1. The reboot has happened during swapping an image:
1996 * The current image upgrade has been determined as a
1997 * partial/aborted swap.
1998 * 2. The reboot has happened between two separate image upgrades:
1999 * In this scenario we must check the swap type of the current image.
2000 * In those cases if it is NONE or REVERT we cannot certainly determine
2001 * the fact of a reboot. In a consistent state images must move in the
2002 * same direction or stay in place, e.g. in practice REVERT and TEST
2003 * swap types cannot be present at the same time. If the swap type of
2004 * the current image is either TEST, PERM or FAIL we must review the
2005 * already determined swap types of other images and set each false
2006 * REVERT swap types to NONE (these images had been successfully
2007 * updated before the system rebooted between two separate image
2008 * upgrades).
2009 */
2010
David Vinczecea8b592019-10-29 16:09:51 +01002011 if (BOOT_CURR_IMG(state) == 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002012 /* Nothing to do */
2013 return;
2014 }
2015
2016 if (!aborted_swap) {
David Vinczecea8b592019-10-29 16:09:51 +01002017 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2018 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vincze7384ee72019-07-23 17:00:42 +02002019 /* Nothing to do */
2020 return;
2021 }
2022 }
2023
David Vinczecea8b592019-10-29 16:09:51 +01002024 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2025 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2026 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002027 }
2028 }
2029}
2030#endif
2031
2032/**
2033 * Prepare image to be updated if required.
2034 *
2035 * Prepare image to be updated if required with completing an image swap
2036 * operation if one was aborted and/or determining the type of the
2037 * swap operation. In case of any error set the swap type to NONE.
2038 *
David Vinczecea8b592019-10-29 16:09:51 +01002039 * @param state Boot loader status information.
David Vincze7384ee72019-07-23 17:00:42 +02002040 * @param bs Pointer where the read and possibly updated
2041 * boot status can be written to.
2042 */
2043static void
David Vinczecea8b592019-10-29 16:09:51 +01002044boot_prepare_image_for_update(struct boot_loader_state *state,
2045 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02002046{
2047 int rc;
2048
2049 /* Determine the sector layout of the image slots and scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01002050 rc = boot_read_sectors(state);
David Vincze7384ee72019-07-23 17:00:42 +02002051 if (rc != 0) {
2052 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2053 " - too small?", BOOT_MAX_IMG_SECTORS);
2054 /* Unable to determine sector layout, continue with next image
2055 * if there is one.
2056 */
David Vinczecea8b592019-10-29 16:09:51 +01002057 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002058 return;
2059 }
2060
2061 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002062 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002063 if (rc != 0) {
2064 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002065 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
2066 BOOT_CURR_IMG(state));
2067 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002068 return;
2069 }
2070
2071 /* If the current image's slots aren't compatible, no swap is possible.
2072 * Just boot into primary slot.
2073 */
David Vinczecea8b592019-10-29 16:09:51 +01002074 if (boot_slots_compatible(state)) {
2075 rc = boot_read_status(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002076 if (rc != 0) {
2077 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
David Vinczecea8b592019-10-29 16:09:51 +01002078 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002079 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002080 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002081 return;
2082 }
2083
2084 /* Determine if we rebooted in the middle of an image swap
2085 * operation. If a partial swap was detected, complete it.
2086 */
2087 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2088
2089#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002090 boot_review_image_swap_types(state, true);
David Vincze7384ee72019-07-23 17:00:42 +02002091#endif
2092
2093#ifdef MCUBOOT_OVERWRITE_ONLY
2094 /* Should never arrive here, overwrite-only mode has
2095 * no swap state.
2096 */
2097 assert(0);
2098#else
2099 /* Determine the type of swap operation being resumed from the
2100 * `swap-type` trailer field.
2101 */
David Vinczecea8b592019-10-29 16:09:51 +01002102 rc = boot_complete_partial_swap(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002103 assert(rc == 0);
2104#endif
2105 /* Attempt to read an image header from each slot. Ensure that
2106 * image headers in slots are aligned with headers in boot_data.
2107 */
David Vinczecea8b592019-10-29 16:09:51 +01002108 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002109 assert(rc == 0);
2110
2111 /* Swap has finished set to NONE */
David Vinczecea8b592019-10-29 16:09:51 +01002112 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002113 } else {
2114 /* There was no partial swap, determine swap type. */
2115 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01002116 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2117 } else if (boot_validate_slot(state,
2118 BOOT_SECONDARY_SLOT, bs) != 0) {
2119 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vincze7384ee72019-07-23 17:00:42 +02002120 } else {
David Vinczecea8b592019-10-29 16:09:51 +01002121 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02002122 }
2123
2124#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002125 boot_review_image_swap_types(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002126#endif
2127 }
2128 } else {
2129 /* In that case if slots are not compatible. */
David Vinczecea8b592019-10-29 16:09:51 +01002130 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002131 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002132}
2133
2134/**
2135 * Prepares the booting process. This function moves images around in flash as
2136 * appropriate, and tells you what address to boot from.
2137 *
David Vinczecea8b592019-10-29 16:09:51 +01002138 * @param state Boot loader status information.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002139 * @param rsp On success, indicates how booting should occur.
2140 *
2141 * @return 0 on success; nonzero on failure.
2142 */
2143int
David Vinczecea8b592019-10-29 16:09:51 +01002144context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Banf70ef8c2017-12-19 15:35:09 +00002145{
Tamas Banf70ef8c2017-12-19 15:35:09 +00002146 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02002147 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01002148 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002149 int fa_id;
David Vinczecea8b592019-10-29 16:09:51 +01002150 int image_index;
2151 bool has_upgrade;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002152
2153 /* The array of slot sectors are defined here (as opposed to file scope) so
2154 * that they don't get allocated for non-boot-loader apps. This is
2155 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2156 * any effect in older gcc versions (e.g., 4.8.4).
2157 */
David Vincze7384ee72019-07-23 17:00:42 +02002158 static boot_sector_t
2159 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2160 static boot_sector_t
2161 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02002162 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00002163
David Vincze7384ee72019-07-23 17:00:42 +02002164 /* Iterate over all the images. By the end of the loop the swap type has
2165 * to be determined for each image and all aborted swaps have to be
2166 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002167 */
David Vinczecea8b592019-10-29 16:09:51 +01002168 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2169
2170 image_index = BOOT_CURR_IMG(state);
2171
2172 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
2173 primary_slot_sectors[image_index];
2174 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
2175 secondary_slot_sectors[image_index];
2176 state->scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002177
David Vincze7384ee72019-07-23 17:00:42 +02002178 /* Open primary and secondary image areas for the duration
2179 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002180 */
David Vincze7384ee72019-07-23 17:00:42 +02002181 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002182 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
2183 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vincze7384ee72019-07-23 17:00:42 +02002184 assert(rc == 0);
2185 }
2186 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
David Vinczecea8b592019-10-29 16:09:51 +01002187 &BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002188 assert(rc == 0);
2189
2190 /* Determine swap type and complete swap if it has been aborted. */
David Vinczecea8b592019-10-29 16:09:51 +01002191 boot_prepare_image_for_update(state, &bs);
2192
2193 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
2194 has_upgrade = true;
2195 }
David Vincze7384ee72019-07-23 17:00:42 +02002196 }
2197
David Vinczebb6e3b62019-07-31 14:24:05 +02002198#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002199 if (has_upgrade) {
2200 /* Iterate over all the images and verify whether the image dependencies
2201 * are all satisfied and update swap type if necessary.
2202 */
2203 rc = boot_verify_dependencies(state);
2204 if (rc == BOOT_EBADVERSION) {
2205 /*
2206 * It was impossible to upgrade because the expected dependency
2207 * version was not available. Here we already changed the swap_type
2208 * so that instead of asserting the bootloader, we continue and no
2209 * upgrade is performed.
2210 */
2211 rc = 0;
2212 }
2213 }
David Vinczebb6e3b62019-07-31 14:24:05 +02002214#endif
2215
David Vincze7384ee72019-07-23 17:00:42 +02002216 /* Iterate over all the images. At this point there are no aborted swaps
2217 * and the swap types are determined for each image. By the end of the loop
2218 * all required update operations will have been finished.
2219 */
David Vinczecea8b592019-10-29 16:09:51 +01002220 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002221
2222#if (BOOT_IMAGE_NUMBER > 1)
2223 /* Indicate that swap is not aborted */
2224 memset(&bs, 0, sizeof bs);
2225 bs.idx = BOOT_STATUS_IDX_0;
2226 bs.state = BOOT_STATUS_STATE_0;
2227#endif /* (BOOT_IMAGE_NUMBER > 1) */
2228
2229 /* Set the previously determined swap type */
David Vinczecea8b592019-10-29 16:09:51 +01002230 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vincze7384ee72019-07-23 17:00:42 +02002231
David Vinczecea8b592019-10-29 16:09:51 +01002232 switch (BOOT_SWAP_TYPE(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002233 case BOOT_SWAP_TYPE_NONE:
2234 break;
2235
2236 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2237 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2238 case BOOT_SWAP_TYPE_REVERT:
David Vinczecea8b592019-10-29 16:09:51 +01002239 rc = boot_perform_update(state, &bs);
David Vincze7384ee72019-07-23 17:00:42 +02002240 assert(rc == 0);
2241 break;
2242
2243 case BOOT_SWAP_TYPE_FAIL:
2244 /* The image in secondary slot was invalid and is now erased. Ensure
2245 * we don't try to boot into it again on the next reboot. Do this by
2246 * pretending we just reverted back to primary slot.
2247 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002248#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002249 /* image_ok needs to be explicitly set to avoid a new revert. */
David Vinczecea8b592019-10-29 16:09:51 +01002250 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00002251 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002252 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002253 }
2254#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002255 break;
2256
2257 default:
David Vinczecea8b592019-10-29 16:09:51 +01002258 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002259 }
David Vincze7384ee72019-07-23 17:00:42 +02002260
David Vinczecea8b592019-10-29 16:09:51 +01002261 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02002262 BOOT_LOG_ERR("panic!");
2263 assert(0);
2264
2265 /* Loop forever... */
2266 while (1) {}
2267 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002268 }
2269
David Vincze7384ee72019-07-23 17:00:42 +02002270 /* Iterate over all the images. At this point all required update operations
2271 * have finished. By the end of the loop each image in the primary slot will
2272 * have been re-validated.
2273 */
David Vinczecea8b592019-10-29 16:09:51 +01002274 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2275 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vincze7384ee72019-07-23 17:00:42 +02002276 /* Attempt to read an image header from each slot. Ensure that image
2277 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002278 */
David Vinczecea8b592019-10-29 16:09:51 +01002279 rc = boot_read_image_headers(state, false);
David Vincze060968d2019-05-23 01:13:14 +02002280 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002281 goto out;
2282 }
2283 /* Since headers were reloaded, it can be assumed we just performed
2284 * a swap or overwrite. Now the header info that should be used to
2285 * provide the data for the bootstrap, which previously was at
2286 * secondary slot, was updated to primary slot.
2287 */
2288 }
2289
2290#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vinczecea8b592019-10-29 16:09:51 +01002291 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vincze7384ee72019-07-23 17:00:42 +02002292 if (rc != 0) {
2293 rc = BOOT_EBADIMAGE;
2294 goto out;
2295 }
2296#else
2297 /* Even if we're not re-validating the primary slot, we could be booting
2298 * onto an empty flash chip. At least do a basic sanity check that
2299 * the magic number on the image is OK.
2300 */
David Vinczecea8b592019-10-29 16:09:51 +01002301 if (!BOOT_IMG_HDR_IS_VALID(state, BOOT_PRIMARY_SLOT)) {
2302 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2303 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_magic,
2304 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002305 rc = BOOT_EBADIMAGE;
2306 goto out;
2307 }
2308#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2309
2310 /* Update the stored security counter with the active image's security
2311 * counter value. It will be updated only if the new security counter is
2312 * greater than the stored value.
2313 *
2314 * In case of a successful image swapping when the swap type is TEST the
2315 * security counter can be increased only after a reset, when the swap
2316 * type is NONE and the image has marked itself "OK" (the image_ok flag
2317 * has been set). This way a "revert" swap can be performed if it's
2318 * necessary.
2319 */
David Vinczecea8b592019-10-29 16:09:51 +01002320 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2321 rc = boot_update_security_counter(
2322 BOOT_CURR_IMG(state),
2323 BOOT_PRIMARY_SLOT,
2324 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02002325 if (rc != 0) {
2326 BOOT_LOG_ERR("Security counter update failed after image "
2327 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002328 goto out;
2329 }
2330 }
2331
David Vincze7384ee72019-07-23 17:00:42 +02002332 /* Save boot status to shared memory area */
2333#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002334 rc = boot_save_boot_status((BOOT_CURR_IMG(state) == 0) ?
2335 SW_SPE : SW_NSPE,
2336 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2337 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002338 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002339#else
David Vincze7384ee72019-07-23 17:00:42 +02002340 rc = boot_save_boot_status(SW_S_NS,
David Vinczecea8b592019-10-29 16:09:51 +01002341 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2342 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002343 );
2344#endif
2345 if (rc) {
2346 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
David Vinczecea8b592019-10-29 16:09:51 +01002347 BOOT_CURR_IMG(state));
David Vincze060968d2019-05-23 01:13:14 +02002348 }
2349 }
2350
David Vinczecea8b592019-10-29 16:09:51 +01002351#if (BOOT_IMAGE_NUMBER > 1)
David Vincze7384ee72019-07-23 17:00:42 +02002352 /* Always boot from the primary slot of Image 0. */
David Vinczecea8b592019-10-29 16:09:51 +01002353 BOOT_CURR_IMG(state) = 0;
2354#endif
Tamas Ban0e8ab302019-01-17 11:45:31 +00002355
David Vinczecea8b592019-10-29 16:09:51 +01002356 rsp->br_flash_dev_id =
2357 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2358 rsp->br_image_off =
2359 boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2360 rsp->br_hdr =
2361 boot_img_hdr(state, BOOT_PRIMARY_SLOT);
2362
2363out:
2364 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2365 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002366 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002367 flash_area_close(BOOT_IMG_AREA(state,
David Vincze7384ee72019-07-23 17:00:42 +02002368 BOOT_NUM_SLOTS - 1 - slot));
2369 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002370 }
2371 return rc;
2372}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002373
Oliver Swedef9982442018-08-24 18:37:44 +01002374#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002375
David Vinczedcba70b2019-05-28 12:02:52 +02002376#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2377 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2378 (area), \
2379 (hdr)->ih_ver.iv_major, \
2380 (hdr)->ih_ver.iv_minor, \
2381 (hdr)->ih_ver.iv_revision, \
2382 (hdr)->ih_ver.iv_build_num, \
2383 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2384 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2385 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002386 (state)->image_ok)
2387
2388struct image_slot_version {
2389 uint64_t version;
2390 uint32_t slot_number;
2391};
2392
2393/**
2394 * Extract the version number from the image header. This function must be
2395 * ported if version number format has changed in the image header.
2396 *
2397 * @param hdr Pointer to an image header structure
2398 *
Oliver Swedef9982442018-08-24 18:37:44 +01002399 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002400 */
2401static uint64_t
2402boot_get_version_number(struct image_header *hdr)
2403{
Oliver Swedef9982442018-08-24 18:37:44 +01002404 uint64_t version = 0;
2405 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2406 + IMAGE_VER_REVISION_LENGTH
2407 + IMAGE_VER_BUILD_NUM_LENGTH);
2408 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2409 + IMAGE_VER_BUILD_NUM_LENGTH);
2410 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2411 version |= hdr->ih_ver.iv_build_num;
2412 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002413}
2414
2415/**
2416 * Comparator function for `qsort` to compare version numbers. This function
2417 * must be ported if version number format has changed in the image header.
2418 *
2419 * @param ver1 Pointer to an array element which holds the version number
2420 * @param ver2 Pointer to another array element which holds the version
2421 * number
2422 *
2423 * @return if version1 > version2 -1
2424 * if version1 == version2 0
2425 * if version1 < version2 1
2426 */
2427static int
2428boot_compare_version_numbers(const void *ver1, const void *ver2)
2429{
2430 if (((struct image_slot_version *)ver1)->version <
2431 ((struct image_slot_version *)ver2)->version) {
2432 return 1;
2433 }
2434
2435 if (((struct image_slot_version *)ver1)->version ==
2436 ((struct image_slot_version *)ver2)->version) {
2437 return 0;
2438 }
2439
2440 return -1;
2441}
2442
2443/**
2444 * Sort the available images based on the version number and puts them in
2445 * a list.
2446 *
David Vinczecea8b592019-10-29 16:09:51 +01002447 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002448 * @param boot_sequence A pointer to an array, whose aim is to carry
2449 * the boot order of candidate images.
2450 * @param slot_cnt The number of flash areas, which can contains firmware
2451 * images.
2452 *
2453 * @return The number of valid images.
2454 */
2455uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01002456boot_get_boot_sequence(struct boot_loader_state *state,
2457 uint32_t *boot_sequence, uint32_t slot_cnt)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002458{
2459 struct boot_swap_state slot_state;
2460 struct image_header *hdr;
2461 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2462 uint32_t image_cnt = 0;
2463 uint32_t slot;
2464 int32_t rc;
2465 int32_t fa_id;
2466
2467 for (slot = 0; slot < slot_cnt; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002468 hdr = boot_img_hdr(state, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002469 fa_id = flash_area_id_from_image_slot(slot);
2470 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2471 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002472 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2473 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002474 continue;
2475 }
2476
David Vinczecea8b592019-10-29 16:09:51 +01002477 if (BOOT_IMG_HDR_IS_VALID(state, slot)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002478 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002479 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002480 /* Valid cases:
2481 * - Test mode: magic is OK in image trailer
2482 * - Permanent mode: image_ok flag has previously set
2483 */
2484 image_versions[slot].slot_number = slot;
2485 image_versions[slot].version = boot_get_version_number(hdr);
2486 image_cnt++;
2487 }
2488
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002489 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2490 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002491 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002492 }
2493 }
2494
2495 /* Sort the images based on version number */
2496 qsort(&image_versions[0],
2497 slot_cnt,
2498 sizeof(struct image_slot_version),
2499 boot_compare_version_numbers);
2500
2501 /* Copy the calculated boot sequence to boot_sequence array */
2502 for (slot = 0; slot < slot_cnt; slot++) {
2503 boot_sequence[slot] = image_versions[slot].slot_number;
2504 }
2505
2506 return image_cnt;
2507}
2508
Oliver Swedef9982442018-08-24 18:37:44 +01002509#ifdef MCUBOOT_RAM_LOADING
Raef Colesaf082382019-10-01 11:10:33 +01002510
2511/**
2512 * Verifies that the image in a slot lies within the predefined bounds that are
2513 * allowed to be used by executable images.
2514 *
2515 * @param img_dst The address to which the image is going to be copied.
2516 *
2517 * @param img_sz The size of the image.
2518 *
2519 * @return 0 on success; nonzero on failure.
2520 */
2521static int
2522boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz)
2523{
2524 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2525 return BOOT_EBADIMAGE;
2526 }
2527
2528 if (boot_add_uint32_overflow_check(img_dst, img_sz)) {
2529 return BOOT_EBADIMAGE;
2530 }
2531
2532 if (img_dst + img_sz > IMAGE_EXECUTABLE_RAM_START +
2533 IMAGE_EXECUTABLE_RAM_SIZE) {
2534 return BOOT_EBADIMAGE;
2535 }
2536
2537 return 0;
2538}
2539
Oliver Swedef9982442018-08-24 18:37:44 +01002540/**
2541 * Copies an image from a slot in the flash to an SRAM address, where the load
2542 * address has already been inserted into the image header by this point and is
2543 * extracted from it within this method. The copying is done sector-by-sector.
2544 *
David Vinczecea8b592019-10-29 16:09:51 +01002545 * @param state Boot loader status information.
Oliver Swedef9982442018-08-24 18:37:44 +01002546 * @param slot The flash slot of the image to be copied to SRAM.
2547 *
2548 * @param hdr Pointer to the image header structure of the image
Raef Colesaf082382019-10-01 11:10:33 +01002549 *
2550 * @param img_dst The address at which the image needs to be copied to
2551 * SRAM.
2552 *
2553 * @param img_sz The size of the image that needs to be copied to SRAM.
Oliver Swedef9982442018-08-24 18:37:44 +01002554 *
2555 * @return 0 on success; nonzero on failure.
2556 */
2557static int
David Vinczecea8b592019-10-29 16:09:51 +01002558boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2559 struct image_header *hdr,
Raef Colesaf082382019-10-01 11:10:33 +01002560 uint32_t img_dst, uint32_t img_sz)
Oliver Swedef9982442018-08-24 18:37:44 +01002561{
2562 int rc;
2563 uint32_t sect_sz;
2564 uint32_t sect = 0;
2565 uint32_t bytes_copied = 0;
2566 const struct flash_area *fap_src = NULL;
Oliver Swedef9982442018-08-24 18:37:44 +01002567
Raef Colesaf082382019-10-01 11:10:33 +01002568 if (img_dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002569 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002570 "- the load address must be aligned with 4 bytes due to SRAM "
Raef Colesaf082382019-10-01 11:10:33 +01002571 "restrictions", img_dst);
Oliver Swedef9982442018-08-24 18:37:44 +01002572 return BOOT_EBADARGS;
2573 }
2574
2575 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2576 if (rc != 0) {
2577 return BOOT_EFLASH;
2578 }
2579
Oliver Swedef9982442018-08-24 18:37:44 +01002580 while (bytes_copied < img_sz) {
David Vinczecea8b592019-10-29 16:09:51 +01002581 sect_sz = boot_img_sector_size(state, slot, sect);
Oliver Swedef9982442018-08-24 18:37:44 +01002582 /*
2583 * Direct copy from where the image sector resides in flash to its new
2584 * location in SRAM
2585 */
2586 rc = flash_area_read(fap_src,
2587 bytes_copied,
Raef Colesaf082382019-10-01 11:10:33 +01002588 (void *)(img_dst + bytes_copied),
Oliver Swedef9982442018-08-24 18:37:44 +01002589 sect_sz);
2590 if (rc != 0) {
2591 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2592 break;
2593 } else {
2594 bytes_copied += sect_sz;
2595 }
2596 sect++;
2597 }
2598
2599 if (fap_src) {
2600 flash_area_close(fap_src);
2601 }
2602 return rc;
2603}
Raef Coles27a61452019-09-25 15:32:25 +01002604
2605/**
2606 * Removes an image from SRAM, by overwriting it with zeros.
2607 *
Raef Colesaf082382019-10-01 11:10:33 +01002608 * @param img_dst The address of the image that needs to be removed from
2609 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002610 *
Raef Colesaf082382019-10-01 11:10:33 +01002611 * @param img_sz The size of the image that needs to be removed from
2612 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002613 *
2614 * @return 0 on success; nonzero on failure.
2615 */
2616static int
Raef Colesaf082382019-10-01 11:10:33 +01002617boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
Raef Coles27a61452019-09-25 15:32:25 +01002618{
Raef Colesaf082382019-10-01 11:10:33 +01002619 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2620 memset((void*)img_dst, 0, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002621
2622 return 0;
2623}
Oliver Swedef9982442018-08-24 18:37:44 +01002624#endif /* MCUBOOT_RAM_LOADING */
2625
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002626/**
2627 * Prepares the booting process. This function choose the newer image in flash
David Vinczecea8b592019-10-29 16:09:51 +01002628 * as appropriate, and tells you what address to boot from.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002629 *
David Vinczecea8b592019-10-29 16:09:51 +01002630 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002631 * @param rsp On success, indicates how booting should occur.
2632 *
2633 * @return 0 on success; nonzero on failure.
2634 */
2635int
David Vinczecea8b592019-10-29 16:09:51 +01002636context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002637{
2638 size_t slot = 0;
2639 int32_t i;
2640 int rc;
2641 int fa_id;
2642 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2643 uint32_t img_cnt;
Raef Coles27a61452019-09-25 15:32:25 +01002644 struct image_header *selected_image_header;
2645#ifdef MCUBOOT_RAM_LOADING
2646 int image_copied = 0;
Raef Colesaf082382019-10-01 11:10:33 +01002647 uint32_t img_dst = 0;
2648 uint32_t img_sz = 0;
Raef Coles27a61452019-09-25 15:32:25 +01002649#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002650
David Vincze8bdfc2d2019-03-18 15:49:23 +01002651 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2652 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002653
David Vinczecea8b592019-10-29 16:09:51 +01002654 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = &primary_slot_sectors[0];
2655 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002656
2657 /* Open boot_data image areas for the duration of this call. */
2658 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2659 fa_id = flash_area_id_from_image_slot(i);
David Vinczecea8b592019-10-29 16:09:51 +01002660 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002661 assert(rc == 0);
2662 }
2663
2664 /* Determine the sector layout of the image slots. */
David Vinczecea8b592019-10-29 16:09:51 +01002665 rc = boot_read_sectors(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002666 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002667 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - "
2668 "too small?", BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002669 goto out;
2670 }
2671
2672 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002673 rc = boot_read_image_headers(state, false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002674 if (rc != 0) {
2675 goto out;
2676 }
2677
David Vinczecea8b592019-10-29 16:09:51 +01002678 img_cnt = boot_get_boot_sequence(state, boot_sequence, BOOT_NUM_SLOTS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002679 if (img_cnt) {
2680 /* Authenticate images */
2681 for (i = 0; i < img_cnt; i++) {
Raef Coles27a61452019-09-25 15:32:25 +01002682
2683 slot = boot_sequence[i];
David Vinczecea8b592019-10-29 16:09:51 +01002684 selected_image_header = boot_img_hdr(state, slot);
Raef Coles27a61452019-09-25 15:32:25 +01002685
2686#ifdef MCUBOOT_RAM_LOADING
2687 if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
Raef Colesaf082382019-10-01 11:10:33 +01002688
2689 img_dst = selected_image_header->ih_load_addr;
2690
David Vinczecea8b592019-10-29 16:09:51 +01002691 rc = boot_read_image_size(state, slot, &img_sz);
Raef Colesaf082382019-10-01 11:10:33 +01002692 if (rc != 0) {
2693 rc = BOOT_EFLASH;
2694 BOOT_LOG_INF("Could not load image headers from the image"
2695 "in the %s slot.",
2696 (slot == BOOT_PRIMARY_SLOT) ?
2697 "primary" : "secondary");
2698 continue;
2699 }
2700
2701 rc = boot_verify_ram_loading_address(img_dst, img_sz);
2702 if (rc != 0) {
2703 BOOT_LOG_INF("Could not copy image from the %s slot in "
2704 "the Flash to load address 0x%x in SRAM as"
2705 " the image would overlap memory outside"
2706 " the defined executable region.",
2707 (slot == BOOT_PRIMARY_SLOT) ?
2708 "primary" : "secondary",
2709 selected_image_header->ih_load_addr);
2710 continue;
2711 }
2712
Raef Coles27a61452019-09-25 15:32:25 +01002713 /* Copy image to the load address from where it
2714 * currently resides in flash
2715 */
David Vinczecea8b592019-10-29 16:09:51 +01002716 rc = boot_copy_image_to_sram(state, slot, selected_image_header,
Raef Colesaf082382019-10-01 11:10:33 +01002717 img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002718 if (rc != 0) {
2719 rc = BOOT_EBADIMAGE;
2720 BOOT_LOG_INF("Could not copy image from the %s slot in "
2721 "the Flash to load address 0x%x in SRAM, "
2722 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2723 "primary" : "secondary",
2724 selected_image_header->ih_load_addr);
2725 continue;
2726 } else {
2727 BOOT_LOG_INF("Image has been copied from the %s slot in "
2728 "the flash to SRAM address 0x%x",
2729 (slot == BOOT_PRIMARY_SLOT) ?
2730 "primary" : "secondary",
2731 selected_image_header->ih_load_addr);
2732 image_copied = 1;
2733 }
2734 } else {
2735 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2736 * MCUBOOT_RAM_LOADING is set.
2737 */
2738 rc = BOOT_EBADIMAGE;
2739 continue;
2740 }
2741#endif /* MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002742 rc = boot_validate_slot(state, slot, NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002743 if (rc == 0) {
Raef Coles27a61452019-09-25 15:32:25 +01002744 /* If a valid image is found then there is no reason to check
2745 * the rest of the images, as they were already ordered by
2746 * preference.
2747 */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002748 break;
2749 }
Raef Coles27a61452019-09-25 15:32:25 +01002750#ifdef MCUBOOT_RAM_LOADING
2751 else if (image_copied) {
2752 /* If an image is found to be invalid then it is removed from
2753 * RAM to prevent it being a shellcode vector.
2754 */
Raef Colesaf082382019-10-01 11:10:33 +01002755 boot_remove_image_from_sram(img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002756 image_copied = 0;
2757 }
2758#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002759 }
2760 if (rc) {
2761 /* If there was no valid image at all */
2762 rc = BOOT_EBADIMAGE;
2763 goto out;
2764 }
2765
David Vincze060968d2019-05-23 01:13:14 +02002766 /* Update the security counter with the newest image's security
2767 * counter value.
2768 */
David Vinczecea8b592019-10-29 16:09:51 +01002769 rc = boot_update_security_counter(BOOT_CURR_IMG(state), slot,
2770 selected_image_header);
David Vincze060968d2019-05-23 01:13:14 +02002771 if (rc != 0) {
2772 BOOT_LOG_ERR("Security counter update failed after image "
2773 "validation.");
2774 goto out;
2775 }
2776
Oliver Swedef9982442018-08-24 18:37:44 +01002777
David Vincze8a2a4e22019-05-24 10:14:23 +02002778#ifdef MCUBOOT_RAM_LOADING
Raef Coles27a61452019-09-25 15:32:25 +01002779 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
2780 selected_image_header->ih_load_addr);
2781#else
2782 BOOT_LOG_INF("Booting image from the %s slot",
2783 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2784#endif /* MCUBOOT_RAM_LOADING */
Oliver Swedef9982442018-08-24 18:37:44 +01002785
Raef Coles27a61452019-09-25 15:32:25 +01002786 rsp->br_hdr = selected_image_header;
David Vinczecea8b592019-10-29 16:09:51 +01002787 rsp->br_image_off = boot_img_slot_off(state, slot);
2788 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002789 } else {
2790 /* No candidate image available */
2791 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002792 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002793 }
2794
Tamas Ban0e8ab302019-01-17 11:45:31 +00002795 /* Save boot status to shared memory area */
2796 rc = boot_save_boot_status(SW_S_NS,
2797 rsp->br_hdr,
David Vinczecea8b592019-10-29 16:09:51 +01002798 BOOT_IMG_AREA(state, slot));
Tamas Ban0e8ab302019-01-17 11:45:31 +00002799 if (rc) {
2800 BOOT_LOG_ERR("Failed to add data to shared area");
2801 }
2802
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002803out:
2804 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002805 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002806 }
2807 return rc;
2808}
Oliver Swedef9982442018-08-24 18:37:44 +01002809#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002810
2811int
2812boot_go(struct boot_rsp *rsp)
2813{
2814 return context_boot_go(&boot_data, rsp);
2815}