blob: c7c71739cfff7e3955433c16a2fdbbc30994300a [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 Vincze401c7422019-06-21 20:44:05 +020023 * Git SHA of the original version: 3c469bc698a9767859ed73cd0201c44161204d5c
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;
David Vinczebb207982019-08-21 15:13:04 +020050uint8_t current_image = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +000051
Oliver Swedef9982442018-08-24 18:37:44 +010052#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze39e78552018-10-10 17:10:01 +020053
David Vincze8bdfc2d2019-03-18 15:49:23 +010054#if defined(MCUBOOT_VALIDATE_PRIMARY_SLOT) && !defined(MCUBOOT_OVERWRITE_ONLY)
David Vincze39e78552018-10-10 17:10:01 +020055static int boot_status_fails = 0;
56#define BOOT_STATUS_ASSERT(x) \
57 do { \
58 if (!(x)) { \
59 boot_status_fails++; \
60 } \
61 } while (0)
62#else
David Vincze401c7422019-06-21 20:44:05 +020063#define BOOT_STATUS_ASSERT(x) ASSERT(x)
David Vincze39e78552018-10-10 17:10:01 +020064#endif
65
Tamas Banf70ef8c2017-12-19 15:35:09 +000066struct boot_status_table {
David Vincze8bdfc2d2019-03-18 15:49:23 +010067 uint8_t bst_magic_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000068 uint8_t bst_magic_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +010069 uint8_t bst_copy_done_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +000070 uint8_t bst_status_source;
71};
72
73/**
74 * This set of tables maps swap state contents to boot status location.
75 * When searching for a match, these tables must be iterated in order.
76 */
77static const struct boot_status_table boot_status_tables[] = {
78 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010079 /* | primary slot | scratch |
80 * ----------+--------------+--------------|
81 * magic | Good | Any |
82 * copy-done | Set | N/A |
83 * ----------+--------------+--------------'
84 * source: none |
85 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +000086 */
David Vincze8bdfc2d2019-03-18 15:49:23 +010087 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +020088 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +010089 .bst_copy_done_primary_slot = BOOT_FLAG_SET,
90 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
Tamas Banf70ef8c2017-12-19 15:35:09 +000091 },
92
93 {
David Vincze8bdfc2d2019-03-18 15:49:23 +010094 /* | primary slot | scratch |
95 * ----------+--------------+--------------|
96 * magic | Good | Any |
97 * copy-done | Unset | N/A |
98 * ----------+--------------+--------------'
99 * source: primary slot |
100 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000101 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100102 .bst_magic_primary_slot = BOOT_MAGIC_GOOD,
David Vincze401c7422019-06-21 20:44:05 +0200103 .bst_magic_scratch = BOOT_MAGIC_NOTGOOD,
David Vincze8bdfc2d2019-03-18 15:49:23 +0100104 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
105 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000106 },
107
108 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100109 /* | primary slot | scratch |
110 * ----------+--------------+--------------|
111 * magic | Any | Good |
112 * copy-done | Any | N/A |
113 * ----------+--------------+--------------'
114 * source: scratch |
115 * ----------------------------------------'
Tamas Banf70ef8c2017-12-19 15:35:09 +0000116 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100117 .bst_magic_primary_slot = BOOT_MAGIC_ANY,
118 .bst_magic_scratch = BOOT_MAGIC_GOOD,
119 .bst_copy_done_primary_slot = BOOT_FLAG_ANY,
120 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000121 },
122
123 {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100124 /* | primary slot | scratch |
125 * ----------+--------------+--------------|
126 * magic | Unset | Any |
127 * copy-done | Unset | N/A |
128 * ----------+--------------+--------------|
129 * source: varies |
130 * ----------------------------------------+--------------------------+
Tamas Banf70ef8c2017-12-19 15:35:09 +0000131 * This represents one of two cases: |
132 * o No swaps ever (no status to read, so no harm in checking). |
David Vincze8bdfc2d2019-03-18 15:49:23 +0100133 * o Mid-revert; status in the primary slot. |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000134 * -------------------------------------------------------------------'
135 */
David Vincze8bdfc2d2019-03-18 15:49:23 +0100136 .bst_magic_primary_slot = BOOT_MAGIC_UNSET,
137 .bst_magic_scratch = BOOT_MAGIC_ANY,
138 .bst_copy_done_primary_slot = BOOT_FLAG_UNSET,
139 .bst_status_source = BOOT_STATUS_SOURCE_PRIMARY_SLOT,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000140 },
141};
142
143#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000144 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000145
146#define BOOT_LOG_SWAP_STATE(area, state) \
David Vincze401c7422019-06-21 20:44:05 +0200147 BOOT_LOG_INF("%s: magic=%5s, swap_type=0x%x, copy_done=0x%x, " \
148 "image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000149 (area), \
150 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
151 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
152 "bad"), \
David Vincze401c7422019-06-21 20:44:05 +0200153 (state)->swap_type, \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000154 (state)->copy_done, \
155 (state)->image_ok)
Oliver Swedef9982442018-08-24 18:37:44 +0100156#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000157
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000158
159static int
160boot_read_image_header(int slot, struct image_header *out_hdr)
161{
162 const struct flash_area *fap = NULL;
163 int area_id;
164 int rc;
165
166 area_id = flash_area_id_from_image_slot(slot);
167 rc = flash_area_open(area_id, &fap);
168 if (rc != 0) {
169 rc = BOOT_EFLASH;
170 goto done;
171 }
172
173 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
174 if (rc != 0) {
175 rc = BOOT_EFLASH;
176 goto done;
177 }
178
179 rc = 0;
180
181done:
182 flash_area_close(fap);
183 return rc;
184}
185
186static int
David Vincze39e78552018-10-10 17:10:01 +0200187boot_read_image_headers(bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000188{
189 int rc;
190 int i;
191
192 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
193 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
194 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200195 /* If `require_all` is set, fail on any single fail, otherwise
196 * if at least the first slot's header was read successfully,
197 * then the boot loader can attempt a boot.
198 *
199 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000200 */
David Vincze39e78552018-10-10 17:10:01 +0200201 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000202 return 0;
203 } else {
204 return rc;
205 }
206 }
207 }
208
209 return 0;
210}
211
212static uint8_t
213boot_write_sz(void)
214{
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000215 uint8_t elem_sz;
216 uint8_t align;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000217
218 /* Figure out what size to write update status update as. The size depends
219 * on what the minimum write size is for scratch area, active image slot.
220 * We need to use the bigger of those 2 values.
221 */
David Vincze401c7422019-06-21 20:44:05 +0200222 elem_sz = flash_area_align(boot_data.imgs[BOOT_PRIMARY_SLOT].area);
223 align = flash_area_align(boot_data.scratch.area);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000224 if (align > elem_sz) {
225 elem_sz = align;
226 }
227
228 return elem_sz;
229}
230
231/**
232 * Determines the sector layout of both image slots and the scratch area.
233 * This information is necessary for calculating the number of bytes to erase
234 * and copy during an image swap. The information collected during this
235 * function is used to populate the boot_data global.
236 */
237static int
238boot_read_sectors(void)
239{
240 int rc;
241
David Vincze8bdfc2d2019-03-18 15:49:23 +0100242 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_PRIMARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000243 if (rc != 0) {
244 return BOOT_EFLASH;
245 }
246
David Vincze8bdfc2d2019-03-18 15:49:23 +0100247 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SECONDARY);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000248 if (rc != 0) {
249 return BOOT_EFLASH;
250 }
251
David Vincze401c7422019-06-21 20:44:05 +0200252 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_SCRATCH);
253 if (rc != 0) {
254 return BOOT_EFLASH;
255 }
256
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000257 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
258
259 return 0;
260}
261
David Vincze060968d2019-05-23 01:13:14 +0200262/**
263 * Validate image hash/signature and security counter in a slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000264 */
265static int
David Vincze401c7422019-06-21 20:44:05 +0200266boot_image_check(struct image_header *hdr, const struct flash_area *fap,
267 struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000268{
269 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
270
David Vincze401c7422019-06-21 20:44:05 +0200271 (void)bs;
272
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000273 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
Tamas Ban0e8ab302019-01-17 11:45:31 +0000274 NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000275 return BOOT_EBADIMAGE;
276 }
277 return 0;
278}
279
David Vincze401c7422019-06-21 20:44:05 +0200280/*
281 * Check that a memory area consists of a given value.
282 */
283static inline bool
284boot_data_is_set_to(uint8_t val, void *data, size_t len)
David Vincze39e78552018-10-10 17:10:01 +0200285{
286 uint8_t i;
David Vincze401c7422019-06-21 20:44:05 +0200287 uint8_t *p = (uint8_t *)data;
288 for (i = 0; i < len; i++) {
289 if (val != p[i]) {
290 return false;
David Vincze39e78552018-10-10 17:10:01 +0200291 }
292 }
David Vincze401c7422019-06-21 20:44:05 +0200293 return true;
David Vincze39e78552018-10-10 17:10:01 +0200294}
295
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000296static int
David Vincze401c7422019-06-21 20:44:05 +0200297boot_check_header_erased(int slot)
298{
299 const struct flash_area *fap;
300 struct image_header *hdr;
301 uint8_t erased_val;
302 int rc;
303
304 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
305 if (rc != 0) {
306 return -1;
307 }
308
309 erased_val = flash_area_erased_val(fap);
310 flash_area_close(fap);
311
312 hdr = boot_img_hdr(&boot_data, slot);
313 if (!boot_data_is_set_to(erased_val, &hdr->ih_magic,
314 sizeof(hdr->ih_magic))) {
315 return -1;
316 }
317
318 return 0;
319}
320
321static int
322boot_validate_slot(int slot, struct boot_status *bs)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000323{
324 const struct flash_area *fap;
325 struct image_header *hdr;
326 int rc;
327
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000328 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
329 if (rc != 0) {
330 return BOOT_EFLASH;
331 }
332
David Vincze39e78552018-10-10 17:10:01 +0200333 hdr = boot_img_hdr(&boot_data, slot);
David Vincze401c7422019-06-21 20:44:05 +0200334 if ((boot_check_header_erased(slot) == 0) ||
335 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100336 /* No bootable image in slot; continue booting from the primary slot. */
David Vincze401c7422019-06-21 20:44:05 +0200337 rc = -1;
338 goto out;
David Vincze39e78552018-10-10 17:10:01 +0200339 }
340
Tamas Bana9de4a62018-09-18 08:09:45 +0100341 if ((hdr->ih_magic != IMAGE_MAGIC ||
David Vincze401c7422019-06-21 20:44:05 +0200342 boot_image_check(hdr, fap, bs) != 0)) {
343 if (slot != BOOT_PRIMARY_SLOT) {
David Vincze26e8c8a2018-08-28 16:59:41 +0200344 rc = flash_area_erase(fap, 0, fap->fa_size);
345 if(rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200346 rc = BOOT_EFLASH;
347 goto out;
David Vincze26e8c8a2018-08-28 16:59:41 +0200348 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100349 /* Image in the secondary slot is invalid. Erase the image and
350 * continue booting from the primary slot.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000351 */
352 }
David Vincze8bdfc2d2019-03-18 15:49:23 +0100353 BOOT_LOG_ERR("Authentication failed! Image in the %s slot is not valid."
354 , (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze401c7422019-06-21 20:44:05 +0200355 rc = -1;
356 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000357 }
358
David Vincze8bdfc2d2019-03-18 15:49:23 +0100359 /* Image in the secondary slot is valid. */
David Vincze401c7422019-06-21 20:44:05 +0200360 rc = 0;
361
362out:
363 flash_area_close(fap);
364 return rc;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000365}
366
David Vincze060968d2019-05-23 01:13:14 +0200367/**
368 * Updates the stored security counter value with the image's security counter
369 * value which resides in the given slot if it's greater than the stored value.
370 *
371 * @param slot Slot number of the image.
372 * @param hdr Pointer to the image header structure of the image that is
373 * currently stored in the given slot.
374 *
375 * @return 0 on success; nonzero on failure.
376 */
377static int
378boot_update_security_counter(int slot, struct image_header *hdr)
379{
380 const struct flash_area *fap = NULL;
381 uint32_t img_security_cnt;
382 int rc;
383
384 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
385 if (rc != 0) {
386 rc = BOOT_EFLASH;
387 goto done;
388 }
389
390 rc = bootutil_get_img_security_cnt(hdr, fap, &img_security_cnt);
391 if (rc != 0) {
392 goto done;
393 }
394
395 rc = boot_nv_security_counter_update(0, img_security_cnt);
396 if (rc != 0) {
397 goto done;
398 }
399
400done:
401 flash_area_close(fap);
402 return rc;
403}
404
Oliver Swedef9982442018-08-24 18:37:44 +0100405#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
406/*
407 * Compute the total size of the given image. Includes the size of
408 * the TLVs.
409 */
410static int
411boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
412{
413 const struct flash_area *fap = NULL;
414 struct image_tlv_info info;
415 int area_id;
416 int rc;
417
418 area_id = flash_area_id_from_image_slot(slot);
419 rc = flash_area_open(area_id, &fap);
420 if (rc != 0) {
421 rc = BOOT_EFLASH;
422 goto done;
423 }
424
425 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
426 &info, sizeof(info));
427 if (rc != 0) {
428 rc = BOOT_EFLASH;
429 goto done;
430 }
431 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
432 rc = BOOT_EBADIMAGE;
433 goto done;
434 }
435 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
436 rc = 0;
437
438done:
439 flash_area_close(fap);
440 return rc;
441}
442#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
443
444#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000445/**
Tamas Ban581034a2017-12-19 19:54:37 +0000446 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000447 * status is necessary for completing a swap that was interrupted by a boot
448 * loader reset.
449 *
David Vincze401c7422019-06-21 20:44:05 +0200450 * @return A BOOT_STATUS_SOURCE_[...] code indicating where status should
451 * be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000452 */
453static int
454boot_status_source(void)
455{
456 const struct boot_status_table *table;
457 struct boot_swap_state state_scratch;
David Vincze8bdfc2d2019-03-18 15:49:23 +0100458 struct boot_swap_state state_primary_slot;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000459 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200460 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000461 uint8_t source;
462
David Vincze8bdfc2d2019-03-18 15:49:23 +0100463 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_PRIMARY,
464 &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000465 assert(rc == 0);
466
467 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
468 assert(rc == 0);
469
David Vincze401c7422019-06-21 20:44:05 +0200470 BOOT_LOG_SWAP_STATE("Primary image", &state_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000471 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
472
473 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
474 table = &boot_status_tables[i];
475
David Vincze401c7422019-06-21 20:44:05 +0200476 if (boot_magic_compatible_check(table->bst_magic_primary_slot,
477 state_primary_slot.magic) &&
478 boot_magic_compatible_check(table->bst_magic_scratch,
479 state_scratch.magic) &&
David Vincze8bdfc2d2019-03-18 15:49:23 +0100480 (table->bst_copy_done_primary_slot == BOOT_FLAG_ANY ||
481 table->bst_copy_done_primary_slot == state_primary_slot.copy_done))
482 {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000483 source = table->bst_status_source;
484 BOOT_LOG_INF("Boot source: %s",
485 source == BOOT_STATUS_SOURCE_NONE ? "none" :
486 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
David Vincze8bdfc2d2019-03-18 15:49:23 +0100487 source == BOOT_STATUS_SOURCE_PRIMARY_SLOT ?
488 "primary slot" : "BUG; can't happen");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000489 return source;
490 }
491 }
492
493 BOOT_LOG_INF("Boot source: none");
494 return BOOT_STATUS_SOURCE_NONE;
495}
496
David Vincze401c7422019-06-21 20:44:05 +0200497/*
498 * Slots are compatible when all sectors that store upto to size of the image
499 * round up to sector size, in both slot's are able to fit in the scratch
500 * area, and have sizes that are a multiple of each other (powers of two
501 * presumably!).
Tamas Banf70ef8c2017-12-19 15:35:09 +0000502 */
503static int
Tamas Banf70ef8c2017-12-19 15:35:09 +0000504boot_slots_compatible(void)
505{
David Vincze401c7422019-06-21 20:44:05 +0200506 size_t num_sectors_primary;
507 size_t num_sectors_secondary;
508 size_t sz0, sz1;
509 size_t primary_slot_sz, secondary_slot_sz;
510 size_t scratch_sz;
511 size_t i, j;
512 int8_t smaller;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000513
David Vincze401c7422019-06-21 20:44:05 +0200514 num_sectors_primary =
515 boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
516 num_sectors_secondary =
517 boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT);
518 if ((num_sectors_primary > BOOT_MAX_IMG_SECTORS) ||
519 (num_sectors_secondary > BOOT_MAX_IMG_SECTORS)) {
David Vincze39e78552018-10-10 17:10:01 +0200520 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000521 return 0;
522 }
David Vincze39e78552018-10-10 17:10:01 +0200523
David Vincze401c7422019-06-21 20:44:05 +0200524 scratch_sz = boot_scratch_area_size(&boot_data);
525
526 /*
527 * The following loop scans all sectors in a linear fashion, assuring that
528 * for each possible sector in each slot, it is able to fit in the other
529 * slot's sector or sectors. Slot's should be compatible as long as any
530 * number of a slot's sectors are able to fit into another, which only
531 * excludes cases where sector sizes are not a multiple of each other.
532 */
533 i = sz0 = primary_slot_sz = 0;
534 j = sz1 = secondary_slot_sz = 0;
535 smaller = 0;
536 while (i < num_sectors_primary || j < num_sectors_secondary) {
537 if (sz0 == sz1) {
538 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
539 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
540 i++;
541 j++;
542 } else if (sz0 < sz1) {
543 sz0 += boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
544 /* Guarantee that multiple sectors of the secondary slot
545 * fit into the primary slot.
546 */
547 if (smaller == 2) {
548 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
549 " sectors");
550 return 0;
551 }
552 smaller = 1;
553 i++;
554 } else {
555 sz1 += boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT, j);
556 /* Guarantee that multiple sectors of the primary slot
557 * fit into the secondary slot.
558 */
559 if (smaller == 1) {
560 BOOT_LOG_WRN("Cannot upgrade: slots have non-compatible"
561 " sectors");
562 return 0;
563 }
564 smaller = 2;
565 j++;
566 }
567 if (sz0 == sz1) {
568 primary_slot_sz += sz0;
569 secondary_slot_sz += sz1;
570 /* Scratch has to fit each swap operation to the size of the larger
571 * sector among the primary slot and the secondary slot.
572 */
573 if (sz0 > scratch_sz || sz1 > scratch_sz) {
574 BOOT_LOG_WRN("Cannot upgrade: not all sectors fit inside"
575 " scratch");
576 return 0;
577 }
578 smaller = sz0 = sz1 = 0;
579 }
David Vincze39e78552018-10-10 17:10:01 +0200580 }
581
David Vincze401c7422019-06-21 20:44:05 +0200582 if ((i != num_sectors_primary) ||
583 (j != num_sectors_secondary) ||
584 (primary_slot_sz != secondary_slot_sz)) {
585 BOOT_LOG_WRN("Cannot upgrade: slots are not compatible");
586 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000587 }
588
589 return 1;
590}
591
Tamas Banf70ef8c2017-12-19 15:35:09 +0000592static uint32_t
593boot_status_internal_off(int idx, int state, int elem_sz)
594{
595 int idx_sz;
596
597 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
598
David Vincze39e78552018-10-10 17:10:01 +0200599 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
600 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000601}
602
603/**
604 * Reads the status of a partially-completed swap, if any. This is necessary
605 * to recover in case the boot lodaer was reset in the middle of a swap
606 * operation.
607 */
608static int
609boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
610{
611 uint32_t off;
612 uint8_t status;
613 int max_entries;
614 int found;
David Vincze39e78552018-10-10 17:10:01 +0200615 int found_idx;
616 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000617 int rc;
618 int i;
619
620 off = boot_status_off(fap);
621 max_entries = boot_status_entries(fap);
622
623 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200624 found_idx = 0;
625 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000626 for (i = 0; i < max_entries; i++) {
David Vincze39e78552018-10-10 17:10:01 +0200627 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
628 &status, 1);
629 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000630 return BOOT_EFLASH;
631 }
632
David Vincze39e78552018-10-10 17:10:01 +0200633 if (rc == 1) {
634 if (found && !found_idx) {
635 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000636 }
637 } else if (!found) {
638 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200639 } else if (found_idx) {
640 invalid = 1;
641 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000642 }
643 }
644
David Vincze39e78552018-10-10 17:10:01 +0200645 if (invalid) {
646 /* This means there was an error writing status on the last
647 * swap. Tell user and move on to validation!
648 */
649 BOOT_LOG_ERR("Detected inconsistent status!");
650
David Vincze8bdfc2d2019-03-18 15:49:23 +0100651#if !defined(MCUBOOT_VALIDATE_PRIMARY_SLOT)
652 /* With validation of the primary slot disabled, there is no way
653 * to be sure the swapped primary slot is OK, so abort!
David Vincze39e78552018-10-10 17:10:01 +0200654 */
655 assert(0);
656#endif
657 }
658
Tamas Banf70ef8c2017-12-19 15:35:09 +0000659 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200660 if (!found_idx) {
661 found_idx = i;
662 }
663 found_idx--;
664 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
665 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000666 }
667
668 return 0;
669}
670
671/**
672 * Reads the boot status from the flash. The boot status contains
673 * the current state of an interrupted image copy operation. If the boot
674 * status is not present, or it indicates that previous copy finished,
675 * there is no operation in progress.
676 */
677static int
678boot_read_status(struct boot_status *bs)
679{
680 const struct flash_area *fap;
David Vincze401c7422019-06-21 20:44:05 +0200681 uint32_t off;
David Vincze91b71ef2019-06-24 13:06:47 +0200682 uint8_t swap_info;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000683 int status_loc;
684 int area_id;
685 int rc;
686
David Vincze39e78552018-10-10 17:10:01 +0200687 memset(bs, 0, sizeof *bs);
688 bs->idx = BOOT_STATUS_IDX_0;
689 bs->state = BOOT_STATUS_STATE_0;
David Vincze401c7422019-06-21 20:44:05 +0200690 bs->swap_type = BOOT_SWAP_TYPE_NONE;
David Vincze39e78552018-10-10 17:10:01 +0200691
692#ifdef MCUBOOT_OVERWRITE_ONLY
693 /* Overwrite-only doesn't make use of the swap status area. */
694 return 0;
695#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000696
697 status_loc = boot_status_source();
698 switch (status_loc) {
699 case BOOT_STATUS_SOURCE_NONE:
700 return 0;
701
702 case BOOT_STATUS_SOURCE_SCRATCH:
703 area_id = FLASH_AREA_IMAGE_SCRATCH;
704 break;
705
David Vincze8bdfc2d2019-03-18 15:49:23 +0100706 case BOOT_STATUS_SOURCE_PRIMARY_SLOT:
707 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000708 break;
709
710 default:
711 assert(0);
712 return BOOT_EBADARGS;
713 }
714
715 rc = flash_area_open(area_id, &fap);
716 if (rc != 0) {
717 return BOOT_EFLASH;
718 }
719
720 rc = boot_read_status_bytes(fap, bs);
David Vincze401c7422019-06-21 20:44:05 +0200721 if (rc == 0) {
David Vincze91b71ef2019-06-24 13:06:47 +0200722 off = boot_swap_info_off(fap);
723 rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200724 if (rc == 1) {
David Vincze91b71ef2019-06-24 13:06:47 +0200725 BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
David Vincze401c7422019-06-21 20:44:05 +0200726 rc = 0;
727 }
David Vincze91b71ef2019-06-24 13:06:47 +0200728
729 /* Extract the swap type info */
730 bs->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
David Vincze401c7422019-06-21 20:44:05 +0200731 }
Tamas Banf70ef8c2017-12-19 15:35:09 +0000732
733 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200734
Tamas Banf70ef8c2017-12-19 15:35:09 +0000735 return rc;
736}
737
738/**
739 * Writes the supplied boot status to the flash file system. The boot status
740 * contains the current state of an in-progress image copy operation.
741 *
742 * @param bs The boot status to write.
743 *
744 * @return 0 on success; nonzero on failure.
745 */
746int
747boot_write_status(struct boot_status *bs)
748{
Tamas Ban581034a2017-12-19 19:54:37 +0000749 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000750 uint32_t off;
751 int area_id;
752 int rc;
753 uint8_t buf[BOOT_MAX_ALIGN];
754 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200755 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000756
757 /* NOTE: The first sector copied (that is the last sector on slot) contains
David Vincze8bdfc2d2019-03-18 15:49:23 +0100758 * the trailer. Since in the last step the primary slot is erased, the
759 * first two status writes go to the scratch which will be copied to
760 * the primary slot!
Tamas Banf70ef8c2017-12-19 15:35:09 +0000761 */
762
763 if (bs->use_scratch) {
764 /* Write to scratch. */
765 area_id = FLASH_AREA_IMAGE_SCRATCH;
766 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100767 /* Write to the primary slot. */
768 area_id = FLASH_AREA_IMAGE_PRIMARY;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000769 }
770
771 rc = flash_area_open(area_id, &fap);
772 if (rc != 0) {
773 rc = BOOT_EFLASH;
774 goto done;
775 }
776
777 off = boot_status_off(fap) +
778 boot_status_internal_off(bs->idx, bs->state,
779 BOOT_WRITE_SZ(&boot_data));
780
Tamas Banc3828852018-02-01 12:24:16 +0000781 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200782 erased_val = flash_area_erased_val(fap);
783 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000784 buf[0] = bs->state;
785
786 rc = flash_area_write(fap, off, buf, align);
787 if (rc != 0) {
788 rc = BOOT_EFLASH;
789 goto done;
790 }
791
792 rc = 0;
793
794done:
795 flash_area_close(fap);
796 return rc;
797}
798
Tamas Banf70ef8c2017-12-19 15:35:09 +0000799/**
800 * Determines which swap operation to perform, if any. If it is determined
David Vincze8bdfc2d2019-03-18 15:49:23 +0100801 * that a swap operation is required, the image in the secondary slot is checked
802 * for validity. If the image in the secondary slot is invalid, it is erased,
803 * and a swap type of "none" is indicated.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000804 *
805 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
806 */
807static int
David Vincze401c7422019-06-21 20:44:05 +0200808boot_validated_swap_type(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000809{
810 int swap_type;
811
812 swap_type = boot_swap_type();
813 switch (swap_type) {
814 case BOOT_SWAP_TYPE_TEST:
815 case BOOT_SWAP_TYPE_PERM:
816 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +0100817 /* Boot loader wants to switch to the secondary slot.
818 * Ensure image is valid.
819 */
David Vincze401c7422019-06-21 20:44:05 +0200820 if (boot_validate_slot(BOOT_SECONDARY_SLOT, bs) != 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000821 swap_type = BOOT_SWAP_TYPE_FAIL;
822 }
823 }
824
825 return swap_type;
826}
827
828/**
829 * Calculates the number of sectors the scratch area can contain. A "last"
830 * source sector is specified because images are copied backwards in flash
831 * (final index to index number 0).
832 *
833 * @param last_sector_idx The index of the last source sector
834 * (inclusive).
835 * @param out_first_sector_idx The index of the first source sector
836 * (inclusive) gets written here.
837 *
838 * @return The number of bytes comprised by the
839 * [first-sector, last-sector] range.
840 */
841#ifndef MCUBOOT_OVERWRITE_ONLY
842static uint32_t
843boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
844{
845 size_t scratch_sz;
846 uint32_t new_sz;
847 uint32_t sz;
848 int i;
849
850 sz = 0;
851
852 scratch_sz = boot_scratch_area_size(&boot_data);
853 for (i = last_sector_idx; i >= 0; i--) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100854 new_sz = sz + boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, i);
David Vincze401c7422019-06-21 20:44:05 +0200855 /*
856 * The secondary slot is not being checked here, because
857 * `boot_slots_compatible` already provides assurance that the copy size
858 * will be compatible with the primary slot and scratch.
859 */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000860 if (new_sz > scratch_sz) {
861 break;
862 }
863 sz = new_sz;
864 }
865
866 /* i currently refers to a sector that doesn't fit or it is -1 because all
867 * sectors have been processed. In both cases, exclude sector i.
868 */
869 *out_first_sector_idx = i + 1;
870 return sz;
871}
872#endif /* !MCUBOOT_OVERWRITE_ONLY */
873
874/**
David Vinczef7641fa2018-09-04 18:29:46 +0200875 * Erases a region of flash.
876 *
David Vincze401c7422019-06-21 20:44:05 +0200877 * @param flash_area The flash_area containing the region to erase.
David Vinczef7641fa2018-09-04 18:29:46 +0200878 * @param off The offset within the flash area to start the
879 * erase.
880 * @param sz The number of bytes to erase.
881 *
882 * @return 0 on success; nonzero on failure.
883 */
David Vincze401c7422019-06-21 20:44:05 +0200884static inline int
885boot_erase_sector(const struct flash_area *fap, uint32_t off, uint32_t sz)
David Vinczef7641fa2018-09-04 18:29:46 +0200886{
David Vincze401c7422019-06-21 20:44:05 +0200887 return flash_area_erase(fap, off, sz);
David Vinczef7641fa2018-09-04 18:29:46 +0200888}
889
890/**
Tamas Banf70ef8c2017-12-19 15:35:09 +0000891 * Copies the contents of one flash region to another. You must erase the
892 * destination region prior to calling this function.
893 *
894 * @param flash_area_id_src The ID of the source flash area.
895 * @param flash_area_id_dst The ID of the destination flash area.
896 * @param off_src The offset within the source flash area to
897 * copy from.
898 * @param off_dst The offset within the destination flash area to
899 * copy to.
900 * @param sz The number of bytes to copy.
901 *
902 * @return 0 on success; nonzero on failure.
903 */
904static int
David Vincze401c7422019-06-21 20:44:05 +0200905boot_copy_sector(const struct flash_area *fap_src,
906 const struct flash_area *fap_dst,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000907 uint32_t off_src, uint32_t off_dst, uint32_t sz)
908{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000909 uint32_t bytes_copied;
910 int chunk_sz;
911 int rc;
912
913 static uint8_t buf[1024];
914
Tamas Banf70ef8c2017-12-19 15:35:09 +0000915 bytes_copied = 0;
916 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +0000917 if (sz - bytes_copied > sizeof(buf)) {
918 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000919 } else {
920 chunk_sz = sz - bytes_copied;
921 }
922
923 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
924 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200925 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000926 }
927
928 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
929 if (rc != 0) {
David Vincze401c7422019-06-21 20:44:05 +0200930 return BOOT_EFLASH;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000931 }
932
933 bytes_copied += chunk_sz;
934 }
935
David Vincze401c7422019-06-21 20:44:05 +0200936 return 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000937}
938
939#ifndef MCUBOOT_OVERWRITE_ONLY
940static inline int
David Vincze401c7422019-06-21 20:44:05 +0200941boot_status_init(const struct flash_area *fap, const struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000942{
Tamas Banf70ef8c2017-12-19 15:35:09 +0000943 struct boot_swap_state swap_state;
944 int rc;
945
David Vincze401c7422019-06-21 20:44:05 +0200946 BOOT_LOG_DBG("initializing status; fa_id=%d", fap->fa_id);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000947
David Vincze8bdfc2d2019-03-18 15:49:23 +0100948 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SECONDARY, &swap_state);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000949 assert(rc == 0);
950
David Vincze401c7422019-06-21 20:44:05 +0200951 if (bs->swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +0200952 rc = boot_write_swap_info(fap,
953 bs->swap_type,
954 0);
David Vincze401c7422019-06-21 20:44:05 +0200955 assert(rc == 0);
956 }
957
Tamas Banf70ef8c2017-12-19 15:35:09 +0000958 if (swap_state.image_ok == BOOT_FLAG_SET) {
959 rc = boot_write_image_ok(fap);
960 assert(rc == 0);
961 }
962
963 rc = boot_write_swap_size(fap, bs->swap_size);
964 assert(rc == 0);
965
966 rc = boot_write_magic(fap);
967 assert(rc == 0);
968
Tamas Banf70ef8c2017-12-19 15:35:09 +0000969 return 0;
970}
David Vinczef7641fa2018-09-04 18:29:46 +0200971
972static int
David Vincze401c7422019-06-21 20:44:05 +0200973boot_erase_trailer_sectors(const struct flash_area *fap)
David Vinczef7641fa2018-09-04 18:29:46 +0200974{
975 uint8_t slot;
David Vincze401c7422019-06-21 20:44:05 +0200976 uint32_t sector;
977 uint32_t trailer_sz;
978 uint32_t total_sz;
979 uint32_t off;
980 uint32_t sz;
David Vinczebb207982019-08-21 15:13:04 +0200981 int fa_id_primary;
982 int fa_id_secondary;
David Vinczef7641fa2018-09-04 18:29:46 +0200983 int rc;
984
David Vincze401c7422019-06-21 20:44:05 +0200985 BOOT_LOG_DBG("erasing trailer; fa_id=%d", fap->fa_id);
986
David Vinczebb207982019-08-21 15:13:04 +0200987 fa_id_primary = flash_area_id_from_image_slot(BOOT_PRIMARY_SLOT);
988 fa_id_secondary = flash_area_id_from_image_slot(BOOT_SECONDARY_SLOT);
989
990 if (fap->fa_id == fa_id_primary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100991 slot = BOOT_PRIMARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +0200992 } else if (fap->fa_id == fa_id_secondary) {
David Vincze8bdfc2d2019-03-18 15:49:23 +0100993 slot = BOOT_SECONDARY_SLOT;
David Vinczebb207982019-08-21 15:13:04 +0200994 } else {
David Vinczef7641fa2018-09-04 18:29:46 +0200995 return BOOT_EFLASH;
996 }
997
David Vincze401c7422019-06-21 20:44:05 +0200998 /* delete starting from last sector and moving to beginning */
999 sector = boot_img_num_sectors(&boot_data, slot) - 1;
1000 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
1001 total_sz = 0;
1002 do {
1003 sz = boot_img_sector_size(&boot_data, slot, sector);
1004 off = boot_img_sector_off(&boot_data, slot, sector);
1005 rc = boot_erase_sector(fap, off, sz);
1006 assert(rc == 0);
1007
1008 sector--;
1009 total_sz += sz;
1010 } while (total_sz < trailer_sz);
David Vinczef7641fa2018-09-04 18:29:46 +02001011
1012 return rc;
1013}
1014#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001015
Tamas Banf70ef8c2017-12-19 15:35:09 +00001016/**
1017 * Swaps the contents of two flash regions within the two image slots.
1018 *
1019 * @param idx The index of the first sector in the range of
1020 * sectors being swapped.
1021 * @param sz The number of bytes to swap.
1022 * @param bs The current boot status. This struct gets
1023 * updated according to the outcome.
1024 *
1025 * @return 0 on success; nonzero on failure.
1026 */
1027#ifndef MCUBOOT_OVERWRITE_ONLY
1028static void
1029boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
1030{
David Vincze401c7422019-06-21 20:44:05 +02001031 const struct flash_area *fap_primary_slot;
1032 const struct flash_area *fap_secondary_slot;
1033 const struct flash_area *fap_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001034 uint32_t copy_sz;
1035 uint32_t trailer_sz;
1036 uint32_t img_off;
1037 uint32_t scratch_trailer_off;
1038 struct boot_swap_state swap_state;
1039 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001040 bool erase_scratch;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001041 int rc;
1042
1043 /* Calculate offset from start of image area. */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001044 img_off = boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT, idx);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001045
1046 copy_sz = sz;
David Vincze401c7422019-06-21 20:44:05 +02001047 trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(&boot_data));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001048
David Vincze401c7422019-06-21 20:44:05 +02001049 /* sz in this function is always sized on a multiple of the sector size.
1050 * The check against the start offset of the last sector
Tamas Banf70ef8c2017-12-19 15:35:09 +00001051 * is to determine if we're swapping the last sector. The last sector
1052 * needs special handling because it's where the trailer lives. If we're
1053 * copying it, we need to use scratch to write the trailer temporarily.
1054 *
1055 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
1056 * controls if special handling is needed (swapping last sector).
1057 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001058 last_sector = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT) - 1;
1059 if (img_off + sz > boot_img_sector_off(&boot_data, BOOT_PRIMARY_SLOT,
1060 last_sector)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001061 copy_sz -= trailer_sz;
1062 }
1063
David Vincze39e78552018-10-10 17:10:01 +02001064 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001065
David Vincze401c7422019-06-21 20:44:05 +02001066 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1067 assert (rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001068
David Vincze401c7422019-06-21 20:44:05 +02001069 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1070 assert (rc == 0);
1071
1072 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap_scratch);
1073 assert (rc == 0);
1074
1075 if (bs->state == BOOT_STATUS_STATE_0) {
1076 BOOT_LOG_DBG("erasing scratch area");
1077 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001078 assert(rc == 0);
1079
David Vincze39e78552018-10-10 17:10:01 +02001080 if (bs->idx == BOOT_STATUS_IDX_0) {
David Vincze401c7422019-06-21 20:44:05 +02001081 /* Write a trailer to the scratch area, even if we don't need the
1082 * scratch area for status. We need a temporary place to store the
1083 * `swap-type` while we erase the primary trailer.
1084 */
1085 rc = boot_status_init(fap_scratch, bs);
1086 assert(rc == 0);
1087
1088 if (!bs->use_scratch) {
1089 /* Prepare the primary status area... here it is known that the
1090 * last sector is not being used by the image data so it's safe
1091 * to erase.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001092 */
David Vincze401c7422019-06-21 20:44:05 +02001093 rc = boot_erase_trailer_sectors(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001094 assert(rc == 0);
1095
David Vincze401c7422019-06-21 20:44:05 +02001096 rc = boot_status_init(fap_primary_slot, bs);
1097 assert(rc == 0);
1098
1099 /* Erase the temporary trailer from the scratch area. */
1100 rc = boot_erase_sector(fap_scratch, 0, fap_scratch->fa_size);
1101 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001102 }
1103 }
1104
David Vincze401c7422019-06-21 20:44:05 +02001105 rc = boot_copy_sector(fap_secondary_slot, fap_scratch,
1106 img_off, 0, copy_sz);
1107 assert(rc == 0);
1108
David Vincze39e78552018-10-10 17:10:01 +02001109 bs->state = BOOT_STATUS_STATE_1;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001110 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001111 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001112 }
1113
David Vincze39e78552018-10-10 17:10:01 +02001114 if (bs->state == BOOT_STATUS_STATE_1) {
David Vincze401c7422019-06-21 20:44:05 +02001115 rc = boot_erase_sector(fap_secondary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001116 assert(rc == 0);
1117
David Vincze401c7422019-06-21 20:44:05 +02001118 rc = boot_copy_sector(fap_primary_slot, fap_secondary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001119 img_off, img_off, copy_sz);
1120 assert(rc == 0);
1121
David Vincze39e78552018-10-10 17:10:01 +02001122 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001123 /* If not all sectors of the slot are being swapped,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001124 * guarantee here that only the primary slot will have the state.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001125 */
David Vincze401c7422019-06-21 20:44:05 +02001126 rc = boot_erase_trailer_sectors(fap_secondary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001127 assert(rc == 0);
1128 }
1129
David Vincze39e78552018-10-10 17:10:01 +02001130 bs->state = BOOT_STATUS_STATE_2;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001131 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001132 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001133 }
1134
David Vincze39e78552018-10-10 17:10:01 +02001135 if (bs->state == BOOT_STATUS_STATE_2) {
David Vincze401c7422019-06-21 20:44:05 +02001136 rc = boot_erase_sector(fap_primary_slot, img_off, sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001137 assert(rc == 0);
1138
David Vincze401c7422019-06-21 20:44:05 +02001139 /* NOTE: If this is the final sector, we exclude the image trailer from
1140 * this copy (copy_sz was truncated earlier).
1141 */
1142 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001143 0, img_off, copy_sz);
1144 assert(rc == 0);
1145
1146 if (bs->use_scratch) {
David Vincze401c7422019-06-21 20:44:05 +02001147 scratch_trailer_off = boot_status_off(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001148
1149 /* copy current status that is being maintained in scratch */
David Vincze401c7422019-06-21 20:44:05 +02001150 rc = boot_copy_sector(fap_scratch, fap_primary_slot,
1151 scratch_trailer_off, img_off + copy_sz,
Tamas Banf70ef8c2017-12-19 15:35:09 +00001152 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
David Vincze39e78552018-10-10 17:10:01 +02001153 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001154
1155 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1156 &swap_state);
1157 assert(rc == 0);
1158
1159 if (swap_state.image_ok == BOOT_FLAG_SET) {
David Vincze401c7422019-06-21 20:44:05 +02001160 rc = boot_write_image_ok(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001161 assert(rc == 0);
1162 }
1163
David Vincze401c7422019-06-21 20:44:05 +02001164 if (swap_state.swap_type != BOOT_SWAP_TYPE_NONE) {
David Vincze91b71ef2019-06-24 13:06:47 +02001165 rc = boot_write_swap_info(fap_primary_slot,
1166 swap_state.swap_type,
1167 0);
David Vincze401c7422019-06-21 20:44:05 +02001168 assert(rc == 0);
1169 }
1170
1171 rc = boot_write_swap_size(fap_primary_slot, bs->swap_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001172 assert(rc == 0);
1173
David Vincze401c7422019-06-21 20:44:05 +02001174 rc = boot_write_magic(fap_primary_slot);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001175 assert(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001176 }
1177
David Vincze401c7422019-06-21 20:44:05 +02001178 /* If we wrote a trailer to the scratch area, erase it after we persist
1179 * a trailer to the primary slot. We do this to prevent mcuboot from
1180 * reading a stale status from the scratch area in case of immediate
1181 * reset.
1182 */
1183 erase_scratch = bs->use_scratch;
1184 bs->use_scratch = 0;
1185
Tamas Banf70ef8c2017-12-19 15:35:09 +00001186 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001187 bs->state = BOOT_STATUS_STATE_0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001188 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001189 BOOT_STATUS_ASSERT(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001190
1191 if (erase_scratch) {
1192 rc = boot_erase_sector(fap_scratch, 0, sz);
1193 assert(rc == 0);
1194 }
Tamas Banf70ef8c2017-12-19 15:35:09 +00001195 }
David Vincze401c7422019-06-21 20:44:05 +02001196
1197 flash_area_close(fap_primary_slot);
1198 flash_area_close(fap_secondary_slot);
1199 flash_area_close(fap_scratch);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001200}
1201#endif /* !MCUBOOT_OVERWRITE_ONLY */
1202
1203/**
David Vincze401c7422019-06-21 20:44:05 +02001204 * Overwrite primary slot with the image contained in the secondary slot.
1205 * If a prior copy operation was interrupted by a system reset, this function
1206 * redos the copy.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001207 *
1208 * @param bs The current boot status. This function reads
1209 * this struct to determine if it is resuming
1210 * an interrupted swap operation. This
1211 * function writes the updated status to this
1212 * function on return.
1213 *
1214 * @return 0 on success; nonzero on failure.
1215 */
1216#ifdef MCUBOOT_OVERWRITE_ONLY
1217static int
1218boot_copy_image(struct boot_status *bs)
1219{
1220 size_t sect_count;
1221 size_t sect;
1222 int rc;
1223 size_t size = 0;
1224 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001225 size_t last_sector;
David Vincze401c7422019-06-21 20:44:05 +02001226 const struct flash_area *fap_primary_slot;
1227 const struct flash_area *fap_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001228
1229 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001230
David Vincze8bdfc2d2019-03-18 15:49:23 +01001231 BOOT_LOG_INF("Image upgrade secondary slot -> primary slot");
1232 BOOT_LOG_INF("Erasing the primary slot");
Tamas Banf70ef8c2017-12-19 15:35:09 +00001233
David Vincze401c7422019-06-21 20:44:05 +02001234 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap_primary_slot);
1235 assert (rc == 0);
1236
1237 rc = flash_area_open(FLASH_AREA_IMAGE_SECONDARY, &fap_secondary_slot);
1238 assert (rc == 0);
1239
David Vincze8bdfc2d2019-03-18 15:49:23 +01001240 sect_count = boot_img_num_sectors(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001241 for (sect = 0; sect < sect_count; sect++) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001242 this_size = boot_img_sector_size(&boot_data, BOOT_PRIMARY_SLOT, sect);
David Vincze401c7422019-06-21 20:44:05 +02001243 rc = boot_erase_sector(fap_primary_slot, size, this_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001244 assert(rc == 0);
1245
1246 size += this_size;
1247 }
1248
David Vincze8bdfc2d2019-03-18 15:49:23 +01001249 BOOT_LOG_INF("Copying the secondary slot to the primary slot: 0x%zx bytes",
1250 size);
David Vincze401c7422019-06-21 20:44:05 +02001251 rc = boot_copy_sector(fap_secondary_slot, fap_primary_slot, 0, 0, size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001252
David Vincze060968d2019-05-23 01:13:14 +02001253 /* Update the stored security counter with the new image's security counter
David Vincze8bdfc2d2019-03-18 15:49:23 +01001254 * value. Both slots hold the new image at this point, but the secondary
1255 * slot's image header must be passed because the read image headers in the
1256 * boot_data structure have not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001257 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001258 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1259 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001260 if (rc != 0) {
1261 BOOT_LOG_ERR("Security counter update failed after image upgrade.");
1262 return rc;
1263 }
1264
David Vincze39e78552018-10-10 17:10:01 +02001265 /*
1266 * Erases header and trailer. The trailer is erased because when a new
1267 * image is written without a trailer as is the case when using newt, the
1268 * trailer that was left might trigger a new upgrade.
1269 */
David Vincze401c7422019-06-21 20:44:05 +02001270 BOOT_LOG_DBG("erasing secondary header");
1271 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001272 boot_img_sector_off(&boot_data,
1273 BOOT_SECONDARY_SLOT, 0),
1274 boot_img_sector_size(&boot_data,
1275 BOOT_SECONDARY_SLOT, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001276 assert(rc == 0);
David Vincze8bdfc2d2019-03-18 15:49:23 +01001277 last_sector = boot_img_num_sectors(&boot_data, BOOT_SECONDARY_SLOT) - 1;
David Vincze401c7422019-06-21 20:44:05 +02001278 BOOT_LOG_DBG("erasing secondary trailer");
1279 rc = boot_erase_sector(fap_secondary_slot,
David Vincze8bdfc2d2019-03-18 15:49:23 +01001280 boot_img_sector_off(&boot_data, BOOT_SECONDARY_SLOT,
1281 last_sector),
1282 boot_img_sector_size(&boot_data, BOOT_SECONDARY_SLOT,
1283 last_sector));
David Vincze39e78552018-10-10 17:10:01 +02001284 assert(rc == 0);
1285
David Vincze401c7422019-06-21 20:44:05 +02001286 flash_area_close(fap_primary_slot);
1287 flash_area_close(fap_secondary_slot);
1288
David Vincze8bdfc2d2019-03-18 15:49:23 +01001289 /* TODO: Perhaps verify the primary slot's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001290
1291 return 0;
1292}
1293#else
David Vincze401c7422019-06-21 20:44:05 +02001294/**
1295 * Swaps the two images in flash. If a prior copy operation was interrupted
1296 * by a system reset, this function completes that operation.
1297 *
1298 * @param bs The current boot status. This function reads
1299 * this struct to determine if it is resuming
1300 * an interrupted swap operation. This
1301 * function writes the updated status to this
1302 * function on return.
1303 *
1304 * @return 0 on success; nonzero on failure.
1305 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001306static int
David Vincze401c7422019-06-21 20:44:05 +02001307boot_swap_image(struct boot_status *bs)
Tamas Banf70ef8c2017-12-19 15:35:09 +00001308{
1309 uint32_t sz;
1310 int first_sector_idx;
1311 int last_sector_idx;
David Vincze401c7422019-06-21 20:44:05 +02001312 int last_idx_secondary_slot;
David Vincze39e78552018-10-10 17:10:01 +02001313 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001314 struct image_header *hdr;
1315 uint32_t size;
1316 uint32_t copy_size;
David Vincze401c7422019-06-21 20:44:05 +02001317 uint32_t primary_slot_size;
1318 uint32_t secondary_slot_size;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001319 int rc;
1320
1321 /* FIXME: just do this if asked by user? */
1322
1323 size = copy_size = 0;
1324
David Vincze39e78552018-10-10 17:10:01 +02001325 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001326 /*
1327 * No swap ever happened, so need to find the largest image which
1328 * will be used to determine the amount of sectors to swap.
1329 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001330 hdr = boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001331 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001332 rc = boot_read_image_size(BOOT_PRIMARY_SLOT, hdr, &copy_size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001333 assert(rc == 0);
1334 }
1335
David Vincze8bdfc2d2019-03-18 15:49:23 +01001336 hdr = boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001337 if (hdr->ih_magic == IMAGE_MAGIC) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001338 rc = boot_read_image_size(BOOT_SECONDARY_SLOT, hdr, &size);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001339 assert(rc == 0);
1340 }
1341
1342 if (size > copy_size) {
1343 copy_size = size;
1344 }
1345
1346 bs->swap_size = copy_size;
1347 } else {
1348 /*
1349 * If a swap was under way, the swap_size should already be present
1350 * in the trailer...
1351 */
1352 rc = boot_read_swap_size(&bs->swap_size);
1353 assert(rc == 0);
1354
1355 copy_size = bs->swap_size;
1356 }
1357
David Vincze401c7422019-06-21 20:44:05 +02001358 primary_slot_size = 0;
1359 secondary_slot_size = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001360 last_sector_idx = 0;
David Vincze401c7422019-06-21 20:44:05 +02001361 last_idx_secondary_slot = 0;
1362
1363 /*
1364 * Knowing the size of the largest image between both slots, here we
1365 * find what is the last sector in the primary slot that needs swapping.
1366 * Since we already know that both slots are compatible, the secondary
1367 * slot's last sector is not really required after this check is finished.
1368 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001369 while (1) {
David Vincze401c7422019-06-21 20:44:05 +02001370 if ((primary_slot_size < copy_size) ||
1371 (primary_slot_size < secondary_slot_size)) {
1372 primary_slot_size += boot_img_sector_size(&boot_data,
1373 BOOT_PRIMARY_SLOT,
1374 last_sector_idx);
1375 }
1376 if ((secondary_slot_size < copy_size) ||
1377 (secondary_slot_size < primary_slot_size)) {
1378 secondary_slot_size += boot_img_sector_size(&boot_data,
1379 BOOT_SECONDARY_SLOT,
1380 last_idx_secondary_slot);
1381 }
1382 if (primary_slot_size >= copy_size &&
1383 secondary_slot_size >= copy_size &&
1384 primary_slot_size == secondary_slot_size) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001385 break;
1386 }
1387 last_sector_idx++;
David Vincze401c7422019-06-21 20:44:05 +02001388 last_idx_secondary_slot++;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001389 }
1390
1391 swap_idx = 0;
1392 while (last_sector_idx >= 0) {
1393 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001394 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001395 boot_swap_sectors(first_sector_idx, sz, bs);
1396 }
1397
1398 last_sector_idx = first_sector_idx - 1;
1399 swap_idx++;
1400 }
1401
David Vincze8bdfc2d2019-03-18 15:49:23 +01001402#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze39e78552018-10-10 17:10:01 +02001403 if (boot_status_fails > 0) {
David Vincze401c7422019-06-21 20:44:05 +02001404 BOOT_LOG_WRN("%d status write fails performing the swap",
1405 boot_status_fails);
David Vincze39e78552018-10-10 17:10:01 +02001406 }
1407#endif
1408
Tamas Banf70ef8c2017-12-19 15:35:09 +00001409 return 0;
1410}
1411#endif
1412
1413/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001414 * Marks the image in the primary slot as fully copied.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001415 */
1416#ifndef MCUBOOT_OVERWRITE_ONLY
1417static int
1418boot_set_copy_done(void)
1419{
1420 const struct flash_area *fap;
1421 int rc;
1422
David Vincze8bdfc2d2019-03-18 15:49:23 +01001423 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001424 if (rc != 0) {
1425 return BOOT_EFLASH;
1426 }
1427
1428 rc = boot_write_copy_done(fap);
1429 flash_area_close(fap);
1430 return rc;
1431}
1432#endif /* !MCUBOOT_OVERWRITE_ONLY */
1433
1434/**
David Vincze8bdfc2d2019-03-18 15:49:23 +01001435 * Marks a reverted image in the primary slot as confirmed. This is necessary to
1436 * ensure the status bytes from the image revert operation don't get processed
1437 * on a subsequent boot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001438 *
1439 * NOTE: image_ok is tested before writing because if there's a valid permanent
David Vincze8bdfc2d2019-03-18 15:49:23 +01001440 * image installed on the primary slot and the new image to be upgrade to has a
1441 * bad sig, image_ok would be overwritten.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001442 */
1443#ifndef MCUBOOT_OVERWRITE_ONLY
1444static int
1445boot_set_image_ok(void)
1446{
1447 const struct flash_area *fap;
1448 struct boot_swap_state state;
1449 int rc;
1450
David Vincze8bdfc2d2019-03-18 15:49:23 +01001451 rc = flash_area_open(FLASH_AREA_IMAGE_PRIMARY, &fap);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001452 if (rc != 0) {
1453 return BOOT_EFLASH;
1454 }
1455
1456 rc = boot_read_swap_state(fap, &state);
1457 if (rc != 0) {
1458 rc = BOOT_EFLASH;
1459 goto out;
1460 }
1461
1462 if (state.image_ok == BOOT_FLAG_UNSET) {
1463 rc = boot_write_image_ok(fap);
1464 }
1465
1466out:
1467 flash_area_close(fap);
1468 return rc;
1469}
1470#endif /* !MCUBOOT_OVERWRITE_ONLY */
1471
1472/**
1473 * Performs an image swap if one is required.
1474 *
1475 * @param out_swap_type On success, the type of swap performed gets
1476 * written here.
1477 *
1478 * @return 0 on success; nonzero on failure.
1479 */
1480static int
1481boot_swap_if_needed(int *out_swap_type)
1482{
1483 struct boot_status bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001484 int rc;
1485
1486 /* Determine if we rebooted in the middle of an image swap
1487 * operation.
1488 */
1489 rc = boot_read_status(&bs);
1490 assert(rc == 0);
1491 if (rc != 0) {
1492 return rc;
1493 }
1494
1495 /* If a partial swap was detected, complete it. */
David Vincze39e78552018-10-10 17:10:01 +02001496 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
David Vincze401c7422019-06-21 20:44:05 +02001497#ifdef MCUBOOT_OVERWRITE_ONLY
1498 /* Should never arrive here, overwrite-only mode has no swap state. */
1499 assert(0);
1500#else
1501 /* Determine the type of swap operation being resumed from the
1502 * `swap-type` trailer field.
1503 */
1504 rc = boot_swap_image(&bs);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001505 assert(rc == 0);
David Vincze401c7422019-06-21 20:44:05 +02001506#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001507
Tamas Banf70ef8c2017-12-19 15:35:09 +00001508 } else {
David Vincze401c7422019-06-21 20:44:05 +02001509 if (bs.swap_type == BOOT_SWAP_TYPE_NONE) {
1510 bs.swap_type = boot_validated_swap_type(&bs);
1511 } else if (boot_validate_slot(BOOT_SECONDARY_SLOT, &bs) != 0) {
1512 bs.swap_type = BOOT_SWAP_TYPE_FAIL;
1513 }
1514 switch (bs.swap_type) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001515 case BOOT_SWAP_TYPE_TEST:
1516 case BOOT_SWAP_TYPE_PERM:
1517 case BOOT_SWAP_TYPE_REVERT:
David Vincze401c7422019-06-21 20:44:05 +02001518#ifdef MCUBOOT_OVERWRITE_ONLY
Tamas Banf70ef8c2017-12-19 15:35:09 +00001519 rc = boot_copy_image(&bs);
David Vincze401c7422019-06-21 20:44:05 +02001520#else
1521 rc = boot_swap_image(&bs);
1522#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +00001523 assert(rc == 0);
1524 break;
1525 }
1526 }
1527
David Vincze401c7422019-06-21 20:44:05 +02001528 *out_swap_type = bs.swap_type;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001529 return 0;
1530}
1531
1532/**
1533 * Prepares the booting process. This function moves images around in flash as
1534 * appropriate, and tells you what address to boot from.
1535 *
1536 * @param rsp On success, indicates how booting should occur.
1537 *
1538 * @return 0 on success; nonzero on failure.
1539 */
1540int
1541boot_go(struct boot_rsp *rsp)
1542{
1543 int swap_type;
1544 size_t slot;
1545 int rc;
1546 int fa_id;
1547 bool reload_headers = false;
1548
1549 /* The array of slot sectors are defined here (as opposed to file scope) so
1550 * that they don't get allocated for non-boot-loader apps. This is
1551 * necessary because the gcc option "-fdata-sections" doesn't seem to have
1552 * any effect in older gcc versions (e.g., 4.8.4).
1553 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001554 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1555 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze401c7422019-06-21 20:44:05 +02001556 static boot_sector_t scratch_sectors[BOOT_MAX_IMG_SECTORS];
David Vincze8bdfc2d2019-03-18 15:49:23 +01001557 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = primary_slot_sectors;
1558 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = secondary_slot_sectors;
David Vincze401c7422019-06-21 20:44:05 +02001559 boot_data.scratch.sectors = scratch_sectors;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001560
1561 /* Open boot_data image areas for the duration of this call. */
1562 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1563 fa_id = flash_area_id_from_image_slot(slot);
1564 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1565 assert(rc == 0);
1566 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001567
Tamas Banf70ef8c2017-12-19 15:35:09 +00001568 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1569 &BOOT_SCRATCH_AREA(&boot_data));
1570 assert(rc == 0);
1571
1572 /* Determine the sector layout of the image slots and scratch area. */
1573 rc = boot_read_sectors();
1574 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001575 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1576 BOOT_MAX_IMG_SECTORS);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001577 goto out;
1578 }
1579
1580 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001581 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001582 if (rc != 0) {
1583 goto out;
1584 }
1585
1586 /* If the image slots aren't compatible, no swap is possible. Just boot
David Vincze8bdfc2d2019-03-18 15:49:23 +01001587 * into the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001588 */
1589 if (boot_slots_compatible()) {
1590 rc = boot_swap_if_needed(&swap_type);
1591 assert(rc == 0);
1592 if (rc != 0) {
1593 goto out;
1594 }
1595
1596 /*
1597 * The following states need image_ok be explicitly set after the
1598 * swap was finished to avoid a new revert.
1599 */
Tamas Ban581034a2017-12-19 19:54:37 +00001600 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
David Vincze401c7422019-06-21 20:44:05 +02001601 swap_type == BOOT_SWAP_TYPE_FAIL ||
1602 swap_type == BOOT_SWAP_TYPE_PERM) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001603#ifndef MCUBOOT_OVERWRITE_ONLY
1604 rc = boot_set_image_ok();
1605 if (rc != 0) {
1606 swap_type = BOOT_SWAP_TYPE_PANIC;
1607 }
1608#endif /* !MCUBOOT_OVERWRITE_ONLY */
1609 }
1610 } else {
1611 swap_type = BOOT_SWAP_TYPE_NONE;
1612 }
1613
1614 switch (swap_type) {
1615 case BOOT_SWAP_TYPE_NONE:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001616 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001617 break;
1618
1619 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1620 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1621 case BOOT_SWAP_TYPE_REVERT:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001622 slot = BOOT_SECONDARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001623 reload_headers = true;
1624#ifndef MCUBOOT_OVERWRITE_ONLY
David Vincze060968d2019-05-23 01:13:14 +02001625 if (swap_type == BOOT_SWAP_TYPE_PERM) {
1626 /* Update the stored security counter with the new image's security
David Vincze8bdfc2d2019-03-18 15:49:23 +01001627 * counter value. The primary slot holds the new image at this
1628 * point, but the secondary slot's image header must be passed
1629 * because the read image headers in the boot_data structure have
1630 * not been updated yet.
David Vincze060968d2019-05-23 01:13:14 +02001631 *
1632 * In case of a permanent image swap mcuboot will never attempt to
1633 * revert the images on the next reboot. Therefore, the security
1634 * counter must be increased right after the image upgrade.
1635 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001636 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1637 boot_img_hdr(&boot_data, BOOT_SECONDARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001638 if (rc != 0) {
1639 BOOT_LOG_ERR("Security counter update failed after "
1640 "image upgrade.");
1641 goto out;
1642 }
1643 }
1644
Tamas Banf70ef8c2017-12-19 15:35:09 +00001645 rc = boot_set_copy_done();
1646 if (rc != 0) {
1647 swap_type = BOOT_SWAP_TYPE_PANIC;
1648 }
1649#endif /* !MCUBOOT_OVERWRITE_ONLY */
1650 break;
1651
1652 case BOOT_SWAP_TYPE_FAIL:
David Vincze8bdfc2d2019-03-18 15:49:23 +01001653 /* The image in the secondary slot was invalid and is now erased.
1654 * Ensure we don't try to boot into it again on the next reboot.
1655 * Do this by pretending we just reverted back to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001656 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001657 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001658 reload_headers = true;
1659 break;
1660
1661 default:
1662 swap_type = BOOT_SWAP_TYPE_PANIC;
1663 }
1664
1665 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1666 BOOT_LOG_ERR("panic!");
1667 assert(0);
1668
1669 /* Loop forever... */
David Vincze39e78552018-10-10 17:10:01 +02001670 while (1) {}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001671 }
1672
Tamas Banf70ef8c2017-12-19 15:35:09 +00001673 if (reload_headers) {
David Vincze39e78552018-10-10 17:10:01 +02001674 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001675 if (rc != 0) {
1676 goto out;
1677 }
1678 /* Since headers were reloaded, it can be assumed we just performed a
1679 * swap or overwrite. Now the header info that should be used to
David Vincze8bdfc2d2019-03-18 15:49:23 +01001680 * provide the data for the bootstrap, which previously was at the
1681 * secondary slot, was updated to the primary slot.
Tamas Banf70ef8c2017-12-19 15:35:09 +00001682 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001683 slot = BOOT_PRIMARY_SLOT;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001684 }
1685
David Vincze8bdfc2d2019-03-18 15:49:23 +01001686#ifdef MCUBOOT_VALIDATE_PRIMARY_SLOT
David Vincze401c7422019-06-21 20:44:05 +02001687 rc = boot_validate_slot(BOOT_PRIMARY_SLOT, NULL);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001688 if (rc != 0) {
1689 rc = BOOT_EBADIMAGE;
1690 goto out;
1691 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001692#else
1693 /* Even if we're not re-validating the primary slot, we could be booting
David Vincze39e78552018-10-10 17:10:01 +02001694 * onto an empty flash chip. At least do a basic sanity check that
1695 * the magic number on the image is OK.
1696 */
David Vincze8bdfc2d2019-03-18 15:49:23 +01001697 if (boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic != IMAGE_MAGIC) {
1698 BOOT_LOG_ERR("bad image magic 0x%lx",
1699 (unsigned long)boot_data.imgs[BOOT_PRIMARY_SLOT].hdr.ih_magic);
David Vincze39e78552018-10-10 17:10:01 +02001700 rc = BOOT_EBADIMAGE;
1701 goto out;
1702 }
David Vincze8bdfc2d2019-03-18 15:49:23 +01001703#endif /* MCUBOOT_VALIDATE_PRIMARY_SLOT */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001704
David Vincze060968d2019-05-23 01:13:14 +02001705 /* Update the stored security counter with the active image's security
1706 * counter value. It will be updated only if the new security counter is
1707 * greater than the stored value.
1708 *
1709 * In case of a successful image swapping when the swap type is TEST the
1710 * security counter can be increased only after a reset, when the swap type
1711 * is NONE and the image has marked itself "OK" (the image_ok flag has been
1712 * set). This way a "revert" swap can be performed if it's necessary.
1713 */
1714 if (swap_type == BOOT_SWAP_TYPE_NONE) {
David Vincze8bdfc2d2019-03-18 15:49:23 +01001715 rc = boot_update_security_counter(BOOT_PRIMARY_SLOT,
1716 boot_img_hdr(&boot_data, BOOT_PRIMARY_SLOT));
David Vincze060968d2019-05-23 01:13:14 +02001717 if (rc != 0) {
1718 BOOT_LOG_ERR("Security counter update failed after image "
1719 "validation.");
1720 goto out;
1721 }
1722 }
1723
Tamas Banf70ef8c2017-12-19 15:35:09 +00001724 /* Always boot from the primary slot. */
David Vincze401c7422019-06-21 20:44:05 +02001725 rsp->br_flash_dev_id = boot_data.imgs[BOOT_PRIMARY_SLOT].area->fa_device_id;
David Vincze8bdfc2d2019-03-18 15:49:23 +01001726 rsp->br_image_off = boot_img_slot_off(&boot_data, BOOT_PRIMARY_SLOT);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001727 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
1728
Tamas Ban0e8ab302019-01-17 11:45:31 +00001729 /* Save boot status to shared memory area */
1730 rc = boot_save_boot_status(SW_S_NS,
1731 rsp->br_hdr,
1732 BOOT_IMG_AREA(&boot_data, slot));
1733 if (rc) {
1734 BOOT_LOG_ERR("Failed to add data to shared area");
1735 }
1736
Tamas Banf70ef8c2017-12-19 15:35:09 +00001737 out:
1738 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1739 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1740 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1741 }
1742 return rc;
1743}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001744
Oliver Swedef9982442018-08-24 18:37:44 +01001745#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001746
David Vinczedcba70b2019-05-28 12:02:52 +02001747#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
1748 BOOT_LOG_INF("Image %u: version=%u.%u.%u+%u, magic=%5s, image_ok=0x%x", \
1749 (area), \
1750 (hdr)->ih_ver.iv_major, \
1751 (hdr)->ih_ver.iv_minor, \
1752 (hdr)->ih_ver.iv_revision, \
1753 (hdr)->ih_ver.iv_build_num, \
1754 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
1755 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
1756 "bad"), \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001757 (state)->image_ok)
1758
1759struct image_slot_version {
1760 uint64_t version;
1761 uint32_t slot_number;
1762};
1763
1764/**
1765 * Extract the version number from the image header. This function must be
1766 * ported if version number format has changed in the image header.
1767 *
1768 * @param hdr Pointer to an image header structure
1769 *
Oliver Swedef9982442018-08-24 18:37:44 +01001770 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001771 */
1772static uint64_t
1773boot_get_version_number(struct image_header *hdr)
1774{
Oliver Swedef9982442018-08-24 18:37:44 +01001775 uint64_t version = 0;
1776 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
1777 + IMAGE_VER_REVISION_LENGTH
1778 + IMAGE_VER_BUILD_NUM_LENGTH);
1779 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
1780 + IMAGE_VER_BUILD_NUM_LENGTH);
1781 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
1782 version |= hdr->ih_ver.iv_build_num;
1783 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001784}
1785
1786/**
1787 * Comparator function for `qsort` to compare version numbers. This function
1788 * must be ported if version number format has changed in the image header.
1789 *
1790 * @param ver1 Pointer to an array element which holds the version number
1791 * @param ver2 Pointer to another array element which holds the version
1792 * number
1793 *
1794 * @return if version1 > version2 -1
1795 * if version1 == version2 0
1796 * if version1 < version2 1
1797 */
1798static int
1799boot_compare_version_numbers(const void *ver1, const void *ver2)
1800{
1801 if (((struct image_slot_version *)ver1)->version <
1802 ((struct image_slot_version *)ver2)->version) {
1803 return 1;
1804 }
1805
1806 if (((struct image_slot_version *)ver1)->version ==
1807 ((struct image_slot_version *)ver2)->version) {
1808 return 0;
1809 }
1810
1811 return -1;
1812}
1813
1814/**
1815 * Sort the available images based on the version number and puts them in
1816 * a list.
1817 *
1818 * @param boot_sequence A pointer to an array, whose aim is to carry
1819 * the boot order of candidate images.
1820 * @param slot_cnt The number of flash areas, which can contains firmware
1821 * images.
1822 *
1823 * @return The number of valid images.
1824 */
1825uint32_t
1826boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
1827{
1828 struct boot_swap_state slot_state;
1829 struct image_header *hdr;
1830 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
1831 uint32_t image_cnt = 0;
1832 uint32_t slot;
1833 int32_t rc;
1834 int32_t fa_id;
1835
1836 for (slot = 0; slot < slot_cnt; slot++) {
1837 hdr = boot_img_hdr(&boot_data, slot);
1838 fa_id = flash_area_id_from_image_slot(slot);
1839 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
1840 if (rc != 0) {
David Vinczedcba70b2019-05-28 12:02:52 +02001841 BOOT_LOG_ERR("Error during reading image trailer from slot: %u",
1842 slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001843 continue;
1844 }
1845
1846 if (hdr->ih_magic == IMAGE_MAGIC) {
1847 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02001848 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001849 /* Valid cases:
1850 * - Test mode: magic is OK in image trailer
1851 * - Permanent mode: image_ok flag has previously set
1852 */
1853 image_versions[slot].slot_number = slot;
1854 image_versions[slot].version = boot_get_version_number(hdr);
1855 image_cnt++;
1856 }
1857
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001858 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
1859 } else {
David Vinczedcba70b2019-05-28 12:02:52 +02001860 BOOT_LOG_INF("Image %u: No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001861 }
1862 }
1863
1864 /* Sort the images based on version number */
1865 qsort(&image_versions[0],
1866 slot_cnt,
1867 sizeof(struct image_slot_version),
1868 boot_compare_version_numbers);
1869
1870 /* Copy the calculated boot sequence to boot_sequence array */
1871 for (slot = 0; slot < slot_cnt; slot++) {
1872 boot_sequence[slot] = image_versions[slot].slot_number;
1873 }
1874
1875 return image_cnt;
1876}
1877
Oliver Swedef9982442018-08-24 18:37:44 +01001878#ifdef MCUBOOT_RAM_LOADING
1879/**
1880 * Copies an image from a slot in the flash to an SRAM address, where the load
1881 * address has already been inserted into the image header by this point and is
1882 * extracted from it within this method. The copying is done sector-by-sector.
1883 *
1884 * @param slot The flash slot of the image to be copied to SRAM.
1885 *
1886 * @param hdr Pointer to the image header structure of the image
1887 * that needs to be copid to SRAM
1888 *
1889 * @return 0 on success; nonzero on failure.
1890 */
1891static int
1892boot_copy_image_to_sram(int slot, struct image_header *hdr)
1893{
1894 int rc;
1895 uint32_t sect_sz;
1896 uint32_t sect = 0;
1897 uint32_t bytes_copied = 0;
1898 const struct flash_area *fap_src = NULL;
1899 uint32_t dst = (uint32_t) hdr->ih_load_addr;
1900 uint32_t img_sz;
1901
1902 if (dst % 4 != 0) {
Tamas Banc27b5c32019-05-28 16:30:19 +01001903 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%x "
Oliver Swedef9982442018-08-24 18:37:44 +01001904 "- the load address must be aligned with 4 bytes due to SRAM "
1905 "restrictions", dst);
1906 return BOOT_EBADARGS;
1907 }
1908
1909 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
1910 if (rc != 0) {
1911 return BOOT_EFLASH;
1912 }
1913
1914 rc = boot_read_image_size(slot, hdr, &img_sz);
1915 if (rc != 0) {
1916 return BOOT_EFLASH;
1917 }
1918
1919 while (bytes_copied < img_sz) {
1920 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
1921 /*
1922 * Direct copy from where the image sector resides in flash to its new
1923 * location in SRAM
1924 */
1925 rc = flash_area_read(fap_src,
1926 bytes_copied,
1927 (void *)(dst + bytes_copied),
1928 sect_sz);
1929 if (rc != 0) {
1930 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
1931 break;
1932 } else {
1933 bytes_copied += sect_sz;
1934 }
1935 sect++;
1936 }
1937
1938 if (fap_src) {
1939 flash_area_close(fap_src);
1940 }
1941 return rc;
1942}
1943#endif /* MCUBOOT_RAM_LOADING */
1944
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001945/**
1946 * Prepares the booting process. This function choose the newer image in flash
1947 * as appropriate, and returns the address to boot from.
1948 *
1949 * @param rsp On success, indicates how booting should occur.
1950 *
1951 * @return 0 on success; nonzero on failure.
1952 */
1953int
1954boot_go(struct boot_rsp *rsp)
1955{
1956 size_t slot = 0;
1957 int32_t i;
1958 int rc;
1959 int fa_id;
1960 uint32_t boot_sequence[BOOT_NUM_SLOTS];
1961 uint32_t img_cnt;
Oliver Swedef9982442018-08-24 18:37:44 +01001962 struct image_header *newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001963
David Vincze8bdfc2d2019-03-18 15:49:23 +01001964 static boot_sector_t primary_slot_sectors[BOOT_MAX_IMG_SECTORS];
1965 static boot_sector_t secondary_slot_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001966
David Vincze8bdfc2d2019-03-18 15:49:23 +01001967 boot_data.imgs[BOOT_PRIMARY_SLOT].sectors = &primary_slot_sectors[0];
1968 boot_data.imgs[BOOT_SECONDARY_SLOT].sectors = &secondary_slot_sectors[0];
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001969
1970 /* Open boot_data image areas for the duration of this call. */
1971 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
1972 fa_id = flash_area_id_from_image_slot(i);
1973 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
1974 assert(rc == 0);
1975 }
1976
1977 /* Determine the sector layout of the image slots. */
1978 rc = boot_read_sectors();
1979 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001980 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1981 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001982 goto out;
1983 }
1984
1985 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001986 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001987 if (rc != 0) {
1988 goto out;
1989 }
1990
1991 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
1992 if (img_cnt) {
1993 /* Authenticate images */
1994 for (i = 0; i < img_cnt; i++) {
David Vincze401c7422019-06-21 20:44:05 +02001995 rc = boot_validate_slot(boot_sequence[i], NULL);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001996 if (rc == 0) {
1997 slot = boot_sequence[i];
1998 break;
1999 }
2000 }
2001 if (rc) {
2002 /* If there was no valid image at all */
2003 rc = BOOT_EBADIMAGE;
2004 goto out;
2005 }
2006
Oliver Swedef9982442018-08-24 18:37:44 +01002007 /* The slot variable now refers to the newest image's slot in flash */
2008 newest_image_header = boot_img_hdr(&boot_data, slot);
2009
David Vincze060968d2019-05-23 01:13:14 +02002010 /* Update the security counter with the newest image's security
2011 * counter value.
2012 */
2013 rc = boot_update_security_counter(slot, newest_image_header);
2014 if (rc != 0) {
2015 BOOT_LOG_ERR("Security counter update failed after image "
2016 "validation.");
2017 goto out;
2018 }
2019
Oliver Swedef9982442018-08-24 18:37:44 +01002020 #ifdef MCUBOOT_RAM_LOADING
2021 if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
2022 /* Copy image to the load address from where it
2023 * currently resides in flash */
2024 rc = boot_copy_image_to_sram(slot, newest_image_header);
2025 if (rc != 0) {
2026 rc = BOOT_EBADIMAGE;
David Vincze8bdfc2d2019-03-18 15:49:23 +01002027 BOOT_LOG_INF("Could not copy image from the %s slot in "
Tamas Banc27b5c32019-05-28 16:30:19 +01002028 "the Flash to load address 0x%x in SRAM, "
David Vincze8bdfc2d2019-03-18 15:49:23 +01002029 "aborting..", (slot == BOOT_PRIMARY_SLOT) ?
2030 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002031 newest_image_header->ih_load_addr);
2032 goto out;
2033 } else {
David Vincze8bdfc2d2019-03-18 15:49:23 +01002034 BOOT_LOG_INF("Image has been copied from the %s slot in "
2035 "the flash to SRAM address 0x%x",
2036 (slot == BOOT_PRIMARY_SLOT) ?
2037 "primary" : "secondary",
Oliver Swedef9982442018-08-24 18:37:44 +01002038 newest_image_header->ih_load_addr);
2039 }
2040
2041 /* Validate the image hash in SRAM after the copy was successful */
2042 rc = bootutil_check_hash_after_loading(newest_image_header);
2043 if (rc != 0) {
2044 rc = BOOT_EBADIMAGE;
2045 BOOT_LOG_INF("Cannot validate the hash of the image that was "
2046 "copied to SRAM, aborting..");
2047 goto out;
2048 }
2049
Tamas Banc27b5c32019-05-28 16:30:19 +01002050 BOOT_LOG_INF("Booting image from SRAM at address 0x%x",
Oliver Swedef9982442018-08-24 18:37:44 +01002051 newest_image_header->ih_load_addr);
2052 } else {
David Vincze8a2a4e22019-05-24 10:14:23 +02002053#endif /* MCUBOOT_RAM_LOADING */
David Vincze8bdfc2d2019-03-18 15:49:23 +01002054 BOOT_LOG_INF("Booting image from the %s slot",
2055 (slot == BOOT_PRIMARY_SLOT) ? "primary" : "secondary");
David Vincze8a2a4e22019-05-24 10:14:23 +02002056#ifdef MCUBOOT_RAM_LOADING
Oliver Swedef9982442018-08-24 18:37:44 +01002057 }
David Vincze8a2a4e22019-05-24 10:14:23 +02002058#endif
Oliver Swedef9982442018-08-24 18:37:44 +01002059
2060 rsp->br_hdr = newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002061 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
David Vincze401c7422019-06-21 20:44:05 +02002062 rsp->br_flash_dev_id = boot_data.imgs[slot].area->fa_device_id;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002063 } else {
2064 /* No candidate image available */
2065 rc = BOOT_EBADIMAGE;
David Vincze060968d2019-05-23 01:13:14 +02002066 goto out;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002067 }
2068
Tamas Ban0e8ab302019-01-17 11:45:31 +00002069 /* Save boot status to shared memory area */
2070 rc = boot_save_boot_status(SW_S_NS,
2071 rsp->br_hdr,
2072 BOOT_IMG_AREA(&boot_data, slot));
2073 if (rc) {
2074 BOOT_LOG_ERR("Failed to add data to shared area");
2075 }
2076
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00002077out:
2078 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
2079 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
2080 }
2081 return rc;
2082}
Oliver Swedef9982442018-08-24 18:37:44 +01002083#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */