blob: b787d32bc5e99034d0437eeec7e6cd6749014db6 [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"
Tamas Banf70ef8c2017-12-19 15:35:09 +000044
45#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
46#include "bootutil/bootutil_log.h"
47
Tamas Banf70ef8c2017-12-19 15:35:09 +000048static struct boot_loader_state boot_data;
49
Oliver Swedef9982442018-08-24 18:37:44 +010050#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
David Vincze39e78552018-10-10 17:10:01 +020051
52#if defined(MCUBOOT_VALIDATE_SLOT0) && !defined(MCUBOOT_OVERWRITE_ONLY)
53static int boot_status_fails = 0;
54#define BOOT_STATUS_ASSERT(x) \
55 do { \
56 if (!(x)) { \
57 boot_status_fails++; \
58 } \
59 } while (0)
60#else
61#define BOOT_STATUS_ASSERT(x) assert(x)
62#endif
63
Tamas Banf70ef8c2017-12-19 15:35:09 +000064struct boot_status_table {
Tamas Banf70ef8c2017-12-19 15:35:09 +000065 uint8_t bst_magic_slot0;
66 uint8_t bst_magic_scratch;
67 uint8_t bst_copy_done_slot0;
68 uint8_t bst_status_source;
69};
70
71/**
72 * This set of tables maps swap state contents to boot status location.
73 * When searching for a match, these tables must be iterated in order.
74 */
75static const struct boot_status_table boot_status_tables[] = {
76 {
77 /* | slot-0 | scratch |
78 * ----------+------------+------------|
79 * magic | Good | Any |
David Vincze39e78552018-10-10 17:10:01 +020080 * copy-done | Set | N/A |
Tamas Banf70ef8c2017-12-19 15:35:09 +000081 * ----------+------------+------------'
82 * source: none |
83 * ------------------------------------'
84 */
85 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
David Vincze39e78552018-10-10 17:10:01 +020086 .bst_magic_scratch = BOOT_MAGIC_ANY,
87 .bst_copy_done_slot0 = BOOT_FLAG_SET,
Tamas Banf70ef8c2017-12-19 15:35:09 +000088 .bst_status_source = BOOT_STATUS_SOURCE_NONE,
89 },
90
91 {
92 /* | slot-0 | scratch |
93 * ----------+------------+------------|
94 * magic | Good | Any |
David Vincze39e78552018-10-10 17:10:01 +020095 * copy-done | Unset | N/A |
Tamas Banf70ef8c2017-12-19 15:35:09 +000096 * ----------+------------+------------'
97 * source: slot 0 |
98 * ------------------------------------'
99 */
100 .bst_magic_slot0 = BOOT_MAGIC_GOOD,
David Vincze39e78552018-10-10 17:10:01 +0200101 .bst_magic_scratch = BOOT_MAGIC_ANY,
102 .bst_copy_done_slot0 = BOOT_FLAG_UNSET,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000103 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
104 },
105
106 {
107 /* | slot-0 | scratch |
108 * ----------+------------+------------|
109 * magic | Any | Good |
110 * copy-done | Any | N/A |
111 * ----------+------------+------------'
112 * source: scratch |
113 * ------------------------------------'
114 */
David Vincze39e78552018-10-10 17:10:01 +0200115 .bst_magic_slot0 = BOOT_MAGIC_ANY,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000116 .bst_magic_scratch = BOOT_MAGIC_GOOD,
David Vincze39e78552018-10-10 17:10:01 +0200117 .bst_copy_done_slot0 = BOOT_FLAG_ANY,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000118 .bst_status_source = BOOT_STATUS_SOURCE_SCRATCH,
119 },
120
121 {
122 /* | slot-0 | scratch |
123 * ----------+------------+------------|
124 * magic | Unset | Any |
David Vincze39e78552018-10-10 17:10:01 +0200125 * copy-done | Unset | N/A |
Tamas Banf70ef8c2017-12-19 15:35:09 +0000126 * ----------+------------+------------|
127 * source: varies |
128 * ------------------------------------+------------------------------+
129 * This represents one of two cases: |
130 * o No swaps ever (no status to read, so no harm in checking). |
131 * o Mid-revert; status in slot 0. |
132 * -------------------------------------------------------------------'
133 */
134 .bst_magic_slot0 = BOOT_MAGIC_UNSET,
David Vincze39e78552018-10-10 17:10:01 +0200135 .bst_magic_scratch = BOOT_MAGIC_ANY,
136 .bst_copy_done_slot0 = BOOT_FLAG_UNSET,
Tamas Banf70ef8c2017-12-19 15:35:09 +0000137 .bst_status_source = BOOT_STATUS_SOURCE_SLOT0,
138 },
139};
140
141#define BOOT_STATUS_TABLES_COUNT \
Tamas Ban581034a2017-12-19 19:54:37 +0000142 (sizeof(boot_status_tables) / sizeof(boot_status_tables[0]))
Tamas Banf70ef8c2017-12-19 15:35:09 +0000143
144#define BOOT_LOG_SWAP_STATE(area, state) \
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000145 BOOT_LOG_INF("%s: magic=%5s, copy_done=0x%x, image_ok=0x%x", \
Tamas Banf70ef8c2017-12-19 15:35:09 +0000146 (area), \
147 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
148 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
149 "bad"), \
150 (state)->copy_done, \
151 (state)->image_ok)
Oliver Swedef9982442018-08-24 18:37:44 +0100152#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_RAM_LOADING */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000153
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000154
155static int
156boot_read_image_header(int slot, struct image_header *out_hdr)
157{
158 const struct flash_area *fap = NULL;
159 int area_id;
160 int rc;
161
162 area_id = flash_area_id_from_image_slot(slot);
163 rc = flash_area_open(area_id, &fap);
164 if (rc != 0) {
165 rc = BOOT_EFLASH;
166 goto done;
167 }
168
169 rc = flash_area_read(fap, 0, out_hdr, sizeof(*out_hdr));
170 if (rc != 0) {
171 rc = BOOT_EFLASH;
172 goto done;
173 }
174
175 rc = 0;
176
177done:
178 flash_area_close(fap);
179 return rc;
180}
181
182static int
David Vincze39e78552018-10-10 17:10:01 +0200183boot_read_image_headers(bool require_all)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000184{
185 int rc;
186 int i;
187
188 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
189 rc = boot_read_image_header(i, boot_img_hdr(&boot_data, i));
190 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +0200191 /* If `require_all` is set, fail on any single fail, otherwise
192 * if at least the first slot's header was read successfully,
193 * then the boot loader can attempt a boot.
194 *
195 * Failure to read any headers is a fatal error.
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000196 */
David Vincze39e78552018-10-10 17:10:01 +0200197 if (i > 0 && !require_all) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000198 return 0;
199 } else {
200 return rc;
201 }
202 }
203 }
204
205 return 0;
206}
207
208static uint8_t
209boot_write_sz(void)
210{
211 const struct flash_area *fap;
212 uint8_t elem_sz;
213 uint8_t align;
214 int rc;
215
216 /* Figure out what size to write update status update as. The size depends
217 * on what the minimum write size is for scratch area, active image slot.
218 * We need to use the bigger of those 2 values.
219 */
220 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
221 assert(rc == 0);
222 elem_sz = flash_area_align(fap);
223 flash_area_close(fap);
224
225 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
226 assert(rc == 0);
227 align = flash_area_align(fap);
228 flash_area_close(fap);
229
230 if (align > elem_sz) {
231 elem_sz = align;
232 }
233
234 return elem_sz;
235}
236
237/**
238 * Determines the sector layout of both image slots and the scratch area.
239 * This information is necessary for calculating the number of bytes to erase
240 * and copy during an image swap. The information collected during this
241 * function is used to populate the boot_data global.
242 */
243static int
244boot_read_sectors(void)
245{
246 int rc;
247
248 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_0);
249 if (rc != 0) {
250 return BOOT_EFLASH;
251 }
252
253 rc = boot_initialize_area(&boot_data, FLASH_AREA_IMAGE_1);
254 if (rc != 0) {
255 return BOOT_EFLASH;
256 }
257
258 BOOT_WRITE_SZ(&boot_data) = boot_write_sz();
259
260 return 0;
261}
262
263/*
264 * Validate image hash/signature in a slot.
265 */
266static int
Tamas Ban0e8ab302019-01-17 11:45:31 +0000267boot_image_check(struct image_header *hdr, const struct flash_area *fap)
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000268{
269 static uint8_t tmpbuf[BOOT_TMPBUF_SZ];
270
271 if (bootutil_img_validate(hdr, fap, tmpbuf, BOOT_TMPBUF_SZ,
Tamas Ban0e8ab302019-01-17 11:45:31 +0000272 NULL, 0, NULL)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000273 return BOOT_EBADIMAGE;
274 }
275 return 0;
276}
277
David Vincze39e78552018-10-10 17:10:01 +0200278static inline int
279boot_magic_is_erased(uint8_t erased_val, uint32_t magic)
280{
281 uint8_t i;
282 for (i = 0; i < sizeof(magic); i++) {
283 if (erased_val != *(((uint8_t *)&magic) + i)) {
284 return 0;
285 }
286 }
287 return 1;
288}
289
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000290static int
291boot_validate_slot(int slot)
292{
293 const struct flash_area *fap;
294 struct image_header *hdr;
295 int rc;
296
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000297 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap);
298 if (rc != 0) {
299 return BOOT_EFLASH;
300 }
301
David Vincze39e78552018-10-10 17:10:01 +0200302 hdr = boot_img_hdr(&boot_data, slot);
303 if (boot_magic_is_erased(flash_area_erased_val(fap), hdr->ih_magic) ||
304 (hdr->ih_flags & IMAGE_F_NON_BOOTABLE)) {
305 /* No bootable image in slot; continue booting from slot 0. */
306 return -1;
307 }
308
Tamas Bana9de4a62018-09-18 08:09:45 +0100309 if ((hdr->ih_magic != IMAGE_MAGIC ||
Tamas Ban0e8ab302019-01-17 11:45:31 +0000310 boot_image_check(hdr, fap) != 0)) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000311 if (slot != 0) {
David Vincze26e8c8a2018-08-28 16:59:41 +0200312 rc = flash_area_erase(fap, 0, fap->fa_size);
313 if(rc != 0) {
314 flash_area_close(fap);
315 return BOOT_EFLASH;
316 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +0000317 /* Image in slot 1 is invalid. Erase the image and
318 * continue booting from slot 0.
319 */
320 }
321 BOOT_LOG_ERR("Authentication failed! Image in slot %d is not valid.",
322 slot);
323 return -1;
324 }
325
326 flash_area_close(fap);
327
328 /* Image in slot 1 is valid. */
329 return 0;
330}
331
Oliver Swedef9982442018-08-24 18:37:44 +0100332#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_OVERWRITE_ONLY)
333/*
334 * Compute the total size of the given image. Includes the size of
335 * the TLVs.
336 */
337static int
338boot_read_image_size(int slot, struct image_header *hdr, uint32_t *size)
339{
340 const struct flash_area *fap = NULL;
341 struct image_tlv_info info;
342 int area_id;
343 int rc;
344
345 area_id = flash_area_id_from_image_slot(slot);
346 rc = flash_area_open(area_id, &fap);
347 if (rc != 0) {
348 rc = BOOT_EFLASH;
349 goto done;
350 }
351
352 rc = flash_area_read(fap, hdr->ih_hdr_size + hdr->ih_img_size,
353 &info, sizeof(info));
354 if (rc != 0) {
355 rc = BOOT_EFLASH;
356 goto done;
357 }
358 if (info.it_magic != IMAGE_TLV_INFO_MAGIC) {
359 rc = BOOT_EBADIMAGE;
360 goto done;
361 }
362 *size = hdr->ih_hdr_size + hdr->ih_img_size + info.it_tlv_tot;
363 rc = 0;
364
365done:
366 flash_area_close(fap);
367 return rc;
368}
369#endif /* !MCUBOOT_NO_SWAP && !MCUBOOT_OVERWRITE_ONLY */
370
371#if !defined(MCUBOOT_NO_SWAP) && !defined(MCUBOOT_RAM_LOADING)
Tamas Banf70ef8c2017-12-19 15:35:09 +0000372/**
Tamas Ban581034a2017-12-19 19:54:37 +0000373 * Determines where in flash the most recent boot status is stored. The boot
Tamas Banf70ef8c2017-12-19 15:35:09 +0000374 * status is necessary for completing a swap that was interrupted by a boot
375 * loader reset.
376 *
Tamas Ban581034a2017-12-19 19:54:37 +0000377 * @return BOOT_STATUS_SOURCE_[...] code indicating where
378 * status should be read from.
Tamas Banf70ef8c2017-12-19 15:35:09 +0000379 */
380static int
381boot_status_source(void)
382{
383 const struct boot_status_table *table;
384 struct boot_swap_state state_scratch;
385 struct boot_swap_state state_slot0;
386 int rc;
David Vincze39e78552018-10-10 17:10:01 +0200387 size_t i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000388 uint8_t source;
389
390 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_0, &state_slot0);
391 assert(rc == 0);
392
393 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH, &state_scratch);
394 assert(rc == 0);
395
396 BOOT_LOG_SWAP_STATE("Image 0", &state_slot0);
397 BOOT_LOG_SWAP_STATE("Scratch", &state_scratch);
398
399 for (i = 0; i < BOOT_STATUS_TABLES_COUNT; i++) {
400 table = &boot_status_tables[i];
401
David Vincze39e78552018-10-10 17:10:01 +0200402 if ((table->bst_magic_slot0 == BOOT_MAGIC_ANY ||
Tamas Banf70ef8c2017-12-19 15:35:09 +0000403 table->bst_magic_slot0 == state_slot0.magic) &&
David Vincze39e78552018-10-10 17:10:01 +0200404 (table->bst_magic_scratch == BOOT_MAGIC_ANY ||
Tamas Banf70ef8c2017-12-19 15:35:09 +0000405 table->bst_magic_scratch == state_scratch.magic) &&
David Vincze39e78552018-10-10 17:10:01 +0200406 (table->bst_copy_done_slot0 == BOOT_FLAG_ANY ||
Tamas Banf70ef8c2017-12-19 15:35:09 +0000407 table->bst_copy_done_slot0 == state_slot0.copy_done)) {
408 source = table->bst_status_source;
409 BOOT_LOG_INF("Boot source: %s",
410 source == BOOT_STATUS_SOURCE_NONE ? "none" :
411 source == BOOT_STATUS_SOURCE_SCRATCH ? "scratch" :
412 source == BOOT_STATUS_SOURCE_SLOT0 ? "slot 0" :
413 "BUG; can't happen");
414 return source;
415 }
416 }
417
418 BOOT_LOG_INF("Boot source: none");
419 return BOOT_STATUS_SOURCE_NONE;
420}
421
422/**
423 * Calculates the type of swap that just completed.
424 *
425 * This is used when a swap is interrupted by an external event. After
426 * finishing the swap operation determines what the initial request was.
427 */
428static int
429boot_previous_swap_type(void)
430{
431 int post_swap_type;
432
433 post_swap_type = boot_swap_type();
434
435 switch (post_swap_type) {
David Vincze39e78552018-10-10 17:10:01 +0200436 case BOOT_SWAP_TYPE_NONE : return BOOT_SWAP_TYPE_PERM;
437 case BOOT_SWAP_TYPE_REVERT : return BOOT_SWAP_TYPE_TEST;
438 case BOOT_SWAP_TYPE_PANIC : return BOOT_SWAP_TYPE_PANIC;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000439 }
440
441 return BOOT_SWAP_TYPE_FAIL;
442}
443
Tamas Banf70ef8c2017-12-19 15:35:09 +0000444static int
Tamas Banf70ef8c2017-12-19 15:35:09 +0000445boot_slots_compatible(void)
446{
447 size_t num_sectors_0 = boot_img_num_sectors(&boot_data, 0);
448 size_t num_sectors_1 = boot_img_num_sectors(&boot_data, 1);
449 size_t size_0, size_1;
450 size_t i;
451
David Vincze39e78552018-10-10 17:10:01 +0200452 if (num_sectors_0 > BOOT_MAX_IMG_SECTORS || num_sectors_1 > BOOT_MAX_IMG_SECTORS) {
453 BOOT_LOG_WRN("Cannot upgrade: more sectors than allowed");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000454 return 0;
455 }
David Vincze39e78552018-10-10 17:10:01 +0200456
457 /* Ensure both image slots have identical sector layouts. */
458 if (num_sectors_0 != num_sectors_1) {
459 BOOT_LOG_WRN("Cannot upgrade: number of sectors differ between slots");
460 return 0;
461 }
462
Tamas Banf70ef8c2017-12-19 15:35:09 +0000463 for (i = 0; i < num_sectors_0; i++) {
464 size_0 = boot_img_sector_size(&boot_data, 0, i);
465 size_1 = boot_img_sector_size(&boot_data, 1, i);
466 if (size_0 != size_1) {
David Vincze39e78552018-10-10 17:10:01 +0200467 BOOT_LOG_WRN("Cannot upgrade: an incompatible sector was found");
Tamas Banf70ef8c2017-12-19 15:35:09 +0000468 return 0;
469 }
470 }
471
472 return 1;
473}
474
Tamas Banf70ef8c2017-12-19 15:35:09 +0000475static uint32_t
476boot_status_internal_off(int idx, int state, int elem_sz)
477{
478 int idx_sz;
479
480 idx_sz = elem_sz * BOOT_STATUS_STATE_COUNT;
481
David Vincze39e78552018-10-10 17:10:01 +0200482 return (idx - BOOT_STATUS_IDX_0) * idx_sz +
483 (state - BOOT_STATUS_STATE_0) * elem_sz;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000484}
485
486/**
487 * Reads the status of a partially-completed swap, if any. This is necessary
488 * to recover in case the boot lodaer was reset in the middle of a swap
489 * operation.
490 */
491static int
492boot_read_status_bytes(const struct flash_area *fap, struct boot_status *bs)
493{
494 uint32_t off;
495 uint8_t status;
496 int max_entries;
497 int found;
David Vincze39e78552018-10-10 17:10:01 +0200498 int found_idx;
499 int invalid;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000500 int rc;
501 int i;
502
503 off = boot_status_off(fap);
504 max_entries = boot_status_entries(fap);
505
506 found = 0;
David Vincze39e78552018-10-10 17:10:01 +0200507 found_idx = 0;
508 invalid = 0;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000509 for (i = 0; i < max_entries; i++) {
David Vincze39e78552018-10-10 17:10:01 +0200510 rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(&boot_data),
511 &status, 1);
512 if (rc < 0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000513 return BOOT_EFLASH;
514 }
515
David Vincze39e78552018-10-10 17:10:01 +0200516 if (rc == 1) {
517 if (found && !found_idx) {
518 found_idx = i;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000519 }
520 } else if (!found) {
521 found = 1;
David Vincze39e78552018-10-10 17:10:01 +0200522 } else if (found_idx) {
523 invalid = 1;
524 break;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000525 }
526 }
527
David Vincze39e78552018-10-10 17:10:01 +0200528 if (invalid) {
529 /* This means there was an error writing status on the last
530 * swap. Tell user and move on to validation!
531 */
532 BOOT_LOG_ERR("Detected inconsistent status!");
533
534#if !defined(MCUBOOT_VALIDATE_SLOT0)
535 /* With validation of slot0 disabled, there is no way to be sure the
536 * swapped slot0 is OK, so abort!
537 */
538 assert(0);
539#endif
540 }
541
Tamas Banf70ef8c2017-12-19 15:35:09 +0000542 if (found) {
David Vincze39e78552018-10-10 17:10:01 +0200543 if (!found_idx) {
544 found_idx = i;
545 }
546 found_idx--;
547 bs->idx = (found_idx / BOOT_STATUS_STATE_COUNT) + 1;
548 bs->state = (found_idx % BOOT_STATUS_STATE_COUNT) + 1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000549 }
550
551 return 0;
552}
553
554/**
555 * Reads the boot status from the flash. The boot status contains
556 * the current state of an interrupted image copy operation. If the boot
557 * status is not present, or it indicates that previous copy finished,
558 * there is no operation in progress.
559 */
560static int
561boot_read_status(struct boot_status *bs)
562{
563 const struct flash_area *fap;
564 int status_loc;
565 int area_id;
566 int rc;
567
David Vincze39e78552018-10-10 17:10:01 +0200568 memset(bs, 0, sizeof *bs);
569 bs->idx = BOOT_STATUS_IDX_0;
570 bs->state = BOOT_STATUS_STATE_0;
571
572#ifdef MCUBOOT_OVERWRITE_ONLY
573 /* Overwrite-only doesn't make use of the swap status area. */
574 return 0;
575#endif
Tamas Banf70ef8c2017-12-19 15:35:09 +0000576
577 status_loc = boot_status_source();
578 switch (status_loc) {
579 case BOOT_STATUS_SOURCE_NONE:
580 return 0;
581
582 case BOOT_STATUS_SOURCE_SCRATCH:
583 area_id = FLASH_AREA_IMAGE_SCRATCH;
584 break;
585
586 case BOOT_STATUS_SOURCE_SLOT0:
587 area_id = FLASH_AREA_IMAGE_0;
588 break;
589
590 default:
591 assert(0);
592 return BOOT_EBADARGS;
593 }
594
595 rc = flash_area_open(area_id, &fap);
596 if (rc != 0) {
597 return BOOT_EFLASH;
598 }
599
600 rc = boot_read_status_bytes(fap, bs);
601
602 flash_area_close(fap);
David Vincze39e78552018-10-10 17:10:01 +0200603
Tamas Banf70ef8c2017-12-19 15:35:09 +0000604 return rc;
605}
606
607/**
608 * Writes the supplied boot status to the flash file system. The boot status
609 * contains the current state of an in-progress image copy operation.
610 *
611 * @param bs The boot status to write.
612 *
613 * @return 0 on success; nonzero on failure.
614 */
615int
616boot_write_status(struct boot_status *bs)
617{
Tamas Ban581034a2017-12-19 19:54:37 +0000618 const struct flash_area *fap = NULL;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000619 uint32_t off;
620 int area_id;
621 int rc;
622 uint8_t buf[BOOT_MAX_ALIGN];
623 uint8_t align;
David Vincze39e78552018-10-10 17:10:01 +0200624 uint8_t erased_val;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000625
626 /* NOTE: The first sector copied (that is the last sector on slot) contains
627 * the trailer. Since in the last step SLOT 0 is erased, the first
628 * two status writes go to the scratch which will be copied to SLOT 0!
629 */
630
631 if (bs->use_scratch) {
632 /* Write to scratch. */
633 area_id = FLASH_AREA_IMAGE_SCRATCH;
634 } else {
635 /* Write to slot 0. */
636 area_id = FLASH_AREA_IMAGE_0;
637 }
638
639 rc = flash_area_open(area_id, &fap);
640 if (rc != 0) {
641 rc = BOOT_EFLASH;
642 goto done;
643 }
644
645 off = boot_status_off(fap) +
646 boot_status_internal_off(bs->idx, bs->state,
647 BOOT_WRITE_SZ(&boot_data));
648
Tamas Banc3828852018-02-01 12:24:16 +0000649 align = flash_area_align(fap);
David Vincze39e78552018-10-10 17:10:01 +0200650 erased_val = flash_area_erased_val(fap);
651 memset(buf, erased_val, BOOT_MAX_ALIGN);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000652 buf[0] = bs->state;
653
654 rc = flash_area_write(fap, off, buf, align);
655 if (rc != 0) {
656 rc = BOOT_EFLASH;
657 goto done;
658 }
659
660 rc = 0;
661
662done:
663 flash_area_close(fap);
664 return rc;
665}
666
Tamas Banf70ef8c2017-12-19 15:35:09 +0000667/**
668 * Determines which swap operation to perform, if any. If it is determined
669 * that a swap operation is required, the image in the second slot is checked
670 * for validity. If the image in the second slot is invalid, it is erased, and
671 * a swap type of "none" is indicated.
672 *
673 * @return The type of swap to perform (BOOT_SWAP_TYPE...)
674 */
675static int
676boot_validated_swap_type(void)
677{
678 int swap_type;
679
680 swap_type = boot_swap_type();
681 switch (swap_type) {
682 case BOOT_SWAP_TYPE_TEST:
683 case BOOT_SWAP_TYPE_PERM:
684 case BOOT_SWAP_TYPE_REVERT:
685 /* Boot loader wants to switch to slot 1. Ensure image is valid. */
686 if (boot_validate_slot(1) != 0) {
687 swap_type = BOOT_SWAP_TYPE_FAIL;
688 }
689 }
690
691 return swap_type;
692}
693
694/**
695 * Calculates the number of sectors the scratch area can contain. A "last"
696 * source sector is specified because images are copied backwards in flash
697 * (final index to index number 0).
698 *
699 * @param last_sector_idx The index of the last source sector
700 * (inclusive).
701 * @param out_first_sector_idx The index of the first source sector
702 * (inclusive) gets written here.
703 *
704 * @return The number of bytes comprised by the
705 * [first-sector, last-sector] range.
706 */
707#ifndef MCUBOOT_OVERWRITE_ONLY
708static uint32_t
709boot_copy_sz(int last_sector_idx, int *out_first_sector_idx)
710{
711 size_t scratch_sz;
712 uint32_t new_sz;
713 uint32_t sz;
714 int i;
715
716 sz = 0;
717
718 scratch_sz = boot_scratch_area_size(&boot_data);
719 for (i = last_sector_idx; i >= 0; i--) {
720 new_sz = sz + boot_img_sector_size(&boot_data, 0, i);
721 if (new_sz > scratch_sz) {
722 break;
723 }
724 sz = new_sz;
725 }
726
727 /* i currently refers to a sector that doesn't fit or it is -1 because all
728 * sectors have been processed. In both cases, exclude sector i.
729 */
730 *out_first_sector_idx = i + 1;
731 return sz;
732}
733#endif /* !MCUBOOT_OVERWRITE_ONLY */
734
735/**
David Vinczef7641fa2018-09-04 18:29:46 +0200736 * Erases a region of flash.
737 *
738 * @param flash_area_idx The ID of the flash area containing the region
739 * to erase.
740 * @param off The offset within the flash area to start the
741 * erase.
742 * @param sz The number of bytes to erase.
743 *
744 * @return 0 on success; nonzero on failure.
745 */
746static int
747boot_erase_sector(int flash_area_id, uint32_t off, uint32_t sz)
748{
749 const struct flash_area *fap = NULL;
750 int rc;
751
752 rc = flash_area_open(flash_area_id, &fap);
753 if (rc != 0) {
754 rc = BOOT_EFLASH;
755 goto done;
756 }
757
758 rc = flash_area_erase(fap, off, sz);
759 if (rc != 0) {
760 rc = BOOT_EFLASH;
761 goto done;
762 }
763
764 rc = 0;
765
766done:
767 flash_area_close(fap);
768 return rc;
769}
770
771/**
Tamas Banf70ef8c2017-12-19 15:35:09 +0000772 * Copies the contents of one flash region to another. You must erase the
773 * destination region prior to calling this function.
774 *
775 * @param flash_area_id_src The ID of the source flash area.
776 * @param flash_area_id_dst The ID of the destination flash area.
777 * @param off_src The offset within the source flash area to
778 * copy from.
779 * @param off_dst The offset within the destination flash area to
780 * copy to.
781 * @param sz The number of bytes to copy.
782 *
783 * @return 0 on success; nonzero on failure.
784 */
785static int
786boot_copy_sector(int flash_area_id_src, int flash_area_id_dst,
787 uint32_t off_src, uint32_t off_dst, uint32_t sz)
788{
789 const struct flash_area *fap_src;
790 const struct flash_area *fap_dst;
791 uint32_t bytes_copied;
792 int chunk_sz;
793 int rc;
794
795 static uint8_t buf[1024];
796
797 fap_src = NULL;
798 fap_dst = NULL;
799
800 rc = flash_area_open(flash_area_id_src, &fap_src);
801 if (rc != 0) {
802 rc = BOOT_EFLASH;
803 goto done;
804 }
805
806 rc = flash_area_open(flash_area_id_dst, &fap_dst);
807 if (rc != 0) {
808 rc = BOOT_EFLASH;
809 goto done;
810 }
811
812 bytes_copied = 0;
813 while (bytes_copied < sz) {
Tamas Ban581034a2017-12-19 19:54:37 +0000814 if (sz - bytes_copied > sizeof(buf)) {
815 chunk_sz = sizeof(buf);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000816 } else {
817 chunk_sz = sz - bytes_copied;
818 }
819
820 rc = flash_area_read(fap_src, off_src + bytes_copied, buf, chunk_sz);
821 if (rc != 0) {
822 rc = BOOT_EFLASH;
823 goto done;
824 }
825
826 rc = flash_area_write(fap_dst, off_dst + bytes_copied, buf, chunk_sz);
827 if (rc != 0) {
828 rc = BOOT_EFLASH;
829 goto done;
830 }
831
832 bytes_copied += chunk_sz;
833 }
834
835 rc = 0;
836
837done:
838 if (fap_src) {
839 flash_area_close(fap_src);
840 }
841 if (fap_dst) {
842 flash_area_close(fap_dst);
843 }
844 return rc;
845}
846
847#ifndef MCUBOOT_OVERWRITE_ONLY
848static inline int
849boot_status_init_by_id(int flash_area_id, const struct boot_status *bs)
850{
851 const struct flash_area *fap;
852 struct boot_swap_state swap_state;
853 int rc;
854
855 rc = flash_area_open(flash_area_id, &fap);
856 assert(rc == 0);
857
858 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_1, &swap_state);
859 assert(rc == 0);
860
861 if (swap_state.image_ok == BOOT_FLAG_SET) {
862 rc = boot_write_image_ok(fap);
863 assert(rc == 0);
864 }
865
866 rc = boot_write_swap_size(fap, bs->swap_size);
867 assert(rc == 0);
868
869 rc = boot_write_magic(fap);
870 assert(rc == 0);
871
872 flash_area_close(fap);
873
874 return 0;
875}
David Vinczef7641fa2018-09-04 18:29:46 +0200876
877static int
878boot_erase_last_sector_by_id(int flash_area_id)
879{
880 uint8_t slot;
881 uint32_t last_sector;
882 int rc;
883
884 switch (flash_area_id) {
885 case FLASH_AREA_IMAGE_0:
886 slot = 0;
887 break;
888 case FLASH_AREA_IMAGE_1:
889 slot = 1;
890 break;
891 default:
892 return BOOT_EFLASH;
893 }
894
895 last_sector = boot_img_num_sectors(&boot_data, slot) - 1;
896 rc = boot_erase_sector(flash_area_id,
897 boot_img_sector_off(&boot_data, slot, last_sector),
898 boot_img_sector_size(&boot_data, slot, last_sector));
899 assert(rc == 0);
900
901 return rc;
902}
903#endif /* !MCUBOOT_OVERWRITE_ONLY */
Tamas Banf70ef8c2017-12-19 15:35:09 +0000904
Tamas Banf70ef8c2017-12-19 15:35:09 +0000905/**
906 * Swaps the contents of two flash regions within the two image slots.
907 *
908 * @param idx The index of the first sector in the range of
909 * sectors being swapped.
910 * @param sz The number of bytes to swap.
911 * @param bs The current boot status. This struct gets
912 * updated according to the outcome.
913 *
914 * @return 0 on success; nonzero on failure.
915 */
916#ifndef MCUBOOT_OVERWRITE_ONLY
917static void
918boot_swap_sectors(int idx, uint32_t sz, struct boot_status *bs)
919{
920 const struct flash_area *fap;
921 uint32_t copy_sz;
922 uint32_t trailer_sz;
923 uint32_t img_off;
924 uint32_t scratch_trailer_off;
925 struct boot_swap_state swap_state;
926 size_t last_sector;
927 int rc;
928
929 /* Calculate offset from start of image area. */
930 img_off = boot_img_sector_off(&boot_data, 0, idx);
931
932 copy_sz = sz;
933 trailer_sz = boot_slots_trailer_sz(BOOT_WRITE_SZ(&boot_data));
934
935 /* sz in this function is always is always sized on a multiple of the
936 * sector size. The check against the start offset of the last sector
937 * is to determine if we're swapping the last sector. The last sector
938 * needs special handling because it's where the trailer lives. If we're
939 * copying it, we need to use scratch to write the trailer temporarily.
940 *
941 * NOTE: `use_scratch` is a temporary flag (never written to flash) which
942 * controls if special handling is needed (swapping last sector).
943 */
944 last_sector = boot_img_num_sectors(&boot_data, 0) - 1;
945 if (img_off + sz > boot_img_sector_off(&boot_data, 0, last_sector)) {
946 copy_sz -= trailer_sz;
947 }
948
David Vincze39e78552018-10-10 17:10:01 +0200949 bs->use_scratch = (bs->idx == BOOT_STATUS_IDX_0 && copy_sz != sz);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000950
David Vincze39e78552018-10-10 17:10:01 +0200951 if (bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000952 rc = boot_erase_sector(FLASH_AREA_IMAGE_SCRATCH, 0, sz);
953 assert(rc == 0);
954
955 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_SCRATCH,
956 img_off, 0, copy_sz);
957 assert(rc == 0);
958
David Vincze39e78552018-10-10 17:10:01 +0200959 if (bs->idx == BOOT_STATUS_IDX_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000960 if (bs->use_scratch) {
961 boot_status_init_by_id(FLASH_AREA_IMAGE_SCRATCH, bs);
962 } else {
963 /* Prepare the status area... here it is known that the
964 * last sector is not being used by the image data so it's
965 * safe to erase.
966 */
967 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_0);
968 assert(rc == 0);
969
970 boot_status_init_by_id(FLASH_AREA_IMAGE_0, bs);
971 }
972 }
973
David Vincze39e78552018-10-10 17:10:01 +0200974 bs->state = BOOT_STATUS_STATE_1;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000975 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +0200976 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000977 }
978
David Vincze39e78552018-10-10 17:10:01 +0200979 if (bs->state == BOOT_STATUS_STATE_1) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000980 rc = boot_erase_sector(FLASH_AREA_IMAGE_1, img_off, sz);
981 assert(rc == 0);
982
983 rc = boot_copy_sector(FLASH_AREA_IMAGE_0, FLASH_AREA_IMAGE_1,
984 img_off, img_off, copy_sz);
985 assert(rc == 0);
986
David Vincze39e78552018-10-10 17:10:01 +0200987 if (bs->idx == BOOT_STATUS_IDX_0 && !bs->use_scratch) {
Tamas Banf70ef8c2017-12-19 15:35:09 +0000988 /* If not all sectors of the slot are being swapped,
989 * guarantee here that only slot0 will have the state.
990 */
991 rc = boot_erase_last_sector_by_id(FLASH_AREA_IMAGE_1);
992 assert(rc == 0);
993 }
994
David Vincze39e78552018-10-10 17:10:01 +0200995 bs->state = BOOT_STATUS_STATE_2;
Tamas Banf70ef8c2017-12-19 15:35:09 +0000996 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +0200997 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +0000998 }
999
David Vincze39e78552018-10-10 17:10:01 +02001000 if (bs->state == BOOT_STATUS_STATE_2) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001001 rc = boot_erase_sector(FLASH_AREA_IMAGE_0, img_off, sz);
1002 assert(rc == 0);
1003
1004 /* NOTE: also copy trailer from scratch (has status info) */
1005 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
1006 0, img_off, copy_sz);
1007 assert(rc == 0);
1008
1009 if (bs->use_scratch) {
1010 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH, &fap);
1011 assert(rc == 0);
1012
1013 scratch_trailer_off = boot_status_off(fap);
1014
1015 flash_area_close(fap);
1016
1017 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1018 assert(rc == 0);
1019
1020 /* copy current status that is being maintained in scratch */
1021 rc = boot_copy_sector(FLASH_AREA_IMAGE_SCRATCH, FLASH_AREA_IMAGE_0,
1022 scratch_trailer_off,
1023 img_off + copy_sz,
1024 BOOT_STATUS_STATE_COUNT * BOOT_WRITE_SZ(&boot_data));
David Vincze39e78552018-10-10 17:10:01 +02001025 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001026
1027 rc = boot_read_swap_state_by_id(FLASH_AREA_IMAGE_SCRATCH,
1028 &swap_state);
1029 assert(rc == 0);
1030
1031 if (swap_state.image_ok == BOOT_FLAG_SET) {
1032 rc = boot_write_image_ok(fap);
1033 assert(rc == 0);
1034 }
1035
1036 rc = boot_write_swap_size(fap, bs->swap_size);
1037 assert(rc == 0);
1038
1039 rc = boot_write_magic(fap);
1040 assert(rc == 0);
1041
1042 flash_area_close(fap);
1043 }
1044
1045 bs->idx++;
David Vincze39e78552018-10-10 17:10:01 +02001046 bs->state = BOOT_STATUS_STATE_0;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001047 bs->use_scratch = 0;
1048 rc = boot_write_status(bs);
David Vincze39e78552018-10-10 17:10:01 +02001049 BOOT_STATUS_ASSERT(rc == 0);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001050 }
1051}
1052#endif /* !MCUBOOT_OVERWRITE_ONLY */
1053
1054/**
1055 * Swaps the two images in flash. If a prior copy operation was interrupted
1056 * by a system reset, this function completes that operation.
1057 *
1058 * @param bs The current boot status. This function reads
1059 * this struct to determine if it is resuming
1060 * an interrupted swap operation. This
1061 * function writes the updated status to this
1062 * function on return.
1063 *
1064 * @return 0 on success; nonzero on failure.
1065 */
1066#ifdef MCUBOOT_OVERWRITE_ONLY
1067static int
1068boot_copy_image(struct boot_status *bs)
1069{
1070 size_t sect_count;
1071 size_t sect;
1072 int rc;
1073 size_t size = 0;
1074 size_t this_size;
David Vincze39e78552018-10-10 17:10:01 +02001075 size_t last_sector;
1076
1077 (void)bs;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001078
1079 BOOT_LOG_INF("Image upgrade slot1 -> slot0");
1080 BOOT_LOG_INF("Erasing slot0");
1081
1082 sect_count = boot_img_num_sectors(&boot_data, 0);
1083 for (sect = 0; sect < sect_count; sect++) {
1084 this_size = boot_img_sector_size(&boot_data, 0, sect);
1085 rc = boot_erase_sector(FLASH_AREA_IMAGE_0,
1086 size,
1087 this_size);
1088 assert(rc == 0);
1089
1090 size += this_size;
1091 }
1092
1093 BOOT_LOG_INF("Copying slot 1 to slot 0: 0x%lx bytes", size);
1094 rc = boot_copy_sector(FLASH_AREA_IMAGE_1, FLASH_AREA_IMAGE_0,
1095 0, 0, size);
1096
David Vincze39e78552018-10-10 17:10:01 +02001097 /*
1098 * Erases header and trailer. The trailer is erased because when a new
1099 * image is written without a trailer as is the case when using newt, the
1100 * trailer that was left might trigger a new upgrade.
1101 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001102 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
David Vincze39e78552018-10-10 17:10:01 +02001103 boot_img_sector_off(&boot_data, 1, 0),
1104 boot_img_sector_size(&boot_data, 1, 0));
Tamas Banf70ef8c2017-12-19 15:35:09 +00001105 assert(rc == 0);
David Vincze39e78552018-10-10 17:10:01 +02001106 last_sector = boot_img_num_sectors(&boot_data, 1) - 1;
1107 rc = boot_erase_sector(FLASH_AREA_IMAGE_1,
1108 boot_img_sector_off(&boot_data, 1, last_sector),
1109 boot_img_sector_size(&boot_data, 1, last_sector));
1110 assert(rc == 0);
1111
1112 /* TODO: Perhaps verify slot 0's signature again? */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001113
1114 return 0;
1115}
1116#else
1117static int
1118boot_copy_image(struct boot_status *bs)
1119{
1120 uint32_t sz;
1121 int first_sector_idx;
1122 int last_sector_idx;
David Vincze39e78552018-10-10 17:10:01 +02001123 uint32_t swap_idx;
Tamas Banf70ef8c2017-12-19 15:35:09 +00001124 struct image_header *hdr;
1125 uint32_t size;
1126 uint32_t copy_size;
1127 int rc;
1128
1129 /* FIXME: just do this if asked by user? */
1130
1131 size = copy_size = 0;
1132
David Vincze39e78552018-10-10 17:10:01 +02001133 if (bs->idx == BOOT_STATUS_IDX_0 && bs->state == BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001134 /*
1135 * No swap ever happened, so need to find the largest image which
1136 * will be used to determine the amount of sectors to swap.
1137 */
1138 hdr = boot_img_hdr(&boot_data, 0);
1139 if (hdr->ih_magic == IMAGE_MAGIC) {
1140 rc = boot_read_image_size(0, hdr, &copy_size);
1141 assert(rc == 0);
1142 }
1143
1144 hdr = boot_img_hdr(&boot_data, 1);
1145 if (hdr->ih_magic == IMAGE_MAGIC) {
1146 rc = boot_read_image_size(1, hdr, &size);
1147 assert(rc == 0);
1148 }
1149
1150 if (size > copy_size) {
1151 copy_size = size;
1152 }
1153
1154 bs->swap_size = copy_size;
1155 } else {
1156 /*
1157 * If a swap was under way, the swap_size should already be present
1158 * in the trailer...
1159 */
1160 rc = boot_read_swap_size(&bs->swap_size);
1161 assert(rc == 0);
1162
1163 copy_size = bs->swap_size;
1164 }
1165
1166 size = 0;
1167 last_sector_idx = 0;
1168 while (1) {
1169 size += boot_img_sector_size(&boot_data, 0, last_sector_idx);
1170 if (size >= copy_size) {
1171 break;
1172 }
1173 last_sector_idx++;
1174 }
1175
1176 swap_idx = 0;
1177 while (last_sector_idx >= 0) {
1178 sz = boot_copy_sz(last_sector_idx, &first_sector_idx);
David Vincze39e78552018-10-10 17:10:01 +02001179 if (swap_idx >= (bs->idx - BOOT_STATUS_IDX_0)) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001180 boot_swap_sectors(first_sector_idx, sz, bs);
1181 }
1182
1183 last_sector_idx = first_sector_idx - 1;
1184 swap_idx++;
1185 }
1186
David Vincze39e78552018-10-10 17:10:01 +02001187#ifdef MCUBOOT_VALIDATE_SLOT0
1188 if (boot_status_fails > 0) {
1189 BOOT_LOG_WRN("%d status write fails performing the swap", boot_status_fails);
1190 }
1191#endif
1192
Tamas Banf70ef8c2017-12-19 15:35:09 +00001193 return 0;
1194}
1195#endif
1196
1197/**
1198 * Marks the image in slot 0 as fully copied.
1199 */
1200#ifndef MCUBOOT_OVERWRITE_ONLY
1201static int
1202boot_set_copy_done(void)
1203{
1204 const struct flash_area *fap;
1205 int rc;
1206
1207 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1208 if (rc != 0) {
1209 return BOOT_EFLASH;
1210 }
1211
1212 rc = boot_write_copy_done(fap);
1213 flash_area_close(fap);
1214 return rc;
1215}
1216#endif /* !MCUBOOT_OVERWRITE_ONLY */
1217
1218/**
1219 * Marks a reverted image in slot 0 as confirmed. This is necessary to ensure
1220 * the status bytes from the image revert operation don't get processed on a
1221 * subsequent boot.
1222 *
1223 * NOTE: image_ok is tested before writing because if there's a valid permanent
1224 * image installed on slot0 and the new image to be upgrade to has a bad sig,
1225 * image_ok would be overwritten.
1226 */
1227#ifndef MCUBOOT_OVERWRITE_ONLY
1228static int
1229boot_set_image_ok(void)
1230{
1231 const struct flash_area *fap;
1232 struct boot_swap_state state;
1233 int rc;
1234
1235 rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
1236 if (rc != 0) {
1237 return BOOT_EFLASH;
1238 }
1239
1240 rc = boot_read_swap_state(fap, &state);
1241 if (rc != 0) {
1242 rc = BOOT_EFLASH;
1243 goto out;
1244 }
1245
1246 if (state.image_ok == BOOT_FLAG_UNSET) {
1247 rc = boot_write_image_ok(fap);
1248 }
1249
1250out:
1251 flash_area_close(fap);
1252 return rc;
1253}
1254#endif /* !MCUBOOT_OVERWRITE_ONLY */
1255
1256/**
1257 * Performs an image swap if one is required.
1258 *
1259 * @param out_swap_type On success, the type of swap performed gets
1260 * written here.
1261 *
1262 * @return 0 on success; nonzero on failure.
1263 */
1264static int
1265boot_swap_if_needed(int *out_swap_type)
1266{
1267 struct boot_status bs;
1268 int swap_type;
1269 int rc;
1270
1271 /* Determine if we rebooted in the middle of an image swap
1272 * operation.
1273 */
1274 rc = boot_read_status(&bs);
1275 assert(rc == 0);
1276 if (rc != 0) {
1277 return rc;
1278 }
1279
1280 /* If a partial swap was detected, complete it. */
David Vincze39e78552018-10-10 17:10:01 +02001281 if (bs.idx != BOOT_STATUS_IDX_0 || bs.state != BOOT_STATUS_STATE_0) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001282 rc = boot_copy_image(&bs);
1283 assert(rc == 0);
1284
1285 /* NOTE: here we have finished a swap resume. The initial request
1286 * was either a TEST or PERM swap, which now after the completed
1287 * swap will be determined to be respectively REVERT (was TEST)
1288 * or NONE (was PERM).
1289 */
1290
1291 /* Extrapolate the type of the partial swap. We need this
1292 * information to know how to mark the swap complete in flash.
1293 */
1294 swap_type = boot_previous_swap_type();
1295 } else {
1296 swap_type = boot_validated_swap_type();
1297 switch (swap_type) {
1298 case BOOT_SWAP_TYPE_TEST:
1299 case BOOT_SWAP_TYPE_PERM:
1300 case BOOT_SWAP_TYPE_REVERT:
1301 rc = boot_copy_image(&bs);
1302 assert(rc == 0);
1303 break;
1304 }
1305 }
1306
1307 *out_swap_type = swap_type;
1308 return 0;
1309}
1310
1311/**
1312 * Prepares the booting process. This function moves images around in flash as
1313 * appropriate, and tells you what address to boot from.
1314 *
1315 * @param rsp On success, indicates how booting should occur.
1316 *
1317 * @return 0 on success; nonzero on failure.
1318 */
1319int
1320boot_go(struct boot_rsp *rsp)
1321{
1322 int swap_type;
1323 size_t slot;
1324 int rc;
1325 int fa_id;
1326 bool reload_headers = false;
1327
1328 /* The array of slot sectors are defined here (as opposed to file scope) so
1329 * that they don't get allocated for non-boot-loader apps. This is
1330 * necessary because the gcc option "-fdata-sections" doesn't seem to have
1331 * any effect in older gcc versions (e.g., 4.8.4).
1332 */
1333 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1334 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
Tamas Ban581034a2017-12-19 19:54:37 +00001335
Tamas Banf70ef8c2017-12-19 15:35:09 +00001336 boot_data.imgs[0].sectors = slot0_sectors;
1337 boot_data.imgs[1].sectors = slot1_sectors;
1338
1339 /* Open boot_data image areas for the duration of this call. */
1340 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1341 fa_id = flash_area_id_from_image_slot(slot);
1342 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, slot));
1343 assert(rc == 0);
1344 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001345
Tamas Banf70ef8c2017-12-19 15:35:09 +00001346 rc = flash_area_open(FLASH_AREA_IMAGE_SCRATCH,
1347 &BOOT_SCRATCH_AREA(&boot_data));
1348 assert(rc == 0);
1349
1350 /* Determine the sector layout of the image slots and scratch area. */
1351 rc = boot_read_sectors();
1352 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001353 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1354 BOOT_MAX_IMG_SECTORS);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001355 goto out;
1356 }
1357
1358 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001359 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001360 if (rc != 0) {
1361 goto out;
1362 }
1363
1364 /* If the image slots aren't compatible, no swap is possible. Just boot
1365 * into slot 0.
1366 */
1367 if (boot_slots_compatible()) {
1368 rc = boot_swap_if_needed(&swap_type);
1369 assert(rc == 0);
1370 if (rc != 0) {
1371 goto out;
1372 }
1373
1374 /*
1375 * The following states need image_ok be explicitly set after the
1376 * swap was finished to avoid a new revert.
1377 */
Tamas Ban581034a2017-12-19 19:54:37 +00001378 if (swap_type == BOOT_SWAP_TYPE_REVERT ||
1379 swap_type == BOOT_SWAP_TYPE_FAIL) {
Tamas Banf70ef8c2017-12-19 15:35:09 +00001380#ifndef MCUBOOT_OVERWRITE_ONLY
1381 rc = boot_set_image_ok();
1382 if (rc != 0) {
1383 swap_type = BOOT_SWAP_TYPE_PANIC;
1384 }
1385#endif /* !MCUBOOT_OVERWRITE_ONLY */
1386 }
1387 } else {
1388 swap_type = BOOT_SWAP_TYPE_NONE;
1389 }
1390
1391 switch (swap_type) {
1392 case BOOT_SWAP_TYPE_NONE:
1393 slot = 0;
1394 break;
1395
1396 case BOOT_SWAP_TYPE_TEST: /* fallthrough */
1397 case BOOT_SWAP_TYPE_PERM: /* fallthrough */
1398 case BOOT_SWAP_TYPE_REVERT:
1399 slot = 1;
1400 reload_headers = true;
1401#ifndef MCUBOOT_OVERWRITE_ONLY
1402 rc = boot_set_copy_done();
1403 if (rc != 0) {
1404 swap_type = BOOT_SWAP_TYPE_PANIC;
1405 }
1406#endif /* !MCUBOOT_OVERWRITE_ONLY */
1407 break;
1408
1409 case BOOT_SWAP_TYPE_FAIL:
1410 /* The image in slot 1 was invalid and is now erased. Ensure we don't
1411 * try to boot into it again on the next reboot. Do this by pretending
1412 * we just reverted back to slot 0.
1413 */
1414 slot = 0;
1415 reload_headers = true;
1416 break;
1417
1418 default:
1419 swap_type = BOOT_SWAP_TYPE_PANIC;
1420 }
1421
1422 if (swap_type == BOOT_SWAP_TYPE_PANIC) {
1423 BOOT_LOG_ERR("panic!");
1424 assert(0);
1425
1426 /* Loop forever... */
David Vincze39e78552018-10-10 17:10:01 +02001427 while (1) {}
Tamas Banf70ef8c2017-12-19 15:35:09 +00001428 }
1429
Tamas Banf70ef8c2017-12-19 15:35:09 +00001430 if (reload_headers) {
David Vincze39e78552018-10-10 17:10:01 +02001431 rc = boot_read_image_headers(false);
Tamas Banf70ef8c2017-12-19 15:35:09 +00001432 if (rc != 0) {
1433 goto out;
1434 }
1435 /* Since headers were reloaded, it can be assumed we just performed a
1436 * swap or overwrite. Now the header info that should be used to
1437 * provide the data for the bootstrap, which previously was at Slot 1,
1438 * was updated to Slot 0.
1439 */
1440 slot = 0;
1441 }
1442
David Vincze39e78552018-10-10 17:10:01 +02001443#ifdef MCUBOOT_VALIDATE_SLOT0
Tamas Banf70ef8c2017-12-19 15:35:09 +00001444 rc = boot_validate_slot(0);
1445 assert(rc == 0);
1446 if (rc != 0) {
1447 rc = BOOT_EBADIMAGE;
1448 goto out;
1449 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001450#else /* MCUBOOT_VALIDATE_SLOT0 */
David Vincze39e78552018-10-10 17:10:01 +02001451 /* Even if we're not re-validating slot 0, we could be booting
1452 * onto an empty flash chip. At least do a basic sanity check that
1453 * the magic number on the image is OK.
1454 */
1455 if (boot_data.imgs[0].hdr.ih_magic != IMAGE_MAGIC) {
1456 BOOT_LOG_ERR("bad image magic 0x%lx", (unsigned long)boot_data.imgs[0].hdr.ih_magic);
1457 rc = BOOT_EBADIMAGE;
1458 goto out;
1459 }
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001460#endif /* MCUBOOT_VALIDATE_SLOT0 */
Tamas Banf70ef8c2017-12-19 15:35:09 +00001461
1462 /* Always boot from the primary slot. */
1463 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, 0);
1464 rsp->br_image_off = boot_img_slot_off(&boot_data, 0);
1465 rsp->br_hdr = boot_img_hdr(&boot_data, slot);
1466
Tamas Ban0e8ab302019-01-17 11:45:31 +00001467 /* Save boot status to shared memory area */
1468 rc = boot_save_boot_status(SW_S_NS,
1469 rsp->br_hdr,
1470 BOOT_IMG_AREA(&boot_data, slot));
1471 if (rc) {
1472 BOOT_LOG_ERR("Failed to add data to shared area");
1473 }
1474
Tamas Banf70ef8c2017-12-19 15:35:09 +00001475 out:
1476 flash_area_close(BOOT_SCRATCH_AREA(&boot_data));
1477 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1478 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1479 }
1480 return rc;
1481}
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001482
Oliver Swedef9982442018-08-24 18:37:44 +01001483#else /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001484
1485#define BOOT_LOG_IMAGE_INFO(area, hdr, state) \
1486 BOOT_LOG_INF("Image %"PRIu32": version=%"PRIu8".%"PRIu8".%"PRIu16"" \
1487 ".%"PRIu32", magic=%5s, image_ok=0x%x", \
1488 (area), \
1489 (hdr)->ih_ver.iv_major, \
1490 (hdr)->ih_ver.iv_minor, \
1491 (hdr)->ih_ver.iv_revision, \
1492 (hdr)->ih_ver.iv_build_num, \
1493 ((state)->magic == BOOT_MAGIC_GOOD ? "good" : \
1494 (state)->magic == BOOT_MAGIC_UNSET ? "unset" : \
1495 "bad"), \
1496 (state)->image_ok)
1497
1498struct image_slot_version {
1499 uint64_t version;
1500 uint32_t slot_number;
1501};
1502
1503/**
1504 * Extract the version number from the image header. This function must be
1505 * ported if version number format has changed in the image header.
1506 *
1507 * @param hdr Pointer to an image header structure
1508 *
Oliver Swedef9982442018-08-24 18:37:44 +01001509 * @return Version number casted to uint64_t
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001510 */
1511static uint64_t
1512boot_get_version_number(struct image_header *hdr)
1513{
Oliver Swedef9982442018-08-24 18:37:44 +01001514 uint64_t version = 0;
1515 version |= (uint64_t)hdr->ih_ver.iv_major << (IMAGE_VER_MINOR_LENGTH
1516 + IMAGE_VER_REVISION_LENGTH
1517 + IMAGE_VER_BUILD_NUM_LENGTH);
1518 version |= (uint64_t)hdr->ih_ver.iv_minor << (IMAGE_VER_REVISION_LENGTH
1519 + IMAGE_VER_BUILD_NUM_LENGTH);
1520 version |= (uint64_t)hdr->ih_ver.iv_revision << IMAGE_VER_BUILD_NUM_LENGTH;
1521 version |= hdr->ih_ver.iv_build_num;
1522 return version;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001523}
1524
1525/**
1526 * Comparator function for `qsort` to compare version numbers. This function
1527 * must be ported if version number format has changed in the image header.
1528 *
1529 * @param ver1 Pointer to an array element which holds the version number
1530 * @param ver2 Pointer to another array element which holds the version
1531 * number
1532 *
1533 * @return if version1 > version2 -1
1534 * if version1 == version2 0
1535 * if version1 < version2 1
1536 */
1537static int
1538boot_compare_version_numbers(const void *ver1, const void *ver2)
1539{
1540 if (((struct image_slot_version *)ver1)->version <
1541 ((struct image_slot_version *)ver2)->version) {
1542 return 1;
1543 }
1544
1545 if (((struct image_slot_version *)ver1)->version ==
1546 ((struct image_slot_version *)ver2)->version) {
1547 return 0;
1548 }
1549
1550 return -1;
1551}
1552
1553/**
1554 * Sort the available images based on the version number and puts them in
1555 * a list.
1556 *
1557 * @param boot_sequence A pointer to an array, whose aim is to carry
1558 * the boot order of candidate images.
1559 * @param slot_cnt The number of flash areas, which can contains firmware
1560 * images.
1561 *
1562 * @return The number of valid images.
1563 */
1564uint32_t
1565boot_get_boot_sequence(uint32_t *boot_sequence, uint32_t slot_cnt)
1566{
1567 struct boot_swap_state slot_state;
1568 struct image_header *hdr;
1569 struct image_slot_version image_versions[BOOT_NUM_SLOTS] = {{0}};
1570 uint32_t image_cnt = 0;
1571 uint32_t slot;
1572 int32_t rc;
1573 int32_t fa_id;
1574
1575 for (slot = 0; slot < slot_cnt; slot++) {
1576 hdr = boot_img_hdr(&boot_data, slot);
1577 fa_id = flash_area_id_from_image_slot(slot);
1578 rc = boot_read_swap_state_by_id(fa_id, &slot_state);
1579 if (rc != 0) {
1580 BOOT_LOG_ERR("Error during reading image trailer from slot:"
1581 " %"PRIu32"", slot);
1582 continue;
1583 }
1584
1585 if (hdr->ih_magic == IMAGE_MAGIC) {
1586 if (slot_state.magic == BOOT_MAGIC_GOOD ||
David Vincze39e78552018-10-10 17:10:01 +02001587 slot_state.image_ok == BOOT_FLAG_SET) {
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001588 /* Valid cases:
1589 * - Test mode: magic is OK in image trailer
1590 * - Permanent mode: image_ok flag has previously set
1591 */
1592 image_versions[slot].slot_number = slot;
1593 image_versions[slot].version = boot_get_version_number(hdr);
1594 image_cnt++;
1595 }
1596
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001597 BOOT_LOG_IMAGE_INFO(slot, hdr, &slot_state);
1598 } else {
Oliver Swedef9982442018-08-24 18:37:44 +01001599 BOOT_LOG_INF("Image %"PRIu32": No valid image", slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001600 }
1601 }
1602
1603 /* Sort the images based on version number */
1604 qsort(&image_versions[0],
1605 slot_cnt,
1606 sizeof(struct image_slot_version),
1607 boot_compare_version_numbers);
1608
1609 /* Copy the calculated boot sequence to boot_sequence array */
1610 for (slot = 0; slot < slot_cnt; slot++) {
1611 boot_sequence[slot] = image_versions[slot].slot_number;
1612 }
1613
1614 return image_cnt;
1615}
1616
Oliver Swedef9982442018-08-24 18:37:44 +01001617#ifdef MCUBOOT_RAM_LOADING
1618/**
1619 * Copies an image from a slot in the flash to an SRAM address, where the load
1620 * address has already been inserted into the image header by this point and is
1621 * extracted from it within this method. The copying is done sector-by-sector.
1622 *
1623 * @param slot The flash slot of the image to be copied to SRAM.
1624 *
1625 * @param hdr Pointer to the image header structure of the image
1626 * that needs to be copid to SRAM
1627 *
1628 * @return 0 on success; nonzero on failure.
1629 */
1630static int
1631boot_copy_image_to_sram(int slot, struct image_header *hdr)
1632{
1633 int rc;
1634 uint32_t sect_sz;
1635 uint32_t sect = 0;
1636 uint32_t bytes_copied = 0;
1637 const struct flash_area *fap_src = NULL;
1638 uint32_t dst = (uint32_t) hdr->ih_load_addr;
1639 uint32_t img_sz;
1640
1641 if (dst % 4 != 0) {
1642 BOOT_LOG_INF("Cannot copy the image to the SRAM address 0x%"PRIx32" "
1643 "- the load address must be aligned with 4 bytes due to SRAM "
1644 "restrictions", dst);
1645 return BOOT_EBADARGS;
1646 }
1647
1648 rc = flash_area_open(flash_area_id_from_image_slot(slot), &fap_src);
1649 if (rc != 0) {
1650 return BOOT_EFLASH;
1651 }
1652
1653 rc = boot_read_image_size(slot, hdr, &img_sz);
1654 if (rc != 0) {
1655 return BOOT_EFLASH;
1656 }
1657
1658 while (bytes_copied < img_sz) {
1659 sect_sz = boot_img_sector_size(&boot_data, slot, sect);
1660 /*
1661 * Direct copy from where the image sector resides in flash to its new
1662 * location in SRAM
1663 */
1664 rc = flash_area_read(fap_src,
1665 bytes_copied,
1666 (void *)(dst + bytes_copied),
1667 sect_sz);
1668 if (rc != 0) {
1669 BOOT_LOG_INF("Error whilst copying image from Flash to SRAM");
1670 break;
1671 } else {
1672 bytes_copied += sect_sz;
1673 }
1674 sect++;
1675 }
1676
1677 if (fap_src) {
1678 flash_area_close(fap_src);
1679 }
1680 return rc;
1681}
1682#endif /* MCUBOOT_RAM_LOADING */
1683
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001684/**
1685 * Prepares the booting process. This function choose the newer image in flash
1686 * as appropriate, and returns the address to boot from.
1687 *
1688 * @param rsp On success, indicates how booting should occur.
1689 *
1690 * @return 0 on success; nonzero on failure.
1691 */
1692int
1693boot_go(struct boot_rsp *rsp)
1694{
1695 size_t slot = 0;
1696 int32_t i;
1697 int rc;
1698 int fa_id;
1699 uint32_t boot_sequence[BOOT_NUM_SLOTS];
1700 uint32_t img_cnt;
Oliver Swedef9982442018-08-24 18:37:44 +01001701 struct image_header *newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001702
1703 static boot_sector_t slot0_sectors[BOOT_MAX_IMG_SECTORS];
1704 static boot_sector_t slot1_sectors[BOOT_MAX_IMG_SECTORS];
1705
1706 boot_data.imgs[0].sectors = &slot0_sectors[0];
1707 boot_data.imgs[1].sectors = &slot1_sectors[0];
1708
1709 /* Open boot_data image areas for the duration of this call. */
1710 for (i = 0; i < BOOT_NUM_SLOTS; i++) {
1711 fa_id = flash_area_id_from_image_slot(i);
1712 rc = flash_area_open(fa_id, &BOOT_IMG_AREA(&boot_data, i));
1713 assert(rc == 0);
1714 }
1715
1716 /* Determine the sector layout of the image slots. */
1717 rc = boot_read_sectors();
1718 if (rc != 0) {
David Vincze39e78552018-10-10 17:10:01 +02001719 BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d - too small?",
1720 BOOT_MAX_IMG_SECTORS);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001721 goto out;
1722 }
1723
1724 /* Attempt to read an image header from each slot. */
David Vincze39e78552018-10-10 17:10:01 +02001725 rc = boot_read_image_headers(false);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001726 if (rc != 0) {
1727 goto out;
1728 }
1729
1730 img_cnt = boot_get_boot_sequence(boot_sequence, BOOT_NUM_SLOTS);
1731 if (img_cnt) {
1732 /* Authenticate images */
1733 for (i = 0; i < img_cnt; i++) {
1734 rc = boot_validate_slot(boot_sequence[i]);
1735 if (rc == 0) {
1736 slot = boot_sequence[i];
1737 break;
1738 }
1739 }
1740 if (rc) {
1741 /* If there was no valid image at all */
1742 rc = BOOT_EBADIMAGE;
1743 goto out;
1744 }
1745
Oliver Swedef9982442018-08-24 18:37:44 +01001746 /* The slot variable now refers to the newest image's slot in flash */
1747 newest_image_header = boot_img_hdr(&boot_data, slot);
1748
1749 #ifdef MCUBOOT_RAM_LOADING
1750 if (newest_image_header->ih_flags & IMAGE_F_RAM_LOAD) {
1751 /* Copy image to the load address from where it
1752 * currently resides in flash */
1753 rc = boot_copy_image_to_sram(slot, newest_image_header);
1754 if (rc != 0) {
1755 rc = BOOT_EBADIMAGE;
1756 BOOT_LOG_INF("Could not copy image from slot 0x%"PRIx32" in "
1757 "the Flash to load address 0x%"PRIx32" in SRAM, "
1758 "aborting..",
1759 slot,
1760 newest_image_header->ih_load_addr);
1761 goto out;
1762 } else {
1763 BOOT_LOG_INF("Image has been copied from slot %d in flash to "
1764 "SRAM address 0x%"PRIx32"",
1765 slot,
1766 newest_image_header->ih_load_addr);
1767 }
1768
1769 /* Validate the image hash in SRAM after the copy was successful */
1770 rc = bootutil_check_hash_after_loading(newest_image_header);
1771 if (rc != 0) {
1772 rc = BOOT_EBADIMAGE;
1773 BOOT_LOG_INF("Cannot validate the hash of the image that was "
1774 "copied to SRAM, aborting..");
1775 goto out;
1776 }
1777
1778 BOOT_LOG_INF("Booting image from SRAM at address 0x%"PRIx32"",
1779 newest_image_header->ih_load_addr);
1780 } else {
1781 BOOT_LOG_INF("Booting image from slot %d", slot);
1782 }
1783 #endif /* MCUBOOT_RAM_LOADING */
1784
1785 rsp->br_hdr = newest_image_header;
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001786 rsp->br_image_off = boot_img_slot_off(&boot_data, slot);
Oliver Swedef9982442018-08-24 18:37:44 +01001787 rsp->br_flash_dev_id = boot_img_fa_device_id(&boot_data, slot);
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001788 } else {
1789 /* No candidate image available */
1790 rc = BOOT_EBADIMAGE;
1791 }
1792
Tamas Ban0e8ab302019-01-17 11:45:31 +00001793 /* Save boot status to shared memory area */
1794 rc = boot_save_boot_status(SW_S_NS,
1795 rsp->br_hdr,
1796 BOOT_IMG_AREA(&boot_data, slot));
1797 if (rc) {
1798 BOOT_LOG_ERR("Failed to add data to shared area");
1799 }
1800
Tamas Ban4fb8e9d2018-02-23 14:22:03 +00001801out:
1802 for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
1803 flash_area_close(BOOT_IMG_AREA(&boot_data, BOOT_NUM_SLOTS - 1 - slot));
1804 }
1805 return rc;
1806}
Oliver Swedef9982442018-08-24 18:37:44 +01001807#endif /* MCUBOOT_NO_SWAP || MCUBOOT_RAM_LOADING */