blob: a3301cf902ea409352ece530f05dba57ad38c638 [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 Vincze2ddc1372019-10-25 11:10:08 +020023 * Git SHA of the original version: ac55554059147fff718015be9f4bd3108123f50a
Tamas Ban5b647472019-01-05 08:59:30 +000024 * Modifications are Copyright (c) 2018-2019 Arm Limited.
Tamas Ban581034a2017-12-19 19:54:37 +000025 */
26
Tamas Banf70ef8c2017-12-19 15:35:09 +000027/**
28 * This file provides an interface to the boot loader. Functions defined in
29 * this file should only be called while the boot loader is running.
30 */
31
32#include <assert.h>
33#include <stddef.h>
34#include <stdbool.h>
35#include <inttypes.h>
36#include <stdlib.h>
37#include <string.h>
Tamas Banc3828852018-02-01 12:24:16 +000038#include "flash_map/flash_map.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000039#include "bootutil/bootutil.h"
40#include "bootutil/image.h"
41#include "bootutil_priv.h"
David Vincze73dfbc52019-10-11 13:54:58 +020042#include "bootutil/bootutil_log.h"
Tamas Bana9de4a62018-09-18 08:09:45 +010043#include "bl2/include/tfm_boot_status.h"
44#include "bl2/include/boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020045#include "security_cnt.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000046
Tamas Banf70ef8c2017-12-19 15:35:09 +000047static struct boot_loader_state boot_data;
David Vinczecea8b592019-10-29 16:09:51 +010048
49#if (BOOT_IMAGE_NUMBER > 1)
50#define IMAGES_ITER(x) for ((x) = 0; (x) < BOOT_IMAGE_NUMBER; ++(x))
51#else
52#define IMAGES_ITER(x)
53#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +000054
Oliver Swedef9982442018-08-24 18:37:44 +010055#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze39e78552018-10-10 17:10:01 +020056
David Vincze8bdfc2d2019-03-18 15:49:23 +010057#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020058static int boot_status_fails = 0;
59#define BOOT_STATUS_ASSERT(x) \
60 do { \
61 if (!(x)) { \
62 boot_status_fails++; \
63 } \
64 } while (0)
65#else
David Vincze401c7422019-06-21 20:44:05 +020066#define BOOT_STATUS_ASSERT(x) ASSERT(x)
David Vincze39e78552018-10-10 17:10:01 +020067#endif
68
Tamas Banf70ef8c2017-12-19 15:35:09 +000069struct boot_status_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010070 uint8_t bst_magic_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000071 uint8_t bst_magic_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +010072 uint8_t bst_copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000073 uint8_t bst_status_source;
74};
75
76/**
77 * This set of tables maps swap state contents to boot status location.
78 * When searching for a match, these tables must be iterated in order.
79 */
80static const struct boot_status_table boot_status_tables[] = {
81 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010082 /* | primary slot | scratch |
83 * ----------+--------------+--------------|
84 * magic | Good | Any |
85 * copy-done | Set | N/A |
86 * ----------+--------------+--------------'
87 * source: none |
88 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +000089 */
David Vincze8bdfc2d2019-03-18 15:49:23 +010090 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +020091 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +010092 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
93 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Tamas Banf70ef8c2017-12-19 15:35:09 +000094 },
95
96 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010097 /* | primary slot | scratch |
98 * ----------+--------------+--------------|
99 * magic | Good | Any |
100 * copy-done | Unset | N/A |
101 * ----------+--------------+--------------'
102 * source: primary slot |
103 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000104 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100105 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +0200106 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +0100107 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
108 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000109 },
110
111 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100112 /* | primary slot | scratch |
113 * ----------+--------------+--------------|
114 * magic | Any | Good |
115 * copy-done | Any | N/A |
116 * ----------+--------------+--------------'
117 * source: scratch |
118 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000119 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100120 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
121 .bst_magic_scratch = BOOT_MAGIC_GOOD,
122 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
123 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000124 },
125
126 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100127 /* | primary slot | scratch |
128 * ----------+--------------+--------------|
129 * magic | Unset | Any |
130 * copy-done | Unset | N/A |
131 * ----------+--------------+--------------|
132 * source: varies |
133 * ----------------------------------------+--------------------------+
Tamas Banf70ef8c2017-12-19 15:35:09 +0000134 * This represents one of two cases: |
135 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze8bdfc2d2019-03-18 15:49:23 +0100136 * o Mid-revert; status in the primary slot. |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000137 * -------------------------------------------------------------------'
138 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100139 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
140 .bst_magic_scratch = BOOT_MAGIC_ANY,
141 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
142 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000143 },
144};
145
146#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000147 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000148
149#define BOOT_LOG_SWAP_STATE(area, state) \
David Vincze401c7422019-06-21 20:44:05 +0200150 BOOT_LOG_INF("%s: magic=%5s, swap_type=0x%x, copy_done=0x%x, " \
151 "image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000152 (area), \
153 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
154 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
155 "bad"), \
David Vincze401c7422019-06-21 20:44:05 +0200156 (state)->swap_type, \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000157 (state)->copy_done, \
158 (state)->image_ok)
Oliver Swedef9982442018-08-24 18:37:44 +0100159#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000160
Tamas Ban056ed0b2019-09-16 12:48:32 +0100161/*
162 * \brief Verifies the image header: magic value, flags, integer overflow.
163 *
164 * \retval 0
165 * \retval BOOT_EBADIMAGE
166 */
167static int
168boot_verify_image_header(struct image_header *hdr)
169{
170 uint32_t image_end;
David Vincze0dc41d22019-10-28 14:56:29 +0100171 uint32_t x;
Tamas Ban056ed0b2019-09-16 12:48:32 +0100172
173 if (hdr->ih_magic != IMAGE_MAGIC) {
174 return BOOT_EBADIMAGE;
175 }
176
177 /* Check input parameters against integer overflow */
David Vincze0dc41d22019-10-28 14:56:29 +0100178 if (!boot_u32_safe_add(&image_end, hdr->ih_hdr_size, hdr->ih_img_size)) {
Tamas Ban056ed0b2019-09-16 12:48:32 +0100179 return BOOT_EBADIMAGE;
180 }
181
David Vincze0dc41d22019-10-28 14:56:29 +0100182 if (!boot_u32_safe_add(&x, image_end, hdr->ih_protect_tlv_size)) {
Tamas Ban056ed0b2019-09-16 12:48:32 +0100183 return BOOT_EBADIMAGE;
184 }
185
Tamas Ban056ed0b2019-09-16 12:48:32 +0100186#if MCUBOOT_RAM_LOADING
187 if (!(hdr->ih_flags & IMAGE_F_RAM_LOAD)) {
188 return BOOT_EBADIMAGE;
189 }
190
191 /* Check input parameters against integer overflow */
David Vincze0dc41d22019-10-28 14:56:29 +0100192 if (!boot_u32_safe_add(&x, image_end, hdr->ih_load_addr)) {
Tamas Ban056ed0b2019-09-16 12:48:32 +0100193 return BOOT_EBADIMAGE;
194 }
195#endif
196
197 return 0;
198}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000199
200static int
David Vinczecea8b592019-10-29 16:09:51 +0100201boot_read_image_header(struct boot_loader_state *state, int slot,
202 struct image_header *out_hdr)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000203{
204 const struct flash_area *fap = NULL;
205 int area_id;
206 int rc;
207
David Vinczecea8b592019-10-29 16:09:51 +0100208#if (BOOT_IMAGE_NUMBER == 1)
209 (void)state;
210#endif
211
212 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000213 rc = flash_area_open(area_id, &fap);
214 if (rc != 0) {
215 rc = BOOT_EFLASH;
216 goto done;
217 }
218
219 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
220 if (rc != 0) {
221 rc = BOOT_EFLASH;
222 goto done;
223 }
224
Tamas Ban056ed0b2019-09-16 12:48:32 +0100225 rc = boot_verify_image_header(out_hdr);
David Vinczecea8b592019-10-29 16:09:51 +0100226 BOOT_IMG_HDR_IS_VALID(state, slot) = (rc == 0);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000227
228done:
229 flash_area_close(fap);
230 return rc;
231}
232
233static int
David Vinczecea8b592019-10-29 16:09:51 +0100234boot_read_image_headers(struct boot_loader_state *state, bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000235{
236 int rc;
237 int i;
238
239 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100240 rc = boot_read_image_header(state, i, boot_img_hdr(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000241 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200242 /* If `require_all` is set, fail on any single fail, otherwise
243 * if at least the first slot's header was read successfully,
244 * then the boot loader can attempt a boot.
245 *
246 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000247 */
David Vincze39e78552018-10-10 17:10:01 +0200248 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000249 return 0;
250 } else {
251 return rc;
252 }
253 }
254 }
255
256 return 0;
257}
258
Raef Coles204c5b42019-09-05 09:18:47 +0100259static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +0100260boot_write_sz(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000261{
Raef Coles204c5b42019-09-05 09:18:47 +0100262 uint32_t elem_sz;
263 uint32_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000264
265 /* Figure out what size to write update status update as. The size depends
266 * on what the minimum write size is for scratch area, active image slot.
267 * We need to use the bigger of those 2 values.
268 */
David Vinczecea8b592019-10-29 16:09:51 +0100269 elem_sz = flash_area_align(BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT));
270 align = flash_area_align(BOOT_SCRATCH_AREA(state));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000271 if (align > elem_sz) {
272 elem_sz = align;
273 }
274
275 return elem_sz;
276}
277
David Vinczecea8b592019-10-29 16:09:51 +0100278#ifndef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
279static int
280boot_initialize_area(struct boot_loader_state *state, int flash_area)
281{
282 int num_sectors = BOOT_MAX_IMG_SECTORS;
283 int rc;
284
285 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
286 rc = flash_area_to_sectors(flash_area, &num_sectors,
287 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors);
288 BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors = (size_t)num_sectors;
289
290 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
291 rc = flash_area_to_sectors(flash_area, &num_sectors,
292 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors);
293 BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors = (size_t)num_sectors;
294
295 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
296 rc = flash_area_to_sectors(flash_area, &num_sectors,
297 state->scratch.sectors);
298 state->scratch.num_sectors = (size_t)num_sectors;
299 } else {
300 return BOOT_EFLASH;
301 }
302
303 return rc;
304}
305#else /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
306static int
307boot_initialize_area(struct boot_loader_state *state, int flash_area)
308{
309 uint32_t num_sectors;
310 struct flash_sector *out_sectors;
311 size_t *out_num_sectors;
312 int rc;
313
314 num_sectors = BOOT_MAX_IMG_SECTORS;
315
316 if (flash_area == FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state))) {
317 out_sectors = BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors;
318 out_num_sectors = &BOOT_IMG(state, BOOT_PRIMARY_SLOT).num_sectors;
319 } else if (flash_area == FLASH_AREA_IMAGE_SECONDARY(BOOT_CURR_IMG(state))) {
320 out_sectors = BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors;
321 out_num_sectors = &BOOT_IMG(state, BOOT_SECONDARY_SLOT).num_sectors;
322 } else if (flash_area == FLASH_AREA_IMAGE_SCRATCH) {
323 out_sectors = state->scratch.sectors;
324 out_num_sectors = &state->scratch.num_sectors;
325 } else {
326 return BOOT_EFLASH;
327 }
328
329 rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
330 if (rc != 0) {
331 return rc;
332 }
333 *out_num_sectors = num_sectors;
334 return 0;
335}
336#endif /* !defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
337
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000338/**
339 * Determines the sector layout of both image slots and the scratch area.
340 * This information is necessary for calculating the number of bytes to erase
341 * and copy during an image swap. The information collected during this
David Vinczecea8b592019-10-29 16:09:51 +0100342 * function is used to populate the state.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000343 */
344static int
David Vinczecea8b592019-10-29 16:09:51 +0100345boot_read_sectors(struct boot_loader_state *state)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000346{
David Vinczecea8b592019-10-29 16:09:51 +0100347 uint8_t image_index;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000348 int rc;
349
David Vinczecea8b592019-10-29 16:09:51 +0100350 image_index = BOOT_CURR_IMG(state);
351
352 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_PRIMARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000353 if (rc != 0) {
354 return BOOT_EFLASH;
355 }
356
David Vinczecea8b592019-10-29 16:09:51 +0100357 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000358 if (rc != 0) {
359 return BOOT_EFLASH;
360 }
361
David Vinczecea8b592019-10-29 16:09:51 +0100362 rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SCRATCH);
David Vincze401c7422019-06-21 20:44:05 +0200363 if (rc != 0) {
364 return BOOT_EFLASH;
365 }
366
David Vinczecea8b592019-10-29 16:09:51 +0100367 BOOT_WRITE_SZ(state) = boot_write_sz(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000368
369 return 0;
370}
371
David Vincze060968d2019-05-23 01:13:14 +0200372/**
373 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000374 */
375static int
David Vinczecea8b592019-10-29 16:09:51 +0100376boot_image_check(struct boot_loader_state *state, struct image_header *hdr,
377 const struct flash_area *fap, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000378{
379 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
David Vinczecea8b592019-10-29 16:09:51 +0100380 uint8_t image_index;
381
382#if (BOOT_IMAGE_NUMBER == 1)
383 (void)state;
384#endif
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000385
David Vincze401c7422019-06-21 20:44:05 +0200386 (void)bs;
387
David Vinczecea8b592019-10-29 16:09:51 +0100388 image_index = BOOT_CURR_IMG(state);
389
390 if (bootutil_img_validate(image_index, hdr, fap, tmpbuf,
391 BOOT_TMPBUF_SZ, NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000392 return BOOT_EBADIMAGE;
393 }
David Vinczecea8b592019-10-29 16:09:51 +0100394
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000395 return 0;
396}
397
David Vincze401c7422019-06-21 20:44:05 +0200398/*
399 * Check that a memory area consists of a given value.
400 */
401static inline bool
402boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200403{
404 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200405 uint8_t *p = (uint8_t *)data;
406 for (i = 0; i < len; i++) {
407 if (val != p[i]) {
408 return false;
David Vincze39e78552018-10-10 17:10:01 +0200409 }
410 }
David Vincze401c7422019-06-21 20:44:05 +0200411 return true;
David Vincze39e78552018-10-10 17:10:01 +0200412}
413
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000414static int
David Vinczecea8b592019-10-29 16:09:51 +0100415boot_check_header_erased(struct boot_loader_state *state, int slot)
David Vincze401c7422019-06-21 20:44:05 +0200416{
417 const struct flash_area *fap;
418 struct image_header *hdr;
419 uint8_t erased_val;
David Vinczecea8b592019-10-29 16:09:51 +0100420 int area_id;
David Vincze401c7422019-06-21 20:44:05 +0200421 int rc;
422
David Vinczecea8b592019-10-29 16:09:51 +0100423 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
424 rc = flash_area_open(area_id, &fap);
David Vincze401c7422019-06-21 20:44:05 +0200425 if (rc != 0) {
426 return -1;
427 }
428
429 erased_val = flash_area_erased_val(fap);
430 flash_area_close(fap);
431
David Vinczecea8b592019-10-29 16:09:51 +0100432 hdr = boot_img_hdr(state, slot);
David Vincze401c7422019-06-21 20:44:05 +0200433 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
434 sizeof(hdr->ih_magic))) {
435 return -1;
436 }
437
438 return 0;
439}
440
David Vinczecea8b592019-10-29 16:09:51 +0100441/*
442 * Check that there is a valid image in a slot
443 *
444 * @returns
445 * 0 if image was succesfully validated
446 * 1 if no bootloable image was found
447 * -1 on any errors
448 */
David Vincze401c7422019-06-21 20:44:05 +0200449static int
David Vinczecea8b592019-10-29 16:09:51 +0100450boot_validate_slot(struct boot_loader_state *state, int slot,
451 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000452{
453 const struct flash_area *fap;
454 struct image_header *hdr;
David Vinczecea8b592019-10-29 16:09:51 +0100455 int area_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000456 int rc;
457
David Vinczecea8b592019-10-29 16:09:51 +0100458 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
459 rc = flash_area_open(area_id, &fap);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000460 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +0100461 return -1;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000462 }
463
David Vinczecea8b592019-10-29 16:09:51 +0100464 hdr = boot_img_hdr(state, slot);
465 if ((boot_check_header_erased(state, slot) == 0) ||
David Vincze401c7422019-06-21 20:44:05 +0200466 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100467 /* No bootable image in slot; continue booting from the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100468 rc = 1;
David Vincze401c7422019-06-21 20:44:05 +0200469 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200470 }
471
David Vinczecea8b592019-10-29 16:09:51 +0100472 if ((!BOOT_IMG_HDR_IS_VALID(state, slot)) ||
473 (boot_image_check(state, hdr, fap, bs) != 0)) {
David Vincze401c7422019-06-21 20:44:05 +0200474 if (slot != BOOT_PRIMARY_SLOT) {
David Vinczecea8b592019-10-29 16:09:51 +0100475 flash_area_erase(fap, 0, fap->fa_size);
David Vincze8bdfc2d2019-03-18 15:49:23 +0100476 /* Image in the secondary slot is invalid. Erase the image and
477 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000478 */
479 }
David Vinczecea8b592019-10-29 16:09:51 +0100480 BOOT_LOG_ERR("Image in the %s slot is not valid!",
481 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200482 rc = -1;
483 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000484 }
485
David Vincze8bdfc2d2019-03-18 15:49:23 +0100486 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200487 rc = 0;
488
489out:
490 flash_area_close(fap);
491 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000492}
493
David Vincze060968d2019-05-23 01:13:14 +0200494/**
495 * Updates the stored security counter value with the image's security counter
496 * value which resides in the given slot if it's greater than the stored value.
497 *
David Vinczecea8b592019-10-29 16:09:51 +0100498 * @param image_index Index of the image to determine which security
499 * counter to update.
500 * @param slot Slot number of the image.
501 * @param hdr Pointer to the image header structure of the image
502 * that is currently stored in the given slot.
David Vincze060968d2019-05-23 01:13:14 +0200503 *
David Vinczecea8b592019-10-29 16:09:51 +0100504 * @return 0 on success; nonzero on failure.
David Vincze060968d2019-05-23 01:13:14 +0200505 */
506static int
David Vinczecea8b592019-10-29 16:09:51 +0100507boot_update_security_counter(uint8_t image_index, int slot,
508 struct image_header *hdr)
David Vincze060968d2019-05-23 01:13:14 +0200509{
510 const struct flash_area *fap = NULL;
511 uint32_t img_security_cnt;
512 int rc;
513
David Vinczecea8b592019-10-29 16:09:51 +0100514 rc = flash_area_open(flash_area_id_from_multi_image_slot(image_index, slot),
515 &fap);
David Vincze060968d2019-05-23 01:13:14 +0200516 if (rc != 0) {
517 rc = BOOT_EFLASH;
518 goto done;
519 }
520
521 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
522 if (rc != 0) {
523 goto done;
524 }
525
David Vinczecea8b592019-10-29 16:09:51 +0100526 rc = boot_nv_security_counter_update(image_index, img_security_cnt);
David Vincze060968d2019-05-23 01:13:14 +0200527 if (rc != 0) {
528 goto done;
529 }
530
531done:
532 flash_area_close(fap);
533 return rc;
534}
535
Oliver Swedef9982442018-08-24 18:37:44 +0100536#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
537/*
538 * Compute the total size of the given image. Includes the size of
539 * the TLVs.
540 */
541static int
David Vinczecea8b592019-10-29 16:09:51 +0100542boot_read_image_size(struct boot_loader_state *state, int slot, uint32_t *size)
Oliver Swedef9982442018-08-24 18:37:44 +0100543{
544 const struct flash_area *fap = NULL;
David Vincze07706a42019-12-12 18:20:12 +0100545 struct image_tlv_info info;
David Vinczecea8b592019-10-29 16:09:51 +0100546 uint32_t off;
David Vincze61bd1e52019-10-24 16:47:31 +0200547 uint32_t protect_tlv_size;
Oliver Swedef9982442018-08-24 18:37:44 +0100548 int area_id;
549 int rc;
550
David Vinczecea8b592019-10-29 16:09:51 +0100551#if (BOOT_IMAGE_NUMBER == 1)
552 (void)state;
553#endif
554
555 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
Oliver Swedef9982442018-08-24 18:37:44 +0100556 rc = flash_area_open(area_id, &fap);
557 if (rc != 0) {
558 rc = BOOT_EFLASH;
559 goto done;
560 }
561
David Vincze07706a42019-12-12 18:20:12 +0100562 off = BOOT_TLV_OFF(boot_img_hdr(state, slot));
563
564 if (flash_area_read(fap, off, &info, sizeof(info))) {
565 rc = BOOT_EFLASH;
Oliver Swedef9982442018-08-24 18:37:44 +0100566 goto done;
567 }
David Vincze07706a42019-12-12 18:20:12 +0100568
David Vincze61bd1e52019-10-24 16:47:31 +0200569 protect_tlv_size = boot_img_hdr(state, slot)->ih_protect_tlv_size;
570 if (info.it_magic == IMAGE_TLV_PROT_INFO_MAGIC) {
571 if (protect_tlv_size != info.it_tlv_tot) {
572 rc = BOOT_EBADIMAGE;
573 goto done;
574 }
575
576 if (flash_area_read(fap, off + info.it_tlv_tot, &info, sizeof(info))) {
577 rc = BOOT_EFLASH;
578 goto done;
579 }
580 } else if (protect_tlv_size != 0) {
581 rc = BOOT_EBADIMAGE;
582 goto done;
583 }
584
David Vincze07706a42019-12-12 18:20:12 +0100585 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
586 rc = BOOT_EBADIMAGE;
587 goto done;
588 }
589
David Vincze61bd1e52019-10-24 16:47:31 +0200590 *size = off + protect_tlv_size + info.it_tlv_tot;
Oliver Swedef9982442018-08-24 18:37:44 +0100591 rc = 0;
592
593done:
594 flash_area_close(fap);
595 return rc;
596}
597#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
598
599#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000600/**
Tamas Ban581034a2017-12-19 19:54:37 +0000601 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000602 * status is necessary for completing a swap that was interrupted by a boot
603 * loader reset.
604 *
David Vincze401c7422019-06-21 20:44:05 +0200605 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
606 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000607 */
608static int
David Vinczecea8b592019-10-29 16:09:51 +0100609boot_status_source(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000610{
611 const struct boot_status_table *table;
612 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100613 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000614 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200615 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000616 uint8_t source;
David Vinczecea8b592019-10-29 16:09:51 +0100617 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000618
David Vinczecea8b592019-10-29 16:09:51 +0100619#if (BOOT_IMAGE_NUMBER == 1)
620 (void)state;
621#endif
622
623 image_index = BOOT_CURR_IMG(state);
624 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY(image_index),
625 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000626 assert(rc == 0);
627
628 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
629 assert(rc == 0);
630
David Vincze401c7422019-06-21 20:44:05 +0200631 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000632 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
633
634 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
635 table = &boot_status_tables[i];
636
David Vincze401c7422019-06-21 20:44:05 +0200637 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
638 state_primary_slot.magic) &&
639 boot_magic_compatible_check(table->bst_magic_scratch,
640 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100641 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
642 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
643 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000644 source = table->bst_status_source;
David Vincze7384ee72019-07-23 17:00:42 +0200645
646#if (BOOT_IMAGE_NUMBER > 1)
647 /* In case of multi-image boot it can happen that if boot status
648 * info is found on scratch area then it does not belong to the
649 * currently examined image.
650 */
651 if (source == BOOT_STATUS_SOURCE_SCRATCH &&
David Vinczecea8b592019-10-29 16:09:51 +0100652 state_scratch.image_num != BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +0200653 source = BOOT_STATUS_SOURCE_NONE;
654 }
655#endif
656
Tamas Banf70ef8c2017-12-19 15:35:09 +0000657 BOOT_LOG_INF("Boot source: %s",
658 source == BOOT_STATUS_SOURCE_NONE ? "none" :
659 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100660 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
661 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000662 return source;
663 }
664 }
665
666 BOOT_LOG_INF("Boot source: none");
667 return BOOT_STATUS_SOURCE_NONE;
668}
669
David Vincze401c7422019-06-21 20:44:05 +0200670/*
671 * Slots are compatible when all sectors that store upto to size of the image
672 * round up to sector size, in both slot's are able to fit in the scratch
673 * area, and have sizes that are a multiple of each other (powers of two
674 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000675 */
676static int
David Vinczecea8b592019-10-29 16:09:51 +0100677boot_slots_compatible(struct boot_loader_state *state)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000678{
David Vincze401c7422019-06-21 20:44:05 +0200679 size_t num_sectors_primary;
680 size_t num_sectors_secondary;
681 size_t sz0, sz1;
682 size_t primary_slot_sz, secondary_slot_sz;
683 size_t scratch_sz;
684 size_t i, j;
685 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000686
David Vinczecea8b592019-10-29 16:09:51 +0100687 num_sectors_primary = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
688 num_sectors_secondary = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT);
David Vincze401c7422019-06-21 20:44:05 +0200689 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
690 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200691 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000692 return 0;
693 }
David Vincze39e78552018-10-10 17:10:01 +0200694
David Vinczecea8b592019-10-29 16:09:51 +0100695 scratch_sz = boot_scratch_area_size(state);
David Vincze401c7422019-06-21 20:44:05 +0200696
697 /*
698 * The following loop scans all sectors in a linear fashion, assuring that
699 * for each possible sector in each slot, it is able to fit in the other
700 * slot's sector or sectors. Slot's should be compatible as long as any
701 * number of a slot's sectors are able to fit into another, which only
702 * excludes cases where sector sizes are not a multiple of each other.
703 */
704 i = sz0 = primary_slot_sz = 0;
705 j = sz1 = secondary_slot_sz = 0;
706 smaller = 0;
707 while (i < num_sectors_primary || j < num_sectors_secondary) {
708 if (sz0 == sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100709 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
710 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200711 i++;
712 j++;
713 } else if (sz0 < sz1) {
David Vinczecea8b592019-10-29 16:09:51 +0100714 sz0 += boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200715 /* Guarantee that multiple sectors of the secondary slot
716 * fit into the primary slot.
717 */
718 if (smaller == 2) {
719 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
720 " sectors");
721 return 0;
722 }
723 smaller = 1;
724 i++;
725 } else {
David Vinczecea8b592019-10-29 16:09:51 +0100726 sz1 += boot_img_sector_size(state, BOOT_SECONDARY_SLOT, j);
David Vincze401c7422019-06-21 20:44:05 +0200727 /* Guarantee that multiple sectors of the primary slot
728 * fit into the secondary slot.
729 */
730 if (smaller == 1) {
731 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
732 " sectors");
733 return 0;
734 }
735 smaller = 2;
736 j++;
737 }
738 if (sz0 == sz1) {
739 primary_slot_sz += sz0;
740 secondary_slot_sz += sz1;
741 /* Scratch has to fit each swap operation to the size of the larger
742 * sector among the primary slot and the secondary slot.
743 */
744 if (sz0 > scratch_sz || sz1 > scratch_sz) {
745 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
746 " scratch");
747 return 0;
748 }
749 smaller = sz0 = sz1 = 0;
750 }
David Vincze39e78552018-10-10 17:10:01 +0200751 }
752
David Vincze401c7422019-06-21 20:44:05 +0200753 if ((i != num_sectors_primary) ||
754 (j != num_sectors_secondary) ||
755 (primary_slot_sz != secondary_slot_sz)) {
756 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
757 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000758 }
759
760 return 1;
761}
762
Tamas Banf70ef8c2017-12-19 15:35:09 +0000763static uint32_t
764boot_status_internal_off(int idx, int state, int elem_sz)
765{
766 int idx_sz;
767
768 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
769
David Vincze39e78552018-10-10 17:10:01 +0200770 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
771 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000772}
773
774/**
775 * Reads the status of a partially-completed swap, if any. This is necessary
776 * to recover in case the boot lodaer was reset in the middle of a swap
777 * operation.
778 */
779static int
David Vinczecea8b592019-10-29 16:09:51 +0100780boot_read_status_bytes(const struct flash_area *fap,
781 struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000782{
783 uint32_t off;
784 uint8_t status;
785 int max_entries;
786 int found;
David Vincze39e78552018-10-10 17:10:01 +0200787 int found_idx;
788 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000789 int rc;
790 int i;
791
792 off = boot_status_off(fap);
David Vinczecea8b592019-10-29 16:09:51 +0100793 max_entries = boot_status_entries(BOOT_CURR_IMG(state), fap);
794 if (max_entries < 0) {
795 return BOOT_EBADARGS;
796 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000797
798 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200799 found_idx = 0;
800 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000801 for (i = 0; i < max_entries; i++) {
David Vinczecea8b592019-10-29 16:09:51 +0100802 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
David Vincze39e78552018-10-10 17:10:01 +0200803 &status, 1);
804 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000805 return BOOT_EFLASH;
806 }
807
David Vincze39e78552018-10-10 17:10:01 +0200808 if (rc == 1) {
809 if (found && !found_idx) {
810 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000811 }
812 } else if (!found) {
813 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200814 } else if (found_idx) {
815 invalid = 1;
816 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000817 }
818 }
819
David Vincze39e78552018-10-10 17:10:01 +0200820 if (invalid) {
821 /* This means there was an error writing status on the last
822 * swap. Tell user and move on to validation!
823 */
824 BOOT_LOG_ERR("Detected inconsistent status!");
825
David Vincze8bdfc2d2019-03-18 15:49:23 +0100826#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
827 /* With validation of the primary slot disabled, there is no way
828 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200829 */
830 assert(0);
831#endif
832 }
833
Tamas Banf70ef8c2017-12-19 15:35:09 +0000834 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200835 if (!found_idx) {
836 found_idx = i;
837 }
David Vincze39e78552018-10-10 17:10:01 +0200838 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
839 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000840 }
841
842 return 0;
843}
844
845/**
846 * Reads the boot status from the flash. The boot status contains
847 * the current state of an interrupted image copy operation. If the boot
848 * status is not present, or it indicates that previous copy finished,
849 * there is no operation in progress.
850 */
851static int
David Vinczecea8b592019-10-29 16:09:51 +0100852boot_read_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000853{
854 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200855 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200856 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000857 int status_loc;
858 int area_id;
859 int rc;
860
David Vincze39e78552018-10-10 17:10:01 +0200861 memset(bs, 0, sizeof *bs);
862 bs->idx = BOOT_STATUS_IDX_0;
863 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200864 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200865
866#ifdef MCUBOOT_OVERWRITE_ONLY
867 /* Overwrite-only doesn't make use of the swap status area. */
868 return 0;
869#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000870
David Vinczecea8b592019-10-29 16:09:51 +0100871 status_loc = boot_status_source(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000872 switch (status_loc) {
873 case BOOT_STATUS_SOURCE_NONE:
874 return 0;
875
876 case BOOT_STATUS_SOURCE_SCRATCH:
877 area_id = FLASH_AREA_IMAGE_SCRATCH;
878 break;
879
David Vincze8bdfc2d2019-03-18 15:49:23 +0100880 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
David Vinczecea8b592019-10-29 16:09:51 +0100881 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000882 break;
883
884 default:
885 assert(0);
886 return BOOT_EBADARGS;
887 }
888
889 rc = flash_area_open(area_id, &fap);
890 if (rc != 0) {
891 return BOOT_EFLASH;
892 }
893
David Vinczecea8b592019-10-29 16:09:51 +0100894 rc = boot_read_status_bytes(fap, state, bs);
David Vincze401c7422019-06-21 20:44:05 +0200895 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200896 off = boot_swap_info_off(fap);
897 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200898 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200899 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200900 rc = 0;
901 }
David Vincze91b71ef2019-06-24 13:06:47 +0200902
903 /* Extract the swap type info */
904 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200905 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000906
907 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200908
Tamas Banf70ef8c2017-12-19 15:35:09 +0000909 return rc;
910}
911
912/**
913 * Writes the supplied boot status to the flash file system. The boot status
914 * contains the current state of an in-progress image copy operation.
915 *
916 * @param bs The boot status to write.
917 *
918 * @return 0 on success; nonzero on failure.
919 */
920int
David Vinczecea8b592019-10-29 16:09:51 +0100921boot_write_status(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000922{
Tamas Ban581034a2017-12-19 19:54:37 +0000923 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000924 uint32_t off;
925 int area_id;
926 int rc;
927 uint8_t buf[BOOT_MAX_ALIGN];
Raef Coles204c5b42019-09-05 09:18:47 +0100928 uint32_t align;
David Vincze39e78552018-10-10 17:10:01 +0200929 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000930
931 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100932 * the trailer. Since in the last step the primary slot is erased, the
933 * first two status writes go to the scratch which will be copied to
934 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000935 */
936
937 if (bs->use_scratch) {
938 /* Write to scratch. */
939 area_id = FLASH_AREA_IMAGE_SCRATCH;
940 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100941 /* Write to the primary slot. */
David Vinczecea8b592019-10-29 16:09:51 +0100942 area_id = FLASH_AREA_IMAGE_PRIMARY(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +0000943 }
944
945 rc = flash_area_open(area_id, &fap);
946 if (rc != 0) {
947 rc = BOOT_EFLASH;
948 goto done;
949 }
950
951 off = boot_status_off(fap) +
David Vinczecea8b592019-10-29 16:09:51 +0100952 boot_status_internal_off(bs->idx, bs->state, BOOT_WRITE_SZ(state));
Tamas Banc3828852018-02-01 12:24:16 +0000953 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200954 erased_val = flash_area_erased_val(fap);
955 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000956 buf[0] = bs->state;
957
958 rc = flash_area_write(fap, off, buf, align);
959 if (rc != 0) {
960 rc = BOOT_EFLASH;
961 goto done;
962 }
963
964 rc = 0;
965
966done:
967 flash_area_close(fap);
968 return rc;
969}
970
Tamas Banf70ef8c2017-12-19 15:35:09 +0000971/**
972 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100973 * that a swap operation is required, the image in the secondary slot is checked
974 * for validity. If the image in the secondary slot is invalid, it is erased,
975 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000976 *
977 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
978 */
979static int
David Vinczecea8b592019-10-29 16:09:51 +0100980boot_validated_swap_type(struct boot_loader_state *state,
981 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000982{
983 int swap_type;
David Vinczecea8b592019-10-29 16:09:51 +0100984 int rc;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000985
David Vinczecea8b592019-10-29 16:09:51 +0100986 swap_type = boot_swap_type_multi(BOOT_CURR_IMG(state));
987 if (BOOT_IS_UPGRADE(swap_type)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100988 /* Boot loader wants to switch to the secondary slot.
989 * Ensure image is valid.
990 */
David Vinczecea8b592019-10-29 16:09:51 +0100991 rc = boot_validate_slot(state, BOOT_SECONDARY_SLOT, bs);
992 if (rc == 1) {
993 swap_type = BOOT_SWAP_TYPE_NONE;
994 } else if (rc != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000995 swap_type = BOOT_SWAP_TYPE_FAIL;
996 }
997 }
998
999 return swap_type;
1000}
1001
1002/**
1003 * Calculates the number of sectors the scratch area can contain. A "last"
1004 * source sector is specified because images are copied backwards in flash
1005 * (final index to index number 0).
1006 *
1007 * @param last_sector_idx The index of the last source sector
1008 * (inclusive).
1009 * @param out_first_sector_idx The index of the first source sector
1010 * (inclusive) gets written here.
1011 *
1012 * @return The number of bytes comprised by the
1013 * [first-sector, last-sector] range.
1014 */
1015#ifndef MCUBOOT_OVERWRITE_ONLY
1016static uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01001017boot_copy_sz(struct boot_loader_state *state, int last_sector_idx,
1018 int *out_first_sector_idx)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001019{
1020 size_t scratch_sz;
1021 uint32_t new_sz;
1022 uint32_t sz;
1023 int i;
1024
1025 sz = 0;
1026
David Vinczecea8b592019-10-29 16:09:51 +01001027 scratch_sz = boot_scratch_area_size(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001028 for (i = last_sector_idx; i >= 0; i--) {
David Vinczecea8b592019-10-29 16:09:51 +01001029 new_sz = sz + boot_img_sector_size(state, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +02001030 /*
1031 * The secondary slot is not being checked here, because
1032 * `boot_slots_compatible` already provides assurance that the copy size
1033 * will be compatible with the primary slot and scratch.
1034 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001035 if (new_sz > scratch_sz) {
1036 break;
1037 }
1038 sz = new_sz;
1039 }
1040
1041 /* i currently refers to a sector that doesn't fit or it is -1 because all
1042 * sectors have been processed. In both cases, exclude sector i.
1043 */
1044 *out_first_sector_idx = i + 1;
1045 return sz;
1046}
1047#endif /* !MCUBOOT_OVERWRITE_ONLY */
1048
1049/**
David Vinczef7641fa2018-09-04 18:29:46 +02001050 * Erases a region of flash.
1051 *
David Vincze401c7422019-06-21 20:44:05 +02001052 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +02001053 * @param off The offset within the flash area to start the
1054 * erase.
1055 * @param sz The number of bytes to erase.
1056 *
1057 * @return 0 on success; nonzero on failure.
1058 */
David Vincze401c7422019-06-21 20:44:05 +02001059static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001060boot_erase_region(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +02001061{
David Vincze401c7422019-06-21 20:44:05 +02001062 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001063}
1064
1065/**
Tamas Banf70ef8c2017-12-19 15:35:09 +00001066 * Copies the contents of one flash region to another. You must erase the
1067 * destination region prior to calling this function.
1068 *
1069 * @param flash_area_id_src The ID of the source flash area.
1070 * @param flash_area_id_dst The ID of the destination flash area.
1071 * @param off_src The offset within the source flash area to
1072 * copy from.
1073 * @param off_dst The offset within the destination flash area to
1074 * copy to.
1075 * @param sz The number of bytes to copy.
1076 *
1077 * @return 0 on success; nonzero on failure.
1078 */
1079static int
David Vinczecea8b592019-10-29 16:09:51 +01001080boot_copy_region(struct boot_loader_state *state,
1081 const struct flash_area *fap_src,
David Vincze401c7422019-06-21 20:44:05 +02001082 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001083 uint32_t off_src, uint32_t off_dst, uint32_t sz)
1084{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001085 uint32_t bytes_copied;
1086 int chunk_sz;
1087 int rc;
1088
1089 static uint8_t buf[1024];
1090
David Vinczecea8b592019-10-29 16:09:51 +01001091 (void)state;
1092
Tamas Banf70ef8c2017-12-19 15:35:09 +00001093 bytes_copied = 0;
1094 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +00001095 if (sz - bytes_copied > sizeof(buf)) {
1096 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001097 } else {
1098 chunk_sz = sz - bytes_copied;
1099 }
1100
1101 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
1102 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001103 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001104 }
1105
1106 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
1107 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +02001108 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001109 }
1110
1111 bytes_copied += chunk_sz;
1112 }
1113
David Vincze401c7422019-06-21 20:44:05 +02001114 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001115}
1116
1117#ifndef MCUBOOT_OVERWRITE_ONLY
1118static inline int
David Vinczecea8b592019-10-29 16:09:51 +01001119boot_status_init(const struct boot_loader_state *state,
1120 const struct flash_area *fap,
1121 const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001122{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001123 struct boot_swap_state swap_state;
David Vinczecea8b592019-10-29 16:09:51 +01001124 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001125 int rc;
1126
David Vinczecea8b592019-10-29 16:09:51 +01001127#if (BOOT_IMAGE_NUMBER == 1)
1128 (void)state;
1129#endif
1130
1131 image_index = BOOT_CURR_IMG(state);
1132
David Vincze401c7422019-06-21 20:44:05 +02001133 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001134
David Vinczecea8b592019-10-29 16:09:51 +01001135 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY(image_index),
1136 &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001137 assert(rc == 0);
1138
David Vincze401c7422019-06-21 20:44:05 +02001139 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01001140 rc = boot_write_swap_info(fap, bs->swap_type, image_index);
David Vincze401c7422019-06-21 20:44:05 +02001141 assert(rc == 0);
1142 }
1143
Tamas Banf70ef8c2017-12-19 15:35:09 +00001144 if (swap_state.image_ok == BOOT_FLAG_SET) {
1145 rc = boot_write_image_ok(fap);
1146 assert(rc == 0);
1147 }
1148
1149 rc = boot_write_swap_size(fap, bs->swap_size);
1150 assert(rc == 0);
1151
1152 rc = boot_write_magic(fap);
1153 assert(rc == 0);
1154
Tamas Banf70ef8c2017-12-19 15:35:09 +00001155 return 0;
1156}
David Vinczef7641fa2018-09-04 18:29:46 +02001157
1158static int
David Vinczecea8b592019-10-29 16:09:51 +01001159boot_erase_trailer_sectors(const struct boot_loader_state *state,
1160 const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +02001161{
1162 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +02001163 uint32_t sector;
1164 uint32_t trailer_sz;
1165 uint32_t total_sz;
1166 uint32_t off;
1167 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +02001168 int fa_id_primary;
1169 int fa_id_secondary;
David Vinczecea8b592019-10-29 16:09:51 +01001170 uint8_t image_index;
David Vinczef7641fa2018-09-04 18:29:46 +02001171 int rc;
1172
David Vincze401c7422019-06-21 20:44:05 +02001173 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
1174
David Vinczecea8b592019-10-29 16:09:51 +01001175 image_index = BOOT_CURR_IMG(state);
1176 fa_id_primary = flash_area_id_from_multi_image_slot(image_index,
1177 BOOT_PRIMARY_SLOT);
1178 fa_id_secondary = flash_area_id_from_multi_image_slot(image_index,
1179 BOOT_SECONDARY_SLOT);
David Vinczebb207982019-08-21 15:13:04 +02001180
1181 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001182 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001183 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001184 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +02001185 } else {
David Vinczef7641fa2018-09-04 18:29:46 +02001186 return BOOT_EFLASH;
1187 }
1188
David Vincze401c7422019-06-21 20:44:05 +02001189 /* delete starting from last sector and moving to beginning */
David Vinczecea8b592019-10-29 16:09:51 +01001190 sector = boot_img_num_sectors(state, slot) - 1;
1191 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
David Vincze401c7422019-06-21 20:44:05 +02001192 total_sz = 0;
1193 do {
David Vinczecea8b592019-10-29 16:09:51 +01001194 sz = boot_img_sector_size(state, slot, sector);
1195 off = boot_img_sector_off(state, slot, sector);
1196 rc = boot_erase_region(fap, off, sz);
David Vincze401c7422019-06-21 20:44:05 +02001197 assert(rc == 0);
1198
1199 sector--;
1200 total_sz += sz;
1201 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001202
1203 return rc;
1204}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001205
Tamas Banf70ef8c2017-12-19 15:35:09 +00001206/**
1207 * Swaps the contents of two flash regions within the two image slots.
1208 *
1209 * @param idx The index of the first sector in the range of
1210 * sectors being swapped.
1211 * @param sz The number of bytes to swap.
1212 * @param bs The current boot status. This struct gets
1213 * updated according to the outcome.
1214 *
1215 * @return 0 on success; nonzero on failure.
1216 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001217static void
David Vinczecea8b592019-10-29 16:09:51 +01001218boot_swap_sectors(int idx, uint32_t sz, struct boot_loader_state *state,
1219 struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001220{
David Vincze401c7422019-06-21 20:44:05 +02001221 const struct flash_area *fap_primary_slot;
1222 const struct flash_area *fap_secondary_slot;
1223 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001224 uint32_t copy_sz;
1225 uint32_t trailer_sz;
1226 uint32_t img_off;
1227 uint32_t scratch_trailer_off;
1228 struct boot_swap_state swap_state;
1229 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001230 bool erase_scratch;
David Vinczecea8b592019-10-29 16:09:51 +01001231 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001232 int rc;
1233
1234 /* Calculate offset from start of image area. */
David Vinczecea8b592019-10-29 16:09:51 +01001235 img_off = boot_img_sector_off(state, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001236
1237 copy_sz = sz;
David Vinczecea8b592019-10-29 16:09:51 +01001238 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001239
David Vincze401c7422019-06-21 20:44:05 +02001240 /* sz in this function is always sized on a multiple of the sector size.
1241 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001242 * is to determine if we're swapping the last sector. The last sector
1243 * needs special handling because it's where the trailer lives. If we're
1244 * copying it, we need to use scratch to write the trailer temporarily.
1245 *
1246 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1247 * controls if special handling is needed (swapping last sector).
1248 */
David Vinczecea8b592019-10-29 16:09:51 +01001249 last_sector = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT) - 1;
David Vincze7384ee72019-07-23 17:00:42 +02001250 if ((img_off + sz) >
David Vinczecea8b592019-10-29 16:09:51 +01001251 boot_img_sector_off(state, BOOT_PRIMARY_SLOT, last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001252 copy_sz -= trailer_sz;
1253 }
1254
David Vincze39e78552018-10-10 17:10:01 +02001255 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001256
David Vinczecea8b592019-10-29 16:09:51 +01001257 image_index = BOOT_CURR_IMG(state);
1258
1259 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1260 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001261 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001262
David Vinczecea8b592019-10-29 16:09:51 +01001263 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1264 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001265 assert (rc == 0);
1266
1267 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1268 assert (rc == 0);
1269
1270 if (bs->state == BOOT_STATUS_STATE_0) {
1271 BOOT_LOG_DBG("erasing scratch area");
David Vinczecea8b592019-10-29 16:09:51 +01001272 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001273 assert(rc == 0);
1274
David Vincze39e78552018-10-10 17:10:01 +02001275 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001276 /* Write a trailer to the scratch area, even if we don't need the
1277 * scratch area for status. We need a temporary place to store the
1278 * `swap-type` while we erase the primary trailer.
1279 */
David Vinczecea8b592019-10-29 16:09:51 +01001280 rc = boot_status_init(state, fap_scratch, bs);
David Vincze401c7422019-06-21 20:44:05 +02001281 assert(rc == 0);
1282
1283 if (!bs->use_scratch) {
1284 /* Prepare the primary status area... here it is known that the
1285 * last sector is not being used by the image data so it's safe
1286 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001287 */
David Vinczecea8b592019-10-29 16:09:51 +01001288 rc = boot_erase_trailer_sectors(state, fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001289 assert(rc == 0);
1290
David Vinczecea8b592019-10-29 16:09:51 +01001291 rc = boot_status_init(state, fap_primary_slot, bs);
David Vincze401c7422019-06-21 20:44:05 +02001292 assert(rc == 0);
1293
1294 /* Erase the temporary trailer from the scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01001295 rc = boot_erase_region(fap_scratch, 0, fap_scratch->fa_size);
David Vincze401c7422019-06-21 20:44:05 +02001296 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001297 }
1298 }
1299
David Vinczecea8b592019-10-29 16:09:51 +01001300 rc = boot_copy_region(state, fap_secondary_slot, fap_scratch,
David Vincze401c7422019-06-21 20:44:05 +02001301 img_off, 0, copy_sz);
1302 assert(rc == 0);
1303
David Vinczecea8b592019-10-29 16:09:51 +01001304 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001305 bs->state = BOOT_STATUS_STATE_1;
David Vincze39e78552018-10-10 17:10:01 +02001306 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001307 }
1308
David Vincze39e78552018-10-10 17:10:01 +02001309 if (bs->state == BOOT_STATUS_STATE_1) {
David Vinczecea8b592019-10-29 16:09:51 +01001310 rc = boot_erase_region(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001311 assert(rc == 0);
1312
David Vinczecea8b592019-10-29 16:09:51 +01001313 rc = boot_copy_region(state, fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001314 img_off, img_off, copy_sz);
1315 assert(rc == 0);
1316
David Vincze39e78552018-10-10 17:10:01 +02001317 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001318 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001319 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001320 */
David Vinczecea8b592019-10-29 16:09:51 +01001321 rc = boot_erase_trailer_sectors(state, fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001322 assert(rc == 0);
1323 }
1324
David Vinczecea8b592019-10-29 16:09:51 +01001325 rc = boot_write_status(state, bs);
David Vincze39e78552018-10-10 17:10:01 +02001326 bs->state = BOOT_STATUS_STATE_2;
David Vincze39e78552018-10-10 17:10:01 +02001327 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001328 }
1329
David Vincze39e78552018-10-10 17:10:01 +02001330 if (bs->state == BOOT_STATUS_STATE_2) {
David Vinczecea8b592019-10-29 16:09:51 +01001331 rc = boot_erase_region(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001332 assert(rc == 0);
1333
David Vincze401c7422019-06-21 20:44:05 +02001334 /* NOTE: If this is the final sector, we exclude the image trailer from
1335 * this copy (copy_sz was truncated earlier).
1336 */
David Vinczecea8b592019-10-29 16:09:51 +01001337 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001338 0, img_off, copy_sz);
1339 assert(rc == 0);
1340
1341 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001342 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001343
1344 /* copy current status that is being maintained in scratch */
David Vinczecea8b592019-10-29 16:09:51 +01001345 rc = boot_copy_region(state, fap_scratch, fap_primary_slot,
David Vincze401c7422019-06-21 20:44:05 +02001346 scratch_trailer_off, img_off + copy_sz,
David Vinczecea8b592019-10-29 16:09:51 +01001347 (BOOT_STATUS_STATE_COUNT - 1) * BOOT_WRITE_SZ(state));
David Vincze39e78552018-10-10 17:10:01 +02001348 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001349
1350 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1351 &swap_state);
1352 assert(rc == 0);
1353
1354 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001355 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001356 assert(rc == 0);
1357 }
1358
David Vincze401c7422019-06-21 20:44:05 +02001359 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001360 rc = boot_write_swap_info(fap_primary_slot,
1361 swap_state.swap_type,
David Vinczecea8b592019-10-29 16:09:51 +01001362 image_index);
David Vincze401c7422019-06-21 20:44:05 +02001363 assert(rc == 0);
1364 }
1365
1366 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001367 assert(rc == 0);
1368
David Vincze401c7422019-06-21 20:44:05 +02001369 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001370 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001371 }
1372
David Vincze401c7422019-06-21 20:44:05 +02001373 /* If we wrote a trailer to the scratch area, erase it after we persist
1374 * a trailer to the primary slot. We do this to prevent mcuboot from
1375 * reading a stale status from the scratch area in case of immediate
1376 * reset.
1377 */
1378 erase_scratch = bs->use_scratch;
1379 bs->use_scratch = 0;
1380
David Vinczecea8b592019-10-29 16:09:51 +01001381 rc = boot_write_status(state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001382 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001383 bs->state = BOOT_STATUS_STATE_0;
David Vincze39e78552018-10-10 17:10:01 +02001384 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001385
1386 if (erase_scratch) {
David Vinczecea8b592019-10-29 16:09:51 +01001387 rc = boot_erase_region(fap_scratch, 0, sz);
David Vincze401c7422019-06-21 20:44:05 +02001388 assert(rc == 0);
1389 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001390 }
David Vincze401c7422019-06-21 20:44:05 +02001391
1392 flash_area_close(fap_primary_slot);
1393 flash_area_close(fap_secondary_slot);
1394 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001395}
1396#endif /* !MCUBOOT_OVERWRITE_ONLY */
1397
1398/**
David Vincze401c7422019-06-21 20:44:05 +02001399 * Overwrite primary slot with the image contained in the secondary slot.
1400 * If a prior copy operation was interrupted by a system reset, this function
1401 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001402 *
1403 * @param bs The current boot status. This function reads
1404 * this struct to determine if it is resuming
1405 * an interrupted swap operation. This
1406 * function writes the updated status to this
1407 * function on return.
1408 *
1409 * @return 0 on success; nonzero on failure.
1410 */
1411#ifdef MCUBOOT_OVERWRITE_ONLY
1412static int
David Vinczecea8b592019-10-29 16:09:51 +01001413boot_copy_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001414{
1415 size_t sect_count;
1416 size_t sect;
1417 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001418 size_t size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001419 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001420 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001421 const struct flash_area *fap_primary_slot;
1422 const struct flash_area *fap_secondary_slot;
David Vinczecea8b592019-10-29 16:09:51 +01001423 uint8_t image_index;
David Vincze39e78552018-10-10 17:10:01 +02001424
1425 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001426
David Vincze8bdfc2d2019-03-18 15:49:23 +01001427 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1428 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001429
David Vinczecea8b592019-10-29 16:09:51 +01001430 image_index = BOOT_CURR_IMG(state);
1431
1432 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1433 &fap_primary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001434 assert (rc == 0);
1435
David Vinczecea8b592019-10-29 16:09:51 +01001436 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY(image_index),
1437 &fap_secondary_slot);
David Vincze401c7422019-06-21 20:44:05 +02001438 assert (rc == 0);
1439
David Vinczecea8b592019-10-29 16:09:51 +01001440 sect_count = boot_img_num_sectors(state, BOOT_PRIMARY_SLOT);
1441 for (sect = 0, size = 0; sect < sect_count; sect++) {
1442 this_size = boot_img_sector_size(state, BOOT_PRIMARY_SLOT, sect);
1443 rc = boot_erase_region(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001444 assert(rc == 0);
1445
1446 size += this_size;
1447 }
1448
David Vincze8bdfc2d2019-03-18 15:49:23 +01001449 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1450 size);
David Vinczecea8b592019-10-29 16:09:51 +01001451 rc = boot_copy_region(state, fap_secondary_slot, fap_primary_slot,
1452 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001453
David Vincze060968d2019-05-23 01:13:14 +02001454 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001455 * value. Both slots hold the new image at this point, but the secondary
1456 * slot's image header must be passed because the read image headers in the
1457 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001458 */
David Vinczecea8b592019-10-29 16:09:51 +01001459 rc = boot_update_security_counter(BOOT_CURR_IMG(state), BOOT_PRIMARY_SLOT,
1460 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001461 if (rc != 0) {
1462 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1463 return rc;
1464 }
1465
David Vincze39e78552018-10-10 17:10:01 +02001466 /*
1467 * Erases header and trailer. The trailer is erased because when a new
1468 * image is written without a trailer as is the case when using newt, the
1469 * trailer that was left might trigger a new upgrade.
1470 */
David Vincze401c7422019-06-21 20:44:05 +02001471 BOOT_LOG_DBG("erasing secondary header");
David Vinczecea8b592019-10-29 16:09:51 +01001472 rc = boot_erase_region(fap_secondary_slot,
1473 boot_img_sector_off(state, BOOT_SECONDARY_SLOT, 0),
1474 boot_img_sector_size(state, BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001475 assert(rc == 0);
David Vinczecea8b592019-10-29 16:09:51 +01001476 last_sector = boot_img_num_sectors(state, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001477 BOOT_LOG_DBG("erasing secondary trailer");
David Vinczecea8b592019-10-29 16:09:51 +01001478 rc = boot_erase_region(fap_secondary_slot,
1479 boot_img_sector_off(state, BOOT_SECONDARY_SLOT,
1480 last_sector),
1481 boot_img_sector_size(state, BOOT_SECONDARY_SLOT,
1482 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001483 assert(rc == 0);
1484
David Vincze401c7422019-06-21 20:44:05 +02001485 flash_area_close(fap_primary_slot);
1486 flash_area_close(fap_secondary_slot);
1487
David Vincze8bdfc2d2019-03-18 15:49:23 +01001488 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001489
1490 return 0;
1491}
1492#else
David Vincze401c7422019-06-21 20:44:05 +02001493/**
1494 * Swaps the two images in flash. If a prior copy operation was interrupted
1495 * by a system reset, this function completes that operation.
1496 *
1497 * @param bs The current boot status. This function reads
1498 * this struct to determine if it is resuming
1499 * an interrupted swap operation. This
1500 * function writes the updated status to this
1501 * function on return.
1502 *
1503 * @return 0 on success; nonzero on failure.
1504 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001505static int
David Vinczecea8b592019-10-29 16:09:51 +01001506boot_swap_image(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001507{
1508 uint32_t sz;
1509 int first_sector_idx;
1510 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001511 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001512 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001513 struct image_header *hdr;
1514 uint32_t size;
1515 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001516 uint32_t primary_slot_size;
1517 uint32_t secondary_slot_size;
David Vinczecea8b592019-10-29 16:09:51 +01001518 uint8_t image_index;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001519 int rc;
1520
1521 /* FIXME: just do this if asked by user? */
1522
1523 size = copy_size = 0;
David Vinczecea8b592019-10-29 16:09:51 +01001524 image_index = BOOT_CURR_IMG(state);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001525
David Vincze39e78552018-10-10 17:10:01 +02001526 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001527 /*
1528 * No swap ever happened, so need to find the largest image which
1529 * will be used to determine the amount of sectors to swap.
1530 */
David Vinczecea8b592019-10-29 16:09:51 +01001531 hdr = boot_img_hdr(state, BOOT_PRIMARY_SLOT);
1532 if (hdr->ih_magic == IMAGE_MAGIC) {
1533 rc = boot_read_image_size(state, BOOT_PRIMARY_SLOT, &copy_size);
1534 assert(rc == 0);
1535 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001536
David Vinczecea8b592019-10-29 16:09:51 +01001537 hdr = boot_img_hdr(state, BOOT_SECONDARY_SLOT);
1538 if (hdr->ih_magic == IMAGE_MAGIC) {
1539 rc = boot_read_image_size(state, BOOT_SECONDARY_SLOT, &size);
1540 assert(rc == 0);
1541 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001542
1543 if (size > copy_size) {
1544 copy_size = size;
1545 }
1546
1547 bs->swap_size = copy_size;
1548 } else {
1549 /*
1550 * If a swap was under way, the swap_size should already be present
1551 * in the trailer...
1552 */
David Vinczecea8b592019-10-29 16:09:51 +01001553 rc = boot_read_swap_size(image_index, &bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001554 assert(rc == 0);
1555
1556 copy_size = bs->swap_size;
1557 }
1558
David Vincze401c7422019-06-21 20:44:05 +02001559 primary_slot_size = 0;
1560 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001561 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001562 last_idx_secondary_slot = 0;
1563
1564 /*
1565 * Knowing the size of the largest image between both slots, here we
1566 * find what is the last sector in the primary slot that needs swapping.
1567 * Since we already know that both slots are compatible, the secondary
1568 * slot's last sector is not really required after this check is finished.
1569 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001570 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001571 if ((primary_slot_size < copy_size) ||
1572 (primary_slot_size < secondary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001573 primary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001574 BOOT_PRIMARY_SLOT,
1575 last_sector_idx);
1576 }
1577 if ((secondary_slot_size < copy_size) ||
1578 (secondary_slot_size < primary_slot_size)) {
David Vinczecea8b592019-10-29 16:09:51 +01001579 secondary_slot_size += boot_img_sector_size(state,
David Vincze401c7422019-06-21 20:44:05 +02001580 BOOT_SECONDARY_SLOT,
1581 last_idx_secondary_slot);
1582 }
1583 if (primary_slot_size >= copy_size &&
1584 secondary_slot_size >= copy_size &&
1585 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001586 break;
1587 }
1588 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001589 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001590 }
1591
1592 swap_idx = 0;
1593 while (last_sector_idx >= 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001594 sz = boot_copy_sz(state, last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001595 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
David Vinczecea8b592019-10-29 16:09:51 +01001596 boot_swap_sectors(first_sector_idx, sz, state, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001597 }
1598
1599 last_sector_idx = first_sector_idx - 1;
1600 swap_idx++;
1601 }
1602
David Vincze8bdfc2d2019-03-18 15:49:23 +01001603#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001604 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001605 BOOT_LOG_WRN("%d status write fails performing the swap",
1606 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001607 }
1608#endif
1609
Tamas Banf70ef8c2017-12-19 15:35:09 +00001610 return 0;
1611}
1612#endif
1613
David Vincze44b79f42019-10-25 15:39:59 +02001614#ifndef MCUBOOT_OVERWRITE_ONLY
Tamas Banf70ef8c2017-12-19 15:35:09 +00001615/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001616 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001617 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001618static int
David Vinczecea8b592019-10-29 16:09:51 +01001619boot_set_copy_done(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001620{
1621 const struct flash_area *fap;
1622 int rc;
1623
David Vinczecea8b592019-10-29 16:09:51 +01001624 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1625 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001626 if (rc != 0) {
1627 return BOOT_EFLASH;
1628 }
1629
1630 rc = boot_write_copy_done(fap);
1631 flash_area_close(fap);
1632 return rc;
1633}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001634
1635/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001636 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1637 * ensure the status bytes from the image revert operation don't get processed
1638 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001639 *
1640 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001641 * image installed on the primary slot and the new image to be upgrade to has a
1642 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001643 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001644static int
David Vinczecea8b592019-10-29 16:09:51 +01001645boot_set_image_ok(uint8_t image_index)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001646{
1647 const struct flash_area *fap;
1648 struct boot_swap_state state;
1649 int rc;
1650
David Vinczecea8b592019-10-29 16:09:51 +01001651 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY(image_index),
1652 &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001653 if (rc != 0) {
1654 return BOOT_EFLASH;
1655 }
1656
1657 rc = boot_read_swap_state(fap, &state);
1658 if (rc != 0) {
1659 rc = BOOT_EFLASH;
1660 goto out;
1661 }
1662
1663 if (state.image_ok == BOOT_FLAG_UNSET) {
1664 rc = boot_write_image_ok(fap);
1665 }
1666
1667out:
1668 flash_area_close(fap);
1669 return rc;
1670}
1671#endif /* !MCUBOOT_OVERWRITE_ONLY */
1672
David Vinczebb6e3b62019-07-31 14:24:05 +02001673#if (BOOT_IMAGE_NUMBER > 1)
1674/**
David Vinczecea8b592019-10-29 16:09:51 +01001675 * Check if the version of the image is not older than required.
1676 *
1677 * @param req Required minimal image version.
1678 * @param ver Version of the image to be checked.
1679 *
1680 * @return 0 if the version is sufficient, nonzero otherwise.
1681 */
1682static int
1683boot_is_version_sufficient(struct image_version *req,
1684 struct image_version *ver)
1685{
1686 if (ver->iv_major > req->iv_major) {
1687 return 0;
1688 }
1689 if (ver->iv_major < req->iv_major) {
1690 return BOOT_EBADVERSION;
1691 }
1692 /* The major version numbers are equal. */
1693 if (ver->iv_minor > req->iv_minor) {
1694 return 0;
1695 }
1696 if (ver->iv_minor < req->iv_minor) {
1697 return BOOT_EBADVERSION;
1698 }
1699 /* The minor version numbers are equal. */
1700 if (ver->iv_revision < req->iv_revision) {
1701 return BOOT_EBADVERSION;
1702 }
1703
1704 return 0;
1705}
1706
1707/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001708 * Check the image dependency whether it is satisfied and modify
1709 * the swap type if necessary.
1710 *
1711 * @param dep Image dependency which has to be verified.
1712 *
1713 * @return 0 on success; nonzero on failure.
1714 */
1715static int
David Vinczecea8b592019-10-29 16:09:51 +01001716boot_verify_slot_dependency(struct boot_loader_state *state,
1717 struct image_dependency *dep)
David Vinczebb6e3b62019-07-31 14:24:05 +02001718{
1719 struct image_version *dep_version;
1720 size_t dep_slot;
1721 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001722 uint8_t swap_type;
David Vinczebb6e3b62019-07-31 14:24:05 +02001723
1724 /* Determine the source of the image which is the subject of
1725 * the dependency and get it's version. */
David Vinczecea8b592019-10-29 16:09:51 +01001726 swap_type = state->swap_type[dep->image_id];
1727 dep_slot = (swap_type != BOOT_SWAP_TYPE_NONE) ?
David Vinczebb6e3b62019-07-31 14:24:05 +02001728 BOOT_SECONDARY_SLOT : BOOT_PRIMARY_SLOT;
David Vinczecea8b592019-10-29 16:09:51 +01001729 dep_version = &state->imgs[dep->image_id][dep_slot].hdr.ih_ver;
David Vinczebb6e3b62019-07-31 14:24:05 +02001730
1731 rc = boot_is_version_sufficient(&dep->image_min_version, dep_version);
1732 if (rc != 0) {
1733 /* Dependency not satisfied.
1734 * Modify the swap type to decrease the version number of the image
1735 * (which will be located in the primary slot after the boot process),
1736 * consequently the number of unsatisfied dependencies will be
1737 * decreased or remain the same.
1738 */
David Vinczecea8b592019-10-29 16:09:51 +01001739 switch (BOOT_SWAP_TYPE(state)) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001740 case BOOT_SWAP_TYPE_TEST:
1741 case BOOT_SWAP_TYPE_PERM:
David Vinczecea8b592019-10-29 16:09:51 +01001742 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vinczebb6e3b62019-07-31 14:24:05 +02001743 break;
1744 case BOOT_SWAP_TYPE_NONE:
David Vinczecea8b592019-10-29 16:09:51 +01001745 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_REVERT;
David Vinczebb6e3b62019-07-31 14:24:05 +02001746 break;
1747 default:
1748 break;
1749 }
1750 }
1751
1752 return rc;
1753}
1754
1755/**
1756 * Read all dependency TLVs of an image from the flash and verify
1757 * one after another to see if they are all satisfied.
1758 *
1759 * @param slot Image slot number.
1760 *
1761 * @return 0 on success; nonzero on failure.
1762 */
1763static int
David Vinczecea8b592019-10-29 16:09:51 +01001764boot_verify_slot_dependencies(struct boot_loader_state *state, uint32_t slot)
David Vinczebb6e3b62019-07-31 14:24:05 +02001765{
1766 const struct flash_area *fap;
David Vincze07706a42019-12-12 18:20:12 +01001767 struct image_tlv_iter it;
David Vinczebb6e3b62019-07-31 14:24:05 +02001768 struct image_dependency dep;
1769 uint32_t off;
David Vincze07706a42019-12-12 18:20:12 +01001770 uint16_t len;
David Vinczecea8b592019-10-29 16:09:51 +01001771 int area_id;
David Vinczebb6e3b62019-07-31 14:24:05 +02001772 int rc;
1773
David Vinczecea8b592019-10-29 16:09:51 +01001774 area_id = flash_area_id_from_multi_image_slot(BOOT_CURR_IMG(state), slot);
1775 rc = flash_area_open(area_id, &fap);
David Vinczebb6e3b62019-07-31 14:24:05 +02001776 if (rc != 0) {
1777 rc = BOOT_EFLASH;
1778 goto done;
1779 }
1780
David Vincze07706a42019-12-12 18:20:12 +01001781 rc = bootutil_tlv_iter_begin(&it, boot_img_hdr(state, slot), fap,
1782 IMAGE_TLV_DEPENDENCY, true);
David Vinczebb6e3b62019-07-31 14:24:05 +02001783 if (rc != 0) {
David Vinczebb6e3b62019-07-31 14:24:05 +02001784 goto done;
1785 }
1786
David Vincze07706a42019-12-12 18:20:12 +01001787 while (true) {
1788 rc = bootutil_tlv_iter_next(&it, &off, &len, NULL);
1789 if (rc < 0) {
1790 return -1;
1791 } else if (rc > 0) {
1792 rc = 0;
David Vinczebb6e3b62019-07-31 14:24:05 +02001793 break;
1794 }
David Vincze07706a42019-12-12 18:20:12 +01001795
1796 if (len != sizeof(dep)) {
1797 rc = BOOT_EBADIMAGE;
1798 goto done;
1799 }
1800
1801 rc = flash_area_read(fap, off, &dep, len);
1802 if (rc != 0) {
1803 rc = BOOT_EFLASH;
1804 goto done;
1805 }
1806
1807 if (dep.image_id >= BOOT_IMAGE_NUMBER) {
1808 rc = BOOT_EBADARGS;
1809 goto done;
1810 }
1811
1812 /* Verify dependency and modify the swap type if not satisfied. */
1813 rc = boot_verify_slot_dependency(state, &dep);
1814 if (rc != 0) {
1815 /* Dependency not satisfied. */
1816 goto done;
Tamas Ban056ed0b2019-09-16 12:48:32 +01001817 }
David Vinczebb6e3b62019-07-31 14:24:05 +02001818 }
1819
1820done:
1821 flash_area_close(fap);
1822 return rc;
1823}
1824
1825/**
David Vinczebb6e3b62019-07-31 14:24:05 +02001826 * Iterate over all the images and verify whether the image dependencies in the
1827 * TLV area are all satisfied and update the related swap type if necessary.
1828 */
David Vinczecea8b592019-10-29 16:09:51 +01001829static int
1830boot_verify_dependencies(struct boot_loader_state *state)
David Vinczebb6e3b62019-07-31 14:24:05 +02001831{
David Vinczebb6e3b62019-07-31 14:24:05 +02001832 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001833 uint8_t slot;
David Vinczebb6e3b62019-07-31 14:24:05 +02001834
David Vinczecea8b592019-10-29 16:09:51 +01001835 BOOT_CURR_IMG(state) = 0;
1836 while (BOOT_CURR_IMG(state) < BOOT_IMAGE_NUMBER) {
1837 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE &&
1838 BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_FAIL) {
1839 slot = BOOT_SECONDARY_SLOT;
1840 } else {
1841 slot = BOOT_PRIMARY_SLOT;
1842 }
1843
1844 rc = boot_verify_slot_dependencies(state, slot);
David Vinczebb6e3b62019-07-31 14:24:05 +02001845 if (rc == 0) {
1846 /* All dependencies've been satisfied, continue with next image. */
David Vinczecea8b592019-10-29 16:09:51 +01001847 BOOT_CURR_IMG(state)++;
David Vinczebb6e3b62019-07-31 14:24:05 +02001848 } else if (rc == BOOT_EBADVERSION) {
David Vinczecea8b592019-10-29 16:09:51 +01001849 /* Cannot upgrade due to non-met dependencies, so disable all
1850 * image upgrades.
1851 */
1852 for (int idx = 0; idx < BOOT_IMAGE_NUMBER; idx++) {
1853 BOOT_CURR_IMG(state) = idx;
1854 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
1855 }
1856 break;
David Vinczebb6e3b62019-07-31 14:24:05 +02001857 } else {
1858 /* Other error happened, images are inconsistent */
David Vinczecea8b592019-10-29 16:09:51 +01001859 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001860 }
1861 }
David Vinczecea8b592019-10-29 16:09:51 +01001862 return rc;
David Vinczebb6e3b62019-07-31 14:24:05 +02001863}
1864#endif /* (BOOT_IMAGE_NUMBER > 1) */
1865
Tamas Banf70ef8c2017-12-19 15:35:09 +00001866/**
David Vincze7384ee72019-07-23 17:00:42 +02001867 * Performs a clean (not aborted) image update.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001868 *
David Vincze7384ee72019-07-23 17:00:42 +02001869 * @param bs The current boot status.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001870 *
1871 * @return 0 on success; nonzero on failure.
1872 */
1873static int
David Vinczecea8b592019-10-29 16:09:51 +01001874boot_perform_update(struct boot_loader_state *state, struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001875{
Tamas Banf70ef8c2017-12-19 15:35:09 +00001876 int rc;
David Vinczecea8b592019-10-29 16:09:51 +01001877#ifndef MCUBOOT_OVERWRITE_ONLY
1878 uint8_t swap_type;
1879#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001880
David Vincze7384ee72019-07-23 17:00:42 +02001881 /* At this point there are no aborted swaps. */
1882#if defined(MCUBOOT_OVERWRITE_ONLY)
David Vinczecea8b592019-10-29 16:09:51 +01001883 rc = boot_copy_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001884#else
David Vinczecea8b592019-10-29 16:09:51 +01001885 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001886#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001887 assert(rc == 0);
David Vincze7384ee72019-07-23 17:00:42 +02001888
1889#ifndef MCUBOOT_OVERWRITE_ONLY
1890 /* The following state needs image_ok be explicitly set after the
1891 * swap was finished to avoid a new revert.
1892 */
David Vinczecea8b592019-10-29 16:09:51 +01001893 swap_type = BOOT_SWAP_TYPE(state);
1894 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1895 swap_type == BOOT_SWAP_TYPE_PERM) {
1896 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001897 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001898 BOOT_SWAP_TYPE(state) = swap_type = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001899 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001900 }
1901
David Vinczecea8b592019-10-29 16:09:51 +01001902 if (swap_type == BOOT_SWAP_TYPE_PERM) {
David Vincze7384ee72019-07-23 17:00:42 +02001903 /* Update the stored security counter with the new image's security
1904 * counter value. The primary slot holds the new image at this
1905 * point, but the secondary slot's image header must be passed
1906 * because the read image headers in the boot_data structure have
1907 * not been updated yet.
1908 *
1909 * In case of a permanent image swap mcuboot will never attempt to
1910 * revert the images on the next reboot. Therefore, the security
1911 * counter must be increased right after the image upgrade.
David Vincze401c7422019-06-21 20:44:05 +02001912 */
David Vinczecea8b592019-10-29 16:09:51 +01001913 rc = boot_update_security_counter(
1914 BOOT_CURR_IMG(state),
1915 BOOT_PRIMARY_SLOT,
1916 boot_img_hdr(state, BOOT_SECONDARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02001917 if (rc != 0) {
1918 BOOT_LOG_ERR("Security counter update failed after "
1919 "image upgrade.");
David Vinczecea8b592019-10-29 16:09:51 +01001920 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001921 }
1922 }
1923
David Vinczecea8b592019-10-29 16:09:51 +01001924 if (BOOT_IS_UPGRADE(swap_type)) {
1925 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001926 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001927 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001928 }
1929 }
1930#endif /* !MCUBOOT_OVERWRITE_ONLY */
1931
1932 return rc;
1933}
1934
1935/**
1936 * Completes a previously aborted image swap.
1937 *
1938 * @param bs The current boot status.
1939 *
1940 * @return 0 on success; nonzero on failure.
1941 */
1942#if !defined(MCUBOOT_OVERWRITE_ONLY)
1943static int
David Vinczecea8b592019-10-29 16:09:51 +01001944boot_complete_partial_swap(struct boot_loader_state *state,
1945 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02001946{
1947 int rc;
1948
1949 /* Determine the type of swap operation being resumed from the
1950 * `swap-type` trailer field.
1951 */
David Vinczecea8b592019-10-29 16:09:51 +01001952 rc = boot_swap_image(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02001953 assert(rc == 0);
1954
David Vinczecea8b592019-10-29 16:09:51 +01001955 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02001956
1957 /* The following states need image_ok be explicitly set after the
1958 * swap was finished to avoid a new revert.
1959 */
1960 if (bs->swap_type == BOOT_SWAP_TYPE_REVERT ||
1961 bs->swap_type == BOOT_SWAP_TYPE_PERM) {
David Vinczecea8b592019-10-29 16:09:51 +01001962 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001963 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001964 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001965 }
1966 }
1967
David Vinczecea8b592019-10-29 16:09:51 +01001968 if (BOOT_IS_UPGRADE(bs->swap_type)) {
1969 rc = boot_set_copy_done(BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02001970 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01001971 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
David Vincze7384ee72019-07-23 17:00:42 +02001972 }
1973 }
1974
David Vinczecea8b592019-10-29 16:09:51 +01001975 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02001976 BOOT_LOG_ERR("panic!");
1977 assert(0);
1978
1979 /* Loop forever... */
1980 while (1) {}
1981 }
1982
1983 return rc;
1984}
1985#endif /* !MCUBOOT_OVERWRITE_ONLY */
1986
1987#if (BOOT_IMAGE_NUMBER > 1)
1988/**
1989 * Review the validity of previously determined swap types of other images.
1990 *
1991 * @param aborted_swap The current image upgrade is a
1992 * partial/aborted swap.
1993 */
1994static void
David Vinczecea8b592019-10-29 16:09:51 +01001995boot_review_image_swap_types(struct boot_loader_state *state,
1996 bool aborted_swap)
David Vincze7384ee72019-07-23 17:00:42 +02001997{
1998 /* In that case if we rebooted in the middle of an image upgrade process, we
1999 * must review the validity of swap types, that were previously determined
2000 * for other images. The image_ok flag had not been set before the reboot
2001 * for any of the updated images (only the copy_done flag) and thus falsely
2002 * the REVERT swap type has been determined for the previous images that had
2003 * been updated before the reboot.
2004 *
2005 * There are two separate scenarios that we have to deal with:
2006 *
2007 * 1. The reboot has happened during swapping an image:
2008 * The current image upgrade has been determined as a
2009 * partial/aborted swap.
2010 * 2. The reboot has happened between two separate image upgrades:
2011 * In this scenario we must check the swap type of the current image.
2012 * In those cases if it is NONE or REVERT we cannot certainly determine
2013 * the fact of a reboot. In a consistent state images must move in the
2014 * same direction or stay in place, e.g. in practice REVERT and TEST
2015 * swap types cannot be present at the same time. If the swap type of
2016 * the current image is either TEST, PERM or FAIL we must review the
2017 * already determined swap types of other images and set each false
2018 * REVERT swap types to NONE (these images had been successfully
2019 * updated before the system rebooted between two separate image
2020 * upgrades).
2021 */
2022
David Vinczecea8b592019-10-29 16:09:51 +01002023 if (BOOT_CURR_IMG(state) == 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002024 /* Nothing to do */
2025 return;
2026 }
2027
2028 if (!aborted_swap) {
David Vinczecea8b592019-10-29 16:09:51 +01002029 if ((BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) ||
2030 (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_REVERT)) {
David Vincze7384ee72019-07-23 17:00:42 +02002031 /* Nothing to do */
2032 return;
2033 }
2034 }
2035
David Vinczecea8b592019-10-29 16:09:51 +01002036 for (uint8_t i = 0; i < BOOT_CURR_IMG(state); i++) {
2037 if (state->swap_type[i] == BOOT_SWAP_TYPE_REVERT) {
2038 state->swap_type[i] = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002039 }
2040 }
2041}
2042#endif
2043
2044/**
2045 * Prepare image to be updated if required.
2046 *
2047 * Prepare image to be updated if required with completing an image swap
2048 * operation if one was aborted and/or determining the type of the
2049 * swap operation. In case of any error set the swap type to NONE.
2050 *
David Vinczecea8b592019-10-29 16:09:51 +01002051 * @param state Boot loader status information.
David Vincze7384ee72019-07-23 17:00:42 +02002052 * @param bs Pointer where the read and possibly updated
2053 * boot status can be written to.
2054 */
2055static void
David Vinczecea8b592019-10-29 16:09:51 +01002056boot_prepare_image_for_update(struct boot_loader_state *state,
2057 struct boot_status *bs)
David Vincze7384ee72019-07-23 17:00:42 +02002058{
2059 int rc;
2060
2061 /* Determine the sector layout of the image slots and scratch area. */
David Vinczecea8b592019-10-29 16:09:51 +01002062 rc = boot_read_sectors(state);
David Vincze7384ee72019-07-23 17:00:42 +02002063 if (rc != 0) {
2064 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
2065 " - too small?", BOOT_MAX_IMG_SECTORS);
2066 /* Unable to determine sector layout, continue with next image
2067 * if there is one.
2068 */
David Vinczecea8b592019-10-29 16:09:51 +01002069 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002070 return;
2071 }
2072
2073 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002074 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002075 if (rc != 0) {
2076 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002077 BOOT_LOG_WRN("Failed reading image headers; Image=%u",
2078 BOOT_CURR_IMG(state));
2079 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002080 return;
2081 }
2082
2083 /* If the current image's slots aren't compatible, no swap is possible.
2084 * Just boot into primary slot.
2085 */
David Vinczecea8b592019-10-29 16:09:51 +01002086 if (boot_slots_compatible(state)) {
2087 rc = boot_read_status(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002088 if (rc != 0) {
2089 BOOT_LOG_WRN("Failed reading boot status; Image=%u",
David Vinczecea8b592019-10-29 16:09:51 +01002090 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002091 /* Continue with next image if there is one. */
David Vinczecea8b592019-10-29 16:09:51 +01002092 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002093 return;
2094 }
2095
2096 /* Determine if we rebooted in the middle of an image swap
2097 * operation. If a partial swap was detected, complete it.
2098 */
2099 if (bs->idx != BOOT_STATUS_IDX_0 || bs->state != BOOT_STATUS_STATE_0) {
2100
2101#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002102 boot_review_image_swap_types(state, true);
David Vincze7384ee72019-07-23 17:00:42 +02002103#endif
2104
2105#ifdef MCUBOOT_OVERWRITE_ONLY
2106 /* Should never arrive here, overwrite-only mode has
2107 * no swap state.
2108 */
2109 assert(0);
2110#else
2111 /* Determine the type of swap operation being resumed from the
2112 * `swap-type` trailer field.
2113 */
David Vinczecea8b592019-10-29 16:09:51 +01002114 rc = boot_complete_partial_swap(state, bs);
David Vincze7384ee72019-07-23 17:00:42 +02002115 assert(rc == 0);
2116#endif
2117 /* Attempt to read an image header from each slot. Ensure that
2118 * image headers in slots are aligned with headers in boot_data.
2119 */
David Vinczecea8b592019-10-29 16:09:51 +01002120 rc = boot_read_image_headers(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002121 assert(rc == 0);
2122
2123 /* Swap has finished set to NONE */
David Vinczecea8b592019-10-29 16:09:51 +01002124 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002125 } else {
2126 /* There was no partial swap, determine swap type. */
2127 if (bs->swap_type == BOOT_SWAP_TYPE_NONE) {
David Vinczecea8b592019-10-29 16:09:51 +01002128 BOOT_SWAP_TYPE(state) = boot_validated_swap_type(state, bs);
2129 } else if (boot_validate_slot(state,
2130 BOOT_SECONDARY_SLOT, bs) != 0) {
2131 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_FAIL;
David Vincze7384ee72019-07-23 17:00:42 +02002132 } else {
David Vinczecea8b592019-10-29 16:09:51 +01002133 BOOT_SWAP_TYPE(state) = bs->swap_type;
David Vincze7384ee72019-07-23 17:00:42 +02002134 }
2135
2136#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002137 boot_review_image_swap_types(state, false);
David Vincze7384ee72019-07-23 17:00:42 +02002138#endif
2139 }
2140 } else {
2141 /* In that case if slots are not compatible. */
David Vinczecea8b592019-10-29 16:09:51 +01002142 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_NONE;
David Vincze7384ee72019-07-23 17:00:42 +02002143 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002144}
2145
2146/**
2147 * Prepares the booting process. This function moves images around in flash as
2148 * appropriate, and tells you what address to boot from.
2149 *
David Vinczecea8b592019-10-29 16:09:51 +01002150 * @param state Boot loader status information.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002151 * @param rsp On success, indicates how booting should occur.
2152 *
2153 * @return 0 on success; nonzero on failure.
2154 */
2155int
David Vinczecea8b592019-10-29 16:09:51 +01002156context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Banf70ef8c2017-12-19 15:35:09 +00002157{
Tamas Banf70ef8c2017-12-19 15:35:09 +00002158 size_t slot;
David Vincze7384ee72019-07-23 17:00:42 +02002159 struct boot_status bs;
Tamas Ban4e5ed8d2019-09-17 09:31:11 +01002160 int rc = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002161 int fa_id;
David Vinczecea8b592019-10-29 16:09:51 +01002162 int image_index;
2163 bool has_upgrade;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002164
2165 /* The array of slot sectors are defined here (as opposed to file scope) so
2166 * that they don't get allocated for non-boot-loader apps. This is
2167 * necessary because the gcc option "-fdata-sections" doesn't seem to have
2168 * any effect in older gcc versions (e.g., 4.8.4).
2169 */
David Vincze7384ee72019-07-23 17:00:42 +02002170 static boot_sector_t
2171 primary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
2172 static boot_sector_t
2173 secondary_slot_sectors[BOOT_IMAGE_NUMBER][BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02002174 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Banf70ef8c2017-12-19 15:35:09 +00002175
David Vincze7384ee72019-07-23 17:00:42 +02002176 /* Iterate over all the images. By the end of the loop the swap type has
2177 * to be determined for each image and all aborted swaps have to be
2178 * completed.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002179 */
David Vinczecea8b592019-10-29 16:09:51 +01002180 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2181
2182 image_index = BOOT_CURR_IMG(state);
2183
2184 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors =
2185 primary_slot_sectors[image_index];
2186 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors =
2187 secondary_slot_sectors[image_index];
2188 state->scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002189
David Vincze7384ee72019-07-23 17:00:42 +02002190 /* Open primary and secondary image areas for the duration
2191 * of this call.
Tamas Banf70ef8c2017-12-19 15:35:09 +00002192 */
David Vincze7384ee72019-07-23 17:00:42 +02002193 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002194 fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
2195 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
David Vincze7384ee72019-07-23 17:00:42 +02002196 assert(rc == 0);
2197 }
2198 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
David Vinczecea8b592019-10-29 16:09:51 +01002199 &BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002200 assert(rc == 0);
2201
2202 /* Determine swap type and complete swap if it has been aborted. */
David Vinczecea8b592019-10-29 16:09:51 +01002203 boot_prepare_image_for_update(state, &bs);
2204
2205 if (BOOT_IS_UPGRADE(BOOT_SWAP_TYPE(state))) {
2206 has_upgrade = true;
2207 }
David Vincze7384ee72019-07-23 17:00:42 +02002208 }
2209
David Vinczebb6e3b62019-07-31 14:24:05 +02002210#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002211 if (has_upgrade) {
2212 /* Iterate over all the images and verify whether the image dependencies
2213 * are all satisfied and update swap type if necessary.
2214 */
2215 rc = boot_verify_dependencies(state);
2216 if (rc == BOOT_EBADVERSION) {
2217 /*
2218 * It was impossible to upgrade because the expected dependency
2219 * version was not available. Here we already changed the swap_type
2220 * so that instead of asserting the bootloader, we continue and no
2221 * upgrade is performed.
2222 */
2223 rc = 0;
2224 }
2225 }
David Vinczebb6e3b62019-07-31 14:24:05 +02002226#endif
2227
David Vincze7384ee72019-07-23 17:00:42 +02002228 /* Iterate over all the images. At this point there are no aborted swaps
2229 * and the swap types are determined for each image. By the end of the loop
2230 * all required update operations will have been finished.
2231 */
David Vinczecea8b592019-10-29 16:09:51 +01002232 IMAGES_ITER(BOOT_CURR_IMG(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002233
2234#if (BOOT_IMAGE_NUMBER > 1)
2235 /* Indicate that swap is not aborted */
2236 memset(&bs, 0, sizeof bs);
2237 bs.idx = BOOT_STATUS_IDX_0;
2238 bs.state = BOOT_STATUS_STATE_0;
2239#endif /* (BOOT_IMAGE_NUMBER > 1) */
2240
2241 /* Set the previously determined swap type */
David Vinczecea8b592019-10-29 16:09:51 +01002242 bs.swap_type = BOOT_SWAP_TYPE(state);
David Vincze7384ee72019-07-23 17:00:42 +02002243
David Vinczecea8b592019-10-29 16:09:51 +01002244 switch (BOOT_SWAP_TYPE(state)) {
David Vincze7384ee72019-07-23 17:00:42 +02002245 case BOOT_SWAP_TYPE_NONE:
2246 break;
2247
2248 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
2249 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
2250 case BOOT_SWAP_TYPE_REVERT:
David Vinczecea8b592019-10-29 16:09:51 +01002251 rc = boot_perform_update(state, &bs);
David Vincze7384ee72019-07-23 17:00:42 +02002252 assert(rc == 0);
2253 break;
2254
2255 case BOOT_SWAP_TYPE_FAIL:
2256 /* The image in secondary slot was invalid and is now erased. Ensure
2257 * we don't try to boot into it again on the next reboot. Do this by
2258 * pretending we just reverted back to primary slot.
2259 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00002260#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze7384ee72019-07-23 17:00:42 +02002261 /* image_ok needs to be explicitly set to avoid a new revert. */
David Vinczecea8b592019-10-29 16:09:51 +01002262 rc = boot_set_image_ok(BOOT_CURR_IMG(state));
Tamas Banf70ef8c2017-12-19 15:35:09 +00002263 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002264 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002265 }
2266#endif /* !MCUBOOT_OVERWRITE_ONLY */
David Vincze7384ee72019-07-23 17:00:42 +02002267 break;
2268
2269 default:
David Vinczecea8b592019-10-29 16:09:51 +01002270 BOOT_SWAP_TYPE(state) = BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +00002271 }
David Vincze7384ee72019-07-23 17:00:42 +02002272
David Vinczecea8b592019-10-29 16:09:51 +01002273 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_PANIC) {
David Vincze7384ee72019-07-23 17:00:42 +02002274 BOOT_LOG_ERR("panic!");
2275 assert(0);
2276
2277 /* Loop forever... */
2278 while (1) {}
2279 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002280 }
2281
David Vincze7384ee72019-07-23 17:00:42 +02002282 /* Iterate over all the images. At this point all required update operations
2283 * have finished. By the end of the loop each image in the primary slot will
2284 * have been re-validated.
2285 */
David Vinczecea8b592019-10-29 16:09:51 +01002286 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2287 if (BOOT_SWAP_TYPE(state) != BOOT_SWAP_TYPE_NONE) {
David Vincze7384ee72019-07-23 17:00:42 +02002288 /* Attempt to read an image header from each slot. Ensure that image
2289 * headers in slots are aligned with headers in boot_data.
David Vincze060968d2019-05-23 01:13:14 +02002290 */
David Vinczecea8b592019-10-29 16:09:51 +01002291 rc = boot_read_image_headers(state, false);
David Vincze060968d2019-05-23 01:13:14 +02002292 if (rc != 0) {
David Vincze7384ee72019-07-23 17:00:42 +02002293 goto out;
2294 }
2295 /* Since headers were reloaded, it can be assumed we just performed
2296 * a swap or overwrite. Now the header info that should be used to
2297 * provide the data for the bootstrap, which previously was at
2298 * secondary slot, was updated to primary slot.
2299 */
2300 }
2301
2302#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vinczecea8b592019-10-29 16:09:51 +01002303 rc = boot_validate_slot(state, BOOT_PRIMARY_SLOT, NULL);
David Vincze7384ee72019-07-23 17:00:42 +02002304 if (rc != 0) {
2305 rc = BOOT_EBADIMAGE;
2306 goto out;
2307 }
2308#else
2309 /* Even if we're not re-validating the primary slot, we could be booting
2310 * onto an empty flash chip. At least do a basic sanity check that
2311 * the magic number on the image is OK.
2312 */
David Vinczecea8b592019-10-29 16:09:51 +01002313 if (!BOOT_IMG_HDR_IS_VALID(state, BOOT_PRIMARY_SLOT)) {
2314 BOOT_LOG_ERR("bad image magic 0x%lx; Image=%u", (unsigned long)
2315 &boot_img_hdr(state, BOOT_PRIMARY_SLOT)->ih_magic,
2316 BOOT_CURR_IMG(state));
David Vincze7384ee72019-07-23 17:00:42 +02002317 rc = BOOT_EBADIMAGE;
2318 goto out;
2319 }
2320#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
2321
2322 /* Update the stored security counter with the active image's security
2323 * counter value. It will be updated only if the new security counter is
2324 * greater than the stored value.
2325 *
2326 * In case of a successful image swapping when the swap type is TEST the
2327 * security counter can be increased only after a reset, when the swap
2328 * type is NONE and the image has marked itself "OK" (the image_ok flag
2329 * has been set). This way a "revert" swap can be performed if it's
2330 * necessary.
2331 */
David Vinczecea8b592019-10-29 16:09:51 +01002332 if (BOOT_SWAP_TYPE(state) == BOOT_SWAP_TYPE_NONE) {
2333 rc = boot_update_security_counter(
2334 BOOT_CURR_IMG(state),
2335 BOOT_PRIMARY_SLOT,
2336 boot_img_hdr(state, BOOT_PRIMARY_SLOT));
David Vincze7384ee72019-07-23 17:00:42 +02002337 if (rc != 0) {
2338 BOOT_LOG_ERR("Security counter update failed after image "
2339 "validation.");
David Vincze060968d2019-05-23 01:13:14 +02002340 goto out;
2341 }
2342 }
2343
David Vincze7384ee72019-07-23 17:00:42 +02002344 /* Save boot status to shared memory area */
2345#if (BOOT_IMAGE_NUMBER > 1)
David Vinczecea8b592019-10-29 16:09:51 +01002346 rc = boot_save_boot_status((BOOT_CURR_IMG(state) == 0) ?
2347 SW_SPE : SW_NSPE,
2348 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2349 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002350 );
David Vincze8bdfc2d2019-03-18 15:49:23 +01002351#else
David Vincze7384ee72019-07-23 17:00:42 +02002352 rc = boot_save_boot_status(SW_S_NS,
David Vinczecea8b592019-10-29 16:09:51 +01002353 boot_img_hdr(state, BOOT_PRIMARY_SLOT),
2354 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)
David Vincze7384ee72019-07-23 17:00:42 +02002355 );
2356#endif
2357 if (rc) {
2358 BOOT_LOG_ERR("Failed to add Image %u data to shared area",
David Vinczecea8b592019-10-29 16:09:51 +01002359 BOOT_CURR_IMG(state));
David Vincze060968d2019-05-23 01:13:14 +02002360 }
2361 }
2362
David Vinczecea8b592019-10-29 16:09:51 +01002363#if (BOOT_IMAGE_NUMBER > 1)
David Vincze7384ee72019-07-23 17:00:42 +02002364 /* Always boot from the primary slot of Image 0. */
David Vinczecea8b592019-10-29 16:09:51 +01002365 BOOT_CURR_IMG(state) = 0;
2366#endif
Tamas Ban0e8ab302019-01-17 11:45:31 +00002367
David Vinczecea8b592019-10-29 16:09:51 +01002368 rsp->br_flash_dev_id =
2369 BOOT_IMG_AREA(state, BOOT_PRIMARY_SLOT)->fa_device_id;
2370 rsp->br_image_off =
2371 boot_img_slot_off(state, BOOT_PRIMARY_SLOT);
2372 rsp->br_hdr =
2373 boot_img_hdr(state, BOOT_PRIMARY_SLOT);
2374
2375out:
2376 IMAGES_ITER(BOOT_CURR_IMG(state)) {
2377 flash_area_close(BOOT_SCRATCH_AREA(state));
David Vincze7384ee72019-07-23 17:00:42 +02002378 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002379 flash_area_close(BOOT_IMG_AREA(state,
David Vincze7384ee72019-07-23 17:00:42 +02002380 BOOT_NUM_SLOTS - 1 - slot));
2381 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00002382 }
2383 return rc;
2384}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002385
Oliver Swedef9982442018-08-24 18:37:44 +01002386#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002387
David Vinczedcba70b2019-05-28 12:02:52 +02002388#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
2389 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
2390 (area), \
2391 (hdr)->ih_ver.iv_major, \
2392 (hdr)->ih_ver.iv_minor, \
2393 (hdr)->ih_ver.iv_revision, \
2394 (hdr)->ih_ver.iv_build_num, \
2395 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
2396 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
2397 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002398 (state)->image_ok)
2399
2400struct image_slot_version {
2401 uint64_t version;
2402 uint32_t slot_number;
2403};
2404
2405/**
2406 * Extract the version number from the image header. This function must be
2407 * ported if version number format has changed in the image header.
2408 *
2409 * @param hdr Pointer to an image header structure
2410 *
Oliver Swedef9982442018-08-24 18:37:44 +01002411 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002412 */
2413static uint64_t
2414boot_get_version_number(struct image_header *hdr)
2415{
Oliver Swedef9982442018-08-24 18:37:44 +01002416 uint64_t version = 0;
2417 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
2418 + IMAGE_VER_REVISION_LENGTH
2419 + IMAGE_VER_BUILD_NUM_LENGTH);
2420 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
2421 + IMAGE_VER_BUILD_NUM_LENGTH);
2422 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
2423 version |= hdr->ih_ver.iv_build_num;
2424 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002425}
2426
2427/**
2428 * Comparator function for `qsort` to compare version numbers. This function
2429 * must be ported if version number format has changed in the image header.
2430 *
2431 * @param ver1 Pointer to an array element which holds the version number
2432 * @param ver2 Pointer to another array element which holds the version
2433 * number
2434 *
2435 * @return if version1 > version2 -1
2436 * if version1 == version2 0
2437 * if version1 < version2 1
2438 */
2439static int
2440boot_compare_version_numbers(const void *ver1, const void *ver2)
2441{
2442 if (((struct image_slot_version *)ver1)->version <
2443 ((struct image_slot_version *)ver2)->version) {
2444 return 1;
2445 }
2446
2447 if (((struct image_slot_version *)ver1)->version ==
2448 ((struct image_slot_version *)ver2)->version) {
2449 return 0;
2450 }
2451
2452 return -1;
2453}
2454
2455/**
2456 * Sort the available images based on the version number and puts them in
2457 * a list.
2458 *
David Vinczecea8b592019-10-29 16:09:51 +01002459 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002460 * @param boot_sequence A pointer to an array, whose aim is to carry
2461 * the boot order of candidate images.
2462 * @param slot_cnt The number of flash areas, which can contains firmware
2463 * images.
2464 *
2465 * @return The number of valid images.
2466 */
2467uint32_t
David Vinczecea8b592019-10-29 16:09:51 +01002468boot_get_boot_sequence(struct boot_loader_state *state,
2469 uint32_t *boot_sequence, uint32_t slot_cnt)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002470{
2471 struct boot_swap_state slot_state;
2472 struct image_header *hdr;
2473 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
2474 uint32_t image_cnt = 0;
2475 uint32_t slot;
2476 int32_t rc;
2477 int32_t fa_id;
2478
2479 for (slot = 0; slot < slot_cnt; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002480 hdr = boot_img_hdr(state, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002481 fa_id = flash_area_id_from_image_slot(slot);
2482 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
2483 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02002484 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
2485 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002486 continue;
2487 }
2488
David Vinczecea8b592019-10-29 16:09:51 +01002489 if (BOOT_IMG_HDR_IS_VALID(state, slot)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002490 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02002491 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002492 /* Valid cases:
2493 * - Test mode: magic is OK in image trailer
2494 * - Permanent mode: image_ok flag has previously set
2495 */
2496 image_versions[slot].slot_number = slot;
2497 image_versions[slot].version = boot_get_version_number(hdr);
2498 image_cnt++;
2499 }
2500
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002501 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
2502 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02002503 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002504 }
2505 }
2506
2507 /* Sort the images based on version number */
2508 qsort(&image_versions[0],
2509 slot_cnt,
2510 sizeof(struct image_slot_version),
2511 boot_compare_version_numbers);
2512
2513 /* Copy the calculated boot sequence to boot_sequence array */
2514 for (slot = 0; slot < slot_cnt; slot++) {
2515 boot_sequence[slot] = image_versions[slot].slot_number;
2516 }
2517
2518 return image_cnt;
2519}
2520
Oliver Swedef9982442018-08-24 18:37:44 +01002521#ifdef MCUBOOT_RAM_LOADING
Raef Colesaf082382019-10-01 11:10:33 +01002522
2523/**
2524 * Verifies that the image in a slot lies within the predefined bounds that are
2525 * allowed to be used by executable images.
2526 *
2527 * @param img_dst The address to which the image is going to be copied.
2528 *
2529 * @param img_sz The size of the image.
2530 *
2531 * @return 0 on success; nonzero on failure.
2532 */
2533static int
2534boot_verify_ram_loading_address(uint32_t img_dst, uint32_t img_sz)
2535{
David Vincze0dc41d22019-10-28 14:56:29 +01002536 uint32_t img_end_addr;
2537
Raef Colesaf082382019-10-01 11:10:33 +01002538 if (img_dst < IMAGE_EXECUTABLE_RAM_START) {
2539 return BOOT_EBADIMAGE;
2540 }
2541
David Vincze0dc41d22019-10-28 14:56:29 +01002542 if (!boot_u32_safe_add(&img_end_addr, img_dst, img_sz)) {
Raef Colesaf082382019-10-01 11:10:33 +01002543 return BOOT_EBADIMAGE;
2544 }
2545
David Vincze0dc41d22019-10-28 14:56:29 +01002546 if (img_end_addr > (IMAGE_EXECUTABLE_RAM_START +
2547 IMAGE_EXECUTABLE_RAM_SIZE)) {
Raef Colesaf082382019-10-01 11:10:33 +01002548 return BOOT_EBADIMAGE;
2549 }
2550
2551 return 0;
2552}
2553
Oliver Swedef9982442018-08-24 18:37:44 +01002554/**
2555 * Copies an image from a slot in the flash to an SRAM address, where the load
2556 * address has already been inserted into the image header by this point and is
2557 * extracted from it within this method. The copying is done sector-by-sector.
2558 *
David Vinczecea8b592019-10-29 16:09:51 +01002559 * @param state Boot loader status information.
Oliver Swedef9982442018-08-24 18:37:44 +01002560 * @param slot The flash slot of the image to be copied to SRAM.
2561 *
2562 * @param hdr Pointer to the image header structure of the image
Raef Colesaf082382019-10-01 11:10:33 +01002563 *
2564 * @param img_dst The address at which the image needs to be copied to
2565 * SRAM.
2566 *
2567 * @param img_sz The size of the image that needs to be copied to SRAM.
Oliver Swedef9982442018-08-24 18:37:44 +01002568 *
2569 * @return 0 on success; nonzero on failure.
2570 */
2571static int
David Vinczecea8b592019-10-29 16:09:51 +01002572boot_copy_image_to_sram(struct boot_loader_state *state, int slot,
2573 struct image_header *hdr,
Raef Colesaf082382019-10-01 11:10:33 +01002574 uint32_t img_dst, uint32_t img_sz)
Oliver Swedef9982442018-08-24 18:37:44 +01002575{
2576 int rc;
2577 uint32_t sect_sz;
2578 uint32_t sect = 0;
2579 uint32_t bytes_copied = 0;
2580 const struct flash_area *fap_src = NULL;
Oliver Swedef9982442018-08-24 18:37:44 +01002581
Raef Colesaf082382019-10-01 11:10:33 +01002582 if (img_dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01002583 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01002584 "- the load address must be aligned with 4 bytes due to SRAM "
Raef Colesaf082382019-10-01 11:10:33 +01002585 "restrictions", img_dst);
Oliver Swedef9982442018-08-24 18:37:44 +01002586 return BOOT_EBADARGS;
2587 }
2588
2589 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
2590 if (rc != 0) {
2591 return BOOT_EFLASH;
2592 }
2593
Oliver Swedef9982442018-08-24 18:37:44 +01002594 while (bytes_copied < img_sz) {
David Vinczecea8b592019-10-29 16:09:51 +01002595 sect_sz = boot_img_sector_size(state, slot, sect);
Oliver Swedef9982442018-08-24 18:37:44 +01002596 /*
2597 * Direct copy from where the image sector resides in flash to its new
2598 * location in SRAM
2599 */
2600 rc = flash_area_read(fap_src,
2601 bytes_copied,
Raef Colesaf082382019-10-01 11:10:33 +01002602 (void *)(img_dst + bytes_copied),
Oliver Swedef9982442018-08-24 18:37:44 +01002603 sect_sz);
2604 if (rc != 0) {
2605 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
2606 break;
2607 } else {
2608 bytes_copied += sect_sz;
2609 }
2610 sect++;
2611 }
2612
2613 if (fap_src) {
2614 flash_area_close(fap_src);
2615 }
2616 return rc;
2617}
Raef Coles27a61452019-09-25 15:32:25 +01002618
2619/**
2620 * Removes an image from SRAM, by overwriting it with zeros.
2621 *
Raef Colesaf082382019-10-01 11:10:33 +01002622 * @param img_dst The address of the image that needs to be removed from
2623 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002624 *
Raef Colesaf082382019-10-01 11:10:33 +01002625 * @param img_sz The size of the image that needs to be removed from
2626 * SRAM.
Raef Coles27a61452019-09-25 15:32:25 +01002627 *
2628 * @return 0 on success; nonzero on failure.
2629 */
2630static int
Raef Colesaf082382019-10-01 11:10:33 +01002631boot_remove_image_from_sram(uint32_t img_dst, uint32_t img_sz)
Raef Coles27a61452019-09-25 15:32:25 +01002632{
Raef Colesaf082382019-10-01 11:10:33 +01002633 BOOT_LOG_INF("Removing image from SRAM at address 0x%x", img_dst);
2634 memset((void*)img_dst, 0, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002635
2636 return 0;
2637}
Oliver Swedef9982442018-08-24 18:37:44 +01002638#endif /* MCUBOOT_RAM_LOADING */
2639
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002640/**
2641 * Prepares the booting process. This function choose the newer image in flash
David Vinczecea8b592019-10-29 16:09:51 +01002642 * as appropriate, and tells you what address to boot from.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002643 *
David Vinczecea8b592019-10-29 16:09:51 +01002644 * @param state Boot loader status information.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002645 * @param rsp On success, indicates how booting should occur.
2646 *
2647 * @return 0 on success; nonzero on failure.
2648 */
2649int
David Vinczecea8b592019-10-29 16:09:51 +01002650context_boot_go(struct boot_loader_state *state, struct boot_rsp *rsp)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002651{
2652 size_t slot = 0;
2653 int32_t i;
2654 int rc;
2655 int fa_id;
2656 uint32_t boot_sequence[BOOT_NUM_SLOTS];
2657 uint32_t img_cnt;
Raef Coles27a61452019-09-25 15:32:25 +01002658 struct image_header *selected_image_header;
2659#ifdef MCUBOOT_RAM_LOADING
2660 int image_copied = 0;
Raef Colesaf082382019-10-01 11:10:33 +01002661 uint32_t img_dst = 0;
2662 uint32_t img_sz = 0;
Raef Coles27a61452019-09-25 15:32:25 +01002663#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002664
David Vincze8bdfc2d2019-03-18 15:49:23 +01002665 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
2666 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002667
David Vinczecea8b592019-10-29 16:09:51 +01002668 BOOT_IMG(state, BOOT_PRIMARY_SLOT).sectors = &primary_slot_sectors[0];
2669 BOOT_IMG(state, BOOT_SECONDARY_SLOT).sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002670
2671 /* Open boot_data image areas for the duration of this call. */
2672 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
2673 fa_id = flash_area_id_from_image_slot(i);
David Vinczecea8b592019-10-29 16:09:51 +01002674 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, i));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002675 assert(rc == 0);
2676 }
2677
2678 /* Determine the sector layout of the image slots. */
David Vinczecea8b592019-10-29 16:09:51 +01002679 rc = boot_read_sectors(state);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002680 if (rc != 0) {
David Vinczecea8b592019-10-29 16:09:51 +01002681 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - "
2682 "too small?", BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002683 goto out;
2684 }
2685
2686 /* Attempt to read an image header from each slot. */
David Vinczecea8b592019-10-29 16:09:51 +01002687 rc = boot_read_image_headers(state, false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002688 if (rc != 0) {
2689 goto out;
2690 }
2691
David Vinczecea8b592019-10-29 16:09:51 +01002692 img_cnt = boot_get_boot_sequence(state, boot_sequence, BOOT_NUM_SLOTS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002693 if (img_cnt) {
2694 /* Authenticate images */
2695 for (i = 0; i < img_cnt; i++) {
Raef Coles27a61452019-09-25 15:32:25 +01002696
2697 slot = boot_sequence[i];
David Vinczecea8b592019-10-29 16:09:51 +01002698 selected_image_header = boot_img_hdr(state, slot);
Raef Coles27a61452019-09-25 15:32:25 +01002699
2700#ifdef MCUBOOT_RAM_LOADING
2701 if (selected_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
Raef Colesaf082382019-10-01 11:10:33 +01002702
2703 img_dst = selected_image_header->ih_load_addr;
2704
David Vinczecea8b592019-10-29 16:09:51 +01002705 rc = boot_read_image_size(state, slot, &img_sz);
Raef Colesaf082382019-10-01 11:10:33 +01002706 if (rc != 0) {
2707 rc = BOOT_EFLASH;
2708 BOOT_LOG_INF("Could not load image headers from the image"
2709 "in the %s slot.",
2710 (slot == BOOT_PRIMARY_SLOT) ?
2711 "primary" : "secondary");
2712 continue;
2713 }
2714
2715 rc = boot_verify_ram_loading_address(img_dst, img_sz);
2716 if (rc != 0) {
2717 BOOT_LOG_INF("Could not copy image from the %s slot in "
2718 "the Flash to load address 0x%x in SRAM as"
2719 " the image would overlap memory outside"
2720 " the defined executable region.",
2721 (slot == BOOT_PRIMARY_SLOT) ?
2722 "primary" : "secondary",
2723 selected_image_header->ih_load_addr);
2724 continue;
2725 }
2726
Raef Coles27a61452019-09-25 15:32:25 +01002727 /* Copy image to the load address from where it
2728 * currently resides in flash
2729 */
David Vinczecea8b592019-10-29 16:09:51 +01002730 rc = boot_copy_image_to_sram(state, slot, selected_image_header,
Raef Colesaf082382019-10-01 11:10:33 +01002731 img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002732 if (rc != 0) {
2733 rc = BOOT_EBADIMAGE;
2734 BOOT_LOG_INF("Could not copy image from the %s slot in "
2735 "the Flash to load address 0x%x in SRAM, "
2736 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2737 "primary" : "secondary",
2738 selected_image_header->ih_load_addr);
2739 continue;
2740 } else {
2741 BOOT_LOG_INF("Image has been copied from the %s slot in "
2742 "the flash to SRAM address 0x%x",
2743 (slot == BOOT_PRIMARY_SLOT) ?
2744 "primary" : "secondary",
2745 selected_image_header->ih_load_addr);
2746 image_copied = 1;
2747 }
2748 } else {
2749 /* Only images that support IMAGE_F_RAM_LOAD are allowed if
2750 * MCUBOOT_RAM_LOADING is set.
2751 */
2752 rc = BOOT_EBADIMAGE;
2753 continue;
2754 }
2755#endif /* MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002756 rc = boot_validate_slot(state, slot, NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002757 if (rc == 0) {
Raef Coles27a61452019-09-25 15:32:25 +01002758 /* If a valid image is found then there is no reason to check
2759 * the rest of the images, as they were already ordered by
2760 * preference.
2761 */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002762 break;
2763 }
Raef Coles27a61452019-09-25 15:32:25 +01002764#ifdef MCUBOOT_RAM_LOADING
2765 else if (image_copied) {
2766 /* If an image is found to be invalid then it is removed from
2767 * RAM to prevent it being a shellcode vector.
2768 */
Raef Colesaf082382019-10-01 11:10:33 +01002769 boot_remove_image_from_sram(img_dst, img_sz);
Raef Coles27a61452019-09-25 15:32:25 +01002770 image_copied = 0;
2771 }
2772#endif /* MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002773 }
2774 if (rc) {
2775 /* If there was no valid image at all */
2776 rc = BOOT_EBADIMAGE;
2777 goto out;
2778 }
2779
David Vincze060968d2019-05-23 01:13:14 +02002780 /* Update the security counter with the newest image's security
2781 * counter value.
2782 */
David Vinczecea8b592019-10-29 16:09:51 +01002783 rc = boot_update_security_counter(BOOT_CURR_IMG(state), slot,
2784 selected_image_header);
David Vincze060968d2019-05-23 01:13:14 +02002785 if (rc != 0) {
2786 BOOT_LOG_ERR("Security counter update failed after image "
2787 "validation.");
2788 goto out;
2789 }
2790
Oliver Swedef9982442018-08-24 18:37:44 +01002791
David Vincze8a2a4e22019-05-24 10:14:23 +02002792#ifdef MCUBOOT_RAM_LOADING
Raef Coles27a61452019-09-25 15:32:25 +01002793 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
2794 selected_image_header->ih_load_addr);
2795#else
2796 BOOT_LOG_INF("Booting image from the %s slot",
2797 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
2798#endif /* MCUBOOT_RAM_LOADING */
Oliver Swedef9982442018-08-24 18:37:44 +01002799
Raef Coles27a61452019-09-25 15:32:25 +01002800 rsp->br_hdr = selected_image_header;
David Vinczecea8b592019-10-29 16:09:51 +01002801 rsp->br_image_off = boot_img_slot_off(state, slot);
2802 rsp->br_flash_dev_id = BOOT_IMG_AREA(state, slot)->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002803 } else {
2804 /* No candidate image available */
2805 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002806 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002807 }
2808
Tamas Ban0e8ab302019-01-17 11:45:31 +00002809 /* Save boot status to shared memory area */
2810 rc = boot_save_boot_status(SW_S_NS,
2811 rsp->br_hdr,
David Vinczecea8b592019-10-29 16:09:51 +01002812 BOOT_IMG_AREA(state, slot));
Tamas Ban0e8ab302019-01-17 11:45:31 +00002813 if (rc) {
2814 BOOT_LOG_ERR("Failed to add data to shared area");
2815 }
2816
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002817out:
2818 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
David Vinczecea8b592019-10-29 16:09:51 +01002819 flash_area_close(BOOT_IMG_AREA(state, BOOT_NUM_SLOTS - 1 - slot));
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002820 }
2821 return rc;
2822}
Oliver Swedef9982442018-08-24 18:37:44 +01002823#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
David Vinczecea8b592019-10-29 16:09:51 +01002824
2825int
2826boot_go(struct boot_rsp *rsp)
2827{
2828 return context_boot_go(&boot_data, rsp);
2829}