blob: 9f47591b756ea7996d84c5673df81b6d0e6074c3 [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 Vinczecea8b592019-10-29 16:09:51 +010023 * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
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/*
David Vinczecea8b592019-10-29 16:09:51 +0100162 * Locate the TLVs in an image.
163 *
164 * @param hdr The image_header struct of the image being checked
165 * @param fap flash_area struct of the slot storing the image being checked
166 * @param off Address of the first TLV (after TLV info)
167 * @param end Address where TLV area ends
168 *
169 * Returns 0 on success.
170 */
171int
172boot_find_tlv_offs(const struct image_header *hdr, const struct flash_area *fap,
David Vinczec2566122019-10-25 13:18:54 +0200173 uint32_t *off, uint32_t *end)
David Vinczecea8b592019-10-29 16:09:51 +0100174{
175 struct image_tlv_info info;
176 uint32_t off_;
177
178 off_ = BOOT_TLV_OFF(hdr);
179
180 if (LOAD_IMAGE_DATA(fap, off_, &info, sizeof(info))) {
181 return BOOT_EFLASH;
182 }
183
184 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
185 return BOOT_EBADIMAGE;
186 }
187
David Vinczec2566122019-10-25 13:18:54 +0200188 if (boot_add_uint32_overflow_check(off_, info.it_tlv_tot))
189 {
190 return -1;
191 }
192
David Vinczecea8b592019-10-29 16:09:51 +0100193 *end = off_ + info.it_tlv_tot;
194 *off = off_ + sizeof(info);
195 return 0;
196}
197
198/*
Tamas Ban056ed0b2019-09-16 12:48:32 +0100199 * \brief Verifies the image header: magic value, flags, integer overflow.
200 *
201 * \retval 0
202 * \retval BOOT_EBADIMAGE
203 */
204static int
205boot_verify_image_header(struct image_header *hdr)
206{
207 uint32_t image_end;
208
209 if (hdr->ih_magic != IMAGE_MAGIC) {
210 return BOOT_EBADIMAGE;
211 }
212
213 /* Check input parameters against integer overflow */
214 if (boot_add_uint32_overflow_check(hdr->ih_hdr_size, hdr->ih_img_size)) {
215 return BOOT_EBADIMAGE;
216 }
217
218 image_end = hdr->ih_hdr_size + hdr->ih_img_size;
219 if (boot_add_uint32_overflow_check(image_end, hdr->ih_protect_tlv_size)) {
220 return BOOT_EBADIMAGE;
221 }
222
223
224#if MCUBOOT_RAM_LOADING
225 if (!(hdr->ih_flags & IMAGE_F_RAM_LOAD)) {
226 return BOOT_EBADIMAGE;
227 }
228
229 /* Check input parameters against integer overflow */
230 if (boot_add_uint32_overflow_check(image_end, hdr->ih_load_addr)) {
231 return BOOT_EBADIMAGE;
232 }
233#endif
234
235 return 0;
236}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000237
238static int
David Vinczecea8b592019-10-29 16:09:51 +0100239boot_read_image_header(struct boot_loader_state *state, int slot,
240 struct image_header *out_hdr)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000241{
242 const struct flash_area *fap = NULL;
243 int area_id;
244 int rc;
245
David Vinczecea8b592019-10-29 16:09:51 +0100246#if (BOOT_IMAGE_NUMBER == 1)
247 (void)state;
248#endif
249
250 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000251 rc = flash_area_open(area_id, &fap);
252 if (rc != 0) {
253 rc = BOOT_EFLASH;
254 goto done;
255 }
256
257 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
258 if (rc != 0) {
259 rc = BOOT_EFLASH;
260 goto done;
261 }
262
Tamas Ban056ed0b2019-09-16 12:48:32 +0100263 rc = boot_verify_image_header(out_hdr);
David Vinczecea8b592019-10-29 16:09:51 +0100264 BOOT_IMG_HDR_IS_VALID(state, slot) = (rc == 0);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000265
266done:
267 flash_area_close(fap);
268 return rc;
269}
270
271static int
David Vinczecea8b592019-10-29 16:09:51 +0100272boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000273{
274 int rc;
275 int i;
276
277 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100278 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000279 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200280 /* If `require_all` is set, fail on any single fail, otherwise
281 * if at least the first slot's header was read successfully,
282 * then the boot loader can attempt a boot.
283 *
284 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000285 */
David Vincze39e78552018-10-10 17:10:01 +0200286 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000287 return 0;
288 } else {
289 return rc;
290 }
291 }
292 }
293
294 return 0;
295}
296
Raef Coles204c5b42019-09-05 09:18:47 +0100297static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +0100298boot_write_sz(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000299{
Raef Coles204c5b42019-09-05 09:18:47 +0100300 uint32_t elem_sz;
301 uint32_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000302
303 /* Figure out what size to write update status update as. The size depends
304 * on what the minimum write size is for scratch area, active image slot.
305 * We need to use the bigger of those 2 values.
306 */
David Vinczecea8b592019-10-29 16:09:51 +0100307 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
308 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000309 if (align > elem_sz) {
310 elem_sz = align;
311 }
312
313 return elem_sz;
314}
315
David Vinczecea8b592019-10-29 16:09:51 +0100316#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
317static int
318boot_initialize_area(struct boot_loader_state *state, int flash_area)
319{
320 int num_sectors = BOOT_MAX_IMG_SECTORS;
321 int rc;
322
323 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
324 rc = flash_area_to_sectors(flash_area, &num_sectors,
325 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
326 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
327
328 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
329 rc = flash_area_to_sectors(flash_area, &num_sectors,
330 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
331 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
332
333 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
334 rc = flash_area_to_sectors(flash_area, &num_sectors,
335 state->scratch.sectors);
336 state->scratch.num_sectors = (size_t)num_sectors;
337 } else {
338 return BOOT_EFLASH;
339 }
340
341 return rc;
342}
343#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
344static int
345boot_initialize_area(struct boot_loader_state *state, int flash_area)
346{
347 uint32_t num_sectors;
348 struct flash_sector *out_sectors;
349 size_t *out_num_sectors;
350 int rc;
351
352 num_sectors = BOOT_MAX_IMG_SECTORS;
353
354 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
355 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
356 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
357 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
358 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
359 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
360 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
361 out_sectors = state->scratch.sectors;
362 out_num_sectors = &state->scratch.num_sectors;
363 } else {
364 return BOOT_EFLASH;
365 }
366
367 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
368 if (rc != 0) {
369 return rc;
370 }
371 *out_num_sectors = num_sectors;
372 return 0;
373}
374#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
375
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000376/**
377 * Determines the sector layout of both image slots and the scratch area.
378 * This information is necessary for calculating the number of bytes to erase
379 * and copy during an image swap. The information collected during this
David Vinczecea8b592019-10-29 16:09:51 +0100380 * function is used to populate the state.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000381 */
382static int
David Vinczecea8b592019-10-29 16:09:51 +0100383boot_read_sectors(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000384{
David Vinczecea8b592019-10-29 16:09:51 +0100385 uint8_t image_index;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000386 int rc;
387
David Vinczecea8b592019-10-29 16:09:51 +0100388 image_index = BOOT_CURR_IMG(state);
389
390 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000391 if (rc != 0) {
392 return BOOT_EFLASH;
393 }
394
David Vinczecea8b592019-10-29 16:09:51 +0100395 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000396 if (rc != 0) {
397 return BOOT_EFLASH;
398 }
399
David Vinczecea8b592019-10-29 16:09:51 +0100400 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
David Vincze401c7422019-06-21 20:44:05 +0200401 if (rc != 0) {
402 return BOOT_EFLASH;
403 }
404
David Vinczecea8b592019-10-29 16:09:51 +0100405 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000406
407 return 0;
408}
409
David Vincze060968d2019-05-23 01:13:14 +0200410/**
411 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000412 */
413static int
David Vinczecea8b592019-10-29 16:09:51 +0100414boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
415 const struct flash_area *fap, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000416{
417 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
David Vinczecea8b592019-10-29 16:09:51 +0100418 uint8_t image_index;
419
420#if (BOOT_IMAGE_NUMBER == 1)
421 (void)state;
422#endif
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000423
David Vincze401c7422019-06-21 20:44:05 +0200424 (void)bs;
425
David Vinczecea8b592019-10-29 16:09:51 +0100426 image_index = BOOT_CURR_IMG(state);
427
428 if (bootutil_img_validate(image_index, hdr, fap, tmpbuf,
429 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000430 return BOOT_EBADIMAGE;
431 }
David Vinczecea8b592019-10-29 16:09:51 +0100432
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000433 return 0;
434}
435
David Vincze401c7422019-06-21 20:44:05 +0200436/*
437 * Check that a memory area consists of a given value.
438 */
439static inline bool
440boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200441{
442 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200443 uint8_t *p = (uint8_t *)data;
444 for (i = 0; i < len; i++) {
445 if (val != p[i]) {
446 return false;
David Vincze39e78552018-10-10 17:10:01 +0200447 }
448 }
David Vincze401c7422019-06-21 20:44:05 +0200449 return true;
David Vincze39e78552018-10-10 17:10:01 +0200450}
451
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000452static int
David Vinczecea8b592019-10-29 16:09:51 +0100453boot_check_header_erased(struct boot_loader_state *state, int slot)
David Vincze401c7422019-06-21 20:44:05 +0200454{
455 const struct flash_area *fap;
456 struct image_header *hdr;
457 uint8_t erased_val;
David Vinczecea8b592019-10-29 16:09:51 +0100458 int area_id;
David Vincze401c7422019-06-21 20:44:05 +0200459 int rc;
460
David Vinczecea8b592019-10-29 16:09:51 +0100461 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
462 rc = flash_area_open(area_id, &fap);
David Vincze401c7422019-06-21 20:44:05 +0200463 if (rc != 0) {
464 return -1;
465 }
466
467 erased_val = flash_area_erased_val(fap);
468 flash_area_close(fap);
469
David Vinczecea8b592019-10-29 16:09:51 +0100470 hdr = boot_img_hdr(state, slot);
David Vincze401c7422019-06-21 20:44:05 +0200471 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
472 sizeof(hdr->ih_magic))) {
473 return -1;
474 }
475
476 return 0;
477}
478
David Vinczecea8b592019-10-29 16:09:51 +0100479/*
480 * Check that there is a valid image in a slot
481 *
482 * @returns
483 * 0 if image was succesfully validated
484 * 1 if no bootloable image was found
485 * -1 on any errors
486 */
David Vincze401c7422019-06-21 20:44:05 +0200487static int
David Vinczecea8b592019-10-29 16:09:51 +0100488boot_validate_slot(struct boot_loader_state *state, int slot,
489 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000490{
491 const struct flash_area *fap;
492 struct image_header *hdr;
David Vinczecea8b592019-10-29 16:09:51 +0100493 int area_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000494 int rc;
495
David Vinczecea8b592019-10-29 16:09:51 +0100496 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
497 rc = flash_area_open(area_id, &fap);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000498 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +0100499 return -1;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000500 }
501
David Vinczecea8b592019-10-29 16:09:51 +0100502 hdr = boot_img_hdr(state, slot);
503 if ((boot_check_header_erased(state, slot) == 0) ||
David Vincze401c7422019-06-21 20:44:05 +0200504 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100505 /* No bootable image in slot; continue booting from the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100506 rc = 1;
David Vincze401c7422019-06-21 20:44:05 +0200507 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200508 }
509
David Vinczecea8b592019-10-29 16:09:51 +0100510 if ((!BOOT_IMG_HDR_IS_VALID(state, slot)) ||
511 (boot_image_check(state, hdr, fap, bs) != 0)) {
David Vincze401c7422019-06-21 20:44:05 +0200512 if (slot != BOOT_PRIMARY_SLOT) {
David Vinczecea8b592019-10-29 16:09:51 +0100513 flash_area_erase(fap, 0, fap->fa_size);
David Vincze8bdfc2d2019-03-18 15:49:23 +0100514 /* Image in the secondary slot is invalid. Erase the image and
515 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000516 */
517 }
David Vinczecea8b592019-10-29 16:09:51 +0100518 BOOT_LOG_ERR("Image in the %s slot is not valid!",
519 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200520 rc = -1;
521 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000522 }
523
David Vincze8bdfc2d2019-03-18 15:49:23 +0100524 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200525 rc = 0;
526
527out:
528 flash_area_close(fap);
529 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000530}
531
David Vincze060968d2019-05-23 01:13:14 +0200532/**
533 * Updates the stored security counter value with the image's security counter
534 * value which resides in the given slot if it's greater than the stored value.
535 *
David Vinczecea8b592019-10-29 16:09:51 +0100536 * @param image_index Index of the image to determine which security
537 * counter to update.
538 * @param slot Slot number of the image.
539 * @param hdr Pointer to the image header structure of the image
540 * that is currently stored in the given slot.
David Vincze060968d2019-05-23 01:13:14 +0200541 *
David Vinczecea8b592019-10-29 16:09:51 +0100542 * @return 0 on success; nonzero on failure.
David Vincze060968d2019-05-23 01:13:14 +0200543 */
544static int
David Vinczecea8b592019-10-29 16:09:51 +0100545boot_update_security_counter(uint8_t image_index, int slot,
546 struct image_header *hdr)
David Vincze060968d2019-05-23 01:13:14 +0200547{
548 const struct flash_area *fap = NULL;
549 uint32_t img_security_cnt;
550 int rc;
551
David Vinczecea8b592019-10-29 16:09:51 +0100552 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
553 &fap);
David Vincze060968d2019-05-23 01:13:14 +0200554 if (rc != 0) {
555 rc = BOOT_EFLASH;
556 goto done;
557 }
558
559 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
560 if (rc != 0) {
561 goto done;
562 }
563
David Vinczecea8b592019-10-29 16:09:51 +0100564 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
David Vincze060968d2019-05-23 01:13:14 +0200565 if (rc != 0) {
566 goto done;
567 }
568
569done:
570 flash_area_close(fap);
571 return rc;
572}
573
Oliver Swedef9982442018-08-24 18:37:44 +0100574#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
575/*
576 * Compute the total size of the given image. Includes the size of
577 * the TLVs.
578 */
579static int
David Vinczecea8b592019-10-29 16:09:51 +0100580boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
Oliver Swedef9982442018-08-24 18:37:44 +0100581{
582 const struct flash_area *fap = NULL;
David Vinczecea8b592019-10-29 16:09:51 +0100583 uint32_t off;
Oliver Swedef9982442018-08-24 18:37:44 +0100584 int area_id;
585 int rc;
586
David Vinczecea8b592019-10-29 16:09:51 +0100587#if (BOOT_IMAGE_NUMBER == 1)
588 (void)state;
589#endif
590
591 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Oliver Swedef9982442018-08-24 18:37:44 +0100592 rc = flash_area_open(area_id, &fap);
593 if (rc != 0) {
594 rc = BOOT_EFLASH;
595 goto done;
596 }
597
David Vinczecea8b592019-10-29 16:09:51 +0100598 rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, size);
Oliver Swedef9982442018-08-24 18:37:44 +0100599 if (rc != 0) {
Oliver Swedef9982442018-08-24 18:37:44 +0100600 goto done;
601 }
Oliver Swedef9982442018-08-24 18:37:44 +0100602 rc = 0;
603
604done:
605 flash_area_close(fap);
606 return rc;
607}
608#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
609
610#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000611/**
Tamas Ban581034a2017-12-19 19:54:37 +0000612 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000613 * status is necessary for completing a swap that was interrupted by a boot
614 * loader reset.
615 *
David Vincze401c7422019-06-21 20:44:05 +0200616 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
617 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000618 */
619static int
David Vinczecea8b592019-10-29 16:09:51 +0100620boot_status_source(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000621{
622 const struct boot_status_table *table;
623 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100624 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000625 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200626 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000627 uint8_t source;
David Vinczecea8b592019-10-29 16:09:51 +0100628 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000629
David Vinczecea8b592019-10-29 16:09:51 +0100630#if (BOOT_IMAGE_NUMBER == 1)
631 (void)state;
632#endif
633
634 image_index = BOOT_CURR_IMG(state);
635 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
636 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000637 assert(rc == 0);
638
639 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
640 assert(rc == 0);
641
David Vincze401c7422019-06-21 20:44:05 +0200642 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000643 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
644
645 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
646 table = &boot_status_tables[i];
647
David Vincze401c7422019-06-21 20:44:05 +0200648 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
649 state_primary_slot.magic) &&
650 boot_magic_compatible_check(table->bst_magic_scratch,
651 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100652 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
653 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
654 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000655 source = table->bst_status_source;
David Vincze7384ee72019-07-23 17:00:42 +0200656
657#if (BOOT_IMAGE_NUMBER > 1)
658 /* In case of multi-image boot it can happen that if boot status
659 * info is found on scratch area then it does not belong to the
660 * currently examined image.
661 */
662 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
David Vinczecea8b592019-10-29 16:09:51 +0100663 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +0200664 source = BOOT_STATUS_SOURCE_NONE;
665 }
666#endif
667
Tamas Banf70ef8c2017-12-19 15:35:09 +0000668 BOOT_LOG_INF("Boot source: %s",
669 source == BOOT_STATUS_SOURCE_NONE ? "none" :
670 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100671 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
672 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000673 return source;
674 }
675 }
676
677 BOOT_LOG_INF("Boot source: none");
678 return BOOT_STATUS_SOURCE_NONE;
679}
680
David Vincze401c7422019-06-21 20:44:05 +0200681/*
682 * Slots are compatible when all sectors that store upto to size of the image
683 * round up to sector size, in both slot's are able to fit in the scratch
684 * area, and have sizes that are a multiple of each other (powers of two
685 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000686 */
687static int
David Vinczecea8b592019-10-29 16:09:51 +0100688boot_slots_compatible(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000689{
David Vincze401c7422019-06-21 20:44:05 +0200690 size_t num_sectors_primary;
691 size_t num_sectors_secondary;
692 size_t sz0, sz1;
693 size_t primary_slot_sz, secondary_slot_sz;
694 size_t scratch_sz;
695 size_t i, j;
696 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000697
David Vinczecea8b592019-10-29 16:09:51 +0100698 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
699 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze401c7422019-06-21 20:44:05 +0200700 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
701 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200702 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000703 return 0;
704 }
David Vincze39e78552018-10-10 17:10:01 +0200705
David Vinczecea8b592019-10-29 16:09:51 +0100706 scratch_sz = boot_scratch_area_size(state);
David Vincze401c7422019-06-21 20:44:05 +0200707
708 /*
709 * The following loop scans all sectors in a linear fashion, assuring that
710 * for each possible sector in each slot, it is able to fit in the other
711 * slot's sector or sectors. Slot's should be compatible as long as any
712 * number of a slot's sectors are able to fit into another, which only
713 * excludes cases where sector sizes are not a multiple of each other.
714 */
715 i = sz0 = primary_slot_sz = 0;
716 j = sz1 = secondary_slot_sz = 0;
717 smaller = 0;
718 while (i < num_sectors_primary || j < num_sectors_secondary) {
719 if (sz0 == sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100720 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
721 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200722 i++;
723 j++;
724 } else if (sz0 < sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100725 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200726 /* Guarantee that multiple sectors of the secondary slot
727 * fit into the primary slot.
728 */
729 if (smaller == 2) {
730 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
731 " sectors");
732 return 0;
733 }
734 smaller = 1;
735 i++;
736 } else {
David Vinczecea8b592019-10-29 16:09:51 +0100737 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200738 /* Guarantee that multiple sectors of the primary slot
739 * fit into the secondary slot.
740 */
741 if (smaller == 1) {
742 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
743 " sectors");
744 return 0;
745 }
746 smaller = 2;
747 j++;
748 }
749 if (sz0 == sz1) {
750 primary_slot_sz += sz0;
751 secondary_slot_sz += sz1;
752 /* Scratch has to fit each swap operation to the size of the larger
753 * sector among the primary slot and the secondary slot.
754 */
755 if (sz0 > scratch_sz || sz1 > scratch_sz) {
756 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
757 " scratch");
758 return 0;
759 }
760 smaller = sz0 = sz1 = 0;
761 }
David Vincze39e78552018-10-10 17:10:01 +0200762 }
763
David Vincze401c7422019-06-21 20:44:05 +0200764 if ((i != num_sectors_primary) ||
765 (j != num_sectors_secondary) ||
766 (primary_slot_sz != secondary_slot_sz)) {
767 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
768 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000769 }
770
771 return 1;
772}
773
Tamas Banf70ef8c2017-12-19 15:35:09 +0000774static uint32_t
775boot_status_internal_off(int idx, int state, int elem_sz)
776{
777 int idx_sz;
778
779 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
780
David Vincze39e78552018-10-10 17:10:01 +0200781 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
782 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000783}
784
785/**
786 * Reads the status of a partially-completed swap, if any. This is necessary
787 * to recover in case the boot lodaer was reset in the middle of a swap
788 * operation.
789 */
790static int
David Vinczecea8b592019-10-29 16:09:51 +0100791boot_read_status_bytes(const struct flash_area *fap,
792 struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000793{
794 uint32_t off;
795 uint8_t status;
796 int max_entries;
797 int found;
David Vincze39e78552018-10-10 17:10:01 +0200798 int found_idx;
799 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000800 int rc;
801 int i;
802
803 off = boot_status_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100804 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
805 if (max_entries < 0) {
806 return BOOT_EBADARGS;
807 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000808
809 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200810 found_idx = 0;
811 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000812 for (i = 0; i < max_entries; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100813 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
David Vincze39e78552018-10-10 17:10:01 +0200814 &status, 1);
815 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000816 return BOOT_EFLASH;
817 }
818
David Vincze39e78552018-10-10 17:10:01 +0200819 if (rc == 1) {
820 if (found && !found_idx) {
821 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000822 }
823 } else if (!found) {
824 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200825 } else if (found_idx) {
826 invalid = 1;
827 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000828 }
829 }
830
David Vincze39e78552018-10-10 17:10:01 +0200831 if (invalid) {
832 /* This means there was an error writing status on the last
833 * swap. Tell user and move on to validation!
834 */
835 BOOT_LOG_ERR("Detected inconsistent status!");
836
David Vincze8bdfc2d2019-03-18 15:49:23 +0100837#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
838 /* With validation of the primary slot disabled, there is no way
839 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200840 */
841 assert(0);
842#endif
843 }
844
Tamas Banf70ef8c2017-12-19 15:35:09 +0000845 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200846 if (!found_idx) {
847 found_idx = i;
848 }
David Vincze39e78552018-10-10 17:10:01 +0200849 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
850 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000851 }
852
853 return 0;
854}
855
856/**
857 * Reads the boot status from the flash. The boot status contains
858 * the current state of an interrupted image copy operation. If the boot
859 * status is not present, or it indicates that previous copy finished,
860 * there is no operation in progress.
861 */
862static int
David Vinczecea8b592019-10-29 16:09:51 +0100863boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000864{
865 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200866 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200867 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000868 int status_loc;
869 int area_id;
870 int rc;
871
David Vincze39e78552018-10-10 17:10:01 +0200872 memset(bs, 0, sizeof *bs);
873 bs->idx = BOOT_STATUS_IDX_0;
874 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200875 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200876
877#ifdef MCUBOOT_OVERWRITE_ONLY
878 /* Overwrite-only doesn't make use of the swap status area. */
879 return 0;
880#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000881
David Vinczecea8b592019-10-29 16:09:51 +0100882 status_loc = boot_status_source(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000883 switch (status_loc) {
884 case BOOT_STATUS_SOURCE_NONE:
885 return 0;
886
887 case BOOT_STATUS_SOURCE_SCRATCH:
888 area_id = FLASH_AREA_IMAGE_SCRATCH;
889 break;
890
David Vincze8bdfc2d2019-03-18 15:49:23 +0100891 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
David Vinczecea8b592019-10-29 16:09:51 +0100892 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000893 break;
894
895 default:
896 assert(0);
897 return BOOT_EBADARGS;
898 }
899
900 rc = flash_area_open(area_id, &fap);
901 if (rc != 0) {
902 return BOOT_EFLASH;
903 }
904
David Vinczecea8b592019-10-29 16:09:51 +0100905 rc = boot_read_status_bytes(fap, state, bs);
David Vincze401c7422019-06-21 20:44:05 +0200906 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200907 off = boot_swap_info_off(fap);
908 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200909 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200910 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200911 rc = 0;
912 }
David Vincze91b71ef2019-06-24 13:06:47 +0200913
914 /* Extract the swap type info */
915 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200916 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000917
918 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200919
Tamas Banf70ef8c2017-12-19 15:35:09 +0000920 return rc;
921}
922
923/**
924 * Writes the supplied boot status to the flash file system. The boot status
925 * contains the current state of an in-progress image copy operation.
926 *
927 * @param bs The boot status to write.
928 *
929 * @return 0 on success; nonzero on failure.
930 */
931int
David Vinczecea8b592019-10-29 16:09:51 +0100932boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000933{
Tamas Ban581034a2017-12-19 19:54:37 +0000934 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000935 uint32_t off;
936 int area_id;
937 int rc;
938 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100939 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200940 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000941
942 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100943 * the trailer. Since in the last step the primary slot is erased, the
944 * first two status writes go to the scratch which will be copied to
945 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000946 */
947
948 if (bs->use_scratch) {
949 /* Write to scratch. */
950 area_id = FLASH_AREA_IMAGE_SCRATCH;
951 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100952 /* Write to the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100953 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000954 }
955
956 rc = flash_area_open(area_id, &fap);
957 if (rc != 0) {
958 rc = BOOT_EFLASH;
959 goto done;
960 }
961
962 off = boot_status_off(fap) +
David Vinczecea8b592019-10-29 16:09:51 +0100963 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Tamas Banc3828852018-02-01 12:24:16 +0000964 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200965 erased_val = flash_area_erased_val(fap);
966 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000967 buf[0] = bs->state;
968
969 rc = flash_area_write(fap, off, buf, align);
970 if (rc != 0) {
971 rc = BOOT_EFLASH;
972 goto done;
973 }
974
975 rc = 0;
976
977done:
978 flash_area_close(fap);
979 return rc;
980}
981
Tamas Banf70ef8c2017-12-19 15:35:09 +0000982/**
983 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100984 * that a swap operation is required, the image in the secondary slot is checked
985 * for validity. If the image in the secondary slot is invalid, it is erased,
986 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000987 *
988 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
989 */
990static int
David Vinczecea8b592019-10-29 16:09:51 +0100991boot_validated_swap_type(struct boot_loader_state *state,
992 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000993{
994 int swap_type;
David Vinczecea8b592019-10-29 16:09:51 +0100995 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000996
David Vinczecea8b592019-10-29 16:09:51 +0100997 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
998 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100999 /* Boot loader wants to switch to the secondary slot.
1000 * Ensure image is valid.
1001 */
David Vinczecea8b592019-10-29 16:09:51 +01001002 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
1003 if (rc == 1) {
1004 swap_type = BOOT_SWAP_TYPE_NONE;
1005 } else if (rc != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001006 swap_type = BOOT_SWAP_TYPE_FAIL;
1007 }
1008 }
1009
1010 return swap_type;
1011}
1012
1013/**
1014 * Calculates the number of sectors the scratch area can contain. A "last"
1015 * source sector is specified because images are copied backwards in flash
1016 * (final index to index number 0).
1017 *
1018 * @param last_sector_idx The index of the last source sector
1019 * (inclusive).
1020 * @param out_first_sector_idx The index of the first source sector
1021 * (inclusive) gets written here.
1022 *
1023 * @return The number of bytes comprised by the
1024 * [first-sector, last-sector] range.
1025 */
1026#ifndef MCUBOOT_OVERWRITE_ONLY
1027static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01001028boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
1029 int *out_first_sector_idx)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001030{
1031 size_t scratch_sz;
1032 uint32_t new_sz;
1033 uint32_t sz;
1034 int i;
1035
1036 sz = 0;
1037
David Vinczecea8b592019-10-29 16:09:51 +01001038 scratch_sz = boot_scratch_area_size(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001039 for (i = last_sector_idx; i >= 0; i--) {
David Vinczecea8b592019-10-29 16:09:51 +01001040 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +02001041 /*
1042 * The secondary slot is not being checked here, because
1043 * `boot_slots_compatible` already provides assurance that the copy size
1044 * will be compatible with the primary slot and scratch.
1045 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001046 if (new_sz > scratch_sz) {
1047 break;
1048 }
1049 sz = new_sz;
1050 }
1051
1052 /* i currently refers to a sector that doesn't fit or it is -1 because all
1053 * sectors have been processed. In both cases, exclude sector i.
1054 */
1055 *out_first_sector_idx = i + 1;
1056 return sz;
1057}
1058#endif /* !MCUBOOT_OVERWRITE_ONLY */
1059
1060/**
David Vinczef7641fa2018-09-04 18:29:46 +02001061 * Erases a region of flash.
1062 *
David Vincze401c7422019-06-21 20:44:05 +02001063 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +02001064 * @param off The offset within the flash area to start the
1065 * erase.
1066 * @param sz The number of bytes to erase.
1067 *
1068 * @return 0 on success; nonzero on failure.
1069 */
David Vincze401c7422019-06-21 20:44:05 +02001070static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001071boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +02001072{
David Vincze401c7422019-06-21 20:44:05 +02001073 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001074}
1075
1076/**
Tamas Banf70ef8c2017-12-19 15:35:09 +00001077 * Copies the contents of one flash region to another. You must erase the
1078 * destination region prior to calling this function.
1079 *
1080 * @param flash_area_id_src The ID of the source flash area.
1081 * @param flash_area_id_dst The ID of the destination flash area.
1082 * @param off_src The offset within the source flash area to
1083 * copy from.
1084 * @param off_dst The offset within the destination flash area to
1085 * copy to.
1086 * @param sz The number of bytes to copy.
1087 *
1088 * @return 0 on success; nonzero on failure.
1089 */
1090static int
David Vinczecea8b592019-10-29 16:09:51 +01001091boot_copy_region(struct boot_loader_state *state,
1092 const struct flash_area *fap_src,
David Vincze401c7422019-06-21 20:44:05 +02001093 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001094 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1095{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001096 uint32_t bytes_copied;
1097 int chunk_sz;
1098 int rc;
1099
1100 static uint8_t buf[1024];
1101
David Vinczecea8b592019-10-29 16:09:51 +01001102 (void)state;
1103
Tamas Banf70ef8c2017-12-19 15:35:09 +00001104 bytes_copied = 0;
1105 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +00001106 if (sz - bytes_copied > sizeof(buf)) {
1107 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001108 } else {
1109 chunk_sz = sz - bytes_copied;
1110 }
1111
1112 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1113 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001114 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001115 }
1116
1117 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1118 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001119 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001120 }
1121
1122 bytes_copied += chunk_sz;
1123 }
1124
David Vincze401c7422019-06-21 20:44:05 +02001125 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001126}
1127
1128#ifndef MCUBOOT_OVERWRITE_ONLY
1129static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001130boot_status_init(const struct boot_loader_state *state,
1131 const struct flash_area *fap,
1132 const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001133{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001134 struct boot_swap_state swap_state;
David Vinczecea8b592019-10-29 16:09:51 +01001135 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001136 int rc;
1137
David Vinczecea8b592019-10-29 16:09:51 +01001138#if (BOOT_IMAGE_NUMBER == 1)
1139 (void)state;
1140#endif
1141
1142 image_index = BOOT_CURR_IMG(state);
1143
David Vincze401c7422019-06-21 20:44:05 +02001144 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001145
David Vinczecea8b592019-10-29 16:09:51 +01001146 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1147 &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001148 assert(rc == 0);
1149
David Vincze401c7422019-06-21 20:44:05 +02001150 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01001151 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
David Vincze401c7422019-06-21 20:44:05 +02001152 assert(rc == 0);
1153 }
1154
Tamas Banf70ef8c2017-12-19 15:35:09 +00001155 if (swap_state.image_ok == BOOT_FLAG_SET) {
1156 rc = boot_write_image_ok(fap);
1157 assert(rc == 0);
1158 }
1159
1160 rc = boot_write_swap_size(fap, bs->swap_size);
1161 assert(rc == 0);
1162
1163 rc = boot_write_magic(fap);
1164 assert(rc == 0);
1165
Tamas Banf70ef8c2017-12-19 15:35:09 +00001166 return 0;
1167}
David Vinczef7641fa2018-09-04 18:29:46 +02001168
1169static int
David Vinczecea8b592019-10-29 16:09:51 +01001170boot_erase_trailer_sectors(const struct boot_loader_state *state,
1171 const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +02001172{
1173 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +02001174 uint32_t sector;
1175 uint32_t trailer_sz;
1176 uint32_t total_sz;
1177 uint32_t off;
1178 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +02001179 int fa_id_primary;
1180 int fa_id_secondary;
David Vinczecea8b592019-10-29 16:09:51 +01001181 uint8_t image_index;
David Vinczef7641fa2018-09-04 18:29:46 +02001182 int rc;
1183
David Vincze401c7422019-06-21 20:44:05 +02001184 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1185
David Vinczecea8b592019-10-29 16:09:51 +01001186 image_index = BOOT_CURR_IMG(state);
1187 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1188 BOOT_PRIMARY_SLOT);
1189 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1190 BOOT_SECONDARY_SLOT);
David Vinczebb207982019-08-21 15:13:04 +02001191
1192 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001193 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001194 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001195 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001196 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001197 return BOOT_EFLASH;
1198 }
1199
David Vincze401c7422019-06-21 20:44:05 +02001200 /* delete starting from last sector and moving to beginning */
David Vinczecea8b592019-10-29 16:09:51 +01001201 sector = boot_img_num_sectors(state, slot) - 1;
1202 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
David Vincze401c7422019-06-21 20:44:05 +02001203 total_sz = 0;
1204 do {
David Vinczecea8b592019-10-29 16:09:51 +01001205 sz = boot_img_sector_size(state, slot, sector);
1206 off = boot_img_sector_off(state, slot, sector);
1207 rc = boot_erase_region(fap, off, sz);
David Vincze401c7422019-06-21 20:44:05 +02001208 assert(rc == 0);
1209
1210 sector--;
1211 total_sz += sz;
1212 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001213
1214 return rc;
1215}
1216#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001217
Tamas Banf70ef8c2017-12-19 15:35:09 +00001218/**
1219 * Swaps the contents of two flash regions within the two image slots.
1220 *
1221 * @param idx The index of the first sector in the range of
1222 * sectors being swapped.
1223 * @param sz The number of bytes to swap.
1224 * @param bs The current boot status. This struct gets
1225 * updated according to the outcome.
1226 *
1227 * @return 0 on success; nonzero on failure.
1228 */
1229#ifndef MCUBOOT_OVERWRITE_ONLY
1230static void
David Vinczecea8b592019-10-29 16:09:51 +01001231boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1232 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001233{
David Vincze401c7422019-06-21 20:44:05 +02001234 const struct flash_area *fap_primary_slot;
1235 const struct flash_area *fap_secondary_slot;
1236 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001237 uint32_t copy_sz;
1238 uint32_t trailer_sz;
1239 uint32_t img_off;
1240 uint32_t scratch_trailer_off;
1241 struct boot_swap_state swap_state;
1242 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001243 bool erase_scratch;
David Vinczecea8b592019-10-29 16:09:51 +01001244 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001245 int rc;
1246
1247 /* Calculate offset from start of image area. */
David Vinczecea8b592019-10-29 16:09:51 +01001248 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001249
1250 copy_sz = sz;
David Vinczecea8b592019-10-29 16:09:51 +01001251 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001252
David Vincze401c7422019-06-21 20:44:05 +02001253 /* sz in this function is always sized on a multiple of the sector size.
1254 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001255 * is to determine if we're swapping the last sector. The last sector
1256 * needs special handling because it's where the trailer lives. If we're
1257 * copying it, we need to use scratch to write the trailer temporarily.
1258 *
1259 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1260 * controls if special handling is needed (swapping last sector).
1261 */
David Vinczecea8b592019-10-29 16:09:51 +01001262 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001263 if ((img_off + sz) >
David Vinczecea8b592019-10-29 16:09:51 +01001264 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001265 copy_sz -= trailer_sz;
1266 }
1267
David Vincze39e78552018-10-10 17:10:01 +02001268 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001269
David Vinczecea8b592019-10-29 16:09:51 +01001270 image_index = BOOT_CURR_IMG(state);
1271
1272 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1273 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001274 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001275
David Vinczecea8b592019-10-29 16:09:51 +01001276 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1277 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001278 assert (rc == 0);
1279
1280 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1281 assert (rc == 0);
1282
1283 if (bs->state == BOOT_STATUS_STATE_0) {
1284 BOOT_LOG_DBG("erasing scratch area");
David Vinczecea8b592019-10-29 16:09:51 +01001285 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001286 assert(rc == 0);
1287
David Vincze39e78552018-10-10 17:10:01 +02001288 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001289 /* Write a trailer to the scratch area, even if we don't need the
1290 * scratch area for status. We need a temporary place to store the
1291 * `swap-type` while we erase the primary trailer.
1292 */
David Vinczecea8b592019-10-29 16:09:51 +01001293 rc = boot_status_init(state, fap_scratch, bs);
David Vincze401c7422019-06-21 20:44:05 +02001294 assert(rc == 0);
1295
1296 if (!bs->use_scratch) {
1297 /* Prepare the primary status area... here it is known that the
1298 * last sector is not being used by the image data so it's safe
1299 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001300 */
David Vinczecea8b592019-10-29 16:09:51 +01001301 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001302 assert(rc == 0);
1303
David Vinczecea8b592019-10-29 16:09:51 +01001304 rc = boot_status_init(state, fap_primary_slot, bs);
David Vincze401c7422019-06-21 20:44:05 +02001305 assert(rc == 0);
1306
1307 /* Erase the temporary trailer from the scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01001308 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
David Vincze401c7422019-06-21 20:44:05 +02001309 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001310 }
1311 }
1312
David Vinczecea8b592019-10-29 16:09:51 +01001313 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
David Vincze401c7422019-06-21 20:44:05 +02001314 img_off, 0, copy_sz);
1315 assert(rc == 0);
1316
David Vinczecea8b592019-10-29 16:09:51 +01001317 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001318 bs->state = BOOT_STATUS_STATE_1;
David Vincze39e78552018-10-10 17:10:01 +02001319 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001320 }
1321
David Vincze39e78552018-10-10 17:10:01 +02001322 if (bs->state == BOOT_STATUS_STATE_1) {
David Vinczecea8b592019-10-29 16:09:51 +01001323 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001324 assert(rc == 0);
1325
David Vinczecea8b592019-10-29 16:09:51 +01001326 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001327 img_off, img_off, copy_sz);
1328 assert(rc == 0);
1329
David Vincze39e78552018-10-10 17:10:01 +02001330 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001331 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001332 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001333 */
David Vinczecea8b592019-10-29 16:09:51 +01001334 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001335 assert(rc == 0);
1336 }
1337
David Vinczecea8b592019-10-29 16:09:51 +01001338 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001339 bs->state = BOOT_STATUS_STATE_2;
David Vincze39e78552018-10-10 17:10:01 +02001340 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001341 }
1342
David Vincze39e78552018-10-10 17:10:01 +02001343 if (bs->state == BOOT_STATUS_STATE_2) {
David Vinczecea8b592019-10-29 16:09:51 +01001344 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001345 assert(rc == 0);
1346
David Vincze401c7422019-06-21 20:44:05 +02001347 /* NOTE: If this is the final sector, we exclude the image trailer from
1348 * this copy (copy_sz was truncated earlier).
1349 */
David Vinczecea8b592019-10-29 16:09:51 +01001350 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001351 0, img_off, copy_sz);
1352 assert(rc == 0);
1353
1354 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001355 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001356
1357 /* copy current status that is being maintained in scratch */
David Vinczecea8b592019-10-29 16:09:51 +01001358 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze401c7422019-06-21 20:44:05 +02001359 scratch_trailer_off, img_off + copy_sz,
David Vinczecea8b592019-10-29 16:09:51 +01001360 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
David Vincze39e78552018-10-10 17:10:01 +02001361 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001362
1363 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1364 &swap_state);
1365 assert(rc == 0);
1366
1367 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001368 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001369 assert(rc == 0);
1370 }
1371
David Vincze401c7422019-06-21 20:44:05 +02001372 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001373 rc = boot_write_swap_info(fap_primary_slot,
1374 swap_state.swap_type,
David Vinczecea8b592019-10-29 16:09:51 +01001375 image_index);
David Vincze401c7422019-06-21 20:44:05 +02001376 assert(rc == 0);
1377 }
1378
1379 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001380 assert(rc == 0);
1381
David Vincze401c7422019-06-21 20:44:05 +02001382 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001383 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001384 }
1385
David Vincze401c7422019-06-21 20:44:05 +02001386 /* If we wrote a trailer to the scratch area, erase it after we persist
1387 * a trailer to the primary slot. We do this to prevent mcuboot from
1388 * reading a stale status from the scratch area in case of immediate
1389 * reset.
1390 */
1391 erase_scratch = bs->use_scratch;
1392 bs->use_scratch = 0;
1393
David Vinczecea8b592019-10-29 16:09:51 +01001394 rc = boot_write_status(state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001395 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001396 bs->state = BOOT_STATUS_STATE_0;
David Vincze39e78552018-10-10 17:10:01 +02001397 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001398
1399 if (erase_scratch) {
David Vinczecea8b592019-10-29 16:09:51 +01001400 rc = boot_erase_region(fap_scratch, 0, sz);
David Vincze401c7422019-06-21 20:44:05 +02001401 assert(rc == 0);
1402 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001403 }
David Vincze401c7422019-06-21 20:44:05 +02001404
1405 flash_area_close(fap_primary_slot);
1406 flash_area_close(fap_secondary_slot);
1407 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001408}
1409#endif /* !MCUBOOT_OVERWRITE_ONLY */
1410
1411/**
David Vincze401c7422019-06-21 20:44:05 +02001412 * Overwrite primary slot with the image contained in the secondary slot.
1413 * If a prior copy operation was interrupted by a system reset, this function
1414 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001415 *
1416 * @param bs The current boot status. This function reads
1417 * this struct to determine if it is resuming
1418 * an interrupted swap operation. This
1419 * function writes the updated status to this
1420 * function on return.
1421 *
1422 * @return 0 on success; nonzero on failure.
1423 */
1424#ifdef MCUBOOT_OVERWRITE_ONLY
1425static int
David Vinczecea8b592019-10-29 16:09:51 +01001426boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001427{
1428 size_t sect_count;
1429 size_t sect;
1430 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001431 size_t size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001432 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001433 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001434 const struct flash_area *fap_primary_slot;
1435 const struct flash_area *fap_secondary_slot;
David Vinczecea8b592019-10-29 16:09:51 +01001436 uint8_t image_index;
David Vincze39e78552018-10-10 17:10:01 +02001437
1438 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001439
David Vincze8bdfc2d2019-03-18 15:49:23 +01001440 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1441 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001442
David Vinczecea8b592019-10-29 16:09:51 +01001443 image_index = BOOT_CURR_IMG(state);
1444
1445 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1446 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001447 assert (rc == 0);
1448
David Vinczecea8b592019-10-29 16:09:51 +01001449 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1450 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001451 assert (rc == 0);
1452
David Vinczecea8b592019-10-29 16:09:51 +01001453 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
1454 for (sect = 0, size = 0; sect < sect_count; sect++) {
1455 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
1456 rc = boot_erase_region(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001457 assert(rc == 0);
1458
1459 size += this_size;
1460 }
1461
David Vincze8bdfc2d2019-03-18 15:49:23 +01001462 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1463 size);
David Vinczecea8b592019-10-29 16:09:51 +01001464 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot,
1465 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001466
David Vincze060968d2019-05-23 01:13:14 +02001467 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001468 * value. Both slots hold the new image at this point, but the secondary
1469 * slot's image header must be passed because the read image headers in the
1470 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001471 */
David Vinczecea8b592019-10-29 16:09:51 +01001472 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1473 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001474 if (rc != 0) {
1475 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1476 return rc;
1477 }
1478
David Vincze39e78552018-10-10 17:10:01 +02001479 /*
1480 * Erases header and trailer. The trailer is erased because when a new
1481 * image is written without a trailer as is the case when using newt, the
1482 * trailer that was left might trigger a new upgrade.
1483 */
David Vincze401c7422019-06-21 20:44:05 +02001484 BOOT_LOG_DBG("erasing secondary header");
David Vinczecea8b592019-10-29 16:09:51 +01001485 rc = boot_erase_region(fap_secondary_slot,
1486 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1487 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001488 assert(rc == 0);
David Vinczecea8b592019-10-29 16:09:51 +01001489 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001490 BOOT_LOG_DBG("erasing secondary trailer");
David Vinczecea8b592019-10-29 16:09:51 +01001491 rc = boot_erase_region(fap_secondary_slot,
1492 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1493 last_sector),
1494 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1495 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001496 assert(rc == 0);
1497
David Vincze401c7422019-06-21 20:44:05 +02001498 flash_area_close(fap_primary_slot);
1499 flash_area_close(fap_secondary_slot);
1500
David Vincze8bdfc2d2019-03-18 15:49:23 +01001501 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001502
1503 return 0;
1504}
1505#else
David Vincze401c7422019-06-21 20:44:05 +02001506/**
1507 * Swaps the two images in flash. If a prior copy operation was interrupted
1508 * by a system reset, this function completes that operation.
1509 *
1510 * @param bs The current boot status. This function reads
1511 * this struct to determine if it is resuming
1512 * an interrupted swap operation. This
1513 * function writes the updated status to this
1514 * function on return.
1515 *
1516 * @return 0 on success; nonzero on failure.
1517 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001518static int
David Vinczecea8b592019-10-29 16:09:51 +01001519boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001520{
1521 uint32_t sz;
1522 int first_sector_idx;
1523 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001524 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001525 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001526 struct image_header *hdr;
1527 uint32_t size;
1528 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001529 uint32_t primary_slot_size;
1530 uint32_t secondary_slot_size;
David Vinczecea8b592019-10-29 16:09:51 +01001531 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001532 int rc;
1533
1534 /* FIXME: just do this if asked by user? */
1535
1536 size = copy_size = 0;
David Vinczecea8b592019-10-29 16:09:51 +01001537 image_index = BOOT_CURR_IMG(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001538
David Vincze39e78552018-10-10 17:10:01 +02001539 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001540 /*
1541 * No swap ever happened, so need to find the largest image which
1542 * will be used to determine the amount of sectors to swap.
1543 */
David Vinczecea8b592019-10-29 16:09:51 +01001544 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1545 if (hdr->ih_magic == IMAGE_MAGIC) {
1546 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
1547 assert(rc == 0);
1548 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001549
David Vinczecea8b592019-10-29 16:09:51 +01001550 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1551 if (hdr->ih_magic == IMAGE_MAGIC) {
1552 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
1553 assert(rc == 0);
1554 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001555
1556 if (size > copy_size) {
1557 copy_size = size;
1558 }
1559
1560 bs->swap_size = copy_size;
1561 } else {
1562 /*
1563 * If a swap was under way, the swap_size should already be present
1564 * in the trailer...
1565 */
David Vinczecea8b592019-10-29 16:09:51 +01001566 rc = boot_read_swap_size(image_index, &bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001567 assert(rc == 0);
1568
1569 copy_size = bs->swap_size;
1570 }
1571
David Vincze401c7422019-06-21 20:44:05 +02001572 primary_slot_size = 0;
1573 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001574 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001575 last_idx_secondary_slot = 0;
1576
1577 /*
1578 * Knowing the size of the largest image between both slots, here we
1579 * find what is the last sector in the primary slot that needs swapping.
1580 * Since we already know that both slots are compatible, the secondary
1581 * slot's last sector is not really required after this check is finished.
1582 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001583 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001584 if ((primary_slot_size < copy_size) ||
1585 (primary_slot_size < secondary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001586 primary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001587 BOOT_PRIMARY_SLOT,
1588 last_sector_idx);
1589 }
1590 if ((secondary_slot_size < copy_size) ||
1591 (secondary_slot_size < primary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001592 secondary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001593 BOOT_SECONDARY_SLOT,
1594 last_idx_secondary_slot);
1595 }
1596 if (primary_slot_size >= copy_size &&
1597 secondary_slot_size >= copy_size &&
1598 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001599 break;
1600 }
1601 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001602 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001603 }
1604
1605 swap_idx = 0;
1606 while (last_sector_idx >= 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001607 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001608 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
David Vinczecea8b592019-10-29 16:09:51 +01001609 boot_swap_sectors(first_sector_idx, sz, state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001610 }
1611
1612 last_sector_idx = first_sector_idx - 1;
1613 swap_idx++;
1614 }
1615
David Vincze8bdfc2d2019-03-18 15:49:23 +01001616#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001617 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001618 BOOT_LOG_WRN("%d status write fails performing the swap",
1619 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001620 }
1621#endif
1622
Tamas Banf70ef8c2017-12-19 15:35:09 +00001623 return 0;
1624}
1625#endif
1626
1627/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001628 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001629 */
1630#ifndef MCUBOOT_OVERWRITE_ONLY
1631static int
David Vinczecea8b592019-10-29 16:09:51 +01001632boot_set_copy_done(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001633{
1634 const struct flash_area *fap;
1635 int rc;
1636
David Vinczecea8b592019-10-29 16:09:51 +01001637 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1638 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001639 if (rc != 0) {
1640 return BOOT_EFLASH;
1641 }
1642
1643 rc = boot_write_copy_done(fap);
1644 flash_area_close(fap);
1645 return rc;
1646}
1647#endif /* !MCUBOOT_OVERWRITE_ONLY */
1648
1649/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001650 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1651 * ensure the status bytes from the image revert operation don't get processed
1652 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001653 *
1654 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001655 * image installed on the primary slot and the new image to be upgrade to has a
1656 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001657 */
1658#ifndef MCUBOOT_OVERWRITE_ONLY
1659static int
David Vinczecea8b592019-10-29 16:09:51 +01001660boot_set_image_ok(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001661{
1662 const struct flash_area *fap;
1663 struct boot_swap_state state;
1664 int rc;
1665
David Vinczecea8b592019-10-29 16:09:51 +01001666 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1667 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001668 if (rc != 0) {
1669 return BOOT_EFLASH;
1670 }
1671
1672 rc = boot_read_swap_state(fap, &state);
1673 if (rc != 0) {
1674 rc = BOOT_EFLASH;
1675 goto out;
1676 }
1677
1678 if (state.image_ok == BOOT_FLAG_UNSET) {
1679 rc = boot_write_image_ok(fap);
1680 }
1681
1682out:
1683 flash_area_close(fap);
1684 return rc;
1685}
1686#endif /* !MCUBOOT_OVERWRITE_ONLY */
1687
David Vinczebb6e3b62019-07-31 14:24:05 +02001688#if (BOOT_IMAGE_NUMBER > 1)
1689/**
David Vinczecea8b592019-10-29 16:09:51 +01001690 * Check if the version of the image is not older than required.
1691 *
1692 * @param req Required minimal image version.
1693 * @param ver Version of the image to be checked.
1694 *
1695 * @return 0 if the version is sufficient, nonzero otherwise.
1696 */
1697static int
1698boot_is_version_sufficient(struct image_version *req,
1699 struct image_version *ver)
1700{
1701 if (ver->iv_major > req->iv_major) {
1702 return 0;
1703 }
1704 if (ver->iv_major < req->iv_major) {
1705 return BOOT_EBADVERSION;
1706 }
1707 /* The major version numbers are equal. */
1708 if (ver->iv_minor > req->iv_minor) {
1709 return 0;
1710 }
1711 if (ver->iv_minor < req->iv_minor) {
1712 return BOOT_EBADVERSION;
1713 }
1714 /* The minor version numbers are equal. */
1715 if (ver->iv_revision < req->iv_revision) {
1716 return BOOT_EBADVERSION;
1717 }
1718
1719 return 0;
1720}
1721
1722/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001723 * Check the image dependency whether it is satisfied and modify
1724 * the swap type if necessary.
1725 *
1726 * @param dep Image dependency which has to be verified.
1727 *
1728 * @return 0 on success; nonzero on failure.
1729 */
1730static int
David Vinczecea8b592019-10-29 16:09:51 +01001731boot_verify_slot_dependency(struct boot_loader_state *state,
1732 struct image_dependency *dep)
David Vinczebb6e3b62019-07-31 14:24:05 +02001733{
1734 struct image_version *dep_version;
1735 size_t dep_slot;
1736 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001737 uint8_t swap_type;
David Vinczebb6e3b62019-07-31 14:24:05 +02001738
1739 /* Determine the source of the image which is the subject of
1740 * the dependency and get it's version. */
David Vinczecea8b592019-10-29 16:09:51 +01001741 swap_type = state->swap_type[dep->image_id];
1742 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczebb6e3b62019-07-31 14:24:05 +02001743 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
David Vinczecea8b592019-10-29 16:09:51 +01001744 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczebb6e3b62019-07-31 14:24:05 +02001745
1746 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1747 if (rc != 0) {
1748 /* Dependency not satisfied.
1749 * Modify the swap type to decrease the version number of the image
1750 * (which will be located in the primary slot after the boot process),
1751 * consequently the number of unsatisfied dependencies will be
1752 * decreased or remain the same.
1753 */
David Vinczecea8b592019-10-29 16:09:51 +01001754 switch (BOOT_SWAP_TYPE(state)) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001755 case BOOT_SWAP_TYPE_TEST:
1756 case BOOT_SWAP_TYPE_PERM:
David Vinczecea8b592019-10-29 16:09:51 +01001757 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczebb6e3b62019-07-31 14:24:05 +02001758 break;
1759 case BOOT_SWAP_TYPE_NONE:
David Vinczecea8b592019-10-29 16:09:51 +01001760 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczebb6e3b62019-07-31 14:24:05 +02001761 break;
1762 default:
1763 break;
1764 }
1765 }
1766
1767 return rc;
1768}
1769
1770/**
1771 * Read all dependency TLVs of an image from the flash and verify
1772 * one after another to see if they are all satisfied.
1773 *
1774 * @param slot Image slot number.
1775 *
1776 * @return 0 on success; nonzero on failure.
1777 */
1778static int
David Vinczecea8b592019-10-29 16:09:51 +01001779boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczebb6e3b62019-07-31 14:24:05 +02001780{
1781 const struct flash_area *fap;
David Vinczebb6e3b62019-07-31 14:24:05 +02001782 struct image_tlv tlv;
1783 struct image_dependency dep;
1784 uint32_t off;
1785 uint32_t end;
1786 bool dep_tlvs_found = false;
David Vinczecea8b592019-10-29 16:09:51 +01001787 int area_id;
David Vinczebb6e3b62019-07-31 14:24:05 +02001788 int rc;
1789
David Vinczecea8b592019-10-29 16:09:51 +01001790 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1791 rc = flash_area_open(area_id, &fap);
David Vinczebb6e3b62019-07-31 14:24:05 +02001792 if (rc != 0) {
1793 rc = BOOT_EFLASH;
1794 goto done;
1795 }
1796
David Vinczecea8b592019-10-29 16:09:51 +01001797 rc = boot_find_tlv_offs(boot_img_hdr(state, slot), fap, &off, &end);
David Vinczebb6e3b62019-07-31 14:24:05 +02001798 if (rc != 0) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001799 goto done;
1800 }
1801
David Vinczebb6e3b62019-07-31 14:24:05 +02001802 /* Traverse through all of the TLVs to find the dependency TLVs. */
Tamas Ban056ed0b2019-09-16 12:48:32 +01001803 while(off < end) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001804 rc = flash_area_read(fap, off, &tlv, sizeof(tlv));
1805 if (rc != 0) {
1806 rc = BOOT_EFLASH;
1807 goto done;
1808 }
1809
1810 if (tlv.it_type == IMAGE_TLV_DEPENDENCY) {
David Vinczecea8b592019-10-29 16:09:51 +01001811 dep_tlvs_found = true;
David Vinczebb6e3b62019-07-31 14:24:05 +02001812
1813 if (tlv.it_len != sizeof(dep)) {
1814 rc = BOOT_EBADIMAGE;
1815 goto done;
1816 }
1817
1818 rc = flash_area_read(fap, off + sizeof(tlv), &dep, tlv.it_len);
1819 if (rc != 0) {
1820 rc = BOOT_EFLASH;
1821 goto done;
1822 }
1823
David Vinczecea8b592019-10-29 16:09:51 +01001824 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1825 rc = BOOT_EBADARGS;
1826 goto done;
1827 }
1828
David Vinczebb6e3b62019-07-31 14:24:05 +02001829 /* Verify dependency and modify the swap type if not satisfied. */
David Vinczecea8b592019-10-29 16:09:51 +01001830 rc = boot_verify_slot_dependency(state, &dep);
David Vinczebb6e3b62019-07-31 14:24:05 +02001831 if (rc != 0) {
1832 /* Dependency not satisfied. */
1833 goto done;
1834 }
1835
1836 /* Dependency satisfied, no action needed.
1837 * Continue with the next TLV entry.
1838 */
1839 } else if (dep_tlvs_found) {
1840 /* The dependency TLVs are contiguous in the TLV area. If a
1841 * dependency had already been found and the last read TLV
1842 * has a different type then there are no more dependency TLVs.
1843 * The search can be finished.
1844 */
1845 break;
1846 }
Tamas Ban056ed0b2019-09-16 12:48:32 +01001847 /* Avoid integer overflow. */
1848 if (boot_add_uint32_overflow_check(off, (sizeof(tlv) + tlv.it_len))) {
1849 /* Potential overflow. */
1850 return BOOT_EBADIMAGE;
1851 } else {
1852 off += sizeof(tlv) + tlv.it_len;
1853 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001854 }
1855
1856done:
1857 flash_area_close(fap);
1858 return rc;
1859}
1860
1861/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001862 * Iterate over all the images and verify whether the image dependencies in the
1863 * TLV area are all satisfied and update the related swap type if necessary.
1864 */
David Vinczecea8b592019-10-29 16:09:51 +01001865static int
1866boot_verify_dependencies(struct boot_loader_state *state)
David Vinczebb6e3b62019-07-31 14:24:05 +02001867{
David Vinczebb6e3b62019-07-31 14:24:05 +02001868 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001869 uint8_t slot;
David Vinczebb6e3b62019-07-31 14:24:05 +02001870
David Vinczecea8b592019-10-29 16:09:51 +01001871 BOOT_CURR_IMG(state) = 0;
1872 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1873 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1874 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1875 slot = BOOT_SECONDARY_SLOT;
1876 } else {
1877 slot = BOOT_PRIMARY_SLOT;
1878 }
1879
1880 rc = boot_verify_slot_dependencies(state, slot);
David Vinczebb6e3b62019-07-31 14:24:05 +02001881 if (rc == 0) {
1882 /* All dependencies've been satisfied, continue with next image. */
David Vinczecea8b592019-10-29 16:09:51 +01001883 BOOT_CURR_IMG(state)++;
David Vinczebb6e3b62019-07-31 14:24:05 +02001884 } else if (rc == BOOT_EBADVERSION) {
David Vinczecea8b592019-10-29 16:09:51 +01001885 /* Cannot upgrade due to non-met dependencies, so disable all
1886 * image upgrades.
1887 */
1888 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1889 BOOT_CURR_IMG(state) = idx;
1890 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1891 }
1892 break;
David Vinczebb6e3b62019-07-31 14:24:05 +02001893 } else {
1894 /* Other error happened, images are inconsistent */
David Vinczecea8b592019-10-29 16:09:51 +01001895 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001896 }
1897 }
David Vinczecea8b592019-10-29 16:09:51 +01001898 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001899}
1900#endif /* (BOOT_IMAGE_NUMBER > 1) */
1901
Tamas Banf70ef8c2017-12-19 15:35:09 +00001902/**
David Vincze7384ee72019-07-23 17:00:42 +02001903 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001904 *
David Vincze7384ee72019-07-23 17:00:42 +02001905 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001906 *
1907 * @return 0 on success; nonzero on failure.
1908 */
1909static int
David Vinczecea8b592019-10-29 16:09:51 +01001910boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001911{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001912 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001913#ifndef MCUBOOT_OVERWRITE_ONLY
1914 uint8_t swap_type;
1915#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001916
David Vincze7384ee72019-07-23 17:00:42 +02001917 /* At this point there are no aborted swaps. */
1918#if defined(MCUBOOT_OVERWRITE_ONLY)
David Vinczecea8b592019-10-29 16:09:51 +01001919 rc = boot_copy_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001920#else
David Vinczecea8b592019-10-29 16:09:51 +01001921 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001922#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001923 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001924
1925#ifndef MCUBOOT_OVERWRITE_ONLY
1926 /* The following state needs image_ok be explicitly set after the
1927 * swap was finished to avoid a new revert.
1928 */
David Vinczecea8b592019-10-29 16:09:51 +01001929 swap_type = BOOT_SWAP_TYPE(state);
1930 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1931 swap_type == BOOT_SWAP_TYPE_PERM) {
1932 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001933 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001934 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001935 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001936 }
1937
David Vinczecea8b592019-10-29 16:09:51 +01001938 if (swap_type == BOOT_SWAP_TYPE_PERM) {
David Vincze7384ee72019-07-23 17:00:42 +02001939 /* Update the stored security counter with the new image's security
1940 * counter value. The primary slot holds the new image at this
1941 * point, but the secondary slot's image header must be passed
1942 * because the read image headers in the boot_data structure have
1943 * not been updated yet.
1944 *
1945 * In case of a permanent image swap mcuboot will never attempt to
1946 * revert the images on the next reboot. Therefore, the security
1947 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001948 */
David Vinczecea8b592019-10-29 16:09:51 +01001949 rc = boot_update_security_counter(
1950 BOOT_CURR_IMG(state),
1951 BOOT_PRIMARY_SLOT,
1952 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02001953 if (rc != 0) {
1954 BOOT_LOG_ERR("Security counter update failed after "
1955 "image upgrade.");
David Vinczecea8b592019-10-29 16:09:51 +01001956 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001957 }
1958 }
1959
David Vinczecea8b592019-10-29 16:09:51 +01001960 if (BOOT_IS_UPGRADE(swap_type)) {
1961 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001962 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001963 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001964 }
1965 }
1966#endif /* !MCUBOOT_OVERWRITE_ONLY */
1967
1968 return rc;
1969}
1970
1971/**
1972 * Completes a previously aborted image swap.
1973 *
1974 * @param bs The current boot status.
1975 *
1976 * @return 0 on success; nonzero on failure.
1977 */
1978#if !defined(MCUBOOT_OVERWRITE_ONLY)
1979static int
David Vinczecea8b592019-10-29 16:09:51 +01001980boot_complete_partial_swap(struct boot_loader_state *state,
1981 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02001982{
1983 int rc;
1984
1985 /* Determine the type of swap operation being resumed from the
1986 * `swap-type` trailer field.
1987 */
David Vinczecea8b592019-10-29 16:09:51 +01001988 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001989 assert(rc == 0);
1990
David Vinczecea8b592019-10-29 16:09:51 +01001991 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02001992
1993 /* The following states need image_ok be explicitly set after the
1994 * swap was finished to avoid a new revert.
1995 */
1996 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1997 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
David Vinczecea8b592019-10-29 16:09:51 +01001998 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001999 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002000 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02002001 }
2002 }
2003
David Vinczecea8b592019-10-29 16:09:51 +01002004 if (BOOT_IS_UPGRADE(bs->swap_type)) {
2005 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002006 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002007 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02002008 }
2009 }
2010
David Vinczecea8b592019-10-29 16:09:51 +01002011 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02002012 BOOT_LOG_ERR("panic!");
2013 assert(0);
2014
2015 /* Loop forever... */
2016 while (1) {}
2017 }
2018
2019 return rc;
2020}
2021#endif /* !MCUBOOT_OVERWRITE_ONLY */
2022
2023#if (BOOT_IMAGE_NUMBER > 1)
2024/**
2025 * Review the validity of previously determined swap types of other images.
2026 *
2027 * @param aborted_swap The current image upgrade is a
2028 * partial/aborted swap.
2029 */
2030static void
David Vinczecea8b592019-10-29 16:09:51 +01002031boot_review_image_swap_types(struct boot_loader_state *state,
2032 bool aborted_swap)
David Vincze7384ee72019-07-23 17:00:42 +02002033{
2034 /* In that case if we rebooted in the middle of an image upgrade process, we
2035 * must review the validity of swap types, that were previously determined
2036 * for other images. The image_ok flag had not been set before the reboot
2037 * for any of the updated images (only the copy_done flag) and thus falsely
2038 * the REVERT swap type has been determined for the previous images that had
2039 * been updated before the reboot.
2040 *
2041 * There are two separate scenarios that we have to deal with:
2042 *
2043 * 1. The reboot has happened during swapping an image:
2044 * The current image upgrade has been determined as a
2045 * partial/aborted swap.
2046 * 2. The reboot has happened between two separate image upgrades:
2047 * In this scenario we must check the swap type of the current image.
2048 * In those cases if it is NONE or REVERT we cannot certainly determine
2049 * the fact of a reboot. In a consistent state images must move in the
2050 * same direction or stay in place, e.g. in practice REVERT and TEST
2051 * swap types cannot be present at the same time. If the swap type of
2052 * the current image is either TEST, PERM or FAIL we must review the
2053 * already determined swap types of other images and set each false
2054 * REVERT swap types to NONE (these images had been successfully
2055 * updated before the system rebooted between two separate image
2056 * upgrades).
2057 */
2058
David Vinczecea8b592019-10-29 16:09:51 +01002059 if (BOOT_CURR_IMG(state) == 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002060 /* Nothing to do */
2061 return;
2062 }
2063
2064 if (!aborted_swap) {
David Vinczecea8b592019-10-29 16:09:51 +01002065 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2066 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vincze7384ee72019-07-23 17:00:42 +02002067 /* Nothing to do */
2068 return;
2069 }
2070 }
2071
David Vinczecea8b592019-10-29 16:09:51 +01002072 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2073 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2074 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002075 }
2076 }
2077}
2078#endif
2079
2080/**
2081 * Prepare image to be updated if required.
2082 *
2083 * Prepare image to be updated if required with completing an image swap
2084 * operation if one was aborted and/or determining the type of the
2085 * swap operation. In case of any error set the swap type to NONE.
2086 *
David Vinczecea8b592019-10-29 16:09:51 +01002087 * @param state Boot loader status information.
David Vincze7384ee72019-07-23 17:00:42 +02002088 * @param bs Pointer where the read and possibly updated
2089 * boot status can be written to.
2090 */
2091static void
David Vinczecea8b592019-10-29 16:09:51 +01002092boot_prepare_image_for_update(struct boot_loader_state *state,
2093 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02002094{
2095 int rc;
2096
2097 /* Determine the sector layout of the image slots and scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01002098 rc = boot_read_sectors(state);
David Vincze7384ee72019-07-23 17:00:42 +02002099 if (rc != 0) {
2100 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2101 " - too small?", BOOT_MAX_IMG_SECTORS);
2102 /* Unable to determine sector layout, continue with next image
2103 * if there is one.
2104 */
David Vinczecea8b592019-10-29 16:09:51 +01002105 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002106 return;
2107 }
2108
2109 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002110 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002111 if (rc != 0) {
2112 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002113 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
2114 BOOT_CURR_IMG(state));
2115 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002116 return;
2117 }
2118
2119 /* If the current image's slots aren't compatible, no swap is possible.
2120 * Just boot into primary slot.
2121 */
David Vinczecea8b592019-10-29 16:09:51 +01002122 if (boot_slots_compatible(state)) {
2123 rc = boot_read_status(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002124 if (rc != 0) {
2125 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
David Vinczecea8b592019-10-29 16:09:51 +01002126 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002127 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002128 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002129 return;
2130 }
2131
2132 /* Determine if we rebooted in the middle of an image swap
2133 * operation. If a partial swap was detected, complete it.
2134 */
2135 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2136
2137#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002138 boot_review_image_swap_types(state, true);
David Vincze7384ee72019-07-23 17:00:42 +02002139#endif
2140
2141#ifdef MCUBOOT_OVERWRITE_ONLY
2142 /* Should never arrive here, overwrite-only mode has
2143 * no swap state.
2144 */
2145 assert(0);
2146#else
2147 /* Determine the type of swap operation being resumed from the
2148 * `swap-type` trailer field.
2149 */
David Vinczecea8b592019-10-29 16:09:51 +01002150 rc = boot_complete_partial_swap(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002151 assert(rc == 0);
2152#endif
2153 /* Attempt to read an image header from each slot. Ensure that
2154 * image headers in slots are aligned with headers in boot_data.
2155 */
David Vinczecea8b592019-10-29 16:09:51 +01002156 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002157 assert(rc == 0);
2158
2159 /* Swap has finished set to NONE */
David Vinczecea8b592019-10-29 16:09:51 +01002160 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002161 } else {
2162 /* There was no partial swap, determine swap type. */
2163 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01002164 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2165 } else if (boot_validate_slot(state,
2166 BOOT_SECONDARY_SLOT, bs) != 0) {
2167 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vincze7384ee72019-07-23 17:00:42 +02002168 } else {
David Vinczecea8b592019-10-29 16:09:51 +01002169 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02002170 }
2171
2172#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002173 boot_review_image_swap_types(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002174#endif
2175 }
2176 } else {
2177 /* In that case if slots are not compatible. */
David Vinczecea8b592019-10-29 16:09:51 +01002178 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002179 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002180}
2181
2182/**
2183 * Prepares the booting process. This function moves images around in flash as
2184 * appropriate, and tells you what address to boot from.
2185 *
David Vinczecea8b592019-10-29 16:09:51 +01002186 * @param state Boot loader status information.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002187 * @param rsp On success, indicates how booting should occur.
2188 *
2189 * @return 0 on success; nonzero on failure.
2190 */
2191int
David Vinczecea8b592019-10-29 16:09:51 +01002192context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Banf70ef8c2017-12-19 15:35:09 +00002193{
Tamas Banf70ef8c2017-12-19 15:35:09 +00002194 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02002195 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01002196 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002197 int fa_id;
David Vinczecea8b592019-10-29 16:09:51 +01002198 int image_index;
2199 bool has_upgrade;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002200
2201 /* The array of slot sectors are defined here (as opposed to file scope) so
2202 * that they don't get allocated for non-boot-loader apps. This is
2203 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2204 * any effect in older gcc versions (e.g., 4.8.4).
2205 */
David Vincze7384ee72019-07-23 17:00:42 +02002206 static boot_sector_t
2207 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2208 static boot_sector_t
2209 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02002210 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00002211
David Vincze7384ee72019-07-23 17:00:42 +02002212 /* Iterate over all the images. By the end of the loop the swap type has
2213 * to be determined for each image and all aborted swaps have to be
2214 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002215 */
David Vinczecea8b592019-10-29 16:09:51 +01002216 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2217
2218 image_index = BOOT_CURR_IMG(state);
2219
2220 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
2221 primary_slot_sectors[image_index];
2222 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
2223 secondary_slot_sectors[image_index];
2224 state->scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002225
David Vincze7384ee72019-07-23 17:00:42 +02002226 /* Open primary and secondary image areas for the duration
2227 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002228 */
David Vincze7384ee72019-07-23 17:00:42 +02002229 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002230 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
2231 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vincze7384ee72019-07-23 17:00:42 +02002232 assert(rc == 0);
2233 }
2234 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
David Vinczecea8b592019-10-29 16:09:51 +01002235 &BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002236 assert(rc == 0);
2237
2238 /* Determine swap type and complete swap if it has been aborted. */
David Vinczecea8b592019-10-29 16:09:51 +01002239 boot_prepare_image_for_update(state, &bs);
2240
2241 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
2242 has_upgrade = true;
2243 }
David Vincze7384ee72019-07-23 17:00:42 +02002244 }
2245
David Vinczebb6e3b62019-07-31 14:24:05 +02002246#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002247 if (has_upgrade) {
2248 /* Iterate over all the images and verify whether the image dependencies
2249 * are all satisfied and update swap type if necessary.
2250 */
2251 rc = boot_verify_dependencies(state);
2252 if (rc == BOOT_EBADVERSION) {
2253 /*
2254 * It was impossible to upgrade because the expected dependency
2255 * version was not available. Here we already changed the swap_type
2256 * so that instead of asserting the bootloader, we continue and no
2257 * upgrade is performed.
2258 */
2259 rc = 0;
2260 }
2261 }
David Vinczebb6e3b62019-07-31 14:24:05 +02002262#endif
2263
David Vincze7384ee72019-07-23 17:00:42 +02002264 /* Iterate over all the images. At this point there are no aborted swaps
2265 * and the swap types are determined for each image. By the end of the loop
2266 * all required update operations will have been finished.
2267 */
David Vinczecea8b592019-10-29 16:09:51 +01002268 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002269
2270#if (BOOT_IMAGE_NUMBER > 1)
2271 /* Indicate that swap is not aborted */
2272 memset(&bs, 0, sizeof bs);
2273 bs.idx = BOOT_STATUS_IDX_0;
2274 bs.state = BOOT_STATUS_STATE_0;
2275#endif /* (BOOT_IMAGE_NUMBER > 1) */
2276
2277 /* Set the previously determined swap type */
David Vinczecea8b592019-10-29 16:09:51 +01002278 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vincze7384ee72019-07-23 17:00:42 +02002279
David Vinczecea8b592019-10-29 16:09:51 +01002280 switch (BOOT_SWAP_TYPE(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002281 case BOOT_SWAP_TYPE_NONE:
2282 break;
2283
2284 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2285 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2286 case BOOT_SWAP_TYPE_REVERT:
David Vinczecea8b592019-10-29 16:09:51 +01002287 rc = boot_perform_update(state, &bs);
David Vincze7384ee72019-07-23 17:00:42 +02002288 assert(rc == 0);
2289 break;
2290
2291 case BOOT_SWAP_TYPE_FAIL:
2292 /* The image in secondary slot was invalid and is now erased. Ensure
2293 * we don't try to boot into it again on the next reboot. Do this by
2294 * pretending we just reverted back to primary slot.
2295 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002296#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002297 /* image_ok needs to be explicitly set to avoid a new revert. */
David Vinczecea8b592019-10-29 16:09:51 +01002298 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00002299 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002300 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002301 }
2302#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002303 break;
2304
2305 default:
David Vinczecea8b592019-10-29 16:09:51 +01002306 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002307 }
David Vincze7384ee72019-07-23 17:00:42 +02002308
David Vinczecea8b592019-10-29 16:09:51 +01002309 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02002310 BOOT_LOG_ERR("panic!");
2311 assert(0);
2312
2313 /* Loop forever... */
2314 while (1) {}
2315 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002316 }
2317
David Vincze7384ee72019-07-23 17:00:42 +02002318 /* Iterate over all the images. At this point all required update operations
2319 * have finished. By the end of the loop each image in the primary slot will
2320 * have been re-validated.
2321 */
David Vinczecea8b592019-10-29 16:09:51 +01002322 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2323 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vincze7384ee72019-07-23 17:00:42 +02002324 /* Attempt to read an image header from each slot. Ensure that image
2325 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002326 */
David Vinczecea8b592019-10-29 16:09:51 +01002327 rc = boot_read_image_headers(state, false);
David Vincze060968d2019-05-23 01:13:14 +02002328 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002329 goto out;
2330 }
2331 /* Since headers were reloaded, it can be assumed we just performed
2332 * a swap or overwrite. Now the header info that should be used to
2333 * provide the data for the bootstrap, which previously was at
2334 * secondary slot, was updated to primary slot.
2335 */
2336 }
2337
2338#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vinczecea8b592019-10-29 16:09:51 +01002339 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vincze7384ee72019-07-23 17:00:42 +02002340 if (rc != 0) {
2341 rc = BOOT_EBADIMAGE;
2342 goto out;
2343 }
2344#else
2345 /* Even if we're not re-validating the primary slot, we could be booting
2346 * onto an empty flash chip. At least do a basic sanity check that
2347 * the magic number on the image is OK.
2348 */
David Vinczecea8b592019-10-29 16:09:51 +01002349 if (!BOOT_IMG_HDR_IS_VALID(state, BOOT_PRIMARY_SLOT)) {
2350 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2351 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_magic,
2352 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002353 rc = BOOT_EBADIMAGE;
2354 goto out;
2355 }
2356#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2357
2358 /* Update the stored security counter with the active image's security
2359 * counter value. It will be updated only if the new security counter is
2360 * greater than the stored value.
2361 *
2362 * In case of a successful image swapping when the swap type is TEST the
2363 * security counter can be increased only after a reset, when the swap
2364 * type is NONE and the image has marked itself "OK" (the image_ok flag
2365 * has been set). This way a "revert" swap can be performed if it's
2366 * necessary.
2367 */
David Vinczecea8b592019-10-29 16:09:51 +01002368 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2369 rc = boot_update_security_counter(
2370 BOOT_CURR_IMG(state),
2371 BOOT_PRIMARY_SLOT,
2372 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02002373 if (rc != 0) {
2374 BOOT_LOG_ERR("Security counter update failed after image "
2375 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002376 goto out;
2377 }
2378 }
2379
David Vincze7384ee72019-07-23 17:00:42 +02002380 /* Save boot status to shared memory area */
2381#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002382 rc = boot_save_boot_status((BOOT_CURR_IMG(state) == 0) ?
2383 SW_SPE : SW_NSPE,
2384 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2385 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002386 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002387#else
David Vincze7384ee72019-07-23 17:00:42 +02002388 rc = boot_save_boot_status(SW_S_NS,
David Vinczecea8b592019-10-29 16:09:51 +01002389 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2390 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002391 );
2392#endif
2393 if (rc) {
2394 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
David Vinczecea8b592019-10-29 16:09:51 +01002395 BOOT_CURR_IMG(state));
David Vincze060968d2019-05-23 01:13:14 +02002396 }
2397 }
2398
David Vinczecea8b592019-10-29 16:09:51 +01002399#if (BOOT_IMAGE_NUMBER > 1)
David Vincze7384ee72019-07-23 17:00:42 +02002400 /* Always boot from the primary slot of Image 0. */
David Vinczecea8b592019-10-29 16:09:51 +01002401 BOOT_CURR_IMG(state) = 0;
2402#endif
Tamas Ban0e8ab302019-01-17 11:45:31 +00002403
David Vinczecea8b592019-10-29 16:09:51 +01002404 rsp->br_flash_dev_id =
2405 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2406 rsp->br_image_off =
2407 boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2408 rsp->br_hdr =
2409 boot_img_hdr(state, BOOT_PRIMARY_SLOT);
2410
2411out:
2412 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2413 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002414 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002415 flash_area_close(BOOT_IMG_AREA(state,
David Vincze7384ee72019-07-23 17:00:42 +02002416 BOOT_NUM_SLOTS - 1 - slot));
2417 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002418 }
2419 return rc;
2420}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002421
Oliver Swedef9982442018-08-24 18:37:44 +01002422#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002423
David Vinczedcba70b2019-05-28 12:02:52 +02002424#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2425 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2426 (area), \
2427 (hdr)->ih_ver.iv_major, \
2428 (hdr)->ih_ver.iv_minor, \
2429 (hdr)->ih_ver.iv_revision, \
2430 (hdr)->ih_ver.iv_build_num, \
2431 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2432 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2433 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002434 (state)->image_ok)
2435
2436struct image_slot_version {
2437 uint64_t version;
2438 uint32_t slot_number;
2439};
2440
2441/**
2442 * Extract the version number from the image header. This function must be
2443 * ported if version number format has changed in the image header.
2444 *
2445 * @param hdr Pointer to an image header structure
2446 *
Oliver Swedef9982442018-08-24 18:37:44 +01002447 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002448 */
2449static uint64_t
2450boot_get_version_number(struct image_header *hdr)
2451{
Oliver Swedef9982442018-08-24 18:37:44 +01002452 uint64_t version = 0;
2453 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2454 + IMAGE_VER_REVISION_LENGTH
2455 + IMAGE_VER_BUILD_NUM_LENGTH);
2456 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2457 + IMAGE_VER_BUILD_NUM_LENGTH);
2458 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2459 version |= hdr->ih_ver.iv_build_num;
2460 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002461}
2462
2463/**
2464 * Comparator function for `qsort` to compare version numbers. This function
2465 * must be ported if version number format has changed in the image header.
2466 *
2467 * @param ver1 Pointer to an array element which holds the version number
2468 * @param ver2 Pointer to another array element which holds the version
2469 * number
2470 *
2471 * @return if version1 > version2 -1
2472 * if version1 == version2 0
2473 * if version1 < version2 1
2474 */
2475static int
2476boot_compare_version_numbers(const void *ver1, const void *ver2)
2477{
2478 if (((struct image_slot_version *)ver1)->version <
2479 ((struct image_slot_version *)ver2)->version) {
2480 return 1;
2481 }
2482
2483 if (((struct image_slot_version *)ver1)->version ==
2484 ((struct image_slot_version *)ver2)->version) {
2485 return 0;
2486 }
2487
2488 return -1;
2489}
2490
2491/**
2492 * Sort the available images based on the version number and puts them in
2493 * a list.
2494 *
David Vinczecea8b592019-10-29 16:09:51 +01002495 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002496 * @param boot_sequence A pointer to an array, whose aim is to carry
2497 * the boot order of candidate images.
2498 * @param slot_cnt The number of flash areas, which can contains firmware
2499 * images.
2500 *
2501 * @return The number of valid images.
2502 */
2503uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01002504boot_get_boot_sequence(struct boot_loader_state *state,
2505 uint32_t *boot_sequence, uint32_t slot_cnt)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002506{
2507 struct boot_swap_state slot_state;
2508 struct image_header *hdr;
2509 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2510 uint32_t image_cnt = 0;
2511 uint32_t slot;
2512 int32_t rc;
2513 int32_t fa_id;
2514
2515 for (slot = 0; slot < slot_cnt; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002516 hdr = boot_img_hdr(state, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002517 fa_id = flash_area_id_from_image_slot(slot);
2518 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2519 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002520 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2521 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002522 continue;
2523 }
2524
David Vinczecea8b592019-10-29 16:09:51 +01002525 if (BOOT_IMG_HDR_IS_VALID(state, slot)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002526 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002527 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002528 /* Valid cases:
2529 * - Test mode: magic is OK in image trailer
2530 * - Permanent mode: image_ok flag has previously set
2531 */
2532 image_versions[slot].slot_number = slot;
2533 image_versions[slot].version = boot_get_version_number(hdr);
2534 image_cnt++;
2535 }
2536
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002537 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2538 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002539 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002540 }
2541 }
2542
2543 /* Sort the images based on version number */
2544 qsort(&image_versions[0],
2545 slot_cnt,
2546 sizeof(struct image_slot_version),
2547 boot_compare_version_numbers);
2548
2549 /* Copy the calculated boot sequence to boot_sequence array */
2550 for (slot = 0; slot < slot_cnt; slot++) {
2551 boot_sequence[slot] = image_versions[slot].slot_number;
2552 }
2553
2554 return image_cnt;
2555}
2556
Oliver Swedef9982442018-08-24 18:37:44 +01002557#ifdef MCUBOOT_RAM_LOADING
Raef Colesaf082382019-10-01 11:10:33 +01002558
2559/**
2560 * Verifies that the image in a slot lies within the predefined bounds that are
2561 * allowed to be used by executable images.
2562 *
2563 * @param img_dst The address to which the image is going to be copied.
2564 *
2565 * @param img_sz The size of the image.
2566 *
2567 * @return 0 on success; nonzero on failure.
2568 */
2569static int
2570boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz)
2571{
2572 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2573 return BOOT_EBADIMAGE;
2574 }
2575
2576 if (boot_add_uint32_overflow_check(img_dst, img_sz)) {
2577 return BOOT_EBADIMAGE;
2578 }
2579
2580 if (img_dst + img_sz > IMAGE_EXECUTABLE_RAM_START +
2581 IMAGE_EXECUTABLE_RAM_SIZE) {
2582 return BOOT_EBADIMAGE;
2583 }
2584
2585 return 0;
2586}
2587
Oliver Swedef9982442018-08-24 18:37:44 +01002588/**
2589 * Copies an image from a slot in the flash to an SRAM address, where the load
2590 * address has already been inserted into the image header by this point and is
2591 * extracted from it within this method. The copying is done sector-by-sector.
2592 *
David Vinczecea8b592019-10-29 16:09:51 +01002593 * @param state Boot loader status information.
Oliver Swedef9982442018-08-24 18:37:44 +01002594 * @param slot The flash slot of the image to be copied to SRAM.
2595 *
2596 * @param hdr Pointer to the image header structure of the image
Raef Colesaf082382019-10-01 11:10:33 +01002597 *
2598 * @param img_dst The address at which the image needs to be copied to
2599 * SRAM.
2600 *
2601 * @param img_sz The size of the image that needs to be copied to SRAM.
Oliver Swedef9982442018-08-24 18:37:44 +01002602 *
2603 * @return 0 on success; nonzero on failure.
2604 */
2605static int
David Vinczecea8b592019-10-29 16:09:51 +01002606boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2607 struct image_header *hdr,
Raef Colesaf082382019-10-01 11:10:33 +01002608 uint32_t img_dst, uint32_t img_sz)
Oliver Swedef9982442018-08-24 18:37:44 +01002609{
2610 int rc;
2611 uint32_t sect_sz;
2612 uint32_t sect = 0;
2613 uint32_t bytes_copied = 0;
2614 const struct flash_area *fap_src = NULL;
Oliver Swedef9982442018-08-24 18:37:44 +01002615
Raef Colesaf082382019-10-01 11:10:33 +01002616 if (img_dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002617 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002618 "- the load address must be aligned with 4 bytes due to SRAM "
Raef Colesaf082382019-10-01 11:10:33 +01002619 "restrictions", img_dst);
Oliver Swedef9982442018-08-24 18:37:44 +01002620 return BOOT_EBADARGS;
2621 }
2622
2623 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2624 if (rc != 0) {
2625 return BOOT_EFLASH;
2626 }
2627
Oliver Swedef9982442018-08-24 18:37:44 +01002628 while (bytes_copied < img_sz) {
David Vinczecea8b592019-10-29 16:09:51 +01002629 sect_sz = boot_img_sector_size(state, slot, sect);
Oliver Swedef9982442018-08-24 18:37:44 +01002630 /*
2631 * Direct copy from where the image sector resides in flash to its new
2632 * location in SRAM
2633 */
2634 rc = flash_area_read(fap_src,
2635 bytes_copied,
Raef Colesaf082382019-10-01 11:10:33 +01002636 (void *)(img_dst + bytes_copied),
Oliver Swedef9982442018-08-24 18:37:44 +01002637 sect_sz);
2638 if (rc != 0) {
2639 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2640 break;
2641 } else {
2642 bytes_copied += sect_sz;
2643 }
2644 sect++;
2645 }
2646
2647 if (fap_src) {
2648 flash_area_close(fap_src);
2649 }
2650 return rc;
2651}
Raef Coles27a61452019-09-25 15:32:25 +01002652
2653/**
2654 * Removes an image from SRAM, by overwriting it with zeros.
2655 *
Raef Colesaf082382019-10-01 11:10:33 +01002656 * @param img_dst The address of the image that needs to be removed from
2657 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002658 *
Raef Colesaf082382019-10-01 11:10:33 +01002659 * @param img_sz The size of the image that needs to be removed from
2660 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002661 *
2662 * @return 0 on success; nonzero on failure.
2663 */
2664static int
Raef Colesaf082382019-10-01 11:10:33 +01002665boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
Raef Coles27a61452019-09-25 15:32:25 +01002666{
Raef Colesaf082382019-10-01 11:10:33 +01002667 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2668 memset((void*)img_dst, 0, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002669
2670 return 0;
2671}
Oliver Swedef9982442018-08-24 18:37:44 +01002672#endif /* MCUBOOT_RAM_LOADING */
2673
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002674/**
2675 * Prepares the booting process. This function choose the newer image in flash
David Vinczecea8b592019-10-29 16:09:51 +01002676 * as appropriate, and tells you what address to boot from.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002677 *
David Vinczecea8b592019-10-29 16:09:51 +01002678 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002679 * @param rsp On success, indicates how booting should occur.
2680 *
2681 * @return 0 on success; nonzero on failure.
2682 */
2683int
David Vinczecea8b592019-10-29 16:09:51 +01002684context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002685{
2686 size_t slot = 0;
2687 int32_t i;
2688 int rc;
2689 int fa_id;
2690 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2691 uint32_t img_cnt;
Raef Coles27a61452019-09-25 15:32:25 +01002692 struct image_header *selected_image_header;
2693#ifdef MCUBOOT_RAM_LOADING
2694 int image_copied = 0;
Raef Colesaf082382019-10-01 11:10:33 +01002695 uint32_t img_dst = 0;
2696 uint32_t img_sz = 0;
Raef Coles27a61452019-09-25 15:32:25 +01002697#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002698
David Vincze8bdfc2d2019-03-18 15:49:23 +01002699 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2700 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002701
David Vinczecea8b592019-10-29 16:09:51 +01002702 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = &primary_slot_sectors[0];
2703 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002704
2705 /* Open boot_data image areas for the duration of this call. */
2706 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2707 fa_id = flash_area_id_from_image_slot(i);
David Vinczecea8b592019-10-29 16:09:51 +01002708 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002709 assert(rc == 0);
2710 }
2711
2712 /* Determine the sector layout of the image slots. */
David Vinczecea8b592019-10-29 16:09:51 +01002713 rc = boot_read_sectors(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002714 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002715 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - "
2716 "too small?", BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002717 goto out;
2718 }
2719
2720 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002721 rc = boot_read_image_headers(state, false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002722 if (rc != 0) {
2723 goto out;
2724 }
2725
David Vinczecea8b592019-10-29 16:09:51 +01002726 img_cnt = boot_get_boot_sequence(state, boot_sequence, BOOT_NUM_SLOTS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002727 if (img_cnt) {
2728 /* Authenticate images */
2729 for (i = 0; i < img_cnt; i++) {
Raef Coles27a61452019-09-25 15:32:25 +01002730
2731 slot = boot_sequence[i];
David Vinczecea8b592019-10-29 16:09:51 +01002732 selected_image_header = boot_img_hdr(state, slot);
Raef Coles27a61452019-09-25 15:32:25 +01002733
2734#ifdef MCUBOOT_RAM_LOADING
2735 if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
Raef Colesaf082382019-10-01 11:10:33 +01002736
2737 img_dst = selected_image_header->ih_load_addr;
2738
David Vinczecea8b592019-10-29 16:09:51 +01002739 rc = boot_read_image_size(state, slot, &img_sz);
Raef Colesaf082382019-10-01 11:10:33 +01002740 if (rc != 0) {
2741 rc = BOOT_EFLASH;
2742 BOOT_LOG_INF("Could not load image headers from the image"
2743 "in the %s slot.",
2744 (slot == BOOT_PRIMARY_SLOT) ?
2745 "primary" : "secondary");
2746 continue;
2747 }
2748
2749 rc = boot_verify_ram_loading_address(img_dst, img_sz);
2750 if (rc != 0) {
2751 BOOT_LOG_INF("Could not copy image from the %s slot in "
2752 "the Flash to load address 0x%x in SRAM as"
2753 " the image would overlap memory outside"
2754 " the defined executable region.",
2755 (slot == BOOT_PRIMARY_SLOT) ?
2756 "primary" : "secondary",
2757 selected_image_header->ih_load_addr);
2758 continue;
2759 }
2760
Raef Coles27a61452019-09-25 15:32:25 +01002761 /* Copy image to the load address from where it
2762 * currently resides in flash
2763 */
David Vinczecea8b592019-10-29 16:09:51 +01002764 rc = boot_copy_image_to_sram(state, slot, selected_image_header,
Raef Colesaf082382019-10-01 11:10:33 +01002765 img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002766 if (rc != 0) {
2767 rc = BOOT_EBADIMAGE;
2768 BOOT_LOG_INF("Could not copy image from the %s slot in "
2769 "the Flash to load address 0x%x in SRAM, "
2770 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2771 "primary" : "secondary",
2772 selected_image_header->ih_load_addr);
2773 continue;
2774 } else {
2775 BOOT_LOG_INF("Image has been copied from the %s slot in "
2776 "the flash to SRAM address 0x%x",
2777 (slot == BOOT_PRIMARY_SLOT) ?
2778 "primary" : "secondary",
2779 selected_image_header->ih_load_addr);
2780 image_copied = 1;
2781 }
2782 } else {
2783 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2784 * MCUBOOT_RAM_LOADING is set.
2785 */
2786 rc = BOOT_EBADIMAGE;
2787 continue;
2788 }
2789#endif /* MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002790 rc = boot_validate_slot(state, slot, NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002791 if (rc == 0) {
Raef Coles27a61452019-09-25 15:32:25 +01002792 /* If a valid image is found then there is no reason to check
2793 * the rest of the images, as they were already ordered by
2794 * preference.
2795 */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002796 break;
2797 }
Raef Coles27a61452019-09-25 15:32:25 +01002798#ifdef MCUBOOT_RAM_LOADING
2799 else if (image_copied) {
2800 /* If an image is found to be invalid then it is removed from
2801 * RAM to prevent it being a shellcode vector.
2802 */
Raef Colesaf082382019-10-01 11:10:33 +01002803 boot_remove_image_from_sram(img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002804 image_copied = 0;
2805 }
2806#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002807 }
2808 if (rc) {
2809 /* If there was no valid image at all */
2810 rc = BOOT_EBADIMAGE;
2811 goto out;
2812 }
2813
David Vincze060968d2019-05-23 01:13:14 +02002814 /* Update the security counter with the newest image's security
2815 * counter value.
2816 */
David Vinczecea8b592019-10-29 16:09:51 +01002817 rc = boot_update_security_counter(BOOT_CURR_IMG(state), slot,
2818 selected_image_header);
David Vincze060968d2019-05-23 01:13:14 +02002819 if (rc != 0) {
2820 BOOT_LOG_ERR("Security counter update failed after image "
2821 "validation.");
2822 goto out;
2823 }
2824
Oliver Swedef9982442018-08-24 18:37:44 +01002825
David Vincze8a2a4e22019-05-24 10:14:23 +02002826#ifdef MCUBOOT_RAM_LOADING
Raef Coles27a61452019-09-25 15:32:25 +01002827 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
2828 selected_image_header->ih_load_addr);
2829#else
2830 BOOT_LOG_INF("Booting image from the %s slot",
2831 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2832#endif /* MCUBOOT_RAM_LOADING */
Oliver Swedef9982442018-08-24 18:37:44 +01002833
Raef Coles27a61452019-09-25 15:32:25 +01002834 rsp->br_hdr = selected_image_header;
David Vinczecea8b592019-10-29 16:09:51 +01002835 rsp->br_image_off = boot_img_slot_off(state, slot);
2836 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002837 } else {
2838 /* No candidate image available */
2839 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002840 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002841 }
2842
Tamas Ban0e8ab302019-01-17 11:45:31 +00002843 /* Save boot status to shared memory area */
2844 rc = boot_save_boot_status(SW_S_NS,
2845 rsp->br_hdr,
David Vinczecea8b592019-10-29 16:09:51 +01002846 BOOT_IMG_AREA(state, slot));
Tamas Ban0e8ab302019-01-17 11:45:31 +00002847 if (rc) {
2848 BOOT_LOG_ERR("Failed to add data to shared area");
2849 }
2850
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002851out:
2852 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002853 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002854 }
2855 return rc;
2856}
Oliver Swedef9982442018-08-24 18:37:44 +01002857#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002858
2859int
2860boot_go(struct boot_rsp *rsp)
2861{
2862 return context_boot_go(&boot_data, rsp);
2863}