blob: 9fe012a41c3eb57ff71c1dda0f6a32150ecf6e86 [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
23 * Git SHA of the original version: 178be54bd6e5f035cc60e98205535682acd26e64
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"
Tamas Bana9de4a62018-09-18 08:09:45 +010042#include "bl2/include/tfm_boot_status.h"
43#include "bl2/include/boot_record.h"
David Vincze060968d2019-05-23 01:13:14 +020044#include "security_cnt.h"
Tamas Banf70ef8c2017-12-19 15:35:09 +000045
46#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
47#include "bootutil/bootutil_log.h"
48
Tamas Banf70ef8c2017-12-19 15:35:09 +000049static struct boot_loader_state boot_data;
50
Oliver Swedef9982442018-08-24 18:37:44 +010051#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze39e78552018-10-10 17:10:01 +020052
David Vincze8bdfc2d2019-03-18 15:49:23 +010053#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020054static int boot_status_fails = 0;
55#define BOOT_STATUS_ASSERT(x) \
56 do { \
57 if (!(x)) { \
58 boot_status_fails++; \
59 } \
60 } while (0)
61#else
62#define BOOT_STATUS_ASSERT(x) assert(x)
63#endif
64
Tamas Banf70ef8c2017-12-19 15:35:09 +000065struct boot_status_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010066 uint8_t bst_magic_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000067 uint8_t bst_magic_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +010068 uint8_t bst_copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000069 uint8_t bst_status_source;
70};
71
72/**
73 * This set of tables maps swap state contents to boot status location.
74 * When searching for a match, these tables must be iterated in order.
75 */
76static const struct boot_status_table boot_status_tables[] = {
77 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010078 /* | primary slot | scratch |
79 * ----------+--------------+--------------|
80 * magic | Good | Any |
81 * copy-done | Set | N/A |
82 * ----------+--------------+--------------'
83 * source: none |
84 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +000085 */
David Vincze8bdfc2d2019-03-18 15:49:23 +010086 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
87 .bst_magic_scratch = BOOT_MAGIC_ANY,
88 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
89 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Tamas Banf70ef8c2017-12-19 15:35:09 +000090 },
91
92 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010093 /* | primary slot | scratch |
94 * ----------+--------------+--------------|
95 * magic | Good | Any |
96 * copy-done | Unset | N/A |
97 * ----------+--------------+--------------'
98 * source: primary slot |
99 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000100 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100101 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
102 .bst_magic_scratch = BOOT_MAGIC_ANY,
103 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
104 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000105 },
106
107 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100108 /* | primary slot | scratch |
109 * ----------+--------------+--------------|
110 * magic | Any | Good |
111 * copy-done | Any | N/A |
112 * ----------+--------------+--------------'
113 * source: scratch |
114 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000115 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100116 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
117 .bst_magic_scratch = BOOT_MAGIC_GOOD,
118 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
119 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000120 },
121
122 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100123 /* | primary slot | scratch |
124 * ----------+--------------+--------------|
125 * magic | Unset | Any |
126 * copy-done | Unset | N/A |
127 * ----------+--------------+--------------|
128 * source: varies |
129 * ----------------------------------------+--------------------------+
Tamas Banf70ef8c2017-12-19 15:35:09 +0000130 * This represents one of two cases: |
131 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze8bdfc2d2019-03-18 15:49:23 +0100132 * o Mid-revert; status in the primary slot. |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000133 * -------------------------------------------------------------------'
134 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100135 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
136 .bst_magic_scratch = BOOT_MAGIC_ANY,
137 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
138 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000139 },
140};
141
142#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000143 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000144
145#define BOOT_LOG_SWAP_STATE(area, state) \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000146 BOOT_LOG_INF("%s: magic=%5s, copy_done=0x%x, image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000147 (area), \
148 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
149 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
150 "bad"), \
151 (state)->copy_done, \
152 (state)->image_ok)
Oliver Swedef9982442018-08-24 18:37:44 +0100153#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000154
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000155
156static int
157boot_read_image_header(int slot, struct image_header *out_hdr)
158{
159 const struct flash_area *fap = NULL;
160 int area_id;
161 int rc;
162
163 area_id = flash_area_id_from_image_slot(slot);
164 rc = flash_area_open(area_id, &fap);
165 if (rc != 0) {
166 rc = BOOT_EFLASH;
167 goto done;
168 }
169
170 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
171 if (rc != 0) {
172 rc = BOOT_EFLASH;
173 goto done;
174 }
175
176 rc = 0;
177
178done:
179 flash_area_close(fap);
180 return rc;
181}
182
183static int
David Vincze39e78552018-10-10 17:10:01 +0200184boot_read_image_headers(bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000185{
186 int rc;
187 int i;
188
189 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
190 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
191 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200192 /* If `require_all` is set, fail on any single fail, otherwise
193 * if at least the first slot's header was read successfully,
194 * then the boot loader can attempt a boot.
195 *
196 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000197 */
David Vincze39e78552018-10-10 17:10:01 +0200198 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000199 return 0;
200 } else {
201 return rc;
202 }
203 }
204 }
205
206 return 0;
207}
208
209static uint8_t
210boot_write_sz(void)
211{
212 const struct flash_area *fap;
213 uint8_t elem_sz;
214 uint8_t align;
215 int rc;
216
217 /* Figure out what size to write update status update as. The size depends
218 * on what the minimum write size is for scratch area, active image slot.
219 * We need to use the bigger of those 2 values.
220 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100221 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000222 assert(rc == 0);
223 elem_sz = flash_area_align(fap);
224 flash_area_close(fap);
225
226 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
227 assert(rc == 0);
228 align = flash_area_align(fap);
229 flash_area_close(fap);
230
231 if (align > elem_sz) {
232 elem_sz = align;
233 }
234
235 return elem_sz;
236}
237
238/**
239 * Determines the sector layout of both image slots and the scratch area.
240 * This information is necessary for calculating the number of bytes to erase
241 * and copy during an image swap. The information collected during this
242 * function is used to populate the boot_data global.
243 */
244static int
245boot_read_sectors(void)
246{
247 int rc;
248
David Vincze8bdfc2d2019-03-18 15:49:23 +0100249 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000250 if (rc != 0) {
251 return BOOT_EFLASH;
252 }
253
David Vincze8bdfc2d2019-03-18 15:49:23 +0100254 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000255 if (rc != 0) {
256 return BOOT_EFLASH;
257 }
258
259 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
260
261 return 0;
262}
263
David Vincze060968d2019-05-23 01:13:14 +0200264/**
265 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000266 */
267static int
Tamas Ban0e8ab302019-01-17 11:45:31 +0000268boot_image_check(struct image_header *hdr, const struct flash_area *fap)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000269{
270 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
271
272 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
Tamas Ban0e8ab302019-01-17 11:45:31 +0000273 NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000274 return BOOT_EBADIMAGE;
275 }
276 return 0;
277}
278
David Vincze39e78552018-10-10 17:10:01 +0200279static inline int
280boot_magic_is_erased(uint8_t erased_val, uint32_t magic)
281{
282 uint8_t i;
283 for (i = 0; i < sizeof(magic); i++) {
284 if (erased_val != *(((uint8_t *)&magic) + i)) {
285 return 0;
286 }
287 }
288 return 1;
289}
290
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000291static int
292boot_validate_slot(int slot)
293{
294 const struct flash_area *fap;
295 struct image_header *hdr;
296 int rc;
297
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000298 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
299 if (rc != 0) {
300 return BOOT_EFLASH;
301 }
302
David Vincze39e78552018-10-10 17:10:01 +0200303 hdr = boot_img_hdr(&boot_data, slot);
304 if (boot_magic_is_erased(flash_area_erased_val(fap), hdr->ih_magic) ||
305 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100306 /* No bootable image in slot; continue booting from the primary slot. */
David Vincze39e78552018-10-10 17:10:01 +0200307 return -1;
308 }
309
Tamas Bana9de4a62018-09-18 08:09:45 +0100310 if ((hdr->ih_magic != IMAGE_MAGIC ||
Tamas Ban0e8ab302019-01-17 11:45:31 +0000311 boot_image_check(hdr, fap) != 0)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000312 if (slot != 0) {
David Vincze26e8c8a2018-08-28 16:59:41 +0200313 rc = flash_area_erase(fap, 0, fap->fa_size);
314 if(rc != 0) {
315 flash_area_close(fap);
316 return BOOT_EFLASH;
317 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100318 /* Image in the secondary slot is invalid. Erase the image and
319 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000320 */
321 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100322 BOOT_LOG_ERR("Authentication failed! Image in the %s slot is not valid."
323 , (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze060968d2019-05-23 01:13:14 +0200324 flash_area_close(fap);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000325 return -1;
326 }
327
328 flash_area_close(fap);
329
David Vincze8bdfc2d2019-03-18 15:49:23 +0100330 /* Image in the secondary slot is valid. */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000331 return 0;
332}
333
David Vincze060968d2019-05-23 01:13:14 +0200334/**
335 * Updates the stored security counter value with the image's security counter
336 * value which resides in the given slot if it's greater than the stored value.
337 *
338 * @param slot Slot number of the image.
339 * @param hdr Pointer to the image header structure of the image that is
340 * currently stored in the given slot.
341 *
342 * @return 0 on success; nonzero on failure.
343 */
344static int
345boot_update_security_counter(int slot, struct image_header *hdr)
346{
347 const struct flash_area *fap = NULL;
348 uint32_t img_security_cnt;
349 int rc;
350
351 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
352 if (rc != 0) {
353 rc = BOOT_EFLASH;
354 goto done;
355 }
356
357 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
358 if (rc != 0) {
359 goto done;
360 }
361
362 rc = boot_nv_security_counter_update(0, img_security_cnt);
363 if (rc != 0) {
364 goto done;
365 }
366
367done:
368 flash_area_close(fap);
369 return rc;
370}
371
Oliver Swedef9982442018-08-24 18:37:44 +0100372#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
373/*
374 * Compute the total size of the given image. Includes the size of
375 * the TLVs.
376 */
377static int
378boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
379{
380 const struct flash_area *fap = NULL;
381 struct image_tlv_info info;
382 int area_id;
383 int rc;
384
385 area_id = flash_area_id_from_image_slot(slot);
386 rc = flash_area_open(area_id, &fap);
387 if (rc != 0) {
388 rc = BOOT_EFLASH;
389 goto done;
390 }
391
392 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
393 &info, sizeof(info));
394 if (rc != 0) {
395 rc = BOOT_EFLASH;
396 goto done;
397 }
398 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
399 rc = BOOT_EBADIMAGE;
400 goto done;
401 }
402 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
403 rc = 0;
404
405done:
406 flash_area_close(fap);
407 return rc;
408}
409#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
410
411#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000412/**
Tamas Ban581034a2017-12-19 19:54:37 +0000413 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000414 * status is necessary for completing a swap that was interrupted by a boot
415 * loader reset.
416 *
Tamas Ban581034a2017-12-19 19:54:37 +0000417 * @return BOOT_STATUS_SOURCE_[...] code indicating where
418 * status should be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000419 */
420static int
421boot_status_source(void)
422{
423 const struct boot_status_table *table;
424 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100425 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000426 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200427 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000428 uint8_t source;
429
David Vincze8bdfc2d2019-03-18 15:49:23 +0100430 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
431 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000432 assert(rc == 0);
433
434 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
435 assert(rc == 0);
436
David Vincze8bdfc2d2019-03-18 15:49:23 +0100437 BOOT_LOG_SWAP_STATE("Image 0", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000438 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
439
440 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
441 table = &boot_status_tables[i];
442
David Vincze8bdfc2d2019-03-18 15:49:23 +0100443 if ((table->bst_magic_primary_slot == BOOT_MAGIC_ANY ||
444 table->bst_magic_primary_slot == state_primary_slot.magic) &&
445 (table->bst_magic_scratch == BOOT_MAGIC_ANY ||
446 table->bst_magic_scratch == state_scratch.magic) &&
447 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
448 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
449 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000450 source = table->bst_status_source;
451 BOOT_LOG_INF("Boot source: %s",
452 source == BOOT_STATUS_SOURCE_NONE ? "none" :
453 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100454 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
455 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000456 return source;
457 }
458 }
459
460 BOOT_LOG_INF("Boot source: none");
461 return BOOT_STATUS_SOURCE_NONE;
462}
463
464/**
465 * Calculates the type of swap that just completed.
466 *
467 * This is used when a swap is interrupted by an external event. After
468 * finishing the swap operation determines what the initial request was.
469 */
470static int
471boot_previous_swap_type(void)
472{
473 int post_swap_type;
474
475 post_swap_type = boot_swap_type();
476
477 switch (post_swap_type) {
David Vincze39e78552018-10-10 17:10:01 +0200478 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
479 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
480 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000481 }
482
483 return BOOT_SWAP_TYPE_FAIL;
484}
485
Tamas Banf70ef8c2017-12-19 15:35:09 +0000486static int
Tamas Banf70ef8c2017-12-19 15:35:09 +0000487boot_slots_compatible(void)
488{
David Vincze8bdfc2d2019-03-18 15:49:23 +0100489 size_t num_sectors_0 = boot_img_num_sectors(&boot_data,
490 BOOT_PRIMARY_SLOT);
491 size_t num_sectors_1 = boot_img_num_sectors(&boot_data,
492 BOOT_SECONDARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000493 size_t size_0, size_1;
494 size_t i;
495
David Vincze39e78552018-10-10 17:10:01 +0200496 if (num_sectors_0 > BOOT_MAX_IMG_SECTORS || num_sectors_1 > BOOT_MAX_IMG_SECTORS) {
497 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000498 return 0;
499 }
David Vincze39e78552018-10-10 17:10:01 +0200500
501 /* Ensure both image slots have identical sector layouts. */
502 if (num_sectors_0 != num_sectors_1) {
503 BOOT_LOG_WRN("Cannot upgrade: number of sectors differ between slots");
504 return 0;
505 }
506
Tamas Banf70ef8c2017-12-19 15:35:09 +0000507 for (i = 0; i < num_sectors_0; i++) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100508 size_0 = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
509 size_1 = boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, i);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000510 if (size_0 != size_1) {
David Vincze39e78552018-10-10 17:10:01 +0200511 BOOT_LOG_WRN("Cannot upgrade: an incompatible sector was found");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000512 return 0;
513 }
514 }
515
516 return 1;
517}
518
Tamas Banf70ef8c2017-12-19 15:35:09 +0000519static uint32_t
520boot_status_internal_off(int idx, int state, int elem_sz)
521{
522 int idx_sz;
523
524 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
525
David Vincze39e78552018-10-10 17:10:01 +0200526 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
527 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000528}
529
530/**
531 * Reads the status of a partially-completed swap, if any. This is necessary
532 * to recover in case the boot lodaer was reset in the middle of a swap
533 * operation.
534 */
535static int
536boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
537{
538 uint32_t off;
539 uint8_t status;
540 int max_entries;
541 int found;
David Vincze39e78552018-10-10 17:10:01 +0200542 int found_idx;
543 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000544 int rc;
545 int i;
546
547 off = boot_status_off(fap);
548 max_entries = boot_status_entries(fap);
549
550 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200551 found_idx = 0;
552 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000553 for (i = 0; i < max_entries; i++) {
David Vincze39e78552018-10-10 17:10:01 +0200554 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
555 &status, 1);
556 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000557 return BOOT_EFLASH;
558 }
559
David Vincze39e78552018-10-10 17:10:01 +0200560 if (rc == 1) {
561 if (found && !found_idx) {
562 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000563 }
564 } else if (!found) {
565 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200566 } else if (found_idx) {
567 invalid = 1;
568 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000569 }
570 }
571
David Vincze39e78552018-10-10 17:10:01 +0200572 if (invalid) {
573 /* This means there was an error writing status on the last
574 * swap. Tell user and move on to validation!
575 */
576 BOOT_LOG_ERR("Detected inconsistent status!");
577
David Vincze8bdfc2d2019-03-18 15:49:23 +0100578#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
579 /* With validation of the primary slot disabled, there is no way
580 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200581 */
582 assert(0);
583#endif
584 }
585
Tamas Banf70ef8c2017-12-19 15:35:09 +0000586 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200587 if (!found_idx) {
588 found_idx = i;
589 }
590 found_idx--;
591 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
592 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000593 }
594
595 return 0;
596}
597
598/**
599 * Reads the boot status from the flash. The boot status contains
600 * the current state of an interrupted image copy operation. If the boot
601 * status is not present, or it indicates that previous copy finished,
602 * there is no operation in progress.
603 */
604static int
605boot_read_status(struct boot_status *bs)
606{
607 const struct flash_area *fap;
608 int status_loc;
609 int area_id;
610 int rc;
611
David Vincze39e78552018-10-10 17:10:01 +0200612 memset(bs, 0, sizeof *bs);
613 bs->idx = BOOT_STATUS_IDX_0;
614 bs->state = BOOT_STATUS_STATE_0;
615
616#ifdef MCUBOOT_OVERWRITE_ONLY
617 /* Overwrite-only doesn't make use of the swap status area. */
618 return 0;
619#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000620
621 status_loc = boot_status_source();
622 switch (status_loc) {
623 case BOOT_STATUS_SOURCE_NONE:
624 return 0;
625
626 case BOOT_STATUS_SOURCE_SCRATCH:
627 area_id = FLASH_AREA_IMAGE_SCRATCH;
628 break;
629
David Vincze8bdfc2d2019-03-18 15:49:23 +0100630 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
631 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000632 break;
633
634 default:
635 assert(0);
636 return BOOT_EBADARGS;
637 }
638
639 rc = flash_area_open(area_id, &fap);
640 if (rc != 0) {
641 return BOOT_EFLASH;
642 }
643
644 rc = boot_read_status_bytes(fap, bs);
645
646 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200647
Tamas Banf70ef8c2017-12-19 15:35:09 +0000648 return rc;
649}
650
651/**
652 * Writes the supplied boot status to the flash file system. The boot status
653 * contains the current state of an in-progress image copy operation.
654 *
655 * @param bs The boot status to write.
656 *
657 * @return 0 on success; nonzero on failure.
658 */
659int
660boot_write_status(struct boot_status *bs)
661{
Tamas Ban581034a2017-12-19 19:54:37 +0000662 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000663 uint32_t off;
664 int area_id;
665 int rc;
666 uint8_t buf[BOOT_MAX_ALIGN];
667 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200668 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000669
670 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100671 * the trailer. Since in the last step the primary slot is erased, the
672 * first two status writes go to the scratch which will be copied to
673 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000674 */
675
676 if (bs->use_scratch) {
677 /* Write to scratch. */
678 area_id = FLASH_AREA_IMAGE_SCRATCH;
679 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100680 /* Write to the primary slot. */
681 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000682 }
683
684 rc = flash_area_open(area_id, &fap);
685 if (rc != 0) {
686 rc = BOOT_EFLASH;
687 goto done;
688 }
689
690 off = boot_status_off(fap) +
691 boot_status_internal_off(bs->idx, bs->state,
692 BOOT_WRITE_SZ(&boot_data));
693
Tamas Banc3828852018-02-01 12:24:16 +0000694 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200695 erased_val = flash_area_erased_val(fap);
696 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000697 buf[0] = bs->state;
698
699 rc = flash_area_write(fap, off, buf, align);
700 if (rc != 0) {
701 rc = BOOT_EFLASH;
702 goto done;
703 }
704
705 rc = 0;
706
707done:
708 flash_area_close(fap);
709 return rc;
710}
711
Tamas Banf70ef8c2017-12-19 15:35:09 +0000712/**
713 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100714 * that a swap operation is required, the image in the secondary slot is checked
715 * for validity. If the image in the secondary slot is invalid, it is erased,
716 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000717 *
718 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
719 */
720static int
721boot_validated_swap_type(void)
722{
723 int swap_type;
724
725 swap_type = boot_swap_type();
726 switch (swap_type) {
727 case BOOT_SWAP_TYPE_TEST:
728 case BOOT_SWAP_TYPE_PERM:
729 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100730 /* Boot loader wants to switch to the secondary slot.
731 * Ensure image is valid.
732 */
733 if (boot_validate_slot(BOOT_SECONDARY_SLOT) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000734 swap_type = BOOT_SWAP_TYPE_FAIL;
735 }
736 }
737
738 return swap_type;
739}
740
741/**
742 * Calculates the number of sectors the scratch area can contain. A "last"
743 * source sector is specified because images are copied backwards in flash
744 * (final index to index number 0).
745 *
746 * @param last_sector_idx The index of the last source sector
747 * (inclusive).
748 * @param out_first_sector_idx The index of the first source sector
749 * (inclusive) gets written here.
750 *
751 * @return The number of bytes comprised by the
752 * [first-sector, last-sector] range.
753 */
754#ifndef MCUBOOT_OVERWRITE_ONLY
755static uint32_t
756boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
757{
758 size_t scratch_sz;
759 uint32_t new_sz;
760 uint32_t sz;
761 int i;
762
763 sz = 0;
764
765 scratch_sz = boot_scratch_area_size(&boot_data);
766 for (i = last_sector_idx; i >= 0; i--) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100767 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000768 if (new_sz > scratch_sz) {
769 break;
770 }
771 sz = new_sz;
772 }
773
774 /* i currently refers to a sector that doesn't fit or it is -1 because all
775 * sectors have been processed. In both cases, exclude sector i.
776 */
777 *out_first_sector_idx = i + 1;
778 return sz;
779}
780#endif /* !MCUBOOT_OVERWRITE_ONLY */
781
782/**
David Vinczef7641fa2018-09-04 18:29:46 +0200783 * Erases a region of flash.
784 *
785 * @param flash_area_idx The ID of the flash area containing the region
786 * to erase.
787 * @param off The offset within the flash area to start the
788 * erase.
789 * @param sz The number of bytes to erase.
790 *
791 * @return 0 on success; nonzero on failure.
792 */
793static int
794boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
795{
796 const struct flash_area *fap = NULL;
797 int rc;
798
799 rc = flash_area_open(flash_area_id, &fap);
800 if (rc != 0) {
801 rc = BOOT_EFLASH;
802 goto done;
803 }
804
805 rc = flash_area_erase(fap, off, sz);
806 if (rc != 0) {
807 rc = BOOT_EFLASH;
808 goto done;
809 }
810
811 rc = 0;
812
813done:
814 flash_area_close(fap);
815 return rc;
816}
817
818/**
Tamas Banf70ef8c2017-12-19 15:35:09 +0000819 * Copies the contents of one flash region to another. You must erase the
820 * destination region prior to calling this function.
821 *
822 * @param flash_area_id_src The ID of the source flash area.
823 * @param flash_area_id_dst The ID of the destination flash area.
824 * @param off_src The offset within the source flash area to
825 * copy from.
826 * @param off_dst The offset within the destination flash area to
827 * copy to.
828 * @param sz The number of bytes to copy.
829 *
830 * @return 0 on success; nonzero on failure.
831 */
832static int
833boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
834 uint32_t off_src, uint32_t off_dst, uint32_t sz)
835{
836 const struct flash_area *fap_src;
837 const struct flash_area *fap_dst;
838 uint32_t bytes_copied;
839 int chunk_sz;
840 int rc;
841
842 static uint8_t buf[1024];
843
844 fap_src = NULL;
845 fap_dst = NULL;
846
847 rc = flash_area_open(flash_area_id_src, &fap_src);
848 if (rc != 0) {
849 rc = BOOT_EFLASH;
850 goto done;
851 }
852
853 rc = flash_area_open(flash_area_id_dst, &fap_dst);
854 if (rc != 0) {
855 rc = BOOT_EFLASH;
856 goto done;
857 }
858
859 bytes_copied = 0;
860 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +0000861 if (sz - bytes_copied > sizeof(buf)) {
862 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000863 } else {
864 chunk_sz = sz - bytes_copied;
865 }
866
867 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
868 if (rc != 0) {
869 rc = BOOT_EFLASH;
870 goto done;
871 }
872
873 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
874 if (rc != 0) {
875 rc = BOOT_EFLASH;
876 goto done;
877 }
878
879 bytes_copied += chunk_sz;
880 }
881
882 rc = 0;
883
884done:
885 if (fap_src) {
886 flash_area_close(fap_src);
887 }
888 if (fap_dst) {
889 flash_area_close(fap_dst);
890 }
891 return rc;
892}
893
894#ifndef MCUBOOT_OVERWRITE_ONLY
895static inline int
896boot_status_init_by_id(int flash_area_id, const struct boot_status *bs)
897{
898 const struct flash_area *fap;
899 struct boot_swap_state swap_state;
900 int rc;
901
902 rc = flash_area_open(flash_area_id, &fap);
903 assert(rc == 0);
904
David Vincze8bdfc2d2019-03-18 15:49:23 +0100905 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000906 assert(rc == 0);
907
908 if (swap_state.image_ok == BOOT_FLAG_SET) {
909 rc = boot_write_image_ok(fap);
910 assert(rc == 0);
911 }
912
913 rc = boot_write_swap_size(fap, bs->swap_size);
914 assert(rc == 0);
915
916 rc = boot_write_magic(fap);
917 assert(rc == 0);
918
919 flash_area_close(fap);
920
921 return 0;
922}
David Vinczef7641fa2018-09-04 18:29:46 +0200923
924static int
925boot_erase_last_sector_by_id(int flash_area_id)
926{
927 uint8_t slot;
928 uint32_t last_sector;
929 int rc;
930
931 switch (flash_area_id) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100932 case FLASH_AREA_IMAGE_PRIMARY:
933 slot = BOOT_PRIMARY_SLOT;
David Vinczef7641fa2018-09-04 18:29:46 +0200934 break;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100935 case FLASH_AREA_IMAGE_SECONDARY:
936 slot = BOOT_SECONDARY_SLOT;
David Vinczef7641fa2018-09-04 18:29:46 +0200937 break;
938 default:
939 return BOOT_EFLASH;
940 }
941
942 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
943 rc = boot_erase_sector(flash_area_id,
944 boot_img_sector_off(&boot_data, slot, last_sector),
945 boot_img_sector_size(&boot_data, slot, last_sector));
946 assert(rc == 0);
947
948 return rc;
949}
950#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000951
Tamas Banf70ef8c2017-12-19 15:35:09 +0000952/**
953 * Swaps the contents of two flash regions within the two image slots.
954 *
955 * @param idx The index of the first sector in the range of
956 * sectors being swapped.
957 * @param sz The number of bytes to swap.
958 * @param bs The current boot status. This struct gets
959 * updated according to the outcome.
960 *
961 * @return 0 on success; nonzero on failure.
962 */
963#ifndef MCUBOOT_OVERWRITE_ONLY
964static void
965boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
966{
967 const struct flash_area *fap;
968 uint32_t copy_sz;
969 uint32_t trailer_sz;
970 uint32_t img_off;
971 uint32_t scratch_trailer_off;
972 struct boot_swap_state swap_state;
973 size_t last_sector;
974 int rc;
975
976 /* Calculate offset from start of image area. */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100977 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000978
979 copy_sz = sz;
980 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
981
982 /* sz in this function is always is always sized on a multiple of the
983 * sector size. The check against the start offset of the last sector
984 * is to determine if we're swapping the last sector. The last sector
985 * needs special handling because it's where the trailer lives. If we're
986 * copying it, we need to use scratch to write the trailer temporarily.
987 *
988 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
989 * controls if special handling is needed (swapping last sector).
990 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100991 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
992 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
993 last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000994 copy_sz -= trailer_sz;
995 }
996
David Vincze39e78552018-10-10 17:10:01 +0200997 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000998
David Vincze39e78552018-10-10 17:10:01 +0200999 if (bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001000 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
1001 assert(rc == 0);
1002
David Vincze8bdfc2d2019-03-18 15:49:23 +01001003 rc = boot_copy_sector(FLASH_AREA_IMAGE_SECONDARY, FLASH_AREA_IMAGE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001004 img_off, 0, copy_sz);
1005 assert(rc == 0);
1006
David Vincze39e78552018-10-10 17:10:01 +02001007 if (bs->idx == BOOT_STATUS_IDX_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001008 if (bs->use_scratch) {
1009 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH, bs);
1010 } else {
1011 /* Prepare the status area... here it is known that the
1012 * last sector is not being used by the image data so it's
1013 * safe to erase.
1014 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001015 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_PRIMARY);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001016 assert(rc == 0);
1017
David Vincze8bdfc2d2019-03-18 15:49:23 +01001018 boot_status_init_by_id(FLASH_AREA_IMAGE_PRIMARY, bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001019 }
1020 }
1021
David Vincze39e78552018-10-10 17:10:01 +02001022 bs->state = BOOT_STATUS_STATE_1;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001023 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001024 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001025 }
1026
David Vincze39e78552018-10-10 17:10:01 +02001027 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001028 rc = boot_erase_sector(FLASH_AREA_IMAGE_SECONDARY, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001029 assert(rc == 0);
1030
David Vincze8bdfc2d2019-03-18 15:49:23 +01001031 rc = boot_copy_sector(FLASH_AREA_IMAGE_PRIMARY,
1032 FLASH_AREA_IMAGE_SECONDARY,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001033 img_off, img_off, copy_sz);
1034 assert(rc == 0);
1035
David Vincze39e78552018-10-10 17:10:01 +02001036 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001037 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001038 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001039 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001040 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_SECONDARY);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001041 assert(rc == 0);
1042 }
1043
David Vincze39e78552018-10-10 17:10:01 +02001044 bs->state = BOOT_STATUS_STATE_2;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001045 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001046 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001047 }
1048
David Vincze39e78552018-10-10 17:10:01 +02001049 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001050 rc = boot_erase_sector(FLASH_AREA_IMAGE_PRIMARY, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001051 assert(rc == 0);
1052
1053 /* NOTE: also copy trailer from scratch (has status info) */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001054 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH,
1055 FLASH_AREA_IMAGE_PRIMARY,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001056 0, img_off, copy_sz);
1057 assert(rc == 0);
1058
1059 if (bs->use_scratch) {
1060 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
1061 assert(rc == 0);
1062
1063 scratch_trailer_off = boot_status_off(fap);
1064
1065 flash_area_close(fap);
1066
David Vincze8bdfc2d2019-03-18 15:49:23 +01001067 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001068 assert(rc == 0);
1069
1070 /* copy current status that is being maintained in scratch */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001071 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH,
1072 FLASH_AREA_IMAGE_PRIMARY,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001073 scratch_trailer_off,
1074 img_off + copy_sz,
1075 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
David Vincze39e78552018-10-10 17:10:01 +02001076 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001077
1078 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1079 &swap_state);
1080 assert(rc == 0);
1081
1082 if (swap_state.image_ok == BOOT_FLAG_SET) {
1083 rc = boot_write_image_ok(fap);
1084 assert(rc == 0);
1085 }
1086
1087 rc = boot_write_swap_size(fap, bs->swap_size);
1088 assert(rc == 0);
1089
1090 rc = boot_write_magic(fap);
1091 assert(rc == 0);
1092
1093 flash_area_close(fap);
1094 }
1095
1096 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001097 bs->state = BOOT_STATUS_STATE_0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001098 bs->use_scratch = 0;
1099 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001100 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001101 }
1102}
1103#endif /* !MCUBOOT_OVERWRITE_ONLY */
1104
1105/**
1106 * Swaps the two images in flash. If a prior copy operation was interrupted
1107 * by a system reset, this function completes that operation.
1108 *
1109 * @param bs The current boot status. This function reads
1110 * this struct to determine if it is resuming
1111 * an interrupted swap operation. This
1112 * function writes the updated status to this
1113 * function on return.
1114 *
1115 * @return 0 on success; nonzero on failure.
1116 */
1117#ifdef MCUBOOT_OVERWRITE_ONLY
1118static int
1119boot_copy_image(struct boot_status *bs)
1120{
1121 size_t sect_count;
1122 size_t sect;
1123 int rc;
1124 size_t size = 0;
1125 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001126 size_t last_sector;
1127
1128 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001129
David Vincze8bdfc2d2019-03-18 15:49:23 +01001130 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1131 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001132
David Vincze8bdfc2d2019-03-18 15:49:23 +01001133 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001134 for (sect = 0; sect < sect_count; sect++) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001135 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
1136 rc = boot_erase_sector(FLASH_AREA_IMAGE_PRIMARY,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001137 size,
1138 this_size);
1139 assert(rc == 0);
1140
1141 size += this_size;
1142 }
1143
David Vincze8bdfc2d2019-03-18 15:49:23 +01001144 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1145 size);
1146 rc = boot_copy_sector(FLASH_AREA_IMAGE_SECONDARY, FLASH_AREA_IMAGE_PRIMARY,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001147 0, 0, size);
1148
David Vincze060968d2019-05-23 01:13:14 +02001149 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001150 * value. Both slots hold the new image at this point, but the secondary
1151 * slot's image header must be passed because the read image headers in the
1152 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001153 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001154 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1155 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001156 if (rc != 0) {
1157 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1158 return rc;
1159 }
1160
David Vincze39e78552018-10-10 17:10:01 +02001161 /*
1162 * Erases header and trailer. The trailer is erased because when a new
1163 * image is written without a trailer as is the case when using newt, the
1164 * trailer that was left might trigger a new upgrade.
1165 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001166 rc = boot_erase_sector(FLASH_AREA_IMAGE_SECONDARY,
1167 boot_img_sector_off(&boot_data,
1168 BOOT_SECONDARY_SLOT, 0),
1169 boot_img_sector_size(&boot_data,
1170 BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001171 assert(rc == 0);
David Vincze8bdfc2d2019-03-18 15:49:23 +01001172 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
1173 rc = boot_erase_sector(FLASH_AREA_IMAGE_SECONDARY,
1174 boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT,
1175 last_sector),
1176 boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT,
1177 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001178 assert(rc == 0);
1179
David Vincze8bdfc2d2019-03-18 15:49:23 +01001180 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001181
1182 return 0;
1183}
1184#else
1185static int
1186boot_copy_image(struct boot_status *bs)
1187{
1188 uint32_t sz;
1189 int first_sector_idx;
1190 int last_sector_idx;
David Vincze39e78552018-10-10 17:10:01 +02001191 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001192 struct image_header *hdr;
1193 uint32_t size;
1194 uint32_t copy_size;
1195 int rc;
1196
1197 /* FIXME: just do this if asked by user? */
1198
1199 size = copy_size = 0;
1200
David Vincze39e78552018-10-10 17:10:01 +02001201 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001202 /*
1203 * No swap ever happened, so need to find the largest image which
1204 * will be used to determine the amount of sectors to swap.
1205 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001206 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001207 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001208 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001209 assert(rc == 0);
1210 }
1211
David Vincze8bdfc2d2019-03-18 15:49:23 +01001212 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001213 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001214 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001215 assert(rc == 0);
1216 }
1217
1218 if (size > copy_size) {
1219 copy_size = size;
1220 }
1221
1222 bs->swap_size = copy_size;
1223 } else {
1224 /*
1225 * If a swap was under way, the swap_size should already be present
1226 * in the trailer...
1227 */
1228 rc = boot_read_swap_size(&bs->swap_size);
1229 assert(rc == 0);
1230
1231 copy_size = bs->swap_size;
1232 }
1233
1234 size = 0;
1235 last_sector_idx = 0;
1236 while (1) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001237 size += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT,
1238 last_sector_idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001239 if (size >= copy_size) {
1240 break;
1241 }
1242 last_sector_idx++;
1243 }
1244
1245 swap_idx = 0;
1246 while (last_sector_idx >= 0) {
1247 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001248 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001249 boot_swap_sectors(first_sector_idx, sz, bs);
1250 }
1251
1252 last_sector_idx = first_sector_idx - 1;
1253 swap_idx++;
1254 }
1255
David Vincze8bdfc2d2019-03-18 15:49:23 +01001256#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001257 if (boot_status_fails > 0) {
1258 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1259 }
1260#endif
1261
Tamas Banf70ef8c2017-12-19 15:35:09 +00001262 return 0;
1263}
1264#endif
1265
1266/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001267 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001268 */
1269#ifndef MCUBOOT_OVERWRITE_ONLY
1270static int
1271boot_set_copy_done(void)
1272{
1273 const struct flash_area *fap;
1274 int rc;
1275
David Vincze8bdfc2d2019-03-18 15:49:23 +01001276 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001277 if (rc != 0) {
1278 return BOOT_EFLASH;
1279 }
1280
1281 rc = boot_write_copy_done(fap);
1282 flash_area_close(fap);
1283 return rc;
1284}
1285#endif /* !MCUBOOT_OVERWRITE_ONLY */
1286
1287/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001288 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1289 * ensure the status bytes from the image revert operation don't get processed
1290 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001291 *
1292 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001293 * image installed on the primary slot and the new image to be upgrade to has a
1294 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001295 */
1296#ifndef MCUBOOT_OVERWRITE_ONLY
1297static int
1298boot_set_image_ok(void)
1299{
1300 const struct flash_area *fap;
1301 struct boot_swap_state state;
1302 int rc;
1303
David Vincze8bdfc2d2019-03-18 15:49:23 +01001304 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001305 if (rc != 0) {
1306 return BOOT_EFLASH;
1307 }
1308
1309 rc = boot_read_swap_state(fap, &state);
1310 if (rc != 0) {
1311 rc = BOOT_EFLASH;
1312 goto out;
1313 }
1314
1315 if (state.image_ok == BOOT_FLAG_UNSET) {
1316 rc = boot_write_image_ok(fap);
1317 }
1318
1319out:
1320 flash_area_close(fap);
1321 return rc;
1322}
1323#endif /* !MCUBOOT_OVERWRITE_ONLY */
1324
1325/**
1326 * Performs an image swap if one is required.
1327 *
1328 * @param out_swap_type On success, the type of swap performed gets
1329 * written here.
1330 *
1331 * @return 0 on success; nonzero on failure.
1332 */
1333static int
1334boot_swap_if_needed(int *out_swap_type)
1335{
1336 struct boot_status bs;
1337 int swap_type;
1338 int rc;
1339
1340 /* Determine if we rebooted in the middle of an image swap
1341 * operation.
1342 */
1343 rc = boot_read_status(&bs);
1344 assert(rc == 0);
1345 if (rc != 0) {
1346 return rc;
1347 }
1348
1349 /* If a partial swap was detected, complete it. */
David Vincze39e78552018-10-10 17:10:01 +02001350 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001351 rc = boot_copy_image(&bs);
1352 assert(rc == 0);
1353
1354 /* NOTE: here we have finished a swap resume. The initial request
1355 * was either a TEST or PERM swap, which now after the completed
1356 * swap will be determined to be respectively REVERT (was TEST)
1357 * or NONE (was PERM).
1358 */
1359
1360 /* Extrapolate the type of the partial swap. We need this
1361 * information to know how to mark the swap complete in flash.
1362 */
1363 swap_type = boot_previous_swap_type();
1364 } else {
1365 swap_type = boot_validated_swap_type();
1366 switch (swap_type) {
1367 case BOOT_SWAP_TYPE_TEST:
1368 case BOOT_SWAP_TYPE_PERM:
1369 case BOOT_SWAP_TYPE_REVERT:
1370 rc = boot_copy_image(&bs);
1371 assert(rc == 0);
1372 break;
1373 }
1374 }
1375
1376 *out_swap_type = swap_type;
1377 return 0;
1378}
1379
1380/**
1381 * Prepares the booting process. This function moves images around in flash as
1382 * appropriate, and tells you what address to boot from.
1383 *
1384 * @param rsp On success, indicates how booting should occur.
1385 *
1386 * @return 0 on success; nonzero on failure.
1387 */
1388int
1389boot_go(struct boot_rsp *rsp)
1390{
1391 int swap_type;
1392 size_t slot;
1393 int rc;
1394 int fa_id;
1395 bool reload_headers = false;
1396
1397 /* The array of slot sectors are defined here (as opposed to file scope) so
1398 * that they don't get allocated for non-boot-loader apps. This is
1399 * necessary because the gcc option "-fdata-sections" doesn't seem to have
1400 * any effect in older gcc versions (e.g., 4.8.4).
1401 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001402 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1403 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban581034a2017-12-19 19:54:37 +00001404
David Vincze8bdfc2d2019-03-18 15:49:23 +01001405 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1406 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001407
1408 /* Open boot_data image areas for the duration of this call. */
1409 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1410 fa_id = flash_area_id_from_image_slot(slot);
1411 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1412 assert(rc == 0);
1413 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001414
Tamas Banf70ef8c2017-12-19 15:35:09 +00001415 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1416 &BOOT_SCRATCH_AREA(&boot_data));
1417 assert(rc == 0);
1418
1419 /* Determine the sector layout of the image slots and scratch area. */
1420 rc = boot_read_sectors();
1421 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001422 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1423 BOOT_MAX_IMG_SECTORS);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001424 goto out;
1425 }
1426
1427 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001428 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001429 if (rc != 0) {
1430 goto out;
1431 }
1432
1433 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze8bdfc2d2019-03-18 15:49:23 +01001434 * into the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001435 */
1436 if (boot_slots_compatible()) {
1437 rc = boot_swap_if_needed(&swap_type);
1438 assert(rc == 0);
1439 if (rc != 0) {
1440 goto out;
1441 }
1442
1443 /*
1444 * The following states need image_ok be explicitly set after the
1445 * swap was finished to avoid a new revert.
1446 */
Tamas Ban581034a2017-12-19 19:54:37 +00001447 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1448 swap_type == BOOT_SWAP_TYPE_FAIL) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001449#ifndef MCUBOOT_OVERWRITE_ONLY
1450 rc = boot_set_image_ok();
1451 if (rc != 0) {
1452 swap_type = BOOT_SWAP_TYPE_PANIC;
1453 }
1454#endif /* !MCUBOOT_OVERWRITE_ONLY */
1455 }
1456 } else {
1457 swap_type = BOOT_SWAP_TYPE_NONE;
1458 }
1459
1460 switch (swap_type) {
1461 case BOOT_SWAP_TYPE_NONE:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001462 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001463 break;
1464
1465 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1466 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1467 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001468 slot = BOOT_SECONDARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001469 reload_headers = true;
1470#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze060968d2019-05-23 01:13:14 +02001471 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1472 /* Update the stored security counter with the new image's security
David Vincze8bdfc2d2019-03-18 15:49:23 +01001473 * counter value. The primary slot holds the new image at this
1474 * point, but the secondary slot's image header must be passed
1475 * because the read image headers in the boot_data structure have
1476 * not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001477 *
1478 * In case of a permanent image swap mcuboot will never attempt to
1479 * revert the images on the next reboot. Therefore, the security
1480 * counter must be increased right after the image upgrade.
1481 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001482 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1483 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001484 if (rc != 0) {
1485 BOOT_LOG_ERR("Security counter update failed after "
1486 "image upgrade.");
1487 goto out;
1488 }
1489 }
1490
Tamas Banf70ef8c2017-12-19 15:35:09 +00001491 rc = boot_set_copy_done();
1492 if (rc != 0) {
1493 swap_type = BOOT_SWAP_TYPE_PANIC;
1494 }
1495#endif /* !MCUBOOT_OVERWRITE_ONLY */
1496 break;
1497
1498 case BOOT_SWAP_TYPE_FAIL:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001499 /* The image in the secondary slot was invalid and is now erased.
1500 * Ensure we don't try to boot into it again on the next reboot.
1501 * Do this by pretending we just reverted back to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001502 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001503 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001504 reload_headers = true;
1505 break;
1506
1507 default:
1508 swap_type = BOOT_SWAP_TYPE_PANIC;
1509 }
1510
1511 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1512 BOOT_LOG_ERR("panic!");
1513 assert(0);
1514
1515 /* Loop forever... */
David Vincze39e78552018-10-10 17:10:01 +02001516 while (1) {}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001517 }
1518
Tamas Banf70ef8c2017-12-19 15:35:09 +00001519 if (reload_headers) {
David Vincze39e78552018-10-10 17:10:01 +02001520 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001521 if (rc != 0) {
1522 goto out;
1523 }
1524 /* Since headers were reloaded, it can be assumed we just performed a
1525 * swap or overwrite. Now the header info that should be used to
David Vincze8bdfc2d2019-03-18 15:49:23 +01001526 * provide the data for the bootstrap, which previously was at the
1527 * secondary slot, was updated to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001528 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001529 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001530 }
1531
David Vincze8bdfc2d2019-03-18 15:49:23 +01001532#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
1533 rc = boot_validate_slot(BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001534 assert(rc == 0);
1535 if (rc != 0) {
1536 rc = BOOT_EBADIMAGE;
1537 goto out;
1538 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001539#else
1540 /* Even if we're not re-validating the primary slot, we could be booting
David Vincze39e78552018-10-10 17:10:01 +02001541 * onto an empty flash chip. At least do a basic sanity check that
1542 * the magic number on the image is OK.
1543 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001544 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1545 BOOT_LOG_ERR("bad image magic 0x%lx",
1546 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
David Vincze39e78552018-10-10 17:10:01 +02001547 rc = BOOT_EBADIMAGE;
1548 goto out;
1549 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001550#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001551
David Vincze060968d2019-05-23 01:13:14 +02001552 /* Update the stored security counter with the active image's security
1553 * counter value. It will be updated only if the new security counter is
1554 * greater than the stored value.
1555 *
1556 * In case of a successful image swapping when the swap type is TEST the
1557 * security counter can be increased only after a reset, when the swap type
1558 * is NONE and the image has marked itself "OK" (the image_ok flag has been
1559 * set). This way a "revert" swap can be performed if it's necessary.
1560 */
1561 if (swap_type == BOOT_SWAP_TYPE_NONE) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001562 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1563 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001564 if (rc != 0) {
1565 BOOT_LOG_ERR("Security counter update failed after image "
1566 "validation.");
1567 goto out;
1568 }
1569 }
1570
Tamas Banf70ef8c2017-12-19 15:35:09 +00001571 /* Always boot from the primary slot. */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001572 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, BOOT_PRIMARY_SLOT);
1573 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001574 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
1575
Tamas Ban0e8ab302019-01-17 11:45:31 +00001576 /* Save boot status to shared memory area */
1577 rc = boot_save_boot_status(SW_S_NS,
1578 rsp->br_hdr,
1579 BOOT_IMG_AREA(&boot_data, slot));
1580 if (rc) {
1581 BOOT_LOG_ERR("Failed to add data to shared area");
1582 }
1583
Tamas Banf70ef8c2017-12-19 15:35:09 +00001584 out:
1585 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1586 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1587 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1588 }
1589 return rc;
1590}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001591
Oliver Swedef9982442018-08-24 18:37:44 +01001592#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001593
David Vinczedcba70b2019-05-28 12:02:52 +02001594#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
1595 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
1596 (area), \
1597 (hdr)->ih_ver.iv_major, \
1598 (hdr)->ih_ver.iv_minor, \
1599 (hdr)->ih_ver.iv_revision, \
1600 (hdr)->ih_ver.iv_build_num, \
1601 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
1602 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
1603 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001604 (state)->image_ok)
1605
1606struct image_slot_version {
1607 uint64_t version;
1608 uint32_t slot_number;
1609};
1610
1611/**
1612 * Extract the version number from the image header. This function must be
1613 * ported if version number format has changed in the image header.
1614 *
1615 * @param hdr Pointer to an image header structure
1616 *
Oliver Swedef9982442018-08-24 18:37:44 +01001617 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001618 */
1619static uint64_t
1620boot_get_version_number(struct image_header *hdr)
1621{
Oliver Swedef9982442018-08-24 18:37:44 +01001622 uint64_t version = 0;
1623 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
1624 + IMAGE_VER_REVISION_LENGTH
1625 + IMAGE_VER_BUILD_NUM_LENGTH);
1626 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
1627 + IMAGE_VER_BUILD_NUM_LENGTH);
1628 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
1629 version |= hdr->ih_ver.iv_build_num;
1630 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001631}
1632
1633/**
1634 * Comparator function for `qsort` to compare version numbers. This function
1635 * must be ported if version number format has changed in the image header.
1636 *
1637 * @param ver1 Pointer to an array element which holds the version number
1638 * @param ver2 Pointer to another array element which holds the version
1639 * number
1640 *
1641 * @return if version1 > version2 -1
1642 * if version1 == version2 0
1643 * if version1 < version2 1
1644 */
1645static int
1646boot_compare_version_numbers(const void *ver1, const void *ver2)
1647{
1648 if (((struct image_slot_version *)ver1)->version <
1649 ((struct image_slot_version *)ver2)->version) {
1650 return 1;
1651 }
1652
1653 if (((struct image_slot_version *)ver1)->version ==
1654 ((struct image_slot_version *)ver2)->version) {
1655 return 0;
1656 }
1657
1658 return -1;
1659}
1660
1661/**
1662 * Sort the available images based on the version number and puts them in
1663 * a list.
1664 *
1665 * @param boot_sequence A pointer to an array, whose aim is to carry
1666 * the boot order of candidate images.
1667 * @param slot_cnt The number of flash areas, which can contains firmware
1668 * images.
1669 *
1670 * @return The number of valid images.
1671 */
1672uint32_t
1673boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
1674{
1675 struct boot_swap_state slot_state;
1676 struct image_header *hdr;
1677 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
1678 uint32_t image_cnt = 0;
1679 uint32_t slot;
1680 int32_t rc;
1681 int32_t fa_id;
1682
1683 for (slot = 0; slot < slot_cnt; slot++) {
1684 hdr = boot_img_hdr(&boot_data, slot);
1685 fa_id = flash_area_id_from_image_slot(slot);
1686 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
1687 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02001688 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
1689 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001690 continue;
1691 }
1692
1693 if (hdr->ih_magic == IMAGE_MAGIC) {
1694 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02001695 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001696 /* Valid cases:
1697 * - Test mode: magic is OK in image trailer
1698 * - Permanent mode: image_ok flag has previously set
1699 */
1700 image_versions[slot].slot_number = slot;
1701 image_versions[slot].version = boot_get_version_number(hdr);
1702 image_cnt++;
1703 }
1704
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001705 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
1706 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02001707 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001708 }
1709 }
1710
1711 /* Sort the images based on version number */
1712 qsort(&image_versions[0],
1713 slot_cnt,
1714 sizeof(struct image_slot_version),
1715 boot_compare_version_numbers);
1716
1717 /* Copy the calculated boot sequence to boot_sequence array */
1718 for (slot = 0; slot < slot_cnt; slot++) {
1719 boot_sequence[slot] = image_versions[slot].slot_number;
1720 }
1721
1722 return image_cnt;
1723}
1724
Oliver Swedef9982442018-08-24 18:37:44 +01001725#ifdef MCUBOOT_RAM_LOADING
1726/**
1727 * Copies an image from a slot in the flash to an SRAM address, where the load
1728 * address has already been inserted into the image header by this point and is
1729 * extracted from it within this method. The copying is done sector-by-sector.
1730 *
1731 * @param slot The flash slot of the image to be copied to SRAM.
1732 *
1733 * @param hdr Pointer to the image header structure of the image
1734 * that needs to be copid to SRAM
1735 *
1736 * @return 0 on success; nonzero on failure.
1737 */
1738static int
1739boot_copy_image_to_sram(int slot, struct image_header *hdr)
1740{
1741 int rc;
1742 uint32_t sect_sz;
1743 uint32_t sect = 0;
1744 uint32_t bytes_copied = 0;
1745 const struct flash_area *fap_src = NULL;
1746 uint32_t dst = (uint32_t) hdr->ih_load_addr;
1747 uint32_t img_sz;
1748
1749 if (dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01001750 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01001751 "- the load address must be aligned with 4 bytes due to SRAM "
1752 "restrictions", dst);
1753 return BOOT_EBADARGS;
1754 }
1755
1756 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
1757 if (rc != 0) {
1758 return BOOT_EFLASH;
1759 }
1760
1761 rc = boot_read_image_size(slot, hdr, &img_sz);
1762 if (rc != 0) {
1763 return BOOT_EFLASH;
1764 }
1765
1766 while (bytes_copied < img_sz) {
1767 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
1768 /*
1769 * Direct copy from where the image sector resides in flash to its new
1770 * location in SRAM
1771 */
1772 rc = flash_area_read(fap_src,
1773 bytes_copied,
1774 (void *)(dst + bytes_copied),
1775 sect_sz);
1776 if (rc != 0) {
1777 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
1778 break;
1779 } else {
1780 bytes_copied += sect_sz;
1781 }
1782 sect++;
1783 }
1784
1785 if (fap_src) {
1786 flash_area_close(fap_src);
1787 }
1788 return rc;
1789}
1790#endif /* MCUBOOT_RAM_LOADING */
1791
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001792/**
1793 * Prepares the booting process. This function choose the newer image in flash
1794 * as appropriate, and returns the address to boot from.
1795 *
1796 * @param rsp On success, indicates how booting should occur.
1797 *
1798 * @return 0 on success; nonzero on failure.
1799 */
1800int
1801boot_go(struct boot_rsp *rsp)
1802{
1803 size_t slot = 0;
1804 int32_t i;
1805 int rc;
1806 int fa_id;
1807 uint32_t boot_sequence[BOOT_NUM_SLOTS];
1808 uint32_t img_cnt;
Oliver Swedef9982442018-08-24 18:37:44 +01001809 struct image_header *newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001810
David Vincze8bdfc2d2019-03-18 15:49:23 +01001811 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1812 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001813
David Vincze8bdfc2d2019-03-18 15:49:23 +01001814 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = &primary_slot_sectors[0];
1815 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001816
1817 /* Open boot_data image areas for the duration of this call. */
1818 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
1819 fa_id = flash_area_id_from_image_slot(i);
1820 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
1821 assert(rc == 0);
1822 }
1823
1824 /* Determine the sector layout of the image slots. */
1825 rc = boot_read_sectors();
1826 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001827 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1828 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001829 goto out;
1830 }
1831
1832 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001833 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001834 if (rc != 0) {
1835 goto out;
1836 }
1837
1838 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
1839 if (img_cnt) {
1840 /* Authenticate images */
1841 for (i = 0; i < img_cnt; i++) {
1842 rc = boot_validate_slot(boot_sequence[i]);
1843 if (rc == 0) {
1844 slot = boot_sequence[i];
1845 break;
1846 }
1847 }
1848 if (rc) {
1849 /* If there was no valid image at all */
1850 rc = BOOT_EBADIMAGE;
1851 goto out;
1852 }
1853
Oliver Swedef9982442018-08-24 18:37:44 +01001854 /* The slot variable now refers to the newest image's slot in flash */
1855 newest_image_header = boot_img_hdr(&boot_data, slot);
1856
David Vincze060968d2019-05-23 01:13:14 +02001857 /* Update the security counter with the newest image's security
1858 * counter value.
1859 */
1860 rc = boot_update_security_counter(slot, newest_image_header);
1861 if (rc != 0) {
1862 BOOT_LOG_ERR("Security counter update failed after image "
1863 "validation.");
1864 goto out;
1865 }
1866
Oliver Swedef9982442018-08-24 18:37:44 +01001867 #ifdef MCUBOOT_RAM_LOADING
1868 if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
1869 /* Copy image to the load address from where it
1870 * currently resides in flash */
1871 rc = boot_copy_image_to_sram(slot, newest_image_header);
1872 if (rc != 0) {
1873 rc = BOOT_EBADIMAGE;
David Vincze8bdfc2d2019-03-18 15:49:23 +01001874 BOOT_LOG_INF("Could not copy image from the %s slot in "
Tamas Banc27b5c32019-05-28 16:30:19 +01001875 "the Flash to load address 0x%x in SRAM, "
David Vincze8bdfc2d2019-03-18 15:49:23 +01001876 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
1877 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01001878 newest_image_header->ih_load_addr);
1879 goto out;
1880 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001881 BOOT_LOG_INF("Image has been copied from the %s slot in "
1882 "the flash to SRAM address 0x%x",
1883 (slot == BOOT_PRIMARY_SLOT) ?
1884 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01001885 newest_image_header->ih_load_addr);
1886 }
1887
1888 /* Validate the image hash in SRAM after the copy was successful */
1889 rc = bootutil_check_hash_after_loading(newest_image_header);
1890 if (rc != 0) {
1891 rc = BOOT_EBADIMAGE;
1892 BOOT_LOG_INF("Cannot validate the hash of the image that was "
1893 "copied to SRAM, aborting..");
1894 goto out;
1895 }
1896
Tamas Banc27b5c32019-05-28 16:30:19 +01001897 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
Oliver Swedef9982442018-08-24 18:37:44 +01001898 newest_image_header->ih_load_addr);
1899 } else {
David Vincze8a2a4e22019-05-24 10:14:23 +02001900#endif /* MCUBOOT_RAM_LOADING */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001901 BOOT_LOG_INF("Booting image from the %s slot",
1902 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze8a2a4e22019-05-24 10:14:23 +02001903#ifdef MCUBOOT_RAM_LOADING
Oliver Swedef9982442018-08-24 18:37:44 +01001904 }
David Vincze8a2a4e22019-05-24 10:14:23 +02001905#endif
Oliver Swedef9982442018-08-24 18:37:44 +01001906
1907 rsp->br_hdr = newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001908 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
Oliver Swedef9982442018-08-24 18:37:44 +01001909 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001910 } else {
1911 /* No candidate image available */
1912 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02001913 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001914 }
1915
Tamas Ban0e8ab302019-01-17 11:45:31 +00001916 /* Save boot status to shared memory area */
1917 rc = boot_save_boot_status(SW_S_NS,
1918 rsp->br_hdr,
1919 BOOT_IMG_AREA(&boot_data, slot));
1920 if (rc) {
1921 BOOT_LOG_ERR("Failed to add data to shared area");
1922 }
1923
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001924out:
1925 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1926 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1927 }
1928 return rc;
1929}
Oliver Swedef9982442018-08-24 18:37:44 +01001930#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */