blob: ab25e43fcee6bceaa899935032f23ac3ebc998bb [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 Vincze61bd1e52019-10-24 16:47:31 +020023 * Git SHA of the original version: 510fddb8e06d76e2442b2a4603d3e1cbefe28be4
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;
David Vincze61bd1e52019-10-24 16:47:31 +0200548 uint32_t protect_tlv_size;
Oliver Swedef9982442018-08-24 18:37:44 +0100549 int area_id;
550 int rc;
551
David Vinczecea8b592019-10-29 16:09:51 +0100552#if (BOOT_IMAGE_NUMBER == 1)
553 (void)state;
554#endif
555
556 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Oliver Swedef9982442018-08-24 18:37:44 +0100557 rc = flash_area_open(area_id, &fap);
558 if (rc != 0) {
559 rc = BOOT_EFLASH;
560 goto done;
561 }
562
David Vincze07706a42019-12-12 18:20:12 +0100563 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
564
565 if (flash_area_read(fap, off, &info, sizeof(info))) {
566 rc = BOOT_EFLASH;
Oliver Swedef9982442018-08-24 18:37:44 +0100567 goto done;
568 }
David Vincze07706a42019-12-12 18:20:12 +0100569
David Vincze61bd1e52019-10-24 16:47:31 +0200570 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
571 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
572 if (protect_tlv_size != info.it_tlv_tot) {
573 rc = BOOT_EBADIMAGE;
574 goto done;
575 }
576
577 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
578 rc = BOOT_EFLASH;
579 goto done;
580 }
581 } else if (protect_tlv_size != 0) {
582 rc = BOOT_EBADIMAGE;
583 goto done;
584 }
585
David Vincze07706a42019-12-12 18:20:12 +0100586 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
587 rc = BOOT_EBADIMAGE;
588 goto done;
589 }
590
David Vincze61bd1e52019-10-24 16:47:31 +0200591 *size = off + protect_tlv_size + info.it_tlv_tot;
Oliver Swedef9982442018-08-24 18:37:44 +0100592 rc = 0;
593
594done:
595 flash_area_close(fap);
596 return rc;
597}
598#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
599
600#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000601/**
Tamas Ban581034a2017-12-19 19:54:37 +0000602 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000603 * status is necessary for completing a swap that was interrupted by a boot
604 * loader reset.
605 *
David Vincze401c7422019-06-21 20:44:05 +0200606 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
607 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000608 */
609static int
David Vinczecea8b592019-10-29 16:09:51 +0100610boot_status_source(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000611{
612 const struct boot_status_table *table;
613 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100614 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000615 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200616 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000617 uint8_t source;
David Vinczecea8b592019-10-29 16:09:51 +0100618 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000619
David Vinczecea8b592019-10-29 16:09:51 +0100620#if (BOOT_IMAGE_NUMBER == 1)
621 (void)state;
622#endif
623
624 image_index = BOOT_CURR_IMG(state);
625 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
626 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000627 assert(rc == 0);
628
629 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
630 assert(rc == 0);
631
David Vincze401c7422019-06-21 20:44:05 +0200632 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000633 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
634
635 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
636 table = &boot_status_tables[i];
637
David Vincze401c7422019-06-21 20:44:05 +0200638 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
639 state_primary_slot.magic) &&
640 boot_magic_compatible_check(table->bst_magic_scratch,
641 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100642 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
643 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
644 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000645 source = table->bst_status_source;
David Vincze7384ee72019-07-23 17:00:42 +0200646
647#if (BOOT_IMAGE_NUMBER > 1)
648 /* In case of multi-image boot it can happen that if boot status
649 * info is found on scratch area then it does not belong to the
650 * currently examined image.
651 */
652 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
David Vinczecea8b592019-10-29 16:09:51 +0100653 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +0200654 source = BOOT_STATUS_SOURCE_NONE;
655 }
656#endif
657
Tamas Banf70ef8c2017-12-19 15:35:09 +0000658 BOOT_LOG_INF("Boot source: %s",
659 source == BOOT_STATUS_SOURCE_NONE ? "none" :
660 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100661 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
662 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000663 return source;
664 }
665 }
666
667 BOOT_LOG_INF("Boot source: none");
668 return BOOT_STATUS_SOURCE_NONE;
669}
670
David Vincze401c7422019-06-21 20:44:05 +0200671/*
672 * Slots are compatible when all sectors that store upto to size of the image
673 * round up to sector size, in both slot's are able to fit in the scratch
674 * area, and have sizes that are a multiple of each other (powers of two
675 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000676 */
677static int
David Vinczecea8b592019-10-29 16:09:51 +0100678boot_slots_compatible(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000679{
David Vincze401c7422019-06-21 20:44:05 +0200680 size_t num_sectors_primary;
681 size_t num_sectors_secondary;
682 size_t sz0, sz1;
683 size_t primary_slot_sz, secondary_slot_sz;
684 size_t scratch_sz;
685 size_t i, j;
686 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000687
David Vinczecea8b592019-10-29 16:09:51 +0100688 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
689 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze401c7422019-06-21 20:44:05 +0200690 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
691 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200692 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000693 return 0;
694 }
David Vincze39e78552018-10-10 17:10:01 +0200695
David Vinczecea8b592019-10-29 16:09:51 +0100696 scratch_sz = boot_scratch_area_size(state);
David Vincze401c7422019-06-21 20:44:05 +0200697
698 /*
699 * The following loop scans all sectors in a linear fashion, assuring that
700 * for each possible sector in each slot, it is able to fit in the other
701 * slot's sector or sectors. Slot's should be compatible as long as any
702 * number of a slot's sectors are able to fit into another, which only
703 * excludes cases where sector sizes are not a multiple of each other.
704 */
705 i = sz0 = primary_slot_sz = 0;
706 j = sz1 = secondary_slot_sz = 0;
707 smaller = 0;
708 while (i < num_sectors_primary || j < num_sectors_secondary) {
709 if (sz0 == sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100710 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
711 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200712 i++;
713 j++;
714 } else if (sz0 < sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100715 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200716 /* Guarantee that multiple sectors of the secondary slot
717 * fit into the primary slot.
718 */
719 if (smaller == 2) {
720 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
721 " sectors");
722 return 0;
723 }
724 smaller = 1;
725 i++;
726 } else {
David Vinczecea8b592019-10-29 16:09:51 +0100727 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200728 /* Guarantee that multiple sectors of the primary slot
729 * fit into the secondary slot.
730 */
731 if (smaller == 1) {
732 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
733 " sectors");
734 return 0;
735 }
736 smaller = 2;
737 j++;
738 }
739 if (sz0 == sz1) {
740 primary_slot_sz += sz0;
741 secondary_slot_sz += sz1;
742 /* Scratch has to fit each swap operation to the size of the larger
743 * sector among the primary slot and the secondary slot.
744 */
745 if (sz0 > scratch_sz || sz1 > scratch_sz) {
746 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
747 " scratch");
748 return 0;
749 }
750 smaller = sz0 = sz1 = 0;
751 }
David Vincze39e78552018-10-10 17:10:01 +0200752 }
753
David Vincze401c7422019-06-21 20:44:05 +0200754 if ((i != num_sectors_primary) ||
755 (j != num_sectors_secondary) ||
756 (primary_slot_sz != secondary_slot_sz)) {
757 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
758 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000759 }
760
761 return 1;
762}
763
Tamas Banf70ef8c2017-12-19 15:35:09 +0000764static uint32_t
765boot_status_internal_off(int idx, int state, int elem_sz)
766{
767 int idx_sz;
768
769 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
770
David Vincze39e78552018-10-10 17:10:01 +0200771 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
772 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000773}
774
775/**
776 * Reads the status of a partially-completed swap, if any. This is necessary
777 * to recover in case the boot lodaer was reset in the middle of a swap
778 * operation.
779 */
780static int
David Vinczecea8b592019-10-29 16:09:51 +0100781boot_read_status_bytes(const struct flash_area *fap,
782 struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000783{
784 uint32_t off;
785 uint8_t status;
786 int max_entries;
787 int found;
David Vincze39e78552018-10-10 17:10:01 +0200788 int found_idx;
789 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000790 int rc;
791 int i;
792
793 off = boot_status_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100794 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
795 if (max_entries < 0) {
796 return BOOT_EBADARGS;
797 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000798
799 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200800 found_idx = 0;
801 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000802 for (i = 0; i < max_entries; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100803 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
David Vincze39e78552018-10-10 17:10:01 +0200804 &status, 1);
805 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000806 return BOOT_EFLASH;
807 }
808
David Vincze39e78552018-10-10 17:10:01 +0200809 if (rc == 1) {
810 if (found && !found_idx) {
811 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000812 }
813 } else if (!found) {
814 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200815 } else if (found_idx) {
816 invalid = 1;
817 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000818 }
819 }
820
David Vincze39e78552018-10-10 17:10:01 +0200821 if (invalid) {
822 /* This means there was an error writing status on the last
823 * swap. Tell user and move on to validation!
824 */
825 BOOT_LOG_ERR("Detected inconsistent status!");
826
David Vincze8bdfc2d2019-03-18 15:49:23 +0100827#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
828 /* With validation of the primary slot disabled, there is no way
829 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200830 */
831 assert(0);
832#endif
833 }
834
Tamas Banf70ef8c2017-12-19 15:35:09 +0000835 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200836 if (!found_idx) {
837 found_idx = i;
838 }
David Vincze39e78552018-10-10 17:10:01 +0200839 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
840 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000841 }
842
843 return 0;
844}
845
846/**
847 * Reads the boot status from the flash. The boot status contains
848 * the current state of an interrupted image copy operation. If the boot
849 * status is not present, or it indicates that previous copy finished,
850 * there is no operation in progress.
851 */
852static int
David Vinczecea8b592019-10-29 16:09:51 +0100853boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000854{
855 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200856 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200857 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000858 int status_loc;
859 int area_id;
860 int rc;
861
David Vincze39e78552018-10-10 17:10:01 +0200862 memset(bs, 0, sizeof *bs);
863 bs->idx = BOOT_STATUS_IDX_0;
864 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200865 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200866
867#ifdef MCUBOOT_OVERWRITE_ONLY
868 /* Overwrite-only doesn't make use of the swap status area. */
869 return 0;
870#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000871
David Vinczecea8b592019-10-29 16:09:51 +0100872 status_loc = boot_status_source(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000873 switch (status_loc) {
874 case BOOT_STATUS_SOURCE_NONE:
875 return 0;
876
877 case BOOT_STATUS_SOURCE_SCRATCH:
878 area_id = FLASH_AREA_IMAGE_SCRATCH;
879 break;
880
David Vincze8bdfc2d2019-03-18 15:49:23 +0100881 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
David Vinczecea8b592019-10-29 16:09:51 +0100882 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000883 break;
884
885 default:
886 assert(0);
887 return BOOT_EBADARGS;
888 }
889
890 rc = flash_area_open(area_id, &fap);
891 if (rc != 0) {
892 return BOOT_EFLASH;
893 }
894
David Vinczecea8b592019-10-29 16:09:51 +0100895 rc = boot_read_status_bytes(fap, state, bs);
David Vincze401c7422019-06-21 20:44:05 +0200896 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200897 off = boot_swap_info_off(fap);
898 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200899 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200900 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200901 rc = 0;
902 }
David Vincze91b71ef2019-06-24 13:06:47 +0200903
904 /* Extract the swap type info */
905 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200906 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000907
908 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200909
Tamas Banf70ef8c2017-12-19 15:35:09 +0000910 return rc;
911}
912
913/**
914 * Writes the supplied boot status to the flash file system. The boot status
915 * contains the current state of an in-progress image copy operation.
916 *
917 * @param bs The boot status to write.
918 *
919 * @return 0 on success; nonzero on failure.
920 */
921int
David Vinczecea8b592019-10-29 16:09:51 +0100922boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000923{
Tamas Ban581034a2017-12-19 19:54:37 +0000924 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000925 uint32_t off;
926 int area_id;
927 int rc;
928 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100929 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200930 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000931
932 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100933 * the trailer. Since in the last step the primary slot is erased, the
934 * first two status writes go to the scratch which will be copied to
935 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000936 */
937
938 if (bs->use_scratch) {
939 /* Write to scratch. */
940 area_id = FLASH_AREA_IMAGE_SCRATCH;
941 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100942 /* Write to the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100943 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000944 }
945
946 rc = flash_area_open(area_id, &fap);
947 if (rc != 0) {
948 rc = BOOT_EFLASH;
949 goto done;
950 }
951
952 off = boot_status_off(fap) +
David Vinczecea8b592019-10-29 16:09:51 +0100953 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Tamas Banc3828852018-02-01 12:24:16 +0000954 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200955 erased_val = flash_area_erased_val(fap);
956 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000957 buf[0] = bs->state;
958
959 rc = flash_area_write(fap, off, buf, align);
960 if (rc != 0) {
961 rc = BOOT_EFLASH;
962 goto done;
963 }
964
965 rc = 0;
966
967done:
968 flash_area_close(fap);
969 return rc;
970}
971
Tamas Banf70ef8c2017-12-19 15:35:09 +0000972/**
973 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100974 * that a swap operation is required, the image in the secondary slot is checked
975 * for validity. If the image in the secondary slot is invalid, it is erased,
976 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000977 *
978 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
979 */
980static int
David Vinczecea8b592019-10-29 16:09:51 +0100981boot_validated_swap_type(struct boot_loader_state *state,
982 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000983{
984 int swap_type;
David Vinczecea8b592019-10-29 16:09:51 +0100985 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000986
David Vinczecea8b592019-10-29 16:09:51 +0100987 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
988 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100989 /* Boot loader wants to switch to the secondary slot.
990 * Ensure image is valid.
991 */
David Vinczecea8b592019-10-29 16:09:51 +0100992 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
993 if (rc == 1) {
994 swap_type = BOOT_SWAP_TYPE_NONE;
995 } else if (rc != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000996 swap_type = BOOT_SWAP_TYPE_FAIL;
997 }
998 }
999
1000 return swap_type;
1001}
1002
1003/**
1004 * Calculates the number of sectors the scratch area can contain. A "last"
1005 * source sector is specified because images are copied backwards in flash
1006 * (final index to index number 0).
1007 *
1008 * @param last_sector_idx The index of the last source sector
1009 * (inclusive).
1010 * @param out_first_sector_idx The index of the first source sector
1011 * (inclusive) gets written here.
1012 *
1013 * @return The number of bytes comprised by the
1014 * [first-sector, last-sector] range.
1015 */
1016#ifndef MCUBOOT_OVERWRITE_ONLY
1017static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01001018boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
1019 int *out_first_sector_idx)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001020{
1021 size_t scratch_sz;
1022 uint32_t new_sz;
1023 uint32_t sz;
1024 int i;
1025
1026 sz = 0;
1027
David Vinczecea8b592019-10-29 16:09:51 +01001028 scratch_sz = boot_scratch_area_size(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001029 for (i = last_sector_idx; i >= 0; i--) {
David Vinczecea8b592019-10-29 16:09:51 +01001030 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +02001031 /*
1032 * The secondary slot is not being checked here, because
1033 * `boot_slots_compatible` already provides assurance that the copy size
1034 * will be compatible with the primary slot and scratch.
1035 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001036 if (new_sz > scratch_sz) {
1037 break;
1038 }
1039 sz = new_sz;
1040 }
1041
1042 /* i currently refers to a sector that doesn't fit or it is -1 because all
1043 * sectors have been processed. In both cases, exclude sector i.
1044 */
1045 *out_first_sector_idx = i + 1;
1046 return sz;
1047}
1048#endif /* !MCUBOOT_OVERWRITE_ONLY */
1049
1050/**
David Vinczef7641fa2018-09-04 18:29:46 +02001051 * Erases a region of flash.
1052 *
David Vincze401c7422019-06-21 20:44:05 +02001053 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +02001054 * @param off The offset within the flash area to start the
1055 * erase.
1056 * @param sz The number of bytes to erase.
1057 *
1058 * @return 0 on success; nonzero on failure.
1059 */
David Vincze401c7422019-06-21 20:44:05 +02001060static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001061boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +02001062{
David Vincze401c7422019-06-21 20:44:05 +02001063 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001064}
1065
1066/**
Tamas Banf70ef8c2017-12-19 15:35:09 +00001067 * Copies the contents of one flash region to another. You must erase the
1068 * destination region prior to calling this function.
1069 *
1070 * @param flash_area_id_src The ID of the source flash area.
1071 * @param flash_area_id_dst The ID of the destination flash area.
1072 * @param off_src The offset within the source flash area to
1073 * copy from.
1074 * @param off_dst The offset within the destination flash area to
1075 * copy to.
1076 * @param sz The number of bytes to copy.
1077 *
1078 * @return 0 on success; nonzero on failure.
1079 */
1080static int
David Vinczecea8b592019-10-29 16:09:51 +01001081boot_copy_region(struct boot_loader_state *state,
1082 const struct flash_area *fap_src,
David Vincze401c7422019-06-21 20:44:05 +02001083 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001084 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1085{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001086 uint32_t bytes_copied;
1087 int chunk_sz;
1088 int rc;
1089
1090 static uint8_t buf[1024];
1091
David Vinczecea8b592019-10-29 16:09:51 +01001092 (void)state;
1093
Tamas Banf70ef8c2017-12-19 15:35:09 +00001094 bytes_copied = 0;
1095 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +00001096 if (sz - bytes_copied > sizeof(buf)) {
1097 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001098 } else {
1099 chunk_sz = sz - bytes_copied;
1100 }
1101
1102 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1103 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001104 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001105 }
1106
1107 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1108 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001109 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001110 }
1111
1112 bytes_copied += chunk_sz;
1113 }
1114
David Vincze401c7422019-06-21 20:44:05 +02001115 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001116}
1117
1118#ifndef MCUBOOT_OVERWRITE_ONLY
1119static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001120boot_status_init(const struct boot_loader_state *state,
1121 const struct flash_area *fap,
1122 const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001123{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001124 struct boot_swap_state swap_state;
David Vinczecea8b592019-10-29 16:09:51 +01001125 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001126 int rc;
1127
David Vinczecea8b592019-10-29 16:09:51 +01001128#if (BOOT_IMAGE_NUMBER == 1)
1129 (void)state;
1130#endif
1131
1132 image_index = BOOT_CURR_IMG(state);
1133
David Vincze401c7422019-06-21 20:44:05 +02001134 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001135
David Vinczecea8b592019-10-29 16:09:51 +01001136 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1137 &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001138 assert(rc == 0);
1139
David Vincze401c7422019-06-21 20:44:05 +02001140 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01001141 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
David Vincze401c7422019-06-21 20:44:05 +02001142 assert(rc == 0);
1143 }
1144
Tamas Banf70ef8c2017-12-19 15:35:09 +00001145 if (swap_state.image_ok == BOOT_FLAG_SET) {
1146 rc = boot_write_image_ok(fap);
1147 assert(rc == 0);
1148 }
1149
1150 rc = boot_write_swap_size(fap, bs->swap_size);
1151 assert(rc == 0);
1152
1153 rc = boot_write_magic(fap);
1154 assert(rc == 0);
1155
Tamas Banf70ef8c2017-12-19 15:35:09 +00001156 return 0;
1157}
David Vinczef7641fa2018-09-04 18:29:46 +02001158
1159static int
David Vinczecea8b592019-10-29 16:09:51 +01001160boot_erase_trailer_sectors(const struct boot_loader_state *state,
1161 const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +02001162{
1163 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +02001164 uint32_t sector;
1165 uint32_t trailer_sz;
1166 uint32_t total_sz;
1167 uint32_t off;
1168 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +02001169 int fa_id_primary;
1170 int fa_id_secondary;
David Vinczecea8b592019-10-29 16:09:51 +01001171 uint8_t image_index;
David Vinczef7641fa2018-09-04 18:29:46 +02001172 int rc;
1173
David Vincze401c7422019-06-21 20:44:05 +02001174 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1175
David Vinczecea8b592019-10-29 16:09:51 +01001176 image_index = BOOT_CURR_IMG(state);
1177 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1178 BOOT_PRIMARY_SLOT);
1179 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1180 BOOT_SECONDARY_SLOT);
David Vinczebb207982019-08-21 15:13:04 +02001181
1182 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001183 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001184 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001185 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001186 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001187 return BOOT_EFLASH;
1188 }
1189
David Vincze401c7422019-06-21 20:44:05 +02001190 /* delete starting from last sector and moving to beginning */
David Vinczecea8b592019-10-29 16:09:51 +01001191 sector = boot_img_num_sectors(state, slot) - 1;
1192 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
David Vincze401c7422019-06-21 20:44:05 +02001193 total_sz = 0;
1194 do {
David Vinczecea8b592019-10-29 16:09:51 +01001195 sz = boot_img_sector_size(state, slot, sector);
1196 off = boot_img_sector_off(state, slot, sector);
1197 rc = boot_erase_region(fap, off, sz);
David Vincze401c7422019-06-21 20:44:05 +02001198 assert(rc == 0);
1199
1200 sector--;
1201 total_sz += sz;
1202 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001203
1204 return rc;
1205}
1206#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001207
Tamas Banf70ef8c2017-12-19 15:35:09 +00001208/**
1209 * Swaps the contents of two flash regions within the two image slots.
1210 *
1211 * @param idx The index of the first sector in the range of
1212 * sectors being swapped.
1213 * @param sz The number of bytes to swap.
1214 * @param bs The current boot status. This struct gets
1215 * updated according to the outcome.
1216 *
1217 * @return 0 on success; nonzero on failure.
1218 */
1219#ifndef MCUBOOT_OVERWRITE_ONLY
1220static void
David Vinczecea8b592019-10-29 16:09:51 +01001221boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1222 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001223{
David Vincze401c7422019-06-21 20:44:05 +02001224 const struct flash_area *fap_primary_slot;
1225 const struct flash_area *fap_secondary_slot;
1226 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001227 uint32_t copy_sz;
1228 uint32_t trailer_sz;
1229 uint32_t img_off;
1230 uint32_t scratch_trailer_off;
1231 struct boot_swap_state swap_state;
1232 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001233 bool erase_scratch;
David Vinczecea8b592019-10-29 16:09:51 +01001234 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001235 int rc;
1236
1237 /* Calculate offset from start of image area. */
David Vinczecea8b592019-10-29 16:09:51 +01001238 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001239
1240 copy_sz = sz;
David Vinczecea8b592019-10-29 16:09:51 +01001241 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001242
David Vincze401c7422019-06-21 20:44:05 +02001243 /* sz in this function is always sized on a multiple of the sector size.
1244 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001245 * is to determine if we're swapping the last sector. The last sector
1246 * needs special handling because it's where the trailer lives. If we're
1247 * copying it, we need to use scratch to write the trailer temporarily.
1248 *
1249 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1250 * controls if special handling is needed (swapping last sector).
1251 */
David Vinczecea8b592019-10-29 16:09:51 +01001252 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001253 if ((img_off + sz) >
David Vinczecea8b592019-10-29 16:09:51 +01001254 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001255 copy_sz -= trailer_sz;
1256 }
1257
David Vincze39e78552018-10-10 17:10:01 +02001258 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001259
David Vinczecea8b592019-10-29 16:09:51 +01001260 image_index = BOOT_CURR_IMG(state);
1261
1262 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1263 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001264 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001265
David Vinczecea8b592019-10-29 16:09:51 +01001266 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1267 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001268 assert (rc == 0);
1269
1270 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1271 assert (rc == 0);
1272
1273 if (bs->state == BOOT_STATUS_STATE_0) {
1274 BOOT_LOG_DBG("erasing scratch area");
David Vinczecea8b592019-10-29 16:09:51 +01001275 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001276 assert(rc == 0);
1277
David Vincze39e78552018-10-10 17:10:01 +02001278 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001279 /* Write a trailer to the scratch area, even if we don't need the
1280 * scratch area for status. We need a temporary place to store the
1281 * `swap-type` while we erase the primary trailer.
1282 */
David Vinczecea8b592019-10-29 16:09:51 +01001283 rc = boot_status_init(state, fap_scratch, bs);
David Vincze401c7422019-06-21 20:44:05 +02001284 assert(rc == 0);
1285
1286 if (!bs->use_scratch) {
1287 /* Prepare the primary status area... here it is known that the
1288 * last sector is not being used by the image data so it's safe
1289 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001290 */
David Vinczecea8b592019-10-29 16:09:51 +01001291 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001292 assert(rc == 0);
1293
David Vinczecea8b592019-10-29 16:09:51 +01001294 rc = boot_status_init(state, fap_primary_slot, bs);
David Vincze401c7422019-06-21 20:44:05 +02001295 assert(rc == 0);
1296
1297 /* Erase the temporary trailer from the scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01001298 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
David Vincze401c7422019-06-21 20:44:05 +02001299 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001300 }
1301 }
1302
David Vinczecea8b592019-10-29 16:09:51 +01001303 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
David Vincze401c7422019-06-21 20:44:05 +02001304 img_off, 0, copy_sz);
1305 assert(rc == 0);
1306
David Vinczecea8b592019-10-29 16:09:51 +01001307 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001308 bs->state = BOOT_STATUS_STATE_1;
David Vincze39e78552018-10-10 17:10:01 +02001309 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001310 }
1311
David Vincze39e78552018-10-10 17:10:01 +02001312 if (bs->state == BOOT_STATUS_STATE_1) {
David Vinczecea8b592019-10-29 16:09:51 +01001313 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001314 assert(rc == 0);
1315
David Vinczecea8b592019-10-29 16:09:51 +01001316 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001317 img_off, img_off, copy_sz);
1318 assert(rc == 0);
1319
David Vincze39e78552018-10-10 17:10:01 +02001320 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001321 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001322 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001323 */
David Vinczecea8b592019-10-29 16:09:51 +01001324 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001325 assert(rc == 0);
1326 }
1327
David Vinczecea8b592019-10-29 16:09:51 +01001328 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001329 bs->state = BOOT_STATUS_STATE_2;
David Vincze39e78552018-10-10 17:10:01 +02001330 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001331 }
1332
David Vincze39e78552018-10-10 17:10:01 +02001333 if (bs->state == BOOT_STATUS_STATE_2) {
David Vinczecea8b592019-10-29 16:09:51 +01001334 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001335 assert(rc == 0);
1336
David Vincze401c7422019-06-21 20:44:05 +02001337 /* NOTE: If this is the final sector, we exclude the image trailer from
1338 * this copy (copy_sz was truncated earlier).
1339 */
David Vinczecea8b592019-10-29 16:09:51 +01001340 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001341 0, img_off, copy_sz);
1342 assert(rc == 0);
1343
1344 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001345 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001346
1347 /* copy current status that is being maintained in scratch */
David Vinczecea8b592019-10-29 16:09:51 +01001348 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze401c7422019-06-21 20:44:05 +02001349 scratch_trailer_off, img_off + copy_sz,
David Vinczecea8b592019-10-29 16:09:51 +01001350 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
David Vincze39e78552018-10-10 17:10:01 +02001351 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001352
1353 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1354 &swap_state);
1355 assert(rc == 0);
1356
1357 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001358 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001359 assert(rc == 0);
1360 }
1361
David Vincze401c7422019-06-21 20:44:05 +02001362 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001363 rc = boot_write_swap_info(fap_primary_slot,
1364 swap_state.swap_type,
David Vinczecea8b592019-10-29 16:09:51 +01001365 image_index);
David Vincze401c7422019-06-21 20:44:05 +02001366 assert(rc == 0);
1367 }
1368
1369 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001370 assert(rc == 0);
1371
David Vincze401c7422019-06-21 20:44:05 +02001372 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001373 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001374 }
1375
David Vincze401c7422019-06-21 20:44:05 +02001376 /* If we wrote a trailer to the scratch area, erase it after we persist
1377 * a trailer to the primary slot. We do this to prevent mcuboot from
1378 * reading a stale status from the scratch area in case of immediate
1379 * reset.
1380 */
1381 erase_scratch = bs->use_scratch;
1382 bs->use_scratch = 0;
1383
David Vinczecea8b592019-10-29 16:09:51 +01001384 rc = boot_write_status(state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001385 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001386 bs->state = BOOT_STATUS_STATE_0;
David Vincze39e78552018-10-10 17:10:01 +02001387 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001388
1389 if (erase_scratch) {
David Vinczecea8b592019-10-29 16:09:51 +01001390 rc = boot_erase_region(fap_scratch, 0, sz);
David Vincze401c7422019-06-21 20:44:05 +02001391 assert(rc == 0);
1392 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001393 }
David Vincze401c7422019-06-21 20:44:05 +02001394
1395 flash_area_close(fap_primary_slot);
1396 flash_area_close(fap_secondary_slot);
1397 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001398}
1399#endif /* !MCUBOOT_OVERWRITE_ONLY */
1400
1401/**
David Vincze401c7422019-06-21 20:44:05 +02001402 * Overwrite primary slot with the image contained in the secondary slot.
1403 * If a prior copy operation was interrupted by a system reset, this function
1404 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001405 *
1406 * @param bs The current boot status. This function reads
1407 * this struct to determine if it is resuming
1408 * an interrupted swap operation. This
1409 * function writes the updated status to this
1410 * function on return.
1411 *
1412 * @return 0 on success; nonzero on failure.
1413 */
1414#ifdef MCUBOOT_OVERWRITE_ONLY
1415static int
David Vinczecea8b592019-10-29 16:09:51 +01001416boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001417{
1418 size_t sect_count;
1419 size_t sect;
1420 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001421 size_t size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001422 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001423 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001424 const struct flash_area *fap_primary_slot;
1425 const struct flash_area *fap_secondary_slot;
David Vinczecea8b592019-10-29 16:09:51 +01001426 uint8_t image_index;
David Vincze39e78552018-10-10 17:10:01 +02001427
1428 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001429
David Vincze8bdfc2d2019-03-18 15:49:23 +01001430 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1431 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001432
David Vinczecea8b592019-10-29 16:09:51 +01001433 image_index = BOOT_CURR_IMG(state);
1434
1435 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1436 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001437 assert (rc == 0);
1438
David Vinczecea8b592019-10-29 16:09:51 +01001439 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1440 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001441 assert (rc == 0);
1442
David Vinczecea8b592019-10-29 16:09:51 +01001443 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
1444 for (sect = 0, size = 0; sect < sect_count; sect++) {
1445 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
1446 rc = boot_erase_region(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001447 assert(rc == 0);
1448
1449 size += this_size;
1450 }
1451
David Vincze8bdfc2d2019-03-18 15:49:23 +01001452 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1453 size);
David Vinczecea8b592019-10-29 16:09:51 +01001454 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot,
1455 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001456
David Vincze060968d2019-05-23 01:13:14 +02001457 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001458 * value. Both slots hold the new image at this point, but the secondary
1459 * slot's image header must be passed because the read image headers in the
1460 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001461 */
David Vinczecea8b592019-10-29 16:09:51 +01001462 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1463 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001464 if (rc != 0) {
1465 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1466 return rc;
1467 }
1468
David Vincze39e78552018-10-10 17:10:01 +02001469 /*
1470 * Erases header and trailer. The trailer is erased because when a new
1471 * image is written without a trailer as is the case when using newt, the
1472 * trailer that was left might trigger a new upgrade.
1473 */
David Vincze401c7422019-06-21 20:44:05 +02001474 BOOT_LOG_DBG("erasing secondary header");
David Vinczecea8b592019-10-29 16:09:51 +01001475 rc = boot_erase_region(fap_secondary_slot,
1476 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1477 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001478 assert(rc == 0);
David Vinczecea8b592019-10-29 16:09:51 +01001479 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001480 BOOT_LOG_DBG("erasing secondary trailer");
David Vinczecea8b592019-10-29 16:09:51 +01001481 rc = boot_erase_region(fap_secondary_slot,
1482 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1483 last_sector),
1484 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1485 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001486 assert(rc == 0);
1487
David Vincze401c7422019-06-21 20:44:05 +02001488 flash_area_close(fap_primary_slot);
1489 flash_area_close(fap_secondary_slot);
1490
David Vincze8bdfc2d2019-03-18 15:49:23 +01001491 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001492
1493 return 0;
1494}
1495#else
David Vincze401c7422019-06-21 20:44:05 +02001496/**
1497 * Swaps the two images in flash. If a prior copy operation was interrupted
1498 * by a system reset, this function completes that operation.
1499 *
1500 * @param bs The current boot status. This function reads
1501 * this struct to determine if it is resuming
1502 * an interrupted swap operation. This
1503 * function writes the updated status to this
1504 * function on return.
1505 *
1506 * @return 0 on success; nonzero on failure.
1507 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001508static int
David Vinczecea8b592019-10-29 16:09:51 +01001509boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001510{
1511 uint32_t sz;
1512 int first_sector_idx;
1513 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001514 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001515 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001516 struct image_header *hdr;
1517 uint32_t size;
1518 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001519 uint32_t primary_slot_size;
1520 uint32_t secondary_slot_size;
David Vinczecea8b592019-10-29 16:09:51 +01001521 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001522 int rc;
1523
1524 /* FIXME: just do this if asked by user? */
1525
1526 size = copy_size = 0;
David Vinczecea8b592019-10-29 16:09:51 +01001527 image_index = BOOT_CURR_IMG(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001528
David Vincze39e78552018-10-10 17:10:01 +02001529 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001530 /*
1531 * No swap ever happened, so need to find the largest image which
1532 * will be used to determine the amount of sectors to swap.
1533 */
David Vinczecea8b592019-10-29 16:09:51 +01001534 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1535 if (hdr->ih_magic == IMAGE_MAGIC) {
1536 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
1537 assert(rc == 0);
1538 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001539
David Vinczecea8b592019-10-29 16:09:51 +01001540 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1541 if (hdr->ih_magic == IMAGE_MAGIC) {
1542 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
1543 assert(rc == 0);
1544 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001545
1546 if (size > copy_size) {
1547 copy_size = size;
1548 }
1549
1550 bs->swap_size = copy_size;
1551 } else {
1552 /*
1553 * If a swap was under way, the swap_size should already be present
1554 * in the trailer...
1555 */
David Vinczecea8b592019-10-29 16:09:51 +01001556 rc = boot_read_swap_size(image_index, &bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001557 assert(rc == 0);
1558
1559 copy_size = bs->swap_size;
1560 }
1561
David Vincze401c7422019-06-21 20:44:05 +02001562 primary_slot_size = 0;
1563 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001564 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001565 last_idx_secondary_slot = 0;
1566
1567 /*
1568 * Knowing the size of the largest image between both slots, here we
1569 * find what is the last sector in the primary slot that needs swapping.
1570 * Since we already know that both slots are compatible, the secondary
1571 * slot's last sector is not really required after this check is finished.
1572 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001573 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001574 if ((primary_slot_size < copy_size) ||
1575 (primary_slot_size < secondary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001576 primary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001577 BOOT_PRIMARY_SLOT,
1578 last_sector_idx);
1579 }
1580 if ((secondary_slot_size < copy_size) ||
1581 (secondary_slot_size < primary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001582 secondary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001583 BOOT_SECONDARY_SLOT,
1584 last_idx_secondary_slot);
1585 }
1586 if (primary_slot_size >= copy_size &&
1587 secondary_slot_size >= copy_size &&
1588 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001589 break;
1590 }
1591 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001592 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001593 }
1594
1595 swap_idx = 0;
1596 while (last_sector_idx >= 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001597 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001598 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
David Vinczecea8b592019-10-29 16:09:51 +01001599 boot_swap_sectors(first_sector_idx, sz, state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001600 }
1601
1602 last_sector_idx = first_sector_idx - 1;
1603 swap_idx++;
1604 }
1605
David Vincze8bdfc2d2019-03-18 15:49:23 +01001606#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001607 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001608 BOOT_LOG_WRN("%d status write fails performing the swap",
1609 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001610 }
1611#endif
1612
Tamas Banf70ef8c2017-12-19 15:35:09 +00001613 return 0;
1614}
1615#endif
1616
1617/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001618 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001619 */
1620#ifndef MCUBOOT_OVERWRITE_ONLY
1621static int
David Vinczecea8b592019-10-29 16:09:51 +01001622boot_set_copy_done(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001623{
1624 const struct flash_area *fap;
1625 int rc;
1626
David Vinczecea8b592019-10-29 16:09:51 +01001627 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1628 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001629 if (rc != 0) {
1630 return BOOT_EFLASH;
1631 }
1632
1633 rc = boot_write_copy_done(fap);
1634 flash_area_close(fap);
1635 return rc;
1636}
1637#endif /* !MCUBOOT_OVERWRITE_ONLY */
1638
1639/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001640 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1641 * ensure the status bytes from the image revert operation don't get processed
1642 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001643 *
1644 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001645 * image installed on the primary slot and the new image to be upgrade to has a
1646 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001647 */
1648#ifndef MCUBOOT_OVERWRITE_ONLY
1649static int
David Vinczecea8b592019-10-29 16:09:51 +01001650boot_set_image_ok(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001651{
1652 const struct flash_area *fap;
1653 struct boot_swap_state state;
1654 int rc;
1655
David Vinczecea8b592019-10-29 16:09:51 +01001656 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1657 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001658 if (rc != 0) {
1659 return BOOT_EFLASH;
1660 }
1661
1662 rc = boot_read_swap_state(fap, &state);
1663 if (rc != 0) {
1664 rc = BOOT_EFLASH;
1665 goto out;
1666 }
1667
1668 if (state.image_ok == BOOT_FLAG_UNSET) {
1669 rc = boot_write_image_ok(fap);
1670 }
1671
1672out:
1673 flash_area_close(fap);
1674 return rc;
1675}
1676#endif /* !MCUBOOT_OVERWRITE_ONLY */
1677
David Vinczebb6e3b62019-07-31 14:24:05 +02001678#if (BOOT_IMAGE_NUMBER > 1)
1679/**
David Vinczecea8b592019-10-29 16:09:51 +01001680 * Check if the version of the image is not older than required.
1681 *
1682 * @param req Required minimal image version.
1683 * @param ver Version of the image to be checked.
1684 *
1685 * @return 0 if the version is sufficient, nonzero otherwise.
1686 */
1687static int
1688boot_is_version_sufficient(struct image_version *req,
1689 struct image_version *ver)
1690{
1691 if (ver->iv_major > req->iv_major) {
1692 return 0;
1693 }
1694 if (ver->iv_major < req->iv_major) {
1695 return BOOT_EBADVERSION;
1696 }
1697 /* The major version numbers are equal. */
1698 if (ver->iv_minor > req->iv_minor) {
1699 return 0;
1700 }
1701 if (ver->iv_minor < req->iv_minor) {
1702 return BOOT_EBADVERSION;
1703 }
1704 /* The minor version numbers are equal. */
1705 if (ver->iv_revision < req->iv_revision) {
1706 return BOOT_EBADVERSION;
1707 }
1708
1709 return 0;
1710}
1711
1712/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001713 * Check the image dependency whether it is satisfied and modify
1714 * the swap type if necessary.
1715 *
1716 * @param dep Image dependency which has to be verified.
1717 *
1718 * @return 0 on success; nonzero on failure.
1719 */
1720static int
David Vinczecea8b592019-10-29 16:09:51 +01001721boot_verify_slot_dependency(struct boot_loader_state *state,
1722 struct image_dependency *dep)
David Vinczebb6e3b62019-07-31 14:24:05 +02001723{
1724 struct image_version *dep_version;
1725 size_t dep_slot;
1726 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001727 uint8_t swap_type;
David Vinczebb6e3b62019-07-31 14:24:05 +02001728
1729 /* Determine the source of the image which is the subject of
1730 * the dependency and get it's version. */
David Vinczecea8b592019-10-29 16:09:51 +01001731 swap_type = state->swap_type[dep->image_id];
1732 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczebb6e3b62019-07-31 14:24:05 +02001733 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
David Vinczecea8b592019-10-29 16:09:51 +01001734 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczebb6e3b62019-07-31 14:24:05 +02001735
1736 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1737 if (rc != 0) {
1738 /* Dependency not satisfied.
1739 * Modify the swap type to decrease the version number of the image
1740 * (which will be located in the primary slot after the boot process),
1741 * consequently the number of unsatisfied dependencies will be
1742 * decreased or remain the same.
1743 */
David Vinczecea8b592019-10-29 16:09:51 +01001744 switch (BOOT_SWAP_TYPE(state)) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001745 case BOOT_SWAP_TYPE_TEST:
1746 case BOOT_SWAP_TYPE_PERM:
David Vinczecea8b592019-10-29 16:09:51 +01001747 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczebb6e3b62019-07-31 14:24:05 +02001748 break;
1749 case BOOT_SWAP_TYPE_NONE:
David Vinczecea8b592019-10-29 16:09:51 +01001750 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczebb6e3b62019-07-31 14:24:05 +02001751 break;
1752 default:
1753 break;
1754 }
1755 }
1756
1757 return rc;
1758}
1759
1760/**
1761 * Read all dependency TLVs of an image from the flash and verify
1762 * one after another to see if they are all satisfied.
1763 *
1764 * @param slot Image slot number.
1765 *
1766 * @return 0 on success; nonzero on failure.
1767 */
1768static int
David Vinczecea8b592019-10-29 16:09:51 +01001769boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczebb6e3b62019-07-31 14:24:05 +02001770{
1771 const struct flash_area *fap;
David Vincze07706a42019-12-12 18:20:12 +01001772 struct image_tlv_iter it;
David Vinczebb6e3b62019-07-31 14:24:05 +02001773 struct image_dependency dep;
1774 uint32_t off;
David Vincze07706a42019-12-12 18:20:12 +01001775 uint16_t len;
David Vinczecea8b592019-10-29 16:09:51 +01001776 int area_id;
David Vinczebb6e3b62019-07-31 14:24:05 +02001777 int rc;
1778
David Vinczecea8b592019-10-29 16:09:51 +01001779 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1780 rc = flash_area_open(area_id, &fap);
David Vinczebb6e3b62019-07-31 14:24:05 +02001781 if (rc != 0) {
1782 rc = BOOT_EFLASH;
1783 goto done;
1784 }
1785
David Vincze07706a42019-12-12 18:20:12 +01001786 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1787 IMAGE_TLV_DEPENDENCY, true);
David Vinczebb6e3b62019-07-31 14:24:05 +02001788 if (rc != 0) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001789 goto done;
1790 }
1791
David Vincze07706a42019-12-12 18:20:12 +01001792 while (true) {
1793 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1794 if (rc < 0) {
1795 return -1;
1796 } else if (rc > 0) {
1797 rc = 0;
David Vinczebb6e3b62019-07-31 14:24:05 +02001798 break;
1799 }
David Vincze07706a42019-12-12 18:20:12 +01001800
1801 if (len != sizeof(dep)) {
1802 rc = BOOT_EBADIMAGE;
1803 goto done;
1804 }
1805
1806 rc = flash_area_read(fap, off, &dep, len);
1807 if (rc != 0) {
1808 rc = BOOT_EFLASH;
1809 goto done;
1810 }
1811
1812 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1813 rc = BOOT_EBADARGS;
1814 goto done;
1815 }
1816
1817 /* Verify dependency and modify the swap type if not satisfied. */
1818 rc = boot_verify_slot_dependency(state, &dep);
1819 if (rc != 0) {
1820 /* Dependency not satisfied. */
1821 goto done;
Tamas Ban056ed0b2019-09-16 12:48:32 +01001822 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001823 }
1824
1825done:
1826 flash_area_close(fap);
1827 return rc;
1828}
1829
1830/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001831 * Iterate over all the images and verify whether the image dependencies in the
1832 * TLV area are all satisfied and update the related swap type if necessary.
1833 */
David Vinczecea8b592019-10-29 16:09:51 +01001834static int
1835boot_verify_dependencies(struct boot_loader_state *state)
David Vinczebb6e3b62019-07-31 14:24:05 +02001836{
David Vinczebb6e3b62019-07-31 14:24:05 +02001837 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001838 uint8_t slot;
David Vinczebb6e3b62019-07-31 14:24:05 +02001839
David Vinczecea8b592019-10-29 16:09:51 +01001840 BOOT_CURR_IMG(state) = 0;
1841 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1842 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1843 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1844 slot = BOOT_SECONDARY_SLOT;
1845 } else {
1846 slot = BOOT_PRIMARY_SLOT;
1847 }
1848
1849 rc = boot_verify_slot_dependencies(state, slot);
David Vinczebb6e3b62019-07-31 14:24:05 +02001850 if (rc == 0) {
1851 /* All dependencies've been satisfied, continue with next image. */
David Vinczecea8b592019-10-29 16:09:51 +01001852 BOOT_CURR_IMG(state)++;
David Vinczebb6e3b62019-07-31 14:24:05 +02001853 } else if (rc == BOOT_EBADVERSION) {
David Vinczecea8b592019-10-29 16:09:51 +01001854 /* Cannot upgrade due to non-met dependencies, so disable all
1855 * image upgrades.
1856 */
1857 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1858 BOOT_CURR_IMG(state) = idx;
1859 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1860 }
1861 break;
David Vinczebb6e3b62019-07-31 14:24:05 +02001862 } else {
1863 /* Other error happened, images are inconsistent */
David Vinczecea8b592019-10-29 16:09:51 +01001864 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001865 }
1866 }
David Vinczecea8b592019-10-29 16:09:51 +01001867 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001868}
1869#endif /* (BOOT_IMAGE_NUMBER > 1) */
1870
Tamas Banf70ef8c2017-12-19 15:35:09 +00001871/**
David Vincze7384ee72019-07-23 17:00:42 +02001872 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001873 *
David Vincze7384ee72019-07-23 17:00:42 +02001874 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001875 *
1876 * @return 0 on success; nonzero on failure.
1877 */
1878static int
David Vinczecea8b592019-10-29 16:09:51 +01001879boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001880{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001881 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001882#ifndef MCUBOOT_OVERWRITE_ONLY
1883 uint8_t swap_type;
1884#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001885
David Vincze7384ee72019-07-23 17:00:42 +02001886 /* At this point there are no aborted swaps. */
1887#if defined(MCUBOOT_OVERWRITE_ONLY)
David Vinczecea8b592019-10-29 16:09:51 +01001888 rc = boot_copy_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001889#else
David Vinczecea8b592019-10-29 16:09:51 +01001890 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001891#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001892 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001893
1894#ifndef MCUBOOT_OVERWRITE_ONLY
1895 /* The following state needs image_ok be explicitly set after the
1896 * swap was finished to avoid a new revert.
1897 */
David Vinczecea8b592019-10-29 16:09:51 +01001898 swap_type = BOOT_SWAP_TYPE(state);
1899 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1900 swap_type == BOOT_SWAP_TYPE_PERM) {
1901 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001902 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001903 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001904 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001905 }
1906
David Vinczecea8b592019-10-29 16:09:51 +01001907 if (swap_type == BOOT_SWAP_TYPE_PERM) {
David Vincze7384ee72019-07-23 17:00:42 +02001908 /* Update the stored security counter with the new image's security
1909 * counter value. The primary slot holds the new image at this
1910 * point, but the secondary slot's image header must be passed
1911 * because the read image headers in the boot_data structure have
1912 * not been updated yet.
1913 *
1914 * In case of a permanent image swap mcuboot will never attempt to
1915 * revert the images on the next reboot. Therefore, the security
1916 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001917 */
David Vinczecea8b592019-10-29 16:09:51 +01001918 rc = boot_update_security_counter(
1919 BOOT_CURR_IMG(state),
1920 BOOT_PRIMARY_SLOT,
1921 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02001922 if (rc != 0) {
1923 BOOT_LOG_ERR("Security counter update failed after "
1924 "image upgrade.");
David Vinczecea8b592019-10-29 16:09:51 +01001925 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001926 }
1927 }
1928
David Vinczecea8b592019-10-29 16:09:51 +01001929 if (BOOT_IS_UPGRADE(swap_type)) {
1930 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001931 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001932 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001933 }
1934 }
1935#endif /* !MCUBOOT_OVERWRITE_ONLY */
1936
1937 return rc;
1938}
1939
1940/**
1941 * Completes a previously aborted image swap.
1942 *
1943 * @param bs The current boot status.
1944 *
1945 * @return 0 on success; nonzero on failure.
1946 */
1947#if !defined(MCUBOOT_OVERWRITE_ONLY)
1948static int
David Vinczecea8b592019-10-29 16:09:51 +01001949boot_complete_partial_swap(struct boot_loader_state *state,
1950 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02001951{
1952 int rc;
1953
1954 /* Determine the type of swap operation being resumed from the
1955 * `swap-type` trailer field.
1956 */
David Vinczecea8b592019-10-29 16:09:51 +01001957 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001958 assert(rc == 0);
1959
David Vinczecea8b592019-10-29 16:09:51 +01001960 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02001961
1962 /* The following states need image_ok be explicitly set after the
1963 * swap was finished to avoid a new revert.
1964 */
1965 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1966 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
David Vinczecea8b592019-10-29 16:09:51 +01001967 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001968 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001969 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001970 }
1971 }
1972
David Vinczecea8b592019-10-29 16:09:51 +01001973 if (BOOT_IS_UPGRADE(bs->swap_type)) {
1974 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001975 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001976 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001977 }
1978 }
1979
David Vinczecea8b592019-10-29 16:09:51 +01001980 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02001981 BOOT_LOG_ERR("panic!");
1982 assert(0);
1983
1984 /* Loop forever... */
1985 while (1) {}
1986 }
1987
1988 return rc;
1989}
1990#endif /* !MCUBOOT_OVERWRITE_ONLY */
1991
1992#if (BOOT_IMAGE_NUMBER > 1)
1993/**
1994 * Review the validity of previously determined swap types of other images.
1995 *
1996 * @param aborted_swap The current image upgrade is a
1997 * partial/aborted swap.
1998 */
1999static void
David Vinczecea8b592019-10-29 16:09:51 +01002000boot_review_image_swap_types(struct boot_loader_state *state,
2001 bool aborted_swap)
David Vincze7384ee72019-07-23 17:00:42 +02002002{
2003 /* In that case if we rebooted in the middle of an image upgrade process, we
2004 * must review the validity of swap types, that were previously determined
2005 * for other images. The image_ok flag had not been set before the reboot
2006 * for any of the updated images (only the copy_done flag) and thus falsely
2007 * the REVERT swap type has been determined for the previous images that had
2008 * been updated before the reboot.
2009 *
2010 * There are two separate scenarios that we have to deal with:
2011 *
2012 * 1. The reboot has happened during swapping an image:
2013 * The current image upgrade has been determined as a
2014 * partial/aborted swap.
2015 * 2. The reboot has happened between two separate image upgrades:
2016 * In this scenario we must check the swap type of the current image.
2017 * In those cases if it is NONE or REVERT we cannot certainly determine
2018 * the fact of a reboot. In a consistent state images must move in the
2019 * same direction or stay in place, e.g. in practice REVERT and TEST
2020 * swap types cannot be present at the same time. If the swap type of
2021 * the current image is either TEST, PERM or FAIL we must review the
2022 * already determined swap types of other images and set each false
2023 * REVERT swap types to NONE (these images had been successfully
2024 * updated before the system rebooted between two separate image
2025 * upgrades).
2026 */
2027
David Vinczecea8b592019-10-29 16:09:51 +01002028 if (BOOT_CURR_IMG(state) == 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002029 /* Nothing to do */
2030 return;
2031 }
2032
2033 if (!aborted_swap) {
David Vinczecea8b592019-10-29 16:09:51 +01002034 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2035 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vincze7384ee72019-07-23 17:00:42 +02002036 /* Nothing to do */
2037 return;
2038 }
2039 }
2040
David Vinczecea8b592019-10-29 16:09:51 +01002041 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2042 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2043 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002044 }
2045 }
2046}
2047#endif
2048
2049/**
2050 * Prepare image to be updated if required.
2051 *
2052 * Prepare image to be updated if required with completing an image swap
2053 * operation if one was aborted and/or determining the type of the
2054 * swap operation. In case of any error set the swap type to NONE.
2055 *
David Vinczecea8b592019-10-29 16:09:51 +01002056 * @param state Boot loader status information.
David Vincze7384ee72019-07-23 17:00:42 +02002057 * @param bs Pointer where the read and possibly updated
2058 * boot status can be written to.
2059 */
2060static void
David Vinczecea8b592019-10-29 16:09:51 +01002061boot_prepare_image_for_update(struct boot_loader_state *state,
2062 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02002063{
2064 int rc;
2065
2066 /* Determine the sector layout of the image slots and scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01002067 rc = boot_read_sectors(state);
David Vincze7384ee72019-07-23 17:00:42 +02002068 if (rc != 0) {
2069 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2070 " - too small?", BOOT_MAX_IMG_SECTORS);
2071 /* Unable to determine sector layout, continue with next image
2072 * if there is one.
2073 */
David Vinczecea8b592019-10-29 16:09:51 +01002074 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002075 return;
2076 }
2077
2078 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002079 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002080 if (rc != 0) {
2081 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002082 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
2083 BOOT_CURR_IMG(state));
2084 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002085 return;
2086 }
2087
2088 /* If the current image's slots aren't compatible, no swap is possible.
2089 * Just boot into primary slot.
2090 */
David Vinczecea8b592019-10-29 16:09:51 +01002091 if (boot_slots_compatible(state)) {
2092 rc = boot_read_status(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002093 if (rc != 0) {
2094 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
David Vinczecea8b592019-10-29 16:09:51 +01002095 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002096 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002097 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002098 return;
2099 }
2100
2101 /* Determine if we rebooted in the middle of an image swap
2102 * operation. If a partial swap was detected, complete it.
2103 */
2104 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2105
2106#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002107 boot_review_image_swap_types(state, true);
David Vincze7384ee72019-07-23 17:00:42 +02002108#endif
2109
2110#ifdef MCUBOOT_OVERWRITE_ONLY
2111 /* Should never arrive here, overwrite-only mode has
2112 * no swap state.
2113 */
2114 assert(0);
2115#else
2116 /* Determine the type of swap operation being resumed from the
2117 * `swap-type` trailer field.
2118 */
David Vinczecea8b592019-10-29 16:09:51 +01002119 rc = boot_complete_partial_swap(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002120 assert(rc == 0);
2121#endif
2122 /* Attempt to read an image header from each slot. Ensure that
2123 * image headers in slots are aligned with headers in boot_data.
2124 */
David Vinczecea8b592019-10-29 16:09:51 +01002125 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002126 assert(rc == 0);
2127
2128 /* Swap has finished set to NONE */
David Vinczecea8b592019-10-29 16:09:51 +01002129 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002130 } else {
2131 /* There was no partial swap, determine swap type. */
2132 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01002133 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2134 } else if (boot_validate_slot(state,
2135 BOOT_SECONDARY_SLOT, bs) != 0) {
2136 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vincze7384ee72019-07-23 17:00:42 +02002137 } else {
David Vinczecea8b592019-10-29 16:09:51 +01002138 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02002139 }
2140
2141#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002142 boot_review_image_swap_types(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002143#endif
2144 }
2145 } else {
2146 /* In that case if slots are not compatible. */
David Vinczecea8b592019-10-29 16:09:51 +01002147 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002148 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002149}
2150
2151/**
2152 * Prepares the booting process. This function moves images around in flash as
2153 * appropriate, and tells you what address to boot from.
2154 *
David Vinczecea8b592019-10-29 16:09:51 +01002155 * @param state Boot loader status information.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002156 * @param rsp On success, indicates how booting should occur.
2157 *
2158 * @return 0 on success; nonzero on failure.
2159 */
2160int
David Vinczecea8b592019-10-29 16:09:51 +01002161context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Banf70ef8c2017-12-19 15:35:09 +00002162{
Tamas Banf70ef8c2017-12-19 15:35:09 +00002163 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02002164 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01002165 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002166 int fa_id;
David Vinczecea8b592019-10-29 16:09:51 +01002167 int image_index;
2168 bool has_upgrade;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002169
2170 /* The array of slot sectors are defined here (as opposed to file scope) so
2171 * that they don't get allocated for non-boot-loader apps. This is
2172 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2173 * any effect in older gcc versions (e.g., 4.8.4).
2174 */
David Vincze7384ee72019-07-23 17:00:42 +02002175 static boot_sector_t
2176 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2177 static boot_sector_t
2178 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02002179 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00002180
David Vincze7384ee72019-07-23 17:00:42 +02002181 /* Iterate over all the images. By the end of the loop the swap type has
2182 * to be determined for each image and all aborted swaps have to be
2183 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002184 */
David Vinczecea8b592019-10-29 16:09:51 +01002185 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2186
2187 image_index = BOOT_CURR_IMG(state);
2188
2189 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
2190 primary_slot_sectors[image_index];
2191 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
2192 secondary_slot_sectors[image_index];
2193 state->scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002194
David Vincze7384ee72019-07-23 17:00:42 +02002195 /* Open primary and secondary image areas for the duration
2196 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002197 */
David Vincze7384ee72019-07-23 17:00:42 +02002198 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002199 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
2200 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vincze7384ee72019-07-23 17:00:42 +02002201 assert(rc == 0);
2202 }
2203 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
David Vinczecea8b592019-10-29 16:09:51 +01002204 &BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002205 assert(rc == 0);
2206
2207 /* Determine swap type and complete swap if it has been aborted. */
David Vinczecea8b592019-10-29 16:09:51 +01002208 boot_prepare_image_for_update(state, &bs);
2209
2210 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
2211 has_upgrade = true;
2212 }
David Vincze7384ee72019-07-23 17:00:42 +02002213 }
2214
David Vinczebb6e3b62019-07-31 14:24:05 +02002215#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002216 if (has_upgrade) {
2217 /* Iterate over all the images and verify whether the image dependencies
2218 * are all satisfied and update swap type if necessary.
2219 */
2220 rc = boot_verify_dependencies(state);
2221 if (rc == BOOT_EBADVERSION) {
2222 /*
2223 * It was impossible to upgrade because the expected dependency
2224 * version was not available. Here we already changed the swap_type
2225 * so that instead of asserting the bootloader, we continue and no
2226 * upgrade is performed.
2227 */
2228 rc = 0;
2229 }
2230 }
David Vinczebb6e3b62019-07-31 14:24:05 +02002231#endif
2232
David Vincze7384ee72019-07-23 17:00:42 +02002233 /* Iterate over all the images. At this point there are no aborted swaps
2234 * and the swap types are determined for each image. By the end of the loop
2235 * all required update operations will have been finished.
2236 */
David Vinczecea8b592019-10-29 16:09:51 +01002237 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002238
2239#if (BOOT_IMAGE_NUMBER > 1)
2240 /* Indicate that swap is not aborted */
2241 memset(&bs, 0, sizeof bs);
2242 bs.idx = BOOT_STATUS_IDX_0;
2243 bs.state = BOOT_STATUS_STATE_0;
2244#endif /* (BOOT_IMAGE_NUMBER > 1) */
2245
2246 /* Set the previously determined swap type */
David Vinczecea8b592019-10-29 16:09:51 +01002247 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vincze7384ee72019-07-23 17:00:42 +02002248
David Vinczecea8b592019-10-29 16:09:51 +01002249 switch (BOOT_SWAP_TYPE(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002250 case BOOT_SWAP_TYPE_NONE:
2251 break;
2252
2253 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2254 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2255 case BOOT_SWAP_TYPE_REVERT:
David Vinczecea8b592019-10-29 16:09:51 +01002256 rc = boot_perform_update(state, &bs);
David Vincze7384ee72019-07-23 17:00:42 +02002257 assert(rc == 0);
2258 break;
2259
2260 case BOOT_SWAP_TYPE_FAIL:
2261 /* The image in secondary slot was invalid and is now erased. Ensure
2262 * we don't try to boot into it again on the next reboot. Do this by
2263 * pretending we just reverted back to primary slot.
2264 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002265#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002266 /* image_ok needs to be explicitly set to avoid a new revert. */
David Vinczecea8b592019-10-29 16:09:51 +01002267 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00002268 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002269 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002270 }
2271#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002272 break;
2273
2274 default:
David Vinczecea8b592019-10-29 16:09:51 +01002275 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002276 }
David Vincze7384ee72019-07-23 17:00:42 +02002277
David Vinczecea8b592019-10-29 16:09:51 +01002278 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02002279 BOOT_LOG_ERR("panic!");
2280 assert(0);
2281
2282 /* Loop forever... */
2283 while (1) {}
2284 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002285 }
2286
David Vincze7384ee72019-07-23 17:00:42 +02002287 /* Iterate over all the images. At this point all required update operations
2288 * have finished. By the end of the loop each image in the primary slot will
2289 * have been re-validated.
2290 */
David Vinczecea8b592019-10-29 16:09:51 +01002291 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2292 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vincze7384ee72019-07-23 17:00:42 +02002293 /* Attempt to read an image header from each slot. Ensure that image
2294 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002295 */
David Vinczecea8b592019-10-29 16:09:51 +01002296 rc = boot_read_image_headers(state, false);
David Vincze060968d2019-05-23 01:13:14 +02002297 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002298 goto out;
2299 }
2300 /* Since headers were reloaded, it can be assumed we just performed
2301 * a swap or overwrite. Now the header info that should be used to
2302 * provide the data for the bootstrap, which previously was at
2303 * secondary slot, was updated to primary slot.
2304 */
2305 }
2306
2307#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vinczecea8b592019-10-29 16:09:51 +01002308 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vincze7384ee72019-07-23 17:00:42 +02002309 if (rc != 0) {
2310 rc = BOOT_EBADIMAGE;
2311 goto out;
2312 }
2313#else
2314 /* Even if we're not re-validating the primary slot, we could be booting
2315 * onto an empty flash chip. At least do a basic sanity check that
2316 * the magic number on the image is OK.
2317 */
David Vinczecea8b592019-10-29 16:09:51 +01002318 if (!BOOT_IMG_HDR_IS_VALID(state, BOOT_PRIMARY_SLOT)) {
2319 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2320 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_magic,
2321 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002322 rc = BOOT_EBADIMAGE;
2323 goto out;
2324 }
2325#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2326
2327 /* Update the stored security counter with the active image's security
2328 * counter value. It will be updated only if the new security counter is
2329 * greater than the stored value.
2330 *
2331 * In case of a successful image swapping when the swap type is TEST the
2332 * security counter can be increased only after a reset, when the swap
2333 * type is NONE and the image has marked itself "OK" (the image_ok flag
2334 * has been set). This way a "revert" swap can be performed if it's
2335 * necessary.
2336 */
David Vinczecea8b592019-10-29 16:09:51 +01002337 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2338 rc = boot_update_security_counter(
2339 BOOT_CURR_IMG(state),
2340 BOOT_PRIMARY_SLOT,
2341 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02002342 if (rc != 0) {
2343 BOOT_LOG_ERR("Security counter update failed after image "
2344 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002345 goto out;
2346 }
2347 }
2348
David Vincze7384ee72019-07-23 17:00:42 +02002349 /* Save boot status to shared memory area */
2350#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002351 rc = boot_save_boot_status((BOOT_CURR_IMG(state) == 0) ?
2352 SW_SPE : SW_NSPE,
2353 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2354 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002355 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002356#else
David Vincze7384ee72019-07-23 17:00:42 +02002357 rc = boot_save_boot_status(SW_S_NS,
David Vinczecea8b592019-10-29 16:09:51 +01002358 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2359 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002360 );
2361#endif
2362 if (rc) {
2363 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
David Vinczecea8b592019-10-29 16:09:51 +01002364 BOOT_CURR_IMG(state));
David Vincze060968d2019-05-23 01:13:14 +02002365 }
2366 }
2367
David Vinczecea8b592019-10-29 16:09:51 +01002368#if (BOOT_IMAGE_NUMBER > 1)
David Vincze7384ee72019-07-23 17:00:42 +02002369 /* Always boot from the primary slot of Image 0. */
David Vinczecea8b592019-10-29 16:09:51 +01002370 BOOT_CURR_IMG(state) = 0;
2371#endif
Tamas Ban0e8ab302019-01-17 11:45:31 +00002372
David Vinczecea8b592019-10-29 16:09:51 +01002373 rsp->br_flash_dev_id =
2374 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2375 rsp->br_image_off =
2376 boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2377 rsp->br_hdr =
2378 boot_img_hdr(state, BOOT_PRIMARY_SLOT);
2379
2380out:
2381 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2382 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002383 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002384 flash_area_close(BOOT_IMG_AREA(state,
David Vincze7384ee72019-07-23 17:00:42 +02002385 BOOT_NUM_SLOTS - 1 - slot));
2386 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002387 }
2388 return rc;
2389}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002390
Oliver Swedef9982442018-08-24 18:37:44 +01002391#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002392
David Vinczedcba70b2019-05-28 12:02:52 +02002393#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2394 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2395 (area), \
2396 (hdr)->ih_ver.iv_major, \
2397 (hdr)->ih_ver.iv_minor, \
2398 (hdr)->ih_ver.iv_revision, \
2399 (hdr)->ih_ver.iv_build_num, \
2400 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2401 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2402 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002403 (state)->image_ok)
2404
2405struct image_slot_version {
2406 uint64_t version;
2407 uint32_t slot_number;
2408};
2409
2410/**
2411 * Extract the version number from the image header. This function must be
2412 * ported if version number format has changed in the image header.
2413 *
2414 * @param hdr Pointer to an image header structure
2415 *
Oliver Swedef9982442018-08-24 18:37:44 +01002416 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002417 */
2418static uint64_t
2419boot_get_version_number(struct image_header *hdr)
2420{
Oliver Swedef9982442018-08-24 18:37:44 +01002421 uint64_t version = 0;
2422 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2423 + IMAGE_VER_REVISION_LENGTH
2424 + IMAGE_VER_BUILD_NUM_LENGTH);
2425 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2426 + IMAGE_VER_BUILD_NUM_LENGTH);
2427 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2428 version |= hdr->ih_ver.iv_build_num;
2429 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002430}
2431
2432/**
2433 * Comparator function for `qsort` to compare version numbers. This function
2434 * must be ported if version number format has changed in the image header.
2435 *
2436 * @param ver1 Pointer to an array element which holds the version number
2437 * @param ver2 Pointer to another array element which holds the version
2438 * number
2439 *
2440 * @return if version1 > version2 -1
2441 * if version1 == version2 0
2442 * if version1 < version2 1
2443 */
2444static int
2445boot_compare_version_numbers(const void *ver1, const void *ver2)
2446{
2447 if (((struct image_slot_version *)ver1)->version <
2448 ((struct image_slot_version *)ver2)->version) {
2449 return 1;
2450 }
2451
2452 if (((struct image_slot_version *)ver1)->version ==
2453 ((struct image_slot_version *)ver2)->version) {
2454 return 0;
2455 }
2456
2457 return -1;
2458}
2459
2460/**
2461 * Sort the available images based on the version number and puts them in
2462 * a list.
2463 *
David Vinczecea8b592019-10-29 16:09:51 +01002464 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002465 * @param boot_sequence A pointer to an array, whose aim is to carry
2466 * the boot order of candidate images.
2467 * @param slot_cnt The number of flash areas, which can contains firmware
2468 * images.
2469 *
2470 * @return The number of valid images.
2471 */
2472uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01002473boot_get_boot_sequence(struct boot_loader_state *state,
2474 uint32_t *boot_sequence, uint32_t slot_cnt)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002475{
2476 struct boot_swap_state slot_state;
2477 struct image_header *hdr;
2478 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2479 uint32_t image_cnt = 0;
2480 uint32_t slot;
2481 int32_t rc;
2482 int32_t fa_id;
2483
2484 for (slot = 0; slot < slot_cnt; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002485 hdr = boot_img_hdr(state, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002486 fa_id = flash_area_id_from_image_slot(slot);
2487 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2488 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002489 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2490 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002491 continue;
2492 }
2493
David Vinczecea8b592019-10-29 16:09:51 +01002494 if (BOOT_IMG_HDR_IS_VALID(state, slot)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002495 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002496 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002497 /* Valid cases:
2498 * - Test mode: magic is OK in image trailer
2499 * - Permanent mode: image_ok flag has previously set
2500 */
2501 image_versions[slot].slot_number = slot;
2502 image_versions[slot].version = boot_get_version_number(hdr);
2503 image_cnt++;
2504 }
2505
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002506 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2507 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002508 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002509 }
2510 }
2511
2512 /* Sort the images based on version number */
2513 qsort(&image_versions[0],
2514 slot_cnt,
2515 sizeof(struct image_slot_version),
2516 boot_compare_version_numbers);
2517
2518 /* Copy the calculated boot sequence to boot_sequence array */
2519 for (slot = 0; slot < slot_cnt; slot++) {
2520 boot_sequence[slot] = image_versions[slot].slot_number;
2521 }
2522
2523 return image_cnt;
2524}
2525
Oliver Swedef9982442018-08-24 18:37:44 +01002526#ifdef MCUBOOT_RAM_LOADING
Raef Colesaf082382019-10-01 11:10:33 +01002527
2528/**
2529 * Verifies that the image in a slot lies within the predefined bounds that are
2530 * allowed to be used by executable images.
2531 *
2532 * @param img_dst The address to which the image is going to be copied.
2533 *
2534 * @param img_sz The size of the image.
2535 *
2536 * @return 0 on success; nonzero on failure.
2537 */
2538static int
2539boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz)
2540{
2541 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2542 return BOOT_EBADIMAGE;
2543 }
2544
2545 if (boot_add_uint32_overflow_check(img_dst, img_sz)) {
2546 return BOOT_EBADIMAGE;
2547 }
2548
2549 if (img_dst + img_sz > IMAGE_EXECUTABLE_RAM_START +
2550 IMAGE_EXECUTABLE_RAM_SIZE) {
2551 return BOOT_EBADIMAGE;
2552 }
2553
2554 return 0;
2555}
2556
Oliver Swedef9982442018-08-24 18:37:44 +01002557/**
2558 * Copies an image from a slot in the flash to an SRAM address, where the load
2559 * address has already been inserted into the image header by this point and is
2560 * extracted from it within this method. The copying is done sector-by-sector.
2561 *
David Vinczecea8b592019-10-29 16:09:51 +01002562 * @param state Boot loader status information.
Oliver Swedef9982442018-08-24 18:37:44 +01002563 * @param slot The flash slot of the image to be copied to SRAM.
2564 *
2565 * @param hdr Pointer to the image header structure of the image
Raef Colesaf082382019-10-01 11:10:33 +01002566 *
2567 * @param img_dst The address at which the image needs to be copied to
2568 * SRAM.
2569 *
2570 * @param img_sz The size of the image that needs to be copied to SRAM.
Oliver Swedef9982442018-08-24 18:37:44 +01002571 *
2572 * @return 0 on success; nonzero on failure.
2573 */
2574static int
David Vinczecea8b592019-10-29 16:09:51 +01002575boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2576 struct image_header *hdr,
Raef Colesaf082382019-10-01 11:10:33 +01002577 uint32_t img_dst, uint32_t img_sz)
Oliver Swedef9982442018-08-24 18:37:44 +01002578{
2579 int rc;
2580 uint32_t sect_sz;
2581 uint32_t sect = 0;
2582 uint32_t bytes_copied = 0;
2583 const struct flash_area *fap_src = NULL;
Oliver Swedef9982442018-08-24 18:37:44 +01002584
Raef Colesaf082382019-10-01 11:10:33 +01002585 if (img_dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002586 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002587 "- the load address must be aligned with 4 bytes due to SRAM "
Raef Colesaf082382019-10-01 11:10:33 +01002588 "restrictions", img_dst);
Oliver Swedef9982442018-08-24 18:37:44 +01002589 return BOOT_EBADARGS;
2590 }
2591
2592 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2593 if (rc != 0) {
2594 return BOOT_EFLASH;
2595 }
2596
Oliver Swedef9982442018-08-24 18:37:44 +01002597 while (bytes_copied < img_sz) {
David Vinczecea8b592019-10-29 16:09:51 +01002598 sect_sz = boot_img_sector_size(state, slot, sect);
Oliver Swedef9982442018-08-24 18:37:44 +01002599 /*
2600 * Direct copy from where the image sector resides in flash to its new
2601 * location in SRAM
2602 */
2603 rc = flash_area_read(fap_src,
2604 bytes_copied,
Raef Colesaf082382019-10-01 11:10:33 +01002605 (void *)(img_dst + bytes_copied),
Oliver Swedef9982442018-08-24 18:37:44 +01002606 sect_sz);
2607 if (rc != 0) {
2608 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2609 break;
2610 } else {
2611 bytes_copied += sect_sz;
2612 }
2613 sect++;
2614 }
2615
2616 if (fap_src) {
2617 flash_area_close(fap_src);
2618 }
2619 return rc;
2620}
Raef Coles27a61452019-09-25 15:32:25 +01002621
2622/**
2623 * Removes an image from SRAM, by overwriting it with zeros.
2624 *
Raef Colesaf082382019-10-01 11:10:33 +01002625 * @param img_dst The address of the image that needs to be removed from
2626 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002627 *
Raef Colesaf082382019-10-01 11:10:33 +01002628 * @param img_sz The size of the image that needs to be removed from
2629 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002630 *
2631 * @return 0 on success; nonzero on failure.
2632 */
2633static int
Raef Colesaf082382019-10-01 11:10:33 +01002634boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
Raef Coles27a61452019-09-25 15:32:25 +01002635{
Raef Colesaf082382019-10-01 11:10:33 +01002636 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2637 memset((void*)img_dst, 0, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002638
2639 return 0;
2640}
Oliver Swedef9982442018-08-24 18:37:44 +01002641#endif /* MCUBOOT_RAM_LOADING */
2642
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002643/**
2644 * Prepares the booting process. This function choose the newer image in flash
David Vinczecea8b592019-10-29 16:09:51 +01002645 * as appropriate, and tells you what address to boot from.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002646 *
David Vinczecea8b592019-10-29 16:09:51 +01002647 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002648 * @param rsp On success, indicates how booting should occur.
2649 *
2650 * @return 0 on success; nonzero on failure.
2651 */
2652int
David Vinczecea8b592019-10-29 16:09:51 +01002653context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002654{
2655 size_t slot = 0;
2656 int32_t i;
2657 int rc;
2658 int fa_id;
2659 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2660 uint32_t img_cnt;
Raef Coles27a61452019-09-25 15:32:25 +01002661 struct image_header *selected_image_header;
2662#ifdef MCUBOOT_RAM_LOADING
2663 int image_copied = 0;
Raef Colesaf082382019-10-01 11:10:33 +01002664 uint32_t img_dst = 0;
2665 uint32_t img_sz = 0;
Raef Coles27a61452019-09-25 15:32:25 +01002666#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002667
David Vincze8bdfc2d2019-03-18 15:49:23 +01002668 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2669 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002670
David Vinczecea8b592019-10-29 16:09:51 +01002671 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = &primary_slot_sectors[0];
2672 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002673
2674 /* Open boot_data image areas for the duration of this call. */
2675 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2676 fa_id = flash_area_id_from_image_slot(i);
David Vinczecea8b592019-10-29 16:09:51 +01002677 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002678 assert(rc == 0);
2679 }
2680
2681 /* Determine the sector layout of the image slots. */
David Vinczecea8b592019-10-29 16:09:51 +01002682 rc = boot_read_sectors(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002683 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002684 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - "
2685 "too small?", BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002686 goto out;
2687 }
2688
2689 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002690 rc = boot_read_image_headers(state, false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002691 if (rc != 0) {
2692 goto out;
2693 }
2694
David Vinczecea8b592019-10-29 16:09:51 +01002695 img_cnt = boot_get_boot_sequence(state, boot_sequence, BOOT_NUM_SLOTS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002696 if (img_cnt) {
2697 /* Authenticate images */
2698 for (i = 0; i < img_cnt; i++) {
Raef Coles27a61452019-09-25 15:32:25 +01002699
2700 slot = boot_sequence[i];
David Vinczecea8b592019-10-29 16:09:51 +01002701 selected_image_header = boot_img_hdr(state, slot);
Raef Coles27a61452019-09-25 15:32:25 +01002702
2703#ifdef MCUBOOT_RAM_LOADING
2704 if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
Raef Colesaf082382019-10-01 11:10:33 +01002705
2706 img_dst = selected_image_header->ih_load_addr;
2707
David Vinczecea8b592019-10-29 16:09:51 +01002708 rc = boot_read_image_size(state, slot, &img_sz);
Raef Colesaf082382019-10-01 11:10:33 +01002709 if (rc != 0) {
2710 rc = BOOT_EFLASH;
2711 BOOT_LOG_INF("Could not load image headers from the image"
2712 "in the %s slot.",
2713 (slot == BOOT_PRIMARY_SLOT) ?
2714 "primary" : "secondary");
2715 continue;
2716 }
2717
2718 rc = boot_verify_ram_loading_address(img_dst, img_sz);
2719 if (rc != 0) {
2720 BOOT_LOG_INF("Could not copy image from the %s slot in "
2721 "the Flash to load address 0x%x in SRAM as"
2722 " the image would overlap memory outside"
2723 " the defined executable region.",
2724 (slot == BOOT_PRIMARY_SLOT) ?
2725 "primary" : "secondary",
2726 selected_image_header->ih_load_addr);
2727 continue;
2728 }
2729
Raef Coles27a61452019-09-25 15:32:25 +01002730 /* Copy image to the load address from where it
2731 * currently resides in flash
2732 */
David Vinczecea8b592019-10-29 16:09:51 +01002733 rc = boot_copy_image_to_sram(state, slot, selected_image_header,
Raef Colesaf082382019-10-01 11:10:33 +01002734 img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002735 if (rc != 0) {
2736 rc = BOOT_EBADIMAGE;
2737 BOOT_LOG_INF("Could not copy image from the %s slot in "
2738 "the Flash to load address 0x%x in SRAM, "
2739 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2740 "primary" : "secondary",
2741 selected_image_header->ih_load_addr);
2742 continue;
2743 } else {
2744 BOOT_LOG_INF("Image has been copied from the %s slot in "
2745 "the flash to SRAM address 0x%x",
2746 (slot == BOOT_PRIMARY_SLOT) ?
2747 "primary" : "secondary",
2748 selected_image_header->ih_load_addr);
2749 image_copied = 1;
2750 }
2751 } else {
2752 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2753 * MCUBOOT_RAM_LOADING is set.
2754 */
2755 rc = BOOT_EBADIMAGE;
2756 continue;
2757 }
2758#endif /* MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002759 rc = boot_validate_slot(state, slot, NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002760 if (rc == 0) {
Raef Coles27a61452019-09-25 15:32:25 +01002761 /* If a valid image is found then there is no reason to check
2762 * the rest of the images, as they were already ordered by
2763 * preference.
2764 */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002765 break;
2766 }
Raef Coles27a61452019-09-25 15:32:25 +01002767#ifdef MCUBOOT_RAM_LOADING
2768 else if (image_copied) {
2769 /* If an image is found to be invalid then it is removed from
2770 * RAM to prevent it being a shellcode vector.
2771 */
Raef Colesaf082382019-10-01 11:10:33 +01002772 boot_remove_image_from_sram(img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002773 image_copied = 0;
2774 }
2775#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002776 }
2777 if (rc) {
2778 /* If there was no valid image at all */
2779 rc = BOOT_EBADIMAGE;
2780 goto out;
2781 }
2782
David Vincze060968d2019-05-23 01:13:14 +02002783 /* Update the security counter with the newest image's security
2784 * counter value.
2785 */
David Vinczecea8b592019-10-29 16:09:51 +01002786 rc = boot_update_security_counter(BOOT_CURR_IMG(state), slot,
2787 selected_image_header);
David Vincze060968d2019-05-23 01:13:14 +02002788 if (rc != 0) {
2789 BOOT_LOG_ERR("Security counter update failed after image "
2790 "validation.");
2791 goto out;
2792 }
2793
Oliver Swedef9982442018-08-24 18:37:44 +01002794
David Vincze8a2a4e22019-05-24 10:14:23 +02002795#ifdef MCUBOOT_RAM_LOADING
Raef Coles27a61452019-09-25 15:32:25 +01002796 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
2797 selected_image_header->ih_load_addr);
2798#else
2799 BOOT_LOG_INF("Booting image from the %s slot",
2800 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2801#endif /* MCUBOOT_RAM_LOADING */
Oliver Swedef9982442018-08-24 18:37:44 +01002802
Raef Coles27a61452019-09-25 15:32:25 +01002803 rsp->br_hdr = selected_image_header;
David Vinczecea8b592019-10-29 16:09:51 +01002804 rsp->br_image_off = boot_img_slot_off(state, slot);
2805 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002806 } else {
2807 /* No candidate image available */
2808 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002809 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002810 }
2811
Tamas Ban0e8ab302019-01-17 11:45:31 +00002812 /* Save boot status to shared memory area */
2813 rc = boot_save_boot_status(SW_S_NS,
2814 rsp->br_hdr,
David Vinczecea8b592019-10-29 16:09:51 +01002815 BOOT_IMG_AREA(state, slot));
Tamas Ban0e8ab302019-01-17 11:45:31 +00002816 if (rc) {
2817 BOOT_LOG_ERR("Failed to add data to shared area");
2818 }
2819
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002820out:
2821 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002822 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002823 }
2824 return rc;
2825}
Oliver Swedef9982442018-08-24 18:37:44 +01002826#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002827
2828int
2829boot_go(struct boot_rsp *rsp)
2830{
2831 return context_boot_go(&boot_data, rsp);
2832}